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