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. */
26 Pointers to bfd structs are the cornerstone of any application using
27 @code{libbfd}. References though the BFD and to data in the BFD give the
28 entire BFD functionality.
30 Here is the BFD struct itself. This contains the major data about
31 the file, and contains pointers to the rest of the data.
37 The filename the application opened the BFD with.
39 $ CONST char *filename;
41 A pointer to the target jump table.
43 $ struct bfd_target *xvec;
46 To avoid dragging too many header files into every file that
47 includes @file{bfd.h}, IOSTREAM has been declared as a "char *", and MTIME
48 as a "long". Their correct types, to which they are cast when used,
49 are "FILE *" and "time_t".
51 The iostream is the result of an fopen on the filename.
55 Is the file being cached @xref{File Caching}.
59 Marks whether there was a default target specified when the BFD was
60 opened. This is used to select what matching algorithm to use to chose
63 $ boolean target_defaulted;
65 The caching routines use these to maintain a least-recently-used list of
66 BFDs (@pxref{File Caching}).
68 $ struct _bfd *lru_prev, *lru_next;
70 When a file is closed by the caching routines, BFD retains state
71 information on the file here:
77 $ boolean opened_once;
84 For output files, channel we locked (is this used?).
88 The format which belongs to the BFD.
92 The direction the BFD was opened with
94 $ enum bfd_direction {no_direction = 0,
96 $ write_direction = 2,
97 $ both_direction = 3} direction;
103 Currently my_archive is tested before adding origin to anything. I
104 believe that this can become always an add of origin, with origin set
105 to 0 for non archive files.
109 Remember when output has begun, to stop strange things happening.
111 $ boolean output_has_begun;
113 Pointer to linked list of sections
115 $ struct sec *sections;
117 The number of sections
119 $ unsigned int section_count;
121 Stuff only useful for object files:
124 $ bfd_vma start_address;
125 Used for input and output
127 $ unsigned int symcount;
128 Symbol table for output BFD
130 $ struct symbol_cache_entry **outsymbols;
132 Architecture of object machine, eg m68k
134 $ enum bfd_architecture obj_arch;
136 Particular machine within arch, e.g. 68010
138 $ unsigned long obj_machine;
140 Stuff only useful for archives:
143 $ struct _bfd *my_archive;
145 $ struct _bfd *archive_head;
148 Used by the back end to hold private data.
152 Used by the application to hold private data
156 Where all the allocated stuff under this BFD goes (@pxref{Memory Usage}).
158 $ struct obstack memory;
169 short _bfd_host_big_endian
= 0x0100;
170 /* Accessing the above as (*(char*)&_bfd_host_big_endian), will
171 return 1 if the host is big-endian, 0 otherwise.
172 (assuming that a short is two bytes long!!! FIXME)
173 (See HOST_IS_BIG_ENDIAN_P in bfd.h.) */
176 o - Most functions return nonzero on success (check doc for
177 precise semantics); 0 or NULL on error.
178 o - Internal errors are documented by the value of bfd_error.
179 If that is system_call_error then check errno.
180 o - The easiest way to report this to the user is to use bfd_perror.
183 bfd_ec bfd_error
= no_error
;
185 char *bfd_errmsgs
[] = { "No error",
188 "File in wrong format",
192 "No relocation info",
193 "No more archived files",
196 "File format not recognized",
197 "File format is ambiguous",
198 "Section has no contents",
199 "Nonrepresentable section on output",
200 "#<Invalid error code>"
205 DEFUN(bfd_nonrepresentable_section
,(abfd
, name
),
206 CONST bfd
* CONST abfd AND
207 CONST
char * CONST name
)
209 printf("bfd error writing file %s, format %s can't represent section %s\n",
216 bfd_error_vector_type bfd_error_vector
=
218 bfd_nonrepresentable_section
221 #if 1 || !defined(ANSI_LIBRARIES) && !defined(__STDC__)
227 extern char *sys_errlist
[];
229 return (((code
< 0) || (code
>= sys_nerr
)) ? "(unknown error)" :
232 #endif /* not ANSI_LIBRARIES */
236 bfd_errmsg (error_tag
)
242 if (error_tag
== system_call_error
)
243 return strerror (errno
);
245 if ((((int)error_tag
<(int) no_error
) ||
246 ((int)error_tag
> (int)invalid_error_code
)))
247 error_tag
= invalid_error_code
;/* sanity check */
249 return bfd_errmsgs
[(int)error_tag
];
253 void bfd_default_error_trap(error_tag
)
256 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag
));
259 void (*bfd_error_trap
)() = bfd_default_error_trap
;
260 void (*bfd_error_nonrepresentabltrap
)() = bfd_default_error_trap
;
263 DEFUN(bfd_perror
,(message
),
266 if (bfd_error
== system_call_error
)
267 perror((char *)message
); /* must be system error then... */
269 if (message
== NULL
|| *message
== '\0')
270 fprintf (stderr
, "%s\n", bfd_errmsg (bfd_error
));
272 fprintf (stderr
, "%s: %s\n", message
, bfd_errmsg (bfd_error
));
279 /* returns the number of octets of storage required */
282 get_reloc_upper_bound (abfd
, asect
)
286 if (abfd
->format
!= bfd_object
) {
287 bfd_error
= invalid_operation
;
291 return BFD_SEND (abfd
, _get_reloc_upper_bound
, (abfd
, asect
));
295 bfd_canonicalize_reloc (abfd
, asect
, location
, symbols
)
301 if (abfd
->format
!= bfd_object
) {
302 bfd_error
= invalid_operation
;
306 return BFD_SEND (abfd
, _bfd_canonicalize_reloc
, (abfd
, asect
, location
, symbols
));
311 bfd_set_file_flags (abfd
, flags
)
315 if (abfd
->format
!= bfd_object
) {
316 bfd_error
= wrong_format
;
320 if (bfd_read_p (abfd
)) {
321 bfd_error
= invalid_operation
;
325 if ((flags
& bfd_applicable_file_flags (abfd
)) != flags
) {
326 bfd_error
= invalid_operation
;
330 bfd_get_file_flags (abfd
) = flags
;
336 bfd_set_reloc (ignore_abfd
, asect
, location
, count
)
342 asect
->orelocation
= location
;
343 asect
->reloc_count
= count
;
347 bfd_assert(file
, line
)
351 printf("bfd assertion fail %s:%d\n",file
,line
);
355 /*proto* bfd_set_start_address
357 Marks the entry point of an output BFD. Returns @code{true} on
358 success, @code{false} otherwise.
360 *; PROTO(boolean, bfd_set_start_address,(bfd *, bfd_vma));
364 bfd_set_start_address(abfd
, vma
)
368 abfd
->start_address
= vma
;
373 /*proto* bfd_get_mtime
375 Return cached file modification time (e.g. as read from archive header
376 for archive members, or from file system if we have been called
377 before); else determine modify time, cache it, and return it.
379 *; PROTO(long, bfd_get_mtime, (bfd *));
393 fp
= bfd_cache_lookup (abfd
);
394 if (0 != fstat (fileno (fp
), &buf
))
397 abfd
->mtime_set
= true;
398 abfd
->mtime
= buf
.st_mtime
;
406 #define bfd_sizeof_headers(abfd, reloc) \
407 BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
409 #define bfd_find_nearest_line(abfd, section, symbols, offset, filename_ptr, func, line_ptr) \
410 BFD_SEND (abfd, _bfd_find_nearest_line, (abfd, section, symbols, offset, filename_ptr, func, line_ptr))
412 #define bfd_debug_info_start(abfd) \
413 BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
415 #define bfd_debug_info_end(abfd) \
416 BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
418 #define bfd_debug_info_accumulate(abfd, section) \
419 BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
421 #define bfd_stat_arch_elt(abfd, stat) \
422 BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
424 #define bfd_coff_swap_aux_in(a,e,t,c,i) \
425 BFD_SEND (a, _bfd_coff_swap_aux_in, (a,e,t,c,i))
427 #define bfd_coff_swap_sym_in(a,e,i) \
428 BFD_SEND (a, _bfd_coff_swap_sym_in, (a,e,i))
430 #define bfd_coff_swap_lineno_in(a,e,i) \
431 BFD_SEND ( a, _bfd_coff_swap_lineno_in, (a,e,i))