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