Commit | Line | Data |
---|---|---|
6724ff46 RP |
1 | /* Generic BFD library interface and support routines. |
2 | Copyright (C) 1990-1991 Free Software Foundation, Inc. | |
3 | Written by Cygnus Support. | |
4a81b561 | 4 | |
6724ff46 | 5 | This file is part of BFD, the Binary File Descriptor library. |
4a81b561 | 6 | |
6724ff46 | 7 | This program is free software; you can redistribute it and/or modify |
4a81b561 | 8 | it under the terms of the GNU General Public License as published by |
6724ff46 RP |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
4a81b561 | 11 | |
6724ff46 | 12 | This program is distributed in the hope that it will be useful, |
4a81b561 DHW |
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
6724ff46 RP |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
6f715d66 | 20 | |
4a81b561 | 21 | /* $Id$ */ |
6f715d66 | 22 | |
93351e91 SC |
23 | /* |
24 | SECTION | |
25 | <<typedef bfd>> | |
6f715d66 | 26 | |
93351e91 SC |
27 | DESCRIPTION |
28 | A BFD is has type <<bfd>>; objects of this type are the | |
29 | cornerstone of any application using <<libbfd>>. References | |
30 | though the BFD and to data in the BFD give the entire BFD | |
31 | functionality. | |
6f715d66 | 32 | |
93351e91 SC |
33 | Here is the struct used to define the type <<bfd>>. This |
34 | contains he major data about the file, and contains pointers | |
35 | to the rest of the data. | |
6f715d66 | 36 | |
93351e91 SC |
37 | .struct _bfd |
38 | .{ | |
39 | The filename the application opened the BFD with. | |
6f715d66 | 40 | |
93351e91 | 41 | . CONST char *filename; |
6f715d66 | 42 | |
93351e91 | 43 | A pointer to the target jump table. |
6f715d66 | 44 | |
93351e91 | 45 | . struct bfd_target *xvec; |
6f715d66 | 46 | |
93351e91 SC |
47 | To avoid dragging too many header files into every file that |
48 | includes @file{bfd.h}, IOSTREAM has been declared as a "char | |
49 | *", and MTIME as a "long". Their correct types, to which they | |
50 | are cast when used, are "FILE *" and "time_t". The iostream | |
51 | is the result of an fopen on the filename. | |
6f715d66 | 52 | |
93351e91 | 53 | . char *iostream; |
6f715d66 SC |
54 | |
55 | Is the file being cached @xref{File Caching}. | |
56 | ||
93351e91 SC |
57 | . boolean cacheable; |
58 | ||
59 | Marks whether there was a default target specified when the | |
60 | BFD was opened. This is used to select what matching algorithm | |
61 | to use to chose the back end. | |
6f715d66 | 62 | |
93351e91 | 63 | . boolean target_defaulted; |
6f715d66 | 64 | |
93351e91 SC |
65 | The caching routines use these to maintain a |
66 | least-recently-used list of BFDs (@pxref{File Caching}). | |
6f715d66 | 67 | |
93351e91 | 68 | . struct _bfd *lru_prev, *lru_next; |
6f715d66 | 69 | |
93351e91 SC |
70 | When a file is closed by the caching routines, BFD retains |
71 | state information on the file here: | |
6f715d66 | 72 | |
93351e91 | 73 | . file_ptr where; |
6f715d66 | 74 | |
93351e91 | 75 | and here: |
6f715d66 | 76 | |
93351e91 | 77 | . boolean opened_once; |
6f715d66 | 78 | |
93351e91 | 79 | . boolean mtime_set; |
6f715d66 | 80 | |
6f715d66 SC |
81 | File modified time |
82 | ||
93351e91 | 83 | . long mtime; |
6f715d66 | 84 | |
93351e91 | 85 | Reserved for an unimplemented file locking extension. |
6f715d66 | 86 | |
93351e91 | 87 | . int ifd; |
6f715d66 | 88 | |
93351e91 | 89 | The format which belongs to the BFD. |
6f715d66 | 90 | |
93351e91 | 91 | . bfd_format format; |
6f715d66 | 92 | |
93351e91 | 93 | The direction the BFD was opened with |
6f715d66 | 94 | |
93351e91 SC |
95 | . enum bfd_direction {no_direction = 0, |
96 | . read_direction = 1, | |
97 | . write_direction = 2, | |
98 | . both_direction = 3} direction; | |
6f715d66 | 99 | |
93351e91 | 100 | Format_specific flags |
6f715d66 | 101 | |
93351e91 | 102 | . flagword flags; |
6f715d66 | 103 | |
93351e91 SC |
104 | Currently my_archive is tested before adding origin to |
105 | anything. I believe that this can become always an add of | |
106 | origin, with origin set to 0 for non archive files. | |
6f715d66 | 107 | |
93351e91 | 108 | . file_ptr origin; |
6f715d66 | 109 | |
93351e91 | 110 | Remember when output has begun, to stop strange things happening. |
6f715d66 | 111 | |
93351e91 | 112 | . boolean output_has_begun; |
6f715d66 | 113 | |
93351e91 | 114 | Pointer to linked list of sections |
6f715d66 | 115 | |
93351e91 | 116 | . struct sec *sections; |
6f715d66 | 117 | |
93351e91 | 118 | The number of sections |
6f715d66 | 119 | |
93351e91 | 120 | . unsigned int section_count; |
6f715d66 | 121 | |
93351e91 SC |
122 | Stuff only useful for object files: |
123 | The start address. | |
6f715d66 | 124 | |
93351e91 | 125 | . bfd_vma start_address; |
6f715d66 | 126 | |
93351e91 | 127 | Used for input and output |
6f715d66 | 128 | |
93351e91 | 129 | . unsigned int symcount; |
6f715d66 | 130 | |
93351e91 | 131 | Symbol table for output BFD |
6f715d66 | 132 | |
93351e91 | 133 | . struct symbol_cache_entry **outsymbols; |
6f715d66 | 134 | |
93351e91 | 135 | Pointer to structure which contains architecture information |
6f715d66 | 136 | |
93351e91 | 137 | . struct bfd_arch_info *arch_info; |
6f715d66 | 138 | |
93351e91 | 139 | Stuff only useful for archives: |
6f715d66 | 140 | |
93351e91 SC |
141 | . PTR arelt_data; |
142 | . struct _bfd *my_archive; | |
143 | . struct _bfd *next; | |
144 | . struct _bfd *archive_head; | |
145 | . boolean has_armap; | |
6f715d66 | 146 | |
93351e91 | 147 | Used by the back end to hold private data. |
6f715d66 | 148 | |
93351e91 | 149 | . PTR tdata; |
6f715d66 | 150 | |
93351e91 | 151 | Used by the application to hold private data |
6f715d66 | 152 | |
93351e91 | 153 | . PTR usrdata; |
6f715d66 | 154 | |
93351e91 SC |
155 | Where all the allocated stuff under this BFD goes |
156 | (@pxref{Memory Usage}). | |
157 | ||
158 | . struct obstack memory; | |
159 | .}; | |
6f715d66 SC |
160 | |
161 | */ | |
4a81b561 | 162 | #include "bfd.h" |
bbc8d484 | 163 | #include "sysdep.h" |
4a81b561 DHW |
164 | #include "libbfd.h" |
165 | ||
cbdc7909 | 166 | #undef strerror |
bbc8d484 JG |
167 | extern char *strerror(); |
168 | ||
6f715d66 | 169 | |
f8adc62d | 170 | CONST short _bfd_host_big_endian = 0x0100; |
6f715d66 SC |
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.) */ | |
4a81b561 DHW |
175 | \f |
176 | /** Error handling | |
177 | o - Most functions return nonzero on success (check doc for | |
6f715d66 | 178 | precise semantics); 0 or NULL on error. |
4a81b561 | 179 | o - Internal errors are documented by the value of bfd_error. |
6f715d66 | 180 | If that is system_call_error then check errno. |
4a81b561 DHW |
181 | o - The easiest way to report this to the user is to use bfd_perror. |
182 | */ | |
183 | ||
184 | bfd_ec bfd_error = no_error; | |
185 | ||
6f715d66 SC |
186 | char *bfd_errmsgs[] = { "No error", |
187 | "System call error", | |
188 | "Invalid target", | |
189 | "File in wrong format", | |
190 | "Invalid operation", | |
191 | "Memory exhausted", | |
192 | "No symbols", | |
193 | "No relocation info", | |
194 | "No more archived files", | |
195 | "Malformed archive", | |
196 | "Symbol not found", | |
197 | "File format not recognized", | |
198 | "File format is ambiguous", | |
199 | "Section has no contents", | |
200 | "Nonrepresentable section on output", | |
cbdc7909 | 201 | "Symbol needs debug section which does not exist", |
6f715d66 SC |
202 | "#<Invalid error code>" |
203 | }; | |
4a81b561 | 204 | |
9846338e SC |
205 | static |
206 | void | |
207 | DEFUN(bfd_nonrepresentable_section,(abfd, name), | |
6f715d66 SC |
208 | CONST bfd * CONST abfd AND |
209 | CONST char * CONST name) | |
9846338e | 210 | { |
1f4d3c79 | 211 | printf("bfd error writing file %s, format %s can't represent section %s\n", |
6f715d66 SC |
212 | abfd->filename, |
213 | abfd->xvec->name, | |
214 | name); | |
9846338e SC |
215 | exit(1); |
216 | } | |
fc723380 | 217 | |
9846338e SC |
218 | bfd_error_vector_type bfd_error_vector = |
219 | { | |
220 | bfd_nonrepresentable_section | |
221 | }; | |
222 | ||
4a81b561 DHW |
223 | char * |
224 | bfd_errmsg (error_tag) | |
225 | bfd_ec error_tag; | |
226 | { | |
7ed4093a SC |
227 | #ifndef errno |
228 | extern int errno; | |
229 | #endif | |
4a81b561 DHW |
230 | if (error_tag == system_call_error) |
231 | return strerror (errno); | |
232 | ||
233 | if ((((int)error_tag <(int) no_error) || | |
234 | ((int)error_tag > (int)invalid_error_code))) | |
235 | error_tag = invalid_error_code;/* sanity check */ | |
236 | ||
237 | return bfd_errmsgs [(int)error_tag]; | |
238 | } | |
239 | ||
9846338e SC |
240 | |
241 | void bfd_default_error_trap(error_tag) | |
242 | bfd_ec error_tag; | |
243 | { | |
244 | printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag)); | |
245 | } | |
246 | ||
247 | void (*bfd_error_trap)() = bfd_default_error_trap; | |
248 | void (*bfd_error_nonrepresentabltrap)() = bfd_default_error_trap; | |
fc723380 | 249 | |
4a81b561 | 250 | void |
1f4d3c79 SC |
251 | DEFUN(bfd_perror,(message), |
252 | CONST char *message) | |
4a81b561 DHW |
253 | { |
254 | if (bfd_error == system_call_error) | |
6f715d66 | 255 | perror((char *)message); /* must be system error then... */ |
4a81b561 DHW |
256 | else { |
257 | if (message == NULL || *message == '\0') | |
258 | fprintf (stderr, "%s\n", bfd_errmsg (bfd_error)); | |
259 | else | |
260 | fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_error)); | |
261 | } | |
262 | } | |
263 | ||
6f715d66 | 264 | \f |
4a81b561 DHW |
265 | /** Symbols */ |
266 | ||
4a81b561 | 267 | /* returns the number of octets of storage required */ |
6f715d66 | 268 | |
4a81b561 DHW |
269 | unsigned int |
270 | get_reloc_upper_bound (abfd, asect) | |
271 | bfd *abfd; | |
272 | sec_ptr asect; | |
273 | { | |
274 | if (abfd->format != bfd_object) { | |
275 | bfd_error = invalid_operation; | |
276 | return 0; | |
277 | } | |
278 | ||
279 | return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect)); | |
280 | } | |
281 | ||
282 | unsigned int | |
f8adc62d JG |
283 | DEFUN(bfd_canonicalize_reloc,(abfd, asect, location, symbols), |
284 | bfd *abfd AND | |
285 | sec_ptr asect AND | |
286 | arelent **location AND | |
287 | asymbol **symbols) | |
4a81b561 | 288 | { |
f8adc62d JG |
289 | if (abfd->format != bfd_object) { |
290 | bfd_error = invalid_operation; | |
291 | return 0; | |
292 | } | |
293 | return BFD_SEND (abfd, _bfd_canonicalize_reloc, (abfd, asect, location, symbols)); | |
4a81b561 DHW |
294 | } |
295 | ||
4a81b561 DHW |
296 | |
297 | boolean | |
298 | bfd_set_file_flags (abfd, flags) | |
299 | bfd *abfd; | |
300 | flagword flags; | |
301 | { | |
302 | if (abfd->format != bfd_object) { | |
303 | bfd_error = wrong_format; | |
304 | return false; | |
305 | } | |
306 | ||
307 | if (bfd_read_p (abfd)) { | |
308 | bfd_error = invalid_operation; | |
309 | return false; | |
310 | } | |
311 | ||
312 | if ((flags & bfd_applicable_file_flags (abfd)) != flags) { | |
313 | bfd_error = invalid_operation; | |
314 | return false; | |
315 | } | |
316 | ||
317 | bfd_get_file_flags (abfd) = flags; | |
318 | return true; | |
319 | } | |
320 | ||
321 | ||
322 | void | |
323 | bfd_set_reloc (ignore_abfd, asect, location, count) | |
324 | bfd *ignore_abfd; | |
325 | sec_ptr asect; | |
326 | arelent **location; | |
327 | unsigned int count; | |
328 | { | |
329 | asect->orelocation = location; | |
330 | asect->reloc_count = count; | |
331 | } | |
4a81b561 DHW |
332 | |
333 | void | |
334 | bfd_assert(file, line) | |
335 | char *file; | |
336 | int line; | |
337 | { | |
338 | printf("bfd assertion fail %s:%d\n",file,line); | |
339 | } | |
340 | ||
341 | ||
93351e91 SC |
342 | /* |
343 | FUNCTION | |
344 | bfd_set_start_address | |
345 | ||
346 | DESCRIPTION | |
347 | ||
348 | Marks the entry point of an output BFD. | |
6f715d66 | 349 | |
93351e91 SC |
350 | RETURNS |
351 | Returns <<true>> on success, <<false>> otherwise. | |
6f715d66 | 352 | |
93351e91 SC |
353 | SYNOPSIS |
354 | boolean bfd_set_start_address(bfd *, bfd_vma); | |
6f715d66 SC |
355 | */ |
356 | ||
4a81b561 DHW |
357 | boolean |
358 | bfd_set_start_address(abfd, vma) | |
359 | bfd *abfd; | |
360 | bfd_vma vma; | |
361 | { | |
362 | abfd->start_address = vma; | |
363 | return true; | |
364 | } | |
365 | ||
366 | ||
93351e91 SC |
367 | /* |
368 | FUNCTION | |
369 | bfd_get_mtime | |
6f715d66 | 370 | |
93351e91 SC |
371 | DESCRIPTION |
372 | Return cached file modification time (e.g. as read from | |
373 | archive header for archive members, or from file system if we | |
374 | have been called before); else determine modify time, cache | |
375 | it, and return it. | |
6f715d66 | 376 | |
93351e91 SC |
377 | SYNOPSIS |
378 | long bfd_get_mtime(bfd *); | |
6f715d66 | 379 | */ |
4a81b561 DHW |
380 | |
381 | long | |
382 | bfd_get_mtime (abfd) | |
383 | bfd *abfd; | |
384 | { | |
385 | FILE *fp; | |
386 | struct stat buf; | |
387 | ||
388 | if (abfd->mtime_set) | |
389 | return abfd->mtime; | |
390 | ||
391 | fp = bfd_cache_lookup (abfd); | |
392 | if (0 != fstat (fileno (fp), &buf)) | |
393 | return 0; | |
394 | ||
395 | abfd->mtime_set = true; | |
396 | abfd->mtime = buf.st_mtime; | |
397 | return abfd->mtime; | |
398 | } | |
6f715d66 | 399 | |
93351e91 SC |
400 | /* |
401 | FUNCTION | |
402 | stuff | |
403 | ||
404 | DESCRIPTION | |
405 | stuff which should be documented | |
406 | ||
407 | .#define bfd_sizeof_headers(abfd, reloc) \ | |
408 | . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc)) | |
409 | . | |
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)) | |
412 | . | |
413 | .#define bfd_debug_info_start(abfd) \ | |
414 | . BFD_SEND (abfd, _bfd_debug_info_start, (abfd)) | |
415 | . | |
416 | .#define bfd_debug_info_end(abfd) \ | |
417 | . BFD_SEND (abfd, _bfd_debug_info_end, (abfd)) | |
418 | . | |
419 | .#define bfd_debug_info_accumulate(abfd, section) \ | |
420 | . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section)) | |
421 | . | |
422 | .#define bfd_stat_arch_elt(abfd, stat) \ | |
423 | . BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat)) | |
424 | . | |
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)) | |
427 | . | |
428 | .#define bfd_coff_swap_sym_in(a,e,i) \ | |
429 | . BFD_SEND (a, _bfd_coff_swap_sym_in, (a,e,i)) | |
430 | . | |
431 | .#define bfd_coff_swap_lineno_in(a,e,i) \ | |
432 | . BFD_SEND ( a, _bfd_coff_swap_lineno_in, (a,e,i)) | |
433 | . | |
434 | .#define bfd_set_arch_mach(abfd, arch, mach)\ | |
435 | . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach)) | |
436 | . | |
437 | .#define bfd_coff_swap_reloc_out(abfd, i, o) \ | |
438 | . BFD_SEND (abfd, _bfd_coff_swap_reloc_out, (abfd, i, o)) | |
439 | . | |
440 | .#define bfd_coff_swap_lineno_out(abfd, i, o) \ | |
441 | . BFD_SEND (abfd, _bfd_coff_swap_lineno_out, (abfd, i, o)) | |
442 | . | |
443 | .#define bfd_coff_swap_aux_out(abfd, i, t,c,o) \ | |
444 | . BFD_SEND (abfd, _bfd_coff_swap_aux_out, (abfd, i,t,c, o)) | |
445 | . | |
446 | .#define bfd_coff_swap_sym_out(abfd, i,o) \ | |
447 | . BFD_SEND (abfd, _bfd_coff_swap_sym_out, (abfd, i, o)) | |
448 | . | |
449 | .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \ | |
450 | . BFD_SEND (abfd, _bfd_coff_swap_scnhdr_out, (abfd, i, o)) | |
451 | . | |
452 | .#define bfd_coff_swap_filehdr_out(abfd, i,o) \ | |
453 | . BFD_SEND (abfd, _bfd_coff_swap_filehdr_out, (abfd, i, o)) | |
454 | . | |
455 | .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \ | |
456 | . BFD_SEND (abfd, _bfd_coff_swap_aouthdr_out, (abfd, i, o)) | |
457 | . | |
6f715d66 SC |
458 | */ |
459 | ||
460 | ||
461 | ||
462 | ||
463 | ||
464 |