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