* binary.c (binary_set_section_contents): Don't return early for a
[deliverable/binutils-gdb.git] / bfd / bfd.c
CommitLineData
6724ff46 1/* Generic BFD library interface and support routines.
871a3f3b 2 Copyright (C) 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
6724ff46 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 18along with this program; if not, write to the Free Software
b7577823 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
6f715d66 20
93351e91
SC
21/*
22SECTION
23 <<typedef bfd>>
6f715d66 24
c188b0be 25 A BFD has type <<bfd>>; objects of this type are the
fcc41f68 26 cornerstone of any application using BFD. Using BFD
c188b0be 27 consists of making references though the BFD and to data in the BFD.
6f715d66 28
c188b0be
DM
29 Here is the structure that defines the type <<bfd>>. It
30 contains the major data about the file and pointers
93351e91 31 to the rest of the data.
6f715d66 32
e98e6ec1
SC
33CODE_FRAGMENT
34.
93351e91
SC
35.struct _bfd
36.{
e98e6ec1
SC
37. {* The filename the application opened the BFD with. *}
38. CONST char *filename;
39.
40. {* A pointer to the target jump table. *}
82735983 41. const struct bfd_target *xvec;
e98e6ec1
SC
42.
43. {* To avoid dragging too many header files into every file that
d3e667e8 44. includes `<<bfd.h>>', IOSTREAM has been declared as a "char
e98e6ec1
SC
45. *", and MTIME as a "long". Their correct types, to which they
46. are cast when used, are "FILE *" and "time_t". The iostream
64d5f5d0
ILT
47. is the result of an fopen on the filename. However, if the
48. BFD_IN_MEMORY flag is set, then iostream is actually a pointer
49. to a bfd_in_memory struct. *}
50. PTR iostream;
e98e6ec1 51.
baf205c4
JG
52. {* Is the file descriptor being cached? That is, can it be closed as
53. needed, and re-opened when accessed later? *}
e98e6ec1
SC
54.
55. boolean cacheable;
56.
57. {* Marks whether there was a default target specified when the
c188b0be
DM
58. BFD was opened. This is used to select which matching algorithm
59. to use to choose the back end. *}
e98e6ec1
SC
60.
61. boolean target_defaulted;
62.
63. {* The caching routines use these to maintain a
64. least-recently-used list of BFDs *}
65.
66. struct _bfd *lru_prev, *lru_next;
67.
68. {* When a file is closed by the caching routines, BFD retains
c188b0be 69. state information on the file here: *}
e98e6ec1
SC
70.
71. file_ptr where;
72.
c188b0be 73. {* and here: (``once'' means at least once) *}
e98e6ec1
SC
74.
75. boolean opened_once;
76.
77. {* Set if we have a locally maintained mtime value, rather than
78. getting it from the file each time: *}
79.
80. boolean mtime_set;
81.
82. {* File modified time, if mtime_set is true: *}
83.
84. long mtime;
85.
86. {* Reserved for an unimplemented file locking extension.*}
87.
88. int ifd;
89.
c188b0be 90. {* The format which belongs to the BFD. (object, core, etc.) *}
e98e6ec1
SC
91.
92. bfd_format format;
93.
94. {* The direction the BFD was opened with*}
95.
96. enum bfd_direction {no_direction = 0,
97. read_direction = 1,
98. write_direction = 2,
99. both_direction = 3} direction;
100.
101. {* Format_specific flags*}
102.
103. flagword flags;
104.
105. {* Currently my_archive is tested before adding origin to
106. anything. I believe that this can become always an add of
107. origin, with origin set to 0 for non archive files. *}
108.
109. file_ptr origin;
110.
111. {* Remember when output has begun, to stop strange things
c188b0be 112. from happening. *}
e98e6ec1
SC
113. boolean output_has_begun;
114.
115. {* Pointer to linked list of sections*}
116. struct sec *sections;
117.
118. {* The number of sections *}
119. unsigned int section_count;
120.
121. {* Stuff only useful for object files:
122. The start address. *}
123. bfd_vma start_address;
124.
125. {* Used for input and output*}
126. unsigned int symcount;
127.
c188b0be 128. {* Symbol table for output BFD (with symcount entries) *}
e98e6ec1
SC
129. struct symbol_cache_entry **outsymbols;
130.
131. {* Pointer to structure which contains architecture information*}
2ffbb79a 132. const struct bfd_arch_info *arch_info;
e98e6ec1
SC
133.
134. {* Stuff only useful for archives:*}
135. PTR arelt_data;
c188b0be
DM
136. struct _bfd *my_archive; {* The containing archive BFD. *}
137. struct _bfd *next; {* The next BFD in the archive. *}
138. struct _bfd *archive_head; {* The first BFD in the archive. *}
e98e6ec1
SC
139. boolean has_armap;
140.
fcc41f68
ILT
141. {* A chain of BFD structures involved in a link. *}
142. struct _bfd *link_next;
143.
144. {* A field used by _bfd_generic_link_add_archive_symbols. This will
145. be used only for archive elements. *}
146. int archive_pass;
147.
e98e6ec1
SC
148. {* Used by the back end to hold private data. *}
149.
150. union
151. {
152. struct aout_data_struct *aout_data;
153. struct artdata *aout_ar_data;
154. struct _oasys_data *oasys_obj_data;
155. struct _oasys_ar_data *oasys_ar_data;
156. struct coff_tdata *coff_obj_data;
b7577823 157. struct pe_tdata *pe_obj_data;
ae0a6bea 158. struct xcoff_tdata *xcoff_obj_data;
515c4292 159. struct ecoff_tdata *ecoff_obj_data;
e98e6ec1
SC
160. struct ieee_data_struct *ieee_data;
161. struct ieee_ar_data_struct *ieee_ar_data;
162. struct srec_data_struct *srec_data;
64d5f5d0 163. struct ihex_data_struct *ihex_data;
d3e667e8
JG
164. struct tekhex_data_struct *tekhex_data;
165. struct elf_obj_tdata *elf_obj_data;
b70cb81e 166. struct nlm_obj_tdata *nlm_obj_data;
e98e6ec1
SC
167. struct bout_data_struct *bout_data;
168. struct sun_core_struct *sun_core_data;
2b74083c 169. struct trad_core_struct *trad_core_data;
baf205c4 170. struct som_data_struct *som_data;
a643e626 171. struct hpux_core_struct *hpux_core_data;
fcc41f68 172. struct hppabsd_core_struct *hppabsd_core_data;
70e00914 173. struct sgi_core_struct *sgi_core_data;
baf205c4
JG
174. struct lynx_core_struct *lynx_core_data;
175. struct osf_core_struct *osf_core_data;
326e32d7 176. struct cisco_core_struct *cisco_core_data;
9675c281 177. struct versados_data_struct *versados_data;
871a3f3b 178. struct netbsd_core_struct *netbsd_core_data;
e98e6ec1
SC
179. PTR any;
180. } tdata;
181.
182. {* Used by the application to hold private data*}
183. PTR usrdata;
184.
185. {* Where all the allocated stuff under this BFD goes *}
186. struct obstack memory;
93351e91 187.};
e98e6ec1 188.
6f715d66 189*/
e98e6ec1 190
4a81b561 191#include "bfd.h"
bbc8d484 192#include "sysdep.h"
9675c281
ILT
193
194#ifdef ANSI_PROTOTYPES
195#include <stdarg.h>
196#else
197#include <varargs.h>
198#endif
199
fcc41f68 200#include "bfdlink.h"
4a81b561 201#include "libbfd.h"
dae31cf5 202#include "coff/internal.h"
34b6a8c3 203#include "coff/sym.h"
48edba81 204#include "libcoff.h"
34b6a8c3 205#include "libecoff.h"
baf205c4 206#undef obj_symbols
ae0a6bea 207#include "elf-bfd.h"
4a81b561 208
b7577823 209#include <ctype.h>
9675c281
ILT
210\f
211/* provide storage for subsystem, stack and heap data which may have been
212 passed in on the command line. Ld puts this data into a bfd_link_info
213 struct which ultimately gets passed in to the bfd. When it arrives, copy
214 it to the following struct so that the data will be available in coffcode.h
215 where it is needed. The typedef's used are defined in bfd.h */
216
9675c281 217
fbb8fe5c
ILT
218\f
219/*
220SECTION
221 Error reporting
222
223 Most BFD functions return nonzero on success (check their
224 individual documentation for precise semantics). On an error,
225 they call <<bfd_set_error>> to set an error condition that callers
226 can check by calling <<bfd_get_error>>.
227 If that returns <<bfd_error_system_call>>, then check
228 <<errno>>.
bbc8d484 229
fbb8fe5c
ILT
230 The easiest way to report a BFD error to the user is to
231 use <<bfd_perror>>.
232
233SUBSECTION
234 Type <<bfd_error_type>>
235
236 The values returned by <<bfd_get_error>> are defined by the
237 enumerated type <<bfd_error_type>>.
238
239CODE_FRAGMENT
240.
241.typedef enum bfd_error
242.{
243. bfd_error_no_error = 0,
244. bfd_error_system_call,
245. bfd_error_invalid_target,
246. bfd_error_wrong_format,
247. bfd_error_invalid_operation,
248. bfd_error_no_memory,
249. bfd_error_no_symbols,
9675c281 250. bfd_error_no_armap,
fbb8fe5c
ILT
251. bfd_error_no_more_archived_files,
252. bfd_error_malformed_archive,
253. bfd_error_file_not_recognized,
254. bfd_error_file_ambiguously_recognized,
255. bfd_error_no_contents,
256. bfd_error_nonrepresentable_section,
257. bfd_error_no_debug_section,
258. bfd_error_bad_value,
259. bfd_error_file_truncated,
9675c281 260. bfd_error_file_too_big,
fbb8fe5c
ILT
261. bfd_error_invalid_error_code
262.} bfd_error_type;
263.
4a81b561
DHW
264*/
265
fbb8fe5c
ILT
266#undef strerror
267extern char *strerror();
268
269static bfd_error_type bfd_error = bfd_error_no_error;
4a81b561 270
34b6a8c3
JK
271CONST char *CONST bfd_errmsgs[] = {
272 "No error",
6f715d66 273 "System call error",
fcc41f68 274 "Invalid bfd target",
6f715d66
SC
275 "File in wrong format",
276 "Invalid operation",
277 "Memory exhausted",
278 "No symbols",
9675c281 279 "Archive has no index; run ranlib to add one",
6f715d66
SC
280 "No more archived files",
281 "Malformed archive",
6f715d66
SC
282 "File format not recognized",
283 "File format is ambiguous",
284 "Section has no contents",
285 "Nonrepresentable section on output",
cbdc7909 286 "Symbol needs debug section which does not exist",
d3e667e8 287 "Bad value",
34b6a8c3 288 "File truncated",
9675c281 289 "File too big",
6f715d66
SC
290 "#<Invalid error code>"
291 };
4a81b561 292
fbb8fe5c
ILT
293/*
294FUNCTION
295 bfd_get_error
296
297SYNOPSIS
298 bfd_error_type bfd_get_error (void);
299
300DESCRIPTION
301 Return the current BFD error condition.
302*/
303
304bfd_error_type
305bfd_get_error ()
306{
307 return bfd_error;
308}
309
310/*
311FUNCTION
312 bfd_set_error
313
314SYNOPSIS
315 void bfd_set_error (bfd_error_type error_tag);
316
317DESCRIPTION
318 Set the BFD error condition to be @var{error_tag}.
319*/
320
321void
322bfd_set_error (error_tag)
323 bfd_error_type error_tag;
324{
325 bfd_error = error_tag;
326}
327
328/*
329FUNCTION
330 bfd_errmsg
331
332SYNOPSIS
333 CONST char *bfd_errmsg (bfd_error_type error_tag);
334
335DESCRIPTION
336 Return a string describing the error @var{error_tag}, or
337 the system error if @var{error_tag} is <<bfd_error_system_call>>.
338*/
339
d3e667e8 340CONST char *
4a81b561 341bfd_errmsg (error_tag)
fbb8fe5c 342 bfd_error_type error_tag;
4a81b561 343{
7ed4093a
SC
344#ifndef errno
345 extern int errno;
346#endif
fbb8fe5c 347 if (error_tag == bfd_error_system_call)
4a81b561
DHW
348 return strerror (errno);
349
fbb8fe5c
ILT
350 if ((((int)error_tag <(int) bfd_error_no_error) ||
351 ((int)error_tag > (int)bfd_error_invalid_error_code)))
352 error_tag = bfd_error_invalid_error_code;/* sanity check */
4a81b561
DHW
353
354 return bfd_errmsgs [(int)error_tag];
355}
356
fbb8fe5c
ILT
357/*
358FUNCTION
359 bfd_perror
360
361SYNOPSIS
362 void bfd_perror (CONST char *message);
363
364DESCRIPTION
365 Print to the standard error stream a string describing the
366 last BFD error that occurred, or the last system error if
367 the last BFD error was a system call failure. If @var{message}
368 is non-NULL and non-empty, the error string printed is preceded
369 by @var{message}, a colon, and a space. It is followed by a newline.
370*/
371
4a81b561 372void
fbb8fe5c
ILT
373bfd_perror (message)
374 CONST char *message;
4a81b561 375{
fbb8fe5c 376 if (bfd_get_error () == bfd_error_system_call)
6f715d66 377 perror((char *)message); /* must be system error then... */
4a81b561
DHW
378 else {
379 if (message == NULL || *message == '\0')
fbb8fe5c 380 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
4a81b561 381 else
fbb8fe5c 382 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
4a81b561
DHW
383 }
384}
385
9675c281
ILT
386/*
387SUBSECTION
388 BFD error handler
389
390 Some BFD functions want to print messages describing the
391 problem. They call a BFD error handler function. This
392 function may be overriden by the program.
393
394 The BFD error handler acts like printf.
395
396CODE_FRAGMENT
397.
398.typedef void (*bfd_error_handler_type) PARAMS ((const char *, ...));
399.
400*/
401
402/* The program name used when printing BFD error messages. */
403
404static const char *_bfd_error_program_name;
405
406/* This is the default routine to handle BFD error messages. */
407
408#ifdef ANSI_PROTOTYPES
409
410static void _bfd_default_error_handler PARAMS ((const char *s, ...));
411
412static void
413_bfd_default_error_handler (const char *s, ...)
414{
415 va_list p;
416
417 if (_bfd_error_program_name != NULL)
418 fprintf (stderr, "%s: ", _bfd_error_program_name);
419
420 va_start (p, s);
421
422 vfprintf (stderr, s, p);
423
424 va_end (p);
425
426 fprintf (stderr, "\n");
427}
428
429#else /* ! defined (ANSI_PROTOTYPES) */
430
431static void _bfd_default_error_handler ();
432
433static void
434_bfd_default_error_handler (va_alist)
435 va_dcl
436{
437 va_list p;
438 const char *s;
439
440 if (_bfd_error_program_name != NULL)
441 fprintf (stderr, "%s: ", _bfd_error_program_name);
442
443 va_start (p);
444
445 s = va_arg (p, const char *);
446 vfprintf (stderr, s, p);
447
448 va_end (p);
449
450 fprintf (stderr, "\n");
451}
452
453#endif /* ! defined (ANSI_PROTOTYPES) */
454
455/* This is a function pointer to the routine which should handle BFD
456 error messages. It is called when a BFD routine encounters an
457 error for which it wants to print a message. Going through a
458 function pointer permits a program linked against BFD to intercept
459 the messages and deal with them itself. */
460
461bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
462
463/*
464FUNCTION
465 bfd_set_error_handler
466
467SYNOPSIS
468 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
469
470DESCRIPTION
471 Set the BFD error handler function. Returns the previous
472 function.
473*/
474
475bfd_error_handler_type
476bfd_set_error_handler (pnew)
477 bfd_error_handler_type pnew;
478{
479 bfd_error_handler_type pold;
480
481 pold = _bfd_error_handler;
482 _bfd_error_handler = pnew;
483 return pold;
484}
485
486/*
487FUNCTION
488 bfd_set_error_program_name
489
490SYNOPSIS
491 void bfd_set_error_program_name (const char *);
492
493DESCRIPTION
494 Set the program name to use when printing a BFD error. This
495 is printed before the error message followed by a colon and
496 space. The string must not be changed after it is passed to
497 this function.
498*/
499
500void
501bfd_set_error_program_name (name)
502 const char *name;
503{
504 _bfd_error_program_name = name;
505}
fbb8fe5c
ILT
506\f
507/*
508SECTION
509 Symbols
510*/
3234eba0
DM
511
512/*
513FUNCTION
514 bfd_get_reloc_upper_bound
515
516SYNOPSIS
326e32d7 517 long bfd_get_reloc_upper_bound(bfd *abfd, asection *sect);
3234eba0
DM
518
519DESCRIPTION
520 Return the number of bytes required to store the
521 relocation information associated with section @var{sect}
326e32d7 522 attached to bfd @var{abfd}. If an error occurs, return -1.
3234eba0
DM
523
524*/
525
526
326e32d7 527long
fbb8fe5c
ILT
528bfd_get_reloc_upper_bound (abfd, asect)
529 bfd *abfd;
530 sec_ptr asect;
3234eba0
DM
531{
532 if (abfd->format != bfd_object) {
fbb8fe5c 533 bfd_set_error (bfd_error_invalid_operation);
326e32d7 534 return -1;
3234eba0
DM
535 }
536
537 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
538}
539
540/*
541FUNCTION
542 bfd_canonicalize_reloc
543
544SYNOPSIS
326e32d7 545 long bfd_canonicalize_reloc
3234eba0
DM
546 (bfd *abfd,
547 asection *sec,
548 arelent **loc,
549 asymbol **syms);
550
551DESCRIPTION
552 Call the back end associated with the open BFD
553 @var{abfd} and translate the external form of the relocation
554 information attached to @var{sec} into the internal canonical
555 form. Place the table into memory at @var{loc}, which has
556 been preallocated, usually by a call to
326e32d7
ILT
557 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
558 -1 on error.
3234eba0
DM
559
560 The @var{syms} table is also needed for horrible internal magic
561 reasons.
562
563
564*/
326e32d7 565long
fbb8fe5c
ILT
566bfd_canonicalize_reloc (abfd, asect, location, symbols)
567 bfd *abfd;
568 sec_ptr asect;
569 arelent **location;
570 asymbol **symbols;
3234eba0
DM
571{
572 if (abfd->format != bfd_object) {
fbb8fe5c 573 bfd_set_error (bfd_error_invalid_operation);
326e32d7 574 return -1;
3234eba0
DM
575 }
576 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
577 (abfd, asect, location, symbols));
578}
579
580/*
581FUNCTION
582 bfd_set_reloc
583
584SYNOPSIS
585 void bfd_set_reloc
586 (bfd *abfd, asection *sec, arelent **rel, unsigned int count)
587
588DESCRIPTION
589 Set the relocation pointer and count within
590 section @var{sec} to the values @var{rel} and @var{count}.
591 The argument @var{abfd} is ignored.
592
593*/
594/*ARGSUSED*/
595void
596bfd_set_reloc (ignore_abfd, asect, location, count)
597 bfd *ignore_abfd;
598 sec_ptr asect;
599 arelent **location;
600 unsigned int count;
601{
602 asect->orelocation = location;
603 asect->reloc_count = count;
604}
4a81b561 605
e98e6ec1
SC
606/*
607FUNCTION
608 bfd_set_file_flags
609
610SYNOPSIS
611 boolean bfd_set_file_flags(bfd *abfd, flagword flags);
612
613DESCRIPTION
c188b0be 614 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
e98e6ec1
SC
615
616 Possible errors are:
fbb8fe5c
ILT
617 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
618 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
619 o <<bfd_error_invalid_operation>> -
e98e6ec1 620 The flag word contained a bit which was not applicable to the
fcc41f68
ILT
621 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
622 on a BFD format which does not support demand paging.
e98e6ec1
SC
623
624*/
625
4a81b561
DHW
626boolean
627bfd_set_file_flags (abfd, flags)
628 bfd *abfd;
629 flagword flags;
630{
631 if (abfd->format != bfd_object) {
fbb8fe5c 632 bfd_set_error (bfd_error_wrong_format);
4a81b561
DHW
633 return false;
634 }
635
636 if (bfd_read_p (abfd)) {
fbb8fe5c 637 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
638 return false;
639 }
640
d3e667e8 641 bfd_get_file_flags (abfd) = flags;
4a81b561 642 if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
fbb8fe5c 643 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
644 return false;
645 }
646
4a81b561
DHW
647return true;
648}
649
4a81b561 650void
7a7fbffb
ILT
651bfd_assert (file, line)
652 const char *file;
653 int line;
4a81b561 654{
ae0a6bea 655 (*_bfd_error_handler) ("bfd assertion fail %s:%d", file, line);
4a81b561
DHW
656}
657
658
93351e91
SC
659/*
660FUNCTION
661 bfd_set_start_address
662
c188b0be
DM
663SYNOPSIS
664 boolean bfd_set_start_address(bfd *abfd, bfd_vma vma);
665
93351e91 666DESCRIPTION
c188b0be 667 Make @var{vma} the entry point of output BFD @var{abfd}.
6f715d66 668
93351e91
SC
669RETURNS
670 Returns <<true>> on success, <<false>> otherwise.
6f715d66
SC
671*/
672
4a81b561
DHW
673boolean
674bfd_set_start_address(abfd, vma)
675bfd *abfd;
676bfd_vma vma;
677{
678 abfd->start_address = vma;
679 return true;
680}
681
682
93351e91
SC
683/*
684FUNCTION
c188b0be 685 bfd_get_mtime
6f715d66 686
93351e91 687SYNOPSIS
c188b0be 688 long bfd_get_mtime(bfd *abfd);
e98e6ec1
SC
689
690DESCRIPTION
c188b0be
DM
691 Return the file modification time (as read from the file system, or
692 from the archive header for archive members).
e98e6ec1 693
6f715d66 694*/
4a81b561
DHW
695
696long
697bfd_get_mtime (abfd)
698 bfd *abfd;
699{
700 FILE *fp;
701 struct stat buf;
702
703 if (abfd->mtime_set)
704 return abfd->mtime;
705
706 fp = bfd_cache_lookup (abfd);
707 if (0 != fstat (fileno (fp), &buf))
708 return 0;
709
e98e6ec1
SC
710 abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */
711 return buf.st_mtime;
4a81b561 712}
6f715d66 713
515c4292
ILT
714/*
715FUNCTION
c188b0be 716 bfd_get_size
515c4292
ILT
717
718SYNOPSIS
c188b0be 719 long bfd_get_size(bfd *abfd);
515c4292
ILT
720
721DESCRIPTION
c188b0be
DM
722 Return the file size (as read from file system) for the file
723 associated with BFD @var{abfd}.
515c4292 724
c188b0be
DM
725 The initial motivation for, and use of, this routine is not
726 so we can get the exact size of the object the BFD applies to, since
727 that might not be generally possible (archive members for example).
728 It would be ideal if someone could eventually modify
515c4292
ILT
729 it so that such results were guaranteed.
730
731 Instead, we want to ask questions like "is this NNN byte sized
732 object I'm about to try read from file offset YYY reasonable?"
c188b0be
DM
733 As as example of where we might do this, some object formats
734 use string tables for which the first <<sizeof(long)>> bytes of the
735 table contain the size of the table itself, including the size bytes.
515c4292
ILT
736 If an application tries to read what it thinks is one of these
737 string tables, without some way to validate the size, and for
738 some reason the size is wrong (byte swapping error, wrong location
c188b0be 739 for the string table, etc.), the only clue is likely to be a read
515c4292 740 error when it tries to read the table, or a "virtual memory
c188b0be 741 exhausted" error when it tries to allocate 15 bazillon bytes
515c4292
ILT
742 of space for the 15 bazillon byte table it is about to read.
743 This function at least allows us to answer the quesion, "is the
744 size reasonable?".
745*/
746
747long
748bfd_get_size (abfd)
749 bfd *abfd;
750{
751 FILE *fp;
752 struct stat buf;
753
64d5f5d0
ILT
754 if ((abfd->flags & BFD_IN_MEMORY) != 0)
755 return ((struct bfd_in_memory *) abfd->iostream)->size;
756
515c4292
ILT
757 fp = bfd_cache_lookup (abfd);
758 if (0 != fstat (fileno (fp), &buf))
759 return 0;
760
761 return buf.st_size;
762}
763
34b6a8c3
JK
764/*
765FUNCTION
c188b0be 766 bfd_get_gp_size
34b6a8c3
JK
767
768SYNOPSIS
c188b0be 769 int bfd_get_gp_size(bfd *abfd);
34b6a8c3
JK
770
771DESCRIPTION
c188b0be 772 Return the maximum size of objects to be optimized using the GP
fcc41f68 773 register under MIPS ECOFF. This is typically set by the <<-G>>
34b6a8c3
JK
774 argument to the compiler, assembler or linker.
775*/
776
777int
778bfd_get_gp_size (abfd)
779 bfd *abfd;
780{
fbb8fe5c
ILT
781 if (abfd->format == bfd_object)
782 {
783 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
784 return ecoff_data (abfd)->gp_size;
785 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
786 return elf_gp_size (abfd);
787 }
34b6a8c3
JK
788 return 0;
789}
790
791/*
792FUNCTION
c188b0be 793 bfd_set_gp_size
34b6a8c3
JK
794
795SYNOPSIS
c188b0be 796 void bfd_set_gp_size(bfd *abfd, int i);
34b6a8c3
JK
797
798DESCRIPTION
799 Set the maximum size of objects to be optimized using the GP
baf205c4 800 register under ECOFF or MIPS ELF. This is typically set by
fcc41f68 801 the <<-G>> argument to the compiler, assembler or linker.
34b6a8c3
JK
802*/
803
804void
805bfd_set_gp_size (abfd, i)
806 bfd *abfd;
807 int i;
808{
fbb8fe5c
ILT
809 /* Don't try to set GP size on an archive or core file! */
810 if (abfd->format != bfd_object)
811 return;
34b6a8c3
JK
812 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
813 ecoff_data (abfd)->gp_size = i;
baf205c4
JG
814 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
815 elf_gp_size (abfd) = i;
816}
817
871a3f3b
C
818/* Get the GP value. This is an internal function used by some of the
819 relocation special_function routines on targets which support a GP
820 register. */
821
822bfd_vma
823_bfd_get_gp_value (abfd)
824 bfd *abfd;
825{
826 if (abfd->format == bfd_object)
827 {
828 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
829 return ecoff_data (abfd)->gp;
830 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
831 return elf_gp (abfd);
832 }
833 return 0;
834}
835
836/* Set the GP value. */
837
838void
839_bfd_set_gp_value (abfd, v)
840 bfd *abfd;
841 bfd_vma v;
842{
843 if (abfd->format != bfd_object)
844 return;
845 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
846 ecoff_data (abfd)->gp = v;
847 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
848 elf_gp (abfd) = v;
849}
850
baf205c4
JG
851/*
852FUNCTION
853 bfd_scan_vma
854
c188b0be
DM
855SYNOPSIS
856 bfd_vma bfd_scan_vma(CONST char *string, CONST char **end, int base);
857
baf205c4 858DESCRIPTION
c188b0be 859 Convert, like <<strtoul>>, a numerical expression
fcc41f68 860 @var{string} into a <<bfd_vma>> integer, and return that integer.
c188b0be 861 (Though without as many bells and whistles as <<strtoul>>.)
fcc41f68 862 The expression is assumed to be unsigned (i.e., positive).
c188b0be 863 If given a @var{base}, it is used as the base for conversion.
baf205c4
JG
864 A base of 0 causes the function to interpret the string
865 in hex if a leading "0x" or "0X" is found, otherwise
866 in octal if a leading zero is found, otherwise in decimal.
867
868 Overflow is not detected.
baf205c4
JG
869*/
870
871bfd_vma
fbb8fe5c
ILT
872bfd_scan_vma (string, end, base)
873 CONST char *string;
874 CONST char **end;
875 int base;
baf205c4
JG
876{
877 bfd_vma value;
878 int digit;
879
880 /* Let the host do it if possible. */
881 if (sizeof(bfd_vma) <= sizeof(unsigned long))
9675c281 882 return (bfd_vma) strtoul (string, (char **) end, base);
baf205c4
JG
883
884 /* A negative base makes no sense, and we only need to go as high as hex. */
885 if ((base < 0) || (base > 16))
886 return (bfd_vma) 0;
887
888 if (base == 0)
889 {
890 if (string[0] == '0')
891 {
892 if ((string[1] == 'x') || (string[1] == 'X'))
893 base = 16;
894 /* XXX should we also allow "0b" or "0B" to set base to 2? */
895 else
896 base = 8;
897 }
898 else
899 base = 10;
900 }
901 if ((base == 16) &&
902 (string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X')))
903 string += 2;
904 /* XXX should we also skip over "0b" or "0B" if base is 2? */
905
906/* Speed could be improved with a table like hex_value[] in gas. */
907#define HEX_VALUE(c) \
908 (isxdigit(c) ? \
909 (isdigit(c) ? \
910 (c - '0') : \
911 (10 + c - (islower(c) ? 'a' : 'A'))) : \
912 42)
913
914 for (value = 0; (digit = HEX_VALUE(*string)) < base; string++)
915 {
916 value = value * base + digit;
917 }
918
919 if (end)
920 *end = string;
921
922 return value;
34b6a8c3
JK
923}
924
326e32d7
ILT
925/*
926FUNCTION
927 bfd_copy_private_bfd_data
928
929SYNOPSIS
930 boolean bfd_copy_private_bfd_data(bfd *ibfd, bfd *obfd);
931
932DESCRIPTION
933 Copy private BFD information from the BFD @var{ibfd} to the
934 the BFD @var{obfd}. Return <<true>> on success, <<false>> on error.
935 Possible error returns are:
936
937 o <<bfd_error_no_memory>> -
938 Not enough memory exists to create private data for @var{obfd}.
939
940.#define bfd_copy_private_bfd_data(ibfd, obfd) \
941. BFD_SEND (ibfd, _bfd_copy_private_bfd_data, \
942. (ibfd, obfd))
943
944*/
945
9675c281
ILT
946/*
947FUNCTION
948 bfd_merge_private_bfd_data
949
950SYNOPSIS
951 boolean bfd_merge_private_bfd_data(bfd *ibfd, bfd *obfd);
952
953DESCRIPTION
954 Merge private BFD information from the BFD @var{ibfd} to the
955 the output file BFD @var{obfd} when linking. Return <<true>>
956 on success, <<false>> on error. Possible error returns are:
957
958 o <<bfd_error_no_memory>> -
959 Not enough memory exists to create private data for @var{obfd}.
960
961.#define bfd_merge_private_bfd_data(ibfd, obfd) \
962. BFD_SEND (ibfd, _bfd_merge_private_bfd_data, \
963. (ibfd, obfd))
964
965*/
966
967/*
968FUNCTION
969 bfd_set_private_flags
970
971SYNOPSIS
972 boolean bfd_set_private_flags(bfd *abfd, flagword flags);
973
974DESCRIPTION
975 Set private BFD flag information in the BFD @var{abfd}.
976 Return <<true>> on success, <<false>> on error. Possible error
977 returns are:
978
979 o <<bfd_error_no_memory>> -
980 Not enough memory exists to create private data for @var{obfd}.
981
982.#define bfd_set_private_flags(abfd, flags) \
983. BFD_SEND (abfd, _bfd_set_private_flags, \
984. (abfd, flags))
985
986*/
987
93351e91
SC
988/*
989FUNCTION
990 stuff
991
992DESCRIPTION
c188b0be 993 Stuff which should be documented:
93351e91
SC
994
995.#define bfd_sizeof_headers(abfd, reloc) \
996. BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
997.
e98e6ec1
SC
998.#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
999. BFD_SEND (abfd, _bfd_find_nearest_line, (abfd, sec, syms, off, file, func, line))
93351e91 1000.
d3e667e8 1001. {* Do these three do anything useful at all, for any back end? *}
93351e91
SC
1002.#define bfd_debug_info_start(abfd) \
1003. BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
1004.
1005.#define bfd_debug_info_end(abfd) \
1006. BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
1007.
1008.#define bfd_debug_info_accumulate(abfd, section) \
1009. BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
1010.
d3e667e8 1011.
93351e91
SC
1012.#define bfd_stat_arch_elt(abfd, stat) \
1013. BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
1014.
82735983
ILT
1015.#define bfd_update_armap_timestamp(abfd) \
1016. BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
1017.
93351e91
SC
1018.#define bfd_set_arch_mach(abfd, arch, mach)\
1019. BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
1020.
326e32d7
ILT
1021.#define bfd_relax_section(abfd, section, link_info, again) \
1022. BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
fcc41f68
ILT
1023.
1024.#define bfd_link_hash_table_create(abfd) \
1025. BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
1026.
1027.#define bfd_link_add_symbols(abfd, info) \
1028. BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
1029.
1030.#define bfd_final_link(abfd, info) \
1031. BFD_SEND (abfd, _bfd_final_link, (abfd, info))
34b6a8c3 1032.
9deaaaf1
ILT
1033.#define bfd_free_cached_info(abfd) \
1034. BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
1035.
82735983
ILT
1036.#define bfd_get_dynamic_symtab_upper_bound(abfd) \
1037. BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
1038.
b7577823
ILT
1039.#define bfd_print_private_bfd_data(abfd, file)\
1040. BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
1041.
82735983
ILT
1042.#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
1043. BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
1044.
1045.#define bfd_get_dynamic_reloc_upper_bound(abfd) \
1046. BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
1047.
1048.#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
1049. BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
1050.
9675c281
ILT
1051.extern bfd_byte *bfd_get_relocated_section_contents
1052. PARAMS ((bfd *, struct bfd_link_info *,
1053. struct bfd_link_order *, bfd_byte *,
1054. boolean, asymbol **));
1055.
6590a8c9 1056
6f715d66 1057*/
9675c281
ILT
1058
1059bfd_byte *
1060bfd_get_relocated_section_contents (abfd, link_info, link_order, data,
1061 relocateable, symbols)
1062 bfd *abfd;
1063 struct bfd_link_info *link_info;
1064 struct bfd_link_order *link_order;
1065 bfd_byte *data;
1066 boolean relocateable;
1067 asymbol **symbols;
1068{
1069 bfd *abfd2;
1070 bfd_byte *(*fn) PARAMS ((bfd *, struct bfd_link_info *,
1071 struct bfd_link_order *, bfd_byte *, boolean,
1072 asymbol **));
1073
1074 if (link_order->type == bfd_indirect_link_order)
1075 {
1076 abfd2 = link_order->u.indirect.section->owner;
1077 if (abfd2 == 0)
1078 abfd2 = abfd;
1079 }
1080 else
1081 abfd2 = abfd;
1082 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
1083
1084 return (*fn) (abfd, link_info, link_order, data, relocateable, symbols);
1085}
b7577823 1086
ae0a6bea 1087/* Record information about an ELF program header. */
b7577823 1088
ae0a6bea
ILT
1089boolean
1090bfd_record_phdr (abfd, type, flags_valid, flags, at_valid, at,
1091 includes_filehdr, includes_phdrs, count, secs)
1092 bfd *abfd;
1093 unsigned long type;
1094 boolean flags_valid;
1095 flagword flags;
1096 boolean at_valid;
1097 bfd_vma at;
1098 boolean includes_filehdr;
1099 boolean includes_phdrs;
1100 unsigned int count;
1101 asection **secs;
1102{
1103 struct elf_segment_map *m, **pm;
1104
1105 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1106 return true;
b7577823 1107
ae0a6bea
ILT
1108 m = ((struct elf_segment_map *)
1109 bfd_alloc (abfd,
1110 (sizeof (struct elf_segment_map)
1111 + (count - 1) * sizeof (asection *))));
1112 if (m == NULL)
1113 return false;
1114
1115 m->next = NULL;
1116 m->p_type = type;
1117 m->p_flags = flags;
1118 m->p_paddr = at;
1119 m->p_flags_valid = flags_valid;
1120 m->p_paddr_valid = at_valid;
1121 m->includes_filehdr = includes_filehdr;
1122 m->includes_phdrs = includes_phdrs;
1123 m->count = count;
1124 if (count > 0)
1125 memcpy (m->sections, secs, count * sizeof (asection *));
1126
1127 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
1128 ;
1129 *pm = m;
1130
1131 return true;
1132}
This page took 0.28838 seconds and 4 git commands to generate.