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