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