Initial revision
[external/binutils.git] / bfd / doc / bfd.texinfo
1 \input texinfo
2 @setfilename bfdinfo
3 @c $Id$
4 @synindex ky cp
5 @ifinfo
6 This file documents the BFD library.
7
8 Copyright (C) 1991 Free Software Foundation, Inc.
9
10 Permission is granted to make and distribute verbatim copies of
11 this manual provided the copyright notice and this permission notice
12 are preserved on all copies.
13
14 @ignore
15 Permission is granted to process this file through Tex and print the
16 results, provided the printed document carries copying permission
17 notice identical to this one except for the removal of this paragraph
18 (this paragraph not being relevant to the printed manual).
19
20 @end ignore
21 Permission is granted to copy and distribute modified versions of this
22 manual under the conditions for verbatim copying, subject to the terms
23 of the GNU General Public License, which includes the provision that the
24 entire resulting derived work is distributed under the terms of a
25 permission notice identical to this one.
26
27 Permission is granted to copy and distribute translations of this manual
28 into another language, under the above conditions for modified versions.
29 @end ifinfo
30 @iftex
31 @c@finalout
32 @setchapternewpage on
33 @c@setchapternewpage odd
34 @settitle LIB BFD, the Binary File Descriptor Library
35 @titlepage
36 @title{libbfd}
37 @subtitle{The Binary File Descriptor Library}
38 @sp 1
39 @subtitle First Edition---BFD version < 2.0
40 @subtitle April 1991
41 @author {Steve Chamberlain}
42 @author {Cygnus Support}
43 @page
44
45 @tex
46 \def\$#1${{#1}}  % Kluge: collect RCS revision info without $...$
47 \xdef\manvers{\$Revision$}  % For use in headers, footers too
48 {\parskip=0pt
49 \hfill Cygnus Support\par
50 \hfill steve\@cygnus.com\par
51 \hfill {\it BFD}, \manvers\par
52 \hfill \TeX{}info \texinfoversion\par
53 }
54 \global\parindent=0pt % Steve likes it this way
55 @end tex
56
57 @vskip 0pt plus 1filll
58 Copyright @copyright{} 1991 Free Software Foundation, Inc.
59
60 Permission is granted to make and distribute verbatim copies of
61 this manual provided the copyright notice and this permission notice
62 are preserved on all copies.
63
64 Permission is granted to copy and distribute modified versions of this
65 manual under the conditions for verbatim copying, subject to the terms
66 of the GNU General Public License, which includes the provision that the
67 entire resulting derived work is distributed under the terms of a
68 permission notice identical to this one.
69
70 Permission is granted to copy and distribute translations of this manual
71 into another language, under the above conditions for modified versions.
72 @end titlepage
73 @end iftex
74
75 @node Top, Overview, (dir), (dir)
76 @ifinfo
77 This file documents the binary file descriptor library libbfd.
78 @end ifinfo
79
80 @menu
81 * Overview::                    Overview of BFD
82 * History::                     History of BFD
83 * Backends::                    Backends
84 * Porting::                     Porting
85 * Future::                      Future
86 * Index::                       Index
87
88 BFD body:
89 * Memory usage::
90 * Sections::
91 * Symbols::
92 * Archives::
93 * Formats::
94 * Relocations::
95 * Core Files::
96 * Targets::
97 * Architecturs::
98 * Opening and Closing::
99 * Internal::
100 * File Caching::
101
102 BFD backends:
103 * a.out backends::
104 * coff backends::
105 @end menu
106
107 @node Overview, History, Top, Top
108 @chapter Introduction
109 @cindex BFD
110 @cindex what is it?
111 Simply put, BFD is a package which allows applications to use the
112 same routines to operate on object files whatever the object file
113 format.  A different object file format can be supported simply by
114 creating a new BFD back end and adding it to the library.
115
116 BFD is split into two parts; the front end and the many back ends.
117 @itemize @bullet
118 @item The front end of BFD provides the interface to the user. It manages
119 memory, and various canonical data structures. The front end also
120 decides which back end to use, and when to call back end routines.
121 @item The back ends provide BFD its view of the real world. Each back
122 end provides a set of calls which the BFD front end can use to maintain
123 its canonical form. The back ends also may keep around information for
124 their own use, for greater efficiency.
125 @end itemize
126 @node History, How It Works, Overview,Top
127 @section History
128
129 One spur behind BFD was the desire, on the part of the GNU 960 team at
130 Intel Oregon, for interoperability of applications on their COFF and
131 b.out file formats.  Cygnus was providing GNU support for the team, and
132 Cygnus was contracted to provide the required functionality.
133
134 The name came from a conversation David Wallace was having with Richard
135 Stallman about the library: RMS said that it would be quite hard---David
136 said ``BFD''.  Stallman was right, but the name stuck.
137
138 At the same time, Ready Systems wanted much the same thing, but for
139 different object file formats: IEEE-695, Oasys, Srecords, a.out and 68k
140 coff.
141
142 BFD was first implemented by Steve Chamberlain (steve@@cygnus.com),
143 John Gilmore (gnu@@cygnus.com),  K. Richard Pixley (rich@@cygnus.com) and
144 David Wallace (gumby@@cygnus.com) at Cygnus Support in Palo Alto,
145 California.
146
147 @node How It Works, History, Porting, Top
148 @section How It Works
149
150 To use the library, include @code{bfd.h} and link with @code{libbfd.a}. 
151
152 BFD provides a common interface to the parts of an object file
153 for a calling application. 
154
155 When an application sucessfully opens a target file (object, archive or
156 whatever) a pointer to an internal structure is returned. This pointer
157 points to a structure called @code{bfd}, described in
158 @code{include/bfd.h}.  Our convention is to call this pointer a BFD, and
159 instances of it within code @code{abfd}.  All operations on
160 the target object file are applied as methods to the BFD.  The mapping is
161 defined within @code{bfd.h} in a set of macros, all beginning
162 @samp{bfd}_.
163
164 For example, this sequence would do what you would probably expect:
165 return the number of sections in an object file attached to a BFD
166 @code{abfd}. 
167
168 @lisp
169 @cartouche
170 #include "bfd.h"
171
172 unsigned int number_of_sections(abfd)
173 bfd *abfd;
174 @{
175   return bfd_count_sections(abfd);
176 @}
177 @end cartouche
178 @end lisp
179
180 The abstraction used within BFD is that an object file has a header,
181 a number of sections containing raw data, a set of relocations, and some
182 symbol information. Also, BFDs opened for archives have the
183 additional attribute of an index and contain subordinate BFDs. This approach is
184 fine for a.out and coff, but loses efficiency when applied to formats
185 such as S-records and IEEE-695. 
186
187 @section What BFD Version 1 Can Do
188 As different information from the the object files is required,
189 BFD reads from different sections of the file and processes them.
190 For example a very common operation for the linker is processing symbol
191 tables.  Each BFD back end provides a routine for converting
192 between the object file's representation of symbols and an internal
193 canonical format. When the linker asks for the symbol table of an object
194 file, it calls through the memory pointer to the relevant BFD
195 back end routine which reads and converts the table into a canonical
196 form.  The linker then operates upon the canonical form. When the link is
197 finished and the linker writes the output file's symbol table,
198 another BFD back end routine is called which takes the newly
199 created symbol table and converts it into the chosen output format.
200
201 @node BFD information loss, Mechanism, BFD outline, BFD
202 @subsection Information Loss
203 @emph{Some information is lost due to the nature of the file format.} The output targets
204 supported by BFD do not provide identical facilities, and
205 information which may be described in one form has nowhere to go in
206 another format. One example of this is alignment information in
207 @code{b.out}. There is nowhere in an @code{a.out} format file to store
208 alignment information on the contained data, so when a file is linked
209 from @code{b.out} and an @code{a.out} image is produced, alignment
210 information will not propagate to the output file. (The linker will
211 still use the alignment information internally, so the link is performed
212 correctly).
213
214 Another example is COFF section names. COFF files may contain an
215 unlimited number of sections, each one with a textual section name. If
216 the target of the link is a format which does not have many sections (eg
217 @code{a.out}) or has sections without names (eg the Oasys format) the
218 link cannot be done simply. You can circumvent this problem by
219 describing the desired input-to-output section mapping with the linker command
220 language.
221
222 @emph{Information can be lost during canonicalization.} The BFD
223 internal canonical form of the external formats is not exhaustive; there
224 are structures in input formats for which there is no direct
225 representation internally.  This means that the BFD back ends
226 cannot maintain all possible data richness through the transformation
227 between external to internal and back to external formats.
228
229 This limitation is only a problem when an application reads one
230 format and writes another.  Each BFD back end is responsible for
231 maintaining as much data as possible, and the internal BFD
232 canonical form has structures which are opaque to the BFD core,
233 and exported only to the back ends. When a file is read in one format,
234 the canonical form is generated for BFD and the application. At the
235 same time, the back end saves away any information which may otherwise
236 be lost. If the data is then written back in the same format, the back
237 end routine will be able to use the canonical form provided by the
238 BFD core as well as the information it prepared earlier.  Since
239 there is a great deal of commonality between back ends, this mechanism
240 is very useful. There is no information lost for this reason when
241 linking or copying big endian COFF to little endian COFF, or @code{a.out} to
242 @code{b.out}.  When a mixture of formats is linked, the information is
243 only lost from the files whose format differs from the destination.
244
245 @node Mechanism,  , BFD information loss, BFD
246 @subsection Mechanism 
247 The greatest potential for loss of information is when there is least
248 overlap between the information provided by the source format, that
249 stored by the canonical format, and the information needed by the
250 destination format. A brief description of the canonical form may help
251 you appreciate what kinds of data you can count on preserving across
252 conversions.
253 @cindex BFD canonical format
254 @cindex internal object-file format
255
256 @table @emph
257 @item files
258 Information on target machine architecture, particular implementation
259 and format type are stored on a per-file basis. Other information
260 includes a demand pageable bit and a write protected bit.  Note that
261 information like Unix magic numbers is not stored here---only the magic
262 numbers' meaning, so a @code{ZMAGIC} file would have both the demand
263 pageable bit and the write protected text bit set.  The byte order of
264 the target is stored on a per-file basis, so that big- and little-endian
265 object files may be linked with one another.
266 @c FIXME: generalize above from "link"?
267
268 @item sections
269 Each section in the input file contains the name of the section, the
270 original address in the object file, various flags, size and alignment
271 information and pointers into other BFD data structures.
272
273 @item symbols
274 Each symbol contains a pointer to the object file which originally
275 defined it, its name, its value, and various flag bits.  When a
276 BFD back end reads in a symbol table, the back end relocates all
277 symbols to make them relative to the base of the section where they were
278 defined.  This ensures that each symbol points to its containing
279 section.  Each symbol also has a varying amount of hidden data to contain
280 private data for the BFD back end.  Since the symbol points to the
281 original file, the private data format for that symbol is accessible.
282 @code{gld} can operate on a collection of symbols of wildly different
283 formats without problems.
284
285 Normal global and simple local symbols are maintained on output, so an
286 output file (no matter its format) will retain symbols pointing to
287 functions and to global, static, and common variables.  Some symbol
288 information is not worth retaining; in @code{a.out} type information is
289 stored in the symbol table as long symbol names.  This information would
290 be useless to most COFF debuggers; the linker has command line switches
291 to allow users to throw it away.
292
293 There is one word of type information within the symbol, so if the
294 format supports symbol type information within symbols (for example COFF,
295 IEEE, Oasys) and the type is simple enough to fit within one word
296 (nearly everything but aggregates) the information will be preserved.
297
298 @item relocation level
299 Each canonical BFD relocation record contains a pointer to the symbol to
300 relocate to, the offset of the data to relocate, the section the data
301 is in and a pointer to a relocation type descriptor. Relocation is
302 performed effectively by message passing through the relocation type
303 descriptor and symbol pointer. It allows relocations to be performed
304 on output data using a relocation method only available in one of the
305 input formats. For instance, Oasys provides a byte relocation format.
306 A relocation record requesting this relocation type would point
307 indirectly to a routine to perform this, so the relocation may be
308 performed on a byte being written to a COFF file, even though 68k COFF
309 has no such relocation type.
310
311 @item line numbers
312 Object formats can contain, for debugging purposes, some form of mapping
313 between symbols, source line numbers, and addresses in the output file.
314 These addresses have to be relocated along with the symbol information.
315 Each symbol with an associated list of line number records points to the
316 first record of the list.  The head of a line number list consists of a
317 pointer to the symbol, which allows divination of the address of the
318 function whose line number is being described. The rest of the list is
319 made up of pairs: offsets into the section and line numbers. Any format
320 which can simply derive this information can pass it successfully
321 between formats (COFF, IEEE and Oasys).
322 @end table
323
324 @c FIXME: what is this line about?  Do we want introductory remarks 
325 @c FIXME... on back ends?  commented out for now.
326 @c What is a backend
327 @node BFD front end, BFD back end, Mechanism, Top
328 @chapter BFD front end
329 @include  bfd.texi
330
331 @node Memory Usage, Sections, bfd, Top
332 @section Memory Usage
333 BFD keeps all its internal structures in obstacks. There is one obstack
334 per open BFD file, into which the current state is stored. When a BFD is
335 closed, the obstack is deleted, and so everything which has been
336 allocated by libbfd for the closing file will be thrown away.
337
338 BFD will not free anything created by an application, but pointers into
339 @code{bfd} structures will be invalidated on a @code{bfd_close}; for example,
340 after a @code{bfd_close} the vector passed to
341 @code{bfd_canonicalize_symtab} will still be around, since it has been
342 allocated by the application, but the data that it pointed to will be
343 lost.
344
345 The general rule is not to close a BFD until all operations dependent
346 upon data from the BFD have been completed, or all the data from within
347 the file has been copied. To help with the management of memory, there is a function
348 (@code{bfd_alloc_size}) which returns the number of bytes in obstacks
349 associated with the supplied BFD. This could be used to select the
350 greediest open BFD, close it to reclaim the memory, perform some
351 operation and reopen the BFD again, to get a fresh copy of the data structures.
352
353 @node Sections,Symbols ,Memory Usage, Top
354 @include  section.texi
355
356 @node Symbols, Archives ,Sections, To
357 @include  syms.texi
358
359 @node Archives, Formats, Symbols, Top
360 @include  archive.texi
361
362 @node Formats, Relocations, Archives, Top
363 @include  format.texi
364
365 @node Relocations, Core Files,Formats, Top
366 @include  reloc.texi
367
368 @node Core Files, Targets,  Relocations, Top
369 @include  core.texi
370
371 @node Targets, Architectures, Core Files, Top
372 @include  targets.texi
373
374 @node Architectures, Opening and Closing, Targets, Top
375 @include  archures.texi
376
377 @node Opening and Closing, Internal, Architectures, Top
378 @include  opncls.texi
379
380 @node Internal, File Caching, Opening and Closing, Top
381 @include  libbfd.texi
382
383 @node File Caching, Top, Internal, Top
384 @include  cache.texi
385
386 @chapter BFD back end
387 @node BFD back end, ,BFD front end, Top
388 @menu
389 * What to put where
390 * a.out backends::
391 * coff backends::
392 * oasys backend::
393 * ieee backend::
394 * srecord backend::
395 @end menu
396 @node What to Put Where, aout backends, BFD back end, BFD back end
397 All of BFD lives in one directory.
398
399 @node aout backends, coff backends, What to Put Where, BFD back end
400 @include  aoutx.texi
401
402 @node coff backends, oasys backends, aout backends, BFD back end
403 @include  coffcode.texi
404
405 @node Index,  , BFD, Top
406 @unnumbered Function Index
407 @printindex  fn
408 @unnumbered Index
409 @printindex cp
410
411 @tex
412 % I think something like @colophon should be in texinfo.  In the
413 % meantime:
414 \long\def\colophon{\hbox to0pt{}\vfill
415 \centerline{The body of this manual is set in}
416 \centerline{\fontname\tenrm,}
417 \centerline{with headings in {\bf\fontname\tenbf}}
418 \centerline{and examples in {\tt\fontname\tentt}.}
419 \centerline{{\it\fontname\tenit\/} and}
420 \centerline{{\sl\fontname\tensl\/}}
421 \centerline{are used for emphasis.}\vfill}
422 \page\colophon
423 % Blame: pesch@cygnus.com, 28mar91.
424 @end tex
425
426
427 @contents
428 @bye
429
430