1 1.1 christos /* objcopy.c -- copy object file from input to output, optionally massaging it. 2 1.10 christos Copyright (C) 1991-2025 Free Software Foundation, Inc. 3 1.1 christos 4 1.1 christos This file is part of GNU Binutils. 5 1.1 christos 6 1.1 christos This program is free software; you can redistribute it and/or modify 7 1.1 christos it under the terms of the GNU General Public License as published by 8 1.1 christos the Free Software Foundation; either version 3 of the License, or 9 1.1 christos (at your option) any later version. 10 1.1 christos 11 1.1 christos This program is distributed in the hope that it will be useful, 12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 christos GNU General Public License for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU General Public License 17 1.1 christos along with this program; if not, write to the Free Software 18 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 19 1.1 christos 02110-1301, USA. */ 20 1.1 christos 21 1.1 christos #include "sysdep.h" 23 1.1 christos #include "bfd.h" 24 1.1 christos #include "getopt.h" 25 1.1 christos #include "libiberty.h" 26 1.1 christos #include "bucomm.h" 27 1.1 christos #include "budbg.h" 28 1.1 christos #include "filenames.h" 29 1.1 christos #include "fnmatch.h" 30 1.1 christos #include "elf-bfd.h" 31 1.1 christos #include "coff/internal.h" 32 1.6 christos #include "libcoff.h" 33 1.10 christos #include "safe-ctype.h" 34 1.10 christos #include "plugin-api.h" 35 1.1 christos #include "plugin.h" 36 1.1 christos 37 1.1 christos /* FIXME: See bfd/peXXigen.c for why we include an architecture specific 38 1.1 christos header in generic PE code. */ 39 1.1 christos #include "coff/i386.h" 40 1.1 christos #include "coff/pe.h" 41 1.1 christos 42 1.1 christos static bfd_vma pe_file_alignment = (bfd_vma) -1; 43 1.1 christos static bfd_vma pe_heap_commit = (bfd_vma) -1; 44 1.1 christos static bfd_vma pe_heap_reserve = (bfd_vma) -1; 45 1.1 christos static bfd_vma pe_image_base = (bfd_vma) -1; 46 1.1 christos static bfd_vma pe_section_alignment = (bfd_vma) -1; 47 1.1 christos static bfd_vma pe_stack_commit = (bfd_vma) -1; 48 1.1 christos static bfd_vma pe_stack_reserve = (bfd_vma) -1; 49 1.1 christos static short pe_subsystem = -1; 50 1.1 christos static short pe_major_subsystem_version = -1; 51 1.1 christos static short pe_minor_subsystem_version = -1; 52 1.1 christos 53 1.1 christos struct is_specified_symbol_predicate_data 54 1.8 christos { 55 1.8 christos const char *name; 56 1.1 christos bool found; 57 1.1 christos }; 58 1.6 christos 59 1.1 christos /* A node includes symbol name mapping to support redefine_sym. */ 60 1.1 christos struct redefine_node 61 1.1 christos { 62 1.1 christos char *source; 63 1.1 christos char *target; 64 1.1 christos }; 65 1.3 christos 66 1.3 christos struct addsym_node 67 1.3 christos { 68 1.3 christos struct addsym_node *next; 69 1.3 christos char * symdef; 70 1.3 christos long symval; 71 1.3 christos flagword flags; 72 1.7 christos char * section; 73 1.3 christos const char * othersym; 74 1.3 christos }; 75 1.1 christos 76 1.1 christos typedef struct section_rename 77 1.1 christos { 78 1.1 christos const char * old_name; 79 1.1 christos const char * new_name; 80 1.1 christos flagword flags; 81 1.1 christos struct section_rename * next; 82 1.1 christos } 83 1.1 christos section_rename; 84 1.1 christos 85 1.1 christos /* List of sections to be renamed. */ 86 1.1 christos static section_rename *section_rename_list; 87 1.1 christos 88 1.1 christos static asymbol **isympp = NULL; /* Input symbols. */ 89 1.1 christos static asymbol **osympp = NULL; /* Output symbols that survive stripping. */ 90 1.1 christos 91 1.1 christos /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */ 92 1.1 christos static int copy_byte = -1; 93 1.1 christos static int interleave = 0; /* Initialised to 4 in copy_main(). */ 94 1.1 christos static int copy_width = 1; 95 1.8 christos 96 1.8 christos static bool keep_section_symbols = false ;/* True if section symbols should be retained. */ 97 1.8 christos static bool verbose; /* Print file and target names. */ 98 1.1 christos static bool preserve_dates; /* Preserve input file timestamp. */ 99 1.6 christos static int deterministic = -1; /* Enable deterministic archives. */ 100 1.6 christos static int status = 0; /* Exit status. */ 101 1.8 christos 102 1.9 christos static bool merge_notes = false; /* Merge note sections. */ 103 1.7 christos static bool strip_section_headers = false;/* Strip section headers. */ 104 1.7 christos 105 1.7 christos typedef struct merged_note_section 106 1.7 christos { 107 1.7 christos asection * sec; /* The section that is being merged. */ 108 1.7 christos bfd_byte * contents;/* New contents of the section. */ 109 1.7 christos bfd_size_type size; /* New size of the section. */ 110 1.7 christos struct merged_note_section * next; /* Link to next merged note section. */ 111 1.1 christos } merged_note_section; 112 1.1 christos 113 1.3 christos enum strip_action 114 1.3 christos { 115 1.3 christos STRIP_UNDEF, 116 1.3 christos STRIP_NONE, /* Don't strip. */ 117 1.3 christos STRIP_DEBUG, /* Strip all debugger symbols. */ 118 1.3 christos STRIP_UNNEEDED, /* Strip unnecessary symbols. */ 119 1.3 christos STRIP_NONDEBUG, /* Strip everything but debug info. */ 120 1.3 christos STRIP_DWO, /* Strip all DWO info. */ 121 1.3 christos STRIP_NONDWO, /* Strip everything but DWO info. */ 122 1.3 christos STRIP_ALL /* Strip all symbols. */ 123 1.1 christos }; 124 1.1 christos 125 1.3 christos /* Which symbols to remove. */ 126 1.1 christos static enum strip_action strip_symbols = STRIP_UNDEF; 127 1.1 christos 128 1.3 christos enum locals_action 129 1.3 christos { 130 1.3 christos LOCALS_UNDEF, 131 1.3 christos LOCALS_START_L, /* Discard locals starting with L. */ 132 1.3 christos LOCALS_ALL /* Discard all locals. */ 133 1.1 christos }; 134 1.1 christos 135 1.1 christos /* Which local symbols to remove. Overrides STRIP_ALL. */ 136 1.1 christos static enum locals_action discard_locals; 137 1.1 christos 138 1.1 christos /* Structure used to hold lists of sections and actions to take. */ 139 1.1 christos struct section_list 140 1.8 christos { 141 1.8 christos struct section_list *next; /* Next section to change. */ 142 1.8 christos const char *pattern; /* Section name pattern. */ 143 1.3 christos bool used; /* Whether this entry was used. */ 144 1.8 christos 145 1.3 christos unsigned int context; /* What to do with matching sections. */ 146 1.8 christos /* Flag bits used in the context field. 147 1.8 christos COPY and REMOVE are mutually exlusive. 148 1.3 christos SET and ALTER are mutually exclusive. */ 149 1.3 christos #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */ 150 1.7 christos #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */ 151 1.7 christos #define SECTION_CONTEXT_KEEP (1 << 2) /* Keep this section. */ 152 1.7 christos #define SECTION_CONTEXT_SET_VMA (1 << 3) /* Set the sections' VMA address. */ 153 1.7 christos #define SECTION_CONTEXT_ALTER_VMA (1 << 4) /* Increment or decrement the section's VMA address. */ 154 1.7 christos #define SECTION_CONTEXT_SET_LMA (1 << 5) /* Set the sections' LMA address. */ 155 1.7 christos #define SECTION_CONTEXT_ALTER_LMA (1 << 6) /* Increment or decrement the section's LMA address. */ 156 1.7 christos #define SECTION_CONTEXT_SET_FLAGS (1 << 7) /* Set the section's flags. */ 157 1.7 christos #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 8) /* Remove relocations for this section. */ 158 1.3 christos #define SECTION_CONTEXT_SET_ALIGNMENT (1 << 9) /* Set alignment for section. */ 159 1.8 christos 160 1.8 christos bfd_vma vma_val; /* Amount to change by or set to. */ 161 1.8 christos bfd_vma lma_val; /* Amount to change by or set to. */ 162 1.8 christos flagword flags; /* What to set the section flags to. */ 163 1.1 christos unsigned int alignment; /* Alignment of output section. */ 164 1.1 christos }; 165 1.1 christos 166 1.1 christos static struct section_list *change_sections; 167 1.1 christos 168 1.8 christos /* TRUE if some sections are to be removed. */ 169 1.1 christos static bool sections_removed; 170 1.10 christos 171 1.10 christos #if BFD_SUPPORTS_PLUGINS 172 1.10 christos /* TRUE if all GCC LTO sections are to be removed. */ 173 1.10 christos static bool lto_sections_removed; 174 1.10 christos #endif 175 1.1 christos 176 1.8 christos /* TRUE if only some sections are to be copied. */ 177 1.1 christos static bool sections_copied; 178 1.1 christos 179 1.1 christos /* Changes to the start address. */ 180 1.8 christos static bfd_vma change_start = 0; 181 1.1 christos static bool set_start_set = false; 182 1.1 christos static bfd_vma set_start; 183 1.1 christos 184 1.1 christos /* Changes to section addresses. */ 185 1.1 christos static bfd_vma change_section_address = 0; 186 1.1 christos 187 1.8 christos /* Filling gaps between sections. */ 188 1.1 christos static bool gap_fill_set = false; 189 1.1 christos static bfd_byte gap_fill = 0; 190 1.1 christos 191 1.8 christos /* Pad to a given address. */ 192 1.1 christos static bool pad_to_set = false; 193 1.1 christos static bfd_vma pad_to; 194 1.1 christos 195 1.1 christos /* Use alternative machine code? */ 196 1.1 christos static unsigned long use_alt_mach_code = 0; 197 1.1 christos 198 1.1 christos /* Output BFD flags user wants to set or clear */ 199 1.1 christos static flagword bfd_flags_to_set; 200 1.1 christos static flagword bfd_flags_to_clear; 201 1.1 christos 202 1.1 christos /* List of sections to add. */ 203 1.1 christos struct section_add 204 1.1 christos { 205 1.1 christos /* Next section to add. */ 206 1.1 christos struct section_add *next; 207 1.1 christos /* Name of section to add. */ 208 1.1 christos const char *name; 209 1.1 christos /* Name of file holding section contents. */ 210 1.1 christos const char *filename; 211 1.1 christos /* Size of file. */ 212 1.1 christos size_t size; 213 1.1 christos /* Contents of file. */ 214 1.1 christos bfd_byte *contents; 215 1.1 christos /* BFD section, after it has been added. */ 216 1.1 christos asection *section; 217 1.1 christos }; 218 1.1 christos 219 1.1 christos /* List of sections to add to the output BFD. */ 220 1.1 christos static struct section_add *add_sections; 221 1.3 christos 222 1.3 christos /* List of sections to update in the output BFD. */ 223 1.3 christos static struct section_add *update_sections; 224 1.3 christos 225 1.3 christos /* List of sections to dump from the output BFD. */ 226 1.3 christos static struct section_add *dump_sections; 227 1.1 christos 228 1.1 christos /* If non-NULL the argument to --add-gnu-debuglink. 229 1.1 christos This should be the filename to store in the .gnu_debuglink section. */ 230 1.1 christos static const char * gnu_debuglink_filename = NULL; 231 1.1 christos 232 1.8 christos /* Whether to convert debugging information. */ 233 1.1 christos static bool convert_debugging = false; 234 1.1 christos 235 1.1 christos /* Whether to compress/decompress DWARF debug sections. */ 236 1.1 christos static enum 237 1.3 christos { 238 1.3 christos nothing = 0, 239 1.3 christos compress = 1 << 0, 240 1.3 christos compress_zlib = compress | 1 << 1, 241 1.3 christos compress_gnu_zlib = compress | 1 << 2, 242 1.9 christos compress_gabi_zlib = compress | 1 << 3, 243 1.9 christos compress_zstd = compress | 1 << 4, 244 1.1 christos decompress = 1 << 5 245 1.1 christos } do_debug_sections = nothing; 246 1.5 christos 247 1.5 christos /* Whether to generate ELF common symbols with the STT_COMMON type. */ 248 1.5 christos static enum bfd_link_elf_stt_common do_elf_stt_common = unchanged; 249 1.1 christos 250 1.8 christos /* Whether to change the leading character in symbol names. */ 251 1.1 christos static bool change_leading_char = false; 252 1.1 christos 253 1.8 christos /* Whether to remove the leading character from global symbol names. */ 254 1.1 christos static bool remove_leading_char = false; 255 1.1 christos 256 1.8 christos /* Whether to permit wildcard in symbol comparison. */ 257 1.1 christos static bool wildcard = false; 258 1.1 christos 259 1.8 christos /* True if --localize-hidden is in effect. */ 260 1.1 christos static bool localize_hidden = false; 261 1.1 christos 262 1.1 christos /* List of symbols to strip, keep, localize, keep-global, weaken, 263 1.1 christos or redefine. */ 264 1.1 christos static htab_t strip_specific_htab = NULL; 265 1.1 christos static htab_t strip_unneeded_htab = NULL; 266 1.1 christos static htab_t keep_specific_htab = NULL; 267 1.1 christos static htab_t localize_specific_htab = NULL; 268 1.1 christos static htab_t globalize_specific_htab = NULL; 269 1.1 christos static htab_t keepglobal_specific_htab = NULL; 270 1.6 christos static htab_t weaken_specific_htab = NULL; 271 1.6 christos static htab_t redefine_specific_htab = NULL; 272 1.3 christos static htab_t redefine_specific_reverse_htab = NULL; 273 1.3 christos static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list; 274 1.1 christos static int add_symbols = 0; 275 1.7 christos 276 1.7 christos static char *strip_specific_buffer = NULL; 277 1.7 christos static char *strip_unneeded_buffer = NULL; 278 1.7 christos static char *keep_specific_buffer = NULL; 279 1.7 christos static char *localize_specific_buffer = NULL; 280 1.7 christos static char *globalize_specific_buffer = NULL; 281 1.7 christos static char *keepglobal_specific_buffer = NULL; 282 1.7 christos static char *weaken_specific_buffer = NULL; 283 1.1 christos 284 1.8 christos /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */ 285 1.1 christos static bool weaken = false; 286 1.1 christos 287 1.8 christos /* If this is TRUE, we retain BSF_FILE symbols. */ 288 1.1 christos static bool keep_file_symbols = false; 289 1.1 christos 290 1.1 christos /* Prefix symbols/sections. */ 291 1.1 christos static char *prefix_symbols_string = 0; 292 1.1 christos static char *prefix_sections_string = 0; 293 1.1 christos static char *prefix_alloc_sections_string = 0; 294 1.1 christos 295 1.8 christos /* True if --extract-symbol was passed on the command line. */ 296 1.1 christos static bool extract_symbol = false; 297 1.1 christos 298 1.1 christos /* If `reverse_bytes' is nonzero, then reverse the order of every chunk 299 1.1 christos of <reverse_bytes> bytes within each output section. */ 300 1.1 christos static int reverse_bytes = 0; 301 1.1 christos 302 1.1 christos /* For Coff objects, we may want to allow or disallow long section names, 303 1.1 christos or preserve them where found in the inputs. Debug info relies on them. */ 304 1.3 christos enum long_section_name_handling 305 1.3 christos { 306 1.3 christos DISABLE, 307 1.3 christos ENABLE, 308 1.3 christos KEEP 309 1.1 christos }; 310 1.1 christos 311 1.1 christos /* The default long section handling mode is to preserve them. 312 1.1 christos This is also the only behaviour for 'strip'. */ 313 1.1 christos static enum long_section_name_handling long_section_names = KEEP; 314 1.1 christos 315 1.1 christos /* 150 isn't special; it's just an arbitrary non-ASCII char value. */ 316 1.3 christos enum command_line_switch 317 1.3 christos { 318 1.3 christos OPTION_ADD_SECTION=150, 319 1.3 christos OPTION_ADD_GNU_DEBUGLINK, 320 1.3 christos OPTION_ADD_SYMBOL, 321 1.3 christos OPTION_ALT_MACH_CODE, 322 1.3 christos OPTION_CHANGE_ADDRESSES, 323 1.3 christos OPTION_CHANGE_LEADING_CHAR, 324 1.3 christos OPTION_CHANGE_SECTION_ADDRESS, 325 1.3 christos OPTION_CHANGE_SECTION_LMA, 326 1.3 christos OPTION_CHANGE_SECTION_VMA, 327 1.3 christos OPTION_CHANGE_START, 328 1.3 christos OPTION_CHANGE_WARNINGS, 329 1.3 christos OPTION_COMPRESS_DEBUG_SECTIONS, 330 1.3 christos OPTION_DEBUGGING, 331 1.3 christos OPTION_DECOMPRESS_DEBUG_SECTIONS, 332 1.5 christos OPTION_DUMP_SECTION, 333 1.3 christos OPTION_ELF_STT_COMMON, 334 1.3 christos OPTION_EXTRACT_DWO, 335 1.3 christos OPTION_EXTRACT_SYMBOL, 336 1.3 christos OPTION_FILE_ALIGNMENT, 337 1.3 christos OPTION_FORMATS_INFO, 338 1.3 christos OPTION_GAP_FILL, 339 1.3 christos OPTION_GLOBALIZE_SYMBOL, 340 1.3 christos OPTION_GLOBALIZE_SYMBOLS, 341 1.3 christos OPTION_HEAP, 342 1.3 christos OPTION_IMAGE_BASE, 343 1.3 christos OPTION_IMPURE, 344 1.3 christos OPTION_INTERLEAVE_WIDTH, 345 1.3 christos OPTION_KEEPGLOBAL_SYMBOLS, 346 1.7 christos OPTION_KEEP_FILE_SYMBOLS, 347 1.3 christos OPTION_KEEP_SECTION, 348 1.8 christos OPTION_KEEP_SYMBOLS, 349 1.3 christos OPTION_KEEP_SECTION_SYMBOLS, 350 1.3 christos OPTION_LOCALIZE_HIDDEN, 351 1.3 christos OPTION_LOCALIZE_SYMBOLS, 352 1.6 christos OPTION_LONG_SECTION_NAMES, 353 1.6 christos OPTION_MERGE_NOTES, 354 1.3 christos OPTION_NO_MERGE_NOTES, 355 1.3 christos OPTION_NO_CHANGE_WARNINGS, 356 1.3 christos OPTION_ONLY_KEEP_DEBUG, 357 1.3 christos OPTION_PAD_TO, 358 1.3 christos OPTION_PREFIX_ALLOC_SECTIONS, 359 1.3 christos OPTION_PREFIX_SECTIONS, 360 1.3 christos OPTION_PREFIX_SYMBOLS, 361 1.3 christos OPTION_PURE, 362 1.3 christos OPTION_READONLY_TEXT, 363 1.3 christos OPTION_REDEFINE_SYM, 364 1.3 christos OPTION_REDEFINE_SYMS, 365 1.6 christos OPTION_REMOVE_LEADING_CHAR, 366 1.3 christos OPTION_REMOVE_RELOCS, 367 1.3 christos OPTION_RENAME_SECTION, 368 1.7 christos OPTION_REVERSE_BYTES, 369 1.10 christos OPTION_PE_SECTION_ALIGNMENT, 370 1.3 christos OPTION_PLUGIN, 371 1.7 christos OPTION_SET_SECTION_FLAGS, 372 1.3 christos OPTION_SET_SECTION_ALIGNMENT, 373 1.3 christos OPTION_SET_START, 374 1.3 christos OPTION_SREC_FORCES3, 375 1.3 christos OPTION_SREC_LEN, 376 1.3 christos OPTION_STACK, 377 1.9 christos OPTION_STRIP_DWO, 378 1.3 christos OPTION_STRIP_SECTION_HEADERS, 379 1.3 christos OPTION_STRIP_SYMBOLS, 380 1.3 christos OPTION_STRIP_UNNEEDED, 381 1.3 christos OPTION_STRIP_UNNEEDED_SYMBOL, 382 1.3 christos OPTION_STRIP_UNNEEDED_SYMBOLS, 383 1.3 christos OPTION_SUBSYSTEM, 384 1.7 christos OPTION_UPDATE_SECTION, 385 1.3 christos OPTION_VERILOG_DATA_WIDTH, 386 1.3 christos OPTION_WEAKEN, 387 1.3 christos OPTION_WEAKEN_SYMBOLS, 388 1.3 christos OPTION_WRITABLE_TEXT 389 1.1 christos }; 390 1.1 christos 391 1.1 christos /* Options to handle if running as "strip". */ 392 1.1 christos 393 1.1 christos static struct option strip_options[] = 394 1.1 christos { 395 1.1 christos {"disable-deterministic-archives", no_argument, 0, 'U'}, 396 1.1 christos {"discard-all", no_argument, 0, 'x'}, 397 1.1 christos {"discard-locals", no_argument, 0, 'X'}, 398 1.1 christos {"enable-deterministic-archives", no_argument, 0, 'D'}, 399 1.1 christos {"format", required_argument, 0, 'F'}, /* Obsolete */ 400 1.1 christos {"help", no_argument, 0, 'h'}, 401 1.1 christos {"info", no_argument, 0, OPTION_FORMATS_INFO}, 402 1.1 christos {"input-format", required_argument, 0, 'I'}, /* Obsolete */ 403 1.8 christos {"input-target", required_argument, 0, 'I'}, 404 1.1 christos {"keep-section-symbols", no_argument, 0, OPTION_KEEP_SECTION_SYMBOLS}, 405 1.7 christos {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS}, 406 1.1 christos {"keep-section", required_argument, 0, OPTION_KEEP_SECTION}, 407 1.6 christos {"keep-symbol", required_argument, 0, 'K'}, 408 1.6 christos {"merge-notes", no_argument, 0, 'M'}, 409 1.1 christos {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES}, 410 1.3 christos {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG}, 411 1.1 christos {"output-file", required_argument, 0, 'o'}, 412 1.1 christos {"output-format", required_argument, 0, 'O'}, /* Obsolete */ 413 1.10 christos {"output-target", required_argument, 0, 'O'}, 414 1.1 christos {"plugin", required_argument, 0, OPTION_PLUGIN}, 415 1.1 christos {"preserve-dates", no_argument, 0, 'p'}, 416 1.6 christos {"remove-section", required_argument, 0, 'R'}, 417 1.9 christos {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS}, 418 1.1 christos {"strip-section-headers", no_argument, 0, OPTION_STRIP_SECTION_HEADERS}, 419 1.1 christos {"strip-all", no_argument, 0, 's'}, 420 1.1 christos {"strip-debug", no_argument, 0, 'S'}, 421 1.3 christos {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO}, 422 1.1 christos {"strip-symbol", required_argument, 0, 'N'}, 423 1.1 christos {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED}, 424 1.1 christos {"target", required_argument, 0, 'F'}, 425 1.1 christos {"verbose", no_argument, 0, 'v'}, 426 1.1 christos {"version", no_argument, 0, 'V'}, 427 1.1 christos {"wildcard", no_argument, 0, 'w'}, 428 1.1 christos {0, no_argument, 0, 0} 429 1.1 christos }; 430 1.1 christos 431 1.1 christos /* Options to handle if running as "objcopy". */ 432 1.1 christos 433 1.1 christos static struct option copy_options[] = 434 1.1 christos { 435 1.1 christos {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK}, 436 1.3 christos {"add-section", required_argument, 0, OPTION_ADD_SECTION}, 437 1.3 christos {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL}, 438 1.1 christos {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS}, 439 1.1 christos {"adjust-start", required_argument, 0, OPTION_CHANGE_START}, 440 1.1 christos {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES}, 441 1.1 christos {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS}, 442 1.1 christos {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE}, 443 1.1 christos {"binary-architecture", required_argument, 0, 'B'}, 444 1.1 christos {"byte", required_argument, 0, 'b'}, 445 1.1 christos {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES}, 446 1.1 christos {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR}, 447 1.1 christos {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS}, 448 1.1 christos {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA}, 449 1.1 christos {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA}, 450 1.1 christos {"change-start", required_argument, 0, OPTION_CHANGE_START}, 451 1.3 christos {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS}, 452 1.1 christos {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS}, 453 1.1 christos {"debugging", no_argument, 0, OPTION_DEBUGGING}, 454 1.1 christos {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS}, 455 1.1 christos {"disable-deterministic-archives", no_argument, 0, 'U'}, 456 1.1 christos {"discard-all", no_argument, 0, 'x'}, 457 1.3 christos {"discard-locals", no_argument, 0, 'X'}, 458 1.5 christos {"dump-section", required_argument, 0, OPTION_DUMP_SECTION}, 459 1.1 christos {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON}, 460 1.1 christos {"enable-deterministic-archives", no_argument, 0, 'D'}, 461 1.1 christos {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO}, 462 1.3 christos {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL}, 463 1.1 christos {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT}, 464 1.1 christos {"format", required_argument, 0, 'F'}, /* Obsolete */ 465 1.1 christos {"gap-fill", required_argument, 0, OPTION_GAP_FILL}, 466 1.1 christos {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL}, 467 1.3 christos {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS}, 468 1.1 christos {"heap", required_argument, 0, OPTION_HEAP}, 469 1.3 christos {"help", no_argument, 0, 'h'}, 470 1.1 christos {"image-base", required_argument, 0 , OPTION_IMAGE_BASE}, 471 1.1 christos {"impure", no_argument, 0, OPTION_IMPURE}, 472 1.1 christos {"info", no_argument, 0, OPTION_FORMATS_INFO}, 473 1.1 christos {"input-format", required_argument, 0, 'I'}, /* Obsolete */ 474 1.1 christos {"input-target", required_argument, 0, 'I'}, 475 1.1 christos {"interleave", optional_argument, 0, 'i'}, 476 1.1 christos {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH}, 477 1.1 christos {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS}, 478 1.1 christos {"keep-global-symbol", required_argument, 0, 'G'}, 479 1.7 christos {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS}, 480 1.1 christos {"keep-section", required_argument, 0, OPTION_KEEP_SECTION}, 481 1.1 christos {"keep-symbol", required_argument, 0, 'K'}, 482 1.8 christos {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS}, 483 1.1 christos {"keep-section-symbols", required_argument, 0, OPTION_KEEP_SECTION_SYMBOLS}, 484 1.1 christos {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN}, 485 1.1 christos {"localize-symbol", required_argument, 0, 'L'}, 486 1.1 christos {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS}, 487 1.6 christos {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES}, 488 1.6 christos {"merge-notes", no_argument, 0, 'M'}, 489 1.1 christos {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES}, 490 1.1 christos {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS}, 491 1.1 christos {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS}, 492 1.1 christos {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG}, 493 1.1 christos {"only-section", required_argument, 0, 'j'}, 494 1.1 christos {"output-format", required_argument, 0, 'O'}, /* Obsolete */ 495 1.1 christos {"output-target", required_argument, 0, 'O'}, 496 1.3 christos {"pad-to", required_argument, 0, OPTION_PAD_TO}, 497 1.3 christos {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS}, 498 1.1 christos {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS}, 499 1.1 christos {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS}, 500 1.1 christos {"preserve-dates", no_argument, 0, 'p'}, 501 1.1 christos {"pure", no_argument, 0, OPTION_PURE}, 502 1.1 christos {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT}, 503 1.1 christos {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM}, 504 1.1 christos {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS}, 505 1.1 christos {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR}, 506 1.6 christos {"remove-section", required_argument, 0, 'R'}, 507 1.9 christos {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS}, 508 1.1 christos {"strip-section-headers", no_argument, 0, OPTION_STRIP_SECTION_HEADERS}, 509 1.1 christos {"rename-section", required_argument, 0, OPTION_RENAME_SECTION}, 510 1.7 christos {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES}, 511 1.1 christos {"section-alignment", required_argument, 0, OPTION_PE_SECTION_ALIGNMENT}, 512 1.7 christos {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS}, 513 1.1 christos {"set-section-alignment", required_argument, 0, OPTION_SET_SECTION_ALIGNMENT}, 514 1.3 christos {"set-start", required_argument, 0, OPTION_SET_START}, 515 1.1 christos {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3}, 516 1.3 christos {"srec-len", required_argument, 0, OPTION_SREC_LEN}, 517 1.1 christos {"stack", required_argument, 0, OPTION_STACK}, 518 1.1 christos {"strip-all", no_argument, 0, 'S'}, 519 1.1 christos {"strip-debug", no_argument, 0, 'g'}, 520 1.3 christos {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO}, 521 1.3 christos {"strip-symbol", required_argument, 0, 'N'}, 522 1.1 christos {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS}, 523 1.1 christos {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED}, 524 1.1 christos {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL}, 525 1.3 christos {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS}, 526 1.1 christos {"subsystem", required_argument, 0, OPTION_SUBSYSTEM}, 527 1.3 christos {"target", required_argument, 0, 'F'}, 528 1.1 christos {"update-section", required_argument, 0, OPTION_UPDATE_SECTION}, 529 1.7 christos {"verbose", no_argument, 0, 'v'}, 530 1.1 christos {"verilog-data-width", required_argument, 0, OPTION_VERILOG_DATA_WIDTH}, 531 1.1 christos {"version", no_argument, 0, 'V'}, 532 1.1 christos {"weaken", no_argument, 0, OPTION_WEAKEN}, 533 1.1 christos {"weaken-symbol", required_argument, 0, 'W'}, 534 1.1 christos {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS}, 535 1.1 christos {"wildcard", no_argument, 0, 'w'}, 536 1.1 christos {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT}, 537 1.1 christos {0, no_argument, 0, 0} 538 1.1 christos }; 539 1.1 christos 540 1.1 christos /* IMPORTS */ 541 1.1 christos extern char *program_name; 542 1.1 christos 543 1.1 christos /* This flag distinguishes between strip and objcopy: 544 1.1 christos 1 means this is 'strip'; 0 means this is 'objcopy'. 545 1.10 christos -1 means if we should use argv[0] to decide. */ 546 1.1 christos #ifndef is_strip 547 1.10 christos extern int is_strip; 548 1.1 christos #endif 549 1.6 christos 550 1.1 christos /* The maximum length of an S record. This variable is defined in srec.c 551 1.6 christos and can be modified by the --srec-len parameter. */ 552 1.1 christos extern unsigned int _bfd_srec_len; 553 1.1 christos 554 1.6 christos /* Restrict the generation of Srecords to type S3 only. 555 1.1 christos This variable is defined in bfd/srec.c and can be toggled 556 1.8 christos on by the --srec-forceS3 command line switch. */ 557 1.1 christos extern bool _bfd_srec_forceS3; 558 1.7 christos 559 1.7 christos /* Width of data in bytes for verilog output. 560 1.7 christos This variable is declared in bfd/verilog.c and can be modified by 561 1.7 christos the --verilog-data-width parameter. */ 562 1.7 christos extern unsigned int VerilogDataWidth; 563 1.9 christos 564 1.9 christos /* Endianness of data for verilog output. 565 1.9 christos This variable is declared in bfd/verilog.c and is set in the 566 1.9 christos copy_object() function. */ 567 1.9 christos extern enum bfd_endian VerilogDataEndianness; 568 1.1 christos 569 1.10 christos /* Forward declarations. */ 570 1.10 christos static bool setup_section (bfd *, asection *, bfd *); 571 1.10 christos static bool setup_bfd_headers (bfd *, bfd *); 572 1.10 christos static bool copy_relocations_in_section (bfd *, asection *, bfd *); 573 1.1 christos static bool copy_section (bfd *, asection *, bfd *); 574 1.10 christos static int compare_section_lma (const void *, const void *); 575 1.8 christos static bool mark_symbols_used_in_relocations (bfd *, asection *, asymbol **); 576 1.1 christos static bool write_debugging_info (bfd *, void *, long *, asymbol ***); 577 1.5 christos static const char *lookup_sym_redefinition (const char *); 578 1.1 christos static const char *find_section_rename (const char *, flagword *); 579 1.6 christos 580 1.1 christos ATTRIBUTE_NORETURN static void 582 1.1 christos copy_usage (FILE *stream, int exit_status) 583 1.1 christos { 584 1.1 christos fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name); 585 1.1 christos fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n")); 586 1.1 christos fprintf (stream, _(" The options are:\n")); 587 1.1 christos fprintf (stream, _("\ 588 1.1 christos -I --input-target <bfdname> Assume input file is in format <bfdname>\n\ 589 1.1 christos -O --output-target <bfdname> Create an output file in format <bfdname>\n\ 590 1.1 christos -B --binary-architecture <arch> Set output arch, when input is arch-less\n\ 591 1.1 christos -F --target <bfdname> Set both input and output format to <bfdname>\n\ 592 1.1 christos --debugging Convert debugging information, if possible\n\ 593 1.1 christos -p --preserve-dates Copy modified/access timestamps to the output\n")); 594 1.1 christos if (DEFAULT_AR_DETERMINISTIC) 595 1.1 christos fprintf (stream, _("\ 596 1.1 christos -D --enable-deterministic-archives\n\ 597 1.1 christos Produce deterministic output when stripping archives (default)\n\ 598 1.1 christos -U --disable-deterministic-archives\n\ 599 1.1 christos Disable -D behavior\n")); 600 1.1 christos else 601 1.1 christos fprintf (stream, _("\ 602 1.1 christos -D --enable-deterministic-archives\n\ 603 1.1 christos Produce deterministic output when stripping archives\n\ 604 1.1 christos -U --disable-deterministic-archives\n\ 605 1.1 christos Disable -D behavior (default)\n")); 606 1.1 christos fprintf (stream, _("\ 607 1.1 christos -j --only-section <name> Only copy section <name> into the output\n\ 608 1.6 christos --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\ 609 1.9 christos -R --remove-section <name> Remove section <name> from the output\n\ 610 1.1 christos --remove-relocations <name> Remove relocations from section <name>\n\ 611 1.1 christos --strip-section-headers Strip section header from the output\n\ 612 1.1 christos -S --strip-all Remove all symbol and relocation information\n\ 613 1.1 christos -g --strip-debug Remove all debugging symbols & sections\n\ 614 1.1 christos --strip-dwo Remove all DWO sections\n\ 615 1.1 christos --strip-unneeded Remove all symbols not needed by relocations\n\ 616 1.1 christos -N --strip-symbol <name> Do not copy symbol <name>\n\ 617 1.1 christos --strip-unneeded-symbol <name>\n\ 618 1.1 christos Do not copy symbol <name> unless needed by\n\ 619 1.1 christos relocations\n\ 620 1.1 christos --only-keep-debug Strip everything but the debug information\n\ 621 1.7 christos --extract-dwo Copy only DWO sections\n\ 622 1.1 christos --extract-symbol Remove section contents but keep symbols\n\ 623 1.8 christos --keep-section <name> Do not strip section <name>\n\ 624 1.1 christos -K --keep-symbol <name> Do not strip symbol <name>\n\ 625 1.1 christos --keep-section-symbols Do not strip section symbols\n\ 626 1.1 christos --keep-file-symbols Do not strip file symbol(s)\n\ 627 1.1 christos --localize-hidden Turn all ELF hidden symbols into locals\n\ 628 1.1 christos -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\ 629 1.1 christos --globalize-symbol <name> Force symbol <name> to be marked as a global\n\ 630 1.1 christos -G --keep-global-symbol <name> Localize all symbols except <name>\n\ 631 1.1 christos -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\ 632 1.1 christos --weaken Force all global symbols to be marked as weak\n\ 633 1.1 christos -w --wildcard Permit wildcard in symbol comparison\n\ 634 1.3 christos -x --discard-all Remove all non-global symbols\n\ 635 1.1 christos -X --discard-locals Remove any compiler-generated symbols\n\ 636 1.1 christos -i --interleave[=<number>] Only copy N out of every <number> bytes\n\ 637 1.1 christos --interleave-width <number> Set N for --interleave\n\ 638 1.1 christos -b --byte <num> Select byte <num> in every interleaved block\n\ 639 1.1 christos --gap-fill <val> Fill gaps between sections with <val>\n\ 640 1.1 christos --pad-to <addr> Pad the last section up to address <addr>\n\ 641 1.1 christos --set-start <addr> Set the start address to <addr>\n\ 642 1.1 christos {--change-start|--adjust-start} <incr>\n\ 643 1.1 christos Add <incr> to the start address\n\ 644 1.1 christos {--change-addresses|--adjust-vma} <incr>\n\ 645 1.1 christos Add <incr> to LMA, VMA and start addresses\n\ 646 1.1 christos {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\ 647 1.1 christos Change LMA and VMA of section <name> by <val>\n\ 648 1.1 christos --change-section-lma <name>{=|+|-}<val>\n\ 649 1.1 christos Change the LMA of section <name> by <val>\n\ 650 1.1 christos --change-section-vma <name>{=|+|-}<val>\n\ 651 1.1 christos Change the VMA of section <name> by <val>\n\ 652 1.1 christos {--[no-]change-warnings|--[no-]adjust-warnings}\n\ 653 1.1 christos Warn if a named section does not exist\n\ 654 1.7 christos --set-section-flags <name>=<flags>\n\ 655 1.7 christos Set section <name>'s properties to <flags>\n\ 656 1.1 christos --set-section-alignment <name>=<align>\n\ 657 1.3 christos Set section <name>'s alignment to <align> bytes\n\ 658 1.3 christos --add-section <name>=<file> Add section <name> found in <file> to output\n\ 659 1.3 christos --update-section <name>=<file>\n\ 660 1.3 christos Update contents of section <name> with\n\ 661 1.1 christos contents found in <file>\n\ 662 1.1 christos --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\ 663 1.1 christos --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\ 664 1.1 christos --long-section-names {enable|disable|keep}\n\ 665 1.1 christos Handle long section names in Coff objects.\n\ 666 1.1 christos --change-leading-char Force output format's leading character style\n\ 667 1.1 christos --remove-leading-char Remove leading character from global symbols\n\ 668 1.1 christos --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\ 669 1.1 christos --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\ 670 1.1 christos --redefine-syms <file> --redefine-sym for all symbol pairs \n\ 671 1.1 christos listed in <file>\n\ 672 1.1 christos --srec-len <number> Restrict the length of generated Srecords\n\ 673 1.1 christos --srec-forceS3 Restrict the type of generated Srecords to S3\n\ 674 1.1 christos --strip-symbols <file> -N for all symbols listed in <file>\n\ 675 1.1 christos --strip-unneeded-symbols <file>\n\ 676 1.1 christos --strip-unneeded-symbol for all symbols listed\n\ 677 1.1 christos in <file>\n\ 678 1.1 christos --keep-symbols <file> -K for all symbols listed in <file>\n\ 679 1.1 christos --localize-symbols <file> -L for all symbols listed in <file>\n\ 680 1.1 christos --globalize-symbols <file> --globalize-symbol for all in <file>\n\ 681 1.3 christos --keep-global-symbols <file> -G for all symbols listed in <file>\n\ 682 1.1 christos --weaken-symbols <file> -W for all symbols listed in <file>\n\ 683 1.1 christos --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\ 684 1.1 christos --alt-machine-code <index> Use the target's <index>'th alternative machine\n\ 685 1.1 christos --writable-text Mark the output text as writable\n\ 686 1.1 christos --readonly-text Make the output text write protected\n\ 687 1.1 christos --pure Mark the output file as demand paged\n\ 688 1.1 christos --impure Mark the output file as impure\n\ 689 1.1 christos --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\ 690 1.1 christos --prefix-sections <prefix> Add <prefix> to start of every section name\n\ 691 1.1 christos --prefix-alloc-sections <prefix>\n\ 692 1.1 christos Add <prefix> to start of every allocatable\n\ 693 1.1 christos section name\n\ 694 1.1 christos --file-alignment <num> Set PE file alignment to <num>\n\ 695 1.1 christos --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\ 696 1.1 christos <commit>\n\ 697 1.1 christos --image-base <address> Set PE image base to <address>\n\ 698 1.1 christos --section-alignment <num> Set PE section alignment to <num>\n\ 699 1.1 christos --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\ 700 1.1 christos <commit>\n\ 701 1.9 christos --subsystem <name>[:<version>]\n\ 702 1.9 christos Set PE subsystem to <name> [& <version>]\n\ 703 1.1 christos --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\ 704 1.5 christos Compress DWARF debug sections\n\ 705 1.5 christos --decompress-debug-sections Decompress DWARF debug sections using zlib\n\ 706 1.7 christos --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\ 707 1.6 christos type\n\ 708 1.6 christos --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\ 709 1.1 christos -M --merge-notes Remove redundant entries in note sections\n\ 710 1.1 christos --no-merge-notes Do not attempt to remove redundant notes (default)\n\ 711 1.1 christos -v --verbose List all object files modified\n\ 712 1.1 christos @<file> Read options from <file>\n\ 713 1.1 christos -V --version Display this program's version number\n\ 714 1.1 christos -h --help Display this output\n\ 715 1.1 christos --info List object formats & architectures supported\n\ 716 1.1 christos ")); 717 1.1 christos list_supported_targets (program_name, stream); 718 1.1 christos if (REPORT_BUGS_TO[0] && exit_status == 0) 719 1.1 christos fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO); 720 1.1 christos exit (exit_status); 721 1.6 christos } 722 1.1 christos 723 1.1 christos ATTRIBUTE_NORETURN static void 724 1.1 christos strip_usage (FILE *stream, int exit_status) 725 1.1 christos { 726 1.1 christos fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name); 727 1.1 christos fprintf (stream, _(" Removes symbols and sections from files\n")); 728 1.1 christos fprintf (stream, _(" The options are:\n")); 729 1.1 christos fprintf (stream, _("\ 730 1.1 christos -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\ 731 1.1 christos -O --output-target=<bfdname> Create an output file in format <bfdname>\n\ 732 1.1 christos -F --target=<bfdname> Set both input and output format to <bfdname>\n\ 733 1.1 christos -p --preserve-dates Copy modified/access timestamps to the output\n\ 734 1.1 christos ")); 735 1.1 christos if (DEFAULT_AR_DETERMINISTIC) 736 1.1 christos fprintf (stream, _("\ 737 1.1 christos -D --enable-deterministic-archives\n\ 738 1.1 christos Produce deterministic output when stripping archives (default)\n\ 739 1.1 christos -U --disable-deterministic-archives\n\ 740 1.1 christos Disable -D behavior\n")); 741 1.1 christos else 742 1.1 christos fprintf (stream, _("\ 743 1.1 christos -D --enable-deterministic-archives\n\ 744 1.1 christos Produce deterministic output when stripping archives\n\ 745 1.1 christos -U --disable-deterministic-archives\n\ 746 1.3 christos Disable -D behavior (default)\n")); 747 1.6 christos fprintf (stream, _("\ 748 1.9 christos -R --remove-section=<name> Also remove section <name> from the output\n\ 749 1.1 christos --remove-relocations <name> Remove relocations from section <name>\n\ 750 1.1 christos --strip-section-headers Strip section headers from the output\n\ 751 1.1 christos -s --strip-all Remove all symbol and relocation information\n\ 752 1.1 christos -g -S -d --strip-debug Remove all debugging symbols & sections\n\ 753 1.1 christos --strip-dwo Remove all DWO sections\n\ 754 1.6 christos --strip-unneeded Remove all symbols not needed by relocations\n\ 755 1.6 christos --only-keep-debug Strip everything but the debug information\n\ 756 1.1 christos -M --merge-notes Remove redundant entries in note sections (default)\n\ 757 1.7 christos --no-merge-notes Do not attempt to remove redundant notes\n\ 758 1.1 christos -N --strip-symbol=<name> Do not copy symbol <name>\n\ 759 1.8 christos --keep-section=<name> Do not strip section <name>\n\ 760 1.1 christos -K --keep-symbol=<name> Do not strip symbol <name>\n\ 761 1.1 christos --keep-section-symbols Do not strip section symbols\n\ 762 1.1 christos --keep-file-symbols Do not strip file symbol(s)\n\ 763 1.1 christos -w --wildcard Permit wildcard in symbol comparison\n\ 764 1.1 christos -x --discard-all Remove all non-global symbols\n\ 765 1.1 christos -X --discard-locals Remove any compiler-generated symbols\n\ 766 1.1 christos -v --verbose List all object files modified\n\ 767 1.1 christos -V --version Display this program's version number\n\ 768 1.1 christos -h --help Display this output\n\ 769 1.1 christos --info List object formats & architectures supported\n\ 770 1.10 christos -o <file> Place stripped output into <file>\n\ 771 1.10 christos ")); 772 1.10 christos #if BFD_SUPPORTS_PLUGINS 773 1.10 christos fprintf (stream, _("\ 774 1.1 christos --plugin NAME Load the specified plugin\n")); 775 1.1 christos #endif 776 1.1 christos 777 1.1 christos list_supported_targets (program_name, stream); 778 1.1 christos if (REPORT_BUGS_TO[0] && exit_status == 0) 779 1.1 christos fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO); 780 1.1 christos exit (exit_status); 781 1.1 christos } 782 1.1 christos 783 1.1 christos /* Parse section flags into a flagword, with a fatal error if the 784 1.1 christos string can't be parsed. */ 785 1.1 christos 786 1.1 christos static flagword 787 1.1 christos parse_flags (const char *s) 788 1.1 christos { 789 1.1 christos flagword ret; 790 1.1 christos const char *snext; 791 1.1 christos int len; 792 1.1 christos 793 1.1 christos ret = SEC_NO_FLAGS; 794 1.1 christos 795 1.1 christos do 796 1.1 christos { 797 1.1 christos snext = strchr (s, ','); 798 1.1 christos if (snext == NULL) 799 1.1 christos len = strlen (s); 800 1.1 christos else 801 1.1 christos { 802 1.1 christos len = snext - s; 803 1.1 christos ++snext; 804 1.1 christos } 805 1.5 christos 806 1.5 christos if (0) ; 807 1.1 christos #define PARSE_FLAG(fname,fval) \ 808 1.1 christos else if (strncasecmp (fname, s, len) == 0) ret |= fval 809 1.1 christos PARSE_FLAG ("alloc", SEC_ALLOC); 810 1.1 christos PARSE_FLAG ("load", SEC_LOAD); 811 1.1 christos PARSE_FLAG ("noload", SEC_NEVER_LOAD); 812 1.1 christos PARSE_FLAG ("readonly", SEC_READONLY); 813 1.1 christos PARSE_FLAG ("debug", SEC_DEBUGGING); 814 1.1 christos PARSE_FLAG ("code", SEC_CODE); 815 1.8 christos PARSE_FLAG ("data", SEC_DATA); 816 1.1 christos PARSE_FLAG ("rom", SEC_ROM); 817 1.1 christos PARSE_FLAG ("exclude", SEC_EXCLUDE); 818 1.3 christos PARSE_FLAG ("share", SEC_COFF_SHARED); 819 1.3 christos PARSE_FLAG ("contents", SEC_HAS_CONTENTS); 820 1.9 christos PARSE_FLAG ("merge", SEC_MERGE); 821 1.1 christos PARSE_FLAG ("strings", SEC_STRINGS); 822 1.1 christos PARSE_FLAG ("large", SEC_ELF_LARGE); 823 1.1 christos #undef PARSE_FLAG 824 1.1 christos else 825 1.1 christos { 826 1.1 christos char *copy; 827 1.1 christos 828 1.1 christos copy = (char *) xmalloc (len + 1); 829 1.1 christos strncpy (copy, s, len); 830 1.9 christos copy[len] = '\0'; 831 1.9 christos non_fatal (_("unrecognized section flag `%s'"), copy); 832 1.9 christos fatal (_ ("supported flags: %s"), 833 1.9 christos "alloc, load, noload, readonly, debug, code, data, rom, " 834 1.3 christos "exclude, contents, merge, strings, (COFF specific) share, " 835 1.3 christos "(ELF x86-64 specific) large"); 836 1.3 christos } 837 1.3 christos 838 1.3 christos s = snext; 839 1.3 christos } 840 1.3 christos while (s != NULL); 841 1.3 christos 842 1.3 christos return ret; 843 1.3 christos } 844 1.3 christos 845 1.3 christos /* Parse symbol flags into a flagword, with a fatal error if the 846 1.3 christos string can't be parsed. */ 847 1.7 christos 848 1.3 christos static flagword 849 1.3 christos parse_symflags (const char *s, const char **other) 850 1.3 christos { 851 1.5 christos flagword ret; 852 1.3 christos const char *snext; 853 1.3 christos size_t len; 854 1.3 christos 855 1.3 christos ret = BSF_NO_FLAGS; 856 1.3 christos 857 1.3 christos do 858 1.3 christos { 859 1.5 christos snext = strchr (s, ','); 860 1.3 christos if (snext == NULL) 861 1.3 christos len = strlen (s); 862 1.3 christos else 863 1.3 christos { 864 1.3 christos len = snext - s; 865 1.3 christos ++snext; 866 1.5 christos } 867 1.5 christos 868 1.5 christos #define PARSE_FLAG(fname, fval) \ 869 1.3 christos else if (len == sizeof fname - 1 \ 870 1.3 christos && strncasecmp (fname, s, len) == 0) \ 871 1.5 christos ret |= fval 872 1.5 christos 873 1.5 christos #define PARSE_OTHER(fname, fval) \ 874 1.3 christos else if (len >= sizeof fname \ 875 1.5 christos && strncasecmp (fname, s, sizeof fname - 1) == 0) \ 876 1.3 christos fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1) 877 1.3 christos 878 1.3 christos if (0) ; 879 1.3 christos PARSE_FLAG ("local", BSF_LOCAL); 880 1.3 christos PARSE_FLAG ("global", BSF_GLOBAL); 881 1.3 christos PARSE_FLAG ("export", BSF_EXPORT); 882 1.3 christos PARSE_FLAG ("debug", BSF_DEBUGGING); 883 1.3 christos PARSE_FLAG ("function", BSF_FUNCTION); 884 1.3 christos PARSE_FLAG ("weak", BSF_WEAK); 885 1.3 christos PARSE_FLAG ("section", BSF_SECTION_SYM); 886 1.3 christos PARSE_FLAG ("constructor", BSF_CONSTRUCTOR); 887 1.3 christos PARSE_FLAG ("warning", BSF_WARNING); 888 1.3 christos PARSE_FLAG ("indirect", BSF_INDIRECT); 889 1.3 christos PARSE_FLAG ("file", BSF_FILE); 890 1.3 christos PARSE_FLAG ("object", BSF_OBJECT); 891 1.3 christos PARSE_FLAG ("synthetic", BSF_SYNTHETIC); 892 1.3 christos PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION); 893 1.3 christos PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT); 894 1.3 christos PARSE_OTHER ("before=", *other); 895 1.3 christos 896 1.3 christos #undef PARSE_FLAG 897 1.3 christos #undef PARSE_OTHER 898 1.3 christos else 899 1.3 christos { 900 1.3 christos char *copy; 901 1.3 christos 902 1.3 christos copy = (char *) xmalloc (len + 1); 903 1.3 christos strncpy (copy, s, len); 904 1.3 christos copy[len] = '\0'; 905 1.5 christos non_fatal (_("unrecognized symbol flag `%s'"), copy); 906 1.5 christos fatal (_("supported flags: %s"), 907 1.5 christos "local, global, export, debug, function, weak, section, " 908 1.1 christos "constructor, warning, indirect, file, object, synthetic, " 909 1.1 christos "indirect-function, unique-object, before=<othersym>"); 910 1.1 christos } 911 1.1 christos 912 1.1 christos s = snext; 913 1.1 christos } 914 1.1 christos while (s != NULL); 915 1.1 christos 916 1.1 christos return ret; 917 1.3 christos } 918 1.3 christos 919 1.3 christos /* Find and optionally add an entry in the change_sections list. 920 1.3 christos 921 1.3 christos We need to be careful in how we match section names because of the support 922 1.3 christos for wildcard characters. For example suppose that the user has invoked 923 1.3 christos objcopy like this: 924 1.3 christos 925 1.3 christos --set-section-flags .debug_*=debug 926 1.3 christos --set-section-flags .debug_str=readonly,debug 927 1.3 christos --change-section-address .debug_*ranges=0x1000 928 1.3 christos 929 1.3 christos With the idea that all debug sections will receive the DEBUG flag, the 930 1.3 christos .debug_str section will also receive the READONLY flag and the 931 1.3 christos .debug_ranges and .debug_aranges sections will have their address set to 932 1.3 christos 0x1000. (This may not make much sense, but it is just an example). 933 1.3 christos 934 1.3 christos When adding the section name patterns to the section list we need to make 935 1.3 christos sure that previous entries do not match with the new entry, unless the 936 1.3 christos match is exact. (In which case we assume that the user is overriding 937 1.3 christos the previous entry with the new context). 938 1.3 christos 939 1.3 christos When matching real section names to the section list we make use of the 940 1.3 christos wildcard characters, but we must do so in context. Eg if we are setting 941 1.3 christos section addresses then we match for .debug_ranges but not for .debug_info. 942 1.3 christos 943 1.1 christos Finally, if ADD is false and we do find a match, we mark the section list 944 1.1 christos entry as used. */ 945 1.8 christos 946 1.1 christos static struct section_list * 947 1.6 christos find_section_list (const char *name, bool add, unsigned int context) 948 1.1 christos { 949 1.3 christos struct section_list *p, *match = NULL; 950 1.3 christos 951 1.1 christos /* assert ((context & ((1 << 7) - 1)) != 0); */ 952 1.3 christos 953 1.3 christos for (p = change_sections; p != NULL; p = p->next) 954 1.3 christos { 955 1.3 christos if (add) 956 1.3 christos { 957 1.3 christos if (strcmp (p->pattern, name) == 0) 958 1.3 christos { 959 1.3 christos /* Check for context conflicts. */ 960 1.3 christos if (((p->context & SECTION_CONTEXT_REMOVE) 961 1.3 christos && (context & SECTION_CONTEXT_COPY)) 962 1.3 christos || ((context & SECTION_CONTEXT_REMOVE) 963 1.3 christos && (p->context & SECTION_CONTEXT_COPY))) 964 1.3 christos fatal (_("error: %s both copied and removed"), name); 965 1.3 christos 966 1.3 christos if (((p->context & SECTION_CONTEXT_SET_VMA) 967 1.3 christos && (context & SECTION_CONTEXT_ALTER_VMA)) 968 1.3 christos || ((context & SECTION_CONTEXT_SET_VMA) 969 1.3 christos && (context & SECTION_CONTEXT_ALTER_VMA))) 970 1.3 christos fatal (_("error: %s both sets and alters VMA"), name); 971 1.3 christos 972 1.3 christos if (((p->context & SECTION_CONTEXT_SET_LMA) 973 1.3 christos && (context & SECTION_CONTEXT_ALTER_LMA)) 974 1.3 christos || ((context & SECTION_CONTEXT_SET_LMA) 975 1.3 christos && (context & SECTION_CONTEXT_ALTER_LMA))) 976 1.3 christos fatal (_("error: %s both sets and alters LMA"), name); 977 1.3 christos 978 1.3 christos /* Extend the context. */ 979 1.3 christos p->context |= context; 980 1.3 christos return p; 981 1.3 christos } 982 1.3 christos } 983 1.6 christos /* If we are not adding a new name/pattern then 984 1.6 christos only check for a match if the context applies. */ 985 1.6 christos else if (p->context & context) 986 1.6 christos { 987 1.6 christos /* We could check for the presence of wildchar characters 988 1.6 christos first and choose between calling strcmp and fnmatch, 989 1.6 christos but is that really worth it ? */ 990 1.6 christos if (p->pattern [0] == '!') 991 1.6 christos { 992 1.8 christos if (fnmatch (p->pattern + 1, name, 0) == 0) 993 1.6 christos { 994 1.6 christos p->used = true; 995 1.6 christos return NULL; 996 1.6 christos } 997 1.6 christos } 998 1.6 christos else 999 1.6 christos { 1000 1.6 christos if (fnmatch (p->pattern, name, 0) == 0) 1001 1.6 christos { 1002 1.6 christos if (match == NULL) 1003 1.6 christos match = p; 1004 1.6 christos } 1005 1.3 christos } 1006 1.1 christos } 1007 1.1 christos } 1008 1.6 christos 1009 1.6 christos if (! add) 1010 1.8 christos { 1011 1.6 christos if (match != NULL) 1012 1.6 christos match->used = true; 1013 1.1 christos return match; 1014 1.1 christos } 1015 1.3 christos 1016 1.8 christos p = (struct section_list *) xmalloc (sizeof (struct section_list)); 1017 1.3 christos p->pattern = name; 1018 1.1 christos p->used = false; 1019 1.1 christos p->context = context; 1020 1.1 christos p->vma_val = 0; 1021 1.7 christos p->lma_val = 0; 1022 1.1 christos p->flags = 0; 1023 1.1 christos p->alignment = 0; 1024 1.1 christos p->next = change_sections; 1025 1.1 christos change_sections = p; 1026 1.1 christos 1027 1.1 christos return p; 1028 1.6 christos } 1029 1.6 christos 1030 1.6 christos /* S1 is the entry node already in the table, S2 is the key node. */ 1031 1.6 christos 1032 1.6 christos static int 1033 1.6 christos eq_string_redefnode (const void *s1, const void *s2) 1034 1.6 christos { 1035 1.6 christos struct redefine_node *node1 = (struct redefine_node *) s1; 1036 1.6 christos struct redefine_node *node2 = (struct redefine_node *) s2; 1037 1.6 christos return !strcmp ((const char *) node1->source, (const char *) node2->source); 1038 1.6 christos } 1039 1.6 christos 1040 1.6 christos /* P is redefine node. Hash value is generated from its "source" filed. */ 1041 1.6 christos 1042 1.6 christos static hashval_t 1043 1.6 christos htab_hash_redefnode (const void *p) 1044 1.6 christos { 1045 1.6 christos struct redefine_node *redefnode = (struct redefine_node *) p; 1046 1.6 christos return htab_hash_string (redefnode->source); 1047 1.6 christos } 1048 1.6 christos 1049 1.6 christos /* Create hashtab used for redefine node. */ 1050 1.6 christos 1051 1.6 christos static htab_t 1052 1.6 christos create_symbol2redef_htab (void) 1053 1.6 christos { 1054 1.6 christos return htab_create_alloc (16, htab_hash_redefnode, eq_string_redefnode, NULL, 1055 1.6 christos xcalloc, free); 1056 1.1 christos } 1057 1.1 christos 1058 1.1 christos static htab_t 1059 1.8 christos create_symbol_htab (void) 1060 1.8 christos { 1061 1.1 christos return htab_create_alloc (16, htab_hash_string, htab_eq_string, NULL, 1062 1.1 christos xcalloc, free); 1063 1.1 christos } 1064 1.1 christos 1065 1.1 christos static void 1066 1.1 christos create_symbol_htabs (void) 1067 1.1 christos { 1068 1.1 christos strip_specific_htab = create_symbol_htab (); 1069 1.1 christos strip_unneeded_htab = create_symbol_htab (); 1070 1.1 christos keep_specific_htab = create_symbol_htab (); 1071 1.1 christos localize_specific_htab = create_symbol_htab (); 1072 1.1 christos globalize_specific_htab = create_symbol_htab (); 1073 1.6 christos keepglobal_specific_htab = create_symbol_htab (); 1074 1.6 christos weaken_specific_htab = create_symbol_htab (); 1075 1.6 christos redefine_specific_htab = create_symbol2redef_htab (); 1076 1.6 christos /* As there is no bidirectional hash table in libiberty, need a reverse table 1077 1.1 christos to check duplicated target string. */ 1078 1.1 christos redefine_specific_reverse_htab = create_symbol_htab (); 1079 1.9 christos } 1080 1.9 christos 1081 1.9 christos static void 1082 1.9 christos delete_symbol_htabs (void) 1083 1.9 christos { 1084 1.9 christos htab_delete (strip_specific_htab); 1085 1.9 christos htab_delete (strip_unneeded_htab); 1086 1.9 christos htab_delete (keep_specific_htab); 1087 1.9 christos htab_delete (localize_specific_htab); 1088 1.9 christos htab_delete (globalize_specific_htab); 1089 1.9 christos htab_delete (keepglobal_specific_htab); 1090 1.9 christos htab_delete (weaken_specific_htab); 1091 1.9 christos htab_delete (redefine_specific_htab); 1092 1.9 christos htab_delete (redefine_specific_reverse_htab); 1093 1.9 christos 1094 1.9 christos free (isympp); 1095 1.9 christos if (osympp != isympp) 1096 1.9 christos free (osympp); 1097 1.1 christos } 1098 1.1 christos 1099 1.1 christos /* Add a symbol to strip_specific_list. */ 1100 1.1 christos 1101 1.1 christos static void 1102 1.1 christos add_specific_symbol (const char *name, htab_t htab) 1103 1.1 christos { 1104 1.1 christos *htab_find_slot (htab, name, INSERT) = (char *) name; 1105 1.6 christos } 1106 1.6 christos 1107 1.6 christos /* Like add_specific_symbol, but the element type is void *. */ 1108 1.6 christos 1109 1.6 christos static void 1110 1.6 christos add_specific_symbol_node (const void *node, htab_t htab) 1111 1.6 christos { 1112 1.6 christos *htab_find_slot (htab, node, INSERT) = (void *) node; 1113 1.1 christos } 1114 1.1 christos 1115 1.1 christos /* Add symbols listed in `filename' to strip_specific_list. */ 1116 1.1 christos 1117 1.1 christos #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t') 1118 1.1 christos #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0') 1119 1.7 christos 1120 1.1 christos static void 1121 1.1 christos add_specific_symbols (const char *filename, htab_t htab, char **buffer_p) 1122 1.1 christos { 1123 1.1 christos off_t size; 1124 1.1 christos FILE * f; 1125 1.1 christos char * line; 1126 1.1 christos char * buffer; 1127 1.1 christos unsigned int line_count; 1128 1.1 christos 1129 1.1 christos size = get_file_size (filename); 1130 1.1 christos if (size == 0) 1131 1.1 christos { 1132 1.1 christos status = 1; 1133 1.1 christos return; 1134 1.1 christos } 1135 1.1 christos 1136 1.1 christos buffer = (char *) xmalloc (size + 2); 1137 1.1 christos f = fopen (filename, FOPEN_RT); 1138 1.1 christos if (f == NULL) 1139 1.1 christos fatal (_("cannot open '%s': %s"), filename, strerror (errno)); 1140 1.1 christos 1141 1.1 christos if (fread (buffer, 1, size, f) == 0 || ferror (f)) 1142 1.1 christos fatal (_("%s: fread failed"), filename); 1143 1.1 christos 1144 1.1 christos fclose (f); 1145 1.1 christos buffer [size] = '\n'; 1146 1.1 christos buffer [size + 1] = '\0'; 1147 1.1 christos 1148 1.1 christos line_count = 1; 1149 1.1 christos 1150 1.1 christos for (line = buffer; * line != '\0'; line ++) 1151 1.1 christos { 1152 1.1 christos char * eol; 1153 1.8 christos char * name; 1154 1.1 christos char * name_end; 1155 1.1 christos int finished = false; 1156 1.1 christos 1157 1.1 christos for (eol = line;; eol ++) 1158 1.1 christos { 1159 1.1 christos switch (* eol) 1160 1.1 christos { 1161 1.1 christos case '\n': 1162 1.1 christos * eol = '\0'; 1163 1.1 christos /* Cope with \n\r. */ 1164 1.8 christos if (eol[1] == '\r') 1165 1.1 christos ++ eol; 1166 1.1 christos finished = true; 1167 1.1 christos break; 1168 1.1 christos 1169 1.1 christos case '\r': 1170 1.1 christos * eol = '\0'; 1171 1.1 christos /* Cope with \r\n. */ 1172 1.8 christos if (eol[1] == '\n') 1173 1.1 christos ++ eol; 1174 1.1 christos finished = true; 1175 1.1 christos break; 1176 1.8 christos 1177 1.1 christos case 0: 1178 1.1 christos finished = true; 1179 1.1 christos break; 1180 1.1 christos 1181 1.1 christos case '#': 1182 1.1 christos /* Line comment, Terminate the line here, in case a 1183 1.1 christos name is present and then allow the rest of the 1184 1.1 christos loop to find the real end of the line. */ 1185 1.1 christos * eol = '\0'; 1186 1.1 christos break; 1187 1.1 christos 1188 1.1 christos default: 1189 1.1 christos break; 1190 1.1 christos } 1191 1.1 christos 1192 1.1 christos if (finished) 1193 1.1 christos break; 1194 1.1 christos } 1195 1.1 christos 1196 1.1 christos /* A name may now exist somewhere between 'line' and 'eol'. 1197 1.1 christos Strip off leading whitespace and trailing whitespace, 1198 1.1 christos then add it to the list. */ 1199 1.1 christos for (name = line; IS_WHITESPACE (* name); name ++) 1200 1.1 christos ; 1201 1.1 christos for (name_end = name; 1202 1.1 christos (! IS_WHITESPACE (* name_end)) 1203 1.1 christos && (! IS_LINE_TERMINATOR (* name_end)); 1204 1.1 christos name_end ++) 1205 1.1 christos ; 1206 1.1 christos 1207 1.1 christos if (! IS_LINE_TERMINATOR (* name_end)) 1208 1.1 christos { 1209 1.1 christos char * extra; 1210 1.1 christos 1211 1.1 christos for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++) 1212 1.1 christos ; 1213 1.1 christos 1214 1.1 christos if (! IS_LINE_TERMINATOR (* extra)) 1215 1.1 christos non_fatal (_("%s:%d: Ignoring rubbish found on this line"), 1216 1.1 christos filename, line_count); 1217 1.1 christos } 1218 1.1 christos 1219 1.1 christos * name_end = '\0'; 1220 1.1 christos 1221 1.1 christos if (name_end > name) 1222 1.1 christos add_specific_symbol (name, htab); 1223 1.1 christos 1224 1.1 christos /* Advance line pointer to end of line. The 'eol ++' in the for 1225 1.1 christos loop above will then advance us to the start of the next line. */ 1226 1.1 christos line = eol; 1227 1.7 christos line_count ++; 1228 1.7 christos } 1229 1.7 christos 1230 1.7 christos /* Do not free the buffer. Parts of it will have been referenced 1231 1.1 christos in the calls to add_specific_symbol. */ 1232 1.1 christos *buffer_p = buffer; 1233 1.1 christos } 1234 1.1 christos 1235 1.1 christos /* See whether a symbol should be stripped or kept 1236 1.1 christos based on strip_specific_list and keep_symbols. */ 1237 1.1 christos 1238 1.1 christos static int 1239 1.1 christos is_specified_symbol_predicate (void **slot, void *data) 1240 1.1 christos { 1241 1.1 christos struct is_specified_symbol_predicate_data *d = 1242 1.1 christos (struct is_specified_symbol_predicate_data *) data; 1243 1.1 christos const char *slot_name = (char *) *slot; 1244 1.1 christos 1245 1.1 christos if (*slot_name != '!') 1246 1.1 christos { 1247 1.8 christos if (! fnmatch (slot_name, d->name, 0)) 1248 1.3 christos { 1249 1.3 christos d->found = true; 1250 1.1 christos /* Continue traversal, there might be a non-match rule. */ 1251 1.1 christos return 1; 1252 1.1 christos } 1253 1.1 christos } 1254 1.3 christos else 1255 1.1 christos { 1256 1.8 christos if (! fnmatch (slot_name + 1, d->name, 0)) 1257 1.1 christos { 1258 1.1 christos d->found = false; 1259 1.1 christos /* Stop traversal. */ 1260 1.1 christos return 0; 1261 1.1 christos } 1262 1.1 christos } 1263 1.1 christos 1264 1.1 christos /* Continue traversal. */ 1265 1.1 christos return 1; 1266 1.8 christos } 1267 1.1 christos 1268 1.1 christos static bool 1269 1.1 christos is_specified_symbol (const char *name, htab_t htab) 1270 1.1 christos { 1271 1.1 christos if (wildcard) 1272 1.1 christos { 1273 1.1 christos struct is_specified_symbol_predicate_data data; 1274 1.8 christos 1275 1.1 christos data.name = name; 1276 1.1 christos data.found = false; 1277 1.1 christos 1278 1.1 christos htab_traverse (htab, is_specified_symbol_predicate, &data); 1279 1.1 christos 1280 1.1 christos return data.found; 1281 1.1 christos } 1282 1.1 christos 1283 1.1 christos return htab_find (htab, name) != NULL; 1284 1.1 christos } 1285 1.1 christos 1286 1.8 christos /* Return TRUE if the section is a DWO section. */ 1287 1.1 christos 1288 1.1 christos static bool 1289 1.8 christos is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1290 1.8 christos { 1291 1.1 christos const char *name; 1292 1.8 christos int len; 1293 1.8 christos 1294 1.8 christos if (sec == NULL || (name = bfd_section_name (sec)) == NULL) 1295 1.8 christos return false; 1296 1.8 christos 1297 1.8 christos len = strlen (name); 1298 1.8 christos if (len < 5) 1299 1.8 christos return false; 1300 1.1 christos 1301 1.1 christos return startswith (name + len - 4, ".dwo"); 1302 1.3 christos } 1303 1.3 christos 1304 1.8 christos /* Return TRUE if section SEC is in the update list. */ 1305 1.3 christos 1306 1.3 christos static bool 1307 1.3 christos is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1308 1.3 christos { 1309 1.3 christos if (update_sections != NULL) 1310 1.3 christos { 1311 1.3 christos struct section_add *pupdate; 1312 1.5 christos 1313 1.5 christos for (pupdate = update_sections; 1314 1.3 christos pupdate != NULL; 1315 1.5 christos pupdate = pupdate->next) 1316 1.8 christos { 1317 1.5 christos if (strcmp (sec->name, pupdate->name) == 0) 1318 1.3 christos return true; 1319 1.3 christos } 1320 1.8 christos } 1321 1.3 christos 1322 1.3 christos return false; 1323 1.8 christos } 1324 1.7 christos 1325 1.6 christos static bool 1326 1.6 christos is_mergeable_note_section (bfd * abfd, asection * sec) 1327 1.6 christos { 1328 1.6 christos if (merge_notes 1329 1.6 christos && bfd_get_flavour (abfd) == bfd_target_elf_flavour 1330 1.6 christos && elf_section_data (sec)->this_hdr.sh_type == SHT_NOTE 1331 1.8 christos /* FIXME: We currently only support merging GNU_BUILD_NOTEs. 1332 1.8 christos We should add support for more note types. */ 1333 1.6 christos && (startswith (sec->name, GNU_BUILD_ATTRS_SECTION_NAME))) 1334 1.8 christos return true; 1335 1.6 christos 1336 1.6 christos return false; 1337 1.1 christos } 1338 1.1 christos 1339 1.8 christos /* See if a non-group section is being removed. */ 1340 1.1 christos 1341 1.1 christos static bool 1342 1.8 christos is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1343 1.7 christos { 1344 1.8 christos if (find_section_list (bfd_section_name (sec), false, SECTION_CONTEXT_KEEP) 1345 1.7 christos != NULL) 1346 1.1 christos return false; 1347 1.1 christos 1348 1.1 christos if (sections_removed || sections_copied) 1349 1.3 christos { 1350 1.1 christos struct section_list *p; 1351 1.8 christos struct section_list *q; 1352 1.3 christos 1353 1.8 christos p = find_section_list (bfd_section_name (sec), false, 1354 1.3 christos SECTION_CONTEXT_REMOVE); 1355 1.3 christos q = find_section_list (bfd_section_name (sec), false, 1356 1.3 christos SECTION_CONTEXT_COPY); 1357 1.3 christos 1358 1.7 christos if (p && q) 1359 1.3 christos fatal (_("error: section %s matches both remove and copy options"), 1360 1.5 christos bfd_section_name (sec)); 1361 1.7 christos if (p && is_update_section (abfd, sec)) 1362 1.1 christos fatal (_("error: section %s matches both update and remove options"), 1363 1.3 christos bfd_section_name (sec)); 1364 1.8 christos 1365 1.3 christos if (p != NULL) 1366 1.8 christos return true; 1367 1.1 christos if (sections_copied && q == NULL) 1368 1.1 christos return true; 1369 1.9 christos } 1370 1.9 christos 1371 1.9 christos /* Remove non-alloc sections for --strip-section-headers. */ 1372 1.9 christos if (strip_section_headers 1373 1.9 christos && (bfd_section_flags (sec) & SEC_ALLOC) == 0) 1374 1.7 christos return true; 1375 1.1 christos 1376 1.1 christos if ((bfd_section_flags (sec) & SEC_DEBUGGING) != 0) 1377 1.1 christos { 1378 1.1 christos if (strip_symbols == STRIP_DEBUG 1379 1.1 christos || strip_symbols == STRIP_UNNEEDED 1380 1.1 christos || strip_symbols == STRIP_ALL 1381 1.3 christos || discard_locals == LOCALS_ALL 1382 1.3 christos || convert_debugging) 1383 1.3 christos { 1384 1.8 christos /* By default we don't want to strip .reloc section. 1385 1.8 christos This section has for pe-coff special meaning. See 1386 1.8 christos pe-dll.c file in ld, and peXXigen.c in bfd for details. 1387 1.8 christos Similarly we do not want to strip debuglink sections. */ 1388 1.8 christos const char * kept_sections[] = 1389 1.8 christos { 1390 1.8 christos ".reloc", 1391 1.8 christos ".gnu_debuglink", 1392 1.8 christos ".gnu_debugaltlink" 1393 1.8 christos }; 1394 1.8 christos int i; 1395 1.8 christos 1396 1.8 christos for (i = ARRAY_SIZE (kept_sections);i--;) 1397 1.8 christos if (strcmp (bfd_section_name (sec), kept_sections[i]) == 0) 1398 1.8 christos break; 1399 1.3 christos if (i == -1) 1400 1.1 christos return true; 1401 1.1 christos } 1402 1.1 christos 1403 1.1 christos if (strip_symbols == STRIP_DWO) 1404 1.1 christos return is_dwo_section (abfd, sec); 1405 1.8 christos 1406 1.1 christos if (strip_symbols == STRIP_NONDEBUG) 1407 1.1 christos return false; 1408 1.1 christos } 1409 1.1 christos 1410 1.1 christos if (strip_symbols == STRIP_NONDWO) 1411 1.8 christos return !is_dwo_section (abfd, sec); 1412 1.1 christos 1413 1.1 christos return false; 1414 1.1 christos } 1415 1.1 christos 1416 1.8 christos /* See if a section is being removed. */ 1417 1.1 christos 1418 1.1 christos static bool 1419 1.1 christos is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec) 1420 1.8 christos { 1421 1.1 christos if (is_strip_section_1 (abfd, sec)) 1422 1.7 christos return true; 1423 1.1 christos 1424 1.1 christos if ((bfd_section_flags (sec) & SEC_GROUP) != 0) 1425 1.1 christos { 1426 1.1 christos asymbol *gsym; 1427 1.1 christos const char *gname; 1428 1.10 christos asection *elt, *first; 1429 1.6 christos 1430 1.6 christos gsym = bfd_group_signature (sec, isympp); 1431 1.8 christos /* Strip groups without a valid signature. */ 1432 1.6 christos if (gsym == NULL) 1433 1.1 christos return true; 1434 1.1 christos 1435 1.1 christos /* PR binutils/3181 1436 1.6 christos If we are going to strip the group signature symbol, then 1437 1.1 christos strip the group section too. */ 1438 1.1 christos gname = gsym->name; 1439 1.1 christos if ((strip_symbols == STRIP_ALL 1440 1.8 christos && !is_specified_symbol (gname, keep_specific_htab)) 1441 1.1 christos || is_specified_symbol (gname, strip_specific_htab)) 1442 1.1 christos return true; 1443 1.1 christos 1444 1.1 christos /* Remove the group section if all members are removed. */ 1445 1.1 christos first = elt = elf_next_in_group (sec); 1446 1.1 christos while (elt != NULL) 1447 1.8 christos { 1448 1.1 christos if (!is_strip_section_1 (abfd, elt)) 1449 1.1 christos return false; 1450 1.1 christos elt = elf_next_in_group (elt); 1451 1.1 christos if (elt == first) 1452 1.1 christos break; 1453 1.8 christos } 1454 1.1 christos 1455 1.1 christos return true; 1456 1.8 christos } 1457 1.1 christos 1458 1.1 christos return false; 1459 1.8 christos } 1460 1.3 christos 1461 1.3 christos static bool 1462 1.3 christos is_nondebug_keep_contents_section (bfd *ibfd, asection *isection) 1463 1.7 christos { 1464 1.7 christos /* Always keep ELF note sections. */ 1465 1.3 christos if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour) 1466 1.3 christos return elf_section_type (isection) == SHT_NOTE; 1467 1.3 christos 1468 1.3 christos /* Always keep the .buildid section for PE/COFF. 1469 1.3 christos 1470 1.3 christos Strictly, this should be written "always keep the section storing the debug 1471 1.7 christos directory", but that may be the .text section for objects produced by some 1472 1.7 christos tools, which it is not sensible to keep. */ 1473 1.3 christos if (bfd_get_flavour (ibfd) == bfd_target_coff_flavour) 1474 1.8 christos return strcmp (bfd_section_name (isection), ".buildid") == 0; 1475 1.3 christos 1476 1.3 christos return false; 1477 1.1 christos } 1478 1.1 christos 1479 1.8 christos /* Return true if SYM is a hidden symbol. */ 1480 1.1 christos 1481 1.1 christos static bool 1482 1.1 christos is_hidden_symbol (asymbol *sym) 1483 1.1 christos { 1484 1.8 christos elf_symbol_type *elf_sym; 1485 1.1 christos 1486 1.1 christos elf_sym = elf_symbol_from (sym); 1487 1.1 christos if (elf_sym != NULL) 1488 1.1 christos switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other)) 1489 1.1 christos { 1490 1.8 christos case STV_HIDDEN: 1491 1.1 christos case STV_INTERNAL: 1492 1.8 christos return true; 1493 1.1 christos } 1494 1.1 christos return false; 1495 1.7 christos } 1496 1.7 christos 1497 1.7 christos /* Empty name is hopefully never a valid symbol name. */ 1498 1.8 christos static const char * empty_name = ""; 1499 1.3 christos 1500 1.3 christos static bool 1501 1.3 christos need_sym_before (struct addsym_node **node, const char *sym) 1502 1.3 christos { 1503 1.3 christos int count; 1504 1.3 christos struct addsym_node *ptr = add_sym_list; 1505 1.3 christos 1506 1.3 christos /* 'othersym' symbols are at the front of the list. */ 1507 1.3 christos for (count = 0; count < add_symbols; count++) 1508 1.3 christos { 1509 1.7 christos if (!ptr->othersym) 1510 1.7 christos break; 1511 1.3 christos if (ptr->othersym == empty_name) 1512 1.3 christos continue; 1513 1.7 christos else if (strcmp (ptr->othersym, sym) == 0) 1514 1.7 christos { 1515 1.3 christos free ((char *) ptr->othersym); 1516 1.8 christos ptr->othersym = empty_name; 1517 1.3 christos *node = ptr; 1518 1.3 christos return true; 1519 1.3 christos } 1520 1.8 christos ptr = ptr->next; 1521 1.3 christos } 1522 1.3 christos return false; 1523 1.3 christos } 1524 1.3 christos 1525 1.3 christos static asymbol * 1526 1.5 christos create_new_symbol (struct addsym_node *ptr, bfd *obfd) 1527 1.3 christos { 1528 1.7 christos asymbol *sym = bfd_make_empty_symbol (obfd); 1529 1.3 christos 1530 1.3 christos bfd_set_asymbol_name (sym, ptr->symdef); 1531 1.3 christos sym->value = ptr->symval; 1532 1.3 christos sym->flags = ptr->flags; 1533 1.3 christos if (ptr->section) 1534 1.3 christos { 1535 1.3 christos asection *sec = bfd_get_section_by_name (obfd, ptr->section); 1536 1.3 christos if (!sec) 1537 1.3 christos fatal (_("Section %s not found"), ptr->section); 1538 1.5 christos sym->section = sec; 1539 1.5 christos } 1540 1.3 christos else 1541 1.3 christos sym->section = bfd_abs_section_ptr; 1542 1.3 christos return sym; 1543 1.1 christos } 1544 1.1 christos 1545 1.10 christos /* Choose which symbol entries to copy; put the result in OSYMS. 1546 1.1 christos We don't copy in place, because that confuses the relocs. 1547 1.10 christos Update the number of symbols to print. */ 1548 1.1 christos 1549 1.10 christos static bool 1550 1.1 christos filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms, 1551 1.1 christos asymbol **isyms, long *symcount) 1552 1.1 christos { 1553 1.1 christos asymbol **from = isyms, **to = osyms; 1554 1.1 christos long src_count = 0, dst_count = 0; 1555 1.10 christos int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0; 1556 1.1 christos 1557 1.1 christos for (; src_count < *symcount; src_count++) 1558 1.1 christos { 1559 1.1 christos asymbol *sym = from[src_count]; 1560 1.8 christos flagword flags = sym->flags; 1561 1.8 christos char *name = (char *) bfd_asymbol_name (sym); 1562 1.8 christos bool keep; 1563 1.8 christos bool used_in_reloc = false; 1564 1.8 christos bool undefined; 1565 1.1 christos bool rem_leading_char; 1566 1.7 christos bool add_leading_char; 1567 1.1 christos 1568 1.3 christos undefined = bfd_is_und_section (bfd_asymbol_section (sym)); 1569 1.3 christos 1570 1.3 christos if (add_sym_list) 1571 1.3 christos { 1572 1.3 christos struct addsym_node *ptr; 1573 1.3 christos 1574 1.3 christos if (need_sym_before (&ptr, name)) 1575 1.3 christos to[dst_count++] = create_new_symbol (ptr, obfd); 1576 1.6 christos } 1577 1.1 christos 1578 1.5 christos if (htab_elements (redefine_specific_htab) || section_rename_list) 1579 1.1 christos { 1580 1.10 christos char *new_name; 1581 1.8 christos 1582 1.8 christos if (name[0] == '_' 1583 1.8 christos && name[1] == '_' 1584 1.10 christos && strcmp (name + (name[2] == '_'), "__gnu_lto_slim") == 0) 1585 1.10 christos { 1586 1.8 christos fatal (_("redefining symbols does not work" 1587 1.10 christos " on LTO-compiled object files")); 1588 1.5 christos } 1589 1.5 christos 1590 1.5 christos new_name = (char *) lookup_sym_redefinition (name); 1591 1.5 christos if (new_name == name 1592 1.7 christos && (flags & BSF_SECTION_SYM) != 0) 1593 1.1 christos new_name = (char *) find_section_rename (name, NULL); 1594 1.1 christos bfd_set_asymbol_name (sym, new_name); 1595 1.1 christos name = new_name; 1596 1.1 christos } 1597 1.1 christos 1598 1.8 christos /* Check if we will remove the current leading character. */ 1599 1.8 christos rem_leading_char = 1600 1.8 christos (name[0] != '\0' 1601 1.8 christos && name[0] == bfd_get_symbol_leading_char (abfd) 1602 1.8 christos && (change_leading_char 1603 1.8 christos || (remove_leading_char 1604 1.8 christos && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0 1605 1.1 christos || undefined 1606 1.1 christos || bfd_is_com_section (bfd_asymbol_section (sym)))))); 1607 1.1 christos 1608 1.1 christos /* Check if we will add a new leading character. */ 1609 1.1 christos add_leading_char = 1610 1.1 christos change_leading_char 1611 1.1 christos && (bfd_get_symbol_leading_char (obfd) != '\0') 1612 1.1 christos && (bfd_get_symbol_leading_char (abfd) == '\0' 1613 1.1 christos || (name[0] == bfd_get_symbol_leading_char (abfd))); 1614 1.1 christos 1615 1.5 christos /* Short circuit for change_leading_char if we can do it in-place. */ 1616 1.1 christos if (rem_leading_char && add_leading_char && !prefix_symbols_string) 1617 1.7 christos { 1618 1.8 christos name[0] = bfd_get_symbol_leading_char (obfd); 1619 1.8 christos bfd_set_asymbol_name (sym, name); 1620 1.5 christos rem_leading_char = false; 1621 1.1 christos add_leading_char = false; 1622 1.1 christos } 1623 1.1 christos 1624 1.7 christos /* Remove leading char. */ 1625 1.1 christos if (rem_leading_char) 1626 1.1 christos bfd_set_asymbol_name (sym, ++name); 1627 1.1 christos 1628 1.5 christos /* Add new leading char and/or prefix. */ 1629 1.5 christos if (add_leading_char || prefix_symbols_string) 1630 1.8 christos { 1631 1.1 christos char *n, *ptr; 1632 1.8 christos size_t len = strlen (name) + 1; 1633 1.8 christos 1634 1.8 christos if (add_leading_char) 1635 1.8 christos len++; 1636 1.8 christos if (prefix_symbols_string) 1637 1.8 christos len += strlen (prefix_symbols_string); 1638 1.5 christos 1639 1.1 christos ptr = n = (char *) xmalloc (len); 1640 1.1 christos if (add_leading_char) 1641 1.5 christos *ptr++ = bfd_get_symbol_leading_char (obfd); 1642 1.5 christos 1643 1.5 christos if (prefix_symbols_string) 1644 1.5 christos { 1645 1.5 christos strcpy (ptr, prefix_symbols_string); 1646 1.5 christos ptr += strlen (prefix_symbols_string); 1647 1.5 christos } 1648 1.7 christos 1649 1.5 christos strcpy (ptr, name); 1650 1.1 christos bfd_set_asymbol_name (sym, n); 1651 1.1 christos name = n; 1652 1.1 christos } 1653 1.8 christos 1654 1.1 christos if (strip_symbols == STRIP_ALL) 1655 1.1 christos keep = false; 1656 1.10 christos else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */ 1657 1.1 christos || ((flags & BSF_SECTION_SYM) != 0 1658 1.1 christos && (bfd_asymbol_section (sym)->symbol->flags 1659 1.8 christos & BSF_KEEP) != 0)) 1660 1.8 christos { 1661 1.1 christos keep = true; 1662 1.1 christos used_in_reloc = true; 1663 1.1 christos } 1664 1.7 christos else if (relocatable /* Relocatable file. */ 1665 1.8 christos && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0 1666 1.1 christos || bfd_is_com_section (bfd_asymbol_section (sym)))) 1667 1.1 christos keep = true; 1668 1.1 christos else if (bfd_decode_symclass (sym) == 'I') 1669 1.1 christos /* Global symbols in $idata sections need to be retained 1670 1.1 christos even if relocatable is FALSE. External users of the 1671 1.8 christos library containing the $idata section may reference these 1672 1.1 christos symbols. */ 1673 1.1 christos keep = true; 1674 1.1 christos else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */ 1675 1.7 christos || (flags & BSF_WEAK) != 0 1676 1.1 christos || undefined 1677 1.1 christos || bfd_is_com_section (bfd_asymbol_section (sym))) 1678 1.1 christos keep = strip_symbols != STRIP_UNNEEDED; 1679 1.1 christos else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */ 1680 1.1 christos keep = (strip_symbols != STRIP_DEBUG 1681 1.7 christos && strip_symbols != STRIP_UNNEEDED 1682 1.1 christos && ! convert_debugging); 1683 1.1 christos else if (bfd_coff_get_comdat_section (abfd, bfd_asymbol_section (sym))) 1684 1.8 christos /* COMDAT sections store special information in local 1685 1.1 christos symbols, so we cannot risk stripping any of them. */ 1686 1.1 christos keep = true; 1687 1.1 christos else /* Local symbol. */ 1688 1.1 christos keep = (strip_symbols != STRIP_UNNEEDED 1689 1.1 christos && (discard_locals != LOCALS_ALL 1690 1.1 christos && (discard_locals != LOCALS_START_L 1691 1.1 christos || ! bfd_is_local_label (abfd, sym)))); 1692 1.1 christos 1693 1.1 christos if (keep && is_specified_symbol (name, strip_specific_htab)) 1694 1.1 christos { 1695 1.1 christos /* There are multiple ways to set 'keep' above, but if it 1696 1.1 christos was the relocatable symbol case, then that's an error. */ 1697 1.1 christos if (used_in_reloc) 1698 1.10 christos { 1699 1.1 christos non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name); 1700 1.1 christos return false; 1701 1.8 christos } 1702 1.1 christos else 1703 1.1 christos keep = false; 1704 1.1 christos } 1705 1.1 christos 1706 1.1 christos if (keep 1707 1.8 christos && !(flags & BSF_KEEP) 1708 1.1 christos && is_specified_symbol (name, strip_unneeded_htab)) 1709 1.1 christos keep = false; 1710 1.1 christos 1711 1.1 christos if (!keep 1712 1.8 christos && ((keep_file_symbols && (flags & BSF_FILE)) 1713 1.1 christos || is_specified_symbol (name, keep_specific_htab))) 1714 1.7 christos keep = true; 1715 1.8 christos 1716 1.1 christos if (keep && is_strip_section (abfd, bfd_asymbol_section (sym))) 1717 1.1 christos keep = false; 1718 1.1 christos 1719 1.1 christos if (keep) 1720 1.1 christos { 1721 1.1 christos if (!undefined 1722 1.1 christos && (flags & (BSF_GLOBAL | BSF_WEAK)) 1723 1.1 christos && (is_specified_symbol (name, localize_specific_htab) 1724 1.1 christos || (htab_elements (keepglobal_specific_htab) != 0 1725 1.1 christos && ! is_specified_symbol (name, keepglobal_specific_htab)) 1726 1.10 christos || (localize_hidden && is_hidden_symbol (sym)))) 1727 1.10 christos { 1728 1.1 christos flags &= ~(BSF_GLOBAL | BSF_WEAK); 1729 1.1 christos flags |= BSF_LOCAL; 1730 1.10 christos } 1731 1.10 christos 1732 1.10 christos else if (!undefined 1733 1.10 christos && (flags & BSF_LOCAL) 1734 1.10 christos && !(flags & BSF_FILE) 1735 1.10 christos && is_specified_symbol (name, globalize_specific_htab)) 1736 1.10 christos { 1737 1.10 christos flags &= ~BSF_LOCAL; 1738 1.10 christos flags |= BSF_GLOBAL; 1739 1.10 christos } 1740 1.10 christos 1741 1.10 christos if (((flags & (BSF_GLOBAL | BSF_GNU_UNIQUE)) 1742 1.1 christos || undefined) 1743 1.10 christos && (weaken || is_specified_symbol (name, weaken_specific_htab))) 1744 1.10 christos { 1745 1.1 christos flags &= ~(BSF_GLOBAL | BSF_GNU_UNIQUE); 1746 1.1 christos flags |= BSF_WEAK; 1747 1.10 christos } 1748 1.1 christos 1749 1.1 christos sym->flags = flags; 1750 1.1 christos to[dst_count++] = sym; 1751 1.3 christos } 1752 1.3 christos } 1753 1.3 christos if (add_sym_list) 1754 1.3 christos { 1755 1.3 christos struct addsym_node *ptr = add_sym_list; 1756 1.3 christos 1757 1.3 christos for (src_count = 0; src_count < add_symbols; src_count++) 1758 1.3 christos { 1759 1.7 christos if (ptr->othersym) 1760 1.3 christos { 1761 1.3 christos if (ptr->othersym != empty_name) 1762 1.3 christos fatal (_("'before=%s' not found"), ptr->othersym); 1763 1.3 christos } 1764 1.3 christos else 1765 1.3 christos to[dst_count++] = create_new_symbol (ptr, obfd); 1766 1.3 christos 1767 1.3 christos ptr = ptr->next; 1768 1.1 christos } 1769 1.1 christos } 1770 1.10 christos 1771 1.1 christos to[dst_count] = NULL; 1772 1.10 christos *symcount = dst_count; 1773 1.1 christos 1774 1.1 christos return true; 1775 1.1 christos } 1776 1.1 christos 1777 1.1 christos /* Find the redefined name of symbol SOURCE. */ 1778 1.1 christos 1779 1.1 christos static const char * 1780 1.6 christos lookup_sym_redefinition (const char *source) 1781 1.6 christos { 1782 1.6 christos struct redefine_node key_node = {(char *) source, NULL}; 1783 1.1 christos struct redefine_node *redef_node 1784 1.6 christos = (struct redefine_node *) htab_find (redefine_specific_htab, &key_node); 1785 1.1 christos 1786 1.1 christos return redef_node == NULL ? source : redef_node->target; 1787 1.6 christos } 1788 1.1 christos 1789 1.1 christos /* Insert a node into symbol redefine hash tabel. */ 1790 1.6 christos 1791 1.6 christos static void 1792 1.1 christos add_redefine_and_check (const char *cause, const char *source, 1793 1.6 christos const char *target) 1794 1.6 christos { 1795 1.1 christos struct redefine_node *new_node 1796 1.1 christos = (struct redefine_node *) xmalloc (sizeof (struct redefine_node)); 1797 1.1 christos 1798 1.1 christos new_node->source = strdup (source); 1799 1.6 christos new_node->target = strdup (target); 1800 1.6 christos 1801 1.6 christos if (htab_find (redefine_specific_htab, new_node) != HTAB_EMPTY_ENTRY) 1802 1.6 christos fatal (_("%s: Multiple redefinition of symbol \"%s\""), 1803 1.6 christos cause, source); 1804 1.6 christos 1805 1.6 christos if (htab_find (redefine_specific_reverse_htab, target) != HTAB_EMPTY_ENTRY) 1806 1.6 christos fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"), 1807 1.6 christos cause, target); 1808 1.6 christos 1809 1.6 christos /* Insert the NEW_NODE into hash table for quick search. */ 1810 1.6 christos add_specific_symbol_node (new_node, redefine_specific_htab); 1811 1.6 christos 1812 1.6 christos /* Insert the target string into the reverse hash table, this is needed for 1813 1.6 christos duplicated target string check. */ 1814 1.1 christos add_specific_symbol (new_node->target, redefine_specific_reverse_htab); 1815 1.1 christos 1816 1.1 christos } 1817 1.1 christos 1818 1.1 christos /* Handle the --redefine-syms option. Read lines containing "old new" 1819 1.1 christos from the file, and add them to the symbol redefine list. */ 1820 1.1 christos 1821 1.1 christos static void 1822 1.1 christos add_redefine_syms_file (const char *filename) 1823 1.1 christos { 1824 1.1 christos FILE *file; 1825 1.1 christos char *buf; 1826 1.1 christos size_t bufsize; 1827 1.1 christos size_t len; 1828 1.1 christos size_t outsym_off; 1829 1.1 christos int c, lineno; 1830 1.1 christos 1831 1.1 christos file = fopen (filename, "r"); 1832 1.1 christos if (file == NULL) 1833 1.1 christos fatal (_("couldn't open symbol redefinition file %s (error: %s)"), 1834 1.1 christos filename, strerror (errno)); 1835 1.1 christos 1836 1.1 christos bufsize = 100; 1837 1.1 christos buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */); 1838 1.1 christos 1839 1.1 christos lineno = 1; 1840 1.1 christos c = getc (file); 1841 1.1 christos len = 0; 1842 1.1 christos outsym_off = 0; 1843 1.1 christos while (c != EOF) 1844 1.1 christos { 1845 1.1 christos /* Collect the input symbol name. */ 1846 1.1 christos while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF) 1847 1.1 christos { 1848 1.1 christos if (c == '#') 1849 1.1 christos goto comment; 1850 1.1 christos buf[len++] = c; 1851 1.1 christos if (len >= bufsize) 1852 1.1 christos { 1853 1.1 christos bufsize *= 2; 1854 1.1 christos buf = (char *) xrealloc (buf, bufsize + 1); 1855 1.1 christos } 1856 1.1 christos c = getc (file); 1857 1.1 christos } 1858 1.1 christos buf[len++] = '\0'; 1859 1.1 christos if (c == EOF) 1860 1.1 christos break; 1861 1.1 christos 1862 1.1 christos /* Eat white space between the symbol names. */ 1863 1.1 christos while (IS_WHITESPACE (c)) 1864 1.1 christos c = getc (file); 1865 1.1 christos if (c == '#' || IS_LINE_TERMINATOR (c)) 1866 1.1 christos goto comment; 1867 1.1 christos if (c == EOF) 1868 1.1 christos break; 1869 1.1 christos 1870 1.1 christos /* Collect the output symbol name. */ 1871 1.1 christos outsym_off = len; 1872 1.1 christos while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF) 1873 1.1 christos { 1874 1.1 christos if (c == '#') 1875 1.1 christos goto comment; 1876 1.1 christos buf[len++] = c; 1877 1.1 christos if (len >= bufsize) 1878 1.1 christos { 1879 1.1 christos bufsize *= 2; 1880 1.1 christos buf = (char *) xrealloc (buf, bufsize + 1); 1881 1.1 christos } 1882 1.1 christos c = getc (file); 1883 1.1 christos } 1884 1.1 christos buf[len++] = '\0'; 1885 1.1 christos if (c == EOF) 1886 1.1 christos break; 1887 1.1 christos 1888 1.1 christos /* Eat white space at end of line. */ 1889 1.1 christos while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c)) 1890 1.1 christos c = getc (file); 1891 1.1 christos if (c == '#') 1892 1.1 christos goto comment; 1893 1.1 christos /* Handle \r\n. */ 1894 1.1 christos if ((c == '\r' && (c = getc (file)) == '\n') 1895 1.5 christos || c == '\n' || c == EOF) 1896 1.1 christos { 1897 1.1 christos end_of_line: 1898 1.6 christos /* Append the redefinition to the list. */ 1899 1.1 christos if (buf[0] != '\0') 1900 1.1 christos add_redefine_and_check (filename, &buf[0], &buf[outsym_off]); 1901 1.1 christos 1902 1.1 christos lineno++; 1903 1.1 christos len = 0; 1904 1.1 christos outsym_off = 0; 1905 1.1 christos if (c == EOF) 1906 1.1 christos break; 1907 1.1 christos c = getc (file); 1908 1.1 christos continue; 1909 1.1 christos } 1910 1.5 christos else 1911 1.1 christos fatal (_("%s:%d: garbage found at end of line"), filename, lineno); 1912 1.1 christos comment: 1913 1.1 christos if (len != 0 && (outsym_off == 0 || outsym_off == len)) 1914 1.1 christos fatal (_("%s:%d: missing new symbol name"), filename, lineno); 1915 1.1 christos buf[len++] = '\0'; 1916 1.1 christos 1917 1.1 christos /* Eat the rest of the line and finish it. */ 1918 1.1 christos while (c != '\n' && c != EOF) 1919 1.1 christos c = getc (file); 1920 1.1 christos goto end_of_line; 1921 1.1 christos } 1922 1.1 christos 1923 1.1 christos if (len != 0) 1924 1.1 christos fatal (_("%s:%d: premature end of file"), filename, lineno); 1925 1.7 christos 1926 1.1 christos free (buf); 1927 1.1 christos fclose (file); 1928 1.6 christos } 1929 1.1 christos 1930 1.1 christos /* Copy unknown object file IBFD onto OBFD. 1931 1.8 christos Returns TRUE upon success, FALSE otherwise. */ 1932 1.10 christos 1933 1.1 christos static bool 1934 1.1 christos copy_unknown_file (bfd *ibfd, bfd *obfd, off_t size, unsigned int mode) 1935 1.8 christos { 1936 1.1 christos char *cbuf; 1937 1.1 christos bfd_size_type tocopy; 1938 1.1 christos 1939 1.1 christos if (size < 0) 1940 1.1 christos { 1941 1.8 christos non_fatal (_("stat returns negative size for `%s'"), 1942 1.1 christos bfd_get_archive_filename (ibfd)); 1943 1.1 christos return false; 1944 1.9 christos } 1945 1.1 christos 1946 1.1 christos if (bfd_seek (ibfd, 0, SEEK_SET) != 0) 1947 1.8 christos { 1948 1.1 christos bfd_nonfatal (bfd_get_archive_filename (ibfd)); 1949 1.1 christos return false; 1950 1.1 christos } 1951 1.1 christos 1952 1.1 christos if (verbose) 1953 1.1 christos printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"), 1954 1.1 christos bfd_get_archive_filename (ibfd), bfd_get_filename (obfd)); 1955 1.8 christos 1956 1.1 christos cbuf = (char *) xmalloc (BUFSIZE); 1957 1.8 christos while (size != 0) 1958 1.1 christos { 1959 1.8 christos if (size > BUFSIZE) 1960 1.8 christos tocopy = BUFSIZE; 1961 1.1 christos else 1962 1.9 christos tocopy = size; 1963 1.1 christos 1964 1.1 christos if (bfd_read (cbuf, tocopy, ibfd) != tocopy) 1965 1.1 christos { 1966 1.8 christos bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1967 1.1 christos free (cbuf); 1968 1.1 christos return false; 1969 1.9 christos } 1970 1.1 christos 1971 1.1 christos if (bfd_write (cbuf, tocopy, obfd) != tocopy) 1972 1.1 christos { 1973 1.8 christos bfd_nonfatal_message (NULL, obfd, NULL, NULL); 1974 1.1 christos free (cbuf); 1975 1.1 christos return false; 1976 1.8 christos } 1977 1.1 christos 1978 1.1 christos size -= tocopy; 1979 1.1 christos } 1980 1.1 christos 1981 1.10 christos /* We should at least to be able to read it back when copying an 1982 1.1 christos unknown object in an archive. */ 1983 1.8 christos chmod (bfd_get_filename (obfd), mode | S_IRUSR); 1984 1.1 christos free (cbuf); 1985 1.1 christos return true; 1986 1.10 christos } 1987 1.10 christos 1988 1.10 christos /* Copy unknown object file archive member IBFD onto OBFD. 1989 1.10 christos Returns TRUE upon success, FALSE otherwise. */ 1990 1.10 christos 1991 1.10 christos static bool 1992 1.10 christos copy_unknown_object (bfd *ibfd, bfd *obfd) 1993 1.10 christos { 1994 1.10 christos struct stat buf; 1995 1.10 christos 1996 1.10 christos if (bfd_stat_arch_elt (ibfd, &buf) != 0) 1997 1.10 christos { 1998 1.10 christos bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 1999 1.10 christos return false; 2000 1.10 christos } 2001 1.10 christos 2002 1.10 christos if (!copy_unknown_file (ibfd, obfd, buf.st_size, buf.st_mode)) 2003 1.10 christos return false; 2004 1.10 christos 2005 1.10 christos return true; 2006 1.6 christos } 2007 1.6 christos 2008 1.6 christos typedef struct objcopy_internal_note 2009 1.7 christos { 2010 1.6 christos Elf_Internal_Note note; 2011 1.6 christos unsigned long padded_namesz; 2012 1.6 christos bfd_vma start; 2013 1.7 christos bfd_vma end; 2014 1.7 christos } objcopy_internal_note; 2015 1.7 christos 2016 1.7 christos #define DEBUG_MERGE 0 2017 1.7 christos 2018 1.7 christos #if DEBUG_MERGE 2019 1.7 christos #define merge_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__) 2020 1.7 christos #else 2021 1.7 christos #define merge_debug(format, ...) 2022 1.7 christos #endif 2023 1.6 christos 2024 1.8 christos /* Returns TRUE iff PNOTE1 overlaps or adjoins PNOTE2. */ 2025 1.7 christos 2026 1.7 christos static bool 2027 1.6 christos overlaps_or_adjoins (objcopy_internal_note * pnote1, 2028 1.7 christos objcopy_internal_note * pnote2) 2029 1.7 christos { 2030 1.7 christos if (pnote1->end < pnote2->start) 2031 1.7 christos /* FIXME: Alignment of 16 bytes taken from x86_64 binaries. 2032 1.7 christos Really we should extract the alignment of the section 2033 1.7 christos covered by the notes. */ 2034 1.7 christos return BFD_ALIGN (pnote1->end, 16) < pnote2->start; 2035 1.7 christos 2036 1.7 christos if (pnote2->end < pnote2->start) 2037 1.7 christos return BFD_ALIGN (pnote2->end, 16) < pnote1->start; 2038 1.8 christos 2039 1.7 christos if (pnote1->end < pnote2->end) 2040 1.7 christos return true; 2041 1.8 christos 2042 1.6 christos if (pnote2->end < pnote1->end) 2043 1.8 christos return true; 2044 1.7 christos 2045 1.7 christos return false; 2046 1.7 christos } 2047 1.7 christos 2048 1.8 christos /* Returns TRUE iff NEEDLE is fully contained by HAYSTACK. */ 2049 1.7 christos 2050 1.7 christos static bool 2051 1.7 christos contained_by (objcopy_internal_note * needle, 2052 1.7 christos objcopy_internal_note * haystack) 2053 1.6 christos { 2054 1.6 christos return needle->start >= haystack->start && needle->end <= haystack->end; 2055 1.9 christos } 2056 1.6 christos 2057 1.6 christos static inline bool 2058 1.7 christos is_open_note (objcopy_internal_note * pnote) 2059 1.6 christos { 2060 1.6 christos return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_OPEN; 2061 1.9 christos } 2062 1.6 christos 2063 1.6 christos static inline bool 2064 1.7 christos is_func_note (objcopy_internal_note * pnote) 2065 1.7 christos { 2066 1.7 christos return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_FUNC; 2067 1.9 christos } 2068 1.7 christos 2069 1.7 christos static inline bool 2070 1.7 christos is_deleted_note (objcopy_internal_note * pnote) 2071 1.7 christos { 2072 1.7 christos return pnote->note.type == 0; 2073 1.8 christos } 2074 1.7 christos 2075 1.7 christos static bool 2076 1.7 christos is_version_note (objcopy_internal_note * pnote) 2077 1.7 christos { 2078 1.7 christos return (pnote->note.namesz > 4 2079 1.7 christos && pnote->note.namedata[0] == 'G' 2080 1.7 christos && pnote->note.namedata[1] == 'A' 2081 1.6 christos && pnote->note.namedata[2] == '$' 2082 1.6 christos && pnote->note.namedata[3] == GNU_BUILD_ATTRIBUTE_VERSION); 2083 1.8 christos } 2084 1.6 christos 2085 1.6 christos static bool 2086 1.6 christos is_64bit (bfd * abfd) 2087 1.6 christos { 2088 1.8 christos /* Should never happen, but let's be paranoid. */ 2089 1.6 christos if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 2090 1.6 christos return false; 2091 1.6 christos 2092 1.6 christos return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64; 2093 1.7 christos } 2094 1.7 christos 2095 1.7 christos /* This sorting function is used to get the notes into an order 2096 1.7 christos that makes merging easy. */ 2097 1.7 christos 2098 1.7 christos static int 2099 1.7 christos compare_gnu_build_notes (const void * data1, const void * data2) 2100 1.7 christos { 2101 1.7 christos objcopy_internal_note * pnote1 = (objcopy_internal_note *) data1; 2102 1.7 christos objcopy_internal_note * pnote2 = (objcopy_internal_note *) data2; 2103 1.7 christos 2104 1.7 christos /* Sort notes based upon the attribute they record. */ 2105 1.7 christos int cmp = memcmp (pnote1->note.namedata + 3, 2106 1.7 christos pnote2->note.namedata + 3, 2107 1.7 christos pnote1->note.namesz < pnote2->note.namesz ? 2108 1.7 christos pnote1->note.namesz - 3 : pnote2->note.namesz - 3); 2109 1.7 christos if (cmp) 2110 1.7 christos return cmp; 2111 1.7 christos 2112 1.7 christos if (pnote1->end < pnote2->start) 2113 1.7 christos return -1; 2114 1.7 christos if (pnote1->start > pnote2->end) 2115 1.7 christos return 1; 2116 1.7 christos 2117 1.7 christos /* Overlaps - we should merge the two ranges. */ 2118 1.7 christos if (pnote1->start < pnote2->start) 2119 1.7 christos return -1; 2120 1.7 christos if (pnote1->end > pnote2->end) 2121 1.7 christos return 1; 2122 1.7 christos if (pnote1->end < pnote2->end) 2123 1.7 christos return -1; 2124 1.7 christos 2125 1.7 christos /* Put OPEN notes before function notes. */ 2126 1.7 christos if (is_open_note (pnote1) && ! is_open_note (pnote2)) 2127 1.7 christos return -1; 2128 1.7 christos if (! is_open_note (pnote1) && is_open_note (pnote2)) 2129 1.7 christos return 1; 2130 1.7 christos 2131 1.7 christos return 0; 2132 1.7 christos } 2133 1.7 christos 2134 1.7 christos /* This sorting function is used to get the notes into an order 2135 1.7 christos that makes eliminating address ranges easier. */ 2136 1.7 christos 2137 1.7 christos static int 2138 1.7 christos sort_gnu_build_notes (const void * data1, const void * data2) 2139 1.7 christos { 2140 1.7 christos objcopy_internal_note * pnote1 = (objcopy_internal_note *) data1; 2141 1.7 christos objcopy_internal_note * pnote2 = (objcopy_internal_note *) data2; 2142 1.7 christos 2143 1.7 christos if (pnote1->note.type != pnote2->note.type) 2144 1.7 christos { 2145 1.7 christos /* Move deleted notes to the end. */ 2146 1.7 christos if (is_deleted_note (pnote1)) /* 1: OFD 2: OFD */ 2147 1.7 christos return 1; 2148 1.7 christos 2149 1.7 christos /* Move OPEN notes to the start. */ 2150 1.7 christos if (is_open_note (pnote1)) /* 1: OF 2: OFD */ 2151 1.7 christos return -1; 2152 1.7 christos 2153 1.7 christos if (is_deleted_note (pnote2)) /* 1: F 2: O D */ 2154 1.7 christos return -1; 2155 1.7 christos 2156 1.7 christos return 1; /* 1: F 2: O */ 2157 1.7 christos } 2158 1.7 christos 2159 1.7 christos /* Sort by starting address. */ 2160 1.7 christos if (pnote1->start < pnote2->start) 2161 1.7 christos return -1; 2162 1.7 christos if (pnote1->start > pnote2->start) 2163 1.7 christos return 1; 2164 1.7 christos 2165 1.7 christos /* Then by end address (bigger range first). */ 2166 1.7 christos if (pnote1->end > pnote2->end) 2167 1.7 christos return -1; 2168 1.7 christos if (pnote1->end < pnote2->end) 2169 1.7 christos return 1; 2170 1.7 christos 2171 1.7 christos /* Then by attribute type. */ 2172 1.7 christos if (pnote1->note.namesz > 4 2173 1.7 christos && pnote2->note.namesz > 4 2174 1.7 christos && pnote1->note.namedata[3] != pnote2->note.namedata[3]) 2175 1.7 christos return pnote1->note.namedata[3] - pnote2->note.namedata[3]; 2176 1.7 christos 2177 1.7 christos return 0; 2178 1.10 christos } 2179 1.10 christos 2180 1.10 christos /* Merge the notes on SEC, removing redundant entries. NEW_SIZE is 2181 1.6 christos set to the new, smaller size of the section. Returns true on 2182 1.10 christos success, false on errors that result in objcopy failing. */ 2183 1.7 christos 2184 1.7 christos static bool 2185 1.7 christos merge_gnu_build_notes (bfd * abfd, 2186 1.10 christos asection * sec, 2187 1.10 christos bfd_size_type size, 2188 1.6 christos bfd_byte * contents, 2189 1.6 christos bfd_size_type *new_size) 2190 1.6 christos { 2191 1.6 christos objcopy_internal_note * pnotes_end; 2192 1.6 christos objcopy_internal_note * pnotes = NULL; 2193 1.6 christos objcopy_internal_note * pnote; 2194 1.6 christos bfd_size_type remain = size; 2195 1.6 christos unsigned version_1_seen = 0; 2196 1.6 christos unsigned version_2_seen = 0; 2197 1.6 christos unsigned version_3_seen = 0; 2198 1.6 christos const char * err = NULL; 2199 1.6 christos bfd_byte * in = contents; 2200 1.6 christos unsigned long previous_func_start = 0; 2201 1.6 christos unsigned long previous_open_start = 0; 2202 1.6 christos unsigned long previous_func_end = 0; 2203 1.6 christos unsigned long previous_open_end = 0; 2204 1.10 christos long relsize; 2205 1.6 christos 2206 1.6 christos *new_size = size; 2207 1.6 christos relsize = bfd_get_reloc_upper_bound (abfd, sec); 2208 1.6 christos if (relsize > 0) 2209 1.6 christos { 2210 1.6 christos arelent ** relpp; 2211 1.6 christos long relcount; 2212 1.6 christos 2213 1.6 christos /* If there are relocs associated with this section then we 2214 1.6 christos cannot safely merge it. */ 2215 1.6 christos relpp = (arelent **) xmalloc (relsize); 2216 1.6 christos relcount = bfd_canonicalize_reloc (abfd, sec, relpp, isympp); 2217 1.7 christos free (relpp); 2218 1.7 christos if (relcount != 0) 2219 1.7 christos { 2220 1.7 christos if (! is_strip) 2221 1.7 christos non_fatal (_("%s[%s]: Cannot merge - there are relocations against this section"), 2222 1.7 christos bfd_get_filename (abfd), bfd_section_name (sec)); 2223 1.6 christos goto done; 2224 1.7 christos } 2225 1.6 christos } 2226 1.7 christos 2227 1.7 christos /* Make a copy of the notes and convert to our internal format. 2228 1.7 christos Minimum size of a note is 12 bytes. Also locate the version 2229 1.7 christos notes and check them. */ 2230 1.6 christos pnote = pnotes = (objcopy_internal_note *) 2231 1.6 christos xcalloc ((size / 12), sizeof (* pnote)); 2232 1.6 christos while (remain >= 12) 2233 1.6 christos { 2234 1.7 christos bfd_vma start, end; 2235 1.7 christos 2236 1.7 christos pnote->note.namesz = bfd_get_32 (abfd, in); 2237 1.7 christos pnote->note.descsz = bfd_get_32 (abfd, in + 4); 2238 1.7 christos pnote->note.type = bfd_get_32 (abfd, in + 8); 2239 1.7 christos pnote->padded_namesz = (pnote->note.namesz + 3) & ~3; 2240 1.7 christos 2241 1.7 christos if (((pnote->note.descsz + 3) & ~3) != pnote->note.descsz) 2242 1.7 christos { 2243 1.7 christos err = _("corrupt GNU build attribute note: description size not a factor of 4"); 2244 1.6 christos goto done; 2245 1.6 christos } 2246 1.6 christos 2247 1.6 christos if (pnote->note.type != NT_GNU_BUILD_ATTRIBUTE_OPEN 2248 1.6 christos && pnote->note.type != NT_GNU_BUILD_ATTRIBUTE_FUNC) 2249 1.6 christos { 2250 1.6 christos err = _("corrupt GNU build attribute note: wrong note type"); 2251 1.6 christos goto done; 2252 1.7 christos } 2253 1.6 christos 2254 1.6 christos if (pnote->padded_namesz + pnote->note.descsz + 12 > remain) 2255 1.6 christos { 2256 1.6 christos err = _("corrupt GNU build attribute note: note too big"); 2257 1.6 christos goto done; 2258 1.6 christos } 2259 1.6 christos 2260 1.6 christos if (pnote->note.namesz < 2) 2261 1.6 christos { 2262 1.6 christos err = _("corrupt GNU build attribute note: name too small"); 2263 1.6 christos goto done; 2264 1.6 christos } 2265 1.7 christos 2266 1.6 christos pnote->note.namedata = (char *)(in + 12); 2267 1.7 christos pnote->note.descdata = (char *)(in + 12 + pnote->padded_namesz); 2268 1.7 christos 2269 1.6 christos remain -= 12 + pnote->padded_namesz + pnote->note.descsz; 2270 1.6 christos in += 12 + pnote->padded_namesz + pnote->note.descsz; 2271 1.6 christos 2272 1.6 christos if (pnote->note.namesz > 2 2273 1.6 christos && pnote->note.namedata[0] == '$' 2274 1.6 christos && pnote->note.namedata[1] == GNU_BUILD_ATTRIBUTE_VERSION 2275 1.7 christos && pnote->note.namedata[2] == '1') 2276 1.6 christos ++ version_1_seen; 2277 1.6 christos else if (is_version_note (pnote)) 2278 1.6 christos { 2279 1.6 christos if (pnote->note.namedata[4] == '2') 2280 1.6 christos ++ version_2_seen; 2281 1.6 christos else if (pnote->note.namedata[4] == '3') 2282 1.6 christos ++ version_3_seen; 2283 1.6 christos else 2284 1.6 christos { 2285 1.6 christos err = _("corrupt GNU build attribute note: unsupported version"); 2286 1.6 christos goto done; 2287 1.6 christos } 2288 1.6 christos } 2289 1.6 christos 2290 1.6 christos switch (pnote->note.descsz) 2291 1.6 christos { 2292 1.6 christos case 0: 2293 1.6 christos start = end = 0; 2294 1.6 christos break; 2295 1.6 christos 2296 1.6 christos case 4: 2297 1.6 christos start = bfd_get_32 (abfd, pnote->note.descdata); 2298 1.6 christos /* FIXME: For version 1 and 2 notes we should try to 2299 1.6 christos calculate the end address by finding a symbol whose 2300 1.6 christos value is START, and then adding in its size. 2301 1.6 christos 2302 1.6 christos For now though, since v1 and v2 was not intended to 2303 1.6 christos handle gaps, we chose an artificially large end 2304 1.6 christos address. */ 2305 1.7 christos end = (bfd_vma) -1; 2306 1.6 christos break; 2307 1.8 christos 2308 1.8 christos case 8: 2309 1.6 christos start = bfd_get_32 (abfd, pnote->note.descdata); 2310 1.6 christos end = bfd_get_32 (abfd, pnote->note.descdata + 4); 2311 1.6 christos break; 2312 1.6 christos 2313 1.6 christos case 16: 2314 1.6 christos start = bfd_get_64 (abfd, pnote->note.descdata); 2315 1.7 christos end = bfd_get_64 (abfd, pnote->note.descdata + 8); 2316 1.6 christos break; 2317 1.6 christos 2318 1.6 christos default: 2319 1.6 christos err = _("corrupt GNU build attribute note: bad description size"); 2320 1.6 christos goto done; 2321 1.8 christos } 2322 1.8 christos 2323 1.8 christos if (start > end) 2324 1.8 christos /* This can happen with PPC64LE binaries where empty notes are 2325 1.8 christos encoded as start = end + 4. */ 2326 1.6 christos start = end; 2327 1.6 christos 2328 1.6 christos if (is_open_note (pnote)) 2329 1.6 christos { 2330 1.6 christos if (start) 2331 1.6 christos previous_open_start = start; 2332 1.6 christos 2333 1.6 christos pnote->start = previous_open_start; 2334 1.6 christos 2335 1.6 christos if (end) 2336 1.6 christos previous_open_end = end; 2337 1.6 christos 2338 1.6 christos pnote->end = previous_open_end; 2339 1.6 christos } 2340 1.6 christos else 2341 1.6 christos { 2342 1.6 christos if (start) 2343 1.6 christos previous_func_start = start; 2344 1.6 christos 2345 1.6 christos pnote->start = previous_func_start; 2346 1.6 christos 2347 1.6 christos if (end) 2348 1.6 christos previous_func_end = end; 2349 1.6 christos 2350 1.6 christos pnote->end = previous_func_end; 2351 1.6 christos } 2352 1.6 christos 2353 1.6 christos if (pnote->note.namedata[pnote->note.namesz - 1] != 0) 2354 1.6 christos { 2355 1.6 christos err = _("corrupt GNU build attribute note: name not NUL terminated"); 2356 1.6 christos goto done; 2357 1.6 christos } 2358 1.6 christos 2359 1.6 christos pnote ++; 2360 1.6 christos } 2361 1.6 christos 2362 1.6 christos pnotes_end = pnote; 2363 1.6 christos 2364 1.6 christos /* Check that the notes are valid. */ 2365 1.6 christos if (remain != 0) 2366 1.6 christos { 2367 1.6 christos err = _("corrupt GNU build attribute notes: excess data at end"); 2368 1.6 christos goto done; 2369 1.6 christos } 2370 1.6 christos 2371 1.7 christos if (version_1_seen == 0 && version_2_seen == 0 && version_3_seen == 0) 2372 1.6 christos { 2373 1.6 christos #if 0 2374 1.7 christos err = _("bad GNU build attribute notes: no known versions detected"); 2375 1.7 christos goto done; 2376 1.7 christos #else 2377 1.7 christos /* This happens with glibc. No idea why. */ 2378 1.7 christos non_fatal (_("%s[%s]: Warning: version note missing - assuming version 3"), 2379 1.7 christos bfd_get_filename (abfd), bfd_section_name (sec)); 2380 1.6 christos version_3_seen = 2; 2381 1.6 christos #endif 2382 1.7 christos } 2383 1.6 christos 2384 1.6 christos if ( (version_1_seen > 0 && version_2_seen > 0) 2385 1.6 christos || (version_1_seen > 0 && version_3_seen > 0) 2386 1.6 christos || (version_2_seen > 0 && version_3_seen > 0)) 2387 1.6 christos { 2388 1.6 christos err = _("bad GNU build attribute notes: multiple different versions"); 2389 1.6 christos goto done; 2390 1.7 christos } 2391 1.7 christos 2392 1.7 christos /* We are now only supporting the merging v3+ notes 2393 1.6 christos - it makes things much simpler. */ 2394 1.7 christos if (version_3_seen == 0) 2395 1.6 christos { 2396 1.6 christos merge_debug ("%s: skipping merge - not using v3 notes", bfd_section_name (sec)); 2397 1.6 christos goto done; 2398 1.7 christos } 2399 1.7 christos 2400 1.7 christos merge_debug ("Merging section %s which contains %ld notes\n", 2401 1.7 christos sec->name, pnotes_end - pnotes); 2402 1.7 christos 2403 1.7 christos /* Sort the notes. */ 2404 1.7 christos qsort (pnotes, pnotes_end - pnotes, sizeof (* pnotes), 2405 1.7 christos compare_gnu_build_notes); 2406 1.7 christos 2407 1.7 christos #if DEBUG_MERGE 2408 1.7 christos merge_debug ("Results of initial sort:\n"); 2409 1.7 christos for (pnote = pnotes; pnote < pnotes_end; pnote ++) 2410 1.7 christos merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n", 2411 1.7 christos (pnote->note.namedata - (char *) contents) - 12, 2412 1.7 christos pnote->start, pnote->end, 2413 1.7 christos pnote->note.type, 2414 1.7 christos pnote->note.namedata[3], 2415 1.7 christos pnote->note.namesz 2416 1.7 christos ); 2417 1.6 christos #endif 2418 1.7 christos 2419 1.7 christos /* Now merge the notes. The rules are: 2420 1.7 christos 1. If a note has a zero range, it can be eliminated. 2421 1.7 christos 2. If two notes have the same namedata then: 2422 1.7 christos 2a. If one note's range is fully covered by the other note 2423 1.7 christos then it can be deleted. 2424 1.7 christos 2b. If one note's range partially overlaps or adjoins the 2425 1.7 christos other note then if they are both of the same type (open 2426 1.9 christos or func) then they can be merged and one deleted. If 2427 1.9 christos they are of different types then they cannot be merged. */ 2428 1.7 christos objcopy_internal_note * prev_note = NULL; 2429 1.7 christos 2430 1.7 christos for (pnote = pnotes; pnote < pnotes_end; pnote ++) 2431 1.7 christos { 2432 1.7 christos /* Skip already deleted notes. 2433 1.7 christos FIXME: Can this happen ? We are scanning forwards and 2434 1.6 christos deleting backwards after all. */ 2435 1.6 christos if (is_deleted_note (pnote)) 2436 1.7 christos continue; 2437 1.7 christos 2438 1.7 christos /* Rule 1 - delete 0-range notes. */ 2439 1.7 christos if (pnote->start == pnote->end) 2440 1.7 christos { 2441 1.7 christos merge_debug ("Delete note at offset %#08lx - empty range\n", 2442 1.7 christos (pnote->note.namedata - (char *) contents) - 12); 2443 1.7 christos pnote->note.type = 0; 2444 1.6 christos continue; 2445 1.7 christos } 2446 1.7 christos 2447 1.6 christos int iter; 2448 1.7 christos objcopy_internal_note * back; 2449 1.9 christos 2450 1.9 christos /* Rule 2: Check to see if there is an identical previous note. */ 2451 1.9 christos for (iter = 0, back = prev_note ? prev_note : pnote - 1; 2452 1.7 christos back >= pnotes; 2453 1.7 christos back --) 2454 1.6 christos { 2455 1.6 christos if (is_deleted_note (back)) 2456 1.7 christos continue; 2457 1.7 christos 2458 1.7 christos /* Our sorting function should have placed all identically 2459 1.7 christos attributed notes together, so if we see a note of a different 2460 1.7 christos attribute type stop searching. */ 2461 1.7 christos if (back->note.namesz != pnote->note.namesz 2462 1.7 christos || memcmp (back->note.namedata, 2463 1.6 christos pnote->note.namedata, pnote->note.namesz) != 0) 2464 1.7 christos break; 2465 1.7 christos 2466 1.6 christos if (back->start == pnote->start 2467 1.7 christos && back->end == pnote->end) 2468 1.7 christos { 2469 1.7 christos merge_debug ("Delete note at offset %#08lx - duplicate of note at offset %#08lx\n", 2470 1.6 christos (pnote->note.namedata - (char *) contents) - 12, 2471 1.6 christos (back->note.namedata - (char *) contents) - 12); 2472 1.6 christos pnote->note.type = 0; 2473 1.6 christos break; 2474 1.7 christos } 2475 1.7 christos 2476 1.6 christos /* Rule 2a. */ 2477 1.7 christos if (contained_by (pnote, back)) 2478 1.7 christos { 2479 1.7 christos merge_debug ("Delete note at offset %#08lx - fully contained by note at %#08lx\n", 2480 1.6 christos (pnote->note.namedata - (char *) contents) - 12, 2481 1.7 christos (back->note.namedata - (char *) contents) - 12); 2482 1.7 christos pnote->note.type = 0; 2483 1.7 christos break; 2484 1.7 christos } 2485 1.7 christos 2486 1.7 christos #if DEBUG_MERGE 2487 1.7 christos /* This should not happen as we have sorted the 2488 1.7 christos notes with earlier starting addresses first. */ 2489 1.7 christos if (contained_by (back, pnote)) 2490 1.6 christos merge_debug ("ERROR: UNEXPECTED CONTAINMENT\n"); 2491 1.7 christos #endif 2492 1.7 christos 2493 1.7 christos /* Rule 2b. */ 2494 1.7 christos if (overlaps_or_adjoins (back, pnote) 2495 1.7 christos && is_func_note (back) == is_func_note (pnote)) 2496 1.7 christos { 2497 1.7 christos merge_debug ("Delete note at offset %#08lx - merge into note at %#08lx\n", 2498 1.6 christos (pnote->note.namedata - (char *) contents) - 12, 2499 1.7 christos (back->note.namedata - (char *) contents) - 12); 2500 1.7 christos 2501 1.7 christos back->end = back->end > pnote->end ? back->end : pnote->end; 2502 1.6 christos back->start = back->start < pnote->start ? back->start : pnote->start; 2503 1.6 christos pnote->note.type = 0; 2504 1.6 christos break; 2505 1.7 christos } 2506 1.7 christos 2507 1.6 christos /* Don't scan too far back however. */ 2508 1.7 christos if (iter ++ > 16) 2509 1.7 christos { 2510 1.7 christos /* FIXME: Not sure if this can ever be triggered. */ 2511 1.6 christos merge_debug ("ITERATION LIMIT REACHED\n"); 2512 1.6 christos break; 2513 1.9 christos } 2514 1.9 christos } 2515 1.9 christos 2516 1.9 christos if (! is_deleted_note (pnote)) 2517 1.9 christos { 2518 1.9 christos /* Keep a pointer to this note, so that we can 2519 1.7 christos start the next search for rule 2 matches here. */ 2520 1.9 christos prev_note = pnote; 2521 1.9 christos #if DEBUG_MERGE 2522 1.7 christos merge_debug ("Unable to do anything with note at %#08lx\n", 2523 1.9 christos (pnote->note.namedata - (char *) contents) - 12); 2524 1.6 christos #endif 2525 1.6 christos } 2526 1.7 christos } 2527 1.7 christos 2528 1.7 christos /* Resort the notes. */ 2529 1.7 christos merge_debug ("Final sorting of notes\n"); 2530 1.7 christos qsort (pnotes, pnotes_end - pnotes, sizeof (* pnotes), sort_gnu_build_notes); 2531 1.7 christos 2532 1.7 christos /* Reconstruct the ELF notes. */ 2533 1.7 christos bfd_byte * new_contents; 2534 1.7 christos bfd_byte * old; 2535 1.7 christos bfd_byte * new; 2536 1.7 christos bfd_vma prev_start = 0; 2537 1.7 christos bfd_vma prev_end = 0; 2538 1.7 christos 2539 1.7 christos /* Not sure how, but the notes might grow in size. 2540 1.7 christos (eg see PR 1774507). Allow for this here. */ 2541 1.7 christos new = new_contents = xmalloc (size * 2); 2542 1.7 christos for (pnote = pnotes, old = contents; 2543 1.7 christos pnote < pnotes_end; 2544 1.7 christos pnote ++) 2545 1.7 christos { 2546 1.7 christos bfd_size_type note_size = 12 + pnote->padded_namesz + pnote->note.descsz; 2547 1.7 christos 2548 1.7 christos if (! is_deleted_note (pnote)) 2549 1.7 christos { 2550 1.7 christos /* Create the note, potentially using the 2551 1.7 christos address range of the previous note. */ 2552 1.7 christos if (pnote->start == prev_start && pnote->end == prev_end) 2553 1.7 christos { 2554 1.7 christos bfd_put_32 (abfd, pnote->note.namesz, new); 2555 1.7 christos bfd_put_32 (abfd, 0, new + 4); 2556 1.7 christos bfd_put_32 (abfd, pnote->note.type, new + 8); 2557 1.7 christos new += 12; 2558 1.7 christos memcpy (new, pnote->note.namedata, pnote->note.namesz); 2559 1.7 christos if (pnote->note.namesz < pnote->padded_namesz) 2560 1.7 christos memset (new + pnote->note.namesz, 0, pnote->padded_namesz - pnote->note.namesz); 2561 1.7 christos new += pnote->padded_namesz; 2562 1.6 christos } 2563 1.7 christos else 2564 1.7 christos { 2565 1.7 christos bfd_put_32 (abfd, pnote->note.namesz, new); 2566 1.7 christos bfd_put_32 (abfd, is_64bit (abfd) ? 16 : 8, new + 4); 2567 1.7 christos bfd_put_32 (abfd, pnote->note.type, new + 8); 2568 1.7 christos new += 12; 2569 1.7 christos memcpy (new, pnote->note.namedata, pnote->note.namesz); 2570 1.7 christos if (pnote->note.namesz < pnote->padded_namesz) 2571 1.7 christos memset (new + pnote->note.namesz, 0, pnote->padded_namesz - pnote->note.namesz); 2572 1.6 christos new += pnote->padded_namesz; 2573 1.7 christos if (is_64bit (abfd)) 2574 1.7 christos { 2575 1.7 christos bfd_put_64 (abfd, pnote->start, new); 2576 1.6 christos bfd_put_64 (abfd, pnote->end, new + 8); 2577 1.6 christos new += 16; 2578 1.6 christos } 2579 1.7 christos else 2580 1.7 christos { 2581 1.7 christos bfd_put_32 (abfd, pnote->start, new); 2582 1.6 christos bfd_put_32 (abfd, pnote->end, new + 4); 2583 1.7 christos new += 8; 2584 1.6 christos } 2585 1.6 christos 2586 1.6 christos prev_start = pnote->start; 2587 1.7 christos prev_end = pnote->end; 2588 1.6 christos } 2589 1.7 christos } 2590 1.7 christos 2591 1.7 christos old += note_size; 2592 1.7 christos } 2593 1.7 christos 2594 1.7 christos #if DEBUG_MERGE 2595 1.7 christos merge_debug ("Results of merge:\n"); 2596 1.7 christos for (pnote = pnotes; pnote < pnotes_end; pnote ++) 2597 1.7 christos if (! is_deleted_note (pnote)) 2598 1.7 christos merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n", 2599 1.7 christos (pnote->note.namedata - (char *) contents) - 12, 2600 1.7 christos pnote->start, pnote->end, 2601 1.7 christos pnote->note.type, 2602 1.7 christos pnote->note.namedata[3], 2603 1.7 christos pnote->note.namesz 2604 1.6 christos ); 2605 1.10 christos #endif 2606 1.10 christos 2607 1.7 christos size_t nsize = new - new_contents; 2608 1.10 christos if (nsize < size) 2609 1.10 christos { 2610 1.6 christos *new_size = nsize; 2611 1.7 christos memcpy (contents, new_contents, nsize); 2612 1.6 christos } 2613 1.6 christos free (new_contents); 2614 1.6 christos 2615 1.6 christos done: 2616 1.6 christos if (err) 2617 1.6 christos { 2618 1.6 christos bfd_set_error (bfd_error_bad_value); 2619 1.6 christos bfd_nonfatal_message (NULL, abfd, sec, err); 2620 1.6 christos } 2621 1.10 christos 2622 1.6 christos free (pnotes); 2623 1.6 christos return !err; 2624 1.8 christos } 2625 1.9 christos 2626 1.8 christos static flagword 2627 1.8 christos check_new_section_flags (flagword flags, bfd *abfd, const char * secname) 2628 1.8 christos { 2629 1.8 christos /* Only set the SEC_COFF_SHARED flag on COFF files. 2630 1.8 christos The same bit value is used by ELF targets to indicate 2631 1.8 christos compressed sections, and setting that flag here breaks 2632 1.8 christos things. */ 2633 1.8 christos if ((flags & SEC_COFF_SHARED) 2634 1.8 christos && bfd_get_flavour (abfd) != bfd_target_coff_flavour) 2635 1.8 christos { 2636 1.8 christos non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"), 2637 1.8 christos bfd_get_filename (abfd), secname); 2638 1.9 christos flags &= ~ SEC_COFF_SHARED; 2639 1.9 christos } 2640 1.9 christos 2641 1.9 christos /* Report a fatal error if 'large' is used with a non-x86-64 ELF target. 2642 1.9 christos Suppress the error for non-ELF targets to allow -O binary and formats that 2643 1.9 christos use the bit value SEC_ELF_LARGE for other purposes. */ 2644 1.9 christos if ((flags & SEC_ELF_LARGE) != 0 2645 1.9 christos && bfd_get_flavour (abfd) == bfd_target_elf_flavour 2646 1.9 christos && get_elf_backend_data (abfd)->elf_machine_code != EM_X86_64) 2647 1.9 christos { 2648 1.9 christos fatal (_ ("%s[%s]: 'large' flag is ELF x86-64 specific"), 2649 1.9 christos bfd_get_filename (abfd), secname); 2650 1.9 christos flags &= ~SEC_ELF_LARGE; 2651 1.8 christos } 2652 1.8 christos 2653 1.8 christos return flags; 2654 1.9 christos } 2655 1.9 christos 2656 1.9 christos static void 2657 1.9 christos set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style) 2658 1.9 christos { 2659 1.9 christos /* This is only relevant to Coff targets. */ 2660 1.9 christos if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour) 2661 1.9 christos { 2662 1.9 christos if (style == KEEP 2663 1.9 christos && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour) 2664 1.9 christos style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE; 2665 1.9 christos bfd_coff_set_long_section_names (output_bfd, style != DISABLE); 2666 1.9 christos } 2667 1.1 christos } 2668 1.1 christos 2669 1.1 christos /* Copy object file IBFD onto OBFD. 2670 1.8 christos Returns TRUE upon success, FALSE otherwise. */ 2671 1.1 christos 2672 1.1 christos static bool 2673 1.1 christos copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch) 2674 1.1 christos { 2675 1.1 christos bfd_vma start; 2676 1.6 christos long symcount; 2677 1.1 christos asection **osections = NULL; 2678 1.1 christos asection *osec; 2679 1.1 christos asection *gnu_debuglink_section = NULL; 2680 1.1 christos bfd_size_type *gaps = NULL; 2681 1.1 christos bfd_size_type max_gap = 0; 2682 1.1 christos long symsize; 2683 1.1 christos void *dhandle; 2684 1.8 christos enum bfd_architecture iarch; 2685 1.1 christos unsigned int imach; 2686 1.1 christos unsigned int num_sec, i; 2687 1.1 christos 2688 1.1 christos if (ibfd->xvec->byteorder != obfd->xvec->byteorder 2689 1.3 christos && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN 2690 1.3 christos && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN) 2691 1.3 christos { 2692 1.8 christos /* PR 17636: Call non-fatal so that we return to our parent who 2693 1.8 christos may need to tidy temporary files. */ 2694 1.8 christos non_fatal (_("unable to change endianness of '%s'"), 2695 1.8 christos bfd_get_archive_filename (ibfd)); 2696 1.8 christos return false; 2697 1.8 christos } 2698 1.8 christos 2699 1.8 christos if (ibfd->read_only) 2700 1.8 christos { 2701 1.8 christos non_fatal (_("unable to modify '%s' due to errors"), 2702 1.3 christos bfd_get_archive_filename (ibfd)); 2703 1.1 christos return false; 2704 1.1 christos } 2705 1.1 christos 2706 1.1 christos if (!bfd_set_format (obfd, bfd_get_format (ibfd))) 2707 1.8 christos { 2708 1.1 christos bfd_nonfatal_message (NULL, obfd, NULL, NULL); 2709 1.1 christos return false; 2710 1.3 christos } 2711 1.3 christos 2712 1.3 christos if (ibfd->sections == NULL) 2713 1.3 christos { 2714 1.8 christos non_fatal (_("error: the input file '%s' has no sections"), 2715 1.3 christos bfd_get_archive_filename (ibfd)); 2716 1.3 christos return false; 2717 1.9 christos } 2718 1.9 christos 2719 1.9 christos /* This is a no-op on non-Coff targets. */ 2720 1.9 christos set_long_section_mode (obfd, ibfd, long_section_names); 2721 1.9 christos 2722 1.9 christos /* Set the Verilog output endianness based upon the input file's 2723 1.9 christos endianness. We may not be producing verilog format output, 2724 1.9 christos but testing this just adds extra code this is not really 2725 1.9 christos necessary. */ 2726 1.9 christos VerilogDataEndianness = ibfd->xvec->byteorder; 2727 1.9 christos 2728 1.9 christos if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour) 2729 1.9 christos { 2730 1.9 christos if (strip_section_headers) 2731 1.9 christos { 2732 1.9 christos ibfd->flags |= BFD_NO_SECTION_HEADER; 2733 1.9 christos strip_symbols = STRIP_ALL; 2734 1.9 christos merge_notes = true; 2735 1.9 christos } 2736 1.3 christos } 2737 1.5 christos else 2738 1.5 christos { 2739 1.5 christos if ((do_debug_sections & compress) != 0 2740 1.9 christos && do_debug_sections != compress) 2741 1.9 christos { 2742 1.5 christos non_fatal (_ ("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi|" 2743 1.8 christos "zstd] is unsupported on `%s'"), 2744 1.5 christos bfd_get_archive_filename (ibfd)); 2745 1.5 christos return false; 2746 1.5 christos } 2747 1.5 christos 2748 1.5 christos if (do_elf_stt_common) 2749 1.5 christos { 2750 1.8 christos non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"), 2751 1.5 christos bfd_get_archive_filename (ibfd)); 2752 1.9 christos return false; 2753 1.9 christos } 2754 1.9 christos 2755 1.9 christos if (strip_section_headers) 2756 1.9 christos { 2757 1.9 christos non_fatal (_("--strip-section-headers is unsupported on `%s'"), 2758 1.9 christos bfd_get_archive_filename (ibfd)); 2759 1.3 christos return false; 2760 1.3 christos } 2761 1.1 christos } 2762 1.1 christos 2763 1.1 christos if (verbose) 2764 1.1 christos printf (_("copy from `%s' [%s] to `%s' [%s]\n"), 2765 1.1 christos bfd_get_archive_filename (ibfd), bfd_get_target (ibfd), 2766 1.1 christos bfd_get_filename (obfd), bfd_get_target (obfd)); 2767 1.1 christos 2768 1.1 christos if (extract_symbol) 2769 1.1 christos start = 0; 2770 1.1 christos else 2771 1.1 christos { 2772 1.1 christos if (set_start_set) 2773 1.1 christos start = set_start; 2774 1.1 christos else 2775 1.1 christos start = bfd_get_start_address (ibfd); 2776 1.1 christos start += change_start; 2777 1.1 christos } 2778 1.1 christos 2779 1.1 christos /* Neither the start address nor the flags 2780 1.1 christos need to be set for a core file. */ 2781 1.1 christos if (bfd_get_format (obfd) != bfd_core) 2782 1.1 christos { 2783 1.1 christos flagword flags; 2784 1.1 christos 2785 1.1 christos flags = bfd_get_file_flags (ibfd); 2786 1.1 christos flags |= bfd_flags_to_set; 2787 1.1 christos flags &= ~bfd_flags_to_clear; 2788 1.1 christos flags &= bfd_applicable_file_flags (obfd); 2789 1.1 christos 2790 1.1 christos if (strip_symbols == STRIP_ALL) 2791 1.1 christos flags &= ~HAS_RELOC; 2792 1.1 christos 2793 1.1 christos if (!bfd_set_start_address (obfd, start) 2794 1.1 christos || !bfd_set_file_flags (obfd, flags)) 2795 1.8 christos { 2796 1.1 christos bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 2797 1.1 christos return false; 2798 1.1 christos } 2799 1.1 christos } 2800 1.1 christos 2801 1.1 christos /* Copy architecture of input file to output file. */ 2802 1.1 christos iarch = bfd_get_arch (ibfd); 2803 1.1 christos imach = bfd_get_mach (ibfd); 2804 1.7 christos if (input_arch) 2805 1.1 christos { 2806 1.1 christos if (iarch == bfd_arch_unknown) 2807 1.1 christos { 2808 1.1 christos iarch = input_arch->arch; 2809 1.1 christos imach = input_arch->mach; 2810 1.1 christos } 2811 1.1 christos else 2812 1.1 christos non_fatal (_("Input file `%s' ignores binary architecture parameter."), 2813 1.7 christos bfd_get_archive_filename (ibfd)); 2814 1.7 christos } 2815 1.7 christos if (iarch == bfd_arch_unknown 2816 1.7 christos && bfd_get_flavour (ibfd) != bfd_target_elf_flavour 2817 1.7 christos && bfd_get_flavour (obfd) == bfd_target_elf_flavour) 2818 1.7 christos { 2819 1.7 christos const struct elf_backend_data *bed = get_elf_backend_data (obfd); 2820 1.7 christos iarch = bed->arch; 2821 1.1 christos imach = 0; 2822 1.1 christos } 2823 1.1 christos if (!bfd_set_arch_mach (obfd, iarch, imach) 2824 1.1 christos && (ibfd->target_defaulted 2825 1.1 christos || bfd_get_arch (ibfd) != bfd_get_arch (obfd))) 2826 1.1 christos { 2827 1.1 christos if (bfd_get_arch (ibfd) == bfd_arch_unknown) 2828 1.1 christos non_fatal (_("Unable to recognise the format of the input file `%s'"), 2829 1.1 christos bfd_get_archive_filename (ibfd)); 2830 1.1 christos else 2831 1.1 christos non_fatal (_("Output file cannot represent architecture `%s'"), 2832 1.8 christos bfd_printable_arch_mach (bfd_get_arch (ibfd), 2833 1.1 christos bfd_get_mach (ibfd))); 2834 1.1 christos return false; 2835 1.1 christos } 2836 1.1 christos 2837 1.1 christos if (!bfd_set_format (obfd, bfd_get_format (ibfd))) 2838 1.8 christos { 2839 1.1 christos bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 2840 1.1 christos return false; 2841 1.1 christos } 2842 1.1 christos 2843 1.1 christos if (bfd_get_flavour (obfd) == bfd_target_coff_flavour 2844 1.1 christos && bfd_pei_p (obfd)) 2845 1.1 christos { 2846 1.1 christos /* Set up PE parameters. */ 2847 1.1 christos pe_data_type *pe = pe_data (obfd); 2848 1.7 christos 2849 1.1 christos /* Copy PE parameters before changing them. */ 2850 1.8 christos if (bfd_get_flavour (ibfd) == bfd_target_coff_flavour 2851 1.8 christos && bfd_pei_p (ibfd)) 2852 1.8 christos { 2853 1.8 christos pe->pe_opthdr = pe_data (ibfd)->pe_opthdr; 2854 1.8 christos 2855 1.8 christos if (preserve_dates) 2856 1.8 christos pe->timestamp = pe_data (ibfd)->coff.timestamp; 2857 1.8 christos else 2858 1.1 christos pe->timestamp = -1; 2859 1.1 christos } 2860 1.1 christos 2861 1.10 christos if (pe_file_alignment != (bfd_vma) -1) 2862 1.1 christos pe->pe_opthdr.FileAlignment = pe_file_alignment; 2863 1.1 christos else if (!is_strip) 2864 1.1 christos pe_file_alignment = PE_DEF_FILE_ALIGNMENT; 2865 1.1 christos 2866 1.1 christos if (pe_heap_commit != (bfd_vma) -1) 2867 1.1 christos pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit; 2868 1.1 christos 2869 1.1 christos if (pe_heap_reserve != (bfd_vma) -1) 2870 1.1 christos pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve; 2871 1.1 christos 2872 1.1 christos if (pe_image_base != (bfd_vma) -1) 2873 1.1 christos pe->pe_opthdr.ImageBase = pe_image_base; 2874 1.1 christos 2875 1.10 christos if (pe_section_alignment != (bfd_vma) -1) 2876 1.1 christos pe->pe_opthdr.SectionAlignment = pe_section_alignment; 2877 1.1 christos else if (!is_strip) 2878 1.1 christos pe_section_alignment = PE_DEF_SECTION_ALIGNMENT; 2879 1.1 christos 2880 1.1 christos if (pe_stack_commit != (bfd_vma) -1) 2881 1.1 christos pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit; 2882 1.9 christos 2883 1.1 christos if (pe_stack_reserve != (bfd_vma) -1) 2884 1.1 christos pe->pe_opthdr.SizeOfStackReserve = pe_stack_reserve; 2885 1.1 christos 2886 1.1 christos if (pe_subsystem != -1) 2887 1.1 christos pe->pe_opthdr.Subsystem = pe_subsystem; 2888 1.1 christos 2889 1.1 christos if (pe_major_subsystem_version != -1) 2890 1.1 christos pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version; 2891 1.1 christos 2892 1.1 christos if (pe_minor_subsystem_version != -1) 2893 1.1 christos pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version; 2894 1.1 christos 2895 1.9 christos if (pe_file_alignment > pe_section_alignment) 2896 1.9 christos { 2897 1.9 christos non_fatal (_("warning: file alignment (0x%" PRIx64 2898 1.9 christos ") > section alignment (0x%" PRIx64 ")"), 2899 1.1 christos (uint64_t) pe_file_alignment, 2900 1.1 christos (uint64_t) pe_section_alignment); 2901 1.1 christos } 2902 1.8 christos } 2903 1.1 christos 2904 1.1 christos free (isympp); 2905 1.1 christos 2906 1.1 christos if (osympp != isympp) 2907 1.1 christos free (osympp); 2908 1.1 christos 2909 1.1 christos isympp = NULL; 2910 1.1 christos osympp = NULL; 2911 1.1 christos 2912 1.1 christos symsize = bfd_get_symtab_upper_bound (ibfd); 2913 1.1 christos if (symsize < 0) 2914 1.8 christos { 2915 1.1 christos bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 2916 1.1 christos return false; 2917 1.1 christos } 2918 1.1 christos 2919 1.1 christos osympp = isympp = (asymbol **) xmalloc (symsize); 2920 1.1 christos symcount = bfd_canonicalize_symtab (ibfd, isympp); 2921 1.1 christos if (symcount < 0) 2922 1.8 christos { 2923 1.1 christos bfd_nonfatal_message (NULL, ibfd, NULL, NULL); 2924 1.3 christos return false; 2925 1.3 christos } 2926 1.3 christos /* PR 17512: file: d6323821 2927 1.3 christos If the symbol table could not be loaded do not pretend that we have 2928 1.3 christos any symbols. This trips us up later on when we load the relocs. */ 2929 1.3 christos if (symcount == 0) 2930 1.3 christos { 2931 1.3 christos free (isympp); 2932 1.1 christos osympp = isympp = NULL; 2933 1.1 christos } 2934 1.1 christos 2935 1.10 christos /* BFD mandates that all output sections be created and sizes set before 2936 1.10 christos any output is done. Thus, we traverse all sections multiple times. */ 2937 1.10 christos for (asection *s = ibfd->sections; s != NULL; s = s->next) 2938 1.1 christos if (!setup_section (ibfd, s, obfd)) 2939 1.1 christos return false; 2940 1.10 christos 2941 1.10 christos if (!extract_symbol) 2942 1.10 christos { 2943 1.10 christos if (!setup_bfd_headers (ibfd, obfd)) 2944 1.1 christos return false; 2945 1.1 christos } 2946 1.1 christos 2947 1.1 christos if (add_sections != NULL) 2948 1.1 christos { 2949 1.1 christos struct section_add *padd; 2950 1.1 christos struct section_list *pset; 2951 1.1 christos 2952 1.1 christos for (padd = add_sections; padd != NULL; padd = padd->next) 2953 1.1 christos { 2954 1.8 christos flagword flags; 2955 1.3 christos 2956 1.1 christos pset = find_section_list (padd->name, false, 2957 1.10 christos SECTION_CONTEXT_SET_FLAGS); 2958 1.8 christos if (pset != NULL) 2959 1.8 christos { 2960 1.8 christos flags = pset->flags | SEC_HAS_CONTENTS; 2961 1.3 christos flags = check_new_section_flags (flags, obfd, padd->name); 2962 1.3 christos } 2963 1.1 christos else 2964 1.1 christos flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA; 2965 1.1 christos 2966 1.1 christos /* bfd_make_section_with_flags() does not return very helpful 2967 1.1 christos error codes, so check for the most likely user error first. */ 2968 1.1 christos if (bfd_get_section_by_name (obfd, padd->name)) 2969 1.5 christos { 2970 1.8 christos bfd_nonfatal_message (NULL, obfd, NULL, 2971 1.1 christos _("can't add section '%s'"), padd->name); 2972 1.1 christos return false; 2973 1.1 christos } 2974 1.1 christos else 2975 1.5 christos { 2976 1.5 christos /* We use LINKER_CREATED here so that the backend hooks 2977 1.5 christos will create any special section type information, 2978 1.1 christos instead of presuming we know what we're doing merely 2979 1.1 christos because we set the flags. */ 2980 1.1 christos padd->section = bfd_make_section_with_flags 2981 1.1 christos (obfd, padd->name, flags | SEC_LINKER_CREATED); 2982 1.1 christos if (padd->section == NULL) 2983 1.1 christos { 2984 1.1 christos bfd_nonfatal_message (NULL, obfd, NULL, 2985 1.8 christos _("can't create section `%s'"), 2986 1.1 christos padd->name); 2987 1.1 christos return false; 2988 1.1 christos } 2989 1.7 christos } 2990 1.1 christos 2991 1.1 christos if (!bfd_set_section_size (padd->section, padd->size)) 2992 1.8 christos { 2993 1.1 christos bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 2994 1.1 christos return false; 2995 1.8 christos } 2996 1.3 christos 2997 1.3 christos pset = find_section_list (padd->name, false, 2998 1.7 christos SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA); 2999 1.3 christos if (pset != NULL 3000 1.3 christos && !bfd_set_section_vma (padd->section, pset->vma_val)) 3001 1.8 christos { 3002 1.3 christos bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 3003 1.3 christos return false; 3004 1.8 christos } 3005 1.3 christos 3006 1.1 christos pset = find_section_list (padd->name, false, 3007 1.1 christos SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA); 3008 1.3 christos if (pset != NULL) 3009 1.1 christos { 3010 1.7 christos padd->section->lma = pset->lma_val; 3011 1.7 christos 3012 1.1 christos if (!bfd_set_section_alignment 3013 1.3 christos (padd->section, bfd_section_alignment (padd->section))) 3014 1.8 christos { 3015 1.1 christos bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 3016 1.1 christos return false; 3017 1.1 christos } 3018 1.1 christos } 3019 1.1 christos } 3020 1.3 christos } 3021 1.1 christos 3022 1.3 christos if (update_sections != NULL) 3023 1.1 christos { 3024 1.3 christos struct section_add *pupdate; 3025 1.3 christos 3026 1.3 christos for (pupdate = update_sections; 3027 1.1 christos pupdate != NULL; 3028 1.3 christos pupdate = pupdate->next) 3029 1.3 christos { 3030 1.3 christos pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name); 3031 1.3 christos if (pupdate->section == NULL) 3032 1.8 christos { 3033 1.3 christos non_fatal (_("error: %s not found, can't be updated"), pupdate->name); 3034 1.3 christos return false; 3035 1.3 christos } 3036 1.7 christos 3037 1.3 christos osec = pupdate->section->output_section; 3038 1.3 christos if (!bfd_set_section_size (osec, pupdate->size)) 3039 1.8 christos { 3040 1.3 christos bfd_nonfatal_message (NULL, obfd, osec, NULL); 3041 1.1 christos return false; 3042 1.3 christos } 3043 1.3 christos } 3044 1.7 christos } 3045 1.6 christos 3046 1.6 christos merged_note_section * merged_note_sections = NULL; 3047 1.6 christos if (merge_notes) 3048 1.6 christos { 3049 1.7 christos /* This palaver is necessary because we must set the output 3050 1.6 christos section size first, before its contents are ready. */ 3051 1.7 christos for (osec = ibfd->sections; osec != NULL; osec = osec->next) 3052 1.7 christos { 3053 1.7 christos if (! is_mergeable_note_section (ibfd, osec)) 3054 1.7 christos continue; 3055 1.7 christos 3056 1.7 christos /* If the section is going to be completly deleted then 3057 1.7 christos do not bother to merge it. */ 3058 1.7 christos if (osec->output_section == NULL) 3059 1.7 christos continue; 3060 1.7 christos 3061 1.6 christos bfd_size_type size = bfd_section_size (osec); 3062 1.9 christos 3063 1.9 christos if (size == 0) 3064 1.9 christos /* This can happen, eg when stripping a binary for a second 3065 1.7 christos time. See BZ 2121365 for an example. */ 3066 1.7 christos continue; 3067 1.7 christos 3068 1.7 christos merged_note_section * merged = xmalloc (sizeof * merged); 3069 1.6 christos merged->contents = NULL; 3070 1.7 christos if (! bfd_get_full_section_contents (ibfd, osec, & merged->contents)) 3071 1.7 christos { 3072 1.7 christos bfd_nonfatal_message (NULL, ibfd, osec, 3073 1.7 christos _("warning: could not load note section")); 3074 1.6 christos free (merged); 3075 1.7 christos continue; 3076 1.10 christos } 3077 1.10 christos 3078 1.10 christos if (!merge_gnu_build_notes (ibfd, osec, size, 3079 1.10 christos merged->contents, &merged->size)) 3080 1.10 christos { 3081 1.10 christos free (merged->contents); 3082 1.10 christos free (merged); 3083 1.7 christos return false; 3084 1.7 christos } 3085 1.10 christos 3086 1.7 christos /* FIXME: Once we have read the contents in, we must write 3087 1.7 christos them out again. So even if the merging has achieved 3088 1.7 christos nothing we still add this entry to the merge list. */ 3089 1.7 christos 3090 1.7 christos if (size != merged->size 3091 1.7 christos && !bfd_set_section_size (osec->output_section, merged->size)) 3092 1.7 christos { 3093 1.7 christos bfd_nonfatal_message (NULL, obfd, osec, 3094 1.7 christos _("warning: failed to set merged notes size")); 3095 1.7 christos free (merged->contents); 3096 1.6 christos free (merged); 3097 1.7 christos continue; 3098 1.7 christos } 3099 1.7 christos 3100 1.7 christos /* Add section to list of merged sections. */ 3101 1.7 christos merged->sec = osec; 3102 1.6 christos merged->next = merged_note_sections; 3103 1.6 christos merged_note_sections = merged; 3104 1.6 christos } 3105 1.3 christos } 3106 1.3 christos 3107 1.3 christos if (dump_sections != NULL) 3108 1.1 christos { 3109 1.3 christos struct section_add * pdump; 3110 1.1 christos 3111 1.8 christos for (pdump = dump_sections; pdump != NULL; pdump = pdump->next) 3112 1.8 christos { 3113 1.8 christos FILE * f; 3114 1.6 christos bfd_byte *contents; 3115 1.6 christos 3116 1.3 christos osec = bfd_get_section_by_name (ibfd, pdump->name); 3117 1.3 christos if (osec == NULL) 3118 1.3 christos { 3119 1.3 christos bfd_nonfatal_message (NULL, ibfd, NULL, 3120 1.3 christos _("can't dump section '%s' - it does not exist"), 3121 1.3 christos pdump->name); 3122 1.3 christos continue; 3123 1.7 christos } 3124 1.3 christos 3125 1.6 christos if ((bfd_section_flags (osec) & SEC_HAS_CONTENTS) == 0) 3126 1.3 christos { 3127 1.3 christos bfd_nonfatal_message (NULL, ibfd, osec, 3128 1.3 christos _("can't dump section - it has no contents")); 3129 1.3 christos continue; 3130 1.7 christos } 3131 1.8 christos 3132 1.8 christos bfd_size_type size = bfd_section_size (osec); 3133 1.3 christos /* Note - we allow the dumping of zero-sized sections, 3134 1.3 christos creating an empty file. */ 3135 1.3 christos 3136 1.3 christos f = fopen (pdump->filename, FOPEN_WB); 3137 1.3 christos if (f == NULL) 3138 1.3 christos { 3139 1.3 christos bfd_nonfatal_message (pdump->filename, NULL, NULL, 3140 1.3 christos _("could not open section dump file")); 3141 1.3 christos continue; 3142 1.6 christos } 3143 1.3 christos 3144 1.8 christos if (bfd_malloc_and_get_section (ibfd, osec, &contents)) 3145 1.3 christos { 3146 1.3 christos if (size != 0 && fwrite (contents, 1, size, f) != size) 3147 1.3 christos { 3148 1.3 christos non_fatal (_("error writing section contents to %s (error: %s)"), 3149 1.6 christos pdump->filename, 3150 1.7 christos strerror (errno)); 3151 1.10 christos free (contents); 3152 1.3 christos fclose (f); 3153 1.3 christos goto fail; 3154 1.1 christos } 3155 1.6 christos } 3156 1.3 christos else 3157 1.1 christos bfd_nonfatal_message (NULL, ibfd, osec, 3158 1.3 christos _("could not retrieve section contents")); 3159 1.3 christos 3160 1.1 christos fclose (f); 3161 1.1 christos free (contents); 3162 1.1 christos } 3163 1.3 christos } 3164 1.3 christos 3165 1.3 christos if (gnu_debuglink_filename != NULL) 3166 1.3 christos { 3167 1.3 christos /* PR 15125: Give a helpful warning message if 3168 1.3 christos the debuglink section already exists, and 3169 1.3 christos allow the rest of the copy to complete. */ 3170 1.3 christos if (bfd_get_section_by_name (obfd, ".gnu_debuglink")) 3171 1.9 christos { 3172 1.3 christos non_fatal (_("%s: debuglink section already exists"), 3173 1.3 christos bfd_get_filename (ibfd)); 3174 1.3 christos gnu_debuglink_filename = NULL; 3175 1.3 christos } 3176 1.3 christos else 3177 1.3 christos { 3178 1.3 christos gnu_debuglink_section = bfd_create_gnu_debuglink_section 3179 1.3 christos (obfd, gnu_debuglink_filename); 3180 1.3 christos 3181 1.3 christos if (gnu_debuglink_section == NULL) 3182 1.3 christos { 3183 1.3 christos bfd_nonfatal_message (NULL, obfd, NULL, 3184 1.10 christos _("cannot create debug link section `%s'"), 3185 1.3 christos gnu_debuglink_filename); 3186 1.3 christos goto fail; 3187 1.3 christos } 3188 1.3 christos 3189 1.3 christos /* Special processing for PE format files. We 3190 1.3 christos have no way to distinguish PE from COFF here. */ 3191 1.3 christos if (bfd_get_flavour (obfd) == bfd_target_coff_flavour) 3192 1.3 christos { 3193 1.3 christos bfd_vma debuglink_vma; 3194 1.3 christos asection * highest_section; 3195 1.3 christos 3196 1.3 christos /* The PE spec requires that all sections be adjacent and sorted 3197 1.3 christos in ascending order of VMA. It also specifies that debug 3198 1.3 christos sections should be last. This is despite the fact that debug 3199 1.3 christos sections are not loaded into memory and so in theory have no 3200 1.3 christos use for a VMA. 3201 1.3 christos 3202 1.3 christos This means that the debuglink section must be given a non-zero 3203 1.3 christos VMA which makes it contiguous with other debug sections. So 3204 1.6 christos walk the current section list, find the section with the 3205 1.6 christos highest VMA and start the debuglink section after that one. */ 3206 1.6 christos for (osec = obfd->sections, highest_section = NULL; 3207 1.6 christos osec != NULL; 3208 1.3 christos osec = osec->next) 3209 1.6 christos if (osec->vma > 0 3210 1.6 christos && (highest_section == NULL 3211 1.3 christos || osec->vma > highest_section->vma)) 3212 1.3 christos highest_section = osec; 3213 1.3 christos 3214 1.3 christos if (highest_section) 3215 1.3 christos debuglink_vma = BFD_ALIGN (highest_section->vma 3216 1.3 christos + highest_section->size, 3217 1.7 christos /* FIXME: We ought to be using 3218 1.3 christos COFF_PAGE_SIZE here or maybe 3219 1.3 christos bfd_section_alignment() (if it 3220 1.3 christos was set) but since this is for PE 3221 1.3 christos and we know the required alignment 3222 1.3 christos it is easier just to hard code it. */ 3223 1.3 christos 0x1000); 3224 1.3 christos else 3225 1.3 christos /* Umm, not sure what to do in this case. */ 3226 1.7 christos debuglink_vma = 0x1000; 3227 1.3 christos 3228 1.3 christos bfd_set_section_vma (gnu_debuglink_section, debuglink_vma); 3229 1.3 christos } 3230 1.3 christos } 3231 1.8 christos } 3232 1.8 christos 3233 1.1 christos num_sec = bfd_count_sections (obfd); 3234 1.1 christos if (num_sec != 0 3235 1.1 christos && (gap_fill_set || pad_to_set)) 3236 1.1 christos { 3237 1.1 christos /* We must fill in gaps between the sections and/or we must pad 3238 1.1 christos the last section to a specified address. We do this by 3239 1.1 christos grabbing a list of the sections, sorting them by VMA, and 3240 1.1 christos increasing the section sizes as required to fill the gaps. 3241 1.8 christos We write out the gap contents below. */ 3242 1.10 christos 3243 1.10 christos osections = xmalloc (num_sec * sizeof (*osections)); 3244 1.10 christos asection **set = osections; 3245 1.1 christos for (asection *s = obfd->sections; s != NULL; s = s->next) 3246 1.8 christos *set++ = s; 3247 1.1 christos 3248 1.8 christos qsort (osections, num_sec, sizeof (*osections), compare_section_lma); 3249 1.8 christos 3250 1.1 christos gaps = xmalloc (num_sec * sizeof (*gaps)); 3251 1.1 christos memset (gaps, 0, num_sec * sizeof (*gaps)); 3252 1.1 christos 3253 1.8 christos if (gap_fill_set) 3254 1.1 christos { 3255 1.1 christos for (i = 0; i < num_sec - 1; i++) 3256 1.8 christos { 3257 1.8 christos flagword flags; 3258 1.8 christos bfd_size_type size; /* Octets. */ 3259 1.8 christos bfd_vma gap_start, gap_stop; /* Octets. */ 3260 1.1 christos unsigned int opb1 = bfd_octets_per_byte (obfd, osections[i]); 3261 1.7 christos unsigned int opb2 = bfd_octets_per_byte (obfd, osections[i+1]); 3262 1.1 christos 3263 1.1 christos flags = bfd_section_flags (osections[i]); 3264 1.1 christos if ((flags & SEC_HAS_CONTENTS) == 0 3265 1.1 christos || (flags & SEC_LOAD) == 0) 3266 1.7 christos continue; 3267 1.8 christos 3268 1.8 christos size = bfd_section_size (osections[i]); 3269 1.1 christos gap_start = bfd_section_lma (osections[i]) * opb1 + size; 3270 1.1 christos gap_stop = bfd_section_lma (osections[i + 1]) * opb2; 3271 1.7 christos if (gap_start < gap_stop) 3272 1.7 christos { 3273 1.1 christos if (!bfd_set_section_size (osections[i], 3274 1.1 christos size + (gap_stop - gap_start))) 3275 1.1 christos { 3276 1.10 christos bfd_nonfatal_message (NULL, obfd, osections[i], 3277 1.1 christos _("Can't fill gap after section")); 3278 1.1 christos goto fail; 3279 1.1 christos } 3280 1.1 christos gaps[i] = gap_stop - gap_start; 3281 1.1 christos if (max_gap < gap_stop - gap_start) 3282 1.1 christos max_gap = gap_stop - gap_start; 3283 1.1 christos } 3284 1.1 christos } 3285 1.1 christos } 3286 1.1 christos 3287 1.8 christos if (pad_to_set) 3288 1.8 christos { 3289 1.8 christos bfd_vma lma; /* Octets. */ 3290 1.8 christos bfd_size_type size; /* Octets. */ 3291 1.8 christos unsigned int opb = bfd_octets_per_byte (obfd, osections[num_sec - 1]); 3292 1.8 christos bfd_vma _pad_to = pad_to * opb; 3293 1.8 christos 3294 1.8 christos lma = bfd_section_lma (osections[num_sec - 1]) * opb; 3295 1.1 christos size = bfd_section_size (osections[num_sec - 1]); 3296 1.8 christos if (lma + size < _pad_to) 3297 1.1 christos { 3298 1.8 christos if (!bfd_set_section_size (osections[num_sec - 1], _pad_to - lma)) 3299 1.1 christos { 3300 1.10 christos bfd_nonfatal_message (NULL, obfd, osections[num_sec - 1], 3301 1.1 christos _("can't add padding")); 3302 1.1 christos goto fail; 3303 1.1 christos } 3304 1.8 christos else 3305 1.8 christos { 3306 1.8 christos gaps[num_sec - 1] = _pad_to - (lma + size); 3307 1.1 christos if (max_gap < _pad_to - (lma + size)) 3308 1.1 christos max_gap = _pad_to - (lma + size); 3309 1.1 christos } 3310 1.1 christos } 3311 1.1 christos } 3312 1.1 christos } 3313 1.1 christos 3314 1.1 christos /* Symbol filtering must happen after the output sections 3315 1.1 christos have been created, but before their contents are set. */ 3316 1.8 christos dhandle = NULL; 3317 1.8 christos if (convert_debugging) 3318 1.8 christos dhandle = read_debugging_info (ibfd, isympp, symcount, false); 3319 1.8 christos 3320 1.8 christos if ((obfd->flags & (EXEC_P | DYNAMIC)) != 0 3321 1.8 christos && (obfd->flags & HAS_RELOC) == 0) 3322 1.8 christos { 3323 1.8 christos if (bfd_keep_unused_section_symbols (obfd) || keep_section_symbols) 3324 1.8 christos { 3325 1.8 christos /* Non-relocatable inputs may not have the unused section 3326 1.8 christos symbols. Mark all section symbols as used to generate 3327 1.8 christos section symbols. */ 3328 1.8 christos asection *asect; 3329 1.8 christos for (asect = obfd->sections; asect != NULL; asect = asect->next) 3330 1.8 christos if (asect->symbol) 3331 1.8 christos asect->symbol->flags |= BSF_SECTION_SYM_USED; 3332 1.8 christos } 3333 1.8 christos else 3334 1.8 christos { 3335 1.8 christos /* Non-relocatable inputs may have the unused section symbols. 3336 1.8 christos Mark all section symbols as unused to excluded them. */ 3337 1.8 christos long s; 3338 1.8 christos for (s = 0; s < symcount; s++) 3339 1.8 christos if ((isympp[s]->flags & BSF_SECTION_SYM_USED)) 3340 1.8 christos isympp[s]->flags &= ~BSF_SECTION_SYM_USED; 3341 1.1 christos } 3342 1.1 christos } 3343 1.1 christos 3344 1.1 christos if (strip_symbols == STRIP_DEBUG 3345 1.1 christos || strip_symbols == STRIP_ALL 3346 1.1 christos || strip_symbols == STRIP_UNNEEDED 3347 1.1 christos || strip_symbols == STRIP_NONDEBUG 3348 1.1 christos || strip_symbols == STRIP_DWO 3349 1.1 christos || strip_symbols == STRIP_NONDWO 3350 1.1 christos || discard_locals != LOCALS_UNDEF 3351 1.1 christos || localize_hidden 3352 1.1 christos || htab_elements (strip_specific_htab) != 0 3353 1.1 christos || htab_elements (keep_specific_htab) != 0 3354 1.1 christos || htab_elements (localize_specific_htab) != 0 3355 1.1 christos || htab_elements (globalize_specific_htab) != 0 3356 1.6 christos || htab_elements (keepglobal_specific_htab) != 0 3357 1.1 christos || htab_elements (weaken_specific_htab) != 0 3358 1.1 christos || htab_elements (redefine_specific_htab) != 0 3359 1.1 christos || prefix_symbols_string 3360 1.1 christos || sections_removed 3361 1.1 christos || sections_copied 3362 1.1 christos || convert_debugging 3363 1.5 christos || change_leading_char 3364 1.3 christos || remove_leading_char 3365 1.3 christos || section_rename_list 3366 1.1 christos || weaken 3367 1.1 christos || add_symbols) 3368 1.1 christos { 3369 1.1 christos /* Mark symbols used in output relocations so that they 3370 1.1 christos are kept, even if they are local labels or static symbols. 3371 1.1 christos 3372 1.1 christos Note we iterate over the input sections examining their 3373 1.1 christos relocations since the relocations for the output sections 3374 1.1 christos haven't been set yet. mark_symbols_used_in_relocations will 3375 1.1 christos ignore input sections which have no corresponding output 3376 1.6 christos section. */ 3377 1.6 christos if (strip_symbols != STRIP_ALL) 3378 1.10 christos { 3379 1.10 christos bfd_set_error (bfd_error_no_error); 3380 1.10 christos for (asection *s = ibfd->sections; s != NULL; s = s->next) 3381 1.10 christos if (!mark_symbols_used_in_relocations (ibfd, s, isympp) 3382 1.6 christos || bfd_get_error () != bfd_error_no_error) 3383 1.6 christos goto fail; 3384 1.3 christos } 3385 1.10 christos 3386 1.10 christos osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *)); 3387 1.1 christos if (!filter_symbols (ibfd, obfd, osympp, isympp, &symcount)) 3388 1.1 christos goto fail; 3389 1.10 christos } 3390 1.10 christos 3391 1.10 christos for (long s = 0; s < symcount; s++) 3392 1.10 christos if (!bfd_copy_private_symbol_data (ibfd, osympp[s], obfd, osympp[s])) 3393 1.9 christos goto fail; 3394 1.1 christos 3395 1.10 christos if (dhandle != NULL) 3396 1.10 christos { 3397 1.1 christos if (!write_debugging_info (obfd, dhandle, &symcount, &osympp)) 3398 1.1 christos goto fail; 3399 1.1 christos } 3400 1.1 christos 3401 1.1 christos bfd_set_symtab (obfd, osympp, symcount); 3402 1.10 christos 3403 1.10 christos /* This has to happen before section positions are set. */ 3404 1.10 christos for (asection *s = ibfd->sections; s != NULL; s = s->next) 3405 1.1 christos if (!copy_relocations_in_section (ibfd, s, obfd)) 3406 1.1 christos goto fail; 3407 1.10 christos 3408 1.10 christos /* This has to happen after the symbol table has been set. */ 3409 1.10 christos for (asection *s = ibfd->sections; s != NULL; s = s->next) 3410 1.1 christos if (!copy_section (ibfd, s, obfd)) 3411 1.1 christos goto fail; 3412 1.1 christos 3413 1.1 christos if (add_sections != NULL) 3414 1.1 christos { 3415 1.1 christos struct section_add *padd; 3416 1.1 christos 3417 1.1 christos for (padd = add_sections; padd != NULL; padd = padd->next) 3418 1.1 christos { 3419 1.1 christos if (! bfd_set_section_contents (obfd, padd->section, padd->contents, 3420 1.1 christos 0, padd->size)) 3421 1.10 christos { 3422 1.1 christos bfd_nonfatal_message (NULL, obfd, padd->section, NULL); 3423 1.1 christos goto fail; 3424 1.1 christos } 3425 1.1 christos } 3426 1.3 christos } 3427 1.3 christos 3428 1.3 christos if (update_sections != NULL) 3429 1.3 christos { 3430 1.3 christos struct section_add *pupdate; 3431 1.5 christos 3432 1.5 christos for (pupdate = update_sections; 3433 1.3 christos pupdate != NULL; 3434 1.3 christos pupdate = pupdate->next) 3435 1.3 christos { 3436 1.5 christos osec = pupdate->section->output_section; 3437 1.3 christos if (! bfd_set_section_contents (obfd, osec, pupdate->contents, 3438 1.3 christos 0, pupdate->size)) 3439 1.10 christos { 3440 1.3 christos bfd_nonfatal_message (NULL, obfd, osec, NULL); 3441 1.3 christos goto fail; 3442 1.3 christos } 3443 1.3 christos } 3444 1.7 christos } 3445 1.6 christos 3446 1.7 christos if (merged_note_sections != NULL) 3447 1.7 christos { 3448 1.7 christos merged_note_section * merged = NULL; 3449 1.6 christos 3450 1.7 christos for (osec = obfd->sections; osec != NULL; osec = osec->next) 3451 1.7 christos { 3452 1.7 christos if (! is_mergeable_note_section (obfd, osec)) 3453 1.7 christos continue; 3454 1.7 christos 3455 1.7 christos if (merged == NULL) 3456 1.7 christos merged = merged_note_sections; 3457 1.7 christos 3458 1.7 christos /* It is likely that output sections are in the same order 3459 1.8 christos as the input sections, but do not assume that this is 3460 1.7 christos the case. */ 3461 1.7 christos if (merged->sec->output_section != osec) 3462 1.7 christos { 3463 1.7 christos for (merged = merged_note_sections; 3464 1.8 christos merged != NULL; 3465 1.7 christos merged = merged->next) 3466 1.7 christos if (merged->sec->output_section == osec) 3467 1.7 christos break; 3468 1.7 christos 3469 1.7 christos if (merged == NULL) 3470 1.7 christos { 3471 1.7 christos bfd_nonfatal_message 3472 1.7 christos (NULL, obfd, osec, 3473 1.7 christos _("error: failed to locate merged notes")); 3474 1.7 christos continue; 3475 1.7 christos } 3476 1.7 christos } 3477 1.7 christos 3478 1.7 christos if (merged->contents == NULL) 3479 1.7 christos { 3480 1.7 christos bfd_nonfatal_message 3481 1.7 christos (NULL, obfd, osec, 3482 1.7 christos _("error: failed to merge notes")); 3483 1.7 christos continue; 3484 1.7 christos } 3485 1.7 christos 3486 1.6 christos if (! bfd_set_section_contents (obfd, osec, merged->contents, 0, 3487 1.7 christos merged->size)) 3488 1.7 christos { 3489 1.7 christos bfd_nonfatal_message 3490 1.10 christos (NULL, obfd, osec, 3491 1.6 christos _("error: failed to copy merged notes into output")); 3492 1.7 christos goto fail; 3493 1.7 christos } 3494 1.7 christos 3495 1.7 christos merged = merged->next; 3496 1.7 christos } 3497 1.10 christos 3498 1.7 christos /* Free the memory. */ 3499 1.10 christos while (merged_note_sections != NULL) 3500 1.10 christos { 3501 1.10 christos merged_note_section *next = merged_note_sections->next; 3502 1.10 christos free (merged_note_sections->contents); 3503 1.6 christos free (merged_note_sections); 3504 1.6 christos merged_note_sections = next; 3505 1.9 christos } 3506 1.7 christos } 3507 1.7 christos else if (merge_notes && ! is_strip && ! strip_section_headers) 3508 1.6 christos non_fatal (_("%s: Could not find any mergeable note sections"), 3509 1.1 christos bfd_get_filename (ibfd)); 3510 1.1 christos 3511 1.1 christos if (gnu_debuglink_filename != NULL) 3512 1.1 christos { 3513 1.1 christos if (! bfd_fill_in_gnu_debuglink_section 3514 1.1 christos (obfd, gnu_debuglink_section, gnu_debuglink_filename)) 3515 1.1 christos { 3516 1.1 christos bfd_nonfatal_message (NULL, obfd, NULL, 3517 1.10 christos _("cannot fill debug link section `%s'"), 3518 1.1 christos gnu_debuglink_filename); 3519 1.1 christos goto fail; 3520 1.1 christos } 3521 1.8 christos } 3522 1.1 christos 3523 1.1 christos if (gaps != NULL) 3524 1.1 christos { 3525 1.1 christos bfd_byte *buf; 3526 1.1 christos 3527 1.1 christos /* Fill in the gaps. */ 3528 1.1 christos if (max_gap > 8192) 3529 1.1 christos max_gap = 8192; 3530 1.1 christos buf = (bfd_byte *) xmalloc (max_gap); 3531 1.8 christos memset (buf, gap_fill, max_gap); 3532 1.1 christos 3533 1.1 christos for (i = 0; i < num_sec; i++) 3534 1.1 christos { 3535 1.1 christos if (gaps[i] != 0) 3536 1.1 christos { 3537 1.1 christos bfd_size_type left; 3538 1.1 christos file_ptr off; 3539 1.7 christos 3540 1.1 christos left = gaps[i]; 3541 1.1 christos off = bfd_section_size (osections[i]) - left; 3542 1.1 christos 3543 1.1 christos while (left > 0) 3544 1.1 christos { 3545 1.1 christos bfd_size_type now; 3546 1.1 christos 3547 1.1 christos if (left > 8192) 3548 1.1 christos now = 8192; 3549 1.1 christos else 3550 1.1 christos now = left; 3551 1.1 christos 3552 1.1 christos if (! bfd_set_section_contents (obfd, osections[i], buf, 3553 1.1 christos off, now)) 3554 1.3 christos { 3555 1.10 christos bfd_nonfatal_message (NULL, obfd, osections[i], NULL); 3556 1.1 christos free (buf); 3557 1.1 christos goto fail; 3558 1.1 christos } 3559 1.1 christos 3560 1.1 christos left -= now; 3561 1.1 christos off += now; 3562 1.1 christos } 3563 1.7 christos } 3564 1.3 christos } 3565 1.7 christos 3566 1.7 christos free (buf); 3567 1.10 christos free (gaps); 3568 1.10 christos gaps = NULL; 3569 1.1 christos free (osections); 3570 1.1 christos osections = NULL; 3571 1.1 christos } 3572 1.1 christos 3573 1.1 christos /* Allow the BFD backend to copy any private data it understands 3574 1.1 christos from the input BFD to the output BFD. This is done last to 3575 1.1 christos permit the routine to look at the filtered symbol table, which is 3576 1.1 christos important for the ECOFF code at least. */ 3577 1.1 christos if (! bfd_copy_private_bfd_data (ibfd, obfd)) 3578 1.1 christos { 3579 1.10 christos bfd_nonfatal_message (NULL, obfd, NULL, 3580 1.1 christos _("error copying private BFD data")); 3581 1.1 christos goto fail; 3582 1.1 christos } 3583 1.1 christos 3584 1.1 christos /* Switch to the alternate machine code. We have to do this at the 3585 1.1 christos very end, because we only initialize the header when we create 3586 1.1 christos the first section. */ 3587 1.1 christos if (use_alt_mach_code != 0) 3588 1.1 christos { 3589 1.1 christos if (! bfd_alt_mach_code (obfd, use_alt_mach_code)) 3590 1.1 christos { 3591 1.1 christos non_fatal (_("this target does not support %lu alternative machine codes"), 3592 1.1 christos use_alt_mach_code); 3593 1.1 christos if (bfd_get_flavour (obfd) == bfd_target_elf_flavour) 3594 1.1 christos { 3595 1.1 christos non_fatal (_("treating that number as an absolute e_machine value instead")); 3596 1.1 christos elf_elfheader (obfd)->e_machine = use_alt_mach_code; 3597 1.1 christos } 3598 1.1 christos else 3599 1.1 christos non_fatal (_("ignoring the alternative value")); 3600 1.1 christos } 3601 1.8 christos } 3602 1.10 christos 3603 1.10 christos return true; 3604 1.10 christos 3605 1.10 christos fail: 3606 1.10 christos while (merged_note_sections != NULL) 3607 1.10 christos { 3608 1.10 christos merged_note_section *next = merged_note_sections->next; 3609 1.10 christos free (merged_note_sections->contents); 3610 1.10 christos free (merged_note_sections); 3611 1.10 christos merged_note_sections = next; 3612 1.10 christos } 3613 1.10 christos free (gaps); 3614 1.1 christos free (osections); 3615 1.1 christos return false; 3616 1.1 christos } 3617 1.1 christos 3618 1.1 christos /* Read each archive element in turn from IBFD, copy the 3619 1.1 christos contents to temp file, and keep the temp file handle. 3620 1.1 christos If 'force_output_target' is TRUE then make sure that 3621 1.1 christos all elements in the new archive are of the type 3622 1.10 christos 'output_target'. */ 3623 1.1 christos 3624 1.8 christos static bool 3625 1.1 christos copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, 3626 1.1 christos bool force_output_target, 3627 1.1 christos const bfd_arch_info_type *input_arch) 3628 1.1 christos { 3629 1.1 christos struct name_list 3630 1.9 christos { 3631 1.1 christos struct name_list *next; 3632 1.9 christos char *name; 3633 1.1 christos bfd *obfd; 3634 1.1 christos } *list; 3635 1.9 christos bfd **ptr = &obfd->archive_head; 3636 1.9 christos bfd *this_element; 3637 1.10 christos char *dir = NULL; 3638 1.9 christos char *filename; 3639 1.9 christos bool ok = true; 3640 1.1 christos 3641 1.7 christos list = NULL; 3642 1.7 christos 3643 1.7 christos /* PR 24281: It is not clear what should happen when copying a thin archive. 3644 1.7 christos One part is straight forward - if the output archive is in a different 3645 1.7 christos directory from the input archive then any relative paths in the library 3646 1.7 christos should be adjusted to the new location. But if any transformation 3647 1.7 christos options are active (eg strip, rename, add, etc) then the implication is 3648 1.7 christos that these should be applied to the files pointed to by the archive. 3649 1.7 christos But since objcopy is not destructive, this means that new files must be 3650 1.7 christos created, and there is no guidance for the names of the new files. (Plus 3651 1.7 christos this conflicts with one of the goals of thin libraries - only taking up 3652 1.7 christos a minimal amount of space in the file system). 3653 1.7 christos 3654 1.7 christos So for now we fail if an attempt is made to copy such libraries. */ 3655 1.10 christos if (ibfd->is_thin_archive) 3656 1.7 christos { 3657 1.7 christos ok = false; 3658 1.7 christos bfd_set_error (bfd_error_invalid_operation); 3659 1.9 christos bfd_nonfatal_message (NULL, ibfd, NULL, 3660 1.7 christos _("sorry: copying thin archives is not currently supported")); 3661 1.7 christos goto cleanup_and_exit; 3662 1.1 christos } 3663 1.1 christos 3664 1.1 christos /* Make a temp directory to hold the contents. */ 3665 1.5 christos dir = make_tempdir (bfd_get_filename (obfd)); 3666 1.1 christos if (dir == NULL) 3667 1.1 christos fatal (_("cannot create tempdir for archive copying (error: %s)"), 3668 1.1 christos strerror (errno)); 3669 1.8 christos 3670 1.1 christos if (strip_symbols == STRIP_ALL) 3671 1.1 christos obfd->has_armap = false; 3672 1.1 christos else 3673 1.1 christos obfd->has_armap = ibfd->has_armap; 3674 1.1 christos obfd->is_thin_archive = ibfd->is_thin_archive; 3675 1.1 christos 3676 1.1 christos if (deterministic) 3677 1.1 christos obfd->flags |= BFD_DETERMINISTIC_OUTPUT; 3678 1.1 christos 3679 1.1 christos this_element = bfd_openr_next_archived_file (ibfd, NULL); 3680 1.1 christos 3681 1.10 christos if (!bfd_set_format (obfd, bfd_get_format (ibfd))) 3682 1.1 christos { 3683 1.3 christos ok = false; 3684 1.1 christos bfd_nonfatal_message (NULL, obfd, NULL, NULL); 3685 1.1 christos goto cleanup_and_exit; 3686 1.10 christos } 3687 1.1 christos 3688 1.1 christos while (ok && this_element != NULL) 3689 1.9 christos { 3690 1.1 christos char *output_name; 3691 1.1 christos bfd *output_element; 3692 1.8 christos struct stat buf; 3693 1.10 christos int stat_status = 0; 3694 1.1 christos bool ok_object; 3695 1.10 christos const char *element_name; 3696 1.3 christos 3697 1.3 christos element_name = bfd_get_filename (this_element); 3698 1.10 christos /* PR binutils/17533: Do not allow directory traversal 3699 1.3 christos outside of the current directory tree by archive members. */ 3700 1.10 christos if (! is_valid_archive_path (element_name)) 3701 1.10 christos { 3702 1.10 christos non_fatal (_("warning: illegal pathname found in archive member: %s"), 3703 1.10 christos element_name); 3704 1.10 christos /* PR binutils/31250: But there tools which create archives 3705 1.10 christos containing absolute paths, so instead of failing here, try to 3706 1.10 christos create a suitable alternative pathname. */ 3707 1.10 christos element_name = lbasename (element_name); 3708 1.3 christos non_fatal (_("warning: using the basename of the member instead: %s"), 3709 1.3 christos element_name); 3710 1.1 christos } 3711 1.10 christos 3712 1.1 christos /* Create an output file for this member. */ 3713 1.1 christos output_name = concat (dir, "/", element_name, (char *) 0); 3714 1.1 christos 3715 1.1 christos /* If the file already exists, make another temp dir. */ 3716 1.7 christos if (stat (output_name, &buf) >= 0) 3717 1.7 christos { 3718 1.7 christos char * tmpdir = make_tempdir (output_name); 3719 1.7 christos 3720 1.3 christos free (output_name); 3721 1.3 christos if (tmpdir == NULL) 3722 1.3 christos { 3723 1.9 christos non_fatal (_("cannot create tempdir for archive copying (error: %s)"), 3724 1.10 christos strerror (errno)); 3725 1.3 christos bfd_close (this_element); 3726 1.3 christos ok = false; 3727 1.1 christos goto cleanup_and_exit; 3728 1.9 christos } 3729 1.7 christos 3730 1.1 christos struct name_list *l = xmalloc (sizeof (*l)); 3731 1.1 christos l->name = tmpdir; 3732 1.1 christos l->next = list; 3733 1.10 christos l->obfd = NULL; 3734 1.1 christos list = l; 3735 1.1 christos output_name = concat (tmpdir, "/", element_name, (char *) 0); 3736 1.1 christos } 3737 1.1 christos 3738 1.8 christos if (preserve_dates) 3739 1.1 christos { 3740 1.1 christos memset (&buf, 0, sizeof (buf)); 3741 1.1 christos stat_status = bfd_stat_arch_elt (this_element, &buf); 3742 1.10 christos 3743 1.1 christos if (stat_status != 0) 3744 1.1 christos non_fatal (_("internal stat error on %s"), element_name); 3745 1.9 christos } 3746 1.1 christos 3747 1.1 christos struct name_list *l = xmalloc (sizeof (*l)); 3748 1.1 christos l->name = output_name; 3749 1.1 christos l->next = list; 3750 1.1 christos l->obfd = NULL; 3751 1.10 christos list = l; 3752 1.10 christos 3753 1.10 christos #if BFD_SUPPORTS_PLUGINS 3754 1.10 christos /* Ignore plugin target if all LTO sections should be removed. */ 3755 1.10 christos ok_object = bfd_check_format_lto (this_element, bfd_object, 3756 1.1 christos lto_sections_removed); 3757 1.10 christos #else 3758 1.1 christos ok_object = bfd_check_format (this_element, bfd_object); 3759 1.1 christos #endif 3760 1.1 christos 3761 1.1 christos /* PR binutils/3110: Cope with archives 3762 1.9 christos containing multiple target types. */ 3763 1.1 christos if (force_output_target || !ok_object) 3764 1.9 christos output_element = bfd_openw (output_name, output_target); 3765 1.1 christos else 3766 1.9 christos output_element = bfd_openw (output_name, bfd_get_target (this_element)); 3767 1.1 christos 3768 1.1 christos if (output_element == NULL) 3769 1.9 christos { 3770 1.10 christos bfd_nonfatal_message (output_name, NULL, NULL, NULL); 3771 1.3 christos bfd_close (this_element); 3772 1.1 christos ok = false; 3773 1.1 christos goto cleanup_and_exit; 3774 1.10 christos } 3775 1.10 christos 3776 1.10 christos #if BFD_SUPPORTS_PLUGINS 3777 1.10 christos /* Copy LTO IR file as unknown object. */ 3778 1.10 christos if (bfd_plugin_target_p (this_element->xvec)) 3779 1.10 christos ok_object = false; 3780 1.1 christos else 3781 1.1 christos #endif 3782 1.10 christos if (ok_object) 3783 1.1 christos { 3784 1.10 christos ok = copy_object (this_element, output_element, input_arch); 3785 1.1 christos 3786 1.8 christos if (!ok && bfd_get_arch (this_element) == bfd_arch_unknown) 3787 1.1 christos /* Try again as an unknown object file. */ 3788 1.1 christos ok_object = false; 3789 1.1 christos } 3790 1.10 christos 3791 1.9 christos if (!ok_object) 3792 1.10 christos ok = copy_unknown_object (this_element, output_element); 3793 1.9 christos 3794 1.1 christos if (!(ok_object && ok 3795 1.9 christos ? bfd_close : bfd_close_all_done) (output_element)) 3796 1.9 christos { 3797 1.10 christos bfd_nonfatal_message (output_name, NULL, NULL, NULL); 3798 1.1 christos /* Error in new object file. Don't change archive. */ 3799 1.1 christos ok = false; 3800 1.10 christos } 3801 1.1 christos 3802 1.1 christos if (!ok) 3803 1.9 christos { 3804 1.9 christos unlink (output_name); 3805 1.9 christos free (output_name); 3806 1.1 christos list->name = NULL; 3807 1.1 christos bfd_close (this_element); 3808 1.1 christos } 3809 1.1 christos else 3810 1.1 christos { 3811 1.1 christos if (preserve_dates && stat_status == 0) 3812 1.9 christos set_times (output_name, &buf); 3813 1.9 christos 3814 1.1 christos /* Open the newly created output file and attach to our list. */ 3815 1.9 christos output_element = bfd_openr (output_name, output_target); 3816 1.1 christos 3817 1.9 christos list->obfd = output_element; 3818 1.9 christos 3819 1.1 christos *ptr = output_element; 3820 1.9 christos ptr = &output_element->archive_next; 3821 1.1 christos 3822 1.1 christos bfd *last_element = this_element; 3823 1.1 christos this_element = bfd_openr_next_archived_file (ibfd, last_element); 3824 1.1 christos bfd_close (last_element); 3825 1.1 christos } 3826 1.1 christos } 3827 1.9 christos *ptr = NULL; 3828 1.9 christos 3829 1.10 christos cleanup_and_exit: 3830 1.1 christos filename = xstrdup (bfd_get_filename (obfd)); 3831 1.10 christos if (!(ok ? bfd_close : bfd_close_all_done) (obfd)) 3832 1.9 christos { 3833 1.10 christos if (ok) 3834 1.1 christos bfd_nonfatal_message (filename, NULL, NULL, NULL); 3835 1.9 christos ok = false; 3836 1.1 christos } 3837 1.9 christos free (filename); 3838 1.1 christos 3839 1.1 christos filename = xstrdup (bfd_get_filename (ibfd)); 3840 1.10 christos if (!bfd_close (ibfd)) 3841 1.9 christos { 3842 1.10 christos if (ok) 3843 1.1 christos bfd_nonfatal_message (filename, NULL, NULL, NULL); 3844 1.9 christos ok = false; 3845 1.1 christos } 3846 1.1 christos free (filename); 3847 1.9 christos 3848 1.9 christos /* Delete all the files that we opened. */ 3849 1.9 christos struct name_list *l, *next; 3850 1.9 christos for (l = list; l != NULL; l = next) 3851 1.9 christos { 3852 1.9 christos if (l->name != NULL) 3853 1.9 christos { 3854 1.9 christos if (l->obfd == NULL) 3855 1.10 christos rmdir (l->name); 3856 1.9 christos else 3857 1.9 christos unlink (l->name); 3858 1.9 christos free (l->name); 3859 1.9 christos } 3860 1.9 christos next = l->next; 3861 1.3 christos free (l); 3862 1.9 christos } 3863 1.1 christos 3864 1.9 christos if (dir) 3865 1.9 christos { 3866 1.1 christos rmdir (dir); 3867 1.10 christos free (dir); 3868 1.1 christos } 3869 1.1 christos return ok; 3870 1.1 christos } 3871 1.1 christos 3872 1.1 christos /* The top-level control. */ 3873 1.8 christos 3874 1.8 christos static void 3875 1.8 christos copy_file (const char *input_filename, const char *output_filename, int ofd, 3876 1.1 christos struct stat *in_stat, const char *input_target, 3877 1.1 christos const char *output_target, const bfd_arch_info_type *input_arch) 3878 1.1 christos { 3879 1.1 christos bfd *ibfd; 3880 1.1 christos char **obj_matching; 3881 1.10 christos char **core_matching; 3882 1.1 christos off_t size = get_file_size (input_filename); 3883 1.1 christos const char *target = input_target; 3884 1.1 christos 3885 1.1 christos if (size < 1) 3886 1.1 christos { 3887 1.1 christos if (size == 0) 3888 1.1 christos non_fatal (_("error: the input file '%s' is empty"), 3889 1.1 christos input_filename); 3890 1.1 christos status = 1; 3891 1.1 christos return; 3892 1.10 christos } 3893 1.10 christos 3894 1.10 christos #if BFD_SUPPORTS_PLUGINS 3895 1.10 christos /* Enable LTO plugin in strip. */ 3896 1.10 christos if (is_strip && !target) 3897 1.10 christos target = "plugin"; 3898 1.1 christos #endif 3899 1.1 christos 3900 1.10 christos /* To allow us to do "strip *" without dying on the first 3901 1.8 christos non-object file, failures are nonfatal. */ 3902 1.1 christos ibfd = bfd_openr (input_filename, target); 3903 1.1 christos if (ibfd == NULL || bfd_stat (ibfd, in_stat) != 0) 3904 1.9 christos { 3905 1.9 christos bfd_nonfatal_message (input_filename, NULL, NULL, NULL); 3906 1.1 christos if (ibfd != NULL) 3907 1.1 christos bfd_close (ibfd); 3908 1.1 christos status = 1; 3909 1.1 christos return; 3910 1.1 christos } 3911 1.1 christos 3912 1.9 christos switch (do_debug_sections) 3913 1.9 christos { 3914 1.9 christos case compress_gnu_zlib: 3915 1.1 christos ibfd->flags |= BFD_COMPRESS; 3916 1.3 christos break; 3917 1.9 christos case compress: 3918 1.9 christos case compress_zlib: 3919 1.9 christos /* The above two cases ought to just set BFD_COMPRESS for non-ELF 3920 1.9 christos but we can't tell whether a file is ELF or not until after 3921 1.3 christos bfd_check_format_matches. FIXME maybe: decide compression 3922 1.9 christos style in BFD after bfd_check_format_matches. */ 3923 1.9 christos case compress_gabi_zlib: 3924 1.9 christos ibfd->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI; 3925 1.9 christos break; 3926 1.9 christos case compress_zstd: 3927 1.9 christos ibfd->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD; 3928 1.9 christos #ifndef HAVE_ZSTD 3929 1.9 christos fatal (_ ("--compress-debug-sections=zstd: binutils is not built with " 3930 1.1 christos "zstd support")); 3931 1.1 christos #endif 3932 1.1 christos break; 3933 1.1 christos case decompress: 3934 1.1 christos ibfd->flags |= BFD_DECOMPRESS; 3935 1.1 christos break; 3936 1.1 christos default: 3937 1.1 christos break; 3938 1.5 christos } 3939 1.5 christos 3940 1.5 christos switch (do_elf_stt_common) 3941 1.5 christos { 3942 1.5 christos case elf_stt_common: 3943 1.5 christos ibfd->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON; 3944 1.5 christos break; 3945 1.5 christos break; 3946 1.5 christos case no_elf_stt_common: 3947 1.5 christos ibfd->flags |= BFD_CONVERT_ELF_COMMON; 3948 1.5 christos break; 3949 1.5 christos default: 3950 1.5 christos break; 3951 1.1 christos } 3952 1.1 christos 3953 1.8 christos if (bfd_check_format (ibfd, bfd_archive)) 3954 1.1 christos { 3955 1.1 christos bool force_output_target; 3956 1.1 christos bfd *obfd; 3957 1.5 christos 3958 1.1 christos /* bfd_get_target does not return the correct value until 3959 1.1 christos bfd_check_format succeeds. */ 3960 1.1 christos if (output_target == NULL) 3961 1.8 christos { 3962 1.1 christos output_target = bfd_get_target (ibfd); 3963 1.1 christos force_output_target = false; 3964 1.8 christos } 3965 1.8 christos else 3966 1.8 christos force_output_target = true; 3967 1.8 christos 3968 1.8 christos if (ofd >= 0) 3969 1.8 christos obfd = bfd_fdopenw (output_filename, output_target, ofd); 3970 1.1 christos else 3971 1.1 christos obfd = bfd_openw (output_filename, output_target); 3972 1.1 christos 3973 1.9 christos if (obfd == NULL) 3974 1.9 christos { 3975 1.1 christos if (ofd >= 0) 3976 1.9 christos close (ofd); 3977 1.1 christos bfd_nonfatal_message (output_filename, NULL, NULL, NULL); 3978 1.1 christos bfd_close (ibfd); 3979 1.1 christos status = 1; 3980 1.7 christos return; 3981 1.7 christos } 3982 1.7 christos 3983 1.7 christos if (gnu_debuglink_filename != NULL) 3984 1.7 christos { 3985 1.7 christos non_fatal (_("--add-gnu-debuglink ignored for archive %s"), 3986 1.7 christos bfd_get_filename (ibfd)); 3987 1.7 christos gnu_debuglink_filename = NULL; 3988 1.10 christos } 3989 1.10 christos 3990 1.10 christos if (!copy_archive (ibfd, obfd, output_target, force_output_target, 3991 1.1 christos input_arch)) 3992 1.10 christos status = 1; 3993 1.10 christos } 3994 1.10 christos else if ( 3995 1.10 christos #if BFD_SUPPORTS_PLUGINS 3996 1.10 christos /* Ignore plugin target first if all LTO sections should be 3997 1.10 christos removed. Try with plugin target next if ignoring plugin 3998 1.10 christos target fails to match the format. */ 3999 1.10 christos bfd_check_format_matches_lto (ibfd, bfd_object, &obj_matching, 4000 1.10 christos lto_sections_removed) 4001 1.10 christos || (lto_sections_removed 4002 1.10 christos && bfd_check_format_matches_lto (ibfd, bfd_object, 4003 1.10 christos &obj_matching, false)) 4004 1.10 christos #else 4005 1.10 christos bfd_check_format_matches_lto (ibfd, bfd_object, &obj_matching, 4006 1.10 christos false) 4007 1.1 christos #endif 4008 1.1 christos ) 4009 1.1 christos { 4010 1.1 christos bfd *obfd; 4011 1.1 christos do_copy: 4012 1.5 christos 4013 1.1 christos /* bfd_get_target does not return the correct value until 4014 1.1 christos bfd_check_format succeeds. */ 4015 1.1 christos if (output_target == NULL) 4016 1.8 christos output_target = bfd_get_target (ibfd); 4017 1.8 christos 4018 1.8 christos if (ofd >= 0) 4019 1.8 christos obfd = bfd_fdopenw (output_filename, output_target, ofd); 4020 1.8 christos else 4021 1.1 christos obfd = bfd_openw (output_filename, output_target); 4022 1.1 christos 4023 1.9 christos if (obfd == NULL) 4024 1.9 christos { 4025 1.1 christos if (ofd >= 0) 4026 1.9 christos close (ofd); 4027 1.1 christos bfd_nonfatal_message (output_filename, NULL, NULL, NULL); 4028 1.1 christos bfd_close (ibfd); 4029 1.1 christos status = 1; 4030 1.8 christos return; 4031 1.10 christos } 4032 1.10 christos 4033 1.10 christos #if BFD_SUPPORTS_PLUGINS 4034 1.10 christos if (bfd_plugin_target_p (ibfd->xvec)) 4035 1.10 christos { 4036 1.10 christos /* Copy LTO IR file as unknown file. */ 4037 1.10 christos if (!copy_unknown_file (ibfd, obfd, in_stat->st_size, 4038 1.10 christos in_stat->st_mode)) 4039 1.10 christos status = 1; 4040 1.10 christos else if (!bfd_close_all_done (obfd)) 4041 1.10 christos status = 1; 4042 1.10 christos } 4043 1.10 christos else 4044 1.10 christos #endif 4045 1.10 christos { 4046 1.1 christos if (! copy_object (ibfd, obfd, input_arch)) 4047 1.10 christos status = 1; 4048 1.10 christos 4049 1.10 christos /* PR 17512: file: 0f15796a. 4050 1.10 christos If the file could not be copied it may not be in a writeable 4051 1.10 christos state. So use bfd_close_all_done to avoid the possibility of 4052 1.10 christos writing uninitialised data into the file. */ 4053 1.10 christos if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd))) 4054 1.10 christos { 4055 1.10 christos status = 1; 4056 1.1 christos bfd_nonfatal_message (output_filename, NULL, NULL, NULL); 4057 1.1 christos } 4058 1.1 christos } 4059 1.1 christos 4060 1.1 christos if (!bfd_close (ibfd)) 4061 1.1 christos { 4062 1.1 christos status = 1; 4063 1.1 christos bfd_nonfatal_message (input_filename, NULL, NULL, NULL); 4064 1.1 christos } 4065 1.1 christos } 4066 1.1 christos else 4067 1.1 christos { 4068 1.1 christos bfd_error_type obj_error = bfd_get_error (); 4069 1.1 christos bfd_error_type core_error; 4070 1.1 christos 4071 1.1 christos if (bfd_check_format_matches (ibfd, bfd_core, &core_matching)) 4072 1.1 christos { 4073 1.1 christos /* This probably can't happen.. */ 4074 1.1 christos if (obj_error == bfd_error_file_ambiguously_recognized) 4075 1.1 christos free (obj_matching); 4076 1.1 christos goto do_copy; 4077 1.1 christos } 4078 1.1 christos 4079 1.1 christos core_error = bfd_get_error (); 4080 1.1 christos /* Report the object error in preference to the core error. */ 4081 1.1 christos if (obj_error != core_error) 4082 1.1 christos bfd_set_error (obj_error); 4083 1.1 christos 4084 1.1 christos bfd_nonfatal_message (input_filename, NULL, NULL, NULL); 4085 1.8 christos 4086 1.1 christos if (obj_error == bfd_error_file_ambiguously_recognized) 4087 1.8 christos list_matching_formats (obj_matching); 4088 1.1 christos if (core_error == bfd_error_file_ambiguously_recognized) 4089 1.9 christos list_matching_formats (core_matching); 4090 1.1 christos 4091 1.1 christos bfd_close (ibfd); 4092 1.1 christos status = 1; 4093 1.1 christos } 4094 1.1 christos } 4095 1.1 christos 4096 1.1 christos /* Add a name to the section renaming list. */ 4097 1.1 christos 4098 1.1 christos static void 4099 1.1 christos add_section_rename (const char * old_name, const char * new_name, 4100 1.1 christos flagword flags) 4101 1.1 christos { 4102 1.1 christos section_rename * srename; 4103 1.1 christos 4104 1.1 christos /* Check for conflicts first. */ 4105 1.1 christos for (srename = section_rename_list; srename != NULL; srename = srename->next) 4106 1.1 christos if (strcmp (srename->old_name, old_name) == 0) 4107 1.1 christos { 4108 1.1 christos /* Silently ignore duplicate definitions. */ 4109 1.1 christos if (strcmp (srename->new_name, new_name) == 0 4110 1.1 christos && srename->flags == flags) 4111 1.1 christos return; 4112 1.1 christos 4113 1.1 christos fatal (_("Multiple renames of section %s"), old_name); 4114 1.1 christos } 4115 1.1 christos 4116 1.1 christos srename = (section_rename *) xmalloc (sizeof (* srename)); 4117 1.1 christos 4118 1.1 christos srename->old_name = old_name; 4119 1.1 christos srename->new_name = new_name; 4120 1.1 christos srename->flags = flags; 4121 1.1 christos srename->next = section_rename_list; 4122 1.1 christos 4123 1.1 christos section_rename_list = srename; 4124 1.1 christos } 4125 1.5 christos 4126 1.5 christos /* Check the section rename list for a new name of the input section 4127 1.1 christos called OLD_NAME. Returns the new name if one is found and sets 4128 1.1 christos RETURNED_FLAGS if non-NULL to the flags to be used for this section. */ 4129 1.5 christos 4130 1.1 christos static const char * 4131 1.5 christos find_section_rename (const char *old_name, flagword *returned_flags) 4132 1.1 christos { 4133 1.1 christos const section_rename *srename; 4134 1.1 christos 4135 1.1 christos for (srename = section_rename_list; srename != NULL; srename = srename->next) 4136 1.5 christos if (strcmp (srename->old_name, old_name) == 0) 4137 1.5 christos { 4138 1.1 christos if (returned_flags != NULL && srename->flags != (flagword) -1) 4139 1.1 christos *returned_flags = srename->flags; 4140 1.1 christos 4141 1.1 christos return srename->new_name; 4142 1.1 christos } 4143 1.1 christos 4144 1.1 christos return old_name; 4145 1.1 christos } 4146 1.1 christos 4147 1.1 christos /* Once each of the sections is copied, we may still need to do some 4148 1.10 christos finalization work for private section headers. Do that here. */ 4149 1.1 christos 4150 1.1 christos static bool 4151 1.1 christos setup_bfd_headers (bfd *ibfd, bfd *obfd) 4152 1.1 christos { 4153 1.1 christos /* Allow the BFD backend to copy any private data it understands 4154 1.1 christos from the input section to the output section. */ 4155 1.1 christos if (! bfd_copy_private_header_data (ibfd, obfd)) 4156 1.1 christos { 4157 1.10 christos bfd_nonfatal_message (NULL, ibfd, NULL, 4158 1.1 christos _("error in private header data")); 4159 1.1 christos return false; 4160 1.1 christos } 4161 1.10 christos 4162 1.10 christos /* All went well. */ 4163 1.10 christos return true; 4164 1.10 christos } 4165 1.10 christos 4166 1.10 christos static inline signed int 4167 1.10 christos power_of_two (bfd_vma val) 4168 1.10 christos { 4169 1.10 christos signed int result = 0; 4170 1.10 christos 4171 1.10 christos if (val == 0) 4172 1.10 christos return 0; 4173 1.10 christos 4174 1.10 christos while ((val & 1) == 0) 4175 1.10 christos { 4176 1.10 christos val >>= 1; 4177 1.10 christos ++result; 4178 1.10 christos } 4179 1.10 christos 4180 1.10 christos if (val != 1) 4181 1.10 christos /* Number has more than one 1, i.e. wasn't a power of 2. */ 4182 1.10 christos return -1; 4183 1.1 christos 4184 1.1 christos return result; 4185 1.1 christos } 4186 1.1 christos 4187 1.1 christos /* Create a section in OBFD with the same 4188 1.10 christos name and attributes as ISECTION in IBFD. */ 4189 1.10 christos 4190 1.1 christos static bool 4191 1.1 christos setup_section (bfd *ibfd, sec_ptr isection, bfd *obfd) 4192 1.1 christos { 4193 1.1 christos struct section_list *p; 4194 1.1 christos sec_ptr osection; 4195 1.1 christos bfd_size_type size; 4196 1.1 christos bfd_vma vma; 4197 1.9 christos bfd_vma lma; 4198 1.1 christos flagword flags; 4199 1.8 christos const char *err = NULL; 4200 1.1 christos const char * name; 4201 1.8 christos const char * new_name; 4202 1.7 christos char *prefix = NULL; 4203 1.1 christos bool make_nobits; 4204 1.1 christos unsigned int alignment; 4205 1.10 christos 4206 1.1 christos if (is_strip_section (ibfd, isection)) 4207 1.1 christos return true; 4208 1.7 christos 4209 1.7 christos /* Get the, possibly new, name of the output section. */ 4210 1.7 christos name = bfd_section_name (isection); 4211 1.7 christos flags = bfd_section_flags (isection); 4212 1.7 christos if (bfd_get_flavour (ibfd) != bfd_get_flavour (obfd)) 4213 1.7 christos { 4214 1.7 christos flags &= bfd_applicable_section_flags (ibfd); 4215 1.8 christos flags &= bfd_applicable_section_flags (obfd); 4216 1.8 christos } 4217 1.8 christos new_name = find_section_rename (name, &flags); 4218 1.8 christos if (new_name != name) 4219 1.8 christos { 4220 1.8 christos name = new_name; 4221 1.1 christos flags = check_new_section_flags (flags, obfd, name); 4222 1.1 christos } 4223 1.7 christos 4224 1.7 christos /* Prefix sections. */ 4225 1.1 christos if (prefix_alloc_sections_string 4226 1.1 christos && (bfd_section_flags (isection) & SEC_ALLOC) != 0) 4227 1.1 christos prefix = prefix_alloc_sections_string; 4228 1.1 christos else if (prefix_sections_string) 4229 1.1 christos prefix = prefix_sections_string; 4230 1.1 christos 4231 1.1 christos if (prefix) 4232 1.1 christos { 4233 1.1 christos char *n; 4234 1.1 christos 4235 1.1 christos n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1); 4236 1.1 christos strcpy (n, prefix); 4237 1.1 christos strcat (n, name); 4238 1.1 christos name = n; 4239 1.8 christos } 4240 1.3 christos 4241 1.8 christos make_nobits = false; 4242 1.3 christos 4243 1.3 christos p = find_section_list (bfd_section_name (isection), false, 4244 1.8 christos SECTION_CONTEXT_SET_FLAGS); 4245 1.8 christos if (p != NULL) 4246 1.8 christos { 4247 1.8 christos flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC)); 4248 1.9 christos flags = check_new_section_flags (flags, obfd, bfd_section_name (isection)); 4249 1.1 christos } 4250 1.9 christos else 4251 1.9 christos { 4252 1.9 christos flagword clr = 0; 4253 1.9 christos 4254 1.9 christos /* For --extract-symbols where section sizes are zeroed, clear 4255 1.9 christos SEC_LOAD to indicate to coff_compute_section_file_positions that 4256 1.9 christos section sizes should not be adjusted for ALIGN_SECTIONS_IN_FILE. 4257 1.9 christos We don't want to clear SEC_HAS_CONTENTS as that will result 4258 1.9 christos in symbols being classified as 'B' by nm. */ 4259 1.9 christos if (extract_symbol) 4260 1.9 christos clr = SEC_LOAD; 4261 1.9 christos /* If only keeping debug sections then we'll be keeping section 4262 1.9 christos sizes in headers but making the sections have no contents. */ 4263 1.9 christos else if (strip_symbols == STRIP_NONDEBUG 4264 1.9 christos && (flags & (SEC_ALLOC | SEC_GROUP)) != 0 4265 1.9 christos && !is_nondebug_keep_contents_section (ibfd, isection)) 4266 1.9 christos clr = SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP; 4267 1.9 christos 4268 1.9 christos if (clr && bfd_get_flavour (obfd) == bfd_target_elf_flavour) 4269 1.9 christos { 4270 1.9 christos /* PR 29532: Copy group sections intact as otherwise we end up with 4271 1.9 christos empty groups. This prevents separate debug info files from 4272 1.9 christos being used with GDB, if they were based upon files that 4273 1.9 christos originally contained groups. */ 4274 1.9 christos if (flags & SEC_GROUP) 4275 1.9 christos clr = SEC_LOAD; 4276 1.1 christos if ((clr & SEC_HAS_CONTENTS) != 0) 4277 1.1 christos make_nobits = true; 4278 1.1 christos 4279 1.1 christos /* Twiddle the input section flags so that it seems to 4280 1.1 christos elf.c:copy_private_bfd_data that section flags have not 4281 1.9 christos changed between input and output sections. This hack 4282 1.1 christos prevents wholesale rewriting of the program headers. */ 4283 1.9 christos isection->flags &= ~clr; 4284 1.9 christos } 4285 1.9 christos flags &= ~clr; 4286 1.9 christos } 4287 1.9 christos 4288 1.9 christos if (!bfd_convert_section_setup (ibfd, isection, obfd, &name, &size)) 4289 1.9 christos { 4290 1.9 christos osection = NULL; 4291 1.1 christos err = _("failed to create output section"); 4292 1.1 christos goto loser; 4293 1.1 christos } 4294 1.1 christos 4295 1.1 christos osection = bfd_make_section_anyway_with_flags (obfd, name, flags); 4296 1.1 christos 4297 1.1 christos if (osection == NULL) 4298 1.1 christos { 4299 1.1 christos err = _("failed to create output section"); 4300 1.1 christos goto loser; 4301 1.1 christos } 4302 1.1 christos 4303 1.1 christos if (copy_byte >= 0) 4304 1.1 christos size = (size + interleave - 1) / interleave * copy_width; 4305 1.7 christos else if (extract_symbol) 4306 1.9 christos size = 0; 4307 1.1 christos if (!bfd_set_section_size (osection, size)) 4308 1.10 christos err = _("failed to set size"); 4309 1.10 christos 4310 1.7 christos bool vma_set_by_user = false; 4311 1.8 christos 4312 1.3 christos vma = bfd_section_vma (isection); 4313 1.3 christos p = find_section_list (bfd_section_name (isection), false, 4314 1.3 christos SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA); 4315 1.3 christos if (p != NULL) 4316 1.3 christos { 4317 1.3 christos if (p->context & SECTION_CONTEXT_SET_VMA) 4318 1.3 christos vma = p->vma_val; 4319 1.10 christos else 4320 1.3 christos vma += p->vma_val; 4321 1.1 christos vma_set_by_user = true; 4322 1.1 christos } 4323 1.1 christos else 4324 1.7 christos vma += change_section_address; 4325 1.9 christos 4326 1.1 christos if (!bfd_set_section_vma (osection, vma)) 4327 1.10 christos err = _("failed to set vma"); 4328 1.10 christos 4329 1.1 christos bool lma_set_by_user = false; 4330 1.8 christos 4331 1.3 christos lma = isection->lma; 4332 1.3 christos p = find_section_list (bfd_section_name (isection), false, 4333 1.1 christos SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA); 4334 1.3 christos if (p != NULL) 4335 1.1 christos { 4336 1.3 christos if (p->context & SECTION_CONTEXT_ALTER_LMA) 4337 1.1 christos lma += p->lma_val; 4338 1.10 christos else 4339 1.1 christos lma = p->lma_val; 4340 1.1 christos lma_set_by_user = true; 4341 1.1 christos } 4342 1.1 christos else 4343 1.1 christos lma += change_section_address; 4344 1.1 christos 4345 1.8 christos osection->lma = lma; 4346 1.7 christos 4347 1.7 christos p = find_section_list (bfd_section_name (isection), false, 4348 1.7 christos SECTION_CONTEXT_SET_ALIGNMENT); 4349 1.10 christos if (p != NULL) 4350 1.10 christos alignment = p->alignment; 4351 1.10 christos else if (pe_section_alignment != (bfd_vma) -1 4352 1.10 christos && bfd_get_flavour (obfd) == bfd_target_coff_flavour 4353 1.7 christos && bfd_pei_p (obfd)) 4354 1.7 christos alignment = power_of_two (pe_section_alignment); 4355 1.7 christos else 4356 1.1 christos alignment = bfd_section_alignment (isection); 4357 1.1 christos 4358 1.7 christos /* FIXME: This is probably not enough. If we change the LMA we 4359 1.9 christos may have to recompute the header for the file as well. */ 4360 1.1 christos if (!bfd_set_section_alignment (osection, alignment)) 4361 1.10 christos err = _("failed to set alignment"); 4362 1.10 christos 4363 1.10 christos /* If the output section's VMA is not aligned 4364 1.10 christos and the alignment has changed 4365 1.10 christos and the VMA was not set by the user 4366 1.10 christos and the section does not have relocations associated with it 4367 1.10 christos then warn the user. */ 4368 1.10 christos if (osection->vma != 0 4369 1.10 christos && (alignment >= sizeof (bfd_vma) * CHAR_BIT 4370 1.10 christos || (osection->vma & (((bfd_vma) 1 << alignment) - 1)) != 0) 4371 1.10 christos && alignment != bfd_section_alignment (isection) 4372 1.10 christos && change_section_address == 0 4373 1.10 christos && ! vma_set_by_user 4374 1.10 christos && bfd_get_reloc_upper_bound (ibfd, isection) < 1) 4375 1.10 christos { 4376 1.10 christos non_fatal (_("output section %s's alignment does not match its VMA"), name); 4377 1.10 christos } 4378 1.10 christos 4379 1.10 christos /* Similar check for a non-aligned LMA. 4380 1.10 christos FIXME: Since this is only an LMA, maybe it does not matter if 4381 1.10 christos it is not aligned ? */ 4382 1.10 christos if (osection->lma != 0 4383 1.10 christos && (alignment >= sizeof (bfd_vma) * CHAR_BIT 4384 1.10 christos || (osection->lma & (((bfd_vma) 1 << alignment) - 1)) != 0) 4385 1.10 christos && alignment != bfd_section_alignment (isection) 4386 1.10 christos && change_section_address == 0 4387 1.10 christos && ! lma_set_by_user 4388 1.10 christos && bfd_get_reloc_upper_bound (ibfd, isection) < 1) 4389 1.10 christos { 4390 1.10 christos non_fatal (_("output section %s's alignment does not match its LMA"), name); 4391 1.1 christos } 4392 1.1 christos 4393 1.1 christos /* Copy merge entity size. */ 4394 1.3 christos osection->entsize = isection->entsize; 4395 1.3 christos 4396 1.3 christos /* Copy compress status. */ 4397 1.1 christos osection->compress_status = isection->compress_status; 4398 1.1 christos 4399 1.1 christos /* This used to be mangle_section; we do here to avoid using 4400 1.1 christos bfd_get_section_by_name since some formats allow multiple 4401 1.1 christos sections with the same name. */ 4402 1.1 christos isection->output_section = osection; 4403 1.1 christos isection->output_offset = 0; 4404 1.1 christos 4405 1.10 christos if ((isection->flags & SEC_GROUP) != 0) 4406 1.1 christos { 4407 1.1 christos asymbol *gsym = bfd_group_signature (isection, isympp); 4408 1.1 christos 4409 1.1 christos if (gsym != NULL) 4410 1.7 christos { 4411 1.1 christos gsym->flags |= BSF_KEEP; 4412 1.1 christos if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour) 4413 1.1 christos elf_group_id (isection) = gsym; 4414 1.1 christos } 4415 1.1 christos } 4416 1.1 christos 4417 1.10 christos /* Allow the BFD backend to copy any private data it understands 4418 1.9 christos from the input section to the output section. */ 4419 1.1 christos if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection, NULL)) 4420 1.8 christos err = _("failed to copy private data"); 4421 1.8 christos 4422 1.8 christos if (make_nobits) 4423 1.9 christos elf_section_type (osection) = SHT_NOBITS; 4424 1.10 christos 4425 1.1 christos if (!err) 4426 1.5 christos return true; 4427 1.1 christos 4428 1.10 christos loser: 4429 1.1 christos bfd_nonfatal_message (NULL, obfd, osection, err); 4430 1.1 christos return false; 4431 1.1 christos } 4432 1.1 christos 4433 1.8 christos /* Return TRUE if input section ISECTION should be skipped. */ 4434 1.8 christos 4435 1.1 christos static bool 4436 1.1 christos skip_section (bfd *ibfd, sec_ptr isection, bool skip_copy) 4437 1.1 christos { 4438 1.1 christos sec_ptr osection; 4439 1.1 christos bfd_size_type size; 4440 1.1 christos flagword flags; 4441 1.1 christos 4442 1.1 christos /* If we have already failed earlier on, 4443 1.8 christos do not keep on generating complaints now. */ 4444 1.1 christos if (status != 0) 4445 1.1 christos return true; 4446 1.8 christos 4447 1.1 christos if (extract_symbol) 4448 1.1 christos return true; 4449 1.8 christos 4450 1.1 christos if (is_strip_section (ibfd, isection)) 4451 1.3 christos return true; 4452 1.8 christos 4453 1.3 christos if (is_update_section (ibfd, isection)) 4454 1.6 christos return true; 4455 1.6 christos 4456 1.7 christos /* When merging a note section we skip the copying of the contents, 4457 1.8 christos but not the copying of the relocs associated with the contents. */ 4458 1.6 christos if (skip_copy && is_mergeable_note_section (ibfd, isection)) 4459 1.7 christos return true; 4460 1.1 christos 4461 1.8 christos flags = bfd_section_flags (isection); 4462 1.1 christos if ((flags & SEC_GROUP) != 0) 4463 1.1 christos return true; 4464 1.7 christos 4465 1.1 christos osection = isection->output_section; 4466 1.1 christos size = bfd_section_size (isection); 4467 1.8 christos 4468 1.1 christos if (size == 0 || osection == 0) 4469 1.8 christos return true; 4470 1.1 christos 4471 1.1 christos return false; 4472 1.6 christos } 4473 1.6 christos 4474 1.6 christos /* Add section SECTION_PATTERN to the list of sections that will have their 4475 1.6 christos relocations removed. */ 4476 1.6 christos 4477 1.6 christos static void 4478 1.8 christos handle_remove_relocations_option (const char *section_pattern) 4479 1.6 christos { 4480 1.6 christos find_section_list (section_pattern, true, SECTION_CONTEXT_REMOVE_RELOCS); 4481 1.6 christos } 4482 1.6 christos 4483 1.6 christos /* Return TRUE if ISECTION from IBFD should have its relocations removed, 4484 1.6 christos otherwise return FALSE. If the user has requested that relocations be 4485 1.6 christos removed from a section that does not have relocations then this 4486 1.8 christos function will still return TRUE. */ 4487 1.6 christos 4488 1.6 christos static bool 4489 1.8 christos discard_relocations (bfd *ibfd ATTRIBUTE_UNUSED, asection *isection) 4490 1.6 christos { 4491 1.6 christos return (find_section_list (bfd_section_name (isection), false, 4492 1.6 christos SECTION_CONTEXT_REMOVE_RELOCS) != NULL); 4493 1.6 christos } 4494 1.6 christos 4495 1.7 christos /* Wrapper for dealing with --remove-section (-R) command line arguments. 4496 1.7 christos A special case is detected here, if the user asks to remove a relocation 4497 1.6 christos section (one starting with ".rela" or ".rel") then this removal must 4498 1.6 christos be done using a different technique in a relocatable object. */ 4499 1.6 christos 4500 1.6 christos static void 4501 1.8 christos handle_remove_section_option (const char *section_pattern) 4502 1.8 christos { 4503 1.6 christos find_section_list (section_pattern, true, SECTION_CONTEXT_REMOVE); 4504 1.7 christos if (startswith (section_pattern, ".rel")) 4505 1.7 christos { 4506 1.7 christos section_pattern += 4; 4507 1.7 christos if (*section_pattern == 'a') 4508 1.7 christos section_pattern++; 4509 1.6 christos if (*section_pattern) 4510 1.8 christos handle_remove_relocations_option (section_pattern); 4511 1.6 christos } 4512 1.6 christos sections_removed = true; 4513 1.1 christos } 4514 1.1 christos 4515 1.1 christos /* Copy relocations in input section ISECTION of IBFD to an output 4516 1.1 christos section with the same name in OBFDARG. If stripping then don't 4517 1.10 christos copy any relocation info. */ 4518 1.10 christos 4519 1.1 christos static bool 4520 1.1 christos copy_relocations_in_section (bfd *ibfd, sec_ptr isection, bfd *obfd) 4521 1.1 christos { 4522 1.1 christos long relsize; 4523 1.1 christos arelent **relpp; 4524 1.1 christos long relcount; 4525 1.8 christos sec_ptr osection; 4526 1.10 christos 4527 1.1 christos if (skip_section (ibfd, isection, false)) 4528 1.1 christos return true; 4529 1.1 christos 4530 1.1 christos osection = isection->output_section; 4531 1.6 christos 4532 1.6 christos /* Core files and DWO files do not need to be relocated. */ 4533 1.9 christos if (bfd_get_format (obfd) == bfd_core 4534 1.9 christos || strip_symbols == STRIP_NONDWO 4535 1.6 christos || (strip_symbols == STRIP_ALL 4536 1.1 christos && htab_elements (keep_specific_htab) == 0) 4537 1.1 christos || discard_relocations (ibfd, isection)) 4538 1.1 christos relsize = 0; 4539 1.1 christos else 4540 1.1 christos { 4541 1.1 christos relsize = bfd_get_reloc_upper_bound (ibfd, isection); 4542 1.1 christos 4543 1.1 christos if (relsize < 0) 4544 1.1 christos { 4545 1.1 christos /* Do not complain if the target does not support relocations. */ 4546 1.1 christos if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation) 4547 1.1 christos relsize = 0; 4548 1.1 christos else 4549 1.10 christos { 4550 1.1 christos bfd_nonfatal_message (NULL, ibfd, isection, NULL); 4551 1.1 christos return false; 4552 1.1 christos } 4553 1.1 christos } 4554 1.1 christos } 4555 1.9 christos 4556 1.1 christos if (relsize == 0) 4557 1.1 christos bfd_set_reloc (obfd, osection, NULL, 0); 4558 1.6 christos else 4559 1.6 christos { 4560 1.6 christos if (isection->orelocation != NULL) 4561 1.6 christos { 4562 1.6 christos /* Some other function has already set up the output relocs 4563 1.6 christos for us, so scan those instead of the default relocs. */ 4564 1.6 christos relcount = isection->reloc_count; 4565 1.6 christos relpp = isection->orelocation; 4566 1.1 christos } 4567 1.8 christos else 4568 1.6 christos { 4569 1.6 christos relpp = bfd_xalloc (obfd, relsize); 4570 1.6 christos relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); 4571 1.6 christos if (relcount < 0) 4572 1.6 christos { 4573 1.10 christos bfd_nonfatal_message (NULL, ibfd, isection, 4574 1.6 christos _("relocation count is negative")); 4575 1.1 christos return false; 4576 1.1 christos } 4577 1.1 christos } 4578 1.1 christos 4579 1.1 christos if (strip_symbols == STRIP_ALL) 4580 1.1 christos { 4581 1.8 christos /* Remove relocations which are not in 4582 1.1 christos keep_strip_specific_list. */ 4583 1.1 christos arelent **w_relpp; 4584 1.8 christos long i; 4585 1.8 christos 4586 1.8 christos for (w_relpp = relpp, i = 0; i < relcount; i++) 4587 1.8 christos /* PR 17512: file: 9e907e0c. */ 4588 1.8 christos if (relpp[i]->sym_ptr_ptr 4589 1.8 christos /* PR 20096 */ 4590 1.8 christos && *relpp[i]->sym_ptr_ptr 4591 1.8 christos && is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr), 4592 1.8 christos keep_specific_htab)) 4593 1.8 christos *w_relpp++ = relpp[i]; 4594 1.1 christos relcount = w_relpp - relpp; 4595 1.1 christos *w_relpp = 0; 4596 1.1 christos } 4597 1.1 christos 4598 1.10 christos bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount); 4599 1.1 christos } 4600 1.1 christos return true; 4601 1.1 christos } 4602 1.1 christos 4603 1.1 christos /* Copy the data of input section ISECTION of IBFD 4604 1.10 christos to an output section with the same name in OBFD. */ 4605 1.10 christos 4606 1.1 christos static bool 4607 1.1 christos copy_section (bfd *ibfd, sec_ptr isection, bfd *obfd) 4608 1.1 christos { 4609 1.1 christos struct section_list *p; 4610 1.1 christos sec_ptr osection; 4611 1.8 christos bfd_size_type size; 4612 1.10 christos 4613 1.1 christos if (skip_section (ibfd, isection, true)) 4614 1.1 christos return true; 4615 1.3 christos 4616 1.3 christos osection = isection->output_section; 4617 1.3 christos /* The output SHF_COMPRESSED section size is different from input if 4618 1.3 christos ELF classes of input and output aren't the same. We can't use 4619 1.7 christos the output section size since --interleave will shrink the output 4620 1.1 christos section. Size will be updated if the section is converted. */ 4621 1.7 christos size = bfd_section_size (isection); 4622 1.7 christos 4623 1.1 christos if (bfd_section_flags (isection) & SEC_HAS_CONTENTS 4624 1.1 christos && bfd_section_flags (osection) & SEC_HAS_CONTENTS) 4625 1.1 christos { 4626 1.3 christos bfd_byte *memhunk = NULL; 4627 1.3 christos 4628 1.3 christos if (!bfd_get_full_section_contents (ibfd, isection, &memhunk) 4629 1.1 christos || !bfd_convert_section_contents (ibfd, isection, obfd, 4630 1.9 christos &memhunk, &size)) 4631 1.1 christos { 4632 1.6 christos bfd_set_section_size (osection, 0); 4633 1.10 christos bfd_nonfatal_message (NULL, ibfd, isection, NULL); 4634 1.1 christos free (memhunk); 4635 1.1 christos return false; 4636 1.1 christos } 4637 1.1 christos 4638 1.1 christos if (reverse_bytes) 4639 1.1 christos { 4640 1.1 christos /* We don't handle leftover bytes (too many possible behaviors, 4641 1.1 christos and we don't know what the user wants). The section length 4642 1.1 christos must be a multiple of the number of bytes to swap. */ 4643 1.1 christos if ((size % reverse_bytes) == 0) 4644 1.1 christos { 4645 1.1 christos unsigned long i, j; 4646 1.1 christos bfd_byte b; 4647 1.1 christos 4648 1.1 christos for (i = 0; i < size; i += reverse_bytes) 4649 1.1 christos for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++) 4650 1.1 christos { 4651 1.1 christos bfd_byte *m = (bfd_byte *) memhunk; 4652 1.1 christos 4653 1.1 christos b = m[i + j]; 4654 1.1 christos m[i + j] = m[(i + reverse_bytes) - (j + 1)]; 4655 1.1 christos m[(i + reverse_bytes) - (j + 1)] = b; 4656 1.1 christos } 4657 1.1 christos } 4658 1.1 christos else 4659 1.7 christos /* User must pad the section up in order to do this. */ 4660 1.1 christos fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"), 4661 1.1 christos bfd_section_name (isection), reverse_bytes); 4662 1.1 christos } 4663 1.1 christos 4664 1.1 christos if (copy_byte >= 0) 4665 1.1 christos { 4666 1.1 christos /* Keep only every `copy_byte'th byte in MEMHUNK. */ 4667 1.1 christos char *from = (char *) memhunk + copy_byte; 4668 1.1 christos char *to = (char *) memhunk; 4669 1.10 christos char *end = (char *) memhunk + size; 4670 1.1 christos int i; 4671 1.6 christos bfd_size_type memhunk_size = size; 4672 1.6 christos 4673 1.6 christos /* If the section address is not exactly divisible by the interleave, 4674 1.6 christos then we must bias the from address. If the copy_byte is less than 4675 1.6 christos the bias, then we must skip forward one interleave, and increment 4676 1.6 christos the final lma. */ 4677 1.6 christos int extra = isection->lma % interleave; 4678 1.6 christos from -= extra; 4679 1.6 christos if (copy_byte < extra) 4680 1.1 christos from += interleave; 4681 1.1 christos 4682 1.3 christos for (; from < end; from += interleave) 4683 1.3 christos for (i = 0; i < copy_width; i++) 4684 1.3 christos { 4685 1.3 christos if (&from[i] >= end) 4686 1.3 christos break; 4687 1.1 christos *to++ = from[i]; 4688 1.1 christos } 4689 1.10 christos 4690 1.10 christos size = (size + interleave - 1 - copy_byte) / interleave * copy_width; 4691 1.10 christos 4692 1.10 christos /* Don't extend the output section size. */ 4693 1.10 christos if (size > memhunk_size) 4694 1.1 christos size = memhunk_size; 4695 1.6 christos 4696 1.6 christos osection->lma /= interleave; 4697 1.1 christos if (copy_byte < extra) 4698 1.1 christos osection->lma++; 4699 1.1 christos } 4700 1.1 christos 4701 1.1 christos if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size)) 4702 1.6 christos { 4703 1.10 christos bfd_nonfatal_message (NULL, obfd, osection, NULL); 4704 1.1 christos free (memhunk); 4705 1.1 christos return false; 4706 1.1 christos } 4707 1.7 christos free (memhunk); 4708 1.8 christos } 4709 1.3 christos else if ((p = find_section_list (bfd_section_name (isection), 4710 1.1 christos false, SECTION_CONTEXT_SET_FLAGS)) != NULL 4711 1.1 christos && (p->flags & SEC_HAS_CONTENTS) != 0) 4712 1.1 christos { 4713 1.1 christos void *memhunk = xmalloc (size); 4714 1.1 christos 4715 1.1 christos /* We don't permit the user to turn off the SEC_HAS_CONTENTS 4716 1.1 christos flag--they can just remove the section entirely and add it 4717 1.1 christos back again. However, we do permit them to turn on the 4718 1.1 christos SEC_HAS_CONTENTS flag, and take it to mean that the section 4719 1.1 christos contents should be zeroed out. */ 4720 1.1 christos 4721 1.1 christos memset (memhunk, 0, size); 4722 1.1 christos if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size)) 4723 1.6 christos { 4724 1.10 christos bfd_nonfatal_message (NULL, obfd, osection, NULL); 4725 1.1 christos free (memhunk); 4726 1.1 christos return false; 4727 1.1 christos } 4728 1.10 christos free (memhunk); 4729 1.1 christos } 4730 1.1 christos return true; 4731 1.7 christos } 4732 1.1 christos 4733 1.1 christos /* Sort sections by LMA. This is called via qsort, and is used when 4734 1.1 christos --gap-fill or --pad-to is used. We force non loadable or empty 4735 1.1 christos sections to the front, where they are easier to ignore. */ 4736 1.1 christos 4737 1.1 christos static int 4738 1.7 christos compare_section_lma (const void *arg1, const void *arg2) 4739 1.7 christos { 4740 1.1 christos const asection *sec1 = *(const asection **) arg1; 4741 1.1 christos const asection *sec2 = *(const asection **) arg2; 4742 1.1 christos flagword flags1, flags2; 4743 1.7 christos 4744 1.7 christos /* Sort non loadable sections to the front. */ 4745 1.1 christos flags1 = sec1->flags; 4746 1.1 christos flags2 = sec2->flags; 4747 1.1 christos if ((flags1 & SEC_HAS_CONTENTS) == 0 4748 1.1 christos || (flags1 & SEC_LOAD) == 0) 4749 1.1 christos { 4750 1.1 christos if ((flags2 & SEC_HAS_CONTENTS) != 0 4751 1.1 christos && (flags2 & SEC_LOAD) != 0) 4752 1.1 christos return -1; 4753 1.1 christos } 4754 1.1 christos else 4755 1.1 christos { 4756 1.1 christos if ((flags2 & SEC_HAS_CONTENTS) == 0 4757 1.1 christos || (flags2 & SEC_LOAD) == 0) 4758 1.1 christos return 1; 4759 1.1 christos } 4760 1.7 christos 4761 1.1 christos /* Sort sections by LMA. */ 4762 1.7 christos if (sec1->lma > sec2->lma) 4763 1.1 christos return 1; 4764 1.1 christos if (sec1->lma < sec2->lma) 4765 1.1 christos return -1; 4766 1.7 christos 4767 1.1 christos /* Sort sections with the same LMA by size. */ 4768 1.7 christos if (bfd_section_size (sec1) > bfd_section_size (sec2)) 4769 1.1 christos return 1; 4770 1.1 christos if (bfd_section_size (sec1) < bfd_section_size (sec2)) 4771 1.7 christos return -1; 4772 1.7 christos 4773 1.7 christos if (sec1->id > sec2->id) 4774 1.7 christos return 1; 4775 1.1 christos if (sec1->id < sec2->id) 4776 1.1 christos return -1; 4777 1.1 christos return 0; 4778 1.1 christos } 4779 1.1 christos 4780 1.1 christos /* Mark all the symbols which will be used in output relocations with 4781 1.1 christos the BSF_KEEP flag so that those symbols will not be stripped. 4782 1.1 christos 4783 1.10 christos Ignore relocations which will not appear in the output file. */ 4784 1.10 christos 4785 1.1 christos static bool 4786 1.1 christos mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, asymbol **symbols) 4787 1.1 christos { 4788 1.1 christos long relsize; 4789 1.1 christos arelent **relpp; 4790 1.1 christos long relcount, i; 4791 1.1 christos 4792 1.10 christos /* Ignore an input section with no corresponding output section. */ 4793 1.1 christos if (isection->output_section == NULL) 4794 1.1 christos return true; 4795 1.1 christos 4796 1.1 christos relsize = bfd_get_reloc_upper_bound (ibfd, isection); 4797 1.1 christos if (relsize < 0) 4798 1.1 christos { 4799 1.10 christos /* Do not complain if the target does not support relocations. */ 4800 1.10 christos if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation) 4801 1.1 christos return true; 4802 1.1 christos return false; 4803 1.1 christos } 4804 1.10 christos 4805 1.1 christos if (relsize == 0) 4806 1.1 christos return true; 4807 1.1 christos 4808 1.1 christos relpp = (arelent **) xmalloc (relsize); 4809 1.10 christos relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols); 4810 1.10 christos if (relcount < 0) 4811 1.10 christos { 4812 1.10 christos free (relpp); 4813 1.1 christos return false; 4814 1.1 christos } 4815 1.1 christos 4816 1.1 christos /* Examine each symbol used in a relocation. If it's not one of the 4817 1.1 christos special bfd section symbols, then mark it with BSF_KEEP. */ 4818 1.6 christos for (i = 0; i < relcount; i++) 4819 1.6 christos { 4820 1.6 christos /* See PRs 20923 and 20930 for reproducers for the NULL tests. */ 4821 1.6 christos if (relpp[i]->sym_ptr_ptr != NULL 4822 1.1 christos && * relpp[i]->sym_ptr_ptr != NULL 4823 1.1 christos && *relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol 4824 1.1 christos && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol 4825 1.1 christos && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol) 4826 1.1 christos (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP; 4827 1.8 christos } 4828 1.10 christos 4829 1.1 christos free (relpp); 4830 1.1 christos return true; 4831 1.1 christos } 4832 1.1 christos 4833 1.8 christos /* Write out debugging information. */ 4834 1.1 christos 4835 1.1 christos static bool 4836 1.1 christos write_debugging_info (bfd *obfd, void *dhandle, 4837 1.1 christos long *symcountp ATTRIBUTE_UNUSED, 4838 1.1 christos asymbol ***symppp ATTRIBUTE_UNUSED) 4839 1.1 christos { 4840 1.1 christos if (bfd_get_flavour (obfd) == bfd_target_coff_flavour 4841 1.7 christos || bfd_get_flavour (obfd) == bfd_target_elf_flavour) 4842 1.1 christos { 4843 1.1 christos bfd_byte *syms, *strings = NULL; 4844 1.1 christos bfd_size_type symsize, stringsize; 4845 1.9 christos asection *stabsec, *stabstrsec; 4846 1.1 christos flagword flags; 4847 1.1 christos bool ret; 4848 1.1 christos 4849 1.1 christos if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms, 4850 1.8 christos &symsize, &strings, 4851 1.1 christos &stringsize)) 4852 1.1 christos return false; 4853 1.1 christos 4854 1.1 christos flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING; 4855 1.9 christos stabsec = bfd_make_section_with_flags (obfd, ".stab", flags); 4856 1.1 christos stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags); 4857 1.1 christos ret = true; 4858 1.7 christos if (stabsec == NULL 4859 1.7 christos || stabstrsec == NULL 4860 1.7 christos || !bfd_set_section_size (stabsec, symsize) 4861 1.7 christos || !bfd_set_section_size (stabstrsec, stringsize) 4862 1.1 christos || !bfd_set_section_alignment (stabsec, 2) 4863 1.1 christos || !bfd_set_section_alignment (stabstrsec, 0)) 4864 1.1 christos { 4865 1.9 christos bfd_nonfatal_message (NULL, obfd, NULL, 4866 1.1 christos _("can't create debugging section")); 4867 1.1 christos ret = false; 4868 1.1 christos } 4869 1.5 christos 4870 1.5 christos /* We can get away with setting the section contents now because 4871 1.5 christos the next thing the caller is going to do is copy over the 4872 1.9 christos real sections. We may someday have to split the contents 4873 1.9 christos setting out of this function. */ 4874 1.9 christos if (ret 4875 1.9 christos && (!bfd_set_section_contents (obfd, stabsec, syms, 0, symsize) 4876 1.1 christos || !bfd_set_section_contents (obfd, stabstrsec, strings, 0, 4877 1.1 christos stringsize))) 4878 1.1 christos { 4879 1.9 christos bfd_nonfatal_message (NULL, obfd, NULL, 4880 1.1 christos _("can't set debugging section contents")); 4881 1.1 christos ret = false; 4882 1.9 christos } 4883 1.9 christos 4884 1.9 christos free (strings); 4885 1.1 christos free (syms); 4886 1.1 christos return ret; 4887 1.1 christos } 4888 1.1 christos 4889 1.5 christos bfd_nonfatal_message (NULL, obfd, NULL, 4890 1.8 christos _("don't know how to write debugging information for %s"), 4891 1.1 christos bfd_get_target (obfd)); 4892 1.1 christos return false; 4893 1.1 christos } 4894 1.1 christos 4895 1.1 christos /* If neither -D nor -U was specified explicitly, 4896 1.1 christos then use the configured default. */ 4897 1.1 christos static void 4898 1.1 christos default_deterministic (void) 4899 1.1 christos { 4900 1.1 christos if (deterministic < 0) 4901 1.1 christos deterministic = DEFAULT_AR_DETERMINISTIC; 4902 1.1 christos } 4903 1.1 christos 4904 1.1 christos static int 4905 1.1 christos strip_main (int argc, char *argv[]) 4906 1.1 christos { 4907 1.8 christos char *input_target = NULL; 4908 1.8 christos char *output_target = NULL; 4909 1.1 christos bool show_version = false; 4910 1.1 christos bool formats_info = false; 4911 1.1 christos int c; 4912 1.8 christos int i; 4913 1.6 christos char *output_file = NULL; 4914 1.10 christos bool merge_notes_set = false; 4915 1.10 christos 4916 1.10 christos #if BFD_SUPPORTS_PLUGINS 4917 1.10 christos bfd_plugin_set_program_name (argv[0]); 4918 1.6 christos #endif 4919 1.1 christos 4920 1.1 christos while ((c = getopt_long (argc, argv, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU", 4921 1.1 christos strip_options, (int *) 0)) != EOF) 4922 1.1 christos { 4923 1.1 christos switch (c) 4924 1.1 christos { 4925 1.1 christos case 'I': 4926 1.1 christos input_target = optarg; 4927 1.1 christos break; 4928 1.1 christos case 'O': 4929 1.1 christos output_target = optarg; 4930 1.1 christos break; 4931 1.1 christos case 'F': 4932 1.1 christos input_target = output_target = optarg; 4933 1.6 christos break; 4934 1.6 christos case 'R': 4935 1.7 christos handle_remove_section_option (optarg); 4936 1.8 christos break; 4937 1.7 christos case OPTION_KEEP_SECTION: 4938 1.6 christos find_section_list (optarg, true, SECTION_CONTEXT_KEEP); 4939 1.6 christos break; 4940 1.1 christos case OPTION_REMOVE_RELOCS: 4941 1.9 christos handle_remove_relocations_option (optarg); 4942 1.9 christos break; 4943 1.9 christos case OPTION_STRIP_SECTION_HEADERS: 4944 1.1 christos strip_section_headers = true; 4945 1.1 christos break; 4946 1.1 christos case 's': 4947 1.1 christos strip_symbols = STRIP_ALL; 4948 1.1 christos break; 4949 1.1 christos case 'S': 4950 1.1 christos case 'g': 4951 1.1 christos case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */ 4952 1.1 christos strip_symbols = STRIP_DEBUG; 4953 1.1 christos break; 4954 1.1 christos case OPTION_STRIP_DWO: 4955 1.1 christos strip_symbols = STRIP_DWO; 4956 1.1 christos break; 4957 1.1 christos case OPTION_STRIP_UNNEEDED: 4958 1.1 christos strip_symbols = STRIP_UNNEEDED; 4959 1.1 christos break; 4960 1.1 christos case 'K': 4961 1.6 christos add_specific_symbol (optarg, keep_specific_htab); 4962 1.8 christos break; 4963 1.8 christos case 'M': 4964 1.6 christos merge_notes = true; 4965 1.6 christos merge_notes_set = true; 4966 1.8 christos break; 4967 1.8 christos case OPTION_NO_MERGE_NOTES: 4968 1.6 christos merge_notes = false; 4969 1.1 christos merge_notes_set = true; 4970 1.1 christos break; 4971 1.1 christos case 'N': 4972 1.1 christos add_specific_symbol (optarg, strip_specific_htab); 4973 1.1 christos break; 4974 1.1 christos case 'o': 4975 1.1 christos output_file = optarg; 4976 1.8 christos break; 4977 1.1 christos case 'p': 4978 1.1 christos preserve_dates = true; 4979 1.8 christos break; 4980 1.1 christos case 'D': 4981 1.1 christos deterministic = true; 4982 1.8 christos break; 4983 1.1 christos case 'U': 4984 1.1 christos deterministic = false; 4985 1.1 christos break; 4986 1.1 christos case 'x': 4987 1.1 christos discard_locals = LOCALS_ALL; 4988 1.1 christos break; 4989 1.1 christos case 'X': 4990 1.1 christos discard_locals = LOCALS_START_L; 4991 1.8 christos break; 4992 1.1 christos case 'v': 4993 1.1 christos verbose = true; 4994 1.8 christos break; 4995 1.1 christos case 'V': 4996 1.1 christos show_version = true; 4997 1.8 christos break; 4998 1.1 christos case OPTION_FORMATS_INFO: 4999 1.1 christos formats_info = true; 5000 1.1 christos break; 5001 1.1 christos case OPTION_ONLY_KEEP_DEBUG: 5002 1.1 christos strip_symbols = STRIP_NONDEBUG; 5003 1.1 christos break; 5004 1.1 christos case OPTION_KEEP_FILE_SYMBOLS: 5005 1.8 christos keep_file_symbols = 1; 5006 1.8 christos break; 5007 1.8 christos case OPTION_KEEP_SECTION_SYMBOLS: 5008 1.10 christos keep_section_symbols = true; 5009 1.10 christos break; 5010 1.10 christos case OPTION_PLUGIN: /* --plugin */ 5011 1.10 christos #if BFD_SUPPORTS_PLUGINS 5012 1.10 christos bfd_plugin_set_plugin (optarg); 5013 1.10 christos #else 5014 1.10 christos fatal (_("sorry - this program has been built without plugin support\n")); 5015 1.1 christos #endif 5016 1.1 christos break; 5017 1.1 christos case 0: 5018 1.1 christos /* We've been given a long option. */ 5019 1.8 christos break; 5020 1.1 christos case 'w': 5021 1.1 christos wildcard = true; 5022 1.1 christos break; 5023 1.1 christos case 'H': 5024 1.1 christos case 'h': 5025 1.1 christos strip_usage (stdout, 0); 5026 1.1 christos default: 5027 1.1 christos strip_usage (stderr, 1); 5028 1.1 christos } 5029 1.7 christos } 5030 1.7 christos 5031 1.7 christos /* If the user has not expressly chosen to merge/not-merge ELF notes 5032 1.7 christos then enable the merging unless we are stripping debug or dwo info. */ 5033 1.7 christos if (! merge_notes_set 5034 1.7 christos && (strip_symbols == STRIP_UNDEF 5035 1.7 christos || strip_symbols == STRIP_ALL 5036 1.7 christos || strip_symbols == STRIP_UNNEEDED 5037 1.8 christos || strip_symbols == STRIP_NONDEBUG 5038 1.7 christos || strip_symbols == STRIP_NONDWO)) 5039 1.1 christos merge_notes = true; 5040 1.1 christos 5041 1.1 christos if (formats_info) 5042 1.1 christos { 5043 1.1 christos display_info (); 5044 1.1 christos return 0; 5045 1.1 christos } 5046 1.1 christos 5047 1.1 christos if (show_version) 5048 1.1 christos print_version ("strip"); 5049 1.1 christos 5050 1.1 christos default_deterministic (); 5051 1.1 christos 5052 1.1 christos /* Default is to strip all symbols. */ 5053 1.1 christos if (strip_symbols == STRIP_UNDEF 5054 1.1 christos && discard_locals == LOCALS_UNDEF 5055 1.1 christos && htab_elements (strip_specific_htab) == 0) 5056 1.1 christos strip_symbols = STRIP_ALL; 5057 1.1 christos 5058 1.1 christos if (output_target == NULL) 5059 1.10 christos output_target = input_target; 5060 1.10 christos 5061 1.10 christos #if BFD_SUPPORTS_PLUGINS 5062 1.10 christos /* Check if all GCC LTO sections should be removed, assuming all LTO 5063 1.10 christos sections will be removed with -R .gnu.lto_.*. * Remove .gnu.lto_.* 5064 1.10 christos sections will also remove .gnu.debuglto_. sections. LLVM IR 5065 1.10 christos bitcode is stored in .llvm.lto section which will be removed with 5066 1.10 christos -R .llvm.lto. */ 5067 1.10 christos lto_sections_removed = (!!find_section_list (".gnu.lto_.*", false, 5068 1.10 christos SECTION_CONTEXT_REMOVE) 5069 1.10 christos || !!find_section_list (".llvm.lto", false, 5070 1.10 christos SECTION_CONTEXT_REMOVE)); 5071 1.1 christos #endif 5072 1.1 christos 5073 1.1 christos i = optind; 5074 1.1 christos if (i == argc 5075 1.1 christos || (output_file != NULL && (i + 1) < argc)) 5076 1.1 christos strip_usage (stderr, 1); 5077 1.1 christos 5078 1.1 christos for (; i < argc; i++) 5079 1.1 christos { 5080 1.1 christos int hold_status = status; 5081 1.8 christos struct stat statbuf; 5082 1.8 christos char *tmpname; 5083 1.1 christos int tmpfd = -1; 5084 1.1 christos int copyfd = -1; 5085 1.1 christos 5086 1.1 christos if (get_file_size (argv[i]) < 1) 5087 1.1 christos { 5088 1.1 christos status = 1; 5089 1.1 christos continue; 5090 1.1 christos } 5091 1.1 christos 5092 1.8 christos if (output_file == NULL 5093 1.8 christos || filename_cmp (argv[i], output_file) == 0) 5094 1.8 christos { 5095 1.8 christos tmpname = make_tempname (argv[i], &tmpfd); 5096 1.8 christos if (tmpfd >= 0) 5097 1.1 christos copyfd = dup (tmpfd); 5098 1.1 christos } 5099 1.1 christos else 5100 1.1 christos tmpname = output_file; 5101 1.1 christos 5102 1.1 christos if (tmpname == NULL) 5103 1.1 christos { 5104 1.1 christos bfd_nonfatal_message (argv[i], NULL, NULL, 5105 1.1 christos _("could not create temporary file to hold stripped copy")); 5106 1.1 christos status = 1; 5107 1.1 christos continue; 5108 1.1 christos } 5109 1.8 christos 5110 1.8 christos status = 0; 5111 1.1 christos copy_file (argv[i], tmpname, tmpfd, &statbuf, input_target, 5112 1.1 christos output_target, NULL); 5113 1.8 christos if (status == 0) 5114 1.8 christos { 5115 1.8 christos const char *oname = output_file ? output_file : argv[i]; 5116 1.1 christos status = smart_rename (tmpname, oname, copyfd, 5117 1.1 christos &statbuf, preserve_dates) != 0; 5118 1.1 christos if (status == 0) 5119 1.1 christos status = hold_status; 5120 1.8 christos } 5121 1.8 christos else 5122 1.8 christos { 5123 1.8 christos if (copyfd >= 0) 5124 1.8 christos close (copyfd); 5125 1.1 christos unlink_if_ordinary (tmpname); 5126 1.1 christos } 5127 1.1 christos if (output_file != tmpname) 5128 1.1 christos free (tmpname); 5129 1.1 christos } 5130 1.1 christos 5131 1.1 christos return status; 5132 1.1 christos } 5133 1.1 christos 5134 1.1 christos /* Set up PE subsystem. */ 5135 1.1 christos 5136 1.1 christos static void 5137 1.1 christos set_pe_subsystem (const char *s) 5138 1.1 christos { 5139 1.1 christos const char *version, *subsystem; 5140 1.1 christos size_t i; 5141 1.1 christos static const struct 5142 1.1 christos { 5143 1.1 christos const char *name; 5144 1.1 christos const char set_def; 5145 1.1 christos const short value; 5146 1.1 christos } 5147 1.1 christos v[] = 5148 1.1 christos { 5149 1.1 christos { "native", 0, IMAGE_SUBSYSTEM_NATIVE }, 5150 1.1 christos { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI }, 5151 1.1 christos { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI }, 5152 1.1 christos { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI }, 5153 1.1 christos { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI }, 5154 1.1 christos { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION }, 5155 1.1 christos { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER }, 5156 1.1 christos { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER }, 5157 1.1 christos { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER }, 5158 1.1 christos { "xbox", 0, IMAGE_SUBSYSTEM_XBOX } 5159 1.1 christos }; 5160 1.1 christos short value; 5161 1.1 christos char *copy; 5162 1.1 christos int set_def = -1; 5163 1.1 christos 5164 1.1 christos /* Check for the presence of a version number. */ 5165 1.1 christos version = strchr (s, ':'); 5166 1.1 christos if (version == NULL) 5167 1.1 christos subsystem = s; 5168 1.1 christos else 5169 1.1 christos { 5170 1.1 christos int len = version - s; 5171 1.1 christos copy = xstrdup (s); 5172 1.1 christos subsystem = copy; 5173 1.1 christos copy[len] = '\0'; 5174 1.1 christos version = copy + 1 + len; 5175 1.1 christos pe_major_subsystem_version = strtoul (version, ©, 0); 5176 1.1 christos if (*copy == '.') 5177 1.1 christos pe_minor_subsystem_version = strtoul (copy + 1, ©, 0); 5178 1.1 christos if (*copy != '\0') 5179 1.1 christos non_fatal (_("%s: bad version in PE subsystem"), s); 5180 1.1 christos } 5181 1.1 christos 5182 1.1 christos /* Check for numeric subsystem. */ 5183 1.1 christos value = (short) strtol (subsystem, ©, 0); 5184 1.1 christos if (*copy == '\0') 5185 1.1 christos { 5186 1.1 christos for (i = 0; i < ARRAY_SIZE (v); i++) 5187 1.1 christos if (v[i].value == value) 5188 1.1 christos { 5189 1.1 christos pe_subsystem = value; 5190 1.1 christos set_def = v[i].set_def; 5191 1.1 christos break; 5192 1.1 christos } 5193 1.1 christos } 5194 1.1 christos else 5195 1.1 christos { 5196 1.1 christos /* Search for subsystem by name. */ 5197 1.1 christos for (i = 0; i < ARRAY_SIZE (v); i++) 5198 1.1 christos if (strcmp (subsystem, v[i].name) == 0) 5199 1.1 christos { 5200 1.1 christos pe_subsystem = v[i].value; 5201 1.1 christos set_def = v[i].set_def; 5202 1.1 christos break; 5203 1.1 christos } 5204 1.1 christos } 5205 1.1 christos 5206 1.1 christos switch (set_def) 5207 1.1 christos { 5208 1.1 christos case -1: 5209 1.1 christos fatal (_("unknown PE subsystem: %s"), s); 5210 1.1 christos break; 5211 1.1 christos case 0: 5212 1.1 christos break; 5213 1.1 christos default: 5214 1.1 christos if (pe_file_alignment == (bfd_vma) -1) 5215 1.1 christos pe_file_alignment = PE_DEF_FILE_ALIGNMENT; 5216 1.1 christos if (pe_section_alignment == (bfd_vma) -1) 5217 1.1 christos pe_section_alignment = PE_DEF_SECTION_ALIGNMENT; 5218 1.1 christos break; 5219 1.1 christos } 5220 1.1 christos if (s != subsystem) 5221 1.1 christos free ((char *) subsystem); 5222 1.1 christos } 5223 1.1 christos 5224 1.8 christos /* Convert EFI target to PEI target. */ 5225 1.8 christos 5226 1.1 christos static int 5227 1.8 christos convert_efi_target (char **targ) 5228 1.8 christos { 5229 1.8 christos size_t len; 5230 1.8 christos char *pei; 5231 1.8 christos char *efi = *targ + 4; 5232 1.8 christos int subsys = -1; 5233 1.8 christos 5234 1.8 christos if (startswith (efi, "app-")) 5235 1.8 christos subsys = IMAGE_SUBSYSTEM_EFI_APPLICATION; 5236 1.8 christos else if (startswith (efi, "bsdrv-")) 5237 1.8 christos { 5238 1.8 christos subsys = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER; 5239 1.8 christos efi += 2; 5240 1.8 christos } 5241 1.8 christos else if (startswith (efi, "rtdrv-")) 5242 1.8 christos { 5243 1.8 christos subsys = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER; 5244 1.8 christos efi += 2; 5245 1.8 christos } 5246 1.8 christos else 5247 1.8 christos return subsys; 5248 1.8 christos 5249 1.8 christos len = strlen (efi); 5250 1.8 christos pei = xmalloc (len + sizeof ("-little")); 5251 1.8 christos memcpy (pei, efi, len + 1); 5252 1.8 christos pei[0] = 'p'; 5253 1.1 christos pei[1] = 'e'; 5254 1.1 christos pei[2] = 'i'; 5255 1.1 christos 5256 1.1 christos if (strcmp (efi + 4, "ia32") == 0) 5257 1.8 christos { 5258 1.8 christos /* Change ia32 to i386. */ 5259 1.8 christos pei[5]= '3'; 5260 1.1 christos pei[6]= '8'; 5261 1.1 christos pei[7]= '6'; 5262 1.1 christos } 5263 1.1 christos else if (strcmp (efi + 4, "x86_64") == 0) 5264 1.8 christos { 5265 1.8 christos /* Change x86_64 to x86-64. */ 5266 1.8 christos pei[7] = '-'; 5267 1.8 christos } 5268 1.8 christos else if (strcmp (efi + 4, "aarch64") == 0) 5269 1.8 christos { 5270 1.1 christos /* Change aarch64 to aarch64-little. */ 5271 1.9 christos memcpy (pei + 4 + sizeof ("aarch64") - 1, "-little", sizeof ("-little")); 5272 1.9 christos } 5273 1.9 christos else if (strcmp (efi + 4, "riscv64") == 0) 5274 1.9 christos { 5275 1.9 christos /* Change riscv64 to riscv64-little. */ 5276 1.8 christos memcpy (pei + 4 + sizeof ("riscv64") - 1, "-little", sizeof ("-little")); 5277 1.8 christos } 5278 1.1 christos *targ = pei; 5279 1.1 christos return subsys; 5280 1.3 christos } 5281 1.3 christos 5282 1.3 christos /* Allocate and return a pointer to a struct section_add, initializing the 5283 1.3 christos structure using ARG, a string in the format "sectionname=filename". 5284 1.3 christos The returned structure will have its next pointer set to NEXT. The 5285 1.3 christos OPTION field is the name of the command line option currently being 5286 1.3 christos parsed, and is only used if an error needs to be reported. */ 5287 1.3 christos 5288 1.5 christos static struct section_add * 5289 1.5 christos init_section_add (const char *arg, 5290 1.3 christos struct section_add *next, 5291 1.3 christos const char *option) 5292 1.3 christos { 5293 1.3 christos struct section_add *pa; 5294 1.3 christos const char *s; 5295 1.3 christos 5296 1.3 christos s = strchr (arg, '='); 5297 1.3 christos if (s == NULL) 5298 1.3 christos fatal (_("bad format for %s"), option); 5299 1.3 christos 5300 1.3 christos pa = (struct section_add *) xmalloc (sizeof (struct section_add)); 5301 1.3 christos pa->name = xstrndup (arg, s - arg); 5302 1.3 christos pa->filename = s + 1; 5303 1.3 christos pa->next = next; 5304 1.3 christos pa->contents = NULL; 5305 1.3 christos pa->size = 0; 5306 1.3 christos 5307 1.3 christos return pa; 5308 1.3 christos } 5309 1.3 christos 5310 1.3 christos /* Load the file specified in PA, allocating memory to hold the file 5311 1.3 christos contents, and store a pointer to the allocated memory in the contents 5312 1.3 christos field of PA. The size field of PA is also updated. All errors call 5313 1.3 christos FATAL. */ 5314 1.3 christos 5315 1.3 christos static void 5316 1.3 christos section_add_load_file (struct section_add *pa) 5317 1.3 christos { 5318 1.3 christos size_t off, alloc; 5319 1.3 christos FILE *f; 5320 1.3 christos 5321 1.3 christos /* We don't use get_file_size so that we can do 5322 1.3 christos --add-section .note.GNU_stack=/dev/null 5323 1.3 christos get_file_size doesn't work on /dev/null. */ 5324 1.3 christos 5325 1.3 christos f = fopen (pa->filename, FOPEN_RB); 5326 1.5 christos if (f == NULL) 5327 1.3 christos fatal (_("cannot open: %s: %s"), 5328 1.3 christos pa->filename, strerror (errno)); 5329 1.3 christos 5330 1.3 christos off = 0; 5331 1.3 christos alloc = 4096; 5332 1.3 christos pa->contents = (bfd_byte *) xmalloc (alloc); 5333 1.3 christos while (!feof (f)) 5334 1.3 christos { 5335 1.3 christos off_t got; 5336 1.5 christos 5337 1.5 christos if (off == alloc) 5338 1.5 christos { 5339 1.5 christos alloc <<= 1; 5340 1.3 christos pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc); 5341 1.3 christos } 5342 1.3 christos 5343 1.5 christos got = fread (pa->contents + off, 1, alloc - off, f); 5344 1.3 christos if (ferror (f)) 5345 1.3 christos fatal (_("%s: fread failed"), pa->filename); 5346 1.3 christos 5347 1.3 christos off += got; 5348 1.3 christos } 5349 1.3 christos 5350 1.3 christos pa->size = off; 5351 1.3 christos 5352 1.3 christos fclose (f); 5353 1.1 christos } 5354 1.1 christos 5355 1.1 christos static int 5356 1.1 christos copy_main (int argc, char *argv[]) 5357 1.1 christos { 5358 1.1 christos char *input_filename = NULL; 5359 1.1 christos char *output_filename = NULL; 5360 1.1 christos char *tmpname; 5361 1.8 christos char *input_target = NULL; 5362 1.8 christos char *output_target = NULL; 5363 1.8 christos bool show_version = false; 5364 1.8 christos bool change_warn = true; 5365 1.8 christos bool formats_info = false; 5366 1.1 christos bool use_globalize = false; 5367 1.8 christos bool use_keep_global = false; 5368 1.8 christos int c; 5369 1.1 christos int tmpfd = -1; 5370 1.1 christos int copyfd; 5371 1.1 christos struct stat statbuf; 5372 1.6 christos const bfd_arch_info_type *input_arch = NULL; 5373 1.1 christos 5374 1.1 christos while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU", 5375 1.1 christos copy_options, (int *) 0)) != EOF) 5376 1.1 christos { 5377 1.1 christos switch (c) 5378 1.1 christos { 5379 1.1 christos case 'b': 5380 1.1 christos copy_byte = atoi (optarg); 5381 1.1 christos if (copy_byte < 0) 5382 1.1 christos fatal (_("byte number must be non-negative")); 5383 1.1 christos break; 5384 1.1 christos 5385 1.1 christos case 'B': 5386 1.1 christos input_arch = bfd_scan_arch (optarg); 5387 1.1 christos if (input_arch == NULL) 5388 1.1 christos fatal (_("architecture %s unknown"), optarg); 5389 1.1 christos break; 5390 1.1 christos 5391 1.1 christos case 'i': 5392 1.1 christos if (optarg) 5393 1.1 christos { 5394 1.1 christos interleave = atoi (optarg); 5395 1.1 christos if (interleave < 1) 5396 1.1 christos fatal (_("interleave must be positive")); 5397 1.1 christos } 5398 1.1 christos else 5399 1.1 christos interleave = 4; 5400 1.1 christos break; 5401 1.1 christos 5402 1.1 christos case OPTION_INTERLEAVE_WIDTH: 5403 1.1 christos copy_width = atoi (optarg); 5404 1.1 christos if (copy_width < 1) 5405 1.1 christos fatal(_("interleave width must be positive")); 5406 1.1 christos break; 5407 1.1 christos 5408 1.1 christos case 'I': 5409 1.1 christos case 's': /* "source" - 'I' is preferred */ 5410 1.1 christos input_target = optarg; 5411 1.1 christos break; 5412 1.1 christos 5413 1.1 christos case 'O': 5414 1.1 christos case 'd': /* "destination" - 'O' is preferred */ 5415 1.1 christos output_target = optarg; 5416 1.1 christos break; 5417 1.1 christos 5418 1.1 christos case 'F': 5419 1.1 christos input_target = output_target = optarg; 5420 1.1 christos break; 5421 1.8 christos 5422 1.8 christos case 'j': 5423 1.1 christos find_section_list (optarg, true, SECTION_CONTEXT_COPY); 5424 1.1 christos sections_copied = true; 5425 1.1 christos break; 5426 1.6 christos 5427 1.6 christos case 'R': 5428 1.6 christos handle_remove_section_option (optarg); 5429 1.7 christos break; 5430 1.8 christos 5431 1.7 christos case OPTION_KEEP_SECTION: 5432 1.7 christos find_section_list (optarg, true, SECTION_CONTEXT_KEEP); 5433 1.6 christos break; 5434 1.6 christos 5435 1.1 christos case OPTION_REMOVE_RELOCS: 5436 1.1 christos handle_remove_relocations_option (optarg); 5437 1.9 christos break; 5438 1.9 christos 5439 1.9 christos case OPTION_STRIP_SECTION_HEADERS: 5440 1.9 christos strip_section_headers = true; 5441 1.1 christos break; 5442 1.1 christos 5443 1.1 christos case 'S': 5444 1.1 christos strip_symbols = STRIP_ALL; 5445 1.1 christos break; 5446 1.1 christos 5447 1.1 christos case 'g': 5448 1.1 christos strip_symbols = STRIP_DEBUG; 5449 1.1 christos break; 5450 1.1 christos 5451 1.1 christos case OPTION_STRIP_DWO: 5452 1.1 christos strip_symbols = STRIP_DWO; 5453 1.1 christos break; 5454 1.1 christos 5455 1.1 christos case OPTION_STRIP_UNNEEDED: 5456 1.1 christos strip_symbols = STRIP_UNNEEDED; 5457 1.1 christos break; 5458 1.1 christos 5459 1.1 christos case OPTION_ONLY_KEEP_DEBUG: 5460 1.1 christos strip_symbols = STRIP_NONDEBUG; 5461 1.1 christos break; 5462 1.1 christos 5463 1.1 christos case OPTION_KEEP_FILE_SYMBOLS: 5464 1.1 christos keep_file_symbols = 1; 5465 1.1 christos break; 5466 1.3 christos 5467 1.1 christos case OPTION_ADD_GNU_DEBUGLINK: 5468 1.1 christos long_section_names = ENABLE ; 5469 1.1 christos gnu_debuglink_filename = optarg; 5470 1.1 christos break; 5471 1.1 christos 5472 1.1 christos case 'K': 5473 1.1 christos add_specific_symbol (optarg, keep_specific_htab); 5474 1.6 christos break; 5475 1.8 christos 5476 1.6 christos case 'M': 5477 1.6 christos merge_notes = true; 5478 1.8 christos break; 5479 1.6 christos case OPTION_NO_MERGE_NOTES: 5480 1.6 christos merge_notes = false; 5481 1.1 christos break; 5482 1.1 christos 5483 1.1 christos case 'N': 5484 1.1 christos add_specific_symbol (optarg, strip_specific_htab); 5485 1.1 christos break; 5486 1.1 christos 5487 1.1 christos case OPTION_STRIP_UNNEEDED_SYMBOL: 5488 1.1 christos add_specific_symbol (optarg, strip_unneeded_htab); 5489 1.1 christos break; 5490 1.1 christos 5491 1.1 christos case 'L': 5492 1.1 christos add_specific_symbol (optarg, localize_specific_htab); 5493 1.1 christos break; 5494 1.8 christos 5495 1.1 christos case OPTION_GLOBALIZE_SYMBOL: 5496 1.1 christos use_globalize = true; 5497 1.1 christos add_specific_symbol (optarg, globalize_specific_htab); 5498 1.1 christos break; 5499 1.8 christos 5500 1.1 christos case 'G': 5501 1.1 christos use_keep_global = true; 5502 1.1 christos add_specific_symbol (optarg, keepglobal_specific_htab); 5503 1.1 christos break; 5504 1.1 christos 5505 1.1 christos case 'W': 5506 1.1 christos add_specific_symbol (optarg, weaken_specific_htab); 5507 1.1 christos break; 5508 1.8 christos 5509 1.1 christos case 'p': 5510 1.1 christos preserve_dates = true; 5511 1.1 christos break; 5512 1.8 christos 5513 1.1 christos case 'D': 5514 1.1 christos deterministic = true; 5515 1.1 christos break; 5516 1.8 christos 5517 1.1 christos case 'U': 5518 1.1 christos deterministic = false; 5519 1.1 christos break; 5520 1.8 christos 5521 1.1 christos case 'w': 5522 1.1 christos wildcard = true; 5523 1.1 christos break; 5524 1.1 christos 5525 1.1 christos case 'x': 5526 1.1 christos discard_locals = LOCALS_ALL; 5527 1.1 christos break; 5528 1.1 christos 5529 1.1 christos case 'X': 5530 1.1 christos discard_locals = LOCALS_START_L; 5531 1.1 christos break; 5532 1.8 christos 5533 1.1 christos case 'v': 5534 1.1 christos verbose = true; 5535 1.1 christos break; 5536 1.8 christos 5537 1.1 christos case 'V': 5538 1.1 christos show_version = true; 5539 1.1 christos break; 5540 1.8 christos 5541 1.1 christos case OPTION_FORMATS_INFO: 5542 1.1 christos formats_info = true; 5543 1.1 christos break; 5544 1.8 christos 5545 1.1 christos case OPTION_WEAKEN: 5546 1.1 christos weaken = true; 5547 1.1 christos break; 5548 1.5 christos 5549 1.5 christos case OPTION_ADD_SECTION: 5550 1.5 christos add_sections = init_section_add (optarg, add_sections, 5551 1.3 christos "--add-section"); 5552 1.3 christos section_add_load_file (add_sections); 5553 1.3 christos break; 5554 1.3 christos 5555 1.5 christos case OPTION_UPDATE_SECTION: 5556 1.3 christos update_sections = init_section_add (optarg, update_sections, 5557 1.3 christos "--update-section"); 5558 1.3 christos section_add_load_file (update_sections); 5559 1.3 christos break; 5560 1.5 christos 5561 1.5 christos case OPTION_DUMP_SECTION: 5562 1.3 christos dump_sections = init_section_add (optarg, dump_sections, 5563 1.3 christos "--dump-section"); 5564 1.3 christos break; 5565 1.1 christos 5566 1.3 christos case OPTION_ADD_SYMBOL: 5567 1.3 christos { 5568 1.1 christos char *s, *t; 5569 1.3 christos struct addsym_node *newsym = xmalloc (sizeof *newsym); 5570 1.1 christos 5571 1.1 christos newsym->next = NULL; 5572 1.3 christos s = strchr (optarg, '='); 5573 1.3 christos if (s == NULL) 5574 1.1 christos fatal (_("bad format for %s"), "--add-symbol"); 5575 1.3 christos t = strchr (s + 1, ':'); 5576 1.3 christos 5577 1.1 christos newsym->symdef = xstrndup (optarg, s - optarg); 5578 1.3 christos if (t) 5579 1.3 christos { 5580 1.3 christos newsym->section = xstrndup (s + 1, t - (s + 1)); 5581 1.3 christos newsym->symval = strtol (t + 1, NULL, 0); 5582 1.3 christos } 5583 1.3 christos else 5584 1.3 christos { 5585 1.3 christos newsym->section = NULL; 5586 1.3 christos newsym->symval = strtol (s + 1, NULL, 0); 5587 1.1 christos t = s; 5588 1.3 christos } 5589 1.3 christos 5590 1.3 christos t = strchr (t + 1, ','); 5591 1.3 christos newsym->othersym = NULL; 5592 1.3 christos if (t) 5593 1.3 christos newsym->flags = parse_symflags (t+1, &newsym->othersym); 5594 1.1 christos else 5595 1.3 christos newsym->flags = BSF_GLOBAL; 5596 1.3 christos 5597 1.3 christos /* Keep 'othersym' symbols at the front of the list. */ 5598 1.3 christos if (newsym->othersym) 5599 1.3 christos { 5600 1.3 christos newsym->next = add_sym_list; 5601 1.3 christos if (!add_sym_list) 5602 1.3 christos add_sym_tail = &newsym->next; 5603 1.3 christos add_sym_list = newsym; 5604 1.3 christos } 5605 1.3 christos else 5606 1.3 christos { 5607 1.1 christos *add_sym_tail = newsym; 5608 1.3 christos add_sym_tail = &newsym->next; 5609 1.1 christos } 5610 1.1 christos add_symbols++; 5611 1.1 christos } 5612 1.1 christos break; 5613 1.1 christos 5614 1.1 christos case OPTION_CHANGE_START: 5615 1.1 christos change_start = parse_vma (optarg, "--change-start"); 5616 1.1 christos break; 5617 1.1 christos 5618 1.1 christos case OPTION_CHANGE_SECTION_ADDRESS: 5619 1.1 christos case OPTION_CHANGE_SECTION_LMA: 5620 1.3 christos case OPTION_CHANGE_SECTION_VMA: 5621 1.3 christos { 5622 1.1 christos struct section_list * p; 5623 1.1 christos unsigned int context = 0; 5624 1.1 christos const char *s; 5625 1.1 christos int len; 5626 1.1 christos char *name; 5627 1.1 christos char *option = NULL; 5628 1.1 christos bfd_vma val; 5629 1.1 christos 5630 1.1 christos switch (c) 5631 1.1 christos { 5632 1.3 christos case OPTION_CHANGE_SECTION_ADDRESS: 5633 1.1 christos option = "--change-section-address"; 5634 1.1 christos context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA; 5635 1.1 christos break; 5636 1.3 christos case OPTION_CHANGE_SECTION_LMA: 5637 1.1 christos option = "--change-section-lma"; 5638 1.1 christos context = SECTION_CONTEXT_ALTER_LMA; 5639 1.1 christos break; 5640 1.3 christos case OPTION_CHANGE_SECTION_VMA: 5641 1.1 christos option = "--change-section-vma"; 5642 1.1 christos context = SECTION_CONTEXT_ALTER_VMA; 5643 1.1 christos break; 5644 1.1 christos } 5645 1.1 christos 5646 1.1 christos s = strchr (optarg, '='); 5647 1.1 christos if (s == NULL) 5648 1.1 christos { 5649 1.1 christos s = strchr (optarg, '+'); 5650 1.1 christos if (s == NULL) 5651 1.1 christos { 5652 1.1 christos s = strchr (optarg, '-'); 5653 1.1 christos if (s == NULL) 5654 1.1 christos fatal (_("bad format for %s"), option); 5655 1.3 christos } 5656 1.3 christos } 5657 1.3 christos else 5658 1.3 christos { 5659 1.3 christos /* Correct the context. */ 5660 1.3 christos switch (c) 5661 1.3 christos { 5662 1.3 christos case OPTION_CHANGE_SECTION_ADDRESS: 5663 1.3 christos context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA; 5664 1.3 christos break; 5665 1.3 christos case OPTION_CHANGE_SECTION_LMA: 5666 1.3 christos context = SECTION_CONTEXT_SET_LMA; 5667 1.3 christos break; 5668 1.3 christos case OPTION_CHANGE_SECTION_VMA: 5669 1.3 christos context = SECTION_CONTEXT_SET_VMA; 5670 1.3 christos break; 5671 1.1 christos } 5672 1.1 christos } 5673 1.1 christos 5674 1.1 christos len = s - optarg; 5675 1.1 christos name = (char *) xmalloc (len + 1); 5676 1.1 christos strncpy (name, optarg, len); 5677 1.8 christos name[len] = '\0'; 5678 1.1 christos 5679 1.1 christos p = find_section_list (name, true, context); 5680 1.3 christos 5681 1.3 christos val = parse_vma (s + 1, option); 5682 1.1 christos if (*s == '-') 5683 1.1 christos val = - val; 5684 1.1 christos 5685 1.1 christos switch (c) 5686 1.3 christos { 5687 1.6 christos case OPTION_CHANGE_SECTION_ADDRESS: 5688 1.1 christos p->vma_val = val; 5689 1.1 christos /* Fall through. */ 5690 1.3 christos 5691 1.1 christos case OPTION_CHANGE_SECTION_LMA: 5692 1.1 christos p->lma_val = val; 5693 1.1 christos break; 5694 1.3 christos 5695 1.1 christos case OPTION_CHANGE_SECTION_VMA: 5696 1.1 christos p->vma_val = val; 5697 1.1 christos break; 5698 1.1 christos } 5699 1.1 christos } 5700 1.1 christos break; 5701 1.1 christos 5702 1.1 christos case OPTION_CHANGE_ADDRESSES: 5703 1.1 christos change_section_address = parse_vma (optarg, "--change-addresses"); 5704 1.1 christos change_start = change_section_address; 5705 1.1 christos break; 5706 1.8 christos 5707 1.1 christos case OPTION_CHANGE_WARNINGS: 5708 1.1 christos change_warn = true; 5709 1.1 christos break; 5710 1.8 christos 5711 1.1 christos case OPTION_CHANGE_LEADING_CHAR: 5712 1.1 christos change_leading_char = true; 5713 1.1 christos break; 5714 1.3 christos 5715 1.3 christos case OPTION_COMPRESS_DEBUG_SECTIONS: 5716 1.3 christos if (optarg) 5717 1.3 christos { 5718 1.3 christos if (strcasecmp (optarg, "none") == 0) 5719 1.3 christos do_debug_sections = decompress; 5720 1.3 christos else if (strcasecmp (optarg, "zlib") == 0) 5721 1.3 christos do_debug_sections = compress_zlib; 5722 1.3 christos else if (strcasecmp (optarg, "zlib-gnu") == 0) 5723 1.3 christos do_debug_sections = compress_gnu_zlib; 5724 1.9 christos else if (strcasecmp (optarg, "zlib-gabi") == 0) 5725 1.9 christos do_debug_sections = compress_gabi_zlib; 5726 1.3 christos else if (strcasecmp (optarg, "zstd") == 0) 5727 1.3 christos do_debug_sections = compress_zstd; 5728 1.3 christos else 5729 1.3 christos fatal (_("unrecognized --compress-debug-sections type `%s'"), 5730 1.3 christos optarg); 5731 1.3 christos } 5732 1.1 christos else 5733 1.1 christos do_debug_sections = compress; 5734 1.1 christos break; 5735 1.8 christos 5736 1.1 christos case OPTION_DEBUGGING: 5737 1.1 christos convert_debugging = true; 5738 1.1 christos break; 5739 1.1 christos 5740 1.1 christos case OPTION_DECOMPRESS_DEBUG_SECTIONS: 5741 1.1 christos do_debug_sections = decompress; 5742 1.5 christos break; 5743 1.5 christos 5744 1.5 christos case OPTION_ELF_STT_COMMON: 5745 1.5 christos if (strcasecmp (optarg, "yes") == 0) 5746 1.5 christos do_elf_stt_common = elf_stt_common; 5747 1.5 christos else if (strcasecmp (optarg, "no") == 0) 5748 1.5 christos do_elf_stt_common = no_elf_stt_common; 5749 1.5 christos else 5750 1.5 christos fatal (_("unrecognized --elf-stt-common= option `%s'"), 5751 1.5 christos optarg); 5752 1.1 christos break; 5753 1.1 christos 5754 1.1 christos case OPTION_GAP_FILL: 5755 1.1 christos { 5756 1.1 christos bfd_vma gap_fill_vma; 5757 1.1 christos 5758 1.1 christos gap_fill_vma = parse_vma (optarg, "--gap-fill"); 5759 1.9 christos gap_fill = (bfd_byte) gap_fill_vma; 5760 1.9 christos if ((bfd_vma) gap_fill != gap_fill_vma) 5761 1.9 christos non_fatal (_("Warning: truncating gap-fill from 0x%" PRIx64 5762 1.8 christos " to 0x%x"), 5763 1.1 christos (uint64_t) gap_fill_vma, gap_fill); 5764 1.1 christos gap_fill_set = true; 5765 1.1 christos } 5766 1.1 christos break; 5767 1.8 christos 5768 1.1 christos case OPTION_NO_CHANGE_WARNINGS: 5769 1.1 christos change_warn = false; 5770 1.1 christos break; 5771 1.1 christos 5772 1.8 christos case OPTION_PAD_TO: 5773 1.1 christos pad_to = parse_vma (optarg, "--pad-to"); 5774 1.1 christos pad_to_set = true; 5775 1.1 christos break; 5776 1.8 christos 5777 1.1 christos case OPTION_REMOVE_LEADING_CHAR: 5778 1.1 christos remove_leading_char = true; 5779 1.1 christos break; 5780 1.1 christos 5781 1.6 christos case OPTION_REDEFINE_SYM: 5782 1.1 christos { 5783 1.1 christos /* Insert this redefinition onto redefine_specific_htab. */ 5784 1.1 christos 5785 1.1 christos int len; 5786 1.1 christos const char *s; 5787 1.1 christos const char *nextarg; 5788 1.1 christos char *source, *target; 5789 1.1 christos 5790 1.1 christos s = strchr (optarg, '='); 5791 1.1 christos if (s == NULL) 5792 1.1 christos fatal (_("bad format for %s"), "--redefine-sym"); 5793 1.1 christos 5794 1.1 christos len = s - optarg; 5795 1.1 christos source = (char *) xmalloc (len + 1); 5796 1.1 christos strncpy (source, optarg, len); 5797 1.1 christos source[len] = '\0'; 5798 1.1 christos 5799 1.1 christos nextarg = s + 1; 5800 1.1 christos len = strlen (nextarg); 5801 1.1 christos target = (char *) xmalloc (len + 1); 5802 1.6 christos strcpy (target, nextarg); 5803 1.1 christos 5804 1.1 christos add_redefine_and_check ("--redefine-sym", source, target); 5805 1.1 christos 5806 1.1 christos free (source); 5807 1.1 christos free (target); 5808 1.1 christos } 5809 1.1 christos break; 5810 1.1 christos 5811 1.1 christos case OPTION_REDEFINE_SYMS: 5812 1.1 christos add_redefine_syms_file (optarg); 5813 1.1 christos break; 5814 1.1 christos 5815 1.3 christos case OPTION_SET_SECTION_FLAGS: 5816 1.1 christos { 5817 1.1 christos struct section_list *p; 5818 1.1 christos const char *s; 5819 1.1 christos int len; 5820 1.1 christos char *name; 5821 1.1 christos 5822 1.1 christos s = strchr (optarg, '='); 5823 1.1 christos if (s == NULL) 5824 1.1 christos fatal (_("bad format for %s"), "--set-section-flags"); 5825 1.1 christos 5826 1.1 christos len = s - optarg; 5827 1.1 christos name = (char *) xmalloc (len + 1); 5828 1.1 christos strncpy (name, optarg, len); 5829 1.8 christos name[len] = '\0'; 5830 1.1 christos 5831 1.1 christos p = find_section_list (name, true, SECTION_CONTEXT_SET_FLAGS); 5832 1.1 christos 5833 1.1 christos p->flags = parse_flags (s + 1); 5834 1.1 christos } 5835 1.7 christos break; 5836 1.7 christos 5837 1.7 christos case OPTION_SET_SECTION_ALIGNMENT: 5838 1.7 christos { 5839 1.7 christos struct section_list *p; 5840 1.7 christos const char *s; 5841 1.7 christos int len; 5842 1.7 christos char *name; 5843 1.7 christos int palign, align; 5844 1.7 christos 5845 1.7 christos s = strchr (optarg, '='); 5846 1.7 christos if (s == NULL) 5847 1.7 christos fatal (_("bad format for --set-section-alignment: argument needed")); 5848 1.7 christos 5849 1.7 christos align = atoi (s + 1); 5850 1.7 christos if (align <= 0) 5851 1.7 christos fatal (_("bad format for --set-section-alignment: numeric argument needed")); 5852 1.10 christos 5853 1.10 christos /* Convert integer alignment into a power-of-two alignment. */ 5854 1.7 christos palign = power_of_two (align); 5855 1.7 christos if (palign == -1) 5856 1.7 christos fatal (_("bad format for --set-section-alignment: alignment is not a power of two")); 5857 1.7 christos 5858 1.7 christos /* Add the alignment setting to the section list. */ 5859 1.7 christos len = s - optarg; 5860 1.7 christos name = (char *) xmalloc (len + 1); 5861 1.7 christos strncpy (name, optarg, len); 5862 1.8 christos name[len] = '\0'; 5863 1.7 christos 5864 1.7 christos p = find_section_list (name, true, SECTION_CONTEXT_SET_ALIGNMENT); 5865 1.7 christos if (p) 5866 1.7 christos p->alignment = palign; 5867 1.7 christos } 5868 1.1 christos break; 5869 1.1 christos 5870 1.1 christos case OPTION_RENAME_SECTION: 5871 1.1 christos { 5872 1.1 christos flagword flags; 5873 1.1 christos const char *eq, *fl; 5874 1.1 christos char *old_name; 5875 1.1 christos char *new_name; 5876 1.1 christos unsigned int len; 5877 1.1 christos 5878 1.1 christos eq = strchr (optarg, '='); 5879 1.1 christos if (eq == NULL) 5880 1.1 christos fatal (_("bad format for %s"), "--rename-section"); 5881 1.1 christos 5882 1.1 christos len = eq - optarg; 5883 1.1 christos if (len == 0) 5884 1.1 christos fatal (_("bad format for %s"), "--rename-section"); 5885 1.1 christos 5886 1.1 christos old_name = (char *) xmalloc (len + 1); 5887 1.1 christos strncpy (old_name, optarg, len); 5888 1.1 christos old_name[len] = 0; 5889 1.1 christos 5890 1.1 christos eq++; 5891 1.1 christos fl = strchr (eq, ','); 5892 1.1 christos if (fl) 5893 1.1 christos { 5894 1.1 christos flags = parse_flags (fl + 1); 5895 1.1 christos len = fl - eq; 5896 1.1 christos } 5897 1.1 christos else 5898 1.1 christos { 5899 1.1 christos flags = -1; 5900 1.1 christos len = strlen (eq); 5901 1.1 christos } 5902 1.1 christos 5903 1.1 christos if (len == 0) 5904 1.1 christos fatal (_("bad format for %s"), "--rename-section"); 5905 1.1 christos 5906 1.1 christos new_name = (char *) xmalloc (len + 1); 5907 1.1 christos strncpy (new_name, eq, len); 5908 1.1 christos new_name[len] = 0; 5909 1.1 christos 5910 1.1 christos add_section_rename (old_name, new_name, flags); 5911 1.1 christos } 5912 1.1 christos break; 5913 1.1 christos 5914 1.8 christos case OPTION_SET_START: 5915 1.1 christos set_start = parse_vma (optarg, "--set-start"); 5916 1.1 christos set_start_set = true; 5917 1.1 christos break; 5918 1.6 christos 5919 1.1 christos case OPTION_SREC_LEN: 5920 1.1 christos _bfd_srec_len = parse_vma (optarg, "--srec-len"); 5921 1.1 christos break; 5922 1.8 christos 5923 1.1 christos case OPTION_SREC_FORCES3: 5924 1.1 christos _bfd_srec_forceS3 = true; 5925 1.1 christos break; 5926 1.7 christos 5927 1.7 christos case OPTION_STRIP_SYMBOLS: 5928 1.1 christos add_specific_symbols (optarg, strip_specific_htab, 5929 1.1 christos &strip_specific_buffer); 5930 1.1 christos break; 5931 1.7 christos 5932 1.7 christos case OPTION_STRIP_UNNEEDED_SYMBOLS: 5933 1.1 christos add_specific_symbols (optarg, strip_unneeded_htab, 5934 1.1 christos &strip_unneeded_buffer); 5935 1.1 christos break; 5936 1.7 christos 5937 1.7 christos case OPTION_KEEP_SYMBOLS: 5938 1.1 christos add_specific_symbols (optarg, keep_specific_htab, 5939 1.1 christos &keep_specific_buffer); 5940 1.8 christos break; 5941 1.8 christos 5942 1.8 christos case OPTION_KEEP_SECTION_SYMBOLS: 5943 1.8 christos keep_section_symbols = true; 5944 1.1 christos break; 5945 1.8 christos 5946 1.1 christos case OPTION_LOCALIZE_HIDDEN: 5947 1.1 christos localize_hidden = true; 5948 1.1 christos break; 5949 1.7 christos 5950 1.7 christos case OPTION_LOCALIZE_SYMBOLS: 5951 1.1 christos add_specific_symbols (optarg, localize_specific_htab, 5952 1.1 christos &localize_specific_buffer); 5953 1.1 christos break; 5954 1.1 christos 5955 1.1 christos case OPTION_LONG_SECTION_NAMES: 5956 1.1 christos if (!strcmp ("enable", optarg)) 5957 1.1 christos long_section_names = ENABLE; 5958 1.1 christos else if (!strcmp ("disable", optarg)) 5959 1.1 christos long_section_names = DISABLE; 5960 1.1 christos else if (!strcmp ("keep", optarg)) 5961 1.1 christos long_section_names = KEEP; 5962 1.1 christos else 5963 1.1 christos fatal (_("unknown long section names option '%s'"), optarg); 5964 1.1 christos break; 5965 1.8 christos 5966 1.7 christos case OPTION_GLOBALIZE_SYMBOLS: 5967 1.7 christos use_globalize = true; 5968 1.1 christos add_specific_symbols (optarg, globalize_specific_htab, 5969 1.1 christos &globalize_specific_buffer); 5970 1.1 christos break; 5971 1.8 christos 5972 1.7 christos case OPTION_KEEPGLOBAL_SYMBOLS: 5973 1.7 christos use_keep_global = true; 5974 1.1 christos add_specific_symbols (optarg, keepglobal_specific_htab, 5975 1.1 christos &keepglobal_specific_buffer); 5976 1.1 christos break; 5977 1.7 christos 5978 1.7 christos case OPTION_WEAKEN_SYMBOLS: 5979 1.1 christos add_specific_symbols (optarg, weaken_specific_htab, 5980 1.1 christos &weaken_specific_buffer); 5981 1.1 christos break; 5982 1.1 christos 5983 1.1 christos case OPTION_ALT_MACH_CODE: 5984 1.1 christos use_alt_mach_code = strtoul (optarg, NULL, 0); 5985 1.1 christos if (use_alt_mach_code == 0) 5986 1.1 christos fatal (_("unable to parse alternative machine code")); 5987 1.1 christos break; 5988 1.1 christos 5989 1.1 christos case OPTION_PREFIX_SYMBOLS: 5990 1.1 christos prefix_symbols_string = optarg; 5991 1.1 christos break; 5992 1.1 christos 5993 1.1 christos case OPTION_PREFIX_SECTIONS: 5994 1.1 christos prefix_sections_string = optarg; 5995 1.1 christos break; 5996 1.1 christos 5997 1.1 christos case OPTION_PREFIX_ALLOC_SECTIONS: 5998 1.1 christos prefix_alloc_sections_string = optarg; 5999 1.1 christos break; 6000 1.1 christos 6001 1.1 christos case OPTION_READONLY_TEXT: 6002 1.1 christos bfd_flags_to_set |= WP_TEXT; 6003 1.1 christos bfd_flags_to_clear &= ~WP_TEXT; 6004 1.1 christos break; 6005 1.1 christos 6006 1.1 christos case OPTION_WRITABLE_TEXT: 6007 1.1 christos bfd_flags_to_clear |= WP_TEXT; 6008 1.1 christos bfd_flags_to_set &= ~WP_TEXT; 6009 1.1 christos break; 6010 1.1 christos 6011 1.1 christos case OPTION_PURE: 6012 1.1 christos bfd_flags_to_set |= D_PAGED; 6013 1.1 christos bfd_flags_to_clear &= ~D_PAGED; 6014 1.1 christos break; 6015 1.1 christos 6016 1.1 christos case OPTION_IMPURE: 6017 1.1 christos bfd_flags_to_clear |= D_PAGED; 6018 1.1 christos bfd_flags_to_set &= ~D_PAGED; 6019 1.1 christos break; 6020 1.1 christos 6021 1.1 christos case OPTION_EXTRACT_DWO: 6022 1.1 christos strip_symbols = STRIP_NONDWO; 6023 1.1 christos break; 6024 1.8 christos 6025 1.1 christos case OPTION_EXTRACT_SYMBOL: 6026 1.1 christos extract_symbol = true; 6027 1.1 christos break; 6028 1.5 christos 6029 1.5 christos case OPTION_REVERSE_BYTES: 6030 1.1 christos { 6031 1.5 christos int prev = reverse_bytes; 6032 1.5 christos 6033 1.5 christos reverse_bytes = atoi (optarg); 6034 1.5 christos if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0)) 6035 1.5 christos fatal (_("number of bytes to reverse must be positive and even")); 6036 1.5 christos 6037 1.5 christos if (prev && prev != reverse_bytes) 6038 1.5 christos non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"), 6039 1.5 christos prev); 6040 1.1 christos break; 6041 1.1 christos } 6042 1.1 christos 6043 1.10 christos case OPTION_FILE_ALIGNMENT: 6044 1.10 christos pe_file_alignment = parse_vma (optarg, "--file-alignment"); 6045 1.10 christos if (power_of_two (pe_file_alignment) == -1) 6046 1.10 christos { 6047 1.10 christos non_fatal (_("--file-alignment argument is not a power of two: %s - ignoring"), optarg); 6048 1.1 christos pe_file_alignment = (bfd_vma) -1; 6049 1.1 christos } 6050 1.1 christos break; 6051 1.5 christos 6052 1.5 christos case OPTION_HEAP: 6053 1.5 christos { 6054 1.5 christos char *end; 6055 1.9 christos pe_heap_reserve = strtoul (optarg, &end, 0); 6056 1.5 christos if (end == optarg 6057 1.5 christos || (*end != ',' && *end != '\0')) 6058 1.5 christos non_fatal (_("%s: invalid reserve value for --heap"), 6059 1.5 christos optarg); 6060 1.5 christos else if (*end != '\0') 6061 1.5 christos { 6062 1.5 christos pe_heap_commit = strtoul (end + 1, &end, 0); 6063 1.5 christos if (*end != '\0') 6064 1.5 christos non_fatal (_("%s: invalid commit value for --heap"), 6065 1.5 christos optarg); 6066 1.1 christos } 6067 1.1 christos } 6068 1.1 christos break; 6069 1.1 christos 6070 1.1 christos case OPTION_IMAGE_BASE: 6071 1.1 christos pe_image_base = parse_vma (optarg, "--image-base"); 6072 1.7 christos break; 6073 1.1 christos 6074 1.1 christos case OPTION_PE_SECTION_ALIGNMENT: 6075 1.10 christos pe_section_alignment = parse_vma (optarg, 6076 1.10 christos "--section-alignment"); 6077 1.10 christos if (power_of_two (pe_section_alignment) == -1) 6078 1.10 christos { 6079 1.10 christos non_fatal (_("--section-alignment argument is not a power of two: %s - ignoring"), optarg); 6080 1.1 christos pe_section_alignment = (bfd_vma) -1; 6081 1.1 christos } 6082 1.1 christos break; 6083 1.1 christos 6084 1.1 christos case OPTION_SUBSYSTEM: 6085 1.1 christos set_pe_subsystem (optarg); 6086 1.1 christos break; 6087 1.5 christos 6088 1.5 christos case OPTION_STACK: 6089 1.5 christos { 6090 1.5 christos char *end; 6091 1.9 christos pe_stack_reserve = strtoul (optarg, &end, 0); 6092 1.5 christos if (end == optarg 6093 1.5 christos || (*end != ',' && *end != '\0')) 6094 1.5 christos non_fatal (_("%s: invalid reserve value for --stack"), 6095 1.5 christos optarg); 6096 1.5 christos else if (*end != '\0') 6097 1.5 christos { 6098 1.5 christos pe_stack_commit = strtoul (end + 1, &end, 0); 6099 1.5 christos if (*end != '\0') 6100 1.5 christos non_fatal (_("%s: invalid commit value for --stack"), 6101 1.5 christos optarg); 6102 1.1 christos } 6103 1.1 christos } 6104 1.7 christos break; 6105 1.7 christos 6106 1.9 christos case OPTION_VERILOG_DATA_WIDTH: 6107 1.9 christos VerilogDataWidth = parse_vma (optarg, "--verilog-data-width"); 6108 1.9 christos switch (VerilogDataWidth) 6109 1.9 christos { 6110 1.9 christos case 1: 6111 1.9 christos case 2: 6112 1.9 christos case 4: 6113 1.9 christos case 8: 6114 1.9 christos case 16: /* We do not support widths > 16 because the verilog 6115 1.9 christos data is handled internally in 16 byte wide packets. */ 6116 1.9 christos break; 6117 1.9 christos default: 6118 1.7 christos fatal (_("error: verilog data width must be 1, 2, 4, 8 or 16")); 6119 1.7 christos } 6120 1.1 christos break; 6121 1.1 christos 6122 1.1 christos case 0: 6123 1.1 christos /* We've been given a long option. */ 6124 1.1 christos break; 6125 1.1 christos 6126 1.1 christos case 'H': 6127 1.1 christos case 'h': 6128 1.1 christos copy_usage (stdout, 0); 6129 1.1 christos 6130 1.1 christos default: 6131 1.1 christos copy_usage (stderr, 1); 6132 1.1 christos } 6133 1.7 christos } 6134 1.7 christos 6135 1.7 christos if (use_globalize && use_keep_global) 6136 1.1 christos fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)")); 6137 1.1 christos 6138 1.1 christos if (formats_info) 6139 1.1 christos { 6140 1.1 christos display_info (); 6141 1.1 christos return 0; 6142 1.1 christos } 6143 1.1 christos 6144 1.1 christos if (show_version) 6145 1.1 christos print_version ("objcopy"); 6146 1.1 christos 6147 1.1 christos if (interleave && copy_byte == -1) 6148 1.1 christos fatal (_("interleave start byte must be set with --byte")); 6149 1.1 christos 6150 1.1 christos if (copy_byte >= interleave) 6151 1.1 christos fatal (_("byte number must be less than interleave")); 6152 1.1 christos 6153 1.1 christos if (copy_width > interleave - copy_byte) 6154 1.1 christos fatal (_("interleave width must be less than or equal to interleave - byte`")); 6155 1.1 christos 6156 1.1 christos if (optind == argc || optind + 2 < argc) 6157 1.1 christos copy_usage (stderr, 1); 6158 1.1 christos 6159 1.1 christos input_filename = argv[optind]; 6160 1.1 christos if (optind + 1 < argc) 6161 1.1 christos output_filename = argv[optind + 1]; 6162 1.1 christos 6163 1.1 christos default_deterministic (); 6164 1.1 christos 6165 1.1 christos /* Default is to strip no symbols. */ 6166 1.1 christos if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF) 6167 1.1 christos strip_symbols = STRIP_NONE; 6168 1.1 christos 6169 1.1 christos if (output_target == NULL) 6170 1.1 christos output_target = input_target; 6171 1.1 christos 6172 1.8 christos /* Convert input EFI target to PEI target. */ 6173 1.1 christos if (input_target != NULL 6174 1.8 christos && startswith (input_target, "efi-")) 6175 1.1 christos { 6176 1.1 christos if (convert_efi_target (&input_target) < 0) 6177 1.1 christos fatal (_("unknown input EFI target: %s"), input_target); 6178 1.1 christos } 6179 1.1 christos 6180 1.8 christos /* Convert output EFI target to PEI target. */ 6181 1.1 christos if (output_target != NULL 6182 1.8 christos && startswith (output_target, "efi-")) 6183 1.1 christos { 6184 1.8 christos int subsys = convert_efi_target (&output_target); 6185 1.1 christos 6186 1.8 christos if (subsys < 0) 6187 1.8 christos fatal (_("unknown output EFI target: %s"), output_target); 6188 1.1 christos if (pe_subsystem == -1) 6189 1.1 christos pe_subsystem = subsys; 6190 1.1 christos if (pe_file_alignment == (bfd_vma) -1) 6191 1.1 christos pe_file_alignment = PE_DEF_FILE_ALIGNMENT; 6192 1.1 christos if (pe_section_alignment == (bfd_vma) -1) 6193 1.1 christos pe_section_alignment = PE_DEF_SECTION_ALIGNMENT; 6194 1.1 christos } 6195 1.8 christos 6196 1.8 christos /* If there is no destination file, or the source and destination files 6197 1.1 christos are the same, then create a temp and copy the result into the input. */ 6198 1.1 christos copyfd = -1; 6199 1.8 christos if (output_filename == NULL 6200 1.8 christos || filename_cmp (input_filename, output_filename) == 0) 6201 1.8 christos { 6202 1.8 christos tmpname = make_tempname (input_filename, &tmpfd); 6203 1.8 christos if (tmpfd >= 0) 6204 1.1 christos copyfd = dup (tmpfd); 6205 1.1 christos } 6206 1.1 christos else 6207 1.1 christos tmpname = output_filename; 6208 1.8 christos 6209 1.8 christos if (tmpname == NULL) 6210 1.8 christos { 6211 1.8 christos fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"), 6212 1.1 christos input_filename, strerror (errno)); 6213 1.8 christos } 6214 1.8 christos 6215 1.1 christos copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target, 6216 1.1 christos output_target, input_arch); 6217 1.8 christos if (status == 0) 6218 1.8 christos { 6219 1.8 christos const char *oname = output_filename ? output_filename : input_filename; 6220 1.1 christos status = smart_rename (tmpname, oname, copyfd, 6221 1.1 christos &statbuf, preserve_dates) != 0; 6222 1.8 christos } 6223 1.8 christos else 6224 1.8 christos { 6225 1.8 christos if (copyfd >= 0) 6226 1.8 christos close (copyfd); 6227 1.1 christos unlink_if_ordinary (tmpname); 6228 1.6 christos } 6229 1.6 christos 6230 1.6 christos if (tmpname != output_filename) 6231 1.1 christos free (tmpname); 6232 1.1 christos 6233 1.3 christos if (change_warn) 6234 1.3 christos { 6235 1.1 christos struct section_list *p; 6236 1.1 christos 6237 1.1 christos for (p = change_sections; p != NULL; p = p->next) 6238 1.1 christos { 6239 1.3 christos if (! p->used) 6240 1.9 christos { 6241 1.9 christos if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA)) 6242 1.9 christos /* xgettext:c-format */ 6243 1.9 christos non_fatal (_("%s %s%c0x%" PRIx64 " never used"), 6244 1.9 christos "--change-section-vma", 6245 1.9 christos p->pattern, 6246 1.1 christos p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+', 6247 1.3 christos (uint64_t) p->vma_val); 6248 1.9 christos 6249 1.9 christos if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA)) 6250 1.9 christos /* xgettext:c-format */ 6251 1.9 christos non_fatal (_("%s %s%c0x%" PRIx64 " never used"), 6252 1.9 christos "--change-section-lma", 6253 1.9 christos p->pattern, 6254 1.1 christos p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+', 6255 1.1 christos (uint64_t) p->lma_val); 6256 1.1 christos } 6257 1.1 christos } 6258 1.8 christos } 6259 1.8 christos 6260 1.8 christos free (strip_specific_buffer); 6261 1.8 christos free (strip_unneeded_buffer); 6262 1.8 christos free (keep_specific_buffer); 6263 1.8 christos free (localize_specific_buffer); 6264 1.8 christos free (globalize_specific_buffer); 6265 1.7 christos free (keepglobal_specific_buffer); 6266 1.1 christos free (weaken_specific_buffer); 6267 1.1 christos 6268 1.1 christos return 0; 6269 1.1 christos } 6270 1.1 christos 6271 1.1 christos int 6272 1.8 christos main (int argc, char *argv[]) 6273 1.1 christos { 6274 1.1 christos #ifdef HAVE_LC_MESSAGES 6275 1.1 christos setlocale (LC_MESSAGES, ""); 6276 1.1 christos #endif 6277 1.1 christos setlocale (LC_CTYPE, ""); 6278 1.1 christos bindtextdomain (PACKAGE, LOCALEDIR); 6279 1.1 christos textdomain (PACKAGE); 6280 1.1 christos 6281 1.1 christos program_name = argv[0]; 6282 1.1 christos xmalloc_set_program_name (program_name); 6283 1.1 christos 6284 1.1 christos expandargv (&argc, &argv); 6285 1.1 christos 6286 1.1 christos strip_symbols = STRIP_UNDEF; 6287 1.7 christos discard_locals = LOCALS_UNDEF; 6288 1.7 christos 6289 1.1 christos if (bfd_init () != BFD_INIT_MAGIC) 6290 1.1 christos fatal (_("fatal error: libbfd ABI mismatch")); 6291 1.10 christos set_default_bfd_target (); 6292 1.1 christos 6293 1.1 christos #ifndef is_strip 6294 1.10 christos if (is_strip < 0) 6295 1.10 christos { 6296 1.1 christos size_t i = strlen (program_name); 6297 1.1 christos 6298 1.1 christos #ifdef HAVE_DOS_BASED_FILE_SYSTEM 6299 1.1 christos /* Drop the .exe suffix, if any. */ 6300 1.1 christos if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0) 6301 1.1 christos { 6302 1.1 christos i -= 4; 6303 1.1 christos program_name[i] = '\0'; 6304 1.1 christos } 6305 1.1 christos #endif 6306 1.10 christos is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0); 6307 1.1 christos } 6308 1.1 christos #endif /* is_strip */ 6309 1.9 christos 6310 1.1 christos create_symbol_htabs (); 6311 1.3 christos xatexit (delete_symbol_htabs); 6312 1.3 christos 6313 1.3 christos if (argv != NULL) 6314 1.1 christos bfd_set_error_program_name (argv[0]); 6315 1.1 christos 6316 1.1 christos if (is_strip) 6317 1.1 christos strip_main (argc, argv); 6318 1.1 christos else 6319 1.9 christos copy_main (argc, argv); 6320 1.1 christos 6321 1.1 christos xexit (status); 6322 return status; 6323 } 6324