objcopy.c revision 1.8
1/* objcopy.c -- copy object file from input to output, optionally massaging it. 2 Copyright (C) 1991-2015 Free Software Foundation, Inc. 3 4 This file is part of GNU Binutils. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 19 02110-1301, USA. */ 20 21#include "sysdep.h" 22#include "bfd.h" 23#include "progress.h" 24#include "getopt.h" 25#include "libiberty.h" 26#include "bucomm.h" 27#include "budbg.h" 28#include "filenames.h" 29#include "fnmatch.h" 30#include "elf-bfd.h" 31#include <sys/stat.h> 32#include <ctype.h> 33#include "libbfd.h" 34#include "coff/internal.h" 35#include "libcoff.h" 36 37/* FIXME: See bfd/peXXigen.c for why we include an architecture specific 38 header in generic PE code. */ 39#include "coff/i386.h" 40#include "coff/pe.h" 41 42static bfd_vma pe_file_alignment = (bfd_vma) -1; 43static bfd_vma pe_heap_commit = (bfd_vma) -1; 44static bfd_vma pe_heap_reserve = (bfd_vma) -1; 45static bfd_vma pe_image_base = (bfd_vma) -1; 46static bfd_vma pe_section_alignment = (bfd_vma) -1; 47static bfd_vma pe_stack_commit = (bfd_vma) -1; 48static bfd_vma pe_stack_reserve = (bfd_vma) -1; 49static short pe_subsystem = -1; 50static short pe_major_subsystem_version = -1; 51static short pe_minor_subsystem_version = -1; 52 53struct is_specified_symbol_predicate_data 54{ 55 const char * name; 56 bfd_boolean found; 57}; 58 59/* A list to support redefine_sym. */ 60struct redefine_node 61{ 62 char *source; 63 char *target; 64 struct redefine_node *next; 65}; 66 67struct addsym_node 68{ 69 struct addsym_node *next; 70 char * symdef; 71 long symval; 72 flagword flags; 73 char * section; 74 char * othersym; 75}; 76 77typedef struct section_rename 78{ 79 const char * old_name; 80 const char * new_name; 81 flagword flags; 82 struct section_rename * next; 83} 84section_rename; 85 86/* List of sections to be renamed. */ 87static section_rename *section_rename_list; 88 89static asymbol **isympp = NULL; /* Input symbols. */ 90static asymbol **osympp = NULL; /* Output symbols that survive stripping. */ 91 92/* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */ 93static int copy_byte = -1; 94static int interleave = 0; /* Initialised to 4 in copy_main(). */ 95static int copy_width = 1; 96 97static bfd_boolean verbose; /* Print file and target names. */ 98static bfd_boolean preserve_dates; /* Preserve input file timestamp. */ 99static int deterministic = -1; /* Enable deterministic archives. */ 100static int status = 0; /* Exit status. */ 101 102enum strip_action 103{ 104 STRIP_UNDEF, 105 STRIP_NONE, /* Don't strip. */ 106 STRIP_DEBUG, /* Strip all debugger symbols. */ 107 STRIP_UNNEEDED, /* Strip unnecessary symbols. */ 108 STRIP_NONDEBUG, /* Strip everything but debug info. */ 109 STRIP_DWO, /* Strip all DWO info. */ 110 STRIP_NONDWO, /* Strip everything but DWO info. */ 111 STRIP_ALL /* Strip all symbols. */ 112}; 113 114/* Which symbols to remove. */ 115static enum strip_action strip_symbols = STRIP_UNDEF; 116 117enum locals_action 118{ 119 LOCALS_UNDEF, 120 LOCALS_START_L, /* Discard locals starting with L. */ 121 LOCALS_ALL /* Discard all locals. */ 122}; 123 124/* Which local symbols to remove. Overrides STRIP_ALL. */ 125static enum locals_action discard_locals; 126 127/* Structure used to hold lists of sections and actions to take. */ 128struct section_list 129{ 130 struct section_list * next; /* Next section to change. */ 131 const char * pattern; /* Section name pattern. */ 132 bfd_boolean used; /* Whether this entry was used. */ 133 134 unsigned int context; /* What to do with matching sections. */ 135 /* Flag bits used in the context field. 136 COPY and REMOVE are mutually exlusive. SET and ALTER are mutually exclusive. */ 137#define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */ 138#define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */ 139#define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */ 140#define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */ 141#define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */ 142#define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */ 143#define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */ 144 145 bfd_vma vma_val; /* Amount to change by or set to. */ 146 bfd_vma lma_val; /* Amount to change by or set to. */ 147 flagword flags; /* What to set the section flags to. */ 148}; 149 150static struct section_list *change_sections; 151 152/* TRUE if some sections are to be removed. */ 153static bfd_boolean sections_removed; 154 155/* TRUE if only some sections are to be copied. */ 156static bfd_boolean sections_copied; 157 158/* Changes to the start address. */ 159static bfd_vma change_start = 0; 160static bfd_boolean set_start_set = FALSE; 161static bfd_vma set_start; 162 163/* Changes to section addresses. */ 164static bfd_vma change_section_address = 0; 165 166/* Filling gaps between sections. */ 167static bfd_boolean gap_fill_set = FALSE; 168static bfd_byte gap_fill = 0; 169 170/* Pad to a given address. */ 171static bfd_boolean pad_to_set = FALSE; 172static bfd_vma pad_to; 173 174/* Use alternative machine code? */ 175static unsigned long use_alt_mach_code = 0; 176 177/* Output BFD flags user wants to set or clear */ 178static flagword bfd_flags_to_set; 179static flagword bfd_flags_to_clear; 180 181/* List of sections to add. */ 182struct section_add 183{ 184 /* Next section to add. */ 185 struct section_add *next; 186 /* Name of section to add. */ 187 const char *name; 188 /* Name of file holding section contents. */ 189 const char *filename; 190 /* Size of file. */ 191 size_t size; 192 /* Contents of file. */ 193 bfd_byte *contents; 194 /* BFD section, after it has been added. */ 195 asection *section; 196}; 197 198/* List of sections to add to the output BFD. */ 199static struct section_add *add_sections; 200 201/* List of sections to update in the output BFD. */ 202static struct section_add *update_sections; 203 204/* List of sections to dump from the output BFD. */ 205static struct section_add *dump_sections; 206 207/* If non-NULL the argument to --add-gnu-debuglink. 208 This should be the filename to store in the .gnu_debuglink section. */ 209static const char * gnu_debuglink_filename = NULL; 210 211/* Whether to convert debugging information. */ 212static bfd_boolean convert_debugging = FALSE; 213 214/* Whether to compress/decompress DWARF debug sections. */ 215static enum 216{ 217 nothing = 0, 218 compress = 1 << 0, 219 compress_zlib = compress | 1 << 1, 220 compress_gnu_zlib = compress | 1 << 2, 221 compress_gabi_zlib = compress | 1 << 3, 222 decompress = 1 << 4 223} do_debug_sections = nothing; 224 225/* Whether to change the leading character in symbol names. */ 226static bfd_boolean change_leading_char = FALSE; 227 228/* Whether to remove the leading character from global symbol names. */ 229static bfd_boolean remove_leading_char = FALSE; 230 231/* Whether to permit wildcard in symbol comparison. */ 232static bfd_boolean wildcard = FALSE; 233 234/* True if --localize-hidden is in effect. */ 235static bfd_boolean localize_hidden = FALSE; 236 237/* List of symbols to strip, keep, localize, keep-global, weaken, 238 or redefine. */ 239static htab_t strip_specific_htab = NULL; 240static htab_t strip_unneeded_htab = NULL; 241static htab_t keep_specific_htab = NULL; 242static htab_t localize_specific_htab = NULL; 243static htab_t globalize_specific_htab = NULL; 244static htab_t keepglobal_specific_htab = NULL; 245static htab_t weaken_specific_htab = NULL; 246static struct redefine_node *redefine_sym_list = NULL; 247static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list; 248static int add_symbols = 0; 249 250/* If this is TRUE, we weaken global symbols (set BSF_WEAK). */ 251static bfd_boolean weaken = FALSE; 252 253/* If this is TRUE, we retain BSF_FILE symbols. */ 254static bfd_boolean keep_file_symbols = FALSE; 255 256/* Prefix symbols/sections. */ 257static char *prefix_symbols_string = 0; 258static char *prefix_sections_string = 0; 259static char *prefix_alloc_sections_string = 0; 260 261/* True if --extract-symbol was passed on the command line. */ 262static bfd_boolean extract_symbol = FALSE; 263 264/* If `reverse_bytes' is nonzero, then reverse the order of every chunk 265 of <reverse_bytes> bytes within each output section. */ 266static int reverse_bytes = 0; 267 268/* For Coff objects, we may want to allow or disallow long section names, 269 or preserve them where found in the inputs. Debug info relies on them. */ 270enum long_section_name_handling 271{ 272 DISABLE, 273 ENABLE, 274 KEEP 275}; 276 277/* The default long section handling mode is to preserve them. 278 This is also the only behaviour for 'strip'. */ 279static enum long_section_name_handling long_section_names = KEEP; 280 281/* 150 isn't special; it's just an arbitrary non-ASCII char value. */ 282enum command_line_switch 283{ 284 OPTION_ADD_SECTION=150, 285 OPTION_ADD_GNU_DEBUGLINK, 286 OPTION_ADD_SYMBOL, 287 OPTION_ALT_MACH_CODE, 288 OPTION_CHANGE_ADDRESSES, 289 OPTION_CHANGE_LEADING_CHAR, 290 OPTION_CHANGE_SECTION_ADDRESS, 291 OPTION_CHANGE_SECTION_LMA, 292 OPTION_CHANGE_SECTION_VMA, 293 OPTION_CHANGE_START, 294 OPTION_CHANGE_WARNINGS, 295 OPTION_COMPRESS_DEBUG_SECTIONS, 296 OPTION_DEBUGGING, 297 OPTION_DECOMPRESS_DEBUG_SECTIONS, 298 OPTION_DUMP_SECTION, 299 OPTION_EXTRACT_DWO, 300 OPTION_EXTRACT_SYMBOL, 301 OPTION_FILE_ALIGNMENT, 302 OPTION_FORMATS_INFO, 303 OPTION_GAP_FILL, 304 OPTION_GLOBALIZE_SYMBOL, 305 OPTION_GLOBALIZE_SYMBOLS, 306 OPTION_HEAP, 307 OPTION_IMAGE_BASE, 308 OPTION_IMPURE, 309 OPTION_INTERLEAVE_WIDTH, 310 OPTION_KEEPGLOBAL_SYMBOLS, 311 OPTION_KEEP_FILE_SYMBOLS, 312 OPTION_KEEP_SYMBOLS, 313 OPTION_LOCALIZE_HIDDEN, 314 OPTION_LOCALIZE_SYMBOLS, 315 OPTION_LONG_SECTION_NAMES, 316 OPTION_NO_CHANGE_WARNINGS, 317 OPTION_ONLY_KEEP_DEBUG, 318 OPTION_PAD_TO, 319 OPTION_PREFIX_ALLOC_SECTIONS, 320 OPTION_PREFIX_SECTIONS, 321 OPTION_PREFIX_SYMBOLS, 322 OPTION_PURE, 323 OPTION_READONLY_TEXT, 324 OPTION_REDEFINE_SYM, 325 OPTION_REDEFINE_SYMS, 326 OPTION_REMOVE_LEADING_CHAR, 327 OPTION_RENAME_SECTION, 328 OPTION_REVERSE_BYTES, 329 OPTION_SECTION_ALIGNMENT, 330 OPTION_SET_SECTION_FLAGS, 331 OPTION_SET_START, 332 OPTION_SREC_FORCES3, 333 OPTION_SREC_LEN, 334 OPTION_STACK, 335 OPTION_STRIP_DWO, 336 OPTION_STRIP_SYMBOLS, 337 OPTION_STRIP_UNNEEDED, 338 OPTION_STRIP_UNNEEDED_SYMBOL, 339 OPTION_STRIP_UNNEEDED_SYMBOLS, 340 OPTION_SUBSYSTEM, 341 OPTION_UPDATE_SECTION, 342 OPTION_WEAKEN, 343 OPTION_WEAKEN_SYMBOLS, 344 OPTION_WRITABLE_TEXT 345}; 346 347/* Options to handle if running as "strip". */ 348 349static struct option strip_options[] = 350{ 351 {"disable-deterministic-archives", no_argument, 0, 'U'}, 352 {"discard-all", no_argument, 0, 'x'}, 353 {"discard-locals", no_argument, 0, 'X'}, 354 {"enable-deterministic-archives", no_argument, 0, 'D'}, 355 {"format", required_argument, 0, 'F'}, /* Obsolete */ 356 {"help", no_argument, 0, 'h'}, 357 {"info", no_argument, 0, OPTION_FORMATS_INFO}, 358 {"input-format", required_argument, 0, 'I'}, /* Obsolete */ 359 {"input-target", required_argument, 0, 'I'}, 360 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS}, 361 {"keep-symbol", required_argument, 0, 'K'}, 362 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG}, 363 {"output-file", required_argument, 0, 'o'}, 364 {"output-format", required_argument, 0, 'O'}, /* Obsolete */ 365 {"output-target", required_argument, 0, 'O'}, 366 {"preserve-dates", no_argument, 0, 'p'}, 367 {"remove-section", required_argument, 0, 'R'}, 368 {"strip-all", no_argument, 0, 's'}, 369 {"strip-debug", no_argument, 0, 'S'}, 370 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO}, 371 {"strip-symbol", required_argument, 0, 'N'}, 372 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED}, 373 {"target", required_argument, 0, 'F'}, 374 {"verbose", no_argument, 0, 'v'}, 375 {"version", no_argument, 0, 'V'}, 376 {"wildcard", no_argument, 0, 'w'}, 377 {0, no_argument, 0, 0} 378}; 379 380/* Options to handle if running as "objcopy". */ 381 382static struct option copy_options[] = 383{ 384 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK}, 385 {"add-section", required_argument, 0, OPTION_ADD_SECTION}, 386 {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL}, 387 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS}, 388 {"adjust-start", required_argument, 0, OPTION_CHANGE_START}, 389 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES}, 390 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS}, 391 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE}, 392 {"binary-architecture", required_argument, 0, 'B'}, 393 {"byte", required_argument, 0, 'b'}, 394 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES}, 395 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR}, 396 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS}, 397 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA}, 398 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA}, 399 {"change-start", required_argument, 0, OPTION_CHANGE_START}, 400 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS}, 401 {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS}, 402 {"debugging", no_argument, 0, OPTION_DEBUGGING}, 403 {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS}, 404 {"disable-deterministic-archives", no_argument, 0, 'U'}, 405 {"discard-all", no_argument, 0, 'x'}, 406 {"discard-locals", no_argument, 0, 'X'}, 407 {"dump-section", required_argument, 0, OPTION_DUMP_SECTION}, 408 {"enable-deterministic-archives", no_argument, 0, 'D'}, 409 {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO}, 410 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL}, 411 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT}, 412 {"format", required_argument, 0, 'F'}, /* Obsolete */ 413 {"gap-fill", required_argument, 0, OPTION_GAP_FILL}, 414 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL}, 415 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS}, 416 {"heap", required_argument, 0, OPTION_HEAP}, 417 {"help", no_argument, 0, 'h'}, 418 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE}, 419 {"impure", no_argument, 0, OPTION_IMPURE}, 420 {"info", no_argument, 0, OPTION_FORMATS_INFO}, 421 {"input-format", required_argument, 0, 'I'}, /* Obsolete */ 422 {"input-target", required_argument, 0, 'I'}, 423 {"interleave", optional_argument, 0, 'i'}, 424 {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH}, 425 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS}, 426 {"keep-global-symbol", required_argument, 0, 'G'}, 427 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS}, 428 {"keep-symbol", required_argument, 0, 'K'}, 429 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS}, 430 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN}, 431 {"localize-symbol", required_argument, 0, 'L'}, 432 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS}, 433 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES}, 434 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS}, 435 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS}, 436 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG}, 437 {"only-section", required_argument, 0, 'j'}, 438 {"output-format", required_argument, 0, 'O'}, /* Obsolete */ 439 {"output-target", required_argument, 0, 'O'}, 440 {"pad-to", required_argument, 0, OPTION_PAD_TO}, 441 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS}, 442 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS}, 443 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS}, 444 {"preserve-dates", no_argument, 0, 'p'}, 445 {"pure", no_argument, 0, OPTION_PURE}, 446 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT}, 447 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM}, 448 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS}, 449 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR}, 450 {"remove-section", required_argument, 0, 'R'}, 451 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION}, 452 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES}, 453 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT}, 454 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS}, 455 {"set-start", required_argument, 0, OPTION_SET_START}, 456 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3}, 457 {"srec-len", required_argument, 0, OPTION_SREC_LEN}, 458 {"stack", required_argument, 0, OPTION_STACK}, 459 {"strip-all", no_argument, 0, 'S'}, 460 {"strip-debug", no_argument, 0, 'g'}, 461 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO}, 462 {"strip-symbol", required_argument, 0, 'N'}, 463 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS}, 464 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED}, 465 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL}, 466 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS}, 467 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM}, 468 {"target", required_argument, 0, 'F'}, 469 {"update-section", required_argument, 0, OPTION_UPDATE_SECTION}, 470 {"verbose", no_argument, 0, 'v'}, 471 {"version", no_argument, 0, 'V'}, 472 {"weaken", no_argument, 0, OPTION_WEAKEN}, 473 {"weaken-symbol", required_argument, 0, 'W'}, 474 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS}, 475 {"wildcard", no_argument, 0, 'w'}, 476 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT}, 477 {0, no_argument, 0, 0} 478}; 479 480/* IMPORTS */ 481extern char *program_name; 482 483/* This flag distinguishes between strip and objcopy: 484 1 means this is 'strip'; 0 means this is 'objcopy'. 485 -1 means if we should use argv[0] to decide. */ 486extern int is_strip; 487 488/* The maximum length of an S record. This variable is declared in srec.c 489 and can be modified by the --srec-len parameter. */ 490extern unsigned int Chunk; 491 492/* Restrict the generation of Srecords to type S3 only. 493 This variable is declare in bfd/srec.c and can be toggled 494 on by the --srec-forceS3 command line switch. */ 495extern bfd_boolean S3Forced; 496 497/* Forward declarations. */ 498static void setup_section (bfd *, asection *, void *); 499static void setup_bfd_headers (bfd *, bfd *); 500static void copy_relocations_in_section (bfd *, asection *, void *); 501static void copy_section (bfd *, asection *, void *); 502static void get_sections (bfd *, asection *, void *); 503static int compare_section_lma (const void *, const void *); 504static void mark_symbols_used_in_relocations (bfd *, asection *, void *); 505static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***); 506static const char *lookup_sym_redefinition (const char *); 507 508static void 509copy_usage (FILE *stream, int exit_status) 510{ 511 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name); 512 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n")); 513 fprintf (stream, _(" The options are:\n")); 514 fprintf (stream, _("\ 515 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\ 516 -O --output-target <bfdname> Create an output file in format <bfdname>\n\ 517 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\ 518 -F --target <bfdname> Set both input and output format to <bfdname>\n\ 519 --debugging Convert debugging information, if possible\n\ 520 -p --preserve-dates Copy modified/access timestamps to the output\n")); 521 if (DEFAULT_AR_DETERMINISTIC) 522 fprintf (stream, _("\ 523 -D --enable-deterministic-archives\n\ 524 Produce deterministic output when stripping archives (default)\n\ 525 -U --disable-deterministic-archives\n\ 526 Disable -D behavior\n")); 527 else 528 fprintf (stream, _("\ 529 -D --enable-deterministic-archives\n\ 530 Produce deterministic output when stripping archives\n\ 531 -U --disable-deterministic-archives\n\ 532 Disable -D behavior (default)\n")); 533 fprintf (stream, _("\ 534 -j --only-section <name> Only copy section <name> into the output\n\ 535 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\ 536 -R --remove-section <name> Remove section <name> from the output\n\ 537 -S --strip-all Remove all symbol and relocation information\n\ 538 -g --strip-debug Remove all debugging symbols & sections\n\ 539 --strip-dwo Remove all DWO sections\n\ 540 --strip-unneeded Remove all symbols not needed by relocations\n\ 541 -N --strip-symbol <name> Do not copy symbol <name>\n\ 542 --strip-unneeded-symbol <name>\n\ 543 Do not copy symbol <name> unless needed by\n\ 544 relocations\n\ 545 --only-keep-debug Strip everything but the debug information\n\ 546 --extract-dwo Copy only DWO sections\n\ 547 --extract-symbol Remove section contents but keep symbols\n\ 548 -K --keep-symbol <name> Do not strip symbol <name>\n\ 549 --keep-file-symbols Do not strip file symbol(s)\n\ 550 --localize-hidden Turn all ELF hidden symbols into locals\n\ 551 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\ 552 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\ 553 -G --keep-global-symbol <name> Localize all symbols except <name>\n\ 554 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\ 555 --weaken Force all global symbols to be marked as weak\n\ 556 -w --wildcard Permit wildcard in symbol comparison\n\ 557 -x --discard-all Remove all non-global symbols\n\ 558 -X --discard-locals Remove any compiler-generated symbols\n\ 559 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\ 560 --interleave-width <number> Set N for --interleave\n\ 561 -b --byte <num> Select byte <num> in every interleaved block\n\ 562 --gap-fill <val> Fill gaps between sections with <val>\n\ 563 --pad-to <addr> Pad the last section up to address <addr>\n\ 564 --set-start <addr> Set the start address to <addr>\n\ 565 {--change-start|--adjust-start} <incr>\n\ 566 Add <incr> to the start address\n\ 567 {--change-addresses|--adjust-vma} <incr>\n\ 568 Add <incr> to LMA, VMA and start addresses\n\ 569 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\ 570 Change LMA and VMA of section <name> by <val>\n\ 571 --change-section-lma <name>{=|+|-}<val>\n\ 572 Change the LMA of section <name> by <val>\n\ 573 --change-section-vma <name>{=|+|-}<val>\n\ 574 Change the VMA of section <name> by <val>\n\ 575 {--[no-]change-warnings|--[no-]adjust-warnings}\n\ 576 Warn if a named section does not exist\n\ 577 --set-section-flags <name>=<flags>\n\ 578 Set section <name>'s properties to <flags>\n\ 579 --add-section <name>=<file> Add section <name> found in <file> to output\n\ 580 --update-section <name>=<file>\n\ 581 Update contents of section <name> with\n\ 582 contents found in <file>\n\ 583 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\ 584 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\ 585 --long-section-names {enable|disable|keep}\n\ 586 Handle long section names in Coff objects.\n\ 587 --change-leading-char Force output format's leading character style\n\ 588 --remove-leading-char Remove leading character from global symbols\n\ 589 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\ 590 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\ 591 --redefine-syms <file> --redefine-sym for all symbol pairs \n\ 592 listed in <file>\n\ 593 --srec-len <number> Restrict the length of generated Srecords\n\ 594 --srec-forceS3 Restrict the type of generated Srecords to S3\n\ 595 --strip-symbols <file> -N for all symbols listed in <file>\n\ 596 --strip-unneeded-symbols <file>\n\ 597 --strip-unneeded-symbol for all symbols listed\n\ 598 in <file>\n\ 599 --keep-symbols <file> -K for all symbols listed in <file>\n\ 600 --localize-symbols <file> -L for all symbols listed in <file>\n\ 601 --globalize-symbols <file> --globalize-symbol for all in <file>\n\ 602 --keep-global-symbols <file> -G for all symbols listed in <file>\n\ 603 --weaken-symbols <file> -W for all symbols listed in <file>\n\ 604 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\ 605 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\ 606 --writable-text Mark the output text as writable\n\ 607 --readonly-text Make the output text write protected\n\ 608 --pure Mark the output file as demand paged\n\ 609 --impure Mark the output file as impure\n\ 610 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\ 611 --prefix-sections <prefix> Add <prefix> to start of every section name\n\ 612 --prefix-alloc-sections <prefix>\n\ 613 Add <prefix> to start of every allocatable\n\ 614 section name\n\ 615 --file-alignment <num> Set PE file alignment to <num>\n\ 616 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\ 617 <commit>\n\ 618 --image-base <address> Set PE image base to <address>\n\ 619 --section-alignment <num> Set PE section alignment to <num>\n\ 620 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\ 621 <commit>\n\ 622 --subsystem <name>[:<version>]\n\ 623 Set PE subsystem to <name> [& <version>]\n\ 624 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\ 625 Compress DWARF debug sections using zlib\n\ 626 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\ 627 -v --verbose List all object files modified\n\ 628 @<file> Read options from <file>\n\ 629 -V --version Display this program's version number\n\ 630 -h --help Display this output\n\ 631 --info List object formats & architectures supported\n\ 632")); 633 list_supported_targets (program_name, stream); 634 if (REPORT_BUGS_TO[0] && exit_status == 0) 635 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO); 636 exit (exit_status); 637} 638 639static void 640strip_usage (FILE *stream, int exit_status) 641{ 642 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name); 643 fprintf (stream, _(" Removes symbols and sections from files\n")); 644 fprintf (stream, _(" The options are:\n")); 645 fprintf (stream, _("\ 646 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\ 647 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\ 648 -F --target=<bfdname> Set both input and output format to <bfdname>\n\ 649 -p --preserve-dates Copy modified/access timestamps to the output\n\ 650")); 651 if (DEFAULT_AR_DETERMINISTIC) 652 fprintf (stream, _("\ 653 -D --enable-deterministic-archives\n\ 654 Produce deterministic output when stripping archives (default)\n\ 655 -U --disable-deterministic-archives\n\ 656 Disable -D behavior\n")); 657 else 658 fprintf (stream, _("\ 659 -D --enable-deterministic-archives\n\ 660 Produce deterministic output when stripping archives\n\ 661 -U --disable-deterministic-archives\n\ 662 Disable -D behavior (default)\n")); 663 fprintf (stream, _("\ 664 -R --remove-section=<name> Also remove section <name> from the output\n\ 665 -s --strip-all Remove all symbol and relocation information\n\ 666 -g -S -d --strip-debug Remove all debugging symbols & sections\n\ 667 --strip-dwo Remove all DWO sections\n\ 668 --strip-unneeded Remove all symbols not needed by relocations\n\ 669 --only-keep-debug Strip everything but the debug information\n\ 670 -N --strip-symbol=<name> Do not copy symbol <name>\n\ 671 -K --keep-symbol=<name> Do not strip symbol <name>\n\ 672 --keep-file-symbols Do not strip file symbol(s)\n\ 673 -w --wildcard Permit wildcard in symbol comparison\n\ 674 -x --discard-all Remove all non-global symbols\n\ 675 -X --discard-locals Remove any compiler-generated symbols\n\ 676 -v --verbose List all object files modified\n\ 677 -V --version Display this program's version number\n\ 678 -h --help Display this output\n\ 679 --info List object formats & architectures supported\n\ 680 -o <file> Place stripped output into <file>\n\ 681")); 682 683 list_supported_targets (program_name, stream); 684 if (REPORT_BUGS_TO[0] && exit_status == 0) 685 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO); 686 exit (exit_status); 687} 688 689/* Parse section flags into a flagword, with a fatal error if the 690 string can't be parsed. */ 691 692static flagword 693parse_flags (const char *s) 694{ 695 flagword ret; 696 const char *snext; 697 int len; 698 699 ret = SEC_NO_FLAGS; 700 701 do 702 { 703 snext = strchr (s, ','); 704 if (snext == NULL) 705 len = strlen (s); 706 else 707 { 708 len = snext - s; 709 ++snext; 710 } 711 712 if (0) ; 713#define PARSE_FLAG(fname,fval) \ 714 else if (strncasecmp (fname, s, len) == 0) ret |= fval 715 PARSE_FLAG ("alloc", SEC_ALLOC); 716 PARSE_FLAG ("load", SEC_LOAD); 717 PARSE_FLAG ("noload", SEC_NEVER_LOAD); 718 PARSE_FLAG ("readonly", SEC_READONLY); 719 PARSE_FLAG ("debug", SEC_DEBUGGING); 720 PARSE_FLAG ("code", SEC_CODE); 721 PARSE_FLAG ("data", SEC_DATA); 722 PARSE_FLAG ("rom", SEC_ROM); 723 PARSE_FLAG ("share", SEC_COFF_SHARED); 724 PARSE_FLAG ("contents", SEC_HAS_CONTENTS); 725 PARSE_FLAG ("merge", SEC_MERGE); 726 PARSE_FLAG ("strings", SEC_STRINGS); 727#undef PARSE_FLAG 728 else 729 { 730 char *copy; 731 732 copy = (char *) xmalloc (len + 1); 733 strncpy (copy, s, len); 734 copy[len] = '\0'; 735 non_fatal (_("unrecognized section flag `%s'"), copy); 736 fatal (_("supported flags: %s"), 737 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings"); 738 } 739 740 s = snext; 741 } 742 while (s != NULL); 743 744 return ret; 745} 746 747/* Parse symbol flags into a flagword, with a fatal error if the 748 string can't be parsed. */ 749 750static flagword 751parse_symflags (const char *s, char **other) 752{ 753 flagword ret; 754 const char *snext; 755 int len; 756 757 ret = BSF_NO_FLAGS; 758 759 do 760 { 761 snext = strchr (s, ','); 762 if (snext == NULL) 763 len = strlen (s); 764 else 765 { 766 len = snext - s; 767 ++snext; 768 } 769 770#define PARSE_FLAG(fname,fval) \ 771 else if (len == (int) sizeof fname - 1 && strncasecmp (fname, s, len) == 0) \ 772 ret |= fval 773 774#define PARSE_OTHER(fname,fval) \ 775 else if (len >= (int) sizeof fname && strncasecmp (fname, s, sizeof fname - 1) == 0) \ 776 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1) 777 778 if (0) ; 779 PARSE_FLAG ("local", BSF_LOCAL); 780 PARSE_FLAG ("global", BSF_GLOBAL); 781 PARSE_FLAG ("export", BSF_EXPORT); 782 PARSE_FLAG ("debug", BSF_DEBUGGING); 783 PARSE_FLAG ("function", BSF_FUNCTION); 784 PARSE_FLAG ("weak", BSF_WEAK); 785 PARSE_FLAG ("section", BSF_SECTION_SYM); 786 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR); 787 PARSE_FLAG ("warning", BSF_WARNING); 788 PARSE_FLAG ("indirect", BSF_INDIRECT); 789 PARSE_FLAG ("file", BSF_FILE); 790 PARSE_FLAG ("object", BSF_OBJECT); 791 PARSE_FLAG ("synthetic", BSF_SYNTHETIC); 792 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION); 793 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT); 794 PARSE_OTHER ("before=", *other); 795 796#undef PARSE_FLAG 797#undef PARSE_OTHER 798 else 799 { 800 char *copy; 801 802 copy = (char *) xmalloc (len + 1); 803 strncpy (copy, s, len); 804 copy[len] = '\0'; 805 non_fatal (_("unrecognized symbol flag `%s'"), copy); 806 fatal (_("supported flags: %s"), 807 "local, global, export, debug, function, weak, section, " 808 "constructor, warning, indirect, file, object, synthetic, " 809 "indirect-function, unique-object, before=<othersym>"); 810 } 811 812 s = snext; 813 } 814 while (s != NULL); 815 816 return ret; 817} 818 819/* Find and optionally add an entry in the change_sections list. 820 821 We need to be careful in how we match section names because of the support 822 for wildcard characters. For example suppose that the user has invoked 823 objcopy like this: 824 825 --set-section-flags .debug_*=debug 826 --set-section-flags .debug_str=readonly,debug 827 --change-section-address .debug_*ranges=0x1000 828 829 With the idea that all debug sections will receive the DEBUG flag, the 830 .debug_str section will also receive the READONLY flag and the 831 .debug_ranges and .debug_aranges sections will have their address set to 832 0x1000. (This may not make much sense, but it is just an example). 833 834 When adding the section name patterns to the section list we need to make 835 sure that previous entries do not match with the new entry, unless the 836 match is exact. (In which case we assume that the user is overriding 837 the previous entry with the new context). 838 839 When matching real section names to the section list we make use of the 840 wildcard characters, but we must do so in context. Eg if we are setting 841 section addresses then we match for .debug_ranges but not for .debug_info. 842 843 Finally, if ADD is false and we do find a match, we mark the section list 844 entry as used. */ 845 846static struct section_list * 847find_section_list (const char *name, bfd_boolean add, unsigned int context) 848{ 849 struct section_list *p; 850 851 /* assert ((context & ((1 << 7) - 1)) != 0); */ 852 853 for (p = change_sections; p != NULL; p = p->next) 854 { 855 if (add) 856 { 857 if (strcmp (p->pattern, name) == 0) 858 { 859 /* Check for context conflicts. */ 860 if (((p->context & SECTION_CONTEXT_REMOVE) 861 && (context & SECTION_CONTEXT_COPY)) 862 || ((context & SECTION_CONTEXT_REMOVE) 863 && (p->context & SECTION_CONTEXT_COPY))) 864 fatal (_("error: %s both copied and removed"), name); 865 866 if (((p->context & SECTION_CONTEXT_SET_VMA) 867 && (context & SECTION_CONTEXT_ALTER_VMA)) 868 || ((context & SECTION_CONTEXT_SET_VMA) 869 && (context & SECTION_CONTEXT_ALTER_VMA))) 870 fatal (_("error: %s both sets and alters VMA"), name); 871 872 if (((p->context & SECTION_CONTEXT_SET_LMA) 873 && (context & SECTION_CONTEXT_ALTER_LMA)) 874 || ((context & SECTION_CONTEXT_SET_LMA) 875 && (context & SECTION_CONTEXT_ALTER_LMA))) 876 fatal (_("error: %s both sets and alters LMA"), name); 877 878 /* Extend the context. */ 879 p->context |= context; 880 return p; 881 } 882 } 883 /* If we are not adding a new name/pattern then 884 only check for a match if the context applies. */ 885 else if ((p->context & context) 886 /* We could check for the presence of wildchar characters 887 first and choose between calling strcmp and fnmatch, 888 but is that really worth it ? */ 889 && fnmatch (p->pattern, name, 0) == 0) 890 { 891 p->used = TRUE; 892 return p; 893 } 894 } 895 896 if (! add) 897 return NULL; 898 899 p = (struct section_list *) xmalloc (sizeof (struct section_list)); 900 p->pattern = name; 901 p->used = FALSE; 902 p->context = context; 903 p->vma_val = 0; 904 p->lma_val = 0; 905 p->flags = 0; 906 p->next = change_sections; 907 change_sections = p; 908 909 return p; 910} 911 912/* There is htab_hash_string but no htab_eq_string. Makes sense. */ 913 914static int 915eq_string (const void *s1, const void *s2) 916{ 917 return strcmp ((const char *) s1, (const char *) s2) == 0; 918} 919 920static htab_t 921create_symbol_htab (void) 922{ 923 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free); 924} 925 926static void 927create_symbol_htabs (void) 928{ 929 strip_specific_htab = create_symbol_htab (); 930 strip_unneeded_htab = create_symbol_htab (); 931 keep_specific_htab = create_symbol_htab (); 932 localize_specific_htab = create_symbol_htab (); 933 globalize_specific_htab = create_symbol_htab (); 934 keepglobal_specific_htab = create_symbol_htab (); 935 weaken_specific_htab = create_symbol_htab (); 936} 937 938/* Add a symbol to strip_specific_list. */ 939 940static void 941add_specific_symbol (const char *name, htab_t htab) 942{ 943 *htab_find_slot (htab, name, INSERT) = (char *) name; 944} 945 946/* Add symbols listed in `filename' to strip_specific_list. */ 947 948#define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t') 949#define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0') 950 951static void 952add_specific_symbols (const char *filename, htab_t htab) 953{ 954 off_t size; 955 FILE * f; 956 char * line; 957 char * buffer; 958 unsigned int line_count; 959 960 size = get_file_size (filename); 961 if (size == 0) 962 { 963 status = 1; 964 return; 965 } 966 967 buffer = (char *) xmalloc (size + 2); 968 f = fopen (filename, FOPEN_RT); 969 if (f == NULL) 970 fatal (_("cannot open '%s': %s"), filename, strerror (errno)); 971 972 if (fread (buffer, 1, size, f) == 0 || ferror (f)) 973 fatal (_("%s: fread failed"), filename); 974 975 fclose (f); 976 buffer [size] = '\n'; 977 buffer [size + 1] = '\0'; 978 979 line_count = 1; 980 981 for (line = buffer; * line != '\0'; line ++) 982 { 983 char * eol; 984 char * name; 985 char * name_end; 986 int finished = FALSE; 987 988 for (eol = line;; eol ++) 989 { 990 switch (* eol) 991 { 992 case '\n': 993 * eol = '\0'; 994 /* Cope with \n\r. */ 995 if (eol[1] == '\r') 996 ++ eol; 997 finished = TRUE; 998 break; 999 1000 case '\r': 1001 * eol = '\0'; 1002 /* Cope with \r\n. */ 1003 if (eol[1] == '\n') 1004 ++ eol; 1005 finished = TRUE; 1006 break; 1007 1008 case 0: 1009 finished = TRUE; 1010 break; 1011 1012 case '#': 1013 /* Line comment, Terminate the line here, in case a 1014 name is present and then allow the rest of the 1015 loop to find the real end of the line. */ 1016 * eol = '\0'; 1017 break; 1018 1019 default: 1020 break; 1021 } 1022 1023 if (finished) 1024 break; 1025 } 1026 1027 /* A name may now exist somewhere between 'line' and 'eol'. 1028 Strip off leading whitespace and trailing whitespace, 1029 then add it to the list. */ 1030 for (name = line; IS_WHITESPACE (* name); name ++) 1031 ; 1032 for (name_end = name; 1033 (! IS_WHITESPACE (* name_end)) 1034 && (! IS_LINE_TERMINATOR (* name_end)); 1035 name_end ++) 1036 ; 1037 1038 if (! IS_LINE_TERMINATOR (* name_end)) 1039 { 1040 char * extra; 1041 1042 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++) 1043 ; 1044 1045 if (! IS_LINE_TERMINATOR (* extra)) 1046 non_fatal (_("%s:%d: Ignoring rubbish found on this line"), 1047 filename, line_count); 1048 } 1049 1050 * name_end = '\0'; 1051 1052 if (name_end > name) 1053 add_specific_symbol (name, htab); 1054 1055 /* Advance line pointer to end of line. The 'eol ++' in the for 1056 loop above will then advance us to the start of the next line. */ 1057 line = eol; 1058 line_count ++; 1059 } 1060} 1061 1062/* See whether a symbol should be stripped or kept 1063 based on strip_specific_list and keep_symbols. */ 1064 1065static int 1066is_specified_symbol_predicate (void **slot, void *data) 1067{ 1068 struct is_specified_symbol_predicate_data *d = 1069 (struct is_specified_symbol_predicate_data *) data; 1070 const char *slot_name = (char *) *slot; 1071 1072 if (*slot_name != '!') 1073 { 1074 if (! fnmatch (slot_name, d->name, 0)) 1075 { 1076 d->found = TRUE; 1077 /* Continue traversal, there might be a non-match rule. */ 1078 return 1; 1079 } 1080 } 1081 else 1082 { 1083 if (! fnmatch (slot_name + 1, d->name, 0)) 1084 { 1085 d->found = FALSE; 1086 /* Stop traversal. */ 1087 return 0; 1088 } 1089 } 1090 1091 /* Continue traversal. */ 1092 return 1; 1093} 1094 1095static bfd_boolean 1096is_specified_symbol (const char *name, htab_t htab) 1097{ 1098 if (wildcard) 1099 { 1100 struct is_specified_symbol_predicate_data data; 1101 1102 data.name = name; 1103 data.found = FALSE; 1104 1105 htab_traverse (htab, is_specified_symbol_predicate, &data); 1106 1107 return data.found; 1108 } 1109 1110 return htab_find (htab, name) != NULL; 1111} 1112 1113/* Return a pointer to the symbol used as a signature for GROUP. */ 1114 1115static asymbol * 1116group_signature (asection *group) 1117{ 1118 bfd *abfd = group->owner; 1119 Elf_Internal_Shdr *ghdr; 1120 1121 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 1122 return NULL; 1123 1124 ghdr = &elf_section_data (group)->this_hdr; 1125 if (ghdr->sh_link < elf_numsections (abfd)) 1126 { 1127 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1128 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link]; 1129 1130 if (symhdr->sh_type == SHT_SYMTAB 1131 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym) 1132 return isympp[ghdr->sh_info - 1]; 1133 } 1134 return NULL; 1135} 1136 1137/* Return TRUE if the section is a DWO section. */ 1138 1139static bfd_boolean 1140is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1141{ 1142 const char *name = bfd_get_section_name (abfd, sec); 1143 int len = strlen (name); 1144 1145 return strncmp (name + len - 4, ".dwo", 4) == 0; 1146} 1147 1148/* Return TRUE if section SEC is in the update list. */ 1149 1150static bfd_boolean 1151is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1152{ 1153 if (update_sections != NULL) 1154 { 1155 struct section_add *pupdate; 1156 1157 for (pupdate = update_sections; 1158 pupdate != NULL; 1159 pupdate = pupdate->next) 1160 { 1161 if (strcmp (sec->name, pupdate->name) == 0) 1162 return TRUE; 1163 } 1164 } 1165 1166 return FALSE; 1167} 1168 1169/* See if a non-group section is being removed. */ 1170 1171static bfd_boolean 1172is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1173{ 1174 if (sections_removed || sections_copied) 1175 { 1176 struct section_list *p; 1177 struct section_list *q; 1178 1179 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE, 1180 SECTION_CONTEXT_REMOVE); 1181 q = find_section_list (bfd_get_section_name (abfd, sec), FALSE, 1182 SECTION_CONTEXT_COPY); 1183 1184 if (p && q) 1185 fatal (_("error: section %s matches both remove and copy options"), 1186 bfd_get_section_name (abfd, sec)); 1187 if (p && is_update_section (abfd, sec)) 1188 fatal (_("error: section %s matches both update and remove options"), 1189 bfd_get_section_name (abfd, sec)); 1190 1191 if (p != NULL) 1192 return TRUE; 1193 if (sections_copied && q == NULL) 1194 return TRUE; 1195 } 1196 1197 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0) 1198 { 1199 if (strip_symbols == STRIP_DEBUG 1200 || strip_symbols == STRIP_UNNEEDED 1201 || strip_symbols == STRIP_ALL 1202 || discard_locals == LOCALS_ALL 1203 || convert_debugging) 1204 { 1205 /* By default we don't want to strip .reloc section. 1206 This section has for pe-coff special meaning. See 1207 pe-dll.c file in ld, and peXXigen.c in bfd for details. */ 1208 if (strcmp (bfd_get_section_name (abfd, sec), ".reloc") != 0) 1209 return TRUE; 1210 } 1211 1212 if (strip_symbols == STRIP_DWO) 1213 return is_dwo_section (abfd, sec); 1214 1215 if (strip_symbols == STRIP_NONDEBUG) 1216 return FALSE; 1217 } 1218 1219 if (strip_symbols == STRIP_NONDWO) 1220 return !is_dwo_section (abfd, sec); 1221 1222 return FALSE; 1223} 1224 1225/* See if a section is being removed. */ 1226 1227static bfd_boolean 1228is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1229{ 1230 if (is_strip_section_1 (abfd, sec)) 1231 return TRUE; 1232 1233 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0) 1234 { 1235 asymbol *gsym; 1236 const char *gname; 1237 asection *elt, *first; 1238 1239 /* PR binutils/3181 1240 If we are going to strip the group signature symbol, then 1241 strip the group section too. */ 1242 gsym = group_signature (sec); 1243 if (gsym != NULL) 1244 gname = gsym->name; 1245 else 1246 gname = sec->name; 1247 if ((strip_symbols == STRIP_ALL 1248 && !is_specified_symbol (gname, keep_specific_htab)) 1249 || is_specified_symbol (gname, strip_specific_htab)) 1250 return TRUE; 1251 1252 /* Remove the group section if all members are removed. */ 1253 first = elt = elf_next_in_group (sec); 1254 while (elt != NULL) 1255 { 1256 if (!is_strip_section_1 (abfd, elt)) 1257 return FALSE; 1258 elt = elf_next_in_group (elt); 1259 if (elt == first) 1260 break; 1261 } 1262 1263 return TRUE; 1264 } 1265 1266 return FALSE; 1267} 1268 1269static bfd_boolean 1270is_nondebug_keep_contents_section (bfd *ibfd, asection *isection) 1271{ 1272 /* Always keep ELF note sections. */ 1273 if (ibfd->xvec->flavour == bfd_target_elf_flavour) 1274 return (elf_section_type (isection) == SHT_NOTE); 1275 1276 /* Always keep the .buildid section for PE/COFF. 1277 1278 Strictly, this should be written "always keep the section storing the debug 1279 directory", but that may be the .text section for objects produced by some 1280 tools, which it is not sensible to keep. */ 1281 if (ibfd->xvec->flavour == bfd_target_coff_flavour) 1282 return (strcmp (bfd_get_section_name (ibfd, isection), ".buildid") == 0); 1283 1284 return FALSE; 1285} 1286 1287/* Return true if SYM is a hidden symbol. */ 1288 1289static bfd_boolean 1290is_hidden_symbol (asymbol *sym) 1291{ 1292 elf_symbol_type *elf_sym; 1293 1294 elf_sym = elf_symbol_from (sym->the_bfd, sym); 1295 if (elf_sym != NULL) 1296 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other)) 1297 { 1298 case STV_HIDDEN: 1299 case STV_INTERNAL: 1300 return TRUE; 1301 } 1302 return FALSE; 1303} 1304 1305static bfd_boolean 1306need_sym_before (struct addsym_node **node, const char *sym) 1307{ 1308 int count; 1309 struct addsym_node *ptr = add_sym_list; 1310 1311 /* 'othersym' symbols are at the front of the list. */ 1312 for (count = 0; count < add_symbols; count++) 1313 { 1314 if (!ptr->othersym) 1315 break; 1316 else if (strcmp (ptr->othersym, sym) == 0) 1317 { 1318 free (ptr->othersym); 1319 ptr->othersym = ""; /* Empty name is hopefully never a valid symbol name. */ 1320 *node = ptr; 1321 return TRUE; 1322 } 1323 ptr = ptr->next; 1324 } 1325 return FALSE; 1326} 1327 1328static asymbol * 1329create_new_symbol (struct addsym_node *ptr, bfd *obfd) 1330{ 1331 asymbol *sym = bfd_make_empty_symbol(obfd); 1332 1333 bfd_asymbol_name(sym) = ptr->symdef; 1334 sym->value = ptr->symval; 1335 sym->flags = ptr->flags; 1336 if (ptr->section) 1337 { 1338 asection *sec = bfd_get_section_by_name (obfd, ptr->section); 1339 if (!sec) 1340 fatal (_("Section %s not found"), ptr->section); 1341 sym->section = sec; 1342 } 1343 else 1344 sym->section = bfd_abs_section_ptr; 1345 return sym; 1346} 1347 1348/* Choose which symbol entries to copy; put the result in OSYMS. 1349 We don't copy in place, because that confuses the relocs. 1350 Return the number of symbols to print. */ 1351 1352static unsigned int 1353filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms, 1354 asymbol **isyms, long symcount) 1355{ 1356 asymbol **from = isyms, **to = osyms; 1357 long src_count = 0, dst_count = 0; 1358 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0; 1359 1360 for (; src_count < symcount; src_count++) 1361 { 1362 asymbol *sym = from[src_count]; 1363 flagword flags = sym->flags; 1364 char *name = (char *) bfd_asymbol_name (sym); 1365 bfd_boolean keep; 1366 bfd_boolean used_in_reloc = FALSE; 1367 bfd_boolean undefined; 1368 bfd_boolean rem_leading_char; 1369 bfd_boolean add_leading_char; 1370 1371 undefined = bfd_is_und_section (bfd_get_section (sym)); 1372 1373 if (add_sym_list) 1374 { 1375 struct addsym_node *ptr; 1376 1377 if (need_sym_before (&ptr, name)) 1378 to[dst_count++] = create_new_symbol (ptr, obfd); 1379 } 1380 1381 if (redefine_sym_list) 1382 { 1383 char *old_name, *new_name; 1384 1385 old_name = (char *) bfd_asymbol_name (sym); 1386 new_name = (char *) lookup_sym_redefinition (old_name); 1387 bfd_asymbol_name (sym) = new_name; 1388 name = new_name; 1389 } 1390 1391 /* Check if we will remove the current leading character. */ 1392 rem_leading_char = 1393 (name[0] == bfd_get_symbol_leading_char (abfd)) 1394 && (change_leading_char 1395 || (remove_leading_char 1396 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0 1397 || undefined 1398 || bfd_is_com_section (bfd_get_section (sym))))); 1399 1400 /* Check if we will add a new leading character. */ 1401 add_leading_char = 1402 change_leading_char 1403 && (bfd_get_symbol_leading_char (obfd) != '\0') 1404 && (bfd_get_symbol_leading_char (abfd) == '\0' 1405 || (name[0] == bfd_get_symbol_leading_char (abfd))); 1406 1407 /* Short circuit for change_leading_char if we can do it in-place. */ 1408 if (rem_leading_char && add_leading_char && !prefix_symbols_string) 1409 { 1410 name[0] = bfd_get_symbol_leading_char (obfd); 1411 bfd_asymbol_name (sym) = name; 1412 rem_leading_char = FALSE; 1413 add_leading_char = FALSE; 1414 } 1415 1416 /* Remove leading char. */ 1417 if (rem_leading_char) 1418 bfd_asymbol_name (sym) = ++name; 1419 1420 /* Add new leading char and/or prefix. */ 1421 if (add_leading_char || prefix_symbols_string) 1422 { 1423 char *n, *ptr; 1424 1425 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string) 1426 + strlen (name) + 1); 1427 if (add_leading_char) 1428 *ptr++ = bfd_get_symbol_leading_char (obfd); 1429 1430 if (prefix_symbols_string) 1431 { 1432 strcpy (ptr, prefix_symbols_string); 1433 ptr += strlen (prefix_symbols_string); 1434 } 1435 1436 strcpy (ptr, name); 1437 bfd_asymbol_name (sym) = n; 1438 name = n; 1439 } 1440 1441 if (strip_symbols == STRIP_ALL) 1442 keep = FALSE; 1443 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */ 1444 || ((flags & BSF_SECTION_SYM) != 0 1445 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags 1446 & BSF_KEEP) != 0)) 1447 { 1448 keep = TRUE; 1449 used_in_reloc = TRUE; 1450 } 1451 else if (relocatable /* Relocatable file. */ 1452 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0 1453 || bfd_is_com_section (bfd_get_section (sym)))) 1454 keep = TRUE; 1455 else if (bfd_decode_symclass (sym) == 'I') 1456 /* Global symbols in $idata sections need to be retained 1457 even if relocatable is FALSE. External users of the 1458 library containing the $idata section may reference these 1459 symbols. */ 1460 keep = TRUE; 1461 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */ 1462 || (flags & BSF_WEAK) != 0 1463 || undefined 1464 || bfd_is_com_section (bfd_get_section (sym))) 1465 keep = strip_symbols != STRIP_UNNEEDED; 1466 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */ 1467 keep = (strip_symbols != STRIP_DEBUG 1468 && strip_symbols != STRIP_UNNEEDED 1469 && ! convert_debugging); 1470 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym))) 1471 /* COMDAT sections store special information in local 1472 symbols, so we cannot risk stripping any of them. */ 1473 keep = TRUE; 1474 else /* Local symbol. */ 1475 keep = (strip_symbols != STRIP_UNNEEDED 1476 && (discard_locals != LOCALS_ALL 1477 && (discard_locals != LOCALS_START_L 1478 || ! bfd_is_local_label (abfd, sym)))); 1479 1480 if (keep && is_specified_symbol (name, strip_specific_htab)) 1481 { 1482 /* There are multiple ways to set 'keep' above, but if it 1483 was the relocatable symbol case, then that's an error. */ 1484 if (used_in_reloc) 1485 { 1486 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name); 1487 status = 1; 1488 } 1489 else 1490 keep = FALSE; 1491 } 1492 1493 if (keep 1494 && !(flags & BSF_KEEP) 1495 && is_specified_symbol (name, strip_unneeded_htab)) 1496 keep = FALSE; 1497 1498 if (!keep 1499 && ((keep_file_symbols && (flags & BSF_FILE)) 1500 || is_specified_symbol (name, keep_specific_htab))) 1501 keep = TRUE; 1502 1503 if (keep && is_strip_section (abfd, bfd_get_section (sym))) 1504 keep = FALSE; 1505 1506 if (keep) 1507 { 1508 if ((flags & BSF_GLOBAL) != 0 1509 && (weaken || is_specified_symbol (name, weaken_specific_htab))) 1510 { 1511 sym->flags &= ~ BSF_GLOBAL; 1512 sym->flags |= BSF_WEAK; 1513 } 1514 1515 if (!undefined 1516 && (flags & (BSF_GLOBAL | BSF_WEAK)) 1517 && (is_specified_symbol (name, localize_specific_htab) 1518 || (htab_elements (keepglobal_specific_htab) != 0 1519 && ! is_specified_symbol (name, keepglobal_specific_htab)) 1520 || (localize_hidden && is_hidden_symbol (sym)))) 1521 { 1522 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK); 1523 sym->flags |= BSF_LOCAL; 1524 } 1525 1526 if (!undefined 1527 && (flags & BSF_LOCAL) 1528 && is_specified_symbol (name, globalize_specific_htab)) 1529 { 1530 sym->flags &= ~ BSF_LOCAL; 1531 sym->flags |= BSF_GLOBAL; 1532 } 1533 1534 to[dst_count++] = sym; 1535 } 1536 } 1537 if (add_sym_list) 1538 { 1539 struct addsym_node *ptr = add_sym_list; 1540 1541 for (src_count = 0; src_count < add_symbols; src_count++) 1542 { 1543 if (ptr->othersym) 1544 { 1545 if (strcmp (ptr->othersym, "")) 1546 fatal (_("'before=%s' not found"), ptr->othersym); 1547 } 1548 else 1549 to[dst_count++] = create_new_symbol (ptr, obfd); 1550 1551 ptr = ptr->next; 1552 } 1553 } 1554 1555 to[dst_count] = NULL; 1556 1557 return dst_count; 1558} 1559 1560/* Find the redefined name of symbol SOURCE. */ 1561 1562static const char * 1563lookup_sym_redefinition (const char *source) 1564{ 1565 struct redefine_node *list; 1566 1567 for (list = redefine_sym_list; list != NULL; list = list->next) 1568 if (strcmp (source, list->source) == 0) 1569 return list->target; 1570 1571 return source; 1572} 1573 1574/* Add a node to a symbol redefine list. */ 1575 1576static void 1577redefine_list_append (const char *cause, const char *source, const char *target) 1578{ 1579 struct redefine_node **p; 1580 struct redefine_node *list; 1581 struct redefine_node *new_node; 1582 1583 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next) 1584 { 1585 if (strcmp (source, list->source) == 0) 1586 fatal (_("%s: Multiple redefinition of symbol \"%s\""), 1587 cause, source); 1588 1589 if (strcmp (target, list->target) == 0) 1590 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"), 1591 cause, target); 1592 } 1593 1594 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node)); 1595 1596 new_node->source = strdup (source); 1597 new_node->target = strdup (target); 1598 new_node->next = NULL; 1599 1600 *p = new_node; 1601} 1602 1603/* Handle the --redefine-syms option. Read lines containing "old new" 1604 from the file, and add them to the symbol redefine list. */ 1605 1606static void 1607add_redefine_syms_file (const char *filename) 1608{ 1609 FILE *file; 1610 char *buf; 1611 size_t bufsize; 1612 size_t len; 1613 size_t outsym_off; 1614 int c, lineno; 1615 1616 file = fopen (filename, "r"); 1617 if (file == NULL) 1618 fatal (_("couldn't open symbol redefinition file %s (error: %s)"), 1619 filename, strerror (errno)); 1620 1621 bufsize = 100; 1622 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */); 1623 1624 lineno = 1; 1625 c = getc (file); 1626 len = 0; 1627 outsym_off = 0; 1628 while (c != EOF) 1629 { 1630 /* Collect the input symbol name. */ 1631 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF) 1632 { 1633 if (c == '#') 1634 goto comment; 1635 buf[len++] = c; 1636 if (len >= bufsize) 1637 { 1638 bufsize *= 2; 1639 buf = (char *) xrealloc (buf, bufsize + 1); 1640 } 1641 c = getc (file); 1642 } 1643 buf[len++] = '\0'; 1644 if (c == EOF) 1645 break; 1646 1647 /* Eat white space between the symbol names. */ 1648 while (IS_WHITESPACE (c)) 1649 c = getc (file); 1650 if (c == '#' || IS_LINE_TERMINATOR (c)) 1651 goto comment; 1652 if (c == EOF) 1653 break; 1654 1655 /* Collect the output symbol name. */ 1656 outsym_off = len; 1657 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF) 1658 { 1659 if (c == '#') 1660 goto comment; 1661 buf[len++] = c; 1662 if (len >= bufsize) 1663 { 1664 bufsize *= 2; 1665 buf = (char *) xrealloc (buf, bufsize + 1); 1666 } 1667 c = getc (file); 1668 } 1669 buf[len++] = '\0'; 1670 if (c == EOF) 1671 break; 1672 1673 /* Eat white space at end of line. */ 1674 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c)) 1675 c = getc (file); 1676 if (c == '#') 1677 goto comment; 1678 /* Handle \r\n. */ 1679 if ((c == '\r' && (c = getc (file)) == '\n') 1680 || c == '\n' || c == EOF) 1681 { 1682 end_of_line: 1683 /* Append the redefinition to the list. */ 1684 if (buf[0] != '\0') 1685 redefine_list_append (filename, &buf[0], &buf[outsym_off]); 1686 1687 lineno++; 1688 len = 0; 1689 outsym_off = 0; 1690 if (c == EOF) 1691 break; 1692 c = getc (file); 1693 continue; 1694 } 1695 else 1696 fatal (_("%s:%d: garbage found at end of line"), filename, lineno); 1697 comment: 1698 if (len != 0 && (outsym_off == 0 || outsym_off == len)) 1699 fatal (_("%s:%d: missing new symbol name"), filename, lineno); 1700 buf[len++] = '\0'; 1701 1702 /* Eat the rest of the line and finish it. */ 1703 while (c != '\n' && c != EOF) 1704 c = getc (file); 1705 goto end_of_line; 1706 } 1707 1708 if (len != 0) 1709 fatal (_("%s:%d: premature end of file"), filename, lineno); 1710 1711 free (buf); 1712} 1713 1714/* Copy unkown object file IBFD onto OBFD. 1715 Returns TRUE upon success, FALSE otherwise. */ 1716 1717static bfd_boolean 1718copy_unknown_object (bfd *ibfd, bfd *obfd) 1719{ 1720 char *cbuf; 1721 int tocopy; 1722 long ncopied; 1723 long size; 1724 struct stat buf; 1725 1726 if (bfd_stat_arch_elt (ibfd, &buf) != 0) 1727 { 1728 bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1729 return FALSE; 1730 } 1731 1732 size = buf.st_size; 1733 if (size < 0) 1734 { 1735 non_fatal (_("stat returns negative size for `%s'"), 1736 bfd_get_archive_filename (ibfd)); 1737 return FALSE; 1738 } 1739 1740 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0) 1741 { 1742 bfd_nonfatal (bfd_get_archive_filename (ibfd)); 1743 return FALSE; 1744 } 1745 1746 if (verbose) 1747 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"), 1748 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd)); 1749 1750 cbuf = (char *) xmalloc (BUFSIZE); 1751 ncopied = 0; 1752 while (ncopied < size) 1753 { 1754 tocopy = size - ncopied; 1755 if (tocopy > BUFSIZE) 1756 tocopy = BUFSIZE; 1757 1758 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd) 1759 != (bfd_size_type) tocopy) 1760 { 1761 bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1762 free (cbuf); 1763 return FALSE; 1764 } 1765 1766 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd) 1767 != (bfd_size_type) tocopy) 1768 { 1769 bfd_nonfatal_message (NULL, obfd, NULL, NULL); 1770 free (cbuf); 1771 return FALSE; 1772 } 1773 1774 ncopied += tocopy; 1775 } 1776 1777 /* We should at least to be able to read it back when copying an 1778 unknown object in an archive. */ 1779 chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR); 1780 free (cbuf); 1781 return TRUE; 1782} 1783 1784/* Copy object file IBFD onto OBFD. 1785 Returns TRUE upon success, FALSE otherwise. */ 1786 1787static bfd_boolean 1788copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch) 1789{ 1790 bfd_vma start; 1791 long symcount; 1792 asection **osections = NULL; 1793 asection *gnu_debuglink_section = NULL; 1794 bfd_size_type *gaps = NULL; 1795 bfd_size_type max_gap = 0; 1796 long symsize; 1797 void *dhandle; 1798 enum bfd_architecture iarch; 1799 unsigned int imach; 1800 unsigned int c, i; 1801 1802 if (ibfd->xvec->byteorder != obfd->xvec->byteorder 1803 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN 1804 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN) 1805 { 1806 /* PR 17636: Call non-fatal so that we return to our parent who 1807 may need to tidy temporary files. */ 1808 non_fatal (_("Unable to change endianness of input file(s)")); 1809 return FALSE; 1810 } 1811 1812 if (!bfd_set_format (obfd, bfd_get_format (ibfd))) 1813 { 1814 bfd_nonfatal_message (NULL, obfd, NULL, NULL); 1815 return FALSE; 1816 } 1817 1818 if (ibfd->sections == NULL) 1819 { 1820 non_fatal (_("error: the input file '%s' has no sections"), 1821 bfd_get_archive_filename (ibfd)); 1822 return FALSE; 1823 } 1824 1825 if ((do_debug_sections & compress) != 0 1826 && do_debug_sections != compress 1827 && ibfd->xvec->flavour != bfd_target_elf_flavour) 1828 { 1829 non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"), 1830 bfd_get_archive_filename (ibfd)); 1831 return FALSE; 1832 } 1833 1834 if (verbose) 1835 printf (_("copy from `%s' [%s] to `%s' [%s]\n"), 1836 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd), 1837 bfd_get_filename (obfd), bfd_get_target (obfd)); 1838 1839 if (extract_symbol) 1840 start = 0; 1841 else 1842 { 1843 if (set_start_set) 1844 start = set_start; 1845 else 1846 start = bfd_get_start_address (ibfd); 1847 start += change_start; 1848 } 1849 1850 /* Neither the start address nor the flags 1851 need to be set for a core file. */ 1852 if (bfd_get_format (obfd) != bfd_core) 1853 { 1854 flagword flags; 1855 1856 flags = bfd_get_file_flags (ibfd); 1857 flags |= bfd_flags_to_set; 1858 flags &= ~bfd_flags_to_clear; 1859 flags &= bfd_applicable_file_flags (obfd); 1860 1861 if (strip_symbols == STRIP_ALL) 1862 flags &= ~HAS_RELOC; 1863 1864 if (!bfd_set_start_address (obfd, start) 1865 || !bfd_set_file_flags (obfd, flags)) 1866 { 1867 bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1868 return FALSE; 1869 } 1870 } 1871 1872 /* Copy architecture of input file to output file. */ 1873 iarch = bfd_get_arch (ibfd); 1874 imach = bfd_get_mach (ibfd); 1875 if (input_arch) 1876 { 1877 if (bfd_get_arch_info (ibfd) == NULL 1878 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown) 1879 { 1880 iarch = input_arch->arch; 1881 imach = input_arch->mach; 1882 } 1883 else 1884 non_fatal (_("Input file `%s' ignores binary architecture parameter."), 1885 bfd_get_archive_filename (ibfd)); 1886 } 1887 if (!bfd_set_arch_mach (obfd, iarch, imach) 1888 && (ibfd->target_defaulted 1889 || bfd_get_arch (ibfd) != bfd_get_arch (obfd))) 1890 { 1891 if (bfd_get_arch (ibfd) == bfd_arch_unknown) 1892 non_fatal (_("Unable to recognise the format of the input file `%s'"), 1893 bfd_get_archive_filename (ibfd)); 1894 else 1895 non_fatal (_("Output file cannot represent architecture `%s'"), 1896 bfd_printable_arch_mach (bfd_get_arch (ibfd), 1897 bfd_get_mach (ibfd))); 1898 return FALSE; 1899 } 1900 1901 if (!bfd_set_format (obfd, bfd_get_format (ibfd))) 1902 { 1903 bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1904 return FALSE; 1905 } 1906 1907 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour 1908 && bfd_pei_p (obfd)) 1909 { 1910 /* Set up PE parameters. */ 1911 pe_data_type *pe = pe_data (obfd); 1912 1913 /* Copy PE parameters before changing them. */ 1914 if (ibfd->xvec->flavour == bfd_target_coff_flavour 1915 && bfd_pei_p (ibfd)) 1916 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr; 1917 1918 if (pe_file_alignment != (bfd_vma) -1) 1919 pe->pe_opthdr.FileAlignment = pe_file_alignment; 1920 else 1921 pe_file_alignment = PE_DEF_FILE_ALIGNMENT; 1922 1923 if (pe_heap_commit != (bfd_vma) -1) 1924 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit; 1925 1926 if (pe_heap_reserve != (bfd_vma) -1) 1927 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve; 1928 1929 if (pe_image_base != (bfd_vma) -1) 1930 pe->pe_opthdr.ImageBase = pe_image_base; 1931 1932 if (pe_section_alignment != (bfd_vma) -1) 1933 pe->pe_opthdr.SectionAlignment = pe_section_alignment; 1934 else 1935 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT; 1936 1937 if (pe_stack_commit != (bfd_vma) -1) 1938 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit; 1939 1940 if (pe_stack_reserve != (bfd_vma) -1) 1941 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve; 1942 1943 if (pe_subsystem != -1) 1944 pe->pe_opthdr.Subsystem = pe_subsystem; 1945 1946 if (pe_major_subsystem_version != -1) 1947 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version; 1948 1949 if (pe_minor_subsystem_version != -1) 1950 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version; 1951 1952 if (pe_file_alignment > pe_section_alignment) 1953 { 1954 char file_alignment[20], section_alignment[20]; 1955 1956 sprintf_vma (file_alignment, pe_file_alignment); 1957 sprintf_vma (section_alignment, pe_section_alignment); 1958 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"), 1959 1960 file_alignment, section_alignment); 1961 } 1962 } 1963 1964 if (isympp) 1965 free (isympp); 1966 1967 if (osympp != isympp) 1968 free (osympp); 1969 1970 isympp = NULL; 1971 osympp = NULL; 1972 1973 symsize = bfd_get_symtab_upper_bound (ibfd); 1974 if (symsize < 0) 1975 { 1976 bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1977 return FALSE; 1978 } 1979 1980 osympp = isympp = (asymbol **) xmalloc (symsize); 1981 symcount = bfd_canonicalize_symtab (ibfd, isympp); 1982 if (symcount < 0) 1983 { 1984 bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1985 return FALSE; 1986 } 1987 /* PR 17512: file: d6323821 1988 If the symbol table could not be loaded do not pretend that we have 1989 any symbols. This trips us up later on when we load the relocs. */ 1990 if (symcount == 0) 1991 { 1992 free (isympp); 1993 osympp = isympp = NULL; 1994 } 1995 1996 /* BFD mandates that all output sections be created and sizes set before 1997 any output is done. Thus, we traverse all sections multiple times. */ 1998 bfd_map_over_sections (ibfd, setup_section, obfd); 1999 2000 if (!extract_symbol) 2001 setup_bfd_headers (ibfd, obfd); 2002 2003 if (add_sections != NULL) 2004 { 2005 struct section_add *padd; 2006 struct section_list *pset; 2007 2008 for (padd = add_sections; padd != NULL; padd = padd->next) 2009 { 2010 flagword flags; 2011 2012 pset = find_section_list (padd->name, FALSE, 2013 SECTION_CONTEXT_SET_FLAGS); 2014 if (pset != NULL) 2015 flags = pset->flags | SEC_HAS_CONTENTS; 2016 else 2017 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA; 2018 2019 /* bfd_make_section_with_flags() does not return very helpful 2020 error codes, so check for the most likely user error first. */ 2021 if (bfd_get_section_by_name (obfd, padd->name)) 2022 { 2023 bfd_nonfatal_message (NULL, obfd, NULL, 2024 _("can't add section '%s'"), padd->name); 2025 return FALSE; 2026 } 2027 else 2028 { 2029 /* We use LINKER_CREATED here so that the backend hooks 2030 will create any special section type information, 2031 instead of presuming we know what we're doing merely 2032 because we set the flags. */ 2033 padd->section = bfd_make_section_with_flags 2034 (obfd, padd->name, flags | SEC_LINKER_CREATED); 2035 if (padd->section == NULL) 2036 { 2037 bfd_nonfatal_message (NULL, obfd, NULL, 2038 _("can't create section `%s'"), 2039 padd->name); 2040 return FALSE; 2041 } 2042 } 2043 2044 if (! bfd_set_section_size (obfd, padd->section, padd->size)) 2045 { 2046 bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 2047 return FALSE; 2048 } 2049 2050 pset = find_section_list (padd->name, FALSE, 2051 SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA); 2052 if (pset != NULL 2053 && ! bfd_set_section_vma (obfd, padd->section, pset->vma_val)) 2054 { 2055 bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 2056 return FALSE; 2057 } 2058 2059 pset = find_section_list (padd->name, FALSE, 2060 SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA); 2061 if (pset != NULL) 2062 { 2063 padd->section->lma = pset->lma_val; 2064 2065 if (! bfd_set_section_alignment 2066 (obfd, padd->section, 2067 bfd_section_alignment (obfd, padd->section))) 2068 { 2069 bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 2070 return FALSE; 2071 } 2072 } 2073 } 2074 } 2075 2076 if (update_sections != NULL) 2077 { 2078 struct section_add *pupdate; 2079 2080 for (pupdate = update_sections; 2081 pupdate != NULL; 2082 pupdate = pupdate->next) 2083 { 2084 asection *osec; 2085 2086 pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name); 2087 if (pupdate->section == NULL) 2088 { 2089 non_fatal (_("error: %s not found, can't be updated"), pupdate->name); 2090 return FALSE; 2091 } 2092 2093 osec = pupdate->section->output_section; 2094 if (! bfd_set_section_size (obfd, osec, pupdate->size)) 2095 { 2096 bfd_nonfatal_message (NULL, obfd, osec, NULL); 2097 return FALSE; 2098 } 2099 } 2100 } 2101 2102 if (dump_sections != NULL) 2103 { 2104 struct section_add * pdump; 2105 2106 for (pdump = dump_sections; pdump != NULL; pdump = pdump->next) 2107 { 2108 asection * sec; 2109 2110 sec = bfd_get_section_by_name (ibfd, pdump->name); 2111 if (sec == NULL) 2112 { 2113 bfd_nonfatal_message (NULL, ibfd, NULL, 2114 _("can't dump section '%s' - it does not exist"), 2115 pdump->name); 2116 continue; 2117 } 2118 2119 if ((bfd_get_section_flags (ibfd, sec) & SEC_HAS_CONTENTS) == 0) 2120 { 2121 bfd_nonfatal_message (NULL, ibfd, sec, 2122 _("can't dump section - it has no contents")); 2123 continue; 2124 } 2125 2126 bfd_size_type size = bfd_get_section_size (sec); 2127 if (size == 0) 2128 { 2129 bfd_nonfatal_message (NULL, ibfd, sec, 2130 _("can't dump section - it is empty")); 2131 continue; 2132 } 2133 2134 FILE * f; 2135 f = fopen (pdump->filename, FOPEN_WB); 2136 if (f == NULL) 2137 { 2138 bfd_nonfatal_message (pdump->filename, NULL, NULL, 2139 _("could not open section dump file")); 2140 continue; 2141 } 2142 2143 bfd_byte * contents = xmalloc (size); 2144 if (bfd_get_section_contents (ibfd, sec, contents, 0, size)) 2145 { 2146 if (fwrite (contents, 1, size, f) != size) 2147 { 2148 non_fatal (_("error writing section contents to %s (error: %s)"), 2149 pdump->filename, 2150 strerror (errno)); 2151 return FALSE; 2152 } 2153 } 2154 else 2155 bfd_nonfatal_message (NULL, ibfd, sec, 2156 _("could not retrieve section contents")); 2157 2158 fclose (f); 2159 free (contents); 2160 } 2161 } 2162 2163 if (gnu_debuglink_filename != NULL) 2164 { 2165 /* PR 15125: Give a helpful warning message if 2166 the debuglink section already exists, and 2167 allow the rest of the copy to complete. */ 2168 if (bfd_get_section_by_name (obfd, ".gnu_debuglink")) 2169 { 2170 non_fatal (_("%s: debuglink section already exists"), 2171 bfd_get_filename (obfd)); 2172 gnu_debuglink_filename = NULL; 2173 } 2174 else 2175 { 2176 gnu_debuglink_section = bfd_create_gnu_debuglink_section 2177 (obfd, gnu_debuglink_filename); 2178 2179 if (gnu_debuglink_section == NULL) 2180 { 2181 bfd_nonfatal_message (NULL, obfd, NULL, 2182 _("cannot create debug link section `%s'"), 2183 gnu_debuglink_filename); 2184 return FALSE; 2185 } 2186 2187 /* Special processing for PE format files. We 2188 have no way to distinguish PE from COFF here. */ 2189 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour) 2190 { 2191 bfd_vma debuglink_vma; 2192 asection * highest_section; 2193 asection * sec; 2194 2195 /* The PE spec requires that all sections be adjacent and sorted 2196 in ascending order of VMA. It also specifies that debug 2197 sections should be last. This is despite the fact that debug 2198 sections are not loaded into memory and so in theory have no 2199 use for a VMA. 2200 2201 This means that the debuglink section must be given a non-zero 2202 VMA which makes it contiguous with other debug sections. So 2203 walk the current section list, find the section with the 2204 highest VMA and start the debuglink section after that one. */ 2205 for (sec = obfd->sections, highest_section = NULL; 2206 sec != NULL; 2207 sec = sec->next) 2208 if (sec->vma > 0 2209 && (highest_section == NULL 2210 || sec->vma > highest_section->vma)) 2211 highest_section = sec; 2212 2213 if (highest_section) 2214 debuglink_vma = BFD_ALIGN (highest_section->vma 2215 + highest_section->size, 2216 /* FIXME: We ought to be using 2217 COFF_PAGE_SIZE here or maybe 2218 bfd_get_section_alignment() (if it 2219 was set) but since this is for PE 2220 and we know the required alignment 2221 it is easier just to hard code it. */ 2222 0x1000); 2223 else 2224 /* Umm, not sure what to do in this case. */ 2225 debuglink_vma = 0x1000; 2226 2227 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma); 2228 } 2229 } 2230 } 2231 2232 c = bfd_count_sections (obfd); 2233 if (c != 0 2234 && (gap_fill_set || pad_to_set)) 2235 { 2236 asection **set; 2237 2238 /* We must fill in gaps between the sections and/or we must pad 2239 the last section to a specified address. We do this by 2240 grabbing a list of the sections, sorting them by VMA, and 2241 increasing the section sizes as required to fill the gaps. 2242 We write out the gap contents below. */ 2243 2244 osections = (asection **) xmalloc (c * sizeof (asection *)); 2245 set = osections; 2246 bfd_map_over_sections (obfd, get_sections, &set); 2247 2248 qsort (osections, c, sizeof (asection *), compare_section_lma); 2249 2250 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type)); 2251 memset (gaps, 0, c * sizeof (bfd_size_type)); 2252 2253 if (gap_fill_set) 2254 { 2255 for (i = 0; i < c - 1; i++) 2256 { 2257 flagword flags; 2258 bfd_size_type size; 2259 bfd_vma gap_start, gap_stop; 2260 2261 flags = bfd_get_section_flags (obfd, osections[i]); 2262 if ((flags & SEC_HAS_CONTENTS) == 0 2263 || (flags & SEC_LOAD) == 0) 2264 continue; 2265 2266 size = bfd_section_size (obfd, osections[i]); 2267 gap_start = bfd_section_lma (obfd, osections[i]) + size; 2268 gap_stop = bfd_section_lma (obfd, osections[i + 1]); 2269 if (gap_start < gap_stop) 2270 { 2271 if (! bfd_set_section_size (obfd, osections[i], 2272 size + (gap_stop - gap_start))) 2273 { 2274 bfd_nonfatal_message (NULL, obfd, osections[i], 2275 _("Can't fill gap after section")); 2276 status = 1; 2277 break; 2278 } 2279 gaps[i] = gap_stop - gap_start; 2280 if (max_gap < gap_stop - gap_start) 2281 max_gap = gap_stop - gap_start; 2282 } 2283 } 2284 } 2285 2286 if (pad_to_set) 2287 { 2288 bfd_vma lma; 2289 bfd_size_type size; 2290 2291 lma = bfd_section_lma (obfd, osections[c - 1]); 2292 size = bfd_section_size (obfd, osections[c - 1]); 2293 if (lma + size < pad_to) 2294 { 2295 if (! bfd_set_section_size (obfd, osections[c - 1], 2296 pad_to - lma)) 2297 { 2298 bfd_nonfatal_message (NULL, obfd, osections[c - 1], 2299 _("can't add padding")); 2300 status = 1; 2301 } 2302 else 2303 { 2304 gaps[c - 1] = pad_to - (lma + size); 2305 if (max_gap < pad_to - (lma + size)) 2306 max_gap = pad_to - (lma + size); 2307 } 2308 } 2309 } 2310 } 2311 2312 /* Symbol filtering must happen after the output sections 2313 have been created, but before their contents are set. */ 2314 dhandle = NULL; 2315 if (convert_debugging) 2316 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE); 2317 2318 if (strip_symbols == STRIP_DEBUG 2319 || strip_symbols == STRIP_ALL 2320 || strip_symbols == STRIP_UNNEEDED 2321 || strip_symbols == STRIP_NONDEBUG 2322 || strip_symbols == STRIP_DWO 2323 || strip_symbols == STRIP_NONDWO 2324 || discard_locals != LOCALS_UNDEF 2325 || localize_hidden 2326 || htab_elements (strip_specific_htab) != 0 2327 || htab_elements (keep_specific_htab) != 0 2328 || htab_elements (localize_specific_htab) != 0 2329 || htab_elements (globalize_specific_htab) != 0 2330 || htab_elements (keepglobal_specific_htab) != 0 2331 || htab_elements (weaken_specific_htab) != 0 2332 || prefix_symbols_string 2333 || sections_removed 2334 || sections_copied 2335 || convert_debugging 2336 || change_leading_char 2337 || remove_leading_char 2338 || redefine_sym_list 2339 || weaken 2340 || add_symbols) 2341 { 2342 /* Mark symbols used in output relocations so that they 2343 are kept, even if they are local labels or static symbols. 2344 2345 Note we iterate over the input sections examining their 2346 relocations since the relocations for the output sections 2347 haven't been set yet. mark_symbols_used_in_relocations will 2348 ignore input sections which have no corresponding output 2349 section. */ 2350 if (strip_symbols != STRIP_ALL) 2351 bfd_map_over_sections (ibfd, 2352 mark_symbols_used_in_relocations, 2353 isympp); 2354 osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *)); 2355 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount); 2356 } 2357 2358 if (convert_debugging && dhandle != NULL) 2359 { 2360 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp)) 2361 { 2362 status = 1; 2363 return FALSE; 2364 } 2365 } 2366 2367 bfd_set_symtab (obfd, osympp, symcount); 2368 2369 /* This has to happen before section positions are set. */ 2370 bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd); 2371 2372 /* This has to happen after the symbol table has been set. */ 2373 bfd_map_over_sections (ibfd, copy_section, obfd); 2374 2375 if (add_sections != NULL) 2376 { 2377 struct section_add *padd; 2378 2379 for (padd = add_sections; padd != NULL; padd = padd->next) 2380 { 2381 if (! bfd_set_section_contents (obfd, padd->section, padd->contents, 2382 0, padd->size)) 2383 { 2384 bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 2385 return FALSE; 2386 } 2387 } 2388 } 2389 2390 if (update_sections != NULL) 2391 { 2392 struct section_add *pupdate; 2393 2394 for (pupdate = update_sections; 2395 pupdate != NULL; 2396 pupdate = pupdate->next) 2397 { 2398 asection *osec; 2399 2400 osec = pupdate->section->output_section; 2401 if (! bfd_set_section_contents (obfd, osec, pupdate->contents, 2402 0, pupdate->size)) 2403 { 2404 bfd_nonfatal_message (NULL, obfd, osec, NULL); 2405 return FALSE; 2406 } 2407 } 2408 } 2409 2410 if (gnu_debuglink_filename != NULL) 2411 { 2412 if (! bfd_fill_in_gnu_debuglink_section 2413 (obfd, gnu_debuglink_section, gnu_debuglink_filename)) 2414 { 2415 bfd_nonfatal_message (NULL, obfd, NULL, 2416 _("cannot fill debug link section `%s'"), 2417 gnu_debuglink_filename); 2418 return FALSE; 2419 } 2420 } 2421 2422 if (gap_fill_set || pad_to_set) 2423 { 2424 bfd_byte *buf; 2425 2426 /* Fill in the gaps. */ 2427 if (max_gap > 8192) 2428 max_gap = 8192; 2429 buf = (bfd_byte *) xmalloc (max_gap); 2430 memset (buf, gap_fill, max_gap); 2431 2432 c = bfd_count_sections (obfd); 2433 for (i = 0; i < c; i++) 2434 { 2435 if (gaps[i] != 0) 2436 { 2437 bfd_size_type left; 2438 file_ptr off; 2439 2440 left = gaps[i]; 2441 off = bfd_section_size (obfd, osections[i]) - left; 2442 2443 while (left > 0) 2444 { 2445 bfd_size_type now; 2446 2447 if (left > 8192) 2448 now = 8192; 2449 else 2450 now = left; 2451 2452 if (! bfd_set_section_contents (obfd, osections[i], buf, 2453 off, now)) 2454 { 2455 bfd_nonfatal_message (NULL, obfd, osections[i], NULL); 2456 free (buf); 2457 return FALSE; 2458 } 2459 2460 left -= now; 2461 off += now; 2462 } 2463 } 2464 } 2465 free (buf); 2466 } 2467 2468 /* Allow the BFD backend to copy any private data it understands 2469 from the input BFD to the output BFD. This is done last to 2470 permit the routine to look at the filtered symbol table, which is 2471 important for the ECOFF code at least. */ 2472 if (! bfd_copy_private_bfd_data (ibfd, obfd)) 2473 { 2474 bfd_nonfatal_message (NULL, obfd, NULL, 2475 _("error copying private BFD data")); 2476 return FALSE; 2477 } 2478 2479 /* Switch to the alternate machine code. We have to do this at the 2480 very end, because we only initialize the header when we create 2481 the first section. */ 2482 if (use_alt_mach_code != 0) 2483 { 2484 if (! bfd_alt_mach_code (obfd, use_alt_mach_code)) 2485 { 2486 non_fatal (_("this target does not support %lu alternative machine codes"), 2487 use_alt_mach_code); 2488 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour) 2489 { 2490 non_fatal (_("treating that number as an absolute e_machine value instead")); 2491 elf_elfheader (obfd)->e_machine = use_alt_mach_code; 2492 } 2493 else 2494 non_fatal (_("ignoring the alternative value")); 2495 } 2496 } 2497 2498 return TRUE; 2499} 2500 2501/* Read each archive element in turn from IBFD, copy the 2502 contents to temp file, and keep the temp file handle. 2503 If 'force_output_target' is TRUE then make sure that 2504 all elements in the new archive are of the type 2505 'output_target'. */ 2506 2507static void 2508copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, 2509 bfd_boolean force_output_target, 2510 const bfd_arch_info_type *input_arch) 2511{ 2512 struct name_list 2513 { 2514 struct name_list *next; 2515 const char *name; 2516 bfd *obfd; 2517 } *list, *l; 2518 bfd **ptr = &obfd->archive_head; 2519 bfd *this_element; 2520 char *dir; 2521 const char *filename; 2522 2523 /* Make a temp directory to hold the contents. */ 2524 dir = make_tempdir (bfd_get_filename (obfd)); 2525 if (dir == NULL) 2526 fatal (_("cannot create tempdir for archive copying (error: %s)"), 2527 strerror (errno)); 2528 2529 if (strip_symbols == STRIP_ALL) 2530 obfd->has_armap = FALSE; 2531 else 2532 obfd->has_armap = ibfd->has_armap; 2533 obfd->is_thin_archive = ibfd->is_thin_archive; 2534 2535 if (deterministic) 2536 obfd->flags |= BFD_DETERMINISTIC_OUTPUT; 2537 2538 list = NULL; 2539 2540 this_element = bfd_openr_next_archived_file (ibfd, NULL); 2541 2542 if (!bfd_set_format (obfd, bfd_get_format (ibfd))) 2543 { 2544 status = 1; 2545 bfd_nonfatal_message (NULL, obfd, NULL, NULL); 2546 goto cleanup_and_exit; 2547 } 2548 2549 while (!status && this_element != NULL) 2550 { 2551 char *output_name; 2552 bfd *output_bfd; 2553 bfd *last_element; 2554 struct stat buf; 2555 int stat_status = 0; 2556 bfd_boolean del = TRUE; 2557 bfd_boolean ok_object; 2558 2559 /* PR binutils/17533: Do not allow directory traversal 2560 outside of the current directory tree by archive members. */ 2561 if (! is_valid_archive_path (bfd_get_filename (this_element))) 2562 { 2563 non_fatal (_("illegal pathname found in archive member: %s"), 2564 bfd_get_filename (this_element)); 2565 status = 1; 2566 goto cleanup_and_exit; 2567 } 2568 2569 /* Create an output file for this member. */ 2570 output_name = concat (dir, "/", 2571 bfd_get_filename (this_element), (char *) 0); 2572 2573 /* If the file already exists, make another temp dir. */ 2574 if (stat (output_name, &buf) >= 0) 2575 { 2576 output_name = make_tempdir (output_name); 2577 if (output_name == NULL) 2578 { 2579 non_fatal (_("cannot create tempdir for archive copying (error: %s)"), 2580 strerror (errno)); 2581 status = 1; 2582 goto cleanup_and_exit; 2583 } 2584 2585 l = (struct name_list *) xmalloc (sizeof (struct name_list)); 2586 l->name = output_name; 2587 l->next = list; 2588 l->obfd = NULL; 2589 list = l; 2590 output_name = concat (output_name, "/", 2591 bfd_get_filename (this_element), (char *) 0); 2592 } 2593 2594 if (preserve_dates) 2595 { 2596 stat_status = bfd_stat_arch_elt (this_element, &buf); 2597 2598 if (stat_status != 0) 2599 non_fatal (_("internal stat error on %s"), 2600 bfd_get_filename (this_element)); 2601 } 2602 2603 l = (struct name_list *) xmalloc (sizeof (struct name_list)); 2604 l->name = output_name; 2605 l->next = list; 2606 l->obfd = NULL; 2607 list = l; 2608 2609 ok_object = bfd_check_format (this_element, bfd_object); 2610 if (!ok_object) 2611 bfd_nonfatal_message (NULL, this_element, NULL, 2612 _("Unable to recognise the format of file")); 2613 2614 /* PR binutils/3110: Cope with archives 2615 containing multiple target types. */ 2616 if (force_output_target || !ok_object) 2617 output_bfd = bfd_openw (output_name, output_target); 2618 else 2619 output_bfd = bfd_openw (output_name, bfd_get_target (this_element)); 2620 2621 if (output_bfd == NULL) 2622 { 2623 bfd_nonfatal_message (output_name, NULL, NULL, NULL); 2624 status = 1; 2625 goto cleanup_and_exit; 2626 } 2627 2628 if (ok_object) 2629 { 2630 del = !copy_object (this_element, output_bfd, input_arch); 2631 2632 if (del && bfd_get_arch (this_element) == bfd_arch_unknown) 2633 /* Try again as an unknown object file. */ 2634 ok_object = FALSE; 2635 else if (!bfd_close (output_bfd)) 2636 { 2637 bfd_nonfatal_message (output_name, NULL, NULL, NULL); 2638 /* Error in new object file. Don't change archive. */ 2639 status = 1; 2640 } 2641 } 2642 2643 if (!ok_object) 2644 { 2645 del = !copy_unknown_object (this_element, output_bfd); 2646 if (!bfd_close_all_done (output_bfd)) 2647 { 2648 bfd_nonfatal_message (output_name, NULL, NULL, NULL); 2649 /* Error in new object file. Don't change archive. */ 2650 status = 1; 2651 } 2652 } 2653 2654 if (del) 2655 { 2656 unlink (output_name); 2657 status = 1; 2658 } 2659 else 2660 { 2661 if (preserve_dates && stat_status == 0) 2662 set_times (output_name, &buf); 2663 2664 /* Open the newly output file and attach to our list. */ 2665 output_bfd = bfd_openr (output_name, output_target); 2666 2667 l->obfd = output_bfd; 2668 2669 *ptr = output_bfd; 2670 ptr = &output_bfd->archive_next; 2671 2672 last_element = this_element; 2673 2674 this_element = bfd_openr_next_archived_file (ibfd, last_element); 2675 2676 bfd_close (last_element); 2677 } 2678 } 2679 *ptr = NULL; 2680 2681 filename = bfd_get_filename (obfd); 2682 if (!bfd_close (obfd)) 2683 { 2684 status = 1; 2685 bfd_nonfatal_message (filename, NULL, NULL, NULL); 2686 } 2687 2688 filename = bfd_get_filename (ibfd); 2689 if (!bfd_close (ibfd)) 2690 { 2691 status = 1; 2692 bfd_nonfatal_message (filename, NULL, NULL, NULL); 2693 } 2694 2695 cleanup_and_exit: 2696 /* Delete all the files that we opened. */ 2697 for (l = list; l != NULL; l = l->next) 2698 { 2699 if (l->obfd == NULL) 2700 rmdir (l->name); 2701 else 2702 { 2703 bfd_close (l->obfd); 2704 unlink (l->name); 2705 } 2706 } 2707 2708 rmdir (dir); 2709} 2710 2711static void 2712set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style) 2713{ 2714 /* This is only relevant to Coff targets. */ 2715 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour) 2716 { 2717 if (style == KEEP 2718 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour) 2719 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE; 2720 bfd_coff_set_long_section_names (output_bfd, style != DISABLE); 2721 } 2722} 2723 2724/* The top-level control. */ 2725 2726static void 2727copy_file (const char *input_filename, const char *output_filename, 2728 const char *input_target, const char *output_target, 2729 const bfd_arch_info_type *input_arch) 2730{ 2731 bfd *ibfd; 2732 char **obj_matching; 2733 char **core_matching; 2734 off_t size = get_file_size (input_filename); 2735 2736 if (size < 1) 2737 { 2738 if (size == 0) 2739 non_fatal (_("error: the input file '%s' is empty"), 2740 input_filename); 2741 status = 1; 2742 return; 2743 } 2744 2745 /* To allow us to do "strip *" without dying on the first 2746 non-object file, failures are nonfatal. */ 2747 ibfd = bfd_openr (input_filename, input_target); 2748 if (ibfd == NULL) 2749 { 2750 bfd_nonfatal_message (input_filename, NULL, NULL, NULL); 2751 status = 1; 2752 return; 2753 } 2754 2755 switch (do_debug_sections) 2756 { 2757 case compress: 2758 case compress_zlib: 2759 case compress_gnu_zlib: 2760 case compress_gabi_zlib: 2761 ibfd->flags |= BFD_COMPRESS; 2762 /* Don't check if input is ELF here since this information is 2763 only available after bfd_check_format_matches is called. */ 2764 if (do_debug_sections != compress_gnu_zlib) 2765 ibfd->flags |= BFD_COMPRESS_GABI; 2766 break; 2767 case decompress: 2768 ibfd->flags |= BFD_DECOMPRESS; 2769 break; 2770 default: 2771 break; 2772 } 2773 2774 if (bfd_check_format (ibfd, bfd_archive)) 2775 { 2776 bfd_boolean force_output_target; 2777 bfd *obfd; 2778 2779 /* bfd_get_target does not return the correct value until 2780 bfd_check_format succeeds. */ 2781 if (output_target == NULL) 2782 { 2783 output_target = bfd_get_target (ibfd); 2784 force_output_target = FALSE; 2785 } 2786 else 2787 force_output_target = TRUE; 2788 2789 obfd = bfd_openw (output_filename, output_target); 2790 if (obfd == NULL) 2791 { 2792 bfd_nonfatal_message (output_filename, NULL, NULL, NULL); 2793 status = 1; 2794 return; 2795 } 2796 /* This is a no-op on non-Coff targets. */ 2797 set_long_section_mode (obfd, ibfd, long_section_names); 2798 2799 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch); 2800 } 2801 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching)) 2802 { 2803 bfd *obfd; 2804 do_copy: 2805 2806 /* bfd_get_target does not return the correct value until 2807 bfd_check_format succeeds. */ 2808 if (output_target == NULL) 2809 output_target = bfd_get_target (ibfd); 2810 2811 obfd = bfd_openw (output_filename, output_target); 2812 if (obfd == NULL) 2813 { 2814 bfd_nonfatal_message (output_filename, NULL, NULL, NULL); 2815 status = 1; 2816 return; 2817 } 2818 /* This is a no-op on non-Coff targets. */ 2819 set_long_section_mode (obfd, ibfd, long_section_names); 2820 2821 if (! copy_object (ibfd, obfd, input_arch)) 2822 status = 1; 2823 2824 /* PR 17512: file: 0f15796a. 2825 If the file could not be copied it may not be in a writeable 2826 state. So use bfd_close_all_done to avoid the possibility of 2827 writing uninitialised data into the file. */ 2828 if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd))) 2829 { 2830 status = 1; 2831 bfd_nonfatal_message (output_filename, NULL, NULL, NULL); 2832 return; 2833 } 2834 2835 if (!bfd_close (ibfd)) 2836 { 2837 status = 1; 2838 bfd_nonfatal_message (input_filename, NULL, NULL, NULL); 2839 return; 2840 } 2841 } 2842 else 2843 { 2844 bfd_error_type obj_error = bfd_get_error (); 2845 bfd_error_type core_error; 2846 2847 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching)) 2848 { 2849 /* This probably can't happen.. */ 2850 if (obj_error == bfd_error_file_ambiguously_recognized) 2851 free (obj_matching); 2852 goto do_copy; 2853 } 2854 2855 core_error = bfd_get_error (); 2856 /* Report the object error in preference to the core error. */ 2857 if (obj_error != core_error) 2858 bfd_set_error (obj_error); 2859 2860 bfd_nonfatal_message (input_filename, NULL, NULL, NULL); 2861 2862 if (obj_error == bfd_error_file_ambiguously_recognized) 2863 { 2864 list_matching_formats (obj_matching); 2865 free (obj_matching); 2866 } 2867 if (core_error == bfd_error_file_ambiguously_recognized) 2868 { 2869 list_matching_formats (core_matching); 2870 free (core_matching); 2871 } 2872 2873 status = 1; 2874 } 2875} 2876 2877/* Add a name to the section renaming list. */ 2878 2879static void 2880add_section_rename (const char * old_name, const char * new_name, 2881 flagword flags) 2882{ 2883 section_rename * srename; 2884 2885 /* Check for conflicts first. */ 2886 for (srename = section_rename_list; srename != NULL; srename = srename->next) 2887 if (strcmp (srename->old_name, old_name) == 0) 2888 { 2889 /* Silently ignore duplicate definitions. */ 2890 if (strcmp (srename->new_name, new_name) == 0 2891 && srename->flags == flags) 2892 return; 2893 2894 fatal (_("Multiple renames of section %s"), old_name); 2895 } 2896 2897 srename = (section_rename *) xmalloc (sizeof (* srename)); 2898 2899 srename->old_name = old_name; 2900 srename->new_name = new_name; 2901 srename->flags = flags; 2902 srename->next = section_rename_list; 2903 2904 section_rename_list = srename; 2905} 2906 2907/* Check the section rename list for a new name of the input section 2908 ISECTION. Return the new name if one is found. 2909 Also set RETURNED_FLAGS to the flags to be used for this section. */ 2910 2911static const char * 2912find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection, 2913 flagword * returned_flags) 2914{ 2915 const char * old_name = bfd_section_name (ibfd, isection); 2916 section_rename * srename; 2917 2918 /* Default to using the flags of the input section. */ 2919 * returned_flags = bfd_get_section_flags (ibfd, isection); 2920 2921 for (srename = section_rename_list; srename != NULL; srename = srename->next) 2922 if (strcmp (srename->old_name, old_name) == 0) 2923 { 2924 if (srename->flags != (flagword) -1) 2925 * returned_flags = srename->flags; 2926 2927 return srename->new_name; 2928 } 2929 2930 return old_name; 2931} 2932 2933/* Once each of the sections is copied, we may still need to do some 2934 finalization work for private section headers. Do that here. */ 2935 2936static void 2937setup_bfd_headers (bfd *ibfd, bfd *obfd) 2938{ 2939 /* Allow the BFD backend to copy any private data it understands 2940 from the input section to the output section. */ 2941 if (! bfd_copy_private_header_data (ibfd, obfd)) 2942 { 2943 status = 1; 2944 bfd_nonfatal_message (NULL, ibfd, NULL, 2945 _("error in private header data")); 2946 return; 2947 } 2948 2949 /* All went well. */ 2950 return; 2951} 2952 2953/* Create a section in OBFD with the same 2954 name and attributes as ISECTION in IBFD. */ 2955 2956static void 2957setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg) 2958{ 2959 bfd *obfd = (bfd *) obfdarg; 2960 struct section_list *p; 2961 sec_ptr osection; 2962 bfd_size_type size; 2963 bfd_vma vma; 2964 bfd_vma lma; 2965 flagword flags; 2966 const char *err; 2967 const char * name; 2968 char *prefix = NULL; 2969 bfd_boolean make_nobits; 2970 2971 if (is_strip_section (ibfd, isection)) 2972 return; 2973 2974 /* Get the, possibly new, name of the output section. */ 2975 name = find_section_rename (ibfd, isection, & flags); 2976 2977 /* Prefix sections. */ 2978 if ((prefix_alloc_sections_string) 2979 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC)) 2980 prefix = prefix_alloc_sections_string; 2981 else if (prefix_sections_string) 2982 prefix = prefix_sections_string; 2983 2984 if (prefix) 2985 { 2986 char *n; 2987 2988 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1); 2989 strcpy (n, prefix); 2990 strcat (n, name); 2991 name = n; 2992 } 2993 2994 make_nobits = FALSE; 2995 2996 p = find_section_list (bfd_section_name (ibfd, isection), FALSE, 2997 SECTION_CONTEXT_SET_FLAGS); 2998 if (p != NULL) 2999 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC)); 3000 else if (strip_symbols == STRIP_NONDEBUG 3001 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0 3002 && !is_nondebug_keep_contents_section (ibfd, isection)) 3003 { 3004 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP); 3005 if (obfd->xvec->flavour == bfd_target_elf_flavour) 3006 { 3007 make_nobits = TRUE; 3008 3009 /* Twiddle the input section flags so that it seems to 3010 elf.c:copy_private_bfd_data that section flags have not 3011 changed between input and output sections. This hack 3012 prevents wholesale rewriting of the program headers. */ 3013 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP); 3014 } 3015 } 3016 3017 osection = bfd_make_section_anyway_with_flags (obfd, name, flags); 3018 3019 if (osection == NULL) 3020 { 3021 err = _("failed to create output section"); 3022 goto loser; 3023 } 3024 3025 if (make_nobits) 3026 elf_section_type (osection) = SHT_NOBITS; 3027 3028 size = bfd_section_size (ibfd, isection); 3029 size = bfd_convert_section_size (ibfd, isection, obfd, size); 3030 if (copy_byte >= 0) 3031 size = (size + interleave - 1) / interleave * copy_width; 3032 else if (extract_symbol) 3033 size = 0; 3034 if (! bfd_set_section_size (obfd, osection, size)) 3035 { 3036 err = _("failed to set size"); 3037 goto loser; 3038 } 3039 3040 vma = bfd_section_vma (ibfd, isection); 3041 p = find_section_list (bfd_section_name (ibfd, isection), FALSE, 3042 SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA); 3043 if (p != NULL) 3044 { 3045 if (p->context & SECTION_CONTEXT_SET_VMA) 3046 vma = p->vma_val; 3047 else 3048 vma += p->vma_val; 3049 } 3050 else 3051 vma += change_section_address; 3052 3053 if (! bfd_set_section_vma (obfd, osection, vma)) 3054 { 3055 err = _("failed to set vma"); 3056 goto loser; 3057 } 3058 3059 lma = isection->lma; 3060 p = find_section_list (bfd_section_name (ibfd, isection), FALSE, 3061 SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA); 3062 if (p != NULL) 3063 { 3064 if (p->context & SECTION_CONTEXT_ALTER_LMA) 3065 lma += p->lma_val; 3066 else 3067 lma = p->lma_val; 3068 } 3069 else 3070 lma += change_section_address; 3071 3072 osection->lma = lma; 3073 3074 /* FIXME: This is probably not enough. If we change the LMA we 3075 may have to recompute the header for the file as well. */ 3076 if (!bfd_set_section_alignment (obfd, 3077 osection, 3078 bfd_section_alignment (ibfd, isection))) 3079 { 3080 err = _("failed to set alignment"); 3081 goto loser; 3082 } 3083 3084 /* Copy merge entity size. */ 3085 osection->entsize = isection->entsize; 3086 3087 /* Copy compress status. */ 3088 osection->compress_status = isection->compress_status; 3089 3090 /* This used to be mangle_section; we do here to avoid using 3091 bfd_get_section_by_name since some formats allow multiple 3092 sections with the same name. */ 3093 isection->output_section = osection; 3094 isection->output_offset = 0; 3095 3096 if ((isection->flags & SEC_GROUP) != 0) 3097 { 3098 asymbol *gsym = group_signature (isection); 3099 3100 if (gsym != NULL) 3101 { 3102 gsym->flags |= BSF_KEEP; 3103 if (ibfd->xvec->flavour == bfd_target_elf_flavour) 3104 elf_group_id (isection) = gsym; 3105 } 3106 } 3107 3108 /* Allow the BFD backend to copy any private data it understands 3109 from the input section to the output section. */ 3110 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection)) 3111 { 3112 err = _("failed to copy private data"); 3113 goto loser; 3114 } 3115 3116 /* All went well. */ 3117 return; 3118 3119loser: 3120 status = 1; 3121 bfd_nonfatal_message (NULL, obfd, osection, err); 3122} 3123 3124/* Return TRUE if input section ISECTION should be skipped. */ 3125 3126static bfd_boolean 3127skip_section (bfd *ibfd, sec_ptr isection) 3128{ 3129 sec_ptr osection; 3130 bfd_size_type size; 3131 flagword flags; 3132 3133 /* If we have already failed earlier on, 3134 do not keep on generating complaints now. */ 3135 if (status != 0) 3136 return TRUE; 3137 3138 if (extract_symbol) 3139 return TRUE; 3140 3141 if (is_strip_section (ibfd, isection)) 3142 return TRUE; 3143 3144 if (is_update_section (ibfd, isection)) 3145 return TRUE; 3146 3147 flags = bfd_get_section_flags (ibfd, isection); 3148 if ((flags & SEC_GROUP) != 0) 3149 return TRUE; 3150 3151 osection = isection->output_section; 3152 size = bfd_get_section_size (isection); 3153 3154 if (size == 0 || osection == 0) 3155 return TRUE; 3156 3157 return FALSE; 3158} 3159 3160/* Copy relocations in input section ISECTION of IBFD to an output 3161 section with the same name in OBFDARG. If stripping then don't 3162 copy any relocation info. */ 3163 3164static void 3165copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg) 3166{ 3167 bfd *obfd = (bfd *) obfdarg; 3168 long relsize; 3169 arelent **relpp; 3170 long relcount; 3171 sec_ptr osection; 3172 3173 if (skip_section (ibfd, isection)) 3174 return; 3175 3176 osection = isection->output_section; 3177 3178 /* Core files and DWO files do not need to be relocated. */ 3179 if (bfd_get_format (obfd) == bfd_core || strip_symbols == STRIP_NONDWO) 3180 relsize = 0; 3181 else 3182 { 3183 relsize = bfd_get_reloc_upper_bound (ibfd, isection); 3184 3185 if (relsize < 0) 3186 { 3187 /* Do not complain if the target does not support relocations. */ 3188 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation) 3189 relsize = 0; 3190 else 3191 { 3192 status = 1; 3193 bfd_nonfatal_message (NULL, ibfd, isection, NULL); 3194 return; 3195 } 3196 } 3197 } 3198 3199 if (relsize == 0) 3200 { 3201 bfd_set_reloc (obfd, osection, NULL, 0); 3202 osection->flags &= ~SEC_RELOC; 3203 } 3204 else 3205 { 3206 relpp = (arelent **) xmalloc (relsize); 3207 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); 3208 if (relcount < 0) 3209 { 3210 status = 1; 3211 bfd_nonfatal_message (NULL, ibfd, isection, 3212 _("relocation count is negative")); 3213 return; 3214 } 3215 3216 if (strip_symbols == STRIP_ALL) 3217 { 3218 /* Remove relocations which are not in 3219 keep_strip_specific_list. */ 3220 arelent **temp_relpp; 3221 long temp_relcount = 0; 3222 long i; 3223 3224 temp_relpp = (arelent **) xmalloc (relsize); 3225 for (i = 0; i < relcount; i++) 3226 { 3227 /* PR 17512: file: 9e907e0c. */ 3228 if (relpp[i]->sym_ptr_ptr) 3229 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr), 3230 keep_specific_htab)) 3231 temp_relpp [temp_relcount++] = relpp [i]; 3232 } 3233 relcount = temp_relcount; 3234 free (relpp); 3235 relpp = temp_relpp; 3236 } 3237 3238 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount); 3239 if (relcount == 0) 3240 { 3241 osection->flags &= ~SEC_RELOC; 3242 free (relpp); 3243 } 3244 } 3245} 3246 3247/* Copy the data of input section ISECTION of IBFD 3248 to an output section with the same name in OBFD. */ 3249 3250static void 3251copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg) 3252{ 3253 bfd *obfd = (bfd *) obfdarg; 3254 struct section_list *p; 3255 sec_ptr osection; 3256 bfd_size_type size; 3257 3258 if (skip_section (ibfd, isection)) 3259 return; 3260 3261 osection = isection->output_section; 3262 /* The output SHF_COMPRESSED section size is different from input if 3263 ELF classes of input and output aren't the same. We can't use 3264 the output section size since --interleave will shrink the output 3265 section. Size will be updated if the section is converted. */ 3266 size = bfd_get_section_size (isection); 3267 3268 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS 3269 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS) 3270 { 3271 bfd_byte *memhunk = NULL; 3272 3273 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk) 3274 || !bfd_convert_section_contents (ibfd, isection, obfd, 3275 &memhunk, &size)) 3276 { 3277 status = 1; 3278 bfd_nonfatal_message (NULL, ibfd, isection, NULL); 3279 return; 3280 } 3281 3282 if (reverse_bytes) 3283 { 3284 /* We don't handle leftover bytes (too many possible behaviors, 3285 and we don't know what the user wants). The section length 3286 must be a multiple of the number of bytes to swap. */ 3287 if ((size % reverse_bytes) == 0) 3288 { 3289 unsigned long i, j; 3290 bfd_byte b; 3291 3292 for (i = 0; i < size; i += reverse_bytes) 3293 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++) 3294 { 3295 bfd_byte *m = (bfd_byte *) memhunk; 3296 3297 b = m[i + j]; 3298 m[i + j] = m[(i + reverse_bytes) - (j + 1)]; 3299 m[(i + reverse_bytes) - (j + 1)] = b; 3300 } 3301 } 3302 else 3303 /* User must pad the section up in order to do this. */ 3304 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"), 3305 bfd_section_name (ibfd, isection), reverse_bytes); 3306 } 3307 3308 if (copy_byte >= 0) 3309 { 3310 /* Keep only every `copy_byte'th byte in MEMHUNK. */ 3311 char *from = (char *) memhunk + copy_byte; 3312 char *to = (char *) memhunk; 3313 char *end = (char *) memhunk + size; 3314 int i; 3315 3316 for (; from < end; from += interleave) 3317 for (i = 0; i < copy_width; i++) 3318 { 3319 if (&from[i] >= end) 3320 break; 3321 *to++ = from[i]; 3322 } 3323 3324 size = (size + interleave - 1 - copy_byte) / interleave * copy_width; 3325 osection->lma /= interleave; 3326 } 3327 3328 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size)) 3329 { 3330 status = 1; 3331 bfd_nonfatal_message (NULL, obfd, osection, NULL); 3332 return; 3333 } 3334 free (memhunk); 3335 } 3336 else if ((p = find_section_list (bfd_get_section_name (ibfd, isection), 3337 FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL 3338 && (p->flags & SEC_HAS_CONTENTS) != 0) 3339 { 3340 void *memhunk = xmalloc (size); 3341 3342 /* We don't permit the user to turn off the SEC_HAS_CONTENTS 3343 flag--they can just remove the section entirely and add it 3344 back again. However, we do permit them to turn on the 3345 SEC_HAS_CONTENTS flag, and take it to mean that the section 3346 contents should be zeroed out. */ 3347 3348 memset (memhunk, 0, size); 3349 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size)) 3350 { 3351 status = 1; 3352 bfd_nonfatal_message (NULL, obfd, osection, NULL); 3353 return; 3354 } 3355 free (memhunk); 3356 } 3357} 3358 3359/* Get all the sections. This is used when --gap-fill or --pad-to is 3360 used. */ 3361 3362static void 3363get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg) 3364{ 3365 asection ***secppp = (asection ***) secppparg; 3366 3367 **secppp = osection; 3368 ++(*secppp); 3369} 3370 3371/* Sort sections by VMA. This is called via qsort, and is used when 3372 --gap-fill or --pad-to is used. We force non loadable or empty 3373 sections to the front, where they are easier to ignore. */ 3374 3375static int 3376compare_section_lma (const void *arg1, const void *arg2) 3377{ 3378 const asection *const *sec1 = (const asection * const *) arg1; 3379 const asection *const *sec2 = (const asection * const *) arg2; 3380 flagword flags1, flags2; 3381 3382 /* Sort non loadable sections to the front. */ 3383 flags1 = (*sec1)->flags; 3384 flags2 = (*sec2)->flags; 3385 if ((flags1 & SEC_HAS_CONTENTS) == 0 3386 || (flags1 & SEC_LOAD) == 0) 3387 { 3388 if ((flags2 & SEC_HAS_CONTENTS) != 0 3389 && (flags2 & SEC_LOAD) != 0) 3390 return -1; 3391 } 3392 else 3393 { 3394 if ((flags2 & SEC_HAS_CONTENTS) == 0 3395 || (flags2 & SEC_LOAD) == 0) 3396 return 1; 3397 } 3398 3399 /* Sort sections by LMA. */ 3400 if ((*sec1)->lma > (*sec2)->lma) 3401 return 1; 3402 else if ((*sec1)->lma < (*sec2)->lma) 3403 return -1; 3404 3405 /* Sort sections with the same LMA by size. */ 3406 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2)) 3407 return 1; 3408 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2)) 3409 return -1; 3410 3411 return 0; 3412} 3413 3414/* Mark all the symbols which will be used in output relocations with 3415 the BSF_KEEP flag so that those symbols will not be stripped. 3416 3417 Ignore relocations which will not appear in the output file. */ 3418 3419static void 3420mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg) 3421{ 3422 asymbol **symbols = (asymbol **) symbolsarg; 3423 long relsize; 3424 arelent **relpp; 3425 long relcount, i; 3426 3427 /* Ignore an input section with no corresponding output section. */ 3428 if (isection->output_section == NULL) 3429 return; 3430 3431 relsize = bfd_get_reloc_upper_bound (ibfd, isection); 3432 if (relsize < 0) 3433 { 3434 /* Do not complain if the target does not support relocations. */ 3435 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation) 3436 return; 3437 bfd_fatal (bfd_get_filename (ibfd)); 3438 } 3439 3440 if (relsize == 0) 3441 return; 3442 3443 relpp = (arelent **) xmalloc (relsize); 3444 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols); 3445 if (relcount < 0) 3446 bfd_fatal (bfd_get_filename (ibfd)); 3447 3448 /* Examine each symbol used in a relocation. If it's not one of the 3449 special bfd section symbols, then mark it with BSF_KEEP. */ 3450 for (i = 0; i < relcount; i++) 3451 { 3452 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol 3453 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol 3454 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol) 3455 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP; 3456 } 3457 3458 if (relpp != NULL) 3459 free (relpp); 3460} 3461 3462/* Write out debugging information. */ 3463 3464static bfd_boolean 3465write_debugging_info (bfd *obfd, void *dhandle, 3466 long *symcountp ATTRIBUTE_UNUSED, 3467 asymbol ***symppp ATTRIBUTE_UNUSED) 3468{ 3469 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour) 3470 return write_ieee_debugging_info (obfd, dhandle); 3471 3472 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour 3473 || bfd_get_flavour (obfd) == bfd_target_elf_flavour) 3474 { 3475 bfd_byte *syms, *strings; 3476 bfd_size_type symsize, stringsize; 3477 asection *stabsec, *stabstrsec; 3478 flagword flags; 3479 3480 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms, 3481 &symsize, &strings, 3482 &stringsize)) 3483 return FALSE; 3484 3485 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING; 3486 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags); 3487 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags); 3488 if (stabsec == NULL 3489 || stabstrsec == NULL 3490 || ! bfd_set_section_size (obfd, stabsec, symsize) 3491 || ! bfd_set_section_size (obfd, stabstrsec, stringsize) 3492 || ! bfd_set_section_alignment (obfd, stabsec, 2) 3493 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)) 3494 { 3495 bfd_nonfatal_message (NULL, obfd, NULL, 3496 _("can't create debugging section")); 3497 return FALSE; 3498 } 3499 3500 /* We can get away with setting the section contents now because 3501 the next thing the caller is going to do is copy over the 3502 real sections. We may someday have to split the contents 3503 setting out of this function. */ 3504 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize) 3505 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0, 3506 stringsize)) 3507 { 3508 bfd_nonfatal_message (NULL, obfd, NULL, 3509 _("can't set debugging section contents")); 3510 return FALSE; 3511 } 3512 3513 return TRUE; 3514 } 3515 3516 bfd_nonfatal_message (NULL, obfd, NULL, 3517 _("don't know how to write debugging information for %s"), 3518 bfd_get_target (obfd)); 3519 return FALSE; 3520} 3521 3522/* If neither -D nor -U was specified explicitly, 3523 then use the configured default. */ 3524static void 3525default_deterministic (void) 3526{ 3527 if (deterministic < 0) 3528 deterministic = DEFAULT_AR_DETERMINISTIC; 3529} 3530 3531static int 3532strip_main (int argc, char *argv[]) 3533{ 3534 char *input_target = NULL; 3535 char *output_target = NULL; 3536 bfd_boolean show_version = FALSE; 3537 bfd_boolean formats_info = FALSE; 3538 int c; 3539 int i; 3540 char *output_file = NULL; 3541 3542 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvwDU", 3543 strip_options, (int *) 0)) != EOF) 3544 { 3545 switch (c) 3546 { 3547 case 'I': 3548 input_target = optarg; 3549 break; 3550 case 'O': 3551 output_target = optarg; 3552 break; 3553 case 'F': 3554 input_target = output_target = optarg; 3555 break; 3556 case 'R': 3557 find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE); 3558 sections_removed = TRUE; 3559 break; 3560 case 's': 3561 strip_symbols = STRIP_ALL; 3562 break; 3563 case 'S': 3564 case 'g': 3565 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */ 3566 strip_symbols = STRIP_DEBUG; 3567 break; 3568 case OPTION_STRIP_DWO: 3569 strip_symbols = STRIP_DWO; 3570 break; 3571 case OPTION_STRIP_UNNEEDED: 3572 strip_symbols = STRIP_UNNEEDED; 3573 break; 3574 case 'K': 3575 add_specific_symbol (optarg, keep_specific_htab); 3576 break; 3577 case 'N': 3578 add_specific_symbol (optarg, strip_specific_htab); 3579 break; 3580 case 'o': 3581 output_file = optarg; 3582 break; 3583 case 'p': 3584 preserve_dates = TRUE; 3585 break; 3586 case 'D': 3587 deterministic = TRUE; 3588 break; 3589 case 'U': 3590 deterministic = FALSE; 3591 break; 3592 case 'x': 3593 discard_locals = LOCALS_ALL; 3594 break; 3595 case 'X': 3596 discard_locals = LOCALS_START_L; 3597 break; 3598 case 'v': 3599 verbose = TRUE; 3600 break; 3601 case 'V': 3602 show_version = TRUE; 3603 break; 3604 case OPTION_FORMATS_INFO: 3605 formats_info = TRUE; 3606 break; 3607 case OPTION_ONLY_KEEP_DEBUG: 3608 strip_symbols = STRIP_NONDEBUG; 3609 break; 3610 case OPTION_KEEP_FILE_SYMBOLS: 3611 keep_file_symbols = 1; 3612 break; 3613 case 0: 3614 /* We've been given a long option. */ 3615 break; 3616 case 'w': 3617 wildcard = TRUE; 3618 break; 3619 case 'H': 3620 case 'h': 3621 strip_usage (stdout, 0); 3622 default: 3623 strip_usage (stderr, 1); 3624 } 3625 } 3626 3627 if (formats_info) 3628 { 3629 display_info (); 3630 return 0; 3631 } 3632 3633 if (show_version) 3634 print_version ("strip"); 3635 3636 default_deterministic (); 3637 3638 /* Default is to strip all symbols. */ 3639 if (strip_symbols == STRIP_UNDEF 3640 && discard_locals == LOCALS_UNDEF 3641 && htab_elements (strip_specific_htab) == 0) 3642 strip_symbols = STRIP_ALL; 3643 3644 if (output_target == NULL) 3645 output_target = input_target; 3646 3647 i = optind; 3648 if (i == argc 3649 || (output_file != NULL && (i + 1) < argc)) 3650 strip_usage (stderr, 1); 3651 3652 for (; i < argc; i++) 3653 { 3654 int hold_status = status; 3655 struct stat statbuf; 3656 char *tmpname; 3657 3658 if (get_file_size (argv[i]) < 1) 3659 { 3660 status = 1; 3661 continue; 3662 } 3663 3664 if (preserve_dates) 3665 /* No need to check the return value of stat(). 3666 It has already been checked in get_file_size(). */ 3667 stat (argv[i], &statbuf); 3668 3669 if (output_file == NULL 3670 || filename_cmp (argv[i], output_file) == 0) 3671 tmpname = make_tempname (argv[i]); 3672 else 3673 tmpname = output_file; 3674 3675 if (tmpname == NULL) 3676 { 3677 bfd_nonfatal_message (argv[i], NULL, NULL, 3678 _("could not create temporary file to hold stripped copy")); 3679 status = 1; 3680 continue; 3681 } 3682 3683 status = 0; 3684 copy_file (argv[i], tmpname, input_target, output_target, NULL); 3685 if (status == 0) 3686 { 3687 if (preserve_dates) 3688 set_times (tmpname, &statbuf); 3689 if (output_file != tmpname) 3690 status = (smart_rename (tmpname, 3691 output_file ? output_file : argv[i], 3692 preserve_dates) != 0); 3693 if (status == 0) 3694 status = hold_status; 3695 } 3696 else 3697 unlink_if_ordinary (tmpname); 3698 if (output_file != tmpname) 3699 free (tmpname); 3700 } 3701 3702 return status; 3703} 3704 3705/* Set up PE subsystem. */ 3706 3707static void 3708set_pe_subsystem (const char *s) 3709{ 3710 const char *version, *subsystem; 3711 size_t i; 3712 static const struct 3713 { 3714 const char *name; 3715 const char set_def; 3716 const short value; 3717 } 3718 v[] = 3719 { 3720 { "native", 0, IMAGE_SUBSYSTEM_NATIVE }, 3721 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI }, 3722 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI }, 3723 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI }, 3724 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI }, 3725 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION }, 3726 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER }, 3727 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER }, 3728 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER }, 3729 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX } 3730 }; 3731 short value; 3732 char *copy; 3733 int set_def = -1; 3734 3735 /* Check for the presence of a version number. */ 3736 version = strchr (s, ':'); 3737 if (version == NULL) 3738 subsystem = s; 3739 else 3740 { 3741 int len = version - s; 3742 copy = xstrdup (s); 3743 subsystem = copy; 3744 copy[len] = '\0'; 3745 version = copy + 1 + len; 3746 pe_major_subsystem_version = strtoul (version, ©, 0); 3747 if (*copy == '.') 3748 pe_minor_subsystem_version = strtoul (copy + 1, ©, 0); 3749 if (*copy != '\0') 3750 non_fatal (_("%s: bad version in PE subsystem"), s); 3751 } 3752 3753 /* Check for numeric subsystem. */ 3754 value = (short) strtol (subsystem, ©, 0); 3755 if (*copy == '\0') 3756 { 3757 for (i = 0; i < ARRAY_SIZE (v); i++) 3758 if (v[i].value == value) 3759 { 3760 pe_subsystem = value; 3761 set_def = v[i].set_def; 3762 break; 3763 } 3764 } 3765 else 3766 { 3767 /* Search for subsystem by name. */ 3768 for (i = 0; i < ARRAY_SIZE (v); i++) 3769 if (strcmp (subsystem, v[i].name) == 0) 3770 { 3771 pe_subsystem = v[i].value; 3772 set_def = v[i].set_def; 3773 break; 3774 } 3775 } 3776 3777 switch (set_def) 3778 { 3779 case -1: 3780 fatal (_("unknown PE subsystem: %s"), s); 3781 break; 3782 case 0: 3783 break; 3784 default: 3785 if (pe_file_alignment == (bfd_vma) -1) 3786 pe_file_alignment = PE_DEF_FILE_ALIGNMENT; 3787 if (pe_section_alignment == (bfd_vma) -1) 3788 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT; 3789 break; 3790 } 3791 if (s != subsystem) 3792 free ((char *) subsystem); 3793} 3794 3795/* Convert EFI target to PEI target. */ 3796 3797static void 3798convert_efi_target (char *efi) 3799{ 3800 efi[0] = 'p'; 3801 efi[1] = 'e'; 3802 efi[2] = 'i'; 3803 3804 if (strcmp (efi + 4, "ia32") == 0) 3805 { 3806 /* Change ia32 to i386. */ 3807 efi[5]= '3'; 3808 efi[6]= '8'; 3809 efi[7]= '6'; 3810 } 3811 else if (strcmp (efi + 4, "x86_64") == 0) 3812 { 3813 /* Change x86_64 to x86-64. */ 3814 efi[7] = '-'; 3815 } 3816} 3817 3818/* Allocate and return a pointer to a struct section_add, initializing the 3819 structure using ARG, a string in the format "sectionname=filename". 3820 The returned structure will have its next pointer set to NEXT. The 3821 OPTION field is the name of the command line option currently being 3822 parsed, and is only used if an error needs to be reported. */ 3823 3824static struct section_add * 3825init_section_add (const char *arg, 3826 struct section_add *next, 3827 const char *option) 3828{ 3829 struct section_add *pa; 3830 const char *s; 3831 3832 s = strchr (arg, '='); 3833 if (s == NULL) 3834 fatal (_("bad format for %s"), option); 3835 3836 pa = (struct section_add *) xmalloc (sizeof (struct section_add)); 3837 pa->name = xstrndup (arg, s - arg); 3838 pa->filename = s + 1; 3839 pa->next = next; 3840 pa->contents = NULL; 3841 pa->size = 0; 3842 3843 return pa; 3844} 3845 3846/* Load the file specified in PA, allocating memory to hold the file 3847 contents, and store a pointer to the allocated memory in the contents 3848 field of PA. The size field of PA is also updated. All errors call 3849 FATAL. */ 3850 3851static void 3852section_add_load_file (struct section_add *pa) 3853{ 3854 size_t off, alloc; 3855 FILE *f; 3856 3857 /* We don't use get_file_size so that we can do 3858 --add-section .note.GNU_stack=/dev/null 3859 get_file_size doesn't work on /dev/null. */ 3860 3861 f = fopen (pa->filename, FOPEN_RB); 3862 if (f == NULL) 3863 fatal (_("cannot open: %s: %s"), 3864 pa->filename, strerror (errno)); 3865 3866 off = 0; 3867 alloc = 4096; 3868 pa->contents = (bfd_byte *) xmalloc (alloc); 3869 while (!feof (f)) 3870 { 3871 off_t got; 3872 3873 if (off == alloc) 3874 { 3875 alloc <<= 1; 3876 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc); 3877 } 3878 3879 got = fread (pa->contents + off, 1, alloc - off, f); 3880 if (ferror (f)) 3881 fatal (_("%s: fread failed"), pa->filename); 3882 3883 off += got; 3884 } 3885 3886 pa->size = off; 3887 3888 fclose (f); 3889} 3890 3891static int 3892copy_main (int argc, char *argv[]) 3893{ 3894 char *input_filename = NULL; 3895 char *output_filename = NULL; 3896 char *tmpname; 3897 char *input_target = NULL; 3898 char *output_target = NULL; 3899 bfd_boolean show_version = FALSE; 3900 bfd_boolean change_warn = TRUE; 3901 bfd_boolean formats_info = FALSE; 3902 int c; 3903 struct stat statbuf; 3904 const bfd_arch_info_type *input_arch = NULL; 3905 3906 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:wDU", 3907 copy_options, (int *) 0)) != EOF) 3908 { 3909 switch (c) 3910 { 3911 case 'b': 3912 copy_byte = atoi (optarg); 3913 if (copy_byte < 0) 3914 fatal (_("byte number must be non-negative")); 3915 break; 3916 3917 case 'B': 3918 input_arch = bfd_scan_arch (optarg); 3919 if (input_arch == NULL) 3920 fatal (_("architecture %s unknown"), optarg); 3921 break; 3922 3923 case 'i': 3924 if (optarg) 3925 { 3926 interleave = atoi (optarg); 3927 if (interleave < 1) 3928 fatal (_("interleave must be positive")); 3929 } 3930 else 3931 interleave = 4; 3932 break; 3933 3934 case OPTION_INTERLEAVE_WIDTH: 3935 copy_width = atoi (optarg); 3936 if (copy_width < 1) 3937 fatal(_("interleave width must be positive")); 3938 break; 3939 3940 case 'I': 3941 case 's': /* "source" - 'I' is preferred */ 3942 input_target = optarg; 3943 break; 3944 3945 case 'O': 3946 case 'd': /* "destination" - 'O' is preferred */ 3947 output_target = optarg; 3948 break; 3949 3950 case 'F': 3951 input_target = output_target = optarg; 3952 break; 3953 3954 case 'j': 3955 find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY); 3956 sections_copied = TRUE; 3957 break; 3958 3959 case 'R': 3960 find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE); 3961 sections_removed = TRUE; 3962 break; 3963 3964 case 'S': 3965 strip_symbols = STRIP_ALL; 3966 break; 3967 3968 case 'g': 3969 strip_symbols = STRIP_DEBUG; 3970 break; 3971 3972 case OPTION_STRIP_DWO: 3973 strip_symbols = STRIP_DWO; 3974 break; 3975 3976 case OPTION_STRIP_UNNEEDED: 3977 strip_symbols = STRIP_UNNEEDED; 3978 break; 3979 3980 case OPTION_ONLY_KEEP_DEBUG: 3981 strip_symbols = STRIP_NONDEBUG; 3982 break; 3983 3984 case OPTION_KEEP_FILE_SYMBOLS: 3985 keep_file_symbols = 1; 3986 break; 3987 3988 case OPTION_ADD_GNU_DEBUGLINK: 3989 long_section_names = ENABLE ; 3990 gnu_debuglink_filename = optarg; 3991 break; 3992 3993 case 'K': 3994 add_specific_symbol (optarg, keep_specific_htab); 3995 break; 3996 3997 case 'N': 3998 add_specific_symbol (optarg, strip_specific_htab); 3999 break; 4000 4001 case OPTION_STRIP_UNNEEDED_SYMBOL: 4002 add_specific_symbol (optarg, strip_unneeded_htab); 4003 break; 4004 4005 case 'L': 4006 add_specific_symbol (optarg, localize_specific_htab); 4007 break; 4008 4009 case OPTION_GLOBALIZE_SYMBOL: 4010 add_specific_symbol (optarg, globalize_specific_htab); 4011 break; 4012 4013 case 'G': 4014 add_specific_symbol (optarg, keepglobal_specific_htab); 4015 break; 4016 4017 case 'W': 4018 add_specific_symbol (optarg, weaken_specific_htab); 4019 break; 4020 4021 case 'p': 4022 preserve_dates = TRUE; 4023 break; 4024 4025 case 'D': 4026 deterministic = TRUE; 4027 break; 4028 4029 case 'U': 4030 deterministic = FALSE; 4031 break; 4032 4033 case 'w': 4034 wildcard = TRUE; 4035 break; 4036 4037 case 'x': 4038 discard_locals = LOCALS_ALL; 4039 break; 4040 4041 case 'X': 4042 discard_locals = LOCALS_START_L; 4043 break; 4044 4045 case 'v': 4046 verbose = TRUE; 4047 break; 4048 4049 case 'V': 4050 show_version = TRUE; 4051 break; 4052 4053 case OPTION_FORMATS_INFO: 4054 formats_info = TRUE; 4055 break; 4056 4057 case OPTION_WEAKEN: 4058 weaken = TRUE; 4059 break; 4060 4061 case OPTION_ADD_SECTION: 4062 add_sections = init_section_add (optarg, add_sections, 4063 "--add-section"); 4064 section_add_load_file (add_sections); 4065 break; 4066 4067 case OPTION_UPDATE_SECTION: 4068 update_sections = init_section_add (optarg, update_sections, 4069 "--update-section"); 4070 section_add_load_file (update_sections); 4071 break; 4072 4073 case OPTION_DUMP_SECTION: 4074 dump_sections = init_section_add (optarg, dump_sections, 4075 "--dump-section"); 4076 break; 4077 4078 case OPTION_ADD_SYMBOL: 4079 { 4080 char *s, *t; 4081 struct addsym_node *newsym = xmalloc (sizeof *newsym); 4082 4083 newsym->next = NULL; 4084 s = strchr (optarg, '='); 4085 if (s == NULL) 4086 fatal (_("bad format for %s"), "--add-symbol"); 4087 t = strchr (s + 1, ':'); 4088 4089 newsym->symdef = xstrndup (optarg, s - optarg); 4090 if (t) 4091 { 4092 newsym->section = xstrndup (s + 1, t - (s + 1)); 4093 newsym->symval = strtol (t + 1, NULL, 0); 4094 } 4095 else 4096 { 4097 newsym->section = NULL; 4098 newsym->symval = strtol (s + 1, NULL, 0); 4099 t = s; 4100 } 4101 4102 t = strchr (t + 1, ','); 4103 newsym->othersym = NULL; 4104 if (t) 4105 newsym->flags = parse_symflags (t+1, &newsym->othersym); 4106 else 4107 newsym->flags = BSF_GLOBAL; 4108 4109 /* Keep 'othersym' symbols at the front of the list. */ 4110 if (newsym->othersym) 4111 { 4112 newsym->next = add_sym_list; 4113 if (!add_sym_list) 4114 add_sym_tail = &newsym->next; 4115 add_sym_list = newsym; 4116 } 4117 else 4118 { 4119 *add_sym_tail = newsym; 4120 add_sym_tail = &newsym->next; 4121 } 4122 add_symbols++; 4123 } 4124 break; 4125 4126 case OPTION_CHANGE_START: 4127 change_start = parse_vma (optarg, "--change-start"); 4128 break; 4129 4130 case OPTION_CHANGE_SECTION_ADDRESS: 4131 case OPTION_CHANGE_SECTION_LMA: 4132 case OPTION_CHANGE_SECTION_VMA: 4133 { 4134 struct section_list * p; 4135 unsigned int context = 0; 4136 const char *s; 4137 int len; 4138 char *name; 4139 char *option = NULL; 4140 bfd_vma val; 4141 4142 switch (c) 4143 { 4144 case OPTION_CHANGE_SECTION_ADDRESS: 4145 option = "--change-section-address"; 4146 context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA; 4147 break; 4148 case OPTION_CHANGE_SECTION_LMA: 4149 option = "--change-section-lma"; 4150 context = SECTION_CONTEXT_ALTER_LMA; 4151 break; 4152 case OPTION_CHANGE_SECTION_VMA: 4153 option = "--change-section-vma"; 4154 context = SECTION_CONTEXT_ALTER_VMA; 4155 break; 4156 } 4157 4158 s = strchr (optarg, '='); 4159 if (s == NULL) 4160 { 4161 s = strchr (optarg, '+'); 4162 if (s == NULL) 4163 { 4164 s = strchr (optarg, '-'); 4165 if (s == NULL) 4166 fatal (_("bad format for %s"), option); 4167 } 4168 } 4169 else 4170 { 4171 /* Correct the context. */ 4172 switch (c) 4173 { 4174 case OPTION_CHANGE_SECTION_ADDRESS: 4175 context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA; 4176 break; 4177 case OPTION_CHANGE_SECTION_LMA: 4178 context = SECTION_CONTEXT_SET_LMA; 4179 break; 4180 case OPTION_CHANGE_SECTION_VMA: 4181 context = SECTION_CONTEXT_SET_VMA; 4182 break; 4183 } 4184 } 4185 4186 len = s - optarg; 4187 name = (char *) xmalloc (len + 1); 4188 strncpy (name, optarg, len); 4189 name[len] = '\0'; 4190 4191 p = find_section_list (name, TRUE, context); 4192 4193 val = parse_vma (s + 1, option); 4194 if (*s == '-') 4195 val = - val; 4196 4197 switch (c) 4198 { 4199 case OPTION_CHANGE_SECTION_ADDRESS: 4200 p->vma_val = val; 4201 /* Drop through. */ 4202 4203 case OPTION_CHANGE_SECTION_LMA: 4204 p->lma_val = val; 4205 break; 4206 4207 case OPTION_CHANGE_SECTION_VMA: 4208 p->vma_val = val; 4209 break; 4210 } 4211 } 4212 break; 4213 4214 case OPTION_CHANGE_ADDRESSES: 4215 change_section_address = parse_vma (optarg, "--change-addresses"); 4216 change_start = change_section_address; 4217 break; 4218 4219 case OPTION_CHANGE_WARNINGS: 4220 change_warn = TRUE; 4221 break; 4222 4223 case OPTION_CHANGE_LEADING_CHAR: 4224 change_leading_char = TRUE; 4225 break; 4226 4227 case OPTION_COMPRESS_DEBUG_SECTIONS: 4228 if (optarg) 4229 { 4230 if (strcasecmp (optarg, "none") == 0) 4231 do_debug_sections = decompress; 4232 else if (strcasecmp (optarg, "zlib") == 0) 4233 do_debug_sections = compress_zlib; 4234 else if (strcasecmp (optarg, "zlib-gnu") == 0) 4235 do_debug_sections = compress_gnu_zlib; 4236 else if (strcasecmp (optarg, "zlib-gabi") == 0) 4237 do_debug_sections = compress_gabi_zlib; 4238 else 4239 fatal (_("unrecognized --compress-debug-sections type `%s'"), 4240 optarg); 4241 } 4242 else 4243 do_debug_sections = compress; 4244 break; 4245 4246 case OPTION_DEBUGGING: 4247 convert_debugging = TRUE; 4248 break; 4249 4250 case OPTION_DECOMPRESS_DEBUG_SECTIONS: 4251 do_debug_sections = decompress; 4252 break; 4253 4254 case OPTION_GAP_FILL: 4255 { 4256 bfd_vma gap_fill_vma; 4257 4258 gap_fill_vma = parse_vma (optarg, "--gap-fill"); 4259 gap_fill = (bfd_byte) gap_fill_vma; 4260 if ((bfd_vma) gap_fill != gap_fill_vma) 4261 { 4262 char buff[20]; 4263 4264 sprintf_vma (buff, gap_fill_vma); 4265 4266 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"), 4267 buff, gap_fill); 4268 } 4269 gap_fill_set = TRUE; 4270 } 4271 break; 4272 4273 case OPTION_NO_CHANGE_WARNINGS: 4274 change_warn = FALSE; 4275 break; 4276 4277 case OPTION_PAD_TO: 4278 pad_to = parse_vma (optarg, "--pad-to"); 4279 pad_to_set = TRUE; 4280 break; 4281 4282 case OPTION_REMOVE_LEADING_CHAR: 4283 remove_leading_char = TRUE; 4284 break; 4285 4286 case OPTION_REDEFINE_SYM: 4287 { 4288 /* Push this redefinition onto redefine_symbol_list. */ 4289 4290 int len; 4291 const char *s; 4292 const char *nextarg; 4293 char *source, *target; 4294 4295 s = strchr (optarg, '='); 4296 if (s == NULL) 4297 fatal (_("bad format for %s"), "--redefine-sym"); 4298 4299 len = s - optarg; 4300 source = (char *) xmalloc (len + 1); 4301 strncpy (source, optarg, len); 4302 source[len] = '\0'; 4303 4304 nextarg = s + 1; 4305 len = strlen (nextarg); 4306 target = (char *) xmalloc (len + 1); 4307 strcpy (target, nextarg); 4308 4309 redefine_list_append ("--redefine-sym", source, target); 4310 4311 free (source); 4312 free (target); 4313 } 4314 break; 4315 4316 case OPTION_REDEFINE_SYMS: 4317 add_redefine_syms_file (optarg); 4318 break; 4319 4320 case OPTION_SET_SECTION_FLAGS: 4321 { 4322 struct section_list *p; 4323 const char *s; 4324 int len; 4325 char *name; 4326 4327 s = strchr (optarg, '='); 4328 if (s == NULL) 4329 fatal (_("bad format for %s"), "--set-section-flags"); 4330 4331 len = s - optarg; 4332 name = (char *) xmalloc (len + 1); 4333 strncpy (name, optarg, len); 4334 name[len] = '\0'; 4335 4336 p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS); 4337 4338 p->flags = parse_flags (s + 1); 4339 } 4340 break; 4341 4342 case OPTION_RENAME_SECTION: 4343 { 4344 flagword flags; 4345 const char *eq, *fl; 4346 char *old_name; 4347 char *new_name; 4348 unsigned int len; 4349 4350 eq = strchr (optarg, '='); 4351 if (eq == NULL) 4352 fatal (_("bad format for %s"), "--rename-section"); 4353 4354 len = eq - optarg; 4355 if (len == 0) 4356 fatal (_("bad format for %s"), "--rename-section"); 4357 4358 old_name = (char *) xmalloc (len + 1); 4359 strncpy (old_name, optarg, len); 4360 old_name[len] = 0; 4361 4362 eq++; 4363 fl = strchr (eq, ','); 4364 if (fl) 4365 { 4366 flags = parse_flags (fl + 1); 4367 len = fl - eq; 4368 } 4369 else 4370 { 4371 flags = -1; 4372 len = strlen (eq); 4373 } 4374 4375 if (len == 0) 4376 fatal (_("bad format for %s"), "--rename-section"); 4377 4378 new_name = (char *) xmalloc (len + 1); 4379 strncpy (new_name, eq, len); 4380 new_name[len] = 0; 4381 4382 add_section_rename (old_name, new_name, flags); 4383 } 4384 break; 4385 4386 case OPTION_SET_START: 4387 set_start = parse_vma (optarg, "--set-start"); 4388 set_start_set = TRUE; 4389 break; 4390 4391 case OPTION_SREC_LEN: 4392 Chunk = parse_vma (optarg, "--srec-len"); 4393 break; 4394 4395 case OPTION_SREC_FORCES3: 4396 S3Forced = TRUE; 4397 break; 4398 4399 case OPTION_STRIP_SYMBOLS: 4400 add_specific_symbols (optarg, strip_specific_htab); 4401 break; 4402 4403 case OPTION_STRIP_UNNEEDED_SYMBOLS: 4404 add_specific_symbols (optarg, strip_unneeded_htab); 4405 break; 4406 4407 case OPTION_KEEP_SYMBOLS: 4408 add_specific_symbols (optarg, keep_specific_htab); 4409 break; 4410 4411 case OPTION_LOCALIZE_HIDDEN: 4412 localize_hidden = TRUE; 4413 break; 4414 4415 case OPTION_LOCALIZE_SYMBOLS: 4416 add_specific_symbols (optarg, localize_specific_htab); 4417 break; 4418 4419 case OPTION_LONG_SECTION_NAMES: 4420 if (!strcmp ("enable", optarg)) 4421 long_section_names = ENABLE; 4422 else if (!strcmp ("disable", optarg)) 4423 long_section_names = DISABLE; 4424 else if (!strcmp ("keep", optarg)) 4425 long_section_names = KEEP; 4426 else 4427 fatal (_("unknown long section names option '%s'"), optarg); 4428 break; 4429 4430 case OPTION_GLOBALIZE_SYMBOLS: 4431 add_specific_symbols (optarg, globalize_specific_htab); 4432 break; 4433 4434 case OPTION_KEEPGLOBAL_SYMBOLS: 4435 add_specific_symbols (optarg, keepglobal_specific_htab); 4436 break; 4437 4438 case OPTION_WEAKEN_SYMBOLS: 4439 add_specific_symbols (optarg, weaken_specific_htab); 4440 break; 4441 4442 case OPTION_ALT_MACH_CODE: 4443 use_alt_mach_code = strtoul (optarg, NULL, 0); 4444 if (use_alt_mach_code == 0) 4445 fatal (_("unable to parse alternative machine code")); 4446 break; 4447 4448 case OPTION_PREFIX_SYMBOLS: 4449 prefix_symbols_string = optarg; 4450 break; 4451 4452 case OPTION_PREFIX_SECTIONS: 4453 prefix_sections_string = optarg; 4454 break; 4455 4456 case OPTION_PREFIX_ALLOC_SECTIONS: 4457 prefix_alloc_sections_string = optarg; 4458 break; 4459 4460 case OPTION_READONLY_TEXT: 4461 bfd_flags_to_set |= WP_TEXT; 4462 bfd_flags_to_clear &= ~WP_TEXT; 4463 break; 4464 4465 case OPTION_WRITABLE_TEXT: 4466 bfd_flags_to_clear |= WP_TEXT; 4467 bfd_flags_to_set &= ~WP_TEXT; 4468 break; 4469 4470 case OPTION_PURE: 4471 bfd_flags_to_set |= D_PAGED; 4472 bfd_flags_to_clear &= ~D_PAGED; 4473 break; 4474 4475 case OPTION_IMPURE: 4476 bfd_flags_to_clear |= D_PAGED; 4477 bfd_flags_to_set &= ~D_PAGED; 4478 break; 4479 4480 case OPTION_EXTRACT_DWO: 4481 strip_symbols = STRIP_NONDWO; 4482 break; 4483 4484 case OPTION_EXTRACT_SYMBOL: 4485 extract_symbol = TRUE; 4486 break; 4487 4488 case OPTION_REVERSE_BYTES: 4489 { 4490 int prev = reverse_bytes; 4491 4492 reverse_bytes = atoi (optarg); 4493 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0)) 4494 fatal (_("number of bytes to reverse must be positive and even")); 4495 4496 if (prev && prev != reverse_bytes) 4497 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"), 4498 prev); 4499 break; 4500 } 4501 4502 case OPTION_FILE_ALIGNMENT: 4503 pe_file_alignment = parse_vma (optarg, "--file-alignment"); 4504 break; 4505 4506 case OPTION_HEAP: 4507 { 4508 char *end; 4509 pe_heap_reserve = strtoul (optarg, &end, 0); 4510 if (end == optarg 4511 || (*end != '.' && *end != '\0')) 4512 non_fatal (_("%s: invalid reserve value for --heap"), 4513 optarg); 4514 else if (*end != '\0') 4515 { 4516 pe_heap_commit = strtoul (end + 1, &end, 0); 4517 if (*end != '\0') 4518 non_fatal (_("%s: invalid commit value for --heap"), 4519 optarg); 4520 } 4521 } 4522 break; 4523 4524 case OPTION_IMAGE_BASE: 4525 pe_image_base = parse_vma (optarg, "--image-base"); 4526 break; 4527 4528 case OPTION_SECTION_ALIGNMENT: 4529 pe_section_alignment = parse_vma (optarg, 4530 "--section-alignment"); 4531 break; 4532 4533 case OPTION_SUBSYSTEM: 4534 set_pe_subsystem (optarg); 4535 break; 4536 4537 case OPTION_STACK: 4538 { 4539 char *end; 4540 pe_stack_reserve = strtoul (optarg, &end, 0); 4541 if (end == optarg 4542 || (*end != '.' && *end != '\0')) 4543 non_fatal (_("%s: invalid reserve value for --stack"), 4544 optarg); 4545 else if (*end != '\0') 4546 { 4547 pe_stack_commit = strtoul (end + 1, &end, 0); 4548 if (*end != '\0') 4549 non_fatal (_("%s: invalid commit value for --stack"), 4550 optarg); 4551 } 4552 } 4553 break; 4554 4555 case 0: 4556 /* We've been given a long option. */ 4557 break; 4558 4559 case 'H': 4560 case 'h': 4561 copy_usage (stdout, 0); 4562 4563 default: 4564 copy_usage (stderr, 1); 4565 } 4566 } 4567 4568 if (formats_info) 4569 { 4570 display_info (); 4571 return 0; 4572 } 4573 4574 if (show_version) 4575 print_version ("objcopy"); 4576 4577 if (interleave && copy_byte == -1) 4578 fatal (_("interleave start byte must be set with --byte")); 4579 4580 if (copy_byte >= interleave) 4581 fatal (_("byte number must be less than interleave")); 4582 4583 if (copy_width > interleave - copy_byte) 4584 fatal (_("interleave width must be less than or equal to interleave - byte`")); 4585 4586 if (optind == argc || optind + 2 < argc) 4587 copy_usage (stderr, 1); 4588 4589 input_filename = argv[optind]; 4590 if (optind + 1 < argc) 4591 output_filename = argv[optind + 1]; 4592 4593 default_deterministic (); 4594 4595 /* Default is to strip no symbols. */ 4596 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF) 4597 strip_symbols = STRIP_NONE; 4598 4599 if (output_target == NULL) 4600 output_target = input_target; 4601 4602 /* Convert input EFI target to PEI target. */ 4603 if (input_target != NULL 4604 && strncmp (input_target, "efi-", 4) == 0) 4605 { 4606 char *efi; 4607 4608 efi = xstrdup (output_target + 4); 4609 if (strncmp (efi, "bsdrv-", 6) == 0 4610 || strncmp (efi, "rtdrv-", 6) == 0) 4611 efi += 2; 4612 else if (strncmp (efi, "app-", 4) != 0) 4613 fatal (_("unknown input EFI target: %s"), input_target); 4614 4615 input_target = efi; 4616 convert_efi_target (efi); 4617 } 4618 4619 /* Convert output EFI target to PEI target. */ 4620 if (output_target != NULL 4621 && strncmp (output_target, "efi-", 4) == 0) 4622 { 4623 char *efi; 4624 4625 efi = xstrdup (output_target + 4); 4626 if (strncmp (efi, "app-", 4) == 0) 4627 { 4628 if (pe_subsystem == -1) 4629 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION; 4630 } 4631 else if (strncmp (efi, "bsdrv-", 6) == 0) 4632 { 4633 if (pe_subsystem == -1) 4634 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER; 4635 efi += 2; 4636 } 4637 else if (strncmp (efi, "rtdrv-", 6) == 0) 4638 { 4639 if (pe_subsystem == -1) 4640 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER; 4641 efi += 2; 4642 } 4643 else 4644 fatal (_("unknown output EFI target: %s"), output_target); 4645 4646 if (pe_file_alignment == (bfd_vma) -1) 4647 pe_file_alignment = PE_DEF_FILE_ALIGNMENT; 4648 if (pe_section_alignment == (bfd_vma) -1) 4649 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT; 4650 4651 output_target = efi; 4652 convert_efi_target (efi); 4653 } 4654 4655 if (preserve_dates) 4656 if (stat (input_filename, & statbuf) < 0) 4657 fatal (_("warning: could not locate '%s'. System error message: %s"), 4658 input_filename, strerror (errno)); 4659 4660 /* If there is no destination file, or the source and destination files 4661 are the same, then create a temp and rename the result into the input. */ 4662 if (output_filename == NULL 4663 || filename_cmp (input_filename, output_filename) == 0) 4664 tmpname = make_tempname (input_filename); 4665 else 4666 tmpname = output_filename; 4667 4668 if (tmpname == NULL) 4669 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"), 4670 input_filename, strerror (errno)); 4671 4672 copy_file (input_filename, tmpname, input_target, output_target, input_arch); 4673 if (status == 0) 4674 { 4675 if (preserve_dates) 4676 set_times (tmpname, &statbuf); 4677 if (tmpname != output_filename) 4678 status = (smart_rename (tmpname, input_filename, 4679 preserve_dates) != 0); 4680 } 4681 else 4682 unlink_if_ordinary (tmpname); 4683 4684 if (change_warn) 4685 { 4686 struct section_list *p; 4687 4688 for (p = change_sections; p != NULL; p = p->next) 4689 { 4690 if (! p->used) 4691 { 4692 if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA)) 4693 { 4694 char buff [20]; 4695 4696 sprintf_vma (buff, p->vma_val); 4697 4698 /* xgettext:c-format */ 4699 non_fatal (_("%s %s%c0x%s never used"), 4700 "--change-section-vma", 4701 p->pattern, 4702 p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+', 4703 buff); 4704 } 4705 4706 if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA)) 4707 { 4708 char buff [20]; 4709 4710 sprintf_vma (buff, p->lma_val); 4711 4712 /* xgettext:c-format */ 4713 non_fatal (_("%s %s%c0x%s never used"), 4714 "--change-section-lma", 4715 p->pattern, 4716 p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+', 4717 buff); 4718 } 4719 } 4720 } 4721 } 4722 4723 return 0; 4724} 4725 4726int 4727main (int argc, char *argv[]) 4728{ 4729#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) 4730 setlocale (LC_MESSAGES, ""); 4731#endif 4732#if defined (HAVE_SETLOCALE) 4733 setlocale (LC_CTYPE, ""); 4734#endif 4735 bindtextdomain (PACKAGE, LOCALEDIR); 4736 textdomain (PACKAGE); 4737 4738 program_name = argv[0]; 4739 xmalloc_set_program_name (program_name); 4740 4741 START_PROGRESS (program_name, 0); 4742 4743 expandargv (&argc, &argv); 4744 4745 strip_symbols = STRIP_UNDEF; 4746 discard_locals = LOCALS_UNDEF; 4747 4748 bfd_init (); 4749 set_default_bfd_target (); 4750 4751 if (is_strip < 0) 4752 { 4753 int i = strlen (program_name); 4754#ifdef HAVE_DOS_BASED_FILE_SYSTEM 4755 /* Drop the .exe suffix, if any. */ 4756 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0) 4757 { 4758 i -= 4; 4759 program_name[i] = '\0'; 4760 } 4761#endif 4762 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0); 4763 } 4764 4765 create_symbol_htabs (); 4766 4767 if (argv != NULL) 4768 bfd_set_error_program_name (argv[0]); 4769 4770 if (is_strip) 4771 strip_main (argc, argv); 4772 else 4773 copy_main (argc, argv); 4774 4775 END_PROGRESS (program_name); 4776 4777 return status; 4778} 4779