1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "libiberty.h"
30 #ifdef HAVE_GOOD_UTIME_H
32 #else /* ! HAVE_GOOD_UTIME_H */
35 #endif /* HAVE_UTIMES */
36 #endif /* ! HAVE_GOOD_UTIME_H */
38 /* A list of symbols to explicitly strip out, or to keep. A linked
39 list is good enough for a small number from the command line, but
40 this will slow things down a lot if many symbols are being
49 static void copy_usage
PARAMS ((FILE *, int));
50 static void strip_usage
PARAMS ((FILE *, int));
51 static flagword parse_flags
PARAMS ((const char *));
52 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
53 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
54 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
55 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
56 static int compare_section_lma
PARAMS ((const PTR
, const PTR
));
57 static void add_specific_symbol
PARAMS ((const char *, struct symlist
**));
58 static boolean is_specified_symbol
PARAMS ((const char *, struct symlist
*));
59 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
60 static unsigned int filter_symbols
61 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
62 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
63 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
64 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
65 static void copy_object
PARAMS ((bfd
*, bfd
*));
66 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
68 PARAMS ((const char *, const char *, const char *, const char *));
69 static int simple_copy
PARAMS ((const char *, const char *));
70 static int smart_rename
PARAMS ((const char *, const char *));
71 static void set_times
PARAMS ((const char *, const struct stat
*));
72 static int strip_main
PARAMS ((int, char **));
73 static int copy_main
PARAMS ((int, char **));
75 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
77 static asymbol
**isympp
= NULL
; /* Input symbols */
78 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
80 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
81 static int copy_byte
= -1;
82 static int interleave
= 4;
84 static boolean verbose
; /* Print file and target names. */
85 static boolean preserve_dates
; /* Preserve input file timestamp. */
86 static int status
= 0; /* Exit status. */
91 strip_none
, /* don't strip */
92 strip_debug
, /* strip all debugger symbols */
93 strip_unneeded
, /* strip unnecessary symbols */
94 strip_all
/* strip all symbols */
97 /* Which symbols to remove. */
98 static enum strip_action strip_symbols
;
103 locals_start_L
, /* discard locals starting with L */
104 locals_all
/* discard all locals */
107 /* Which local symbols to remove. Overrides strip_all. */
108 static enum locals_action discard_locals
;
110 /* Structure used to hold lists of sections and actions to take. */
114 /* Next section to adjust. */
115 struct section_list
*next
;
118 /* Whether this entry was used. */
120 /* Whether to remove this section. */
122 /* Whether to adjust or set VMA. */
123 enum { ignore_vma
, adjust_vma
, set_vma
} adjust
;
124 /* Amount to adjust by or set to. */
126 /* Whether to set the section flags. */
128 /* What to set the section flags to. */
132 static struct section_list
*adjust_sections
;
133 static boolean sections_removed
;
135 /* Adjustments to the start address. */
136 static bfd_vma adjust_start
= 0;
137 static boolean set_start_set
= false;
138 static bfd_vma set_start
;
140 /* Adjustments to section VMA's. */
141 static bfd_vma adjust_section_vma
= 0;
143 /* Filling gaps between sections. */
144 static boolean gap_fill_set
= false;
145 static bfd_byte gap_fill
= 0;
147 /* Pad to a given address. */
148 static boolean pad_to_set
= false;
149 static bfd_vma pad_to
;
151 /* List of sections to add. */
155 /* Next section to add. */
156 struct section_add
*next
;
157 /* Name of section to add. */
159 /* Name of file holding section contents. */
160 const char *filename
;
163 /* Contents of file. */
165 /* BFD section, after it has been added. */
169 static struct section_add
*add_sections
;
171 /* Whether to convert debugging information. */
173 static boolean convert_debugging
= false;
175 /* Whether to change the leading character in symbol names. */
177 static boolean change_leading_char
= false;
179 /* Whether to remove the leading character from global symbol names. */
181 static boolean remove_leading_char
= false;
183 /* List of symbols to strip, keep, localize, and weaken. */
185 static struct symlist
*strip_specific_list
= NULL
;
186 static struct symlist
*keep_specific_list
= NULL
;
187 static struct symlist
*localize_specific_list
= NULL
;
188 static struct symlist
*weaken_specific_list
= NULL
;
190 /* If this is true, we weaken global symbols (set BSF_WEAK). */
192 static boolean weaken
= false;
194 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
196 #define OPTION_ADD_SECTION 150
197 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
198 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
199 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
200 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
201 #define OPTION_CHANGE_LEADING_CHAR (OPTION_ADJUST_WARNINGS + 1)
202 #define OPTION_DEBUGGING (OPTION_CHANGE_LEADING_CHAR + 1)
203 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
204 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
205 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
206 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
207 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
208 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
209 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
210 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
212 /* Options to handle if running as "strip". */
214 static struct option strip_options
[] =
216 {"discard-all", no_argument
, 0, 'x'},
217 {"discard-locals", no_argument
, 0, 'X'},
218 {"format", required_argument
, 0, 'F'}, /* Obsolete */
219 {"help", no_argument
, 0, 'h'},
220 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
221 {"input-target", required_argument
, 0, 'I'},
222 {"keep-symbol", required_argument
, 0, 'K'},
223 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
224 {"output-target", required_argument
, 0, 'O'},
225 {"preserve-dates", no_argument
, 0, 'p'},
226 {"remove-section", required_argument
, 0, 'R'},
227 {"strip-all", no_argument
, 0, 's'},
228 {"strip-debug", no_argument
, 0, 'S'},
229 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
230 {"strip-symbol", required_argument
, 0, 'N'},
231 {"target", required_argument
, 0, 'F'},
232 {"verbose", no_argument
, 0, 'v'},
233 {"version", no_argument
, 0, 'V'},
234 {0, no_argument
, 0, 0}
237 /* Options to handle if running as "objcopy". */
239 static struct option copy_options
[] =
241 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
242 {"adjust-start", required_argument
, 0, OPTION_ADJUST_START
},
243 {"adjust-vma", required_argument
, 0, OPTION_ADJUST_VMA
},
244 {"adjust-section-vma", required_argument
, 0, OPTION_ADJUST_SECTION_VMA
},
245 {"adjust-warnings", no_argument
, 0, OPTION_ADJUST_WARNINGS
},
246 {"byte", required_argument
, 0, 'b'},
247 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
248 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
249 {"discard-all", no_argument
, 0, 'x'},
250 {"discard-locals", no_argument
, 0, 'X'},
251 {"format", required_argument
, 0, 'F'}, /* Obsolete */
252 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
253 {"help", no_argument
, 0, 'h'},
254 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
255 {"input-target", required_argument
, 0, 'I'},
256 {"interleave", required_argument
, 0, 'i'},
257 {"keep-symbol", required_argument
, 0, 'K'},
258 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_ADJUST_WARNINGS
},
259 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
260 {"output-target", required_argument
, 0, 'O'},
261 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
262 {"preserve-dates", no_argument
, 0, 'p'},
263 {"localize-symbol", required_argument
, 0, 'L'},
264 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
265 {"remove-section", required_argument
, 0, 'R'},
266 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
267 {"set-start", required_argument
, 0, OPTION_SET_START
},
268 {"strip-all", no_argument
, 0, 'S'},
269 {"strip-debug", no_argument
, 0, 'g'},
270 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
271 {"strip-symbol", required_argument
, 0, 'N'},
272 {"target", required_argument
, 0, 'F'},
273 {"verbose", no_argument
, 0, 'v'},
274 {"version", no_argument
, 0, 'V'},
275 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
276 {"weaken-symbol", required_argument
, 0, 'W'},
277 {0, no_argument
, 0, 0}
281 extern char *program_name
;
283 /* This flag distinguishes between strip and objcopy:
284 1 means this is 'strip'; 0 means this is 'objcopy'.
285 -1 means if we should use argv[0] to decide. */
290 copy_usage (stream
, exit_status
)
295 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
296 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
297 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
298 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
299 [--discard-locals] [--debugging] [--remove-section=section]\n",
302 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
303 [--set-start=val] [--adjust-start=incr]\n\
304 [--adjust-vma=incr] [--adjust-section-vma=section{=,+,-}val]\n\
305 [--adjust-warnings] [--no-adjust-warnings]\n\
306 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
307 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
308 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
309 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
310 [--verbose] [--version] [--help] in-file [out-file]\n");
311 list_supported_targets (program_name
, stream
);
312 if (exit_status
== 0)
313 fprintf (stream
, "Report bugs to bug-gnu-utils@gnu.org\n");
318 strip_usage (stream
, exit_status
)
323 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
324 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
325 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
326 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
327 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
328 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n",
330 list_supported_targets (program_name
, stream
);
331 if (exit_status
== 0)
332 fprintf (stream
, "Report bugs to bug-gnu-utils@gnu.org\n");
336 /* Parse section flags into a flagword, with a fatal error if the
337 string can't be parsed. */
351 snext
= strchr (s
, ',');
361 #define PARSE_FLAG(fname,fval) \
362 else if (strncasecmp (fname, s, len) == 0) ret |= fval
363 PARSE_FLAG ("alloc", SEC_ALLOC
);
364 PARSE_FLAG ("load", SEC_LOAD
);
365 PARSE_FLAG ("readonly", SEC_READONLY
);
366 PARSE_FLAG ("code", SEC_CODE
);
367 PARSE_FLAG ("data", SEC_DATA
);
368 PARSE_FLAG ("rom", SEC_ROM
);
369 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
375 copy
= xmalloc (len
+ 1);
376 strncpy (copy
, s
, len
);
378 fprintf (stderr
, "%s: unrecognized section flag `%s'\n",
381 "%s: supported flags: alloc, load, readonly, code, data, rom, contents\n",
393 /* Find and optionally add an entry in the adjust_sections list. */
395 static struct section_list
*
396 find_section_list (name
, add
)
400 register struct section_list
*p
;
402 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
403 if (strcmp (p
->name
, name
) == 0)
409 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
413 p
->adjust
= ignore_vma
;
415 p
->set_flags
= false;
418 p
->next
= adjust_sections
;
424 /* Add a symbol to strip_specific_list. */
427 add_specific_symbol (name
, list
)
429 struct symlist
**list
;
431 struct symlist
*tmp_list
;
433 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
434 tmp_list
->name
= name
;
435 tmp_list
->next
= *list
;
439 /* See whether a symbol should be stripped or kept based on
440 strip_specific_list and keep_symbols. */
443 is_specified_symbol (name
, list
)
445 struct symlist
*list
;
447 struct symlist
*tmp_list
;
449 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
451 if (strcmp (name
, tmp_list
->name
) == 0)
457 /* See if a section is being removed. */
460 is_strip_section (abfd
, sec
)
464 struct section_list
*p
;
466 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
467 && (strip_symbols
== strip_debug
468 || strip_symbols
== strip_unneeded
469 || strip_symbols
== strip_all
470 || discard_locals
== locals_all
471 || convert_debugging
))
474 if (! sections_removed
)
476 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
477 return p
!= NULL
&& p
->remove
? true : false;
480 /* Choose which symbol entries to copy; put the result in OSYMS.
481 We don't copy in place, because that confuses the relocs.
482 Return the number of symbols to print. */
485 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
488 asymbol
**osyms
, **isyms
;
491 register asymbol
**from
= isyms
, **to
= osyms
;
492 long src_count
= 0, dst_count
= 0;
494 for (; src_count
< symcount
; src_count
++)
496 asymbol
*sym
= from
[src_count
];
497 flagword flags
= sym
->flags
;
498 const char *name
= bfd_asymbol_name (sym
);
501 if (change_leading_char
502 && (bfd_get_symbol_leading_char (abfd
)
503 != bfd_get_symbol_leading_char (obfd
))
504 && (bfd_get_symbol_leading_char (abfd
) == '\0'
505 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
507 if (bfd_get_symbol_leading_char (obfd
) == '\0')
508 name
= bfd_asymbol_name (sym
) = name
+ 1;
513 n
= xmalloc (strlen (name
) + 2);
514 n
[0] = bfd_get_symbol_leading_char (obfd
);
515 if (bfd_get_symbol_leading_char (abfd
) == '\0')
516 strcpy (n
+ 1, name
);
518 strcpy (n
+ 1, name
+ 1);
519 name
= bfd_asymbol_name (sym
) = n
;
523 if (remove_leading_char
524 && ((flags
& BSF_GLOBAL
) != 0
525 || (flags
& BSF_WEAK
) != 0
526 || bfd_is_und_section (bfd_get_section (sym
))
527 || bfd_is_com_section (bfd_get_section (sym
)))
528 && name
[0] == bfd_get_symbol_leading_char (abfd
))
529 name
= bfd_asymbol_name (sym
) = name
+ 1;
531 if ((flags
& BSF_KEEP
) != 0) /* Used in relocation. */
533 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
534 || (flags
& BSF_WEAK
) != 0
535 || bfd_is_und_section (bfd_get_section (sym
))
536 || bfd_is_com_section (bfd_get_section (sym
)))
537 keep
= strip_symbols
!= strip_unneeded
;
538 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
539 keep
= (strip_symbols
!= strip_debug
540 && strip_symbols
!= strip_unneeded
541 && ! convert_debugging
);
542 else /* Local symbol. */
543 keep
= (strip_symbols
!= strip_unneeded
544 && (discard_locals
!= locals_all
545 && (discard_locals
!= locals_start_L
546 || ! bfd_is_local_label (abfd
, sym
))));
548 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
550 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
552 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
555 if (keep
&& (flags
& BSF_GLOBAL
) != 0
556 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
558 sym
->flags
&=~ BSF_GLOBAL
;
559 sym
->flags
|= BSF_WEAK
;
561 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
562 && is_specified_symbol (name
, localize_specific_list
))
564 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
565 sym
->flags
|= BSF_LOCAL
;
569 to
[dst_count
++] = sym
;
572 to
[dst_count
] = NULL
;
577 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
581 filter_bytes (memhunk
, size
)
585 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
587 for (; from
< end
; from
+= interleave
)
592 /* Copy object file IBFD onto OBFD. */
595 copy_object (ibfd
, obfd
)
601 asection
**osections
= NULL
;
602 bfd_size_type
*gaps
= NULL
;
603 bfd_size_type max_gap
= 0;
605 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
607 nonfatal (bfd_get_filename (obfd
));
611 printf ("copy from %s(%s) to %s(%s)\n",
612 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
613 bfd_get_filename(obfd
), bfd_get_target(obfd
));
618 start
= bfd_get_start_address (ibfd
);
619 start
+= adjust_start
;
621 if (!bfd_set_start_address (obfd
, start
)
622 || !bfd_set_file_flags (obfd
,
623 (bfd_get_file_flags (ibfd
)
624 & bfd_applicable_file_flags (obfd
))))
626 nonfatal (bfd_get_filename (ibfd
));
629 /* Copy architecture of input file to output file */
630 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
631 bfd_get_mach (ibfd
)))
634 "Warning: Output file cannot represent architecture %s\n",
635 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
636 bfd_get_mach (ibfd
)));
638 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
640 nonfatal (bfd_get_filename(ibfd
));
645 if (osympp
!= isympp
)
648 /* bfd mandates that all output sections be created and sizes set before
649 any output is done. Thus, we traverse all sections multiple times. */
650 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
652 if (add_sections
!= NULL
)
654 struct section_add
*padd
;
655 struct section_list
*pset
;
657 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
659 padd
->section
= bfd_make_section (obfd
, padd
->name
);
660 if (padd
->section
== NULL
)
662 fprintf (stderr
, "%s: can't create section `%s': %s\n",
663 program_name
, padd
->name
,
664 bfd_errmsg (bfd_get_error ()));
672 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
673 nonfatal (bfd_get_filename (obfd
));
675 pset
= find_section_list (padd
->name
, false);
679 if (pset
!= NULL
&& pset
->set_flags
)
680 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
682 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
683 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
684 nonfatal (bfd_get_filename (obfd
));
687 && (pset
->adjust
== adjust_vma
688 || pset
->adjust
== set_vma
))
690 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->val
))
691 nonfatal (bfd_get_filename (obfd
));
697 if (gap_fill_set
|| pad_to_set
)
702 /* We must fill in gaps between the sections and/or we must pad
703 the last section to a specified address. We do this by
704 grabbing a list of the sections, sorting them by VMA, and
705 increasing the section sizes as required to fill the gaps.
706 We write out the gap contents below. */
708 c
= bfd_count_sections (obfd
);
709 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
711 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
713 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
715 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
716 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
720 for (i
= 0; i
< c
- 1; i
++)
724 bfd_vma gap_start
, gap_stop
;
726 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
727 if ((flags
& SEC_HAS_CONTENTS
) == 0
728 || (flags
& SEC_LOAD
) == 0)
731 size
= bfd_section_size (obfd
, osections
[i
]);
732 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
733 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
734 if (gap_start
< gap_stop
)
736 if (! bfd_set_section_size (obfd
, osections
[i
],
737 size
+ (gap_stop
- gap_start
)))
739 fprintf (stderr
, "%s: Can't fill gap after %s: %s\n",
741 bfd_get_section_name (obfd
, osections
[i
]),
742 bfd_errmsg (bfd_get_error()));
746 gaps
[i
] = gap_stop
- gap_start
;
747 if (max_gap
< gap_stop
- gap_start
)
748 max_gap
= gap_stop
- gap_start
;
758 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
759 size
= bfd_section_size (obfd
, osections
[c
- 1]);
760 if (lma
+ size
< pad_to
)
762 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
765 fprintf (stderr
, "%s: Can't add padding to %s: %s\n",
767 bfd_get_section_name (obfd
, osections
[c
- 1]),
768 bfd_errmsg (bfd_get_error ()));
773 gaps
[c
- 1] = pad_to
- (lma
+ size
);
774 if (max_gap
< pad_to
- (lma
+ size
))
775 max_gap
= pad_to
- (lma
+ size
);
781 /* Symbol filtering must happen after the output sections have
782 been created, but before their contents are set. */
783 if (strip_symbols
== strip_all
)
785 osympp
= isympp
= NULL
;
793 symsize
= bfd_get_symtab_upper_bound (ibfd
);
796 nonfatal (bfd_get_filename (ibfd
));
799 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
800 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
803 nonfatal (bfd_get_filename (ibfd
));
806 if (convert_debugging
)
807 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
809 if (strip_symbols
== strip_debug
810 || strip_symbols
== strip_unneeded
811 || discard_locals
!= locals_undef
812 || strip_specific_list
!= NULL
813 || keep_specific_list
!= NULL
814 || localize_specific_list
!= NULL
815 || weaken_specific_list
!= NULL
818 || change_leading_char
819 || remove_leading_char
822 /* Mark symbols used in output relocations so that they
823 are kept, even if they are local labels or static symbols.
825 Note we iterate over the input sections examining their
826 relocations since the relocations for the output sections
827 haven't been set yet. mark_symbols_used_in_relocations will
828 ignore input sections which have no corresponding output
830 bfd_map_over_sections (ibfd
,
831 mark_symbols_used_in_relocations
,
833 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
834 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
837 if (convert_debugging
&& dhandle
!= NULL
)
839 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
847 bfd_set_symtab (obfd
, osympp
, symcount
);
849 /* This has to happen after the symbol table has been set. */
850 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
852 if (add_sections
!= NULL
)
854 struct section_add
*padd
;
856 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
858 if (! bfd_set_section_contents (obfd
, padd
->section
,
859 (PTR
) padd
->contents
,
861 (bfd_size_type
) padd
->size
))
862 nonfatal (bfd_get_filename (obfd
));
866 if (gap_fill_set
|| pad_to_set
)
871 /* Fill in the gaps. */
875 buf
= (bfd_byte
*) xmalloc (max_gap
);
876 memset (buf
, gap_fill
, (size_t) max_gap
);
878 c
= bfd_count_sections (obfd
);
879 for (i
= 0; i
< c
; i
++)
887 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
896 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
899 nonfatal (bfd_get_filename (obfd
));
908 /* Allow the BFD backend to copy any private data it understands
909 from the input BFD to the output BFD. This is done last to
910 permit the routine to look at the filtered symbol table, which is
911 important for the ECOFF code at least. */
912 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
914 fprintf (stderr
, "%s: %s: error copying private BFD data: %s\n",
915 program_name
, bfd_get_filename (obfd
),
916 bfd_errmsg (bfd_get_error ()));
922 /* Read each archive element in turn from IBFD, copy the
923 contents to temp file, and keep the temp file handle. */
926 copy_archive (ibfd
, obfd
, output_target
)
929 const char *output_target
;
933 struct name_list
*next
;
937 bfd
**ptr
= &obfd
->archive_head
;
939 char *dir
= make_tempname (bfd_get_filename (obfd
));
941 /* Make a temp directory to hold the contents. */
942 #if defined (_WIN32) && !defined (__CYGWIN32__)
943 if (mkdir (dir
) != 0)
945 if (mkdir (dir
, 0700) != 0)
948 fatal ("cannot mkdir %s for archive copying (error: %s)",
949 dir
, strerror (errno
));
951 obfd
->has_armap
= ibfd
->has_armap
;
955 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
956 while (this_element
!= (bfd
*) NULL
)
958 /* Create an output file for this member. */
959 char *output_name
= concat (dir
, "/", bfd_get_filename(this_element
),
961 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
964 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
965 l
->name
= output_name
;
969 if (output_bfd
== (bfd
*) NULL
)
971 nonfatal (output_name
);
973 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
975 nonfatal (bfd_get_filename (obfd
));
978 if (bfd_check_format (this_element
, bfd_object
) == true)
980 copy_object (this_element
, output_bfd
);
983 bfd_close (output_bfd
);
985 /* Open the newly output file and attach to our list. */
986 output_bfd
= bfd_openr (output_name
, output_target
);
988 l
->obfd
= output_bfd
;
991 ptr
= &output_bfd
->next
;
993 last_element
= this_element
;
995 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
997 bfd_close (last_element
);
1001 if (!bfd_close (obfd
))
1003 nonfatal (bfd_get_filename (obfd
));
1006 if (!bfd_close (ibfd
))
1008 nonfatal (bfd_get_filename (ibfd
));
1011 /* Delete all the files that we opened. */
1012 for (l
= list
; l
!= NULL
; l
= l
->next
)
1014 bfd_close (l
->obfd
);
1020 /* The top-level control. */
1023 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1024 const char *input_filename
;
1025 const char *output_filename
;
1026 const char *input_target
;
1027 const char *output_target
;
1032 /* To allow us to do "strip *" without dying on the first
1033 non-object file, failures are nonfatal. */
1035 ibfd
= bfd_openr (input_filename
, input_target
);
1038 nonfatal (input_filename
);
1041 if (bfd_check_format (ibfd
, bfd_archive
))
1045 /* bfd_get_target does not return the correct value until
1046 bfd_check_format succeeds. */
1047 if (output_target
== NULL
)
1048 output_target
= bfd_get_target (ibfd
);
1050 obfd
= bfd_openw (output_filename
, output_target
);
1053 nonfatal (output_filename
);
1055 copy_archive (ibfd
, obfd
, output_target
);
1057 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1061 /* bfd_get_target does not return the correct value until
1062 bfd_check_format succeeds. */
1063 if (output_target
== NULL
)
1064 output_target
= bfd_get_target (ibfd
);
1066 obfd
= bfd_openw (output_filename
, output_target
);
1069 nonfatal (output_filename
);
1072 copy_object (ibfd
, obfd
);
1074 if (!bfd_close (obfd
))
1076 nonfatal (output_filename
);
1079 if (!bfd_close (ibfd
))
1081 nonfatal (input_filename
);
1086 bfd_nonfatal (input_filename
);
1087 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1089 list_matching_formats (matching
);
1096 /* Create a section in OBFD with the same name and attributes
1097 as ISECTION in IBFD. */
1100 setup_section (ibfd
, isection
, obfdarg
)
1105 bfd
*obfd
= (bfd
*) obfdarg
;
1106 struct section_list
*p
;
1113 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1114 && (strip_symbols
== strip_debug
1115 || strip_symbols
== strip_unneeded
1116 || strip_symbols
== strip_all
1117 || discard_locals
== locals_all
1118 || convert_debugging
))
1121 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1125 if (p
!= NULL
&& p
->remove
)
1128 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1129 if (osection
== NULL
)
1135 if (!bfd_set_section_size (obfd
,
1137 bfd_section_size (ibfd
, isection
)))
1143 vma
= bfd_section_vma (ibfd
, isection
);
1144 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1146 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1149 vma
+= adjust_section_vma
;
1150 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1156 lma
= isection
->lma
;
1157 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1159 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1162 lma
+= adjust_section_vma
;
1163 osection
->lma
= lma
;
1165 if (bfd_set_section_alignment (obfd
,
1167 bfd_section_alignment (ibfd
, isection
))
1174 flags
= bfd_get_section_flags (ibfd
, isection
);
1175 if (p
!= NULL
&& p
->set_flags
)
1176 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1177 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1183 /* This used to be mangle_section; we do here to avoid using
1184 bfd_get_section_by_name since some formats allow multiple
1185 sections with the same name. */
1186 isection
->output_section
= osection
;
1187 isection
->output_offset
= 0;
1189 /* Allow the BFD backend to copy any private data it understands
1190 from the input section to the output section. */
1191 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1193 err
= "private data";
1201 fprintf (stderr
, "%s: %s: section `%s': error in %s: %s\n",
1203 bfd_get_filename (ibfd
), bfd_section_name (ibfd
, isection
),
1204 err
, bfd_errmsg (bfd_get_error ()));
1208 /* Copy the data of input section ISECTION of IBFD
1209 to an output section with the same name in OBFD.
1210 If stripping then don't copy any relocation info. */
1213 copy_section (ibfd
, isection
, obfdarg
)
1218 bfd
*obfd
= (bfd
*) obfdarg
;
1219 struct section_list
*p
;
1225 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1226 && (strip_symbols
== strip_debug
1227 || strip_symbols
== strip_unneeded
1228 || strip_symbols
== strip_all
1229 || discard_locals
== locals_all
1230 || convert_debugging
))
1235 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1237 if (p
!= NULL
&& p
->remove
)
1240 osection
= isection
->output_section
;
1241 size
= bfd_get_section_size_before_reloc (isection
);
1243 if (size
== 0 || osection
== 0)
1246 if (strip_symbols
== strip_all
)
1247 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1252 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1255 nonfatal (bfd_get_filename (ibfd
));
1258 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1261 relpp
= (arelent
**) xmalloc (relsize
);
1262 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1265 nonfatal (bfd_get_filename (ibfd
));
1267 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1271 isection
->_cooked_size
= isection
->_raw_size
;
1272 isection
->reloc_done
= true;
1274 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1276 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1278 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1281 nonfatal (bfd_get_filename (ibfd
));
1286 filter_bytes (memhunk
, &size
);
1287 /* The section has gotten smaller. */
1288 if (!bfd_set_section_size (obfd
, osection
, size
))
1289 nonfatal (bfd_get_filename (obfd
));
1292 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1295 nonfatal (bfd_get_filename (obfd
));
1299 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1301 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1303 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1304 flag--they can just remove the section entirely and add it
1305 back again. However, we do permit them to turn on the
1306 SEC_HAS_CONTENTS flag, and take it to mean that the section
1307 contents should be zeroed out. */
1309 memset (memhunk
, 0, size
);
1310 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1312 nonfatal (bfd_get_filename (obfd
));
1317 /* Get all the sections. This is used when --gap-fill or --pad-to is
1321 get_sections (obfd
, osection
, secppparg
)
1326 asection
***secppp
= (asection
***) secppparg
;
1328 **secppp
= osection
;
1332 /* Sort sections by VMA. This is called via qsort, and is used when
1333 --gap-fill or --pad-to is used. We force non loadable or empty
1334 sections to the front, where they are easier to ignore. */
1337 compare_section_lma (arg1
, arg2
)
1341 const asection
**sec1
= (const asection
**) arg1
;
1342 const asection
**sec2
= (const asection
**) arg2
;
1343 flagword flags1
, flags2
;
1345 /* Sort non loadable sections to the front. */
1346 flags1
= (*sec1
)->flags
;
1347 flags2
= (*sec2
)->flags
;
1348 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1349 || (flags1
& SEC_LOAD
) == 0)
1351 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1352 && (flags2
& SEC_LOAD
) != 0)
1357 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1358 || (flags2
& SEC_LOAD
) == 0)
1362 /* Sort sections by LMA. */
1363 if ((*sec1
)->lma
> (*sec2
)->lma
)
1365 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1368 /* Sort sections with the same LMA by size. */
1369 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1371 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1377 /* Mark all the symbols which will be used in output relocations with
1378 the BSF_KEEP flag so that those symbols will not be stripped.
1380 Ignore relocations which will not appear in the output file. */
1383 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1388 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1393 /* Ignore an input section with no corresponding output section. */
1394 if (isection
->output_section
== NULL
)
1397 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1399 bfd_fatal (bfd_get_filename (ibfd
));
1404 relpp
= (arelent
**) xmalloc (relsize
);
1405 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1407 bfd_fatal (bfd_get_filename (ibfd
));
1409 /* Examine each symbol used in a relocation. If it's not one of the
1410 special bfd section symbols, then mark it with BSF_KEEP. */
1411 for (i
= 0; i
< relcount
; i
++)
1413 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1414 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1415 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1416 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1423 /* Write out debugging information. */
1426 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1432 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1433 return write_ieee_debugging_info (obfd
, dhandle
);
1435 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1436 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1438 bfd_byte
*syms
, *strings
;
1439 bfd_size_type symsize
, stringsize
;
1440 asection
*stabsec
, *stabstrsec
;
1442 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1447 stabsec
= bfd_make_section (obfd
, ".stab");
1448 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1450 || stabstrsec
== NULL
1451 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1452 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1453 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1454 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1455 || ! bfd_set_section_flags (obfd
, stabsec
,
1459 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1464 fprintf (stderr
, "%s: can't create debugging section: %s\n",
1465 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1469 /* We can get away with setting the section contents now because
1470 the next thing the caller is going to do is copy over the
1471 real sections. We may someday have to split the contents
1472 setting out of this function. */
1473 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1475 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1476 (file_ptr
) 0, stringsize
))
1478 fprintf (stderr
, "%s: can't set debugging section contents: %s\n",
1479 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1487 "%s: don't know how to write debugging information for %s\n",
1488 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1492 /* The number of bytes to copy at once. */
1493 #define COPY_BUF 8192
1495 /* Copy file FROM to file TO, performing no translations.
1496 Return 0 if ok, -1 if error. */
1499 simple_copy (from
, to
)
1503 int fromfd
, tofd
, nread
;
1507 fromfd
= open (from
, O_RDONLY
);
1510 tofd
= creat (to
, 0777);
1518 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1520 if (write (tofd
, buf
, nread
) != nread
)
1542 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1544 #define S_ISLNK(m) 0
1549 /* Rename FROM to TO, copying if TO is a link.
1550 Assumes that TO already exists, because FROM is a temp file.
1551 Return 0 if ok, -1 if error. */
1554 smart_rename (from
, to
)
1564 #if defined (_WIN32) && !defined (__CYGWIN32__)
1565 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1566 fail instead. Also, chown is not present. */
1568 if (stat (to
, &s
) == 0)
1571 ret
= rename (from
, to
);
1574 /* We have to clean up here. */
1576 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1582 /* Use rename only if TO is not a symbolic link and has
1583 only one hard link. */
1584 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1586 ret
= rename (from
, to
);
1589 /* Try to preserve the permission bits and ownership of TO.
1590 First get the mode right except for the setuid bit. Then
1591 change the ownership. Then fix the setuid bit. We do
1592 the chmod before the chown because if the chown succeeds,
1593 and we are a normal user, we won't be able to do the
1594 chmod afterward. We don't bother to fix the setuid bit
1595 first because that might introduce a fleeting security
1596 problem, and because the chown will clear the setuid bit
1597 anyhow. We only fix the setuid bit if the chown
1598 succeeds, because we don't want to introduce an
1599 unexpected setuid file owned by the user running objcopy. */
1600 chmod (to
, s
.st_mode
& 0777);
1601 if (chown (to
, s
.st_uid
, s
.st_gid
) >= 0)
1602 chmod (to
, s
.st_mode
& 07777);
1606 /* We have to clean up here. */
1608 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1616 ret
= simple_copy (from
, to
);
1620 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1622 perror ("simple_copy");
1628 #endif /* _WIN32 && !__CYGWIN32__ */
1633 /* Set the times of the file DESTINATION to be the same as those in
1637 set_times (destination
, statbuf
)
1638 const char *destination
;
1639 const struct stat
*statbuf
;
1644 #ifdef HAVE_GOOD_UTIME_H
1647 tb
.actime
= statbuf
->st_atime
;
1648 tb
.modtime
= statbuf
->st_mtime
;
1649 result
= utime (destination
, &tb
);
1650 #else /* ! HAVE_GOOD_UTIME_H */
1654 tb
[0] = statbuf
->st_atime
;
1655 tb
[1] = statbuf
->st_mtime
;
1656 result
= utime (destination
, tb
);
1657 #else /* HAVE_UTIMES */
1658 struct timeval tv
[2];
1660 tv
[0].tv_sec
= statbuf
->st_atime
;
1662 tv
[1].tv_sec
= statbuf
->st_mtime
;
1664 result
= utimes (destination
, tv
);
1665 #endif /* HAVE_UTIMES */
1666 #endif /* ! HAVE_GOOD_UTIME_H */
1671 fprintf (stderr
, "%s: ", destination
);
1672 perror ("can not set time");
1677 strip_main (argc
, argv
)
1681 char *input_target
= NULL
, *output_target
= NULL
;
1682 boolean show_version
= false;
1684 struct section_list
*p
;
1685 char *output_file
= NULL
;
1687 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1688 strip_options
, (int *) 0)) != EOF
)
1693 input_target
= optarg
;
1696 output_target
= optarg
;
1699 input_target
= output_target
= optarg
;
1702 p
= find_section_list (optarg
, true);
1704 sections_removed
= true;
1707 strip_symbols
= strip_all
;
1711 strip_symbols
= strip_debug
;
1713 case OPTION_STRIP_UNNEEDED
:
1714 strip_symbols
= strip_unneeded
;
1717 add_specific_symbol (optarg
, &keep_specific_list
);
1720 add_specific_symbol (optarg
, &strip_specific_list
);
1723 output_file
= optarg
;
1726 preserve_dates
= true;
1729 discard_locals
= locals_all
;
1732 discard_locals
= locals_start_L
;
1738 show_version
= true;
1741 break; /* we've been given a long option */
1743 strip_usage (stdout
, 0);
1745 strip_usage (stderr
, 1);
1750 print_version ("strip");
1752 /* Default is to strip all symbols. */
1753 if (strip_symbols
== strip_undef
1754 && discard_locals
== locals_undef
1755 && strip_specific_list
== NULL
)
1756 strip_symbols
= strip_all
;
1758 if (output_target
== (char *) NULL
)
1759 output_target
= input_target
;
1763 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1764 strip_usage (stderr
, 1);
1766 for (; i
< argc
; i
++)
1768 int hold_status
= status
;
1769 struct stat statbuf
;
1774 if (stat (argv
[i
], &statbuf
) < 0)
1776 fprintf (stderr
, "%s: ", argv
[i
]);
1777 perror ("cannot stat");
1782 if (output_file
!= NULL
)
1783 tmpname
= output_file
;
1785 tmpname
= make_tempname (argv
[i
]);
1788 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1792 set_times (tmpname
, &statbuf
);
1793 if (output_file
== NULL
)
1794 smart_rename (tmpname
, argv
[i
]);
1795 status
= hold_status
;
1799 if (output_file
== NULL
)
1807 copy_main (argc
, argv
)
1811 char *input_filename
= NULL
, *output_filename
= NULL
;
1812 char *input_target
= NULL
, *output_target
= NULL
;
1813 boolean show_version
= false;
1814 boolean adjust_warn
= true;
1816 struct section_list
*p
;
1817 struct stat statbuf
;
1819 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1820 copy_options
, (int *) 0)) != EOF
)
1825 copy_byte
= atoi(optarg
);
1828 fprintf (stderr
, "%s: byte number must be non-negative\n",
1834 interleave
= atoi(optarg
);
1837 fprintf(stderr
, "%s: interleave must be positive\n",
1843 case 's': /* "source" - 'I' is preferred */
1844 input_target
= optarg
;
1847 case 'd': /* "destination" - 'O' is preferred */
1848 output_target
= optarg
;
1851 input_target
= output_target
= optarg
;
1854 p
= find_section_list (optarg
, true);
1856 sections_removed
= true;
1859 strip_symbols
= strip_all
;
1862 strip_symbols
= strip_debug
;
1864 case OPTION_STRIP_UNNEEDED
:
1865 strip_symbols
= strip_unneeded
;
1868 add_specific_symbol (optarg
, &keep_specific_list
);
1871 add_specific_symbol (optarg
, &strip_specific_list
);
1874 add_specific_symbol (optarg
, &localize_specific_list
);
1877 add_specific_symbol (optarg
, &weaken_specific_list
);
1880 preserve_dates
= true;
1883 discard_locals
= locals_all
;
1886 discard_locals
= locals_start_L
;
1892 show_version
= true;
1897 case OPTION_ADD_SECTION
:
1901 struct section_add
*pa
;
1906 s
= strchr (optarg
, '=');
1910 "%s: bad format for --add-section NAME=FILENAME\n",
1915 if (stat (s
+ 1, &st
) < 0)
1917 fprintf (stderr
, "%s: ", program_name
);
1922 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1925 name
= (char *) xmalloc (len
+ 1);
1926 strncpy (name
, optarg
, len
);
1930 pa
->filename
= s
+ 1;
1932 pa
->size
= st
.st_size
;
1934 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1935 f
= fopen (pa
->filename
, FOPEN_RB
);
1938 fprintf (stderr
, "%s: ", program_name
);
1939 perror (pa
->filename
);
1942 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1945 fprintf (stderr
, "%s: %s: fread failed\n",
1946 program_name
, pa
->filename
);
1951 pa
->next
= add_sections
;
1955 case OPTION_ADJUST_START
:
1956 adjust_start
= parse_vma (optarg
, "--adjust-start");
1958 case OPTION_ADJUST_SECTION_VMA
:
1964 s
= strchr (optarg
, '=');
1967 s
= strchr (optarg
, '+');
1970 s
= strchr (optarg
, '-');
1974 "%s: bad format for --adjust-section-vma\n",
1982 name
= (char *) xmalloc (len
+ 1);
1983 strncpy (name
, optarg
, len
);
1986 p
= find_section_list (name
, true);
1988 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1991 p
->adjust
= set_vma
;
1994 p
->adjust
= adjust_vma
;
2000 case OPTION_ADJUST_VMA
:
2001 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
2002 adjust_start
= adjust_section_vma
;
2004 case OPTION_ADJUST_WARNINGS
:
2007 case OPTION_CHANGE_LEADING_CHAR
:
2008 change_leading_char
= true;
2010 case OPTION_DEBUGGING
:
2011 convert_debugging
= true;
2013 case OPTION_GAP_FILL
:
2015 bfd_vma gap_fill_vma
;
2017 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2018 gap_fill
= (bfd_byte
) gap_fill_vma
;
2019 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2021 fprintf (stderr
, "%s: warning: truncating gap-fill from 0x",
2023 fprintf_vma (stderr
, gap_fill_vma
);
2024 fprintf (stderr
, "to 0x%x\n", (unsigned int) gap_fill
);
2026 gap_fill_set
= true;
2029 case OPTION_NO_ADJUST_WARNINGS
:
2030 adjust_warn
= false;
2033 pad_to
= parse_vma (optarg
, "--pad-to");
2036 case OPTION_REMOVE_LEADING_CHAR
:
2037 remove_leading_char
= true;
2039 case OPTION_SET_SECTION_FLAGS
:
2045 s
= strchr (optarg
, '=');
2048 fprintf (stderr
, "%s: bad format for --set-section-flags\n",
2054 name
= (char *) xmalloc (len
+ 1);
2055 strncpy (name
, optarg
, len
);
2058 p
= find_section_list (name
, true);
2060 p
->set_flags
= true;
2061 p
->flags
= parse_flags (s
+ 1);
2064 case OPTION_SET_START
:
2065 set_start
= parse_vma (optarg
, "--set-start");
2066 set_start_set
= true;
2069 break; /* we've been given a long option */
2071 copy_usage (stdout
, 0);
2073 copy_usage (stderr
, 1);
2078 print_version ("objcopy");
2080 if (copy_byte
>= interleave
)
2082 fprintf (stderr
, "%s: byte number must be less than interleave\n",
2087 if (optind
== argc
|| optind
+ 2 < argc
)
2088 copy_usage (stderr
, 1);
2090 input_filename
= argv
[optind
];
2091 if (optind
+ 1 < argc
)
2092 output_filename
= argv
[optind
+ 1];
2094 /* Default is to strip no symbols. */
2095 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
2096 strip_symbols
= strip_none
;
2098 if (output_target
== (char *) NULL
)
2099 output_target
= input_target
;
2103 if (stat (input_filename
, &statbuf
) < 0)
2105 fprintf (stderr
, "%s: ", input_filename
);
2106 perror ("cannot stat");
2111 /* If there is no destination file then create a temp and rename
2112 the result into the input. */
2114 if (output_filename
== (char *) NULL
)
2116 char *tmpname
= make_tempname (input_filename
);
2118 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2122 set_times (tmpname
, &statbuf
);
2123 smart_rename (tmpname
, input_filename
);
2130 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2131 if (status
== 0 && preserve_dates
)
2132 set_times (output_filename
, &statbuf
);
2137 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
2139 if (! p
->used
&& p
->adjust
!= ignore_vma
)
2141 fprintf (stderr
, "%s: warning: --adjust-section-vma %s%c0x",
2142 program_name
, p
->name
,
2143 p
->adjust
== set_vma
? '=' : '+');
2144 fprintf_vma (stderr
, p
->val
);
2145 fprintf (stderr
, " never used\n");
2158 program_name
= argv
[0];
2159 xmalloc_set_program_name (program_name
);
2161 START_PROGRESS (program_name
, 0);
2163 strip_symbols
= strip_undef
;
2164 discard_locals
= locals_undef
;
2167 set_default_bfd_target ();
2171 int i
= strlen (program_name
);
2172 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2176 strip_main (argc
, argv
);
2178 copy_main (argc
, argv
);
2180 END_PROGRESS (program_name
);