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