1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "libiberty.h"
28 #ifdef HAVE_GOOD_UTIME_H
30 #else /* ! HAVE_GOOD_UTIME_H */
33 #endif /* HAVE_UTIMES */
34 #endif /* ! HAVE_GOOD_UTIME_H */
36 static void copy_usage
PARAMS ((FILE *, int));
37 static void strip_usage
PARAMS ((FILE *, int));
38 static flagword parse_flags
PARAMS ((const char *));
39 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
40 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
41 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
42 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
43 static int compare_section_vma
PARAMS ((const PTR
, const PTR
));
44 static void add_strip_symbol
PARAMS ((const char *));
45 static boolean is_strip_symbol
PARAMS ((const char *));
46 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
47 static unsigned int filter_symbols
48 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
49 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
50 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
51 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
52 static void copy_object
PARAMS ((bfd
*, bfd
*));
53 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
55 PARAMS ((const char *, const char *, const char *, const char *));
56 static int simple_copy
PARAMS ((const char *, const char *));
57 static int smart_rename
PARAMS ((const char *, const char *));
58 static void set_times
PARAMS ((const char *, const struct stat
*));
59 static int strip_main
PARAMS ((int, char **));
60 static int copy_main
PARAMS ((int, char **));
62 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
64 static asymbol
**isympp
= NULL
; /* Input symbols */
65 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
67 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
68 static int copy_byte
= -1;
69 static int interleave
= 4;
71 static boolean verbose
; /* Print file and target names. */
72 static int status
= 0; /* Exit status. */
77 strip_none
, /* don't strip */
78 strip_debug
, /* strip all debugger symbols */
79 strip_unneeded
, /* strip unnecessary symbols */
80 strip_all
/* strip all symbols */
83 /* Which symbols to remove. */
84 static enum strip_action strip_symbols
;
89 locals_start_L
, /* discard locals starting with L */
90 locals_all
/* discard all locals */
93 /* Which local symbols to remove. Overrides strip_all. */
94 static enum locals_action discard_locals
;
96 /* Structure used to hold lists of sections and actions to take. */
100 /* Next section to adjust. */
101 struct section_list
*next
;
104 /* Whether this entry was used. */
106 /* Whether to remove this section. */
108 /* Whether to adjust or set VMA. */
109 enum { ignore_vma
, adjust_vma
, set_vma
} adjust
;
110 /* Amount to adjust by or set to. */
112 /* Whether to set the section flags. */
114 /* What to set the section flags to. */
118 static struct section_list
*adjust_sections
;
119 static boolean sections_removed
;
121 /* Adjustments to the start address. */
122 static bfd_vma adjust_start
= 0;
123 static boolean set_start_set
= false;
124 static bfd_vma set_start
;
126 /* Adjustments to section VMA's. */
127 static bfd_vma adjust_section_vma
= 0;
129 /* Filling gaps between sections. */
130 static boolean gap_fill_set
= false;
131 static bfd_byte gap_fill
= 0;
133 /* Pad to a given address. */
134 static boolean pad_to_set
= false;
135 static bfd_vma pad_to
;
137 /* List of sections to add. */
141 /* Next section to add. */
142 struct section_add
*next
;
143 /* Name of section to add. */
145 /* Name of file holding section contents. */
146 const char *filename
;
149 /* Contents of file. */
151 /* BFD section, after it has been added. */
155 static struct section_add
*add_sections
;
157 /* Whether to convert debugging information. */
159 static boolean convert_debugging
= false;
161 /* Whether to change the leading character in symbol names. */
163 static boolean change_leading_char
= false;
165 /* Whether to remove the leading character from global symbol names. */
167 static boolean remove_leading_char
= false;
169 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
171 #define OPTION_ADD_SECTION 150
172 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
173 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
174 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
175 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
176 #define OPTION_CHANGE_LEADING_CHAR (OPTION_ADJUST_WARNINGS + 1)
177 #define OPTION_DEBUGGING (OPTION_CHANGE_LEADING_CHAR + 1)
178 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
179 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
180 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
181 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
182 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
183 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
184 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
185 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
187 /* Options to handle if running as "strip". */
189 static struct option strip_options
[] =
191 {"discard-all", no_argument
, 0, 'x'},
192 {"discard-locals", no_argument
, 0, 'X'},
193 {"format", required_argument
, 0, 'F'}, /* Obsolete */
194 {"help", no_argument
, 0, 'h'},
195 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
196 {"input-target", required_argument
, 0, 'I'},
197 {"keep-symbol", required_argument
, 0, 'K'},
198 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
199 {"output-target", required_argument
, 0, 'O'},
200 {"preserve-dates", no_argument
, 0, 'p'},
201 {"remove-section", required_argument
, 0, 'R'},
202 {"strip-all", no_argument
, 0, 's'},
203 {"strip-debug", no_argument
, 0, 'S'},
204 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
205 {"strip-symbol", required_argument
, 0, 'N'},
206 {"target", required_argument
, 0, 'F'},
207 {"verbose", no_argument
, 0, 'v'},
208 {"version", no_argument
, 0, 'V'},
209 {0, no_argument
, 0, 0}
212 /* Options to handle if running as "objcopy". */
214 static struct option copy_options
[] =
216 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
217 {"adjust-start", required_argument
, 0, OPTION_ADJUST_START
},
218 {"adjust-vma", required_argument
, 0, OPTION_ADJUST_VMA
},
219 {"adjust-section-vma", required_argument
, 0, OPTION_ADJUST_SECTION_VMA
},
220 {"adjust-warnings", no_argument
, 0, OPTION_ADJUST_WARNINGS
},
221 {"byte", required_argument
, 0, 'b'},
222 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
223 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
224 {"discard-all", no_argument
, 0, 'x'},
225 {"discard-locals", no_argument
, 0, 'X'},
226 {"format", required_argument
, 0, 'F'}, /* Obsolete */
227 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
228 {"help", no_argument
, 0, 'h'},
229 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
230 {"input-target", required_argument
, 0, 'I'},
231 {"interleave", required_argument
, 0, 'i'},
232 {"keep-symbol", required_argument
, 0, 'K'},
233 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_ADJUST_WARNINGS
},
234 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
235 {"output-target", required_argument
, 0, 'O'},
236 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
237 {"preserve-dates", no_argument
, 0, 'p'},
238 {"localize-symbol", required_argument
, 0, 'L'},
239 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
240 {"remove-section", required_argument
, 0, 'R'},
241 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
242 {"set-start", required_argument
, 0, OPTION_SET_START
},
243 {"strip-all", no_argument
, 0, 'S'},
244 {"strip-debug", no_argument
, 0, 'g'},
245 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
246 {"strip-symbol", required_argument
, 0, 'N'},
247 {"target", required_argument
, 0, 'F'},
248 {"verbose", no_argument
, 0, 'v'},
249 {"version", no_argument
, 0, 'V'},
250 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
251 {"weaken-symbol", required_argument
, 0, 'W'},
252 {0, no_argument
, 0, 0}
256 extern char *program_name
;
258 /* This flag distinguishes between strip and objcopy:
259 1 means this is 'strip'; 0 means this is 'objcopy'.
260 -1 means if we should use argv[0] to decide. */
265 copy_usage (stream
, exit_status
)
270 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
271 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
272 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
273 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
274 [--discard-locals] [--debugging] [--remove-section=section]\n",
277 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
278 [--set-start=val] [--adjust-start=incr]\n\
279 [--adjust-vma=incr] [--adjust-section-vma=section{=,+,-}val]\n\
280 [--adjust-warnings] [--no-adjust-warnings]\n\
281 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
282 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
283 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
284 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
285 [--verbose] [--version] [--help] in-file [out-file]\n");
286 list_supported_targets (program_name
, stream
);
287 if (exit_status
== 0)
288 fprintf (stream
, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
293 strip_usage (stream
, exit_status
)
298 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
299 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
300 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
301 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
302 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
303 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n",
305 list_supported_targets (program_name
, stream
);
306 if (exit_status
== 0)
307 fprintf (stream
, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
311 /* Parse section flags into a flagword, with a fatal error if the
312 string can't be parsed. */
326 snext
= strchr (s
, ',');
335 #define PARSE_FLAG(fname,fval) if (strncmp (fname, s, len) == 0) ret |= fval;
336 PARSE_FLAG ("alloc", SEC_ALLOC
);
337 PARSE_FLAG ("load", SEC_LOAD
);
338 PARSE_FLAG ("readonly", SEC_READONLY
);
339 PARSE_FLAG ("code", SEC_CODE
);
340 PARSE_FLAG ("data", SEC_DATA
);
341 PARSE_FLAG ("rom", SEC_ROM
);
351 /* Find and optionally add an entry in the adjust_sections list. */
353 static struct section_list
*
354 find_section_list (name
, add
)
358 register struct section_list
*p
;
360 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
361 if (strcmp (p
->name
, name
) == 0)
367 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
371 p
->adjust
= ignore_vma
;
373 p
->set_flags
= false;
376 p
->next
= adjust_sections
;
382 /* Make a list of symbols to explicitly strip out, or to keep. A
383 linked list is good enough for a small number from the command
384 line, but this will slow things down a lot if many symbols are
390 struct symlist
*next
;
393 /* List of symbols to strip, keep, localize, and weaken. */
395 static struct symlist
*strip_specific_list
= NULL
;
396 static struct symlist
*keep_specific_list
= NULL
;
397 static struct symlist
*localize_specific_list
= NULL
;
398 static struct symlist
*weaken_specific_list
= NULL
;
400 /* If this is true, we weaken global symbols (set BSF_WEAK). */
402 static boolean weaken
= false;
404 /* Add a symbol to strip_specific_list. */
407 add_specific_symbol (name
, list
)
409 struct symlist
**list
;
411 struct symlist
*tmp_list
;
413 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
414 tmp_list
->name
= name
;
415 tmp_list
->next
= *list
;
419 /* See whether a symbol should be stripped or kept based on
420 strip_specific_list and keep_symbols. */
423 is_specified_symbol (name
, list
)
425 struct symlist
*list
;
427 struct symlist
*tmp_list
;
429 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
431 if (strcmp (name
, tmp_list
->name
) == 0)
437 /* See if a section is being removed. */
440 is_strip_section (abfd
, sec
)
444 struct section_list
*p
;
446 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
447 && (strip_symbols
== strip_debug
448 || strip_symbols
== strip_unneeded
449 || strip_symbols
== strip_all
450 || discard_locals
== locals_all
451 || convert_debugging
))
454 if (! sections_removed
)
456 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
457 return p
!= NULL
&& p
->remove
? true : false;
460 /* Choose which symbol entries to copy; put the result in OSYMS.
461 We don't copy in place, because that confuses the relocs.
462 Return the number of symbols to print. */
465 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
468 asymbol
**osyms
, **isyms
;
471 register asymbol
**from
= isyms
, **to
= osyms
;
472 long src_count
= 0, dst_count
= 0;
474 for (; src_count
< symcount
; src_count
++)
476 asymbol
*sym
= from
[src_count
];
477 flagword flags
= sym
->flags
;
478 const char *name
= bfd_asymbol_name (sym
);
481 if (change_leading_char
482 && (bfd_get_symbol_leading_char (abfd
)
483 != bfd_get_symbol_leading_char (obfd
))
484 && (bfd_get_symbol_leading_char (abfd
) == '\0'
485 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
487 if (bfd_get_symbol_leading_char (obfd
) == '\0')
488 name
= bfd_asymbol_name (sym
) = name
+ 1;
493 n
= xmalloc (strlen (name
) + 2);
494 n
[0] = bfd_get_symbol_leading_char (obfd
);
495 if (bfd_get_symbol_leading_char (abfd
) == '\0')
496 strcpy (n
+ 1, name
);
498 strcpy (n
+ 1, name
+ 1);
499 name
= bfd_asymbol_name (sym
) = n
;
503 if (remove_leading_char
504 && ((flags
& BSF_GLOBAL
) != 0
505 || (flags
& BSF_WEAK
) != 0
506 || bfd_is_und_section (bfd_get_section (sym
))
507 || bfd_is_com_section (bfd_get_section (sym
)))
508 && name
[0] == bfd_get_symbol_leading_char (abfd
))
509 name
= bfd_asymbol_name (sym
) = name
+ 1;
511 if ((flags
& BSF_KEEP
) != 0) /* Used in relocation. */
513 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
514 || (flags
& BSF_WEAK
) != 0
515 || bfd_is_und_section (bfd_get_section (sym
))
516 || bfd_is_com_section (bfd_get_section (sym
)))
517 keep
= strip_symbols
!= strip_unneeded
;
518 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
519 keep
= (strip_symbols
!= strip_debug
520 && strip_symbols
!= strip_unneeded
521 && ! convert_debugging
);
522 else /* Local symbol. */
523 keep
= (strip_symbols
!= strip_unneeded
524 && (discard_locals
!= locals_all
525 && (discard_locals
!= locals_start_L
526 || ! bfd_is_local_label (abfd
, sym
))));
528 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
530 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
532 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
535 if (keep
&& (flags
& BSF_GLOBAL
) != 0
536 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
538 sym
->flags
&=~ BSF_GLOBAL
;
539 sym
->flags
|= BSF_WEAK
;
541 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
542 && is_specified_symbol (name
, localize_specific_list
))
544 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
545 sym
->flags
|= BSF_LOCAL
;
549 to
[dst_count
++] = sym
;
552 to
[dst_count
] = NULL
;
557 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
561 filter_bytes (memhunk
, size
)
565 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
567 for (; from
< end
; from
+= interleave
)
572 /* Copy object file IBFD onto OBFD. */
575 copy_object (ibfd
, obfd
)
581 asection
**osections
= NULL
;
582 bfd_size_type
*gaps
= NULL
;
583 bfd_size_type max_gap
= 0;
585 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
587 nonfatal (bfd_get_filename (obfd
));
591 printf ("copy from %s(%s) to %s(%s)\n",
592 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
593 bfd_get_filename(obfd
), bfd_get_target(obfd
));
598 start
= bfd_get_start_address (ibfd
);
599 start
+= adjust_start
;
601 if (!bfd_set_start_address (obfd
, start
)
602 || !bfd_set_file_flags (obfd
,
603 (bfd_get_file_flags (ibfd
)
604 & bfd_applicable_file_flags (obfd
))))
606 nonfatal (bfd_get_filename (ibfd
));
609 /* Copy architecture of input file to output file */
610 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
611 bfd_get_mach (ibfd
)))
614 "Warning: Output file cannot represent architecture %s\n",
615 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
616 bfd_get_mach (ibfd
)));
618 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
620 nonfatal (bfd_get_filename(ibfd
));
625 if (osympp
!= isympp
)
628 /* bfd mandates that all output sections be created and sizes set before
629 any output is done. Thus, we traverse all sections multiple times. */
630 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
632 if (add_sections
!= NULL
)
634 struct section_add
*padd
;
635 struct section_list
*pset
;
637 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
639 padd
->section
= bfd_make_section (obfd
, padd
->name
);
640 if (padd
->section
== NULL
)
642 fprintf (stderr
, "%s: can't create section `%s': %s\n",
643 program_name
, padd
->name
,
644 bfd_errmsg (bfd_get_error ()));
652 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
653 nonfatal (bfd_get_filename (obfd
));
655 pset
= find_section_list (padd
->name
, false);
659 if (pset
!= NULL
&& pset
->set_flags
)
660 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
662 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
663 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
664 nonfatal (bfd_get_filename (obfd
));
667 && (pset
->adjust
== adjust_vma
668 || pset
->adjust
== set_vma
))
670 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->val
))
671 nonfatal (bfd_get_filename (obfd
));
677 if (gap_fill_set
|| pad_to_set
)
682 /* We must fill in gaps between the sections and/or we must pad
683 the last section to a specified address. We do this by
684 grabbing a list of the sections, sorting them by VMA, and
685 increasing the section sizes as required to fill the gaps.
686 We write out the gap contents below. */
688 c
= bfd_count_sections (obfd
);
689 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
691 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
693 qsort (osections
, c
, sizeof (asection
*), compare_section_vma
);
695 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
696 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
700 for (i
= 0; i
< c
- 1; i
++)
704 bfd_vma gap_start
, gap_stop
;
706 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
707 if ((flags
& SEC_HAS_CONTENTS
) == 0
708 || (flags
& SEC_LOAD
) == 0)
711 size
= bfd_section_size (obfd
, osections
[i
]);
712 gap_start
= bfd_section_vma (obfd
, osections
[i
]) + size
;
713 gap_stop
= bfd_section_vma (obfd
, osections
[i
+ 1]);
714 if (gap_start
< gap_stop
)
716 if (! bfd_set_section_size (obfd
, osections
[i
],
717 size
+ (gap_stop
- gap_start
)))
719 fprintf (stderr
, "%s: Can't fill gap after %s: %s\n",
721 bfd_get_section_name (obfd
, osections
[i
]),
722 bfd_errmsg (bfd_get_error()));
726 gaps
[i
] = gap_stop
- gap_start
;
727 if (max_gap
< gap_stop
- gap_start
)
728 max_gap
= gap_stop
- gap_start
;
738 vma
= bfd_section_vma (obfd
, osections
[c
- 1]);
739 size
= bfd_section_size (obfd
, osections
[c
- 1]);
740 if (vma
+ size
< pad_to
)
742 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
745 fprintf (stderr
, "%s: Can't add padding to %s: %s\n",
747 bfd_get_section_name (obfd
, osections
[c
- 1]),
748 bfd_errmsg (bfd_get_error ()));
753 gaps
[c
- 1] = pad_to
- (vma
+ size
);
754 if (max_gap
< pad_to
- (vma
+ size
))
755 max_gap
= pad_to
- (vma
+ size
);
761 /* Symbol filtering must happen after the output sections have
762 been created, but before their contents are set. */
763 if (strip_symbols
== strip_all
)
765 osympp
= isympp
= NULL
;
773 symsize
= bfd_get_symtab_upper_bound (ibfd
);
776 nonfatal (bfd_get_filename (ibfd
));
779 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
780 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
783 nonfatal (bfd_get_filename (ibfd
));
786 if (convert_debugging
)
787 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
789 if (strip_symbols
== strip_debug
790 || strip_symbols
== strip_unneeded
791 || discard_locals
!= locals_undef
792 || strip_specific_list
!= NULL
793 || keep_specific_list
!= NULL
794 || localize_specific_list
!= NULL
795 || weaken_specific_list
!= NULL
798 || change_leading_char
799 || remove_leading_char
802 /* Mark symbols used in output relocations so that they
803 are kept, even if they are local labels or static symbols.
805 Note we iterate over the input sections examining their
806 relocations since the relocations for the output sections
807 haven't been set yet. mark_symbols_used_in_relocations will
808 ignore input sections which have no corresponding output
810 bfd_map_over_sections (ibfd
,
811 mark_symbols_used_in_relocations
,
813 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
814 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
817 if (convert_debugging
&& dhandle
!= NULL
)
819 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
827 bfd_set_symtab (obfd
, osympp
, symcount
);
829 /* This has to happen after the symbol table has been set. */
830 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
832 if (add_sections
!= NULL
)
834 struct section_add
*padd
;
836 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
838 if (! bfd_set_section_contents (obfd
, padd
->section
,
839 (PTR
) padd
->contents
,
841 (bfd_size_type
) padd
->size
))
842 nonfatal (bfd_get_filename (obfd
));
846 if (gap_fill_set
|| pad_to_set
)
851 /* Fill in the gaps. */
855 buf
= (bfd_byte
*) xmalloc (max_gap
);
856 memset (buf
, gap_fill
, (size_t) max_gap
);
858 c
= bfd_count_sections (obfd
);
859 for (i
= 0; i
< c
; i
++)
867 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
876 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
879 nonfatal (bfd_get_filename (obfd
));
888 /* Allow the BFD backend to copy any private data it understands
889 from the input BFD to the output BFD. This is done last to
890 permit the routine to look at the filtered symbol table, which is
891 important for the ECOFF code at least. */
892 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
894 fprintf (stderr
, "%s: %s: error copying private BFD data: %s\n",
895 program_name
, bfd_get_filename (obfd
),
896 bfd_errmsg (bfd_get_error ()));
902 /* Read each archive element in turn from IBFD, copy the
903 contents to temp file, and keep the temp file handle. */
906 copy_archive (ibfd
, obfd
, output_target
)
909 const char *output_target
;
913 struct name_list
*next
;
917 bfd
**ptr
= &obfd
->archive_head
;
919 char *dir
= make_tempname (bfd_get_filename (obfd
));
921 /* Make a temp directory to hold the contents. */
922 if (mkdir (dir
, 0700) != 0)
924 fatal ("cannot mkdir %s for archive copying (error: %s)",
925 dir
, strerror (errno
));
927 obfd
->has_armap
= ibfd
->has_armap
;
931 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
932 while (this_element
!= (bfd
*) NULL
)
934 /* Create an output file for this member. */
935 char *output_name
= concat (dir
, "/", bfd_get_filename(this_element
),
937 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
940 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
941 l
->name
= output_name
;
945 if (output_bfd
== (bfd
*) NULL
)
947 nonfatal (output_name
);
949 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
951 nonfatal (bfd_get_filename (obfd
));
954 if (bfd_check_format (this_element
, bfd_object
) == true)
956 copy_object (this_element
, output_bfd
);
959 bfd_close (output_bfd
);
961 /* Open the newly output file and attach to our list. */
962 output_bfd
= bfd_openr (output_name
, output_target
);
964 l
->obfd
= output_bfd
;
967 ptr
= &output_bfd
->next
;
969 last_element
= this_element
;
971 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
973 bfd_close (last_element
);
977 if (!bfd_close (obfd
))
979 nonfatal (bfd_get_filename (obfd
));
982 if (!bfd_close (ibfd
))
984 nonfatal (bfd_get_filename (ibfd
));
987 /* Delete all the files that we opened. */
988 for (l
= list
; l
!= NULL
; l
= l
->next
)
996 /* The top-level control. */
999 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1000 const char *input_filename
;
1001 const char *output_filename
;
1002 const char *input_target
;
1003 const char *output_target
;
1008 /* To allow us to do "strip *" without dying on the first
1009 non-object file, failures are nonfatal. */
1011 ibfd
= bfd_openr (input_filename
, input_target
);
1014 nonfatal (input_filename
);
1017 if (bfd_check_format (ibfd
, bfd_archive
))
1021 /* bfd_get_target does not return the correct value until
1022 bfd_check_format succeeds. */
1023 if (output_target
== NULL
)
1024 output_target
= bfd_get_target (ibfd
);
1026 obfd
= bfd_openw (output_filename
, output_target
);
1029 nonfatal (output_filename
);
1031 copy_archive (ibfd
, obfd
, output_target
);
1033 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1037 /* bfd_get_target does not return the correct value until
1038 bfd_check_format succeeds. */
1039 if (output_target
== NULL
)
1040 output_target
= bfd_get_target (ibfd
);
1042 obfd
= bfd_openw (output_filename
, output_target
);
1045 nonfatal (output_filename
);
1048 copy_object (ibfd
, obfd
);
1050 if (!bfd_close (obfd
))
1052 nonfatal (output_filename
);
1055 if (!bfd_close (ibfd
))
1057 nonfatal (input_filename
);
1062 bfd_nonfatal (input_filename
);
1063 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1065 list_matching_formats (matching
);
1072 /* Create a section in OBFD with the same name and attributes
1073 as ISECTION in IBFD. */
1076 setup_section (ibfd
, isection
, obfdarg
)
1081 bfd
*obfd
= (bfd
*) obfdarg
;
1082 struct section_list
*p
;
1089 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1090 && (strip_symbols
== strip_debug
1091 || strip_symbols
== strip_unneeded
1092 || strip_symbols
== strip_all
1093 || discard_locals
== locals_all
1094 || convert_debugging
))
1097 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1101 if (p
!= NULL
&& p
->remove
)
1104 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1105 if (osection
== NULL
)
1111 if (!bfd_set_section_size (obfd
,
1113 bfd_section_size (ibfd
, isection
)))
1119 vma
= bfd_section_vma (ibfd
, isection
);
1120 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1122 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1125 vma
+= adjust_section_vma
;
1126 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1132 lma
= isection
->lma
;
1133 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1135 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1138 lma
+= adjust_section_vma
;
1139 osection
->lma
= lma
;
1141 if (bfd_set_section_alignment (obfd
,
1143 bfd_section_alignment (ibfd
, isection
))
1150 flags
= bfd_get_section_flags (ibfd
, isection
);
1151 if (p
!= NULL
&& p
->set_flags
)
1152 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1153 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1159 /* This used to be mangle_section; we do here to avoid using
1160 bfd_get_section_by_name since some formats allow multiple
1161 sections with the same name. */
1162 isection
->output_section
= osection
;
1163 isection
->output_offset
= 0;
1165 /* Allow the BFD backend to copy any private data it understands
1166 from the input section to the output section. */
1167 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1169 err
= "private data";
1177 fprintf (stderr
, "%s: %s: section `%s': error in %s: %s\n",
1179 bfd_get_filename (ibfd
), bfd_section_name (ibfd
, isection
),
1180 err
, bfd_errmsg (bfd_get_error ()));
1184 /* Copy the data of input section ISECTION of IBFD
1185 to an output section with the same name in OBFD.
1186 If stripping then don't copy any relocation info. */
1189 copy_section (ibfd
, isection
, obfdarg
)
1194 bfd
*obfd
= (bfd
*) obfdarg
;
1195 struct section_list
*p
;
1201 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1202 && (strip_symbols
== strip_debug
1203 || strip_symbols
== strip_unneeded
1204 || strip_symbols
== strip_all
1205 || discard_locals
== locals_all
1206 || convert_debugging
))
1211 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1213 if (p
!= NULL
&& p
->remove
)
1216 osection
= isection
->output_section
;
1217 size
= bfd_get_section_size_before_reloc (isection
);
1219 if (size
== 0 || osection
== 0)
1222 if (strip_symbols
== strip_all
)
1223 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1228 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1231 nonfatal (bfd_get_filename (ibfd
));
1234 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1237 relpp
= (arelent
**) xmalloc (relsize
);
1238 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1241 nonfatal (bfd_get_filename (ibfd
));
1243 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1247 isection
->_cooked_size
= isection
->_raw_size
;
1248 isection
->reloc_done
= true;
1250 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1252 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1254 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1257 nonfatal (bfd_get_filename (ibfd
));
1262 filter_bytes (memhunk
, &size
);
1263 /* The section has gotten smaller. */
1264 if (!bfd_set_section_size (obfd
, osection
, size
))
1265 nonfatal (bfd_get_filename (obfd
));
1268 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1271 nonfatal (bfd_get_filename (obfd
));
1277 /* Get all the sections. This is used when --gap-fill or --pad-to is
1281 get_sections (obfd
, osection
, secppparg
)
1286 asection
***secppp
= (asection
***) secppparg
;
1288 **secppp
= osection
;
1292 /* Sort sections by VMA. This is called via qsort, and is used when
1293 --gap-fill or --pad-to is used. We force non loadable or empty
1294 sections to the front, where they are easier to ignore. */
1297 compare_section_vma (arg1
, arg2
)
1301 const asection
**sec1
= (const asection
**) arg1
;
1302 const asection
**sec2
= (const asection
**) arg2
;
1303 flagword flags1
, flags2
;
1305 /* Sort non loadable sections to the front. */
1306 flags1
= (*sec1
)->flags
;
1307 flags2
= (*sec2
)->flags
;
1308 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1309 || (flags1
& SEC_LOAD
) == 0)
1311 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1312 && (flags2
& SEC_LOAD
) != 0)
1317 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1318 || (flags2
& SEC_LOAD
) == 0)
1322 /* Sort sections by VMA. */
1323 if ((*sec1
)->vma
> (*sec2
)->vma
)
1325 else if ((*sec1
)->vma
< (*sec2
)->vma
)
1328 /* Sort sections with the same VMA by size. */
1329 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1331 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1337 /* Mark all the symbols which will be used in output relocations with
1338 the BSF_KEEP flag so that those symbols will not be stripped.
1340 Ignore relocations which will not appear in the output file. */
1343 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1348 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1353 /* Ignore an input section with no corresponding output section. */
1354 if (isection
->output_section
== NULL
)
1357 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1359 bfd_fatal (bfd_get_filename (ibfd
));
1364 relpp
= (arelent
**) xmalloc (relsize
);
1365 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1367 bfd_fatal (bfd_get_filename (ibfd
));
1369 /* Examine each symbol used in a relocation. If it's not one of the
1370 special bfd section symbols, then mark it with BSF_KEEP. */
1371 for (i
= 0; i
< relcount
; i
++)
1373 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1374 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1375 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1376 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1383 /* Write out debugging information. */
1386 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1392 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1393 return write_ieee_debugging_info (obfd
, dhandle
);
1395 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1396 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1398 bfd_byte
*syms
, *strings
;
1399 bfd_size_type symsize
, stringsize
;
1400 asection
*stabsec
, *stabstrsec
;
1402 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1407 stabsec
= bfd_make_section (obfd
, ".stab");
1408 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1410 || stabstrsec
== NULL
1411 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1412 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1413 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1414 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1415 || ! bfd_set_section_flags (obfd
, stabsec
,
1419 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1424 fprintf (stderr
, "%s: can't create debugging section: %s\n",
1425 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1429 /* We can get away with setting the section contents now because
1430 the next thing the caller is going to do is copy over the
1431 real sections. We may someday have to split the contents
1432 setting out of this function. */
1433 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1435 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1436 (file_ptr
) 0, stringsize
))
1438 fprintf (stderr
, "%s: can't set debugging section contents: %s\n",
1439 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1447 "%s: don't know how to write debugging information for %s\n",
1448 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1452 /* The number of bytes to copy at once. */
1453 #define COPY_BUF 8192
1455 /* Copy file FROM to file TO, performing no translations.
1456 Return 0 if ok, -1 if error. */
1459 simple_copy (from
, to
)
1463 int fromfd
, tofd
, nread
;
1467 fromfd
= open (from
, O_RDONLY
);
1470 tofd
= creat (to
, 0777);
1478 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1480 if (write (tofd
, buf
, nread
) != nread
)
1502 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1504 #define S_ISLNK(m) 0
1509 /* Rename FROM to TO, copying if TO is a link.
1510 Assumes that TO already exists, because FROM is a temp file.
1511 Return 0 if ok, -1 if error. */
1514 smart_rename (from
, to
)
1524 /* Use rename only if TO is not a symbolic link and has
1525 only one hard link. */
1526 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1528 ret
= rename (from
, to
);
1531 /* Try to preserve the permission bits and ownership of TO.
1532 First get the mode right except for the setuid bit. Then
1533 change the ownership. Then fix the setuid bit. We do
1534 the chmod before the chown because if the chown succeeds,
1535 and we are a normal user, we won't be able to do the
1536 chmod afterward. We don't bother to fix the setuid bit
1537 first because that might introduce a fleeting security
1538 problem, and because the chown will clear the setuid bit
1539 anyhow. We only fix the setuid bit if the chown
1540 succeeds, because we don't want to introduce an
1541 unexpected setuid file owned by the user running objcopy. */
1542 chmod (to
, s
.st_mode
& 0777);
1543 if (chown (to
, s
.st_uid
, s
.st_gid
) >= 0)
1544 chmod (to
, s
.st_mode
& 07777);
1548 /* We have to clean up here. */
1550 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1558 ret
= simple_copy (from
, to
);
1562 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1564 perror ("simple_copy");
1571 /* Set the times of the file DESTINATION to be the same as those in
1575 set_times (destination
, statbuf
)
1576 const char *destination
;
1577 const struct stat
*statbuf
;
1582 #ifdef HAVE_GOOD_UTIME_H
1585 tb
.actime
= statbuf
->st_atime
;
1586 tb
.modtime
= statbuf
->st_mtime
;
1587 result
= utime (destination
, &tb
);
1588 #else /* ! HAVE_GOOD_UTIME_H */
1592 tb
[0] = statbuf
->st_atime
;
1593 tb
[1] = statbuf
->st_mtime
;
1594 result
= utime (destination
, tb
);
1595 #else /* HAVE_UTIMES */
1596 struct timeval tv
[2];
1598 tv
[0].tv_sec
= statbuf
->st_atime
;
1600 tv
[1].tv_sec
= statbuf
->st_mtime
;
1602 result
= utimes (destination
, tv
);
1603 #endif /* HAVE_UTIMES */
1604 #endif /* ! HAVE_GOOD_UTIME_H */
1609 fprintf (stderr
, "%s: ", destination
);
1610 perror ("can not set time");
1615 strip_main (argc
, argv
)
1619 char *input_target
= NULL
, *output_target
= NULL
;
1620 boolean show_version
= false;
1621 boolean preserve_dates
= false;
1623 struct section_list
*p
;
1624 char *output_file
= NULL
;
1626 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1627 strip_options
, (int *) 0)) != EOF
)
1632 input_target
= optarg
;
1635 output_target
= optarg
;
1638 input_target
= output_target
= optarg
;
1641 p
= find_section_list (optarg
, true);
1643 sections_removed
= true;
1646 strip_symbols
= strip_all
;
1650 strip_symbols
= strip_debug
;
1652 case OPTION_STRIP_UNNEEDED
:
1653 strip_symbols
= strip_unneeded
;
1656 add_specific_symbol (optarg
, &keep_specific_list
);
1659 add_specific_symbol (optarg
, &strip_specific_list
);
1662 output_file
= optarg
;
1665 preserve_dates
= true;
1668 discard_locals
= locals_all
;
1671 discard_locals
= locals_start_L
;
1677 show_version
= true;
1680 break; /* we've been given a long option */
1682 strip_usage (stdout
, 0);
1684 strip_usage (stderr
, 1);
1689 print_version ("strip");
1691 /* Default is to strip all symbols. */
1692 if (strip_symbols
== strip_undef
1693 && discard_locals
== locals_undef
1694 && strip_specific_list
== NULL
)
1695 strip_symbols
= strip_all
;
1697 if (output_target
== (char *) NULL
)
1698 output_target
= input_target
;
1702 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1703 strip_usage (stderr
, 1);
1705 for (; i
< argc
; i
++)
1707 int hold_status
= status
;
1708 struct stat statbuf
;
1713 if (stat (argv
[i
], &statbuf
) < 0)
1715 fprintf (stderr
, "%s: ", argv
[i
]);
1716 perror ("cannot stat");
1721 if (output_file
!= NULL
)
1722 tmpname
= output_file
;
1724 tmpname
= make_tempname (argv
[i
]);
1727 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1731 set_times (tmpname
, &statbuf
);
1732 if (output_file
== NULL
)
1733 smart_rename (tmpname
, argv
[i
]);
1734 status
= hold_status
;
1738 if (output_file
== NULL
)
1746 copy_main (argc
, argv
)
1750 char *input_filename
= NULL
, *output_filename
= NULL
;
1751 char *input_target
= NULL
, *output_target
= NULL
;
1752 boolean show_version
= false;
1753 boolean adjust_warn
= true;
1754 boolean preserve_dates
= false;
1756 struct section_list
*p
;
1757 struct stat statbuf
;
1759 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1760 copy_options
, (int *) 0)) != EOF
)
1765 copy_byte
= atoi(optarg
);
1768 fprintf (stderr
, "%s: byte number must be non-negative\n",
1774 interleave
= atoi(optarg
);
1777 fprintf(stderr
, "%s: interleave must be positive\n",
1783 case 's': /* "source" - 'I' is preferred */
1784 input_target
= optarg
;
1787 case 'd': /* "destination" - 'O' is preferred */
1788 output_target
= optarg
;
1791 input_target
= output_target
= optarg
;
1794 p
= find_section_list (optarg
, true);
1796 sections_removed
= true;
1799 strip_symbols
= strip_all
;
1802 strip_symbols
= strip_debug
;
1804 case OPTION_STRIP_UNNEEDED
:
1805 strip_symbols
= strip_unneeded
;
1808 add_specific_symbol (optarg
, &keep_specific_list
);
1811 add_specific_symbol (optarg
, &strip_specific_list
);
1814 add_specific_symbol (optarg
, &localize_specific_list
);
1817 add_specific_symbol (optarg
, &weaken_specific_list
);
1820 preserve_dates
= true;
1823 discard_locals
= locals_all
;
1826 discard_locals
= locals_start_L
;
1832 show_version
= true;
1837 case OPTION_ADD_SECTION
:
1841 struct section_add
*pa
;
1846 s
= strchr (optarg
, '=');
1850 "%s: bad format for --add-section NAME=FILENAME\n",
1855 if (stat (s
+ 1, &st
) < 0)
1857 fprintf (stderr
, "%s: ", program_name
);
1862 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1865 name
= (char *) xmalloc (len
+ 1);
1866 strncpy (name
, optarg
, len
);
1870 pa
->filename
= s
+ 1;
1872 pa
->size
= st
.st_size
;
1874 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1875 f
= fopen (pa
->filename
, FOPEN_RB
);
1878 fprintf (stderr
, "%s: ", program_name
);
1879 perror (pa
->filename
);
1882 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1885 fprintf (stderr
, "%s: %s: fread failed\n",
1886 program_name
, pa
->filename
);
1891 pa
->next
= add_sections
;
1895 case OPTION_ADJUST_START
:
1896 adjust_start
= parse_vma (optarg
, "--adjust-start");
1898 case OPTION_ADJUST_SECTION_VMA
:
1904 s
= strchr (optarg
, '=');
1907 s
= strchr (optarg
, '+');
1910 s
= strchr (optarg
, '-');
1914 "%s: bad format for --adjust-section-vma\n",
1922 name
= (char *) xmalloc (len
+ 1);
1923 strncpy (name
, optarg
, len
);
1926 p
= find_section_list (name
, true);
1928 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1931 p
->adjust
= set_vma
;
1934 p
->adjust
= adjust_vma
;
1940 case OPTION_ADJUST_VMA
:
1941 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
1942 adjust_start
= adjust_section_vma
;
1944 case OPTION_ADJUST_WARNINGS
:
1947 case OPTION_CHANGE_LEADING_CHAR
:
1948 change_leading_char
= true;
1950 case OPTION_DEBUGGING
:
1951 convert_debugging
= true;
1953 case OPTION_GAP_FILL
:
1955 bfd_vma gap_fill_vma
;
1957 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
1958 gap_fill
= (bfd_byte
) gap_fill_vma
;
1959 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
1961 fprintf (stderr
, "%s: warning: truncating gap-fill from 0x",
1963 fprintf_vma (stderr
, gap_fill_vma
);
1964 fprintf (stderr
, "to 0x%x\n", (unsigned int) gap_fill
);
1966 gap_fill_set
= true;
1969 case OPTION_NO_ADJUST_WARNINGS
:
1970 adjust_warn
= false;
1973 pad_to
= parse_vma (optarg
, "--pad-to");
1976 case OPTION_REMOVE_LEADING_CHAR
:
1977 remove_leading_char
= true;
1979 case OPTION_SET_SECTION_FLAGS
:
1985 s
= strchr (optarg
, '=');
1988 fprintf (stderr
, "%s: bad format for --set-section-flags\n",
1994 name
= (char *) xmalloc (len
+ 1);
1995 strncpy (name
, optarg
, len
);
1998 p
= find_section_list (name
, true);
2000 p
->set_flags
= true;
2001 p
->flags
= parse_flags (s
+ 1);
2004 case OPTION_SET_START
:
2005 set_start
= parse_vma (optarg
, "--set-start");
2006 set_start_set
= true;
2009 break; /* we've been given a long option */
2011 copy_usage (stdout
, 0);
2013 copy_usage (stderr
, 1);
2018 print_version ("objcopy");
2020 if (copy_byte
>= interleave
)
2022 fprintf (stderr
, "%s: byte number must be less than interleave\n",
2027 if (optind
== argc
|| optind
+ 2 < argc
)
2028 copy_usage (stderr
, 1);
2030 input_filename
= argv
[optind
];
2031 if (optind
+ 1 < argc
)
2032 output_filename
= argv
[optind
+ 1];
2034 /* Default is to strip no symbols. */
2035 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
2036 strip_symbols
= strip_none
;
2038 if (output_target
== (char *) NULL
)
2039 output_target
= input_target
;
2043 if (stat (input_filename
, &statbuf
) < 0)
2045 fprintf (stderr
, "%s: ", input_filename
);
2046 perror ("cannot stat");
2051 /* If there is no destination file then create a temp and rename
2052 the result into the input. */
2054 if (output_filename
== (char *) NULL
)
2056 char *tmpname
= make_tempname (input_filename
);
2058 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2062 set_times (tmpname
, &statbuf
);
2063 smart_rename (tmpname
, input_filename
);
2070 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2071 if (status
== 0 && preserve_dates
)
2072 set_times (output_filename
, &statbuf
);
2077 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
2079 if (! p
->used
&& p
->adjust
!= ignore_vma
)
2081 fprintf (stderr
, "%s: warning: --adjust-section-vma %s%c0x",
2082 program_name
, p
->name
,
2083 p
->adjust
== set_vma
? '=' : '+');
2084 fprintf_vma (stderr
, p
->val
);
2085 fprintf (stderr
, " never used\n");
2098 program_name
= argv
[0];
2099 xmalloc_set_program_name (program_name
);
2101 START_PROGRESS (program_name
, 0);
2103 strip_symbols
= strip_undef
;
2104 discard_locals
= locals_undef
;
2107 set_default_bfd_target ();
2111 int i
= strlen (program_name
);
2112 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2116 strip_main (argc
, argv
);
2118 copy_main (argc
, argv
);
2120 END_PROGRESS (program_name
);