* bfdlink.h (enum bfd_link_hash_type): Rename bfd_link_hash_weak
[platform/upstream/binutils.git] / include / bfdlink.h
1 /* bfdlink.h -- header file for BFD link routines
2    Copyright 1993 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., 675 Mass Ave, Cambridge, MA 02139, 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 locals with a certain prefix.  */
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.  On the assumption
113              that a single common symbol will not take up incredible
114              amounts of memory, we pack the size and the alignment
115              into the space of a single integer.  The alignment is
116              stored as a power of two.  */
117           unsigned int alignment_power : 6;     /* Alignment.  */
118           unsigned int size : 26;               /* Common symbol size.  */
119           asection *section;                    /* Symbol section.  */
120         } c;
121     } u;
122 };
123
124 /* This is the link hash table.  It is a derived class of
125    bfd_hash_table.  */
126
127 struct bfd_link_hash_table
128 {
129   /* The hash table itself.  */
130   struct bfd_hash_table table;
131   /* The back end which created this hash table.  This indicates the
132      type of the entries in the hash table, which is sometimes
133      important information when linking object files of different
134      types together.  */
135   const bfd_target *creator;
136   /* A linked list of undefined and common symbols, linked through the
137      next field in the bfd_link_hash_entry structure.  */
138   struct bfd_link_hash_entry *undefs;
139   /* Entries are added to the tail of the undefs list.  */
140   struct bfd_link_hash_entry *undefs_tail;
141 };
142
143 /* Look up an entry in a link hash table.  If FOLLOW is true, this
144    follows bfd_link_hash_indirect and bfd_link_hash_warning links to
145    the real symbol.  */
146 extern struct bfd_link_hash_entry *bfd_link_hash_lookup
147   PARAMS ((struct bfd_link_hash_table *, const char *, boolean create,
148            boolean copy, boolean follow));
149
150 /* Traverse a link hash table.  */
151 extern void bfd_link_hash_traverse
152   PARAMS ((struct bfd_link_hash_table *,
153            boolean (*) (struct bfd_link_hash_entry *, PTR),
154            PTR));
155
156 /* Add an entry to the undefs list.  */
157 extern void bfd_link_add_undef
158   PARAMS ((struct bfd_link_hash_table *, struct bfd_link_hash_entry *));
159 \f
160 /* This structure holds all the information needed to communicate
161    between BFD and the linker when doing a link.  */
162
163 struct bfd_link_info
164 {
165   /* Function callbacks.  */
166   const struct bfd_link_callbacks *callbacks;
167   /* true if BFD should generate a relocateable object file.  */
168   boolean relocateable;
169   /* true if BFD should generate a shared object.  */
170   boolean shared;
171   /* Which symbols to strip.  */
172   enum bfd_link_strip strip;
173   /* Which local symbols to discard.  */
174   enum bfd_link_discard discard;
175   /* The local symbol prefix to discard if using discard_l.  */
176   unsigned int lprefix_len;
177   const char *lprefix;
178   /* true if symbols should be retained in memory, false if they
179      should be freed and reread.  */
180   boolean keep_memory;
181   /* The list of input BFD's involved in the link.  These are chained
182      together via the link_next field.  */
183   bfd *input_bfds;
184   /* If a symbol should be created for each input BFD, this is section
185      where those symbols should be placed.  It must be a section in
186      the output BFD.  It may be NULL, in which case no such symbols
187      will be created.  This is to support CREATE_OBJECT_SYMBOLS in the
188      linker command language.  */
189   asection *create_object_symbols_section;
190   /* Hash table handled by BFD.  */
191   struct bfd_link_hash_table *hash;
192   /* Hash table of symbols to keep.  This is NULL unless strip is
193      strip_some.  */
194   struct bfd_hash_table *keep_hash;
195   /* Hash table of symbols to report back via notice_callback.  If
196      this is NULL no symbols are reported back.  */
197   struct bfd_hash_table *notice_hash;
198 };
199
200 /* This structures holds a set of callback functions.  These are
201    called by the BFD linker routines.  The first argument to each
202    callback function is the bfd_link_info structure being used.  Each
203    function returns a boolean value.  If the function returns false,
204    then the BFD function which called it will return with a failure
205    indication.  */
206
207 struct bfd_link_callbacks
208 {
209   /* A function which is called when an object is added from an
210      archive.  ABFD is the archive element being added.  NAME is the
211      name of the symbol which caused the archive element to be pulled
212      in.  */
213   boolean (*add_archive_element) PARAMS ((struct bfd_link_info *,
214                                           bfd *abfd,
215                                           const char *name));
216   /* A function which is called when a symbol is found with multiple
217      definitions.  NAME is the symbol which is defined multiple times.
218      OBFD is the old BFD, OSEC is the old section, OVAL is the old
219      value, NBFD is the new BFD, NSEC is the new section, and NVAL is
220      the new value.  OBFD may be NULL.  OSEC and NSEC may be
221      bfd_com_section or bfd_ind_section.  */
222   boolean (*multiple_definition) PARAMS ((struct bfd_link_info *,
223                                           const char *name,
224                                           bfd *obfd,
225                                           asection *osec,
226                                           bfd_vma oval,
227                                           bfd *nbfd,
228                                           asection *nsec,
229                                           bfd_vma nval));
230   /* A function which is called when a common symbol is defined
231      multiple times.  NAME is the symbol appearing multiple times.
232      OBFD is the BFD of the existing symbol; it may be NULL if this is
233      not known.  OTYPE is the type of the existing symbol, which may
234      be bfd_link_hash_defined, bfd_link_hash_defweak,
235      bfd_link_hash_common, or bfd_link_hash_indirect.  If OTYPE is
236      bfd_link_hash_common, OSIZE is the size of the existing symbol.
237      NBFD is the BFD of the new symbol.  NTYPE is the type of the new
238      symbol, one of bfd_link_hash_defined, bfd_link_hash_common, or
239      bfd_link_hash_indirect.  If NTYPE is bfd_link_hash_common, NSIZE
240      is the size of the new symbol.  */
241   boolean (*multiple_common) PARAMS ((struct bfd_link_info *,
242                                       const char *name,
243                                       bfd *obfd,
244                                       enum bfd_link_hash_type otype,
245                                       bfd_vma osize,
246                                       bfd *nbfd,
247                                       enum bfd_link_hash_type ntype,
248                                       bfd_vma nsize));
249   /* A function which is called to add a symbol to a set.  ENTRY is
250      the link hash table entry for the set itself (e.g.,
251      __CTOR_LIST__).  RELOC is the relocation to use for an entry in
252      the set when generating a relocateable file, and is also used to
253      get the size of the entry when generating an executable file.
254      ABFD, SEC and VALUE identify the value to add to the set.  */
255   boolean (*add_to_set) PARAMS ((struct bfd_link_info *,
256                                  struct bfd_link_hash_entry *entry,
257                                  bfd_reloc_code_real_type reloc,
258                                  bfd *abfd, asection *sec, bfd_vma value));
259   /* A function which is called when the name of a g++ constructor or
260      destructor is found.  This is only called by some object file
261      formats.  CONSTRUCTOR is true for a constructor, false for a
262      destructor.  This will use BFD_RELOC_CTOR when generating a
263      relocateable file.  NAME is the name of the symbol found.  ABFD,
264      SECTION and VALUE are the value of the symbol.  */
265   boolean (*constructor) PARAMS ((struct bfd_link_info *,
266                                   boolean constructor,
267                                   const char *name, bfd *abfd, asection *sec,
268                                   bfd_vma value));
269   /* A function which is called when there is a reference to a warning
270      symbol.  WARNING is the warning to be issued.  */
271   boolean (*warning) PARAMS ((struct bfd_link_info *,
272                               const char *warning));
273   /* A function which is called when a relocation is attempted against
274      an undefined symbol.  NAME is the symbol which is undefined.
275      ABFD, SECTION and ADDRESS identify the location from which the
276      reference is made.  */
277   boolean (*undefined_symbol) PARAMS ((struct bfd_link_info *,
278                                        const char *name, bfd *abfd,
279                                        asection *section, bfd_vma address));
280   /* A function which is called when a reloc overflow occurs.  NAME is
281      the name of the symbol or section the reloc is against,
282      RELOC_NAME is the name of the relocation, and ADDEND is any
283      addend that is used.  ABFD, SECTION and ADDRESS identify the
284      location at which the overflow occurs; if this is the result of a
285      bfd_section_reloc_link_order or bfd_symbol_reloc_link_order, then
286      ABFD will be NULL.  */
287   boolean (*reloc_overflow) PARAMS ((struct bfd_link_info *,
288                                      const char *name,
289                                      const char *reloc_name, bfd_vma addend,
290                                      bfd *abfd, asection *section,
291                                      bfd_vma address));
292   /* A function which is called when a dangerous reloc is performed.
293      The canonical example is an a29k IHCONST reloc which does not
294      follow an IHIHALF reloc.  MESSAGE is an appropriate message.
295      ABFD, SECTION and ADDRESS identify the location at which the
296      problem occurred; if this is the result of a
297      bfd_section_reloc_link_order or bfd_symbol_reloc_link_order, then
298      ABFD will be NULL.  */
299   boolean (*reloc_dangerous) PARAMS ((struct bfd_link_info *,
300                                       const char *message,
301                                       bfd *abfd, asection *section,
302                                       bfd_vma address));
303   /* A function which is called when a reloc is found to be attached
304      to a symbol which is not being written out.  NAME is the name of
305      the symbol.  ABFD, SECTION and ADDRESS identify the location of
306      the reloc; if this is the result of a
307      bfd_section_reloc_link_order or bfd_symbol_reloc_link_order, then
308      ABFD will be NULL.  */
309   boolean (*unattached_reloc) PARAMS ((struct bfd_link_info *,
310                                        const char *name,
311                                        bfd *abfd, asection *section,
312                                        bfd_vma address));
313   /* A function which is called when a symbol in notice_hash is
314      defined or referenced.  NAME is the symbol.  ABFD, SECTION and
315      ADDRESS are the value of the symbol.  If SECTION is
316      bfd_und_section, this is a reference.  */
317   boolean (*notice) PARAMS ((struct bfd_link_info *, const char *name,
318                              bfd *abfd, asection *section, bfd_vma address));
319 };
320 \f
321 /* The linker builds link_order structures which tell the code how to
322    include input data in the output file.  */
323
324 /* These are the types of link_order structures.  */
325
326 enum bfd_link_order_type
327 {
328   bfd_undefined_link_order,     /* Undefined.  */
329   bfd_indirect_link_order,      /* Built from a section.  */
330   bfd_fill_link_order,          /* Fill with a 16 bit constant.  */
331   bfd_data_link_order,          /* Set to explicit data.  */
332   bfd_section_reloc_link_order, /* Relocate against a section.  */
333   bfd_symbol_reloc_link_order   /* Relocate against a symbol.  */
334 };
335
336 /* This is the link_order structure itself.  These form a chain
337    attached to the section whose contents they are describing.  */
338
339 struct bfd_link_order 
340 {
341   /* Next link_order in chain.  */
342   struct bfd_link_order *next;
343   /* Type of link_order.  */
344   enum bfd_link_order_type type;
345   /* Offset within output section.  */
346   bfd_vma offset;  
347   /* Size within output section.  */
348   bfd_size_type size;
349   /* Type specific information.  */
350   union 
351     {
352       struct 
353         {
354           /* Section to include.  If this is used, then
355              section->output_section must be the section the
356              link_order is attached to, section->output_offset must
357              equal the link_order offset field, and section->_raw_size
358              must equal the link_order size field.  Maybe these
359              restrictions should be relaxed someday.  */
360           asection *section;
361         } indirect;
362       struct
363         {
364           /* Value to fill with.  */
365           unsigned int value;
366         } fill;
367       struct
368         {
369           /* Data to put into file.  The size field gives the number
370              of bytes which this field points to.  */
371           bfd_byte *contents;
372         } data;
373       struct
374         {
375           /* Description of reloc to generate.  Used for
376              bfd_section_reloc_link_order and
377              bfd_symbol_reloc_link_order.  */
378           struct bfd_link_order_reloc *p;
379         } reloc;
380     } u;
381 };
382
383 /* A linker order of type bfd_section_reloc_link_order or
384    bfd_symbol_reloc_link_order means to create a reloc against a
385    section or symbol, respectively.  This is used to implement -Ur to
386    generate relocs for the constructor tables.  The
387    bfd_link_order_reloc structure describes the reloc that BFD should
388    create.  It is similar to a arelent, but I didn't use arelent
389    because the linker does not know anything about most symbols, and
390    any asymbol structure it creates will be partially meaningless.
391    This information could logically be in the bfd_link_order struct,
392    but I didn't want to waste the space since these types of relocs
393    are relatively rare.  */
394
395 struct bfd_link_order_reloc
396 {
397   /* Reloc type.  */
398   bfd_reloc_code_real_type reloc;
399
400   union
401     {
402       /* For type bfd_section_reloc_link_order, this is the section
403          the reloc should be against.  This must be a section in the
404          output BFD, not any of the input BFDs.  */
405       asection *section;
406       /* For type bfd_symbol_reloc_link_order, this is the name of the
407          symbol the reloc should be against.  */
408       const char *name;
409     } u;
410
411   /* Addend to use.  The object file should contain zero.  The BFD
412      backend is responsible for filling in the contents of the object
413      file correctly.  For some object file formats (e.g., COFF) the
414      addend must be stored into in the object file, and for some
415      (e.g., SPARC a.out) it is kept in the reloc.  */
416   bfd_vma addend;
417 };
418
419 /* Allocate a new link_order for a section.  */
420 extern struct bfd_link_order *bfd_new_link_order PARAMS ((bfd *, asection *));
421
422 #endif