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