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