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