Don't add the zlib header to SHF_COMPRESSED section
[deliverable/binutils-gdb.git] / bfd / bfd.c
1 /* Generic BFD library interface and support routines.
2 Copyright (C) 1990-2015 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 /*
23 INODE
24 typedef bfd, Error reporting, BFD front end, BFD front end
25
26 SECTION
27 <<typedef bfd>>
28
29 A BFD has type <<bfd>>; objects of this type are the
30 cornerstone of any application using BFD. Using BFD
31 consists of making references though the BFD and to data in the BFD.
32
33 Here is the structure that defines the type <<bfd>>. It
34 contains the major data about the file and pointers
35 to the rest of the data.
36
37 CODE_FRAGMENT
38 .
39 .enum bfd_direction
40 . {
41 . no_direction = 0,
42 . read_direction = 1,
43 . write_direction = 2,
44 . both_direction = 3
45 . };
46 .
47 .enum bfd_plugin_format
48 . {
49 . bfd_plugin_uknown = 0,
50 . bfd_plugin_yes = 1,
51 . bfd_plugin_no = 2
52 . };
53 .
54 .struct bfd
55 .{
56 . {* The filename the application opened the BFD with. *}
57 . const char *filename;
58 .
59 . {* A pointer to the target jump table. *}
60 . const struct bfd_target *xvec;
61 .
62 . {* The IOSTREAM, and corresponding IO vector that provide access
63 . to the file backing the BFD. *}
64 . void *iostream;
65 . const struct bfd_iovec *iovec;
66 .
67 . {* The caching routines use these to maintain a
68 . least-recently-used list of BFDs. *}
69 . struct bfd *lru_prev, *lru_next;
70 .
71 . {* When a file is closed by the caching routines, BFD retains
72 . state information on the file here... *}
73 . ufile_ptr where;
74 .
75 . {* File modified time, if mtime_set is TRUE. *}
76 . long mtime;
77 .
78 . {* A unique identifier of the BFD *}
79 . unsigned int id;
80 .
81 . {* The format which belongs to the BFD. (object, core, etc.) *}
82 . ENUM_BITFIELD (bfd_format) format : 3;
83 .
84 . {* The direction with which the BFD was opened. *}
85 . ENUM_BITFIELD (bfd_direction) direction : 2;
86 .
87 . {* Format_specific flags. *}
88 . flagword flags : 18;
89 .
90 . {* Values that may appear in the flags field of a BFD. These also
91 . appear in the object_flags field of the bfd_target structure, where
92 . they indicate the set of flags used by that backend (not all flags
93 . are meaningful for all object file formats) (FIXME: at the moment,
94 . the object_flags values have mostly just been copied from backend
95 . to another, and are not necessarily correct). *}
96 .
97 .#define BFD_NO_FLAGS 0x00
98 .
99 . {* BFD contains relocation entries. *}
100 .#define HAS_RELOC 0x01
101 .
102 . {* BFD is directly executable. *}
103 .#define EXEC_P 0x02
104 .
105 . {* BFD has line number information (basically used for F_LNNO in a
106 . COFF header). *}
107 .#define HAS_LINENO 0x04
108 .
109 . {* BFD has debugging information. *}
110 .#define HAS_DEBUG 0x08
111 .
112 . {* BFD has symbols. *}
113 .#define HAS_SYMS 0x10
114 .
115 . {* BFD has local symbols (basically used for F_LSYMS in a COFF
116 . header). *}
117 .#define HAS_LOCALS 0x20
118 .
119 . {* BFD is a dynamic object. *}
120 .#define DYNAMIC 0x40
121 .
122 . {* Text section is write protected (if D_PAGED is not set, this is
123 . like an a.out NMAGIC file) (the linker sets this by default, but
124 . clears it for -r or -N). *}
125 .#define WP_TEXT 0x80
126 .
127 . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
128 . linker sets this by default, but clears it for -r or -n or -N). *}
129 .#define D_PAGED 0x100
130 .
131 . {* BFD is relaxable (this means that bfd_relax_section may be able to
132 . do something) (sometimes bfd_relax_section can do something even if
133 . this is not set). *}
134 .#define BFD_IS_RELAXABLE 0x200
135 .
136 . {* This may be set before writing out a BFD to request using a
137 . traditional format. For example, this is used to request that when
138 . writing out an a.out object the symbols not be hashed to eliminate
139 . duplicates. *}
140 .#define BFD_TRADITIONAL_FORMAT 0x400
141 .
142 . {* This flag indicates that the BFD contents are actually cached
143 . in memory. If this is set, iostream points to a bfd_in_memory
144 . struct. *}
145 .#define BFD_IN_MEMORY 0x800
146 .
147 . {* This BFD has been created by the linker and doesn't correspond
148 . to any input file. *}
149 .#define BFD_LINKER_CREATED 0x1000
150 .
151 . {* This may be set before writing out a BFD to request that it
152 . be written using values for UIDs, GIDs, timestamps, etc. that
153 . will be consistent from run to run. *}
154 .#define BFD_DETERMINISTIC_OUTPUT 0x2000
155 .
156 . {* Compress sections in this BFD. *}
157 .#define BFD_COMPRESS 0x4000
158 .
159 . {* Decompress sections in this BFD. *}
160 .#define BFD_DECOMPRESS 0x8000
161 .
162 . {* BFD is a dummy, for plugins. *}
163 .#define BFD_PLUGIN 0x10000
164 .
165 . {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
166 .#define BFD_COMPRESS_GABI 0x20000
167 .
168 . {* Flags bits to be saved in bfd_preserve_save. *}
169 .#define BFD_FLAGS_SAVED \
170 . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_PLUGIN \
171 . | BFD_COMPRESS_GABI)
172 .
173 . {* Flags bits which are for BFD use only. *}
174 .#define BFD_FLAGS_FOR_BFD_USE_MASK \
175 . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
176 . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
177 . | BFD_COMPRESS_GABI)
178 .
179 . {* Is the file descriptor being cached? That is, can it be closed as
180 . needed, and re-opened when accessed later? *}
181 . unsigned int cacheable : 1;
182 .
183 . {* Marks whether there was a default target specified when the
184 . BFD was opened. This is used to select which matching algorithm
185 . to use to choose the back end. *}
186 . unsigned int target_defaulted : 1;
187 .
188 . {* ... and here: (``once'' means at least once). *}
189 . unsigned int opened_once : 1;
190 .
191 . {* Set if we have a locally maintained mtime value, rather than
192 . getting it from the file each time. *}
193 . unsigned int mtime_set : 1;
194 .
195 . {* Flag set if symbols from this BFD should not be exported. *}
196 . unsigned int no_export : 1;
197 .
198 . {* Remember when output has begun, to stop strange things
199 . from happening. *}
200 . unsigned int output_has_begun : 1;
201 .
202 . {* Have archive map. *}
203 . unsigned int has_armap : 1;
204 .
205 . {* Set if this is a thin archive. *}
206 . unsigned int is_thin_archive : 1;
207 .
208 . {* Set if only required symbols should be added in the link hash table for
209 . this object. Used by VMS linkers. *}
210 . unsigned int selective_search : 1;
211 .
212 . {* Set if this is the linker output BFD. *}
213 . unsigned int is_linker_output : 1;
214 .
215 . {* Set if this is the linker input BFD. *}
216 . unsigned int is_linker_input : 1;
217 .
218 . {* If this is an input for a compiler plug-in library. *}
219 . ENUM_BITFIELD (bfd_plugin_format) plugin_format : 2;
220 .
221 . {* Set if this is a plugin output file. *}
222 . unsigned int lto_output : 1;
223 .
224 . {* Set to dummy BFD created when claimed by a compiler plug-in
225 . library. *}
226 . bfd *plugin_dummy_bfd;
227 .
228 . {* Currently my_archive is tested before adding origin to
229 . anything. I believe that this can become always an add of
230 . origin, with origin set to 0 for non archive files. *}
231 . ufile_ptr origin;
232 .
233 . {* The origin in the archive of the proxy entry. This will
234 . normally be the same as origin, except for thin archives,
235 . when it will contain the current offset of the proxy in the
236 . thin archive rather than the offset of the bfd in its actual
237 . container. *}
238 . ufile_ptr proxy_origin;
239 .
240 . {* A hash table for section names. *}
241 . struct bfd_hash_table section_htab;
242 .
243 . {* Pointer to linked list of sections. *}
244 . struct bfd_section *sections;
245 .
246 . {* The last section on the section list. *}
247 . struct bfd_section *section_last;
248 .
249 . {* The number of sections. *}
250 . unsigned int section_count;
251 .
252 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
253 . be used only for archive elements. *}
254 . int archive_pass;
255 .
256 . {* Stuff only useful for object files:
257 . The start address. *}
258 . bfd_vma start_address;
259 .
260 . {* Symbol table for output BFD (with symcount entries).
261 . Also used by the linker to cache input BFD symbols. *}
262 . struct bfd_symbol **outsymbols;
263 .
264 . {* Used for input and output. *}
265 . unsigned int symcount;
266 .
267 . {* Used for slurped dynamic symbol tables. *}
268 . unsigned int dynsymcount;
269 .
270 . {* Pointer to structure which contains architecture information. *}
271 . const struct bfd_arch_info *arch_info;
272 .
273 . {* Stuff only useful for archives. *}
274 . void *arelt_data;
275 . struct bfd *my_archive; {* The containing archive BFD. *}
276 . struct bfd *archive_next; {* The next BFD in the archive. *}
277 . struct bfd *archive_head; {* The first BFD in the archive. *}
278 . struct bfd *nested_archives; {* List of nested archive in a flattened
279 . thin archive. *}
280 .
281 . union {
282 . {* For input BFDs, a chain of BFDs involved in a link. *}
283 . struct bfd *next;
284 . {* For output BFD, the linker hash table. *}
285 . struct bfd_link_hash_table *hash;
286 . } link;
287 .
288 . {* Used by the back end to hold private data. *}
289 . union
290 . {
291 . struct aout_data_struct *aout_data;
292 . struct artdata *aout_ar_data;
293 . struct _oasys_data *oasys_obj_data;
294 . struct _oasys_ar_data *oasys_ar_data;
295 . struct coff_tdata *coff_obj_data;
296 . struct pe_tdata *pe_obj_data;
297 . struct xcoff_tdata *xcoff_obj_data;
298 . struct ecoff_tdata *ecoff_obj_data;
299 . struct ieee_data_struct *ieee_data;
300 . struct ieee_ar_data_struct *ieee_ar_data;
301 . struct srec_data_struct *srec_data;
302 . struct verilog_data_struct *verilog_data;
303 . struct ihex_data_struct *ihex_data;
304 . struct tekhex_data_struct *tekhex_data;
305 . struct elf_obj_tdata *elf_obj_data;
306 . struct nlm_obj_tdata *nlm_obj_data;
307 . struct bout_data_struct *bout_data;
308 . struct mmo_data_struct *mmo_data;
309 . struct sun_core_struct *sun_core_data;
310 . struct sco5_core_struct *sco5_core_data;
311 . struct trad_core_struct *trad_core_data;
312 . struct som_data_struct *som_data;
313 . struct hpux_core_struct *hpux_core_data;
314 . struct hppabsd_core_struct *hppabsd_core_data;
315 . struct sgi_core_struct *sgi_core_data;
316 . struct lynx_core_struct *lynx_core_data;
317 . struct osf_core_struct *osf_core_data;
318 . struct cisco_core_struct *cisco_core_data;
319 . struct versados_data_struct *versados_data;
320 . struct netbsd_core_struct *netbsd_core_data;
321 . struct mach_o_data_struct *mach_o_data;
322 . struct mach_o_fat_data_struct *mach_o_fat_data;
323 . struct plugin_data_struct *plugin_data;
324 . struct bfd_pef_data_struct *pef_data;
325 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
326 . struct bfd_sym_data_struct *sym_data;
327 . void *any;
328 . }
329 . tdata;
330 .
331 . {* Used by the application to hold private data. *}
332 . void *usrdata;
333 .
334 . {* Where all the allocated stuff under this BFD goes. This is a
335 . struct objalloc *, but we use void * to avoid requiring the inclusion
336 . of objalloc.h. *}
337 . void *memory;
338 .};
339 .
340 .{* See note beside bfd_set_section_userdata. *}
341 .static inline bfd_boolean
342 .bfd_set_cacheable (bfd * abfd, bfd_boolean val)
343 .{
344 . abfd->cacheable = val;
345 . return TRUE;
346 .}
347 .
348 */
349
350 #include "sysdep.h"
351 #include <stdarg.h>
352 #include "bfd.h"
353 #include "bfdver.h"
354 #include "libiberty.h"
355 #include "demangle.h"
356 #include "safe-ctype.h"
357 #include "bfdlink.h"
358 #include "libbfd.h"
359 #include "coff/internal.h"
360 #include "coff/sym.h"
361 #include "libcoff.h"
362 #include "libecoff.h"
363 #undef obj_symbols
364 #include "elf-bfd.h"
365
366 #ifndef EXIT_FAILURE
367 #define EXIT_FAILURE 1
368 #endif
369
370 \f
371 /* provide storage for subsystem, stack and heap data which may have been
372 passed in on the command line. Ld puts this data into a bfd_link_info
373 struct which ultimately gets passed in to the bfd. When it arrives, copy
374 it to the following struct so that the data will be available in coffcode.h
375 where it is needed. The typedef's used are defined in bfd.h */
376 \f
377 /*
378 INODE
379 Error reporting, Miscellaneous, typedef bfd, BFD front end
380
381 SECTION
382 Error reporting
383
384 Most BFD functions return nonzero on success (check their
385 individual documentation for precise semantics). On an error,
386 they call <<bfd_set_error>> to set an error condition that callers
387 can check by calling <<bfd_get_error>>.
388 If that returns <<bfd_error_system_call>>, then check
389 <<errno>>.
390
391 The easiest way to report a BFD error to the user is to
392 use <<bfd_perror>>.
393
394 SUBSECTION
395 Type <<bfd_error_type>>
396
397 The values returned by <<bfd_get_error>> are defined by the
398 enumerated type <<bfd_error_type>>.
399
400 CODE_FRAGMENT
401 .
402 .typedef enum bfd_error
403 .{
404 . bfd_error_no_error = 0,
405 . bfd_error_system_call,
406 . bfd_error_invalid_target,
407 . bfd_error_wrong_format,
408 . bfd_error_wrong_object_format,
409 . bfd_error_invalid_operation,
410 . bfd_error_no_memory,
411 . bfd_error_no_symbols,
412 . bfd_error_no_armap,
413 . bfd_error_no_more_archived_files,
414 . bfd_error_malformed_archive,
415 . bfd_error_missing_dso,
416 . bfd_error_file_not_recognized,
417 . bfd_error_file_ambiguously_recognized,
418 . bfd_error_no_contents,
419 . bfd_error_nonrepresentable_section,
420 . bfd_error_no_debug_section,
421 . bfd_error_bad_value,
422 . bfd_error_file_truncated,
423 . bfd_error_file_too_big,
424 . bfd_error_on_input,
425 . bfd_error_invalid_error_code
426 .}
427 .bfd_error_type;
428 .
429 */
430
431 static bfd_error_type bfd_error = bfd_error_no_error;
432 static bfd *input_bfd = NULL;
433 static bfd_error_type input_error = bfd_error_no_error;
434
435 const char *const bfd_errmsgs[] =
436 {
437 N_("No error"),
438 N_("System call error"),
439 N_("Invalid bfd target"),
440 N_("File in wrong format"),
441 N_("Archive object file in wrong format"),
442 N_("Invalid operation"),
443 N_("Memory exhausted"),
444 N_("No symbols"),
445 N_("Archive has no index; run ranlib to add one"),
446 N_("No more archived files"),
447 N_("Malformed archive"),
448 N_("DSO missing from command line"),
449 N_("File format not recognized"),
450 N_("File format is ambiguous"),
451 N_("Section has no contents"),
452 N_("Nonrepresentable section on output"),
453 N_("Symbol needs debug section which does not exist"),
454 N_("Bad value"),
455 N_("File truncated"),
456 N_("File too big"),
457 N_("Error reading %s: %s"),
458 N_("#<Invalid error code>")
459 };
460
461 /*
462 FUNCTION
463 bfd_get_error
464
465 SYNOPSIS
466 bfd_error_type bfd_get_error (void);
467
468 DESCRIPTION
469 Return the current BFD error condition.
470 */
471
472 bfd_error_type
473 bfd_get_error (void)
474 {
475 return bfd_error;
476 }
477
478 /*
479 FUNCTION
480 bfd_set_error
481
482 SYNOPSIS
483 void bfd_set_error (bfd_error_type error_tag, ...);
484
485 DESCRIPTION
486 Set the BFD error condition to be @var{error_tag}.
487 If @var{error_tag} is bfd_error_on_input, then this function
488 takes two more parameters, the input bfd where the error
489 occurred, and the bfd_error_type error.
490 */
491
492 void
493 bfd_set_error (bfd_error_type error_tag, ...)
494 {
495 bfd_error = error_tag;
496 if (error_tag == bfd_error_on_input)
497 {
498 /* This is an error that occurred during bfd_close when
499 writing an archive, but on one of the input files. */
500 va_list ap;
501
502 va_start (ap, error_tag);
503 input_bfd = va_arg (ap, bfd *);
504 input_error = (bfd_error_type) va_arg (ap, int);
505 if (input_error >= bfd_error_on_input)
506 abort ();
507 va_end (ap);
508 }
509 }
510
511 /*
512 FUNCTION
513 bfd_errmsg
514
515 SYNOPSIS
516 const char *bfd_errmsg (bfd_error_type error_tag);
517
518 DESCRIPTION
519 Return a string describing the error @var{error_tag}, or
520 the system error if @var{error_tag} is <<bfd_error_system_call>>.
521 */
522
523 const char *
524 bfd_errmsg (bfd_error_type error_tag)
525 {
526 #ifndef errno
527 extern int errno;
528 #endif
529 if (error_tag == bfd_error_on_input)
530 {
531 char *buf;
532 const char *msg = bfd_errmsg (input_error);
533
534 if (asprintf (&buf, _(bfd_errmsgs [error_tag]), input_bfd->filename, msg)
535 != -1)
536 return buf;
537
538 /* Ick, what to do on out of memory? */
539 return msg;
540 }
541
542 if (error_tag == bfd_error_system_call)
543 return xstrerror (errno);
544
545 if (error_tag > bfd_error_invalid_error_code)
546 error_tag = bfd_error_invalid_error_code; /* sanity check */
547
548 return _(bfd_errmsgs [error_tag]);
549 }
550
551 /*
552 FUNCTION
553 bfd_perror
554
555 SYNOPSIS
556 void bfd_perror (const char *message);
557
558 DESCRIPTION
559 Print to the standard error stream a string describing the
560 last BFD error that occurred, or the last system error if
561 the last BFD error was a system call failure. If @var{message}
562 is non-NULL and non-empty, the error string printed is preceded
563 by @var{message}, a colon, and a space. It is followed by a newline.
564 */
565
566 void
567 bfd_perror (const char *message)
568 {
569 fflush (stdout);
570 if (message == NULL || *message == '\0')
571 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
572 else
573 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
574 fflush (stderr);
575 }
576
577 /*
578 SUBSECTION
579 BFD error handler
580
581 Some BFD functions want to print messages describing the
582 problem. They call a BFD error handler function. This
583 function may be overridden by the program.
584
585 The BFD error handler acts like printf.
586
587 CODE_FRAGMENT
588 .
589 .typedef void (*bfd_error_handler_type) (const char *, ...);
590 .
591 */
592
593 /* The program name used when printing BFD error messages. */
594
595 static const char *_bfd_error_program_name;
596
597 /* This is the default routine to handle BFD error messages.
598 Like fprintf (stderr, ...), but also handles some extra format specifiers.
599
600 %A section name from section. For group components, print group name too.
601 %B file name from bfd. For archive components, prints archive too.
602
603 Note - because these two extra format specifiers require special handling
604 they are scanned for and processed in this function, before calling
605 vfprintf. This means that the *arguments* for these format specifiers
606 must be the first ones in the variable argument list, regardless of where
607 the specifiers appear in the format string. Thus for example calling
608 this function with a format string of:
609
610 "blah %s blah %A blah %d blah %B"
611
612 would involve passing the arguments as:
613
614 "blah %s blah %A blah %d blah %B",
615 asection_for_the_%A,
616 bfd_for_the_%B,
617 string_for_the_%s,
618 integer_for_the_%d);
619 */
620
621 void
622 _bfd_default_error_handler (const char *fmt, ...)
623 {
624 va_list ap;
625 char *bufp;
626 const char *new_fmt, *p;
627 size_t avail = 1000;
628 char buf[1000];
629
630 /* PR 4992: Don't interrupt output being sent to stdout. */
631 fflush (stdout);
632
633 if (_bfd_error_program_name != NULL)
634 fprintf (stderr, "%s: ", _bfd_error_program_name);
635 else
636 fprintf (stderr, "BFD: ");
637
638 va_start (ap, fmt);
639 new_fmt = fmt;
640 bufp = buf;
641
642 /* Reserve enough space for the existing format string. */
643 avail -= strlen (fmt) + 1;
644 if (avail > 1000)
645 _exit (EXIT_FAILURE);
646
647 p = fmt;
648 while (1)
649 {
650 char *q;
651 size_t len, extra, trim;
652
653 p = strchr (p, '%');
654 if (p == NULL || p[1] == '\0')
655 {
656 if (new_fmt == buf)
657 {
658 len = strlen (fmt);
659 memcpy (bufp, fmt, len + 1);
660 }
661 break;
662 }
663
664 if (p[1] == 'A' || p[1] == 'B')
665 {
666 len = p - fmt;
667 memcpy (bufp, fmt, len);
668 bufp += len;
669 fmt = p + 2;
670 new_fmt = buf;
671
672 /* If we run out of space, tough, you lose your ridiculously
673 long file or section name. It's not safe to try to alloc
674 memory here; We might be printing an out of memory message. */
675 if (avail == 0)
676 {
677 *bufp++ = '*';
678 *bufp++ = '*';
679 *bufp = '\0';
680 }
681 else
682 {
683 if (p[1] == 'B')
684 {
685 bfd *abfd = va_arg (ap, bfd *);
686
687 if (abfd == NULL)
688 /* Invoking %B with a null bfd pointer is an internal error. */
689 abort ();
690 else if (abfd->my_archive)
691 snprintf (bufp, avail, "%s(%s)",
692 abfd->my_archive->filename, abfd->filename);
693 else
694 snprintf (bufp, avail, "%s", abfd->filename);
695 }
696 else
697 {
698 asection *sec = va_arg (ap, asection *);
699 bfd *abfd;
700 const char *group = NULL;
701 struct coff_comdat_info *ci;
702
703 if (sec == NULL)
704 /* Invoking %A with a null section pointer is an internal error. */
705 abort ();
706 abfd = sec->owner;
707 if (abfd != NULL
708 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
709 && elf_next_in_group (sec) != NULL
710 && (sec->flags & SEC_GROUP) == 0)
711 group = elf_group_name (sec);
712 else if (abfd != NULL
713 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
714 && (ci = bfd_coff_get_comdat_section (sec->owner,
715 sec)) != NULL)
716 group = ci->name;
717 if (group != NULL)
718 snprintf (bufp, avail, "%s[%s]", sec->name, group);
719 else
720 snprintf (bufp, avail, "%s", sec->name);
721 }
722 len = strlen (bufp);
723 avail = avail - len + 2;
724
725 /* We need to replace any '%' we printed by "%%".
726 First count how many. */
727 q = bufp;
728 bufp += len;
729 extra = 0;
730 while ((q = strchr (q, '%')) != NULL)
731 {
732 ++q;
733 ++extra;
734 }
735
736 /* If there isn't room, trim off the end of the string. */
737 q = bufp;
738 bufp += extra;
739 if (extra > avail)
740 {
741 trim = extra - avail;
742 bufp -= trim;
743 do
744 {
745 if (*--q == '%')
746 --extra;
747 }
748 while (--trim != 0);
749 *q = '\0';
750 avail = extra;
751 }
752 avail -= extra;
753
754 /* Now double all '%' chars, shuffling the string as we go. */
755 while (extra != 0)
756 {
757 while ((q[extra] = *q) != '%')
758 --q;
759 q[--extra] = '%';
760 --q;
761 }
762 }
763 }
764 p = p + 2;
765 }
766
767 vfprintf (stderr, new_fmt, ap);
768 va_end (ap);
769
770 /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
771 warning, so use the fputc function to avoid it. */
772 fputc ('\n', stderr);
773 fflush (stderr);
774 }
775
776 /* This is a function pointer to the routine which should handle BFD
777 error messages. It is called when a BFD routine encounters an
778 error for which it wants to print a message. Going through a
779 function pointer permits a program linked against BFD to intercept
780 the messages and deal with them itself. */
781
782 bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
783
784 /*
785 FUNCTION
786 bfd_set_error_handler
787
788 SYNOPSIS
789 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
790
791 DESCRIPTION
792 Set the BFD error handler function. Returns the previous
793 function.
794 */
795
796 bfd_error_handler_type
797 bfd_set_error_handler (bfd_error_handler_type pnew)
798 {
799 bfd_error_handler_type pold;
800
801 pold = _bfd_error_handler;
802 _bfd_error_handler = pnew;
803 return pold;
804 }
805
806 /*
807 FUNCTION
808 bfd_set_error_program_name
809
810 SYNOPSIS
811 void bfd_set_error_program_name (const char *);
812
813 DESCRIPTION
814 Set the program name to use when printing a BFD error. This
815 is printed before the error message followed by a colon and
816 space. The string must not be changed after it is passed to
817 this function.
818 */
819
820 void
821 bfd_set_error_program_name (const char *name)
822 {
823 _bfd_error_program_name = name;
824 }
825
826 /*
827 FUNCTION
828 bfd_get_error_handler
829
830 SYNOPSIS
831 bfd_error_handler_type bfd_get_error_handler (void);
832
833 DESCRIPTION
834 Return the BFD error handler function.
835 */
836
837 bfd_error_handler_type
838 bfd_get_error_handler (void)
839 {
840 return _bfd_error_handler;
841 }
842
843 /*
844 SUBSECTION
845 BFD assert handler
846
847 If BFD finds an internal inconsistency, the bfd assert
848 handler is called with information on the BFD version, BFD
849 source file and line. If this happens, most programs linked
850 against BFD are expected to want to exit with an error, or mark
851 the current BFD operation as failed, so it is recommended to
852 override the default handler, which just calls
853 _bfd_error_handler and continues.
854
855 CODE_FRAGMENT
856 .
857 .typedef void (*bfd_assert_handler_type) (const char *bfd_formatmsg,
858 . const char *bfd_version,
859 . const char *bfd_file,
860 . int bfd_line);
861 .
862 */
863
864 /* Note the use of bfd_ prefix on the parameter names above: we want to
865 show which one is the message and which is the version by naming the
866 parameters, but avoid polluting the program-using-bfd namespace as
867 the typedef is visible in the exported headers that the program
868 includes. Below, it's just for consistency. */
869
870 static void
871 _bfd_default_assert_handler (const char *bfd_formatmsg,
872 const char *bfd_version,
873 const char *bfd_file,
874 int bfd_line)
875
876 {
877 (*_bfd_error_handler) (bfd_formatmsg, bfd_version, bfd_file, bfd_line);
878 }
879
880 /* Similar to _bfd_error_handler, a program can decide to exit on an
881 internal BFD error. We use a non-variadic type to simplify passing
882 on parameters to other functions, e.g. _bfd_error_handler. */
883
884 bfd_assert_handler_type _bfd_assert_handler = _bfd_default_assert_handler;
885
886 /*
887 FUNCTION
888 bfd_set_assert_handler
889
890 SYNOPSIS
891 bfd_assert_handler_type bfd_set_assert_handler (bfd_assert_handler_type);
892
893 DESCRIPTION
894 Set the BFD assert handler function. Returns the previous
895 function.
896 */
897
898 bfd_assert_handler_type
899 bfd_set_assert_handler (bfd_assert_handler_type pnew)
900 {
901 bfd_assert_handler_type pold;
902
903 pold = _bfd_assert_handler;
904 _bfd_assert_handler = pnew;
905 return pold;
906 }
907
908 /*
909 FUNCTION
910 bfd_get_assert_handler
911
912 SYNOPSIS
913 bfd_assert_handler_type bfd_get_assert_handler (void);
914
915 DESCRIPTION
916 Return the BFD assert handler function.
917 */
918
919 bfd_assert_handler_type
920 bfd_get_assert_handler (void)
921 {
922 return _bfd_assert_handler;
923 }
924 \f
925 /*
926 INODE
927 Miscellaneous, Memory Usage, Error reporting, BFD front end
928
929 SECTION
930 Miscellaneous
931
932 SUBSECTION
933 Miscellaneous functions
934 */
935
936 /*
937 FUNCTION
938 bfd_get_reloc_upper_bound
939
940 SYNOPSIS
941 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
942
943 DESCRIPTION
944 Return the number of bytes required to store the
945 relocation information associated with section @var{sect}
946 attached to bfd @var{abfd}. If an error occurs, return -1.
947
948 */
949
950 long
951 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
952 {
953 if (abfd->format != bfd_object)
954 {
955 bfd_set_error (bfd_error_invalid_operation);
956 return -1;
957 }
958
959 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
960 }
961
962 /*
963 FUNCTION
964 bfd_canonicalize_reloc
965
966 SYNOPSIS
967 long bfd_canonicalize_reloc
968 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
969
970 DESCRIPTION
971 Call the back end associated with the open BFD
972 @var{abfd} and translate the external form of the relocation
973 information attached to @var{sec} into the internal canonical
974 form. Place the table into memory at @var{loc}, which has
975 been preallocated, usually by a call to
976 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
977 -1 on error.
978
979 The @var{syms} table is also needed for horrible internal magic
980 reasons.
981
982 */
983 long
984 bfd_canonicalize_reloc (bfd *abfd,
985 sec_ptr asect,
986 arelent **location,
987 asymbol **symbols)
988 {
989 if (abfd->format != bfd_object)
990 {
991 bfd_set_error (bfd_error_invalid_operation);
992 return -1;
993 }
994
995 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
996 (abfd, asect, location, symbols));
997 }
998
999 /*
1000 FUNCTION
1001 bfd_set_reloc
1002
1003 SYNOPSIS
1004 void bfd_set_reloc
1005 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
1006
1007 DESCRIPTION
1008 Set the relocation pointer and count within
1009 section @var{sec} to the values @var{rel} and @var{count}.
1010 The argument @var{abfd} is ignored.
1011
1012 */
1013
1014 void
1015 bfd_set_reloc (bfd *ignore_abfd ATTRIBUTE_UNUSED,
1016 sec_ptr asect,
1017 arelent **location,
1018 unsigned int count)
1019 {
1020 asect->orelocation = location;
1021 asect->reloc_count = count;
1022 }
1023
1024 /*
1025 FUNCTION
1026 bfd_set_file_flags
1027
1028 SYNOPSIS
1029 bfd_boolean bfd_set_file_flags (bfd *abfd, flagword flags);
1030
1031 DESCRIPTION
1032 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
1033
1034 Possible errors are:
1035 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
1036 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
1037 o <<bfd_error_invalid_operation>> -
1038 The flag word contained a bit which was not applicable to the
1039 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
1040 on a BFD format which does not support demand paging.
1041
1042 */
1043
1044 bfd_boolean
1045 bfd_set_file_flags (bfd *abfd, flagword flags)
1046 {
1047 if (abfd->format != bfd_object)
1048 {
1049 bfd_set_error (bfd_error_wrong_format);
1050 return FALSE;
1051 }
1052
1053 if (bfd_read_p (abfd))
1054 {
1055 bfd_set_error (bfd_error_invalid_operation);
1056 return FALSE;
1057 }
1058
1059 bfd_get_file_flags (abfd) = flags;
1060 if ((flags & bfd_applicable_file_flags (abfd)) != flags)
1061 {
1062 bfd_set_error (bfd_error_invalid_operation);
1063 return FALSE;
1064 }
1065
1066 return TRUE;
1067 }
1068
1069 void
1070 bfd_assert (const char *file, int line)
1071 {
1072 (*_bfd_assert_handler) (_("BFD %s assertion fail %s:%d"),
1073 BFD_VERSION_STRING, file, line);
1074 }
1075
1076 /* A more or less friendly abort message. In libbfd.h abort is
1077 defined to call this function. */
1078
1079 void
1080 _bfd_abort (const char *file, int line, const char *fn)
1081 {
1082 if (fn != NULL)
1083 (*_bfd_error_handler)
1084 (_("BFD %s internal error, aborting at %s line %d in %s\n"),
1085 BFD_VERSION_STRING, file, line, fn);
1086 else
1087 (*_bfd_error_handler)
1088 (_("BFD %s internal error, aborting at %s line %d\n"),
1089 BFD_VERSION_STRING, file, line);
1090 (*_bfd_error_handler) (_("Please report this bug.\n"));
1091 _exit (EXIT_FAILURE);
1092 }
1093
1094 /*
1095 FUNCTION
1096 bfd_get_arch_size
1097
1098 SYNOPSIS
1099 int bfd_get_arch_size (bfd *abfd);
1100
1101 DESCRIPTION
1102 Returns the normalized architecture address size, in bits, as
1103 determined by the object file's format. By normalized, we mean
1104 either 32 or 64. For ELF, this information is included in the
1105 header. Use bfd_arch_bits_per_address for number of bits in
1106 the architecture address.
1107
1108 RETURNS
1109 Returns the arch size in bits if known, <<-1>> otherwise.
1110 */
1111
1112 int
1113 bfd_get_arch_size (bfd *abfd)
1114 {
1115 if (abfd->xvec->flavour == bfd_target_elf_flavour)
1116 return get_elf_backend_data (abfd)->s->arch_size;
1117
1118 return bfd_arch_bits_per_address (abfd) > 32 ? 64 : 32;
1119 }
1120
1121 /*
1122 FUNCTION
1123 bfd_get_sign_extend_vma
1124
1125 SYNOPSIS
1126 int bfd_get_sign_extend_vma (bfd *abfd);
1127
1128 DESCRIPTION
1129 Indicates if the target architecture "naturally" sign extends
1130 an address. Some architectures implicitly sign extend address
1131 values when they are converted to types larger than the size
1132 of an address. For instance, bfd_get_start_address() will
1133 return an address sign extended to fill a bfd_vma when this is
1134 the case.
1135
1136 RETURNS
1137 Returns <<1>> if the target architecture is known to sign
1138 extend addresses, <<0>> if the target architecture is known to
1139 not sign extend addresses, and <<-1>> otherwise.
1140 */
1141
1142 int
1143 bfd_get_sign_extend_vma (bfd *abfd)
1144 {
1145 char *name;
1146
1147 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1148 return get_elf_backend_data (abfd)->sign_extend_vma;
1149
1150 name = bfd_get_target (abfd);
1151
1152 /* Return a proper value for DJGPP & PE COFF.
1153 This function is required for DWARF2 support, but there is
1154 no place to store this information in the COFF back end.
1155 Should enough other COFF targets add support for DWARF2,
1156 a place will have to be found. Until then, this hack will do. */
1157 if (CONST_STRNEQ (name, "coff-go32")
1158 || strcmp (name, "pe-i386") == 0
1159 || strcmp (name, "pei-i386") == 0
1160 || strcmp (name, "pe-x86-64") == 0
1161 || strcmp (name, "pei-x86-64") == 0
1162 || strcmp (name, "pe-arm-wince-little") == 0
1163 || strcmp (name, "pei-arm-wince-little") == 0
1164 || strcmp (name, "aixcoff-rs6000") == 0)
1165 return 1;
1166
1167 if (CONST_STRNEQ (name, "mach-o"))
1168 return 0;
1169
1170 bfd_set_error (bfd_error_wrong_format);
1171 return -1;
1172 }
1173
1174 /*
1175 FUNCTION
1176 bfd_set_start_address
1177
1178 SYNOPSIS
1179 bfd_boolean bfd_set_start_address (bfd *abfd, bfd_vma vma);
1180
1181 DESCRIPTION
1182 Make @var{vma} the entry point of output BFD @var{abfd}.
1183
1184 RETURNS
1185 Returns <<TRUE>> on success, <<FALSE>> otherwise.
1186 */
1187
1188 bfd_boolean
1189 bfd_set_start_address (bfd *abfd, bfd_vma vma)
1190 {
1191 abfd->start_address = vma;
1192 return TRUE;
1193 }
1194
1195 /*
1196 FUNCTION
1197 bfd_get_gp_size
1198
1199 SYNOPSIS
1200 unsigned int bfd_get_gp_size (bfd *abfd);
1201
1202 DESCRIPTION
1203 Return the maximum size of objects to be optimized using the GP
1204 register under MIPS ECOFF. This is typically set by the <<-G>>
1205 argument to the compiler, assembler or linker.
1206 */
1207
1208 unsigned int
1209 bfd_get_gp_size (bfd *abfd)
1210 {
1211 if (abfd->format == bfd_object)
1212 {
1213 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1214 return ecoff_data (abfd)->gp_size;
1215 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1216 return elf_gp_size (abfd);
1217 }
1218 return 0;
1219 }
1220
1221 /*
1222 FUNCTION
1223 bfd_set_gp_size
1224
1225 SYNOPSIS
1226 void bfd_set_gp_size (bfd *abfd, unsigned int i);
1227
1228 DESCRIPTION
1229 Set the maximum size of objects to be optimized using the GP
1230 register under ECOFF or MIPS ELF. This is typically set by
1231 the <<-G>> argument to the compiler, assembler or linker.
1232 */
1233
1234 void
1235 bfd_set_gp_size (bfd *abfd, unsigned int i)
1236 {
1237 /* Don't try to set GP size on an archive or core file! */
1238 if (abfd->format != bfd_object)
1239 return;
1240
1241 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1242 ecoff_data (abfd)->gp_size = i;
1243 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1244 elf_gp_size (abfd) = i;
1245 }
1246
1247 /* Get the GP value. This is an internal function used by some of the
1248 relocation special_function routines on targets which support a GP
1249 register. */
1250
1251 bfd_vma
1252 _bfd_get_gp_value (bfd *abfd)
1253 {
1254 if (! abfd)
1255 return 0;
1256 if (abfd->format != bfd_object)
1257 return 0;
1258
1259 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1260 return ecoff_data (abfd)->gp;
1261 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1262 return elf_gp (abfd);
1263
1264 return 0;
1265 }
1266
1267 /* Set the GP value. */
1268
1269 void
1270 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
1271 {
1272 if (! abfd)
1273 abort ();
1274 if (abfd->format != bfd_object)
1275 return;
1276
1277 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1278 ecoff_data (abfd)->gp = v;
1279 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1280 elf_gp (abfd) = v;
1281 }
1282
1283 /*
1284 FUNCTION
1285 bfd_scan_vma
1286
1287 SYNOPSIS
1288 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
1289
1290 DESCRIPTION
1291 Convert, like <<strtoul>>, a numerical expression
1292 @var{string} into a <<bfd_vma>> integer, and return that integer.
1293 (Though without as many bells and whistles as <<strtoul>>.)
1294 The expression is assumed to be unsigned (i.e., positive).
1295 If given a @var{base}, it is used as the base for conversion.
1296 A base of 0 causes the function to interpret the string
1297 in hex if a leading "0x" or "0X" is found, otherwise
1298 in octal if a leading zero is found, otherwise in decimal.
1299
1300 If the value would overflow, the maximum <<bfd_vma>> value is
1301 returned.
1302 */
1303
1304 bfd_vma
1305 bfd_scan_vma (const char *string, const char **end, int base)
1306 {
1307 bfd_vma value;
1308 bfd_vma cutoff;
1309 unsigned int cutlim;
1310 int overflow;
1311
1312 /* Let the host do it if possible. */
1313 if (sizeof (bfd_vma) <= sizeof (unsigned long))
1314 return strtoul (string, (char **) end, base);
1315
1316 #ifdef HAVE_STRTOULL
1317 if (sizeof (bfd_vma) <= sizeof (unsigned long long))
1318 return strtoull (string, (char **) end, base);
1319 #endif
1320
1321 if (base == 0)
1322 {
1323 if (string[0] == '0')
1324 {
1325 if ((string[1] == 'x') || (string[1] == 'X'))
1326 base = 16;
1327 else
1328 base = 8;
1329 }
1330 }
1331
1332 if ((base < 2) || (base > 36))
1333 base = 10;
1334
1335 if (base == 16
1336 && string[0] == '0'
1337 && (string[1] == 'x' || string[1] == 'X')
1338 && ISXDIGIT (string[2]))
1339 {
1340 string += 2;
1341 }
1342
1343 cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
1344 cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
1345 value = 0;
1346 overflow = 0;
1347 while (1)
1348 {
1349 unsigned int digit;
1350
1351 digit = *string;
1352 if (ISDIGIT (digit))
1353 digit = digit - '0';
1354 else if (ISALPHA (digit))
1355 digit = TOUPPER (digit) - 'A' + 10;
1356 else
1357 break;
1358 if (digit >= (unsigned int) base)
1359 break;
1360 if (value > cutoff || (value == cutoff && digit > cutlim))
1361 overflow = 1;
1362 value = value * base + digit;
1363 ++string;
1364 }
1365
1366 if (overflow)
1367 value = ~ (bfd_vma) 0;
1368
1369 if (end != NULL)
1370 *end = string;
1371
1372 return value;
1373 }
1374
1375 /*
1376 FUNCTION
1377 bfd_copy_private_header_data
1378
1379 SYNOPSIS
1380 bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
1381
1382 DESCRIPTION
1383 Copy private BFD header information from the BFD @var{ibfd} to the
1384 the BFD @var{obfd}. This copies information that may require
1385 sections to exist, but does not require symbol tables. Return
1386 <<true>> on success, <<false>> on error.
1387 Possible error returns are:
1388
1389 o <<bfd_error_no_memory>> -
1390 Not enough memory exists to create private data for @var{obfd}.
1391
1392 .#define bfd_copy_private_header_data(ibfd, obfd) \
1393 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
1394 . (ibfd, obfd))
1395
1396 */
1397
1398 /*
1399 FUNCTION
1400 bfd_copy_private_bfd_data
1401
1402 SYNOPSIS
1403 bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
1404
1405 DESCRIPTION
1406 Copy private BFD information from the BFD @var{ibfd} to the
1407 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
1408 Possible error returns are:
1409
1410 o <<bfd_error_no_memory>> -
1411 Not enough memory exists to create private data for @var{obfd}.
1412
1413 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
1414 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
1415 . (ibfd, obfd))
1416
1417 */
1418
1419 /*
1420 FUNCTION
1421 bfd_merge_private_bfd_data
1422
1423 SYNOPSIS
1424 bfd_boolean bfd_merge_private_bfd_data (bfd *ibfd, bfd *obfd);
1425
1426 DESCRIPTION
1427 Merge private BFD information from the BFD @var{ibfd} to the
1428 the output file BFD @var{obfd} when linking. Return <<TRUE>>
1429 on success, <<FALSE>> on error. Possible error returns are:
1430
1431 o <<bfd_error_no_memory>> -
1432 Not enough memory exists to create private data for @var{obfd}.
1433
1434 .#define bfd_merge_private_bfd_data(ibfd, obfd) \
1435 . BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
1436 . (ibfd, obfd))
1437
1438 */
1439
1440 /*
1441 FUNCTION
1442 bfd_set_private_flags
1443
1444 SYNOPSIS
1445 bfd_boolean bfd_set_private_flags (bfd *abfd, flagword flags);
1446
1447 DESCRIPTION
1448 Set private BFD flag information in the BFD @var{abfd}.
1449 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
1450 returns are:
1451
1452 o <<bfd_error_no_memory>> -
1453 Not enough memory exists to create private data for @var{obfd}.
1454
1455 .#define bfd_set_private_flags(abfd, flags) \
1456 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
1457
1458 */
1459
1460 /*
1461 FUNCTION
1462 Other functions
1463
1464 DESCRIPTION
1465 The following functions exist but have not yet been documented.
1466
1467 .#define bfd_sizeof_headers(abfd, info) \
1468 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
1469 .
1470 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
1471 . BFD_SEND (abfd, _bfd_find_nearest_line, \
1472 . (abfd, syms, sec, off, file, func, line, NULL))
1473 .
1474 .#define bfd_find_nearest_line_discriminator(abfd, sec, syms, off, file, func, \
1475 . line, disc) \
1476 . BFD_SEND (abfd, _bfd_find_nearest_line, \
1477 . (abfd, syms, sec, off, file, func, line, disc))
1478 .
1479 .#define bfd_find_line(abfd, syms, sym, file, line) \
1480 . BFD_SEND (abfd, _bfd_find_line, \
1481 . (abfd, syms, sym, file, line))
1482 .
1483 .#define bfd_find_inliner_info(abfd, file, func, line) \
1484 . BFD_SEND (abfd, _bfd_find_inliner_info, \
1485 . (abfd, file, func, line))
1486 .
1487 .#define bfd_debug_info_start(abfd) \
1488 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
1489 .
1490 .#define bfd_debug_info_end(abfd) \
1491 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
1492 .
1493 .#define bfd_debug_info_accumulate(abfd, section) \
1494 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
1495 .
1496 .#define bfd_stat_arch_elt(abfd, stat) \
1497 . BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
1498 .
1499 .#define bfd_update_armap_timestamp(abfd) \
1500 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
1501 .
1502 .#define bfd_set_arch_mach(abfd, arch, mach)\
1503 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
1504 .
1505 .#define bfd_relax_section(abfd, section, link_info, again) \
1506 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
1507 .
1508 .#define bfd_gc_sections(abfd, link_info) \
1509 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
1510 .
1511 .#define bfd_lookup_section_flags(link_info, flag_info, section) \
1512 . BFD_SEND (abfd, _bfd_lookup_section_flags, (link_info, flag_info, section))
1513 .
1514 .#define bfd_merge_sections(abfd, link_info) \
1515 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
1516 .
1517 .#define bfd_is_group_section(abfd, sec) \
1518 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
1519 .
1520 .#define bfd_discard_group(abfd, sec) \
1521 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
1522 .
1523 .#define bfd_link_hash_table_create(abfd) \
1524 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
1525 .
1526 .#define bfd_link_add_symbols(abfd, info) \
1527 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
1528 .
1529 .#define bfd_link_just_syms(abfd, sec, info) \
1530 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
1531 .
1532 .#define bfd_final_link(abfd, info) \
1533 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
1534 .
1535 .#define bfd_free_cached_info(abfd) \
1536 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
1537 .
1538 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
1539 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
1540 .
1541 .#define bfd_print_private_bfd_data(abfd, file)\
1542 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
1543 .
1544 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
1545 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
1546 .
1547 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
1548 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
1549 . dyncount, dynsyms, ret))
1550 .
1551 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
1552 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
1553 .
1554 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
1555 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
1556 .
1557 .extern bfd_byte *bfd_get_relocated_section_contents
1558 . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
1559 . bfd_boolean, asymbol **);
1560 .
1561
1562 */
1563
1564 bfd_byte *
1565 bfd_get_relocated_section_contents (bfd *abfd,
1566 struct bfd_link_info *link_info,
1567 struct bfd_link_order *link_order,
1568 bfd_byte *data,
1569 bfd_boolean relocatable,
1570 asymbol **symbols)
1571 {
1572 bfd *abfd2;
1573 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
1574 bfd_byte *, bfd_boolean, asymbol **);
1575
1576 if (link_order->type == bfd_indirect_link_order)
1577 {
1578 abfd2 = link_order->u.indirect.section->owner;
1579 if (abfd2 == NULL)
1580 abfd2 = abfd;
1581 }
1582 else
1583 abfd2 = abfd;
1584
1585 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
1586
1587 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
1588 }
1589
1590 /* Record information about an ELF program header. */
1591
1592 bfd_boolean
1593 bfd_record_phdr (bfd *abfd,
1594 unsigned long type,
1595 bfd_boolean flags_valid,
1596 flagword flags,
1597 bfd_boolean at_valid,
1598 bfd_vma at,
1599 bfd_boolean includes_filehdr,
1600 bfd_boolean includes_phdrs,
1601 unsigned int count,
1602 asection **secs)
1603 {
1604 struct elf_segment_map *m, **pm;
1605 bfd_size_type amt;
1606
1607 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1608 return TRUE;
1609
1610 amt = sizeof (struct elf_segment_map);
1611 amt += ((bfd_size_type) count - 1) * sizeof (asection *);
1612 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
1613 if (m == NULL)
1614 return FALSE;
1615
1616 m->p_type = type;
1617 m->p_flags = flags;
1618 m->p_paddr = at;
1619 m->p_flags_valid = flags_valid;
1620 m->p_paddr_valid = at_valid;
1621 m->includes_filehdr = includes_filehdr;
1622 m->includes_phdrs = includes_phdrs;
1623 m->count = count;
1624 if (count > 0)
1625 memcpy (m->sections, secs, count * sizeof (asection *));
1626
1627 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
1628 ;
1629 *pm = m;
1630
1631 return TRUE;
1632 }
1633
1634 #ifdef BFD64
1635 /* Return true iff this target is 32-bit. */
1636
1637 static bfd_boolean
1638 is32bit (bfd *abfd)
1639 {
1640 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1641 {
1642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1643 return bed->s->elfclass == ELFCLASS32;
1644 }
1645
1646 /* For non-ELF targets, use architecture information. */
1647 return bfd_arch_bits_per_address (abfd) <= 32;
1648 }
1649 #endif
1650
1651 /* bfd_sprintf_vma and bfd_fprintf_vma display an address in the
1652 target's address size. */
1653
1654 void
1655 bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
1656 {
1657 #ifdef BFD64
1658 if (is32bit (abfd))
1659 {
1660 sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
1661 return;
1662 }
1663 #endif
1664 sprintf_vma (buf, value);
1665 }
1666
1667 void
1668 bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
1669 {
1670 #ifdef BFD64
1671 if (is32bit (abfd))
1672 {
1673 fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
1674 return;
1675 }
1676 #endif
1677 fprintf_vma ((FILE *) stream, value);
1678 }
1679
1680 /*
1681 FUNCTION
1682 bfd_alt_mach_code
1683
1684 SYNOPSIS
1685 bfd_boolean bfd_alt_mach_code (bfd *abfd, int alternative);
1686
1687 DESCRIPTION
1688
1689 When more than one machine code number is available for the
1690 same machine type, this function can be used to switch between
1691 the preferred one (alternative == 0) and any others. Currently,
1692 only ELF supports this feature, with up to two alternate
1693 machine codes.
1694 */
1695
1696 bfd_boolean
1697 bfd_alt_mach_code (bfd *abfd, int alternative)
1698 {
1699 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1700 {
1701 int code;
1702
1703 switch (alternative)
1704 {
1705 case 0:
1706 code = get_elf_backend_data (abfd)->elf_machine_code;
1707 break;
1708
1709 case 1:
1710 code = get_elf_backend_data (abfd)->elf_machine_alt1;
1711 if (code == 0)
1712 return FALSE;
1713 break;
1714
1715 case 2:
1716 code = get_elf_backend_data (abfd)->elf_machine_alt2;
1717 if (code == 0)
1718 return FALSE;
1719 break;
1720
1721 default:
1722 return FALSE;
1723 }
1724
1725 elf_elfheader (abfd)->e_machine = code;
1726
1727 return TRUE;
1728 }
1729
1730 return FALSE;
1731 }
1732
1733 /*
1734 FUNCTION
1735 bfd_emul_get_maxpagesize
1736
1737 SYNOPSIS
1738 bfd_vma bfd_emul_get_maxpagesize (const char *);
1739
1740 DESCRIPTION
1741 Returns the maximum page size, in bytes, as determined by
1742 emulation.
1743
1744 RETURNS
1745 Returns the maximum page size in bytes for ELF, 0 otherwise.
1746 */
1747
1748 bfd_vma
1749 bfd_emul_get_maxpagesize (const char *emul)
1750 {
1751 const bfd_target *target;
1752
1753 target = bfd_find_target (emul, NULL);
1754 if (target != NULL
1755 && target->flavour == bfd_target_elf_flavour)
1756 return xvec_get_elf_backend_data (target)->maxpagesize;
1757
1758 return 0;
1759 }
1760
1761 static void
1762 bfd_elf_set_pagesize (const bfd_target *target, bfd_vma size,
1763 int offset, const bfd_target *orig_target)
1764 {
1765 if (target->flavour == bfd_target_elf_flavour)
1766 {
1767 const struct elf_backend_data *bed;
1768
1769 bed = xvec_get_elf_backend_data (target);
1770 *((bfd_vma *) ((char *) bed + offset)) = size;
1771 }
1772
1773 if (target->alternative_target
1774 && target->alternative_target != orig_target)
1775 bfd_elf_set_pagesize (target->alternative_target, size, offset,
1776 orig_target);
1777 }
1778
1779 /*
1780 FUNCTION
1781 bfd_emul_set_maxpagesize
1782
1783 SYNOPSIS
1784 void bfd_emul_set_maxpagesize (const char *, bfd_vma);
1785
1786 DESCRIPTION
1787 For ELF, set the maximum page size for the emulation. It is
1788 a no-op for other formats.
1789
1790 */
1791
1792 void
1793 bfd_emul_set_maxpagesize (const char *emul, bfd_vma size)
1794 {
1795 const bfd_target *target;
1796
1797 target = bfd_find_target (emul, NULL);
1798 if (target)
1799 bfd_elf_set_pagesize (target, size,
1800 offsetof (struct elf_backend_data,
1801 maxpagesize), target);
1802 }
1803
1804 /*
1805 FUNCTION
1806 bfd_emul_get_commonpagesize
1807
1808 SYNOPSIS
1809 bfd_vma bfd_emul_get_commonpagesize (const char *);
1810
1811 DESCRIPTION
1812 Returns the common page size, in bytes, as determined by
1813 emulation.
1814
1815 RETURNS
1816 Returns the common page size in bytes for ELF, 0 otherwise.
1817 */
1818
1819 bfd_vma
1820 bfd_emul_get_commonpagesize (const char *emul)
1821 {
1822 const bfd_target *target;
1823
1824 target = bfd_find_target (emul, NULL);
1825 if (target != NULL
1826 && target->flavour == bfd_target_elf_flavour)
1827 return xvec_get_elf_backend_data (target)->commonpagesize;
1828
1829 return 0;
1830 }
1831
1832 /*
1833 FUNCTION
1834 bfd_emul_set_commonpagesize
1835
1836 SYNOPSIS
1837 void bfd_emul_set_commonpagesize (const char *, bfd_vma);
1838
1839 DESCRIPTION
1840 For ELF, set the common page size for the emulation. It is
1841 a no-op for other formats.
1842
1843 */
1844
1845 void
1846 bfd_emul_set_commonpagesize (const char *emul, bfd_vma size)
1847 {
1848 const bfd_target *target;
1849
1850 target = bfd_find_target (emul, NULL);
1851 if (target)
1852 bfd_elf_set_pagesize (target, size,
1853 offsetof (struct elf_backend_data,
1854 commonpagesize), target);
1855 }
1856
1857 /*
1858 FUNCTION
1859 bfd_demangle
1860
1861 SYNOPSIS
1862 char *bfd_demangle (bfd *, const char *, int);
1863
1864 DESCRIPTION
1865 Wrapper around cplus_demangle. Strips leading underscores and
1866 other such chars that would otherwise confuse the demangler.
1867 If passed a g++ v3 ABI mangled name, returns a buffer allocated
1868 with malloc holding the demangled name. Returns NULL otherwise
1869 and on memory alloc failure.
1870 */
1871
1872 char *
1873 bfd_demangle (bfd *abfd, const char *name, int options)
1874 {
1875 char *res, *alloc;
1876 const char *pre, *suf;
1877 size_t pre_len;
1878 bfd_boolean skip_lead;
1879
1880 skip_lead = (abfd != NULL
1881 && *name != '\0'
1882 && bfd_get_symbol_leading_char (abfd) == *name);
1883 if (skip_lead)
1884 ++name;
1885
1886 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
1887 or the MS PE format. These formats have a number of leading '.'s
1888 on at least some symbols, so we remove all dots to avoid
1889 confusing the demangler. */
1890 pre = name;
1891 while (*name == '.' || *name == '$')
1892 ++name;
1893 pre_len = name - pre;
1894
1895 /* Strip off @plt and suchlike too. */
1896 alloc = NULL;
1897 suf = strchr (name, '@');
1898 if (suf != NULL)
1899 {
1900 alloc = (char *) bfd_malloc (suf - name + 1);
1901 if (alloc == NULL)
1902 return NULL;
1903 memcpy (alloc, name, suf - name);
1904 alloc[suf - name] = '\0';
1905 name = alloc;
1906 }
1907
1908 res = cplus_demangle (name, options);
1909
1910 if (alloc != NULL)
1911 free (alloc);
1912
1913 if (res == NULL)
1914 {
1915 if (skip_lead)
1916 {
1917 size_t len = strlen (pre) + 1;
1918 alloc = (char *) bfd_malloc (len);
1919 if (alloc == NULL)
1920 return NULL;
1921 memcpy (alloc, pre, len);
1922 return alloc;
1923 }
1924 return NULL;
1925 }
1926
1927 /* Put back any prefix or suffix. */
1928 if (pre_len != 0 || suf != NULL)
1929 {
1930 size_t len;
1931 size_t suf_len;
1932 char *final;
1933
1934 len = strlen (res);
1935 if (suf == NULL)
1936 suf = res + len;
1937 suf_len = strlen (suf) + 1;
1938 final = (char *) bfd_malloc (pre_len + len + suf_len);
1939 if (final != NULL)
1940 {
1941 memcpy (final, pre, pre_len);
1942 memcpy (final + pre_len, res, len);
1943 memcpy (final + pre_len + len, suf, suf_len);
1944 }
1945 free (res);
1946 res = final;
1947 }
1948
1949 return res;
1950 }
1951
1952 /*
1953 FUNCTION
1954 bfd_update_compression_header
1955
1956 SYNOPSIS
1957 void bfd_update_compression_header
1958 (bfd *abfd, bfd_byte *contents, asection *sec);
1959
1960 DESCRIPTION
1961 Set the compression header at CONTENTS of SEC in ABFD and update
1962 elf_section_flags for compression.
1963 */
1964
1965 void
1966 bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
1967 asection *sec)
1968 {
1969 if ((abfd->flags & BFD_COMPRESS) != 0)
1970 {
1971 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1972 {
1973 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1974 {
1975 const struct elf_backend_data *bed
1976 = get_elf_backend_data (abfd);
1977
1978 /* Set the SHF_COMPRESSED bit. */
1979 elf_section_flags (sec) |= SHF_COMPRESSED;
1980
1981 if (bed->s->elfclass == ELFCLASS32)
1982 {
1983 Elf32_External_Chdr *echdr
1984 = (Elf32_External_Chdr *) contents;
1985 bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
1986 bfd_put_32 (abfd, sec->size, &echdr->ch_size);
1987 bfd_put_32 (abfd, 1 << sec->alignment_power,
1988 &echdr->ch_addralign);
1989 }
1990 else
1991 {
1992 Elf64_External_Chdr *echdr
1993 = (Elf64_External_Chdr *) contents;
1994 bfd_put_64 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
1995 bfd_put_64 (abfd, sec->size, &echdr->ch_size);
1996 bfd_put_64 (abfd, 1 << sec->alignment_power,
1997 &echdr->ch_addralign);
1998 }
1999 }
2000 else
2001 {
2002 /* Clear the SHF_COMPRESSED bit. */
2003 elf_section_flags (sec) &= ~SHF_COMPRESSED;
2004
2005 /* Write the zlib header. It should be "ZLIB" followed by
2006 the uncompressed section size, 8 bytes in big-endian
2007 order. */
2008 memcpy (contents, "ZLIB", 4);
2009 bfd_putb64 (sec->size, contents + 4);
2010 }
2011 }
2012 }
2013 else
2014 abort ();
2015 }
2016
2017 /*
2018 FUNCTION
2019 bfd_check_compression_header
2020
2021 SYNOPSIS
2022 bfd_boolean bfd_check_compression_header
2023 (bfd *abfd, bfd_byte *contents, asection *sec,
2024 bfd_size_type *uncompressed_size);
2025
2026 DESCRIPTION
2027 Check the compression header at CONTENTS of SEC in ABFD and
2028 store the uncompressed size in UNCOMPRESSED_SIZE if the
2029 compression header is valid.
2030
2031 RETURNS
2032 Return TRUE if the compression header is valid.
2033 */
2034
2035 bfd_boolean
2036 bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
2037 asection *sec,
2038 bfd_size_type *uncompressed_size)
2039 {
2040 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2041 && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
2042 {
2043 Elf_Internal_Chdr chdr;
2044 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2045 if (bed->s->elfclass == ELFCLASS32)
2046 {
2047 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
2048 chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
2049 chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
2050 chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
2051 }
2052 else
2053 {
2054 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
2055 chdr.ch_type = bfd_get_64 (abfd, &echdr->ch_type);
2056 chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
2057 chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
2058 }
2059 if (chdr.ch_type == ELFCOMPRESS_ZLIB
2060 && chdr.ch_addralign == 1U << sec->alignment_power)
2061 {
2062 *uncompressed_size = chdr.ch_size;
2063 return TRUE;
2064 }
2065 }
2066
2067 return FALSE;
2068 }
2069
2070 /*
2071 FUNCTION
2072 bfd_get_compression_header_size
2073
2074 SYNOPSIS
2075 int bfd_get_compression_header_size (bfd *abfd, asection *sec);
2076
2077 DESCRIPTION
2078 Return the size of the compression header of SEC in ABFD.
2079
2080 RETURNS
2081 Return the size of the compression header in bytes.
2082 */
2083
2084 int
2085 bfd_get_compression_header_size (bfd *abfd, asection *sec)
2086 {
2087 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2088 {
2089 if (sec == NULL)
2090 {
2091 if (!(abfd->flags & BFD_COMPRESS_GABI))
2092 return 0;
2093 }
2094 else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
2095 return 0;
2096
2097 if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
2098 return sizeof (Elf32_External_Chdr);
2099 else
2100 return sizeof (Elf64_External_Chdr);
2101 }
2102
2103 return 0;
2104 }
This page took 0.07748 seconds and 4 git commands to generate.