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