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