1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "libiberty.h"
28 static flagword parse_flags PARAMS ((const char *));
29 static struct section_list *find_section_list PARAMS ((const char *, boolean));
30 static void setup_section PARAMS ((bfd *, asection *, PTR));
31 static void copy_section PARAMS ((bfd *, asection *, PTR));
32 static void get_sections PARAMS ((bfd *, asection *, PTR));
33 static int compare_section_vma PARAMS ((const PTR, const PTR));
34 static void add_strip_symbol PARAMS ((const char *));
35 static boolean is_strip_symbol PARAMS ((const char *));
36 static boolean is_strip_section PARAMS ((bfd *, asection *));
37 static unsigned int filter_symbols
38 PARAMS ((bfd *, asymbol **, asymbol **, long));
39 static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
40 static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***));
42 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
44 static asymbol **isympp = NULL; /* Input symbols */
45 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
47 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
48 static int copy_byte = -1;
49 static int interleave = 4;
51 static boolean verbose; /* Print file and target names. */
52 static int status = 0; /* Exit status. */
57 strip_none, /* don't strip */
58 strip_debug, /* strip all debugger symbols */
59 strip_unneeded, /* strip unnecessary symbols */
60 strip_all /* strip all symbols */
63 /* Which symbols to remove. */
64 static enum strip_action strip_symbols;
69 locals_start_L, /* discard locals starting with L */
70 locals_all /* discard all locals */
73 /* Which local symbols to remove. Overrides strip_all. */
74 static enum locals_action discard_locals;
76 /* Structure used to hold lists of sections and actions to take. */
80 /* Next section to adjust. */
81 struct section_list *next;
84 /* Whether this entry was used. */
86 /* Whether to remove this section. */
88 /* Whether to adjust or set VMA. */
89 enum { ignore_vma, adjust_vma, set_vma } adjust;
90 /* Amount to adjust by or set to. */
92 /* Whether to set the section flags. */
94 /* What to set the section flags to. */
98 static struct section_list *adjust_sections;
99 static boolean sections_removed;
101 /* Adjustments to the start address. */
102 static bfd_vma adjust_start = 0;
103 static boolean set_start_set = false;
104 static bfd_vma set_start;
106 /* Adjustments to section VMA's. */
107 static bfd_vma adjust_section_vma = 0;
109 /* Filling gaps between sections. */
110 static boolean gap_fill_set = false;
111 static bfd_byte gap_fill = 0;
113 /* Pad to a given address. */
114 static boolean pad_to_set = false;
115 static bfd_vma pad_to;
117 /* List of sections to add. */
121 /* Next section to add. */
122 struct section_add *next;
123 /* Name of section to add. */
125 /* Name of file holding section contents. */
126 const char *filename;
129 /* Contents of file. */
131 /* BFD section, after it has been added. */
135 static struct section_add *add_sections;
137 /* Whether to convert debugging information. */
139 static boolean convert_debugging = false;
141 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
143 #define OPTION_ADD_SECTION 150
144 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
145 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
146 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
147 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
148 #define OPTION_DEBUGGING (OPTION_ADJUST_WARNINGS + 1)
149 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
150 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
151 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
152 #define OPTION_SET_SECTION_FLAGS (OPTION_PAD_TO + 1)
153 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
154 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
156 /* Options to handle if running as "strip". */
158 static struct option strip_options[] =
160 {"discard-all", no_argument, 0, 'x'},
161 {"discard-locals", no_argument, 0, 'X'},
162 {"format", required_argument, 0, 'F'}, /* Obsolete */
163 {"help", no_argument, 0, 'h'},
164 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
165 {"input-target", required_argument, 0, 'I'},
166 {"keep-symbol", required_argument, 0, 'K'},
167 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
168 {"output-target", required_argument, 0, 'O'},
169 {"remove-section", required_argument, 0, 'R'},
170 {"strip-all", no_argument, 0, 's'},
171 {"strip-debug", no_argument, 0, 'S'},
172 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
173 {"strip-symbol", required_argument, 0, 'N'},
174 {"target", required_argument, 0, 'F'},
175 {"verbose", no_argument, 0, 'v'},
176 {"version", no_argument, 0, 'V'},
177 {0, no_argument, 0, 0}
180 /* Options to handle if running as "objcopy". */
182 static struct option copy_options[] =
184 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
185 {"adjust-start", required_argument, 0, OPTION_ADJUST_START},
186 {"adjust-vma", required_argument, 0, OPTION_ADJUST_VMA},
187 {"adjust-section-vma", required_argument, 0, OPTION_ADJUST_SECTION_VMA},
188 {"adjust-warnings", no_argument, 0, OPTION_ADJUST_WARNINGS},
189 {"byte", required_argument, 0, 'b'},
190 {"debugging", no_argument, 0, OPTION_DEBUGGING},
191 {"discard-all", no_argument, 0, 'x'},
192 {"discard-locals", no_argument, 0, 'X'},
193 {"format", required_argument, 0, 'F'}, /* Obsolete */
194 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
195 {"help", no_argument, 0, 'h'},
196 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
197 {"input-target", required_argument, 0, 'I'},
198 {"interleave", required_argument, 0, 'i'},
199 {"keep-symbol", required_argument, 0, 'K'},
200 {"no-adjust-warnings", no_argument, 0, OPTION_NO_ADJUST_WARNINGS},
201 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
202 {"output-target", required_argument, 0, 'O'},
203 {"pad-to", required_argument, 0, OPTION_PAD_TO},
204 {"remove-section", required_argument, 0, 'R'},
205 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
206 {"set-start", required_argument, 0, OPTION_SET_START},
207 {"strip-all", no_argument, 0, 'S'},
208 {"strip-debug", no_argument, 0, 'g'},
209 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
210 {"strip-symbol", required_argument, 0, 'N'},
211 {"target", required_argument, 0, 'F'},
212 {"verbose", no_argument, 0, 'v'},
213 {"version", no_argument, 0, 'V'},
214 {0, no_argument, 0, 0}
218 extern char *program_name;
219 extern char *program_version;
221 /* This flag distinguishes between strip and objcopy:
222 1 means this is 'strip'; 0 means this is 'objcopy'.
223 -1 means if we should use argv[0] to decide. */
228 copy_usage (stream, exit_status)
233 Usage: %s [-vVSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
234 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
235 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
236 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
237 [--discard-locals] [--debugging] [--remove-section=section]\n",
240 [--gap-fill=val] [--pad-to=address]\n\
241 [--set-start=val] [--adjust-start=incr]\n\
242 [--adjust-vma=incr] [--adjust-section-vma=section{=,+,-}val]\n\
243 [--adjust-warnings] [--no-adjust-warnings]\n\
244 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
245 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
246 [--verbose] [--version] [--help]\n\
247 in-file [out-file]\n");
248 list_supported_targets (program_name, stream);
253 strip_usage (stream, exit_status)
258 Usage: %s [-vVsSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
259 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
260 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
261 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
262 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
263 [--verbose] [--version] [--help] file...\n",
265 list_supported_targets (program_name, stream);
269 /* Parse section flags into a flagword, with a fatal error if the
270 string can't be parsed. */
284 snext = strchr (s, ',');
293 #define PARSE_FLAG(fname,fval) if (strncmp (fname, s, len) == 0) ret |= fval;
294 PARSE_FLAG ("alloc", SEC_ALLOC);
295 PARSE_FLAG ("load", SEC_LOAD);
296 PARSE_FLAG ("readonly", SEC_READONLY);
297 PARSE_FLAG ("code", SEC_CODE);
298 PARSE_FLAG ("data", SEC_DATA);
299 PARSE_FLAG ("rom", SEC_ROM);
309 /* Find and optionally add an entry in the adjust_sections list. */
311 static struct section_list *
312 find_section_list (name, add)
316 register struct section_list *p;
318 for (p = adjust_sections; p != NULL; p = p->next)
319 if (strcmp (p->name, name) == 0)
325 p = (struct section_list *) xmalloc (sizeof (struct section_list));
329 p->adjust = ignore_vma;
331 p->set_flags = false;
334 p->next = adjust_sections;
340 /* Make a list of symbols to explicitly strip out, or to keep. A
341 linked list is good enough for a small number from the command
342 line, but this will slow things down a lot if many symbols are
348 struct symlist *next;
351 /* List of symbols to strip. */
353 static struct symlist *strip_specific_list = NULL;
355 /* If this is false, we strip the symbols in strip_specific_list.
356 Otherwise, we keep only the symbols in the list. */
358 static boolean keep_symbols = false;
360 /* Add a symbol to strip_specific_list. */
363 add_strip_symbol (name)
366 struct symlist *tmp_list;
368 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
369 tmp_list->name = name;
370 tmp_list->next = strip_specific_list;
371 strip_specific_list = tmp_list;
374 /* See whether a symbol should be stripped or kept based on
375 strip_specific_list and keep_symbols. */
378 is_strip_symbol (name)
381 struct symlist *tmp_list;
383 for (tmp_list = strip_specific_list; tmp_list; tmp_list = tmp_list->next)
385 if (strcmp (name, tmp_list->name) == 0)
386 return keep_symbols ? false : true;
391 /* See if a section is being removed. */
394 is_strip_section (abfd, sec)
398 struct section_list *p;
400 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
401 && (strip_symbols == strip_debug
402 || strip_symbols == strip_unneeded
403 || strip_symbols == strip_all
404 || discard_locals == locals_all
405 || convert_debugging))
408 if (! sections_removed)
410 p = find_section_list (bfd_get_section_name (abfd, sec), false);
411 return p != NULL && p->remove ? true : false;
414 /* Choose which symbol entries to copy; put the result in OSYMS.
415 We don't copy in place, because that confuses the relocs.
416 Return the number of symbols to print. */
419 filter_symbols (abfd, osyms, isyms, symcount)
421 asymbol **osyms, **isyms;
424 register asymbol **from = isyms, **to = osyms;
425 long src_count = 0, dst_count = 0;
427 for (; src_count < symcount; src_count++)
429 asymbol *sym = from[src_count];
430 flagword flags = sym->flags;
433 if ((flags & BSF_KEEP) != 0) /* Used in relocation. */
435 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
436 || bfd_is_und_section (bfd_get_section (sym))
437 || bfd_is_com_section (bfd_get_section (sym)))
438 keep = strip_symbols != strip_unneeded;
439 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
440 keep = (strip_symbols != strip_debug
441 && strip_symbols != strip_unneeded
442 && ! convert_debugging);
443 else /* Local symbol. */
444 keep = (strip_symbols != strip_unneeded
445 && (discard_locals != locals_all
446 && (discard_locals != locals_start_L
447 || ! bfd_is_local_label (abfd, sym))));
449 if (keep && is_strip_symbol (bfd_asymbol_name (sym)))
451 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
455 to[dst_count++] = sym;
461 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
465 filter_bytes (memhunk, size)
469 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
471 for (; from < end; from += interleave)
476 /* Copy object file IBFD onto OBFD. */
479 copy_object (ibfd, obfd)
485 asection **osections = NULL;
486 bfd_size_type *gaps = NULL;
487 bfd_size_type max_gap = 0;
489 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
491 nonfatal (bfd_get_filename (obfd));
495 printf ("copy from %s(%s) to %s(%s)\n",
496 bfd_get_filename(ibfd), bfd_get_target(ibfd),
497 bfd_get_filename(obfd), bfd_get_target(obfd));
502 start = bfd_get_start_address (ibfd);
503 start += adjust_start;
505 if (!bfd_set_start_address (obfd, start)
506 || !bfd_set_file_flags (obfd,
507 (bfd_get_file_flags (ibfd)
508 & bfd_applicable_file_flags (obfd))))
510 nonfatal (bfd_get_filename (ibfd));
513 /* Copy architecture of input file to output file */
514 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
515 bfd_get_mach (ibfd)))
517 fprintf (stderr, "Output file cannot represent architecture %s\n",
518 bfd_printable_arch_mach (bfd_get_arch (ibfd),
519 bfd_get_mach (ibfd)));
521 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
523 nonfatal (bfd_get_filename(ibfd));
528 if (osympp != isympp)
531 /* bfd mandates that all output sections be created and sizes set before
532 any output is done. Thus, we traverse all sections multiple times. */
533 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
535 if (add_sections != NULL)
537 struct section_add *padd;
538 struct section_list *pset;
540 for (padd = add_sections; padd != NULL; padd = padd->next)
542 padd->section = bfd_make_section (obfd, padd->name);
543 if (padd->section == NULL)
545 fprintf (stderr, "%s: can't create section `%s': %s\n",
546 program_name, padd->name,
547 bfd_errmsg (bfd_get_error ()));
555 if (! bfd_set_section_size (obfd, padd->section, padd->size))
556 nonfatal (bfd_get_filename (obfd));
558 pset = find_section_list (padd->name, false);
562 if (pset != NULL && pset->set_flags)
563 flags = pset->flags | SEC_HAS_CONTENTS;
565 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
566 if (! bfd_set_section_flags (obfd, padd->section, flags))
567 nonfatal (bfd_get_filename (obfd));
570 && (pset->adjust == adjust_vma
571 || pset->adjust == set_vma))
573 if (! bfd_set_section_vma (obfd, padd->section, pset->val))
574 nonfatal (bfd_get_filename (obfd));
580 if (gap_fill_set || pad_to_set)
585 /* We must fill in gaps between the sections and/or we must pad
586 the last section to a specified address. We do this by
587 grabbing a list of the sections, sorting them by VMA, and
588 increasing the section sizes as required to fill the gaps.
589 We write out the gap contents below. */
591 c = bfd_count_sections (obfd);
592 osections = (asection **) xmalloc (c * sizeof (asection *));
594 bfd_map_over_sections (obfd, get_sections, (void *) &set);
596 qsort (osections, c, sizeof (asection *), compare_section_vma);
598 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
599 memset (gaps, 0, c * sizeof (bfd_size_type));
603 for (i = 0; i < c - 1; i++)
607 bfd_vma gap_start, gap_stop;
609 flags = bfd_get_section_flags (obfd, osections[i]);
610 if ((flags & SEC_HAS_CONTENTS) == 0
611 || (flags & SEC_LOAD) == 0)
614 size = bfd_section_size (obfd, osections[i]);
615 gap_start = bfd_section_vma (obfd, osections[i]) + size;
616 gap_stop = bfd_section_vma (obfd, osections[i + 1]);
617 if (gap_start < gap_stop)
619 if (! bfd_set_section_size (obfd, osections[i],
620 size + (gap_stop - gap_start)))
622 fprintf (stderr, "%s: Can't fill gap after %s: %s\n",
624 bfd_get_section_name (obfd, osections[i]),
625 bfd_errmsg (bfd_get_error()));
629 gaps[i] = gap_stop - gap_start;
630 if (max_gap < gap_stop - gap_start)
631 max_gap = gap_stop - gap_start;
641 vma = bfd_section_vma (obfd, osections[c - 1]);
642 size = bfd_section_size (obfd, osections[c - 1]);
643 if (vma + size < pad_to)
645 if (! bfd_set_section_size (obfd, osections[c - 1],
648 fprintf (stderr, "%s: Can't add padding to %s: %s\n",
650 bfd_get_section_name (obfd, osections[c - 1]),
651 bfd_errmsg (bfd_get_error ()));
656 gaps[c - 1] = pad_to - (vma + size);
657 if (max_gap < pad_to - (vma + size))
658 max_gap = pad_to - (vma + size);
664 /* Symbol filtering must happen after the output sections have
665 been created, but before their contents are set. */
666 if (strip_symbols == strip_all)
668 osympp = isympp = NULL;
676 symsize = bfd_get_symtab_upper_bound (ibfd);
679 nonfatal (bfd_get_filename (ibfd));
682 osympp = isympp = (asymbol **) xmalloc (symsize);
683 symcount = bfd_canonicalize_symtab (ibfd, isympp);
686 nonfatal (bfd_get_filename (ibfd));
689 if (convert_debugging)
690 dhandle = read_debugging_info (ibfd, isympp, symcount);
692 if (strip_symbols == strip_debug
693 || strip_symbols == strip_unneeded
694 || discard_locals != locals_undef
695 || strip_specific_list != NULL
697 || convert_debugging)
699 /* Mark symbols used in output relocations so that they
700 are kept, even if they are local labels or static symbols.
702 Note we iterate over the input sections examining their
703 relocations since the relocations for the output sections
704 haven't been set yet. mark_symbols_used_in_relocations will
705 ignore input sections which have no corresponding output
707 bfd_map_over_sections (ibfd,
708 mark_symbols_used_in_relocations,
710 osympp = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
711 symcount = filter_symbols (ibfd, osympp, isympp, symcount);
714 if (convert_debugging && dhandle != NULL)
716 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
724 bfd_set_symtab (obfd, osympp, symcount);
726 /* This has to happen after the symbol table has been set. */
727 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
729 if (add_sections != NULL)
731 struct section_add *padd;
733 for (padd = add_sections; padd != NULL; padd = padd->next)
735 if (! bfd_set_section_contents (obfd, padd->section,
736 (PTR) padd->contents,
738 (bfd_size_type) padd->size))
739 nonfatal (bfd_get_filename (obfd));
743 if (gap_fill_set || pad_to_set)
748 /* Fill in the gaps. */
752 buf = (bfd_byte *) xmalloc (max_gap);
753 memset (buf, gap_fill, (size_t) max_gap);
755 c = bfd_count_sections (obfd);
756 for (i = 0; i < c; i++)
764 off = bfd_section_size (obfd, osections[i]) - left;
773 if (! bfd_set_section_contents (obfd, osections[i], buf,
776 nonfatal (bfd_get_filename (obfd));
785 /* Allow the BFD backend to copy any private data it understands
786 from the input BFD to the output BFD. This is done last to
787 permit the routine to look at the filtered symbol table, which is
788 important for the ECOFF code at least. */
789 if (!bfd_copy_private_bfd_data (ibfd, obfd))
791 fprintf (stderr, "%s: %s: error copying private BFD data: %s\n",
792 program_name, bfd_get_filename (obfd),
793 bfd_errmsg (bfd_get_error ()));
805 size_t size = strlen (a) + strlen (b) + strlen (c);
806 char *r = xmalloc (size + 1);
814 /* Read each archive element in turn from IBFD, copy the
815 contents to temp file, and keep the temp file handle. */
818 copy_archive (ibfd, obfd, output_target)
825 struct name_list *next;
829 bfd **ptr = &obfd->archive_head;
831 char *dir = make_tempname (bfd_get_filename (obfd));
833 /* Make a temp directory to hold the contents. */
834 if (mkdir (dir, 0700) != 0)
836 fatal ("cannot mkdir %s for archive copying (error: %s)",
837 dir, strerror (errno));
839 obfd->has_armap = ibfd->has_armap;
843 this_element = bfd_openr_next_archived_file (ibfd, NULL);
844 while (this_element != (bfd *) NULL)
846 /* Create an output file for this member. */
847 char *output_name = cat (dir, "/", bfd_get_filename(this_element));
848 bfd *output_bfd = bfd_openw (output_name, output_target);
851 l = (struct name_list *) xmalloc (sizeof (struct name_list));
852 l->name = output_name;
856 if (output_bfd == (bfd *) NULL)
858 nonfatal (output_name);
860 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
862 nonfatal (bfd_get_filename (obfd));
865 if (bfd_check_format (this_element, bfd_object) == true)
867 copy_object (this_element, output_bfd);
870 bfd_close (output_bfd);
872 /* Open the newly output file and attach to our list. */
873 output_bfd = bfd_openr (output_name, output_target);
875 l->obfd = output_bfd;
878 ptr = &output_bfd->next;
880 last_element = this_element;
882 this_element = bfd_openr_next_archived_file (ibfd, last_element);
884 bfd_close (last_element);
888 if (!bfd_close (obfd))
890 nonfatal (bfd_get_filename (obfd));
893 if (!bfd_close (ibfd))
895 nonfatal (bfd_get_filename (ibfd));
898 /* Delete all the files that we opened. */
899 for (l = list; l != NULL; l = l->next)
907 /* The top-level control. */
910 copy_file (input_filename, output_filename, input_target, output_target)
911 char *input_filename;
912 char *output_filename;
919 /* To allow us to do "strip *" without dying on the first
920 non-object file, failures are nonfatal. */
922 ibfd = bfd_openr (input_filename, input_target);
925 nonfatal (input_filename);
928 if (bfd_check_format (ibfd, bfd_archive))
932 /* bfd_get_target does not return the correct value until
933 bfd_check_format succeeds. */
934 if (output_target == NULL)
935 output_target = bfd_get_target (ibfd);
937 obfd = bfd_openw (output_filename, output_target);
940 nonfatal (output_filename);
942 copy_archive (ibfd, obfd, output_target);
944 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
948 /* bfd_get_target does not return the correct value until
949 bfd_check_format succeeds. */
950 if (output_target == NULL)
951 output_target = bfd_get_target (ibfd);
953 obfd = bfd_openw (output_filename, output_target);
956 nonfatal (output_filename);
959 copy_object (ibfd, obfd);
961 if (!bfd_close (obfd))
963 nonfatal (output_filename);
966 if (!bfd_close (ibfd))
968 nonfatal (input_filename);
973 bfd_nonfatal (input_filename);
974 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
976 list_matching_formats (matching);
983 /* Create a section in OBFD with the same name and attributes
984 as ISECTION in IBFD. */
987 setup_section (ibfd, isection, obfdarg)
992 bfd *obfd = (bfd *) obfdarg;
993 struct section_list *p;
1000 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1001 && (strip_symbols == strip_debug
1002 || strip_symbols == strip_unneeded
1003 || strip_symbols == strip_all
1004 || discard_locals == locals_all
1005 || convert_debugging))
1008 p = find_section_list (bfd_section_name (ibfd, isection), false);
1012 if (p != NULL && p->remove)
1015 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1016 if (osection == NULL)
1022 if (!bfd_set_section_size (obfd,
1024 bfd_section_size (ibfd, isection)))
1030 vma = bfd_section_vma (ibfd, isection);
1031 if (p != NULL && p->adjust == adjust_vma)
1033 else if (p != NULL && p->adjust == set_vma)
1036 vma += adjust_section_vma;
1037 if (! bfd_set_section_vma (obfd, osection, vma))
1043 lma = isection->lma;
1044 if (p != NULL && p->adjust == adjust_vma)
1046 else if (p != NULL && p->adjust == set_vma)
1049 lma += adjust_section_vma;
1050 osection->lma = lma;
1052 if (bfd_set_section_alignment (obfd,
1054 bfd_section_alignment (ibfd, isection))
1061 flags = bfd_get_section_flags (ibfd, isection);
1062 if (p != NULL && p->set_flags)
1063 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1064 if (!bfd_set_section_flags (obfd, osection, flags))
1070 /* This used to be mangle_section; we do here to avoid using
1071 bfd_get_section_by_name since some formats allow multiple
1072 sections with the same name. */
1073 isection->output_section = osection;
1074 isection->output_offset = 0;
1076 /* Allow the BFD backend to copy any private data it understands
1077 from the input section to the output section. */
1078 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1080 err = "private data";
1088 fprintf (stderr, "%s: %s: section `%s': error in %s: %s\n",
1090 bfd_get_filename (ibfd), bfd_section_name (ibfd, isection),
1091 err, bfd_errmsg (bfd_get_error ()));
1095 /* Copy the data of input section ISECTION of IBFD
1096 to an output section with the same name in OBFD.
1097 If stripping then don't copy any relocation info. */
1100 copy_section (ibfd, isection, obfdarg)
1105 bfd *obfd = (bfd *) obfdarg;
1106 struct section_list *p;
1112 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1113 && (strip_symbols == strip_debug
1114 || strip_symbols == strip_unneeded
1115 || strip_symbols == strip_all
1116 || discard_locals == locals_all
1117 || convert_debugging))
1122 p = find_section_list (bfd_section_name (ibfd, isection), false);
1124 if (p != NULL && p->remove)
1127 osection = isection->output_section;
1128 size = bfd_get_section_size_before_reloc (isection);
1130 if (size == 0 || osection == 0)
1133 if (strip_symbols == strip_all)
1134 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1139 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1142 nonfatal (bfd_get_filename (ibfd));
1145 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1148 relpp = (arelent **) xmalloc (relsize);
1149 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1152 nonfatal (bfd_get_filename (ibfd));
1154 bfd_set_reloc (obfd, osection, relpp, relcount);
1158 isection->_cooked_size = isection->_raw_size;
1159 isection->reloc_done = true;
1161 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1163 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1165 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1168 nonfatal (bfd_get_filename (ibfd));
1173 filter_bytes (memhunk, &size);
1174 /* The section has gotten smaller. */
1175 if (!bfd_set_section_size (obfd, osection, size))
1176 nonfatal (bfd_get_filename (obfd));
1179 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1182 nonfatal (bfd_get_filename (obfd));
1188 /* Get all the sections. This is used when --gap-fill or --pad-to is
1192 get_sections (obfd, osection, secppparg)
1197 asection ***secppp = (asection ***) secppparg;
1199 **secppp = osection;
1203 /* Sort sections by VMA. This is called via qsort, and is used when
1204 --gap-fill or --pad-to is used. We force non loadable or empty
1205 sections to the front, where they are easier to ignore. */
1208 compare_section_vma (arg1, arg2)
1212 const asection **sec1 = (const asection **) arg1;
1213 const asection **sec2 = (const asection **) arg2;
1214 flagword flags1, flags2;
1216 /* Sort non loadable sections to the front. */
1217 flags1 = (*sec1)->flags;
1218 flags2 = (*sec2)->flags;
1219 if ((flags1 & SEC_HAS_CONTENTS) == 0
1220 || (flags1 & SEC_LOAD) == 0)
1222 if ((flags2 & SEC_HAS_CONTENTS) != 0
1223 && (flags2 & SEC_LOAD) != 0)
1228 if ((flags2 & SEC_HAS_CONTENTS) == 0
1229 || (flags2 & SEC_LOAD) == 0)
1233 /* Sort sections by VMA. */
1234 if ((*sec1)->vma > (*sec2)->vma)
1236 else if ((*sec1)->vma < (*sec2)->vma)
1239 /* Sort sections with the same VMA by size. */
1240 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1242 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1248 /* Mark all the symbols which will be used in output relocations with
1249 the BSF_KEEP flag so that those symbols will not be stripped.
1251 Ignore relocations which will not appear in the output file. */
1254 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1259 asymbol **symbols = (asymbol **) symbolsarg;
1264 /* Ignore an input section with no corresponding output section. */
1265 if (isection->output_section == NULL)
1268 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1270 bfd_fatal (bfd_get_filename (ibfd));
1275 relpp = (arelent **) xmalloc (relsize);
1276 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1278 bfd_fatal (bfd_get_filename (ibfd));
1280 /* Examine each symbol used in a relocation. If it's not one of the
1281 special bfd section symbols, then mark it with BSF_KEEP. */
1282 for (i = 0; i < relcount; i++)
1284 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1285 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1286 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1287 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1294 /* Write out debugging information. */
1297 write_debugging_info (obfd, dhandle, symcountp, symppp)
1303 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1304 return write_ieee_debugging_info (obfd, dhandle);
1307 "%s: don't know how to write debugging information for %s\n",
1308 bfd_get_filename (obfd), bfd_get_target (obfd));
1312 /* The number of bytes to copy at once. */
1313 #define COPY_BUF 8192
1315 /* Copy file FROM to file TO, performing no translations.
1316 Return 0 if ok, -1 if error. */
1319 simple_copy (from, to)
1322 int fromfd, tofd, nread;
1326 fromfd = open (from, O_RDONLY);
1329 tofd = creat (to, 0777);
1337 while ((nread = read (fromfd, buf, sizeof buf)) > 0)
1339 if (write (tofd, buf, nread) != nread)
1361 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1363 #define S_ISLNK(m) 0
1368 /* Rename FROM to TO, copying if TO is a link.
1369 Assumes that TO already exists, because FROM is a temp file.
1370 Return 0 if ok, -1 if error. */
1373 smart_rename (from, to)
1382 /* Use rename only if TO is not a symbolic link and has
1383 only one hard link. */
1384 if (!S_ISLNK (s.st_mode) && s.st_nlink == 1)
1386 ret = rename (from, to);
1389 /* Try to preserve the permission bits and ownership of TO. */
1390 chmod (to, s.st_mode & 07777);
1391 chown (to, s.st_uid, s.st_gid);
1395 /* We have to clean up here. */
1397 fprintf (stderr, "%s: %s: ", program_name, to);
1405 ret = simple_copy (from, to);
1409 fprintf (stderr, "%s: %s: ", program_name, to);
1411 perror ("simple_copy");
1419 strip_main (argc, argv)
1423 char *input_target = NULL, *output_target = NULL;
1424 boolean show_version = false;
1426 struct section_list *p;
1428 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:sSgxXVv",
1429 strip_options, (int *) 0)) != EOF)
1434 input_target = optarg;
1437 output_target = optarg;
1440 input_target = output_target = optarg;
1443 p = find_section_list (optarg, true);
1445 sections_removed = true;
1448 strip_symbols = strip_all;
1452 strip_symbols = strip_debug;
1454 case OPTION_STRIP_UNNEEDED:
1455 strip_symbols = strip_unneeded;
1458 if (! keep_symbols && strip_specific_list != NULL)
1460 fprintf (stderr, "%s: Can not specify both -K and -N\n",
1462 strip_usage (stderr, 1);
1464 keep_symbols = true;
1465 add_strip_symbol (optarg);
1470 fprintf (stderr, "%s: Can not specify both -K and -N\n",
1472 strip_usage (stderr, 1);
1474 add_strip_symbol (optarg);
1477 discard_locals = locals_all;
1480 discard_locals = locals_start_L;
1486 show_version = true;
1489 break; /* we've been given a long option */
1491 strip_usage (stdout, 0);
1493 strip_usage (stderr, 1);
1499 printf ("GNU %s version %s\n", program_name, program_version);
1503 /* Default is to strip all symbols. */
1504 if (strip_symbols == strip_undef
1505 && discard_locals == locals_undef
1506 && strip_specific_list == NULL)
1507 strip_symbols = strip_all;
1509 if (output_target == (char *) NULL)
1510 output_target = input_target;
1514 strip_usage (stderr, 1);
1516 for (; i < argc; i++)
1518 int hold_status = status;
1520 char *tmpname = make_tempname (argv[i]);
1522 copy_file (argv[i], tmpname, input_target, output_target);
1525 smart_rename (tmpname, argv[i]);
1526 status = hold_status;
1537 copy_main (argc, argv)
1541 char *input_filename = NULL, *output_filename = NULL;
1542 char *input_target = NULL, *output_target = NULL;
1543 boolean show_version = false;
1544 boolean adjust_warn = true;
1546 struct section_list *p;
1548 while ((c = getopt_long (argc, argv, "b:i:I:K:N:s:O:d:F:R:SgxXVv",
1549 copy_options, (int *) 0)) != EOF)
1554 copy_byte = atoi(optarg);
1557 fprintf (stderr, "%s: byte number must be non-negative\n",
1563 interleave = atoi(optarg);
1566 fprintf(stderr, "%s: interleave must be positive\n",
1572 case 's': /* "source" - 'I' is preferred */
1573 input_target = optarg;
1576 case 'd': /* "destination" - 'O' is preferred */
1577 output_target = optarg;
1580 input_target = output_target = optarg;
1583 p = find_section_list (optarg, true);
1585 sections_removed = true;
1588 strip_symbols = strip_all;
1591 strip_symbols = strip_debug;
1593 case OPTION_STRIP_UNNEEDED:
1594 strip_symbols = strip_unneeded;
1597 if (! keep_symbols && strip_specific_list != NULL)
1599 fprintf (stderr, "%s: Can not specify both -K and -N\n",
1601 strip_usage (stderr, 1);
1603 keep_symbols = true;
1604 add_strip_symbol (optarg);
1609 fprintf (stderr, "%s: Can not specify both -K and -N\n",
1611 strip_usage (stderr, 1);
1613 add_strip_symbol (optarg);
1616 discard_locals = locals_all;
1619 discard_locals = locals_start_L;
1625 show_version = true;
1627 case OPTION_ADD_SECTION:
1631 struct section_add *pa;
1636 s = strchr (optarg, '=');
1640 "%s: bad format for --add-section NAME=FILENAME\n",
1645 if (stat (s + 1, &st) < 0)
1647 fprintf (stderr, "%s: ", program_name);
1652 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1655 name = (char *) xmalloc (len + 1);
1656 strncpy (name, optarg, len);
1660 pa->filename = s + 1;
1662 pa->size = st.st_size;
1664 pa->contents = (bfd_byte *) xmalloc (pa->size);
1665 f = fopen (pa->filename, FOPEN_RB);
1668 fprintf (stderr, "%s: ", program_name);
1669 perror (pa->filename);
1672 if (fread (pa->contents, 1, pa->size, f) == 0
1675 fprintf (stderr, "%s: %s: fread failed\n",
1676 program_name, pa->filename);
1681 pa->next = add_sections;
1685 case OPTION_ADJUST_START:
1686 adjust_start = parse_vma (optarg, "--adjust-start");
1688 case OPTION_ADJUST_SECTION_VMA:
1694 s = strchr (optarg, '=');
1697 s = strchr (optarg, '+');
1700 s = strchr (optarg, '-');
1704 "%s: bad format for --adjust-section-vma\n",
1712 name = (char *) xmalloc (len + 1);
1713 strncpy (name, optarg, len);
1716 p = find_section_list (name, true);
1718 p->val = parse_vma (s + 1, "--adjust-section-vma");
1721 p->adjust = set_vma;
1724 p->adjust = adjust_vma;
1730 case OPTION_ADJUST_VMA:
1731 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
1732 adjust_start = adjust_section_vma;
1734 case OPTION_ADJUST_WARNINGS:
1737 case OPTION_DEBUGGING:
1738 convert_debugging = true;
1740 case OPTION_GAP_FILL:
1742 bfd_vma gap_fill_vma;
1744 gap_fill_vma = parse_vma (optarg, "--gap-fill");
1745 gap_fill = (bfd_byte) gap_fill_vma;
1746 if ((bfd_vma) gap_fill != gap_fill_vma)
1748 fprintf (stderr, "%s: warning: truncating gap-fill from 0x",
1750 fprintf_vma (stderr, gap_fill_vma);
1751 fprintf (stderr, "to 0x%x\n", (unsigned int) gap_fill);
1753 gap_fill_set = true;
1756 case OPTION_NO_ADJUST_WARNINGS:
1757 adjust_warn = false;
1760 pad_to = parse_vma (optarg, "--pad-to");
1763 case OPTION_SET_SECTION_FLAGS:
1769 s = strchr (optarg, '=');
1772 fprintf (stderr, "%s: bad format for --set-section-flags\n",
1778 name = (char *) xmalloc (len + 1);
1779 strncpy (name, optarg, len);
1782 p = find_section_list (name, true);
1784 p->set_flags = true;
1785 p->flags = parse_flags (s + 1);
1788 case OPTION_SET_START:
1789 set_start = parse_vma (optarg, "--set-start");
1790 set_start_set = true;
1793 break; /* we've been given a long option */
1795 copy_usage (stdout, 0);
1797 copy_usage (stderr, 1);
1803 printf ("GNU %s version %s\n", program_name, program_version);
1807 if (copy_byte >= interleave)
1809 fprintf (stderr, "%s: byte number must be less than interleave\n",
1814 if (optind == argc || optind + 2 < argc)
1815 copy_usage (stderr, 1);
1817 input_filename = argv[optind];
1818 if (optind + 1 < argc)
1819 output_filename = argv[optind + 1];
1821 /* Default is to strip no symbols. */
1822 if (strip_symbols == strip_undef && discard_locals == locals_undef)
1823 strip_symbols = strip_none;
1825 if (output_target == (char *) NULL)
1826 output_target = input_target;
1828 /* If there is no destination file then create a temp and rename
1829 the result into the input. */
1831 if (output_filename == (char *) NULL)
1833 char *tmpname = make_tempname (input_filename);
1834 copy_file (input_filename, tmpname, input_target, output_target);
1836 smart_rename (tmpname, input_filename);
1842 copy_file (input_filename, output_filename, input_target, output_target);
1847 for (p = adjust_sections; p != NULL; p = p->next)
1849 if (! p->used && p->adjust != ignore_vma)
1851 fprintf (stderr, "%s: warning: --adjust-section-vma %s%c0x",
1852 program_name, p->name,
1853 p->adjust == set_vma ? '=' : '+');
1854 fprintf_vma (stderr, p->val);
1855 fprintf (stderr, " never used\n");
1868 program_name = argv[0];
1869 xmalloc_set_program_name (program_name);
1871 START_PROGRESS (program_name, 0);
1873 strip_symbols = strip_undef;
1874 discard_locals = locals_undef;
1880 int i = strlen (program_name);
1881 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
1885 strip_main (argc, argv);
1887 copy_main (argc, argv);
1889 END_PROGRESS (program_name);