Even more documentation
[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 /*
24 SECTION
25 <<typedef bfd>>
26
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.
32
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.
36
37 .struct _bfd
38 .{
39 The filename the application opened the BFD with.
40
41 . CONST char *filename;
42
43 A pointer to the target jump table.
44
45 . struct bfd_target *xvec;
46
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.
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
60 BFD was opened. This is used to select what matching algorithm
61 to use to chose the back end.
62
63 . boolean target_defaulted;
64
65 The caching routines use these to maintain a
66 least-recently-used list of 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
71 state 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
81 File modified time
82
83 . long mtime;
84
85 Reserved for an unimplemented file locking extension.
86
87 . int ifd;
88
89 The format which belongs to the BFD.
90
91 . bfd_format format;
92
93 The direction the BFD was opened with
94
95 . enum bfd_direction {no_direction = 0,
96 . read_direction = 1,
97 . write_direction = 2,
98 . both_direction = 3} direction;
99
100 Format_specific flags
101
102 . flagword flags;
103
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.
107
108 . file_ptr origin;
109
110 Remember when output has begun, to stop strange things happening.
111
112 . boolean output_has_begun;
113
114 Pointer to linked list of sections
115
116 . struct sec *sections;
117
118 The number of sections
119
120 . unsigned int section_count;
121
122 Stuff only useful for object files:
123 The start address.
124
125 . bfd_vma start_address;
126
127 Used for input and output
128
129 . unsigned int symcount;
130
131 Symbol table for output BFD
132
133 . struct symbol_cache_entry **outsymbols;
134
135 Pointer to structure which contains architecture information
136
137 . struct bfd_arch_info *arch_info;
138
139 Stuff only useful 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 (@pxref{Memory Usage}).
157
158 . struct obstack memory;
159 .};
160
161 */
162 #include "bfd.h"
163 #include "sysdep.h"
164 #include "libbfd.h"
165
166 #undef strerror
167 extern char *strerror();
168
169
170 CONST 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.) */
175 \f
176 /** Error handling
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.
182 */
183
184 bfd_ec bfd_error = no_error;
185
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",
201 "Symbol needs debug section which does not exist",
202 "#<Invalid error code>"
203 };
204
205 static
206 void
207 DEFUN(bfd_nonrepresentable_section,(abfd, name),
208 CONST bfd * CONST abfd AND
209 CONST char * CONST name)
210 {
211 printf("bfd error writing file %s, format %s can't represent section %s\n",
212 abfd->filename,
213 abfd->xvec->name,
214 name);
215 exit(1);
216 }
217
218 bfd_error_vector_type bfd_error_vector =
219 {
220 bfd_nonrepresentable_section
221 };
222
223 char *
224 bfd_errmsg (error_tag)
225 bfd_ec error_tag;
226 {
227 #ifndef errno
228 extern int errno;
229 #endif
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
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;
249
250 void
251 DEFUN(bfd_perror,(message),
252 CONST char *message)
253 {
254 if (bfd_error == system_call_error)
255 perror((char *)message); /* must be system error then... */
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
264 \f
265 /** Symbols */
266
267 /* returns the number of octets of storage required */
268
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
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)
288 {
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));
294 }
295
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 }
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
342 /*
343 FUNCTION
344 bfd_set_start_address
345
346 DESCRIPTION
347
348 Marks the entry point of an output BFD.
349
350 RETURNS
351 Returns <<true>> on success, <<false>> otherwise.
352
353 SYNOPSIS
354 boolean bfd_set_start_address(bfd *, bfd_vma);
355 */
356
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
367 /*
368 FUNCTION
369 bfd_get_mtime
370
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.
376
377 SYNOPSIS
378 long bfd_get_mtime(bfd *);
379 */
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 }
399
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 .
458 */
459
460
461
462
463
464