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