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