1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "libiberty.h"
28 #ifdef HAVE_GOOD_UTIME_H
30 #else /* ! HAVE_GOOD_UTIME_H */
33 #endif /* HAVE_UTIMES */
34 #endif /* ! HAVE_GOOD_UTIME_H */
36 /* A list of symbols to explicitly strip out, or to keep. A linked
37 list is good enough for a small number from the command line, but
38 this will slow things down a lot if many symbols are being
47 static void copy_usage
PARAMS ((FILE *, int));
48 static void strip_usage
PARAMS ((FILE *, int));
49 static flagword parse_flags
PARAMS ((const char *));
50 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
51 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
52 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
53 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
54 static int compare_section_vma
PARAMS ((const PTR
, const PTR
));
55 static void add_specific_symbol
PARAMS ((const char *, struct symlist
**));
56 static boolean is_specified_symbol
PARAMS ((const char *, struct symlist
*));
57 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
58 static unsigned int filter_symbols
59 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
60 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
61 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
62 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
63 static void copy_object
PARAMS ((bfd
*, bfd
*));
64 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
66 PARAMS ((const char *, const char *, const char *, const char *));
67 static int simple_copy
PARAMS ((const char *, const char *));
68 static int smart_rename
PARAMS ((const char *, const char *));
69 static void set_times
PARAMS ((const char *, const struct stat
*));
70 static int strip_main
PARAMS ((int, char **));
71 static int copy_main
PARAMS ((int, char **));
73 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
75 static asymbol
**isympp
= NULL
; /* Input symbols */
76 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
78 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
79 static int copy_byte
= -1;
80 static int interleave
= 4;
82 static boolean verbose
; /* Print file and target names. */
83 static int status
= 0; /* Exit status. */
88 strip_none
, /* don't strip */
89 strip_debug
, /* strip all debugger symbols */
90 strip_unneeded
, /* strip unnecessary symbols */
91 strip_all
/* strip all symbols */
94 /* Which symbols to remove. */
95 static enum strip_action strip_symbols
;
100 locals_start_L
, /* discard locals starting with L */
101 locals_all
/* discard all locals */
104 /* Which local symbols to remove. Overrides strip_all. */
105 static enum locals_action discard_locals
;
107 /* Structure used to hold lists of sections and actions to take. */
111 /* Next section to adjust. */
112 struct section_list
*next
;
115 /* Whether this entry was used. */
117 /* Whether to remove this section. */
119 /* Whether to adjust or set VMA. */
120 enum { ignore_vma
, adjust_vma
, set_vma
} adjust
;
121 /* Amount to adjust by or set to. */
123 /* Whether to set the section flags. */
125 /* What to set the section flags to. */
129 static struct section_list
*adjust_sections
;
130 static boolean sections_removed
;
132 /* Adjustments to the start address. */
133 static bfd_vma adjust_start
= 0;
134 static boolean set_start_set
= false;
135 static bfd_vma set_start
;
137 /* Adjustments to section VMA's. */
138 static bfd_vma adjust_section_vma
= 0;
140 /* Filling gaps between sections. */
141 static boolean gap_fill_set
= false;
142 static bfd_byte gap_fill
= 0;
144 /* Pad to a given address. */
145 static boolean pad_to_set
= false;
146 static bfd_vma pad_to
;
148 /* List of sections to add. */
152 /* Next section to add. */
153 struct section_add
*next
;
154 /* Name of section to add. */
156 /* Name of file holding section contents. */
157 const char *filename
;
160 /* Contents of file. */
162 /* BFD section, after it has been added. */
166 static struct section_add
*add_sections
;
168 /* Whether to convert debugging information. */
170 static boolean convert_debugging
= false;
172 /* Whether to change the leading character in symbol names. */
174 static boolean change_leading_char
= false;
176 /* Whether to remove the leading character from global symbol names. */
178 static boolean remove_leading_char
= false;
180 /* List of symbols to strip, keep, localize, and weaken. */
182 static struct symlist
*strip_specific_list
= NULL
;
183 static struct symlist
*keep_specific_list
= NULL
;
184 static struct symlist
*localize_specific_list
= NULL
;
185 static struct symlist
*weaken_specific_list
= NULL
;
187 /* If this is true, we weaken global symbols (set BSF_WEAK). */
189 static boolean weaken
= false;
191 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
193 #define OPTION_ADD_SECTION 150
194 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
195 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
196 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
197 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
198 #define OPTION_CHANGE_LEADING_CHAR (OPTION_ADJUST_WARNINGS + 1)
199 #define OPTION_DEBUGGING (OPTION_CHANGE_LEADING_CHAR + 1)
200 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
201 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
202 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
203 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
204 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
205 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
206 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
207 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
209 /* Options to handle if running as "strip". */
211 static struct option strip_options
[] =
213 {"discard-all", no_argument
, 0, 'x'},
214 {"discard-locals", no_argument
, 0, 'X'},
215 {"format", required_argument
, 0, 'F'}, /* Obsolete */
216 {"help", no_argument
, 0, 'h'},
217 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
218 {"input-target", required_argument
, 0, 'I'},
219 {"keep-symbol", required_argument
, 0, 'K'},
220 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
221 {"output-target", required_argument
, 0, 'O'},
222 {"preserve-dates", no_argument
, 0, 'p'},
223 {"remove-section", required_argument
, 0, 'R'},
224 {"strip-all", no_argument
, 0, 's'},
225 {"strip-debug", no_argument
, 0, 'S'},
226 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
227 {"strip-symbol", required_argument
, 0, 'N'},
228 {"target", required_argument
, 0, 'F'},
229 {"verbose", no_argument
, 0, 'v'},
230 {"version", no_argument
, 0, 'V'},
231 {0, no_argument
, 0, 0}
234 /* Options to handle if running as "objcopy". */
236 static struct option copy_options
[] =
238 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
239 {"adjust-start", required_argument
, 0, OPTION_ADJUST_START
},
240 {"adjust-vma", required_argument
, 0, OPTION_ADJUST_VMA
},
241 {"adjust-section-vma", required_argument
, 0, OPTION_ADJUST_SECTION_VMA
},
242 {"adjust-warnings", no_argument
, 0, OPTION_ADJUST_WARNINGS
},
243 {"byte", required_argument
, 0, 'b'},
244 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
245 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
246 {"discard-all", no_argument
, 0, 'x'},
247 {"discard-locals", no_argument
, 0, 'X'},
248 {"format", required_argument
, 0, 'F'}, /* Obsolete */
249 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
250 {"help", no_argument
, 0, 'h'},
251 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
252 {"input-target", required_argument
, 0, 'I'},
253 {"interleave", required_argument
, 0, 'i'},
254 {"keep-symbol", required_argument
, 0, 'K'},
255 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_ADJUST_WARNINGS
},
256 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
257 {"output-target", required_argument
, 0, 'O'},
258 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
259 {"preserve-dates", no_argument
, 0, 'p'},
260 {"localize-symbol", required_argument
, 0, 'L'},
261 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
262 {"remove-section", required_argument
, 0, 'R'},
263 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
264 {"set-start", required_argument
, 0, OPTION_SET_START
},
265 {"strip-all", no_argument
, 0, 'S'},
266 {"strip-debug", no_argument
, 0, 'g'},
267 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
268 {"strip-symbol", required_argument
, 0, 'N'},
269 {"target", required_argument
, 0, 'F'},
270 {"verbose", no_argument
, 0, 'v'},
271 {"version", no_argument
, 0, 'V'},
272 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
273 {"weaken-symbol", required_argument
, 0, 'W'},
274 {0, no_argument
, 0, 0}
278 extern char *program_name
;
280 /* This flag distinguishes between strip and objcopy:
281 1 means this is 'strip'; 0 means this is 'objcopy'.
282 -1 means if we should use argv[0] to decide. */
287 copy_usage (stream
, exit_status
)
292 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
293 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
294 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
295 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
296 [--discard-locals] [--debugging] [--remove-section=section]\n",
299 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
300 [--set-start=val] [--adjust-start=incr]\n\
301 [--adjust-vma=incr] [--adjust-section-vma=section{=,+,-}val]\n\
302 [--adjust-warnings] [--no-adjust-warnings]\n\
303 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
304 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
305 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
306 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
307 [--verbose] [--version] [--help] in-file [out-file]\n");
308 list_supported_targets (program_name
, stream
);
309 if (exit_status
== 0)
310 fprintf (stream
, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
315 strip_usage (stream
, exit_status
)
320 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
321 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
322 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
323 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
324 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
325 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n",
327 list_supported_targets (program_name
, stream
);
328 if (exit_status
== 0)
329 fprintf (stream
, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
333 /* Parse section flags into a flagword, with a fatal error if the
334 string can't be parsed. */
348 snext
= strchr (s
, ',');
358 #define PARSE_FLAG(fname,fval) \
359 else if (strncasecmp (fname, s, len) == 0) ret |= fval
360 PARSE_FLAG ("alloc", SEC_ALLOC
);
361 PARSE_FLAG ("load", SEC_LOAD
);
362 PARSE_FLAG ("readonly", SEC_READONLY
);
363 PARSE_FLAG ("code", SEC_CODE
);
364 PARSE_FLAG ("data", SEC_DATA
);
365 PARSE_FLAG ("rom", SEC_ROM
);
366 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
372 copy
= xmalloc (len
+ 1);
373 strncpy (copy
, s
, len
);
375 fprintf (stderr
, "%s: unrecognized section flag `%s'\n",
378 "%s: supported flags: alloc, load, readonly, code, data, rom, contents\n",
390 /* Find and optionally add an entry in the adjust_sections list. */
392 static struct section_list
*
393 find_section_list (name
, add
)
397 register struct section_list
*p
;
399 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
400 if (strcmp (p
->name
, name
) == 0)
406 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
410 p
->adjust
= ignore_vma
;
412 p
->set_flags
= false;
415 p
->next
= adjust_sections
;
421 /* Add a symbol to strip_specific_list. */
424 add_specific_symbol (name
, list
)
426 struct symlist
**list
;
428 struct symlist
*tmp_list
;
430 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
431 tmp_list
->name
= name
;
432 tmp_list
->next
= *list
;
436 /* See whether a symbol should be stripped or kept based on
437 strip_specific_list and keep_symbols. */
440 is_specified_symbol (name
, list
)
442 struct symlist
*list
;
444 struct symlist
*tmp_list
;
446 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
448 if (strcmp (name
, tmp_list
->name
) == 0)
454 /* See if a section is being removed. */
457 is_strip_section (abfd
, sec
)
461 struct section_list
*p
;
463 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
464 && (strip_symbols
== strip_debug
465 || strip_symbols
== strip_unneeded
466 || strip_symbols
== strip_all
467 || discard_locals
== locals_all
468 || convert_debugging
))
471 if (! sections_removed
)
473 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
474 return p
!= NULL
&& p
->remove
? true : false;
477 /* Choose which symbol entries to copy; put the result in OSYMS.
478 We don't copy in place, because that confuses the relocs.
479 Return the number of symbols to print. */
482 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
485 asymbol
**osyms
, **isyms
;
488 register asymbol
**from
= isyms
, **to
= osyms
;
489 long src_count
= 0, dst_count
= 0;
491 for (; src_count
< symcount
; src_count
++)
493 asymbol
*sym
= from
[src_count
];
494 flagword flags
= sym
->flags
;
495 const char *name
= bfd_asymbol_name (sym
);
498 if (change_leading_char
499 && (bfd_get_symbol_leading_char (abfd
)
500 != bfd_get_symbol_leading_char (obfd
))
501 && (bfd_get_symbol_leading_char (abfd
) == '\0'
502 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
504 if (bfd_get_symbol_leading_char (obfd
) == '\0')
505 name
= bfd_asymbol_name (sym
) = name
+ 1;
510 n
= xmalloc (strlen (name
) + 2);
511 n
[0] = bfd_get_symbol_leading_char (obfd
);
512 if (bfd_get_symbol_leading_char (abfd
) == '\0')
513 strcpy (n
+ 1, name
);
515 strcpy (n
+ 1, name
+ 1);
516 name
= bfd_asymbol_name (sym
) = n
;
520 if (remove_leading_char
521 && ((flags
& BSF_GLOBAL
) != 0
522 || (flags
& BSF_WEAK
) != 0
523 || bfd_is_und_section (bfd_get_section (sym
))
524 || bfd_is_com_section (bfd_get_section (sym
)))
525 && name
[0] == bfd_get_symbol_leading_char (abfd
))
526 name
= bfd_asymbol_name (sym
) = name
+ 1;
528 if ((flags
& BSF_KEEP
) != 0) /* Used in relocation. */
530 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
531 || (flags
& BSF_WEAK
) != 0
532 || bfd_is_und_section (bfd_get_section (sym
))
533 || bfd_is_com_section (bfd_get_section (sym
)))
534 keep
= strip_symbols
!= strip_unneeded
;
535 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
536 keep
= (strip_symbols
!= strip_debug
537 && strip_symbols
!= strip_unneeded
538 && ! convert_debugging
);
539 else /* Local symbol. */
540 keep
= (strip_symbols
!= strip_unneeded
541 && (discard_locals
!= locals_all
542 && (discard_locals
!= locals_start_L
543 || ! bfd_is_local_label (abfd
, sym
))));
545 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
547 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
549 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
552 if (keep
&& (flags
& BSF_GLOBAL
) != 0
553 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
555 sym
->flags
&=~ BSF_GLOBAL
;
556 sym
->flags
|= BSF_WEAK
;
558 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
559 && is_specified_symbol (name
, localize_specific_list
))
561 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
562 sym
->flags
|= BSF_LOCAL
;
566 to
[dst_count
++] = sym
;
569 to
[dst_count
] = NULL
;
574 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
578 filter_bytes (memhunk
, size
)
582 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
584 for (; from
< end
; from
+= interleave
)
589 /* Copy object file IBFD onto OBFD. */
592 copy_object (ibfd
, obfd
)
598 asection
**osections
= NULL
;
599 bfd_size_type
*gaps
= NULL
;
600 bfd_size_type max_gap
= 0;
602 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
604 nonfatal (bfd_get_filename (obfd
));
608 printf ("copy from %s(%s) to %s(%s)\n",
609 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
610 bfd_get_filename(obfd
), bfd_get_target(obfd
));
615 start
= bfd_get_start_address (ibfd
);
616 start
+= adjust_start
;
618 if (!bfd_set_start_address (obfd
, start
)
619 || !bfd_set_file_flags (obfd
,
620 (bfd_get_file_flags (ibfd
)
621 & bfd_applicable_file_flags (obfd
))))
623 nonfatal (bfd_get_filename (ibfd
));
626 /* Copy architecture of input file to output file */
627 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
628 bfd_get_mach (ibfd
)))
631 "Warning: Output file cannot represent architecture %s\n",
632 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
633 bfd_get_mach (ibfd
)));
635 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
637 nonfatal (bfd_get_filename(ibfd
));
642 if (osympp
!= isympp
)
645 /* bfd mandates that all output sections be created and sizes set before
646 any output is done. Thus, we traverse all sections multiple times. */
647 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
649 if (add_sections
!= NULL
)
651 struct section_add
*padd
;
652 struct section_list
*pset
;
654 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
656 padd
->section
= bfd_make_section (obfd
, padd
->name
);
657 if (padd
->section
== NULL
)
659 fprintf (stderr
, "%s: can't create section `%s': %s\n",
660 program_name
, padd
->name
,
661 bfd_errmsg (bfd_get_error ()));
669 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
670 nonfatal (bfd_get_filename (obfd
));
672 pset
= find_section_list (padd
->name
, false);
676 if (pset
!= NULL
&& pset
->set_flags
)
677 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
679 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
680 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
681 nonfatal (bfd_get_filename (obfd
));
684 && (pset
->adjust
== adjust_vma
685 || pset
->adjust
== set_vma
))
687 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->val
))
688 nonfatal (bfd_get_filename (obfd
));
694 if (gap_fill_set
|| pad_to_set
)
699 /* We must fill in gaps between the sections and/or we must pad
700 the last section to a specified address. We do this by
701 grabbing a list of the sections, sorting them by VMA, and
702 increasing the section sizes as required to fill the gaps.
703 We write out the gap contents below. */
705 c
= bfd_count_sections (obfd
);
706 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
708 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
710 qsort (osections
, c
, sizeof (asection
*), compare_section_vma
);
712 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
713 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
717 for (i
= 0; i
< c
- 1; i
++)
721 bfd_vma gap_start
, gap_stop
;
723 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
724 if ((flags
& SEC_HAS_CONTENTS
) == 0
725 || (flags
& SEC_LOAD
) == 0)
728 size
= bfd_section_size (obfd
, osections
[i
]);
729 gap_start
= bfd_section_vma (obfd
, osections
[i
]) + size
;
730 gap_stop
= bfd_section_vma (obfd
, osections
[i
+ 1]);
731 if (gap_start
< gap_stop
)
733 if (! bfd_set_section_size (obfd
, osections
[i
],
734 size
+ (gap_stop
- gap_start
)))
736 fprintf (stderr
, "%s: Can't fill gap after %s: %s\n",
738 bfd_get_section_name (obfd
, osections
[i
]),
739 bfd_errmsg (bfd_get_error()));
743 gaps
[i
] = gap_stop
- gap_start
;
744 if (max_gap
< gap_stop
- gap_start
)
745 max_gap
= gap_stop
- gap_start
;
755 vma
= bfd_section_vma (obfd
, osections
[c
- 1]);
756 size
= bfd_section_size (obfd
, osections
[c
- 1]);
757 if (vma
+ size
< pad_to
)
759 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
762 fprintf (stderr
, "%s: Can't add padding to %s: %s\n",
764 bfd_get_section_name (obfd
, osections
[c
- 1]),
765 bfd_errmsg (bfd_get_error ()));
770 gaps
[c
- 1] = pad_to
- (vma
+ size
);
771 if (max_gap
< pad_to
- (vma
+ size
))
772 max_gap
= pad_to
- (vma
+ size
);
778 /* Symbol filtering must happen after the output sections have
779 been created, but before their contents are set. */
780 if (strip_symbols
== strip_all
)
782 osympp
= isympp
= NULL
;
790 symsize
= bfd_get_symtab_upper_bound (ibfd
);
793 nonfatal (bfd_get_filename (ibfd
));
796 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
797 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
800 nonfatal (bfd_get_filename (ibfd
));
803 if (convert_debugging
)
804 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
806 if (strip_symbols
== strip_debug
807 || strip_symbols
== strip_unneeded
808 || discard_locals
!= locals_undef
809 || strip_specific_list
!= NULL
810 || keep_specific_list
!= NULL
811 || localize_specific_list
!= NULL
812 || weaken_specific_list
!= NULL
815 || change_leading_char
816 || remove_leading_char
819 /* Mark symbols used in output relocations so that they
820 are kept, even if they are local labels or static symbols.
822 Note we iterate over the input sections examining their
823 relocations since the relocations for the output sections
824 haven't been set yet. mark_symbols_used_in_relocations will
825 ignore input sections which have no corresponding output
827 bfd_map_over_sections (ibfd
,
828 mark_symbols_used_in_relocations
,
830 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
831 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
834 if (convert_debugging
&& dhandle
!= NULL
)
836 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
844 bfd_set_symtab (obfd
, osympp
, symcount
);
846 /* This has to happen after the symbol table has been set. */
847 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
849 if (add_sections
!= NULL
)
851 struct section_add
*padd
;
853 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
855 if (! bfd_set_section_contents (obfd
, padd
->section
,
856 (PTR
) padd
->contents
,
858 (bfd_size_type
) padd
->size
))
859 nonfatal (bfd_get_filename (obfd
));
863 if (gap_fill_set
|| pad_to_set
)
868 /* Fill in the gaps. */
872 buf
= (bfd_byte
*) xmalloc (max_gap
);
873 memset (buf
, gap_fill
, (size_t) max_gap
);
875 c
= bfd_count_sections (obfd
);
876 for (i
= 0; i
< c
; i
++)
884 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
893 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
896 nonfatal (bfd_get_filename (obfd
));
905 /* Allow the BFD backend to copy any private data it understands
906 from the input BFD to the output BFD. This is done last to
907 permit the routine to look at the filtered symbol table, which is
908 important for the ECOFF code at least. */
909 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
911 fprintf (stderr
, "%s: %s: error copying private BFD data: %s\n",
912 program_name
, bfd_get_filename (obfd
),
913 bfd_errmsg (bfd_get_error ()));
919 /* Read each archive element in turn from IBFD, copy the
920 contents to temp file, and keep the temp file handle. */
923 copy_archive (ibfd
, obfd
, output_target
)
926 const char *output_target
;
930 struct name_list
*next
;
934 bfd
**ptr
= &obfd
->archive_head
;
936 char *dir
= make_tempname (bfd_get_filename (obfd
));
938 /* Make a temp directory to hold the contents. */
939 if (mkdir (dir
, 0700) != 0)
941 fatal ("cannot mkdir %s for archive copying (error: %s)",
942 dir
, strerror (errno
));
944 obfd
->has_armap
= ibfd
->has_armap
;
948 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
949 while (this_element
!= (bfd
*) NULL
)
951 /* Create an output file for this member. */
952 char *output_name
= concat (dir
, "/", bfd_get_filename(this_element
),
954 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
957 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
958 l
->name
= output_name
;
962 if (output_bfd
== (bfd
*) NULL
)
964 nonfatal (output_name
);
966 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
968 nonfatal (bfd_get_filename (obfd
));
971 if (bfd_check_format (this_element
, bfd_object
) == true)
973 copy_object (this_element
, output_bfd
);
976 bfd_close (output_bfd
);
978 /* Open the newly output file and attach to our list. */
979 output_bfd
= bfd_openr (output_name
, output_target
);
981 l
->obfd
= output_bfd
;
984 ptr
= &output_bfd
->next
;
986 last_element
= this_element
;
988 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
990 bfd_close (last_element
);
994 if (!bfd_close (obfd
))
996 nonfatal (bfd_get_filename (obfd
));
999 if (!bfd_close (ibfd
))
1001 nonfatal (bfd_get_filename (ibfd
));
1004 /* Delete all the files that we opened. */
1005 for (l
= list
; l
!= NULL
; l
= l
->next
)
1007 bfd_close (l
->obfd
);
1013 /* The top-level control. */
1016 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1017 const char *input_filename
;
1018 const char *output_filename
;
1019 const char *input_target
;
1020 const char *output_target
;
1025 /* To allow us to do "strip *" without dying on the first
1026 non-object file, failures are nonfatal. */
1028 ibfd
= bfd_openr (input_filename
, input_target
);
1031 nonfatal (input_filename
);
1034 if (bfd_check_format (ibfd
, bfd_archive
))
1038 /* bfd_get_target does not return the correct value until
1039 bfd_check_format succeeds. */
1040 if (output_target
== NULL
)
1041 output_target
= bfd_get_target (ibfd
);
1043 obfd
= bfd_openw (output_filename
, output_target
);
1046 nonfatal (output_filename
);
1048 copy_archive (ibfd
, obfd
, output_target
);
1050 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1054 /* bfd_get_target does not return the correct value until
1055 bfd_check_format succeeds. */
1056 if (output_target
== NULL
)
1057 output_target
= bfd_get_target (ibfd
);
1059 obfd
= bfd_openw (output_filename
, output_target
);
1062 nonfatal (output_filename
);
1065 copy_object (ibfd
, obfd
);
1067 if (!bfd_close (obfd
))
1069 nonfatal (output_filename
);
1072 if (!bfd_close (ibfd
))
1074 nonfatal (input_filename
);
1079 bfd_nonfatal (input_filename
);
1080 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1082 list_matching_formats (matching
);
1089 /* Create a section in OBFD with the same name and attributes
1090 as ISECTION in IBFD. */
1093 setup_section (ibfd
, isection
, obfdarg
)
1098 bfd
*obfd
= (bfd
*) obfdarg
;
1099 struct section_list
*p
;
1106 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1107 && (strip_symbols
== strip_debug
1108 || strip_symbols
== strip_unneeded
1109 || strip_symbols
== strip_all
1110 || discard_locals
== locals_all
1111 || convert_debugging
))
1114 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1118 if (p
!= NULL
&& p
->remove
)
1121 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1122 if (osection
== NULL
)
1128 if (!bfd_set_section_size (obfd
,
1130 bfd_section_size (ibfd
, isection
)))
1136 vma
= bfd_section_vma (ibfd
, isection
);
1137 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1139 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1142 vma
+= adjust_section_vma
;
1143 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1149 lma
= isection
->lma
;
1150 if (p
!= NULL
&& p
->adjust
== adjust_vma
)
1152 else if (p
!= NULL
&& p
->adjust
== set_vma
)
1155 lma
+= adjust_section_vma
;
1156 osection
->lma
= lma
;
1158 if (bfd_set_section_alignment (obfd
,
1160 bfd_section_alignment (ibfd
, isection
))
1167 flags
= bfd_get_section_flags (ibfd
, isection
);
1168 if (p
!= NULL
&& p
->set_flags
)
1169 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1170 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1176 /* This used to be mangle_section; we do here to avoid using
1177 bfd_get_section_by_name since some formats allow multiple
1178 sections with the same name. */
1179 isection
->output_section
= osection
;
1180 isection
->output_offset
= 0;
1182 /* Allow the BFD backend to copy any private data it understands
1183 from the input section to the output section. */
1184 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1186 err
= "private data";
1194 fprintf (stderr
, "%s: %s: section `%s': error in %s: %s\n",
1196 bfd_get_filename (ibfd
), bfd_section_name (ibfd
, isection
),
1197 err
, bfd_errmsg (bfd_get_error ()));
1201 /* Copy the data of input section ISECTION of IBFD
1202 to an output section with the same name in OBFD.
1203 If stripping then don't copy any relocation info. */
1206 copy_section (ibfd
, isection
, obfdarg
)
1211 bfd
*obfd
= (bfd
*) obfdarg
;
1212 struct section_list
*p
;
1218 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1219 && (strip_symbols
== strip_debug
1220 || strip_symbols
== strip_unneeded
1221 || strip_symbols
== strip_all
1222 || discard_locals
== locals_all
1223 || convert_debugging
))
1228 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1230 if (p
!= NULL
&& p
->remove
)
1233 osection
= isection
->output_section
;
1234 size
= bfd_get_section_size_before_reloc (isection
);
1236 if (size
== 0 || osection
== 0)
1239 if (strip_symbols
== strip_all
)
1240 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1245 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1248 nonfatal (bfd_get_filename (ibfd
));
1251 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1254 relpp
= (arelent
**) xmalloc (relsize
);
1255 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1258 nonfatal (bfd_get_filename (ibfd
));
1260 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1264 isection
->_cooked_size
= isection
->_raw_size
;
1265 isection
->reloc_done
= true;
1267 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1269 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1271 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1274 nonfatal (bfd_get_filename (ibfd
));
1279 filter_bytes (memhunk
, &size
);
1280 /* The section has gotten smaller. */
1281 if (!bfd_set_section_size (obfd
, osection
, size
))
1282 nonfatal (bfd_get_filename (obfd
));
1285 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1288 nonfatal (bfd_get_filename (obfd
));
1292 else if (p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1294 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1296 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1297 flag--they can just remove the section entirely and add it
1298 back again. However, we do permit them to turn on the
1299 SEC_HAS_CONTENTS flag, and take it to mean that the section
1300 contents should be zeroed out. */
1302 memset (memhunk
, 0, size
);
1303 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1305 nonfatal (bfd_get_filename (obfd
));
1310 /* Get all the sections. This is used when --gap-fill or --pad-to is
1314 get_sections (obfd
, osection
, secppparg
)
1319 asection
***secppp
= (asection
***) secppparg
;
1321 **secppp
= osection
;
1325 /* Sort sections by VMA. This is called via qsort, and is used when
1326 --gap-fill or --pad-to is used. We force non loadable or empty
1327 sections to the front, where they are easier to ignore. */
1330 compare_section_vma (arg1
, arg2
)
1334 const asection
**sec1
= (const asection
**) arg1
;
1335 const asection
**sec2
= (const asection
**) arg2
;
1336 flagword flags1
, flags2
;
1338 /* Sort non loadable sections to the front. */
1339 flags1
= (*sec1
)->flags
;
1340 flags2
= (*sec2
)->flags
;
1341 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1342 || (flags1
& SEC_LOAD
) == 0)
1344 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1345 && (flags2
& SEC_LOAD
) != 0)
1350 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1351 || (flags2
& SEC_LOAD
) == 0)
1355 /* Sort sections by VMA. */
1356 if ((*sec1
)->vma
> (*sec2
)->vma
)
1358 else if ((*sec1
)->vma
< (*sec2
)->vma
)
1361 /* Sort sections with the same VMA by size. */
1362 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1364 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1370 /* Mark all the symbols which will be used in output relocations with
1371 the BSF_KEEP flag so that those symbols will not be stripped.
1373 Ignore relocations which will not appear in the output file. */
1376 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1381 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1386 /* Ignore an input section with no corresponding output section. */
1387 if (isection
->output_section
== NULL
)
1390 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1392 bfd_fatal (bfd_get_filename (ibfd
));
1397 relpp
= (arelent
**) xmalloc (relsize
);
1398 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1400 bfd_fatal (bfd_get_filename (ibfd
));
1402 /* Examine each symbol used in a relocation. If it's not one of the
1403 special bfd section symbols, then mark it with BSF_KEEP. */
1404 for (i
= 0; i
< relcount
; i
++)
1406 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1407 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1408 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1409 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1416 /* Write out debugging information. */
1419 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1425 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1426 return write_ieee_debugging_info (obfd
, dhandle
);
1428 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1429 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1431 bfd_byte
*syms
, *strings
;
1432 bfd_size_type symsize
, stringsize
;
1433 asection
*stabsec
, *stabstrsec
;
1435 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1440 stabsec
= bfd_make_section (obfd
, ".stab");
1441 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1443 || stabstrsec
== NULL
1444 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1445 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1446 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1447 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1448 || ! bfd_set_section_flags (obfd
, stabsec
,
1452 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1457 fprintf (stderr
, "%s: can't create debugging section: %s\n",
1458 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1462 /* We can get away with setting the section contents now because
1463 the next thing the caller is going to do is copy over the
1464 real sections. We may someday have to split the contents
1465 setting out of this function. */
1466 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1468 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1469 (file_ptr
) 0, stringsize
))
1471 fprintf (stderr
, "%s: can't set debugging section contents: %s\n",
1472 bfd_get_filename (obfd
), bfd_errmsg (bfd_get_error ()));
1480 "%s: don't know how to write debugging information for %s\n",
1481 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1485 /* The number of bytes to copy at once. */
1486 #define COPY_BUF 8192
1488 /* Copy file FROM to file TO, performing no translations.
1489 Return 0 if ok, -1 if error. */
1492 simple_copy (from
, to
)
1496 int fromfd
, tofd
, nread
;
1500 fromfd
= open (from
, O_RDONLY
);
1503 tofd
= creat (to
, 0777);
1511 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1513 if (write (tofd
, buf
, nread
) != nread
)
1535 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1537 #define S_ISLNK(m) 0
1542 /* Rename FROM to TO, copying if TO is a link.
1543 Assumes that TO already exists, because FROM is a temp file.
1544 Return 0 if ok, -1 if error. */
1547 smart_rename (from
, to
)
1557 /* Use rename only if TO is not a symbolic link and has
1558 only one hard link. */
1559 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1561 ret
= rename (from
, to
);
1564 /* Try to preserve the permission bits and ownership of TO.
1565 First get the mode right except for the setuid bit. Then
1566 change the ownership. Then fix the setuid bit. We do
1567 the chmod before the chown because if the chown succeeds,
1568 and we are a normal user, we won't be able to do the
1569 chmod afterward. We don't bother to fix the setuid bit
1570 first because that might introduce a fleeting security
1571 problem, and because the chown will clear the setuid bit
1572 anyhow. We only fix the setuid bit if the chown
1573 succeeds, because we don't want to introduce an
1574 unexpected setuid file owned by the user running objcopy. */
1575 chmod (to
, s
.st_mode
& 0777);
1576 if (chown (to
, s
.st_uid
, s
.st_gid
) >= 0)
1577 chmod (to
, s
.st_mode
& 07777);
1581 /* We have to clean up here. */
1583 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1591 ret
= simple_copy (from
, to
);
1595 fprintf (stderr
, "%s: %s: ", program_name
, to
);
1597 perror ("simple_copy");
1604 /* Set the times of the file DESTINATION to be the same as those in
1608 set_times (destination
, statbuf
)
1609 const char *destination
;
1610 const struct stat
*statbuf
;
1615 #ifdef HAVE_GOOD_UTIME_H
1618 tb
.actime
= statbuf
->st_atime
;
1619 tb
.modtime
= statbuf
->st_mtime
;
1620 result
= utime (destination
, &tb
);
1621 #else /* ! HAVE_GOOD_UTIME_H */
1625 tb
[0] = statbuf
->st_atime
;
1626 tb
[1] = statbuf
->st_mtime
;
1627 result
= utime (destination
, tb
);
1628 #else /* HAVE_UTIMES */
1629 struct timeval tv
[2];
1631 tv
[0].tv_sec
= statbuf
->st_atime
;
1633 tv
[1].tv_sec
= statbuf
->st_mtime
;
1635 result
= utimes (destination
, tv
);
1636 #endif /* HAVE_UTIMES */
1637 #endif /* ! HAVE_GOOD_UTIME_H */
1642 fprintf (stderr
, "%s: ", destination
);
1643 perror ("can not set time");
1648 strip_main (argc
, argv
)
1652 char *input_target
= NULL
, *output_target
= NULL
;
1653 boolean show_version
= false;
1654 boolean preserve_dates
= false;
1656 struct section_list
*p
;
1657 char *output_file
= NULL
;
1659 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1660 strip_options
, (int *) 0)) != EOF
)
1665 input_target
= optarg
;
1668 output_target
= optarg
;
1671 input_target
= output_target
= optarg
;
1674 p
= find_section_list (optarg
, true);
1676 sections_removed
= true;
1679 strip_symbols
= strip_all
;
1683 strip_symbols
= strip_debug
;
1685 case OPTION_STRIP_UNNEEDED
:
1686 strip_symbols
= strip_unneeded
;
1689 add_specific_symbol (optarg
, &keep_specific_list
);
1692 add_specific_symbol (optarg
, &strip_specific_list
);
1695 output_file
= optarg
;
1698 preserve_dates
= true;
1701 discard_locals
= locals_all
;
1704 discard_locals
= locals_start_L
;
1710 show_version
= true;
1713 break; /* we've been given a long option */
1715 strip_usage (stdout
, 0);
1717 strip_usage (stderr
, 1);
1722 print_version ("strip");
1724 /* Default is to strip all symbols. */
1725 if (strip_symbols
== strip_undef
1726 && discard_locals
== locals_undef
1727 && strip_specific_list
== NULL
)
1728 strip_symbols
= strip_all
;
1730 if (output_target
== (char *) NULL
)
1731 output_target
= input_target
;
1735 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1736 strip_usage (stderr
, 1);
1738 for (; i
< argc
; i
++)
1740 int hold_status
= status
;
1741 struct stat statbuf
;
1746 if (stat (argv
[i
], &statbuf
) < 0)
1748 fprintf (stderr
, "%s: ", argv
[i
]);
1749 perror ("cannot stat");
1754 if (output_file
!= NULL
)
1755 tmpname
= output_file
;
1757 tmpname
= make_tempname (argv
[i
]);
1760 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1764 set_times (tmpname
, &statbuf
);
1765 if (output_file
== NULL
)
1766 smart_rename (tmpname
, argv
[i
]);
1767 status
= hold_status
;
1771 if (output_file
== NULL
)
1779 copy_main (argc
, argv
)
1783 char *input_filename
= NULL
, *output_filename
= NULL
;
1784 char *input_target
= NULL
, *output_target
= NULL
;
1785 boolean show_version
= false;
1786 boolean adjust_warn
= true;
1787 boolean preserve_dates
= false;
1789 struct section_list
*p
;
1790 struct stat statbuf
;
1792 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1793 copy_options
, (int *) 0)) != EOF
)
1798 copy_byte
= atoi(optarg
);
1801 fprintf (stderr
, "%s: byte number must be non-negative\n",
1807 interleave
= atoi(optarg
);
1810 fprintf(stderr
, "%s: interleave must be positive\n",
1816 case 's': /* "source" - 'I' is preferred */
1817 input_target
= optarg
;
1820 case 'd': /* "destination" - 'O' is preferred */
1821 output_target
= optarg
;
1824 input_target
= output_target
= optarg
;
1827 p
= find_section_list (optarg
, true);
1829 sections_removed
= true;
1832 strip_symbols
= strip_all
;
1835 strip_symbols
= strip_debug
;
1837 case OPTION_STRIP_UNNEEDED
:
1838 strip_symbols
= strip_unneeded
;
1841 add_specific_symbol (optarg
, &keep_specific_list
);
1844 add_specific_symbol (optarg
, &strip_specific_list
);
1847 add_specific_symbol (optarg
, &localize_specific_list
);
1850 add_specific_symbol (optarg
, &weaken_specific_list
);
1853 preserve_dates
= true;
1856 discard_locals
= locals_all
;
1859 discard_locals
= locals_start_L
;
1865 show_version
= true;
1870 case OPTION_ADD_SECTION
:
1874 struct section_add
*pa
;
1879 s
= strchr (optarg
, '=');
1883 "%s: bad format for --add-section NAME=FILENAME\n",
1888 if (stat (s
+ 1, &st
) < 0)
1890 fprintf (stderr
, "%s: ", program_name
);
1895 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1898 name
= (char *) xmalloc (len
+ 1);
1899 strncpy (name
, optarg
, len
);
1903 pa
->filename
= s
+ 1;
1905 pa
->size
= st
.st_size
;
1907 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1908 f
= fopen (pa
->filename
, FOPEN_RB
);
1911 fprintf (stderr
, "%s: ", program_name
);
1912 perror (pa
->filename
);
1915 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1918 fprintf (stderr
, "%s: %s: fread failed\n",
1919 program_name
, pa
->filename
);
1924 pa
->next
= add_sections
;
1928 case OPTION_ADJUST_START
:
1929 adjust_start
= parse_vma (optarg
, "--adjust-start");
1931 case OPTION_ADJUST_SECTION_VMA
:
1937 s
= strchr (optarg
, '=');
1940 s
= strchr (optarg
, '+');
1943 s
= strchr (optarg
, '-');
1947 "%s: bad format for --adjust-section-vma\n",
1955 name
= (char *) xmalloc (len
+ 1);
1956 strncpy (name
, optarg
, len
);
1959 p
= find_section_list (name
, true);
1961 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1964 p
->adjust
= set_vma
;
1967 p
->adjust
= adjust_vma
;
1973 case OPTION_ADJUST_VMA
:
1974 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
1975 adjust_start
= adjust_section_vma
;
1977 case OPTION_ADJUST_WARNINGS
:
1980 case OPTION_CHANGE_LEADING_CHAR
:
1981 change_leading_char
= true;
1983 case OPTION_DEBUGGING
:
1984 convert_debugging
= true;
1986 case OPTION_GAP_FILL
:
1988 bfd_vma gap_fill_vma
;
1990 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
1991 gap_fill
= (bfd_byte
) gap_fill_vma
;
1992 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
1994 fprintf (stderr
, "%s: warning: truncating gap-fill from 0x",
1996 fprintf_vma (stderr
, gap_fill_vma
);
1997 fprintf (stderr
, "to 0x%x\n", (unsigned int) gap_fill
);
1999 gap_fill_set
= true;
2002 case OPTION_NO_ADJUST_WARNINGS
:
2003 adjust_warn
= false;
2006 pad_to
= parse_vma (optarg
, "--pad-to");
2009 case OPTION_REMOVE_LEADING_CHAR
:
2010 remove_leading_char
= true;
2012 case OPTION_SET_SECTION_FLAGS
:
2018 s
= strchr (optarg
, '=');
2021 fprintf (stderr
, "%s: bad format for --set-section-flags\n",
2027 name
= (char *) xmalloc (len
+ 1);
2028 strncpy (name
, optarg
, len
);
2031 p
= find_section_list (name
, true);
2033 p
->set_flags
= true;
2034 p
->flags
= parse_flags (s
+ 1);
2037 case OPTION_SET_START
:
2038 set_start
= parse_vma (optarg
, "--set-start");
2039 set_start_set
= true;
2042 break; /* we've been given a long option */
2044 copy_usage (stdout
, 0);
2046 copy_usage (stderr
, 1);
2051 print_version ("objcopy");
2053 if (copy_byte
>= interleave
)
2055 fprintf (stderr
, "%s: byte number must be less than interleave\n",
2060 if (optind
== argc
|| optind
+ 2 < argc
)
2061 copy_usage (stderr
, 1);
2063 input_filename
= argv
[optind
];
2064 if (optind
+ 1 < argc
)
2065 output_filename
= argv
[optind
+ 1];
2067 /* Default is to strip no symbols. */
2068 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
2069 strip_symbols
= strip_none
;
2071 if (output_target
== (char *) NULL
)
2072 output_target
= input_target
;
2076 if (stat (input_filename
, &statbuf
) < 0)
2078 fprintf (stderr
, "%s: ", input_filename
);
2079 perror ("cannot stat");
2084 /* If there is no destination file then create a temp and rename
2085 the result into the input. */
2087 if (output_filename
== (char *) NULL
)
2089 char *tmpname
= make_tempname (input_filename
);
2091 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2095 set_times (tmpname
, &statbuf
);
2096 smart_rename (tmpname
, input_filename
);
2103 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2104 if (status
== 0 && preserve_dates
)
2105 set_times (output_filename
, &statbuf
);
2110 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
2112 if (! p
->used
&& p
->adjust
!= ignore_vma
)
2114 fprintf (stderr
, "%s: warning: --adjust-section-vma %s%c0x",
2115 program_name
, p
->name
,
2116 p
->adjust
== set_vma
? '=' : '+');
2117 fprintf_vma (stderr
, p
->val
);
2118 fprintf (stderr
, " never used\n");
2131 program_name
= argv
[0];
2132 xmalloc_set_program_name (program_name
);
2134 START_PROGRESS (program_name
, 0);
2136 strip_symbols
= strip_undef
;
2137 discard_locals
= locals_undef
;
2140 set_default_bfd_target ();
2144 int i
= strlen (program_name
);
2145 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2149 strip_main (argc
, argv
);
2151 copy_main (argc
, argv
);
2153 END_PROGRESS (program_name
);