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\
313 [--set-start=val] \n\
314 [--change-start=incr] [--change-addresses=incr] \n\
315 (--adjust-start and --adjust-vma are aliases for these two) \n\
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 (!status
&& 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 if (!bfd_close (output_bfd
))
988 bfd_nonfatal (bfd_get_filename (output_bfd
));
989 /* Error in new object file. Don't change archive. */
993 /* Open the newly output file and attach to our list. */
994 output_bfd
= bfd_openr (output_name
, output_target
);
996 l
->obfd
= output_bfd
;
999 ptr
= &output_bfd
->next
;
1001 last_element
= this_element
;
1003 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1005 bfd_close (last_element
);
1007 *ptr
= (bfd
*) NULL
;
1009 if (!bfd_close (obfd
))
1010 RETURN_NONFATAL (bfd_get_filename (obfd
));
1012 if (!bfd_close (ibfd
))
1013 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1015 /* Delete all the files that we opened. */
1016 for (l
= list
; l
!= NULL
; l
= l
->next
)
1018 bfd_close (l
->obfd
);
1024 /* The top-level control. */
1027 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1028 const char *input_filename
;
1029 const char *output_filename
;
1030 const char *input_target
;
1031 const char *output_target
;
1036 /* To allow us to do "strip *" without dying on the first
1037 non-object file, failures are nonfatal. */
1039 ibfd
= bfd_openr (input_filename
, input_target
);
1041 RETURN_NONFATAL (input_filename
);
1043 if (bfd_check_format (ibfd
, bfd_archive
))
1047 /* bfd_get_target does not return the correct value until
1048 bfd_check_format succeeds. */
1049 if (output_target
== NULL
)
1050 output_target
= bfd_get_target (ibfd
);
1052 obfd
= bfd_openw (output_filename
, output_target
);
1054 RETURN_NONFATAL (output_filename
);
1056 copy_archive (ibfd
, obfd
, output_target
);
1058 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1062 /* bfd_get_target does not return the correct value until
1063 bfd_check_format succeeds. */
1064 if (output_target
== NULL
)
1065 output_target
= bfd_get_target (ibfd
);
1067 obfd
= bfd_openw (output_filename
, output_target
);
1069 RETURN_NONFATAL (output_filename
);
1071 copy_object (ibfd
, obfd
);
1073 if (!bfd_close (obfd
))
1074 RETURN_NONFATAL (output_filename
);
1076 if (!bfd_close (ibfd
))
1077 RETURN_NONFATAL (input_filename
);
1081 bfd_nonfatal (input_filename
);
1083 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1085 list_matching_formats (matching
);
1093 /* Create a section in OBFD with the same name and attributes
1094 as ISECTION in IBFD. */
1097 setup_section (ibfd
, isection
, obfdarg
)
1102 bfd
*obfd
= (bfd
*) obfdarg
;
1103 struct section_list
*p
;
1111 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1112 && (strip_symbols
== STRIP_DEBUG
1113 || strip_symbols
== STRIP_UNNEEDED
1114 || strip_symbols
== STRIP_ALL
1115 || discard_locals
== LOCALS_ALL
1116 || convert_debugging
))
1119 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1123 if (p
!= NULL
&& p
->remove
)
1126 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1127 if (osection
== NULL
)
1133 size
= bfd_section_size (ibfd
, isection
);
1135 size
= (size
+ interleave
- 1) / interleave
;
1136 if (! bfd_set_section_size (obfd
, osection
, size
))
1142 vma
= bfd_section_vma (ibfd
, isection
);
1143 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1145 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1148 vma
+= change_section_address
;
1150 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1156 lma
= isection
->lma
;
1157 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1159 if (p
->change_lma
== CHANGE_MODIFY
)
1161 else if (p
->change_lma
== CHANGE_SET
)
1167 lma
+= change_section_address
;
1169 osection
->lma
= lma
;
1171 /* FIXME: This is probably not enough. If we change the LMA we
1172 may have to recompute the header for the file as well. */
1173 if (bfd_set_section_alignment (obfd
,
1175 bfd_section_alignment (ibfd
, isection
))
1182 flags
= bfd_get_section_flags (ibfd
, isection
);
1183 if (p
!= NULL
&& p
->set_flags
)
1184 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1185 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1191 /* This used to be mangle_section; we do here to avoid using
1192 bfd_get_section_by_name since some formats allow multiple
1193 sections with the same name. */
1194 isection
->output_section
= osection
;
1195 isection
->output_offset
= 0;
1197 /* Allow the BFD backend to copy any private data it understands
1198 from the input section to the output section. */
1199 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1201 err
= "private data";
1209 non_fatal (_("%s: section `%s': error in %s: %s"),
1210 bfd_get_filename (ibfd
),
1211 bfd_section_name (ibfd
, isection
),
1212 err
, bfd_errmsg (bfd_get_error ()));
1216 /* Copy the data of input section ISECTION of IBFD
1217 to an output section with the same name in OBFD.
1218 If stripping then don't copy any relocation info. */
1221 copy_section (ibfd
, isection
, obfdarg
)
1226 bfd
*obfd
= (bfd
*) obfdarg
;
1227 struct section_list
*p
;
1233 /* If we have already failed earlier on, do not keep on generating
1238 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1239 && (strip_symbols
== STRIP_DEBUG
1240 || strip_symbols
== STRIP_UNNEEDED
1241 || strip_symbols
== STRIP_ALL
1242 || discard_locals
== LOCALS_ALL
1243 || convert_debugging
))
1248 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1250 if (p
!= NULL
&& p
->remove
)
1253 osection
= isection
->output_section
;
1254 size
= bfd_get_section_size_before_reloc (isection
);
1256 if (size
== 0 || osection
== 0)
1259 if (strip_symbols
== STRIP_ALL
)
1260 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1265 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1267 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1270 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1273 relpp
= (arelent
**) xmalloc (relsize
);
1274 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1276 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1278 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1282 isection
->_cooked_size
= isection
->_raw_size
;
1283 isection
->reloc_done
= true;
1285 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1287 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1289 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1291 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1294 filter_bytes (memhunk
, &size
);
1296 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1298 RETURN_NONFATAL (bfd_get_filename (obfd
));
1302 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1304 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1306 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1307 flag--they can just remove the section entirely and add it
1308 back again. However, we do permit them to turn on the
1309 SEC_HAS_CONTENTS flag, and take it to mean that the section
1310 contents should be zeroed out. */
1312 memset (memhunk
, 0, size
);
1313 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1315 RETURN_NONFATAL (bfd_get_filename (obfd
));
1320 /* Get all the sections. This is used when --gap-fill or --pad-to is
1324 get_sections (obfd
, osection
, secppparg
)
1329 asection
***secppp
= (asection
***) secppparg
;
1331 **secppp
= osection
;
1335 /* Sort sections by VMA. This is called via qsort, and is used when
1336 --gap-fill or --pad-to is used. We force non loadable or empty
1337 sections to the front, where they are easier to ignore. */
1340 compare_section_lma (arg1
, arg2
)
1344 const asection
**sec1
= (const asection
**) arg1
;
1345 const asection
**sec2
= (const asection
**) arg2
;
1346 flagword flags1
, flags2
;
1348 /* Sort non loadable sections to the front. */
1349 flags1
= (*sec1
)->flags
;
1350 flags2
= (*sec2
)->flags
;
1351 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1352 || (flags1
& SEC_LOAD
) == 0)
1354 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1355 && (flags2
& SEC_LOAD
) != 0)
1360 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1361 || (flags2
& SEC_LOAD
) == 0)
1365 /* Sort sections by LMA. */
1366 if ((*sec1
)->lma
> (*sec2
)->lma
)
1368 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1371 /* Sort sections with the same LMA by size. */
1372 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1374 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1380 /* Mark all the symbols which will be used in output relocations with
1381 the BSF_KEEP flag so that those symbols will not be stripped.
1383 Ignore relocations which will not appear in the output file. */
1386 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1391 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1396 /* Ignore an input section with no corresponding output section. */
1397 if (isection
->output_section
== NULL
)
1400 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1402 bfd_fatal (bfd_get_filename (ibfd
));
1407 relpp
= (arelent
**) xmalloc (relsize
);
1408 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1410 bfd_fatal (bfd_get_filename (ibfd
));
1412 /* Examine each symbol used in a relocation. If it's not one of the
1413 special bfd section symbols, then mark it with BSF_KEEP. */
1414 for (i
= 0; i
< relcount
; i
++)
1416 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1417 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1418 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1419 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1426 /* Write out debugging information. */
1429 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1435 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1436 return write_ieee_debugging_info (obfd
, dhandle
);
1438 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1439 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1441 bfd_byte
*syms
, *strings
;
1442 bfd_size_type symsize
, stringsize
;
1443 asection
*stabsec
, *stabstrsec
;
1445 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1450 stabsec
= bfd_make_section (obfd
, ".stab");
1451 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1453 || stabstrsec
== NULL
1454 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1455 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1456 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1457 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1458 || ! bfd_set_section_flags (obfd
, stabsec
,
1462 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1467 non_fatal (_("%s: can't create debugging section: %s"),
1468 bfd_get_filename (obfd
),
1469 bfd_errmsg (bfd_get_error ()));
1473 /* We can get away with setting the section contents now because
1474 the next thing the caller is going to do is copy over the
1475 real sections. We may someday have to split the contents
1476 setting out of this function. */
1477 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1479 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1480 (file_ptr
) 0, stringsize
))
1482 non_fatal (_("%s: can't set debugging section contents: %s"),
1483 bfd_get_filename (obfd
),
1484 bfd_errmsg (bfd_get_error ()));
1491 non_fatal (_("%s: don't know how to write debugging information for %s"),
1492 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1496 /* The number of bytes to copy at once. */
1497 #define COPY_BUF 8192
1499 /* Copy file FROM to file TO, performing no translations.
1500 Return 0 if ok, -1 if error. */
1503 simple_copy (from
, to
)
1507 int fromfd
, tofd
, nread
;
1511 fromfd
= open (from
, O_RDONLY
);
1514 tofd
= creat (to
, 0777);
1522 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1524 if (write (tofd
, buf
, nread
) != nread
)
1546 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1548 #define S_ISLNK(m) 0
1553 /* Rename FROM to TO, copying if TO is a link.
1554 Assumes that TO already exists, because FROM is a temp file.
1555 Return 0 if ok, -1 if error. */
1558 smart_rename (from
, to
)
1568 #if defined (_WIN32) && !defined (__CYGWIN32__)
1569 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1570 fail instead. Also, chown is not present. */
1572 if (stat (to
, &s
) == 0)
1575 ret
= rename (from
, to
);
1578 /* We have to clean up here. */
1580 non_fatal (_("%s: rename: %s"), to
, strerror (errno
));
1584 /* Use rename only if TO is not a symbolic link and has
1585 only one hard link. */
1586 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1588 ret
= rename (from
, to
);
1591 /* Try to preserve the permission bits and ownership of TO.
1592 First get the mode right except for the setuid bit. Then
1593 change the ownership. Then fix the setuid bit. We do
1594 the chmod before the chown because if the chown succeeds,
1595 and we are a normal user, we won't be able to do the
1596 chmod afterward. We don't bother to fix the setuid bit
1597 first because that might introduce a fleeting security
1598 problem, and because the chown will clear the setuid bit
1599 anyhow. We only fix the setuid bit if the chown
1600 succeeds, because we don't want to introduce an
1601 unexpected setuid file owned by the user running objcopy. */
1602 chmod (to
, s
.st_mode
& 0777);
1603 if (chown (to
, s
.st_uid
, s
.st_gid
) >= 0)
1604 chmod (to
, s
.st_mode
& 07777);
1608 /* We have to clean up here. */
1609 non_fatal (_("%s: rename: %s"), to
, strerror (errno
));
1615 ret
= simple_copy (from
, to
);
1617 non_fatal (_("%s: simple_copy: %s"), to
, strerror (errno
));
1623 #endif /* _WIN32 && !__CYGWIN32__ */
1628 /* Set the times of the file DESTINATION to be the same as those in
1632 set_times (destination
, statbuf
)
1633 const char *destination
;
1634 const struct stat
*statbuf
;
1639 #ifdef HAVE_GOOD_UTIME_H
1642 tb
.actime
= statbuf
->st_atime
;
1643 tb
.modtime
= statbuf
->st_mtime
;
1644 result
= utime (destination
, &tb
);
1645 #else /* ! HAVE_GOOD_UTIME_H */
1649 tb
[0] = statbuf
->st_atime
;
1650 tb
[1] = statbuf
->st_mtime
;
1651 result
= utime (destination
, tb
);
1652 #else /* HAVE_UTIMES */
1653 struct timeval tv
[2];
1655 tv
[0].tv_sec
= statbuf
->st_atime
;
1657 tv
[1].tv_sec
= statbuf
->st_mtime
;
1659 result
= utimes (destination
, tv
);
1660 #endif /* HAVE_UTIMES */
1661 #endif /* ! HAVE_GOOD_UTIME_H */
1665 non_fatal (_("%s: cannot set time: %s"), destination
, strerror (errno
));
1669 strip_main (argc
, argv
)
1673 char *input_target
= NULL
, *output_target
= NULL
;
1674 boolean show_version
= false;
1676 struct section_list
*p
;
1677 char *output_file
= NULL
;
1679 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1680 strip_options
, (int *) 0)) != EOF
)
1685 input_target
= optarg
;
1688 output_target
= optarg
;
1691 input_target
= output_target
= optarg
;
1694 p
= find_section_list (optarg
, true);
1696 sections_removed
= true;
1699 strip_symbols
= STRIP_ALL
;
1703 strip_symbols
= STRIP_DEBUG
;
1705 case OPTION_STRIP_UNNEEDED
:
1706 strip_symbols
= STRIP_UNNEEDED
;
1709 add_specific_symbol (optarg
, &keep_specific_list
);
1712 add_specific_symbol (optarg
, &strip_specific_list
);
1715 output_file
= optarg
;
1718 preserve_dates
= true;
1721 discard_locals
= LOCALS_ALL
;
1724 discard_locals
= LOCALS_START_L
;
1730 show_version
= true;
1733 break; /* we've been given a long option */
1735 strip_usage (stdout
, 0);
1737 strip_usage (stderr
, 1);
1742 print_version ("strip");
1744 /* Default is to strip all symbols. */
1745 if (strip_symbols
== STRIP_UNDEF
1746 && discard_locals
== LOCALS_UNDEF
1747 && strip_specific_list
== NULL
1748 && keep_specific_list
== NULL
)
1749 strip_symbols
= STRIP_ALL
;
1751 if (output_target
== (char *) NULL
)
1752 output_target
= input_target
;
1756 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1757 strip_usage (stderr
, 1);
1759 for (; i
< argc
; i
++)
1761 int hold_status
= status
;
1762 struct stat statbuf
;
1767 if (stat (argv
[i
], &statbuf
) < 0)
1769 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
1774 if (output_file
!= NULL
)
1775 tmpname
= output_file
;
1777 tmpname
= make_tempname (argv
[i
]);
1780 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1784 set_times (tmpname
, &statbuf
);
1785 if (output_file
== NULL
)
1786 smart_rename (tmpname
, argv
[i
]);
1787 status
= hold_status
;
1791 if (output_file
== NULL
)
1799 copy_main (argc
, argv
)
1803 char *input_filename
= NULL
, *output_filename
= NULL
;
1804 char *input_target
= NULL
, *output_target
= NULL
;
1805 boolean show_version
= false;
1806 boolean change_warn
= true;
1808 struct section_list
*p
;
1809 struct stat statbuf
;
1811 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1812 copy_options
, (int *) 0)) != EOF
)
1817 copy_byte
= atoi(optarg
);
1819 fatal (_("byte number must be non-negative"));
1822 interleave
= atoi(optarg
);
1824 fatal (_("interleave must be positive"));
1827 case 's': /* "source" - 'I' is preferred */
1828 input_target
= optarg
;
1831 case 'd': /* "destination" - 'O' is preferred */
1832 output_target
= optarg
;
1835 input_target
= output_target
= optarg
;
1838 p
= find_section_list (optarg
, true);
1840 sections_removed
= true;
1843 strip_symbols
= STRIP_ALL
;
1846 strip_symbols
= STRIP_DEBUG
;
1848 case OPTION_STRIP_UNNEEDED
:
1849 strip_symbols
= STRIP_UNNEEDED
;
1852 add_specific_symbol (optarg
, &keep_specific_list
);
1855 add_specific_symbol (optarg
, &strip_specific_list
);
1858 add_specific_symbol (optarg
, &localize_specific_list
);
1861 add_specific_symbol (optarg
, &weaken_specific_list
);
1864 preserve_dates
= true;
1867 discard_locals
= LOCALS_ALL
;
1870 discard_locals
= LOCALS_START_L
;
1876 show_version
= true;
1881 case OPTION_ADD_SECTION
:
1885 struct section_add
*pa
;
1890 s
= strchr (optarg
, '=');
1893 fatal (_("bad format for --add-section NAME=FILENAME"));
1895 if (stat (s
+ 1, & st
) < 0)
1896 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
1898 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1901 name
= (char *) xmalloc (len
+ 1);
1902 strncpy (name
, optarg
, len
);
1906 pa
->filename
= s
+ 1;
1908 pa
->size
= st
.st_size
;
1910 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1911 f
= fopen (pa
->filename
, FOPEN_RB
);
1914 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
1916 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1918 fatal (_("%s: fread failed"), pa
->filename
);
1922 pa
->next
= add_sections
;
1926 case OPTION_CHANGE_START
:
1927 change_start
= parse_vma (optarg
, "--change-start");
1929 case OPTION_CHANGE_SECTION_ADDRESS
:
1930 case OPTION_CHANGE_SECTION_LMA
:
1931 case OPTION_CHANGE_SECTION_VMA
:
1938 enum change_action what
;
1942 case OPTION_CHANGE_SECTION_ADDRESS
: option
= "--change-section-address"; break;
1943 case OPTION_CHANGE_SECTION_LMA
: option
= "--change-section-lma"; break;
1944 case OPTION_CHANGE_SECTION_VMA
: option
= "--change-section-vma"; break;
1947 s
= strchr (optarg
, '=');
1950 s
= strchr (optarg
, '+');
1953 s
= strchr (optarg
, '-');
1955 fatal (_("bad format for %s"), option
);
1960 name
= (char *) xmalloc (len
+ 1);
1961 strncpy (name
, optarg
, len
);
1964 p
= find_section_list (name
, true);
1966 val
= parse_vma (s
+ 1, option
);
1970 case '=': what
= CHANGE_SET
; break;
1971 case '-': val
= - val
; /* Drop through. */
1972 case '+': what
= CHANGE_MODIFY
; break;
1977 case OPTION_CHANGE_SECTION_ADDRESS
:
1978 p
->change_vma
= what
;
1982 case OPTION_CHANGE_SECTION_LMA
:
1983 p
->change_lma
= what
;
1987 case OPTION_CHANGE_SECTION_VMA
:
1988 p
->change_vma
= what
;
1994 case OPTION_CHANGE_ADDRESSES
:
1995 change_section_address
= parse_vma (optarg
, "--change-addresses");
1996 change_start
= change_section_address
;
1998 case OPTION_CHANGE_WARNINGS
:
2001 case OPTION_CHANGE_LEADING_CHAR
:
2002 change_leading_char
= true;
2004 case OPTION_DEBUGGING
:
2005 convert_debugging
= true;
2007 case OPTION_GAP_FILL
:
2009 bfd_vma gap_fill_vma
;
2011 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2012 gap_fill
= (bfd_byte
) gap_fill_vma
;
2013 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2017 sprintf_vma (buff
, gap_fill_vma
);
2019 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2022 gap_fill_set
= true;
2025 case OPTION_NO_CHANGE_WARNINGS
:
2026 change_warn
= false;
2029 pad_to
= parse_vma (optarg
, "--pad-to");
2032 case OPTION_REMOVE_LEADING_CHAR
:
2033 remove_leading_char
= true;
2035 case OPTION_SET_SECTION_FLAGS
:
2041 s
= strchr (optarg
, '=');
2043 fatal (_("bad format for --set-section-flags"));
2046 name
= (char *) xmalloc (len
+ 1);
2047 strncpy (name
, optarg
, len
);
2050 p
= find_section_list (name
, true);
2052 p
->set_flags
= true;
2053 p
->flags
= parse_flags (s
+ 1);
2056 case OPTION_SET_START
:
2057 set_start
= parse_vma (optarg
, "--set-start");
2058 set_start_set
= true;
2061 break; /* we've been given a long option */
2063 copy_usage (stdout
, 0);
2065 copy_usage (stderr
, 1);
2070 print_version ("objcopy");
2072 if (copy_byte
>= interleave
)
2073 fatal (_("byte number must be less than interleave"));
2075 if (optind
== argc
|| optind
+ 2 < argc
)
2076 copy_usage (stderr
, 1);
2078 input_filename
= argv
[optind
];
2079 if (optind
+ 1 < argc
)
2080 output_filename
= argv
[optind
+ 1];
2082 /* Default is to strip no symbols. */
2083 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2084 strip_symbols
= STRIP_NONE
;
2086 if (output_target
== (char *) NULL
)
2087 output_target
= input_target
;
2091 if (stat (input_filename
, &statbuf
) < 0)
2092 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2095 /* If there is no destination file then create a temp and rename
2096 the result into the input. */
2098 if (output_filename
== (char *) NULL
)
2100 char *tmpname
= make_tempname (input_filename
);
2102 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2106 set_times (tmpname
, &statbuf
);
2107 smart_rename (tmpname
, input_filename
);
2114 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2115 if (status
== 0 && preserve_dates
)
2116 set_times (output_filename
, &statbuf
);
2121 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2125 if (p
->change_vma
!= CHANGE_IGNORE
)
2129 sprintf_vma (buff
, p
->vma_val
);
2131 /* xgettext:c-format */
2132 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
2134 p
->change_vma
== CHANGE_SET
? '=' : '+',
2138 if (p
->change_lma
!= CHANGE_IGNORE
)
2142 sprintf_vma (buff
, p
->lma_val
);
2144 /* xgettext:c-format */
2145 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
2147 p
->change_lma
== CHANGE_SET
? '=' : '+',
2162 #ifdef HAVE_SETLOCALE
2163 setlocale (LC_MESSAGES
, "");
2165 bindtextdomain (PACKAGE
, LOCALEDIR
);
2166 textdomain (PACKAGE
);
2168 program_name
= argv
[0];
2169 xmalloc_set_program_name (program_name
);
2171 START_PROGRESS (program_name
, 0);
2173 strip_symbols
= STRIP_UNDEF
;
2174 discard_locals
= LOCALS_UNDEF
;
2177 set_default_bfd_target ();
2181 int i
= strlen (program_name
);
2182 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2186 strip_main (argc
, argv
);
2188 copy_main (argc
, argv
);
2190 END_PROGRESS (program_name
);