PR ld/11843
[binutils.git] / bfd / bfd.c
blob771989b75899383357744d03a6ba63dbf14c0d92
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, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
12 (at your option) any later version.
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.
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,
22 MA 02110-1301, USA. */
25 SECTION
26 <<typedef bfd>>
28 A BFD has type <<bfd>>; objects of this type are the
29 cornerstone of any application using BFD. Using BFD
30 consists of making references though the BFD and to data in the BFD.
32 Here is the structure that defines the type <<bfd>>. It
33 contains the major data about the file and pointers
34 to the rest of the data.
36 CODE_FRAGMENT
38 .enum bfd_direction
39 . {
40 . no_direction = 0,
41 . read_direction = 1,
42 . write_direction = 2,
43 . both_direction = 3
44 . };
46 .struct bfd
48 . {* A unique identifier of the BFD *}
49 . unsigned int id;
51 . {* The filename the application opened the BFD with. *}
52 . const char *filename;
54 . {* A pointer to the target jump table. *}
55 . const struct bfd_target *xvec;
57 . {* The IOSTREAM, and corresponding IO vector that provide access
58 . to the file backing the BFD. *}
59 . void *iostream;
60 . const struct bfd_iovec *iovec;
62 . {* The caching routines use these to maintain a
63 . least-recently-used list of BFDs. *}
64 . struct bfd *lru_prev, *lru_next;
66 . {* When a file is closed by the caching routines, BFD retains
67 . state information on the file here... *}
68 . ufile_ptr where;
70 . {* File modified time, if mtime_set is TRUE. *}
71 . long mtime;
73 . {* Reserved for an unimplemented file locking extension. *}
74 . int ifd;
76 . {* The format which belongs to the BFD. (object, core, etc.) *}
77 . bfd_format format;
79 . {* The direction with which the BFD was opened. *}
80 . enum bfd_direction direction;
82 . {* Format_specific flags. *}
83 . flagword flags;
85 . {* Values that may appear in the flags field of a BFD. These also
86 . appear in the object_flags field of the bfd_target structure, where
87 . they indicate the set of flags used by that backend (not all flags
88 . are meaningful for all object file formats) (FIXME: at the moment,
89 . the object_flags values have mostly just been copied from backend
90 . to another, and are not necessarily correct). *}
92 .#define BFD_NO_FLAGS 0x00
94 . {* BFD contains relocation entries. *}
95 .#define HAS_RELOC 0x01
97 . {* BFD is directly executable. *}
98 .#define EXEC_P 0x02
100 . {* BFD has line number information (basically used for F_LNNO in a
101 . COFF header). *}
102 .#define HAS_LINENO 0x04
104 . {* BFD has debugging information. *}
105 .#define HAS_DEBUG 0x08
107 . {* BFD has symbols. *}
108 .#define HAS_SYMS 0x10
110 . {* BFD has local symbols (basically used for F_LSYMS in a COFF
111 . header). *}
112 .#define HAS_LOCALS 0x20
114 . {* BFD is a dynamic object. *}
115 .#define DYNAMIC 0x40
117 . {* Text section is write protected (if D_PAGED is not set, this is
118 . like an a.out NMAGIC file) (the linker sets this by default, but
119 . clears it for -r or -N). *}
120 .#define WP_TEXT 0x80
122 . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
123 . linker sets this by default, but clears it for -r or -n or -N). *}
124 .#define D_PAGED 0x100
126 . {* BFD is relaxable (this means that bfd_relax_section may be able to
127 . do something) (sometimes bfd_relax_section can do something even if
128 . this is not set). *}
129 .#define BFD_IS_RELAXABLE 0x200
131 . {* This may be set before writing out a BFD to request using a
132 . traditional format. For example, this is used to request that when
133 . writing out an a.out object the symbols not be hashed to eliminate
134 . duplicates. *}
135 .#define BFD_TRADITIONAL_FORMAT 0x400
137 . {* This flag indicates that the BFD contents are actually cached
138 . in memory. If this is set, iostream points to a bfd_in_memory
139 . struct. *}
140 .#define BFD_IN_MEMORY 0x800
142 . {* The sections in this BFD specify a memory page. *}
143 .#define HAS_LOAD_PAGE 0x1000
145 . {* This BFD has been created by the linker and doesn't correspond
146 . to any input file. *}
147 .#define BFD_LINKER_CREATED 0x2000
149 . {* This may be set before writing out a BFD to request that it
150 . be written using values for UIDs, GIDs, timestamps, etc. that
151 . will be consistent from run to run. *}
152 .#define BFD_DETERMINISTIC_OUTPUT 0x4000
154 . {* Currently my_archive is tested before adding origin to
155 . anything. I believe that this can become always an add of
156 . origin, with origin set to 0 for non archive files. *}
157 . ufile_ptr origin;
159 . {* The origin in the archive of the proxy entry. This will
160 . normally be the same as origin, except for thin archives,
161 . when it will contain the current offset of the proxy in the
162 . thin archive rather than the offset of the bfd in its actual
163 . container. *}
164 . ufile_ptr proxy_origin;
166 . {* A hash table for section names. *}
167 . struct bfd_hash_table section_htab;
169 . {* Pointer to linked list of sections. *}
170 . struct bfd_section *sections;
172 . {* The last section on the section list. *}
173 . struct bfd_section *section_last;
175 . {* The number of sections. *}
176 . unsigned int section_count;
178 . {* Stuff only useful for object files:
179 . The start address. *}
180 . bfd_vma start_address;
182 . {* Used for input and output. *}
183 . unsigned int symcount;
185 . {* Symbol table for output BFD (with symcount entries).
186 . Also used by the linker to cache input BFD symbols. *}
187 . struct bfd_symbol **outsymbols;
189 . {* Used for slurped dynamic symbol tables. *}
190 . unsigned int dynsymcount;
192 . {* Pointer to structure which contains architecture information. *}
193 . const struct bfd_arch_info *arch_info;
195 . {* Stuff only useful for archives. *}
196 . void *arelt_data;
197 . struct bfd *my_archive; {* The containing archive BFD. *}
198 . struct bfd *archive_next; {* The next BFD in the archive. *}
199 . struct bfd *archive_head; {* The first BFD in the archive. *}
200 . struct bfd *nested_archives; {* List of nested archive in a flattened
201 . thin archive. *}
203 . {* A chain of BFD structures involved in a link. *}
204 . struct bfd *link_next;
206 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
207 . be used only for archive elements. *}
208 . int archive_pass;
210 . {* Used by the back end to hold private data. *}
211 . union
213 . struct aout_data_struct *aout_data;
214 . struct artdata *aout_ar_data;
215 . struct _oasys_data *oasys_obj_data;
216 . struct _oasys_ar_data *oasys_ar_data;
217 . struct coff_tdata *coff_obj_data;
218 . struct pe_tdata *pe_obj_data;
219 . struct xcoff_tdata *xcoff_obj_data;
220 . struct ecoff_tdata *ecoff_obj_data;
221 . struct ieee_data_struct *ieee_data;
222 . struct ieee_ar_data_struct *ieee_ar_data;
223 . struct srec_data_struct *srec_data;
224 . struct verilog_data_struct *verilog_data;
225 . struct ihex_data_struct *ihex_data;
226 . struct tekhex_data_struct *tekhex_data;
227 . struct elf_obj_tdata *elf_obj_data;
228 . struct nlm_obj_tdata *nlm_obj_data;
229 . struct bout_data_struct *bout_data;
230 . struct mmo_data_struct *mmo_data;
231 . struct sun_core_struct *sun_core_data;
232 . struct sco5_core_struct *sco5_core_data;
233 . struct trad_core_struct *trad_core_data;
234 . struct som_data_struct *som_data;
235 . struct hpux_core_struct *hpux_core_data;
236 . struct hppabsd_core_struct *hppabsd_core_data;
237 . struct sgi_core_struct *sgi_core_data;
238 . struct lynx_core_struct *lynx_core_data;
239 . struct osf_core_struct *osf_core_data;
240 . struct cisco_core_struct *cisco_core_data;
241 . struct versados_data_struct *versados_data;
242 . struct netbsd_core_struct *netbsd_core_data;
243 . struct mach_o_data_struct *mach_o_data;
244 . struct mach_o_fat_data_struct *mach_o_fat_data;
245 . struct plugin_data_struct *plugin_data;
246 . struct bfd_pef_data_struct *pef_data;
247 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
248 . struct bfd_sym_data_struct *sym_data;
249 . void *any;
251 . tdata;
253 . {* Used by the application to hold private data. *}
254 . void *usrdata;
256 . {* Where all the allocated stuff under this BFD goes. This is a
257 . struct objalloc *, but we use void * to avoid requiring the inclusion
258 . of objalloc.h. *}
259 . void *memory;
261 . {* Is the file descriptor being cached? That is, can it be closed as
262 . needed, and re-opened when accessed later? *}
263 . unsigned int cacheable : 1;
265 . {* Marks whether there was a default target specified when the
266 . BFD was opened. This is used to select which matching algorithm
267 . to use to choose the back end. *}
268 . unsigned int target_defaulted : 1;
270 . {* ... and here: (``once'' means at least once). *}
271 . unsigned int opened_once : 1;
273 . {* Set if we have a locally maintained mtime value, rather than
274 . getting it from the file each time. *}
275 . unsigned int mtime_set : 1;
277 . {* Flag set if symbols from this BFD should not be exported. *}
278 . unsigned int no_export : 1;
280 . {* Remember when output has begun, to stop strange things
281 . from happening. *}
282 . unsigned int output_has_begun : 1;
284 . {* Have archive map. *}
285 . unsigned int has_armap : 1;
287 . {* Set if this is a thin archive. *}
288 . unsigned int is_thin_archive : 1;
290 . {* Set if only required symbols should be added in the link hash table for
291 . this object. Used by VMS linkers. *}
292 . unsigned int selective_search : 1;
297 #include "sysdep.h"
298 #include <stdarg.h>
299 #include "bfd.h"
300 #include "bfdver.h"
301 #include "libiberty.h"
302 #include "demangle.h"
303 #include "safe-ctype.h"
304 #include "bfdlink.h"
305 #include "libbfd.h"
306 #include "coff/internal.h"
307 #include "coff/sym.h"
308 #include "libcoff.h"
309 #include "libecoff.h"
310 #undef obj_symbols
311 #include "elf-bfd.h"
313 #ifndef EXIT_FAILURE
314 #define EXIT_FAILURE 1
315 #endif
318 /* provide storage for subsystem, stack and heap data which may have been
319 passed in on the command line. Ld puts this data into a bfd_link_info
320 struct which ultimately gets passed in to the bfd. When it arrives, copy
321 it to the following struct so that the data will be available in coffcode.h
322 where it is needed. The typedef's used are defined in bfd.h */
325 SECTION
326 Error reporting
328 Most BFD functions return nonzero on success (check their
329 individual documentation for precise semantics). On an error,
330 they call <<bfd_set_error>> to set an error condition that callers
331 can check by calling <<bfd_get_error>>.
332 If that returns <<bfd_error_system_call>>, then check
333 <<errno>>.
335 The easiest way to report a BFD error to the user is to
336 use <<bfd_perror>>.
338 SUBSECTION
339 Type <<bfd_error_type>>
341 The values returned by <<bfd_get_error>> are defined by the
342 enumerated type <<bfd_error_type>>.
344 CODE_FRAGMENT
346 .typedef enum bfd_error
348 . bfd_error_no_error = 0,
349 . bfd_error_system_call,
350 . bfd_error_invalid_target,
351 . bfd_error_wrong_format,
352 . bfd_error_wrong_object_format,
353 . bfd_error_invalid_operation,
354 . bfd_error_no_memory,
355 . bfd_error_no_symbols,
356 . bfd_error_no_armap,
357 . bfd_error_no_more_archived_files,
358 . bfd_error_malformed_archive,
359 . bfd_error_file_not_recognized,
360 . bfd_error_file_ambiguously_recognized,
361 . bfd_error_no_contents,
362 . bfd_error_nonrepresentable_section,
363 . bfd_error_no_debug_section,
364 . bfd_error_bad_value,
365 . bfd_error_file_truncated,
366 . bfd_error_file_too_big,
367 . bfd_error_on_input,
368 . bfd_error_invalid_error_code
370 .bfd_error_type;
374 static bfd_error_type bfd_error = bfd_error_no_error;
375 static bfd *input_bfd = NULL;
376 static bfd_error_type input_error = bfd_error_no_error;
378 const char *const bfd_errmsgs[] =
380 N_("No error"),
381 N_("System call error"),
382 N_("Invalid bfd target"),
383 N_("File in wrong format"),
384 N_("Archive object file in wrong format"),
385 N_("Invalid operation"),
386 N_("Memory exhausted"),
387 N_("No symbols"),
388 N_("Archive has no index; run ranlib to add one"),
389 N_("No more archived files"),
390 N_("Malformed archive"),
391 N_("File format not recognized"),
392 N_("File format is ambiguous"),
393 N_("Section has no contents"),
394 N_("Nonrepresentable section on output"),
395 N_("Symbol needs debug section which does not exist"),
396 N_("Bad value"),
397 N_("File truncated"),
398 N_("File too big"),
399 N_("Error reading %s: %s"),
400 N_("#<Invalid error code>")
404 FUNCTION
405 bfd_get_error
407 SYNOPSIS
408 bfd_error_type bfd_get_error (void);
410 DESCRIPTION
411 Return the current BFD error condition.
414 bfd_error_type
415 bfd_get_error (void)
417 return bfd_error;
421 FUNCTION
422 bfd_set_error
424 SYNOPSIS
425 void bfd_set_error (bfd_error_type error_tag, ...);
427 DESCRIPTION
428 Set the BFD error condition to be @var{error_tag}.
429 If @var{error_tag} is bfd_error_on_input, then this function
430 takes two more parameters, the input bfd where the error
431 occurred, and the bfd_error_type error.
434 void
435 bfd_set_error (bfd_error_type error_tag, ...)
437 bfd_error = error_tag;
438 if (error_tag == bfd_error_on_input)
440 /* This is an error that occurred during bfd_close when
441 writing an archive, but on one of the input files. */
442 va_list ap;
444 va_start (ap, error_tag);
445 input_bfd = va_arg (ap, bfd *);
446 input_error = (bfd_error_type) va_arg (ap, int);
447 if (input_error >= bfd_error_on_input)
448 abort ();
449 va_end (ap);
454 FUNCTION
455 bfd_errmsg
457 SYNOPSIS
458 const char *bfd_errmsg (bfd_error_type error_tag);
460 DESCRIPTION
461 Return a string describing the error @var{error_tag}, or
462 the system error if @var{error_tag} is <<bfd_error_system_call>>.
465 const char *
466 bfd_errmsg (bfd_error_type error_tag)
468 #ifndef errno
469 extern int errno;
470 #endif
471 if (error_tag == bfd_error_on_input)
473 char *buf;
474 const char *msg = bfd_errmsg (input_error);
476 if (asprintf (&buf, _(bfd_errmsgs [error_tag]), input_bfd->filename, msg)
477 != -1)
478 return buf;
480 /* Ick, what to do on out of memory? */
481 return msg;
484 if (error_tag == bfd_error_system_call)
485 return xstrerror (errno);
487 if (error_tag > bfd_error_invalid_error_code)
488 error_tag = bfd_error_invalid_error_code; /* sanity check */
490 return _(bfd_errmsgs [error_tag]);
494 FUNCTION
495 bfd_perror
497 SYNOPSIS
498 void bfd_perror (const char *message);
500 DESCRIPTION
501 Print to the standard error stream a string describing the
502 last BFD error that occurred, or the last system error if
503 the last BFD error was a system call failure. If @var{message}
504 is non-NULL and non-empty, the error string printed is preceded
505 by @var{message}, a colon, and a space. It is followed by a newline.
508 void
509 bfd_perror (const char *message)
511 if (message == NULL || *message == '\0')
512 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
513 else
514 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
518 SUBSECTION
519 BFD error handler
521 Some BFD functions want to print messages describing the
522 problem. They call a BFD error handler function. This
523 function may be overridden by the program.
525 The BFD error handler acts like printf.
527 CODE_FRAGMENT
529 .typedef void (*bfd_error_handler_type) (const char *, ...);
533 /* The program name used when printing BFD error messages. */
535 static const char *_bfd_error_program_name;
537 /* This is the default routine to handle BFD error messages.
538 Like fprintf (stderr, ...), but also handles some extra format specifiers.
540 %A section name from section. For group components, print group name too.
541 %B file name from bfd. For archive components, prints archive too.
543 Note - because these two extra format specifiers require special handling
544 they are scanned for and processed in this function, before calling
545 vfprintf. This means that the *arguments* for these format specifiers
546 must be the first ones in the variable argument list, regardless of where
547 the specifiers appear in the format string. Thus for example calling
548 this function with a format string of:
550 "blah %s blah %A blah %d blah %B"
552 would involve passing the arguments as:
554 "blah %s blah %A blah %d blah %B",
555 asection_for_the_%A,
556 bfd_for_the_%B,
557 string_for_the_%s,
558 integer_for_the_%d);
561 void
562 _bfd_default_error_handler (const char *fmt, ...)
564 va_list ap;
565 char *bufp;
566 const char *new_fmt, *p;
567 size_t avail = 1000;
568 char buf[1000];
570 /* PR 4992: Don't interrupt output being sent to stdout. */
571 fflush (stdout);
573 if (_bfd_error_program_name != NULL)
574 fprintf (stderr, "%s: ", _bfd_error_program_name);
575 else
576 fprintf (stderr, "BFD: ");
578 va_start (ap, fmt);
579 new_fmt = fmt;
580 bufp = buf;
582 /* Reserve enough space for the existing format string. */
583 avail -= strlen (fmt) + 1;
584 if (avail > 1000)
585 _exit (EXIT_FAILURE);
587 p = fmt;
588 while (1)
590 char *q;
591 size_t len, extra, trim;
593 p = strchr (p, '%');
594 if (p == NULL || p[1] == '\0')
596 if (new_fmt == buf)
598 len = strlen (fmt);
599 memcpy (bufp, fmt, len + 1);
601 break;
604 if (p[1] == 'A' || p[1] == 'B')
606 len = p - fmt;
607 memcpy (bufp, fmt, len);
608 bufp += len;
609 fmt = p + 2;
610 new_fmt = buf;
612 /* If we run out of space, tough, you lose your ridiculously
613 long file or section name. It's not safe to try to alloc
614 memory here; We might be printing an out of memory message. */
615 if (avail == 0)
617 *bufp++ = '*';
618 *bufp++ = '*';
619 *bufp = '\0';
621 else
623 if (p[1] == 'B')
625 bfd *abfd = va_arg (ap, bfd *);
627 if (abfd == NULL)
628 /* Invoking %B with a null bfd pointer is an internal error. */
629 abort ();
630 else if (abfd->my_archive)
631 snprintf (bufp, avail, "%s(%s)",
632 abfd->my_archive->filename, abfd->filename);
633 else
634 snprintf (bufp, avail, "%s", abfd->filename);
636 else
638 asection *sec = va_arg (ap, asection *);
639 bfd *abfd;
640 const char *group = NULL;
641 struct coff_comdat_info *ci;
643 if (sec == NULL)
644 /* Invoking %A with a null section pointer is an internal error. */
645 abort ();
646 abfd = sec->owner;
647 if (abfd != NULL
648 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
649 && elf_next_in_group (sec) != NULL
650 && (sec->flags & SEC_GROUP) == 0)
651 group = elf_group_name (sec);
652 else if (abfd != NULL
653 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
654 && (ci = bfd_coff_get_comdat_section (sec->owner,
655 sec)) != NULL)
656 group = ci->name;
657 if (group != NULL)
658 snprintf (bufp, avail, "%s[%s]", sec->name, group);
659 else
660 snprintf (bufp, avail, "%s", sec->name);
662 len = strlen (bufp);
663 avail = avail - len + 2;
665 /* We need to replace any '%' we printed by "%%".
666 First count how many. */
667 q = bufp;
668 bufp += len;
669 extra = 0;
670 while ((q = strchr (q, '%')) != NULL)
672 ++q;
673 ++extra;
676 /* If there isn't room, trim off the end of the string. */
677 q = bufp;
678 bufp += extra;
679 if (extra > avail)
681 trim = extra - avail;
682 bufp -= trim;
685 if (*--q == '%')
686 --extra;
688 while (--trim != 0);
689 *q = '\0';
690 avail = extra;
692 avail -= extra;
694 /* Now double all '%' chars, shuffling the string as we go. */
695 while (extra != 0)
697 while ((q[extra] = *q) != '%')
698 --q;
699 q[--extra] = '%';
700 --q;
704 p = p + 2;
707 vfprintf (stderr, new_fmt, ap);
708 va_end (ap);
710 putc ('\n', stderr);
713 /* This is a function pointer to the routine which should handle BFD
714 error messages. It is called when a BFD routine encounters an
715 error for which it wants to print a message. Going through a
716 function pointer permits a program linked against BFD to intercept
717 the messages and deal with them itself. */
719 bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
722 FUNCTION
723 bfd_set_error_handler
725 SYNOPSIS
726 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
728 DESCRIPTION
729 Set the BFD error handler function. Returns the previous
730 function.
733 bfd_error_handler_type
734 bfd_set_error_handler (bfd_error_handler_type pnew)
736 bfd_error_handler_type pold;
738 pold = _bfd_error_handler;
739 _bfd_error_handler = pnew;
740 return pold;
744 FUNCTION
745 bfd_set_error_program_name
747 SYNOPSIS
748 void bfd_set_error_program_name (const char *);
750 DESCRIPTION
751 Set the program name to use when printing a BFD error. This
752 is printed before the error message followed by a colon and
753 space. The string must not be changed after it is passed to
754 this function.
757 void
758 bfd_set_error_program_name (const char *name)
760 _bfd_error_program_name = name;
764 FUNCTION
765 bfd_get_error_handler
767 SYNOPSIS
768 bfd_error_handler_type bfd_get_error_handler (void);
770 DESCRIPTION
771 Return the BFD error handler function.
774 bfd_error_handler_type
775 bfd_get_error_handler (void)
777 return _bfd_error_handler;
781 SECTION
782 Miscellaneous
784 SUBSECTION
785 Miscellaneous functions
789 FUNCTION
790 bfd_get_reloc_upper_bound
792 SYNOPSIS
793 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
795 DESCRIPTION
796 Return the number of bytes required to store the
797 relocation information associated with section @var{sect}
798 attached to bfd @var{abfd}. If an error occurs, return -1.
802 long
803 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
805 if (abfd->format != bfd_object)
807 bfd_set_error (bfd_error_invalid_operation);
808 return -1;
811 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
815 FUNCTION
816 bfd_canonicalize_reloc
818 SYNOPSIS
819 long bfd_canonicalize_reloc
820 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
822 DESCRIPTION
823 Call the back end associated with the open BFD
824 @var{abfd} and translate the external form of the relocation
825 information attached to @var{sec} into the internal canonical
826 form. Place the table into memory at @var{loc}, which has
827 been preallocated, usually by a call to
828 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
829 -1 on error.
831 The @var{syms} table is also needed for horrible internal magic
832 reasons.
835 long
836 bfd_canonicalize_reloc (bfd *abfd,
837 sec_ptr asect,
838 arelent **location,
839 asymbol **symbols)
841 if (abfd->format != bfd_object)
843 bfd_set_error (bfd_error_invalid_operation);
844 return -1;
847 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
848 (abfd, asect, location, symbols));
852 FUNCTION
853 bfd_set_reloc
855 SYNOPSIS
856 void bfd_set_reloc
857 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
859 DESCRIPTION
860 Set the relocation pointer and count within
861 section @var{sec} to the values @var{rel} and @var{count}.
862 The argument @var{abfd} is ignored.
866 void
867 bfd_set_reloc (bfd *ignore_abfd ATTRIBUTE_UNUSED,
868 sec_ptr asect,
869 arelent **location,
870 unsigned int count)
872 asect->orelocation = location;
873 asect->reloc_count = count;
877 FUNCTION
878 bfd_set_file_flags
880 SYNOPSIS
881 bfd_boolean bfd_set_file_flags (bfd *abfd, flagword flags);
883 DESCRIPTION
884 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
886 Possible errors are:
887 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
888 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
889 o <<bfd_error_invalid_operation>> -
890 The flag word contained a bit which was not applicable to the
891 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
892 on a BFD format which does not support demand paging.
896 bfd_boolean
897 bfd_set_file_flags (bfd *abfd, flagword flags)
899 if (abfd->format != bfd_object)
901 bfd_set_error (bfd_error_wrong_format);
902 return FALSE;
905 if (bfd_read_p (abfd))
907 bfd_set_error (bfd_error_invalid_operation);
908 return FALSE;
911 bfd_get_file_flags (abfd) = flags;
912 if ((flags & bfd_applicable_file_flags (abfd)) != flags)
914 bfd_set_error (bfd_error_invalid_operation);
915 return FALSE;
918 return TRUE;
921 void
922 bfd_assert (const char *file, int line)
924 (*_bfd_error_handler) (_("BFD %s assertion fail %s:%d"),
925 BFD_VERSION_STRING, file, line);
928 /* A more or less friendly abort message. In libbfd.h abort is
929 defined to call this function. */
931 void
932 _bfd_abort (const char *file, int line, const char *fn)
934 if (fn != NULL)
935 (*_bfd_error_handler)
936 (_("BFD %s internal error, aborting at %s line %d in %s\n"),
937 BFD_VERSION_STRING, file, line, fn);
938 else
939 (*_bfd_error_handler)
940 (_("BFD %s internal error, aborting at %s line %d\n"),
941 BFD_VERSION_STRING, file, line);
942 (*_bfd_error_handler) (_("Please report this bug.\n"));
943 _exit (EXIT_FAILURE);
947 FUNCTION
948 bfd_get_arch_size
950 SYNOPSIS
951 int bfd_get_arch_size (bfd *abfd);
953 DESCRIPTION
954 Returns the architecture address size, in bits, as determined
955 by the object file's format. For ELF, this information is
956 included in the header.
958 RETURNS
959 Returns the arch size in bits if known, <<-1>> otherwise.
963 bfd_get_arch_size (bfd *abfd)
965 if (abfd->xvec->flavour == bfd_target_elf_flavour)
966 return get_elf_backend_data (abfd)->s->arch_size;
968 return -1;
972 FUNCTION
973 bfd_get_sign_extend_vma
975 SYNOPSIS
976 int bfd_get_sign_extend_vma (bfd *abfd);
978 DESCRIPTION
979 Indicates if the target architecture "naturally" sign extends
980 an address. Some architectures implicitly sign extend address
981 values when they are converted to types larger than the size
982 of an address. For instance, bfd_get_start_address() will
983 return an address sign extended to fill a bfd_vma when this is
984 the case.
986 RETURNS
987 Returns <<1>> if the target architecture is known to sign
988 extend addresses, <<0>> if the target architecture is known to
989 not sign extend addresses, and <<-1>> otherwise.
993 bfd_get_sign_extend_vma (bfd *abfd)
995 char *name;
997 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
998 return get_elf_backend_data (abfd)->sign_extend_vma;
1000 name = bfd_get_target (abfd);
1002 /* Return a proper value for DJGPP & PE COFF.
1003 This function is required for DWARF2 support, but there is
1004 no place to store this information in the COFF back end.
1005 Should enough other COFF targets add support for DWARF2,
1006 a place will have to be found. Until then, this hack will do. */
1007 if (CONST_STRNEQ (name, "coff-go32")
1008 || strcmp (name, "pe-i386") == 0
1009 || strcmp (name, "pei-i386") == 0
1010 || strcmp (name, "pe-x86-64") == 0
1011 || strcmp (name, "pei-x86-64") == 0
1012 || strcmp (name, "pe-arm-wince-little") == 0
1013 || strcmp (name, "pei-arm-wince-little") == 0)
1014 return 1;
1016 if (CONST_STRNEQ (name, "mach-o"))
1017 return 0;
1019 bfd_set_error (bfd_error_wrong_format);
1020 return -1;
1024 FUNCTION
1025 bfd_set_start_address
1027 SYNOPSIS
1028 bfd_boolean bfd_set_start_address (bfd *abfd, bfd_vma vma);
1030 DESCRIPTION
1031 Make @var{vma} the entry point of output BFD @var{abfd}.
1033 RETURNS
1034 Returns <<TRUE>> on success, <<FALSE>> otherwise.
1037 bfd_boolean
1038 bfd_set_start_address (bfd *abfd, bfd_vma vma)
1040 abfd->start_address = vma;
1041 return TRUE;
1045 FUNCTION
1046 bfd_get_gp_size
1048 SYNOPSIS
1049 unsigned int bfd_get_gp_size (bfd *abfd);
1051 DESCRIPTION
1052 Return the maximum size of objects to be optimized using the GP
1053 register under MIPS ECOFF. This is typically set by the <<-G>>
1054 argument to the compiler, assembler or linker.
1057 unsigned int
1058 bfd_get_gp_size (bfd *abfd)
1060 if (abfd->format == bfd_object)
1062 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1063 return ecoff_data (abfd)->gp_size;
1064 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1065 return elf_gp_size (abfd);
1067 return 0;
1071 FUNCTION
1072 bfd_set_gp_size
1074 SYNOPSIS
1075 void bfd_set_gp_size (bfd *abfd, unsigned int i);
1077 DESCRIPTION
1078 Set the maximum size of objects to be optimized using the GP
1079 register under ECOFF or MIPS ELF. This is typically set by
1080 the <<-G>> argument to the compiler, assembler or linker.
1083 void
1084 bfd_set_gp_size (bfd *abfd, unsigned int i)
1086 /* Don't try to set GP size on an archive or core file! */
1087 if (abfd->format != bfd_object)
1088 return;
1090 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1091 ecoff_data (abfd)->gp_size = i;
1092 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1093 elf_gp_size (abfd) = i;
1096 /* Get the GP value. This is an internal function used by some of the
1097 relocation special_function routines on targets which support a GP
1098 register. */
1100 bfd_vma
1101 _bfd_get_gp_value (bfd *abfd)
1103 if (! abfd)
1104 return 0;
1105 if (abfd->format != bfd_object)
1106 return 0;
1108 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1109 return ecoff_data (abfd)->gp;
1110 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1111 return elf_gp (abfd);
1113 return 0;
1116 /* Set the GP value. */
1118 void
1119 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
1121 if (! abfd)
1122 abort ();
1123 if (abfd->format != bfd_object)
1124 return;
1126 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1127 ecoff_data (abfd)->gp = v;
1128 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1129 elf_gp (abfd) = v;
1133 FUNCTION
1134 bfd_scan_vma
1136 SYNOPSIS
1137 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
1139 DESCRIPTION
1140 Convert, like <<strtoul>>, a numerical expression
1141 @var{string} into a <<bfd_vma>> integer, and return that integer.
1142 (Though without as many bells and whistles as <<strtoul>>.)
1143 The expression is assumed to be unsigned (i.e., positive).
1144 If given a @var{base}, it is used as the base for conversion.
1145 A base of 0 causes the function to interpret the string
1146 in hex if a leading "0x" or "0X" is found, otherwise
1147 in octal if a leading zero is found, otherwise in decimal.
1149 If the value would overflow, the maximum <<bfd_vma>> value is
1150 returned.
1153 bfd_vma
1154 bfd_scan_vma (const char *string, const char **end, int base)
1156 bfd_vma value;
1157 bfd_vma cutoff;
1158 unsigned int cutlim;
1159 int overflow;
1161 /* Let the host do it if possible. */
1162 if (sizeof (bfd_vma) <= sizeof (unsigned long))
1163 return strtoul (string, (char **) end, base);
1165 #ifdef HAVE_STRTOULL
1166 if (sizeof (bfd_vma) <= sizeof (unsigned long long))
1167 return strtoull (string, (char **) end, base);
1168 #endif
1170 if (base == 0)
1172 if (string[0] == '0')
1174 if ((string[1] == 'x') || (string[1] == 'X'))
1175 base = 16;
1176 else
1177 base = 8;
1181 if ((base < 2) || (base > 36))
1182 base = 10;
1184 if (base == 16
1185 && string[0] == '0'
1186 && (string[1] == 'x' || string[1] == 'X')
1187 && ISXDIGIT (string[2]))
1189 string += 2;
1192 cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
1193 cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
1194 value = 0;
1195 overflow = 0;
1196 while (1)
1198 unsigned int digit;
1200 digit = *string;
1201 if (ISDIGIT (digit))
1202 digit = digit - '0';
1203 else if (ISALPHA (digit))
1204 digit = TOUPPER (digit) - 'A' + 10;
1205 else
1206 break;
1207 if (digit >= (unsigned int) base)
1208 break;
1209 if (value > cutoff || (value == cutoff && digit > cutlim))
1210 overflow = 1;
1211 value = value * base + digit;
1212 ++string;
1215 if (overflow)
1216 value = ~ (bfd_vma) 0;
1218 if (end != NULL)
1219 *end = string;
1221 return value;
1225 FUNCTION
1226 bfd_copy_private_header_data
1228 SYNOPSIS
1229 bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
1231 DESCRIPTION
1232 Copy private BFD header information from the BFD @var{ibfd} to the
1233 the BFD @var{obfd}. This copies information that may require
1234 sections to exist, but does not require symbol tables. Return
1235 <<true>> on success, <<false>> on error.
1236 Possible error returns are:
1238 o <<bfd_error_no_memory>> -
1239 Not enough memory exists to create private data for @var{obfd}.
1241 .#define bfd_copy_private_header_data(ibfd, obfd) \
1242 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
1243 . (ibfd, obfd))
1248 FUNCTION
1249 bfd_copy_private_bfd_data
1251 SYNOPSIS
1252 bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
1254 DESCRIPTION
1255 Copy private BFD information from the BFD @var{ibfd} to the
1256 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
1257 Possible error returns are:
1259 o <<bfd_error_no_memory>> -
1260 Not enough memory exists to create private data for @var{obfd}.
1262 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
1263 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
1264 . (ibfd, obfd))
1269 FUNCTION
1270 bfd_merge_private_bfd_data
1272 SYNOPSIS
1273 bfd_boolean bfd_merge_private_bfd_data (bfd *ibfd, bfd *obfd);
1275 DESCRIPTION
1276 Merge private BFD information from the BFD @var{ibfd} to the
1277 the output file BFD @var{obfd} when linking. Return <<TRUE>>
1278 on success, <<FALSE>> on error. Possible error returns are:
1280 o <<bfd_error_no_memory>> -
1281 Not enough memory exists to create private data for @var{obfd}.
1283 .#define bfd_merge_private_bfd_data(ibfd, obfd) \
1284 . BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
1285 . (ibfd, obfd))
1290 FUNCTION
1291 bfd_set_private_flags
1293 SYNOPSIS
1294 bfd_boolean bfd_set_private_flags (bfd *abfd, flagword flags);
1296 DESCRIPTION
1297 Set private BFD flag information in the BFD @var{abfd}.
1298 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
1299 returns are:
1301 o <<bfd_error_no_memory>> -
1302 Not enough memory exists to create private data for @var{obfd}.
1304 .#define bfd_set_private_flags(abfd, flags) \
1305 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
1310 FUNCTION
1311 Other functions
1313 DESCRIPTION
1314 The following functions exist but have not yet been documented.
1316 .#define bfd_sizeof_headers(abfd, info) \
1317 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
1319 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
1320 . BFD_SEND (abfd, _bfd_find_nearest_line, \
1321 . (abfd, sec, syms, off, file, func, line))
1323 .#define bfd_find_line(abfd, syms, sym, file, line) \
1324 . BFD_SEND (abfd, _bfd_find_line, \
1325 . (abfd, syms, sym, file, line))
1327 .#define bfd_find_inliner_info(abfd, file, func, line) \
1328 . BFD_SEND (abfd, _bfd_find_inliner_info, \
1329 . (abfd, file, func, line))
1331 .#define bfd_debug_info_start(abfd) \
1332 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
1334 .#define bfd_debug_info_end(abfd) \
1335 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
1337 .#define bfd_debug_info_accumulate(abfd, section) \
1338 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
1340 .#define bfd_stat_arch_elt(abfd, stat) \
1341 . BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
1343 .#define bfd_update_armap_timestamp(abfd) \
1344 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
1346 .#define bfd_set_arch_mach(abfd, arch, mach)\
1347 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
1349 .#define bfd_relax_section(abfd, section, link_info, again) \
1350 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
1352 .#define bfd_gc_sections(abfd, link_info) \
1353 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
1355 .#define bfd_merge_sections(abfd, link_info) \
1356 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
1358 .#define bfd_is_group_section(abfd, sec) \
1359 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
1361 .#define bfd_discard_group(abfd, sec) \
1362 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
1364 .#define bfd_link_hash_table_create(abfd) \
1365 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
1367 .#define bfd_link_hash_table_free(abfd, hash) \
1368 . BFD_SEND (abfd, _bfd_link_hash_table_free, (hash))
1370 .#define bfd_link_add_symbols(abfd, info) \
1371 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
1373 .#define bfd_link_just_syms(abfd, sec, info) \
1374 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
1376 .#define bfd_final_link(abfd, info) \
1377 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
1379 .#define bfd_free_cached_info(abfd) \
1380 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
1382 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
1383 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
1385 .#define bfd_print_private_bfd_data(abfd, file)\
1386 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
1388 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
1389 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
1391 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
1392 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
1393 . dyncount, dynsyms, ret))
1395 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
1396 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
1398 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
1399 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
1401 .extern bfd_byte *bfd_get_relocated_section_contents
1402 . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
1403 . bfd_boolean, asymbol **);
1408 bfd_byte *
1409 bfd_get_relocated_section_contents (bfd *abfd,
1410 struct bfd_link_info *link_info,
1411 struct bfd_link_order *link_order,
1412 bfd_byte *data,
1413 bfd_boolean relocatable,
1414 asymbol **symbols)
1416 bfd *abfd2;
1417 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
1418 bfd_byte *, bfd_boolean, asymbol **);
1420 if (link_order->type == bfd_indirect_link_order)
1422 abfd2 = link_order->u.indirect.section->owner;
1423 if (abfd2 == NULL)
1424 abfd2 = abfd;
1426 else
1427 abfd2 = abfd;
1429 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
1431 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
1434 /* Record information about an ELF program header. */
1436 bfd_boolean
1437 bfd_record_phdr (bfd *abfd,
1438 unsigned long type,
1439 bfd_boolean flags_valid,
1440 flagword flags,
1441 bfd_boolean at_valid,
1442 bfd_vma at,
1443 bfd_boolean includes_filehdr,
1444 bfd_boolean includes_phdrs,
1445 unsigned int count,
1446 asection **secs)
1448 struct elf_segment_map *m, **pm;
1449 bfd_size_type amt;
1451 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1452 return TRUE;
1454 amt = sizeof (struct elf_segment_map);
1455 amt += ((bfd_size_type) count - 1) * sizeof (asection *);
1456 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
1457 if (m == NULL)
1458 return FALSE;
1460 m->p_type = type;
1461 m->p_flags = flags;
1462 m->p_paddr = at;
1463 m->p_flags_valid = flags_valid;
1464 m->p_paddr_valid = at_valid;
1465 m->includes_filehdr = includes_filehdr;
1466 m->includes_phdrs = includes_phdrs;
1467 m->count = count;
1468 if (count > 0)
1469 memcpy (m->sections, secs, count * sizeof (asection *));
1471 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
1473 *pm = m;
1475 return TRUE;
1478 #ifdef BFD64
1479 /* Return true iff this target is 32-bit. */
1481 static bfd_boolean
1482 is32bit (bfd *abfd)
1484 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1486 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1487 return bed->s->elfclass == ELFCLASS32;
1490 /* For non-ELF targets, use architecture information. */
1491 return bfd_arch_bits_per_address (abfd) <= 32;
1493 #endif
1495 /* bfd_sprintf_vma and bfd_fprintf_vma display an address in the
1496 target's address size. */
1498 void
1499 bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
1501 #ifdef BFD64
1502 if (is32bit (abfd))
1504 sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
1505 return;
1507 #endif
1508 sprintf_vma (buf, value);
1511 void
1512 bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
1514 #ifdef BFD64
1515 if (is32bit (abfd))
1517 fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
1518 return;
1520 #endif
1521 fprintf_vma ((FILE *) stream, value);
1525 FUNCTION
1526 bfd_alt_mach_code
1528 SYNOPSIS
1529 bfd_boolean bfd_alt_mach_code (bfd *abfd, int alternative);
1531 DESCRIPTION
1533 When more than one machine code number is available for the
1534 same machine type, this function can be used to switch between
1535 the preferred one (alternative == 0) and any others. Currently,
1536 only ELF supports this feature, with up to two alternate
1537 machine codes.
1540 bfd_boolean
1541 bfd_alt_mach_code (bfd *abfd, int alternative)
1543 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1545 int code;
1547 switch (alternative)
1549 case 0:
1550 code = get_elf_backend_data (abfd)->elf_machine_code;
1551 break;
1553 case 1:
1554 code = get_elf_backend_data (abfd)->elf_machine_alt1;
1555 if (code == 0)
1556 return FALSE;
1557 break;
1559 case 2:
1560 code = get_elf_backend_data (abfd)->elf_machine_alt2;
1561 if (code == 0)
1562 return FALSE;
1563 break;
1565 default:
1566 return FALSE;
1569 elf_elfheader (abfd)->e_machine = code;
1571 return TRUE;
1574 return FALSE;
1578 CODE_FRAGMENT
1580 .struct bfd_preserve
1582 . void *marker;
1583 . void *tdata;
1584 . flagword flags;
1585 . const struct bfd_arch_info *arch_info;
1586 . struct bfd_section *sections;
1587 . struct bfd_section *section_last;
1588 . unsigned int section_count;
1589 . struct bfd_hash_table section_htab;
1595 FUNCTION
1596 bfd_preserve_save
1598 SYNOPSIS
1599 bfd_boolean bfd_preserve_save (bfd *, struct bfd_preserve *);
1601 DESCRIPTION
1602 When testing an object for compatibility with a particular
1603 target back-end, the back-end object_p function needs to set
1604 up certain fields in the bfd on successfully recognizing the
1605 object. This typically happens in a piecemeal fashion, with
1606 failures possible at many points. On failure, the bfd is
1607 supposed to be restored to its initial state, which is
1608 virtually impossible. However, restoring a subset of the bfd
1609 state works in practice. This function stores the subset and
1610 reinitializes the bfd.
1614 bfd_boolean
1615 bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
1617 preserve->tdata = abfd->tdata.any;
1618 preserve->arch_info = abfd->arch_info;
1619 preserve->flags = abfd->flags;
1620 preserve->sections = abfd->sections;
1621 preserve->section_last = abfd->section_last;
1622 preserve->section_count = abfd->section_count;
1623 preserve->section_htab = abfd->section_htab;
1625 if (! bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
1626 sizeof (struct section_hash_entry)))
1627 return FALSE;
1629 abfd->tdata.any = NULL;
1630 abfd->arch_info = &bfd_default_arch_struct;
1631 abfd->flags &= BFD_IN_MEMORY;
1632 abfd->sections = NULL;
1633 abfd->section_last = NULL;
1634 abfd->section_count = 0;
1636 return TRUE;
1640 FUNCTION
1641 bfd_preserve_restore
1643 SYNOPSIS
1644 void bfd_preserve_restore (bfd *, struct bfd_preserve *);
1646 DESCRIPTION
1647 This function restores bfd state saved by bfd_preserve_save.
1648 If MARKER is non-NULL in struct bfd_preserve then that block
1649 and all subsequently bfd_alloc'd memory is freed.
1653 void
1654 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
1656 bfd_hash_table_free (&abfd->section_htab);
1658 abfd->tdata.any = preserve->tdata;
1659 abfd->arch_info = preserve->arch_info;
1660 abfd->flags = preserve->flags;
1661 abfd->section_htab = preserve->section_htab;
1662 abfd->sections = preserve->sections;
1663 abfd->section_last = preserve->section_last;
1664 abfd->section_count = preserve->section_count;
1666 /* bfd_release frees all memory more recently bfd_alloc'd than
1667 its arg, as well as its arg. */
1668 if (preserve->marker != NULL)
1670 bfd_release (abfd, preserve->marker);
1671 preserve->marker = NULL;
1676 FUNCTION
1677 bfd_preserve_finish
1679 SYNOPSIS
1680 void bfd_preserve_finish (bfd *, struct bfd_preserve *);
1682 DESCRIPTION
1683 This function should be called when the bfd state saved by
1684 bfd_preserve_save is no longer needed. ie. when the back-end
1685 object_p function returns with success.
1689 void
1690 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
1692 /* It would be nice to be able to free more memory here, eg. old
1693 tdata, but that's not possible since these blocks are sitting
1694 inside bfd_alloc'd memory. The section hash is on a separate
1695 objalloc. */
1696 bfd_hash_table_free (&preserve->section_htab);
1700 FUNCTION
1701 bfd_emul_get_maxpagesize
1703 SYNOPSIS
1704 bfd_vma bfd_emul_get_maxpagesize (const char *);
1706 DESCRIPTION
1707 Returns the maximum page size, in bytes, as determined by
1708 emulation.
1710 RETURNS
1711 Returns the maximum page size in bytes for ELF, 0 otherwise.
1714 bfd_vma
1715 bfd_emul_get_maxpagesize (const char *emul)
1717 const bfd_target *target;
1719 target = bfd_find_target (emul, NULL);
1720 if (target != NULL
1721 && target->flavour == bfd_target_elf_flavour)
1722 return xvec_get_elf_backend_data (target)->maxpagesize;
1724 return 0;
1727 static void
1728 bfd_elf_set_pagesize (const bfd_target *target, bfd_vma size,
1729 int offset, const bfd_target *orig_target)
1731 if (target->flavour == bfd_target_elf_flavour)
1733 const struct elf_backend_data *bed;
1735 bed = xvec_get_elf_backend_data (target);
1736 *((bfd_vma *) ((char *) bed + offset)) = size;
1739 if (target->alternative_target
1740 && target->alternative_target != orig_target)
1741 bfd_elf_set_pagesize (target->alternative_target, size, offset,
1742 orig_target);
1746 FUNCTION
1747 bfd_emul_set_maxpagesize
1749 SYNOPSIS
1750 void bfd_emul_set_maxpagesize (const char *, bfd_vma);
1752 DESCRIPTION
1753 For ELF, set the maximum page size for the emulation. It is
1754 a no-op for other formats.
1758 void
1759 bfd_emul_set_maxpagesize (const char *emul, bfd_vma size)
1761 const bfd_target *target;
1763 target = bfd_find_target (emul, NULL);
1764 if (target)
1765 bfd_elf_set_pagesize (target, size,
1766 offsetof (struct elf_backend_data,
1767 maxpagesize), target);
1771 FUNCTION
1772 bfd_emul_get_commonpagesize
1774 SYNOPSIS
1775 bfd_vma bfd_emul_get_commonpagesize (const char *);
1777 DESCRIPTION
1778 Returns the common page size, in bytes, as determined by
1779 emulation.
1781 RETURNS
1782 Returns the common page size in bytes for ELF, 0 otherwise.
1785 bfd_vma
1786 bfd_emul_get_commonpagesize (const char *emul)
1788 const bfd_target *target;
1790 target = bfd_find_target (emul, NULL);
1791 if (target != NULL
1792 && target->flavour == bfd_target_elf_flavour)
1793 return xvec_get_elf_backend_data (target)->commonpagesize;
1795 return 0;
1799 FUNCTION
1800 bfd_emul_set_commonpagesize
1802 SYNOPSIS
1803 void bfd_emul_set_commonpagesize (const char *, bfd_vma);
1805 DESCRIPTION
1806 For ELF, set the common page size for the emulation. It is
1807 a no-op for other formats.
1811 void
1812 bfd_emul_set_commonpagesize (const char *emul, bfd_vma size)
1814 const bfd_target *target;
1816 target = bfd_find_target (emul, NULL);
1817 if (target)
1818 bfd_elf_set_pagesize (target, size,
1819 offsetof (struct elf_backend_data,
1820 commonpagesize), target);
1824 FUNCTION
1825 bfd_demangle
1827 SYNOPSIS
1828 char *bfd_demangle (bfd *, const char *, int);
1830 DESCRIPTION
1831 Wrapper around cplus_demangle. Strips leading underscores and
1832 other such chars that would otherwise confuse the demangler.
1833 If passed a g++ v3 ABI mangled name, returns a buffer allocated
1834 with malloc holding the demangled name. Returns NULL otherwise
1835 and on memory alloc failure.
1838 char *
1839 bfd_demangle (bfd *abfd, const char *name, int options)
1841 char *res, *alloc;
1842 const char *pre, *suf;
1843 size_t pre_len;
1844 bfd_boolean skip_lead;
1846 skip_lead = (abfd != NULL
1847 && *name != '\0'
1848 && bfd_get_symbol_leading_char (abfd) == *name);
1849 if (skip_lead)
1850 ++name;
1852 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
1853 or the MS PE format. These formats have a number of leading '.'s
1854 on at least some symbols, so we remove all dots to avoid
1855 confusing the demangler. */
1856 pre = name;
1857 while (*name == '.' || *name == '$')
1858 ++name;
1859 pre_len = name - pre;
1861 /* Strip off @plt and suchlike too. */
1862 alloc = NULL;
1863 suf = strchr (name, '@');
1864 if (suf != NULL)
1866 alloc = (char *) bfd_malloc (suf - name + 1);
1867 if (alloc == NULL)
1868 return NULL;
1869 memcpy (alloc, name, suf - name);
1870 alloc[suf - name] = '\0';
1871 name = alloc;
1874 res = cplus_demangle (name, options);
1876 if (alloc != NULL)
1877 free (alloc);
1879 if (res == NULL)
1881 if (skip_lead)
1883 size_t len = strlen (pre) + 1;
1884 alloc = (char *) bfd_malloc (len);
1885 if (alloc == NULL)
1886 return NULL;
1887 memcpy (alloc, pre, len);
1888 return alloc;
1890 return NULL;
1893 /* Put back any prefix or suffix. */
1894 if (pre_len != 0 || suf != NULL)
1896 size_t len;
1897 size_t suf_len;
1898 char *final;
1900 len = strlen (res);
1901 if (suf == NULL)
1902 suf = res + len;
1903 suf_len = strlen (suf) + 1;
1904 final = (char *) bfd_malloc (pre_len + len + suf_len);
1905 if (final != NULL)
1907 memcpy (final, pre, pre_len);
1908 memcpy (final + pre_len, res, len);
1909 memcpy (final + pre_len + len, suf, suf_len);
1911 free (res);
1912 res = final;
1915 return res;