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