]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | \input texinfo |
219d1afa | 2 | @c Copyright (C) 1988-2018 Free Software Foundation, Inc. |
252b5132 RH |
3 | @setfilename bfdint.info |
4 | ||
5 | @settitle BFD Internals | |
6 | @iftex | |
7 | @titlepage | |
8 | @title{BFD Internals} | |
9 | @author{Ian Lance Taylor} | |
10 | @author{Cygnus Solutions} | |
11 | @page | |
12 | @end iftex | |
13 | ||
0e9517a9 | 14 | @copying |
f0757517 NC |
15 | This file documents the internals of the BFD library. |
16 | ||
219d1afa | 17 | Copyright @copyright{} 1988-2018 Free Software Foundation, Inc. |
f0757517 NC |
18 | Contributed by Cygnus Support. |
19 | ||
0e9517a9 NC |
20 | Permission is granted to copy, distribute and/or modify this document |
21 | under the terms of the GNU Free Documentation License, Version 1.1 or | |
22 | any later version published by the Free Software Foundation; with the | |
23 | Invariant Sections being ``GNU General Public License'' and ``Funding | |
24 | Free Software'', the Front-Cover texts being (a) (see below), and with | |
25 | the Back-Cover Texts being (b) (see below). A copy of the license is | |
26 | included in the section entitled ``GNU Free Documentation License''. | |
f0757517 | 27 | |
0e9517a9 | 28 | (a) The FSF's Front-Cover Text is: |
f0757517 | 29 | |
0e9517a9 NC |
30 | A GNU Manual |
31 | ||
32 | (b) The FSF's Back-Cover Text is: | |
33 | ||
34 | You have freedom to copy and modify this GNU Manual, like GNU | |
35 | software. Copies published by the Free Software Foundation raise | |
36 | funds for GNU development. | |
37 | @end copying | |
f0757517 | 38 | |
252b5132 RH |
39 | @node Top |
40 | @top BFD Internals | |
41 | @raisesections | |
42 | @cindex bfd internals | |
43 | ||
44 | This document describes some BFD internal information which may be | |
45 | helpful when working on BFD. It is very incomplete. | |
46 | ||
5b343f5a | 47 | This document is not updated regularly, and may be out of date. |
252b5132 RH |
48 | |
49 | The initial version of this document was written by Ian Lance Taylor | |
50 | @email{ian@@cygnus.com}. | |
51 | ||
52 | @menu | |
53 | * BFD overview:: BFD overview | |
54 | * BFD guidelines:: BFD programming guidelines | |
55 | * BFD target vector:: BFD target vector | |
56 | * BFD generated files:: BFD generated files | |
57 | * BFD multiple compilations:: Files compiled multiple times in BFD | |
58 | * BFD relocation handling:: BFD relocation handling | |
59 | * BFD ELF support:: BFD ELF support | |
60 | * BFD glossary:: Glossary | |
61 | * Index:: Index | |
62 | @end menu | |
63 | ||
64 | @node BFD overview | |
65 | @section BFD overview | |
66 | ||
67 | BFD is a library which provides a single interface to read and write | |
68 | object files, executables, archive files, and core files in any format. | |
69 | ||
70 | @menu | |
71 | * BFD library interfaces:: BFD library interfaces | |
72 | * BFD library users:: BFD library users | |
73 | * BFD view:: The BFD view of a file | |
74 | * BFD blindness:: BFD loses information | |
75 | @end menu | |
76 | ||
77 | @node BFD library interfaces | |
78 | @subsection BFD library interfaces | |
79 | ||
80 | One way to look at the BFD library is to divide it into four parts by | |
81 | type of interface. | |
82 | ||
83 | The first interface is the set of generic functions which programs using | |
84 | the BFD library will call. These generic function normally translate | |
85 | directly or indirectly into calls to routines which are specific to a | |
86 | particular object file format. Many of these generic functions are | |
87 | actually defined as macros in @file{bfd.h}. These functions comprise | |
88 | the official BFD interface. | |
89 | ||
90 | The second interface is the set of functions which appear in the target | |
91 | vectors. This is the bulk of the code in BFD. A target vector is a set | |
92 | of function pointers specific to a particular object file format. The | |
93 | target vector is used to implement the generic BFD functions. These | |
94 | functions are always called through the target vector, and are never | |
95 | called directly. The target vector is described in detail in @ref{BFD | |
96 | target vector}. The set of functions which appear in a particular | |
97 | target vector is often referred to as a BFD backend. | |
98 | ||
99 | The third interface is a set of oddball functions which are typically | |
100 | specific to a particular object file format, are not generic functions, | |
101 | and are called from outside of the BFD library. These are used as hooks | |
102 | by the linker and the assembler when a particular object file format | |
103 | requires some action which the BFD generic interface does not provide. | |
104 | These functions are typically declared in @file{bfd.h}, but in many | |
105 | cases they are only provided when BFD is configured with support for a | |
106 | particular object file format. These functions live in a grey area, and | |
107 | are not really part of the official BFD interface. | |
108 | ||
109 | The fourth interface is the set of BFD support functions which are | |
110 | called by the other BFD functions. These manage issues like memory | |
111 | allocation, error handling, file access, hash tables, swapping, and the | |
112 | like. These functions are never called from outside of the BFD library. | |
113 | ||
114 | @node BFD library users | |
115 | @subsection BFD library users | |
116 | ||
117 | Another way to look at the BFD library is to divide it into three parts | |
118 | by the manner in which it is used. | |
119 | ||
120 | The first use is to read an object file. The object file readers are | |
121 | programs like @samp{gdb}, @samp{nm}, @samp{objdump}, and @samp{objcopy}. | |
122 | These programs use BFD to view an object file in a generic form. The | |
123 | official BFD interface is normally fully adequate for these programs. | |
124 | ||
125 | The second use is to write an object file. The object file writers are | |
126 | programs like @samp{gas} and @samp{objcopy}. These programs use BFD to | |
127 | create an object file. The official BFD interface is normally adequate | |
128 | for these programs, but for some object file formats the assembler needs | |
129 | some additional hooks in order to set particular flags or other | |
130 | information. The official BFD interface includes functions to copy | |
131 | private information from one object file to another, and these functions | |
132 | are used by @samp{objcopy} to avoid information loss. | |
133 | ||
134 | The third use is to link object files. There is only one object file | |
135 | linker, @samp{ld}. Originally, @samp{ld} was an object file reader and | |
136 | an object file writer, and it did the link operation using the generic | |
137 | BFD structures. However, this turned out to be too slow and too memory | |
138 | intensive. | |
139 | ||
140 | The official BFD linker functions were written to permit specific BFD | |
141 | backends to perform the link without translating through the generic | |
142 | structures, in the normal case where all the input files and output file | |
143 | have the same object file format. Not all of the backends currently | |
144 | implement the new interface, and there are default linking functions | |
145 | within BFD which use the generic structures and which work with all | |
146 | backends. | |
147 | ||
148 | For several object file formats the linker needs additional hooks which | |
149 | are not provided by the official BFD interface, particularly for dynamic | |
150 | linking support. These functions are typically called from the linker | |
151 | emulation template. | |
152 | ||
153 | @node BFD view | |
154 | @subsection The BFD view of a file | |
155 | ||
156 | BFD uses generic structures to manage information. It translates data | |
157 | into the generic form when reading files, and out of the generic form | |
158 | when writing files. | |
159 | ||
160 | BFD describes a file as a pointer to the @samp{bfd} type. A @samp{bfd} | |
161 | is composed of the following elements. The BFD information can be | |
162 | displayed using the @samp{objdump} program with various options. | |
163 | ||
164 | @table @asis | |
165 | @item general information | |
166 | The object file format, a few general flags, the start address. | |
167 | @item architecture | |
168 | The architecture, including both a general processor type (m68k, MIPS | |
169 | etc.) and a specific machine number (m68000, R4000, etc.). | |
170 | @item sections | |
171 | A list of sections. | |
172 | @item symbols | |
173 | A symbol table. | |
174 | @end table | |
175 | ||
176 | BFD represents a section as a pointer to the @samp{asection} type. Each | |
177 | section has a name and a size. Most sections also have an associated | |
178 | block of data, known as the section contents. Sections also have | |
179 | associated flags, a virtual memory address, a load memory address, a | |
180 | required alignment, a list of relocations, and other miscellaneous | |
181 | information. | |
182 | ||
183 | BFD represents a relocation as a pointer to the @samp{arelent} type. A | |
184 | relocation describes an action which the linker must take to modify the | |
185 | section contents. Relocations have a symbol, an address, an addend, and | |
186 | a pointer to a howto structure which describes how to perform the | |
187 | relocation. For more information, see @ref{BFD relocation handling}. | |
188 | ||
189 | BFD represents a symbol as a pointer to the @samp{asymbol} type. A | |
190 | symbol has a name, a pointer to a section, an offset within that | |
191 | section, and some flags. | |
192 | ||
193 | Archive files do not have any sections or symbols. Instead, BFD | |
194 | represents an archive file as a file which contains a list of | |
195 | @samp{bfd}s. BFD also provides access to the archive symbol map, as a | |
196 | list of symbol names. BFD provides a function to return the @samp{bfd} | |
197 | within the archive which corresponds to a particular entry in the | |
198 | archive symbol map. | |
199 | ||
200 | @node BFD blindness | |
201 | @subsection BFD loses information | |
202 | ||
203 | Most object file formats have information which BFD can not represent in | |
204 | its generic form, at least as currently defined. | |
205 | ||
206 | There is often explicit information which BFD can not represent. For | |
207 | example, the COFF version stamp, or the ELF program segments. BFD | |
208 | provides special hooks to handle this information when copying, | |
209 | printing, or linking an object file. The BFD support for a particular | |
210 | object file format will normally store this information in private data | |
211 | and handle it using the special hooks. | |
212 | ||
213 | In some cases there is also implicit information which BFD can not | |
214 | represent. For example, the MIPS processor distinguishes small and | |
b45619c0 | 215 | large symbols, and requires that all small symbols be within 32K of the |
252b5132 RH |
216 | GP register. This means that the MIPS assembler must be able to mark |
217 | variables as either small or large, and the MIPS linker must know to put | |
218 | small symbols within range of the GP register. Since BFD can not | |
219 | represent this information, this means that the assembler and linker | |
220 | must have information that is specific to a particular object file | |
221 | format which is outside of the BFD library. | |
222 | ||
223 | This loss of information indicates areas where the BFD paradigm breaks | |
224 | down. It is not actually possible to represent the myriad differences | |
225 | among object file formats using a single generic interface, at least not | |
226 | in the manner which BFD does it today. | |
227 | ||
228 | Nevertheless, the BFD library does greatly simplify the task of dealing | |
229 | with object files, and particular problems caused by information loss | |
230 | can normally be solved using some sort of relatively constrained hook | |
231 | into the library. | |
232 | ||
233 | ||
234 | ||
235 | @node BFD guidelines | |
236 | @section BFD programming guidelines | |
237 | @cindex bfd programming guidelines | |
238 | @cindex programming guidelines for bfd | |
239 | @cindex guidelines, bfd programming | |
240 | ||
241 | There is a lot of poorly written and confusing code in BFD. New BFD | |
242 | code should be written to a higher standard. Merely because some BFD | |
243 | code is written in a particular manner does not mean that you should | |
244 | emulate it. | |
245 | ||
246 | Here are some general BFD programming guidelines: | |
247 | ||
248 | @itemize @bullet | |
249 | @item | |
250 | Follow the GNU coding standards. | |
251 | ||
252 | @item | |
253 | Avoid global variables. We ideally want BFD to be fully reentrant, so | |
254 | that it can be used in multiple threads. All uses of global or static | |
255 | variables interfere with that. Initialized constant variables are OK, | |
b45619c0 | 256 | and they should be explicitly marked with @samp{const}. Instead of global |
252b5132 RH |
257 | variables, use data attached to a BFD or to a linker hash table. |
258 | ||
259 | @item | |
260 | All externally visible functions should have names which start with | |
261 | @samp{bfd_}. All such functions should be declared in some header file, | |
262 | typically @file{bfd.h}. See, for example, the various declarations near | |
263 | the end of @file{bfd-in.h}, which mostly declare functions required by | |
264 | specific linker emulations. | |
265 | ||
266 | @item | |
267 | All functions which need to be visible from one file to another within | |
268 | BFD, but should not be visible outside of BFD, should start with | |
269 | @samp{_bfd_}. Although external names beginning with @samp{_} are | |
270 | prohibited by the ANSI standard, in practice this usage will always | |
271 | work, and it is required by the GNU coding standards. | |
272 | ||
273 | @item | |
274 | Always remember that people can compile using @samp{--enable-targets} to | |
275 | build several, or all, targets at once. It must be possible to link | |
276 | together the files for all targets. | |
277 | ||
278 | @item | |
279 | BFD code should compile with few or no warnings using @samp{gcc -Wall}. | |
280 | Some warnings are OK, like the absence of certain function declarations | |
281 | which may or may not be declared in system header files. Warnings about | |
282 | ambiguous expressions and the like should always be fixed. | |
283 | @end itemize | |
284 | ||
285 | @node BFD target vector | |
286 | @section BFD target vector | |
287 | @cindex bfd target vector | |
288 | @cindex target vector in bfd | |
289 | ||
290 | BFD supports multiple object file formats by using the @dfn{target | |
291 | vector}. This is simply a set of function pointers which implement | |
292 | behaviour that is specific to a particular object file format. | |
293 | ||
294 | In this section I list all of the entries in the target vector and | |
295 | describe what they do. | |
296 | ||
297 | @menu | |
298 | * BFD target vector miscellaneous:: Miscellaneous constants | |
299 | * BFD target vector swap:: Swapping functions | |
300 | * BFD target vector format:: Format type dependent functions | |
301 | * BFD_JUMP_TABLE macros:: BFD_JUMP_TABLE macros | |
302 | * BFD target vector generic:: Generic functions | |
303 | * BFD target vector copy:: Copy functions | |
304 | * BFD target vector core:: Core file support functions | |
305 | * BFD target vector archive:: Archive functions | |
306 | * BFD target vector symbols:: Symbol table functions | |
307 | * BFD target vector relocs:: Relocation support | |
308 | * BFD target vector write:: Output functions | |
309 | * BFD target vector link:: Linker functions | |
310 | * BFD target vector dynamic:: Dynamic linking information functions | |
311 | @end menu | |
312 | ||
313 | @node BFD target vector miscellaneous | |
314 | @subsection Miscellaneous constants | |
315 | ||
316 | The target vector starts with a set of constants. | |
317 | ||
318 | @table @samp | |
319 | @item name | |
320 | The name of the target vector. This is an arbitrary string. This is | |
321 | how the target vector is named in command line options for tools which | |
d9bc7a44 | 322 | use BFD, such as the @samp{--oformat} linker option. |
252b5132 RH |
323 | |
324 | @item flavour | |
325 | A general description of the type of target. The following flavours are | |
326 | currently defined: | |
327 | ||
328 | @table @samp | |
329 | @item bfd_target_unknown_flavour | |
330 | Undefined or unknown. | |
331 | @item bfd_target_aout_flavour | |
332 | a.out. | |
333 | @item bfd_target_coff_flavour | |
334 | COFF. | |
335 | @item bfd_target_ecoff_flavour | |
336 | ECOFF. | |
337 | @item bfd_target_elf_flavour | |
338 | ELF. | |
252b5132 RH |
339 | @item bfd_target_oasys_flavour |
340 | OASYS. | |
341 | @item bfd_target_tekhex_flavour | |
342 | Tektronix hex format. | |
343 | @item bfd_target_srec_flavour | |
344 | Motorola S-record format. | |
345 | @item bfd_target_ihex_flavour | |
346 | Intel hex format. | |
347 | @item bfd_target_som_flavour | |
348 | SOM (used on HP/UX). | |
c067354b NC |
349 | @item bfd_target_verilog_flavour |
350 | Verilog memory hex dump format. | |
252b5132 RH |
351 | @item bfd_target_os9k_flavour |
352 | os9000. | |
353 | @item bfd_target_versados_flavour | |
354 | VERSAdos. | |
355 | @item bfd_target_msdos_flavour | |
356 | MS-DOS. | |
357 | @item bfd_target_evax_flavour | |
358 | openVMS. | |
3c3bdf30 NC |
359 | @item bfd_target_mmo_flavour |
360 | Donald Knuth's MMIXware object format. | |
252b5132 RH |
361 | @end table |
362 | ||
363 | @item byteorder | |
364 | The byte order of data in the object file. One of | |
365 | @samp{BFD_ENDIAN_BIG}, @samp{BFD_ENDIAN_LITTLE}, or | |
366 | @samp{BFD_ENDIAN_UNKNOWN}. The latter would be used for a format such | |
367 | as S-records which do not record the architecture of the data. | |
368 | ||
369 | @item header_byteorder | |
370 | The byte order of header information in the object file. Normally the | |
371 | same as the @samp{byteorder} field, but there are certain cases where it | |
372 | may be different. | |
373 | ||
374 | @item object_flags | |
375 | Flags which may appear in the @samp{flags} field of a BFD with this | |
376 | format. | |
377 | ||
378 | @item section_flags | |
379 | Flags which may appear in the @samp{flags} field of a section within a | |
380 | BFD with this format. | |
381 | ||
382 | @item symbol_leading_char | |
383 | A character which the C compiler normally puts before a symbol. For | |
384 | example, an a.out compiler will typically generate the symbol | |
385 | @samp{_foo} for a function named @samp{foo} in the C source, in which | |
386 | case this field would be @samp{_}. If there is no such character, this | |
387 | field will be @samp{0}. | |
388 | ||
389 | @item ar_pad_char | |
390 | The padding character to use at the end of an archive name. Normally | |
391 | @samp{/}. | |
392 | ||
393 | @item ar_max_namelen | |
394 | The maximum length of a short name in an archive. Normally @samp{14}. | |
395 | ||
396 | @item backend_data | |
397 | A pointer to constant backend data. This is used by backends to store | |
398 | whatever additional information they need to distinguish similar target | |
399 | vectors which use the same sets of functions. | |
400 | @end table | |
401 | ||
402 | @node BFD target vector swap | |
403 | @subsection Swapping functions | |
404 | ||
d1d013c3 | 405 | Every target vector has function pointers used for swapping information |
252b5132 RH |
406 | in and out of the target representation. There are two sets of |
407 | functions: one for data information, and one for header information. | |
408 | Each set has three sizes: 64-bit, 32-bit, and 16-bit. Each size has | |
409 | three actual functions: put, get unsigned, and get signed. | |
410 | ||
411 | These 18 functions are used to convert data between the host and target | |
412 | representations. | |
413 | ||
414 | @node BFD target vector format | |
415 | @subsection Format type dependent functions | |
416 | ||
417 | Every target vector has three arrays of function pointers which are | |
418 | indexed by the BFD format type. The BFD format types are as follows: | |
419 | ||
420 | @table @samp | |
421 | @item bfd_unknown | |
422 | Unknown format. Not used for anything useful. | |
423 | @item bfd_object | |
424 | Object file. | |
425 | @item bfd_archive | |
426 | Archive file. | |
427 | @item bfd_core | |
428 | Core file. | |
429 | @end table | |
430 | ||
431 | The three arrays of function pointers are as follows: | |
432 | ||
433 | @table @samp | |
434 | @item bfd_check_format | |
435 | Check whether the BFD is of a particular format (object file, archive | |
436 | file, or core file) corresponding to this target vector. This is called | |
437 | by the @samp{bfd_check_format} function when examining an existing BFD. | |
438 | If the BFD matches the desired format, this function will initialize any | |
439 | format specific information such as the @samp{tdata} field of the BFD. | |
440 | This function must be called before any other BFD target vector function | |
441 | on a file opened for reading. | |
442 | ||
443 | @item bfd_set_format | |
444 | Set the format of a BFD which was created for output. This is called by | |
445 | the @samp{bfd_set_format} function after creating the BFD with a | |
446 | function such as @samp{bfd_openw}. This function will initialize format | |
447 | specific information required to write out an object file or whatever of | |
448 | the given format. This function must be called before any other BFD | |
449 | target vector function on a file opened for writing. | |
450 | ||
451 | @item bfd_write_contents | |
452 | Write out the contents of the BFD in the given format. This is called | |
453 | by @samp{bfd_close} function for a BFD opened for writing. This really | |
454 | should not be an array selected by format type, as the | |
455 | @samp{bfd_set_format} function provides all the required information. | |
456 | In fact, BFD will fail if a different format is used when calling | |
457 | through the @samp{bfd_set_format} and the @samp{bfd_write_contents} | |
458 | arrays; fortunately, since @samp{bfd_close} gets it right, this is a | |
459 | difficult error to make. | |
460 | @end table | |
461 | ||
462 | @node BFD_JUMP_TABLE macros | |
463 | @subsection @samp{BFD_JUMP_TABLE} macros | |
464 | @cindex @samp{BFD_JUMP_TABLE} | |
465 | ||
466 | Most target vectors are defined using @samp{BFD_JUMP_TABLE} macros. | |
467 | These macros take a single argument, which is a prefix applied to a set | |
468 | of functions. The macros are then used to initialize the fields in the | |
469 | target vector. | |
470 | ||
471 | For example, the @samp{BFD_JUMP_TABLE_RELOCS} macro defines three | |
472 | functions: @samp{_get_reloc_upper_bound}, @samp{_canonicalize_reloc}, | |
473 | and @samp{_bfd_reloc_type_lookup}. A reference like | |
474 | @samp{BFD_JUMP_TABLE_RELOCS (foo)} will expand into three functions | |
5398f678 | 475 | prefixed with @samp{foo}: @samp{foo_get_reloc_upper_bound}, etc. The |
252b5132 RH |
476 | @samp{BFD_JUMP_TABLE_RELOCS} macro will be placed such that those three |
477 | functions initialize the appropriate fields in the BFD target vector. | |
478 | ||
479 | This is done because it turns out that many different target vectors can | |
480 | share certain classes of functions. For example, archives are similar | |
481 | on most platforms, so most target vectors can use the same archive | |
482 | functions. Those target vectors all use @samp{BFD_JUMP_TABLE_ARCHIVE} | |
483 | with the same argument, calling a set of functions which is defined in | |
484 | @file{archive.c}. | |
485 | ||
486 | Each of the @samp{BFD_JUMP_TABLE} macros is mentioned below along with | |
487 | the description of the function pointers which it defines. The function | |
488 | pointers will be described using the name without the prefix which the | |
489 | @samp{BFD_JUMP_TABLE} macro defines. This name is normally the same as | |
490 | the name of the field in the target vector structure. Any differences | |
491 | will be noted. | |
492 | ||
493 | @node BFD target vector generic | |
494 | @subsection Generic functions | |
495 | @cindex @samp{BFD_JUMP_TABLE_GENERIC} | |
496 | ||
497 | The @samp{BFD_JUMP_TABLE_GENERIC} macro is used for some catch all | |
498 | functions which don't easily fit into other categories. | |
499 | ||
500 | @table @samp | |
501 | @item _close_and_cleanup | |
502 | Free any target specific information associated with the BFD. This is | |
503 | called when any BFD is closed (the @samp{bfd_write_contents} function | |
504 | mentioned earlier is only called for a BFD opened for writing). Most | |
505 | targets use @samp{bfd_alloc} to allocate all target specific | |
506 | information, and therefore don't have to do anything in this function. | |
507 | This function pointer is typically set to | |
508 | @samp{_bfd_generic_close_and_cleanup}, which simply returns true. | |
509 | ||
510 | @item _bfd_free_cached_info | |
511 | Free any cached information associated with the BFD which can be | |
512 | recreated later if necessary. This is used to reduce the memory | |
513 | consumption required by programs using BFD. This is normally called via | |
514 | the @samp{bfd_free_cached_info} macro. It is used by the default | |
515 | archive routines when computing the archive map. Most targets do not | |
516 | do anything special for this entry point, and just set it to | |
517 | @samp{_bfd_generic_free_cached_info}, which simply returns true. | |
518 | ||
519 | @item _new_section_hook | |
520 | This is called from @samp{bfd_make_section_anyway} whenever a new | |
521 | section is created. Most targets use it to initialize section specific | |
522 | information. This function is called whether or not the section | |
523 | corresponds to an actual section in an actual BFD. | |
524 | ||
525 | @item _get_section_contents | |
526 | Get the contents of a section. This is called from | |
527 | @samp{bfd_get_section_contents}. Most targets set this to | |
528 | @samp{_bfd_generic_get_section_contents}, which does a @samp{bfd_seek} | |
17c1c87f | 529 | based on the section's @samp{filepos} field and a @samp{bfd_bread}. The |
252b5132 RH |
530 | corresponding field in the target vector is named |
531 | @samp{_bfd_get_section_contents}. | |
532 | ||
533 | @item _get_section_contents_in_window | |
534 | Set a @samp{bfd_window} to hold the contents of a section. This is | |
535 | called from @samp{bfd_get_section_contents_in_window}. The | |
536 | @samp{bfd_window} idea never really caught on, and I don't think this is | |
537 | ever called. Pretty much all targets implement this as | |
538 | @samp{bfd_generic_get_section_contents_in_window}, which uses | |
539 | @samp{bfd_get_section_contents} to do the right thing. The | |
540 | corresponding field in the target vector is named | |
541 | @samp{_bfd_get_section_contents_in_window}. | |
542 | @end table | |
543 | ||
544 | @node BFD target vector copy | |
545 | @subsection Copy functions | |
546 | @cindex @samp{BFD_JUMP_TABLE_COPY} | |
547 | ||
548 | The @samp{BFD_JUMP_TABLE_COPY} macro is used for functions which are | |
549 | called when copying BFDs, and for a couple of functions which deal with | |
550 | internal BFD information. | |
551 | ||
552 | @table @samp | |
553 | @item _bfd_copy_private_bfd_data | |
554 | This is called when copying a BFD, via @samp{bfd_copy_private_bfd_data}. | |
555 | If the input and output BFDs have the same format, this will copy any | |
556 | private information over. This is called after all the section contents | |
557 | have been written to the output file. Only a few targets do anything in | |
558 | this function. | |
559 | ||
560 | @item _bfd_merge_private_bfd_data | |
561 | This is called when linking, via @samp{bfd_merge_private_bfd_data}. It | |
562 | gives the backend linker code a chance to set any special flags in the | |
563 | output file based on the contents of the input file. Only a few targets | |
564 | do anything in this function. | |
565 | ||
566 | @item _bfd_copy_private_section_data | |
567 | This is similar to @samp{_bfd_copy_private_bfd_data}, but it is called | |
568 | for each section, via @samp{bfd_copy_private_section_data}. This | |
569 | function is called before any section contents have been written. Only | |
570 | a few targets do anything in this function. | |
571 | ||
572 | @item _bfd_copy_private_symbol_data | |
573 | This is called via @samp{bfd_copy_private_symbol_data}, but I don't | |
574 | think anything actually calls it. If it were defined, it could be used | |
575 | to copy private symbol data from one BFD to another. However, most BFDs | |
576 | store extra symbol information by allocating space which is larger than | |
577 | the @samp{asymbol} structure and storing private information in the | |
578 | extra space. Since @samp{objcopy} and other programs copy symbol | |
579 | information by copying pointers to @samp{asymbol} structures, the | |
580 | private symbol information is automatically copied as well. Most | |
581 | targets do not do anything in this function. | |
582 | ||
583 | @item _bfd_set_private_flags | |
584 | This is called via @samp{bfd_set_private_flags}. It is basically a hook | |
585 | for the assembler to set magic information. For example, the PowerPC | |
586 | ELF assembler uses it to set flags which appear in the e_flags field of | |
587 | the ELF header. Most targets do not do anything in this function. | |
588 | ||
589 | @item _bfd_print_private_bfd_data | |
590 | This is called by @samp{objdump} when the @samp{-p} option is used. It | |
591 | is called via @samp{bfd_print_private_data}. It prints any interesting | |
592 | information about the BFD which can not be otherwise represented by BFD | |
593 | and thus can not be printed by @samp{objdump}. Most targets do not do | |
594 | anything in this function. | |
595 | @end table | |
596 | ||
597 | @node BFD target vector core | |
598 | @subsection Core file support functions | |
599 | @cindex @samp{BFD_JUMP_TABLE_CORE} | |
600 | ||
601 | The @samp{BFD_JUMP_TABLE_CORE} macro is used for functions which deal | |
602 | with core files. Obviously, these functions only do something | |
603 | interesting for targets which have core file support. | |
604 | ||
605 | @table @samp | |
606 | @item _core_file_failing_command | |
607 | Given a core file, this returns the command which was run to produce the | |
608 | core file. | |
609 | ||
610 | @item _core_file_failing_signal | |
611 | Given a core file, this returns the signal number which produced the | |
612 | core file. | |
613 | ||
614 | @item _core_file_matches_executable_p | |
615 | Given a core file and a BFD for an executable, this returns whether the | |
616 | core file was generated by the executable. | |
617 | @end table | |
618 | ||
619 | @node BFD target vector archive | |
620 | @subsection Archive functions | |
621 | @cindex @samp{BFD_JUMP_TABLE_ARCHIVE} | |
622 | ||
623 | The @samp{BFD_JUMP_TABLE_ARCHIVE} macro is used for functions which deal | |
624 | with archive files. Most targets use COFF style archive files | |
625 | (including ELF targets), and these use @samp{_bfd_archive_coff} as the | |
626 | argument to @samp{BFD_JUMP_TABLE_ARCHIVE}. Some targets use BSD/a.out | |
627 | style archives, and these use @samp{_bfd_archive_bsd}. (The main | |
628 | difference between BSD and COFF archives is the format of the archive | |
629 | symbol table). Targets with no archive support use | |
630 | @samp{_bfd_noarchive}. Finally, a few targets have unusual archive | |
631 | handling. | |
632 | ||
633 | @table @samp | |
634 | @item _slurp_armap | |
635 | Read in the archive symbol table, storing it in private BFD data. This | |
636 | is normally called from the archive @samp{check_format} routine. The | |
637 | corresponding field in the target vector is named | |
638 | @samp{_bfd_slurp_armap}. | |
639 | ||
640 | @item _slurp_extended_name_table | |
641 | Read in the extended name table from the archive, if there is one, | |
642 | storing it in private BFD data. This is normally called from the | |
643 | archive @samp{check_format} routine. The corresponding field in the | |
644 | target vector is named @samp{_bfd_slurp_extended_name_table}. | |
645 | ||
646 | @item construct_extended_name_table | |
647 | Build and return an extended name table if one is needed to write out | |
648 | the archive. This also adjusts the archive headers to refer to the | |
649 | extended name table appropriately. This is normally called from the | |
650 | archive @samp{write_contents} routine. The corresponding field in the | |
651 | target vector is named @samp{_bfd_construct_extended_name_table}. | |
652 | ||
653 | @item _truncate_arname | |
654 | This copies a file name into an archive header, truncating it as | |
655 | required. It is normally called from the archive @samp{write_contents} | |
656 | routine. This function is more interesting in targets which do not | |
657 | support extended name tables, but I think the GNU @samp{ar} program | |
658 | always uses extended name tables anyhow. The corresponding field in the | |
659 | target vector is named @samp{_bfd_truncate_arname}. | |
660 | ||
661 | @item _write_armap | |
17c1c87f | 662 | Write out the archive symbol table using calls to @samp{bfd_bwrite}. |
252b5132 RH |
663 | This is normally called from the archive @samp{write_contents} routine. |
664 | The corresponding field in the target vector is named @samp{write_armap} | |
665 | (no leading underscore). | |
666 | ||
667 | @item _read_ar_hdr | |
668 | Read and parse an archive header. This handles expanding the archive | |
669 | header name into the real file name using the extended name table. This | |
670 | is called by routines which read the archive symbol table or the archive | |
671 | itself. The corresponding field in the target vector is named | |
672 | @samp{_bfd_read_ar_hdr_fn}. | |
673 | ||
674 | @item _openr_next_archived_file | |
675 | Given an archive and a BFD representing a file stored within the | |
676 | archive, return a BFD for the next file in the archive. This is called | |
677 | via @samp{bfd_openr_next_archived_file}. The corresponding field in the | |
678 | target vector is named @samp{openr_next_archived_file} (no leading | |
679 | underscore). | |
680 | ||
681 | @item _get_elt_at_index | |
682 | Given an archive and an index, return a BFD for the file in the archive | |
683 | corresponding to that entry in the archive symbol table. This is called | |
684 | via @samp{bfd_get_elt_at_index}. The corresponding field in the target | |
685 | vector is named @samp{_bfd_get_elt_at_index}. | |
686 | ||
687 | @item _generic_stat_arch_elt | |
688 | Do a stat on an element of an archive, returning information read from | |
689 | the archive header (modification time, uid, gid, file mode, size). This | |
690 | is called via @samp{bfd_stat_arch_elt}. The corresponding field in the | |
691 | target vector is named @samp{_bfd_stat_arch_elt}. | |
692 | ||
693 | @item _update_armap_timestamp | |
694 | After the entire contents of an archive have been written out, update | |
695 | the timestamp of the archive symbol table to be newer than that of the | |
696 | file. This is required for a.out style archives. This is normally | |
697 | called by the archive @samp{write_contents} routine. The corresponding | |
698 | field in the target vector is named @samp{_bfd_update_armap_timestamp}. | |
699 | @end table | |
700 | ||
701 | @node BFD target vector symbols | |
702 | @subsection Symbol table functions | |
703 | @cindex @samp{BFD_JUMP_TABLE_SYMBOLS} | |
704 | ||
705 | The @samp{BFD_JUMP_TABLE_SYMBOLS} macro is used for functions which deal | |
706 | with symbols. | |
707 | ||
708 | @table @samp | |
709 | @item _get_symtab_upper_bound | |
710 | Return a sensible upper bound on the amount of memory which will be | |
711 | required to read the symbol table. In practice most targets return the | |
712 | amount of memory required to hold @samp{asymbol} pointers for all the | |
713 | symbols plus a trailing @samp{NULL} entry, and store the actual symbol | |
714 | information in BFD private data. This is called via | |
715 | @samp{bfd_get_symtab_upper_bound}. The corresponding field in the | |
716 | target vector is named @samp{_bfd_get_symtab_upper_bound}. | |
717 | ||
6cee3f79 | 718 | @item _canonicalize_symtab |
252b5132 RH |
719 | Read in the symbol table. This is called via |
720 | @samp{bfd_canonicalize_symtab}. The corresponding field in the target | |
721 | vector is named @samp{_bfd_canonicalize_symtab}. | |
722 | ||
723 | @item _make_empty_symbol | |
724 | Create an empty symbol for the BFD. This is needed because most targets | |
725 | store extra information with each symbol by allocating a structure | |
726 | larger than an @samp{asymbol} and storing the extra information at the | |
727 | end. This function will allocate the right amount of memory, and return | |
728 | what looks like a pointer to an empty @samp{asymbol}. This is called | |
729 | via @samp{bfd_make_empty_symbol}. The corresponding field in the target | |
730 | vector is named @samp{_bfd_make_empty_symbol}. | |
731 | ||
732 | @item _print_symbol | |
733 | Print information about the symbol. This is called via | |
734 | @samp{bfd_print_symbol}. One of the arguments indicates what sort of | |
735 | information should be printed: | |
736 | ||
737 | @table @samp | |
738 | @item bfd_print_symbol_name | |
739 | Just print the symbol name. | |
740 | @item bfd_print_symbol_more | |
741 | Print the symbol name and some interesting flags. I don't think | |
742 | anything actually uses this. | |
743 | @item bfd_print_symbol_all | |
744 | Print all information about the symbol. This is used by @samp{objdump} | |
745 | when run with the @samp{-t} option. | |
746 | @end table | |
747 | The corresponding field in the target vector is named | |
748 | @samp{_bfd_print_symbol}. | |
749 | ||
750 | @item _get_symbol_info | |
751 | Return a standard set of information about the symbol. This is called | |
752 | via @samp{bfd_symbol_info}. The corresponding field in the target | |
753 | vector is named @samp{_bfd_get_symbol_info}. | |
754 | ||
755 | @item _bfd_is_local_label_name | |
756 | Return whether the given string would normally represent the name of a | |
757 | local label. This is called via @samp{bfd_is_local_label} and | |
758 | @samp{bfd_is_local_label_name}. Local labels are normally discarded by | |
759 | the assembler. In the linker, this defines the difference between the | |
760 | @samp{-x} and @samp{-X} options. | |
761 | ||
762 | @item _get_lineno | |
763 | Return line number information for a symbol. This is only meaningful | |
764 | for a COFF target. This is called when writing out COFF line numbers. | |
765 | ||
766 | @item _find_nearest_line | |
767 | Given an address within a section, use the debugging information to find | |
768 | the matching file name, function name, and line number, if any. This is | |
769 | called via @samp{bfd_find_nearest_line}. The corresponding field in the | |
770 | target vector is named @samp{_bfd_find_nearest_line}. | |
771 | ||
772 | @item _bfd_make_debug_symbol | |
773 | Make a debugging symbol. This is only meaningful for a COFF target, | |
774 | where it simply returns a symbol which will be placed in the | |
775 | @samp{N_DEBUG} section when it is written out. This is called via | |
776 | @samp{bfd_make_debug_symbol}. | |
777 | ||
778 | @item _read_minisymbols | |
779 | Minisymbols are used to reduce the memory requirements of programs like | |
780 | @samp{nm}. A minisymbol is a cookie pointing to internal symbol | |
781 | information which the caller can use to extract complete symbol | |
782 | information. This permits BFD to not convert all the symbols into | |
783 | generic form, but to instead convert them one at a time. This is called | |
784 | via @samp{bfd_read_minisymbols}. Most targets do not implement this, | |
785 | and just use generic support which is based on using standard | |
786 | @samp{asymbol} structures. | |
787 | ||
788 | @item _minisymbol_to_symbol | |
789 | Convert a minisymbol to a standard @samp{asymbol}. This is called via | |
790 | @samp{bfd_minisymbol_to_symbol}. | |
791 | @end table | |
792 | ||
793 | @node BFD target vector relocs | |
794 | @subsection Relocation support | |
795 | @cindex @samp{BFD_JUMP_TABLE_RELOCS} | |
796 | ||
797 | The @samp{BFD_JUMP_TABLE_RELOCS} macro is used for functions which deal | |
798 | with relocations. | |
799 | ||
800 | @table @samp | |
801 | @item _get_reloc_upper_bound | |
802 | Return a sensible upper bound on the amount of memory which will be | |
803 | required to read the relocations for a section. In practice most | |
804 | targets return the amount of memory required to hold @samp{arelent} | |
805 | pointers for all the relocations plus a trailing @samp{NULL} entry, and | |
806 | store the actual relocation information in BFD private data. This is | |
807 | called via @samp{bfd_get_reloc_upper_bound}. | |
808 | ||
809 | @item _canonicalize_reloc | |
810 | Return the relocation information for a section. This is called via | |
811 | @samp{bfd_canonicalize_reloc}. The corresponding field in the target | |
812 | vector is named @samp{_bfd_canonicalize_reloc}. | |
813 | ||
814 | @item _bfd_reloc_type_lookup | |
815 | Given a relocation code, return the corresponding howto structure | |
816 | (@pxref{BFD relocation codes}). This is called via | |
817 | @samp{bfd_reloc_type_lookup}. The corresponding field in the target | |
818 | vector is named @samp{reloc_type_lookup}. | |
819 | @end table | |
820 | ||
821 | @node BFD target vector write | |
822 | @subsection Output functions | |
823 | @cindex @samp{BFD_JUMP_TABLE_WRITE} | |
824 | ||
825 | The @samp{BFD_JUMP_TABLE_WRITE} macro is used for functions which deal | |
826 | with writing out a BFD. | |
827 | ||
828 | @table @samp | |
829 | @item _set_arch_mach | |
830 | Set the architecture and machine number for a BFD. This is called via | |
831 | @samp{bfd_set_arch_mach}. Most targets implement this by calling | |
832 | @samp{bfd_default_set_arch_mach}. The corresponding field in the target | |
833 | vector is named @samp{_bfd_set_arch_mach}. | |
834 | ||
835 | @item _set_section_contents | |
836 | Write out the contents of a section. This is called via | |
837 | @samp{bfd_set_section_contents}. The corresponding field in the target | |
838 | vector is named @samp{_bfd_set_section_contents}. | |
839 | @end table | |
840 | ||
841 | @node BFD target vector link | |
842 | @subsection Linker functions | |
843 | @cindex @samp{BFD_JUMP_TABLE_LINK} | |
844 | ||
845 | The @samp{BFD_JUMP_TABLE_LINK} macro is used for functions called by the | |
846 | linker. | |
847 | ||
848 | @table @samp | |
849 | @item _sizeof_headers | |
850 | Return the size of the header information required for a BFD. This is | |
851 | used to implement the @samp{SIZEOF_HEADERS} linker script function. It | |
852 | is normally used to align the first section at an efficient position on | |
853 | the page. This is called via @samp{bfd_sizeof_headers}. The | |
854 | corresponding field in the target vector is named | |
855 | @samp{_bfd_sizeof_headers}. | |
856 | ||
857 | @item _bfd_get_relocated_section_contents | |
858 | Read the contents of a section and apply the relocation information. | |
1049f94e | 859 | This handles both a final link and a relocatable link; in the latter |
252b5132 RH |
860 | case, it adjust the relocation information as well. This is called via |
861 | @samp{bfd_get_relocated_section_contents}. Most targets implement it by | |
862 | calling @samp{bfd_generic_get_relocated_section_contents}. | |
863 | ||
864 | @item _bfd_relax_section | |
865 | Try to use relaxation to shrink the size of a section. This is called | |
866 | by the linker when the @samp{-relax} option is used. This is called via | |
867 | @samp{bfd_relax_section}. Most targets do not support any sort of | |
868 | relaxation. | |
869 | ||
870 | @item _bfd_link_hash_table_create | |
871 | Create the symbol hash table to use for the linker. This linker hook | |
872 | permits the backend to control the size and information of the elements | |
873 | in the linker symbol hash table. This is called via | |
874 | @samp{bfd_link_hash_table_create}. | |
875 | ||
876 | @item _bfd_link_add_symbols | |
877 | Given an object file or an archive, add all symbols into the linker | |
878 | symbol hash table. Use callbacks to the linker to include archive | |
879 | elements in the link. This is called via @samp{bfd_link_add_symbols}. | |
880 | ||
881 | @item _bfd_final_link | |
882 | Finish the linking process. The linker calls this hook after all of the | |
883 | input files have been read, when it is ready to finish the link and | |
884 | generate the output file. This is called via @samp{bfd_final_link}. | |
885 | ||
886 | @item _bfd_link_split_section | |
887 | I don't know what this is for. Nothing seems to call it. The only | |
888 | non-trivial definition is in @file{som.c}. | |
889 | @end table | |
890 | ||
891 | @node BFD target vector dynamic | |
892 | @subsection Dynamic linking information functions | |
893 | @cindex @samp{BFD_JUMP_TABLE_DYNAMIC} | |
894 | ||
895 | The @samp{BFD_JUMP_TABLE_DYNAMIC} macro is used for functions which read | |
896 | dynamic linking information. | |
897 | ||
898 | @table @samp | |
899 | @item _get_dynamic_symtab_upper_bound | |
900 | Return a sensible upper bound on the amount of memory which will be | |
901 | required to read the dynamic symbol table. In practice most targets | |
902 | return the amount of memory required to hold @samp{asymbol} pointers for | |
903 | all the symbols plus a trailing @samp{NULL} entry, and store the actual | |
904 | symbol information in BFD private data. This is called via | |
905 | @samp{bfd_get_dynamic_symtab_upper_bound}. The corresponding field in | |
906 | the target vector is named @samp{_bfd_get_dynamic_symtab_upper_bound}. | |
907 | ||
908 | @item _canonicalize_dynamic_symtab | |
909 | Read the dynamic symbol table. This is called via | |
910 | @samp{bfd_canonicalize_dynamic_symtab}. The corresponding field in the | |
911 | target vector is named @samp{_bfd_canonicalize_dynamic_symtab}. | |
912 | ||
913 | @item _get_dynamic_reloc_upper_bound | |
914 | Return a sensible upper bound on the amount of memory which will be | |
915 | required to read the dynamic relocations. In practice most targets | |
916 | return the amount of memory required to hold @samp{arelent} pointers for | |
917 | all the relocations plus a trailing @samp{NULL} entry, and store the | |
918 | actual relocation information in BFD private data. This is called via | |
919 | @samp{bfd_get_dynamic_reloc_upper_bound}. The corresponding field in | |
920 | the target vector is named @samp{_bfd_get_dynamic_reloc_upper_bound}. | |
921 | ||
922 | @item _canonicalize_dynamic_reloc | |
923 | Read the dynamic relocations. This is called via | |
924 | @samp{bfd_canonicalize_dynamic_reloc}. The corresponding field in the | |
925 | target vector is named @samp{_bfd_canonicalize_dynamic_reloc}. | |
926 | @end table | |
927 | ||
928 | @node BFD generated files | |
929 | @section BFD generated files | |
930 | @cindex generated files in bfd | |
931 | @cindex bfd generated files | |
932 | ||
933 | BFD contains several automatically generated files. This section | |
934 | describes them. Some files are created at configure time, when you | |
935 | configure BFD. Some files are created at make time, when you build | |
afdaa25f | 936 | BFD. Some files are automatically rebuilt at make time, but only if |
252b5132 RH |
937 | you configure with the @samp{--enable-maintainer-mode} option. Some |
938 | files live in the object directory---the directory from which you run | |
939 | configure---and some live in the source directory. All files that live | |
20cef68c | 940 | in the source directory are checked into the git repository. |
252b5132 RH |
941 | |
942 | @table @file | |
943 | @item bfd.h | |
944 | @cindex @file{bfd.h} | |
945 | @cindex @file{bfd-in3.h} | |
946 | Lives in the object directory. Created at make time from | |
947 | @file{bfd-in2.h} via @file{bfd-in3.h}. @file{bfd-in3.h} is created at | |
948 | configure time from @file{bfd-in2.h}. There are automatic dependencies | |
949 | to rebuild @file{bfd-in3.h} and hence @file{bfd.h} if @file{bfd-in2.h} | |
950 | changes, so you can normally ignore @file{bfd-in3.h}, and just think | |
951 | about @file{bfd-in2.h} and @file{bfd.h}. | |
952 | ||
953 | @file{bfd.h} is built by replacing a few strings in @file{bfd-in2.h}. | |
954 | To see them, search for @samp{@@} in @file{bfd-in2.h}. They mainly | |
955 | control whether BFD is built for a 32 bit target or a 64 bit target. | |
956 | ||
957 | @item bfd-in2.h | |
958 | @cindex @file{bfd-in2.h} | |
959 | Lives in the source directory. Created from @file{bfd-in.h} and several | |
960 | other BFD source files. If you configure with the | |
961 | @samp{--enable-maintainer-mode} option, @file{bfd-in2.h} is rebuilt | |
962 | automatically when a source file changes. | |
963 | ||
964 | @item elf32-target.h | |
965 | @itemx elf64-target.h | |
966 | @cindex @file{elf32-target.h} | |
967 | @cindex @file{elf64-target.h} | |
968 | Live in the object directory. Created from @file{elfxx-target.h}. | |
969 | These files are versions of @file{elfxx-target.h} customized for either | |
970 | a 32 bit ELF target or a 64 bit ELF target. | |
971 | ||
972 | @item libbfd.h | |
973 | @cindex @file{libbfd.h} | |
974 | Lives in the source directory. Created from @file{libbfd-in.h} and | |
975 | several other BFD source files. If you configure with the | |
976 | @samp{--enable-maintainer-mode} option, @file{libbfd.h} is rebuilt | |
977 | automatically when a source file changes. | |
978 | ||
979 | @item libcoff.h | |
980 | @cindex @file{libcoff.h} | |
981 | Lives in the source directory. Created from @file{libcoff-in.h} and | |
982 | @file{coffcode.h}. If you configure with the | |
983 | @samp{--enable-maintainer-mode} option, @file{libcoff.h} is rebuilt | |
984 | automatically when a source file changes. | |
985 | ||
986 | @item targmatch.h | |
987 | @cindex @file{targmatch.h} | |
988 | Lives in the object directory. Created at make time from | |
989 | @file{config.bfd}. This file is used to map configuration triplets into | |
990 | BFD target vector variable names at run time. | |
991 | @end table | |
992 | ||
993 | @node BFD multiple compilations | |
994 | @section Files compiled multiple times in BFD | |
995 | Several files in BFD are compiled multiple times. By this I mean that | |
996 | there are header files which contain function definitions. These header | |
997 | files are included by other files, and thus the functions are compiled | |
998 | once per file which includes them. | |
999 | ||
1000 | Preprocessor macros are used to control the compilation, so that each | |
1001 | time the files are compiled the resulting functions are slightly | |
1002 | different. Naturally, if they weren't different, there would be no | |
1003 | reason to compile them multiple times. | |
1004 | ||
1005 | This is a not a particularly good programming technique, and future BFD | |
1006 | work should avoid it. | |
1007 | ||
1008 | @itemize @bullet | |
1009 | @item | |
1010 | Since this technique is rarely used, even experienced C programmers find | |
1011 | it confusing. | |
1012 | ||
1013 | @item | |
1014 | It is difficult to debug programs which use BFD, since there is no way | |
1015 | to describe which version of a particular function you are looking at. | |
1016 | ||
1017 | @item | |
1018 | Programs which use BFD wind up incorporating two or more slightly | |
1019 | different versions of the same function, which wastes space in the | |
1020 | executable. | |
1021 | ||
1022 | @item | |
1023 | This technique is never required nor is it especially efficient. It is | |
1024 | always possible to use statically initialized structures holding | |
1025 | function pointers and magic constants instead. | |
1026 | @end itemize | |
1027 | ||
1028 | The following is a list of the files which are compiled multiple times. | |
1029 | ||
1030 | @table @file | |
1031 | @item aout-target.h | |
1032 | @cindex @file{aout-target.h} | |
1033 | Describes a few functions and the target vector for a.out targets. This | |
1034 | is used by individual a.out targets with different definitions of | |
1035 | @samp{N_TXTADDR} and similar a.out macros. | |
1036 | ||
1037 | @item aoutf1.h | |
1038 | @cindex @file{aoutf1.h} | |
1039 | Implements standard SunOS a.out files. In principle it supports 64 bit | |
1040 | a.out targets based on the preprocessor macro @samp{ARCH_SIZE}, but | |
1041 | since all known a.out targets are 32 bits, this code may or may not | |
1042 | work. This file is only included by a few other files, and it is | |
1043 | difficult to justify its existence. | |
1044 | ||
1045 | @item aoutx.h | |
1046 | @cindex @file{aoutx.h} | |
1047 | Implements basic a.out support routines. This file can be compiled for | |
1048 | either 32 or 64 bit support. Since all known a.out targets are 32 bits, | |
1049 | the 64 bit support may or may not work. I believe the original | |
1050 | intention was that this file would only be included by @samp{aout32.c} | |
1051 | and @samp{aout64.c}, and that other a.out targets would simply refer to | |
1052 | the functions it defined. Unfortunately, some other a.out targets | |
1053 | started including it directly, leading to a somewhat confused state of | |
1054 | affairs. | |
1055 | ||
1056 | @item coffcode.h | |
1057 | @cindex @file{coffcode.h} | |
1058 | Implements basic COFF support routines. This file is included by every | |
1059 | COFF target. It implements code which handles COFF magic numbers as | |
1060 | well as various hook functions called by the generic COFF functions in | |
1061 | @file{coffgen.c}. This file is controlled by a number of different | |
1062 | macros, and more are added regularly. | |
1063 | ||
1064 | @item coffswap.h | |
1065 | @cindex @file{coffswap.h} | |
1066 | Implements COFF swapping routines. This file is included by | |
1067 | @file{coffcode.h}, and thus by every COFF target. It implements the | |
1068 | routines which swap COFF structures between internal and external | |
1069 | format. The main control for this file is the external structure | |
1070 | definitions in the files in the @file{include/coff} directory. A COFF | |
1071 | target file will include one of those files before including | |
1072 | @file{coffcode.h} and thus @file{coffswap.h}. There are a few other | |
1073 | macros which affect @file{coffswap.h} as well, mostly describing whether | |
1074 | certain fields are present in the external structures. | |
1075 | ||
1076 | @item ecoffswap.h | |
1077 | @cindex @file{ecoffswap.h} | |
1078 | Implements ECOFF swapping routines. This is like @file{coffswap.h}, but | |
1079 | for ECOFF. It is included by the ECOFF target files (of which there are | |
1080 | only two). The control is the preprocessor macro @samp{ECOFF_32} or | |
1081 | @samp{ECOFF_64}. | |
1082 | ||
1083 | @item elfcode.h | |
1084 | @cindex @file{elfcode.h} | |
1085 | Implements ELF functions that use external structure definitions. This | |
1086 | file is included by two other files: @file{elf32.c} and @file{elf64.c}. | |
1087 | It is controlled by the @samp{ARCH_SIZE} macro which is defined to be | |
1088 | @samp{32} or @samp{64} before including it. The @samp{NAME} macro is | |
1089 | used internally to give the functions different names for the two target | |
1090 | sizes. | |
1091 | ||
1092 | @item elfcore.h | |
1093 | @cindex @file{elfcore.h} | |
1094 | Like @file{elfcode.h}, but for functions that are specific to ELF core | |
1095 | files. This is included only by @file{elfcode.h}. | |
1096 | ||
252b5132 RH |
1097 | @item elfxx-target.h |
1098 | @cindex @file{elfxx-target.h} | |
1099 | This file is the source for the generated files @file{elf32-target.h} | |
1100 | and @file{elf64-target.h}, one of which is included by every ELF target. | |
1101 | It defines the ELF target vector. | |
1102 | ||
1103 | @item freebsd.h | |
1104 | @cindex @file{freebsd.h} | |
1105 | Presumably intended to be included by all FreeBSD targets, but in fact | |
1106 | there is only one such target, @samp{i386-freebsd}. This defines a | |
1107 | function used to set the right magic number for FreeBSD, as well as | |
1108 | various macros, and includes @file{aout-target.h}. | |
1109 | ||
1110 | @item netbsd.h | |
1111 | @cindex @file{netbsd.h} | |
1112 | Like @file{freebsd.h}, except that there are several files which include | |
1113 | it. | |
1114 | ||
252b5132 RH |
1115 | @item peicode.h |
1116 | @cindex @file{peicode.h} | |
1117 | Provides swapping routines and other hooks for PE targets. | |
1118 | @file{coffcode.h} will include this rather than @file{coffswap.h} for a | |
1119 | PE target. This defines PE specific versions of the COFF swapping | |
1120 | routines, and also defines some macros which control @file{coffcode.h} | |
1121 | itself. | |
1122 | @end table | |
1123 | ||
1124 | @node BFD relocation handling | |
1125 | @section BFD relocation handling | |
1126 | @cindex bfd relocation handling | |
1127 | @cindex relocations in bfd | |
1128 | ||
1129 | The handling of relocations is one of the more confusing aspects of BFD. | |
1130 | Relocation handling has been implemented in various different ways, all | |
1131 | somewhat incompatible, none perfect. | |
1132 | ||
1133 | @menu | |
1134 | * BFD relocation concepts:: BFD relocation concepts | |
1135 | * BFD relocation functions:: BFD relocation functions | |
1136 | * BFD relocation codes:: BFD relocation codes | |
1137 | * BFD relocation future:: BFD relocation future | |
1138 | @end menu | |
1139 | ||
1140 | @node BFD relocation concepts | |
1141 | @subsection BFD relocation concepts | |
1142 | ||
1143 | A relocation is an action which the linker must take when linking. It | |
1144 | describes a change to the contents of a section. The change is normally | |
1145 | based on the final value of one or more symbols. Relocations are | |
1146 | created by the assembler when it creates an object file. | |
1147 | ||
1148 | Most relocations are simple. A typical simple relocation is to set 32 | |
1149 | bits at a given offset in a section to the value of a symbol. This type | |
1150 | of relocation would be generated for code like @code{int *p = &i;} where | |
1151 | @samp{p} and @samp{i} are global variables. A relocation for the symbol | |
1152 | @samp{i} would be generated such that the linker would initialize the | |
1153 | area of memory which holds the value of @samp{p} to the value of the | |
1154 | symbol @samp{i}. | |
1155 | ||
1156 | Slightly more complex relocations may include an addend, which is a | |
1157 | constant to add to the symbol value before using it. In some cases a | |
1158 | relocation will require adding the symbol value to the existing contents | |
1159 | of the section in the object file. In others the relocation will simply | |
1160 | replace the contents of the section with the symbol value. Some | |
1161 | relocations are PC relative, so that the value to be stored in the | |
1162 | section is the difference between the value of a symbol and the final | |
1163 | address of the section contents. | |
1164 | ||
1165 | In general, relocations can be arbitrarily complex. For example, | |
1166 | relocations used in dynamic linking systems often require the linker to | |
1167 | allocate space in a different section and use the offset within that | |
fdef3943 | 1168 | section as the value to store. |
252b5132 | 1169 | |
1049f94e | 1170 | When doing a relocatable link, the linker may or may not have to do |
252b5132 RH |
1171 | anything with a relocation, depending upon the definition of the |
1172 | relocation. Simple relocations generally do not require any special | |
1173 | action. | |
1174 | ||
1175 | @node BFD relocation functions | |
1176 | @subsection BFD relocation functions | |
1177 | ||
1178 | In BFD, each section has an array of @samp{arelent} structures. Each | |
1179 | structure has a pointer to a symbol, an address within the section, an | |
1180 | addend, and a pointer to a @samp{reloc_howto_struct} structure. The | |
1181 | howto structure has a bunch of fields describing the reloc, including a | |
1182 | type field. The type field is specific to the object file format | |
1183 | backend; none of the generic code in BFD examines it. | |
1184 | ||
1185 | Originally, the function @samp{bfd_perform_relocation} was supposed to | |
1186 | handle all relocations. In theory, many relocations would be simple | |
1187 | enough to be described by the fields in the howto structure. For those | |
1188 | that weren't, the howto structure included a @samp{special_function} | |
1189 | field to use as an escape. | |
1190 | ||
1191 | While this seems plausible, a look at @samp{bfd_perform_relocation} | |
1192 | shows that it failed. The function has odd special cases. Some of the | |
1193 | fields in the howto structure, such as @samp{pcrel_offset}, were not | |
1194 | adequately documented. | |
1195 | ||
1196 | The linker uses @samp{bfd_perform_relocation} to do all relocations when | |
1197 | the input and output file have different formats (e.g., when generating | |
1198 | S-records). The generic linker code, which is used by all targets which | |
1199 | do not define their own special purpose linker, uses | |
1200 | @samp{bfd_get_relocated_section_contents}, which for most targets turns | |
1201 | into a call to @samp{bfd_generic_get_relocated_section_contents}, which | |
1202 | calls @samp{bfd_perform_relocation}. So @samp{bfd_perform_relocation} | |
1203 | is still widely used, which makes it difficult to change, since it is | |
1204 | difficult to test all possible cases. | |
1205 | ||
1206 | The assembler used @samp{bfd_perform_relocation} for a while. This | |
1207 | turned out to be the wrong thing to do, since | |
1208 | @samp{bfd_perform_relocation} was written to handle relocations on an | |
1209 | existing object file, while the assembler needed to create relocations | |
1210 | in a new object file. The assembler was changed to use the new function | |
1211 | @samp{bfd_install_relocation} instead, and @samp{bfd_install_relocation} | |
1212 | was created as a copy of @samp{bfd_perform_relocation}. | |
1213 | ||
1214 | Unfortunately, the work did not progress any farther, so | |
1215 | @samp{bfd_install_relocation} remains a simple copy of | |
1216 | @samp{bfd_perform_relocation}, with all the odd special cases and | |
1217 | confusing code. This again is difficult to change, because again any | |
1218 | change can affect any assembler target, and so is difficult to test. | |
1219 | ||
1220 | The new linker, when using the same object file format for all input | |
1221 | files and the output file, does not convert relocations into | |
1222 | @samp{arelent} structures, so it can not use | |
1223 | @samp{bfd_perform_relocation} at all. Instead, users of the new linker | |
1224 | are expected to write a @samp{relocate_section} function which will | |
1225 | handle relocations in a target specific fashion. | |
1226 | ||
1227 | There are two helper functions for target specific relocation: | |
1228 | @samp{_bfd_final_link_relocate} and @samp{_bfd_relocate_contents}. | |
1229 | These functions use a howto structure, but they @emph{do not} use the | |
1230 | @samp{special_function} field. Since the functions are normally called | |
1231 | from target specific code, the @samp{special_function} field adds | |
1232 | little; any relocations which require special handling can be handled | |
1233 | without calling those functions. | |
1234 | ||
1235 | So, if you want to add a new target, or add a new relocation to an | |
1236 | existing target, you need to do the following: | |
1237 | ||
1238 | @itemize @bullet | |
1239 | @item | |
1240 | Make sure you clearly understand what the contents of the section should | |
1049f94e | 1241 | look like after assembly, after a relocatable link, and after a final |
252b5132 | 1242 | link. Make sure you clearly understand the operations the linker must |
1049f94e | 1243 | perform during a relocatable link and during a final link. |
252b5132 RH |
1244 | |
1245 | @item | |
1246 | Write a howto structure for the relocation. The howto structure is | |
1247 | flexible enough to represent any relocation which should be handled by | |
1248 | setting a contiguous bitfield in the destination to the value of a | |
1249 | symbol, possibly with an addend, possibly adding the symbol value to the | |
1250 | value already present in the destination. | |
1251 | ||
1252 | @item | |
1253 | Change the assembler to generate your relocation. The assembler will | |
1254 | call @samp{bfd_install_relocation}, so your howto structure has to be | |
1255 | able to handle that. You may need to set the @samp{special_function} | |
1256 | field to handle assembly correctly. Be careful to ensure that any code | |
1257 | you write to handle the assembler will also work correctly when doing a | |
1049f94e | 1258 | relocatable link. For example, see @samp{bfd_elf_generic_reloc}. |
252b5132 RH |
1259 | |
1260 | @item | |
1261 | Test the assembler. Consider the cases of relocation against an | |
1262 | undefined symbol, a common symbol, a symbol defined in the object file | |
1263 | in the same section, and a symbol defined in the object file in a | |
1264 | different section. These cases may not all be applicable for your | |
1265 | reloc. | |
1266 | ||
1267 | @item | |
1268 | If your target uses the new linker, which is recommended, add any | |
1269 | required handling to the target specific relocation function. In simple | |
1270 | cases this will just involve a call to @samp{_bfd_final_link_relocate} | |
1271 | or @samp{_bfd_relocate_contents}, depending upon the definition of the | |
1049f94e | 1272 | relocation and whether the link is relocatable or not. |
252b5132 RH |
1273 | |
1274 | @item | |
1275 | Test the linker. Test the case of a final link. If the relocation can | |
1276 | overflow, use a linker script to force an overflow and make sure the | |
1049f94e AM |
1277 | error is reported correctly. Test a relocatable link, whether the |
1278 | symbol is defined or undefined in the relocatable output. For both the | |
1279 | final and relocatable link, test the case when the symbol is a common | |
252b5132 RH |
1280 | symbol, when the symbol looked like a common symbol but became a defined |
1281 | symbol, when the symbol is defined in a different object file, and when | |
1282 | the symbol is defined in the same object file. | |
1283 | ||
1284 | @item | |
1285 | In order for linking to another object file format, such as S-records, | |
1286 | to work correctly, @samp{bfd_perform_relocation} has to do the right | |
1287 | thing for the relocation. You may need to set the | |
1288 | @samp{special_function} field to handle this correctly. Test this by | |
1289 | doing a link in which the output object file format is S-records. | |
1290 | ||
1291 | @item | |
1049f94e | 1292 | Using the linker to generate relocatable output in a different object |
252b5132 | 1293 | file format is impossible in the general case, so you generally don't |
d1d013c3 HPN |
1294 | have to worry about that. The GNU linker makes sure to stop that from |
1295 | happening when an input file in a different format has relocations. | |
1296 | ||
1297 | Linking input files of different object file formats together is quite | |
1298 | unusual, but if you're really dedicated you may want to consider testing | |
1299 | this case, both when the output object file format is the same as your | |
1300 | format, and when it is different. | |
252b5132 RH |
1301 | @end itemize |
1302 | ||
1303 | @node BFD relocation codes | |
1304 | @subsection BFD relocation codes | |
1305 | ||
1306 | BFD has another way of describing relocations besides the howto | |
1307 | structures described above: the enum @samp{bfd_reloc_code_real_type}. | |
1308 | ||
1309 | Every known relocation type can be described as a value in this | |
1310 | enumeration. The enumeration contains many target specific relocations, | |
1311 | but where two or more targets have the same relocation, a single code is | |
1312 | used. For example, the single value @samp{BFD_RELOC_32} is used for all | |
1313 | simple 32 bit relocation types. | |
1314 | ||
1315 | The main purpose of this relocation code is to give the assembler some | |
1316 | mechanism to create @samp{arelent} structures. In order for the | |
1317 | assembler to create an @samp{arelent} structure, it has to be able to | |
1318 | obtain a howto structure. The function @samp{bfd_reloc_type_lookup}, | |
1319 | which simply calls the target vector entry point | |
1320 | @samp{reloc_type_lookup}, takes a relocation code and returns a howto | |
1321 | structure. | |
1322 | ||
1323 | The function @samp{bfd_get_reloc_code_name} returns the name of a | |
1324 | relocation code. This is mainly used in error messages. | |
1325 | ||
1326 | Using both howto structures and relocation codes can be somewhat | |
1327 | confusing. There are many processor specific relocation codes. | |
1328 | However, the relocation is only fully defined by the howto structure. | |
1329 | The same relocation code will map to different howto structures in | |
1330 | different object file formats. For example, the addend handling may be | |
1331 | different. | |
1332 | ||
1333 | Most of the relocation codes are not really general. The assembler can | |
1334 | not use them without already understanding what sorts of relocations can | |
1335 | be used for a particular target. It might be possible to replace the | |
1336 | relocation codes with something simpler. | |
1337 | ||
1338 | @node BFD relocation future | |
1339 | @subsection BFD relocation future | |
1340 | ||
1341 | Clearly the current BFD relocation support is in bad shape. A | |
1342 | wholescale rewrite would be very difficult, because it would require | |
1343 | thorough testing of every BFD target. So some sort of incremental | |
1344 | change is required. | |
1345 | ||
1346 | My vague thoughts on this would involve defining a new, clearly defined, | |
1347 | howto structure. Some mechanism would be used to determine which type | |
1348 | of howto structure was being used by a particular format. | |
1349 | ||
1350 | The new howto structure would clearly define the relocation behaviour in | |
1049f94e | 1351 | the case of an assembly, a relocatable link, and a final link. At |
252b5132 RH |
1352 | least one special function would be defined as an escape, and it might |
1353 | make sense to define more. | |
1354 | ||
1355 | One or more generic functions similar to @samp{bfd_perform_relocation} | |
1356 | would be written to handle the new howto structure. | |
1357 | ||
1358 | This should make it possible to write a generic version of the relocate | |
1359 | section functions used by the new linker. The target specific code | |
1360 | would provide some mechanism (a function pointer or an initial | |
1361 | conversion) to convert target specific relocations into howto | |
1362 | structures. | |
1363 | ||
1364 | Ideally it would be possible to use this generic relocate section | |
1365 | function for the generic linker as well. That is, it would replace the | |
1366 | @samp{bfd_generic_get_relocated_section_contents} function which is | |
1367 | currently normally used. | |
1368 | ||
1369 | For the special case of ELF dynamic linking, more consideration needs to | |
1370 | be given to writing ELF specific but ELF target generic code to handle | |
1371 | special relocation types such as GOT and PLT. | |
1372 | ||
1373 | @node BFD ELF support | |
1374 | @section BFD ELF support | |
1375 | @cindex elf support in bfd | |
1376 | @cindex bfd elf support | |
1377 | ||
1378 | The ELF object file format is defined in two parts: a generic ABI and a | |
1379 | processor specific supplement. The ELF support in BFD is split in a | |
1380 | similar fashion. The processor specific support is largely kept within | |
1381 | a single file. The generic support is provided by several other files. | |
1382 | The processor specific support provides a set of function pointers and | |
1383 | constants used by the generic support. | |
1384 | ||
1385 | @menu | |
1386 | * BFD ELF sections and segments:: ELF sections and segments | |
1387 | * BFD ELF generic support:: BFD ELF generic support | |
1388 | * BFD ELF processor specific support:: BFD ELF processor specific support | |
1389 | * BFD ELF core files:: BFD ELF core files | |
1390 | * BFD ELF future:: BFD ELF future | |
1391 | @end menu | |
1392 | ||
1393 | @node BFD ELF sections and segments | |
1394 | @subsection ELF sections and segments | |
1395 | ||
1396 | The ELF ABI permits a file to have either sections or segments or both. | |
b45619c0 | 1397 | Relocatable object files conventionally have only sections. |
252b5132 RH |
1398 | Executables conventionally have both. Core files conventionally have |
1399 | only program segments. | |
1400 | ||
1401 | ELF sections are similar to sections in other object file formats: they | |
1402 | have a name, a VMA, file contents, flags, and other miscellaneous | |
1403 | information. ELF relocations are stored in sections of a particular | |
1404 | type; BFD automatically converts these sections into internal relocation | |
1405 | information. | |
1406 | ||
1407 | ELF program segments are intended for fast interpretation by a system | |
1408 | loader. They have a type, a VMA, an LMA, file contents, and a couple of | |
1409 | other fields. When an ELF executable is run on a Unix system, the | |
1410 | system loader will examine the program segments to decide how to load | |
1411 | it. The loader will ignore the section information. Loadable program | |
1412 | segments (type @samp{PT_LOAD}) are directly loaded into memory. Other | |
1413 | program segments are interpreted by the loader, and generally provide | |
1414 | dynamic linking information. | |
1415 | ||
1416 | When an ELF file has both program segments and sections, an ELF program | |
1417 | segment may encompass one or more ELF sections, in the sense that the | |
1418 | portion of the file which corresponds to the program segment may include | |
1419 | the portions of the file corresponding to one or more sections. When | |
1420 | there is more than one section in a loadable program segment, the | |
1421 | relative positions of the section contents in the file must correspond | |
1422 | to the relative positions they should hold when the program segment is | |
1423 | loaded. This requirement should be obvious if you consider that the | |
1424 | system loader will load an entire program segment at a time. | |
1425 | ||
1426 | On a system which supports dynamic paging, such as any native Unix | |
1427 | system, the contents of a loadable program segment must be at the same | |
1428 | offset in the file as in memory, modulo the memory page size used on the | |
1429 | system. This is because the system loader will map the file into memory | |
1430 | starting at the start of a page. The system loader can easily remap | |
1431 | entire pages to the correct load address. However, if the contents of | |
1432 | the file were not correctly aligned within the page, the system loader | |
1433 | would have to shift the contents around within the page, which is too | |
1434 | expensive. For example, if the LMA of a loadable program segment is | |
1435 | @samp{0x40080} and the page size is @samp{0x1000}, then the position of | |
1436 | the segment contents within the file must equal @samp{0x80} modulo | |
1437 | @samp{0x1000}. | |
1438 | ||
1439 | BFD has only a single set of sections. It does not provide any generic | |
1440 | way to examine both sections and segments. When BFD is used to open an | |
1441 | object file or executable, the BFD sections will represent ELF sections. | |
1442 | When BFD is used to open a core file, the BFD sections will represent | |
1443 | ELF program segments. | |
1444 | ||
1445 | When BFD is used to examine an object file or executable, any program | |
1446 | segments will be read to set the LMA of the sections. This is because | |
1447 | ELF sections only have a VMA, while ELF program segments have both a VMA | |
1448 | and an LMA. Any program segments will be copied by the | |
1449 | @samp{copy_private} entry points. They will be printed by the | |
1450 | @samp{print_private} entry point. Otherwise, the program segments are | |
1451 | ignored. In particular, programs which use BFD currently have no direct | |
1452 | access to the program segments. | |
1453 | ||
1454 | When BFD is used to create an executable, the program segments will be | |
1455 | created automatically based on the section information. This is done in | |
1456 | the function @samp{assign_file_positions_for_segments} in @file{elf.c}. | |
1457 | This function has been tweaked many times, and probably still has | |
1458 | problems that arise in particular cases. | |
1459 | ||
1460 | There is a hook which may be used to explicitly define the program | |
1461 | segments when creating an executable: the @samp{bfd_record_phdr} | |
1462 | function in @file{bfd.c}. If this function is called, BFD will not | |
1463 | create program segments itself, but will only create the program | |
1464 | segments specified by the caller. The linker uses this function to | |
1465 | implement the @samp{PHDRS} linker script command. | |
1466 | ||
1467 | @node BFD ELF generic support | |
1468 | @subsection BFD ELF generic support | |
1469 | ||
1470 | In general, functions which do not read external data from the ELF file | |
1471 | are found in @file{elf.c}. They operate on the internal forms of the | |
1472 | ELF structures, which are defined in @file{include/elf/internal.h}. The | |
1473 | internal structures are defined in terms of @samp{bfd_vma}, and so may | |
1474 | be used for both 32 bit and 64 bit ELF targets. | |
1475 | ||
1476 | The file @file{elfcode.h} contains functions which operate on the | |
1477 | external data. @file{elfcode.h} is compiled twice, once via | |
1478 | @file{elf32.c} with @samp{ARCH_SIZE} defined as @samp{32}, and once via | |
1479 | @file{elf64.c} with @samp{ARCH_SIZE} defined as @samp{64}. | |
1480 | @file{elfcode.h} includes functions to swap the ELF structures in and | |
1481 | out of external form, as well as a few more complex functions. | |
1482 | ||
c152c796 | 1483 | Linker support is found in @file{elflink.c}. The |
252b5132 RH |
1484 | linker support is only used if the processor specific file defines |
1485 | @samp{elf_backend_relocate_section}, which is required to relocate the | |
1486 | section contents. If that macro is not defined, the generic linker code | |
1487 | is used, and relocations are handled via @samp{bfd_perform_relocation}. | |
1488 | ||
1489 | The core file support is in @file{elfcore.h}, which is compiled twice, | |
1490 | for both 32 and 64 bit support. The more interesting cases of core file | |
1491 | support only work on a native system which has the @file{sys/procfs.h} | |
1492 | header file. Without that file, the core file support does little more | |
1493 | than read the ELF program segments as BFD sections. | |
1494 | ||
1495 | The BFD internal header file @file{elf-bfd.h} is used for communication | |
1496 | among these files and the processor specific files. | |
1497 | ||
1498 | The default entries for the BFD ELF target vector are found mainly in | |
1499 | @file{elf.c}. Some functions are found in @file{elfcode.h}. | |
1500 | ||
1501 | The processor specific files may override particular entries in the | |
1502 | target vector, but most do not, with one exception: the | |
1503 | @samp{bfd_reloc_type_lookup} entry point is always processor specific. | |
1504 | ||
1505 | @node BFD ELF processor specific support | |
1506 | @subsection BFD ELF processor specific support | |
1507 | ||
1508 | By convention, the processor specific support for a particular processor | |
1509 | will be found in @file{elf@var{nn}-@var{cpu}.c}, where @var{nn} is | |
1510 | either 32 or 64, and @var{cpu} is the name of the processor. | |
1511 | ||
1512 | @menu | |
1513 | * BFD ELF processor required:: Required processor specific support | |
1514 | * BFD ELF processor linker:: Processor specific linker support | |
1515 | * BFD ELF processor other:: Other processor specific support options | |
1516 | @end menu | |
1517 | ||
1518 | @node BFD ELF processor required | |
1519 | @subsubsection Required processor specific support | |
1520 | ||
1521 | When writing a @file{elf@var{nn}-@var{cpu}.c} file, you must do the | |
1522 | following: | |
1523 | ||
1524 | @itemize @bullet | |
1525 | @item | |
1526 | Define either @samp{TARGET_BIG_SYM} or @samp{TARGET_LITTLE_SYM}, or | |
1527 | both, to a unique C name to use for the target vector. This name should | |
1528 | appear in the list of target vectors in @file{targets.c}, and will also | |
1110793a | 1529 | have to appear in @file{config.bfd} and @file{configure.ac}. Define |
252b5132 RH |
1530 | @samp{TARGET_BIG_SYM} for a big-endian processor, |
1531 | @samp{TARGET_LITTLE_SYM} for a little-endian processor, and define both | |
1532 | for a bi-endian processor. | |
1533 | @item | |
1534 | Define either @samp{TARGET_BIG_NAME} or @samp{TARGET_LITTLE_NAME}, or | |
1535 | both, to a string used as the name of the target vector. This is the | |
1536 | name which a user of the BFD tool would use to specify the object file | |
1537 | format. It would normally appear in a linker emulation parameters | |
1538 | file. | |
1539 | @item | |
1540 | Define @samp{ELF_ARCH} to the BFD architecture (an element of the | |
1541 | @samp{bfd_architecture} enum, typically @samp{bfd_arch_@var{cpu}}). | |
1542 | @item | |
1543 | Define @samp{ELF_MACHINE_CODE} to the magic number which should appear | |
1544 | in the @samp{e_machine} field of the ELF header. As of this writing, | |
abd4c6a2 | 1545 | these magic numbers are assigned by Caldera; if you want to get a magic |
252b5132 | 1546 | number for a particular processor, try sending a note to |
abd4c6a2 | 1547 | @email{registry@@caldera.com}. In the BFD sources, the magic numbers are |
252b5132 RH |
1548 | found in @file{include/elf/common.h}; they have names beginning with |
1549 | @samp{EM_}. | |
1550 | @item | |
1551 | Define @samp{ELF_MAXPAGESIZE} to the maximum size of a virtual page in | |
1552 | memory. This can normally be found at the start of chapter 5 in the | |
1553 | processor specific supplement. For a processor which will only be used | |
1554 | in an embedded system, or which has no memory management hardware, this | |
1555 | can simply be @samp{1}. | |
1556 | @item | |
1557 | If the format should use @samp{Rel} rather than @samp{Rela} relocations, | |
1558 | define @samp{USE_REL}. This is normally defined in chapter 4 of the | |
1559 | processor specific supplement. | |
1560 | ||
1561 | In the absence of a supplement, it's easier to work with @samp{Rela} | |
1562 | relocations. @samp{Rela} relocations will require more space in object | |
1563 | files (but not in executables, except when using dynamic linking). | |
1564 | However, this is outweighed by the simplicity of addend handling when | |
1565 | using @samp{Rela} relocations. With @samp{Rel} relocations, the addend | |
1049f94e | 1566 | must be stored in the section contents, which makes relocatable links |
252b5132 RH |
1567 | more complex. |
1568 | ||
1569 | For example, consider C code like @code{i = a[1000];} where @samp{a} is | |
1570 | a global array. The instructions which load the value of @samp{a[1000]} | |
1571 | will most likely use a relocation which refers to the symbol | |
1572 | representing @samp{a}, with an addend that gives the offset from the | |
1573 | start of @samp{a} to element @samp{1000}. When using @samp{Rel} | |
1574 | relocations, that addend must be stored in the instructions themselves. | |
1575 | If you are adding support for a RISC chip which uses two or more | |
1576 | instructions to load an address, then the addend may not fit in a single | |
1577 | instruction, and will have to be somehow split among the instructions. | |
1049f94e | 1578 | This makes linking awkward, particularly when doing a relocatable link |
252b5132 RH |
1579 | in which the addend may have to be updated. It can be done---the MIPS |
1580 | ELF support does it---but it should be avoided when possible. | |
1581 | ||
1582 | It is possible, though somewhat awkward, to support both @samp{Rel} and | |
1583 | @samp{Rela} relocations for a single target; @file{elf64-mips.c} does it | |
1584 | by overriding the relocation reading and writing routines. | |
1585 | @item | |
1586 | Define howto structures for all the relocation types. | |
1587 | @item | |
1588 | Define a @samp{bfd_reloc_type_lookup} routine. This must be named | |
1589 | @samp{bfd_elf@var{nn}_bfd_reloc_type_lookup}, and may be either a | |
1590 | function or a macro. It must translate a BFD relocation code into a | |
1591 | howto structure. This is normally a table lookup or a simple switch. | |
1592 | @item | |
1593 | If using @samp{Rel} relocations, define @samp{elf_info_to_howto_rel}. | |
1594 | If using @samp{Rela} relocations, define @samp{elf_info_to_howto}. | |
1595 | Either way, this is a macro defined as the name of a function which | |
1596 | takes an @samp{arelent} and a @samp{Rel} or @samp{Rela} structure, and | |
1597 | sets the @samp{howto} field of the @samp{arelent} based on the | |
1598 | @samp{Rel} or @samp{Rela} structure. This is normally uses | |
1599 | @samp{ELF@var{nn}_R_TYPE} to get the ELF relocation type and uses it as | |
1600 | an index into a table of howto structures. | |
1601 | @end itemize | |
1602 | ||
1603 | You must also add the magic number for this processor to the | |
1604 | @samp{prep_headers} function in @file{elf.c}. | |
1605 | ||
1606 | You must also create a header file in the @file{include/elf} directory | |
1607 | called @file{@var{cpu}.h}. This file should define any target specific | |
1608 | information which may be needed outside of the BFD code. In particular | |
1609 | it should use the @samp{START_RELOC_NUMBERS}, @samp{RELOC_NUMBER}, | |
1610 | @samp{FAKE_RELOC}, @samp{EMPTY_RELOC} and @samp{END_RELOC_NUMBERS} | |
4ee79850 | 1611 | macros to create a table mapping the number used to identify a |
252b5132 RH |
1612 | relocation to a name describing that relocation. |
1613 | ||
dd167cc8 HPN |
1614 | While not a BFD component, you probably also want to make the binutils |
1615 | program @samp{readelf} parse your ELF objects. For this, you need to add | |
964802a8 | 1616 | code for @code{EM_@var{cpu}} as appropriate in @file{binutils/readelf.c}. |
dd167cc8 | 1617 | |
252b5132 RH |
1618 | @node BFD ELF processor linker |
1619 | @subsubsection Processor specific linker support | |
1620 | ||
1621 | The linker will be much more efficient if you define a relocate section | |
1622 | function. This will permit BFD to use the ELF specific linker support. | |
1623 | ||
1624 | If you do not define a relocate section function, BFD must use the | |
1625 | generic linker support, which requires converting all symbols and | |
1626 | relocations into BFD @samp{asymbol} and @samp{arelent} structures. In | |
1627 | this case, relocations will be handled by calling | |
1628 | @samp{bfd_perform_relocation}, which will use the howto structures you | |
1629 | have defined. @xref{BFD relocation handling}. | |
1630 | ||
1631 | In order to support linking into a different object file format, such as | |
1632 | S-records, @samp{bfd_perform_relocation} must work correctly with your | |
1633 | howto structures, so you can't skip that step. However, if you define | |
1634 | the relocate section function, then in the normal case of linking into | |
1635 | an ELF file the linker will not need to convert symbols and relocations, | |
1636 | and will be much more efficient. | |
1637 | ||
1638 | To use a relocation section function, define the macro | |
1639 | @samp{elf_backend_relocate_section} as the name of a function which will | |
1640 | take the contents of a section, as well as relocation, symbol, and other | |
1641 | information, and modify the section contents according to the relocation | |
1642 | information. In simple cases, this is little more than a loop over the | |
1643 | relocations which computes the value of each relocation and calls | |
1644 | @samp{_bfd_final_link_relocate}. The function must check for a | |
1049f94e | 1645 | relocatable link, and in that case normally needs to do nothing other |
252b5132 RH |
1646 | than adjust the addend for relocations against a section symbol. |
1647 | ||
1648 | The complex cases generally have to do with dynamic linker support. GOT | |
1649 | and PLT relocations must be handled specially, and the linker normally | |
1650 | arranges to set up the GOT and PLT sections while handling relocations. | |
1651 | When generating a shared library, random relocations must normally be | |
1652 | copied into the shared library, or converted to RELATIVE relocations | |
1653 | when possible. | |
1654 | ||
1655 | @node BFD ELF processor other | |
1656 | @subsubsection Other processor specific support options | |
1657 | ||
1658 | There are many other macros which may be defined in | |
1659 | @file{elf@var{nn}-@var{cpu}.c}. These macros may be found in | |
1660 | @file{elfxx-target.h}. | |
1661 | ||
1662 | Macros may be used to override some of the generic ELF target vector | |
1663 | functions. | |
1664 | ||
1665 | Several processor specific hook functions which may be defined as | |
1666 | macros. These functions are found as function pointers in the | |
1667 | @samp{elf_backend_data} structure defined in @file{elf-bfd.h}. In | |
1668 | general, a hook function is set by defining a macro | |
1669 | @samp{elf_backend_@var{name}}. | |
1670 | ||
1671 | There are a few processor specific constants which may also be defined. | |
1672 | These are again found in the @samp{elf_backend_data} structure. | |
1673 | ||
1674 | I will not define the various functions and constants here; see the | |
1675 | comments in @file{elf-bfd.h}. | |
1676 | ||
1677 | Normally any odd characteristic of a particular ELF processor is handled | |
1678 | via a hook function. For example, the special @samp{SHN_MIPS_SCOMMON} | |
1679 | section number found in MIPS ELF is handled via the hooks | |
1680 | @samp{section_from_bfd_section}, @samp{symbol_processing}, | |
1681 | @samp{add_symbol_hook}, and @samp{output_symbol_hook}. | |
1682 | ||
1683 | Dynamic linking support, which involves processor specific relocations | |
1684 | requiring special handling, is also implemented via hook functions. | |
1685 | ||
1686 | @node BFD ELF core files | |
1687 | @subsection BFD ELF core files | |
1688 | @cindex elf core files | |
1689 | ||
1690 | On native ELF Unix systems, core files are generated without any | |
1691 | sections. Instead, they only have program segments. | |
1692 | ||
1693 | When BFD is used to read an ELF core file, the BFD sections will | |
1694 | actually represent program segments. Since ELF program segments do not | |
1695 | have names, BFD will invent names like @samp{segment@var{n}} where | |
1696 | @var{n} is a number. | |
1697 | ||
1698 | A single ELF program segment may include both an initialized part and an | |
1699 | uninitialized part. The size of the initialized part is given by the | |
1700 | @samp{p_filesz} field. The total size of the segment is given by the | |
1701 | @samp{p_memsz} field. If @samp{p_memsz} is larger than @samp{p_filesz}, | |
1702 | then the extra space is uninitialized, or, more precisely, initialized | |
1703 | to zero. | |
1704 | ||
1705 | BFD will represent such a program segment as two different sections. | |
1706 | The first, named @samp{segment@var{n}a}, will represent the initialized | |
1707 | part of the program segment. The second, named @samp{segment@var{n}b}, | |
1708 | will represent the uninitialized part. | |
1709 | ||
1710 | ELF core files store special information such as register values in | |
1711 | program segments with the type @samp{PT_NOTE}. BFD will attempt to | |
1712 | interpret the information in these segments, and will create additional | |
1713 | sections holding the information. Some of this interpretation requires | |
1714 | information found in the host header file @file{sys/procfs.h}, and so | |
1715 | will only work when BFD is built on a native system. | |
1716 | ||
1717 | BFD does not currently provide any way to create an ELF core file. In | |
1718 | general, BFD does not provide a way to create core files. The way to | |
1719 | implement this would be to write @samp{bfd_set_format} and | |
1720 | @samp{bfd_write_contents} routines for the @samp{bfd_core} type; see | |
1721 | @ref{BFD target vector format}. | |
1722 | ||
1723 | @node BFD ELF future | |
1724 | @subsection BFD ELF future | |
1725 | ||
1726 | The current dynamic linking support has too much code duplication. | |
1727 | While each processor has particular differences, much of the dynamic | |
1728 | linking support is quite similar for each processor. The GOT and PLT | |
1729 | are handled in fairly similar ways, the details of -Bsymbolic linking | |
1730 | are generally similar, etc. This code should be reworked to use more | |
1731 | generic functions, eliminating the duplication. | |
1732 | ||
1733 | Similarly, the relocation handling has too much duplication. Many of | |
1734 | the @samp{reloc_type_lookup} and @samp{info_to_howto} functions are | |
1735 | quite similar. The relocate section functions are also often quite | |
1736 | similar, both in the standard linker handling and the dynamic linker | |
1737 | handling. Many of the COFF processor specific backends share a single | |
1738 | relocate section function (@samp{_bfd_coff_generic_relocate_section}), | |
1739 | and it should be possible to do something like this for the ELF targets | |
1740 | as well. | |
1741 | ||
1742 | The appearance of the processor specific magic number in | |
1743 | @samp{prep_headers} in @file{elf.c} is somewhat bogus. It should be | |
1744 | possible to add support for a new processor without changing the generic | |
1745 | support. | |
1746 | ||
1747 | The processor function hooks and constants are ad hoc and need better | |
1748 | documentation. | |
1749 | ||
252b5132 RH |
1750 | @node BFD glossary |
1751 | @section BFD glossary | |
1752 | @cindex glossary for bfd | |
1753 | @cindex bfd glossary | |
1754 | ||
1755 | This is a short glossary of some BFD terms. | |
1756 | ||
1757 | @table @asis | |
1758 | @item a.out | |
1759 | The a.out object file format. The original Unix object file format. | |
1760 | Still used on SunOS, though not Solaris. Supports only three sections. | |
1761 | ||
1762 | @item archive | |
1763 | A collection of object files produced and manipulated by the @samp{ar} | |
1764 | program. | |
1765 | ||
1766 | @item backend | |
1767 | The implementation within BFD of a particular object file format. The | |
1768 | set of functions which appear in a particular target vector. | |
1769 | ||
1770 | @item BFD | |
4ee79850 | 1771 | The BFD library itself. Also, each object file, archive, or executable |
252b5132 RH |
1772 | opened by the BFD library has the type @samp{bfd *}, and is sometimes |
1773 | referred to as a bfd. | |
1774 | ||
1775 | @item COFF | |
1776 | The Common Object File Format. Used on Unix SVR3. Used by some | |
1777 | embedded targets, although ELF is normally better. | |
1778 | ||
1779 | @item DLL | |
1780 | A shared library on Windows. | |
1781 | ||
1782 | @item dynamic linker | |
1783 | When a program linked against a shared library is run, the dynamic | |
1784 | linker will locate the appropriate shared library and arrange to somehow | |
1785 | include it in the running image. | |
1786 | ||
1787 | @item dynamic object | |
1788 | Another name for an ELF shared library. | |
1789 | ||
1790 | @item ECOFF | |
1791 | The Extended Common Object File Format. Used on Alpha Digital Unix | |
1792 | (formerly OSF/1), as well as Ultrix and Irix 4. A variant of COFF. | |
1793 | ||
1794 | @item ELF | |
1795 | The Executable and Linking Format. The object file format used on most | |
1796 | modern Unix systems, including GNU/Linux, Solaris, Irix, and SVR4. Also | |
1797 | used on many embedded systems. | |
1798 | ||
1799 | @item executable | |
1800 | A program, with instructions and symbols, and perhaps dynamic linking | |
1801 | information. Normally produced by a linker. | |
1802 | ||
1803 | @item LMA | |
1804 | Load Memory Address. This is the address at which a section will be | |
1805 | loaded. Compare with VMA, below. | |
1806 | ||
252b5132 RH |
1807 | @item object file |
1808 | A binary file including machine instructions, symbols, and relocation | |
1809 | information. Normally produced by an assembler. | |
1810 | ||
1811 | @item object file format | |
1812 | The format of an object file. Typically object files and executables | |
1813 | for a particular system are in the same format, although executables | |
1814 | will not contain any relocation information. | |
1815 | ||
1816 | @item PE | |
1817 | The Portable Executable format. This is the object file format used for | |
1818 | Windows (specifically, Win32) object files. It is based closely on | |
1819 | COFF, but has a few significant differences. | |
1820 | ||
1821 | @item PEI | |
1822 | The Portable Executable Image format. This is the object file format | |
1823 | used for Windows (specifically, Win32) executables. It is very similar | |
1824 | to PE, but includes some additional header information. | |
1825 | ||
1826 | @item relocations | |
1827 | Information used by the linker to adjust section contents. Also called | |
1828 | relocs. | |
1829 | ||
1830 | @item section | |
1831 | Object files and executable are composed of sections. Sections have | |
1832 | optional data and optional relocation information. | |
1833 | ||
1834 | @item shared library | |
1835 | A library of functions which may be used by many executables without | |
1836 | actually being linked into each executable. There are several different | |
1837 | implementations of shared libraries, each having slightly different | |
1838 | features. | |
1839 | ||
1840 | @item symbol | |
1841 | Each object file and executable may have a list of symbols, often | |
1842 | referred to as the symbol table. A symbol is basically a name and an | |
1843 | address. There may also be some additional information like the type of | |
1844 | symbol, although the type of a symbol is normally something simple like | |
1845 | function or object, and should be confused with the more complex C | |
1846 | notion of type. Typically every global function and variable in a C | |
1847 | program will have an associated symbol. | |
1848 | ||
1849 | @item target vector | |
1850 | A set of functions which implement support for a particular object file | |
1851 | format. The @samp{bfd_target} structure. | |
1852 | ||
1853 | @item Win32 | |
1854 | The current Windows API, implemented by Windows 95 and later and Windows | |
1855 | NT 3.51 and later, but not by Windows 3.1. | |
1856 | ||
1857 | @item XCOFF | |
1858 | The eXtended Common Object File Format. Used on AIX. A variant of | |
1859 | COFF, with a completely different symbol table implementation. | |
1860 | ||
1861 | @item VMA | |
1862 | Virtual Memory Address. This is the address a section will have when | |
1863 | an executable is run. Compare with LMA, above. | |
1864 | @end table | |
1865 | ||
1866 | @node Index | |
1867 | @unnumberedsec Index | |
1868 | @printindex cp | |
1869 | ||
1870 | @contents | |
1871 | @bye |