Mon Aug 19 13:48:22 1991 Roland H. Pesch (pesch at cygint.cygnus.com)
[deliverable/binutils-gdb.git] / bfd / bfd.c
1 /* Generic BFD library interface and support routines.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
21 /* $Id$ */
22
23 /*proto*
24 @section typedef bfd
25
26 Pointers to bfd structs are the cornerstone of any application using
27 libbfd. References though the BFD and to data in the BFD give the
28 entire BFD functionality.
29
30 Finally! The BFD struct itself. This contains the major data about
31 the file, and contains pointers to the rest of the data.
32
33 *+++
34
35 $struct _bfd
36 ${
37 The filename the application opened the BFD with.
38
39 $ CONST char *filename;
40
41 A pointer to the target jump table.
42
43 $ struct bfd_target *xvec;
44
45
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".
50
51 The iostream is the result of an fopen on the filename.
52
53 $ char *iostream;
54
55 Is the file being cached @xref{File Caching}.
56
57 $ boolean cacheable;
58
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
61 the back end.
62
63 $ boolean target_defaulted;
64
65 The caching routines use these to maintain an LRU list of BFDs.
66
67 $ struct _bfd *lru_prev, *lru_next;
68
69 When a file is closed by the caching routines, it retains the state
70 here:
71
72 $ file_ptr where;
73
74 and here:
75
76 $ boolean opened_once;
77
78 $ boolean mtime_set;
79 File modified time
80
81 $ long mtime;
82
83 For output files, channel we locked (is this used?).
84
85 $int ifd;
86
87 The format which belongs to the BFD.
88
89 $ bfd_format format;
90
91 The direction the BFD was opened with
92
93 $ enum bfd_direction {no_direction = 0,
94 $ read_direction = 1,
95 $ write_direction = 2,
96 $ both_direction = 3} direction;
97
98 Format_specific flags
99
100 $ flagword flags;
101
102 Currently my_archive is tested before adding origin to anything. I
103 believe that this can become always an add of origin, with origin set
104 to 0 for non archive files.
105
106 $ file_ptr origin;
107
108 Remember when output has begun, to stop strange things happening.
109
110 $ boolean output_has_begun;
111
112 Pointer to linked list of sections
113
114 $ struct sec *sections;
115
116 The number of sections
117
118 $ unsigned int section_count;
119
120 Stuff only usefull for object files:
121 The start address.
122
123 $ bfd_vma start_address;
124 Used for input and output
125
126 $ unsigned int symcount;
127 Symtab for output BFD
128
129 $ struct symbol_cache_entry **outsymbols;
130
131 Architecture of object machine, eg m68k
132
133 $ enum bfd_architecture obj_arch;
134
135 Particular machine within arch, e.g. 68010
136
137 $ unsigned long obj_machine;
138
139 Stuff only usefull for archives:
140
141 $ PTR arelt_data;
142 $ struct _bfd *my_archive;
143 $ struct _bfd *next;
144 $ struct _bfd *archive_head;
145 $ boolean has_armap;
146
147 Used by the back end to hold private data.
148
149 $ PTR tdata;
150
151 Used by the application to hold private data
152
153 $ PTR usrdata;
154
155 Where all the allocated stuff under this BFD goes
156
157 $ struct obstack memory;
158 $};
159
160 *---
161
162 */
163 #include <sysdep.h>
164 #include "bfd.h"
165 #include "libbfd.h"
166
167
168 short _bfd_host_big_endian = 0x0100;
169 /* Accessing the above as (*(char*)&_bfd_host_big_endian), will
170 return 1 if the host is big-endian, 0 otherwise.
171 (assuming that a short is two bytes long!!! FIXME)
172 (See HOST_IS_BIG_ENDIAN_P in bfd.h.) */
173 \f
174 /** Error handling
175 o - Most functions return nonzero on success (check doc for
176 precise semantics); 0 or NULL on error.
177 o - Internal errors are documented by the value of bfd_error.
178 If that is system_call_error then check errno.
179 o - The easiest way to report this to the user is to use bfd_perror.
180 */
181
182 bfd_ec bfd_error = no_error;
183
184 char *bfd_errmsgs[] = { "No error",
185 "System call error",
186 "Invalid target",
187 "File in wrong format",
188 "Invalid operation",
189 "Memory exhausted",
190 "No symbols",
191 "No relocation info",
192 "No more archived files",
193 "Malformed archive",
194 "Symbol not found",
195 "File format not recognized",
196 "File format is ambiguous",
197 "Section has no contents",
198 "Nonrepresentable section on output",
199 "#<Invalid error code>"
200 };
201
202 static
203 void
204 DEFUN(bfd_nonrepresentable_section,(abfd, name),
205 CONST bfd * CONST abfd AND
206 CONST char * CONST name)
207 {
208 printf("bfd error writing file %s, format %s can't represent section %s\n",
209 abfd->filename,
210 abfd->xvec->name,
211 name);
212 exit(1);
213 }
214
215 bfd_error_vector_type bfd_error_vector =
216 {
217 bfd_nonrepresentable_section
218 };
219
220 #if 1 || !defined(ANSI_LIBRARIES) && !defined(__STDC__)
221 char *
222 strerror (code)
223 int code;
224 {
225 extern int sys_nerr;
226 extern char *sys_errlist[];
227
228 return (((code < 0) || (code >= sys_nerr)) ? "(unknown error)" :
229 sys_errlist [code]);
230 }
231 #endif /* not ANSI_LIBRARIES */
232
233
234 char *
235 bfd_errmsg (error_tag)
236 bfd_ec error_tag;
237 {
238 #ifndef errno
239 extern int errno;
240 #endif
241 if (error_tag == system_call_error)
242 return strerror (errno);
243
244 if ((((int)error_tag <(int) no_error) ||
245 ((int)error_tag > (int)invalid_error_code)))
246 error_tag = invalid_error_code;/* sanity check */
247
248 return bfd_errmsgs [(int)error_tag];
249 }
250
251
252 void bfd_default_error_trap(error_tag)
253 bfd_ec error_tag;
254 {
255 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag));
256 }
257
258 void (*bfd_error_trap)() = bfd_default_error_trap;
259 void (*bfd_error_nonrepresentabltrap)() = bfd_default_error_trap;
260
261 void
262 DEFUN(bfd_perror,(message),
263 CONST char *message)
264 {
265 if (bfd_error == system_call_error)
266 perror((char *)message); /* must be system error then... */
267 else {
268 if (message == NULL || *message == '\0')
269 fprintf (stderr, "%s\n", bfd_errmsg (bfd_error));
270 else
271 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_error));
272 }
273 }
274
275 \f
276 /** Symbols */
277
278 /* returns the number of octets of storage required */
279
280 unsigned int
281 get_reloc_upper_bound (abfd, asect)
282 bfd *abfd;
283 sec_ptr asect;
284 {
285 if (abfd->format != bfd_object) {
286 bfd_error = invalid_operation;
287 return 0;
288 }
289
290 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
291 }
292
293 unsigned int
294 bfd_canonicalize_reloc (abfd, asect, location, symbols)
295 bfd *abfd;
296 sec_ptr asect;
297 arelent **location;
298 asymbol **symbols;
299 {
300 if (abfd->format != bfd_object) {
301 bfd_error = invalid_operation;
302 return 0;
303 }
304
305 return BFD_SEND (abfd, _bfd_canonicalize_reloc, (abfd, asect, location, symbols));
306 }
307
308
309 boolean
310 bfd_set_file_flags (abfd, flags)
311 bfd *abfd;
312 flagword flags;
313 {
314 if (abfd->format != bfd_object) {
315 bfd_error = wrong_format;
316 return false;
317 }
318
319 if (bfd_read_p (abfd)) {
320 bfd_error = invalid_operation;
321 return false;
322 }
323
324 if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
325 bfd_error = invalid_operation;
326 return false;
327 }
328
329 bfd_get_file_flags (abfd) = flags;
330 return true;
331 }
332
333
334 void
335 bfd_set_reloc (ignore_abfd, asect, location, count)
336 bfd *ignore_abfd;
337 sec_ptr asect;
338 arelent **location;
339 unsigned int count;
340 {
341 asect->orelocation = location;
342 asect->reloc_count = count;
343 }
344
345 void
346 bfd_assert(file, line)
347 char *file;
348 int line;
349 {
350 printf("bfd assertion fail %s:%d\n",file,line);
351 }
352
353
354 /*proto* bfd_set_start_address
355
356 Marks the entry point of an output BFD. Returns @code{true} on
357 success, @code{false} otherwise.
358
359 *; PROTO(boolean, bfd_set_start_address,(bfd *, bfd_vma));
360 */
361
362 boolean
363 bfd_set_start_address(abfd, vma)
364 bfd *abfd;
365 bfd_vma vma;
366 {
367 abfd->start_address = vma;
368 return true;
369 }
370
371
372 /*proto* bfd_get_mtime
373
374 Return cached file modification time (e.g. as read from archive header
375 for archive members, or from file system if we have been called
376 before); else determine modify time, cache it, and return it.
377
378 *; PROTO(long, bfd_get_mtime, (bfd *));
379
380 */
381
382 long
383 bfd_get_mtime (abfd)
384 bfd *abfd;
385 {
386 FILE *fp;
387 struct stat buf;
388
389 if (abfd->mtime_set)
390 return abfd->mtime;
391
392 fp = bfd_cache_lookup (abfd);
393 if (0 != fstat (fileno (fp), &buf))
394 return 0;
395
396 abfd->mtime_set = true;
397 abfd->mtime = buf.st_mtime;
398 return abfd->mtime;
399 }
400
401 /*proto* stuff
402
403 *+
404
405 #define bfd_sizeof_headers(abfd, reloc) \
406 BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
407
408 #define bfd_find_nearest_line(abfd, section, symbols, offset, filename_ptr, func, line_ptr) \
409 BFD_SEND (abfd, _bfd_find_nearest_line, (abfd, section, symbols, offset, filename_ptr, func, line_ptr))
410
411 #define bfd_debug_info_start(abfd) \
412 BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
413
414 #define bfd_debug_info_end(abfd) \
415 BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
416
417 #define bfd_debug_info_accumulate(abfd, section) \
418 BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
419
420 #define bfd_stat_arch_elt(abfd, stat) \
421 BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
422
423 #define bfd_coff_swap_aux_in(a,e,t,c,i) \
424 BFD_SEND (a, _bfd_coff_swap_aux_in, (a,e,t,c,i))
425
426 #define bfd_coff_swap_sym_in(a,e,i) \
427 BFD_SEND (a, _bfd_coff_swap_sym_in, (a,e,i))
428
429 #define bfd_coff_swap_lineno_in(a,e,i) \
430 BFD_SEND ( a, _bfd_coff_swap_lineno_in, (a,e,i))
431 *-
432
433 */
434
435
436
437
438
439
This page took 0.038816 seconds and 4 git commands to generate.