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_vma
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 int status
= 0; /* Exit status. */
90 strip_none
, /* don't strip */
91 strip_debug
, /* strip all debugger symbols */
92 strip_unneeded
, /* strip unnecessary symbols */
93 strip_all
/* strip all symbols */
96 /* Which symbols to remove. */
97 static enum strip_action strip_symbols
;
102 locals_start_L
, /* discard locals starting with L */
103 locals_all
/* discard all locals */
106 /* Which local symbols to remove. Overrides strip_all. */
107 static enum locals_action discard_locals
;
109 /* Structure used to hold lists of sections and actions to take. */
113 /* Next section to adjust. */
114 struct section_list
*next
;
117 /* Whether this entry was used. */
119 /* Whether to remove this section. */
121 /* Whether to adjust or set VMA. */
122 enum { ignore_vma
, adjust_vma
, set_vma
} adjust
;
123 /* Amount to adjust by or set to. */
125 /* Whether to set the section flags. */
127 /* What to set the section flags to. */
131 static struct section_list
*adjust_sections
;
132 static boolean sections_removed
;
134 /* Adjustments to the start address. */
135 static bfd_vma adjust_start
= 0;
136 static boolean set_start_set
= false;
137 static bfd_vma set_start
;
139 /* Adjustments to section VMA's. */
140 static bfd_vma adjust_section_vma
= 0;
142 /* Filling gaps between sections. */
143 static boolean gap_fill_set
= false;
144 static bfd_byte gap_fill
= 0;
146 /* Pad to a given address. */
147 static boolean pad_to_set
= false;
148 static bfd_vma pad_to
;
150 /* List of sections to add. */
154 /* Next section to add. */
155 struct section_add
*next
;
156 /* Name of section to add. */
158 /* Name of file holding section contents. */
159 const char *filename
;
162 /* Contents of file. */
164 /* BFD section, after it has been added. */
168 static struct section_add
*add_sections
;
170 /* Whether to convert debugging information. */
172 static boolean convert_debugging
= false;
174 /* Whether to change the leading character in symbol names. */
176 static boolean change_leading_char
= false;
178 /* Whether to remove the leading character from global symbol names. */
180 static boolean remove_leading_char
= false;
182 /* List of symbols to strip, keep, localize, and weaken. */
184 static struct symlist
*strip_specific_list
= NULL
;
185 static struct symlist
*keep_specific_list
= NULL
;
186 static struct symlist
*localize_specific_list
= NULL
;
187 static struct symlist
*weaken_specific_list
= NULL
;
189 /* If this is true, we weaken global symbols (set BSF_WEAK). */
191 static boolean weaken
= false;
193 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
195 #define OPTION_ADD_SECTION 150
196 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
197 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
198 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
199 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
200 #define OPTION_CHANGE_LEADING_CHAR (OPTION_ADJUST_WARNINGS + 1)
201 #define OPTION_DEBUGGING (OPTION_CHANGE_LEADING_CHAR + 1)
202 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
203 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
204 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
205 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
206 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
207 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
208 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
209 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
211 /* Options to handle if running as "strip". */
213 static struct option strip_options
[] =
215 {"discard-all", no_argument
, 0, 'x'},
216 {"discard-locals", no_argument
, 0, 'X'},
217 {"format", required_argument
, 0, 'F'}, /* Obsolete */
218 {"help", no_argument
, 0, 'h'},
219 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
220 {"input-target", required_argument
, 0, 'I'},
221 {"keep-symbol", required_argument
, 0, 'K'},
222 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
223 {"output-target", required_argument
, 0, 'O'},
224 {"preserve-dates", no_argument
, 0, 'p'},
225 {"remove-section", required_argument
, 0, 'R'},
226 {"strip-all", no_argument
, 0, 's'},
227 {"strip-debug", no_argument
, 0, 'S'},
228 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
229 {"strip-symbol", required_argument
, 0, 'N'},
230 {"target", required_argument
, 0, 'F'},
231 {"verbose", no_argument
, 0, 'v'},
232 {"version", no_argument
, 0, 'V'},
233 {0, no_argument
, 0, 0}
236 /* Options to handle if running as "objcopy". */
238 static struct option copy_options
[] =
240 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
241 {"adjust-start", required_argument
, 0, OPTION_ADJUST_START
},
242 {"adjust-vma", required_argument
, 0, OPTION_ADJUST_VMA
},
243 {"adjust-section-vma", required_argument
, 0, OPTION_ADJUST_SECTION_VMA
},
244 {"adjust-warnings", no_argument
, 0, OPTION_ADJUST_WARNINGS
},
245 {"byte", required_argument
, 0, 'b'},
246 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
247 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
248 {"discard-all", no_argument
, 0, 'x'},
249 {"discard-locals", no_argument
, 0, 'X'},
250 {"format", required_argument
, 0, 'F'}, /* Obsolete */
251 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
252 {"help", no_argument
, 0, 'h'},
253 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
254 {"input-target", required_argument
, 0, 'I'},
255 {"interleave", required_argument
, 0, 'i'},
256 {"keep-symbol", required_argument
, 0, 'K'},
257 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_ADJUST_WARNINGS
},
258 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
259 {"output-target", required_argument
, 0, 'O'},
260 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
261 {"preserve-dates", no_argument
, 0, 'p'},
262 {"localize-symbol", required_argument
, 0, 'L'},
263 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
264 {"remove-section", required_argument
, 0, 'R'},
265 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
266 {"set-start", required_argument
, 0, OPTION_SET_START
},
267 {"strip-all", no_argument
, 0, 'S'},
268 {"strip-debug", no_argument
, 0, 'g'},
269 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
270 {"strip-symbol", required_argument
, 0, 'N'},
271 {"target", required_argument
, 0, 'F'},
272 {"verbose", no_argument
, 0, 'v'},
273 {"version", no_argument
, 0, 'V'},
274 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
275 {"weaken-symbol", required_argument
, 0, 'W'},
276 {0, no_argument
, 0, 0}
280 extern char *program_name
;
282 /* This flag distinguishes between strip and objcopy:
283 1 means this is 'strip'; 0 means this is 'objcopy'.
284 -1 means if we should use argv[0] to decide. */
289 copy_usage (stream
, exit_status
)
294 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
295 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
296 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
297 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
298 [--discard-locals] [--debugging] [--remove-section=section]\n",
301 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
302 [--set-start=val] [--adjust-start=incr]\n\
303 [--adjust-vma=incr] [--adjust-section-vma=section{=,+,-}val]\n\
304 [--adjust-warnings] [--no-adjust-warnings]\n\
305 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
306 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
307 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
308 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
309 [--verbose] [--version] [--help] in-file [out-file]\n");
310 list_supported_targets (program_name
, stream
);
311 if (exit_status
== 0)
312 fprintf (stream
, "Report bugs to bug-gnu-utils@gnu.org\n");
317 strip_usage (stream
, exit_status
)
322 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
323 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
324 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
325 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
326 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
327 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n",
329 list_supported_targets (program_name
, stream
);
330 if (exit_status
== 0)
331 fprintf (stream
, "Report bugs to bug-gnu-utils@gnu.org\n");
335 /* Parse section flags into a flagword, with a fatal error if the
336 string can't be parsed. */
350 snext
= strchr (s
, ',');
360 #define PARSE_FLAG(fname,fval) \
361 else if (strncasecmp (fname, s, len) == 0) ret |= fval
362 PARSE_FLAG ("alloc", SEC_ALLOC
);
363 PARSE_FLAG ("load", SEC_LOAD
);
364 PARSE_FLAG ("readonly", SEC_READONLY
);
365 PARSE_FLAG ("code", SEC_CODE
);
366 PARSE_FLAG ("data", SEC_DATA
);
367 PARSE_FLAG ("rom", SEC_ROM
);
368 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
374 copy
= xmalloc (len
+ 1);
375 strncpy (copy
, s
, len
);
377 fprintf (stderr
, "%s: unrecognized section flag `%s'\n",
380 "%s: supported flags: alloc, load, readonly, code, data, rom, contents\n",
392 /* Find and optionally add an entry in the adjust_sections list. */
394 static struct section_list
*
395 find_section_list (name
, add
)
399 register struct section_list
*p
;
401 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
402 if (strcmp (p
->name
, name
) == 0)
408 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
412 p
->adjust
= ignore_vma
;
414 p
->set_flags
= false;
417 p
->next
= adjust_sections
;
423 /* Add a symbol to strip_specific_list. */
426 add_specific_symbol (name
, list
)
428 struct symlist
**list
;
430 struct symlist
*tmp_list
;
432 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
433 tmp_list
->name
= name
;
434 tmp_list
->next
= *list
;
438 /* See whether a symbol should be stripped or kept based on
439 strip_specific_list and keep_symbols. */
442 is_specified_symbol (name
, list
)
444 struct symlist
*list
;
446 struct symlist
*tmp_list
;
448 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
450 if (strcmp (name
, tmp_list
->name
) == 0)
456 /* See if a section is being removed. */
459 is_strip_section (abfd
, sec
)
463 struct section_list
*p
;
465 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
466 && (strip_symbols
== strip_debug
467 || strip_symbols
== strip_unneeded
468 || strip_symbols
== strip_all
469 || discard_locals
== locals_all
470 || convert_debugging
))
473 if (! sections_removed
)
475 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
476 return p
!= NULL
&& p
->remove
? true : false;
479 /* Choose which symbol entries to copy; put the result in OSYMS.
480 We don't copy in place, because that confuses the relocs.
481 Return the number of symbols to print. */
484 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
487 asymbol
**osyms
, **isyms
;
490 register asymbol
**from
= isyms
, **to
= osyms
;
491 long src_count
= 0, dst_count
= 0;
493 for (; src_count
< symcount
; src_count
++)
495 asymbol
*sym
= from
[src_count
];
496 flagword flags
= sym
->flags
;
497 const char *name
= bfd_asymbol_name (sym
);
500 if (change_leading_char
501 && (bfd_get_symbol_leading_char (abfd
)
502 != bfd_get_symbol_leading_char (obfd
))
503 && (bfd_get_symbol_leading_char (abfd
) == '\0'
504 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
506 if (bfd_get_symbol_leading_char (obfd
) == '\0')
507 name
= bfd_asymbol_name (sym
) = name
+ 1;
512 n
= xmalloc (strlen (name
) + 2);
513 n
[0] = bfd_get_symbol_leading_char (obfd
);
514 if (bfd_get_symbol_leading_char (abfd
) == '\0')
515 strcpy (n
+ 1, name
);
517 strcpy (n
+ 1, name
+ 1);
518 name
= bfd_asymbol_name (sym
) = n
;
522 if (remove_leading_char
523 && ((flags
& BSF_GLOBAL
) != 0
524 || (flags
& BSF_WEAK
) != 0
525 || bfd_is_und_section (bfd_get_section (sym
))
526 || bfd_is_com_section (bfd_get_section (sym
)))
527 && name
[0] == bfd_get_symbol_leading_char (abfd
))
528 name
= bfd_asymbol_name (sym
) = name
+ 1;
530 if ((flags
& BSF_KEEP
) != 0) /* Used in relocation. */
532 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
533 || (flags
& BSF_WEAK
) != 0
534 || bfd_is_und_section (bfd_get_section (sym
))
535 || bfd_is_com_section (bfd_get_section (sym
)))
536 keep
= strip_symbols
!= strip_unneeded
;
537 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
538 keep
= (strip_symbols
!= strip_debug
539 && strip_symbols
!= strip_unneeded
540 && ! convert_debugging
);
541 else /* Local symbol. */
542 keep
= (strip_symbols
!= strip_unneeded
543 && (discard_locals
!= locals_all
544 && (discard_locals
!= locals_start_L
545 || ! bfd_is_local_label (abfd
, sym
))));
547 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
549 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
551 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
554 if (keep
&& (flags
& BSF_GLOBAL
) != 0
555 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
557 sym
->flags
&=~ BSF_GLOBAL
;
558 sym
->flags
|= BSF_WEAK
;
560 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
561 && is_specified_symbol (name
, localize_specific_list
))
563 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
564 sym
->flags
|= BSF_LOCAL
;
568 to
[dst_count
++] = sym
;
571 to
[dst_count
] = NULL
;
576 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
580 filter_bytes (memhunk
, size
)
584 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
586 for (; from
< end
; from
+= interleave
)
591 /* Copy object file IBFD onto OBFD. */
594 copy_object (ibfd
, obfd
)
600 asection
**osections
= NULL
;
601 bfd_size_type
*gaps
= NULL
;
602 bfd_size_type max_gap
= 0;
604 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
606 nonfatal (bfd_get_filename (obfd
));
610 printf ("copy from %s(%s) to %s(%s)\n",
611 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
612 bfd_get_filename(obfd
), bfd_get_target(obfd
));
617 start
= bfd_get_start_address (ibfd
);
618 start
+= adjust_start
;
620 if (!bfd_set_start_address (obfd
, start
)
621 || !bfd_set_file_flags (obfd
,
622 (bfd_get_file_flags (ibfd
)
623 & bfd_applicable_file_flags (obfd
))))
625 nonfatal (bfd_get_filename (ibfd
));
628 /* Copy architecture of input file to output file */
629 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
630 bfd_get_mach (ibfd
)))
633 "Warning: Output file cannot represent architecture %s\n",
634 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
635 bfd_get_mach (ibfd
)));
637 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
639 nonfatal (bfd_get_filename(ibfd
));
644 if (osympp
!= isympp
)
647 /* bfd mandates that all output sections be created and sizes set before
648 any output is done. Thus, we traverse all sections multiple times. */
649 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
651 if (add_sections
!= NULL
)
653 struct section_add
*padd
;
654 struct section_list
*pset
;
656 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
658 padd
->section
= bfd_make_section (obfd
, padd
->name
);
659 if (padd
->section
== NULL
)
661 fprintf (stderr
, "%s: can't create section `%s': %s\n",
662 program_name
, padd
->name
,
663 bfd_errmsg (bfd_get_error ()));
671 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
672 nonfatal (bfd_get_filename (obfd
));
674 pset
= find_section_list (padd
->name
, false);
678 if (pset
!= NULL
&& pset
->set_flags
)
679 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
681 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
682 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
683 nonfatal (bfd_get_filename (obfd
));
686 && (pset
->adjust
== adjust_vma
687 || pset
->adjust
== set_vma
))
689 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->val
))
690 nonfatal (bfd_get_filename (obfd
));
696 if (gap_fill_set
|| pad_to_set
)
701 /* We must fill in gaps between the sections and/or we must pad
702 the last section to a specified address. We do this by
703 grabbing a list of the sections, sorting them by VMA, and
704 increasing the section sizes as required to fill the gaps.
705 We write out the gap contents below. */
707 c
= bfd_count_sections (obfd
);
708 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
710 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
712 qsort (osections
, c
, sizeof (asection
*), compare_section_vma
);
714 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
715 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
719 for (i
= 0; i
< c
- 1; i
++)
723 bfd_vma gap_start
, gap_stop
;
725 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
726 if ((flags
& SEC_HAS_CONTENTS
) == 0
727 || (flags
& SEC_LOAD
) == 0)
730 size
= bfd_section_size (obfd
, osections
[i
]);
731 gap_start
= bfd_section_vma (obfd
, osections
[i
]) + size
;
732 gap_stop
= bfd_section_vma (obfd
, osections
[i
+ 1]);
733 if (gap_start
< gap_stop
)
735 if (! bfd_set_section_size (obfd
, osections
[i
],
736 size
+ (gap_stop
- gap_start
)))
738 fprintf (stderr
, "%s: Can't fill gap after %s: %s\n",
740 bfd_get_section_name (obfd
, osections
[i
]),
741 bfd_errmsg (bfd_get_error()));
745 gaps
[i
] = gap_stop
- gap_start
;
746 if (max_gap
< gap_stop
- gap_start
)
747 max_gap
= gap_stop
- gap_start
;
757 vma
= bfd_section_vma (obfd
, osections
[c
- 1]);
758 size
= bfd_section_size (obfd
, osections
[c
- 1]);
759 if (vma
+ size
< pad_to
)
761 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
764 fprintf (stderr
, "%s: Can't add padding to %s: %s\n",
766 bfd_get_section_name (obfd
, osections
[c
- 1]),
767 bfd_errmsg (bfd_get_error ()));
772 gaps
[c
- 1] = pad_to
- (vma
+ size
);
773 if (max_gap
< pad_to
- (vma
+ size
))
774 max_gap
= pad_to
- (vma
+ size
);
780 /* Symbol filtering must happen after the output sections have
781 been created, but before their contents are set. */
782 if (strip_symbols
== strip_all
)
784 osympp
= isympp
= NULL
;
792 symsize
= bfd_get_symtab_upper_bound (ibfd
);
795 nonfatal (bfd_get_filename (ibfd
));
798 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
799 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
802 nonfatal (bfd_get_filename (ibfd
));
805 if (convert_debugging
)
806 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
808 if (strip_symbols
== strip_debug
809 || strip_symbols
== strip_unneeded
810 || discard_locals
!= locals_undef
811 || strip_specific_list
!= NULL
812 || keep_specific_list
!= NULL
813 || localize_specific_list
!= NULL
814 || weaken_specific_list
!= NULL
817 || change_leading_char
818 || remove_leading_char
821 /* Mark symbols used in output relocations so that they
822 are kept, even if they are local labels or static symbols.
824 Note we iterate over the input sections examining their
825 relocations since the relocations for the output sections
826 haven't been set yet. mark_symbols_used_in_relocations will
827 ignore input sections which have no corresponding output
829 bfd_map_over_sections (ibfd
,
830 mark_symbols_used_in_relocations
,
832 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
833 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
836 if (convert_debugging
&& dhandle
!= NULL
)
838 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
846 bfd_set_symtab (obfd
, osympp
, symcount
);
848 /* This has to happen after the symbol table has been set. */
849 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
851 if (add_sections
!= NULL
)
853 struct section_add
*padd
;
855 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
857 if (! bfd_set_section_contents (obfd
, padd
->section
,
858 (PTR
) padd
->contents
,
860 (bfd_size_type
) padd
->size
))
861 nonfatal (bfd_get_filename (obfd
));
865 if (gap_fill_set
|| pad_to_set
)
870 /* Fill in the gaps. */
874 buf
= (bfd_byte
*) xmalloc (max_gap
);
875 memset (buf
, gap_fill
, (size_t) max_gap
);
877 c
= bfd_count_sections (obfd
);
878 for (i
= 0; i
< c
; i
++)
886 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
895 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
898 nonfatal (bfd_get_filename (obfd
));
907 /* Allow the BFD backend to copy any private data it understands
908 from the input BFD to the output BFD. This is done last to
909 permit the routine to look at the filtered symbol table, which is
910 important for the ECOFF code at least. */
911 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
913 fprintf (stderr
, "%s: %s: error copying private BFD data: %s\n",
914 program_name
, bfd_get_filename (obfd
),
915 bfd_errmsg (bfd_get_error ()));
921 /* Read each archive element in turn from IBFD, copy the
922 contents to temp file, and keep the temp file handle. */
925 copy_archive (ibfd
, obfd
, output_target
)
928 const char *output_target
;
932 struct name_list
*next
;
936 bfd
**ptr
= &obfd
->archive_head
;
938 char *dir
= make_tempname (bfd_get_filename (obfd
));
940 /* Make a temp directory to hold the contents. */
941 #if defined (_WIN32) && !defined (__CYGWIN32__)
942 if (mkdir (dir
) != 0)
944 if (mkdir (dir
, 0700) != 0)
947 fatal ("cannot mkdir %s for archive copying (error: %s)",
948 dir
, strerror (errno
));
950 obfd
->has_armap
= ibfd
->has_armap
;
954 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
955 while (this_element
!= (bfd
*) NULL
)
957 /* Create an output file for this member. */
958 char *output_name
= concat (dir
, "/", bfd_get_filename(this_element
),
960 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
963 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
964 l
->name
= output_name
;
968 if (output_bfd
== (bfd
*) NULL
)
970 nonfatal (output_name
);
972 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
974 nonfatal (bfd_get_filename (obfd
));
977 if (bfd_check_format (this_element
, bfd_object
) == true)
979 copy_object (this_element
, output_bfd
);
982 bfd_close (output_bfd
);
984 /* Open the newly output file and attach to our list. */
985 output_bfd
= bfd_openr (output_name
, output_target
);
987 l
->obfd
= output_bfd
;
990 ptr
= &output_bfd
->next
;
992 last_element
= this_element
;
994 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
996 bfd_close (last_element
);
1000 if (!bfd_close (obfd
))
1002 nonfatal (bfd_get_filename (obfd
));
1005 if (!bfd_close (ibfd
))
1007 nonfatal (bfd_get_filename (ibfd
));
1010 /* Delete all the files that we opened. */
1011 for (l
= list
; l
!= NULL
; l
= l
->next
)
1013 bfd_close (l
->obfd
);
1019 /* The top-level control. */
1022 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1023 const char *input_filename
;
1024 const char *output_filename
;
1025 const char *input_target
;
1026 const char *output_target
;
1031 /* To allow us to do "strip *" without dying on the first
1032 non-object file, failures are nonfatal. */
1034 ibfd
= bfd_openr (input_filename
, input_target
);
1037 nonfatal (input_filename
);
1040 if (bfd_check_format (ibfd
, bfd_archive
))
1044 /* bfd_get_target does not return the correct value until
1045 bfd_check_format succeeds. */
1046 if (output_target
== NULL
)
1047 output_target
= bfd_get_target (ibfd
);
1049 obfd
= bfd_openw (output_filename
, output_target
);
1052 nonfatal (output_filename
);
1054 copy_archive (ibfd
, obfd
, output_target
);
1056 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1060 /* bfd_get_target does not return the correct value until
1061 bfd_check_format succeeds. */
1062 if (output_target
== NULL
)
1063 output_target
= bfd_get_target (ibfd
);
1065 obfd
= bfd_openw (output_filename
, output_target
);
1068 nonfatal (output_filename
);
1071 copy_object (ibfd
, obfd
);
1073 if (!bfd_close (obfd
))
1075 nonfatal (output_filename
);
1078 if (!bfd_close (ibfd
))
1080 nonfatal (input_filename
);
1085 bfd_nonfatal (input_filename
);
1086 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1088 list_matching_formats (matching
);
1095 /* Create a section in OBFD with the same name and attributes
1096 as ISECTION in IBFD. */
1099 setup_section (ibfd
, isection
, obfdarg
)
1104 bfd
*obfd
= (bfd
*) obfdarg
;
1105 struct section_list
*p
;
1112 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1113 && (strip_symbols
== strip_debug
1114 || strip_symbols
== strip_unneeded
1115 || strip_symbols
== strip_all
1116 || discard_locals
== locals_all
1117 || convert_debugging
))
1120 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1124 if (p
!= NULL
&& p
->remove
)
1127 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1128 if (osection
== NULL
)
1134 if (!bfd_set_section_size (obfd
,
1136 bfd_section_size (ibfd
, isection
)))
1142 vma
= bfd_section_vma (ibfd
, isection
);
1143 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1145 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1148 vma
+= adjust_section_vma
;
1149 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1155 lma
= isection
->lma
;
1156 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1158 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1161 lma
+= adjust_section_vma
;
1162 osection
->lma
= lma
;
1164 if (bfd_set_section_alignment (obfd
,
1166 bfd_section_alignment (ibfd
, isection
))
1173 flags
= bfd_get_section_flags (ibfd
, isection
);
1174 if (p
!= NULL
&& p
->set_flags
)
1175 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1176 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1182 /* This used to be mangle_section; we do here to avoid using
1183 bfd_get_section_by_name since some formats allow multiple
1184 sections with the same name. */
1185 isection
->output_section
= osection
;
1186 isection
->output_offset
= 0;
1188 /* Allow the BFD backend to copy any private data it understands
1189 from the input section to the output section. */
1190 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1192 err
= "private data";
1200 fprintf (stderr
, "%s: %s: section `%s': error in %s: %s\n",
1202 bfd_get_filename (ibfd
), bfd_section_name (ibfd
, isection
),
1203 err
, bfd_errmsg (bfd_get_error ()));
1207 /* Copy the data of input section ISECTION of IBFD
1208 to an output section with the same name in OBFD.
1209 If stripping then don't copy any relocation info. */
1212 copy_section (ibfd
, isection
, obfdarg
)
1217 bfd
*obfd
= (bfd
*) obfdarg
;
1218 struct section_list
*p
;
1224 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1225 && (strip_symbols
== strip_debug
1226 || strip_symbols
== strip_unneeded
1227 || strip_symbols
== strip_all
1228 || discard_locals
== locals_all
1229 || convert_debugging
))
1234 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1236 if (p
!= NULL
&& p
->remove
)
1239 osection
= isection
->output_section
;
1240 size
= bfd_get_section_size_before_reloc (isection
);
1242 if (size
== 0 || osection
== 0)
1245 if (strip_symbols
== strip_all
)
1246 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1251 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1254 nonfatal (bfd_get_filename (ibfd
));
1257 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1260 relpp
= (arelent
**) xmalloc (relsize
);
1261 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1264 nonfatal (bfd_get_filename (ibfd
));
1266 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1270 isection
->_cooked_size
= isection
->_raw_size
;
1271 isection
->reloc_done
= true;
1273 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1275 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1277 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1280 nonfatal (bfd_get_filename (ibfd
));
1285 filter_bytes (memhunk
, &size
);
1286 /* The section has gotten smaller. */
1287 if (!bfd_set_section_size (obfd
, osection
, size
))
1288 nonfatal (bfd_get_filename (obfd
));
1291 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1294 nonfatal (bfd_get_filename (obfd
));
1298 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1300 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1302 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1303 flag--they can just remove the section entirely and add it
1304 back again. However, we do permit them to turn on the
1305 SEC_HAS_CONTENTS flag, and take it to mean that the section
1306 contents should be zeroed out. */
1308 memset (memhunk
, 0, size
);
1309 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1311 nonfatal (bfd_get_filename (obfd
));
1316 /* Get all the sections. This is used when --gap-fill or --pad-to is
1320 get_sections (obfd
, osection
, secppparg
)
1325 asection
***secppp
= (asection
***) secppparg
;
1327 **secppp
= osection
;
1331 /* Sort sections by VMA. This is called via qsort, and is used when
1332 --gap-fill or --pad-to is used. We force non loadable or empty
1333 sections to the front, where they are easier to ignore. */
1336 compare_section_vma (arg1
, arg2
)
1340 const asection
**sec1
= (const asection
**) arg1
;
1341 const asection
**sec2
= (const asection
**) arg2
;
1342 flagword flags1
, flags2
;
1344 /* Sort non loadable sections to the front. */
1345 flags1
= (*sec1
)->flags
;
1346 flags2
= (*sec2
)->flags
;
1347 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1348 || (flags1
& SEC_LOAD
) == 0)
1350 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1351 && (flags2
& SEC_LOAD
) != 0)
1356 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1357 || (flags2
& SEC_LOAD
) == 0)
1361 /* Sort sections by VMA. */
1362 if ((*sec1
)->vma
> (*sec2
)->vma
)
1364 else if ((*sec1
)->vma
< (*sec2
)->vma
)
1367 /* Sort sections with the same VMA by size. */
1368 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1370 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1376 /* Mark all the symbols which will be used in output relocations with
1377 the BSF_KEEP flag so that those symbols will not be stripped.
1379 Ignore relocations which will not appear in the output file. */
1382 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1387 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1392 /* Ignore an input section with no corresponding output section. */
1393 if (isection
->output_section
== NULL
)
1396 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1398 bfd_fatal (bfd_get_filename (ibfd
));
1403 relpp
= (arelent
**) xmalloc (relsize
);
1404 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1406 bfd_fatal (bfd_get_filename (ibfd
));
1408 /* Examine each symbol used in a relocation. If it's not one of the
1409 special bfd section symbols, then mark it with BSF_KEEP. */
1410 for (i
= 0; i
< relcount
; i
++)
1412 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1413 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1414 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1415 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1422 /* Write out debugging information. */
1425 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1431 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1432 return write_ieee_debugging_info (obfd
, dhandle
);
1434 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1435 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1437 bfd_byte
*syms
, *strings
;
1438 bfd_size_type symsize
, stringsize
;
1439 asection
*stabsec
, *stabstrsec
;
1441 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1446 stabsec
= bfd_make_section (obfd
, ".stab");
1447 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1449 || stabstrsec
== NULL
1450 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1451 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1452 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1453 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1454 || ! bfd_set_section_flags (obfd
, stabsec
,
1458 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1463 fprintf (stderr
, "%s: can't create debugging section: %s\n",
1464 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1468 /* We can get away with setting the section contents now because
1469 the next thing the caller is going to do is copy over the
1470 real sections. We may someday have to split the contents
1471 setting out of this function. */
1472 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1474 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1475 (file_ptr
) 0, stringsize
))
1477 fprintf (stderr
, "%s: can't set debugging section contents: %s\n",
1478 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1486 "%s: don't know how to write debugging information for %s\n",
1487 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1491 /* The number of bytes to copy at once. */
1492 #define COPY_BUF 8192
1494 /* Copy file FROM to file TO, performing no translations.
1495 Return 0 if ok, -1 if error. */
1498 simple_copy (from
, to
)
1502 int fromfd
, tofd
, nread
;
1506 fromfd
= open (from
, O_RDONLY
);
1509 tofd
= creat (to
, 0777);
1517 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1519 if (write (tofd
, buf
, nread
) != nread
)
1541 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1543 #define S_ISLNK(m) 0
1548 /* Rename FROM to TO, copying if TO is a link.
1549 Assumes that TO already exists, because FROM is a temp file.
1550 Return 0 if ok, -1 if error. */
1553 smart_rename (from
, to
)
1563 #if defined (_WIN32) && !defined (__CYGWIN32__)
1564 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1565 fail instead. Also, chown is not present. */
1567 if (stat (to
, &s
) == 0)
1570 ret
= rename (from
, to
);
1573 /* We have to clean up here. */
1575 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1581 /* Use rename only if TO is not a symbolic link and has
1582 only one hard link. */
1583 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1585 ret
= rename (from
, to
);
1588 /* Try to preserve the permission bits and ownership of TO.
1589 First get the mode right except for the setuid bit. Then
1590 change the ownership. Then fix the setuid bit. We do
1591 the chmod before the chown because if the chown succeeds,
1592 and we are a normal user, we won't be able to do the
1593 chmod afterward. We don't bother to fix the setuid bit
1594 first because that might introduce a fleeting security
1595 problem, and because the chown will clear the setuid bit
1596 anyhow. We only fix the setuid bit if the chown
1597 succeeds, because we don't want to introduce an
1598 unexpected setuid file owned by the user running objcopy. */
1599 chmod (to
, s
.st_mode
& 0777);
1600 if (chown (to
, s
.st_uid
, s
.st_gid
) >= 0)
1601 chmod (to
, s
.st_mode
& 07777);
1605 /* We have to clean up here. */
1607 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1615 ret
= simple_copy (from
, to
);
1619 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1621 perror ("simple_copy");
1625 #endif /* _WIN32 && !__CYGWIN32__ */
1630 /* Set the times of the file DESTINATION to be the same as those in
1634 set_times (destination
, statbuf
)
1635 const char *destination
;
1636 const struct stat
*statbuf
;
1641 #ifdef HAVE_GOOD_UTIME_H
1644 tb
.actime
= statbuf
->st_atime
;
1645 tb
.modtime
= statbuf
->st_mtime
;
1646 result
= utime (destination
, &tb
);
1647 #else /* ! HAVE_GOOD_UTIME_H */
1651 tb
[0] = statbuf
->st_atime
;
1652 tb
[1] = statbuf
->st_mtime
;
1653 result
= utime (destination
, tb
);
1654 #else /* HAVE_UTIMES */
1655 struct timeval tv
[2];
1657 tv
[0].tv_sec
= statbuf
->st_atime
;
1659 tv
[1].tv_sec
= statbuf
->st_mtime
;
1661 result
= utimes (destination
, tv
);
1662 #endif /* HAVE_UTIMES */
1663 #endif /* ! HAVE_GOOD_UTIME_H */
1668 fprintf (stderr
, "%s: ", destination
);
1669 perror ("can not set time");
1674 strip_main (argc
, argv
)
1678 char *input_target
= NULL
, *output_target
= NULL
;
1679 boolean show_version
= false;
1680 boolean preserve_dates
= false;
1682 struct section_list
*p
;
1683 char *output_file
= NULL
;
1685 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1686 strip_options
, (int *) 0)) != EOF
)
1691 input_target
= optarg
;
1694 output_target
= optarg
;
1697 input_target
= output_target
= optarg
;
1700 p
= find_section_list (optarg
, true);
1702 sections_removed
= true;
1705 strip_symbols
= strip_all
;
1709 strip_symbols
= strip_debug
;
1711 case OPTION_STRIP_UNNEEDED
:
1712 strip_symbols
= strip_unneeded
;
1715 add_specific_symbol (optarg
, &keep_specific_list
);
1718 add_specific_symbol (optarg
, &strip_specific_list
);
1721 output_file
= optarg
;
1724 preserve_dates
= true;
1727 discard_locals
= locals_all
;
1730 discard_locals
= locals_start_L
;
1736 show_version
= true;
1739 break; /* we've been given a long option */
1741 strip_usage (stdout
, 0);
1743 strip_usage (stderr
, 1);
1748 print_version ("strip");
1750 /* Default is to strip all symbols. */
1751 if (strip_symbols
== strip_undef
1752 && discard_locals
== locals_undef
1753 && strip_specific_list
== NULL
)
1754 strip_symbols
= strip_all
;
1756 if (output_target
== (char *) NULL
)
1757 output_target
= input_target
;
1761 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1762 strip_usage (stderr
, 1);
1764 for (; i
< argc
; i
++)
1766 int hold_status
= status
;
1767 struct stat statbuf
;
1772 if (stat (argv
[i
], &statbuf
) < 0)
1774 fprintf (stderr
, "%s: ", argv
[i
]);
1775 perror ("cannot stat");
1780 if (output_file
!= NULL
)
1781 tmpname
= output_file
;
1783 tmpname
= make_tempname (argv
[i
]);
1786 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1790 set_times (tmpname
, &statbuf
);
1791 if (output_file
== NULL
)
1792 smart_rename (tmpname
, argv
[i
]);
1793 status
= hold_status
;
1797 if (output_file
== NULL
)
1805 copy_main (argc
, argv
)
1809 char *input_filename
= NULL
, *output_filename
= NULL
;
1810 char *input_target
= NULL
, *output_target
= NULL
;
1811 boolean show_version
= false;
1812 boolean adjust_warn
= true;
1813 boolean preserve_dates
= false;
1815 struct section_list
*p
;
1816 struct stat statbuf
;
1818 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1819 copy_options
, (int *) 0)) != EOF
)
1824 copy_byte
= atoi(optarg
);
1827 fprintf (stderr
, "%s: byte number must be non-negative\n",
1833 interleave
= atoi(optarg
);
1836 fprintf(stderr
, "%s: interleave must be positive\n",
1842 case 's': /* "source" - 'I' is preferred */
1843 input_target
= optarg
;
1846 case 'd': /* "destination" - 'O' is preferred */
1847 output_target
= optarg
;
1850 input_target
= output_target
= optarg
;
1853 p
= find_section_list (optarg
, true);
1855 sections_removed
= true;
1858 strip_symbols
= strip_all
;
1861 strip_symbols
= strip_debug
;
1863 case OPTION_STRIP_UNNEEDED
:
1864 strip_symbols
= strip_unneeded
;
1867 add_specific_symbol (optarg
, &keep_specific_list
);
1870 add_specific_symbol (optarg
, &strip_specific_list
);
1873 add_specific_symbol (optarg
, &localize_specific_list
);
1876 add_specific_symbol (optarg
, &weaken_specific_list
);
1879 preserve_dates
= true;
1882 discard_locals
= locals_all
;
1885 discard_locals
= locals_start_L
;
1891 show_version
= true;
1896 case OPTION_ADD_SECTION
:
1900 struct section_add
*pa
;
1905 s
= strchr (optarg
, '=');
1909 "%s: bad format for --add-section NAME=FILENAME\n",
1914 if (stat (s
+ 1, &st
) < 0)
1916 fprintf (stderr
, "%s: ", program_name
);
1921 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1924 name
= (char *) xmalloc (len
+ 1);
1925 strncpy (name
, optarg
, len
);
1929 pa
->filename
= s
+ 1;
1931 pa
->size
= st
.st_size
;
1933 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1934 f
= fopen (pa
->filename
, FOPEN_RB
);
1937 fprintf (stderr
, "%s: ", program_name
);
1938 perror (pa
->filename
);
1941 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1944 fprintf (stderr
, "%s: %s: fread failed\n",
1945 program_name
, pa
->filename
);
1950 pa
->next
= add_sections
;
1954 case OPTION_ADJUST_START
:
1955 adjust_start
= parse_vma (optarg
, "--adjust-start");
1957 case OPTION_ADJUST_SECTION_VMA
:
1963 s
= strchr (optarg
, '=');
1966 s
= strchr (optarg
, '+');
1969 s
= strchr (optarg
, '-');
1973 "%s: bad format for --adjust-section-vma\n",
1981 name
= (char *) xmalloc (len
+ 1);
1982 strncpy (name
, optarg
, len
);
1985 p
= find_section_list (name
, true);
1987 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1990 p
->adjust
= set_vma
;
1993 p
->adjust
= adjust_vma
;
1999 case OPTION_ADJUST_VMA
:
2000 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
2001 adjust_start
= adjust_section_vma
;
2003 case OPTION_ADJUST_WARNINGS
:
2006 case OPTION_CHANGE_LEADING_CHAR
:
2007 change_leading_char
= true;
2009 case OPTION_DEBUGGING
:
2010 convert_debugging
= true;
2012 case OPTION_GAP_FILL
:
2014 bfd_vma gap_fill_vma
;
2016 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2017 gap_fill
= (bfd_byte
) gap_fill_vma
;
2018 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2020 fprintf (stderr
, "%s: warning: truncating gap-fill from 0x",
2022 fprintf_vma (stderr
, gap_fill_vma
);
2023 fprintf (stderr
, "to 0x%x\n", (unsigned int) gap_fill
);
2025 gap_fill_set
= true;
2028 case OPTION_NO_ADJUST_WARNINGS
:
2029 adjust_warn
= false;
2032 pad_to
= parse_vma (optarg
, "--pad-to");
2035 case OPTION_REMOVE_LEADING_CHAR
:
2036 remove_leading_char
= true;
2038 case OPTION_SET_SECTION_FLAGS
:
2044 s
= strchr (optarg
, '=');
2047 fprintf (stderr
, "%s: bad format for --set-section-flags\n",
2053 name
= (char *) xmalloc (len
+ 1);
2054 strncpy (name
, optarg
, len
);
2057 p
= find_section_list (name
, true);
2059 p
->set_flags
= true;
2060 p
->flags
= parse_flags (s
+ 1);
2063 case OPTION_SET_START
:
2064 set_start
= parse_vma (optarg
, "--set-start");
2065 set_start_set
= true;
2068 break; /* we've been given a long option */
2070 copy_usage (stdout
, 0);
2072 copy_usage (stderr
, 1);
2077 print_version ("objcopy");
2079 if (copy_byte
>= interleave
)
2081 fprintf (stderr
, "%s: byte number must be less than interleave\n",
2086 if (optind
== argc
|| optind
+ 2 < argc
)
2087 copy_usage (stderr
, 1);
2089 input_filename
= argv
[optind
];
2090 if (optind
+ 1 < argc
)
2091 output_filename
= argv
[optind
+ 1];
2093 /* Default is to strip no symbols. */
2094 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
2095 strip_symbols
= strip_none
;
2097 if (output_target
== (char *) NULL
)
2098 output_target
= input_target
;
2102 if (stat (input_filename
, &statbuf
) < 0)
2104 fprintf (stderr
, "%s: ", input_filename
);
2105 perror ("cannot stat");
2110 /* If there is no destination file then create a temp and rename
2111 the result into the input. */
2113 if (output_filename
== (char *) NULL
)
2115 char *tmpname
= make_tempname (input_filename
);
2117 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2121 set_times (tmpname
, &statbuf
);
2122 smart_rename (tmpname
, input_filename
);
2129 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2130 if (status
== 0 && preserve_dates
)
2131 set_times (output_filename
, &statbuf
);
2136 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
2138 if (! p
->used
&& p
->adjust
!= ignore_vma
)
2140 fprintf (stderr
, "%s: warning: --adjust-section-vma %s%c0x",
2141 program_name
, p
->name
,
2142 p
->adjust
== set_vma
? '=' : '+');
2143 fprintf_vma (stderr
, p
->val
);
2144 fprintf (stderr
, " never used\n");
2157 program_name
= argv
[0];
2158 xmalloc_set_program_name (program_name
);
2160 START_PROGRESS (program_name
, 0);
2162 strip_symbols
= strip_undef
;
2163 discard_locals
= locals_undef
;
2166 set_default_bfd_target ();
2170 int i
= strlen (program_name
);
2171 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2175 strip_main (argc
, argv
);
2177 copy_main (argc
, argv
);
2179 END_PROGRESS (program_name
);