Fixed minor typos.
[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 @code{libbfd}. References though the BFD and to data in the BFD give the
28 entire BFD functionality.
29
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.
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 a least-recently-used list of
66 BFDs (@pxref{File Caching}).
67
68 $ struct _bfd *lru_prev, *lru_next;
69
70 When a file is closed by the caching routines, BFD retains state
71 information on the file here:
72
73 $ file_ptr where;
74
75 and here:
76
77 $ boolean opened_once;
78
79 $ boolean mtime_set;
80 File modified time
81
82 $ long mtime;
83
84 For output files, channel we locked (is this used?).
85
86 $int ifd;
87
88 The format which belongs to the BFD.
89
90 $ bfd_format format;
91
92 The direction the BFD was opened with
93
94 $ enum bfd_direction {no_direction = 0,
95 $ read_direction = 1,
96 $ write_direction = 2,
97 $ both_direction = 3} direction;
98
99 Format_specific flags
100
101 $ flagword flags;
102
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.
106
107 $ file_ptr origin;
108
109 Remember when output has begun, to stop strange things happening.
110
111 $ boolean output_has_begun;
112
113 Pointer to linked list of sections
114
115 $ struct sec *sections;
116
117 The number of sections
118
119 $ unsigned int section_count;
120
121 Stuff only useful for object files:
122 The start address.
123
124 $ bfd_vma start_address;
125 Used for input and output
126
127 $ unsigned int symcount;
128 Symbol table for output BFD
129
130 $ struct symbol_cache_entry **outsymbols;
131
132 Architecture of object machine, eg m68k
133
134 $ enum bfd_architecture obj_arch;
135
136 Particular machine within arch, e.g. 68010
137
138 $ unsigned long obj_machine;
139
140 Stuff only useful for archives:
141
142 $ PTR arelt_data;
143 $ struct _bfd *my_archive;
144 $ struct _bfd *next;
145 $ struct _bfd *archive_head;
146 $ boolean has_armap;
147
148 Used by the back end to hold private data.
149
150 $ PTR tdata;
151
152 Used by the application to hold private data
153
154 $ PTR usrdata;
155
156 Where all the allocated stuff under this BFD goes (@pxref{Memory Usage}).
157
158 $ struct obstack memory;
159 $};
160
161 *---
162
163 */
164 #include <sysdep.h>
165 #include "bfd.h"
166 #include "libbfd.h"
167
168
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.) */
174 \f
175 /** Error handling
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.
181 */
182
183 bfd_ec bfd_error = no_error;
184
185 char *bfd_errmsgs[] = { "No error",
186 "System call error",
187 "Invalid target",
188 "File in wrong format",
189 "Invalid operation",
190 "Memory exhausted",
191 "No symbols",
192 "No relocation info",
193 "No more archived files",
194 "Malformed archive",
195 "Symbol not found",
196 "File format not recognized",
197 "File format is ambiguous",
198 "Section has no contents",
199 "Nonrepresentable section on output",
200 "#<Invalid error code>"
201 };
202
203 static
204 void
205 DEFUN(bfd_nonrepresentable_section,(abfd, name),
206 CONST bfd * CONST abfd AND
207 CONST char * CONST name)
208 {
209 printf("bfd error writing file %s, format %s can't represent section %s\n",
210 abfd->filename,
211 abfd->xvec->name,
212 name);
213 exit(1);
214 }
215
216 bfd_error_vector_type bfd_error_vector =
217 {
218 bfd_nonrepresentable_section
219 };
220
221 #if 1 || !defined(ANSI_LIBRARIES) && !defined(__STDC__)
222 char *
223 strerror (code)
224 int code;
225 {
226 extern int sys_nerr;
227 extern char *sys_errlist[];
228
229 return (((code < 0) || (code >= sys_nerr)) ? "(unknown error)" :
230 sys_errlist [code]);
231 }
232 #endif /* not ANSI_LIBRARIES */
233
234
235 char *
236 bfd_errmsg (error_tag)
237 bfd_ec error_tag;
238 {
239 #ifndef errno
240 extern int errno;
241 #endif
242 if (error_tag == system_call_error)
243 return strerror (errno);
244
245 if ((((int)error_tag <(int) no_error) ||
246 ((int)error_tag > (int)invalid_error_code)))
247 error_tag = invalid_error_code;/* sanity check */
248
249 return bfd_errmsgs [(int)error_tag];
250 }
251
252
253 void bfd_default_error_trap(error_tag)
254 bfd_ec error_tag;
255 {
256 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag));
257 }
258
259 void (*bfd_error_trap)() = bfd_default_error_trap;
260 void (*bfd_error_nonrepresentabltrap)() = bfd_default_error_trap;
261
262 void
263 DEFUN(bfd_perror,(message),
264 CONST char *message)
265 {
266 if (bfd_error == system_call_error)
267 perror((char *)message); /* must be system error then... */
268 else {
269 if (message == NULL || *message == '\0')
270 fprintf (stderr, "%s\n", bfd_errmsg (bfd_error));
271 else
272 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_error));
273 }
274 }
275
276 \f
277 /** Symbols */
278
279 /* returns the number of octets of storage required */
280
281 unsigned int
282 get_reloc_upper_bound (abfd, asect)
283 bfd *abfd;
284 sec_ptr asect;
285 {
286 if (abfd->format != bfd_object) {
287 bfd_error = invalid_operation;
288 return 0;
289 }
290
291 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
292 }
293
294 unsigned int
295 bfd_canonicalize_reloc (abfd, asect, location, symbols)
296 bfd *abfd;
297 sec_ptr asect;
298 arelent **location;
299 asymbol **symbols;
300 {
301 if (abfd->format != bfd_object) {
302 bfd_error = invalid_operation;
303 return 0;
304 }
305
306 return BFD_SEND (abfd, _bfd_canonicalize_reloc, (abfd, asect, location, symbols));
307 }
308
309
310 boolean
311 bfd_set_file_flags (abfd, flags)
312 bfd *abfd;
313 flagword flags;
314 {
315 if (abfd->format != bfd_object) {
316 bfd_error = wrong_format;
317 return false;
318 }
319
320 if (bfd_read_p (abfd)) {
321 bfd_error = invalid_operation;
322 return false;
323 }
324
325 if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
326 bfd_error = invalid_operation;
327 return false;
328 }
329
330 bfd_get_file_flags (abfd) = flags;
331 return true;
332 }
333
334
335 void
336 bfd_set_reloc (ignore_abfd, asect, location, count)
337 bfd *ignore_abfd;
338 sec_ptr asect;
339 arelent **location;
340 unsigned int count;
341 {
342 asect->orelocation = location;
343 asect->reloc_count = count;
344 }
345
346 void
347 bfd_assert(file, line)
348 char *file;
349 int line;
350 {
351 printf("bfd assertion fail %s:%d\n",file,line);
352 }
353
354
355 /*proto* bfd_set_start_address
356
357 Marks the entry point of an output BFD. Returns @code{true} on
358 success, @code{false} otherwise.
359
360 *; PROTO(boolean, bfd_set_start_address,(bfd *, bfd_vma));
361 */
362
363 boolean
364 bfd_set_start_address(abfd, vma)
365 bfd *abfd;
366 bfd_vma vma;
367 {
368 abfd->start_address = vma;
369 return true;
370 }
371
372
373 /*proto* bfd_get_mtime
374
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.
378
379 *; PROTO(long, bfd_get_mtime, (bfd *));
380
381 */
382
383 long
384 bfd_get_mtime (abfd)
385 bfd *abfd;
386 {
387 FILE *fp;
388 struct stat buf;
389
390 if (abfd->mtime_set)
391 return abfd->mtime;
392
393 fp = bfd_cache_lookup (abfd);
394 if (0 != fstat (fileno (fp), &buf))
395 return 0;
396
397 abfd->mtime_set = true;
398 abfd->mtime = buf.st_mtime;
399 return abfd->mtime;
400 }
401
402 /*proto* stuff
403
404 *+
405
406 #define bfd_sizeof_headers(abfd, reloc) \
407 BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
408
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))
411
412 #define bfd_debug_info_start(abfd) \
413 BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
414
415 #define bfd_debug_info_end(abfd) \
416 BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
417
418 #define bfd_debug_info_accumulate(abfd, section) \
419 BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
420
421 #define bfd_stat_arch_elt(abfd, stat) \
422 BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
423
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))
426
427 #define bfd_coff_swap_sym_in(a,e,i) \
428 BFD_SEND (a, _bfd_coff_swap_sym_in, (a,e,i))
429
430 #define bfd_coff_swap_lineno_in(a,e,i) \
431 BFD_SEND ( a, _bfd_coff_swap_lineno_in, (a,e,i))
432 *-
433
434 */
435
436
437
438
439
440