1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "libiberty.h"
26 static bfd_vma parse_vma PARAMS ((const char *, const char *));
27 static void setup_section PARAMS ((bfd *, asection *, PTR));
28 static void copy_section PARAMS ((bfd *, asection *, PTR));
29 static void get_sections PARAMS ((bfd *, asection *, PTR));
30 static int compare_section_vma PARAMS ((const PTR, const PTR));
31 static void add_strip_symbol PARAMS ((const char *));
32 static int is_strip_symbol PARAMS ((const char *));
33 static unsigned int filter_symbols
34 PARAMS ((bfd *, asymbol **, asymbol **, long));
35 static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
37 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
39 static asymbol **isympp = NULL; /* Input symbols */
40 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
42 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
43 static int copy_byte = -1;
44 static int interleave = 4;
46 static boolean verbose; /* Print file and target names. */
47 static int status = 0; /* Exit status. */
52 strip_none, /* don't strip */
53 strip_debug, /* strip all debugger symbols */
54 strip_all /* strip all symbols */
57 /* Which symbols to remove. */
58 static enum strip_action strip_symbols;
63 locals_start_L, /* discard locals starting with L */
64 locals_all /* discard all locals */
67 /* Which local symbols to remove. Overrides strip_all. */
68 static enum locals_action discard_locals;
70 /* Structure used to hold lists of sections and actions to take. */
74 /* Next section to adjust. */
75 struct section_list *next;
78 /* Whether this entry was used. */
80 /* Remaining fields only used if not on remove_sections list. */
81 /* Whether to adjust or set VMA. */
83 /* Amount to adjust by or set to. */
87 /* List of sections to remove. */
89 static struct section_list *remove_sections;
91 /* Adjustments to the start address. */
92 static bfd_vma adjust_start = 0;
93 static boolean set_start_set = false;
94 static bfd_vma set_start;
96 /* Adjustments to section VMA's. */
97 static bfd_vma adjust_section_vma = 0;
98 static struct section_list *adjust_sections;
100 /* Filling gaps between sections. */
101 static boolean gap_fill_set = false;
102 static bfd_byte gap_fill = 0;
104 /* Pad to a given address. */
105 static boolean pad_to_set = false;
106 static bfd_vma pad_to;
108 /* Options to handle if running as "strip". */
110 static struct option strip_options[] =
112 {"discard-all", no_argument, 0, 'x'},
113 {"discard-locals", no_argument, 0, 'X'},
114 {"format", required_argument, 0, 'F'}, /* Obsolete */
115 {"help", no_argument, 0, 'h'},
116 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
117 {"input-target", required_argument, 0, 'I'},
118 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
119 {"output-target", required_argument, 0, 'O'},
120 {"remove-section", required_argument, 0, 'R'},
121 {"strip-all", no_argument, 0, 's'},
122 {"strip-debug", no_argument, 0, 'S'},
123 {"strip-symbol", required_argument, 0, 'N'},
124 {"target", required_argument, 0, 'F'},
125 {"verbose", no_argument, 0, 'v'},
126 {"version", no_argument, 0, 'V'},
127 {0, no_argument, 0, 0}
130 /* Options to handle if running as "objcopy". */
132 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
134 #define OPTION_ADJUST_START 150
135 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
136 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
137 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
138 #define OPTION_GAP_FILL (OPTION_ADJUST_WARNINGS + 1)
139 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
140 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
141 #define OPTION_SET_START (OPTION_PAD_TO + 1)
143 static struct option copy_options[] =
145 {"adjust-start", required_argument, 0, OPTION_ADJUST_START},
146 {"adjust-vma", required_argument, 0, OPTION_ADJUST_VMA},
147 {"adjust-section-vma", required_argument, 0, OPTION_ADJUST_SECTION_VMA},
148 {"adjust-warnings", no_argument, 0, OPTION_ADJUST_WARNINGS},
149 {"byte", required_argument, 0, 'b'},
150 {"discard-all", no_argument, 0, 'x'},
151 {"discard-locals", no_argument, 0, 'X'},
152 {"format", required_argument, 0, 'F'}, /* Obsolete */
153 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
154 {"help", no_argument, 0, 'h'},
155 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
156 {"input-target", required_argument, 0, 'I'},
157 {"interleave", required_argument, 0, 'i'},
158 {"no-adjust-warnings", no_argument, 0, OPTION_NO_ADJUST_WARNINGS},
159 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
160 {"output-target", required_argument, 0, 'O'},
161 {"pad-to", required_argument, 0, OPTION_PAD_TO},
162 {"remove-section", required_argument, 0, 'R'},
163 {"set-start", required_argument, 0, OPTION_SET_START},
164 {"strip-all", no_argument, 0, 'S'},
165 {"strip-debug", no_argument, 0, 'g'},
166 {"strip-symbol", required_argument, 0, 'N'},
167 {"target", required_argument, 0, 'F'},
168 {"verbose", no_argument, 0, 'v'},
169 {"version", no_argument, 0, 'V'},
170 {0, no_argument, 0, 0}
174 extern char *program_name;
175 extern char *program_version;
177 /* This flag distinguishes between strip and objcopy:
178 1 means this is 'strip'; 0 means this is 'objcopy'.
179 -1 means if we should use argv[0] to decide. */
184 copy_usage (stream, status)
189 Usage: %s [-vVSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
190 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
191 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
192 [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
193 [--remove-section=section] [--gap-fill=val] [--pad-to=address]\n\
194 [--set-start=val] [--adjust-start=incr] [--adjust-vma=incr]\n\
195 [--adjust-section-vma=section{=,+,-}val] [--adjust-warnings]\n\
196 [--no-adjust-warnings] [--verbose] [--version] [--help]\n\
197 [--strip-symbol symbol] [-N symbol]\n\
198 in-file [out-file]\n",
204 strip_usage (stream, status)
209 Usage: %s [-vVsSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
210 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
211 [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
212 [--strip-symbol symbol] [-N symbol]\n\
213 [--remove-section=section] [--verbose] [--version] [--help] file...\n",
218 /* Parse a string into a VMA, with a fatal error if it can't be
229 ret = bfd_scan_vma (s, &end, 0);
232 fprintf (stderr, "%s: %s: bad number: %s\n", program_name, arg, s);
238 /* Return the name of a temporary file in the same directory as FILENAME. */
241 make_tempname (filename)
244 static char template[] = "stXXXXXX";
246 char *slash = strrchr (filename, '/');
248 if (slash != (char *) NULL)
251 tmpname = xmalloc (strlen (filename) + sizeof (template) + 1);
252 strcpy (tmpname, filename);
253 strcat (tmpname, "/");
254 strcat (tmpname, template);
260 tmpname = xmalloc (sizeof (template));
261 strcpy (tmpname, template);
267 /* Make a list of symbols to explicitly strip out. A linked list is
268 good enough for a small number from the command line, but this will
269 slow things down a lot if many symbols are being deleted. */
274 struct symlist *next;
277 static struct symlist *strip_specific_list = NULL;
280 add_strip_symbol (name)
283 struct symlist *tmp_list;
285 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
286 tmp_list->name = name;
287 tmp_list->next = strip_specific_list;
288 strip_specific_list = tmp_list;
292 is_strip_symbol (name)
295 struct symlist *tmp_list;
297 for (tmp_list = strip_specific_list; tmp_list; tmp_list = tmp_list->next)
299 if (strcmp (name, tmp_list->name) == 0)
305 /* Choose which symbol entries to copy; put the result in OSYMS.
306 We don't copy in place, because that confuses the relocs.
307 Return the number of symbols to print. */
310 filter_symbols (abfd, osyms, isyms, symcount)
312 asymbol **osyms, **isyms;
315 register asymbol **from = isyms, **to = osyms;
316 long src_count = 0, dst_count = 0;
318 for (; src_count < symcount; src_count++)
320 asymbol *sym = from[src_count];
321 flagword flags = sym->flags;
324 if ((flags & BSF_GLOBAL) /* Keep if external. */
325 || (flags & BSF_KEEP) /* Keep if used in a relocation. */
326 || bfd_is_und_section (bfd_get_section (sym))
327 || bfd_is_com_section (bfd_get_section (sym)))
329 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
330 keep = strip_symbols != strip_debug;
331 else /* Local symbol. */
332 keep = discard_locals != locals_all
333 && (discard_locals != locals_start_L ||
334 ! bfd_is_local_label (abfd, sym));
336 if (keep && is_strip_symbol (bfd_asymbol_name (sym)))
340 to[dst_count++] = sym;
346 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
350 filter_bytes (memhunk, size)
354 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
356 for (; from < end; from += interleave)
361 /* Copy object file IBFD onto OBFD. */
364 copy_object (ibfd, obfd)
370 asection **osections = NULL;
371 bfd_size_type *gaps = NULL;
372 bfd_size_type max_gap = 0;
374 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
376 nonfatal (bfd_get_filename (obfd));
380 printf ("copy from %s(%s) to %s(%s)\n",
381 bfd_get_filename(ibfd), bfd_get_target(ibfd),
382 bfd_get_filename(obfd), bfd_get_target(obfd));
387 start = bfd_get_start_address (ibfd);
388 start += adjust_start;
390 if (!bfd_set_start_address (obfd, start)
391 || !bfd_set_file_flags (obfd,
392 (bfd_get_file_flags (ibfd)
393 & bfd_applicable_file_flags (obfd))))
395 nonfatal (bfd_get_filename (ibfd));
398 /* Copy architecture of input file to output file */
399 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
400 bfd_get_mach (ibfd)))
402 fprintf (stderr, "Output file cannot represent architecture %s\n",
403 bfd_printable_arch_mach (bfd_get_arch (ibfd),
404 bfd_get_mach (ibfd)));
406 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
408 nonfatal (bfd_get_filename(ibfd));
413 if (osympp != isympp)
416 /* bfd mandates that all output sections be created and sizes set before
417 any output is done. Thus, we traverse all sections multiple times. */
418 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
420 if (gap_fill_set || pad_to_set)
425 /* We must fill in gaps between the sections and/or we must pad
426 the last section to a specified address. We do this by
427 grabbing a list of the sections, sorting them by VMA, and
428 increasing the section sizes as required to fill the gaps.
429 We write out the gap contents below. */
431 c = bfd_count_sections (obfd);
432 osections = (asection **) xmalloc (c * sizeof (asection *));
434 bfd_map_over_sections (obfd, get_sections, (void *) &set);
436 qsort (osections, c, sizeof (asection *), compare_section_vma);
438 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
439 memset (gaps, 0, c * sizeof (bfd_size_type));
443 for (i = 0; i < c - 1; i++)
447 bfd_vma gap_start, gap_stop;
449 flags = bfd_get_section_flags (obfd, osections[i]);
450 if ((flags & SEC_HAS_CONTENTS) == 0
451 || (flags & SEC_LOAD) == 0)
454 size = bfd_section_size (obfd, osections[i]);
455 gap_start = bfd_section_vma (obfd, osections[i]) + size;
456 gap_stop = bfd_section_vma (obfd, osections[i + 1]);
457 if (gap_start < gap_stop)
459 if (! bfd_set_section_size (obfd, osections[i],
460 size + (gap_stop - gap_start)))
462 fprintf (stderr, "%s: Can't fill gap after %s: %s\n",
464 bfd_get_section_name (obfd, osections[i]),
465 bfd_errmsg (bfd_get_error()));
469 gaps[i] = gap_stop - gap_start;
470 if (max_gap < gap_stop - gap_start)
471 max_gap = gap_stop - gap_start;
481 vma = bfd_section_vma (obfd, osections[c - 1]);
482 size = bfd_section_size (obfd, osections[c - 1]);
483 if (vma + size < pad_to)
485 if (! bfd_set_section_size (obfd, osections[c - 1],
488 fprintf (stderr, "%s: Can't add padding to %s: %s\n",
490 bfd_get_section_name (obfd, osections[c - 1]),
491 bfd_errmsg (bfd_get_error ()));
496 gaps[c - 1] = pad_to - (vma + size);
497 if (max_gap < pad_to - (vma + size))
498 max_gap = pad_to - (vma + size);
504 /* Symbol filtering must happen after the output sections have
505 been created, but before their contents are set. */
506 if (strip_symbols == strip_all && discard_locals == locals_undef)
508 osympp = isympp = NULL;
515 symsize = bfd_get_symtab_upper_bound (ibfd);
518 nonfatal (bfd_get_filename (ibfd));
521 osympp = isympp = (asymbol **) xmalloc (symsize);
522 symcount = bfd_canonicalize_symtab (ibfd, isympp);
525 nonfatal (bfd_get_filename (ibfd));
528 if (strip_symbols == strip_debug
529 || discard_locals != locals_undef
530 || strip_specific_list)
532 /* Mark symbols used in output relocations so that they
533 are kept, even if they are local labels or static symbols.
535 Note we iterate over the input sections examining their
536 relocations since the relocations for the output sections
537 haven't been set yet. mark_symbols_used_in_relocations will
538 ignore input sections which have no corresponding output
540 bfd_map_over_sections (ibfd,
541 mark_symbols_used_in_relocations,
543 osympp = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
544 symcount = filter_symbols (ibfd, osympp, isympp, symcount);
548 bfd_set_symtab (obfd, osympp, symcount);
550 /* This has to happen after the symbol table has been set. */
551 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
553 if (gap_fill_set || pad_to_set)
558 /* Fill in the gaps. */
562 buf = (bfd_byte *) xmalloc (max_gap);
563 memset (buf, gap_fill, max_gap);
565 c = bfd_count_sections (obfd);
566 for (i = 0; i < c; i++)
574 off = bfd_section_size (obfd, osections[i]) - left;
583 if (! bfd_set_section_contents (obfd, osections[i], buf,
586 nonfatal (bfd_get_filename (obfd));
596 /* Allow the BFD backend to copy any private data it understands
597 from the input BFD to the output BFD. This is done last to
598 permit the routine to look at the filtered symbol table, which is
599 important for the ECOFF code at least. */
600 if (!bfd_copy_private_bfd_data (ibfd, obfd))
602 fprintf (stderr, "%s: %s: error copying private BFD data: %s\n",
603 program_name, bfd_get_filename (obfd),
604 bfd_errmsg (bfd_get_error ()));
616 size_t size = strlen (a) + strlen (b) + strlen (c);
617 char *r = xmalloc (size + 1);
625 /* Read each archive element in turn from IBFD, copy the
626 contents to temp file, and keep the temp file handle. */
629 copy_archive (ibfd, obfd, output_target)
636 struct name_list *next;
639 bfd **ptr = &obfd->archive_head;
641 char *dir = make_tempname (bfd_get_filename (obfd));
643 /* Make a temp directory to hold the contents. */
645 obfd->has_armap = ibfd->has_armap;
649 this_element = bfd_openr_next_archived_file (ibfd, NULL);
650 while (this_element != (bfd *) NULL)
652 /* Create an output file for this member. */
653 char *output_name = cat (dir, "/", bfd_get_filename(this_element));
654 bfd *output_bfd = bfd_openw (output_name, output_target);
657 l = (struct name_list *) xmalloc (sizeof (struct name_list));
658 l->name = output_name;
662 if (output_bfd == (bfd *) NULL)
664 nonfatal (output_name);
666 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
668 nonfatal (bfd_get_filename (obfd));
671 if (bfd_check_format (this_element, bfd_object) == true)
673 copy_object (this_element, output_bfd);
676 bfd_close (output_bfd);
678 /* Open the newly output file and attach to our list. */
679 output_bfd = bfd_openr (output_name, output_target);
682 ptr = &output_bfd->next;
684 last_element = this_element;
686 this_element = bfd_openr_next_archived_file (ibfd, last_element);
688 bfd_close (last_element);
692 if (!bfd_close (obfd))
694 nonfatal (bfd_get_filename (obfd));
697 /* Delete all the files that we opened. */
698 for (l = list; l != NULL; l = l->next)
702 if (!bfd_close (ibfd))
704 nonfatal (bfd_get_filename (ibfd));
708 /* The top-level control. */
711 copy_file (input_filename, output_filename, input_target, output_target)
712 char *input_filename;
713 char *output_filename;
720 /* To allow us to do "strip *" without dying on the first
721 non-object file, failures are nonfatal. */
723 ibfd = bfd_openr (input_filename, input_target);
726 nonfatal (input_filename);
729 if (bfd_check_format (ibfd, bfd_archive))
733 /* bfd_get_target does not return the correct value until
734 bfd_check_format succeeds. */
735 if (output_target == NULL)
736 output_target = bfd_get_target (ibfd);
738 obfd = bfd_openw (output_filename, output_target);
741 nonfatal (output_filename);
743 copy_archive (ibfd, obfd, output_target);
745 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
749 /* bfd_get_target does not return the correct value until
750 bfd_check_format succeeds. */
751 if (output_target == NULL)
752 output_target = bfd_get_target (ibfd);
754 obfd = bfd_openw (output_filename, output_target);
757 nonfatal (output_filename);
760 copy_object (ibfd, obfd);
762 if (!bfd_close (obfd))
764 nonfatal (output_filename);
767 if (!bfd_close (ibfd))
769 nonfatal (input_filename);
774 bfd_nonfatal (input_filename);
775 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
777 list_matching_formats (matching);
784 /* Create a section in OBFD with the same name and attributes
785 as ISECTION in IBFD. */
788 setup_section (ibfd, isection, obfdarg)
793 bfd *obfd = (bfd *) obfdarg;
794 struct section_list *p;
799 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
800 && (strip_symbols == strip_debug
801 || strip_symbols == strip_all
802 || discard_locals == locals_all))
805 for (p = remove_sections; p != NULL; p = p->next)
807 if (strcmp (p->name, bfd_section_name (ibfd, isection)) == 0)
814 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
815 if (osection == NULL)
821 if (!bfd_set_section_size (obfd,
823 bfd_section_size (ibfd, isection)))
829 vma = bfd_section_vma (ibfd, isection);
830 for (p = adjust_sections; p != NULL; p = p->next)
832 if (strcmp (p->name, bfd_section_name (ibfd, isection)) == 0)
843 vma += adjust_section_vma;
845 if (! bfd_set_section_vma (obfd, osection, vma))
851 if (bfd_set_section_alignment (obfd,
853 bfd_section_alignment (ibfd, isection))
860 if (!bfd_set_section_flags (obfd, osection,
861 bfd_get_section_flags (ibfd, isection)))
867 /* This used to be mangle_section; we do here to avoid using
868 bfd_get_section_by_name since some formats allow multiple
869 sections with the same name. */
870 isection->output_section = osection;
871 isection->output_offset = 0;
873 /* Allow the BFD backend to copy any private data it understands
874 from the input section to the output section. */
875 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
877 err = "private data";
885 fprintf (stderr, "%s: %s: section `%s': error in %s: %s\n",
887 bfd_get_filename (ibfd), bfd_section_name (ibfd, isection),
888 err, bfd_errmsg (bfd_get_error ()));
892 /* Copy the data of input section ISECTION of IBFD
893 to an output section with the same name in OBFD.
894 If stripping then don't copy any relocation info. */
897 copy_section (ibfd, isection, obfdarg)
902 bfd *obfd = (bfd *) obfdarg;
903 struct section_list *p;
909 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
910 && (strip_symbols == strip_debug
911 || strip_symbols == strip_all
912 || discard_locals == locals_all))
917 for (p = remove_sections; p != NULL; p = p->next)
918 if (strcmp (p->name, bfd_section_name (ibfd, isection)) == 0)
921 osection = isection->output_section;
922 size = bfd_get_section_size_before_reloc (isection);
924 if (size == 0 || osection == 0)
927 if (strip_symbols == strip_all)
928 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
933 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
936 nonfatal (bfd_get_filename (ibfd));
939 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
942 relpp = (arelent **) xmalloc (relsize);
943 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
946 nonfatal (bfd_get_filename (ibfd));
948 bfd_set_reloc (obfd, osection, relpp, relcount);
952 isection->_cooked_size = isection->_raw_size;
953 isection->reloc_done = true;
955 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
957 PTR memhunk = (PTR) xmalloc ((unsigned) size);
959 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
962 nonfatal (bfd_get_filename (ibfd));
967 filter_bytes (memhunk, &size);
968 /* The section has gotten smaller. */
969 if (!bfd_set_section_size (obfd, osection, size))
970 nonfatal (bfd_get_filename (obfd));
973 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
976 nonfatal (bfd_get_filename (obfd));
982 /* Get all the sections. This is used when --gap-fill or --pad-to is
986 get_sections (obfd, osection, secppparg)
991 asection ***secppp = (asection ***) secppparg;
997 /* Sort sections by VMA. This is called via qsort, and is used when
998 --gap-fill or --pad-to is used. We force non loadable or empty
999 sections to the front, where they are easier to ignore. */
1002 compare_section_vma (arg1, arg2)
1006 const asection **sec1 = (const asection **) arg1;
1007 const asection **sec2 = (const asection **) arg2;
1008 flagword flags1, flags2;
1010 /* Sort non loadable sections to the front. */
1011 flags1 = (*sec1)->flags;
1012 flags2 = (*sec2)->flags;
1013 if ((flags1 & SEC_HAS_CONTENTS) == 0
1014 || (flags1 & SEC_LOAD) == 0)
1016 if ((flags2 & SEC_HAS_CONTENTS) != 0
1017 && (flags2 & SEC_LOAD) != 0)
1022 if ((flags2 & SEC_HAS_CONTENTS) == 0
1023 || (flags2 & SEC_LOAD) == 0)
1027 /* Sort sections by VMA. */
1028 if ((*sec1)->vma > (*sec2)->vma)
1030 else if ((*sec1)->vma < (*sec2)->vma)
1033 /* Sort sections with the same VMA by size. */
1034 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1036 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1042 /* Mark all the symbols which will be used in output relocations with
1043 the BSF_KEEP flag so that those symbols will not be stripped.
1045 Ignore relocations which will not appear in the output file. */
1048 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1053 asymbol **symbols = (asymbol **) symbolsarg;
1058 /* Ignore an input section with no corresponding output section. */
1059 if (isection->output_section == NULL)
1062 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1064 bfd_fatal (bfd_get_filename (ibfd));
1066 relpp = (arelent **) xmalloc (relsize);
1067 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1069 bfd_fatal (bfd_get_filename (ibfd));
1071 /* Examine each symbol used in a relocation. If it's not one of the
1072 special bfd section symbols, then mark it with BSF_KEEP. */
1073 for (i = 0; i < relcount; i++)
1075 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1076 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1077 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1078 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1085 /* The number of bytes to copy at once. */
1086 #define COPY_BUF 8192
1088 /* Copy file FROM to file TO, performing no translations.
1089 Return 0 if ok, -1 if error. */
1092 simple_copy (from, to)
1095 int fromfd, tofd, nread;
1098 fromfd = open (from, O_RDONLY);
1101 tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC);
1107 while ((nread = read (fromfd, buf, sizeof buf)) > 0)
1109 if (write (tofd, buf, nread) != nread)
1125 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1127 #define S_ISLNK(m) 0
1132 /* Rename FROM to TO, copying if TO is a link.
1133 Assumes that TO already exists, because FROM is a temp file.
1134 Return 0 if ok, -1 if error. */
1137 smart_rename (from, to)
1146 /* Use rename only if TO is not a symbolic link and has
1147 only one hard link. */
1148 if (!S_ISLNK (s.st_mode) && s.st_nlink == 1)
1150 ret = rename (from, to);
1153 /* Try to preserve the permission bits and ownership of TO. */
1154 chmod (to, s.st_mode & 07777);
1155 chown (to, s.st_uid, s.st_gid);
1160 ret = simple_copy (from, to);
1168 strip_main (argc, argv)
1172 char *input_target = NULL, *output_target = NULL;
1173 boolean show_version = false;
1176 while ((c = getopt_long (argc, argv, "I:O:F:R:sSgxXVvN:",
1177 strip_options, (int *) 0)) != EOF)
1182 input_target = optarg;
1185 output_target = optarg;
1188 input_target = output_target = optarg;
1192 struct section_list *n;
1194 n = (struct section_list *) xmalloc (sizeof (struct section_list));
1197 n->next = remove_sections;
1198 remove_sections = n;
1202 strip_symbols = strip_all;
1206 strip_symbols = strip_debug;
1209 add_strip_symbol (optarg);
1212 discard_locals = locals_all;
1215 discard_locals = locals_start_L;
1221 show_version = true;
1224 break; /* we've been given a long option */
1226 strip_usage (stdout, 0);
1228 strip_usage (stderr, 1);
1234 printf ("GNU %s version %s\n", program_name, program_version);
1238 /* Default is to strip all symbols. */
1239 if (strip_symbols == strip_undef
1240 && discard_locals == locals_undef
1241 && strip_specific_list == NULL)
1242 strip_symbols = strip_all;
1244 if (output_target == (char *) NULL)
1245 output_target = input_target;
1249 strip_usage (stderr, 1);
1251 for (; i < argc; i++)
1253 int hold_status = status;
1255 char *tmpname = make_tempname (argv[i]);
1257 copy_file (argv[i], tmpname, input_target, output_target);
1260 smart_rename (tmpname, argv[i]);
1261 status = hold_status;
1272 copy_main (argc, argv)
1276 char *input_filename = NULL, *output_filename = NULL;
1277 char *input_target = NULL, *output_target = NULL;
1278 boolean show_version = false;
1279 boolean adjust_warn = true;
1281 struct section_list *p;
1283 while ((c = getopt_long (argc, argv, "b:i:I:s:O:d:F:R:SgxXVvN:",
1284 copy_options, (int *) 0)) != EOF)
1289 copy_byte = atoi(optarg);
1292 fprintf (stderr, "%s: byte number must be non-negative\n",
1298 interleave = atoi(optarg);
1301 fprintf(stderr, "%s: interleave must be positive\n",
1307 case 's': /* "source" - 'I' is preferred */
1308 input_target = optarg;
1311 case 'd': /* "destination" - 'O' is preferred */
1312 output_target = optarg;
1315 input_target = output_target = optarg;
1318 p = (struct section_list *) xmalloc (sizeof (struct section_list));
1321 p->next = remove_sections;
1322 remove_sections = p;
1325 strip_symbols = strip_all;
1328 strip_symbols = strip_debug;
1331 add_strip_symbol (optarg);
1334 discard_locals = locals_all;
1337 discard_locals = locals_start_L;
1343 show_version = true;
1345 case OPTION_ADJUST_START:
1346 adjust_start = parse_vma (optarg, "--adjust-start");
1348 case OPTION_ADJUST_SECTION_VMA:
1354 p = (struct section_list *) xmalloc (sizeof (struct section_list));
1355 s = strchr (optarg, '=');
1359 p->val = parse_vma (s + 1, "--adjust-section-vma");
1363 s = strchr (optarg, '+');
1366 s = strchr (optarg, '-');
1370 "%s: bad format for --adjust-section-vma\n",
1376 p->val = parse_vma (s + 1, "--adjust-section-vma");
1382 name = (char *) xmalloc (len + 1);
1383 strncpy (name, optarg, len);
1389 p->next = adjust_sections;
1390 adjust_sections = p;
1393 case OPTION_ADJUST_VMA:
1394 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
1395 adjust_start = adjust_section_vma;
1397 case OPTION_ADJUST_WARNINGS:
1400 case OPTION_GAP_FILL:
1402 bfd_vma gap_fill_vma;
1404 gap_fill_vma = parse_vma (optarg, "--gap-fill");
1405 gap_fill = (bfd_byte) gap_fill_vma;
1406 if ((bfd_vma) gap_fill != gap_fill_vma)
1408 fprintf (stderr, "%s: warning: truncating gap-fill from 0x",
1410 fprintf_vma (stderr, gap_fill_vma);
1411 fprintf (stderr, "to 0x%x\n", (unsigned int) gap_fill);
1413 gap_fill_set = true;
1416 case OPTION_NO_ADJUST_WARNINGS:
1417 adjust_warn = false;
1420 pad_to = parse_vma (optarg, "--pad-to");
1423 case OPTION_SET_START:
1424 set_start = parse_vma (optarg, "--set-start");
1425 set_start_set = true;
1428 break; /* we've been given a long option */
1430 copy_usage (stdout, 0);
1432 copy_usage (stderr, 1);
1438 printf ("GNU %s version %s\n", program_name, program_version);
1442 if (copy_byte >= interleave)
1444 fprintf (stderr, "%s: byte number must be less than interleave\n",
1449 if (optind == argc || optind + 2 < argc)
1450 copy_usage (stderr, 1);
1452 input_filename = argv[optind];
1453 if (optind + 1 < argc)
1454 output_filename = argv[optind + 1];
1456 /* Default is to strip no symbols. */
1457 if (strip_symbols == strip_undef && discard_locals == locals_undef)
1458 strip_symbols = strip_none;
1460 if (output_target == (char *) NULL)
1461 output_target = input_target;
1463 /* If there is no destination file then create a temp and rename
1464 the result into the input. */
1466 if (output_filename == (char *) NULL)
1468 char *tmpname = make_tempname (input_filename);
1469 copy_file (input_filename, tmpname, input_target, output_target);
1471 smart_rename (tmpname, input_filename);
1477 copy_file (input_filename, output_filename, input_target, output_target);
1482 for (p = adjust_sections; p != NULL; p = p->next)
1486 fprintf (stderr, "%s: warning: --adjust-section-vma %s%c0x",
1487 program_name, p->name,
1488 p->adjust ? '=' : '+');
1489 fprintf_vma (stderr, p->val);
1490 fprintf (stderr, " never used\n");
1495 /* We could issue similar warnings for remove_sections, but I don't
1496 think that would be as useful. */
1506 program_name = argv[0];
1507 xmalloc_set_program_name (program_name);
1508 strip_symbols = strip_undef;
1509 discard_locals = locals_undef;
1515 int i = strlen (program_name);
1516 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
1520 strip_main (argc, argv);
1522 copy_main (argc, argv);