Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* objcopy.c -- copy object file from input to output, optionally massaging it. |
2 | Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GNU Binutils. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | \f | |
22 | #include "bfd.h" | |
23 | #include "progress.h" | |
24 | #include "bucomm.h" | |
25 | #include "getopt.h" | |
26 | #include "libiberty.h" | |
27 | #include "budbg.h" | |
28 | #include <sys/stat.h> | |
29 | ||
30 | /* A list of symbols to explicitly strip out, or to keep. A linked | |
31 | list is good enough for a small number from the command line, but | |
32 | this will slow things down a lot if many symbols are being | |
33 | deleted. */ | |
34 | ||
35 | struct symlist | |
36 | { | |
37 | const char *name; | |
38 | struct symlist *next; | |
39 | }; | |
40 | ||
41 | static void copy_usage PARAMS ((FILE *, int)); | |
42 | static void strip_usage PARAMS ((FILE *, int)); | |
43 | static flagword parse_flags PARAMS ((const char *)); | |
44 | static struct section_list *find_section_list PARAMS ((const char *, boolean)); | |
45 | static void setup_section PARAMS ((bfd *, asection *, PTR)); | |
46 | static void copy_section PARAMS ((bfd *, asection *, PTR)); | |
47 | static void get_sections PARAMS ((bfd *, asection *, PTR)); | |
48 | static int compare_section_lma PARAMS ((const PTR, const PTR)); | |
49 | static void add_specific_symbol PARAMS ((const char *, struct symlist **)); | |
50 | static boolean is_specified_symbol PARAMS ((const char *, struct symlist *)); | |
51 | static boolean is_strip_section PARAMS ((bfd *, asection *)); | |
52 | static unsigned int filter_symbols | |
53 | PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long)); | |
54 | static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR)); | |
55 | static void filter_bytes PARAMS ((char *, bfd_size_type *)); | |
56 | static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***)); | |
57 | static void copy_object PARAMS ((bfd *, bfd *)); | |
58 | static void copy_archive PARAMS ((bfd *, bfd *, const char *)); | |
59 | static void copy_file | |
60 | PARAMS ((const char *, const char *, const char *, const char *)); | |
61 | static int strip_main PARAMS ((int, char **)); | |
62 | static int copy_main PARAMS ((int, char **)); | |
63 | ||
64 | #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;} | |
65 | ||
66 | static asymbol **isympp = NULL; /* Input symbols */ | |
67 | static asymbol **osympp = NULL; /* Output symbols that survive stripping */ | |
68 | ||
69 | /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */ | |
70 | static int copy_byte = -1; | |
71 | static int interleave = 4; | |
72 | ||
73 | static boolean verbose; /* Print file and target names. */ | |
74 | static boolean preserve_dates; /* Preserve input file timestamp. */ | |
75 | static int status = 0; /* Exit status. */ | |
76 | ||
77 | enum strip_action | |
78 | { | |
79 | STRIP_UNDEF, | |
80 | STRIP_NONE, /* don't strip */ | |
81 | STRIP_DEBUG, /* strip all debugger symbols */ | |
82 | STRIP_UNNEEDED, /* strip unnecessary symbols */ | |
83 | STRIP_ALL /* strip all symbols */ | |
84 | }; | |
85 | ||
86 | /* Which symbols to remove. */ | |
87 | static enum strip_action strip_symbols; | |
88 | ||
89 | enum locals_action | |
90 | { | |
91 | LOCALS_UNDEF, | |
92 | LOCALS_START_L, /* discard locals starting with L */ | |
93 | LOCALS_ALL /* discard all locals */ | |
94 | }; | |
95 | ||
96 | /* Which local symbols to remove. Overrides STRIP_ALL. */ | |
97 | static enum locals_action discard_locals; | |
98 | ||
99 | /* What kind of change to perform. */ | |
100 | enum change_action | |
101 | { | |
102 | CHANGE_IGNORE, | |
103 | CHANGE_MODIFY, | |
104 | CHANGE_SET | |
105 | }; | |
106 | ||
107 | /* Structure used to hold lists of sections and actions to take. */ | |
108 | struct section_list | |
109 | { | |
110 | struct section_list * next; /* Next section to change. */ | |
111 | const char * name; /* Section name. */ | |
112 | boolean used; /* Whether this entry was used. */ | |
113 | boolean remove; /* Whether to remove this section. */ | |
114 | enum change_action change_vma;/* Whether to change or set VMA. */ | |
115 | bfd_vma vma_val; /* Amount to change by or set to. */ | |
116 | enum change_action change_lma;/* Whether to change or set LMA. */ | |
117 | bfd_vma lma_val; /* Amount to change by or set to. */ | |
118 | boolean set_flags; /* Whether to set the section flags. */ | |
119 | flagword flags; /* What to set the section flags to. */ | |
120 | }; | |
121 | ||
122 | static struct section_list *change_sections; | |
123 | static boolean sections_removed; | |
124 | ||
125 | /* Changes to the start address. */ | |
126 | static bfd_vma change_start = 0; | |
127 | static boolean set_start_set = false; | |
128 | static bfd_vma set_start; | |
129 | ||
130 | /* Changes to section addresses. */ | |
131 | static bfd_vma change_section_address = 0; | |
132 | ||
133 | /* Filling gaps between sections. */ | |
134 | static boolean gap_fill_set = false; | |
135 | static bfd_byte gap_fill = 0; | |
136 | ||
137 | /* Pad to a given address. */ | |
138 | static boolean pad_to_set = false; | |
139 | static bfd_vma pad_to; | |
140 | ||
141 | /* List of sections to add. */ | |
142 | ||
143 | struct section_add | |
144 | { | |
145 | /* Next section to add. */ | |
146 | struct section_add *next; | |
147 | /* Name of section to add. */ | |
148 | const char *name; | |
149 | /* Name of file holding section contents. */ | |
150 | const char *filename; | |
151 | /* Size of file. */ | |
152 | size_t size; | |
153 | /* Contents of file. */ | |
154 | bfd_byte *contents; | |
155 | /* BFD section, after it has been added. */ | |
156 | asection *section; | |
157 | }; | |
158 | ||
159 | static struct section_add *add_sections; | |
160 | ||
161 | /* Whether to convert debugging information. */ | |
162 | ||
163 | static boolean convert_debugging = false; | |
164 | ||
165 | /* Whether to change the leading character in symbol names. */ | |
166 | ||
167 | static boolean change_leading_char = false; | |
168 | ||
169 | /* Whether to remove the leading character from global symbol names. */ | |
170 | ||
171 | static boolean remove_leading_char = false; | |
172 | ||
173 | /* List of symbols to strip, keep, localize, and weaken. */ | |
174 | ||
175 | static struct symlist *strip_specific_list = NULL; | |
176 | static struct symlist *keep_specific_list = NULL; | |
177 | static struct symlist *localize_specific_list = NULL; | |
178 | static struct symlist *weaken_specific_list = NULL; | |
179 | ||
180 | /* If this is true, we weaken global symbols (set BSF_WEAK). */ | |
181 | ||
182 | static boolean weaken = false; | |
183 | ||
184 | /* 150 isn't special; it's just an arbitrary non-ASCII char value. */ | |
185 | ||
186 | #define OPTION_ADD_SECTION 150 | |
187 | #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1) | |
188 | #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1) | |
189 | #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1) | |
190 | #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1) | |
191 | #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1) | |
192 | #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1) | |
193 | #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1) | |
194 | #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1) | |
195 | #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1) | |
196 | #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1) | |
197 | #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1) | |
198 | #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1) | |
199 | #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1) | |
200 | #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1) | |
201 | #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1) | |
202 | #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1) | |
203 | ||
204 | /* Options to handle if running as "strip". */ | |
205 | ||
206 | static struct option strip_options[] = | |
207 | { | |
208 | {"discard-all", no_argument, 0, 'x'}, | |
209 | {"discard-locals", no_argument, 0, 'X'}, | |
210 | {"format", required_argument, 0, 'F'}, /* Obsolete */ | |
211 | {"help", no_argument, 0, 'h'}, | |
212 | {"input-format", required_argument, 0, 'I'}, /* Obsolete */ | |
213 | {"input-target", required_argument, 0, 'I'}, | |
214 | {"keep-symbol", required_argument, 0, 'K'}, | |
215 | {"output-format", required_argument, 0, 'O'}, /* Obsolete */ | |
216 | {"output-target", required_argument, 0, 'O'}, | |
217 | {"preserve-dates", no_argument, 0, 'p'}, | |
218 | {"remove-section", required_argument, 0, 'R'}, | |
219 | {"strip-all", no_argument, 0, 's'}, | |
220 | {"strip-debug", no_argument, 0, 'S'}, | |
221 | {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED}, | |
222 | {"strip-symbol", required_argument, 0, 'N'}, | |
223 | {"target", required_argument, 0, 'F'}, | |
224 | {"verbose", no_argument, 0, 'v'}, | |
225 | {"version", no_argument, 0, 'V'}, | |
226 | {0, no_argument, 0, 0} | |
227 | }; | |
228 | ||
229 | /* Options to handle if running as "objcopy". */ | |
230 | ||
231 | static struct option copy_options[] = | |
232 | { | |
233 | {"add-section", required_argument, 0, OPTION_ADD_SECTION}, | |
234 | {"adjust-start", required_argument, 0, OPTION_CHANGE_START}, | |
235 | {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES}, | |
236 | {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS}, | |
237 | {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS}, | |
238 | {"byte", required_argument, 0, 'b'}, | |
239 | {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES}, | |
240 | {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR}, | |
241 | {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS}, | |
242 | {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA}, | |
243 | {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA}, | |
244 | {"change-start", required_argument, 0, OPTION_CHANGE_START}, | |
245 | {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS}, | |
246 | {"debugging", no_argument, 0, OPTION_DEBUGGING}, | |
247 | {"discard-all", no_argument, 0, 'x'}, | |
248 | {"discard-locals", no_argument, 0, 'X'}, | |
249 | {"format", required_argument, 0, 'F'}, /* Obsolete */ | |
250 | {"gap-fill", required_argument, 0, OPTION_GAP_FILL}, | |
251 | {"help", no_argument, 0, 'h'}, | |
252 | {"input-format", required_argument, 0, 'I'}, /* Obsolete */ | |
253 | {"input-target", required_argument, 0, 'I'}, | |
254 | {"interleave", required_argument, 0, 'i'}, | |
255 | {"keep-symbol", required_argument, 0, 'K'}, | |
256 | {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS}, | |
257 | {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS}, | |
258 | {"output-format", required_argument, 0, 'O'}, /* Obsolete */ | |
259 | {"output-target", required_argument, 0, 'O'}, | |
260 | {"pad-to", required_argument, 0, OPTION_PAD_TO}, | |
261 | {"preserve-dates", no_argument, 0, 'p'}, | |
262 | {"localize-symbol", required_argument, 0, 'L'}, | |
263 | {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR}, | |
264 | {"remove-section", required_argument, 0, 'R'}, | |
265 | {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS}, | |
266 | {"set-start", required_argument, 0, OPTION_SET_START}, | |
267 | {"strip-all", no_argument, 0, 'S'}, | |
268 | {"strip-debug", no_argument, 0, 'g'}, | |
269 | {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED}, | |
270 | {"strip-symbol", required_argument, 0, 'N'}, | |
271 | {"target", required_argument, 0, 'F'}, | |
272 | {"verbose", no_argument, 0, 'v'}, | |
273 | {"version", no_argument, 0, 'V'}, | |
274 | {"weaken", no_argument, 0, OPTION_WEAKEN}, | |
275 | {"weaken-symbol", required_argument, 0, 'W'}, | |
276 | {0, no_argument, 0, 0} | |
277 | }; | |
278 | ||
279 | /* IMPORTS */ | |
280 | extern char *program_name; | |
281 | ||
282 | /* This flag distinguishes between strip and objcopy: | |
283 | 1 means this is 'strip'; 0 means this is 'objcopy'. | |
284 | -1 means if we should use argv[0] to decide. */ | |
285 | extern int is_strip; | |
286 | ||
287 | ||
288 | static void | |
289 | copy_usage (stream, exit_status) | |
290 | FILE *stream; | |
291 | int exit_status; | |
292 | { | |
293 | fprintf (stream, _("\ | |
294 | Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\ | |
295 | [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\ | |
296 | [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\ | |
297 | [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\ | |
298 | [--discard-locals] [--debugging] [--remove-section=section]\n"), | |
299 | program_name); | |
300 | fprintf (stream, _("\ | |
301 | [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\ | |
302 | [--set-start=val] \n\ | |
303 | [--change-start=incr] [--change-addresses=incr] \n\ | |
304 | (--adjust-start and --adjust-vma are aliases for these two) \n\ | |
305 | [--change-section-address=section{=,+,-}val]\n\ | |
306 | (--adjust-section-vma is an alias for --change-section-address)\n\ | |
307 | [--change-section-lma=section{=,+,-}val]\n\ | |
308 | [--change-section-vma=section{=,+,-}val]\n\ | |
309 | [--adjust-warnings] [--no-adjust-warnings]\n\ | |
310 | [--change-warnings] [--no-change-warnings]\n\ | |
311 | [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\ | |
312 | [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\ | |
313 | [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\ | |
314 | [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\ | |
315 | [--verbose] [--version] [--help] in-file [out-file]\n")); | |
316 | list_supported_targets (program_name, stream); | |
317 | if (exit_status == 0) | |
318 | fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n")); | |
319 | exit (exit_status); | |
320 | } | |
321 | ||
322 | static void | |
323 | strip_usage (stream, exit_status) | |
324 | FILE *stream; | |
325 | int exit_status; | |
326 | { | |
327 | fprintf (stream, _("\ | |
328 | Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\ | |
329 | [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\ | |
330 | [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\ | |
331 | [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\ | |
332 | [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\ | |
333 | [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n"), | |
334 | program_name); | |
335 | list_supported_targets (program_name, stream); | |
336 | if (exit_status == 0) | |
337 | fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n")); | |
338 | exit (exit_status); | |
339 | } | |
340 | ||
341 | /* Parse section flags into a flagword, with a fatal error if the | |
342 | string can't be parsed. */ | |
343 | ||
344 | static flagword | |
345 | parse_flags (s) | |
346 | const char *s; | |
347 | { | |
348 | flagword ret; | |
349 | const char *snext; | |
350 | int len; | |
351 | ||
352 | ret = SEC_NO_FLAGS; | |
353 | ||
354 | do | |
355 | { | |
356 | snext = strchr (s, ','); | |
357 | if (snext == NULL) | |
358 | len = strlen (s); | |
359 | else | |
360 | { | |
361 | len = snext - s; | |
362 | ++snext; | |
363 | } | |
364 | ||
365 | if (0) ; | |
366 | #define PARSE_FLAG(fname,fval) \ | |
367 | else if (strncasecmp (fname, s, len) == 0) ret |= fval | |
368 | PARSE_FLAG ("alloc", SEC_ALLOC); | |
369 | PARSE_FLAG ("load", SEC_LOAD); | |
370 | PARSE_FLAG ("readonly", SEC_READONLY); | |
371 | PARSE_FLAG ("code", SEC_CODE); | |
372 | PARSE_FLAG ("data", SEC_DATA); | |
373 | PARSE_FLAG ("rom", SEC_ROM); | |
374 | PARSE_FLAG ("contents", SEC_HAS_CONTENTS); | |
375 | #undef PARSE_FLAG | |
376 | else | |
377 | { | |
378 | char *copy; | |
379 | ||
380 | copy = xmalloc (len + 1); | |
381 | strncpy (copy, s, len); | |
382 | copy[len] = '\0'; | |
383 | non_fatal (_("unrecognized section flag `%s'"), copy); | |
384 | fatal (_("supported flags: alloc, load, readonly, code, data, rom, contents")); | |
385 | } | |
386 | ||
387 | s = snext; | |
388 | } | |
389 | while (s != NULL); | |
390 | ||
391 | return ret; | |
392 | } | |
393 | ||
394 | /* Find and optionally add an entry in the change_sections list. */ | |
395 | ||
396 | static struct section_list * | |
397 | find_section_list (name, add) | |
398 | const char *name; | |
399 | boolean add; | |
400 | { | |
401 | register struct section_list *p; | |
402 | ||
403 | for (p = change_sections; p != NULL; p = p->next) | |
404 | if (strcmp (p->name, name) == 0) | |
405 | return p; | |
406 | ||
407 | if (! add) | |
408 | return NULL; | |
409 | ||
410 | p = (struct section_list *) xmalloc (sizeof (struct section_list)); | |
411 | p->name = name; | |
412 | p->used = false; | |
413 | p->remove = false; | |
414 | p->change_vma = CHANGE_IGNORE; | |
415 | p->change_lma = CHANGE_IGNORE; | |
416 | p->vma_val = 0; | |
417 | p->lma_val = 0; | |
418 | p->set_flags = false; | |
419 | p->flags = 0; | |
420 | ||
421 | p->next = change_sections; | |
422 | change_sections = p; | |
423 | ||
424 | return p; | |
425 | } | |
426 | ||
427 | /* Add a symbol to strip_specific_list. */ | |
428 | ||
429 | static void | |
430 | add_specific_symbol (name, list) | |
431 | const char *name; | |
432 | struct symlist **list; | |
433 | { | |
434 | struct symlist *tmp_list; | |
435 | ||
436 | tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist)); | |
437 | tmp_list->name = name; | |
438 | tmp_list->next = *list; | |
439 | *list = tmp_list; | |
440 | } | |
441 | ||
442 | /* See whether a symbol should be stripped or kept based on | |
443 | strip_specific_list and keep_symbols. */ | |
444 | ||
445 | static boolean | |
446 | is_specified_symbol (name, list) | |
447 | const char *name; | |
448 | struct symlist *list; | |
449 | { | |
450 | struct symlist *tmp_list; | |
451 | ||
452 | for (tmp_list = list; tmp_list; tmp_list = tmp_list->next) | |
453 | { | |
454 | if (strcmp (name, tmp_list->name) == 0) | |
455 | return true; | |
456 | } | |
457 | return false; | |
458 | } | |
459 | ||
460 | /* See if a section is being removed. */ | |
461 | ||
462 | static boolean | |
463 | is_strip_section (abfd, sec) | |
464 | bfd *abfd; | |
465 | asection *sec; | |
466 | { | |
467 | struct section_list *p; | |
468 | ||
469 | if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0 | |
470 | && (strip_symbols == STRIP_DEBUG | |
471 | || strip_symbols == STRIP_UNNEEDED | |
472 | || strip_symbols == STRIP_ALL | |
473 | || discard_locals == LOCALS_ALL | |
474 | || convert_debugging)) | |
475 | return true; | |
476 | ||
477 | if (! sections_removed) | |
478 | return false; | |
479 | p = find_section_list (bfd_get_section_name (abfd, sec), false); | |
480 | return p != NULL && p->remove ? true : false; | |
481 | } | |
482 | ||
483 | /* Choose which symbol entries to copy; put the result in OSYMS. | |
484 | We don't copy in place, because that confuses the relocs. | |
485 | Return the number of symbols to print. */ | |
486 | ||
487 | static unsigned int | |
488 | filter_symbols (abfd, obfd, osyms, isyms, symcount) | |
489 | bfd *abfd; | |
490 | bfd *obfd; | |
491 | asymbol **osyms, **isyms; | |
492 | long symcount; | |
493 | { | |
494 | register asymbol **from = isyms, **to = osyms; | |
495 | long src_count = 0, dst_count = 0; | |
496 | ||
497 | for (; src_count < symcount; src_count++) | |
498 | { | |
499 | asymbol *sym = from[src_count]; | |
500 | flagword flags = sym->flags; | |
501 | const char *name = bfd_asymbol_name (sym); | |
502 | int keep; | |
503 | ||
504 | if (change_leading_char | |
505 | && (bfd_get_symbol_leading_char (abfd) | |
506 | != bfd_get_symbol_leading_char (obfd)) | |
507 | && (bfd_get_symbol_leading_char (abfd) == '\0' | |
508 | || (name[0] == bfd_get_symbol_leading_char (abfd)))) | |
509 | { | |
510 | if (bfd_get_symbol_leading_char (obfd) == '\0') | |
511 | name = bfd_asymbol_name (sym) = name + 1; | |
512 | else | |
513 | { | |
514 | char *n; | |
515 | ||
516 | n = xmalloc (strlen (name) + 2); | |
517 | n[0] = bfd_get_symbol_leading_char (obfd); | |
518 | if (bfd_get_symbol_leading_char (abfd) == '\0') | |
519 | strcpy (n + 1, name); | |
520 | else | |
521 | strcpy (n + 1, name + 1); | |
522 | name = bfd_asymbol_name (sym) = n; | |
523 | } | |
524 | } | |
525 | ||
526 | if (remove_leading_char | |
527 | && ((flags & BSF_GLOBAL) != 0 | |
528 | || (flags & BSF_WEAK) != 0 | |
529 | || bfd_is_und_section (bfd_get_section (sym)) | |
530 | || bfd_is_com_section (bfd_get_section (sym))) | |
531 | && name[0] == bfd_get_symbol_leading_char (abfd)) | |
532 | name = bfd_asymbol_name (sym) = name + 1; | |
533 | ||
534 | if (strip_symbols == STRIP_ALL) | |
535 | keep = 0; | |
536 | else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */ | |
537 | || ((flags & BSF_SECTION_SYM) != 0 | |
538 | && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags | |
539 | & BSF_KEEP) != 0)) | |
540 | keep = 1; | |
541 | else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */ | |
542 | || (flags & BSF_WEAK) != 0 | |
543 | || bfd_is_und_section (bfd_get_section (sym)) | |
544 | || bfd_is_com_section (bfd_get_section (sym))) | |
545 | keep = strip_symbols != STRIP_UNNEEDED; | |
546 | else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */ | |
547 | keep = (strip_symbols != STRIP_DEBUG | |
548 | && strip_symbols != STRIP_UNNEEDED | |
549 | && ! convert_debugging); | |
550 | else /* Local symbol. */ | |
551 | keep = (strip_symbols != STRIP_UNNEEDED | |
552 | && (discard_locals != LOCALS_ALL | |
553 | && (discard_locals != LOCALS_START_L | |
554 | || ! bfd_is_local_label (abfd, sym)))); | |
555 | ||
556 | if (keep && is_specified_symbol (name, strip_specific_list)) | |
557 | keep = 0; | |
558 | if (!keep && is_specified_symbol (name, keep_specific_list)) | |
559 | keep = 1; | |
560 | if (keep && is_strip_section (abfd, bfd_get_section (sym))) | |
561 | keep = 0; | |
562 | ||
563 | if (keep && (flags & BSF_GLOBAL) != 0 | |
564 | && (weaken || is_specified_symbol (name, weaken_specific_list))) | |
565 | { | |
566 | sym->flags &=~ BSF_GLOBAL; | |
567 | sym->flags |= BSF_WEAK; | |
568 | } | |
569 | if (keep && (flags & (BSF_GLOBAL | BSF_WEAK)) | |
570 | && is_specified_symbol (name, localize_specific_list)) | |
571 | { | |
572 | sym->flags &= ~(BSF_GLOBAL | BSF_WEAK); | |
573 | sym->flags |= BSF_LOCAL; | |
574 | } | |
575 | ||
576 | if (keep) | |
577 | to[dst_count++] = sym; | |
578 | } | |
579 | ||
580 | to[dst_count] = NULL; | |
581 | ||
582 | return dst_count; | |
583 | } | |
584 | ||
585 | /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long. | |
586 | Adjust *SIZE. */ | |
587 | ||
588 | static void | |
589 | filter_bytes (memhunk, size) | |
590 | char *memhunk; | |
591 | bfd_size_type *size; | |
592 | { | |
593 | char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size; | |
594 | ||
595 | for (; from < end; from += interleave) | |
596 | *to++ = *from; | |
597 | if (*size % interleave > copy_byte) | |
598 | *size = (*size / interleave) + 1; | |
599 | else | |
600 | *size /= interleave; | |
601 | } | |
602 | ||
603 | /* Copy object file IBFD onto OBFD. */ | |
604 | ||
605 | static void | |
606 | copy_object (ibfd, obfd) | |
607 | bfd *ibfd; | |
608 | bfd *obfd; | |
609 | { | |
610 | bfd_vma start; | |
611 | long symcount; | |
612 | asection **osections = NULL; | |
613 | bfd_size_type *gaps = NULL; | |
614 | bfd_size_type max_gap = 0; | |
615 | long symsize; | |
616 | PTR dhandle; | |
617 | ||
618 | ||
619 | if (!bfd_set_format (obfd, bfd_get_format (ibfd))) | |
620 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
621 | ||
622 | if (verbose) | |
623 | printf (_("copy from %s(%s) to %s(%s)\n"), | |
624 | bfd_get_filename (ibfd), bfd_get_target (ibfd), | |
625 | bfd_get_filename (obfd), bfd_get_target (obfd)); | |
626 | ||
627 | if (set_start_set) | |
628 | start = set_start; | |
629 | else | |
630 | start = bfd_get_start_address (ibfd); | |
631 | start += change_start; | |
632 | ||
633 | if (!bfd_set_start_address (obfd, start) | |
634 | || !bfd_set_file_flags (obfd, | |
635 | (bfd_get_file_flags (ibfd) | |
636 | & bfd_applicable_file_flags (obfd)))) | |
637 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
638 | ||
639 | /* Copy architecture of input file to output file */ | |
640 | if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), | |
641 | bfd_get_mach (ibfd))) | |
642 | non_fatal (_("Warning: Output file cannot represent architecture %s"), | |
643 | bfd_printable_arch_mach (bfd_get_arch (ibfd), | |
644 | bfd_get_mach (ibfd))); | |
645 | ||
646 | if (!bfd_set_format (obfd, bfd_get_format (ibfd))) | |
647 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
648 | ||
649 | if (isympp) | |
650 | free (isympp); | |
651 | ||
652 | if (osympp != isympp) | |
653 | free (osympp); | |
654 | ||
655 | /* BFD mandates that all output sections be created and sizes set before | |
656 | any output is done. Thus, we traverse all sections multiple times. */ | |
657 | bfd_map_over_sections (ibfd, setup_section, (void *) obfd); | |
658 | ||
659 | if (add_sections != NULL) | |
660 | { | |
661 | struct section_add *padd; | |
662 | struct section_list *pset; | |
663 | ||
664 | for (padd = add_sections; padd != NULL; padd = padd->next) | |
665 | { | |
666 | padd->section = bfd_make_section (obfd, padd->name); | |
667 | if (padd->section == NULL) | |
668 | { | |
669 | non_fatal (_("can't create section `%s': %s"), | |
670 | padd->name, bfd_errmsg (bfd_get_error ())); | |
671 | status = 1; | |
672 | return; | |
673 | } | |
674 | else | |
675 | { | |
676 | flagword flags; | |
677 | ||
678 | if (! bfd_set_section_size (obfd, padd->section, padd->size)) | |
679 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
680 | ||
681 | pset = find_section_list (padd->name, false); | |
682 | if (pset != NULL) | |
683 | pset->used = true; | |
684 | ||
685 | if (pset != NULL && pset->set_flags) | |
686 | flags = pset->flags | SEC_HAS_CONTENTS; | |
687 | else | |
688 | flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA; | |
689 | ||
690 | if (! bfd_set_section_flags (obfd, padd->section, flags)) | |
691 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
692 | ||
693 | if (pset != NULL) | |
694 | { | |
695 | if (pset->change_vma != CHANGE_IGNORE) | |
696 | if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val)) | |
697 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
698 | ||
699 | if (pset->change_lma != CHANGE_IGNORE) | |
700 | { | |
701 | padd->section->lma = pset->lma_val; | |
702 | ||
703 | if (! bfd_set_section_alignment | |
704 | (obfd, padd->section, | |
705 | bfd_section_alignment (obfd, padd->section))) | |
706 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
707 | } | |
708 | } | |
709 | } | |
710 | } | |
711 | } | |
712 | ||
713 | if (gap_fill_set || pad_to_set) | |
714 | { | |
715 | asection **set; | |
716 | unsigned int c, i; | |
717 | ||
718 | /* We must fill in gaps between the sections and/or we must pad | |
719 | the last section to a specified address. We do this by | |
720 | grabbing a list of the sections, sorting them by VMA, and | |
721 | increasing the section sizes as required to fill the gaps. | |
722 | We write out the gap contents below. */ | |
723 | ||
724 | c = bfd_count_sections (obfd); | |
725 | osections = (asection **) xmalloc (c * sizeof (asection *)); | |
726 | set = osections; | |
727 | bfd_map_over_sections (obfd, get_sections, (void *) &set); | |
728 | ||
729 | qsort (osections, c, sizeof (asection *), compare_section_lma); | |
730 | ||
731 | gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type)); | |
732 | memset (gaps, 0, c * sizeof (bfd_size_type)); | |
733 | ||
734 | if (gap_fill_set) | |
735 | { | |
736 | for (i = 0; i < c - 1; i++) | |
737 | { | |
738 | flagword flags; | |
739 | bfd_size_type size; | |
740 | bfd_vma gap_start, gap_stop; | |
741 | ||
742 | flags = bfd_get_section_flags (obfd, osections[i]); | |
743 | if ((flags & SEC_HAS_CONTENTS) == 0 | |
744 | || (flags & SEC_LOAD) == 0) | |
745 | continue; | |
746 | ||
747 | size = bfd_section_size (obfd, osections[i]); | |
748 | gap_start = bfd_section_lma (obfd, osections[i]) + size; | |
749 | gap_stop = bfd_section_lma (obfd, osections[i + 1]); | |
750 | if (gap_start < gap_stop) | |
751 | { | |
752 | if (! bfd_set_section_size (obfd, osections[i], | |
753 | size + (gap_stop - gap_start))) | |
754 | { | |
755 | non_fatal (_("Can't fill gap after %s: %s"), | |
756 | bfd_get_section_name (obfd, osections[i]), | |
757 | bfd_errmsg (bfd_get_error ())); | |
758 | status = 1; | |
759 | break; | |
760 | } | |
761 | gaps[i] = gap_stop - gap_start; | |
762 | if (max_gap < gap_stop - gap_start) | |
763 | max_gap = gap_stop - gap_start; | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
768 | if (pad_to_set) | |
769 | { | |
770 | bfd_vma lma; | |
771 | bfd_size_type size; | |
772 | ||
773 | lma = bfd_section_lma (obfd, osections[c - 1]); | |
774 | size = bfd_section_size (obfd, osections[c - 1]); | |
775 | if (lma + size < pad_to) | |
776 | { | |
777 | if (! bfd_set_section_size (obfd, osections[c - 1], | |
778 | pad_to - lma)) | |
779 | { | |
780 | non_fatal (_("Can't add padding to %s: %s"), | |
781 | bfd_get_section_name (obfd, osections[c - 1]), | |
782 | bfd_errmsg (bfd_get_error ())); | |
783 | status = 1; | |
784 | } | |
785 | else | |
786 | { | |
787 | gaps[c - 1] = pad_to - (lma + size); | |
788 | if (max_gap < pad_to - (lma + size)) | |
789 | max_gap = pad_to - (lma + size); | |
790 | } | |
791 | } | |
792 | } | |
793 | } | |
794 | ||
795 | /* Symbol filtering must happen after the output sections have | |
796 | been created, but before their contents are set. */ | |
797 | dhandle = NULL; | |
798 | symsize = bfd_get_symtab_upper_bound (ibfd); | |
799 | if (symsize < 0) | |
800 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
801 | ||
802 | osympp = isympp = (asymbol **) xmalloc (symsize); | |
803 | symcount = bfd_canonicalize_symtab (ibfd, isympp); | |
804 | if (symcount < 0) | |
805 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
806 | ||
807 | if (convert_debugging) | |
808 | dhandle = read_debugging_info (ibfd, isympp, symcount); | |
809 | ||
810 | if (strip_symbols == STRIP_DEBUG | |
811 | || strip_symbols == STRIP_ALL | |
812 | || strip_symbols == STRIP_UNNEEDED | |
813 | || discard_locals != LOCALS_UNDEF | |
814 | || strip_specific_list != NULL | |
815 | || keep_specific_list != NULL | |
816 | || localize_specific_list != NULL | |
817 | || weaken_specific_list != NULL | |
818 | || sections_removed | |
819 | || convert_debugging | |
820 | || change_leading_char | |
821 | || remove_leading_char | |
822 | || weaken) | |
823 | { | |
824 | /* Mark symbols used in output relocations so that they | |
825 | are kept, even if they are local labels or static symbols. | |
826 | ||
827 | Note we iterate over the input sections examining their | |
828 | relocations since the relocations for the output sections | |
829 | haven't been set yet. mark_symbols_used_in_relocations will | |
830 | ignore input sections which have no corresponding output | |
831 | section. */ | |
832 | if (strip_symbols != STRIP_ALL) | |
833 | bfd_map_over_sections (ibfd, | |
834 | mark_symbols_used_in_relocations, | |
835 | (PTR)isympp); | |
836 | osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *)); | |
837 | symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount); | |
838 | } | |
839 | ||
840 | if (convert_debugging && dhandle != NULL) | |
841 | { | |
842 | if (! write_debugging_info (obfd, dhandle, &symcount, &osympp)) | |
843 | { | |
844 | status = 1; | |
845 | return; | |
846 | } | |
847 | } | |
848 | ||
849 | bfd_set_symtab (obfd, osympp, symcount); | |
850 | ||
851 | /* This has to happen after the symbol table has been set. */ | |
852 | bfd_map_over_sections (ibfd, copy_section, (void *) obfd); | |
853 | ||
854 | if (add_sections != NULL) | |
855 | { | |
856 | struct section_add *padd; | |
857 | ||
858 | for (padd = add_sections; padd != NULL; padd = padd->next) | |
859 | { | |
860 | if (! bfd_set_section_contents (obfd, padd->section, | |
861 | (PTR) padd->contents, | |
862 | (file_ptr) 0, | |
863 | (bfd_size_type) padd->size)) | |
864 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
865 | } | |
866 | } | |
867 | ||
868 | if (gap_fill_set || pad_to_set) | |
869 | { | |
870 | bfd_byte *buf; | |
871 | int c, i; | |
872 | ||
873 | /* Fill in the gaps. */ | |
874 | ||
875 | if (max_gap > 8192) | |
876 | max_gap = 8192; | |
877 | buf = (bfd_byte *) xmalloc (max_gap); | |
878 | memset (buf, gap_fill, (size_t) max_gap); | |
879 | ||
880 | c = bfd_count_sections (obfd); | |
881 | for (i = 0; i < c; i++) | |
882 | { | |
883 | if (gaps[i] != 0) | |
884 | { | |
885 | bfd_size_type left; | |
886 | file_ptr off; | |
887 | ||
888 | left = gaps[i]; | |
889 | off = bfd_section_size (obfd, osections[i]) - left; | |
890 | while (left > 0) | |
891 | { | |
892 | bfd_size_type now; | |
893 | ||
894 | if (left > 8192) | |
895 | now = 8192; | |
896 | else | |
897 | now = left; | |
898 | ||
899 | if (! bfd_set_section_contents (obfd, osections[i], buf, | |
900 | off, now)) | |
901 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
902 | ||
903 | left -= now; | |
904 | off += now; | |
905 | } | |
906 | } | |
907 | } | |
908 | } | |
909 | ||
910 | /* Allow the BFD backend to copy any private data it understands | |
911 | from the input BFD to the output BFD. This is done last to | |
912 | permit the routine to look at the filtered symbol table, which is | |
913 | important for the ECOFF code at least. */ | |
914 | if (!bfd_copy_private_bfd_data (ibfd, obfd)) | |
915 | { | |
916 | non_fatal (_("%s: error copying private BFD data: %s"), | |
917 | bfd_get_filename (obfd), | |
918 | bfd_errmsg (bfd_get_error ())); | |
919 | status = 1; | |
920 | return; | |
921 | } | |
922 | } | |
923 | ||
924 | /* Read each archive element in turn from IBFD, copy the | |
925 | contents to temp file, and keep the temp file handle. */ | |
926 | ||
927 | static void | |
928 | copy_archive (ibfd, obfd, output_target) | |
929 | bfd *ibfd; | |
930 | bfd *obfd; | |
931 | const char *output_target; | |
932 | { | |
933 | struct name_list | |
934 | { | |
935 | struct name_list *next; | |
936 | char *name; | |
937 | bfd *obfd; | |
938 | } *list, *l; | |
939 | bfd **ptr = &obfd->archive_head; | |
940 | bfd *this_element; | |
941 | char *dir = make_tempname (bfd_get_filename (obfd)); | |
942 | ||
943 | /* Make a temp directory to hold the contents. */ | |
944 | #if defined (_WIN32) && !defined (__CYGWIN32__) | |
945 | if (mkdir (dir) != 0) | |
946 | #else | |
947 | if (mkdir (dir, 0700) != 0) | |
948 | #endif | |
949 | { | |
950 | fatal (_("cannot mkdir %s for archive copying (error: %s)"), | |
951 | dir, strerror (errno)); | |
952 | } | |
953 | obfd->has_armap = ibfd->has_armap; | |
954 | ||
955 | list = NULL; | |
956 | ||
957 | this_element = bfd_openr_next_archived_file (ibfd, NULL); | |
958 | while (!status && this_element != (bfd *) NULL) | |
959 | { | |
960 | /* Create an output file for this member. */ | |
961 | char *output_name = concat (dir, "/", bfd_get_filename (this_element), | |
962 | (char *) NULL); | |
963 | bfd *output_bfd = bfd_openw (output_name, output_target); | |
964 | bfd *last_element; | |
965 | ||
966 | l = (struct name_list *) xmalloc (sizeof (struct name_list)); | |
967 | l->name = output_name; | |
968 | l->next = list; | |
969 | list = l; | |
970 | ||
971 | if (output_bfd == (bfd *) NULL) | |
972 | RETURN_NONFATAL (output_name); | |
973 | ||
974 | if (!bfd_set_format (obfd, bfd_get_format (ibfd))) | |
975 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
976 | ||
977 | if (bfd_check_format (this_element, bfd_object) == true) | |
978 | copy_object (this_element, output_bfd); | |
979 | ||
980 | if (!bfd_close (output_bfd)) | |
981 | { | |
982 | bfd_nonfatal (bfd_get_filename (output_bfd)); | |
983 | /* Error in new object file. Don't change archive. */ | |
984 | status = 1; | |
985 | } | |
986 | ||
987 | /* Open the newly output file and attach to our list. */ | |
988 | output_bfd = bfd_openr (output_name, output_target); | |
989 | ||
990 | l->obfd = output_bfd; | |
991 | ||
992 | *ptr = output_bfd; | |
993 | ptr = &output_bfd->next; | |
994 | ||
995 | last_element = this_element; | |
996 | ||
997 | this_element = bfd_openr_next_archived_file (ibfd, last_element); | |
998 | ||
999 | bfd_close (last_element); | |
1000 | } | |
1001 | *ptr = (bfd *) NULL; | |
1002 | ||
1003 | if (!bfd_close (obfd)) | |
1004 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
1005 | ||
1006 | if (!bfd_close (ibfd)) | |
1007 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
1008 | ||
1009 | /* Delete all the files that we opened. */ | |
1010 | for (l = list; l != NULL; l = l->next) | |
1011 | { | |
1012 | bfd_close (l->obfd); | |
1013 | unlink (l->name); | |
1014 | } | |
1015 | rmdir (dir); | |
1016 | } | |
1017 | ||
1018 | /* The top-level control. */ | |
1019 | ||
1020 | static void | |
1021 | copy_file (input_filename, output_filename, input_target, output_target) | |
1022 | const char *input_filename; | |
1023 | const char *output_filename; | |
1024 | const char *input_target; | |
1025 | const char *output_target; | |
1026 | { | |
1027 | bfd *ibfd; | |
1028 | char **matching; | |
1029 | ||
1030 | /* To allow us to do "strip *" without dying on the first | |
1031 | non-object file, failures are nonfatal. */ | |
1032 | ||
1033 | ibfd = bfd_openr (input_filename, input_target); | |
1034 | if (ibfd == NULL) | |
1035 | RETURN_NONFATAL (input_filename); | |
1036 | ||
1037 | if (bfd_check_format (ibfd, bfd_archive)) | |
1038 | { | |
1039 | bfd *obfd; | |
1040 | ||
1041 | /* bfd_get_target does not return the correct value until | |
1042 | bfd_check_format succeeds. */ | |
1043 | if (output_target == NULL) | |
1044 | output_target = bfd_get_target (ibfd); | |
1045 | ||
1046 | obfd = bfd_openw (output_filename, output_target); | |
1047 | if (obfd == NULL) | |
1048 | RETURN_NONFATAL (output_filename); | |
1049 | ||
1050 | copy_archive (ibfd, obfd, output_target); | |
1051 | } | |
1052 | else if (bfd_check_format_matches (ibfd, bfd_object, &matching)) | |
1053 | { | |
1054 | bfd *obfd; | |
1055 | ||
1056 | /* bfd_get_target does not return the correct value until | |
1057 | bfd_check_format succeeds. */ | |
1058 | if (output_target == NULL) | |
1059 | output_target = bfd_get_target (ibfd); | |
1060 | ||
1061 | obfd = bfd_openw (output_filename, output_target); | |
1062 | if (obfd == NULL) | |
1063 | RETURN_NONFATAL (output_filename); | |
1064 | ||
1065 | copy_object (ibfd, obfd); | |
1066 | ||
1067 | if (!bfd_close (obfd)) | |
1068 | RETURN_NONFATAL (output_filename); | |
1069 | ||
1070 | if (!bfd_close (ibfd)) | |
1071 | RETURN_NONFATAL (input_filename); | |
1072 | } | |
1073 | else | |
1074 | { | |
1075 | bfd_nonfatal (input_filename); | |
1076 | ||
1077 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) | |
1078 | { | |
1079 | list_matching_formats (matching); | |
1080 | free (matching); | |
1081 | } | |
1082 | ||
1083 | status = 1; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | /* Create a section in OBFD with the same name and attributes | |
1088 | as ISECTION in IBFD. */ | |
1089 | ||
1090 | static void | |
1091 | setup_section (ibfd, isection, obfdarg) | |
1092 | bfd *ibfd; | |
1093 | sec_ptr isection; | |
1094 | PTR obfdarg; | |
1095 | { | |
1096 | bfd *obfd = (bfd *) obfdarg; | |
1097 | struct section_list *p; | |
1098 | sec_ptr osection; | |
1099 | bfd_size_type size; | |
1100 | bfd_vma vma; | |
1101 | bfd_vma lma; | |
1102 | flagword flags; | |
1103 | char *err; | |
1104 | ||
1105 | if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0 | |
1106 | && (strip_symbols == STRIP_DEBUG | |
1107 | || strip_symbols == STRIP_UNNEEDED | |
1108 | || strip_symbols == STRIP_ALL | |
1109 | || discard_locals == LOCALS_ALL | |
1110 | || convert_debugging)) | |
1111 | return; | |
1112 | ||
1113 | p = find_section_list (bfd_section_name (ibfd, isection), false); | |
1114 | if (p != NULL) | |
1115 | p->used = true; | |
1116 | ||
1117 | if (p != NULL && p->remove) | |
1118 | return; | |
1119 | ||
1120 | osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection)); | |
1121 | ||
1122 | if (osection == NULL) | |
1123 | { | |
1124 | err = "making"; | |
1125 | goto loser; | |
1126 | } | |
1127 | ||
1128 | size = bfd_section_size (ibfd, isection); | |
1129 | if (copy_byte >= 0) | |
1130 | size = (size + interleave - 1) / interleave; | |
1131 | if (! bfd_set_section_size (obfd, osection, size)) | |
1132 | { | |
1133 | err = "size"; | |
1134 | goto loser; | |
1135 | } | |
1136 | ||
1137 | vma = bfd_section_vma (ibfd, isection); | |
1138 | if (p != NULL && p->change_vma == CHANGE_MODIFY) | |
1139 | vma += p->vma_val; | |
1140 | else if (p != NULL && p->change_vma == CHANGE_SET) | |
1141 | vma = p->vma_val; | |
1142 | else | |
1143 | vma += change_section_address; | |
1144 | ||
1145 | if (! bfd_set_section_vma (obfd, osection, vma)) | |
1146 | { | |
1147 | err = "vma"; | |
1148 | goto loser; | |
1149 | } | |
1150 | ||
1151 | lma = isection->lma; | |
1152 | if ((p != NULL) && p->change_lma != CHANGE_IGNORE) | |
1153 | { | |
1154 | if (p->change_lma == CHANGE_MODIFY) | |
1155 | lma += p->lma_val; | |
1156 | else if (p->change_lma == CHANGE_SET) | |
1157 | lma = p->lma_val; | |
1158 | else | |
1159 | abort (); | |
1160 | } | |
1161 | else | |
1162 | lma += change_section_address; | |
1163 | ||
1164 | osection->lma = lma; | |
1165 | ||
1166 | /* FIXME: This is probably not enough. If we change the LMA we | |
1167 | may have to recompute the header for the file as well. */ | |
1168 | if (bfd_set_section_alignment (obfd, | |
1169 | osection, | |
1170 | bfd_section_alignment (ibfd, isection)) | |
1171 | == false) | |
1172 | { | |
1173 | err = "alignment"; | |
1174 | goto loser; | |
1175 | } | |
1176 | ||
1177 | flags = bfd_get_section_flags (ibfd, isection); | |
1178 | if (p != NULL && p->set_flags) | |
1179 | flags = p->flags | (flags & SEC_HAS_CONTENTS); | |
1180 | if (!bfd_set_section_flags (obfd, osection, flags)) | |
1181 | { | |
1182 | err = "flags"; | |
1183 | goto loser; | |
1184 | } | |
1185 | ||
1186 | /* This used to be mangle_section; we do here to avoid using | |
1187 | bfd_get_section_by_name since some formats allow multiple | |
1188 | sections with the same name. */ | |
1189 | isection->output_section = osection; | |
1190 | isection->output_offset = 0; | |
1191 | ||
1192 | /* Allow the BFD backend to copy any private data it understands | |
1193 | from the input section to the output section. */ | |
1194 | if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection)) | |
1195 | { | |
1196 | err = "private data"; | |
1197 | goto loser; | |
1198 | } | |
1199 | ||
1200 | /* All went well */ | |
1201 | return; | |
1202 | ||
1203 | loser: | |
1204 | non_fatal (_("%s: section `%s': error in %s: %s"), | |
1205 | bfd_get_filename (ibfd), | |
1206 | bfd_section_name (ibfd, isection), | |
1207 | err, bfd_errmsg (bfd_get_error ())); | |
1208 | status = 1; | |
1209 | } | |
1210 | ||
1211 | /* Copy the data of input section ISECTION of IBFD | |
1212 | to an output section with the same name in OBFD. | |
1213 | If stripping then don't copy any relocation info. */ | |
1214 | ||
1215 | static void | |
1216 | copy_section (ibfd, isection, obfdarg) | |
1217 | bfd *ibfd; | |
1218 | sec_ptr isection; | |
1219 | PTR obfdarg; | |
1220 | { | |
1221 | bfd *obfd = (bfd *) obfdarg; | |
1222 | struct section_list *p; | |
1223 | arelent **relpp; | |
1224 | long relcount; | |
1225 | sec_ptr osection; | |
1226 | bfd_size_type size; | |
1227 | long relsize; | |
1228 | ||
1229 | /* If we have already failed earlier on, do not keep on generating | |
1230 | complaints now. */ | |
1231 | if (status != 0) | |
1232 | return; | |
1233 | ||
1234 | if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0 | |
1235 | && (strip_symbols == STRIP_DEBUG | |
1236 | || strip_symbols == STRIP_UNNEEDED | |
1237 | || strip_symbols == STRIP_ALL | |
1238 | || discard_locals == LOCALS_ALL | |
1239 | || convert_debugging)) | |
1240 | { | |
1241 | return; | |
1242 | } | |
1243 | ||
1244 | p = find_section_list (bfd_section_name (ibfd, isection), false); | |
1245 | ||
1246 | if (p != NULL && p->remove) | |
1247 | return; | |
1248 | ||
1249 | osection = isection->output_section; | |
1250 | size = bfd_get_section_size_before_reloc (isection); | |
1251 | ||
1252 | if (size == 0 || osection == 0) | |
1253 | return; | |
1254 | ||
1255 | ||
1256 | relsize = bfd_get_reloc_upper_bound (ibfd, isection); | |
1257 | if (relsize < 0) | |
1258 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
1259 | ||
1260 | if (relsize == 0) | |
1261 | bfd_set_reloc (obfd, osection, (arelent **) NULL, 0); | |
1262 | else | |
1263 | { | |
1264 | relpp = (arelent **) xmalloc (relsize); | |
1265 | relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); | |
1266 | if (relcount < 0) | |
1267 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
1268 | ||
1269 | if (strip_symbols == STRIP_ALL) | |
1270 | { | |
1271 | /* Remove relocations which are not in | |
1272 | keep_strip_specific_list. */ | |
1273 | arelent **temp_relpp; | |
1274 | long temp_relcount = 0; | |
1275 | long i; | |
1276 | ||
1277 | temp_relpp = (arelent **) xmalloc (relsize); | |
1278 | for (i = 0; i < relcount; i++) | |
1279 | if (is_specified_symbol | |
1280 | (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr), | |
1281 | keep_specific_list)) | |
1282 | temp_relpp [temp_relcount++] = relpp [i]; | |
1283 | relcount = temp_relcount; | |
1284 | free (relpp); | |
1285 | relpp = temp_relpp; | |
1286 | } | |
1287 | bfd_set_reloc (obfd, osection, | |
1288 | (relcount == 0 ? (arelent **) NULL : relpp), relcount); | |
1289 | } | |
1290 | ||
1291 | isection->_cooked_size = isection->_raw_size; | |
1292 | isection->reloc_done = true; | |
1293 | ||
1294 | if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS) | |
1295 | { | |
1296 | PTR memhunk = (PTR) xmalloc ((unsigned) size); | |
1297 | ||
1298 | if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0, | |
1299 | size)) | |
1300 | RETURN_NONFATAL (bfd_get_filename (ibfd)); | |
1301 | ||
1302 | if (copy_byte >= 0) | |
1303 | filter_bytes (memhunk, &size); | |
1304 | ||
1305 | if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0, | |
1306 | size)) | |
1307 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
1308 | ||
1309 | free (memhunk); | |
1310 | } | |
1311 | else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0) | |
1312 | { | |
1313 | PTR memhunk = (PTR) xmalloc ((unsigned) size); | |
1314 | ||
1315 | /* We don't permit the user to turn off the SEC_HAS_CONTENTS | |
1316 | flag--they can just remove the section entirely and add it | |
1317 | back again. However, we do permit them to turn on the | |
1318 | SEC_HAS_CONTENTS flag, and take it to mean that the section | |
1319 | contents should be zeroed out. */ | |
1320 | ||
1321 | memset (memhunk, 0, size); | |
1322 | if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0, | |
1323 | size)) | |
1324 | RETURN_NONFATAL (bfd_get_filename (obfd)); | |
1325 | free (memhunk); | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | /* Get all the sections. This is used when --gap-fill or --pad-to is | |
1330 | used. */ | |
1331 | ||
1332 | static void | |
1333 | get_sections (obfd, osection, secppparg) | |
1334 | bfd *obfd; | |
1335 | asection *osection; | |
1336 | PTR secppparg; | |
1337 | { | |
1338 | asection ***secppp = (asection ***) secppparg; | |
1339 | ||
1340 | **secppp = osection; | |
1341 | ++(*secppp); | |
1342 | } | |
1343 | ||
1344 | /* Sort sections by VMA. This is called via qsort, and is used when | |
1345 | --gap-fill or --pad-to is used. We force non loadable or empty | |
1346 | sections to the front, where they are easier to ignore. */ | |
1347 | ||
1348 | static int | |
1349 | compare_section_lma (arg1, arg2) | |
1350 | const PTR arg1; | |
1351 | const PTR arg2; | |
1352 | { | |
1353 | const asection **sec1 = (const asection **) arg1; | |
1354 | const asection **sec2 = (const asection **) arg2; | |
1355 | flagword flags1, flags2; | |
1356 | ||
1357 | /* Sort non loadable sections to the front. */ | |
1358 | flags1 = (*sec1)->flags; | |
1359 | flags2 = (*sec2)->flags; | |
1360 | if ((flags1 & SEC_HAS_CONTENTS) == 0 | |
1361 | || (flags1 & SEC_LOAD) == 0) | |
1362 | { | |
1363 | if ((flags2 & SEC_HAS_CONTENTS) != 0 | |
1364 | && (flags2 & SEC_LOAD) != 0) | |
1365 | return -1; | |
1366 | } | |
1367 | else | |
1368 | { | |
1369 | if ((flags2 & SEC_HAS_CONTENTS) == 0 | |
1370 | || (flags2 & SEC_LOAD) == 0) | |
1371 | return 1; | |
1372 | } | |
1373 | ||
1374 | /* Sort sections by LMA. */ | |
1375 | if ((*sec1)->lma > (*sec2)->lma) | |
1376 | return 1; | |
1377 | else if ((*sec1)->lma < (*sec2)->lma) | |
1378 | return -1; | |
1379 | ||
1380 | /* Sort sections with the same LMA by size. */ | |
1381 | if ((*sec1)->_raw_size > (*sec2)->_raw_size) | |
1382 | return 1; | |
1383 | else if ((*sec1)->_raw_size < (*sec2)->_raw_size) | |
1384 | return -1; | |
1385 | ||
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | /* Mark all the symbols which will be used in output relocations with | |
1390 | the BSF_KEEP flag so that those symbols will not be stripped. | |
1391 | ||
1392 | Ignore relocations which will not appear in the output file. */ | |
1393 | ||
1394 | static void | |
1395 | mark_symbols_used_in_relocations (ibfd, isection, symbolsarg) | |
1396 | bfd *ibfd; | |
1397 | sec_ptr isection; | |
1398 | PTR symbolsarg; | |
1399 | { | |
1400 | asymbol **symbols = (asymbol **) symbolsarg; | |
1401 | long relsize; | |
1402 | arelent **relpp; | |
1403 | long relcount, i; | |
1404 | ||
1405 | /* Ignore an input section with no corresponding output section. */ | |
1406 | if (isection->output_section == NULL) | |
1407 | return; | |
1408 | ||
1409 | relsize = bfd_get_reloc_upper_bound (ibfd, isection); | |
1410 | if (relsize < 0) | |
1411 | bfd_fatal (bfd_get_filename (ibfd)); | |
1412 | ||
1413 | if (relsize == 0) | |
1414 | return; | |
1415 | ||
1416 | relpp = (arelent **) xmalloc (relsize); | |
1417 | relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols); | |
1418 | if (relcount < 0) | |
1419 | bfd_fatal (bfd_get_filename (ibfd)); | |
1420 | ||
1421 | /* Examine each symbol used in a relocation. If it's not one of the | |
1422 | special bfd section symbols, then mark it with BSF_KEEP. */ | |
1423 | for (i = 0; i < relcount; i++) | |
1424 | { | |
1425 | if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol | |
1426 | && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol | |
1427 | && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol) | |
1428 | (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP; | |
1429 | } | |
1430 | ||
1431 | if (relpp != NULL) | |
1432 | free (relpp); | |
1433 | } | |
1434 | ||
1435 | /* Write out debugging information. */ | |
1436 | ||
1437 | static boolean | |
1438 | write_debugging_info (obfd, dhandle, symcountp, symppp) | |
1439 | bfd *obfd; | |
1440 | PTR dhandle; | |
1441 | long *symcountp; | |
1442 | asymbol ***symppp; | |
1443 | { | |
1444 | if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour) | |
1445 | return write_ieee_debugging_info (obfd, dhandle); | |
1446 | ||
1447 | if (bfd_get_flavour (obfd) == bfd_target_coff_flavour | |
1448 | || bfd_get_flavour (obfd) == bfd_target_elf_flavour) | |
1449 | { | |
1450 | bfd_byte *syms, *strings; | |
1451 | bfd_size_type symsize, stringsize; | |
1452 | asection *stabsec, *stabstrsec; | |
1453 | ||
1454 | if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms, | |
1455 | &symsize, &strings, | |
1456 | &stringsize)) | |
1457 | return false; | |
1458 | ||
1459 | stabsec = bfd_make_section (obfd, ".stab"); | |
1460 | stabstrsec = bfd_make_section (obfd, ".stabstr"); | |
1461 | if (stabsec == NULL | |
1462 | || stabstrsec == NULL | |
1463 | || ! bfd_set_section_size (obfd, stabsec, symsize) | |
1464 | || ! bfd_set_section_size (obfd, stabstrsec, stringsize) | |
1465 | || ! bfd_set_section_alignment (obfd, stabsec, 2) | |
1466 | || ! bfd_set_section_alignment (obfd, stabstrsec, 0) | |
1467 | || ! bfd_set_section_flags (obfd, stabsec, | |
1468 | (SEC_HAS_CONTENTS | |
1469 | | SEC_READONLY | |
1470 | | SEC_DEBUGGING)) | |
1471 | || ! bfd_set_section_flags (obfd, stabstrsec, | |
1472 | (SEC_HAS_CONTENTS | |
1473 | | SEC_READONLY | |
1474 | | SEC_DEBUGGING))) | |
1475 | { | |
1476 | non_fatal (_("%s: can't create debugging section: %s"), | |
1477 | bfd_get_filename (obfd), | |
1478 | bfd_errmsg (bfd_get_error ())); | |
1479 | return false; | |
1480 | } | |
1481 | ||
1482 | /* We can get away with setting the section contents now because | |
1483 | the next thing the caller is going to do is copy over the | |
1484 | real sections. We may someday have to split the contents | |
1485 | setting out of this function. */ | |
1486 | if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0, | |
1487 | symsize) | |
1488 | || ! bfd_set_section_contents (obfd, stabstrsec, strings, | |
1489 | (file_ptr) 0, stringsize)) | |
1490 | { | |
1491 | non_fatal (_("%s: can't set debugging section contents: %s"), | |
1492 | bfd_get_filename (obfd), | |
1493 | bfd_errmsg (bfd_get_error ())); | |
1494 | return false; | |
1495 | } | |
1496 | ||
1497 | return true; | |
1498 | } | |
1499 | ||
1500 | non_fatal (_("%s: don't know how to write debugging information for %s"), | |
1501 | bfd_get_filename (obfd), bfd_get_target (obfd)); | |
1502 | return false; | |
1503 | } | |
1504 | ||
1505 | static int | |
1506 | strip_main (argc, argv) | |
1507 | int argc; | |
1508 | char *argv[]; | |
1509 | { | |
1510 | char *input_target = NULL, *output_target = NULL; | |
1511 | boolean show_version = false; | |
1512 | int c, i; | |
1513 | struct section_list *p; | |
1514 | char *output_file = NULL; | |
1515 | ||
1516 | while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv", | |
1517 | strip_options, (int *) 0)) != EOF) | |
1518 | { | |
1519 | switch (c) | |
1520 | { | |
1521 | case 'I': | |
1522 | input_target = optarg; | |
1523 | break; | |
1524 | case 'O': | |
1525 | output_target = optarg; | |
1526 | break; | |
1527 | case 'F': | |
1528 | input_target = output_target = optarg; | |
1529 | break; | |
1530 | case 'R': | |
1531 | p = find_section_list (optarg, true); | |
1532 | p->remove = true; | |
1533 | sections_removed = true; | |
1534 | break; | |
1535 | case 's': | |
1536 | strip_symbols = STRIP_ALL; | |
1537 | break; | |
1538 | case 'S': | |
1539 | case 'g': | |
1540 | strip_symbols = STRIP_DEBUG; | |
1541 | break; | |
1542 | case OPTION_STRIP_UNNEEDED: | |
1543 | strip_symbols = STRIP_UNNEEDED; | |
1544 | break; | |
1545 | case 'K': | |
1546 | add_specific_symbol (optarg, &keep_specific_list); | |
1547 | break; | |
1548 | case 'N': | |
1549 | add_specific_symbol (optarg, &strip_specific_list); | |
1550 | break; | |
1551 | case 'o': | |
1552 | output_file = optarg; | |
1553 | break; | |
1554 | case 'p': | |
1555 | preserve_dates = true; | |
1556 | break; | |
1557 | case 'x': | |
1558 | discard_locals = LOCALS_ALL; | |
1559 | break; | |
1560 | case 'X': | |
1561 | discard_locals = LOCALS_START_L; | |
1562 | break; | |
1563 | case 'v': | |
1564 | verbose = true; | |
1565 | break; | |
1566 | case 'V': | |
1567 | show_version = true; | |
1568 | break; | |
1569 | case 0: | |
1570 | break; /* we've been given a long option */ | |
1571 | case 'h': | |
1572 | strip_usage (stdout, 0); | |
1573 | default: | |
1574 | strip_usage (stderr, 1); | |
1575 | } | |
1576 | } | |
1577 | ||
1578 | if (show_version) | |
1579 | print_version ("strip"); | |
1580 | ||
1581 | /* Default is to strip all symbols. */ | |
1582 | if (strip_symbols == STRIP_UNDEF | |
1583 | && discard_locals == LOCALS_UNDEF | |
1584 | && strip_specific_list == NULL) | |
1585 | strip_symbols = STRIP_ALL; | |
1586 | ||
1587 | if (output_target == (char *) NULL) | |
1588 | output_target = input_target; | |
1589 | ||
1590 | i = optind; | |
1591 | if (i == argc | |
1592 | || (output_file != NULL && (i + 1) < argc)) | |
1593 | strip_usage (stderr, 1); | |
1594 | ||
1595 | for (; i < argc; i++) | |
1596 | { | |
1597 | int hold_status = status; | |
1598 | struct stat statbuf; | |
1599 | char *tmpname; | |
1600 | ||
1601 | if (preserve_dates) | |
1602 | { | |
1603 | if (stat (argv[i], &statbuf) < 0) | |
1604 | { | |
1605 | non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno)); | |
1606 | continue; | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | if (output_file != NULL) | |
1611 | tmpname = output_file; | |
1612 | else | |
1613 | tmpname = make_tempname (argv[i]); | |
1614 | status = 0; | |
1615 | ||
1616 | copy_file (argv[i], tmpname, input_target, output_target); | |
1617 | if (status == 0) | |
1618 | { | |
1619 | if (preserve_dates) | |
1620 | set_times (tmpname, &statbuf); | |
1621 | if (output_file == NULL) | |
1622 | smart_rename (tmpname, argv[i], preserve_dates); | |
1623 | status = hold_status; | |
1624 | } | |
1625 | else | |
1626 | unlink (tmpname); | |
1627 | if (output_file == NULL) | |
1628 | free (tmpname); | |
1629 | } | |
1630 | ||
1631 | return 0; | |
1632 | } | |
1633 | ||
1634 | static int | |
1635 | copy_main (argc, argv) | |
1636 | int argc; | |
1637 | char *argv[]; | |
1638 | { | |
1639 | char *input_filename = NULL, *output_filename = NULL; | |
1640 | char *input_target = NULL, *output_target = NULL; | |
1641 | boolean show_version = false; | |
1642 | boolean change_warn = true; | |
1643 | int c; | |
1644 | struct section_list *p; | |
1645 | struct stat statbuf; | |
1646 | ||
1647 | while ((c = getopt_long (argc, argv, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:", | |
1648 | copy_options, (int *) 0)) != EOF) | |
1649 | { | |
1650 | switch (c) | |
1651 | { | |
1652 | case 'b': | |
1653 | copy_byte = atoi (optarg); | |
1654 | if (copy_byte < 0) | |
1655 | fatal (_("byte number must be non-negative")); | |
1656 | break; | |
1657 | case 'i': | |
1658 | interleave = atoi (optarg); | |
1659 | if (interleave < 1) | |
1660 | fatal (_("interleave must be positive")); | |
1661 | break; | |
1662 | case 'I': | |
1663 | case 's': /* "source" - 'I' is preferred */ | |
1664 | input_target = optarg; | |
1665 | break; | |
1666 | case 'O': | |
1667 | case 'd': /* "destination" - 'O' is preferred */ | |
1668 | output_target = optarg; | |
1669 | break; | |
1670 | case 'F': | |
1671 | input_target = output_target = optarg; | |
1672 | break; | |
1673 | case 'R': | |
1674 | p = find_section_list (optarg, true); | |
1675 | p->remove = true; | |
1676 | sections_removed = true; | |
1677 | break; | |
1678 | case 'S': | |
1679 | strip_symbols = STRIP_ALL; | |
1680 | break; | |
1681 | case 'g': | |
1682 | strip_symbols = STRIP_DEBUG; | |
1683 | break; | |
1684 | case OPTION_STRIP_UNNEEDED: | |
1685 | strip_symbols = STRIP_UNNEEDED; | |
1686 | break; | |
1687 | case 'K': | |
1688 | add_specific_symbol (optarg, &keep_specific_list); | |
1689 | break; | |
1690 | case 'N': | |
1691 | add_specific_symbol (optarg, &strip_specific_list); | |
1692 | break; | |
1693 | case 'L': | |
1694 | add_specific_symbol (optarg, &localize_specific_list); | |
1695 | break; | |
1696 | case 'W': | |
1697 | add_specific_symbol (optarg, &weaken_specific_list); | |
1698 | break; | |
1699 | case 'p': | |
1700 | preserve_dates = true; | |
1701 | break; | |
1702 | case 'x': | |
1703 | discard_locals = LOCALS_ALL; | |
1704 | break; | |
1705 | case 'X': | |
1706 | discard_locals = LOCALS_START_L; | |
1707 | break; | |
1708 | case 'v': | |
1709 | verbose = true; | |
1710 | break; | |
1711 | case 'V': | |
1712 | show_version = true; | |
1713 | break; | |
1714 | case OPTION_WEAKEN: | |
1715 | weaken = true; | |
1716 | break; | |
1717 | case OPTION_ADD_SECTION: | |
1718 | { | |
1719 | const char *s; | |
1720 | struct stat st; | |
1721 | struct section_add *pa; | |
1722 | int len; | |
1723 | char *name; | |
1724 | FILE *f; | |
1725 | ||
1726 | s = strchr (optarg, '='); | |
1727 | ||
1728 | if (s == NULL) | |
1729 | fatal (_("bad format for --add-section NAME=FILENAME")); | |
1730 | ||
1731 | if (stat (s + 1, & st) < 0) | |
1732 | fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno)); | |
1733 | ||
1734 | pa = (struct section_add *) xmalloc (sizeof (struct section_add)); | |
1735 | ||
1736 | len = s - optarg; | |
1737 | name = (char *) xmalloc (len + 1); | |
1738 | strncpy (name, optarg, len); | |
1739 | name[len] = '\0'; | |
1740 | pa->name = name; | |
1741 | ||
1742 | pa->filename = s + 1; | |
1743 | ||
1744 | pa->size = st.st_size; | |
1745 | ||
1746 | pa->contents = (bfd_byte *) xmalloc (pa->size); | |
1747 | f = fopen (pa->filename, FOPEN_RB); | |
1748 | ||
1749 | if (f == NULL) | |
1750 | fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno)); | |
1751 | ||
1752 | if (fread (pa->contents, 1, pa->size, f) == 0 | |
1753 | || ferror (f)) | |
1754 | fatal (_("%s: fread failed"), pa->filename); | |
1755 | ||
1756 | fclose (f); | |
1757 | ||
1758 | pa->next = add_sections; | |
1759 | add_sections = pa; | |
1760 | } | |
1761 | break; | |
1762 | case OPTION_CHANGE_START: | |
1763 | change_start = parse_vma (optarg, "--change-start"); | |
1764 | break; | |
1765 | case OPTION_CHANGE_SECTION_ADDRESS: | |
1766 | case OPTION_CHANGE_SECTION_LMA: | |
1767 | case OPTION_CHANGE_SECTION_VMA: | |
1768 | { | |
1769 | const char *s; | |
1770 | int len; | |
1771 | char *name; | |
1772 | char *option; | |
1773 | bfd_vma val; | |
1774 | enum change_action what; | |
1775 | ||
1776 | switch (c) | |
1777 | { | |
1778 | case OPTION_CHANGE_SECTION_ADDRESS: option = "--change-section-address"; break; | |
1779 | case OPTION_CHANGE_SECTION_LMA: option = "--change-section-lma"; break; | |
1780 | case OPTION_CHANGE_SECTION_VMA: option = "--change-section-vma"; break; | |
1781 | } | |
1782 | ||
1783 | s = strchr (optarg, '='); | |
1784 | if (s == NULL) | |
1785 | { | |
1786 | s = strchr (optarg, '+'); | |
1787 | if (s == NULL) | |
1788 | { | |
1789 | s = strchr (optarg, '-'); | |
1790 | if (s == NULL) | |
1791 | fatal (_("bad format for %s"), option); | |
1792 | } | |
1793 | } | |
1794 | ||
1795 | len = s - optarg; | |
1796 | name = (char *) xmalloc (len + 1); | |
1797 | strncpy (name, optarg, len); | |
1798 | name[len] = '\0'; | |
1799 | ||
1800 | p = find_section_list (name, true); | |
1801 | ||
1802 | val = parse_vma (s + 1, option); | |
1803 | ||
1804 | switch (*s) | |
1805 | { | |
1806 | case '=': what = CHANGE_SET; break; | |
1807 | case '-': val = - val; /* Drop through. */ | |
1808 | case '+': what = CHANGE_MODIFY; break; | |
1809 | } | |
1810 | ||
1811 | switch (c) | |
1812 | { | |
1813 | case OPTION_CHANGE_SECTION_ADDRESS: | |
1814 | p->change_vma = what; | |
1815 | p->vma_val = val; | |
1816 | /* Drop through. */ | |
1817 | ||
1818 | case OPTION_CHANGE_SECTION_LMA: | |
1819 | p->change_lma = what; | |
1820 | p->lma_val = val; | |
1821 | break; | |
1822 | ||
1823 | case OPTION_CHANGE_SECTION_VMA: | |
1824 | p->change_vma = what; | |
1825 | p->vma_val = val; | |
1826 | break; | |
1827 | } | |
1828 | } | |
1829 | break; | |
1830 | case OPTION_CHANGE_ADDRESSES: | |
1831 | change_section_address = parse_vma (optarg, "--change-addresses"); | |
1832 | change_start = change_section_address; | |
1833 | break; | |
1834 | case OPTION_CHANGE_WARNINGS: | |
1835 | change_warn = true; | |
1836 | break; | |
1837 | case OPTION_CHANGE_LEADING_CHAR: | |
1838 | change_leading_char = true; | |
1839 | break; | |
1840 | case OPTION_DEBUGGING: | |
1841 | convert_debugging = true; | |
1842 | break; | |
1843 | case OPTION_GAP_FILL: | |
1844 | { | |
1845 | bfd_vma gap_fill_vma; | |
1846 | ||
1847 | gap_fill_vma = parse_vma (optarg, "--gap-fill"); | |
1848 | gap_fill = (bfd_byte) gap_fill_vma; | |
1849 | if ((bfd_vma) gap_fill != gap_fill_vma) | |
1850 | { | |
1851 | char buff[20]; | |
1852 | ||
1853 | sprintf_vma (buff, gap_fill_vma); | |
1854 | ||
1855 | non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"), | |
1856 | buff, gap_fill); | |
1857 | } | |
1858 | gap_fill_set = true; | |
1859 | } | |
1860 | break; | |
1861 | case OPTION_NO_CHANGE_WARNINGS: | |
1862 | change_warn = false; | |
1863 | break; | |
1864 | case OPTION_PAD_TO: | |
1865 | pad_to = parse_vma (optarg, "--pad-to"); | |
1866 | pad_to_set = true; | |
1867 | break; | |
1868 | case OPTION_REMOVE_LEADING_CHAR: | |
1869 | remove_leading_char = true; | |
1870 | break; | |
1871 | case OPTION_SET_SECTION_FLAGS: | |
1872 | { | |
1873 | const char *s; | |
1874 | int len; | |
1875 | char *name; | |
1876 | ||
1877 | s = strchr (optarg, '='); | |
1878 | if (s == NULL) | |
1879 | fatal (_("bad format for --set-section-flags")); | |
1880 | ||
1881 | len = s - optarg; | |
1882 | name = (char *) xmalloc (len + 1); | |
1883 | strncpy (name, optarg, len); | |
1884 | name[len] = '\0'; | |
1885 | ||
1886 | p = find_section_list (name, true); | |
1887 | ||
1888 | p->set_flags = true; | |
1889 | p->flags = parse_flags (s + 1); | |
1890 | } | |
1891 | break; | |
1892 | case OPTION_SET_START: | |
1893 | set_start = parse_vma (optarg, "--set-start"); | |
1894 | set_start_set = true; | |
1895 | break; | |
1896 | case 0: | |
1897 | break; /* we've been given a long option */ | |
1898 | case 'h': | |
1899 | copy_usage (stdout, 0); | |
1900 | default: | |
1901 | copy_usage (stderr, 1); | |
1902 | } | |
1903 | } | |
1904 | ||
1905 | if (show_version) | |
1906 | print_version ("objcopy"); | |
1907 | ||
1908 | if (copy_byte >= interleave) | |
1909 | fatal (_("byte number must be less than interleave")); | |
1910 | ||
1911 | if (optind == argc || optind + 2 < argc) | |
1912 | copy_usage (stderr, 1); | |
1913 | ||
1914 | input_filename = argv[optind]; | |
1915 | if (optind + 1 < argc) | |
1916 | output_filename = argv[optind + 1]; | |
1917 | ||
1918 | /* Default is to strip no symbols. */ | |
1919 | if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF) | |
1920 | strip_symbols = STRIP_NONE; | |
1921 | ||
1922 | if (output_target == (char *) NULL) | |
1923 | output_target = input_target; | |
1924 | ||
1925 | if (preserve_dates) | |
1926 | { | |
1927 | if (stat (input_filename, &statbuf) < 0) | |
1928 | fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno)); | |
1929 | } | |
1930 | ||
1931 | /* If there is no destination file then create a temp and rename | |
1932 | the result into the input. */ | |
1933 | ||
1934 | if (output_filename == (char *) NULL) | |
1935 | { | |
1936 | char *tmpname = make_tempname (input_filename); | |
1937 | ||
1938 | copy_file (input_filename, tmpname, input_target, output_target); | |
1939 | if (status == 0) | |
1940 | { | |
1941 | if (preserve_dates) | |
1942 | set_times (tmpname, &statbuf); | |
1943 | smart_rename (tmpname, input_filename, preserve_dates); | |
1944 | } | |
1945 | else | |
1946 | unlink (tmpname); | |
1947 | } | |
1948 | else | |
1949 | { | |
1950 | copy_file (input_filename, output_filename, input_target, output_target); | |
1951 | if (status == 0 && preserve_dates) | |
1952 | set_times (output_filename, &statbuf); | |
1953 | } | |
1954 | ||
1955 | if (change_warn) | |
1956 | { | |
1957 | for (p = change_sections; p != NULL; p = p->next) | |
1958 | { | |
1959 | if (! p->used) | |
1960 | { | |
1961 | if (p->change_vma != CHANGE_IGNORE) | |
1962 | { | |
1963 | char buff [20]; | |
1964 | ||
1965 | sprintf_vma (buff, p->vma_val); | |
1966 | ||
1967 | /* xgettext:c-format */ | |
1968 | non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"), | |
1969 | p->name, | |
1970 | p->change_vma == CHANGE_SET ? '=' : '+', | |
1971 | buff); | |
1972 | } | |
1973 | ||
1974 | if (p->change_lma != CHANGE_IGNORE) | |
1975 | { | |
1976 | char buff [20]; | |
1977 | ||
1978 | sprintf_vma (buff, p->lma_val); | |
1979 | ||
1980 | /* xgettext:c-format */ | |
1981 | non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"), | |
1982 | p->name, | |
1983 | p->change_lma == CHANGE_SET ? '=' : '+', | |
1984 | buff); | |
1985 | } | |
1986 | } | |
1987 | } | |
1988 | } | |
1989 | ||
1990 | return 0; | |
1991 | } | |
1992 | ||
1993 | int | |
1994 | main (argc, argv) | |
1995 | int argc; | |
1996 | char *argv[]; | |
1997 | { | |
1998 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
1999 | setlocale (LC_MESSAGES, ""); | |
2000 | #endif | |
2001 | bindtextdomain (PACKAGE, LOCALEDIR); | |
2002 | textdomain (PACKAGE); | |
2003 | ||
2004 | program_name = argv[0]; | |
2005 | xmalloc_set_program_name (program_name); | |
2006 | ||
2007 | START_PROGRESS (program_name, 0); | |
2008 | ||
2009 | strip_symbols = STRIP_UNDEF; | |
2010 | discard_locals = LOCALS_UNDEF; | |
2011 | ||
2012 | bfd_init (); | |
2013 | set_default_bfd_target (); | |
2014 | ||
2015 | if (is_strip < 0) | |
2016 | { | |
2017 | int i = strlen (program_name); | |
2018 | is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0); | |
2019 | } | |
2020 | ||
2021 | if (is_strip) | |
2022 | strip_main (argc, argv); | |
2023 | else | |
2024 | copy_main (argc, argv); | |
2025 | ||
2026 | END_PROGRESS (program_name); | |
2027 | ||
2028 | return status; | |
2029 | } |