* peicode.h (coff_swap_filehdr_in): can't use e_magic because we
[external/binutils.git] / include / bfdlink.h
1 /* bfdlink.h -- header file for BFD link routines
2    Copyright 1993, 94, 95, 96, 97, 1999 Free Software Foundation, Inc.
3    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #ifndef BFDLINK_H
22 #define BFDLINK_H
23
24 /* Which symbols to strip during a link.  */
25 enum bfd_link_strip
26 {
27   strip_none,           /* Don't strip any symbols.  */
28   strip_debugger,       /* Strip debugging symbols.  */
29   strip_some,           /* keep_hash is the list of symbols to keep.  */
30   strip_all             /* Strip all symbols.  */
31 };
32
33 /* Which local symbols to discard during a link.  This is irrelevant
34    if strip_all is used.  */
35 enum bfd_link_discard
36 {
37   discard_none,         /* Don't discard any locals.  */
38   discard_l,            /* Discard local temporary symbols.  */
39   discard_all           /* Discard all locals.  */
40 };
41 \f
42 /* These are the possible types of an entry in the BFD link hash
43    table.  */
44
45 enum bfd_link_hash_type
46 {
47   bfd_link_hash_new,            /* Symbol is new.  */
48   bfd_link_hash_undefined,      /* Symbol seen before, but undefined.  */
49   bfd_link_hash_undefweak,      /* Symbol is weak and undefined.  */
50   bfd_link_hash_defined,        /* Symbol is defined.  */
51   bfd_link_hash_defweak,        /* Symbol is weak and defined.  */
52   bfd_link_hash_common,         /* Symbol is common.  */
53   bfd_link_hash_indirect,       /* Symbol is an indirect link.  */
54   bfd_link_hash_warning         /* Like indirect, but warn if referenced.  */
55 };
56
57 /* The linking routines use a hash table which uses this structure for
58    its elements.  */
59
60 struct bfd_link_hash_entry
61 {
62   /* Base hash table entry structure.  */
63   struct bfd_hash_entry root;
64   /* Type of this entry.  */
65   enum bfd_link_hash_type type;
66
67   /* Undefined and common symbols are kept in a linked list through
68      this field.  This field is not in the union because that would
69      force us to remove entries from the list when we changed their
70      type, which would force the list to be doubly linked, which would
71      waste more memory.  When an undefined or common symbol is
72      created, it should be added to this list, the head of which is in
73      the link hash table itself.  As symbols are defined, they need
74      not be removed from the list; anything which reads the list must
75      doublecheck the symbol type.
76
77      Weak symbols are not kept on this list.
78
79      Defined and defweak symbols use this field as a reference marker.
80      If the field is not NULL, or this structure is the tail of the
81      undefined symbol list, the symbol has been referenced.  If the
82      symbol is undefined and becomes defined, this field will
83      automatically be non-NULL since the symbol will have been on the
84      undefined symbol list.  */
85   struct bfd_link_hash_entry *next;
86   /* A union of information depending upon the type.  */
87   union
88     {
89       /* Nothing is kept for bfd_hash_new.  */
90       /* bfd_link_hash_undefined, bfd_link_hash_undefweak.  */
91       struct
92         {
93           bfd *abfd;            /* BFD symbol was found in.  */
94         } undef;
95       /* bfd_link_hash_defined, bfd_link_hash_defweak.  */
96       struct
97         {
98           bfd_vma value;        /* Symbol value.  */
99           asection *section;    /* Symbol section.  */
100         } def;
101       /* bfd_link_hash_indirect, bfd_link_hash_warning.  */
102       struct
103         {
104           struct bfd_link_hash_entry *link;     /* Real symbol.  */
105           const char *warning;  /* Warning (bfd_link_hash_warning only).  */
106         } i;
107       /* bfd_link_hash_common.  */
108       struct
109         {
110           /* The linker needs to know three things about common
111              symbols: the size, the alignment, and the section in
112              which the symbol should be placed.  We store the size
113              here, and we allocate a small structure to hold the
114              section and the alignment.  The alignment is stored as a
115              power of two.  We don't store all the information
116              directly because we don't want to increase the size of
117              the union; this structure is a major space user in the
118              linker.  */
119           bfd_size_type size;   /* Common symbol size.  */
120           struct bfd_link_hash_common_entry
121             {
122               unsigned int alignment_power;     /* Alignment.  */
123               asection *section;                /* Symbol section.  */
124             } *p;
125         } c;
126     } u;
127 };
128
129 /* This is the link hash table.  It is a derived class of
130    bfd_hash_table.  */
131
132 struct bfd_link_hash_table
133 {
134   /* The hash table itself.  */
135   struct bfd_hash_table table;
136   /* The back end which created this hash table.  This indicates the
137      type of the entries in the hash table, which is sometimes
138      important information when linking object files of different
139      types together.  */
140   const bfd_target *creator;
141   /* A linked list of undefined and common symbols, linked through the
142      next field in the bfd_link_hash_entry structure.  */
143   struct bfd_link_hash_entry *undefs;
144   /* Entries are added to the tail of the undefs list.  */
145   struct bfd_link_hash_entry *undefs_tail;
146 };
147
148 /* Look up an entry in a link hash table.  If FOLLOW is true, this
149    follows bfd_link_hash_indirect and bfd_link_hash_warning links to
150    the real symbol.  */
151 extern struct bfd_link_hash_entry *bfd_link_hash_lookup
152   PARAMS ((struct bfd_link_hash_table *, const char *, boolean create,
153            boolean copy, boolean follow));
154
155 /* Look up an entry in the main linker hash table if the symbol might
156    be wrapped.  This should only be used for references to an
157    undefined symbol, not for definitions of a symbol.  */
158
159 extern struct bfd_link_hash_entry *bfd_wrapped_link_hash_lookup
160   PARAMS ((bfd *, struct bfd_link_info *, const char *, boolean, boolean,
161            boolean));
162
163 /* Traverse a link hash table.  */
164 extern void bfd_link_hash_traverse
165   PARAMS ((struct bfd_link_hash_table *,
166            boolean (*) (struct bfd_link_hash_entry *, PTR),
167            PTR));
168
169 /* Add an entry to the undefs list.  */
170 extern void bfd_link_add_undef
171   PARAMS ((struct bfd_link_hash_table *, struct bfd_link_hash_entry *));
172 \f
173 /* This structure holds all the information needed to communicate
174    between BFD and the linker when doing a link.  */
175
176 struct bfd_link_info
177 {
178   /* Function callbacks.  */
179   const struct bfd_link_callbacks *callbacks;
180   /* true if BFD should generate a relocateable object file.  */
181   boolean relocateable;
182   /* true if BFD should generate relocation information in the final executable.  */
183   boolean emitrelocations;
184   /* true if BFD should generate a "task linked" object file,
185      similar to relocatable but also with globals converted to statics. */
186   boolean task_link;
187   /* true if BFD should generate a shared object.  */
188   boolean shared;
189   /* true if BFD should pre-bind symbols in a shared object.  */
190   boolean symbolic;
191   /* true if shared objects should be linked directly, not shared.  */
192   boolean static_link;
193   /* true if the output file should be in a traditional format.  This
194      is equivalent to the setting of the BFD_TRADITIONAL_FORMAT flag
195      on the output file, but may be checked when reading the input
196      files.  */
197   boolean traditional_format;
198   /* true if we want to produced optimized output files.  This might
199      need much more time and therefore must be explicitly selected.  */
200   boolean optimize;
201   /* true if BFD should generate errors for undefined symbols
202      even if generating a shared object.  */
203   boolean no_undefined;
204   /* Which symbols to strip.  */
205   enum bfd_link_strip strip;
206   /* Which local symbols to discard.  */
207   enum bfd_link_discard discard;
208   /* true if symbols should be retained in memory, false if they
209      should be freed and reread.  */
210   boolean keep_memory;
211   /* The list of input BFD's involved in the link.  These are chained
212      together via the link_next field.  */
213   bfd *input_bfds;
214   /* If a symbol should be created for each input BFD, this is section
215      where those symbols should be placed.  It must be a section in
216      the output BFD.  It may be NULL, in which case no such symbols
217      will be created.  This is to support CREATE_OBJECT_SYMBOLS in the
218      linker command language.  */
219   asection *create_object_symbols_section;
220   /* Hash table handled by BFD.  */
221   struct bfd_link_hash_table *hash;
222   /* Hash table of symbols to keep.  This is NULL unless strip is
223      strip_some.  */
224   struct bfd_hash_table *keep_hash;
225   /* true if every symbol should be reported back via the notice
226      callback.  */
227   boolean notice_all;
228   /* Hash table of symbols to report back via the notice callback.  If
229      this is NULL, and notice_all is false, then no symbols are
230      reported back.  */
231   struct bfd_hash_table *notice_hash;
232   /* Hash table of symbols which are being wrapped (the --wrap linker
233      option).  If this is NULL, no symbols are being wrapped.  */
234   struct bfd_hash_table *wrap_hash;
235   /* If a base output file is wanted, then this points to it */
236   PTR base_file;
237
238   /* If non-zero, specifies that branches which are problematic for the
239   MPC860 C0 (or earlier) should be checked for and modified.  It gives the
240   number of bytes that should be checked at the end of each text page. */
241   int mpc860c0;
242
243   /* The function to call when the executable or shared object is
244      loaded.  */
245   const char *init_function;
246   /* The function to call when the executable or shared object is
247      unloaded.  */
248   const char *fini_function;
249 };
250
251 /* This structures holds a set of callback functions.  These are
252    called by the BFD linker routines.  The first argument to each
253    callback function is the bfd_link_info structure being used.  Each
254    function returns a boolean value.  If the function returns false,
255    then the BFD function which called it will return with a failure
256    indication.  */
257
258 struct bfd_link_callbacks
259 {
260   /* A function which is called when an object is added from an
261      archive.  ABFD is the archive element being added.  NAME is the
262      name of the symbol which caused the archive element to be pulled
263      in.  */
264   boolean (*add_archive_element) PARAMS ((struct bfd_link_info *,
265                                           bfd *abfd,
266                                           const char *name));
267   /* A function which is called when a symbol is found with multiple
268      definitions.  NAME is the symbol which is defined multiple times.
269      OBFD is the old BFD, OSEC is the old section, OVAL is the old
270      value, NBFD is the new BFD, NSEC is the new section, and NVAL is
271      the new value.  OBFD may be NULL.  OSEC and NSEC may be
272      bfd_com_section or bfd_ind_section.  */
273   boolean (*multiple_definition) PARAMS ((struct bfd_link_info *,
274                                           const char *name,
275                                           bfd *obfd,
276                                           asection *osec,
277                                           bfd_vma oval,
278                                           bfd *nbfd,
279                                           asection *nsec,
280                                           bfd_vma nval));
281   /* A function which is called when a common symbol is defined
282      multiple times.  NAME is the symbol appearing multiple times.
283      OBFD is the BFD of the existing symbol; it may be NULL if this is
284      not known.  OTYPE is the type of the existing symbol, which may
285      be bfd_link_hash_defined, bfd_link_hash_defweak,
286      bfd_link_hash_common, or bfd_link_hash_indirect.  If OTYPE is
287      bfd_link_hash_common, OSIZE is the size of the existing symbol.
288      NBFD is the BFD of the new symbol.  NTYPE is the type of the new
289      symbol, one of bfd_link_hash_defined, bfd_link_hash_common, or
290      bfd_link_hash_indirect.  If NTYPE is bfd_link_hash_common, NSIZE
291      is the size of the new symbol.  */
292   boolean (*multiple_common) PARAMS ((struct bfd_link_info *,
293                                       const char *name,
294                                       bfd *obfd,
295                                       enum bfd_link_hash_type otype,
296                                       bfd_vma osize,
297                                       bfd *nbfd,
298                                       enum bfd_link_hash_type ntype,
299                                       bfd_vma nsize));
300   /* A function which is called to add a symbol to a set.  ENTRY is
301      the link hash table entry for the set itself (e.g.,
302      __CTOR_LIST__).  RELOC is the relocation to use for an entry in
303      the set when generating a relocateable file, and is also used to
304      get the size of the entry when generating an executable file.
305      ABFD, SEC and VALUE identify the value to add to the set.  */
306   boolean (*add_to_set) PARAMS ((struct bfd_link_info *,
307                                  struct bfd_link_hash_entry *entry,
308                                  bfd_reloc_code_real_type reloc,
309                                  bfd *abfd, asection *sec, bfd_vma value));
310   /* A function which is called when the name of a g++ constructor or
311      destructor is found.  This is only called by some object file
312      formats.  CONSTRUCTOR is true for a constructor, false for a
313      destructor.  This will use BFD_RELOC_CTOR when generating a
314      relocateable file.  NAME is the name of the symbol found.  ABFD,
315      SECTION and VALUE are the value of the symbol.  */
316   boolean (*constructor) PARAMS ((struct bfd_link_info *,
317                                   boolean constructor,
318                                   const char *name, bfd *abfd, asection *sec,
319                                   bfd_vma value));
320   /* A function which is called to issue a linker warning.  For
321      example, this is called when there is a reference to a warning
322      symbol.  WARNING is the warning to be issued.  SYMBOL is the name
323      of the symbol which triggered the warning; it may be NULL if
324      there is none.  ABFD, SECTION and ADDRESS identify the location
325      which trigerred the warning; either ABFD or SECTION or both may
326      be NULL if the location is not known.  */
327   boolean (*warning) PARAMS ((struct bfd_link_info *,
328                               const char *warning, const char *symbol,
329                               bfd *abfd, asection *section,
330                               bfd_vma address));
331   /* A function which is called when a relocation is attempted against
332      an undefined symbol.  NAME is the symbol which is undefined.
333      ABFD, SECTION and ADDRESS identify the location from which the
334      reference is made. FATAL indicates whether an undefined symbol is
335      a fatal error or not. In some cases SECTION may be NULL.  */
336   boolean (*undefined_symbol) PARAMS ((struct bfd_link_info *,
337                                        const char *name, bfd *abfd,
338                                        asection *section,
339                                        bfd_vma address,
340                                        boolean fatal));
341   /* A function which is called when a reloc overflow occurs.  NAME is
342      the name of the symbol or section the reloc is against,
343      RELOC_NAME is the name of the relocation, and ADDEND is any
344      addend that is used.  ABFD, SECTION and ADDRESS identify the
345      location at which the overflow occurs; if this is the result of a
346      bfd_section_reloc_link_order or bfd_symbol_reloc_link_order, then
347      ABFD will be NULL.  */
348   boolean (*reloc_overflow) PARAMS ((struct bfd_link_info *,
349                                      const char *name,
350                                      const char *reloc_name, bfd_vma addend,
351                                      bfd *abfd, asection *section,
352                                      bfd_vma address));
353   /* A function which is called when a dangerous reloc is performed.
354      The canonical example is an a29k IHCONST reloc which does not
355      follow an IHIHALF reloc.  MESSAGE is an appropriate message.
356      ABFD, SECTION and ADDRESS identify the location at which the
357      problem occurred; if this is the result of a
358      bfd_section_reloc_link_order or bfd_symbol_reloc_link_order, then
359      ABFD will be NULL.  */
360   boolean (*reloc_dangerous) PARAMS ((struct bfd_link_info *,
361                                       const char *message,
362                                       bfd *abfd, asection *section,
363                                       bfd_vma address));
364   /* A function which is called when a reloc is found to be attached
365      to a symbol which is not being written out.  NAME is the name of
366      the symbol.  ABFD, SECTION and ADDRESS identify the location of
367      the reloc; if this is the result of a
368      bfd_section_reloc_link_order or bfd_symbol_reloc_link_order, then
369      ABFD will be NULL.  */
370   boolean (*unattached_reloc) PARAMS ((struct bfd_link_info *,
371                                        const char *name,
372                                        bfd *abfd, asection *section,
373                                        bfd_vma address));
374   /* A function which is called when a symbol in notice_hash is
375      defined or referenced.  NAME is the symbol.  ABFD, SECTION and
376      ADDRESS are the value of the symbol.  If SECTION is
377      bfd_und_section, this is a reference.  */
378   boolean (*notice) PARAMS ((struct bfd_link_info *, const char *name,
379                              bfd *abfd, asection *section, bfd_vma address));
380 };
381 \f
382 /* The linker builds link_order structures which tell the code how to
383    include input data in the output file.  */
384
385 /* These are the types of link_order structures.  */
386
387 enum bfd_link_order_type
388 {
389   bfd_undefined_link_order,     /* Undefined.  */
390   bfd_indirect_link_order,      /* Built from a section.  */
391   bfd_fill_link_order,          /* Fill with a 16 bit constant.  */
392   bfd_data_link_order,          /* Set to explicit data.  */
393   bfd_section_reloc_link_order, /* Relocate against a section.  */
394   bfd_symbol_reloc_link_order   /* Relocate against a symbol.  */
395 };
396
397 /* This is the link_order structure itself.  These form a chain
398    attached to the section whose contents they are describing.  */
399
400 struct bfd_link_order 
401 {
402   /* Next link_order in chain.  */
403   struct bfd_link_order *next;
404   /* Type of link_order.  */
405   enum bfd_link_order_type type;
406   /* Offset within output section.  */
407   bfd_vma offset;  
408   /* Size within output section.  */
409   bfd_size_type size;
410   /* Type specific information.  */
411   union 
412     {
413       struct 
414         {
415           /* Section to include.  If this is used, then
416              section->output_section must be the section the
417              link_order is attached to, section->output_offset must
418              equal the link_order offset field, and section->_raw_size
419              must equal the link_order size field.  Maybe these
420              restrictions should be relaxed someday.  */
421           asection *section;
422         } indirect;
423       struct
424         {
425           /* Value to fill with.  */
426           unsigned int value;
427         } fill;
428       struct
429         {
430           /* Data to put into file.  The size field gives the number
431              of bytes which this field points to.  */
432           bfd_byte *contents;
433         } data;
434       struct
435         {
436           /* Description of reloc to generate.  Used for
437              bfd_section_reloc_link_order and
438              bfd_symbol_reloc_link_order.  */
439           struct bfd_link_order_reloc *p;
440         } reloc;
441     } u;
442 };
443
444 /* A linker order of type bfd_section_reloc_link_order or
445    bfd_symbol_reloc_link_order means to create a reloc against a
446    section or symbol, respectively.  This is used to implement -Ur to
447    generate relocs for the constructor tables.  The
448    bfd_link_order_reloc structure describes the reloc that BFD should
449    create.  It is similar to a arelent, but I didn't use arelent
450    because the linker does not know anything about most symbols, and
451    any asymbol structure it creates will be partially meaningless.
452    This information could logically be in the bfd_link_order struct,
453    but I didn't want to waste the space since these types of relocs
454    are relatively rare.  */
455
456 struct bfd_link_order_reloc
457 {
458   /* Reloc type.  */
459   bfd_reloc_code_real_type reloc;
460
461   union
462     {
463       /* For type bfd_section_reloc_link_order, this is the section
464          the reloc should be against.  This must be a section in the
465          output BFD, not any of the input BFDs.  */
466       asection *section;
467       /* For type bfd_symbol_reloc_link_order, this is the name of the
468          symbol the reloc should be against.  */
469       const char *name;
470     } u;
471
472   /* Addend to use.  The object file should contain zero.  The BFD
473      backend is responsible for filling in the contents of the object
474      file correctly.  For some object file formats (e.g., COFF) the
475      addend must be stored into in the object file, and for some
476      (e.g., SPARC a.out) it is kept in the reloc.  */
477   bfd_vma addend;
478 };
479
480 /* Allocate a new link_order for a section.  */
481 extern struct bfd_link_order *bfd_new_link_order PARAMS ((bfd *, asection *));
482
483 /* These structures are used to describe version information for the
484    ELF linker.  These structures could be manipulated entirely inside
485    BFD, but it would be a pain.  Instead, the regular linker sets up
486    these structures, and then passes them into BFD.  */
487
488 /* Regular expressions for a version.  */
489
490 struct bfd_elf_version_expr
491 {
492   /* Next regular expression for this version.  */
493   struct bfd_elf_version_expr *next;
494   /* Regular expression.  */
495   const char *pattern;
496   /* Matching function.  */
497   int (*match) PARAMS((struct bfd_elf_version_expr *, const char *));
498 };
499
500 /* Version dependencies.  */
501
502 struct bfd_elf_version_deps
503 {
504   /* Next dependency for this version.  */
505   struct bfd_elf_version_deps *next;
506   /* The version which this version depends upon.  */
507   struct bfd_elf_version_tree *version_needed;
508 };
509
510 /* A node in the version tree.  */
511
512 struct bfd_elf_version_tree
513 {
514   /* Next version.  */
515   struct bfd_elf_version_tree *next;
516   /* Name of this version.  */
517   const char *name;
518   /* Version number.  */
519   unsigned int vernum;
520   /* Regular expressions for global symbols in this version.  */
521   struct bfd_elf_version_expr *globals;
522   /* Regular expressions for local symbols in this version.  */
523   struct bfd_elf_version_expr *locals;
524   /* List of versions which this version depends upon.  */
525   struct bfd_elf_version_deps *deps;
526   /* Index of the version name.  This is used within BFD.  */
527   unsigned int name_indx;
528   /* Whether this version tree was used.  This is used within BFD.  */
529   int used;
530 };
531
532 #endif