source: trunk/binutils/bfd/doc/bfd.info-6@ 3604

Last change on this file since 3604 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 44.2 KB
Line 
1This is bfd.info, produced by makeinfo version 4.3 from bfd.texinfo.
2
3START-INFO-DIR-ENTRY
4* Bfd: (bfd). The Binary File Descriptor library.
5END-INFO-DIR-ENTRY
6
7 This file documents the BFD library.
8
9 Copyright (C) 1991, 2000, 2001, 2003 Free Software Foundation, Inc.
10
11 Permission is granted to copy, distribute and/or modify this document
12 under the terms of the GNU Free Documentation License, Version 1.1
13 or any later version published by the Free Software Foundation;
14 with no Invariant Sections, with no Front-Cover Texts, and with no
15 Back-Cover Texts. A copy of the license is included in the
16section entitled "GNU Free Documentation License".
17
18
19File: bfd.info, Node: Internal, Next: File Caching, Prev: Opening and Closing, Up: BFD front end
20
21Internal functions
22==================
23
24 *Description*
25These routines are used within BFD. They are not intended for export,
26but are documented here for completeness.
27
28`bfd_write_bigendian_4byte_int'
29...............................
30
31 *Synopsis*
32 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
33 *Description*
34Write a 4 byte integer I to the output BFD ABFD, in big endian order
35regardless of what else is going on. This is useful in archives.
36
37`bfd_put_size'
38..............
39
40`bfd_get_size'
41..............
42
43 *Description*
44These macros as used for reading and writing raw data in sections; each
45access (except for bytes) is vectored through the target format of the
46BFD and mangled accordingly. The mangling performs any necessary endian
47translations and removes alignment restrictions. Note that types
48accepted and returned by these macros are identical so they can be
49swapped around in macros--for example, `libaout.h' defines `GET_WORD'
50to either `bfd_get_32' or `bfd_get_64'.
51
52 In the put routines, VAL must be a `bfd_vma'. If we are on a system
53without prototypes, the caller is responsible for making sure that is
54true, with a cast if necessary. We don't cast them in the macro
55definitions because that would prevent `lint' or `gcc -Wall' from
56detecting sins such as passing a pointer. To detect calling these with
57less than a `bfd_vma', use `gcc -Wconversion' on a host with 64 bit
58`bfd_vma''s.
59
60 /* Byte swapping macros for user section data. */
61
62 #define bfd_put_8(abfd, val, ptr) \
63 ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val)))
64 #define bfd_put_signed_8 \
65 bfd_put_8
66 #define bfd_get_8(abfd, ptr) \
67 (*(unsigned char *) (ptr) & 0xff)
68 #define bfd_get_signed_8(abfd, ptr) \
69 (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
70
71 #define bfd_put_16(abfd, val, ptr) \
72 BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
73 #define bfd_put_signed_16 \
74 bfd_put_16
75 #define bfd_get_16(abfd, ptr) \
76 BFD_SEND(abfd, bfd_getx16, (ptr))
77 #define bfd_get_signed_16(abfd, ptr) \
78 BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
79
80 #define bfd_put_32(abfd, val, ptr) \
81 BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
82 #define bfd_put_signed_32 \
83 bfd_put_32
84 #define bfd_get_32(abfd, ptr) \
85 BFD_SEND(abfd, bfd_getx32, (ptr))
86 #define bfd_get_signed_32(abfd, ptr) \
87 BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
88
89 #define bfd_put_64(abfd, val, ptr) \
90 BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
91 #define bfd_put_signed_64 \
92 bfd_put_64
93 #define bfd_get_64(abfd, ptr) \
94 BFD_SEND(abfd, bfd_getx64, (ptr))
95 #define bfd_get_signed_64(abfd, ptr) \
96 BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
97
98 #define bfd_get(bits, abfd, ptr) \
99 ( (bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
100 : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
101 : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
102 : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
103 : (abort (), (bfd_vma) - 1))
104
105 #define bfd_put(bits, abfd, val, ptr) \
106 ( (bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
107 : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
108 : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
109 : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
110 : (abort (), (void) 0))
111
112`bfd_h_put_size'
113................
114
115 *Description*
116These macros have the same function as their `bfd_get_x' brethren,
117except that they are used for removing information for the header
118records of object files. Believe it or not, some object files keep
119their header records in big endian order and their data in little
120endian order.
121
122 /* Byte swapping macros for file header data. */
123
124 #define bfd_h_put_8(abfd, val, ptr) \
125 bfd_put_8 (abfd, val, ptr)
126 #define bfd_h_put_signed_8(abfd, val, ptr) \
127 bfd_put_8 (abfd, val, ptr)
128 #define bfd_h_get_8(abfd, ptr) \
129 bfd_get_8 (abfd, ptr)
130 #define bfd_h_get_signed_8(abfd, ptr) \
131 bfd_get_signed_8 (abfd, ptr)
132
133 #define bfd_h_put_16(abfd, val, ptr) \
134 BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
135 #define bfd_h_put_signed_16 \
136 bfd_h_put_16
137 #define bfd_h_get_16(abfd, ptr) \
138 BFD_SEND (abfd, bfd_h_getx16, (ptr))
139 #define bfd_h_get_signed_16(abfd, ptr) \
140 BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
141
142 #define bfd_h_put_32(abfd, val, ptr) \
143 BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
144 #define bfd_h_put_signed_32 \
145 bfd_h_put_32
146 #define bfd_h_get_32(abfd, ptr) \
147 BFD_SEND (abfd, bfd_h_getx32, (ptr))
148 #define bfd_h_get_signed_32(abfd, ptr) \
149 BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
150
151 #define bfd_h_put_64(abfd, val, ptr) \
152 BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
153 #define bfd_h_put_signed_64 \
154 bfd_h_put_64
155 #define bfd_h_get_64(abfd, ptr) \
156 BFD_SEND (abfd, bfd_h_getx64, (ptr))
157 #define bfd_h_get_signed_64(abfd, ptr) \
158 BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
159
160 /* Refinements on the above, which should eventually go away. Save
161 cluttering the source with (bfd_vma) and (bfd_byte *) casts. */
162
163 #define H_PUT_64(abfd, val, where) \
164 bfd_h_put_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
165
166 #define H_PUT_32(abfd, val, where) \
167 bfd_h_put_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
168
169 #define H_PUT_16(abfd, val, where) \
170 bfd_h_put_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
171
172 #define H_PUT_8 bfd_h_put_8
173
174 #define H_PUT_S64(abfd, val, where) \
175 bfd_h_put_signed_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
176
177 #define H_PUT_S32(abfd, val, where) \
178 bfd_h_put_signed_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
179
180 #define H_PUT_S16(abfd, val, where) \
181 bfd_h_put_signed_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
182
183 #define H_PUT_S8 bfd_h_put_signed_8
184
185 #define H_GET_64(abfd, where) \
186 bfd_h_get_64 ((abfd), (bfd_byte *) (where))
187
188 #define H_GET_32(abfd, where) \
189 bfd_h_get_32 ((abfd), (bfd_byte *) (where))
190
191 #define H_GET_16(abfd, where) \
192 bfd_h_get_16 ((abfd), (bfd_byte *) (where))
193
194 #define H_GET_8 bfd_h_get_8
195
196 #define H_GET_S64(abfd, where) \
197 bfd_h_get_signed_64 ((abfd), (bfd_byte *) (where))
198
199 #define H_GET_S32(abfd, where) \
200 bfd_h_get_signed_32 ((abfd), (bfd_byte *) (where))
201
202 #define H_GET_S16(abfd, where) \
203 bfd_h_get_signed_16 ((abfd), (bfd_byte *) (where))
204
205 #define H_GET_S8 bfd_h_get_signed_8
206
207`bfd_log2'
208..........
209
210 *Synopsis*
211 unsigned int bfd_log2 (bfd_vma x);
212 *Description*
213Return the log base 2 of the value supplied, rounded up. E.g., an X of
2141025 returns 11. A X of 0 returns 0.
215
216
217File: bfd.info, Node: File Caching, Next: Linker Functions, Prev: Internal, Up: BFD front end
218
219File caching
220============
221
222 The file caching mechanism is embedded within BFD and allows the
223application to open as many BFDs as it wants without regard to the
224underlying operating system's file descriptor limit (often as low as 20
225open files). The module in `cache.c' maintains a least recently used
226list of `BFD_CACHE_MAX_OPEN' files, and exports the name
227`bfd_cache_lookup', which runs around and makes sure that the required
228BFD is open. If not, then it chooses a file to close, closes it and
229opens the one wanted, returning its file handle.
230
231`BFD_CACHE_MAX_OPEN macro'
232..........................
233
234 *Description*
235The maximum number of files which the cache will keep open at one time.
236 #define BFD_CACHE_MAX_OPEN 10
237
238`bfd_last_cache'
239................
240
241 *Synopsis*
242 extern bfd *bfd_last_cache;
243 *Description*
244Zero, or a pointer to the topmost BFD on the chain. This is used by
245the `bfd_cache_lookup' macro in `libbfd.h' to determine when it can
246avoid a function call.
247
248`bfd_cache_lookup'
249..................
250
251 *Description*
252Check to see if the required BFD is the same as the last one looked up.
253If so, then it can use the stream in the BFD with impunity, since it
254can't have changed since the last lookup; otherwise, it has to perform
255the complicated lookup function.
256 #define bfd_cache_lookup(x) \
257 ((x)==bfd_last_cache? \
258 (FILE*) (bfd_last_cache->iostream): \
259 bfd_cache_lookup_worker(x))
260
261`bfd_cache_init'
262................
263
264 *Synopsis*
265 bfd_boolean bfd_cache_init (bfd *abfd);
266 *Description*
267Add a newly opened BFD to the cache.
268
269`bfd_cache_close'
270.................
271
272 *Synopsis*
273 bfd_boolean bfd_cache_close (bfd *abfd);
274 *Description*
275Remove the BFD ABFD from the cache. If the attached file is open, then
276close it too.
277
278 *Returns*
279`FALSE' is returned if closing the file fails, `TRUE' is returned if
280all is well.
281
282`bfd_open_file'
283...............
284
285 *Synopsis*
286 FILE* bfd_open_file(bfd *abfd);
287 *Description*
288Call the OS to open a file for ABFD. Return the `FILE *' (possibly
289`NULL') that results from this operation. Set up the BFD so that
290future accesses know the file is open. If the `FILE *' returned is
291`NULL', then it won't have been put in the cache, so it won't have to
292be removed from it.
293
294`bfd_cache_lookup_worker'
295.........................
296
297 *Synopsis*
298 FILE *bfd_cache_lookup_worker(bfd *abfd);
299 *Description*
300Called when the macro `bfd_cache_lookup' fails to find a quick answer.
301Find a file descriptor for ABFD. If necessary, it open it. If there
302are already more than `BFD_CACHE_MAX_OPEN' files open, it tries to
303close one first, to avoid running out of file descriptors.
304
305
306File: bfd.info, Node: Linker Functions, Next: Hash Tables, Prev: File Caching, Up: BFD front end
307
308Linker Functions
309================
310
311 The linker uses three special entry points in the BFD target vector.
312It is not necessary to write special routines for these entry points
313when creating a new BFD back end, since generic versions are provided.
314However, writing them can speed up linking and make it use
315significantly less runtime memory.
316
317 The first routine creates a hash table used by the other routines.
318The second routine adds the symbols from an object file to the hash
319table. The third routine takes all the object files and links them
320together to create the output file. These routines are designed so
321that the linker proper does not need to know anything about the symbols
322in the object files that it is linking. The linker merely arranges the
323sections as directed by the linker script and lets BFD handle the
324details of symbols and relocs.
325
326 The second routine and third routines are passed a pointer to a
327`struct bfd_link_info' structure (defined in `bfdlink.h') which holds
328information relevant to the link, including the linker hash table
329(which was created by the first routine) and a set of callback
330functions to the linker proper.
331
332 The generic linker routines are in `linker.c', and use the header
333file `genlink.h'. As of this writing, the only back ends which have
334implemented versions of these routines are a.out (in `aoutx.h') and
335ECOFF (in `ecoff.c'). The a.out routines are used as examples
336throughout this section.
337
338* Menu:
339
340* Creating a Linker Hash Table::
341* Adding Symbols to the Hash Table::
342* Performing the Final Link::
343
344
345File: bfd.info, Node: Creating a Linker Hash Table, Next: Adding Symbols to the Hash Table, Prev: Linker Functions, Up: Linker Functions
346
347Creating a linker hash table
348----------------------------
349
350 The linker routines must create a hash table, which must be derived
351from `struct bfd_link_hash_table' described in `bfdlink.c'. *Note Hash
352Tables::, for information on how to create a derived hash table. This
353entry point is called using the target vector of the linker output file.
354
355 The `_bfd_link_hash_table_create' entry point must allocate and
356initialize an instance of the desired hash table. If the back end does
357not require any additional information to be stored with the entries in
358the hash table, the entry point may simply create a `struct
359bfd_link_hash_table'. Most likely, however, some additional
360information will be needed.
361
362 For example, with each entry in the hash table the a.out linker
363keeps the index the symbol has in the final output file (this index
364number is used so that when doing a relocateable link the symbol index
365used in the output file can be quickly filled in when copying over a
366reloc). The a.out linker code defines the required structures and
367functions for a hash table derived from `struct bfd_link_hash_table'.
368The a.out linker hash table is created by the function
369`NAME(aout,link_hash_table_create)'; it simply allocates space for the
370hash table, initializes it, and returns a pointer to it.
371
372 When writing the linker routines for a new back end, you will
373generally not know exactly which fields will be required until you have
374finished. You should simply create a new hash table which defines no
375additional fields, and then simply add fields as they become necessary.
376
377
378File: bfd.info, Node: Adding Symbols to the Hash Table, Next: Performing the Final Link, Prev: Creating a Linker Hash Table, Up: Linker Functions
379
380Adding symbols to the hash table
381--------------------------------
382
383 The linker proper will call the `_bfd_link_add_symbols' entry point
384for each object file or archive which is to be linked (typically these
385are the files named on the command line, but some may also come from
386the linker script). The entry point is responsible for examining the
387file. For an object file, BFD must add any relevant symbol information
388to the hash table. For an archive, BFD must determine which elements
389of the archive should be used and adding them to the link.
390
391 The a.out version of this entry point is
392`NAME(aout,link_add_symbols)'.
393
394* Menu:
395
396* Differing file formats::
397* Adding symbols from an object file::
398* Adding symbols from an archive::
399
400
401File: bfd.info, Node: Differing file formats, Next: Adding symbols from an object file, Prev: Adding Symbols to the Hash Table, Up: Adding Symbols to the Hash Table
402
403Differing file formats
404......................
405
406 Normally all the files involved in a link will be of the same
407format, but it is also possible to link together different format
408object files, and the back end must support that. The
409`_bfd_link_add_symbols' entry point is called via the target vector of
410the file to be added. This has an important consequence: the function
411may not assume that the hash table is the type created by the
412corresponding `_bfd_link_hash_table_create' vector. All the
413`_bfd_link_add_symbols' function can assume about the hash table is
414that it is derived from `struct bfd_link_hash_table'.
415
416 Sometimes the `_bfd_link_add_symbols' function must store some
417information in the hash table entry to be used by the `_bfd_final_link'
418function. In such a case the `creator' field of the hash table must be
419checked to make sure that the hash table was created by an object file
420of the same format.
421
422 The `_bfd_final_link' routine must be prepared to handle a hash
423entry without any extra information added by the
424`_bfd_link_add_symbols' function. A hash entry without extra
425information will also occur when the linker script directs the linker
426to create a symbol. Note that, regardless of how a hash table entry is
427added, all the fields will be initialized to some sort of null value by
428the hash table entry initialization function.
429
430 See `ecoff_link_add_externals' for an example of how to check the
431`creator' field before saving information (in this case, the ECOFF
432external symbol debugging information) in a hash table entry.
433
434
435File: bfd.info, Node: Adding symbols from an object file, Next: Adding symbols from an archive, Prev: Differing file formats, Up: Adding Symbols to the Hash Table
436
437Adding symbols from an object file
438..................................
439
440 When the `_bfd_link_add_symbols' routine is passed an object file,
441it must add all externally visible symbols in that object file to the
442hash table. The actual work of adding the symbol to the hash table is
443normally handled by the function `_bfd_generic_link_add_one_symbol'.
444The `_bfd_link_add_symbols' routine is responsible for reading all the
445symbols from the object file and passing the correct information to
446`_bfd_generic_link_add_one_symbol'.
447
448 The `_bfd_link_add_symbols' routine should not use
449`bfd_canonicalize_symtab' to read the symbols. The point of providing
450this routine is to avoid the overhead of converting the symbols into
451generic `asymbol' structures.
452
453 `_bfd_generic_link_add_one_symbol' handles the details of combining
454common symbols, warning about multiple definitions, and so forth. It
455takes arguments which describe the symbol to add, notably symbol flags,
456a section, and an offset. The symbol flags include such things as
457`BSF_WEAK' or `BSF_INDIRECT'. The section is a section in the object
458file, or something like `bfd_und_section_ptr' for an undefined symbol
459or `bfd_com_section_ptr' for a common symbol.
460
461 If the `_bfd_final_link' routine is also going to need to read the
462symbol information, the `_bfd_link_add_symbols' routine should save it
463somewhere attached to the object file BFD. However, the information
464should only be saved if the `keep_memory' field of the `info' argument
465is TRUE, so that the `-no-keep-memory' linker switch is effective.
466
467 The a.out function which adds symbols from an object file is
468`aout_link_add_object_symbols', and most of the interesting work is in
469`aout_link_add_symbols'. The latter saves pointers to the hash tables
470entries created by `_bfd_generic_link_add_one_symbol' indexed by symbol
471number, so that the `_bfd_final_link' routine does not have to call the
472hash table lookup routine to locate the entry.
473
474
475File: bfd.info, Node: Adding symbols from an archive, Prev: Adding symbols from an object file, Up: Adding Symbols to the Hash Table
476
477Adding symbols from an archive
478..............................
479
480 When the `_bfd_link_add_symbols' routine is passed an archive, it
481must look through the symbols defined by the archive and decide which
482elements of the archive should be included in the link. For each such
483element it must call the `add_archive_element' linker callback, and it
484must add the symbols from the object file to the linker hash table.
485
486 In most cases the work of looking through the symbols in the archive
487should be done by the `_bfd_generic_link_add_archive_symbols' function.
488This function builds a hash table from the archive symbol table and
489looks through the list of undefined symbols to see which elements
490should be included. `_bfd_generic_link_add_archive_symbols' is passed
491a function to call to make the final decision about adding an archive
492element to the link and to do the actual work of adding the symbols to
493the linker hash table.
494
495 The function passed to `_bfd_generic_link_add_archive_symbols' must
496read the symbols of the archive element and decide whether the archive
497element should be included in the link. If the element is to be
498included, the `add_archive_element' linker callback routine must be
499called with the element as an argument, and the elements symbols must
500be added to the linker hash table just as though the element had itself
501been passed to the `_bfd_link_add_symbols' function.
502
503 When the a.out `_bfd_link_add_symbols' function receives an archive,
504it calls `_bfd_generic_link_add_archive_symbols' passing
505`aout_link_check_archive_element' as the function argument.
506`aout_link_check_archive_element' calls `aout_link_check_ar_symbols'.
507If the latter decides to add the element (an element is only added if
508it provides a real, non-common, definition for a previously undefined
509or common symbol) it calls the `add_archive_element' callback and then
510`aout_link_check_archive_element' calls `aout_link_add_symbols' to
511actually add the symbols to the linker hash table.
512
513 The ECOFF back end is unusual in that it does not normally call
514`_bfd_generic_link_add_archive_symbols', because ECOFF archives already
515contain a hash table of symbols. The ECOFF back end searches the
516archive itself to avoid the overhead of creating a new hash table.
517
518
519File: bfd.info, Node: Performing the Final Link, Prev: Adding Symbols to the Hash Table, Up: Linker Functions
520
521Performing the final link
522-------------------------
523
524 When all the input files have been processed, the linker calls the
525`_bfd_final_link' entry point of the output BFD. This routine is
526responsible for producing the final output file, which has several
527aspects. It must relocate the contents of the input sections and copy
528the data into the output sections. It must build an output symbol
529table including any local symbols from the input files and the global
530symbols from the hash table. When producing relocateable output, it
531must modify the input relocs and write them into the output file.
532There may also be object format dependent work to be done.
533
534 The linker will also call the `write_object_contents' entry point
535when the BFD is closed. The two entry points must work together in
536order to produce the correct output file.
537
538 The details of how this works are inevitably dependent upon the
539specific object file format. The a.out `_bfd_final_link' routine is
540`NAME(aout,final_link)'.
541
542* Menu:
543
544* Information provided by the linker::
545* Relocating the section contents::
546* Writing the symbol table::
547
548
549File: bfd.info, Node: Information provided by the linker, Next: Relocating the section contents, Prev: Performing the Final Link, Up: Performing the Final Link
550
551Information provided by the linker
552..................................
553
554 Before the linker calls the `_bfd_final_link' entry point, it sets
555up some data structures for the function to use.
556
557 The `input_bfds' field of the `bfd_link_info' structure will point
558to a list of all the input files included in the link. These files are
559linked through the `link_next' field of the `bfd' structure.
560
561 Each section in the output file will have a list of `link_order'
562structures attached to the `link_order_head' field (the `link_order'
563structure is defined in `bfdlink.h'). These structures describe how to
564create the contents of the output section in terms of the contents of
565various input sections, fill constants, and, eventually, other types of
566information. They also describe relocs that must be created by the BFD
567backend, but do not correspond to any input file; this is used to
568support -Ur, which builds constructors while generating a relocateable
569object file.
570
571
572File: bfd.info, Node: Relocating the section contents, Next: Writing the symbol table, Prev: Information provided by the linker, Up: Performing the Final Link
573
574Relocating the section contents
575...............................
576
577 The `_bfd_final_link' function should look through the `link_order'
578structures attached to each section of the output file. Each
579`link_order' structure should either be handled specially, or it should
580be passed to the function `_bfd_default_link_order' which will do the
581right thing (`_bfd_default_link_order' is defined in `linker.c').
582
583 For efficiency, a `link_order' of type `bfd_indirect_link_order'
584whose associated section belongs to a BFD of the same format as the
585output BFD must be handled specially. This type of `link_order'
586describes part of an output section in terms of a section belonging to
587one of the input files. The `_bfd_final_link' function should read the
588contents of the section and any associated relocs, apply the relocs to
589the section contents, and write out the modified section contents. If
590performing a relocateable link, the relocs themselves must also be
591modified and written out.
592
593 The functions `_bfd_relocate_contents' and
594`_bfd_final_link_relocate' provide some general support for performing
595the actual relocations, notably overflow checking. Their arguments
596include information about the symbol the relocation is against and a
597`reloc_howto_type' argument which describes the relocation to perform.
598These functions are defined in `reloc.c'.
599
600 The a.out function which handles reading, relocating, and writing
601section contents is `aout_link_input_section'. The actual relocation
602is done in `aout_link_input_section_std' and
603`aout_link_input_section_ext'.
604
605
606File: bfd.info, Node: Writing the symbol table, Prev: Relocating the section contents, Up: Performing the Final Link
607
608Writing the symbol table
609........................
610
611 The `_bfd_final_link' function must gather all the symbols in the
612input files and write them out. It must also write out all the symbols
613in the global hash table. This must be controlled by the `strip' and
614`discard' fields of the `bfd_link_info' structure.
615
616 The local symbols of the input files will not have been entered into
617the linker hash table. The `_bfd_final_link' routine must consider
618each input file and include the symbols in the output file. It may be
619convenient to do this when looking through the `link_order' structures,
620or it may be done by stepping through the `input_bfds' list.
621
622 The `_bfd_final_link' routine must also traverse the global hash
623table to gather all the externally visible symbols. It is possible
624that most of the externally visible symbols may be written out when
625considering the symbols of each input file, but it is still necessary
626to traverse the hash table since the linker script may have defined
627some symbols that are not in any of the input files.
628
629 The `strip' field of the `bfd_link_info' structure controls which
630symbols are written out. The possible values are listed in
631`bfdlink.h'. If the value is `strip_some', then the `keep_hash' field
632of the `bfd_link_info' structure is a hash table of symbols to keep;
633each symbol should be looked up in this hash table, and only symbols
634which are present should be included in the output file.
635
636 If the `strip' field of the `bfd_link_info' structure permits local
637symbols to be written out, the `discard' field is used to further
638controls which local symbols are included in the output file. If the
639value is `discard_l', then all local symbols which begin with a certain
640prefix are discarded; this is controlled by the
641`bfd_is_local_label_name' entry point.
642
643 The a.out backend handles symbols by calling
644`aout_link_write_symbols' on each input BFD and then traversing the
645global hash table with the function `aout_link_write_other_symbol'. It
646builds a string table while writing out the symbols, which is written
647to the output file at the end of `NAME(aout,final_link)'.
648
649`bfd_link_split_section'
650........................
651
652 *Synopsis*
653 bfd_boolean bfd_link_split_section(bfd *abfd, asection *sec);
654 *Description*
655Return nonzero if SEC should be split during a reloceatable or final
656link.
657 #define bfd_link_split_section(abfd, sec) \
658 BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
659
660
661File: bfd.info, Node: Hash Tables, Prev: Linker Functions, Up: BFD front end
662
663Hash Tables
664===========
665
666 BFD provides a simple set of hash table functions. Routines are
667provided to initialize a hash table, to free a hash table, to look up a
668string in a hash table and optionally create an entry for it, and to
669traverse a hash table. There is currently no routine to delete an
670string from a hash table.
671
672 The basic hash table does not permit any data to be stored with a
673string. However, a hash table is designed to present a base class from
674which other types of hash tables may be derived. These derived types
675may store additional information with the string. Hash tables were
676implemented in this way, rather than simply providing a data pointer in
677a hash table entry, because they were designed for use by the linker
678back ends. The linker may create thousands of hash table entries, and
679the overhead of allocating private data and storing and following
680pointers becomes noticeable.
681
682 The basic hash table code is in `hash.c'.
683
684* Menu:
685
686* Creating and Freeing a Hash Table::
687* Looking Up or Entering a String::
688* Traversing a Hash Table::
689* Deriving a New Hash Table Type::
690
691
692File: bfd.info, Node: Creating and Freeing a Hash Table, Next: Looking Up or Entering a String, Prev: Hash Tables, Up: Hash Tables
693
694Creating and freeing a hash table
695---------------------------------
696
697 To create a hash table, create an instance of a `struct
698bfd_hash_table' (defined in `bfd.h') and call `bfd_hash_table_init' (if
699you know approximately how many entries you will need, the function
700`bfd_hash_table_init_n', which takes a SIZE argument, may be used).
701`bfd_hash_table_init' returns `FALSE' if some sort of error occurs.
702
703 The function `bfd_hash_table_init' take as an argument a function to
704use to create new entries. For a basic hash table, use the function
705`bfd_hash_newfunc'. *Note Deriving a New Hash Table Type::, for why
706you would want to use a different value for this argument.
707
708 `bfd_hash_table_init' will create an objalloc which will be used to
709allocate new entries. You may allocate memory on this objalloc using
710`bfd_hash_allocate'.
711
712 Use `bfd_hash_table_free' to free up all the memory that has been
713allocated for a hash table. This will not free up the `struct
714bfd_hash_table' itself, which you must provide.
715
716
717File: bfd.info, Node: Looking Up or Entering a String, Next: Traversing a Hash Table, Prev: Creating and Freeing a Hash Table, Up: Hash Tables
718
719Looking up or entering a string
720-------------------------------
721
722 The function `bfd_hash_lookup' is used both to look up a string in
723the hash table and to create a new entry.
724
725 If the CREATE argument is `FALSE', `bfd_hash_lookup' will look up a
726string. If the string is found, it will returns a pointer to a `struct
727bfd_hash_entry'. If the string is not found in the table
728`bfd_hash_lookup' will return `NULL'. You should not modify any of the
729fields in the returns `struct bfd_hash_entry'.
730
731 If the CREATE argument is `TRUE', the string will be entered into
732the hash table if it is not already there. Either way a pointer to a
733`struct bfd_hash_entry' will be returned, either to the existing
734structure or to a newly created one. In this case, a `NULL' return
735means that an error occurred.
736
737 If the CREATE argument is `TRUE', and a new entry is created, the
738COPY argument is used to decide whether to copy the string onto the
739hash table objalloc or not. If COPY is passed as `FALSE', you must be
740careful not to deallocate or modify the string as long as the hash table
741exists.
742
743
744File: bfd.info, Node: Traversing a Hash Table, Next: Deriving a New Hash Table Type, Prev: Looking Up or Entering a String, Up: Hash Tables
745
746Traversing a hash table
747-----------------------
748
749 The function `bfd_hash_traverse' may be used to traverse a hash
750table, calling a function on each element. The traversal is done in a
751random order.
752
753 `bfd_hash_traverse' takes as arguments a function and a generic
754`void *' pointer. The function is called with a hash table entry (a
755`struct bfd_hash_entry *') and the generic pointer passed to
756`bfd_hash_traverse'. The function must return a `boolean' value, which
757indicates whether to continue traversing the hash table. If the
758function returns `FALSE', `bfd_hash_traverse' will stop the traversal
759and return immediately.
760
761
762File: bfd.info, Node: Deriving a New Hash Table Type, Prev: Traversing a Hash Table, Up: Hash Tables
763
764Deriving a new hash table type
765------------------------------
766
767 Many uses of hash tables want to store additional information which
768each entry in the hash table. Some also find it convenient to store
769additional information with the hash table itself. This may be done
770using a derived hash table.
771
772 Since C is not an object oriented language, creating a derived hash
773table requires sticking together some boilerplate routines with a few
774differences specific to the type of hash table you want to create.
775
776 An example of a derived hash table is the linker hash table. The
777structures for this are defined in `bfdlink.h'. The functions are in
778`linker.c'.
779
780 You may also derive a hash table from an already derived hash table.
781For example, the a.out linker backend code uses a hash table derived
782from the linker hash table.
783
784* Menu:
785
786* Define the Derived Structures::
787* Write the Derived Creation Routine::
788* Write Other Derived Routines::
789
790
791File: bfd.info, Node: Define the Derived Structures, Next: Write the Derived Creation Routine, Prev: Deriving a New Hash Table Type, Up: Deriving a New Hash Table Type
792
793Define the derived structures
794.............................
795
796 You must define a structure for an entry in the hash table, and a
797structure for the hash table itself.
798
799 The first field in the structure for an entry in the hash table must
800be of the type used for an entry in the hash table you are deriving
801from. If you are deriving from a basic hash table this is `struct
802bfd_hash_entry', which is defined in `bfd.h'. The first field in the
803structure for the hash table itself must be of the type of the hash
804table you are deriving from itself. If you are deriving from a basic
805hash table, this is `struct bfd_hash_table'.
806
807 For example, the linker hash table defines `struct
808bfd_link_hash_entry' (in `bfdlink.h'). The first field, `root', is of
809type `struct bfd_hash_entry'. Similarly, the first field in `struct
810bfd_link_hash_table', `table', is of type `struct bfd_hash_table'.
811
812
813File: bfd.info, Node: Write the Derived Creation Routine, Next: Write Other Derived Routines, Prev: Define the Derived Structures, Up: Deriving a New Hash Table Type
814
815Write the derived creation routine
816..................................
817
818 You must write a routine which will create and initialize an entry
819in the hash table. This routine is passed as the function argument to
820`bfd_hash_table_init'.
821
822 In order to permit other hash tables to be derived from the hash
823table you are creating, this routine must be written in a standard way.
824
825 The first argument to the creation routine is a pointer to a hash
826table entry. This may be `NULL', in which case the routine should
827allocate the right amount of space. Otherwise the space has already
828been allocated by a hash table type derived from this one.
829
830 After allocating space, the creation routine must call the creation
831routine of the hash table type it is derived from, passing in a pointer
832to the space it just allocated. This will initialize any fields used
833by the base hash table.
834
835 Finally the creation routine must initialize any local fields for
836the new hash table type.
837
838 Here is a boilerplate example of a creation routine. FUNCTION_NAME
839is the name of the routine. ENTRY_TYPE is the type of an entry in the
840hash table you are creating. BASE_NEWFUNC is the name of the creation
841routine of the hash table type your hash table is derived from.
842
843 struct bfd_hash_entry *
844 FUNCTION_NAME (entry, table, string)
845 struct bfd_hash_entry *entry;
846 struct bfd_hash_table *table;
847 const char *string;
848 {
849 struct ENTRY_TYPE *ret = (ENTRY_TYPE *) entry;
850
851 /* Allocate the structure if it has not already been allocated by a
852 derived class. */
853 if (ret == (ENTRY_TYPE *) NULL)
854 {
855 ret = ((ENTRY_TYPE *)
856 bfd_hash_allocate (table, sizeof (ENTRY_TYPE)));
857 if (ret == (ENTRY_TYPE *) NULL)
858 return NULL;
859 }
860
861 /* Call the allocation method of the base class. */
862 ret = ((ENTRY_TYPE *)
863 BASE_NEWFUNC ((struct bfd_hash_entry *) ret, table, string));
864
865 /* Initialize the local fields here. */
866
867 return (struct bfd_hash_entry *) ret;
868 }
869 *Description*
870The creation routine for the linker hash table, which is in `linker.c',
871looks just like this example. FUNCTION_NAME is
872`_bfd_link_hash_newfunc'. ENTRY_TYPE is `struct bfd_link_hash_entry'.
873BASE_NEWFUNC is `bfd_hash_newfunc', the creation routine for a basic
874hash table.
875
876 `_bfd_link_hash_newfunc' also initializes the local fields in a
877linker hash table entry: `type', `written' and `next'.
878
879
880File: bfd.info, Node: Write Other Derived Routines, Prev: Write the Derived Creation Routine, Up: Deriving a New Hash Table Type
881
882Write other derived routines
883............................
884
885 You will want to write other routines for your new hash table, as
886well.
887
888 You will want an initialization routine which calls the
889initialization routine of the hash table you are deriving from and
890initializes any other local fields. For the linker hash table, this is
891`_bfd_link_hash_table_init' in `linker.c'.
892
893 You will want a lookup routine which calls the lookup routine of the
894hash table you are deriving from and casts the result. The linker hash
895table uses `bfd_link_hash_lookup' in `linker.c' (this actually takes an
896additional argument which it uses to decide how to return the looked up
897value).
898
899 You may want a traversal routine. This should just call the
900traversal routine of the hash table you are deriving from with
901appropriate casts. The linker hash table uses `bfd_link_hash_traverse'
902in `linker.c'.
903
904 These routines may simply be defined as macros. For example, the
905a.out backend linker hash table, which is derived from the linker hash
906table, uses macros for the lookup and traversal routines. These are
907`aout_link_hash_lookup' and `aout_link_hash_traverse' in aoutx.h.
908
909
910File: bfd.info, Node: BFD back ends, Next: GNU Free Documentation License, Prev: BFD front end, Up: Top
911
912BFD back ends
913*************
914
915* Menu:
916
917* What to Put Where::
918* aout :: a.out backends
919* coff :: coff backends
920* elf :: elf backends
921* mmo :: mmo backend
922
923
924File: bfd.info, Node: What to Put Where, Next: aout, Prev: BFD back ends, Up: BFD back ends
925
926 All of BFD lives in one directory.
927
928
929File: bfd.info, Node: aout, Next: coff, Prev: What to Put Where, Up: BFD back ends
930
931a.out backends
932==============
933
934 *Description*
935BFD supports a number of different flavours of a.out format, though the
936major differences are only the sizes of the structures on disk, and the
937shape of the relocation information.
938
939 The support is split into a basic support file `aoutx.h' and other
940files which derive functions from the base. One derivation file is
941`aoutf1.h' (for a.out flavour 1), and adds to the basic a.out functions
942support for sun3, sun4, 386 and 29k a.out files, to create a target
943jump vector for a specific target.
944
945 This information is further split out into more specific files for
946each machine, including `sunos.c' for sun3 and sun4, `newsos3.c' for
947the Sony NEWS, and `demo64.c' for a demonstration of a 64 bit a.out
948format.
949
950 The base file `aoutx.h' defines general mechanisms for reading and
951writing records to and from disk and various other methods which BFD
952requires. It is included by `aout32.c' and `aout64.c' to form the names
953`aout_32_swap_exec_header_in', `aout_64_swap_exec_header_in', etc.
954
955 As an example, this is what goes on to make the back end for a sun4,
956from `aout32.c':
957
958 #define ARCH_SIZE 32
959 #include "aoutx.h"
960
961 Which exports names:
962
963 ...
964 aout_32_canonicalize_reloc
965 aout_32_find_nearest_line
966 aout_32_get_lineno
967 aout_32_get_reloc_upper_bound
968 ...
969
970 from `sunos.c':
971
972 #define TARGET_NAME "a.out-sunos-big"
973 #define VECNAME sunos_big_vec
974 #include "aoutf1.h"
975
976 requires all the names from `aout32.c', and produces the jump vector
977
978 sunos_big_vec
979
980 The file `host-aout.c' is a special case. It is for a large set of
981hosts that use "more or less standard" a.out files, and for which
982cross-debugging is not interesting. It uses the standard 32-bit a.out
983support routines, but determines the file offsets and addresses of the
984text, data, and BSS sections, the machine architecture and machine
985type, and the entry point address, in a host-dependent manner. Once
986these values have been determined, generic code is used to handle the
987object file.
988
989 When porting it to run on a new system, you must supply:
990
991 HOST_PAGE_SIZE
992 HOST_SEGMENT_SIZE
993 HOST_MACHINE_ARCH (optional)
994 HOST_MACHINE_MACHINE (optional)
995 HOST_TEXT_START_ADDR
996 HOST_STACK_END_ADDR
997
998 in the file `../include/sys/h-XXX.h' (for your host). These values,
999plus the structures and macros defined in `a.out.h' on your host
1000system, will produce a BFD target that will access ordinary a.out files
1001on your host. To configure a new machine to use `host-aout.c', specify:
1002
1003 TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
1004 TDEPFILES= host-aout.o trad-core.o
1005
1006 in the `config/XXX.mt' file, and modify `configure.in' to use the
1007`XXX.mt' file (by setting "`bfd_target=XXX'") when your configuration
1008is selected.
1009
1010Relocations
1011-----------
1012
1013 *Description*
1014The file `aoutx.h' provides for both the _standard_ and _extended_
1015forms of a.out relocation records.
1016
1017 The standard records contain only an address, a symbol index, and a
1018type field. The extended records (used on 29ks and sparcs) also have a
1019full integer for an addend.
1020
1021Internal entry points
1022---------------------
1023
1024 *Description*
1025`aoutx.h' exports several routines for accessing the contents of an
1026a.out file, which are gathered and exported in turn by various format
1027specific files (eg sunos.c).
1028
1029`aout_SIZE_swap_exec_header_in'
1030...............................
1031
1032 *Synopsis*
1033 void aout_SIZE_swap_exec_header_in,
1034 (bfd *abfd,
1035 struct external_exec *raw_bytes,
1036 struct internal_exec *execp);
1037 *Description*
1038Swap the information in an executable header RAW_BYTES taken from a raw
1039byte stream memory image into the internal exec header structure EXECP.
1040
1041`aout_SIZE_swap_exec_header_out'
1042................................
1043
1044 *Synopsis*
1045 void aout_SIZE_swap_exec_header_out
1046 (bfd *abfd,
1047 struct internal_exec *execp,
1048 struct external_exec *raw_bytes);
1049 *Description*
1050Swap the information in an internal exec header structure EXECP into
1051the buffer RAW_BYTES ready for writing to disk.
1052
1053`aout_SIZE_some_aout_object_p'
1054..............................
1055
1056 *Synopsis*
1057 const bfd_target *aout_SIZE_some_aout_object_p
1058 (bfd *abfd,
1059 const bfd_target *(*callback_to_real_object_p) ());
1060 *Description*
1061Some a.out variant thinks that the file open in ABFD checking is an
1062a.out file. Do some more checking, and set up for access if it really
1063is. Call back to the calling environment's "finish up" function just
1064before returning, to handle any last-minute setup.
1065
1066`aout_SIZE_mkobject'
1067....................
1068
1069 *Synopsis*
1070 bfd_boolean aout_SIZE_mkobject, (bfd *abfd);
1071 *Description*
1072Initialize BFD ABFD for use with a.out files.
1073
1074`aout_SIZE_machine_type'
1075........................
1076
1077 *Synopsis*
1078 enum machine_type aout_SIZE_machine_type
1079 (enum bfd_architecture arch,
1080 unsigned long machine));
1081 *Description*
1082Keep track of machine architecture and machine type for a.out's. Return
1083the `machine_type' for a particular architecture and machine, or
1084`M_UNKNOWN' if that exact architecture and machine can't be represented
1085in a.out format.
1086
1087 If the architecture is understood, machine type 0 (default) is
1088always understood.
1089
1090`aout_SIZE_set_arch_mach'
1091.........................
1092
1093 *Synopsis*
1094 bfd_boolean aout_SIZE_set_arch_mach,
1095 (bfd *,
1096 enum bfd_architecture arch,
1097 unsigned long machine));
1098 *Description*
1099Set the architecture and the machine of the BFD ABFD to the values ARCH
1100and MACHINE. Verify that ABFD's format can support the architecture
1101required.
1102
1103`aout_SIZE_new_section_hook'
1104............................
1105
1106 *Synopsis*
1107 bfd_boolean aout_SIZE_new_section_hook,
1108 (bfd *abfd,
1109 asection *newsect));
1110 *Description*
1111Called by the BFD in response to a `bfd_make_section' request.
1112
Note: See TracBrowser for help on using the repository browser.