]> Git Repo - binutils.git/blame - bfd/bfd.c
* srec.c: Better error checking, partly from Peter Schauer.
[binutils.git] / bfd / bfd.c
CommitLineData
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 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
6724ff46 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
6724ff46
RP
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
6724ff46 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
6724ff46
RP
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
6f715d66 20
4a81b561 21/* $Id$ */
6f715d66
SC
22
23/*proto*
b1c86ef1 24@section @code{typedef bfd}
6f715d66 25
b1c86ef1
SEF
26A BFD is has type @code{bfd}; objects of this type are the cornerstone
27of any application using @code{libbfd}. References though the BFD and
28to data in the BFD give the entire BFD functionality.
6f715d66 29
b1c86ef1
SEF
30Here is the struct used to define the type @code{bfd}. This contains
31the major data about the file, and contains pointers to the rest of
32the data.
6f715d66
SC
33
34*+++
35
36$struct _bfd
37${
6724ff46 38 The filename the application opened the BFD with.
6f715d66
SC
39
40$ CONST char *filename;
41
42A pointer to the target jump table.
43
44$ struct bfd_target *xvec;
45
46
47To avoid dragging too many header files into every file that
6724ff46 48includes @file{bfd.h}, IOSTREAM has been declared as a "char *", and MTIME
6f715d66
SC
49as a "long". Their correct types, to which they are cast when used,
50are "FILE *" and "time_t".
51
52The iostream is the result of an fopen on the filename.
53
54$ char *iostream;
55
56Is the file being cached @xref{File Caching}.
57
58$ boolean cacheable;
59
6724ff46 60Marks whether there was a default target specified when the BFD was
6f715d66
SC
61opened. This is used to select what matching algorithm to use to chose
62the back end.
63
64$ boolean target_defaulted;
65
188d6d22
RP
66The caching routines use these to maintain a least-recently-used list of
67BFDs (@pxref{File Caching}).
6f715d66
SC
68
69$ struct _bfd *lru_prev, *lru_next;
70
188d6d22
RP
71When a file is closed by the caching routines, BFD retains state
72information on the file here:
6f715d66
SC
73
74$ file_ptr where;
75
76and here:
77
78$ boolean opened_once;
79
80$ boolean mtime_set;
81File modified time
82
83$ long mtime;
84
b1c86ef1 85Reserved for an unimplemented file locking extension.
6f715d66
SC
86
87$int ifd;
88
6724ff46 89The format which belongs to the BFD.
6f715d66
SC
90
91$ bfd_format format;
92
6724ff46 93The direction the BFD was opened with
6f715d66
SC
94
95$ enum bfd_direction {no_direction = 0,
96$ read_direction = 1,
97$ write_direction = 2,
98$ both_direction = 3} direction;
99
100Format_specific flags
101
102$ flagword flags;
103
104Currently my_archive is tested before adding origin to anything. I
105believe that this can become always an add of origin, with origin set
106to 0 for non archive files.
107
108$ file_ptr origin;
109
110Remember when output has begun, to stop strange things happening.
111
112$ boolean output_has_begun;
113
114Pointer to linked list of sections
115
116$ struct sec *sections;
117
118The number of sections
119
120$ unsigned int section_count;
121
188d6d22 122Stuff only useful for object files:
6f715d66
SC
123The start address.
124
125$ bfd_vma start_address;
126Used for input and output
127
128$ unsigned int symcount;
188d6d22 129Symbol table for output BFD
6f715d66
SC
130
131$ struct symbol_cache_entry **outsymbols;
132
133Architecture of object machine, eg m68k
134
135$ enum bfd_architecture obj_arch;
136
137Particular machine within arch, e.g. 68010
138
139$ unsigned long obj_machine;
140
188d6d22 141Stuff only useful for archives:
6f715d66
SC
142
143$ PTR arelt_data;
144$ struct _bfd *my_archive;
145$ struct _bfd *next;
146$ struct _bfd *archive_head;
147$ boolean has_armap;
148
149Used by the back end to hold private data.
150
151$ PTR tdata;
152
153Used by the application to hold private data
154
155$ PTR usrdata;
156
188d6d22 157Where all the allocated stuff under this BFD goes (@pxref{Memory Usage}).
6f715d66
SC
158
159$ struct obstack memory;
160$};
161
162*---
163
164*/
7ed4093a 165#include <sysdep.h>
4a81b561
DHW
166#include "bfd.h"
167#include "libbfd.h"
168
6f715d66 169
4a81b561 170short _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
184bfd_ec bfd_error = no_error;
185
6f715d66
SC
186char *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 "#<Invalid error code>"
202 };
4a81b561 203
9846338e
SC
204static
205void
206DEFUN(bfd_nonrepresentable_section,(abfd, name),
6f715d66
SC
207 CONST bfd * CONST abfd AND
208 CONST char * CONST name)
9846338e 209{
1f4d3c79 210 printf("bfd error writing file %s, format %s can't represent section %s\n",
6f715d66
SC
211 abfd->filename,
212 abfd->xvec->name,
213 name);
9846338e
SC
214 exit(1);
215}
fc723380 216
9846338e
SC
217bfd_error_vector_type bfd_error_vector =
218 {
219 bfd_nonrepresentable_section
220 };
221
b1c86ef1 222#if !defined(ANSI_LIBRARIES) && !defined(__STDC__) || HOST_SYS==SUN4_SYS
4a81b561
DHW
223char *
224strerror (code)
225 int code;
226{
227 extern int sys_nerr;
228 extern char *sys_errlist[];
229
230 return (((code < 0) || (code >= sys_nerr)) ? "(unknown error)" :
6f715d66 231 sys_errlist [code]);
4a81b561
DHW
232}
233#endif /* not ANSI_LIBRARIES */
234
9846338e 235
4a81b561
DHW
236char *
237bfd_errmsg (error_tag)
238 bfd_ec error_tag;
239{
7ed4093a
SC
240#ifndef errno
241 extern int errno;
242#endif
4a81b561
DHW
243 if (error_tag == system_call_error)
244 return strerror (errno);
245
246 if ((((int)error_tag <(int) no_error) ||
247 ((int)error_tag > (int)invalid_error_code)))
248 error_tag = invalid_error_code;/* sanity check */
249
250 return bfd_errmsgs [(int)error_tag];
251}
252
9846338e
SC
253
254void bfd_default_error_trap(error_tag)
255bfd_ec error_tag;
256{
257 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag));
258}
259
260void (*bfd_error_trap)() = bfd_default_error_trap;
261void (*bfd_error_nonrepresentabltrap)() = bfd_default_error_trap;
fc723380 262
4a81b561 263void
1f4d3c79
SC
264DEFUN(bfd_perror,(message),
265 CONST char *message)
4a81b561
DHW
266{
267 if (bfd_error == system_call_error)
6f715d66 268 perror((char *)message); /* must be system error then... */
4a81b561
DHW
269 else {
270 if (message == NULL || *message == '\0')
271 fprintf (stderr, "%s\n", bfd_errmsg (bfd_error));
272 else
273 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_error));
274 }
275}
276
6f715d66 277 \f
4a81b561
DHW
278/** Symbols */
279
4a81b561 280/* returns the number of octets of storage required */
6f715d66 281
4a81b561
DHW
282unsigned int
283get_reloc_upper_bound (abfd, asect)
284 bfd *abfd;
285 sec_ptr asect;
286{
287 if (abfd->format != bfd_object) {
288 bfd_error = invalid_operation;
289 return 0;
290 }
291
292 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
293}
294
295unsigned int
296bfd_canonicalize_reloc (abfd, asect, location, symbols)
297 bfd *abfd;
298 sec_ptr asect;
299 arelent **location;
300 asymbol **symbols;
301{
302 if (abfd->format != bfd_object) {
303 bfd_error = invalid_operation;
304 return 0;
305 }
306
307 return BFD_SEND (abfd, _bfd_canonicalize_reloc, (abfd, asect, location, symbols));
308}
309
4a81b561
DHW
310
311boolean
312bfd_set_file_flags (abfd, flags)
313 bfd *abfd;
314 flagword flags;
315{
316 if (abfd->format != bfd_object) {
317 bfd_error = wrong_format;
318 return false;
319 }
320
321 if (bfd_read_p (abfd)) {
322 bfd_error = invalid_operation;
323 return false;
324 }
325
326 if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
327 bfd_error = invalid_operation;
328 return false;
329 }
330
331 bfd_get_file_flags (abfd) = flags;
332return true;
333}
334
335
336void
337bfd_set_reloc (ignore_abfd, asect, location, count)
338 bfd *ignore_abfd;
339 sec_ptr asect;
340 arelent **location;
341 unsigned int count;
342{
343 asect->orelocation = location;
344 asect->reloc_count = count;
345}
4a81b561
DHW
346
347void
348bfd_assert(file, line)
349char *file;
350int line;
351{
352 printf("bfd assertion fail %s:%d\n",file,line);
353}
354
355
6f715d66
SC
356/*proto* bfd_set_start_address
357
6724ff46 358Marks the entry point of an output BFD. Returns @code{true} on
6f715d66
SC
359success, @code{false} otherwise.
360
361*; PROTO(boolean, bfd_set_start_address,(bfd *, bfd_vma));
362*/
363
4a81b561
DHW
364boolean
365bfd_set_start_address(abfd, vma)
366bfd *abfd;
367bfd_vma vma;
368{
369 abfd->start_address = vma;
370 return true;
371}
372
373
6f715d66
SC
374/*proto* bfd_get_mtime
375
376Return cached file modification time (e.g. as read from archive header
377for archive members, or from file system if we have been called
378before); else determine modify time, cache it, and return it.
4a81b561 379
41f50af0 380*; PROTO(long, bfd_get_mtime, (bfd *));
6f715d66
SC
381
382*/
4a81b561
DHW
383
384long
385bfd_get_mtime (abfd)
386 bfd *abfd;
387{
388 FILE *fp;
389 struct stat buf;
390
391 if (abfd->mtime_set)
392 return abfd->mtime;
393
394 fp = bfd_cache_lookup (abfd);
395 if (0 != fstat (fileno (fp), &buf))
396 return 0;
397
398 abfd->mtime_set = true;
399 abfd->mtime = buf.st_mtime;
400 return abfd->mtime;
401}
6f715d66 402
6724ff46
RP
403/*proto* stuff
404
6f715d66 405*+
6724ff46 406
6f715d66
SC
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))
8fd6eb08
SC
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))
6f715d66
SC
433*-
434
435*/
436
437
438
439
440
441
This page took 0.103918 seconds and 4 git commands to generate.