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. */
25 #include "libiberty.h"
27 static bfd_vma parse_vma
PARAMS ((const char *, const char *));
28 static flagword parse_flags
PARAMS ((const char *));
29 static char *make_tempname
PARAMS ((char *));
30 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
31 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
32 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
33 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
34 static int compare_section_vma
PARAMS ((const PTR
, const PTR
));
35 static void add_strip_symbol
PARAMS ((const char *));
36 static boolean is_strip_symbol
PARAMS ((const char *));
37 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
38 static unsigned int filter_symbols
39 PARAMS ((bfd
*, asymbol
**, asymbol
**, long));
40 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
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_all
/* strip all symbols */
62 /* Which symbols to remove. */
63 static enum strip_action strip_symbols
;
68 locals_start_L
, /* discard locals starting with L */
69 locals_all
/* discard all locals */
72 /* Which local symbols to remove. Overrides strip_all. */
73 static enum locals_action discard_locals
;
75 /* Structure used to hold lists of sections and actions to take. */
79 /* Next section to adjust. */
80 struct section_list
*next
;
83 /* Whether this entry was used. */
85 /* Whether to remove this section. */
87 /* Whether to adjust or set VMA. */
88 enum { ignore_vma
, adjust_vma
, set_vma
} adjust
;
89 /* Amount to adjust by or set to. */
91 /* Whether to set the section flags. */
93 /* What to set the section flags to. */
97 static struct section_list
*adjust_sections
;
98 static boolean sections_removed
;
100 /* Adjustments to the start address. */
101 static bfd_vma adjust_start
= 0;
102 static boolean set_start_set
= false;
103 static bfd_vma set_start
;
105 /* Adjustments to section VMA's. */
106 static bfd_vma adjust_section_vma
= 0;
108 /* Filling gaps between sections. */
109 static boolean gap_fill_set
= false;
110 static bfd_byte gap_fill
= 0;
112 /* Pad to a given address. */
113 static boolean pad_to_set
= false;
114 static bfd_vma pad_to
;
116 /* List of sections to add. */
120 /* Next section to add. */
121 struct section_add
*next
;
122 /* Name of section to add. */
124 /* Name of file holding section contents. */
125 const char *filename
;
128 /* Contents of file. */
130 /* BFD section, after it has been added. */
134 static struct section_add
*add_sections
;
136 /* Options to handle if running as "strip". */
138 static struct option strip_options
[] =
140 {"discard-all", no_argument
, 0, 'x'},
141 {"discard-locals", no_argument
, 0, 'X'},
142 {"format", required_argument
, 0, 'F'}, /* Obsolete */
143 {"help", no_argument
, 0, 'h'},
144 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
145 {"input-target", required_argument
, 0, 'I'},
146 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
147 {"output-target", required_argument
, 0, 'O'},
148 {"remove-section", required_argument
, 0, 'R'},
149 {"strip-all", no_argument
, 0, 's'},
150 {"strip-debug", no_argument
, 0, 'S'},
151 {"strip-symbol", required_argument
, 0, 'N'},
152 {"target", required_argument
, 0, 'F'},
153 {"verbose", no_argument
, 0, 'v'},
154 {"version", no_argument
, 0, 'V'},
155 {0, no_argument
, 0, 0}
158 /* Options to handle if running as "objcopy". */
160 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
162 #define OPTION_ADD_SECTION 150
163 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
164 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
165 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
166 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
167 #define OPTION_GAP_FILL (OPTION_ADJUST_WARNINGS + 1)
168 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
169 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
170 #define OPTION_SET_SECTION_FLAGS (OPTION_PAD_TO + 1)
171 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
173 static struct option copy_options
[] =
175 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
176 {"adjust-start", required_argument
, 0, OPTION_ADJUST_START
},
177 {"adjust-vma", required_argument
, 0, OPTION_ADJUST_VMA
},
178 {"adjust-section-vma", required_argument
, 0, OPTION_ADJUST_SECTION_VMA
},
179 {"adjust-warnings", no_argument
, 0, OPTION_ADJUST_WARNINGS
},
180 {"byte", required_argument
, 0, 'b'},
181 {"discard-all", no_argument
, 0, 'x'},
182 {"discard-locals", no_argument
, 0, 'X'},
183 {"format", required_argument
, 0, 'F'}, /* Obsolete */
184 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
185 {"help", no_argument
, 0, 'h'},
186 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
187 {"input-target", required_argument
, 0, 'I'},
188 {"interleave", required_argument
, 0, 'i'},
189 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_ADJUST_WARNINGS
},
190 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
191 {"output-target", required_argument
, 0, 'O'},
192 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
193 {"remove-section", required_argument
, 0, 'R'},
194 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
195 {"set-start", required_argument
, 0, OPTION_SET_START
},
196 {"strip-all", no_argument
, 0, 'S'},
197 {"strip-debug", no_argument
, 0, 'g'},
198 {"strip-symbol", required_argument
, 0, 'N'},
199 {"target", required_argument
, 0, 'F'},
200 {"verbose", no_argument
, 0, 'v'},
201 {"version", no_argument
, 0, 'V'},
202 {0, no_argument
, 0, 0}
206 extern char *program_name
;
207 extern char *program_version
;
209 /* This flag distinguishes between strip and objcopy:
210 1 means this is 'strip'; 0 means this is 'objcopy'.
211 -1 means if we should use argv[0] to decide. */
216 copy_usage (stream
, exit_status
)
221 Usage: %s [-vVSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
222 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
223 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
224 [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
225 [--remove-section=section] [--gap-fill=val] [--pad-to=address]\n",
228 [--set-start=val] [--adjust-start=incr] [--adjust-vma=incr]\n\
229 [--adjust-section-vma=section{=,+,-}val] [--adjust-warnings]\n\
230 [--no-adjust-warnings] [--set-section-flags=section=flags]\n\
231 [--add-section=sectionname=filename]\n\
232 [--strip-symbol symbol] [-N symbol] [--verbose]\n\
233 [--version] [--help]\n\
234 in-file [out-file]\n");
235 list_supported_targets (program_name
, stream
);
240 strip_usage (stream
, exit_status
)
245 Usage: %s [-vVsSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
246 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
247 [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
248 [--strip-symbol symbol] [-N symbol]\n\
249 [--remove-section=section] [--verbose] [--version] [--help] file...\n",
251 list_supported_targets (program_name
, stream
);
255 /* Parse a string into a VMA, with a fatal error if it can't be
266 ret
= bfd_scan_vma (s
, &end
, 0);
269 fprintf (stderr
, "%s: %s: bad number: %s\n", program_name
, arg
, s
);
275 /* Parse section flags into a flagword, with a fatal error if the
276 string can't be parsed. */
290 snext
= strchr (s
, ',');
299 #define PARSE_FLAG(fname,fval) if (strncmp (fname, s, len) == 0) ret |= fval;
300 PARSE_FLAG ("alloc", SEC_ALLOC
);
301 PARSE_FLAG ("load", SEC_LOAD
);
302 PARSE_FLAG ("readonly", SEC_READONLY
);
303 PARSE_FLAG ("code", SEC_CODE
);
304 PARSE_FLAG ("data", SEC_DATA
);
305 PARSE_FLAG ("rom", SEC_ROM
);
315 /* Return the name of a temporary file in the same directory as FILENAME. */
318 make_tempname (filename
)
321 static char template[] = "stXXXXXX";
323 char *slash
= strrchr (filename
, '/');
325 if (slash
!= (char *) NULL
)
328 tmpname
= xmalloc (strlen (filename
) + sizeof (template) + 1);
329 strcpy (tmpname
, filename
);
330 strcat (tmpname
, "/");
331 strcat (tmpname
, template);
337 tmpname
= xmalloc (sizeof (template));
338 strcpy (tmpname
, template);
344 /* Find and optionally add an entry in the adjust_sections list. */
346 static struct section_list
*
347 find_section_list (name
, add
)
351 register struct section_list
*p
;
353 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
354 if (strcmp (p
->name
, name
) == 0)
360 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
364 p
->adjust
= ignore_vma
;
366 p
->set_flags
= false;
369 p
->next
= adjust_sections
;
375 /* Make a list of symbols to explicitly strip out. A linked list is
376 good enough for a small number from the command line, but this will
377 slow things down a lot if many symbols are being deleted. */
382 struct symlist
*next
;
385 static struct symlist
*strip_specific_list
= NULL
;
388 add_strip_symbol (name
)
391 struct symlist
*tmp_list
;
393 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
394 tmp_list
->name
= name
;
395 tmp_list
->next
= strip_specific_list
;
396 strip_specific_list
= tmp_list
;
400 is_strip_symbol (name
)
403 struct symlist
*tmp_list
;
405 for (tmp_list
= strip_specific_list
; tmp_list
; tmp_list
= tmp_list
->next
)
407 if (strcmp (name
, tmp_list
->name
) == 0)
413 /* See if a section is being removed. */
416 is_strip_section (abfd
, sec
)
420 struct section_list
*p
;
422 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
423 && (strip_symbols
== strip_debug
424 || strip_symbols
== strip_all
425 || discard_locals
== locals_all
))
428 if (! sections_removed
)
430 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
431 return p
!= NULL
&& p
->remove
? true : false;
434 /* Choose which symbol entries to copy; put the result in OSYMS.
435 We don't copy in place, because that confuses the relocs.
436 Return the number of symbols to print. */
439 filter_symbols (abfd
, osyms
, isyms
, symcount
)
441 asymbol
**osyms
, **isyms
;
444 register asymbol
**from
= isyms
, **to
= osyms
;
445 long src_count
= 0, dst_count
= 0;
447 for (; src_count
< symcount
; src_count
++)
449 asymbol
*sym
= from
[src_count
];
450 flagword flags
= sym
->flags
;
453 if ((flags
& BSF_GLOBAL
) /* Keep if external. */
454 || (flags
& BSF_KEEP
) /* Keep if used in a relocation. */
455 || bfd_is_und_section (bfd_get_section (sym
))
456 || bfd_is_com_section (bfd_get_section (sym
)))
458 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
459 keep
= strip_symbols
!= strip_debug
;
460 else /* Local symbol. */
461 keep
= discard_locals
!= locals_all
462 && (discard_locals
!= locals_start_L
||
463 ! bfd_is_local_label (abfd
, sym
));
465 if (keep
&& is_strip_symbol (bfd_asymbol_name (sym
)))
467 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
471 to
[dst_count
++] = sym
;
477 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
481 filter_bytes (memhunk
, size
)
485 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
487 for (; from
< end
; from
+= interleave
)
492 /* Copy object file IBFD onto OBFD. */
495 copy_object (ibfd
, obfd
)
501 asection
**osections
= NULL
;
502 bfd_size_type
*gaps
= NULL
;
503 bfd_size_type max_gap
= 0;
505 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
507 nonfatal (bfd_get_filename (obfd
));
511 printf ("copy from %s(%s) to %s(%s)\n",
512 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
513 bfd_get_filename(obfd
), bfd_get_target(obfd
));
518 start
= bfd_get_start_address (ibfd
);
519 start
+= adjust_start
;
521 if (!bfd_set_start_address (obfd
, start
)
522 || !bfd_set_file_flags (obfd
,
523 (bfd_get_file_flags (ibfd
)
524 & bfd_applicable_file_flags (obfd
))))
526 nonfatal (bfd_get_filename (ibfd
));
529 /* Copy architecture of input file to output file */
530 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
531 bfd_get_mach (ibfd
)))
533 fprintf (stderr
, "Output file cannot represent architecture %s\n",
534 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
535 bfd_get_mach (ibfd
)));
537 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
539 nonfatal (bfd_get_filename(ibfd
));
544 if (osympp
!= isympp
)
547 /* bfd mandates that all output sections be created and sizes set before
548 any output is done. Thus, we traverse all sections multiple times. */
549 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
551 if (add_sections
!= NULL
)
553 struct section_add
*padd
;
554 struct section_list
*pset
;
556 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
558 padd
->section
= bfd_make_section (obfd
, padd
->name
);
559 if (padd
->section
== NULL
)
561 fprintf (stderr
, "%s: can't create section `%s': %s\n",
562 program_name
, padd
->name
,
563 bfd_errmsg (bfd_get_error ()));
571 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
572 nonfatal (bfd_get_filename (obfd
));
574 pset
= find_section_list (padd
->name
, false);
578 if (pset
!= NULL
&& pset
->set_flags
)
579 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
581 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
582 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
583 nonfatal (bfd_get_filename (obfd
));
586 && (pset
->adjust
== adjust_vma
587 || pset
->adjust
== set_vma
))
589 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->val
))
590 nonfatal (bfd_get_filename (obfd
));
596 if (gap_fill_set
|| pad_to_set
)
601 /* We must fill in gaps between the sections and/or we must pad
602 the last section to a specified address. We do this by
603 grabbing a list of the sections, sorting them by VMA, and
604 increasing the section sizes as required to fill the gaps.
605 We write out the gap contents below. */
607 c
= bfd_count_sections (obfd
);
608 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
610 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
612 qsort (osections
, c
, sizeof (asection
*), compare_section_vma
);
614 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
615 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
619 for (i
= 0; i
< c
- 1; i
++)
623 bfd_vma gap_start
, gap_stop
;
625 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
626 if ((flags
& SEC_HAS_CONTENTS
) == 0
627 || (flags
& SEC_LOAD
) == 0)
630 size
= bfd_section_size (obfd
, osections
[i
]);
631 gap_start
= bfd_section_vma (obfd
, osections
[i
]) + size
;
632 gap_stop
= bfd_section_vma (obfd
, osections
[i
+ 1]);
633 if (gap_start
< gap_stop
)
635 if (! bfd_set_section_size (obfd
, osections
[i
],
636 size
+ (gap_stop
- gap_start
)))
638 fprintf (stderr
, "%s: Can't fill gap after %s: %s\n",
640 bfd_get_section_name (obfd
, osections
[i
]),
641 bfd_errmsg (bfd_get_error()));
645 gaps
[i
] = gap_stop
- gap_start
;
646 if (max_gap
< gap_stop
- gap_start
)
647 max_gap
= gap_stop
- gap_start
;
657 vma
= bfd_section_vma (obfd
, osections
[c
- 1]);
658 size
= bfd_section_size (obfd
, osections
[c
- 1]);
659 if (vma
+ size
< pad_to
)
661 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
664 fprintf (stderr
, "%s: Can't add padding to %s: %s\n",
666 bfd_get_section_name (obfd
, osections
[c
- 1]),
667 bfd_errmsg (bfd_get_error ()));
672 gaps
[c
- 1] = pad_to
- (vma
+ size
);
673 if (max_gap
< pad_to
- (vma
+ size
))
674 max_gap
= pad_to
- (vma
+ size
);
680 /* Symbol filtering must happen after the output sections have
681 been created, but before their contents are set. */
682 if (strip_symbols
== strip_all
&& discard_locals
== locals_undef
)
684 osympp
= isympp
= NULL
;
691 symsize
= bfd_get_symtab_upper_bound (ibfd
);
694 nonfatal (bfd_get_filename (ibfd
));
697 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
698 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
701 nonfatal (bfd_get_filename (ibfd
));
704 if (strip_symbols
== strip_debug
705 || discard_locals
!= locals_undef
706 || strip_specific_list
709 /* Mark symbols used in output relocations so that they
710 are kept, even if they are local labels or static symbols.
712 Note we iterate over the input sections examining their
713 relocations since the relocations for the output sections
714 haven't been set yet. mark_symbols_used_in_relocations will
715 ignore input sections which have no corresponding output
717 bfd_map_over_sections (ibfd
,
718 mark_symbols_used_in_relocations
,
720 osympp
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
721 symcount
= filter_symbols (ibfd
, osympp
, isympp
, symcount
);
725 bfd_set_symtab (obfd
, osympp
, symcount
);
727 /* This has to happen after the symbol table has been set. */
728 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
730 if (add_sections
!= NULL
)
732 struct section_add
*padd
;
734 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
736 if (! bfd_set_section_contents (obfd
, padd
->section
,
737 (PTR
) padd
->contents
,
739 (bfd_size_type
) padd
->size
))
740 nonfatal (bfd_get_filename (obfd
));
744 if (gap_fill_set
|| pad_to_set
)
749 /* Fill in the gaps. */
753 buf
= (bfd_byte
*) xmalloc (max_gap
);
754 memset (buf
, gap_fill
, max_gap
);
756 c
= bfd_count_sections (obfd
);
757 for (i
= 0; i
< c
; i
++)
765 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
774 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
777 nonfatal (bfd_get_filename (obfd
));
786 /* Allow the BFD backend to copy any private data it understands
787 from the input BFD to the output BFD. This is done last to
788 permit the routine to look at the filtered symbol table, which is
789 important for the ECOFF code at least. */
790 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
792 fprintf (stderr
, "%s: %s: error copying private BFD data: %s\n",
793 program_name
, bfd_get_filename (obfd
),
794 bfd_errmsg (bfd_get_error ()));
806 size_t size
= strlen (a
) + strlen (b
) + strlen (c
);
807 char *r
= xmalloc (size
+ 1);
815 /* Read each archive element in turn from IBFD, copy the
816 contents to temp file, and keep the temp file handle. */
819 copy_archive (ibfd
, obfd
, output_target
)
826 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. */
835 obfd
->has_armap
= ibfd
->has_armap
;
839 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
840 while (this_element
!= (bfd
*) NULL
)
842 /* Create an output file for this member. */
843 char *output_name
= cat (dir
, "/", bfd_get_filename(this_element
));
844 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
847 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
848 l
->name
= output_name
;
852 if (output_bfd
== (bfd
*) NULL
)
854 nonfatal (output_name
);
856 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
858 nonfatal (bfd_get_filename (obfd
));
861 if (bfd_check_format (this_element
, bfd_object
) == true)
863 copy_object (this_element
, output_bfd
);
866 bfd_close (output_bfd
);
868 /* Open the newly output file and attach to our list. */
869 output_bfd
= bfd_openr (output_name
, output_target
);
872 ptr
= &output_bfd
->next
;
874 last_element
= this_element
;
876 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
878 bfd_close (last_element
);
882 if (!bfd_close (obfd
))
884 nonfatal (bfd_get_filename (obfd
));
887 /* Delete all the files that we opened. */
888 for (l
= list
; l
!= NULL
; l
= l
->next
)
892 if (!bfd_close (ibfd
))
894 nonfatal (bfd_get_filename (ibfd
));
898 /* The top-level control. */
901 copy_file (input_filename
, output_filename
, input_target
, output_target
)
902 char *input_filename
;
903 char *output_filename
;
910 /* To allow us to do "strip *" without dying on the first
911 non-object file, failures are nonfatal. */
913 ibfd
= bfd_openr (input_filename
, input_target
);
916 nonfatal (input_filename
);
919 if (bfd_check_format (ibfd
, bfd_archive
))
923 /* bfd_get_target does not return the correct value until
924 bfd_check_format succeeds. */
925 if (output_target
== NULL
)
926 output_target
= bfd_get_target (ibfd
);
928 obfd
= bfd_openw (output_filename
, output_target
);
931 nonfatal (output_filename
);
933 copy_archive (ibfd
, obfd
, output_target
);
935 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
939 /* bfd_get_target does not return the correct value until
940 bfd_check_format succeeds. */
941 if (output_target
== NULL
)
942 output_target
= bfd_get_target (ibfd
);
944 obfd
= bfd_openw (output_filename
, output_target
);
947 nonfatal (output_filename
);
950 copy_object (ibfd
, obfd
);
952 if (!bfd_close (obfd
))
954 nonfatal (output_filename
);
957 if (!bfd_close (ibfd
))
959 nonfatal (input_filename
);
964 bfd_nonfatal (input_filename
);
965 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
967 list_matching_formats (matching
);
974 /* Create a section in OBFD with the same name and attributes
975 as ISECTION in IBFD. */
978 setup_section (ibfd
, isection
, obfdarg
)
983 bfd
*obfd
= (bfd
*) obfdarg
;
984 struct section_list
*p
;
990 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
991 && (strip_symbols
== strip_debug
992 || strip_symbols
== strip_all
993 || discard_locals
== locals_all
))
996 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1000 if (p
!= NULL
&& p
->remove
)
1003 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1004 if (osection
== NULL
)
1010 if (!bfd_set_section_size (obfd
,
1012 bfd_section_size (ibfd
, isection
)))
1018 vma
= bfd_section_vma (ibfd
, isection
);
1019 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1021 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1024 vma
+= adjust_section_vma
;
1025 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1031 if (bfd_set_section_alignment (obfd
,
1033 bfd_section_alignment (ibfd
, isection
))
1040 flags
= bfd_get_section_flags (ibfd
, isection
);
1041 if (p
!= NULL
&& p
->set_flags
)
1042 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1043 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1049 /* This used to be mangle_section; we do here to avoid using
1050 bfd_get_section_by_name since some formats allow multiple
1051 sections with the same name. */
1052 isection
->output_section
= osection
;
1053 isection
->output_offset
= 0;
1055 /* Allow the BFD backend to copy any private data it understands
1056 from the input section to the output section. */
1057 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1059 err
= "private data";
1067 fprintf (stderr
, "%s: %s: section `%s': error in %s: %s\n",
1069 bfd_get_filename (ibfd
), bfd_section_name (ibfd
, isection
),
1070 err
, bfd_errmsg (bfd_get_error ()));
1074 /* Copy the data of input section ISECTION of IBFD
1075 to an output section with the same name in OBFD.
1076 If stripping then don't copy any relocation info. */
1079 copy_section (ibfd
, isection
, obfdarg
)
1084 bfd
*obfd
= (bfd
*) obfdarg
;
1085 struct section_list
*p
;
1091 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1092 && (strip_symbols
== strip_debug
1093 || strip_symbols
== strip_all
1094 || discard_locals
== locals_all
))
1099 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1101 if (p
!= NULL
&& p
->remove
)
1104 osection
= isection
->output_section
;
1105 size
= bfd_get_section_size_before_reloc (isection
);
1107 if (size
== 0 || osection
== 0)
1110 if (strip_symbols
== strip_all
)
1111 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1116 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1119 nonfatal (bfd_get_filename (ibfd
));
1122 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1125 relpp
= (arelent
**) xmalloc (relsize
);
1126 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1129 nonfatal (bfd_get_filename (ibfd
));
1131 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1135 isection
->_cooked_size
= isection
->_raw_size
;
1136 isection
->reloc_done
= true;
1138 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1140 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1142 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1145 nonfatal (bfd_get_filename (ibfd
));
1150 filter_bytes (memhunk
, &size
);
1151 /* The section has gotten smaller. */
1152 if (!bfd_set_section_size (obfd
, osection
, size
))
1153 nonfatal (bfd_get_filename (obfd
));
1156 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1159 nonfatal (bfd_get_filename (obfd
));
1165 /* Get all the sections. This is used when --gap-fill or --pad-to is
1169 get_sections (obfd
, osection
, secppparg
)
1174 asection
***secppp
= (asection
***) secppparg
;
1176 **secppp
= osection
;
1180 /* Sort sections by VMA. This is called via qsort, and is used when
1181 --gap-fill or --pad-to is used. We force non loadable or empty
1182 sections to the front, where they are easier to ignore. */
1185 compare_section_vma (arg1
, arg2
)
1189 const asection
**sec1
= (const asection
**) arg1
;
1190 const asection
**sec2
= (const asection
**) arg2
;
1191 flagword flags1
, flags2
;
1193 /* Sort non loadable sections to the front. */
1194 flags1
= (*sec1
)->flags
;
1195 flags2
= (*sec2
)->flags
;
1196 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1197 || (flags1
& SEC_LOAD
) == 0)
1199 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1200 && (flags2
& SEC_LOAD
) != 0)
1205 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1206 || (flags2
& SEC_LOAD
) == 0)
1210 /* Sort sections by VMA. */
1211 if ((*sec1
)->vma
> (*sec2
)->vma
)
1213 else if ((*sec1
)->vma
< (*sec2
)->vma
)
1216 /* Sort sections with the same VMA by size. */
1217 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1219 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1225 /* Mark all the symbols which will be used in output relocations with
1226 the BSF_KEEP flag so that those symbols will not be stripped.
1228 Ignore relocations which will not appear in the output file. */
1231 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1236 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1241 /* Ignore an input section with no corresponding output section. */
1242 if (isection
->output_section
== NULL
)
1245 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1247 bfd_fatal (bfd_get_filename (ibfd
));
1249 relpp
= (arelent
**) xmalloc (relsize
);
1250 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1252 bfd_fatal (bfd_get_filename (ibfd
));
1254 /* Examine each symbol used in a relocation. If it's not one of the
1255 special bfd section symbols, then mark it with BSF_KEEP. */
1256 for (i
= 0; i
< relcount
; i
++)
1258 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1259 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1260 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1261 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1268 /* The number of bytes to copy at once. */
1269 #define COPY_BUF 8192
1271 /* Copy file FROM to file TO, performing no translations.
1272 Return 0 if ok, -1 if error. */
1275 simple_copy (from
, to
)
1278 int fromfd
, tofd
, nread
;
1281 fromfd
= open (from
, O_RDONLY
);
1284 tofd
= open (to
, O_WRONLY
| O_CREAT
| O_TRUNC
);
1290 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1292 if (write (tofd
, buf
, nread
) != nread
)
1308 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1310 #define S_ISLNK(m) 0
1315 /* Rename FROM to TO, copying if TO is a link.
1316 Assumes that TO already exists, because FROM is a temp file.
1317 Return 0 if ok, -1 if error. */
1320 smart_rename (from
, to
)
1329 /* Use rename only if TO is not a symbolic link and has
1330 only one hard link. */
1331 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1333 ret
= rename (from
, to
);
1336 /* Try to preserve the permission bits and ownership of TO. */
1337 chmod (to
, s
.st_mode
& 07777);
1338 chown (to
, s
.st_uid
, s
.st_gid
);
1343 ret
= simple_copy (from
, to
);
1351 strip_main (argc
, argv
)
1355 char *input_target
= NULL
, *output_target
= NULL
;
1356 boolean show_version
= false;
1358 struct section_list
*p
;
1360 while ((c
= getopt_long (argc
, argv
, "I:O:F:R:sSgxXVvN:",
1361 strip_options
, (int *) 0)) != EOF
)
1366 input_target
= optarg
;
1369 output_target
= optarg
;
1372 input_target
= output_target
= optarg
;
1375 p
= find_section_list (optarg
, true);
1377 sections_removed
= true;
1380 strip_symbols
= strip_all
;
1384 strip_symbols
= strip_debug
;
1387 add_strip_symbol (optarg
);
1390 discard_locals
= locals_all
;
1393 discard_locals
= locals_start_L
;
1399 show_version
= true;
1402 break; /* we've been given a long option */
1404 strip_usage (stdout
, 0);
1406 strip_usage (stderr
, 1);
1412 printf ("GNU %s version %s\n", program_name
, program_version
);
1416 /* Default is to strip all symbols. */
1417 if (strip_symbols
== strip_undef
1418 && discard_locals
== locals_undef
1419 && strip_specific_list
== NULL
)
1420 strip_symbols
= strip_all
;
1422 if (output_target
== (char *) NULL
)
1423 output_target
= input_target
;
1427 strip_usage (stderr
, 1);
1429 for (; i
< argc
; i
++)
1431 int hold_status
= status
;
1433 char *tmpname
= make_tempname (argv
[i
]);
1435 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1438 smart_rename (tmpname
, argv
[i
]);
1439 status
= hold_status
;
1450 copy_main (argc
, argv
)
1454 char *input_filename
= NULL
, *output_filename
= NULL
;
1455 char *input_target
= NULL
, *output_target
= NULL
;
1456 boolean show_version
= false;
1457 boolean adjust_warn
= true;
1459 struct section_list
*p
;
1461 while ((c
= getopt_long (argc
, argv
, "b:i:I:s:O:d:F:R:SgxXVvN:",
1462 copy_options
, (int *) 0)) != EOF
)
1467 copy_byte
= atoi(optarg
);
1470 fprintf (stderr
, "%s: byte number must be non-negative\n",
1476 interleave
= atoi(optarg
);
1479 fprintf(stderr
, "%s: interleave must be positive\n",
1485 case 's': /* "source" - 'I' is preferred */
1486 input_target
= optarg
;
1489 case 'd': /* "destination" - 'O' is preferred */
1490 output_target
= optarg
;
1493 input_target
= output_target
= optarg
;
1496 p
= find_section_list (optarg
, true);
1498 sections_removed
= true;
1501 strip_symbols
= strip_all
;
1504 strip_symbols
= strip_debug
;
1507 add_strip_symbol (optarg
);
1510 discard_locals
= locals_all
;
1513 discard_locals
= locals_start_L
;
1519 show_version
= true;
1521 case OPTION_ADD_SECTION
:
1525 struct section_add
*pa
;
1530 s
= strchr (optarg
, '=');
1534 "%s: bad format for --add-section NAME=FILENAME\n",
1539 if (stat (s
+ 1, &st
) < 0)
1541 fprintf (stderr
, "%s: ", program_name
);
1546 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1549 name
= (char *) xmalloc (len
+ 1);
1550 strncpy (name
, optarg
, len
);
1554 pa
->filename
= s
+ 1;
1556 pa
->size
= st
.st_size
;
1558 pa
->contents
= xmalloc (pa
->size
);
1559 f
= fopen (pa
->filename
, FOPEN_RB
);
1562 fprintf (stderr
, "%s: ", program_name
);
1563 perror (pa
->filename
);
1566 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1569 fprintf (stderr
, "%s: %s: fread failed\n",
1570 program_name
, pa
->filename
);
1575 pa
->next
= add_sections
;
1579 case OPTION_ADJUST_START
:
1580 adjust_start
= parse_vma (optarg
, "--adjust-start");
1582 case OPTION_ADJUST_SECTION_VMA
:
1588 s
= strchr (optarg
, '=');
1591 s
= strchr (optarg
, '+');
1594 s
= strchr (optarg
, '-');
1598 "%s: bad format for --adjust-section-vma\n",
1606 name
= (char *) xmalloc (len
+ 1);
1607 strncpy (name
, optarg
, len
);
1610 p
= find_section_list (name
, true);
1612 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1615 p
->adjust
= set_vma
;
1618 p
->adjust
= adjust_vma
;
1624 case OPTION_ADJUST_VMA
:
1625 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
1626 adjust_start
= adjust_section_vma
;
1628 case OPTION_ADJUST_WARNINGS
:
1631 case OPTION_GAP_FILL
:
1633 bfd_vma gap_fill_vma
;
1635 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
1636 gap_fill
= (bfd_byte
) gap_fill_vma
;
1637 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
1639 fprintf (stderr
, "%s: warning: truncating gap-fill from 0x",
1641 fprintf_vma (stderr
, gap_fill_vma
);
1642 fprintf (stderr
, "to 0x%x\n", (unsigned int) gap_fill
);
1644 gap_fill_set
= true;
1647 case OPTION_NO_ADJUST_WARNINGS
:
1648 adjust_warn
= false;
1651 pad_to
= parse_vma (optarg
, "--pad-to");
1654 case OPTION_SET_SECTION_FLAGS
:
1660 s
= strchr (optarg
, '=');
1663 fprintf (stderr
, "%s: bad format for --set-section-flags\n",
1669 name
= (char *) xmalloc (len
+ 1);
1670 strncpy (name
, optarg
, len
);
1673 p
= find_section_list (name
, true);
1675 p
->set_flags
= true;
1676 p
->flags
= parse_flags (s
+ 1);
1679 case OPTION_SET_START
:
1680 set_start
= parse_vma (optarg
, "--set-start");
1681 set_start_set
= true;
1684 break; /* we've been given a long option */
1686 copy_usage (stdout
, 0);
1688 copy_usage (stderr
, 1);
1694 printf ("GNU %s version %s\n", program_name
, program_version
);
1698 if (copy_byte
>= interleave
)
1700 fprintf (stderr
, "%s: byte number must be less than interleave\n",
1705 if (optind
== argc
|| optind
+ 2 < argc
)
1706 copy_usage (stderr
, 1);
1708 input_filename
= argv
[optind
];
1709 if (optind
+ 1 < argc
)
1710 output_filename
= argv
[optind
+ 1];
1712 /* Default is to strip no symbols. */
1713 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
1714 strip_symbols
= strip_none
;
1716 if (output_target
== (char *) NULL
)
1717 output_target
= input_target
;
1719 /* If there is no destination file then create a temp and rename
1720 the result into the input. */
1722 if (output_filename
== (char *) NULL
)
1724 char *tmpname
= make_tempname (input_filename
);
1725 copy_file (input_filename
, tmpname
, input_target
, output_target
);
1727 smart_rename (tmpname
, input_filename
);
1733 copy_file (input_filename
, output_filename
, input_target
, output_target
);
1738 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
1740 if (! p
->used
&& p
->adjust
!= ignore_vma
)
1742 fprintf (stderr
, "%s: warning: --adjust-section-vma %s%c0x",
1743 program_name
, p
->name
,
1744 p
->adjust
== set_vma
? '=' : '+');
1745 fprintf_vma (stderr
, p
->val
);
1746 fprintf (stderr
, " never used\n");
1759 program_name
= argv
[0];
1760 xmalloc_set_program_name (program_name
);
1762 START_PROGRESS (program_name
, 0);
1764 strip_symbols
= strip_undef
;
1765 discard_locals
= locals_undef
;
1771 int i
= strlen (program_name
);
1772 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
1776 strip_main (argc
, argv
);
1778 copy_main (argc
, argv
);
1780 END_PROGRESS (program_name
);