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