• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2001, 2002, 2003, 2005, 2006, 2008 Red Hat, Inc.
2    This file is part of Red Hat elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2001.
4 
5    Red Hat elfutils is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by the
7    Free Software Foundation; version 2 of the License.
8 
9    Red Hat elfutils is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with Red Hat elfutils; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17 
18    Red Hat elfutils is an included package of the Open Invention Network.
19    An included package of the Open Invention Network is a package for which
20    Open Invention Network licensees cross-license their patents.  No patent
21    license is granted, either expressly or impliedly, by designation as an
22    included package.  Should you wish to participate in the Open Invention
23    Network licensing program, please visit www.openinventionnetwork.com
24    <http://www.openinventionnetwork.com>.  */
25 
26 #ifndef LD_H
27 #define LD_H	1
28 
29 #include <dlfcn.h>
30 #include <obstack.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include "xelf.h"
34 
35 
36 /* Recommended size of the buffer passed to ld_strerror.  */
37 #define ERRBUFSIZE	(512)
38 
39 /* Character used to introduce version name after symbol.  */
40 #define VER_CHR	'@'
41 
42 
43 /* Methods for handling archives.  */
44 enum extract_rule
45   {
46     defaultextract,	/* Weak references don't cause archive member to
47 			   be used.  */
48     weakextract,	/* Weak references cause archive member to be
49 			   extracted.  */
50     allextract		/* Extract all archive members regardless of
51 			   references (aka whole-archive).  */
52   };
53 
54 
55 /* Type of output file.  */
56 enum file_type
57   {
58     no_file_type = 0,		/* None selected so far.  */
59     executable_file_type,	/* Executable.  */
60     dso_file_type,		/* DSO.  */
61     dso_needed_file_type,	/* DSO introduced by DT_NEEDED.  */
62     relocatable_file_type,	/* Relocatable object file.  */
63     archive_file_type		/* Archive (input only).  */
64   };
65 
66 
67 struct usedfiles
68 {
69   /* The next file given at the command line.  */
70   struct usedfiles *next;
71   /* Nonzero if this file is the beginning of a group.  */
72   bool group_start;
73   /* Nonzero if this file is the end of a group.  */
74   bool group_end;
75   /* Pointer to the beginning of the group.  It is necessary to
76      explain why we cannot simply use the 'next' pointer and have a
77      circular single-linked list like in many cases.  The problem is
78      that the last archive of the group, if it is the last file of the
79      group, contains the only existing pointer to the next file we
80      have to look at.  All files are initially connected via the
81      'next' pointer in a single-linked list.  Therefore we cannot
82      overwrite this value.  It instead will be used once the group is
83      handled and we go on processing the rest of the files.  */
84   struct usedfiles *group_backref;
85 
86   /* Name/path of the file.  */
87   const char *fname;
88   /* Resolved file name.  */
89   const char *rfname;
90   /* Name used as reference in DT_NEEDED entries.  This is normally
91      the SONAME.  If it is missing it's normally the fname above.  */
92   const char *soname;
93   /* Handle for the SONAME in the string table.  */
94   struct Ebl_Strent *sonameent;
95 
96   /* Help to identify duplicates.  */
97   dev_t dev;
98   ino_t ino;
99 
100   enum
101     {
102       not_opened,
103       opened,
104       in_archive,
105       closed
106     } status;
107 
108   /* How to extract elements from archives.  */
109   enum extract_rule extract_rule;
110 
111   /* Lazy-loading rule.  */
112   bool lazyload;
113 
114   /* If this is a DSO the flag indicates whether the file is directly
115      used in a reference.  */
116   bool used;
117 
118   /* True when file should be added to DT_NEEDED list only when
119      directly referenced.  */
120   bool as_needed;
121 
122   /* If nonzero this is the archive sequence number which can be used to
123      determine whether back refernces from -( -) or GROUP statements
124      have to be followed.  */
125   int archive_seq;
126 
127   /* Pointer to the record for the archive containing this file.  */
128   struct usedfiles *archive_file;
129 
130   /* Type of file.  We have to distinguish these types since they
131      are searched for differently.  */
132   enum file_type file_type;
133   /* This is the ELF library handle for this file.  */
134   Elf *elf;
135 
136   /* The ELF header.  */
137 #if NATIVE_ELF != 0
138   XElf_Ehdr *ehdr;
139 # define FILEINFO_EHDR(fi) (*(fi))
140 #else
141   XElf_Ehdr ehdr;
142 # define FILEINFO_EHDR(fi) (fi)
143 #endif
144 
145   /* Index of the section header string table section.  We use a
146      separate field and not the e_shstrndx field in the ELF header
147      since in case of a file with more than 64000 sections the index
148      might be stored in the section header of section zero.  The
149      elf_getshstrndx() function can find the value but it is too
150      costly to repeat this call over and over.  */
151   size_t shstrndx;
152 
153   /* Info about the sections of the file.  */
154   struct scninfo
155   {
156     /* Handle for the section.  Note that we can store a section
157        handle here because the file is not changing.  This together
158        with the knowledge about the libelf library is enough for us to
159        assume the section reference remains valid at all times.  */
160     Elf_Scn *scn;
161     /* Section header.  */
162 #if NATIVE_ELF != 0
163     XElf_Shdr *shdr;
164 # define SCNINFO_SHDR(si) (*(si))
165 #else
166     XElf_Shdr shdr;
167 # define SCNINFO_SHDR(si) (si)
168 #endif
169     /* Offset of this files section in the combined section.  */
170     XElf_Off offset;
171     /* Index of the section in the output file.  */
172     Elf32_Word outscnndx;
173     /* Index of the output section in the 'allsection' array.  */
174     Elf32_Word allsectionsidx;
175     /* True if the section is used.  */
176     bool used;
177     /* True if section is an unused COMDAT section.  */
178     bool unused_comdat;
179     /* True if this is a COMDAT group section.  */
180     bool comdat_group;
181     /* Section group number.  This is the index of the SHT_GROUP section.  */
182     Elf32_Word grpid;
183     /* Pointer back to the containing file information structure.  */
184     struct usedfiles *fileinfo;
185     /* List of symbols in this section (set only for merge-able sections
186        and group sections).  */
187     struct symbol *symbols;
188     /* Size of relocations in this section.  Only used for relocation
189        sections.  */
190     size_t relsize;
191     /* Pointer to next section which is put in the given output
192        section.  */
193     struct scninfo *next;
194   } *scninfo;
195 
196   /* List of section group sections.  */
197   struct scninfo *groups;
198 
199   /* The symbol table section.
200 
201      XXX Maybe support for more than one symbol table is needed.  */
202   Elf_Data *symtabdata;
203   /* Extra section index table section.  */
204   Elf_Data *xndxdata;
205   /* Dynamic symbol table section.  */
206   Elf_Data *dynsymtabdata;
207   /* The version number section.  */
208   Elf_Data *versymdata;
209   /* The defined versions.  */
210   Elf_Data *verdefdata;
211   /* Number of versions defined.  */
212   size_t nverdef;
213   /* True if the version with the given index number is used in the
214      output.  */
215   XElf_Versym *verdefused;
216   /* How many versions are used.  */
217   size_t nverdefused;
218   /* Handle for name of the version.  */
219   struct Ebl_Strent **verdefent;
220   /* The needed versions.  */
221   Elf_Data *verneeddata;
222   /* String table section associated with the symbol table.  */
223   Elf32_Word symstridx;
224   /* String table section associated with the dynamic symbol table.  */
225   Elf32_Word dynsymstridx;
226   /* Number of entries in the symbol table.  */
227   size_t nsymtab;
228   size_t nlocalsymbols;
229   size_t ndynsymtab;
230   /* Dynamic section.  */
231   Elf_Scn *dynscn;
232 
233   /* Indirection table for the symbols defined here.  */
234   Elf32_Word *symindirect;
235   Elf32_Word *dynsymindirect;
236   /* For undefined or common symbols we need a reference to the symbol
237      record.  */
238   struct symbol **symref;
239   struct symbol **dynsymref;
240 
241   /* This is the file descriptor.  The value is -1 if the descriptor
242      was already closed.  This can happen if we needed file descriptors
243      to open new files.  */
244   int fd;
245   /* This flag is true if the descriptor was passed to the generic
246      functions from somewhere else.  This is an implementation detail;
247      no machine-specific code must use this flag.  */
248   bool fd_passed;
249 
250   /* True if any of the sections is merge-able.  */
251   bool has_merge_sections;
252 };
253 
254 
255 /* Functions to test for the various types of files we handle.  */
256 static inline int
ld_file_rel_p(struct usedfiles * file)257 ld_file_rel_p (struct usedfiles *file)
258 {
259   return (elf_kind (file->elf) == ELF_K_ELF
260 	  && FILEINFO_EHDR (file->ehdr).e_type == ET_REL);
261 }
262 
263 static inline int
ld_file_dso_p(struct usedfiles * file)264 ld_file_dso_p (struct usedfiles *file)
265 {
266   return (elf_kind (file->elf) == ELF_K_ELF
267 	  && FILEINFO_EHDR (file->ehdr).e_type == ET_DYN);
268 }
269 
270 static inline int
ld_file_ar_p(struct usedfiles * file)271 ld_file_ar_p (struct usedfiles *file)
272 {
273   return elf_kind (file->elf) == ELF_K_AR;
274 }
275 
276 
277 struct pathelement
278 {
279   /* The next path to search.  */
280   struct pathelement *next;
281   /* The path name.  */
282   const char *pname;
283   /* Larger than zero if the directory exists, smaller than zero if not,
284      zero if it is not yet known.  */
285   int exist;
286 };
287 
288 
289 /* Forward declaration.  */
290 struct ld_state;
291 
292 
293 /* Callback functions.  */
294 struct callbacks
295 {
296   /* Library names passed to the linker as -lXXX represent files named
297      libXXX.YY.  The YY part can have different forms, depending on the
298      architecture.  The generic set is .so and .a (in this order).  */
299   const char **(*lib_extensions) (struct ld_state *)
300        __attribute__ ((__const__));
301 #define LIB_EXTENSION(state) \
302   DL_CALL_FCT ((state)->callbacks.lib_extensions, (state))
303 
304   /* Process the given file.  If the file is not yet open, open it.
305      The first parameter is a file descriptor for the file which can
306      be -1 to indicate the file has not yet been found.  The second
307      parameter describes the file to be opened, the last one is the
308      state of the linker which among other information contain the
309      paths we look at.*/
310   int (*file_process) (int fd, struct usedfiles *, struct ld_state *,
311 		       struct usedfiles **);
312 #define FILE_PROCESS(fd, file, state, nextp) \
313   DL_CALL_FCT ((state)->callbacks.file_process, (fd, file, state, nextp))
314 
315   /* Close the given file.  */
316   int (*file_close) (struct usedfiles *, struct ld_state *);
317 #define FILE_CLOSE(file, state) \
318   DL_CALL_FCT ((state)->callbacks.file_close, (file, state))
319 
320   /* Create the output sections now.  This requires knowledge about
321      all the sections we will need.  It may be necessary to sort the
322      sections in the order they are supposed to appear in the
323      executable.  The sorting use many different kinds of information
324      to optimize the resulting binary.  Important is to respect
325      segment boundaries and the needed alignment.  The mode of the
326      segments will be determined afterwards automatically by the
327      output routines.  */
328   void (*create_sections) (struct ld_state *);
329 #define CREATE_SECTIONS(state) \
330   DL_CALL_FCT ((state)->callbacks.create_sections, (state))
331 
332   /* Determine whether we have any non-weak unresolved references left.  */
333   int (*flag_unresolved) (struct ld_state *);
334 #define FLAG_UNRESOLVED(state) \
335   DL_CALL_FCT ((state)->callbacks.flag_unresolved, (state))
336 
337   /* Create the sections which are generated by the linker and are not
338      present in the input file.  */
339   void (*generate_sections) (struct ld_state *);
340 #define GENERATE_SECTIONS(state) \
341   DL_CALL_FCT ((state)->callbacks.generate_sections, (state))
342 
343   /* Open the output file.  The file name is given or "a.out".  We
344      create as much of the ELF structure as possible.  */
345   int (*open_outfile) (struct ld_state *, int, int, int);
346 #define OPEN_OUTFILE(state, machine, class, data) \
347   DL_CALL_FCT ((state)->callbacks.open_outfile, (state, machine, class, data))
348 
349   /* Create the data for the output file.  */
350   int (*create_outfile) (struct ld_state *);
351 #define CREATE_OUTFILE(state) \
352   DL_CALL_FCT ((state)->callbacks.create_outfile, (state))
353 
354   /* Process a relocation section.  */
355   void (*relocate_section) (struct ld_state *, Elf_Scn *, struct scninfo *,
356 			    const Elf32_Word *);
357 #define RELOCATE_SECTION(state, outscn, first, dblindirect) \
358   DL_CALL_FCT ((state)->callbacks.relocate_section, (state, outscn, first,    \
359 						     dblindirect))
360 
361   /* Allocate a data buffer for the relocations of the given output
362      section.  */
363   void (*count_relocations) (struct ld_state *, struct scninfo *);
364 #define COUNT_RELOCATIONS(state, scninfo) \
365   DL_CALL_FCT ((state)->callbacks.count_relocations, (state, scninfo))
366 
367   /* Create relocations for executable or DSO.  */
368   void (*create_relocations) (struct ld_state *, const Elf32_Word *);
369 #define CREATE_RELOCATIONS(state, dlbindirect) \
370   DL_CALL_FCT ((state)->callbacks.create_relocations, (state, dblindirect))
371 
372   /* Finalize the output file.  */
373   int (*finalize) (struct ld_state *);
374 #define FINALIZE(state) \
375   DL_CALL_FCT ((state)->callbacks.finalize, (state))
376 
377   /* Check whether special section number is known.  */
378   bool (*special_section_number_p) (struct ld_state *, size_t);
379 #define SPECIAL_SECTION_NUMBER_P(state, number) \
380   DL_CALL_FCT ((state)->callbacks.special_section_number_p, (state, number))
381 
382   /* Check whether section type is known.  */
383   bool (*section_type_p) (struct ld_state *, XElf_Word);
384 #define SECTION_TYPE_P(state, type) \
385   DL_CALL_FCT ((state)->callbacks.section_type_p, (state, type))
386 
387   /* Return section flags for .dynamic section.  */
388   XElf_Xword (*dynamic_section_flags) (struct ld_state *);
389 #define DYNAMIC_SECTION_FLAGS(state) \
390   DL_CALL_FCT ((state)->callbacks.dynamic_section_flags, (state))
391 
392   /* Create the data structures for the .plt section and initialize it.  */
393   void (*initialize_plt) (struct ld_state *, Elf_Scn *scn);
394 #define INITIALIZE_PLT(state, scn) \
395   DL_CALL_FCT ((state)->callbacks.initialize_plt, (state, scn))
396 
397   /* Create the data structures for the .rel.plt section and initialize it.  */
398   void (*initialize_pltrel) (struct ld_state *, Elf_Scn *scn);
399 #define INITIALIZE_PLTREL(state, scn) \
400   DL_CALL_FCT ((state)->callbacks.initialize_pltrel, (state, scn))
401 
402   /* Finalize the .plt section the what belongs to them.  */
403   void (*finalize_plt) (struct ld_state *, size_t, size_t, struct symbol **);
404 #define FINALIZE_PLT(state, nsym, nsym_dyn, ndxtosym) \
405   DL_CALL_FCT ((state)->callbacks.finalize_plt, (state, nsym, nsym_dyn, \
406 						 ndxtosym))
407 
408   /* Create the data structures for the .got section and initialize it.  */
409   void (*initialize_got) (struct ld_state *, Elf_Scn *scn);
410 #define INITIALIZE_GOT(state, scn) \
411   DL_CALL_FCT ((state)->callbacks.initialize_got, (state, scn))
412 
413   /* Create the data structures for the .got.plt section and initialize it.  */
414   void (*initialize_gotplt) (struct ld_state *, Elf_Scn *scn);
415 #define INITIALIZE_GOTPLT(state, scn) \
416   DL_CALL_FCT ((state)->callbacks.initialize_gotplt, (state, scn))
417 
418   /* Return the tag corresponding to the native relocation type for
419      the platform.  */
420   int (*rel_type) (struct ld_state *);
421 #define REL_TYPE(state) \
422   DL_CALL_FCT ((state)->callbacks.rel_type, (state))
423 };
424 
425 
426 /* Structure for symbol representation.  This data structure is used a
427    lot, so size is important.  */
428 struct symbol
429 {
430   /* Symbol name.  */
431   const char *name;
432   /* Size of the object.  */
433   XElf_Xword size;
434   /* Index of the symbol in the symbol table of the object.  */
435   size_t symidx;
436   /* Index of the symbol in the symbol table of the output file.  */
437   size_t outsymidx;
438 
439   /* Description where the symbol is found/needed.  */
440   size_t scndx;
441   struct usedfiles *file;
442   /* Index of the symbol table.  */
443   Elf32_Word symscndx;
444 
445   /* Index of the symbol in the dynamic symbol table of the output
446      file.  Note that the value only needs to be 16 bit wide since
447      there cannot be more sections in an executable or DSO.  */
448   unsigned int outdynsymidx:16;
449 
450   /* Type of the symbol.  */
451   unsigned int type:4;
452   /* Various flags.  */
453   unsigned int defined:1;
454   unsigned int common:1;
455   unsigned int weak:1;
456   unsigned int added:1;
457   unsigned int merged:1;
458   unsigned int local:1;
459   unsigned int hidden:1;
460   /* Nonzero if the symbol is on the from_dso list.  */
461   unsigned int on_dsolist:1;
462   /* Nonzero if symbol needs copy relocation, reset when the
463      relocation has been created.  */
464   unsigned int need_copy:1;
465   unsigned int in_dso:1;
466 
467   union
468   {
469     /* Pointer to the handle created by the functions which create
470        merged section contents.  We use 'void *' because there are
471        different implementations used.  */
472     void *handle;
473     XElf_Addr value;
474   } merge;
475 
476   /* Pointer to next/previous symbol on whatever list the symbol is.  */
477   struct symbol *next;
478   struct symbol *previous;
479   /* Pointer to next symbol of the same section (only set for merge-able
480      sections).  */
481   struct symbol *next_in_scn;
482 };
483 
484 
485 /* Get the definition for the symbol table.  */
486 #include <symbolhash.h>
487 
488 /* Simple single linked list of file names.  */
489 struct filename_list
490 {
491   const char *name;
492   struct usedfiles *real;
493   struct filename_list *next;
494   bool group_start;
495   bool group_end;
496   bool as_needed;
497 };
498 
499 
500 /* Data structure to describe expression in linker script.  */
501 struct expression
502 {
503   enum expression_tag
504     {
505       exp_num,
506       exp_sizeof_headers,
507       exp_pagesize,
508       exp_id,
509       exp_mult,
510       exp_div,
511       exp_mod,
512       exp_plus,
513       exp_minus,
514       exp_and,
515       exp_or,
516       exp_align
517     } tag;
518 
519   union
520   {
521     uintmax_t num;
522     struct expression *child;
523     struct
524     {
525       struct expression *left;
526       struct expression *right;
527     } binary;
528     const char *str;
529   } val;
530 };
531 
532 
533 /* Data structure for section name with flags.  */
534 struct input_section_name
535 {
536   const char *name;
537   bool sort_flag;
538 };
539 
540 /* File name mask with section name.  */
541 struct filemask_section_name
542 {
543   const char *filemask;
544   const char *excludemask;
545   struct input_section_name *section_name;
546   bool keep_flag;
547 };
548 
549 /* Data structure for assignments.  */
550 struct assignment
551 {
552   const char *variable;
553   struct expression *expression;
554   struct symbol *sym;
555   bool provide_flag;
556 };
557 
558 
559 /* Data structure describing input for an output section.  */
560 struct input_rule
561 {
562   enum
563     {
564       input_section,
565       input_assignment
566     } tag;
567 
568   union
569   {
570     struct assignment *assignment;
571     struct filemask_section_name *section;
572   } val;
573 
574   struct input_rule *next;
575 };
576 
577 
578 /* Data structure to describe output section.  */
579 struct output_section
580 {
581   const char *name;
582   struct input_rule *input;
583   XElf_Addr max_alignment;
584   bool ignored;
585 };
586 
587 
588 /* Data structure to describe output file format.  */
589 struct output_rule
590 {
591   enum
592     {
593       output_section,
594       output_assignment
595     } tag;
596 
597   union
598   {
599     struct assignment *assignment;
600     struct output_section section;
601   } val;
602 
603   struct output_rule *next;
604 };
605 
606 
607 /* List of all the segments the linker script describes.  */
608 struct output_segment
609 {
610   int mode;
611   struct output_rule *output_rules;
612   struct output_segment *next;
613 
614   XElf_Off offset;
615   XElf_Addr addr;
616   XElf_Xword align;
617 };
618 
619 
620 /* List of identifiers.  */
621 struct id_list
622 {
623   union
624   {
625     enum id_type
626       {
627 	id_str,		/* Normal string.  */
628 	id_all,		/* "*", matches all.  */
629 	id_wild		/* Globbing wildcard string.  */
630       } id_type;
631     struct
632     {
633       bool local;
634       const char *versionname;
635     } s;
636   } u;
637   const char *id;
638   struct id_list *next;
639 };
640 
641 
642 /* Version information.  */
643 struct version
644 {
645   struct version *next;
646   struct id_list *local_names;
647   struct id_list *global_names;
648   const char *versionname;
649   const char *parentname;
650 };
651 
652 
653 /* Head for list of sections.  */
654 struct scnhead
655 {
656   /* Name of the sections.  */
657   const char *name;
658 
659   /* Accumulated flags for the sections.  */
660   XElf_Xword flags;
661 
662   /* Type of the sections.  */
663   XElf_Word type;
664 
665   /* Entry size.  If there are differencs between the sections with
666      the same name this field contains 1.  */
667   XElf_Word entsize;
668 
669   /* If non-NULL pointer to group signature.  */
670   const char *grp_signature;
671 
672   /* Maximum alignment for all sections.  */
673   XElf_Word align;
674 
675   /* Distinguish between normal sections coming from the input file
676      and sections generated by the linker.  */
677   enum scn_kind
678     {
679       scn_normal,		/* Section from the input file(s).  */
680       scn_dot_interp,		/* Generated .interp section.  */
681       scn_dot_got,		/* Generated .got section.  */
682       scn_dot_gotplt,		/* Generated .got.plt section.  */
683       scn_dot_dynrel,		/* Generated .rel.dyn section.  */
684       scn_dot_dynamic,		/* Generated .dynamic section.  */
685       scn_dot_dynsym,		/* Generated .dynsym section.  */
686       scn_dot_dynstr,		/* Generated .dynstr section.  */
687       scn_dot_hash,		/* Generated .hash section.  */
688       scn_dot_gnu_hash,		/* Generated .gnu.hash section.  */
689       scn_dot_plt,		/* Generated .plt section.  */
690       scn_dot_pltrel,		/* Generated .rel.plt section.  */
691       scn_dot_version,		/* Generated .gnu.version section.  */
692       scn_dot_version_r,	/* Generated .gnu.version_r section.  */
693       scn_dot_note_gnu_build_id	/* Generated .note.gnu.build-id section.  */
694     } kind;
695 
696   /* True is the section is used in the output.  */
697   bool used;
698 
699   /* Total size (only determined this way for relocation sections).  */
700   size_t relsize;
701 
702   /* Filled in by the section sorting to indicate which segment the
703      section goes in.  */
704   int segment_nr;
705 
706   /* Index of the output section.  We cannot store the section handle
707      directly here since the handle is a pointer in a dynamically
708      allocated table which might move if it becomes too small for all
709      the sections.  Using the index the correct value can be found at
710      all times.  */
711   XElf_Word scnidx;
712 
713   /* Index of the STT_SECTION entry for this section in the symbol
714      table.  */
715   XElf_Word scnsymidx;
716 
717   /* Address of the section in the output file.  */
718   XElf_Addr addr;
719 
720   /* Handle for the section name in the output file's section header
721      string table.  */
722   struct Ebl_Strent *nameent;
723 
724   /* Tail of list of symbols for this section.  Only set if the
725      section is merge-able.  */
726   struct symbol *symbols;
727 
728   /* Pointer to last section.  */
729   struct scninfo *last;
730 };
731 
732 
733 /* Define hash table for sections.  */
734 #include <sectionhash.h>
735 
736 /* Define hash table for version symbols.  */
737 #include <versionhash.h>
738 
739 
740 /* State of the linker.  */
741 struct ld_state
742 {
743   /* ELF backend library handle.  */
744   Ebl *ebl;
745 
746   /* List of all archives participating, in this order.  */
747   struct usedfiles *archives;
748   /* End of the list.  */
749   struct usedfiles *tailarchives;
750   /* If nonzero we are looking for the beginning of a group.  */
751   bool group_start_requested;
752   /* Pointer to the archive starting the group.  */
753   struct usedfiles *group_start_archive;
754 
755   /* List of the DSOs we found.  */
756   struct usedfiles *dsofiles;
757   /* Number of DSO files.  */
758   size_t ndsofiles;
759   /* Ultimate list of object files which are linked in.  */
760   struct usedfiles *relfiles;
761 
762   /* List the DT_NEEDED DSOs.  */
763   struct usedfiles *needed;
764 
765   /* Temporary storage for the parser.  */
766   struct filename_list *srcfiles;
767 
768   /* List of all the paths to look at.  */
769   struct pathelement *paths;
770   /* Tail of the list.  */
771   struct pathelement *tailpaths;
772 
773   /* User provided paths for lookup of DSOs.  */
774   struct pathelement *rpath;
775   struct pathelement *rpath_link;
776   struct pathelement *runpath;
777   struct pathelement *runpath_link;
778   struct Ebl_Strent *rxxpath_strent;
779   int rxxpath_tag;
780 
781   /* From the environment variable LD_LIBRARY_PATH.  */
782   struct pathelement *ld_library_path1;
783   struct pathelement *ld_library_path2;
784 
785   /* Name of the output file.  */
786   const char *outfname;
787   /* Name of the temporary file we initially create.  */
788   const char *tempfname;
789   /* File descriptor opened for the output file.  */
790   int outfd;
791   /* The ELF descriptor for the output file.  */
792   Elf *outelf;
793 
794   /* Type of output file.  */
795   enum file_type file_type;
796 
797   /* Is this a system library or not.  */
798   bool is_system_library;
799 
800   /* Page size to be assumed for the binary.  */
801   size_t pagesize;
802 
803   /* Name of the interpreter for dynamically linked objects.  */
804   const char *interp;
805   /* Index of the .interp section.  */
806   Elf32_Word interpscnidx;
807 
808   /* Optimization level.  */
809   unsigned long int optlevel;
810 
811   /* If true static linking is requested.  */
812   bool statically;
813 
814   /* If true, add DT_NEEDED entries for following files if they are
815      needed.  */
816   bool as_needed;
817 
818   /* How to extract elements from archives.  */
819   enum extract_rule extract_rule;
820 
821   /* Sequence number of the last archive we used.  */
822   int last_archive_used;
823 
824   /* If true print to stdout information about the files we are
825      trying to open.  */
826   bool trace_files;
827 
828   /* If true multiple definitions are not considered an error; the
829      first is used.  */
830   bool muldefs;
831 
832   /* If true undefined symbols when building DSOs are not fatal.  */
833   bool nodefs;
834 
835   /* If true add line indentifying link-editor to .comment section.  */
836   bool add_ld_comment;
837 
838   /* Stripping while linking.  */
839   enum
840     {
841       strip_none,
842       strip_debug,
843       strip_all,
844       strip_everything
845     } strip;
846 
847   /* The callback function vector.  */
848   struct callbacks callbacks;
849 
850   /* Name of the entry symbol.  Can also be a numeric value.  */
851   const char *entry;
852 
853   /* The description of the segments in the output file.  */
854   struct output_segment *output_segments;
855 
856   /* List of the symbols we created from linker script definitions.  */
857   struct symbol *lscript_syms;
858   size_t nlscript_syms;
859 
860   /* Table with known symbols.  */
861   ld_symbol_tab symbol_tab;
862 
863   /* Table with used sections.  */
864   ld_section_tab section_tab;
865 
866   /* The list of sections once we collected them.   */
867   struct scnhead **allsections;
868   size_t nallsections;
869   size_t nusedsections;
870   size_t nnotesections;
871 
872   /* Beginning of the list of symbols which are still unresolved.  */
873   struct symbol *unresolved;
874   /* Number of truely unresolved entries in the list.  */
875   size_t nunresolved;
876   /* Number of truely unresolved, non-weak entries in the list.  */
877   size_t nunresolved_nonweak;
878 
879   /* List of common symbols.  */
880   struct symbol *common_syms;
881   /* Section for the common symbols.  */
882   struct scninfo *common_section;
883 
884   /* List of symbols defined in DSOs and used in a relocatable file.
885      DSO symbols not referenced in the relocatable files are not on
886      the list.  If a symbol is on the list the on_dsolist field in the
887      'struct symbol' is nonzero.  */
888   struct symbol *from_dso;
889   /* Number of entries in from_dso.  */
890   size_t nfrom_dso;
891   /* Number of entries in the dynamic symbol table.  */
892   size_t ndynsym;
893   /* Number of PLT entries from DSO references.  */
894   size_t nplt;
895   /* Number of PLT entries from DSO references.  */
896   size_t ngot;
897   /* Number of copy relocations.  */
898   size_t ncopy;
899   /* Section for copy relocations.  */
900   struct scninfo *copy_section;
901 
902   /* Keeping track of the number of symbols in the output file.  */
903   size_t nsymtab;
904   size_t nlocalsymbols;
905 
906   /* Special symbols.  */
907   struct symbol *init_symbol;
908   struct symbol *fini_symbol;
909 
910   /* The description of the segments in the output file as described
911      in the default linker script.  This information will be used in
912      addition to the user-provided information.  */
913   struct output_segment *default_output_segments;
914   /* Search paths added by the default linker script.  */
915   struct pathelement *default_paths;
916 
917 #ifndef BASE_ELF_NAME
918   /* The handle of the ld backend library.  */
919   void *ldlib;
920 #endif
921 
922   /* String table for the section headers.  */
923   struct Ebl_Strtab *shstrtab;
924 
925   /* True if output file should contain symbol table.  */
926   bool need_symtab;
927   /* Symbol table section.  */
928   Elf32_Word symscnidx;
929   /* Extended section table section.  */
930   Elf32_Word xndxscnidx;
931   /* Symbol string table section.  */
932   Elf32_Word strscnidx;
933 
934   /* True if output file should contain dynamic symbol table.  */
935   bool need_dynsym;
936   /* Dynamic symbol table section.  */
937   Elf32_Word dynsymscnidx;
938   /* Dynamic symbol string table section.  */
939   Elf32_Word dynstrscnidx;
940   /* Dynamic symbol hash tables.  */
941   size_t hashscnidx;
942   size_t gnuhashscnidx;
943 
944   /* Procedure linkage table section.  */
945   Elf32_Word pltscnidx;
946   /* Number of entries already in the PLT section.  */
947   size_t nplt_used;
948   /* Relocation for procedure linkage table section.  */
949   Elf32_Word pltrelscnidx;
950 
951   /* Global offset table section.  */
952   Elf32_Word gotscnidx;
953   /* And the part of the PLT.  */
954   Elf32_Word gotpltscnidx;
955 
956   /* This section will hole all non-PLT relocations.  */
957   Elf32_Word reldynscnidx;
958 
959   /* Index of the sections to handle versioning.  */
960   Elf32_Word versymscnidx;
961   Elf32_Word verneedscnidx;
962   /* XXX Should the following names be verneed...?  */
963   /* Number of version definitions in input DSOs used.  */
964   int nverdefused;
965   /* Number of input DSOs using versioning.  */
966   int nverdeffile;
967   /* Index of next version.  */
968   int nextveridx;
969 
970   /* TLS segment.  */
971   bool need_tls;
972   XElf_Addr tls_start;
973   XElf_Addr tls_tcb;
974 
975   /* Hash table for version symbol strings.  Only strings without
976      special characters are hashed here.  */
977   ld_version_str_tab version_str_tab;
978   /* At most one of the following two variables is set to true if either
979      global or local symbol binding is selected as the default.  */
980   bool default_bind_local;
981   bool default_bind_global;
982 
983   /* Execuatable stack selection.  */
984   enum execstack
985     {
986       execstack_false = 0,
987       execstack_true,
988       execstack_false_force
989     } execstack;
990 
991   /* True if only used sections are used.  */
992   bool gc_sections;
993 
994   /* Array to determine final index of symbol.  */
995   Elf32_Word *dblindirect;
996 
997   /* Section group handling.  */
998   struct scngroup
999   {
1000     Elf32_Word outscnidx;
1001     int nscns;
1002     struct member
1003     {
1004       struct scnhead *scn;
1005       struct member *next;
1006     } *member;
1007     struct Ebl_Strent *nameent;
1008     struct symbol *symbol;
1009     struct scngroup *next;
1010   } *groups;
1011 
1012   /* True if the output file needs a .got section.  */
1013   bool need_got;
1014   /* Number of relocations for GOT section caused.  */
1015   size_t nrel_got;
1016 
1017   /* Number of entries needed in the .dynamic section.  */
1018   int ndynamic;
1019   /* To keep track of added entries.  */
1020   int ndynamic_filled;
1021   /* Index for the dynamic section.  */
1022   Elf32_Word dynamicscnidx;
1023 
1024   /* Flags set in the DT_FLAGS word.  */
1025   Elf32_Word dt_flags;
1026   /* Flags set in the DT_FLAGS_1 word.  */
1027   Elf32_Word dt_flags_1;
1028   /* Flags set in the DT_FEATURE_1 word.  */
1029   Elf32_Word dt_feature_1;
1030 
1031   /* Lazy-loading state for dependencies.  */
1032   bool lazyload;
1033 
1034   /* True if an .eh_frame_hdr section should be generated.  */
1035   bool eh_frame_hdr;
1036 
1037   /* What hash style to generate.  */
1038   enum
1039     {
1040       hash_style_none = 0,
1041       hash_style_sysv = 1,
1042 #define GENERATE_SYSV_HASH ((ld_state.hash_style & hash_style_sysv) != 0)
1043       hash_style_gnu = 2
1044 #define GENERATE_GNU_HASH ((ld_state.hash_style & hash_style_gnu) != 0)
1045     }
1046   hash_style;
1047 
1048 
1049   /* True if in executables all global symbols should be exported in
1050      the dynamic symbol table.  */
1051   bool export_all_dynamic;
1052 
1053   /* Build-ID style.  NULL is none.  */
1054   const char *build_id;
1055   Elf32_Word buildidscnidx;
1056 
1057   /* If DSO is generated, this is the SONAME.  */
1058   const char *soname;
1059 
1060   /* List of all relocation sections.  */
1061   struct scninfo *rellist;
1062   /* Total size of non-PLT relocations.  */
1063   size_t relsize_total;
1064 
1065   /* Record for the GOT symbol, if known.  */
1066   struct symbol *got_symbol;
1067   /* Record for the dynamic section symbol, if known.  */
1068   struct symbol *dyn_symbol;
1069 
1070   /* Obstack used for small objects which will not be deleted.  */
1071   struct obstack smem;
1072 };
1073 
1074 
1075 /* The interface to the scanner.  */
1076 
1077 /* Parser entry point.  */
1078 extern int ldparse (void);
1079 
1080 /* The input file.  */
1081 extern FILE *ldin;
1082 
1083 /* Name of the input file.  */
1084 extern const char *ldin_fname;
1085 
1086 /* Current line number.  Must be reset for a new file.  */
1087 extern int ldlineno;
1088 
1089 /* If nonzero we are currently parsing a version script.  */
1090 extern int ld_scan_version_script;
1091 
1092 /* Flags defined in ld.c.  */
1093 extern int verbose;
1094 extern int conserve_memory;
1095 
1096 
1097 /* Linker state.  This contains all global information.  */
1098 extern struct ld_state ld_state;
1099 
1100 
1101 /* Generic ld helper functions.  */
1102 
1103 /* Append a new directory to search libraries in.  */
1104 extern void ld_new_searchdir (const char *dir);
1105 
1106 /* Append a new file to the list of input files.  */
1107 extern struct usedfiles *ld_new_inputfile (const char *fname,
1108 					   enum file_type type);
1109 
1110 
1111 /* These are the generic implementations for the callbacks used by ld.  */
1112 
1113 /* Initialize state object.  This callback function is called after the
1114    parameters are parsed but before any file is searched for.  */
1115 extern int ld_prepare_state (const char *emulation);
1116 
1117 
1118 /* Function to determine whether an object will be dynamically linked.  */
1119 extern bool dynamically_linked_p (void);
1120 
1121 /* Helper functions for the architecture specific code.  */
1122 
1123 /* Checked whether the symbol is undefined and referenced from a DSO.  */
1124 extern bool linked_from_dso_p (struct scninfo *scninfo, size_t symidx);
1125 #ifdef __GNUC_STDC_INLINE__
1126 __attribute__ ((__gnu_inline__))
1127 #endif
1128 extern inline bool
linked_from_dso_p(struct scninfo * scninfo,size_t symidx)1129 linked_from_dso_p (struct scninfo *scninfo, size_t symidx)
1130 {
1131   struct usedfiles *file = scninfo->fileinfo;
1132 
1133   /* If this symbol is not undefined in this file it cannot come from
1134      a DSO.  */
1135   if (symidx < file->nlocalsymbols)
1136     return false;
1137 
1138   struct symbol *sym = file->symref[symidx];
1139 
1140   return sym->defined && sym->in_dso;
1141 }
1142 
1143 #endif	/* ld.h */
1144