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 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
));
41 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
43 static asymbol
**isympp
= NULL
; /* Input symbols */
44 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
46 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
47 static int copy_byte
= -1;
48 static int interleave
= 4;
50 static boolean verbose
; /* Print file and target names. */
51 static int status
= 0; /* Exit status. */
56 strip_none
, /* don't strip */
57 strip_debug
, /* strip all debugger symbols */
58 strip_unneeded
, /* strip unnecessary 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 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
138 #define OPTION_ADD_SECTION 150
139 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
140 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
141 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
142 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
143 #define OPTION_GAP_FILL (OPTION_ADJUST_WARNINGS + 1)
144 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
145 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
146 #define OPTION_SET_SECTION_FLAGS (OPTION_PAD_TO + 1)
147 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
148 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
150 /* Options to handle if running as "strip". */
152 static struct option strip_options
[] =
154 {"discard-all", no_argument
, 0, 'x'},
155 {"discard-locals", no_argument
, 0, 'X'},
156 {"format", required_argument
, 0, 'F'}, /* Obsolete */
157 {"help", no_argument
, 0, 'h'},
158 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
159 {"input-target", required_argument
, 0, 'I'},
160 {"keep-symbol", required_argument
, 0, 'K'},
161 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
162 {"output-target", required_argument
, 0, 'O'},
163 {"remove-section", required_argument
, 0, 'R'},
164 {"strip-all", no_argument
, 0, 's'},
165 {"strip-debug", no_argument
, 0, 'S'},
166 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
167 {"strip-symbol", required_argument
, 0, 'N'},
168 {"target", required_argument
, 0, 'F'},
169 {"verbose", no_argument
, 0, 'v'},
170 {"version", no_argument
, 0, 'V'},
171 {0, no_argument
, 0, 0}
174 /* Options to handle if running as "objcopy". */
176 static struct option copy_options
[] =
178 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
179 {"adjust-start", required_argument
, 0, OPTION_ADJUST_START
},
180 {"adjust-vma", required_argument
, 0, OPTION_ADJUST_VMA
},
181 {"adjust-section-vma", required_argument
, 0, OPTION_ADJUST_SECTION_VMA
},
182 {"adjust-warnings", no_argument
, 0, OPTION_ADJUST_WARNINGS
},
183 {"byte", required_argument
, 0, 'b'},
184 {"discard-all", no_argument
, 0, 'x'},
185 {"discard-locals", no_argument
, 0, 'X'},
186 {"format", required_argument
, 0, 'F'}, /* Obsolete */
187 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
188 {"help", no_argument
, 0, 'h'},
189 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
190 {"input-target", required_argument
, 0, 'I'},
191 {"interleave", required_argument
, 0, 'i'},
192 {"keep-symbol", required_argument
, 0, 'K'},
193 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_ADJUST_WARNINGS
},
194 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
195 {"output-target", required_argument
, 0, 'O'},
196 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
197 {"remove-section", required_argument
, 0, 'R'},
198 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
199 {"set-start", required_argument
, 0, OPTION_SET_START
},
200 {"strip-all", no_argument
, 0, 'S'},
201 {"strip-debug", no_argument
, 0, 'g'},
202 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
203 {"strip-symbol", required_argument
, 0, 'N'},
204 {"target", required_argument
, 0, 'F'},
205 {"verbose", no_argument
, 0, 'v'},
206 {"version", no_argument
, 0, 'V'},
207 {0, no_argument
, 0, 0}
211 extern char *program_name
;
212 extern char *program_version
;
214 /* This flag distinguishes between strip and objcopy:
215 1 means this is 'strip'; 0 means this is 'objcopy'.
216 -1 means if we should use argv[0] to decide. */
221 copy_usage (stream
, exit_status
)
226 Usage: %s [-vVSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
227 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
228 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
229 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
230 [--discard-locals] [--remove-section=section] [--gap-fill=val]\n",
233 [--pad-to=address] [--set-start=val] [--adjust-start=incr]\n\
234 [--adjust-vma=incr] [--adjust-section-vma=section{=,+,-}val]\n\
235 [--adjust-warnings] [--no-adjust-warnings]\n\
236 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
237 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
238 [--verbose] [--version] [--help]\n\
239 in-file [out-file]\n");
240 list_supported_targets (program_name
, stream
);
245 strip_usage (stream
, exit_status
)
250 Usage: %s [-vVsSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
251 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
252 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
253 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
254 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
255 [--verbose] [--version] [--help] file...\n",
257 list_supported_targets (program_name
, stream
);
261 /* Parse a string into a VMA, with a fatal error if it can't be
272 ret
= bfd_scan_vma (s
, &end
, 0);
275 fprintf (stderr
, "%s: %s: bad number: %s\n", program_name
, arg
, s
);
281 /* Parse section flags into a flagword, with a fatal error if the
282 string can't be parsed. */
296 snext
= strchr (s
, ',');
305 #define PARSE_FLAG(fname,fval) if (strncmp (fname, s, len) == 0) ret |= fval;
306 PARSE_FLAG ("alloc", SEC_ALLOC
);
307 PARSE_FLAG ("load", SEC_LOAD
);
308 PARSE_FLAG ("readonly", SEC_READONLY
);
309 PARSE_FLAG ("code", SEC_CODE
);
310 PARSE_FLAG ("data", SEC_DATA
);
311 PARSE_FLAG ("rom", SEC_ROM
);
321 /* Find and optionally add an entry in the adjust_sections list. */
323 static struct section_list
*
324 find_section_list (name
, add
)
328 register struct section_list
*p
;
330 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
331 if (strcmp (p
->name
, name
) == 0)
337 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
341 p
->adjust
= ignore_vma
;
343 p
->set_flags
= false;
346 p
->next
= adjust_sections
;
352 /* Make a list of symbols to explicitly strip out, or to keep. A
353 linked list is good enough for a small number from the command
354 line, but this will slow things down a lot if many symbols are
360 struct symlist
*next
;
363 /* List of symbols to strip. */
365 static struct symlist
*strip_specific_list
= NULL
;
367 /* If this is false, we strip the symbols in strip_specific_list.
368 Otherwise, we keep only the symbols in the list. */
370 static boolean keep_symbols
= false;
372 /* Add a symbol to strip_specific_list. */
375 add_strip_symbol (name
)
378 struct symlist
*tmp_list
;
380 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
381 tmp_list
->name
= name
;
382 tmp_list
->next
= strip_specific_list
;
383 strip_specific_list
= tmp_list
;
386 /* See whether a symbol should be stripped or kept based on
387 strip_specific_list and keep_symbols. */
390 is_strip_symbol (name
)
393 struct symlist
*tmp_list
;
395 for (tmp_list
= strip_specific_list
; tmp_list
; tmp_list
= tmp_list
->next
)
397 if (strcmp (name
, tmp_list
->name
) == 0)
398 return keep_symbols
? false : true;
403 /* See if a section is being removed. */
406 is_strip_section (abfd
, sec
)
410 struct section_list
*p
;
412 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
413 && (strip_symbols
== strip_debug
414 || strip_symbols
== strip_unneeded
415 || strip_symbols
== strip_all
416 || discard_locals
== locals_all
))
419 if (! sections_removed
)
421 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
422 return p
!= NULL
&& p
->remove
? true : false;
425 /* Choose which symbol entries to copy; put the result in OSYMS.
426 We don't copy in place, because that confuses the relocs.
427 Return the number of symbols to print. */
430 filter_symbols (abfd
, osyms
, isyms
, symcount
)
432 asymbol
**osyms
, **isyms
;
435 register asymbol
**from
= isyms
, **to
= osyms
;
436 long src_count
= 0, dst_count
= 0;
438 for (; src_count
< symcount
; src_count
++)
440 asymbol
*sym
= from
[src_count
];
441 flagword flags
= sym
->flags
;
444 if ((flags
& BSF_KEEP
) != 0) /* Used in relocation. */
446 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
447 || bfd_is_und_section (bfd_get_section (sym
))
448 || bfd_is_com_section (bfd_get_section (sym
)))
449 keep
= strip_symbols
!= strip_unneeded
;
450 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
451 keep
= (strip_symbols
!= strip_debug
452 && strip_symbols
!= strip_unneeded
);
453 else /* Local symbol. */
454 keep
= (strip_symbols
!= strip_unneeded
455 && (discard_locals
!= locals_all
456 && (discard_locals
!= locals_start_L
457 || ! bfd_is_local_label (abfd
, sym
))));
459 if (keep
&& is_strip_symbol (bfd_asymbol_name (sym
)))
461 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
465 to
[dst_count
++] = sym
;
471 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
475 filter_bytes (memhunk
, size
)
479 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
481 for (; from
< end
; from
+= interleave
)
486 /* Copy object file IBFD onto OBFD. */
489 copy_object (ibfd
, obfd
)
495 asection
**osections
= NULL
;
496 bfd_size_type
*gaps
= NULL
;
497 bfd_size_type max_gap
= 0;
499 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
501 nonfatal (bfd_get_filename (obfd
));
505 printf ("copy from %s(%s) to %s(%s)\n",
506 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
507 bfd_get_filename(obfd
), bfd_get_target(obfd
));
512 start
= bfd_get_start_address (ibfd
);
513 start
+= adjust_start
;
515 if (!bfd_set_start_address (obfd
, start
)
516 || !bfd_set_file_flags (obfd
,
517 (bfd_get_file_flags (ibfd
)
518 & bfd_applicable_file_flags (obfd
))))
520 nonfatal (bfd_get_filename (ibfd
));
523 /* Copy architecture of input file to output file */
524 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
525 bfd_get_mach (ibfd
)))
527 fprintf (stderr
, "Output file cannot represent architecture %s\n",
528 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
529 bfd_get_mach (ibfd
)));
531 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
533 nonfatal (bfd_get_filename(ibfd
));
538 if (osympp
!= isympp
)
541 /* bfd mandates that all output sections be created and sizes set before
542 any output is done. Thus, we traverse all sections multiple times. */
543 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
545 if (add_sections
!= NULL
)
547 struct section_add
*padd
;
548 struct section_list
*pset
;
550 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
552 padd
->section
= bfd_make_section (obfd
, padd
->name
);
553 if (padd
->section
== NULL
)
555 fprintf (stderr
, "%s: can't create section `%s': %s\n",
556 program_name
, padd
->name
,
557 bfd_errmsg (bfd_get_error ()));
565 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
566 nonfatal (bfd_get_filename (obfd
));
568 pset
= find_section_list (padd
->name
, false);
572 if (pset
!= NULL
&& pset
->set_flags
)
573 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
575 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
576 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
577 nonfatal (bfd_get_filename (obfd
));
580 && (pset
->adjust
== adjust_vma
581 || pset
->adjust
== set_vma
))
583 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->val
))
584 nonfatal (bfd_get_filename (obfd
));
590 if (gap_fill_set
|| pad_to_set
)
595 /* We must fill in gaps between the sections and/or we must pad
596 the last section to a specified address. We do this by
597 grabbing a list of the sections, sorting them by VMA, and
598 increasing the section sizes as required to fill the gaps.
599 We write out the gap contents below. */
601 c
= bfd_count_sections (obfd
);
602 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
604 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
606 qsort (osections
, c
, sizeof (asection
*), compare_section_vma
);
608 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
609 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
613 for (i
= 0; i
< c
- 1; i
++)
617 bfd_vma gap_start
, gap_stop
;
619 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
620 if ((flags
& SEC_HAS_CONTENTS
) == 0
621 || (flags
& SEC_LOAD
) == 0)
624 size
= bfd_section_size (obfd
, osections
[i
]);
625 gap_start
= bfd_section_vma (obfd
, osections
[i
]) + size
;
626 gap_stop
= bfd_section_vma (obfd
, osections
[i
+ 1]);
627 if (gap_start
< gap_stop
)
629 if (! bfd_set_section_size (obfd
, osections
[i
],
630 size
+ (gap_stop
- gap_start
)))
632 fprintf (stderr
, "%s: Can't fill gap after %s: %s\n",
634 bfd_get_section_name (obfd
, osections
[i
]),
635 bfd_errmsg (bfd_get_error()));
639 gaps
[i
] = gap_stop
- gap_start
;
640 if (max_gap
< gap_stop
- gap_start
)
641 max_gap
= gap_stop
- gap_start
;
651 vma
= bfd_section_vma (obfd
, osections
[c
- 1]);
652 size
= bfd_section_size (obfd
, osections
[c
- 1]);
653 if (vma
+ size
< pad_to
)
655 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
658 fprintf (stderr
, "%s: Can't add padding to %s: %s\n",
660 bfd_get_section_name (obfd
, osections
[c
- 1]),
661 bfd_errmsg (bfd_get_error ()));
666 gaps
[c
- 1] = pad_to
- (vma
+ size
);
667 if (max_gap
< pad_to
- (vma
+ size
))
668 max_gap
= pad_to
- (vma
+ size
);
674 /* Symbol filtering must happen after the output sections have
675 been created, but before their contents are set. */
676 if (strip_symbols
== strip_all
)
678 osympp
= isympp
= NULL
;
685 symsize
= bfd_get_symtab_upper_bound (ibfd
);
688 nonfatal (bfd_get_filename (ibfd
));
691 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
692 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
695 nonfatal (bfd_get_filename (ibfd
));
698 if (strip_symbols
== strip_debug
699 || strip_symbols
== strip_unneeded
700 || discard_locals
!= locals_undef
701 || strip_specific_list
!= NULL
704 /* Mark symbols used in output relocations so that they
705 are kept, even if they are local labels or static symbols.
707 Note we iterate over the input sections examining their
708 relocations since the relocations for the output sections
709 haven't been set yet. mark_symbols_used_in_relocations will
710 ignore input sections which have no corresponding output
712 bfd_map_over_sections (ibfd
,
713 mark_symbols_used_in_relocations
,
715 osympp
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
716 symcount
= filter_symbols (ibfd
, osympp
, isympp
, symcount
);
720 bfd_set_symtab (obfd
, osympp
, symcount
);
722 /* This has to happen after the symbol table has been set. */
723 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
725 if (add_sections
!= NULL
)
727 struct section_add
*padd
;
729 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
731 if (! bfd_set_section_contents (obfd
, padd
->section
,
732 (PTR
) padd
->contents
,
734 (bfd_size_type
) padd
->size
))
735 nonfatal (bfd_get_filename (obfd
));
739 if (gap_fill_set
|| pad_to_set
)
744 /* Fill in the gaps. */
748 buf
= (bfd_byte
*) xmalloc (max_gap
);
749 memset (buf
, gap_fill
, max_gap
);
751 c
= bfd_count_sections (obfd
);
752 for (i
= 0; i
< c
; i
++)
760 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
769 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
772 nonfatal (bfd_get_filename (obfd
));
781 /* Allow the BFD backend to copy any private data it understands
782 from the input BFD to the output BFD. This is done last to
783 permit the routine to look at the filtered symbol table, which is
784 important for the ECOFF code at least. */
785 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
787 fprintf (stderr
, "%s: %s: error copying private BFD data: %s\n",
788 program_name
, bfd_get_filename (obfd
),
789 bfd_errmsg (bfd_get_error ()));
801 size_t size
= strlen (a
) + strlen (b
) + strlen (c
);
802 char *r
= xmalloc (size
+ 1);
810 /* Read each archive element in turn from IBFD, copy the
811 contents to temp file, and keep the temp file handle. */
814 copy_archive (ibfd
, obfd
, output_target
)
821 struct name_list
*next
;
824 bfd
**ptr
= &obfd
->archive_head
;
826 char *dir
= make_tempname (bfd_get_filename (obfd
));
828 /* Make a temp directory to hold the contents. */
829 if (mkdir (dir
, 0700) != 0)
831 fatal ("cannot mkdir %s for archive copying (error: %s)",
832 dir
, strerror (errno
));
834 obfd
->has_armap
= ibfd
->has_armap
;
838 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
839 while (this_element
!= (bfd
*) NULL
)
841 /* Create an output file for this member. */
842 char *output_name
= cat (dir
, "/", bfd_get_filename(this_element
));
843 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
846 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
847 l
->name
= output_name
;
851 if (output_bfd
== (bfd
*) NULL
)
853 nonfatal (output_name
);
855 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
857 nonfatal (bfd_get_filename (obfd
));
860 if (bfd_check_format (this_element
, bfd_object
) == true)
862 copy_object (this_element
, output_bfd
);
865 bfd_close (output_bfd
);
867 /* Open the newly output file and attach to our list. */
868 output_bfd
= bfd_openr (output_name
, output_target
);
871 ptr
= &output_bfd
->next
;
873 last_element
= this_element
;
875 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
877 bfd_close (last_element
);
881 if (!bfd_close (obfd
))
883 nonfatal (bfd_get_filename (obfd
));
886 /* Delete all the files that we opened. */
887 for (l
= list
; l
!= NULL
; l
= l
->next
)
891 if (!bfd_close (ibfd
))
893 nonfatal (bfd_get_filename (ibfd
));
897 /* The top-level control. */
900 copy_file (input_filename
, output_filename
, input_target
, output_target
)
901 char *input_filename
;
902 char *output_filename
;
909 /* To allow us to do "strip *" without dying on the first
910 non-object file, failures are nonfatal. */
912 ibfd
= bfd_openr (input_filename
, input_target
);
915 nonfatal (input_filename
);
918 if (bfd_check_format (ibfd
, bfd_archive
))
922 /* bfd_get_target does not return the correct value until
923 bfd_check_format succeeds. */
924 if (output_target
== NULL
)
925 output_target
= bfd_get_target (ibfd
);
927 obfd
= bfd_openw (output_filename
, output_target
);
930 nonfatal (output_filename
);
932 copy_archive (ibfd
, obfd
, output_target
);
934 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
938 /* bfd_get_target does not return the correct value until
939 bfd_check_format succeeds. */
940 if (output_target
== NULL
)
941 output_target
= bfd_get_target (ibfd
);
943 obfd
= bfd_openw (output_filename
, output_target
);
946 nonfatal (output_filename
);
949 copy_object (ibfd
, obfd
);
951 if (!bfd_close (obfd
))
953 nonfatal (output_filename
);
956 if (!bfd_close (ibfd
))
958 nonfatal (input_filename
);
963 bfd_nonfatal (input_filename
);
964 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
966 list_matching_formats (matching
);
973 /* Create a section in OBFD with the same name and attributes
974 as ISECTION in IBFD. */
977 setup_section (ibfd
, isection
, obfdarg
)
982 bfd
*obfd
= (bfd
*) obfdarg
;
983 struct section_list
*p
;
989 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
990 && (strip_symbols
== strip_debug
991 || strip_symbols
== strip_unneeded
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_unneeded
1094 || strip_symbols
== strip_all
1095 || discard_locals
== locals_all
))
1100 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1102 if (p
!= NULL
&& p
->remove
)
1105 osection
= isection
->output_section
;
1106 size
= bfd_get_section_size_before_reloc (isection
);
1108 if (size
== 0 || osection
== 0)
1111 if (strip_symbols
== strip_all
)
1112 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1117 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1120 nonfatal (bfd_get_filename (ibfd
));
1123 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1126 relpp
= (arelent
**) xmalloc (relsize
);
1127 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1130 nonfatal (bfd_get_filename (ibfd
));
1132 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1136 isection
->_cooked_size
= isection
->_raw_size
;
1137 isection
->reloc_done
= true;
1139 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1141 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1143 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1146 nonfatal (bfd_get_filename (ibfd
));
1151 filter_bytes (memhunk
, &size
);
1152 /* The section has gotten smaller. */
1153 if (!bfd_set_section_size (obfd
, osection
, size
))
1154 nonfatal (bfd_get_filename (obfd
));
1157 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1160 nonfatal (bfd_get_filename (obfd
));
1166 /* Get all the sections. This is used when --gap-fill or --pad-to is
1170 get_sections (obfd
, osection
, secppparg
)
1175 asection
***secppp
= (asection
***) secppparg
;
1177 **secppp
= osection
;
1181 /* Sort sections by VMA. This is called via qsort, and is used when
1182 --gap-fill or --pad-to is used. We force non loadable or empty
1183 sections to the front, where they are easier to ignore. */
1186 compare_section_vma (arg1
, arg2
)
1190 const asection
**sec1
= (const asection
**) arg1
;
1191 const asection
**sec2
= (const asection
**) arg2
;
1192 flagword flags1
, flags2
;
1194 /* Sort non loadable sections to the front. */
1195 flags1
= (*sec1
)->flags
;
1196 flags2
= (*sec2
)->flags
;
1197 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1198 || (flags1
& SEC_LOAD
) == 0)
1200 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1201 && (flags2
& SEC_LOAD
) != 0)
1206 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1207 || (flags2
& SEC_LOAD
) == 0)
1211 /* Sort sections by VMA. */
1212 if ((*sec1
)->vma
> (*sec2
)->vma
)
1214 else if ((*sec1
)->vma
< (*sec2
)->vma
)
1217 /* Sort sections with the same VMA by size. */
1218 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1220 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1226 /* Mark all the symbols which will be used in output relocations with
1227 the BSF_KEEP flag so that those symbols will not be stripped.
1229 Ignore relocations which will not appear in the output file. */
1232 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1237 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1242 /* Ignore an input section with no corresponding output section. */
1243 if (isection
->output_section
== NULL
)
1246 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1248 bfd_fatal (bfd_get_filename (ibfd
));
1250 relpp
= (arelent
**) xmalloc (relsize
);
1251 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1253 bfd_fatal (bfd_get_filename (ibfd
));
1255 /* Examine each symbol used in a relocation. If it's not one of the
1256 special bfd section symbols, then mark it with BSF_KEEP. */
1257 for (i
= 0; i
< relcount
; i
++)
1259 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1260 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1261 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1262 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1269 /* The number of bytes to copy at once. */
1270 #define COPY_BUF 8192
1272 /* Copy file FROM to file TO, performing no translations.
1273 Return 0 if ok, -1 if error. */
1276 simple_copy (from
, to
)
1279 int fromfd
, tofd
, nread
;
1282 fromfd
= open (from
, O_RDONLY
);
1285 tofd
= open (to
, O_WRONLY
| O_CREAT
| O_TRUNC
);
1291 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1293 if (write (tofd
, buf
, nread
) != nread
)
1309 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1311 #define S_ISLNK(m) 0
1316 /* Rename FROM to TO, copying if TO is a link.
1317 Assumes that TO already exists, because FROM is a temp file.
1318 Return 0 if ok, -1 if error. */
1321 smart_rename (from
, to
)
1330 /* Use rename only if TO is not a symbolic link and has
1331 only one hard link. */
1332 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1334 ret
= rename (from
, to
);
1337 /* Try to preserve the permission bits and ownership of TO. */
1338 chmod (to
, s
.st_mode
& 07777);
1339 chown (to
, s
.st_uid
, s
.st_gid
);
1343 /* We have to clean up here. */
1345 fprintf (stderr
, "%s: `%s': ", program_name
, to
);
1353 ret
= simple_copy (from
, to
);
1361 strip_main (argc
, argv
)
1365 char *input_target
= NULL
, *output_target
= NULL
;
1366 boolean show_version
= false;
1368 struct section_list
*p
;
1370 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:sSgxXVv",
1371 strip_options
, (int *) 0)) != EOF
)
1376 input_target
= optarg
;
1379 output_target
= optarg
;
1382 input_target
= output_target
= optarg
;
1385 p
= find_section_list (optarg
, true);
1387 sections_removed
= true;
1390 strip_symbols
= strip_all
;
1394 strip_symbols
= strip_debug
;
1396 case OPTION_STRIP_UNNEEDED
:
1397 strip_symbols
= strip_unneeded
;
1400 if (! keep_symbols
&& strip_specific_list
!= NULL
)
1402 fprintf (stderr
, "%s: Can not specify both -K and -N\n",
1404 strip_usage (stderr
, 1);
1406 keep_symbols
= true;
1407 add_strip_symbol (optarg
);
1412 fprintf (stderr
, "%s: Can not specify both -K and -N\n",
1414 strip_usage (stderr
, 1);
1416 add_strip_symbol (optarg
);
1419 discard_locals
= locals_all
;
1422 discard_locals
= locals_start_L
;
1428 show_version
= true;
1431 break; /* we've been given a long option */
1433 strip_usage (stdout
, 0);
1435 strip_usage (stderr
, 1);
1441 printf ("GNU %s version %s\n", program_name
, program_version
);
1445 /* Default is to strip all symbols. */
1446 if (strip_symbols
== strip_undef
1447 && discard_locals
== locals_undef
1448 && strip_specific_list
== NULL
)
1449 strip_symbols
= strip_all
;
1451 if (output_target
== (char *) NULL
)
1452 output_target
= input_target
;
1456 strip_usage (stderr
, 1);
1458 for (; i
< argc
; i
++)
1460 int hold_status
= status
;
1462 char *tmpname
= make_tempname (argv
[i
]);
1464 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1467 smart_rename (tmpname
, argv
[i
]);
1468 status
= hold_status
;
1479 copy_main (argc
, argv
)
1483 char *input_filename
= NULL
, *output_filename
= NULL
;
1484 char *input_target
= NULL
, *output_target
= NULL
;
1485 boolean show_version
= false;
1486 boolean adjust_warn
= true;
1488 struct section_list
*p
;
1490 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:R:SgxXVv",
1491 copy_options
, (int *) 0)) != EOF
)
1496 copy_byte
= atoi(optarg
);
1499 fprintf (stderr
, "%s: byte number must be non-negative\n",
1505 interleave
= atoi(optarg
);
1508 fprintf(stderr
, "%s: interleave must be positive\n",
1514 case 's': /* "source" - 'I' is preferred */
1515 input_target
= optarg
;
1518 case 'd': /* "destination" - 'O' is preferred */
1519 output_target
= optarg
;
1522 input_target
= output_target
= optarg
;
1525 p
= find_section_list (optarg
, true);
1527 sections_removed
= true;
1530 strip_symbols
= strip_all
;
1533 strip_symbols
= strip_debug
;
1535 case OPTION_STRIP_UNNEEDED
:
1536 strip_symbols
= strip_unneeded
;
1539 if (! keep_symbols
&& strip_specific_list
!= NULL
)
1541 fprintf (stderr
, "%s: Can not specify both -K and -N\n",
1543 strip_usage (stderr
, 1);
1545 keep_symbols
= true;
1546 add_strip_symbol (optarg
);
1551 fprintf (stderr
, "%s: Can not specify both -K and -N\n",
1553 strip_usage (stderr
, 1);
1555 add_strip_symbol (optarg
);
1558 discard_locals
= locals_all
;
1561 discard_locals
= locals_start_L
;
1567 show_version
= true;
1569 case OPTION_ADD_SECTION
:
1573 struct section_add
*pa
;
1578 s
= strchr (optarg
, '=');
1582 "%s: bad format for --add-section NAME=FILENAME\n",
1587 if (stat (s
+ 1, &st
) < 0)
1589 fprintf (stderr
, "%s: ", program_name
);
1594 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1597 name
= (char *) xmalloc (len
+ 1);
1598 strncpy (name
, optarg
, len
);
1602 pa
->filename
= s
+ 1;
1604 pa
->size
= st
.st_size
;
1606 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1607 f
= fopen (pa
->filename
, FOPEN_RB
);
1610 fprintf (stderr
, "%s: ", program_name
);
1611 perror (pa
->filename
);
1614 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1617 fprintf (stderr
, "%s: %s: fread failed\n",
1618 program_name
, pa
->filename
);
1623 pa
->next
= add_sections
;
1627 case OPTION_ADJUST_START
:
1628 adjust_start
= parse_vma (optarg
, "--adjust-start");
1630 case OPTION_ADJUST_SECTION_VMA
:
1636 s
= strchr (optarg
, '=');
1639 s
= strchr (optarg
, '+');
1642 s
= strchr (optarg
, '-');
1646 "%s: bad format for --adjust-section-vma\n",
1654 name
= (char *) xmalloc (len
+ 1);
1655 strncpy (name
, optarg
, len
);
1658 p
= find_section_list (name
, true);
1660 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1663 p
->adjust
= set_vma
;
1666 p
->adjust
= adjust_vma
;
1672 case OPTION_ADJUST_VMA
:
1673 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
1674 adjust_start
= adjust_section_vma
;
1676 case OPTION_ADJUST_WARNINGS
:
1679 case OPTION_GAP_FILL
:
1681 bfd_vma gap_fill_vma
;
1683 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
1684 gap_fill
= (bfd_byte
) gap_fill_vma
;
1685 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
1687 fprintf (stderr
, "%s: warning: truncating gap-fill from 0x",
1689 fprintf_vma (stderr
, gap_fill_vma
);
1690 fprintf (stderr
, "to 0x%x\n", (unsigned int) gap_fill
);
1692 gap_fill_set
= true;
1695 case OPTION_NO_ADJUST_WARNINGS
:
1696 adjust_warn
= false;
1699 pad_to
= parse_vma (optarg
, "--pad-to");
1702 case OPTION_SET_SECTION_FLAGS
:
1708 s
= strchr (optarg
, '=');
1711 fprintf (stderr
, "%s: bad format for --set-section-flags\n",
1717 name
= (char *) xmalloc (len
+ 1);
1718 strncpy (name
, optarg
, len
);
1721 p
= find_section_list (name
, true);
1723 p
->set_flags
= true;
1724 p
->flags
= parse_flags (s
+ 1);
1727 case OPTION_SET_START
:
1728 set_start
= parse_vma (optarg
, "--set-start");
1729 set_start_set
= true;
1732 break; /* we've been given a long option */
1734 copy_usage (stdout
, 0);
1736 copy_usage (stderr
, 1);
1742 printf ("GNU %s version %s\n", program_name
, program_version
);
1746 if (copy_byte
>= interleave
)
1748 fprintf (stderr
, "%s: byte number must be less than interleave\n",
1753 if (optind
== argc
|| optind
+ 2 < argc
)
1754 copy_usage (stderr
, 1);
1756 input_filename
= argv
[optind
];
1757 if (optind
+ 1 < argc
)
1758 output_filename
= argv
[optind
+ 1];
1760 /* Default is to strip no symbols. */
1761 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
1762 strip_symbols
= strip_none
;
1764 if (output_target
== (char *) NULL
)
1765 output_target
= input_target
;
1767 /* If there is no destination file then create a temp and rename
1768 the result into the input. */
1770 if (output_filename
== (char *) NULL
)
1772 char *tmpname
= make_tempname (input_filename
);
1773 copy_file (input_filename
, tmpname
, input_target
, output_target
);
1775 smart_rename (tmpname
, input_filename
);
1781 copy_file (input_filename
, output_filename
, input_target
, output_target
);
1786 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
1788 if (! p
->used
&& p
->adjust
!= ignore_vma
)
1790 fprintf (stderr
, "%s: warning: --adjust-section-vma %s%c0x",
1791 program_name
, p
->name
,
1792 p
->adjust
== set_vma
? '=' : '+');
1793 fprintf_vma (stderr
, p
->val
);
1794 fprintf (stderr
, " never used\n");
1807 program_name
= argv
[0];
1808 xmalloc_set_program_name (program_name
);
1810 START_PROGRESS (program_name
, 0);
1812 strip_symbols
= strip_undef
;
1813 discard_locals
= locals_undef
;
1819 int i
= strlen (program_name
);
1820 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
1824 strip_main (argc
, argv
);
1826 copy_main (argc
, argv
);
1828 END_PROGRESS (program_name
);