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