1 /* Generic BFD library interface and support routines.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 /*
24 SECTION
25 <<typedef bfd>>
26
27 A BFD has type <<bfd>>; objects of this type are the
28 cornerstone of any application using BFD. Using BFD
29 consists of making references though the BFD and to data in the BFD.
30
31 Here is the structure that defines the type <<bfd>>. It
32 contains the major data about the file and pointers
33 to the rest of the data.
34
35 CODE_FRAGMENT
36 .
37 .struct bfd
38 .{
39 . {* A unique identifier of the BFD *}
40 . unsigned int id;
41 .
42 . {* The filename the application opened the BFD with. *}
43 . const char *filename;
44 .
45 . {* A pointer to the target jump table. *}
46 . const struct bfd_target *xvec;
47 .
48 . {* The IOSTREAM, and corresponding IO vector that provide access
49 . to the file backing the BFD. *}
50 . void *iostream;
51 . const struct bfd_iovec *iovec;
52 .
53 . {* Is the file descriptor being cached? That is, can it be closed as
54 . needed, and re-opened when accessed later? *}
55 . bfd_boolean cacheable;
56 .
57 . {* Marks whether there was a default target specified when the
58 . BFD was opened. This is used to select which matching algorithm
59 . to use to choose the back end. *}
60 . bfd_boolean target_defaulted;
61 .
62 . {* The caching routines use these to maintain a
63 . least-recently-used list of BFDs. *}
64 . struct bfd *lru_prev, *lru_next;
65 .
66 . {* When a file is closed by the caching routines, BFD retains
67 . state information on the file here... *}
68 . ufile_ptr where;
69 .
70 . {* ... and here: (``once'' means at least once). *}
71 . bfd_boolean opened_once;
72 .
73 . {* Set if we have a locally maintained mtime value, rather than
74 . getting it from the file each time. *}
75 . bfd_boolean mtime_set;
76 .
77 . {* File modified time, if mtime_set is TRUE. *}
78 . long mtime;
79 .
80 . {* Reserved for an unimplemented file locking extension. *}
81 . int ifd;
82 .
83 . {* The format which belongs to the BFD. (object, core, etc.) *}
84 . bfd_format format;
85 .
86 . {* The direction with which the BFD was opened. *}
87 . enum bfd_direction
88 . {
89 . no_direction = 0,
90 . read_direction = 1,
91 . write_direction = 2,
92 . both_direction = 3
93 . }
94 . direction;
95 .
96 . {* Format_specific flags. *}
97 . flagword flags;
98 .
99 . {* Currently my_archive is tested before adding origin to
100 . anything. I believe that this can become always an add of
101 . origin, with origin set to 0 for non archive files. *}
102 . ufile_ptr origin;
103 .
104 . {* Remember when output has begun, to stop strange things
105 . from happening. *}
106 . bfd_boolean output_has_begun;
107 .
108 . {* A hash table for section names. *}
109 . struct bfd_hash_table section_htab;
110 .
111 . {* Pointer to linked list of sections. *}
112 . struct bfd_section *sections;
113 .
114 . {* The last section on the section list. *}
115 . struct bfd_section *section_last;
116 .
117 . {* The number of sections. *}
118 . unsigned int section_count;
119 .
120 . {* Stuff only useful for object files:
121 . The start address. *}
122 . bfd_vma start_address;
123 .
124 . {* Used for input and output. *}
125 . unsigned int symcount;
126 .
127 . {* Symbol table for output BFD (with symcount entries). *}
128 . struct bfd_symbol **outsymbols;
129 .
130 . {* Used for slurped dynamic symbol tables. *}
131 . unsigned int dynsymcount;
132 .
133 . {* Pointer to structure which contains architecture information. *}
134 . const struct bfd_arch_info *arch_info;
135 .
136 . {* Flag set if symbols from this BFD should not be exported. *}
137 . bfd_boolean no_export;
138 .
139 . {* Stuff only useful for archives. *}
140 . void *arelt_data;
141 . struct bfd *my_archive; {* The containing archive BFD. *}
142 . struct bfd *next; {* The next BFD in the archive. *}
143 . struct bfd *archive_head; {* The first BFD in the archive. *}
144 . bfd_boolean has_armap;
145 .
146 . {* A chain of BFD structures involved in a link. *}
147 . struct bfd *link_next;
148 .
149 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
150 . be used only for archive elements. *}
151 . int archive_pass;
152 .
153 . {* Used by the back end to hold private data. *}
154 . union
155 . {
156 . struct aout_data_struct *aout_data;
157 . struct artdata *aout_ar_data;
158 . struct _oasys_data *oasys_obj_data;
159 . struct _oasys_ar_data *oasys_ar_data;
160 . struct coff_tdata *coff_obj_data;
161 . struct pe_tdata *pe_obj_data;
162 . struct xcoff_tdata *xcoff_obj_data;
163 . struct ecoff_tdata *ecoff_obj_data;
164 . struct ieee_data_struct *ieee_data;
165 . struct ieee_ar_data_struct *ieee_ar_data;
166 . struct srec_data_struct *srec_data;
167 . struct ihex_data_struct *ihex_data;
168 . struct tekhex_data_struct *tekhex_data;
169 . struct elf_obj_tdata *elf_obj_data;
170 . struct nlm_obj_tdata *nlm_obj_data;
171 . struct bout_data_struct *bout_data;
172 . struct mmo_data_struct *mmo_data;
173 . struct sun_core_struct *sun_core_data;
174 . struct sco5_core_struct *sco5_core_data;
175 . struct trad_core_struct *trad_core_data;
176 . struct som_data_struct *som_data;
177 . struct hpux_core_struct *hpux_core_data;
178 . struct hppabsd_core_struct *hppabsd_core_data;
179 . struct sgi_core_struct *sgi_core_data;
180 . struct lynx_core_struct *lynx_core_data;
181 . struct osf_core_struct *osf_core_data;
182 . struct cisco_core_struct *cisco_core_data;
183 . struct versados_data_struct *versados_data;
184 . struct netbsd_core_struct *netbsd_core_data;
185 . struct mach_o_data_struct *mach_o_data;
186 . struct mach_o_fat_data_struct *mach_o_fat_data;
187 . struct bfd_pef_data_struct *pef_data;
188 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
189 . struct bfd_sym_data_struct *sym_data;
190 . void *any;
191 . }
192 . tdata;
193 .
194 . {* Used by the application to hold private data. *}
195 . void *usrdata;
196 .
197 . {* Where all the allocated stuff under this BFD goes. This is a
198 . struct objalloc *, but we use void * to avoid requiring the inclusion
199 . of objalloc.h. *}
200 . void *memory;
201 .};
202 .
203 */
204
205 #include "bfd.h"
206 #include "bfdver.h"
207 #include "sysdep.h"
208 #include <stdarg.h>
209 #include "libiberty.h"
210 #include "safe-ctype.h"
211 #include "bfdlink.h"
212 #include "libbfd.h"
213 #include "coff/internal.h"
214 #include "coff/sym.h"
215 #include "libcoff.h"
216 #include "libecoff.h"
217 #undef obj_symbols
218 #include "elf-bfd.h"
219
220 __RCSID("$MirOS: src/gnu/usr.bin/binutils/bfd/bfd.c,v 1.6 2006/10/17 22:17:04 tg Exp $");
221
222 /* provide storage for subsystem, stack and heap data which may have been
223 passed in on the command line. Ld puts this data into a bfd_link_info
224 struct which ultimately gets passed in to the bfd. When it arrives, copy
225 it to the following struct so that the data will be available in coffcode.h
226 where it is needed. The typedef's used are defined in bfd.h */
227
228 /*
229 SECTION
230 Error reporting
231
232 Most BFD functions return nonzero on success (check their
233 individual documentation for precise semantics). On an error,
234 they call <<bfd_set_error>> to set an error condition that callers
235 can check by calling <<bfd_get_error>>.
236 If that returns <<bfd_error_system_call>>, then check
237 <<errno>>.
238
239 The easiest way to report a BFD error to the user is to
240 use <<bfd_perror>>.
241
242 SUBSECTION
243 Type <<bfd_error_type>>
244
245 The values returned by <<bfd_get_error>> are defined by the
246 enumerated type <<bfd_error_type>>.
247
248 CODE_FRAGMENT
249 .
250 .typedef enum bfd_error
251 .{
252 . bfd_error_no_error = 0,
253 . bfd_error_system_call,
254 . bfd_error_invalid_target,
255 . bfd_error_wrong_format,
256 . bfd_error_wrong_object_format,
257 . bfd_error_invalid_operation,
258 . bfd_error_no_memory,
259 . bfd_error_no_symbols,
260 . bfd_error_no_armap,
261 . bfd_error_no_more_archived_files,
262 . bfd_error_malformed_archive,
263 . bfd_error_file_not_recognized,
264 . bfd_error_file_ambiguously_recognized,
265 . bfd_error_no_contents,
266 . bfd_error_nonrepresentable_section,
267 . bfd_error_no_debug_section,
268 . bfd_error_bad_value,
269 . bfd_error_file_truncated,
270 . bfd_error_file_too_big,
271 . bfd_error_invalid_error_code
272 .}
273 .bfd_error_type;
274 .
275 */
276
277 static bfd_error_type bfd_error = bfd_error_no_error;
278
279 const char *const bfd_errmsgs[] =
280 {
281 N_("No error"),
282 N_("System call error"),
283 N_("Invalid bfd target"),
284 N_("File in wrong format"),
285 N_("Archive object file in wrong format"),
286 N_("Invalid operation"),
287 N_("Memory exhausted"),
288 N_("No symbols"),
289 N_("Archive has no index; run ranlib to add one"),
290 N_("No more archived files"),
291 N_("Malformed archive"),
292 N_("File format not recognized"),
293 N_("File format is ambiguous"),
294 N_("Section has no contents"),
295 N_("Nonrepresentable section on output"),
296 N_("Symbol needs debug section which does not exist"),
297 N_("Bad value"),
298 N_("File truncated"),
299 N_("File too big"),
300 N_("#<Invalid error code>")
301 };
302
303 /*
304 FUNCTION
305 bfd_get_error
306
307 SYNOPSIS
308 bfd_error_type bfd_get_error (void);
309
310 DESCRIPTION
311 Return the current BFD error condition.
312 */
313
314 bfd_error_type
bfd_get_error(void)315 bfd_get_error (void)
316 {
317 return bfd_error;
318 }
319
320 /*
321 FUNCTION
322 bfd_set_error
323
324 SYNOPSIS
325 void bfd_set_error (bfd_error_type error_tag);
326
327 DESCRIPTION
328 Set the BFD error condition to be @var{error_tag}.
329 */
330
331 void
bfd_set_error(bfd_error_type error_tag)332 bfd_set_error (bfd_error_type error_tag)
333 {
334 bfd_error = error_tag;
335 }
336
337 /*
338 FUNCTION
339 bfd_errmsg
340
341 SYNOPSIS
342 const char *bfd_errmsg (bfd_error_type error_tag);
343
344 DESCRIPTION
345 Return a string describing the error @var{error_tag}, or
346 the system error if @var{error_tag} is <<bfd_error_system_call>>.
347 */
348
349 const char *
bfd_errmsg(bfd_error_type error_tag)350 bfd_errmsg (bfd_error_type error_tag)
351 {
352 #ifndef errno
353 extern int errno;
354 #endif
355 if (error_tag == bfd_error_system_call)
356 return xstrerror (errno);
357
358 if (error_tag > bfd_error_invalid_error_code)
359 error_tag = bfd_error_invalid_error_code; /* sanity check */
360
361 return _(bfd_errmsgs [error_tag]);
362 }
363
364 /*
365 FUNCTION
366 bfd_perror
367
368 SYNOPSIS
369 void bfd_perror (const char *message);
370
371 DESCRIPTION
372 Print to the standard error stream a string describing the
373 last BFD error that occurred, or the last system error if
374 the last BFD error was a system call failure. If @var{message}
375 is non-NULL and non-empty, the error string printed is preceded
376 by @var{message}, a colon, and a space. It is followed by a newline.
377 */
378
379 void
bfd_perror(const char * message)380 bfd_perror (const char *message)
381 {
382 if (bfd_get_error () == bfd_error_system_call)
383 /* Must be a system error then. */
384 perror ((char *) message);
385 else
386 {
387 if (message == NULL || *message == '\0')
388 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
389 else
390 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
391 }
392 }
393
394 /*
395 SUBSECTION
396 BFD error handler
397
398 Some BFD functions want to print messages describing the
399 problem. They call a BFD error handler function. This
400 function may be overridden by the program.
401
402 The BFD error handler acts like printf.
403
404 CODE_FRAGMENT
405 .
406 .typedef void (*bfd_error_handler_type) (const char *, ...);
407 .
408 */
409
410 /* The program name used when printing BFD error messages. */
411
412 static const char *_bfd_error_program_name;
413
414 /* This is the default routine to handle BFD error messages.
415 Like fprintf (stderr, ...), but also handles some extra format specifiers.
416
417 %A section name from section. For group components, print group name too.
418 %B file name from bfd. For archive components, prints archive too.
419 */
420
421 void
_bfd_default_error_handler(const char * fmt,...)422 _bfd_default_error_handler (const char *fmt, ...)
423 {
424 va_list ap;
425 char *bufp;
426 const char *new_fmt, *p;
427 size_t avail = 1000;
428 char buf[1000];
429
430 if (_bfd_error_program_name != NULL)
431 fprintf (stderr, "%s: ", _bfd_error_program_name);
432 else
433 fprintf (stderr, "BFD: ");
434
435 va_start (ap, fmt);
436 new_fmt = fmt;
437 bufp = buf;
438
439 /* Reserve enough space for the existing format string. */
440 avail -= strlen (fmt) + 1;
441 if (avail > 1000)
442 abort ();
443
444 p = fmt;
445 while (1)
446 {
447 char *q;
448 size_t len, extra, trim;
449
450 p = strchr (p, '%');
451 if (p == NULL || p[1] == '\0')
452 {
453 if (new_fmt == buf)
454 {
455 len = strlen (fmt);
456 memcpy (bufp, fmt, len + 1);
457 }
458 break;
459 }
460
461 if (p[1] == 'A' || p[1] == 'B')
462 {
463 len = p - fmt;
464 memcpy (bufp, fmt, len);
465 bufp += len;
466 fmt = p + 2;
467 new_fmt = buf;
468
469 /* If we run out of space, tough, you lose your ridiculously
470 long file or section name. It's not safe to try to alloc
471 memory here; We might be printing an out of memory message. */
472 if (avail == 0)
473 {
474 *bufp++ = '*';
475 *bufp++ = '*';
476 *bufp = '\0';
477 }
478 else
479 {
480 if (p[1] == 'B')
481 {
482 bfd *abfd = va_arg (ap, bfd *);
483 if (abfd->my_archive)
484 snprintf (bufp, avail, "%s(%s)",
485 abfd->my_archive->filename, abfd->filename);
486 else
487 snprintf (bufp, avail, "%s", abfd->filename);
488 }
489 else
490 {
491 asection *sec = va_arg (ap, asection *);
492 bfd *abfd = sec->owner;
493 const char *group = NULL;
494 struct coff_comdat_info *ci;
495
496 if (abfd != NULL
497 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
498 && elf_next_in_group (sec) != NULL
499 && (sec->flags & SEC_GROUP) == 0)
500 group = elf_group_name (sec);
501 else if (abfd != NULL
502 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
503 && (ci = bfd_coff_get_comdat_section (sec->owner,
504 sec)) != NULL)
505 group = ci->name;
506 if (group != NULL)
507 snprintf (bufp, avail, "%s[%s]", sec->name, group);
508 else
509 snprintf (bufp, avail, "%s", sec->name);
510 }
511 len = strlen (bufp);
512 avail = avail - len + 2;
513
514 /* We need to replace any '%' we printed by "%%".
515 First count how many. */
516 q = bufp;
517 bufp += len;
518 extra = 0;
519 while ((q = strchr (q, '%')) != NULL)
520 {
521 ++q;
522 ++extra;
523 }
524
525 /* If there isn't room, trim off the end of the string. */
526 q = bufp;
527 bufp += extra;
528 if (extra > avail)
529 {
530 trim = extra - avail;
531 bufp -= trim;
532 do
533 {
534 if (*--q == '%')
535 --extra;
536 }
537 while (--trim != 0);
538 *q = '\0';
539 avail = extra;
540 }
541 avail -= extra;
542
543 /* Now double all '%' chars, shuffling the string as we go. */
544 while (extra != 0)
545 {
546 while ((q[extra] = *q) != '%')
547 --q;
548 q[--extra] = '%';
549 --q;
550 }
551 }
552 }
553 p = p + 2;
554 }
555
556 vfprintf (stderr, new_fmt, ap);
557 va_end (ap);
558
559 putc ('\n', stderr);
560 }
561
562 /* This is a function pointer to the routine which should handle BFD
563 error messages. It is called when a BFD routine encounters an
564 error for which it wants to print a message. Going through a
565 function pointer permits a program linked against BFD to intercept
566 the messages and deal with them itself. */
567
568 bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
569
570 /*
571 FUNCTION
572 bfd_set_error_handler
573
574 SYNOPSIS
575 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
576
577 DESCRIPTION
578 Set the BFD error handler function. Returns the previous
579 function.
580 */
581
582 bfd_error_handler_type
bfd_set_error_handler(bfd_error_handler_type pnew)583 bfd_set_error_handler (bfd_error_handler_type pnew)
584 {
585 bfd_error_handler_type pold;
586
587 pold = _bfd_error_handler;
588 _bfd_error_handler = pnew;
589 return pold;
590 }
591
592 /*
593 FUNCTION
594 bfd_set_error_program_name
595
596 SYNOPSIS
597 void bfd_set_error_program_name (const char *);
598
599 DESCRIPTION
600 Set the program name to use when printing a BFD error. This
601 is printed before the error message followed by a colon and
602 space. The string must not be changed after it is passed to
603 this function.
604 */
605
606 void
bfd_set_error_program_name(const char * name)607 bfd_set_error_program_name (const char *name)
608 {
609 _bfd_error_program_name = name;
610 }
611
612 /*
613 FUNCTION
614 bfd_get_error_handler
615
616 SYNOPSIS
617 bfd_error_handler_type bfd_get_error_handler (void);
618
619 DESCRIPTION
620 Return the BFD error handler function.
621 */
622
623 bfd_error_handler_type
bfd_get_error_handler(void)624 bfd_get_error_handler (void)
625 {
626 return _bfd_error_handler;
627 }
628
629 /*
630 SECTION
631 Symbols
632 */
633
634 /*
635 FUNCTION
636 bfd_get_reloc_upper_bound
637
638 SYNOPSIS
639 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
640
641 DESCRIPTION
642 Return the number of bytes required to store the
643 relocation information associated with section @var{sect}
644 attached to bfd @var{abfd}. If an error occurs, return -1.
645
646 */
647
648 long
bfd_get_reloc_upper_bound(bfd * abfd,sec_ptr asect)649 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
650 {
651 if (abfd->format != bfd_object)
652 {
653 bfd_set_error (bfd_error_invalid_operation);
654 return -1;
655 }
656
657 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
658 }
659
660 /*
661 FUNCTION
662 bfd_canonicalize_reloc
663
664 SYNOPSIS
665 long bfd_canonicalize_reloc
666 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
667
668 DESCRIPTION
669 Call the back end associated with the open BFD
670 @var{abfd} and translate the external form of the relocation
671 information attached to @var{sec} into the internal canonical
672 form. Place the table into memory at @var{loc}, which has
673 been preallocated, usually by a call to
674 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
675 -1 on error.
676
677 The @var{syms} table is also needed for horrible internal magic
678 reasons.
679
680 */
681 long
bfd_canonicalize_reloc(bfd * abfd,sec_ptr asect,arelent ** location,asymbol ** symbols)682 bfd_canonicalize_reloc (bfd *abfd,
683 sec_ptr asect,
684 arelent **location,
685 asymbol **symbols)
686 {
687 if (abfd->format != bfd_object)
688 {
689 bfd_set_error (bfd_error_invalid_operation);
690 return -1;
691 }
692
693 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
694 (abfd, asect, location, symbols));
695 }
696
697 /*
698 FUNCTION
699 bfd_set_reloc
700
701 SYNOPSIS
702 void bfd_set_reloc
703 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
704
705 DESCRIPTION
706 Set the relocation pointer and count within
707 section @var{sec} to the values @var{rel} and @var{count}.
708 The argument @var{abfd} is ignored.
709
710 */
711
712 void
bfd_set_reloc(bfd * ignore_abfd ATTRIBUTE_UNUSED,sec_ptr asect,arelent ** location,unsigned int count)713 bfd_set_reloc (bfd *ignore_abfd ATTRIBUTE_UNUSED,
714 sec_ptr asect,
715 arelent **location,
716 unsigned int count)
717 {
718 asect->orelocation = location;
719 asect->reloc_count = count;
720 }
721
722 /*
723 FUNCTION
724 bfd_set_file_flags
725
726 SYNOPSIS
727 bfd_boolean bfd_set_file_flags (bfd *abfd, flagword flags);
728
729 DESCRIPTION
730 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
731
732 Possible errors are:
733 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
734 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
735 o <<bfd_error_invalid_operation>> -
736 The flag word contained a bit which was not applicable to the
737 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
738 on a BFD format which does not support demand paging.
739
740 */
741
742 bfd_boolean
bfd_set_file_flags(bfd * abfd,flagword flags)743 bfd_set_file_flags (bfd *abfd, flagword flags)
744 {
745 if (abfd->format != bfd_object)
746 {
747 bfd_set_error (bfd_error_wrong_format);
748 return FALSE;
749 }
750
751 if (bfd_read_p (abfd))
752 {
753 bfd_set_error (bfd_error_invalid_operation);
754 return FALSE;
755 }
756
757 bfd_get_file_flags (abfd) = flags;
758 if ((flags & bfd_applicable_file_flags (abfd)) != flags)
759 {
760 bfd_set_error (bfd_error_invalid_operation);
761 return FALSE;
762 }
763
764 return TRUE;
765 }
766
767 void
bfd_assert(const char * file,int line)768 bfd_assert (const char *file, int line)
769 {
770 (*_bfd_error_handler) (_("BFD %s assertion fail %s:%d"),
771 BFD_VERSION_STRING, file, line);
772 }
773
774 /* A more or less friendly abort message. In libbfd.h abort is
775 defined to call this function. */
776
777 #ifndef EXIT_FAILURE
778 #define EXIT_FAILURE 1
779 #endif
780
781 void
_bfd_abort(const char * file,int line,const char * fn)782 _bfd_abort (const char *file, int line, const char *fn)
783 {
784 if (fn != NULL)
785 (*_bfd_error_handler)
786 (_("BFD %s internal error, aborting at %s line %d in %s\n"),
787 BFD_VERSION_STRING, file, line, fn);
788 else
789 (*_bfd_error_handler)
790 (_("BFD %s internal error, aborting at %s line %d\n"),
791 BFD_VERSION_STRING, file, line);
792 (*_bfd_error_handler) (_("Please report this bug.\n"));
793 xexit (EXIT_FAILURE);
794 }
795
796 /*
797 FUNCTION
798 bfd_get_arch_size
799
800 SYNOPSIS
801 int bfd_get_arch_size (bfd *abfd);
802
803 DESCRIPTION
804 Returns the architecture address size, in bits, as determined
805 by the object file's format. For ELF, this information is
806 included in the header.
807
808 RETURNS
809 Returns the arch size in bits if known, <<-1>> otherwise.
810 */
811
812 int
bfd_get_arch_size(bfd * abfd)813 bfd_get_arch_size (bfd *abfd)
814 {
815 if (abfd->xvec->flavour == bfd_target_elf_flavour)
816 return get_elf_backend_data (abfd)->s->arch_size;
817
818 return -1;
819 }
820
821 /*
822 FUNCTION
823 bfd_get_sign_extend_vma
824
825 SYNOPSIS
826 int bfd_get_sign_extend_vma (bfd *abfd);
827
828 DESCRIPTION
829 Indicates if the target architecture "naturally" sign extends
830 an address. Some architectures implicitly sign extend address
831 values when they are converted to types larger than the size
832 of an address. For instance, bfd_get_start_address() will
833 return an address sign extended to fill a bfd_vma when this is
834 the case.
835
836 RETURNS
837 Returns <<1>> if the target architecture is known to sign
838 extend addresses, <<0>> if the target architecture is known to
839 not sign extend addresses, and <<-1>> otherwise.
840 */
841
842 int
bfd_get_sign_extend_vma(bfd * abfd)843 bfd_get_sign_extend_vma (bfd *abfd)
844 {
845 char *name;
846
847 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
848 return get_elf_backend_data (abfd)->sign_extend_vma;
849
850 name = bfd_get_target (abfd);
851
852 /* Return a proper value for DJGPP & PE COFF (x86 COFF variants).
853 This function is required for DWARF2 support, but there is
854 no place to store this information in the COFF back end.
855 Should enough other COFF targets add support for DWARF2,
856 a place will have to be found. Until then, this hack will do. */
857 if (strncmp (name, "coff-go32", sizeof ("coff-go32") - 1) == 0
858 || strcmp (name, "pe-i386") == 0
859 || strcmp (name, "pei-i386") == 0)
860 return 1;
861
862 bfd_set_error (bfd_error_wrong_format);
863 return -1;
864 }
865
866 /*
867 FUNCTION
868 bfd_set_start_address
869
870 SYNOPSIS
871 bfd_boolean bfd_set_start_address (bfd *abfd, bfd_vma vma);
872
873 DESCRIPTION
874 Make @var{vma} the entry point of output BFD @var{abfd}.
875
876 RETURNS
877 Returns <<TRUE>> on success, <<FALSE>> otherwise.
878 */
879
880 bfd_boolean
bfd_set_start_address(bfd * abfd,bfd_vma vma)881 bfd_set_start_address (bfd *abfd, bfd_vma vma)
882 {
883 abfd->start_address = vma;
884 return TRUE;
885 }
886
887 /*
888 FUNCTION
889 bfd_get_gp_size
890
891 SYNOPSIS
892 unsigned int bfd_get_gp_size (bfd *abfd);
893
894 DESCRIPTION
895 Return the maximum size of objects to be optimized using the GP
896 register under MIPS ECOFF. This is typically set by the <<-G>>
897 argument to the compiler, assembler or linker.
898 */
899
900 unsigned int
bfd_get_gp_size(bfd * abfd)901 bfd_get_gp_size (bfd *abfd)
902 {
903 if (abfd->format == bfd_object)
904 {
905 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
906 return ecoff_data (abfd)->gp_size;
907 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
908 return elf_gp_size (abfd);
909 }
910 return 0;
911 }
912
913 /*
914 FUNCTION
915 bfd_set_gp_size
916
917 SYNOPSIS
918 void bfd_set_gp_size (bfd *abfd, unsigned int i);
919
920 DESCRIPTION
921 Set the maximum size of objects to be optimized using the GP
922 register under ECOFF or MIPS ELF. This is typically set by
923 the <<-G>> argument to the compiler, assembler or linker.
924 */
925
926 void
bfd_set_gp_size(bfd * abfd,unsigned int i)927 bfd_set_gp_size (bfd *abfd, unsigned int i)
928 {
929 /* Don't try to set GP size on an archive or core file! */
930 if (abfd->format != bfd_object)
931 return;
932
933 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
934 ecoff_data (abfd)->gp_size = i;
935 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
936 elf_gp_size (abfd) = i;
937 }
938
939 /* Get the GP value. This is an internal function used by some of the
940 relocation special_function routines on targets which support a GP
941 register. */
942
943 bfd_vma
_bfd_get_gp_value(bfd * abfd)944 _bfd_get_gp_value (bfd *abfd)
945 {
946 if (! abfd)
947 return 0;
948 if (abfd->format != bfd_object)
949 return 0;
950
951 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
952 return ecoff_data (abfd)->gp;
953 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
954 return elf_gp (abfd);
955
956 return 0;
957 }
958
959 /* Set the GP value. */
960
961 void
_bfd_set_gp_value(bfd * abfd,bfd_vma v)962 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
963 {
964 if (! abfd)
965 BFD_FAIL ();
966 if (abfd->format != bfd_object)
967 return;
968
969 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
970 ecoff_data (abfd)->gp = v;
971 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
972 elf_gp (abfd) = v;
973 }
974
975 /*
976 FUNCTION
977 bfd_scan_vma
978
979 SYNOPSIS
980 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
981
982 DESCRIPTION
983 Convert, like <<strtoul>>, a numerical expression
984 @var{string} into a <<bfd_vma>> integer, and return that integer.
985 (Though without as many bells and whistles as <<strtoul>>.)
986 The expression is assumed to be unsigned (i.e., positive).
987 If given a @var{base}, it is used as the base for conversion.
988 A base of 0 causes the function to interpret the string
989 in hex if a leading "0x" or "0X" is found, otherwise
990 in octal if a leading zero is found, otherwise in decimal.
991
992 If the value would overflow, the maximum <<bfd_vma>> value is
993 returned.
994 */
995
996 bfd_vma
bfd_scan_vma(const char * string,const char ** end,int base)997 bfd_scan_vma (const char *string, const char **end, int base)
998 {
999 bfd_vma value;
1000 bfd_vma cutoff;
1001 unsigned int cutlim;
1002 int overflow;
1003
1004 /* Let the host do it if possible. */
1005 if (sizeof (bfd_vma) <= sizeof (unsigned long))
1006 return strtoul (string, (char **) end, base);
1007
1008 #ifdef HAVE_STRTOULL
1009 if (sizeof (bfd_vma) <= sizeof (unsigned long long))
1010 return strtoull (string, (char **) end, base);
1011 #endif
1012
1013 if (base == 0)
1014 {
1015 if (string[0] == '0')
1016 {
1017 if ((string[1] == 'x') || (string[1] == 'X'))
1018 base = 16;
1019 else
1020 base = 8;
1021 }
1022 }
1023
1024 if ((base < 2) || (base > 36))
1025 base = 10;
1026
1027 if (base == 16
1028 && string[0] == '0'
1029 && (string[1] == 'x' || string[1] == 'X')
1030 && ISXDIGIT (string[2]))
1031 {
1032 string += 2;
1033 }
1034
1035 cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
1036 cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
1037 value = 0;
1038 overflow = 0;
1039 while (1)
1040 {
1041 unsigned int digit;
1042
1043 digit = *string;
1044 if (ISDIGIT (digit))
1045 digit = digit - '0';
1046 else if (ISALPHA (digit))
1047 digit = TOUPPER (digit) - 'A' + 10;
1048 else
1049 break;
1050 if (digit >= (unsigned int) base)
1051 break;
1052 if (value > cutoff || (value == cutoff && digit > cutlim))
1053 overflow = 1;
1054 value = value * base + digit;
1055 ++string;
1056 }
1057
1058 if (overflow)
1059 value = ~ (bfd_vma) 0;
1060
1061 if (end != NULL)
1062 *end = string;
1063
1064 return value;
1065 }
1066
1067 /*
1068 FUNCTION
1069 bfd_copy_private_header_data
1070
1071 SYNOPSIS
1072 bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
1073
1074 DESCRIPTION
1075 Copy private BFD header information from the BFD @var{ibfd} to the
1076 the BFD @var{obfd}. This copies information that may require
1077 sections to exist, but does not require symbol tables. Return
1078 <<true>> on success, <<false>> on error.
1079 Possible error returns are:
1080
1081 o <<bfd_error_no_memory>> -
1082 Not enough memory exists to create private data for @var{obfd}.
1083
1084 .#define bfd_copy_private_header_data(ibfd, obfd) \
1085 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
1086 . (ibfd, obfd))
1087
1088 */
1089
1090 /*
1091 FUNCTION
1092 bfd_copy_private_bfd_data
1093
1094 SYNOPSIS
1095 bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
1096
1097 DESCRIPTION
1098 Copy private BFD information from the BFD @var{ibfd} to the
1099 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
1100 Possible error returns are:
1101
1102 o <<bfd_error_no_memory>> -
1103 Not enough memory exists to create private data for @var{obfd}.
1104
1105 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
1106 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
1107 . (ibfd, obfd))
1108
1109 */
1110
1111 /*
1112 FUNCTION
1113 bfd_merge_private_bfd_data
1114
1115 SYNOPSIS
1116 bfd_boolean bfd_merge_private_bfd_data (bfd *ibfd, bfd *obfd);
1117
1118 DESCRIPTION
1119 Merge private BFD information from the BFD @var{ibfd} to the
1120 the output file BFD @var{obfd} when linking. Return <<TRUE>>
1121 on success, <<FALSE>> on error. Possible error returns are:
1122
1123 o <<bfd_error_no_memory>> -
1124 Not enough memory exists to create private data for @var{obfd}.
1125
1126 .#define bfd_merge_private_bfd_data(ibfd, obfd) \
1127 . BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
1128 . (ibfd, obfd))
1129
1130 */
1131
1132 /*
1133 FUNCTION
1134 bfd_set_private_flags
1135
1136 SYNOPSIS
1137 bfd_boolean bfd_set_private_flags (bfd *abfd, flagword flags);
1138
1139 DESCRIPTION
1140 Set private BFD flag information in the BFD @var{abfd}.
1141 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
1142 returns are:
1143
1144 o <<bfd_error_no_memory>> -
1145 Not enough memory exists to create private data for @var{obfd}.
1146
1147 .#define bfd_set_private_flags(abfd, flags) \
1148 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
1149
1150 */
1151
1152 /*
1153 FUNCTION
1154 Other functions
1155
1156 DESCRIPTION
1157 The following functions exist but have not yet been documented.
1158
1159 .#define bfd_sizeof_headers(abfd, reloc) \
1160 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
1161 .
1162 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
1163 . BFD_SEND (abfd, _bfd_find_nearest_line, \
1164 . (abfd, sec, syms, off, file, func, line))
1165 .
1166 .#define bfd_find_line(abfd, syms, sym, file, line) \
1167 . BFD_SEND (abfd, _bfd_find_line, \
1168 . (abfd, syms, sym, file, line))
1169 .
1170 .#define bfd_find_inliner_info(abfd, file, func, line) \
1171 . BFD_SEND (abfd, _bfd_find_inliner_info, \
1172 . (abfd, file, func, line))
1173 .
1174 .#define bfd_debug_info_start(abfd) \
1175 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
1176 .
1177 .#define bfd_debug_info_end(abfd) \
1178 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
1179 .
1180 .#define bfd_debug_info_accumulate(abfd, section) \
1181 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
1182 .
1183 .#define bfd_stat_arch_elt(abfd, stat) \
1184 . BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
1185 .
1186 .#define bfd_update_armap_timestamp(abfd) \
1187 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
1188 .
1189 .#define bfd_set_arch_mach(abfd, arch, mach)\
1190 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
1191 .
1192 .#define bfd_relax_section(abfd, section, link_info, again) \
1193 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
1194 .
1195 .#define bfd_gc_sections(abfd, link_info) \
1196 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
1197 .
1198 .#define bfd_merge_sections(abfd, link_info) \
1199 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
1200 .
1201 .#define bfd_is_group_section(abfd, sec) \
1202 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
1203 .
1204 .#define bfd_discard_group(abfd, sec) \
1205 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
1206 .
1207 .#define bfd_link_hash_table_create(abfd) \
1208 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
1209 .
1210 .#define bfd_link_hash_table_free(abfd, hash) \
1211 . BFD_SEND (abfd, _bfd_link_hash_table_free, (hash))
1212 .
1213 .#define bfd_link_add_symbols(abfd, info) \
1214 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
1215 .
1216 .#define bfd_link_just_syms(abfd, sec, info) \
1217 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
1218 .
1219 .#define bfd_final_link(abfd, info) \
1220 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
1221 .
1222 .#define bfd_free_cached_info(abfd) \
1223 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
1224 .
1225 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
1226 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
1227 .
1228 .#define bfd_print_private_bfd_data(abfd, file)\
1229 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
1230 .
1231 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
1232 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
1233 .
1234 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
1235 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
1236 . dyncount, dynsyms, ret))
1237 .
1238 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
1239 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
1240 .
1241 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
1242 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
1243 .
1244 .extern bfd_byte *bfd_get_relocated_section_contents
1245 . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
1246 . bfd_boolean, asymbol **);
1247 .
1248
1249 */
1250
1251 bfd_byte *
bfd_get_relocated_section_contents(bfd * abfd,struct bfd_link_info * link_info,struct bfd_link_order * link_order,bfd_byte * data,bfd_boolean relocatable,asymbol ** symbols)1252 bfd_get_relocated_section_contents (bfd *abfd,
1253 struct bfd_link_info *link_info,
1254 struct bfd_link_order *link_order,
1255 bfd_byte *data,
1256 bfd_boolean relocatable,
1257 asymbol **symbols)
1258 {
1259 bfd *abfd2;
1260 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
1261 bfd_byte *, bfd_boolean, asymbol **);
1262
1263 if (link_order->type == bfd_indirect_link_order)
1264 {
1265 abfd2 = link_order->u.indirect.section->owner;
1266 if (abfd2 == NULL)
1267 abfd2 = abfd;
1268 }
1269 else
1270 abfd2 = abfd;
1271
1272 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
1273
1274 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
1275 }
1276
1277 /* Record information about an ELF program header. */
1278
1279 bfd_boolean
bfd_record_phdr(bfd * abfd,unsigned long type,bfd_boolean flags_valid,flagword flags,bfd_boolean at_valid,bfd_vma at,bfd_boolean includes_filehdr,bfd_boolean includes_phdrs,unsigned int count,asection ** secs)1280 bfd_record_phdr (bfd *abfd,
1281 unsigned long type,
1282 bfd_boolean flags_valid,
1283 flagword flags,
1284 bfd_boolean at_valid,
1285 bfd_vma at,
1286 bfd_boolean includes_filehdr,
1287 bfd_boolean includes_phdrs,
1288 unsigned int count,
1289 asection **secs)
1290 {
1291 struct elf_segment_map *m, **pm;
1292 bfd_size_type amt;
1293
1294 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1295 return TRUE;
1296
1297 amt = sizeof (struct elf_segment_map);
1298 amt += ((bfd_size_type) count - 1) * sizeof (asection *);
1299 m = bfd_alloc (abfd, amt);
1300 if (m == NULL)
1301 return FALSE;
1302
1303 m->next = NULL;
1304 m->p_type = type;
1305 m->p_flags = flags;
1306 m->p_paddr = at;
1307 m->p_flags_valid = flags_valid;
1308 m->p_paddr_valid = at_valid;
1309 m->includes_filehdr = includes_filehdr;
1310 m->includes_phdrs = includes_phdrs;
1311 m->count = count;
1312 if (count > 0)
1313 memcpy (m->sections, secs, count * sizeof (asection *));
1314
1315 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
1316 ;
1317 *pm = m;
1318
1319 return TRUE;
1320 }
1321
1322 void
bfd_sprintf_vma(bfd * abfd,char * buf,bfd_vma value)1323 bfd_sprintf_vma (bfd *abfd, char *buf, bfd_vma value)
1324 {
1325 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1326 get_elf_backend_data (abfd)->elf_backend_sprintf_vma (abfd, buf, value);
1327 else
1328 sprintf_vma (buf, value);
1329 }
1330
1331 void
bfd_fprintf_vma(bfd * abfd,void * stream,bfd_vma value)1332 bfd_fprintf_vma (bfd *abfd, void *stream, bfd_vma value)
1333 {
1334 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1335 get_elf_backend_data (abfd)->elf_backend_fprintf_vma (abfd, stream, value);
1336 else
1337 fprintf_vma ((FILE *) stream, value);
1338 }
1339
1340 /*
1341 FUNCTION
1342 bfd_alt_mach_code
1343
1344 SYNOPSIS
1345 bfd_boolean bfd_alt_mach_code (bfd *abfd, int alternative);
1346
1347 DESCRIPTION
1348
1349 When more than one machine code number is available for the
1350 same machine type, this function can be used to switch between
1351 the preferred one (alternative == 0) and any others. Currently,
1352 only ELF supports this feature, with up to two alternate
1353 machine codes.
1354 */
1355
1356 bfd_boolean
bfd_alt_mach_code(bfd * abfd,int alternative)1357 bfd_alt_mach_code (bfd *abfd, int alternative)
1358 {
1359 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1360 {
1361 int code;
1362
1363 switch (alternative)
1364 {
1365 case 0:
1366 code = get_elf_backend_data (abfd)->elf_machine_code;
1367 break;
1368
1369 case 1:
1370 code = get_elf_backend_data (abfd)->elf_machine_alt1;
1371 if (code == 0)
1372 return FALSE;
1373 break;
1374
1375 case 2:
1376 code = get_elf_backend_data (abfd)->elf_machine_alt2;
1377 if (code == 0)
1378 return FALSE;
1379 break;
1380
1381 default:
1382 return FALSE;
1383 }
1384
1385 elf_elfheader (abfd)->e_machine = code;
1386
1387 return TRUE;
1388 }
1389
1390 return FALSE;
1391 }
1392
1393 /*
1394 CODE_FRAGMENT
1395
1396 .struct bfd_preserve
1397 .{
1398 . void *marker;
1399 . void *tdata;
1400 . flagword flags;
1401 . const struct bfd_arch_info *arch_info;
1402 . struct bfd_section *sections;
1403 . struct bfd_section *section_last;
1404 . unsigned int section_count;
1405 . struct bfd_hash_table section_htab;
1406 .};
1407 .
1408 */
1409
1410 /*
1411 FUNCTION
1412 bfd_preserve_save
1413
1414 SYNOPSIS
1415 bfd_boolean bfd_preserve_save (bfd *, struct bfd_preserve *);
1416
1417 DESCRIPTION
1418 When testing an object for compatibility with a particular
1419 target back-end, the back-end object_p function needs to set
1420 up certain fields in the bfd on successfully recognizing the
1421 object. This typically happens in a piecemeal fashion, with
1422 failures possible at many points. On failure, the bfd is
1423 supposed to be restored to its initial state, which is
1424 virtually impossible. However, restoring a subset of the bfd
1425 state works in practice. This function stores the subset and
1426 reinitializes the bfd.
1427
1428 */
1429
1430 bfd_boolean
bfd_preserve_save(bfd * abfd,struct bfd_preserve * preserve)1431 bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
1432 {
1433 preserve->tdata = abfd->tdata.any;
1434 preserve->arch_info = abfd->arch_info;
1435 preserve->flags = abfd->flags;
1436 preserve->sections = abfd->sections;
1437 preserve->section_last = abfd->section_last;
1438 preserve->section_count = abfd->section_count;
1439 preserve->section_htab = abfd->section_htab;
1440
1441 if (! bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc))
1442 return FALSE;
1443
1444 abfd->tdata.any = NULL;
1445 abfd->arch_info = &bfd_default_arch_struct;
1446 abfd->flags &= BFD_IN_MEMORY;
1447 abfd->sections = NULL;
1448 abfd->section_last = NULL;
1449 abfd->section_count = 0;
1450
1451 return TRUE;
1452 }
1453
1454 /*
1455 FUNCTION
1456 bfd_preserve_restore
1457
1458 SYNOPSIS
1459 void bfd_preserve_restore (bfd *, struct bfd_preserve *);
1460
1461 DESCRIPTION
1462 This function restores bfd state saved by bfd_preserve_save.
1463 If MARKER is non-NULL in struct bfd_preserve then that block
1464 and all subsequently bfd_alloc'd memory is freed.
1465
1466 */
1467
1468 void
bfd_preserve_restore(bfd * abfd,struct bfd_preserve * preserve)1469 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
1470 {
1471 bfd_hash_table_free (&abfd->section_htab);
1472
1473 abfd->tdata.any = preserve->tdata;
1474 abfd->arch_info = preserve->arch_info;
1475 abfd->flags = preserve->flags;
1476 abfd->section_htab = preserve->section_htab;
1477 abfd->sections = preserve->sections;
1478 abfd->section_last = preserve->section_last;
1479 abfd->section_count = preserve->section_count;
1480
1481 /* bfd_release frees all memory more recently bfd_alloc'd than
1482 its arg, as well as its arg. */
1483 if (preserve->marker != NULL)
1484 {
1485 bfd_release (abfd, preserve->marker);
1486 preserve->marker = NULL;
1487 }
1488 }
1489
1490 /*
1491 FUNCTION
1492 bfd_preserve_finish
1493
1494 SYNOPSIS
1495 void bfd_preserve_finish (bfd *, struct bfd_preserve *);
1496
1497 DESCRIPTION
1498 This function should be called when the bfd state saved by
1499 bfd_preserve_save is no longer needed. ie. when the back-end
1500 object_p function returns with success.
1501
1502 */
1503
1504 void
bfd_preserve_finish(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_preserve * preserve)1505 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
1506 {
1507 /* It would be nice to be able to free more memory here, eg. old
1508 tdata, but that's not possible since these blocks are sitting
1509 inside bfd_alloc'd memory. The section hash is on a separate
1510 objalloc. */
1511 bfd_hash_table_free (&preserve->section_htab);
1512 }
1513