1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "libiberty.h"
30 #ifdef HAVE_GOOD_UTIME_H
32 #else /* ! HAVE_GOOD_UTIME_H */
35 #endif /* HAVE_UTIMES */
36 #endif /* ! HAVE_GOOD_UTIME_H */
38 /* A list of symbols to explicitly strip out, or to keep. A linked
39 list is good enough for a small number from the command line, but
40 this will slow things down a lot if many symbols are being
49 static void copy_usage
PARAMS ((FILE *, int));
50 static void strip_usage
PARAMS ((FILE *, int));
51 static flagword parse_flags
PARAMS ((const char *));
52 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
53 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
54 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
55 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
56 static int compare_section_lma
PARAMS ((const PTR
, const PTR
));
57 static void add_specific_symbol
PARAMS ((const char *, struct symlist
**));
58 static boolean is_specified_symbol
PARAMS ((const char *, struct symlist
*));
59 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
60 static unsigned int filter_symbols
61 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
62 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
63 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
64 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
65 static void copy_object
PARAMS ((bfd
*, bfd
*));
66 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
68 PARAMS ((const char *, const char *, const char *, const char *));
69 static int simple_copy
PARAMS ((const char *, const char *));
70 static int smart_rename
PARAMS ((const char *, const char *));
71 static void set_times
PARAMS ((const char *, const struct stat
*));
72 static int strip_main
PARAMS ((int, char **));
73 static int copy_main
PARAMS ((int, char **));
75 #define RETURN_NONFATAL(s) {bfd_nonfatal(s); status = 1; return;}
77 static asymbol
**isympp
= NULL
; /* Input symbols */
78 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
80 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
81 static int copy_byte
= -1;
82 static int interleave
= 4;
84 static boolean verbose
; /* Print file and target names. */
85 static boolean preserve_dates
; /* Preserve input file timestamp. */
86 static int status
= 0; /* Exit status. */
91 STRIP_NONE
, /* don't strip */
92 STRIP_DEBUG
, /* strip all debugger symbols */
93 STRIP_UNNEEDED
, /* strip unnecessary symbols */
94 STRIP_ALL
/* strip all symbols */
97 /* Which symbols to remove. */
98 static enum strip_action strip_symbols
;
103 LOCALS_START_L
, /* discard locals starting with L */
104 LOCALS_ALL
/* discard all locals */
107 /* Which local symbols to remove. Overrides STRIP_ALL. */
108 static enum locals_action discard_locals
;
110 /* What kind of change to perform. */
118 /* Structure used to hold lists of sections and actions to take. */
121 struct section_list
* next
; /* Next section to change. */
122 const char * name
; /* Section name. */
123 boolean used
; /* Whether this entry was used. */
124 boolean remove
; /* Whether to remove this section. */
125 enum change_action change_vma
;/* Whether to change or set VMA. */
126 bfd_vma vma_val
; /* Amount to change by or set to. */
127 enum change_action change_lma
;/* Whether to change or set LMA. */
128 bfd_vma lma_val
; /* Amount to change by or set to. */
129 boolean set_flags
; /* Whether to set the section flags. */
130 flagword flags
; /* What to set the section flags to. */
133 static struct section_list
*change_sections
;
134 static boolean sections_removed
;
136 /* Changes to the start address. */
137 static bfd_vma change_start
= 0;
138 static boolean set_start_set
= false;
139 static bfd_vma set_start
;
141 /* Changes to section addresses. */
142 static bfd_vma change_section_address
= 0;
144 /* Filling gaps between sections. */
145 static boolean gap_fill_set
= false;
146 static bfd_byte gap_fill
= 0;
148 /* Pad to a given address. */
149 static boolean pad_to_set
= false;
150 static bfd_vma pad_to
;
152 /* List of sections to add. */
156 /* Next section to add. */
157 struct section_add
*next
;
158 /* Name of section to add. */
160 /* Name of file holding section contents. */
161 const char *filename
;
164 /* Contents of file. */
166 /* BFD section, after it has been added. */
170 static struct section_add
*add_sections
;
172 /* Whether to convert debugging information. */
174 static boolean convert_debugging
= false;
176 /* Whether to change the leading character in symbol names. */
178 static boolean change_leading_char
= false;
180 /* Whether to remove the leading character from global symbol names. */
182 static boolean remove_leading_char
= false;
184 /* List of symbols to strip, keep, localize, and weaken. */
186 static struct symlist
*strip_specific_list
= NULL
;
187 static struct symlist
*keep_specific_list
= NULL
;
188 static struct symlist
*localize_specific_list
= NULL
;
189 static struct symlist
*weaken_specific_list
= NULL
;
191 /* If this is true, we weaken global symbols (set BSF_WEAK). */
193 static boolean weaken
= false;
195 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
197 #define OPTION_ADD_SECTION 150
198 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
199 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
200 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
201 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
202 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
203 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
204 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
205 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
206 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
207 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
208 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
209 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
210 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
211 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
212 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
213 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
215 /* Options to handle if running as "strip". */
217 static struct option strip_options
[] =
219 {"discard-all", no_argument
, 0, 'x'},
220 {"discard-locals", no_argument
, 0, 'X'},
221 {"format", required_argument
, 0, 'F'}, /* Obsolete */
222 {"help", no_argument
, 0, 'h'},
223 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
224 {"input-target", required_argument
, 0, 'I'},
225 {"keep-symbol", required_argument
, 0, 'K'},
226 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
227 {"output-target", required_argument
, 0, 'O'},
228 {"preserve-dates", no_argument
, 0, 'p'},
229 {"remove-section", required_argument
, 0, 'R'},
230 {"strip-all", no_argument
, 0, 's'},
231 {"strip-debug", no_argument
, 0, 'S'},
232 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
233 {"strip-symbol", required_argument
, 0, 'N'},
234 {"target", required_argument
, 0, 'F'},
235 {"verbose", no_argument
, 0, 'v'},
236 {"version", no_argument
, 0, 'V'},
237 {0, no_argument
, 0, 0}
240 /* Options to handle if running as "objcopy". */
242 static struct option copy_options
[] =
244 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
245 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
246 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
247 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
248 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
249 {"byte", required_argument
, 0, 'b'},
250 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
251 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
252 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
253 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
254 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
255 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
256 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
257 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
258 {"discard-all", no_argument
, 0, 'x'},
259 {"discard-locals", no_argument
, 0, 'X'},
260 {"format", required_argument
, 0, 'F'}, /* Obsolete */
261 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
262 {"help", no_argument
, 0, 'h'},
263 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
264 {"input-target", required_argument
, 0, 'I'},
265 {"interleave", required_argument
, 0, 'i'},
266 {"keep-symbol", required_argument
, 0, 'K'},
267 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
268 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
269 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
270 {"output-target", required_argument
, 0, 'O'},
271 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
272 {"preserve-dates", no_argument
, 0, 'p'},
273 {"localize-symbol", required_argument
, 0, 'L'},
274 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
275 {"remove-section", required_argument
, 0, 'R'},
276 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
277 {"set-start", required_argument
, 0, OPTION_SET_START
},
278 {"strip-all", no_argument
, 0, 'S'},
279 {"strip-debug", no_argument
, 0, 'g'},
280 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
281 {"strip-symbol", required_argument
, 0, 'N'},
282 {"target", required_argument
, 0, 'F'},
283 {"verbose", no_argument
, 0, 'v'},
284 {"version", no_argument
, 0, 'V'},
285 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
286 {"weaken-symbol", required_argument
, 0, 'W'},
287 {0, no_argument
, 0, 0}
291 extern char *program_name
;
293 /* This flag distinguishes between strip and objcopy:
294 1 means this is 'strip'; 0 means this is 'objcopy'.
295 -1 means if we should use argv[0] to decide. */
300 copy_usage (stream
, exit_status
)
304 fprintf (stream
, _("\
305 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
306 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
307 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
308 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
309 [--discard-locals] [--debugging] [--remove-section=section]\n"),
311 fprintf (stream
, _("\
312 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
314 [--change-start=incr] [--change-addresses=incr]
315 (--adjust-start and --adjust-vma are aliases for these two)
316 [--change-section-address=section{=,+,-}val]\n\
317 (--adjust-section-vma is an alias for --change-section-address)\n\
318 [--change-section-lma=section{=,+,-}val]\n\
319 [--change-section-vma=section{=,+,-}val]\n\
320 [--adjust-warnings] [--no-adjust-warnings]\n\
321 [--change-warnings] [--no-change-warnings]\n\
322 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
323 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
324 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
325 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
326 [--verbose] [--version] [--help] in-file [out-file]\n"));
327 list_supported_targets (program_name
, stream
);
328 if (exit_status
== 0)
329 fprintf (stream
, _("Report bugs to bug-gnu-utils@gnu.org\n"));
334 strip_usage (stream
, exit_status
)
338 fprintf (stream
, _("\
339 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
340 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
341 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
342 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
343 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
344 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n"),
346 list_supported_targets (program_name
, stream
);
347 if (exit_status
== 0)
348 fprintf (stream
, _("Report bugs to bug-gnu-utils@gnu.org\n"));
352 /* Parse section flags into a flagword, with a fatal error if the
353 string can't be parsed. */
367 snext
= strchr (s
, ',');
377 #define PARSE_FLAG(fname,fval) \
378 else if (strncasecmp (fname, s, len) == 0) ret |= fval
379 PARSE_FLAG ("alloc", SEC_ALLOC
);
380 PARSE_FLAG ("load", SEC_LOAD
);
381 PARSE_FLAG ("readonly", SEC_READONLY
);
382 PARSE_FLAG ("code", SEC_CODE
);
383 PARSE_FLAG ("data", SEC_DATA
);
384 PARSE_FLAG ("rom", SEC_ROM
);
385 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
391 copy
= xmalloc (len
+ 1);
392 strncpy (copy
, s
, len
);
394 non_fatal (_("unrecognized section flag `%s'"), copy
);
395 fatal (_("supported flags: alloc, load, readonly, code, data, rom, contents"));
405 /* Find and optionally add an entry in the change_sections list. */
407 static struct section_list
*
408 find_section_list (name
, add
)
412 register struct section_list
*p
;
414 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
415 if (strcmp (p
->name
, name
) == 0)
421 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
425 p
->change_vma
= CHANGE_IGNORE
;
426 p
->change_lma
= CHANGE_IGNORE
;
429 p
->set_flags
= false;
432 p
->next
= change_sections
;
438 /* Add a symbol to strip_specific_list. */
441 add_specific_symbol (name
, list
)
443 struct symlist
**list
;
445 struct symlist
*tmp_list
;
447 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
448 tmp_list
->name
= name
;
449 tmp_list
->next
= *list
;
453 /* See whether a symbol should be stripped or kept based on
454 strip_specific_list and keep_symbols. */
457 is_specified_symbol (name
, list
)
459 struct symlist
*list
;
461 struct symlist
*tmp_list
;
463 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
465 if (strcmp (name
, tmp_list
->name
) == 0)
471 /* See if a section is being removed. */
474 is_strip_section (abfd
, sec
)
478 struct section_list
*p
;
480 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
481 && (strip_symbols
== STRIP_DEBUG
482 || strip_symbols
== STRIP_UNNEEDED
483 || strip_symbols
== STRIP_ALL
484 || discard_locals
== LOCALS_ALL
485 || convert_debugging
))
488 if (! sections_removed
)
490 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
491 return p
!= NULL
&& p
->remove
? true : false;
494 /* Choose which symbol entries to copy; put the result in OSYMS.
495 We don't copy in place, because that confuses the relocs.
496 Return the number of symbols to print. */
499 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
502 asymbol
**osyms
, **isyms
;
505 register asymbol
**from
= isyms
, **to
= osyms
;
506 long src_count
= 0, dst_count
= 0;
508 for (; src_count
< symcount
; src_count
++)
510 asymbol
*sym
= from
[src_count
];
511 flagword flags
= sym
->flags
;
512 const char *name
= bfd_asymbol_name (sym
);
515 if (change_leading_char
516 && (bfd_get_symbol_leading_char (abfd
)
517 != bfd_get_symbol_leading_char (obfd
))
518 && (bfd_get_symbol_leading_char (abfd
) == '\0'
519 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
521 if (bfd_get_symbol_leading_char (obfd
) == '\0')
522 name
= bfd_asymbol_name (sym
) = name
+ 1;
527 n
= xmalloc (strlen (name
) + 2);
528 n
[0] = bfd_get_symbol_leading_char (obfd
);
529 if (bfd_get_symbol_leading_char (abfd
) == '\0')
530 strcpy (n
+ 1, name
);
532 strcpy (n
+ 1, name
+ 1);
533 name
= bfd_asymbol_name (sym
) = n
;
537 if (remove_leading_char
538 && ((flags
& BSF_GLOBAL
) != 0
539 || (flags
& BSF_WEAK
) != 0
540 || bfd_is_und_section (bfd_get_section (sym
))
541 || bfd_is_com_section (bfd_get_section (sym
)))
542 && name
[0] == bfd_get_symbol_leading_char (abfd
))
543 name
= bfd_asymbol_name (sym
) = name
+ 1;
545 if ((flags
& BSF_KEEP
) != 0) /* Used in relocation. */
547 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
548 || (flags
& BSF_WEAK
) != 0
549 || bfd_is_und_section (bfd_get_section (sym
))
550 || bfd_is_com_section (bfd_get_section (sym
)))
551 keep
= strip_symbols
!= STRIP_UNNEEDED
;
552 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
553 keep
= (strip_symbols
!= STRIP_DEBUG
554 && strip_symbols
!= STRIP_UNNEEDED
555 && ! convert_debugging
);
556 else /* Local symbol. */
557 keep
= (strip_symbols
!= STRIP_UNNEEDED
558 && (discard_locals
!= LOCALS_ALL
559 && (discard_locals
!= LOCALS_START_L
560 || ! bfd_is_local_label (abfd
, sym
))));
562 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
564 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
566 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
569 if (keep
&& (flags
& BSF_GLOBAL
) != 0
570 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
572 sym
->flags
&=~ BSF_GLOBAL
;
573 sym
->flags
|= BSF_WEAK
;
575 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
576 && is_specified_symbol (name
, localize_specific_list
))
578 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
579 sym
->flags
|= BSF_LOCAL
;
583 to
[dst_count
++] = sym
;
586 to
[dst_count
] = NULL
;
591 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
595 filter_bytes (memhunk
, size
)
599 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
601 for (; from
< end
; from
+= interleave
)
606 /* Copy object file IBFD onto OBFD. */
609 copy_object (ibfd
, obfd
)
615 asection
**osections
= NULL
;
616 bfd_size_type
*gaps
= NULL
;
617 bfd_size_type max_gap
= 0;
619 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
620 RETURN_NONFATAL (bfd_get_filename (obfd
));
623 printf (_("copy from %s(%s) to %s(%s)\n"),
624 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
625 bfd_get_filename(obfd
), bfd_get_target(obfd
));
630 start
= bfd_get_start_address (ibfd
);
631 start
+= change_start
;
633 if (!bfd_set_start_address (obfd
, start
)
634 || !bfd_set_file_flags (obfd
,
635 (bfd_get_file_flags (ibfd
)
636 & bfd_applicable_file_flags (obfd
))))
637 RETURN_NONFATAL (bfd_get_filename (ibfd
));
639 /* Copy architecture of input file to output file */
640 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
641 bfd_get_mach (ibfd
)))
642 non_fatal (_("Warning: Output file cannot represent architecture %s"),
643 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
644 bfd_get_mach (ibfd
)));
646 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
647 RETURN_NONFATAL (bfd_get_filename(ibfd
));
651 if (osympp
!= isympp
)
654 /* bfd mandates that all output sections be created and sizes set before
655 any output is done. Thus, we traverse all sections multiple times. */
656 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
658 if (add_sections
!= NULL
)
660 struct section_add
*padd
;
661 struct section_list
*pset
;
663 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
665 padd
->section
= bfd_make_section (obfd
, padd
->name
);
666 if (padd
->section
== NULL
)
668 non_fatal (_("can't create section `%s': %s"),
669 padd
->name
, bfd_errmsg (bfd_get_error ()));
677 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
678 RETURN_NONFATAL (bfd_get_filename (obfd
));
680 pset
= find_section_list (padd
->name
, false);
684 if (pset
!= NULL
&& pset
->set_flags
)
685 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
687 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
689 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
690 RETURN_NONFATAL (bfd_get_filename (obfd
));
694 if (pset
->change_vma
!= CHANGE_IGNORE
)
695 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
696 RETURN_NONFATAL (bfd_get_filename (obfd
));
698 if (pset
->change_lma
!= CHANGE_IGNORE
)
700 padd
->section
->lma
= pset
->lma_val
;
702 if (! bfd_set_section_alignment
703 (obfd
, padd
->section
,
704 bfd_section_alignment (obfd
, padd
->section
)))
705 RETURN_NONFATAL (bfd_get_filename (obfd
));
712 if (gap_fill_set
|| pad_to_set
)
717 /* We must fill in gaps between the sections and/or we must pad
718 the last section to a specified address. We do this by
719 grabbing a list of the sections, sorting them by VMA, and
720 increasing the section sizes as required to fill the gaps.
721 We write out the gap contents below. */
723 c
= bfd_count_sections (obfd
);
724 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
726 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
728 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
730 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
731 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
735 for (i
= 0; i
< c
- 1; i
++)
739 bfd_vma gap_start
, gap_stop
;
741 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
742 if ((flags
& SEC_HAS_CONTENTS
) == 0
743 || (flags
& SEC_LOAD
) == 0)
746 size
= bfd_section_size (obfd
, osections
[i
]);
747 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
748 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
749 if (gap_start
< gap_stop
)
751 if (! bfd_set_section_size (obfd
, osections
[i
],
752 size
+ (gap_stop
- gap_start
)))
754 non_fatal (_("Can't fill gap after %s: %s"),
755 bfd_get_section_name (obfd
, osections
[i
]),
756 bfd_errmsg (bfd_get_error()));
760 gaps
[i
] = gap_stop
- gap_start
;
761 if (max_gap
< gap_stop
- gap_start
)
762 max_gap
= gap_stop
- gap_start
;
772 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
773 size
= bfd_section_size (obfd
, osections
[c
- 1]);
774 if (lma
+ size
< pad_to
)
776 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
779 non_fatal (_("Can't add padding to %s: %s"),
780 bfd_get_section_name (obfd
, osections
[c
- 1]),
781 bfd_errmsg (bfd_get_error ()));
786 gaps
[c
- 1] = pad_to
- (lma
+ size
);
787 if (max_gap
< pad_to
- (lma
+ size
))
788 max_gap
= pad_to
- (lma
+ size
);
794 /* Symbol filtering must happen after the output sections have
795 been created, but before their contents are set. */
796 if (strip_symbols
== STRIP_ALL
)
798 osympp
= isympp
= NULL
;
806 symsize
= bfd_get_symtab_upper_bound (ibfd
);
808 RETURN_NONFATAL (bfd_get_filename (ibfd
));
810 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
811 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
813 RETURN_NONFATAL (bfd_get_filename (ibfd
));
815 if (convert_debugging
)
816 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
818 if (strip_symbols
== STRIP_DEBUG
819 || strip_symbols
== STRIP_UNNEEDED
820 || discard_locals
!= LOCALS_UNDEF
821 || strip_specific_list
!= NULL
822 || keep_specific_list
!= NULL
823 || localize_specific_list
!= NULL
824 || weaken_specific_list
!= NULL
827 || change_leading_char
828 || remove_leading_char
831 /* Mark symbols used in output relocations so that they
832 are kept, even if they are local labels or static symbols.
834 Note we iterate over the input sections examining their
835 relocations since the relocations for the output sections
836 haven't been set yet. mark_symbols_used_in_relocations will
837 ignore input sections which have no corresponding output
839 bfd_map_over_sections (ibfd
,
840 mark_symbols_used_in_relocations
,
842 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
843 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
846 if (convert_debugging
&& dhandle
!= NULL
)
848 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
856 bfd_set_symtab (obfd
, osympp
, symcount
);
858 /* This has to happen after the symbol table has been set. */
859 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
861 if (add_sections
!= NULL
)
863 struct section_add
*padd
;
865 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
867 if (! bfd_set_section_contents (obfd
, padd
->section
,
868 (PTR
) padd
->contents
,
870 (bfd_size_type
) padd
->size
))
871 RETURN_NONFATAL (bfd_get_filename (obfd
));
875 if (gap_fill_set
|| pad_to_set
)
880 /* Fill in the gaps. */
884 buf
= (bfd_byte
*) xmalloc (max_gap
);
885 memset (buf
, gap_fill
, (size_t) max_gap
);
887 c
= bfd_count_sections (obfd
);
888 for (i
= 0; i
< c
; i
++)
896 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
905 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
907 RETURN_NONFATAL (bfd_get_filename (obfd
));
916 /* Allow the BFD backend to copy any private data it understands
917 from the input BFD to the output BFD. This is done last to
918 permit the routine to look at the filtered symbol table, which is
919 important for the ECOFF code at least. */
920 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
922 non_fatal (_("%s: error copying private BFD data: %s"),
923 bfd_get_filename (obfd
),
924 bfd_errmsg (bfd_get_error ()));
930 /* Read each archive element in turn from IBFD, copy the
931 contents to temp file, and keep the temp file handle. */
934 copy_archive (ibfd
, obfd
, output_target
)
937 const char *output_target
;
941 struct name_list
*next
;
945 bfd
**ptr
= &obfd
->archive_head
;
947 char *dir
= make_tempname (bfd_get_filename (obfd
));
949 /* Make a temp directory to hold the contents. */
950 #if defined (_WIN32) && !defined (__CYGWIN32__)
951 if (mkdir (dir
) != 0)
953 if (mkdir (dir
, 0700) != 0)
956 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
957 dir
, strerror (errno
));
959 obfd
->has_armap
= ibfd
->has_armap
;
963 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
964 while (this_element
!= (bfd
*) NULL
)
966 /* Create an output file for this member. */
967 char *output_name
= concat (dir
, "/", bfd_get_filename(this_element
),
969 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
972 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
973 l
->name
= output_name
;
977 if (output_bfd
== (bfd
*) NULL
)
978 RETURN_NONFATAL (output_name
);
980 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
981 RETURN_NONFATAL (bfd_get_filename (obfd
));
983 if (bfd_check_format (this_element
, bfd_object
) == true)
984 copy_object (this_element
, output_bfd
);
986 bfd_close (output_bfd
);
988 /* Open the newly output file and attach to our list. */
989 output_bfd
= bfd_openr (output_name
, output_target
);
991 l
->obfd
= output_bfd
;
994 ptr
= &output_bfd
->next
;
996 last_element
= this_element
;
998 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1000 bfd_close (last_element
);
1002 *ptr
= (bfd
*) NULL
;
1004 if (!bfd_close (obfd
))
1005 RETURN_NONFATAL (bfd_get_filename (obfd
));
1007 if (!bfd_close (ibfd
))
1008 RETURN_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
);
1036 RETURN_NONFATAL (input_filename
);
1038 if (bfd_check_format (ibfd
, bfd_archive
))
1042 /* bfd_get_target does not return the correct value until
1043 bfd_check_format succeeds. */
1044 if (output_target
== NULL
)
1045 output_target
= bfd_get_target (ibfd
);
1047 obfd
= bfd_openw (output_filename
, output_target
);
1049 RETURN_NONFATAL (output_filename
);
1051 copy_archive (ibfd
, obfd
, output_target
);
1053 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1057 /* bfd_get_target does not return the correct value until
1058 bfd_check_format succeeds. */
1059 if (output_target
== NULL
)
1060 output_target
= bfd_get_target (ibfd
);
1062 obfd
= bfd_openw (output_filename
, output_target
);
1064 RETURN_NONFATAL (output_filename
);
1066 copy_object (ibfd
, obfd
);
1068 if (!bfd_close (obfd
))
1069 RETURN_NONFATAL (output_filename
);
1071 if (!bfd_close (ibfd
))
1072 RETURN_NONFATAL (input_filename
);
1076 bfd_nonfatal (input_filename
);
1078 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1080 list_matching_formats (matching
);
1088 /* Create a section in OBFD with the same name and attributes
1089 as ISECTION in IBFD. */
1092 setup_section (ibfd
, isection
, obfdarg
)
1097 bfd
*obfd
= (bfd
*) obfdarg
;
1098 struct section_list
*p
;
1105 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1106 && (strip_symbols
== STRIP_DEBUG
1107 || strip_symbols
== STRIP_UNNEEDED
1108 || strip_symbols
== STRIP_ALL
1109 || discard_locals
== LOCALS_ALL
1110 || convert_debugging
))
1113 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1117 if (p
!= NULL
&& p
->remove
)
1120 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1121 if (osection
== NULL
)
1127 if (!bfd_set_section_size (obfd
,
1129 bfd_section_size (ibfd
, isection
)))
1135 vma
= bfd_section_vma (ibfd
, isection
);
1136 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1138 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1141 vma
+= change_section_address
;
1143 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1149 lma
= isection
->lma
;
1150 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1152 if (p
->change_lma
== CHANGE_MODIFY
)
1154 else if (p
->change_lma
== CHANGE_SET
)
1160 lma
+= change_section_address
;
1162 osection
->lma
= lma
;
1164 /* FIXME: This is probably not enough. If we change the LMA we
1165 may have to recompute the header for the file as well. */
1166 if (bfd_set_section_alignment (obfd
,
1168 bfd_section_alignment (ibfd
, isection
))
1175 flags
= bfd_get_section_flags (ibfd
, isection
);
1176 if (p
!= NULL
&& p
->set_flags
)
1177 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1178 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1184 /* This used to be mangle_section; we do here to avoid using
1185 bfd_get_section_by_name since some formats allow multiple
1186 sections with the same name. */
1187 isection
->output_section
= osection
;
1188 isection
->output_offset
= 0;
1190 /* Allow the BFD backend to copy any private data it understands
1191 from the input section to the output section. */
1192 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1194 err
= "private data";
1202 non_fatal (_("%s: section `%s': error in %s: %s"),
1203 bfd_get_filename (ibfd
),
1204 bfd_section_name (ibfd
, isection
),
1205 err
, bfd_errmsg (bfd_get_error ()));
1209 /* Copy the data of input section ISECTION of IBFD
1210 to an output section with the same name in OBFD.
1211 If stripping then don't copy any relocation info. */
1214 copy_section (ibfd
, isection
, obfdarg
)
1219 bfd
*obfd
= (bfd
*) obfdarg
;
1220 struct section_list
*p
;
1226 /* If we have already failed earlier on, do not keep on generating
1231 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1232 && (strip_symbols
== STRIP_DEBUG
1233 || strip_symbols
== STRIP_UNNEEDED
1234 || strip_symbols
== STRIP_ALL
1235 || discard_locals
== LOCALS_ALL
1236 || convert_debugging
))
1241 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1243 if (p
!= NULL
&& p
->remove
)
1246 osection
= isection
->output_section
;
1247 size
= bfd_get_section_size_before_reloc (isection
);
1249 if (size
== 0 || osection
== 0)
1252 if (strip_symbols
== STRIP_ALL
)
1253 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1258 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1260 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1263 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1266 relpp
= (arelent
**) xmalloc (relsize
);
1267 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1269 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1271 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1275 isection
->_cooked_size
= isection
->_raw_size
;
1276 isection
->reloc_done
= true;
1278 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1280 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1282 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1284 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1288 filter_bytes (memhunk
, &size
);
1289 /* The section has gotten smaller. */
1290 if (!bfd_set_section_size (obfd
, osection
, size
))
1291 RETURN_NONFATAL (bfd_get_filename (obfd
));
1294 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1296 RETURN_NONFATAL (bfd_get_filename (obfd
));
1300 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1302 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1304 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1305 flag--they can just remove the section entirely and add it
1306 back again. However, we do permit them to turn on the
1307 SEC_HAS_CONTENTS flag, and take it to mean that the section
1308 contents should be zeroed out. */
1310 memset (memhunk
, 0, size
);
1311 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1313 RETURN_NONFATAL (bfd_get_filename (obfd
));
1318 /* Get all the sections. This is used when --gap-fill or --pad-to is
1322 get_sections (obfd
, osection
, secppparg
)
1327 asection
***secppp
= (asection
***) secppparg
;
1329 **secppp
= osection
;
1333 /* Sort sections by VMA. This is called via qsort, and is used when
1334 --gap-fill or --pad-to is used. We force non loadable or empty
1335 sections to the front, where they are easier to ignore. */
1338 compare_section_lma (arg1
, arg2
)
1342 const asection
**sec1
= (const asection
**) arg1
;
1343 const asection
**sec2
= (const asection
**) arg2
;
1344 flagword flags1
, flags2
;
1346 /* Sort non loadable sections to the front. */
1347 flags1
= (*sec1
)->flags
;
1348 flags2
= (*sec2
)->flags
;
1349 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1350 || (flags1
& SEC_LOAD
) == 0)
1352 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1353 && (flags2
& SEC_LOAD
) != 0)
1358 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1359 || (flags2
& SEC_LOAD
) == 0)
1363 /* Sort sections by LMA. */
1364 if ((*sec1
)->lma
> (*sec2
)->lma
)
1366 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1369 /* Sort sections with the same LMA by size. */
1370 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1372 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1378 /* Mark all the symbols which will be used in output relocations with
1379 the BSF_KEEP flag so that those symbols will not be stripped.
1381 Ignore relocations which will not appear in the output file. */
1384 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1389 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1394 /* Ignore an input section with no corresponding output section. */
1395 if (isection
->output_section
== NULL
)
1398 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1400 bfd_fatal (bfd_get_filename (ibfd
));
1405 relpp
= (arelent
**) xmalloc (relsize
);
1406 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1408 bfd_fatal (bfd_get_filename (ibfd
));
1410 /* Examine each symbol used in a relocation. If it's not one of the
1411 special bfd section symbols, then mark it with BSF_KEEP. */
1412 for (i
= 0; i
< relcount
; i
++)
1414 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1415 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1416 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1417 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1424 /* Write out debugging information. */
1427 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1433 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1434 return write_ieee_debugging_info (obfd
, dhandle
);
1436 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1437 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1439 bfd_byte
*syms
, *strings
;
1440 bfd_size_type symsize
, stringsize
;
1441 asection
*stabsec
, *stabstrsec
;
1443 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1448 stabsec
= bfd_make_section (obfd
, ".stab");
1449 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1451 || stabstrsec
== NULL
1452 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1453 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1454 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1455 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1456 || ! bfd_set_section_flags (obfd
, stabsec
,
1460 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1465 non_fatal (_("%s: can't create debugging section: %s"),
1466 bfd_get_filename (obfd
),
1467 bfd_errmsg (bfd_get_error ()));
1471 /* We can get away with setting the section contents now because
1472 the next thing the caller is going to do is copy over the
1473 real sections. We may someday have to split the contents
1474 setting out of this function. */
1475 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1477 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1478 (file_ptr
) 0, stringsize
))
1480 non_fatal (_("%s: can't set debugging section contents: %s"),
1481 bfd_get_filename (obfd
),
1482 bfd_errmsg (bfd_get_error ()));
1489 non_fatal (_("%s: don't know how to write debugging information for %s"),
1490 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1494 /* The number of bytes to copy at once. */
1495 #define COPY_BUF 8192
1497 /* Copy file FROM to file TO, performing no translations.
1498 Return 0 if ok, -1 if error. */
1501 simple_copy (from
, to
)
1505 int fromfd
, tofd
, nread
;
1509 fromfd
= open (from
, O_RDONLY
);
1512 tofd
= creat (to
, 0777);
1520 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1522 if (write (tofd
, buf
, nread
) != nread
)
1544 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1546 #define S_ISLNK(m) 0
1551 /* Rename FROM to TO, copying if TO is a link.
1552 Assumes that TO already exists, because FROM is a temp file.
1553 Return 0 if ok, -1 if error. */
1556 smart_rename (from
, to
)
1566 #if defined (_WIN32) && !defined (__CYGWIN32__)
1567 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1568 fail instead. Also, chown is not present. */
1570 if (stat (to
, &s
) == 0)
1573 ret
= rename (from
, to
);
1576 /* We have to clean up here. */
1578 non_fatal (_("%s: rename: %s"), to
, strerror (errno
));
1582 /* Use rename only if TO is not a symbolic link and has
1583 only one hard link. */
1584 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1586 ret
= rename (from
, to
);
1589 /* Try to preserve the permission bits and ownership of TO.
1590 First get the mode right except for the setuid bit. Then
1591 change the ownership. Then fix the setuid bit. We do
1592 the chmod before the chown because if the chown succeeds,
1593 and we are a normal user, we won't be able to do the
1594 chmod afterward. We don't bother to fix the setuid bit
1595 first because that might introduce a fleeting security
1596 problem, and because the chown will clear the setuid bit
1597 anyhow. We only fix the setuid bit if the chown
1598 succeeds, because we don't want to introduce an
1599 unexpected setuid file owned by the user running objcopy. */
1600 chmod (to
, s
.st_mode
& 0777);
1601 if (chown (to
, s
.st_uid
, s
.st_gid
) >= 0)
1602 chmod (to
, s
.st_mode
& 07777);
1606 /* We have to clean up here. */
1607 non_fatal (_("%s: rename: %s"), to
, strerror (errno
));
1613 ret
= simple_copy (from
, to
);
1615 non_fatal (_("%s: simple_copy: %s"), to
, strerror (errno
));
1621 #endif /* _WIN32 && !__CYGWIN32__ */
1626 /* Set the times of the file DESTINATION to be the same as those in
1630 set_times (destination
, statbuf
)
1631 const char *destination
;
1632 const struct stat
*statbuf
;
1637 #ifdef HAVE_GOOD_UTIME_H
1640 tb
.actime
= statbuf
->st_atime
;
1641 tb
.modtime
= statbuf
->st_mtime
;
1642 result
= utime (destination
, &tb
);
1643 #else /* ! HAVE_GOOD_UTIME_H */
1647 tb
[0] = statbuf
->st_atime
;
1648 tb
[1] = statbuf
->st_mtime
;
1649 result
= utime (destination
, tb
);
1650 #else /* HAVE_UTIMES */
1651 struct timeval tv
[2];
1653 tv
[0].tv_sec
= statbuf
->st_atime
;
1655 tv
[1].tv_sec
= statbuf
->st_mtime
;
1657 result
= utimes (destination
, tv
);
1658 #endif /* HAVE_UTIMES */
1659 #endif /* ! HAVE_GOOD_UTIME_H */
1663 non_fatal (_("%s: cannot set time: %s"), destination
, strerror (errno
));
1667 strip_main (argc
, argv
)
1671 char *input_target
= NULL
, *output_target
= NULL
;
1672 boolean show_version
= false;
1674 struct section_list
*p
;
1675 char *output_file
= NULL
;
1677 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1678 strip_options
, (int *) 0)) != EOF
)
1683 input_target
= optarg
;
1686 output_target
= optarg
;
1689 input_target
= output_target
= optarg
;
1692 p
= find_section_list (optarg
, true);
1694 sections_removed
= true;
1697 strip_symbols
= STRIP_ALL
;
1701 strip_symbols
= STRIP_DEBUG
;
1703 case OPTION_STRIP_UNNEEDED
:
1704 strip_symbols
= STRIP_UNNEEDED
;
1707 add_specific_symbol (optarg
, &keep_specific_list
);
1710 add_specific_symbol (optarg
, &strip_specific_list
);
1713 output_file
= optarg
;
1716 preserve_dates
= true;
1719 discard_locals
= LOCALS_ALL
;
1722 discard_locals
= LOCALS_START_L
;
1728 show_version
= true;
1731 break; /* we've been given a long option */
1733 strip_usage (stdout
, 0);
1735 strip_usage (stderr
, 1);
1740 print_version ("strip");
1742 /* Default is to strip all symbols. */
1743 if (strip_symbols
== STRIP_UNDEF
1744 && discard_locals
== LOCALS_UNDEF
1745 && strip_specific_list
== NULL
)
1746 strip_symbols
= STRIP_ALL
;
1748 if (output_target
== (char *) NULL
)
1749 output_target
= input_target
;
1753 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1754 strip_usage (stderr
, 1);
1756 for (; i
< argc
; i
++)
1758 int hold_status
= status
;
1759 struct stat statbuf
;
1764 if (stat (argv
[i
], &statbuf
) < 0)
1766 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
1771 if (output_file
!= NULL
)
1772 tmpname
= output_file
;
1774 tmpname
= make_tempname (argv
[i
]);
1777 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1781 set_times (tmpname
, &statbuf
);
1782 if (output_file
== NULL
)
1783 smart_rename (tmpname
, argv
[i
]);
1784 status
= hold_status
;
1788 if (output_file
== NULL
)
1796 copy_main (argc
, argv
)
1800 char *input_filename
= NULL
, *output_filename
= NULL
;
1801 char *input_target
= NULL
, *output_target
= NULL
;
1802 boolean show_version
= false;
1803 boolean change_warn
= true;
1805 struct section_list
*p
;
1806 struct stat statbuf
;
1808 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1809 copy_options
, (int *) 0)) != EOF
)
1814 copy_byte
= atoi(optarg
);
1816 fatal (_("byte number must be non-negative"));
1819 interleave
= atoi(optarg
);
1821 fatal (_("interleave must be positive"));
1824 case 's': /* "source" - 'I' is preferred */
1825 input_target
= optarg
;
1828 case 'd': /* "destination" - 'O' is preferred */
1829 output_target
= optarg
;
1832 input_target
= output_target
= optarg
;
1835 p
= find_section_list (optarg
, true);
1837 sections_removed
= true;
1840 strip_symbols
= STRIP_ALL
;
1843 strip_symbols
= STRIP_DEBUG
;
1845 case OPTION_STRIP_UNNEEDED
:
1846 strip_symbols
= STRIP_UNNEEDED
;
1849 add_specific_symbol (optarg
, &keep_specific_list
);
1852 add_specific_symbol (optarg
, &strip_specific_list
);
1855 add_specific_symbol (optarg
, &localize_specific_list
);
1858 add_specific_symbol (optarg
, &weaken_specific_list
);
1861 preserve_dates
= true;
1864 discard_locals
= LOCALS_ALL
;
1867 discard_locals
= LOCALS_START_L
;
1873 show_version
= true;
1878 case OPTION_ADD_SECTION
:
1882 struct section_add
*pa
;
1887 s
= strchr (optarg
, '=');
1890 fatal (_("bad format for --add-section NAME=FILENAME"));
1892 if (stat (s
+ 1, & st
) < 0)
1893 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
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 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
1913 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1915 fatal (_("%s: fread failed"), pa
->filename
);
1919 pa
->next
= add_sections
;
1923 case OPTION_CHANGE_START
:
1924 change_start
= parse_vma (optarg
, "--change-start");
1926 case OPTION_CHANGE_SECTION_ADDRESS
:
1927 case OPTION_CHANGE_SECTION_LMA
:
1928 case OPTION_CHANGE_SECTION_VMA
:
1935 enum change_action what
;
1939 case OPTION_CHANGE_SECTION_ADDRESS
: option
= "--change-section-address"; break;
1940 case OPTION_CHANGE_SECTION_LMA
: option
= "--change-section-lma"; break;
1941 case OPTION_CHANGE_SECTION_VMA
: option
= "--change-section-vma"; break;
1944 s
= strchr (optarg
, '=');
1947 s
= strchr (optarg
, '+');
1950 s
= strchr (optarg
, '-');
1952 fatal (_("bad format for %s"), option
);
1957 name
= (char *) xmalloc (len
+ 1);
1958 strncpy (name
, optarg
, len
);
1961 p
= find_section_list (name
, true);
1963 val
= parse_vma (s
+ 1, option
);
1967 case '=': what
= CHANGE_SET
; break;
1968 case '-': val
= - val
; /* Drop through. */
1969 case '+': what
= CHANGE_MODIFY
; break;
1974 case OPTION_CHANGE_SECTION_ADDRESS
:
1975 p
->change_vma
= what
;
1979 case OPTION_CHANGE_SECTION_LMA
:
1980 p
->change_lma
= what
;
1984 case OPTION_CHANGE_SECTION_VMA
:
1985 p
->change_vma
= what
;
1991 case OPTION_CHANGE_ADDRESSES
:
1992 change_section_address
= parse_vma (optarg
, "--change-addresses");
1993 change_start
= change_section_address
;
1995 case OPTION_CHANGE_WARNINGS
:
1998 case OPTION_CHANGE_LEADING_CHAR
:
1999 change_leading_char
= true;
2001 case OPTION_DEBUGGING
:
2002 convert_debugging
= true;
2004 case OPTION_GAP_FILL
:
2006 bfd_vma gap_fill_vma
;
2008 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2009 gap_fill
= (bfd_byte
) gap_fill_vma
;
2010 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2014 sprintf_vma (buff
, gap_fill_vma
);
2016 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2019 gap_fill_set
= true;
2022 case OPTION_NO_CHANGE_WARNINGS
:
2023 change_warn
= false;
2026 pad_to
= parse_vma (optarg
, "--pad-to");
2029 case OPTION_REMOVE_LEADING_CHAR
:
2030 remove_leading_char
= true;
2032 case OPTION_SET_SECTION_FLAGS
:
2038 s
= strchr (optarg
, '=');
2040 fatal (_("bad format for --set-section-flags"));
2043 name
= (char *) xmalloc (len
+ 1);
2044 strncpy (name
, optarg
, len
);
2047 p
= find_section_list (name
, true);
2049 p
->set_flags
= true;
2050 p
->flags
= parse_flags (s
+ 1);
2053 case OPTION_SET_START
:
2054 set_start
= parse_vma (optarg
, "--set-start");
2055 set_start_set
= true;
2058 break; /* we've been given a long option */
2060 copy_usage (stdout
, 0);
2062 copy_usage (stderr
, 1);
2067 print_version ("objcopy");
2069 if (copy_byte
>= interleave
)
2070 fatal (_("byte number must be less than interleave"));
2072 if (optind
== argc
|| optind
+ 2 < argc
)
2073 copy_usage (stderr
, 1);
2075 input_filename
= argv
[optind
];
2076 if (optind
+ 1 < argc
)
2077 output_filename
= argv
[optind
+ 1];
2079 /* Default is to strip no symbols. */
2080 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2081 strip_symbols
= STRIP_NONE
;
2083 if (output_target
== (char *) NULL
)
2084 output_target
= input_target
;
2088 if (stat (input_filename
, &statbuf
) < 0)
2089 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2092 /* If there is no destination file then create a temp and rename
2093 the result into the input. */
2095 if (output_filename
== (char *) NULL
)
2097 char *tmpname
= make_tempname (input_filename
);
2099 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2103 set_times (tmpname
, &statbuf
);
2104 smart_rename (tmpname
, input_filename
);
2111 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2112 if (status
== 0 && preserve_dates
)
2113 set_times (output_filename
, &statbuf
);
2118 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2122 if (p
->change_vma
!= CHANGE_IGNORE
)
2126 sprintf_vma (buff
, p
->vma_val
);
2128 /* xgettext:c-format */
2129 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
2131 p
->change_vma
== CHANGE_SET
? '=' : '+',
2135 if (p
->change_lma
!= CHANGE_IGNORE
)
2139 sprintf_vma (buff
, p
->lma_val
);
2141 /* xgettext:c-format */
2142 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
2144 p
->change_lma
== CHANGE_SET
? '=' : '+',
2159 #ifdef HAVE_SETLOCALE
2160 setlocale (LC_MESSAGES
, "");
2162 bindtextdomain (PACKAGE
, LOCALEDIR
);
2163 textdomain (PACKAGE
);
2165 program_name
= argv
[0];
2166 xmalloc_set_program_name (program_name
);
2168 START_PROGRESS (program_name
, 0);
2170 strip_symbols
= STRIP_UNDEF
;
2171 discard_locals
= LOCALS_UNDEF
;
2174 set_default_bfd_target ();
2178 int i
= strlen (program_name
);
2179 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2183 strip_main (argc
, argv
);
2185 copy_main (argc
, argv
);
2187 END_PROGRESS (program_name
);