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 #include "elf/internal.h"
33 #ifdef HAVE_GOOD_UTIME_H
35 #else /* ! HAVE_GOOD_UTIME_H */
38 #endif /* HAVE_UTIMES */
39 #endif /* ! HAVE_GOOD_UTIME_H */
41 /* A list of symbols to explicitly strip out, or to keep. A linked
42 list is good enough for a small number from the command line, but
43 this will slow things down a lot if many symbols are being
52 static void copy_usage
PARAMS ((FILE *, int));
53 static void strip_usage
PARAMS ((FILE *, int));
54 static flagword parse_flags
PARAMS ((const char *));
55 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
56 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
57 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
58 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
59 static int compare_section_lma
PARAMS ((const PTR
, const PTR
));
60 static void add_specific_symbol
PARAMS ((const char *, struct symlist
**));
61 static boolean is_specified_symbol
PARAMS ((const char *, struct symlist
*));
62 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
63 static unsigned int filter_symbols
64 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
65 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
66 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
67 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
68 static void copy_object
PARAMS ((bfd
*, bfd
*));
69 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
71 PARAMS ((const char *, const char *, const char *, const char *));
72 static int simple_copy
PARAMS ((const char *, const char *));
73 static int smart_rename
PARAMS ((const char *, const char *));
74 static void set_times
PARAMS ((const char *, const struct stat
*));
75 static int strip_main
PARAMS ((int, char **));
76 static int copy_main
PARAMS ((int, char **));
78 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
80 static asymbol
**isympp
= NULL
; /* Input symbols */
81 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
83 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
84 static int copy_byte
= -1;
85 static int interleave
= 4;
87 static boolean verbose
; /* Print file and target names. */
88 static boolean preserve_dates
; /* Preserve input file timestamp. */
89 static int status
= 0; /* Exit status. */
94 STRIP_NONE
, /* don't strip */
95 STRIP_DEBUG
, /* strip all debugger symbols */
96 STRIP_UNNEEDED
, /* strip unnecessary symbols */
97 STRIP_ALL
/* strip all symbols */
100 /* Which symbols to remove. */
101 static enum strip_action strip_symbols
;
106 LOCALS_START_L
, /* discard locals starting with L */
107 LOCALS_ALL
/* discard all locals */
110 /* Which local symbols to remove. Overrides STRIP_ALL. */
111 static enum locals_action discard_locals
;
113 /* What kind of change to perform. */
121 /* Structure used to hold lists of sections and actions to take. */
124 struct section_list
* next
; /* Next section to change. */
125 const char * name
; /* Section name. */
126 boolean used
; /* Whether this entry was used. */
127 boolean remove
; /* Whether to remove this section. */
128 enum change_action change_vma
;/* Whether to change or set VMA. */
129 bfd_vma vma_val
; /* Amount to change by or set to. */
130 enum change_action change_lma
;/* Whether to change or set LMA. */
131 bfd_vma lma_val
; /* Amount to change by or set to. */
132 boolean set_flags
; /* Whether to set the section flags. */
133 flagword flags
; /* What to set the section flags to. */
136 static struct section_list
*change_sections
;
137 static boolean sections_removed
;
139 /* Changes to the start address. */
140 static bfd_vma change_start
= 0;
141 static boolean set_start_set
= false;
142 static bfd_vma set_start
;
144 /* Changes to section addresses. */
145 static bfd_vma change_section_address
= 0;
147 /* Filling gaps between sections. */
148 static boolean gap_fill_set
= false;
149 static bfd_byte gap_fill
= 0;
151 /* Pad to a given address. */
152 static boolean pad_to_set
= false;
153 static bfd_vma pad_to
;
155 /* List of sections to add. */
159 /* Next section to add. */
160 struct section_add
*next
;
161 /* Name of section to add. */
163 /* Name of file holding section contents. */
164 const char *filename
;
167 /* Contents of file. */
169 /* BFD section, after it has been added. */
173 static struct section_add
*add_sections
;
175 /* Whether to convert debugging information. */
177 static boolean convert_debugging
= false;
179 /* Whether to change the leading character in symbol names. */
181 static boolean change_leading_char
= false;
183 /* Whether to remove the leading character from global symbol names. */
185 static boolean remove_leading_char
= false;
187 /* List of symbols to strip, keep, localize, and weaken. */
189 static struct symlist
*strip_specific_list
= NULL
;
190 static struct symlist
*keep_specific_list
= NULL
;
191 static struct symlist
*localize_specific_list
= NULL
;
192 static struct symlist
*weaken_specific_list
= NULL
;
194 /* If this is true, we weaken global symbols (set BSF_WEAK). */
196 static boolean weaken
= false;
198 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
200 #define OPTION_ADD_SECTION 150
201 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
202 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
203 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
204 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
205 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
206 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
207 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
208 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
209 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
210 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
211 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
212 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
213 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
214 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
215 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
216 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
218 /* Options to handle if running as "strip". */
220 static struct option strip_options
[] =
222 {"discard-all", no_argument
, 0, 'x'},
223 {"discard-locals", no_argument
, 0, 'X'},
224 {"format", required_argument
, 0, 'F'}, /* Obsolete */
225 {"help", no_argument
, 0, 'h'},
226 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
227 {"input-target", required_argument
, 0, 'I'},
228 {"keep-symbol", required_argument
, 0, 'K'},
229 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
230 {"output-target", required_argument
, 0, 'O'},
231 {"preserve-dates", no_argument
, 0, 'p'},
232 {"remove-section", required_argument
, 0, 'R'},
233 {"strip-all", no_argument
, 0, 's'},
234 {"strip-debug", no_argument
, 0, 'S'},
235 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
236 {"strip-symbol", required_argument
, 0, 'N'},
237 {"target", required_argument
, 0, 'F'},
238 {"verbose", no_argument
, 0, 'v'},
239 {"version", no_argument
, 0, 'V'},
240 {0, no_argument
, 0, 0}
243 /* Options to handle if running as "objcopy". */
245 static struct option copy_options
[] =
247 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
248 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
249 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
250 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
251 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
252 {"byte", required_argument
, 0, 'b'},
253 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
254 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
255 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
256 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
257 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
258 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
259 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
260 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
261 {"discard-all", no_argument
, 0, 'x'},
262 {"discard-locals", no_argument
, 0, 'X'},
263 {"format", required_argument
, 0, 'F'}, /* Obsolete */
264 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
265 {"help", no_argument
, 0, 'h'},
266 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
267 {"input-target", required_argument
, 0, 'I'},
268 {"interleave", required_argument
, 0, 'i'},
269 {"keep-symbol", required_argument
, 0, 'K'},
270 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
271 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
272 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
273 {"output-target", required_argument
, 0, 'O'},
274 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
275 {"preserve-dates", no_argument
, 0, 'p'},
276 {"localize-symbol", required_argument
, 0, 'L'},
277 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
278 {"remove-section", required_argument
, 0, 'R'},
279 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
280 {"set-start", required_argument
, 0, OPTION_SET_START
},
281 {"strip-all", no_argument
, 0, 'S'},
282 {"strip-debug", no_argument
, 0, 'g'},
283 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
284 {"strip-symbol", required_argument
, 0, 'N'},
285 {"target", required_argument
, 0, 'F'},
286 {"verbose", no_argument
, 0, 'v'},
287 {"version", no_argument
, 0, 'V'},
288 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
289 {"weaken-symbol", required_argument
, 0, 'W'},
290 {0, no_argument
, 0, 0}
294 extern char *program_name
;
296 /* This flag distinguishes between strip and objcopy:
297 1 means this is 'strip'; 0 means this is 'objcopy'.
298 -1 means if we should use argv[0] to decide. */
303 copy_usage (stream
, exit_status
)
307 fprintf (stream
, _("\
308 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
309 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
310 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
311 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
312 [--discard-locals] [--debugging] [--remove-section=section]\n"),
314 fprintf (stream
, _("\
315 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
316 [--set-start=val] \n\
317 [--change-start=incr] [--change-addresses=incr] \n\
318 (--adjust-start and --adjust-vma are aliases for these two) \n\
319 [--change-section-address=section{=,+,-}val]\n\
320 (--adjust-section-vma is an alias for --change-section-address)\n\
321 [--change-section-lma=section{=,+,-}val]\n\
322 [--change-section-vma=section{=,+,-}val]\n\
323 [--adjust-warnings] [--no-adjust-warnings]\n\
324 [--change-warnings] [--no-change-warnings]\n\
325 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
326 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
327 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
328 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
329 [--verbose] [--version] [--help] in-file [out-file]\n"));
330 list_supported_targets (program_name
, stream
);
331 if (exit_status
== 0)
332 fprintf (stream
, _("Report bugs to bug-gnu-utils@gnu.org\n"));
337 strip_usage (stream
, exit_status
)
341 fprintf (stream
, _("\
342 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
343 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
344 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
345 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
346 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
347 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n"),
349 list_supported_targets (program_name
, stream
);
350 if (exit_status
== 0)
351 fprintf (stream
, _("Report bugs to bug-gnu-utils@gnu.org\n"));
355 /* Parse section flags into a flagword, with a fatal error if the
356 string can't be parsed. */
370 snext
= strchr (s
, ',');
380 #define PARSE_FLAG(fname,fval) \
381 else if (strncasecmp (fname, s, len) == 0) ret |= fval
382 PARSE_FLAG ("alloc", SEC_ALLOC
);
383 PARSE_FLAG ("load", SEC_LOAD
);
384 PARSE_FLAG ("readonly", SEC_READONLY
);
385 PARSE_FLAG ("code", SEC_CODE
);
386 PARSE_FLAG ("data", SEC_DATA
);
387 PARSE_FLAG ("rom", SEC_ROM
);
388 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
394 copy
= xmalloc (len
+ 1);
395 strncpy (copy
, s
, len
);
397 non_fatal (_("unrecognized section flag `%s'"), copy
);
398 fatal (_("supported flags: alloc, load, readonly, code, data, rom, contents"));
408 /* Find and optionally add an entry in the change_sections list. */
410 static struct section_list
*
411 find_section_list (name
, add
)
415 register struct section_list
*p
;
417 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
418 if (strcmp (p
->name
, name
) == 0)
424 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
428 p
->change_vma
= CHANGE_IGNORE
;
429 p
->change_lma
= CHANGE_IGNORE
;
432 p
->set_flags
= false;
435 p
->next
= change_sections
;
441 /* Add a symbol to strip_specific_list. */
444 add_specific_symbol (name
, list
)
446 struct symlist
**list
;
448 struct symlist
*tmp_list
;
450 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
451 tmp_list
->name
= name
;
452 tmp_list
->next
= *list
;
456 /* See whether a symbol should be stripped or kept based on
457 strip_specific_list and keep_symbols. */
460 is_specified_symbol (name
, list
)
462 struct symlist
*list
;
464 struct symlist
*tmp_list
;
466 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
468 if (strcmp (name
, tmp_list
->name
) == 0)
474 /* See if a section is being removed. */
477 is_strip_section (abfd
, sec
)
481 struct section_list
*p
;
483 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
484 && (strip_symbols
== STRIP_DEBUG
485 || strip_symbols
== STRIP_UNNEEDED
486 || strip_symbols
== STRIP_ALL
487 || discard_locals
== LOCALS_ALL
488 || convert_debugging
))
491 if (! sections_removed
)
493 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
494 return p
!= NULL
&& p
->remove
? true : false;
497 /* Choose which symbol entries to copy; put the result in OSYMS.
498 We don't copy in place, because that confuses the relocs.
499 Return the number of symbols to print. */
502 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
505 asymbol
**osyms
, **isyms
;
508 register asymbol
**from
= isyms
, **to
= osyms
;
509 long src_count
= 0, dst_count
= 0;
511 for (; src_count
< symcount
; src_count
++)
513 asymbol
*sym
= from
[src_count
];
514 flagword flags
= sym
->flags
;
515 const char *name
= bfd_asymbol_name (sym
);
518 if (change_leading_char
519 && (bfd_get_symbol_leading_char (abfd
)
520 != bfd_get_symbol_leading_char (obfd
))
521 && (bfd_get_symbol_leading_char (abfd
) == '\0'
522 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
524 if (bfd_get_symbol_leading_char (obfd
) == '\0')
525 name
= bfd_asymbol_name (sym
) = name
+ 1;
530 n
= xmalloc (strlen (name
) + 2);
531 n
[0] = bfd_get_symbol_leading_char (obfd
);
532 if (bfd_get_symbol_leading_char (abfd
) == '\0')
533 strcpy (n
+ 1, name
);
535 strcpy (n
+ 1, name
+ 1);
536 name
= bfd_asymbol_name (sym
) = n
;
540 if (remove_leading_char
541 && ((flags
& BSF_GLOBAL
) != 0
542 || (flags
& BSF_WEAK
) != 0
543 || bfd_is_und_section (bfd_get_section (sym
))
544 || bfd_is_com_section (bfd_get_section (sym
)))
545 && name
[0] == bfd_get_symbol_leading_char (abfd
))
546 name
= bfd_asymbol_name (sym
) = name
+ 1;
548 if ((flags
& BSF_KEEP
) != 0) /* Used in relocation. */
550 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
551 || (flags
& BSF_WEAK
) != 0
552 || bfd_is_und_section (bfd_get_section (sym
))
553 || bfd_is_com_section (bfd_get_section (sym
)))
554 keep
= strip_symbols
!= STRIP_UNNEEDED
;
555 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
556 keep
= (strip_symbols
!= STRIP_DEBUG
557 && strip_symbols
!= STRIP_UNNEEDED
558 && ! convert_debugging
);
559 else /* Local symbol. */
560 keep
= (strip_symbols
!= STRIP_UNNEEDED
561 && (discard_locals
!= LOCALS_ALL
562 && (discard_locals
!= LOCALS_START_L
563 || ! bfd_is_local_label (abfd
, sym
))));
565 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
567 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
569 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
572 if (keep
&& (flags
& BSF_GLOBAL
) != 0
573 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
575 sym
->flags
&=~ BSF_GLOBAL
;
576 sym
->flags
|= BSF_WEAK
;
578 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
579 && is_specified_symbol (name
, localize_specific_list
))
581 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
582 sym
->flags
|= BSF_LOCAL
;
586 to
[dst_count
++] = sym
;
589 to
[dst_count
] = NULL
;
594 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
598 filter_bytes (memhunk
, size
)
602 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
604 for (; from
< end
; from
+= interleave
)
609 /* Copy object file IBFD onto OBFD. */
612 copy_object (ibfd
, obfd
)
618 asection
**osections
= NULL
;
619 bfd_size_type
*gaps
= NULL
;
620 bfd_size_type max_gap
= 0;
622 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
623 RETURN_NONFATAL (bfd_get_filename (obfd
));
626 printf (_("copy from %s(%s) to %s(%s)\n"),
627 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
628 bfd_get_filename (obfd
), bfd_get_target (obfd
));
633 start
= bfd_get_start_address (ibfd
);
634 start
+= change_start
;
636 if (!bfd_set_start_address (obfd
, start
)
637 || !bfd_set_file_flags (obfd
,
638 (bfd_get_file_flags (ibfd
)
639 & bfd_applicable_file_flags (obfd
))))
640 RETURN_NONFATAL (bfd_get_filename (ibfd
));
642 /* Copy architecture of input file to output file */
643 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
644 bfd_get_mach (ibfd
)))
645 non_fatal (_("Warning: Output file cannot represent architecture %s"),
646 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
647 bfd_get_mach (ibfd
)));
649 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
650 RETURN_NONFATAL (bfd_get_filename (ibfd
));
655 if (osympp
!= isympp
)
658 /* BFD mandates that all output sections be created and sizes set before
659 any output is done. Thus, we traverse all sections multiple times. */
660 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
662 if (add_sections
!= NULL
)
664 struct section_add
*padd
;
665 struct section_list
*pset
;
667 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
669 padd
->section
= bfd_make_section (obfd
, padd
->name
);
670 if (padd
->section
== NULL
)
672 non_fatal (_("can't create section `%s': %s"),
673 padd
->name
, bfd_errmsg (bfd_get_error ()));
681 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
682 RETURN_NONFATAL (bfd_get_filename (obfd
));
684 pset
= find_section_list (padd
->name
, false);
688 if (pset
!= NULL
&& pset
->set_flags
)
689 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
691 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
693 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
694 RETURN_NONFATAL (bfd_get_filename (obfd
));
698 if (pset
->change_vma
!= CHANGE_IGNORE
)
699 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
700 RETURN_NONFATAL (bfd_get_filename (obfd
));
702 if (pset
->change_lma
!= CHANGE_IGNORE
)
704 padd
->section
->lma
= pset
->lma_val
;
706 if (! bfd_set_section_alignment
707 (obfd
, padd
->section
,
708 bfd_section_alignment (obfd
, padd
->section
)))
709 RETURN_NONFATAL (bfd_get_filename (obfd
));
716 if (gap_fill_set
|| pad_to_set
)
721 /* We must fill in gaps between the sections and/or we must pad
722 the last section to a specified address. We do this by
723 grabbing a list of the sections, sorting them by VMA, and
724 increasing the section sizes as required to fill the gaps.
725 We write out the gap contents below. */
727 c
= bfd_count_sections (obfd
);
728 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
730 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
732 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
734 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
735 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
739 for (i
= 0; i
< c
- 1; i
++)
743 bfd_vma gap_start
, gap_stop
;
745 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
746 if ((flags
& SEC_HAS_CONTENTS
) == 0
747 || (flags
& SEC_LOAD
) == 0)
750 size
= bfd_section_size (obfd
, osections
[i
]);
751 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
752 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
753 if (gap_start
< gap_stop
)
755 if (! bfd_set_section_size (obfd
, osections
[i
],
756 size
+ (gap_stop
- gap_start
)))
758 non_fatal (_("Can't fill gap after %s: %s"),
759 bfd_get_section_name (obfd
, osections
[i
]),
760 bfd_errmsg (bfd_get_error ()));
764 gaps
[i
] = gap_stop
- gap_start
;
765 if (max_gap
< gap_stop
- gap_start
)
766 max_gap
= gap_stop
- gap_start
;
776 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
777 size
= bfd_section_size (obfd
, osections
[c
- 1]);
778 if (lma
+ size
< pad_to
)
780 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
783 non_fatal (_("Can't add padding to %s: %s"),
784 bfd_get_section_name (obfd
, osections
[c
- 1]),
785 bfd_errmsg (bfd_get_error ()));
790 gaps
[c
- 1] = pad_to
- (lma
+ size
);
791 if (max_gap
< pad_to
- (lma
+ size
))
792 max_gap
= pad_to
- (lma
+ size
);
798 /* Symbol filtering must happen after the output sections have
799 been created, but before their contents are set. */
800 if (strip_symbols
== STRIP_ALL
)
802 osympp
= isympp
= NULL
;
810 symsize
= bfd_get_symtab_upper_bound (ibfd
);
812 RETURN_NONFATAL (bfd_get_filename (ibfd
));
814 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
815 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
817 RETURN_NONFATAL (bfd_get_filename (ibfd
));
819 if (convert_debugging
)
820 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
822 if (strip_symbols
== STRIP_DEBUG
823 || strip_symbols
== STRIP_UNNEEDED
824 || discard_locals
!= LOCALS_UNDEF
825 || strip_specific_list
!= NULL
826 || keep_specific_list
!= NULL
827 || localize_specific_list
!= NULL
828 || weaken_specific_list
!= NULL
831 || change_leading_char
832 || remove_leading_char
835 /* Mark symbols used in output relocations so that they
836 are kept, even if they are local labels or static symbols.
838 Note we iterate over the input sections examining their
839 relocations since the relocations for the output sections
840 haven't been set yet. mark_symbols_used_in_relocations will
841 ignore input sections which have no corresponding output
843 bfd_map_over_sections (ibfd
,
844 mark_symbols_used_in_relocations
,
846 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
847 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
850 if (convert_debugging
&& dhandle
!= NULL
)
852 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
860 bfd_set_symtab (obfd
, osympp
, symcount
);
862 /* This has to happen after the symbol table has been set. */
863 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
865 if (add_sections
!= NULL
)
867 struct section_add
*padd
;
869 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
871 if (! bfd_set_section_contents (obfd
, padd
->section
,
872 (PTR
) padd
->contents
,
874 (bfd_size_type
) padd
->size
))
875 RETURN_NONFATAL (bfd_get_filename (obfd
));
879 if (gap_fill_set
|| pad_to_set
)
884 /* Fill in the gaps. */
888 buf
= (bfd_byte
*) xmalloc (max_gap
);
889 memset (buf
, gap_fill
, (size_t) max_gap
);
891 c
= bfd_count_sections (obfd
);
892 for (i
= 0; i
< c
; i
++)
900 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
910 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
912 RETURN_NONFATAL (bfd_get_filename (obfd
));
921 /* Allow the BFD backend to copy any private data it understands
922 from the input BFD to the output BFD. This is done last to
923 permit the routine to look at the filtered symbol table, which is
924 important for the ECOFF code at least. */
925 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
927 non_fatal (_("%s: error copying private BFD data: %s"),
928 bfd_get_filename (obfd
),
929 bfd_errmsg (bfd_get_error ()));
935 /* Read each archive element in turn from IBFD, copy the
936 contents to temp file, and keep the temp file handle. */
939 copy_archive (ibfd
, obfd
, output_target
)
942 const char *output_target
;
946 struct name_list
*next
;
950 bfd
**ptr
= &obfd
->archive_head
;
952 char *dir
= make_tempname (bfd_get_filename (obfd
));
954 /* Make a temp directory to hold the contents. */
955 #if defined (_WIN32) && !defined (__CYGWIN32__)
956 if (mkdir (dir
) != 0)
958 if (mkdir (dir
, 0700) != 0)
961 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
962 dir
, strerror (errno
));
964 obfd
->has_armap
= ibfd
->has_armap
;
968 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
969 while (!status
&& this_element
!= (bfd
*) NULL
)
971 /* Create an output file for this member. */
972 char *output_name
= concat (dir
, "/", bfd_get_filename (this_element
),
974 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
977 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
978 l
->name
= output_name
;
982 if (output_bfd
== (bfd
*) NULL
)
983 RETURN_NONFATAL (output_name
);
985 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
986 RETURN_NONFATAL (bfd_get_filename (obfd
));
988 if (bfd_check_format (this_element
, bfd_object
) == true)
989 copy_object (this_element
, output_bfd
);
991 if (!bfd_close (output_bfd
))
993 bfd_nonfatal (bfd_get_filename (output_bfd
));
994 /* Error in new object file. Don't change archive. */
998 /* Open the newly output file and attach to our list. */
999 output_bfd
= bfd_openr (output_name
, output_target
);
1001 l
->obfd
= output_bfd
;
1004 ptr
= &output_bfd
->next
;
1006 last_element
= this_element
;
1008 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1010 bfd_close (last_element
);
1012 *ptr
= (bfd
*) NULL
;
1014 if (!bfd_close (obfd
))
1015 RETURN_NONFATAL (bfd_get_filename (obfd
));
1017 if (!bfd_close (ibfd
))
1018 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1020 /* Delete all the files that we opened. */
1021 for (l
= list
; l
!= NULL
; l
= l
->next
)
1023 bfd_close (l
->obfd
);
1029 /* The top-level control. */
1032 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1033 const char *input_filename
;
1034 const char *output_filename
;
1035 const char *input_target
;
1036 const char *output_target
;
1041 /* To allow us to do "strip *" without dying on the first
1042 non-object file, failures are nonfatal. */
1044 ibfd
= bfd_openr (input_filename
, input_target
);
1046 RETURN_NONFATAL (input_filename
);
1048 if (bfd_check_format (ibfd
, bfd_archive
))
1052 /* bfd_get_target does not return the correct value until
1053 bfd_check_format succeeds. */
1054 if (output_target
== NULL
)
1055 output_target
= bfd_get_target (ibfd
);
1057 obfd
= bfd_openw (output_filename
, output_target
);
1059 RETURN_NONFATAL (output_filename
);
1061 copy_archive (ibfd
, obfd
, output_target
);
1063 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1067 /* bfd_get_target does not return the correct value until
1068 bfd_check_format succeeds. */
1069 if (output_target
== NULL
)
1070 output_target
= bfd_get_target (ibfd
);
1072 obfd
= bfd_openw (output_filename
, output_target
);
1074 RETURN_NONFATAL (output_filename
);
1076 copy_object (ibfd
, obfd
);
1078 if (!bfd_close (obfd
))
1079 RETURN_NONFATAL (output_filename
);
1081 if (!bfd_close (ibfd
))
1082 RETURN_NONFATAL (input_filename
);
1086 bfd_nonfatal (input_filename
);
1088 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1090 list_matching_formats (matching
);
1098 /* Create a section in OBFD with the same name and attributes
1099 as ISECTION in IBFD. */
1102 setup_section (ibfd
, isection
, obfdarg
)
1107 bfd
*obfd
= (bfd
*) obfdarg
;
1108 struct section_list
*p
;
1116 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1117 && (strip_symbols
== STRIP_DEBUG
1118 || strip_symbols
== STRIP_UNNEEDED
1119 || strip_symbols
== STRIP_ALL
1120 || discard_locals
== LOCALS_ALL
1121 || convert_debugging
))
1124 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1128 if (p
!= NULL
&& p
->remove
)
1131 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1133 if (osection
== NULL
)
1139 size
= bfd_section_size (ibfd
, isection
);
1141 size
= (size
+ interleave
- 1) / interleave
;
1142 if (! bfd_set_section_size (obfd
, osection
, size
))
1148 vma
= bfd_section_vma (ibfd
, isection
);
1149 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1151 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1154 vma
+= change_section_address
;
1156 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1162 lma
= isection
->lma
;
1163 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1165 if (p
->change_lma
== CHANGE_MODIFY
)
1167 else if (p
->change_lma
== CHANGE_SET
)
1173 lma
+= change_section_address
;
1175 osection
->lma
= lma
;
1177 /* FIXME: This is probably not enough. If we change the LMA we
1178 may have to recompute the header for the file as well. */
1179 if (bfd_set_section_alignment (obfd
,
1181 bfd_section_alignment (ibfd
, isection
))
1188 flags
= bfd_get_section_flags (ibfd
, isection
);
1189 if (p
!= NULL
&& p
->set_flags
)
1190 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1191 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1197 /* This used to be mangle_section; we do here to avoid using
1198 bfd_get_section_by_name since some formats allow multiple
1199 sections with the same name. */
1200 isection
->output_section
= osection
;
1201 isection
->output_offset
= 0;
1203 /* Allow the BFD backend to copy any private data it understands
1204 from the input section to the output section. */
1205 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1207 err
= "private data";
1215 non_fatal (_("%s: section `%s': error in %s: %s"),
1216 bfd_get_filename (ibfd
),
1217 bfd_section_name (ibfd
, isection
),
1218 err
, bfd_errmsg (bfd_get_error ()));
1222 /* Copy the data of input section ISECTION of IBFD
1223 to an output section with the same name in OBFD.
1224 If stripping then don't copy any relocation info. */
1227 copy_section (ibfd
, isection
, obfdarg
)
1232 bfd
*obfd
= (bfd
*) obfdarg
;
1233 struct section_list
*p
;
1239 /* If we have already failed earlier on, do not keep on generating
1244 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1245 && (strip_symbols
== STRIP_DEBUG
1246 || strip_symbols
== STRIP_UNNEEDED
1247 || strip_symbols
== STRIP_ALL
1248 || discard_locals
== LOCALS_ALL
1249 || convert_debugging
))
1254 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1256 if (p
!= NULL
&& p
->remove
)
1259 osection
= isection
->output_section
;
1260 size
= bfd_get_section_size_before_reloc (isection
);
1262 if (size
== 0 || osection
== 0)
1265 if (strip_symbols
== STRIP_ALL
)
1266 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1271 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1273 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1276 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1279 relpp
= (arelent
**) xmalloc (relsize
);
1280 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1282 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1284 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
1288 isection
->_cooked_size
= isection
->_raw_size
;
1289 isection
->reloc_done
= true;
1291 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1293 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1295 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1297 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1300 filter_bytes (memhunk
, &size
);
1302 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1304 RETURN_NONFATAL (bfd_get_filename (obfd
));
1308 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1310 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1312 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1313 flag--they can just remove the section entirely and add it
1314 back again. However, we do permit them to turn on the
1315 SEC_HAS_CONTENTS flag, and take it to mean that the section
1316 contents should be zeroed out. */
1318 memset (memhunk
, 0, size
);
1319 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1321 RETURN_NONFATAL (bfd_get_filename (obfd
));
1326 /* Get all the sections. This is used when --gap-fill or --pad-to is
1330 get_sections (obfd
, osection
, secppparg
)
1335 asection
***secppp
= (asection
***) secppparg
;
1337 **secppp
= osection
;
1341 /* Sort sections by VMA. This is called via qsort, and is used when
1342 --gap-fill or --pad-to is used. We force non loadable or empty
1343 sections to the front, where they are easier to ignore. */
1346 compare_section_lma (arg1
, arg2
)
1350 const asection
**sec1
= (const asection
**) arg1
;
1351 const asection
**sec2
= (const asection
**) arg2
;
1352 flagword flags1
, flags2
;
1354 /* Sort non loadable sections to the front. */
1355 flags1
= (*sec1
)->flags
;
1356 flags2
= (*sec2
)->flags
;
1357 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1358 || (flags1
& SEC_LOAD
) == 0)
1360 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1361 && (flags2
& SEC_LOAD
) != 0)
1366 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1367 || (flags2
& SEC_LOAD
) == 0)
1371 /* Sort sections by LMA. */
1372 if ((*sec1
)->lma
> (*sec2
)->lma
)
1374 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1377 /* Sort sections with the same LMA by size. */
1378 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1380 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1386 /* Mark all the symbols which will be used in output relocations with
1387 the BSF_KEEP flag so that those symbols will not be stripped.
1389 Ignore relocations which will not appear in the output file. */
1392 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1397 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1402 /* Ignore an input section with no corresponding output section. */
1403 if (isection
->output_section
== NULL
)
1406 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1408 bfd_fatal (bfd_get_filename (ibfd
));
1413 relpp
= (arelent
**) xmalloc (relsize
);
1414 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1416 bfd_fatal (bfd_get_filename (ibfd
));
1418 /* Examine each symbol used in a relocation. If it's not one of the
1419 special bfd section symbols, then mark it with BSF_KEEP. */
1420 for (i
= 0; i
< relcount
; i
++)
1422 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1423 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1424 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1425 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1432 /* Write out debugging information. */
1435 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1441 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1442 return write_ieee_debugging_info (obfd
, dhandle
);
1444 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1445 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1447 bfd_byte
*syms
, *strings
;
1448 bfd_size_type symsize
, stringsize
;
1449 asection
*stabsec
, *stabstrsec
;
1451 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1456 stabsec
= bfd_make_section (obfd
, ".stab");
1457 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1459 || stabstrsec
== NULL
1460 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1461 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1462 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1463 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1464 || ! bfd_set_section_flags (obfd
, stabsec
,
1468 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1473 non_fatal (_("%s: can't create debugging section: %s"),
1474 bfd_get_filename (obfd
),
1475 bfd_errmsg (bfd_get_error ()));
1479 /* We can get away with setting the section contents now because
1480 the next thing the caller is going to do is copy over the
1481 real sections. We may someday have to split the contents
1482 setting out of this function. */
1483 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1485 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1486 (file_ptr
) 0, stringsize
))
1488 non_fatal (_("%s: can't set debugging section contents: %s"),
1489 bfd_get_filename (obfd
),
1490 bfd_errmsg (bfd_get_error ()));
1497 non_fatal (_("%s: don't know how to write debugging information for %s"),
1498 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1502 /* The number of bytes to copy at once. */
1503 #define COPY_BUF 8192
1505 /* Copy file FROM to file TO, performing no translations.
1506 Return 0 if ok, -1 if error. */
1509 simple_copy (from
, to
)
1513 int fromfd
, tofd
, nread
;
1517 fromfd
= open (from
, O_RDONLY
);
1520 tofd
= creat (to
, 0777);
1528 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
1530 if (write (tofd
, buf
, nread
) != nread
)
1552 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1554 #define S_ISLNK(m) 0
1559 /* Rename FROM to TO, copying if TO is a link.
1560 Assumes that TO already exists, because FROM is a temp file.
1561 Return 0 if ok, -1 if error. */
1564 smart_rename (from
, to
)
1574 #if defined (_WIN32) && !defined (__CYGWIN32__)
1575 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1576 fail instead. Also, chown is not present. */
1578 if (stat (to
, &s
) == 0)
1581 ret
= rename (from
, to
);
1584 /* We have to clean up here. */
1586 non_fatal (_("%s: rename: %s"), to
, strerror (errno
));
1590 /* Use rename only if TO is not a symbolic link and has
1591 only one hard link. */
1592 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1594 ret
= rename (from
, to
);
1597 /* Try to preserve the permission bits and ownership of TO.
1598 First get the mode right except for the setuid bit. Then
1599 change the ownership. Then fix the setuid bit. We do
1600 the chmod before the chown because if the chown succeeds,
1601 and we are a normal user, we won't be able to do the
1602 chmod afterward. We don't bother to fix the setuid bit
1603 first because that might introduce a fleeting security
1604 problem, and because the chown will clear the setuid bit
1605 anyhow. We only fix the setuid bit if the chown
1606 succeeds, because we don't want to introduce an
1607 unexpected setuid file owned by the user running objcopy. */
1608 chmod (to
, s
.st_mode
& 0777);
1609 if (chown (to
, s
.st_uid
, s
.st_gid
) >= 0)
1610 chmod (to
, s
.st_mode
& 07777);
1614 /* We have to clean up here. */
1615 non_fatal (_("%s: rename: %s"), to
, strerror (errno
));
1621 ret
= simple_copy (from
, to
);
1623 non_fatal (_("%s: simple_copy: %s"), to
, strerror (errno
));
1629 #endif /* _WIN32 && !__CYGWIN32__ */
1634 /* Set the times of the file DESTINATION to be the same as those in
1638 set_times (destination
, statbuf
)
1639 const char *destination
;
1640 const struct stat
*statbuf
;
1645 #ifdef HAVE_GOOD_UTIME_H
1648 tb
.actime
= statbuf
->st_atime
;
1649 tb
.modtime
= statbuf
->st_mtime
;
1650 result
= utime (destination
, &tb
);
1651 #else /* ! HAVE_GOOD_UTIME_H */
1655 tb
[0] = statbuf
->st_atime
;
1656 tb
[1] = statbuf
->st_mtime
;
1657 result
= utime (destination
, tb
);
1658 #else /* HAVE_UTIMES */
1659 struct timeval tv
[2];
1661 tv
[0].tv_sec
= statbuf
->st_atime
;
1663 tv
[1].tv_sec
= statbuf
->st_mtime
;
1665 result
= utimes (destination
, tv
);
1666 #endif /* HAVE_UTIMES */
1667 #endif /* ! HAVE_GOOD_UTIME_H */
1671 non_fatal (_("%s: cannot set time: %s"), destination
, strerror (errno
));
1675 strip_main (argc
, argv
)
1679 char *input_target
= NULL
, *output_target
= NULL
;
1680 boolean show_version
= false;
1682 struct section_list
*p
;
1683 char *output_file
= NULL
;
1685 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1686 strip_options
, (int *) 0)) != EOF
)
1691 input_target
= optarg
;
1694 output_target
= optarg
;
1697 input_target
= output_target
= optarg
;
1700 p
= find_section_list (optarg
, true);
1702 sections_removed
= true;
1705 strip_symbols
= STRIP_ALL
;
1709 strip_symbols
= STRIP_DEBUG
;
1711 case OPTION_STRIP_UNNEEDED
:
1712 strip_symbols
= STRIP_UNNEEDED
;
1715 add_specific_symbol (optarg
, &keep_specific_list
);
1718 add_specific_symbol (optarg
, &strip_specific_list
);
1721 output_file
= optarg
;
1724 preserve_dates
= true;
1727 discard_locals
= LOCALS_ALL
;
1730 discard_locals
= LOCALS_START_L
;
1736 show_version
= true;
1739 break; /* we've been given a long option */
1741 strip_usage (stdout
, 0);
1743 strip_usage (stderr
, 1);
1748 print_version ("strip");
1750 /* Default is to strip all symbols. */
1751 if (strip_symbols
== STRIP_UNDEF
1752 && discard_locals
== LOCALS_UNDEF
1753 && strip_specific_list
== NULL
1754 && keep_specific_list
== NULL
)
1755 strip_symbols
= STRIP_ALL
;
1757 if (output_target
== (char *) NULL
)
1758 output_target
= input_target
;
1762 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1763 strip_usage (stderr
, 1);
1765 for (; i
< argc
; i
++)
1767 int hold_status
= status
;
1768 struct stat statbuf
;
1773 if (stat (argv
[i
], &statbuf
) < 0)
1775 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
1780 if (output_file
!= NULL
)
1781 tmpname
= output_file
;
1783 tmpname
= make_tempname (argv
[i
]);
1786 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1790 set_times (tmpname
, &statbuf
);
1791 if (output_file
== NULL
)
1792 smart_rename (tmpname
, argv
[i
]);
1793 status
= hold_status
;
1797 if (output_file
== NULL
)
1805 copy_main (argc
, argv
)
1809 char *input_filename
= NULL
, *output_filename
= NULL
;
1810 char *input_target
= NULL
, *output_target
= NULL
;
1811 boolean show_version
= false;
1812 boolean change_warn
= true;
1814 struct section_list
*p
;
1815 struct stat statbuf
;
1817 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1818 copy_options
, (int *) 0)) != EOF
)
1823 copy_byte
= atoi (optarg
);
1825 fatal (_("byte number must be non-negative"));
1828 interleave
= atoi (optarg
);
1830 fatal (_("interleave must be positive"));
1833 case 's': /* "source" - 'I' is preferred */
1834 input_target
= optarg
;
1837 case 'd': /* "destination" - 'O' is preferred */
1838 output_target
= optarg
;
1841 input_target
= output_target
= optarg
;
1844 p
= find_section_list (optarg
, true);
1846 sections_removed
= true;
1849 strip_symbols
= STRIP_ALL
;
1852 strip_symbols
= STRIP_DEBUG
;
1854 case OPTION_STRIP_UNNEEDED
:
1855 strip_symbols
= STRIP_UNNEEDED
;
1858 add_specific_symbol (optarg
, &keep_specific_list
);
1861 add_specific_symbol (optarg
, &strip_specific_list
);
1864 add_specific_symbol (optarg
, &localize_specific_list
);
1867 add_specific_symbol (optarg
, &weaken_specific_list
);
1870 preserve_dates
= true;
1873 discard_locals
= LOCALS_ALL
;
1876 discard_locals
= LOCALS_START_L
;
1882 show_version
= true;
1887 case OPTION_ADD_SECTION
:
1891 struct section_add
*pa
;
1896 s
= strchr (optarg
, '=');
1899 fatal (_("bad format for --add-section NAME=FILENAME"));
1901 if (stat (s
+ 1, & st
) < 0)
1902 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
1904 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1907 name
= (char *) xmalloc (len
+ 1);
1908 strncpy (name
, optarg
, len
);
1912 pa
->filename
= s
+ 1;
1914 pa
->size
= st
.st_size
;
1916 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1917 f
= fopen (pa
->filename
, FOPEN_RB
);
1920 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
1922 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1924 fatal (_("%s: fread failed"), pa
->filename
);
1928 pa
->next
= add_sections
;
1932 case OPTION_CHANGE_START
:
1933 change_start
= parse_vma (optarg
, "--change-start");
1935 case OPTION_CHANGE_SECTION_ADDRESS
:
1936 case OPTION_CHANGE_SECTION_LMA
:
1937 case OPTION_CHANGE_SECTION_VMA
:
1944 enum change_action what
;
1948 case OPTION_CHANGE_SECTION_ADDRESS
: option
= "--change-section-address"; break;
1949 case OPTION_CHANGE_SECTION_LMA
: option
= "--change-section-lma"; break;
1950 case OPTION_CHANGE_SECTION_VMA
: option
= "--change-section-vma"; break;
1953 s
= strchr (optarg
, '=');
1956 s
= strchr (optarg
, '+');
1959 s
= strchr (optarg
, '-');
1961 fatal (_("bad format for %s"), option
);
1966 name
= (char *) xmalloc (len
+ 1);
1967 strncpy (name
, optarg
, len
);
1970 p
= find_section_list (name
, true);
1972 val
= parse_vma (s
+ 1, option
);
1976 case '=': what
= CHANGE_SET
; break;
1977 case '-': val
= - val
; /* Drop through. */
1978 case '+': what
= CHANGE_MODIFY
; break;
1983 case OPTION_CHANGE_SECTION_ADDRESS
:
1984 p
->change_vma
= what
;
1988 case OPTION_CHANGE_SECTION_LMA
:
1989 p
->change_lma
= what
;
1993 case OPTION_CHANGE_SECTION_VMA
:
1994 p
->change_vma
= what
;
2000 case OPTION_CHANGE_ADDRESSES
:
2001 change_section_address
= parse_vma (optarg
, "--change-addresses");
2002 change_start
= change_section_address
;
2004 case OPTION_CHANGE_WARNINGS
:
2007 case OPTION_CHANGE_LEADING_CHAR
:
2008 change_leading_char
= true;
2010 case OPTION_DEBUGGING
:
2011 convert_debugging
= true;
2013 case OPTION_GAP_FILL
:
2015 bfd_vma gap_fill_vma
;
2017 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2018 gap_fill
= (bfd_byte
) gap_fill_vma
;
2019 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2023 sprintf_vma (buff
, gap_fill_vma
);
2025 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2028 gap_fill_set
= true;
2031 case OPTION_NO_CHANGE_WARNINGS
:
2032 change_warn
= false;
2035 pad_to
= parse_vma (optarg
, "--pad-to");
2038 case OPTION_REMOVE_LEADING_CHAR
:
2039 remove_leading_char
= true;
2041 case OPTION_SET_SECTION_FLAGS
:
2047 s
= strchr (optarg
, '=');
2049 fatal (_("bad format for --set-section-flags"));
2052 name
= (char *) xmalloc (len
+ 1);
2053 strncpy (name
, optarg
, len
);
2056 p
= find_section_list (name
, true);
2058 p
->set_flags
= true;
2059 p
->flags
= parse_flags (s
+ 1);
2062 case OPTION_SET_START
:
2063 set_start
= parse_vma (optarg
, "--set-start");
2064 set_start_set
= true;
2067 break; /* we've been given a long option */
2069 copy_usage (stdout
, 0);
2071 copy_usage (stderr
, 1);
2076 print_version ("objcopy");
2078 if (copy_byte
>= interleave
)
2079 fatal (_("byte number must be less than interleave"));
2081 if (optind
== argc
|| optind
+ 2 < argc
)
2082 copy_usage (stderr
, 1);
2084 input_filename
= argv
[optind
];
2085 if (optind
+ 1 < argc
)
2086 output_filename
= argv
[optind
+ 1];
2088 /* Default is to strip no symbols. */
2089 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2090 strip_symbols
= STRIP_NONE
;
2092 if (output_target
== (char *) NULL
)
2093 output_target
= input_target
;
2097 if (stat (input_filename
, &statbuf
) < 0)
2098 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2101 /* If there is no destination file then create a temp and rename
2102 the result into the input. */
2104 if (output_filename
== (char *) NULL
)
2106 char *tmpname
= make_tempname (input_filename
);
2108 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2112 set_times (tmpname
, &statbuf
);
2113 smart_rename (tmpname
, input_filename
);
2120 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2121 if (status
== 0 && preserve_dates
)
2122 set_times (output_filename
, &statbuf
);
2127 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2131 if (p
->change_vma
!= CHANGE_IGNORE
)
2135 sprintf_vma (buff
, p
->vma_val
);
2137 /* xgettext:c-format */
2138 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
2140 p
->change_vma
== CHANGE_SET
? '=' : '+',
2144 if (p
->change_lma
!= CHANGE_IGNORE
)
2148 sprintf_vma (buff
, p
->lma_val
);
2150 /* xgettext:c-format */
2151 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
2153 p
->change_lma
== CHANGE_SET
? '=' : '+',
2168 #ifdef HAVE_SETLOCALE
2169 setlocale (LC_MESSAGES
, "");
2171 bindtextdomain (PACKAGE
, LOCALEDIR
);
2172 textdomain (PACKAGE
);
2174 program_name
= argv
[0];
2175 xmalloc_set_program_name (program_name
);
2177 START_PROGRESS (program_name
, 0);
2179 strip_symbols
= STRIP_UNDEF
;
2180 discard_locals
= LOCALS_UNDEF
;
2183 set_default_bfd_target ();
2187 int i
= strlen (program_name
);
2188 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2192 strip_main (argc
, argv
);
2194 copy_main (argc
, argv
);
2196 END_PROGRESS (program_name
);