]>
Commit | Line | Data |
---|---|---|
c91a48dd ILT |
1 | \input texinfo |
2 | @setfilename bfdint.info | |
3 | @node Top | |
4 | @top BFD Internals | |
5 | @raisesections | |
6 | @cindex bfd internals | |
7 | ||
8 | This document describes some BFD internal information which may be | |
9 | helpful when working on BFD. It is very incomplete. | |
10 | ||
11 | This document is not updated regularly, and may be out of date. It was | |
12 | last modified on $Date$. | |
13 | ||
14 | The initial version of this document was written by Ian Lance Taylor | |
15 | @email{ian@@cygnus.com}. | |
16 | ||
17 | @menu | |
18 | * BFD glossary:: BFD glossary | |
19 | * BFD guidelines:: BFD programming guidelines | |
20 | * BFD generated files:: BFD generated files | |
21 | * BFD multiple compilations:: Files compiled multiple times in BFD | |
22 | * Index:: Index | |
23 | @end menu | |
24 | ||
25 | @node BFD glossary | |
26 | @section BFD glossary | |
27 | @cindex glossary for bfd | |
28 | @cindex bfd glossary | |
29 | ||
30 | This is a short glossary of some BFD terms. | |
31 | ||
32 | @table @asis | |
33 | @item a.out | |
34 | The a.out object file format. The original Unix object file format. | |
35 | Still used on SunOS, though not Solaris. Supports only three sections. | |
36 | ||
37 | @item archive | |
38 | A collection of object files produced and manipulated by the @samp{ar} | |
39 | program. | |
40 | ||
41 | @item BFD | |
42 | The BFD library itself. Also, each object file, archive, or exectable | |
43 | opened by the BFD library has the type @samp{bfd *}, and is sometimes | |
44 | referred to as a bfd. | |
45 | ||
46 | @item COFF | |
47 | The Common Object File Format. Used on Unix SVR3. Used by some | |
48 | embedded targets, although ELF is normally better. | |
49 | ||
50 | @item DLL | |
51 | A shared library on Windows. | |
52 | ||
53 | @item dynamic linker | |
54 | When a program linked against a shared library is run, the dynamic | |
55 | linker will locate the appropriate shared library and arrange to somehow | |
56 | include it in the running image. | |
57 | ||
58 | @item dynamic object | |
59 | Another name for an ELF shared library. | |
60 | ||
61 | @item ECOFF | |
62 | The Extended Common Object File Format. Used on Alpha Digital Unix | |
63 | (formerly OSF/1), as well as Ultrix and Irix 4. A variant of COFF. | |
64 | ||
65 | @item ELF | |
66 | The Executable and Linking Format. The object file format used on most | |
67 | modern Unix systems, including GNU/Linux, Solaris, Irix, and SVR4. Also | |
68 | used on many embedded systems. | |
69 | ||
70 | @item executable | |
71 | A program, with instructions and symbols, and perhaps dynamic linking | |
72 | information. Normally produced by a linker. | |
73 | ||
74 | @item NLM | |
75 | NetWare Loadable Module. Used to describe the format of an object which | |
76 | be loaded into NetWare, which is some kind of PC based network server | |
77 | program. | |
78 | ||
79 | @item object file | |
80 | A binary file including machine instructions, symbols, and relocation | |
81 | information. Normally produced by an assembler. | |
82 | ||
83 | @item object file format | |
84 | The format of an object file. Typically object files and executables | |
85 | for a particular system are in the same format, although executables | |
86 | will not contain any relocation information. | |
87 | ||
88 | @item PE | |
89 | The Portable Executable format. This is the object file format used for | |
90 | Windows (specifically, Win32) object files. It is based closely on | |
91 | COFF, but has a few significant differences. | |
92 | ||
93 | @item PEI | |
94 | The Portable Executable Image format. This is the object file format | |
95 | used for Windows (specifically, Win32) executables. It is very similar | |
96 | to PE, but includes some additional header information. | |
97 | ||
98 | @item relocations | |
99 | Information used by the linker to adjust section contents. | |
100 | ||
101 | @item section | |
102 | Object files and executable are composed of sections. Sections have | |
103 | optional data and optional relocation information. | |
104 | ||
105 | @item shared library | |
106 | A library of functions which may be used by many executables without | |
107 | actually being linked into each executable. There are several different | |
108 | implementations of shared libraries, each having slightly different | |
109 | features. | |
110 | ||
111 | @item symbol | |
112 | Each object file and executable may have a list of symbols, often | |
113 | referred to as the symbol table. A symbol is basically a name and an | |
114 | address. There may also be some additional information like the type of | |
115 | symbol, although the type of a symbol is normally something simple like | |
116 | function or object, and should be confused with the more complex C | |
117 | notion of type. Typically every global function and variable in a C | |
118 | program will have an associated symbol. | |
119 | ||
120 | @item Win32 | |
121 | The current Windows API, implemented by Windows 95 and later and Windows | |
122 | NT 3.51 and later, but not by Windows 3.1. | |
123 | ||
124 | @item XCOFF | |
125 | The eXtended Common Object File Format. Used on AIX. A variant of | |
126 | COFF, with a completely different symbol table implementation. | |
127 | @end table | |
128 | ||
129 | @node BFD guidelines | |
130 | @section BFD programming guidelines | |
131 | @cindex bfd programming guidelines | |
132 | @cindex programming guidelines for bfd | |
133 | @cindex guidelines, bfd programming | |
134 | ||
135 | There is a lot of poorly written and confusing code in BFD. New BFD | |
136 | code should be written to a higher standard. Merely because some BFD | |
137 | code is written in a particular manner does not mean that you should | |
138 | emulate it. | |
139 | ||
140 | Here are some general BFD programming guidelines: | |
141 | ||
142 | @itemize @bullet | |
143 | @item | |
144 | Avoid global variables. We ideally want BFD to be fully reentrant, so | |
145 | that it can be used in multiple threads. All uses of global or static | |
146 | variables interfere with that. Initialized constant variables are OK, | |
147 | and they should be explicitly marked with const. Instead of global | |
148 | variables, use data attached to a BFD or to a linker hash table. | |
149 | ||
150 | @item | |
151 | All externally visible functions should have names which start with | |
152 | @samp{bfd_}. All such functions should be declared in some header file, | |
153 | typically @file{bfd.h}. See, for example, the various declarations near | |
154 | the end of @file{bfd-in.h}, which mostly declare functions required by | |
155 | specific linker emulations. | |
156 | ||
157 | @item | |
158 | All functions which need to be visible from one file to another within | |
159 | BFD, but should not be visible outside of BFD, should start with | |
160 | @samp{_bfd_}. Although external names beginning with @samp{_} are | |
161 | prohibited by the ANSI standard, in practice this usage will always | |
162 | work, and it is required by the GNU coding standards. | |
163 | ||
164 | @item | |
165 | Always remember that people can compile using --enable-targets to build | |
166 | several, or all, targets at once. It must be possible to link together | |
167 | the files for all targets. | |
168 | ||
169 | @item | |
170 | BFD code should compile with few or no warnings using @samp{gcc -Wall}. | |
171 | Some warnings are OK, like the absence of certain function declarations | |
172 | which may or may not be declared in system header files. Warnings about | |
173 | ambiguous expressions and the like should always be fixed. | |
174 | @end itemize | |
175 | ||
176 | @node BFD generated files | |
177 | @section BFD generated files | |
178 | @cindex generated files in bfd | |
179 | @cindex bfd generated files | |
180 | ||
181 | BFD contains several automatically generated files. This section | |
182 | describes them. Some files are created at configure time, when you | |
183 | configure BFD. Some files are created at make time, when you build | |
184 | time. Some files are automatically rebuilt at make time, but only if | |
185 | you configure with the @samp{--enable-maintainer-mode} option. Some | |
186 | files live in the object directory---the directory from which you run | |
187 | configure---and some live in the source directory. All files that live | |
188 | in the source directory are checked into the CVS repository. | |
189 | ||
190 | @table @file | |
191 | @item bfd.h | |
192 | @cindex @file{bfd.h} | |
193 | @cindex @file{bfd-in3.h} | |
194 | Lives in the object directory. Created at make time from | |
195 | @file{bfd-in2.h} via @file{bfd-in3.h}. @file{bfd-in3.h} is created at | |
196 | configure time from @file{bfd-in2.h}. There are automatic dependencies | |
197 | to rebuild @file{bfd-in3.h} and hence @file{bfd.h} if @file{bfd-in2.h} | |
198 | changes, so you can normally ignore @file{bfd-in3.h}, and just think | |
199 | about @file{bfd-in2.h} and @file{bfd.h}. | |
200 | ||
201 | @file{bfd.h} is built by replacing a few strings in @file{bfd-in2.h}. | |
202 | To see them, search for @samp{@@} in @file{bfd-in2.h}. They mainly | |
203 | control whether BFD is built for a 32 bit target or a 64 bit target. | |
204 | ||
205 | @item bfd-in2.h | |
206 | @cindex @file{bfd-in2.h} | |
207 | Lives in the source directory. Created from @file{bfd-in.h} and several | |
208 | other BFD source files. If you configure with the | |
209 | @samp{--enable-maintainer-mode} option, @file{bfd-in2.h} is rebuilt | |
210 | automatically when a source file changes. | |
211 | ||
212 | @item elf32-target.h | |
213 | @itemx elf64-target.h | |
214 | @cindex @file{elf32-target.h} | |
215 | @cindex @file{elf64-target.h} | |
216 | Live in the object directory. Created from @file{elfxx-target.h}. | |
217 | These files are versions of @file{elfxx-target.h} customized for either | |
218 | a 32 bit ELF target or a 64 bit ELF target. | |
219 | ||
220 | @item libbfd.h | |
221 | @cindex @file{libbfd.h} | |
222 | Lives in the source directory. Created from @file{libbfd-in.h} and | |
223 | several other BFD source files. If you configure with the | |
224 | @samp{--enable-maintainer-mode} option, @file{libbfd.h} is rebuilt | |
225 | automatically when a source file changes. | |
226 | ||
227 | @item libcoff.h | |
228 | @cindex @file{libcoff.h} | |
229 | Lives in the source directory. Created from @file{libcoff-in.h} and | |
230 | @file{coffcode.h}. If you configure with the | |
231 | @samp{--enable-maintainer-mode} option, @file{libcoff.h} is rebuilt | |
232 | automatically when a source file changes. | |
233 | ||
234 | @item targmatch.h | |
235 | @cindex @file{targmatch.h} | |
236 | Lives in the object directory. Created at make time from | |
237 | @file{config.bfd}. This file is used to map configuration triplets into | |
238 | BFD target vector variable names at run time. | |
239 | @end table | |
240 | ||
241 | @node BFD multiple compilations | |
242 | @section Files compiled multiple times in BFD | |
243 | Several files in BFD are compiled multiple times. By this I mean that | |
244 | there are header files which contain function definitions. These header | |
245 | filesare included by other files, and thus the functions are compiled | |
246 | once per file which includes them. | |
247 | ||
248 | Preprocessor macros are used to control the compilation, so that each | |
249 | time the files are compiled the resulting functions are slightly | |
250 | different. Naturally, if they weren't different, there would be no | |
251 | reason to compile them multiple times. | |
252 | ||
253 | This is a not a particularly good programming technique, and future BFD | |
254 | work should avoid it. | |
255 | ||
256 | @itemize @bullet | |
257 | @item | |
258 | Since this technique is rarely used, even experienced C programmers find | |
259 | it confusing. | |
260 | ||
261 | @item | |
262 | It is difficult to debug programs which use BFD, since there is no way | |
263 | to describe which version of a particular function you are looking at. | |
264 | ||
265 | @item | |
266 | Programs which use BFD wind up incorporating two or more slightly | |
267 | different versions of the same function, which wastes space in the | |
268 | executable. | |
269 | ||
270 | @item | |
271 | This technique is never required nor is it especially efficient. It is | |
272 | always possible to use statically initialized structures holding | |
273 | function pointers and magic constants instead. | |
274 | @end itemize | |
275 | ||
276 | The following a list of the files which are compiled multiple times. | |
277 | ||
278 | @table @file | |
279 | @item aout-target.h | |
280 | @cindex @file{aout-target.h} | |
281 | Describes a few functions and the target vector for a.out targets. This | |
282 | is used by individual a.out targets with different definitions of | |
283 | @samp{N_TXTADDR} and similar a.out macros. | |
284 | ||
285 | @item aoutf1.h | |
286 | @cindex @file{aoutf1.h} | |
287 | Implements standard SunOS a.out files. In principle it supports 64 bit | |
288 | a.out targets based on the preprocessor macro @samp{ARCH_SIZE}, but | |
289 | since all known a.out targets are 32 bits, this code may or may not | |
290 | work. This file is only included by a few other files, and it is | |
291 | difficult to justify its existence. | |
292 | ||
293 | @item aoutx.h | |
294 | @cindex @file{aoutx.h} | |
295 | Implements basic a.out support routines. This file can be compiled for | |
296 | either 32 or 64 bit support. Since all known a.out targets are 32 bits, | |
297 | the 64 bit support may or may not work. I believe the original | |
298 | intention was that this file would only be included by @samp{aout32.c} | |
299 | and @samp{aout64.c}, and that other a.out targets would simply refer to | |
300 | the functions it defined. Unfortunately, some other a.out targets | |
301 | started including it directly, leading to a somewhat confused state of | |
302 | affairs. | |
303 | ||
304 | @item coffcode.h | |
305 | @cindex @file{coffcode.h} | |
306 | Implements basic COFF support routines. This file is included by every | |
307 | COFF target. It implements code which handles COFF magic numbers as | |
308 | well as various hook functions called by the generic COFF functions in | |
309 | @file{coffgen.c}. This file is controlled by a number of different | |
310 | macros, and more are added regularly. | |
311 | ||
312 | @item coffswap.h | |
313 | @cindex @file{coffswap.h} | |
314 | Implements COFF swapping routines. This file is included by | |
315 | @file{coffcode.h}, and thus by every COFF target. It implements the | |
316 | routines which swap COFF structures between internal and external | |
317 | format. The main control for this file is the external structure | |
318 | definitions in the files in the @file{include/coff} directory. A COFF | |
319 | target file will include one of those files before including | |
320 | @file{coffcode.h} and thus @file{coffswap.h}. There are a few other | |
321 | macros which affect @file{coffswap.h} as well, mostly describing whether | |
322 | certain fields are present in the external structures. | |
323 | ||
324 | @item ecoffswap.h | |
325 | @cindex @file{ecoffswap.h} | |
326 | Implements ECOFF swapping routines. This is like @file{coffswap.h}, but | |
327 | for ECOFF. It is included by the ECOFF target files (of which there are | |
328 | only two). The control is the preprocessor macro @samp{ECOFF_32} or | |
329 | @samp{ECOFF_64}. | |
330 | ||
331 | @item elfcode.h | |
332 | @cindex @file{elfcode.h} | |
333 | Implements ELF functions that use external structure definitions. This | |
334 | file is included by two other files: @file{elf32.c} and @file{elf64.c}. | |
335 | It is controlled by the @samp{ARCH_SIZE} macro which is defined to be | |
336 | @samp{32} or @samp{64} before including it. The @samp{NAME} macro is | |
337 | used internally to give the functions different names for the two target | |
338 | sizes. | |
339 | ||
340 | @item elfcore.h | |
341 | @cindex @file{elfcore.h} | |
342 | Like @file{elfcode.h}, but for functions that are specific to ELF core | |
343 | files. This is included only by @file{elfcode.h}. | |
344 | ||
345 | @item elflink.h | |
346 | @cindex @file{elflink.h} | |
347 | Like @file{elfcode.h}, but for functions used by the ELF linker. This | |
348 | is included only by @file{elfcode.h}. | |
349 | ||
350 | @item elfxx-target.h | |
351 | @cindex @file{elfxx-target.h} | |
352 | This file is the source for the generated files @file{elf32-target.h} | |
353 | and @file{elf64-target.h}, one of which is included by every ELF target. | |
354 | It defines the ELF target vector. | |
355 | ||
356 | @item freebsd.h | |
357 | @cindex @file{freebsd.h} | |
358 | Presumably intended to be included by all FreeBSD targets, but in fact | |
359 | there is only one such target, @samp{i386-freebsd}. This defines a | |
360 | function used to set the right magic number for FreeBSD, as well as | |
361 | various macros, and includes @file{aout-target.h}. | |
362 | ||
363 | @item netbsd.h | |
364 | @cindex @file{netbsd.h} | |
365 | Like @file{freebsd.h}, except that there are several files which include | |
366 | it. | |
367 | ||
368 | @item nlm-target.h | |
369 | @cindex @file{nlm-target.h} | |
370 | Defines the target vector for a standard NLM target. | |
371 | ||
372 | @item nlmcode.h | |
373 | @cindex @file{nlmcode.h} | |
374 | Like @file{elfcode.h}, but for NLM targets. This is only included by | |
375 | @file{nlm32.c} and @file{nlm64.c}, both of which define the macro | |
376 | @samp{ARCH_SIZE} to an appropriate value. There are no 64 bit NLM | |
377 | targets anyhow, so this is sort of useless. | |
378 | ||
379 | @item nlmswap.h | |
380 | @cindex @file{nlmswap.h} | |
381 | Like @file{coffswap.h}, but for NLM targets. This is included by each | |
382 | NLM target, but I think it winds up compiling to the exact same code for | |
383 | every target, and as such is fairly useless. | |
384 | ||
385 | @item peicode.h | |
386 | @cindex @file{peicode.h} | |
387 | Provides swapping routines and other hooks for PE targets. | |
388 | @file{coffcode.h} will include this rather than @file{coffswap.h} for a | |
389 | PE target. This defines PE specific versions of the COFF swapping | |
390 | routines, and also defines some macros which control @file{coffcode.h} | |
391 | itself. | |
392 | @end table | |
393 | ||
394 | @node Index | |
395 | @unnumberedsec Index | |
396 | @printindex cp | |
397 | ||
398 | @contents | |
399 | @bye |