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