1 /* Generic BFD library interface and support routines.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 @section @code{typedef bfd}
26 A BFD is has type @code{bfd}; objects of this type are the cornerstone
27 of any application using @code{libbfd}. References though the BFD and
28 to data in the BFD give the entire BFD functionality.
30 Here is the struct used to define the type @code{bfd}. This contains
31 the major data about the file, and contains pointers to the rest of
38 The filename the application opened the BFD with.
40 $ CONST char *filename;
42 A pointer to the target jump table.
44 $ struct bfd_target *xvec;
47 To avoid dragging too many header files into every file that
48 includes @file{bfd.h}, IOSTREAM has been declared as a "char *", and MTIME
49 as a "long". Their correct types, to which they are cast when used,
50 are "FILE *" and "time_t".
52 The iostream is the result of an fopen on the filename.
56 Is the file being cached @xref{File Caching}.
60 Marks whether there was a default target specified when the BFD was
61 opened. This is used to select what matching algorithm to use to chose
64 $ boolean target_defaulted;
66 The caching routines use these to maintain a least-recently-used list of
67 BFDs (@pxref{File Caching}).
69 $ struct _bfd *lru_prev, *lru_next;
71 When a file is closed by the caching routines, BFD retains state
72 information on the file here:
78 $ boolean opened_once;
85 Reserved for an unimplemented file locking extension.
89 The format which belongs to the BFD.
93 The direction the BFD was opened with
95 $ enum bfd_direction {no_direction = 0,
97 $ write_direction = 2,
98 $ both_direction = 3} direction;
100 Format_specific flags
104 Currently my_archive is tested before adding origin to anything. I
105 believe that this can become always an add of origin, with origin set
106 to 0 for non archive files.
110 Remember when output has begun, to stop strange things happening.
112 $ boolean output_has_begun;
114 Pointer to linked list of sections
116 $ struct sec *sections;
118 The number of sections
120 $ unsigned int section_count;
122 Stuff only useful for object files:
125 $ bfd_vma start_address;
126 Used for input and output
128 $ unsigned int symcount;
129 Symbol table for output BFD
131 $ struct symbol_cache_entry **outsymbols;
133 Architecture of object machine, eg m68k
135 $ enum bfd_architecture obj_arch;
137 Particular machine within arch, e.g. 68010
139 $ unsigned long obj_machine;
141 Stuff only useful for archives:
144 $ struct _bfd *my_archive;
146 $ struct _bfd *archive_head;
149 Used by the back end to hold private data.
153 Used by the application to hold private data
157 Where all the allocated stuff under this BFD goes (@pxref{Memory Usage}).
159 $ struct obstack memory;
170 short _bfd_host_big_endian
= 0x0100;
171 /* Accessing the above as (*(char*)&_bfd_host_big_endian), will
172 return 1 if the host is big-endian, 0 otherwise.
173 (assuming that a short is two bytes long!!! FIXME)
174 (See HOST_IS_BIG_ENDIAN_P in bfd.h.) */
177 o - Most functions return nonzero on success (check doc for
178 precise semantics); 0 or NULL on error.
179 o - Internal errors are documented by the value of bfd_error.
180 If that is system_call_error then check errno.
181 o - The easiest way to report this to the user is to use bfd_perror.
184 bfd_ec bfd_error
= no_error
;
186 char *bfd_errmsgs
[] = { "No error",
189 "File in wrong format",
193 "No relocation info",
194 "No more archived files",
197 "File format not recognized",
198 "File format is ambiguous",
199 "Section has no contents",
200 "Nonrepresentable section on output",
201 "#<Invalid error code>"
206 DEFUN(bfd_nonrepresentable_section
,(abfd
, name
),
207 CONST bfd
* CONST abfd AND
208 CONST
char * CONST name
)
210 printf("bfd error writing file %s, format %s can't represent section %s\n",
217 bfd_error_vector_type bfd_error_vector
=
219 bfd_nonrepresentable_section
222 #if !defined(ANSI_LIBRARIES) && !defined(__STDC__) || HOST_SYS==SUN4_SYS
228 extern char *sys_errlist
[];
230 return (((code
< 0) || (code
>= sys_nerr
)) ? "(unknown error)" :
233 #endif /* not ANSI_LIBRARIES */
237 bfd_errmsg (error_tag
)
243 if (error_tag
== system_call_error
)
244 return strerror (errno
);
246 if ((((int)error_tag
<(int) no_error
) ||
247 ((int)error_tag
> (int)invalid_error_code
)))
248 error_tag
= invalid_error_code
;/* sanity check */
250 return bfd_errmsgs
[(int)error_tag
];
254 void bfd_default_error_trap(error_tag
)
257 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag
));
260 void (*bfd_error_trap
)() = bfd_default_error_trap
;
261 void (*bfd_error_nonrepresentabltrap
)() = bfd_default_error_trap
;
264 DEFUN(bfd_perror
,(message
),
267 if (bfd_error
== system_call_error
)
268 perror((char *)message
); /* must be system error then... */
270 if (message
== NULL
|| *message
== '\0')
271 fprintf (stderr
, "%s\n", bfd_errmsg (bfd_error
));
273 fprintf (stderr
, "%s: %s\n", message
, bfd_errmsg (bfd_error
));
280 /* returns the number of octets of storage required */
283 get_reloc_upper_bound (abfd
, asect
)
287 if (abfd
->format
!= bfd_object
) {
288 bfd_error
= invalid_operation
;
292 return BFD_SEND (abfd
, _get_reloc_upper_bound
, (abfd
, asect
));
296 bfd_canonicalize_reloc (abfd
, asect
, location
, symbols
)
302 if (abfd
->format
!= bfd_object
) {
303 bfd_error
= invalid_operation
;
307 return BFD_SEND (abfd
, _bfd_canonicalize_reloc
, (abfd
, asect
, location
, symbols
));
312 bfd_set_file_flags (abfd
, flags
)
316 if (abfd
->format
!= bfd_object
) {
317 bfd_error
= wrong_format
;
321 if (bfd_read_p (abfd
)) {
322 bfd_error
= invalid_operation
;
326 if ((flags
& bfd_applicable_file_flags (abfd
)) != flags
) {
327 bfd_error
= invalid_operation
;
331 bfd_get_file_flags (abfd
) = flags
;
337 bfd_set_reloc (ignore_abfd
, asect
, location
, count
)
343 asect
->orelocation
= location
;
344 asect
->reloc_count
= count
;
348 bfd_assert(file
, line
)
352 printf("bfd assertion fail %s:%d\n",file
,line
);
356 /*proto* bfd_set_start_address
358 Marks the entry point of an output BFD. Returns @code{true} on
359 success, @code{false} otherwise.
361 *; PROTO(boolean, bfd_set_start_address,(bfd *, bfd_vma));
365 bfd_set_start_address(abfd
, vma
)
369 abfd
->start_address
= vma
;
374 /*proto* bfd_get_mtime
376 Return cached file modification time (e.g. as read from archive header
377 for archive members, or from file system if we have been called
378 before); else determine modify time, cache it, and return it.
380 *; PROTO(long, bfd_get_mtime, (bfd *));
394 fp
= bfd_cache_lookup (abfd
);
395 if (0 != fstat (fileno (fp
), &buf
))
398 abfd
->mtime_set
= true;
399 abfd
->mtime
= buf
.st_mtime
;
407 #define bfd_sizeof_headers(abfd, reloc) \
408 BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
410 #define bfd_find_nearest_line(abfd, section, symbols, offset, filename_ptr, func, line_ptr) \
411 BFD_SEND (abfd, _bfd_find_nearest_line, (abfd, section, symbols, offset, filename_ptr, func, line_ptr))
413 #define bfd_debug_info_start(abfd) \
414 BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
416 #define bfd_debug_info_end(abfd) \
417 BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
419 #define bfd_debug_info_accumulate(abfd, section) \
420 BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
422 #define bfd_stat_arch_elt(abfd, stat) \
423 BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
425 #define bfd_coff_swap_aux_in(a,e,t,c,i) \
426 BFD_SEND (a, _bfd_coff_swap_aux_in, (a,e,t,c,i))
428 #define bfd_coff_swap_sym_in(a,e,i) \
429 BFD_SEND (a, _bfd_coff_swap_sym_in, (a,e,i))
431 #define bfd_coff_swap_lineno_in(a,e,i) \
432 BFD_SEND ( a, _bfd_coff_swap_lineno_in, (a,e,i))