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