1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
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 /* A list of symbols to explicitly strip out, or to keep. A linked
31 list is good enough for a small number from the command line, but
32 this will slow things down a lot if many symbols are being
41 static void copy_usage
PARAMS ((FILE *, int));
42 static void strip_usage
PARAMS ((FILE *, int));
43 static flagword parse_flags
PARAMS ((const char *));
44 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
45 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
46 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
47 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
48 static int compare_section_lma
PARAMS ((const PTR
, const PTR
));
49 static void add_specific_symbol
PARAMS ((const char *, struct symlist
**));
50 static boolean is_specified_symbol
PARAMS ((const char *, struct symlist
*));
51 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
52 static unsigned int filter_symbols
53 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
54 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
55 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
56 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
57 static void copy_object
PARAMS ((bfd
*, bfd
*));
58 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
60 PARAMS ((const char *, const char *, const char *, const char *));
61 static int strip_main
PARAMS ((int, char **));
62 static int copy_main
PARAMS ((int, char **));
64 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
66 static asymbol
**isympp
= NULL
; /* Input symbols */
67 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
69 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
70 static int copy_byte
= -1;
71 static int interleave
= 4;
73 static boolean verbose
; /* Print file and target names. */
74 static boolean preserve_dates
; /* Preserve input file timestamp. */
75 static int status
= 0; /* Exit status. */
80 STRIP_NONE
, /* don't strip */
81 STRIP_DEBUG
, /* strip all debugger symbols */
82 STRIP_UNNEEDED
, /* strip unnecessary symbols */
83 STRIP_ALL
/* strip all symbols */
86 /* Which symbols to remove. */
87 static enum strip_action strip_symbols
;
92 LOCALS_START_L
, /* discard locals starting with L */
93 LOCALS_ALL
/* discard all locals */
96 /* Which local symbols to remove. Overrides STRIP_ALL. */
97 static enum locals_action discard_locals
;
99 /* What kind of change to perform. */
107 /* Structure used to hold lists of sections and actions to take. */
110 struct section_list
* next
; /* Next section to change. */
111 const char * name
; /* Section name. */
112 boolean used
; /* Whether this entry was used. */
113 boolean remove
; /* Whether to remove this section. */
114 enum change_action change_vma
;/* Whether to change or set VMA. */
115 bfd_vma vma_val
; /* Amount to change by or set to. */
116 enum change_action change_lma
;/* Whether to change or set LMA. */
117 bfd_vma lma_val
; /* Amount to change by or set to. */
118 boolean set_flags
; /* Whether to set the section flags. */
119 flagword flags
; /* What to set the section flags to. */
122 static struct section_list
*change_sections
;
123 static boolean sections_removed
;
125 /* Changes to the start address. */
126 static bfd_vma change_start
= 0;
127 static boolean set_start_set
= false;
128 static bfd_vma set_start
;
130 /* Changes to section addresses. */
131 static bfd_vma change_section_address
= 0;
133 /* Filling gaps between sections. */
134 static boolean gap_fill_set
= false;
135 static bfd_byte gap_fill
= 0;
137 /* Pad to a given address. */
138 static boolean pad_to_set
= false;
139 static bfd_vma pad_to
;
141 /* List of sections to add. */
145 /* Next section to add. */
146 struct section_add
*next
;
147 /* Name of section to add. */
149 /* Name of file holding section contents. */
150 const char *filename
;
153 /* Contents of file. */
155 /* BFD section, after it has been added. */
159 static struct section_add
*add_sections
;
161 /* Whether to convert debugging information. */
163 static boolean convert_debugging
= false;
165 /* Whether to change the leading character in symbol names. */
167 static boolean change_leading_char
= false;
169 /* Whether to remove the leading character from global symbol names. */
171 static boolean remove_leading_char
= false;
173 /* List of symbols to strip, keep, localize, and weaken. */
175 static struct symlist
*strip_specific_list
= NULL
;
176 static struct symlist
*keep_specific_list
= NULL
;
177 static struct symlist
*localize_specific_list
= NULL
;
178 static struct symlist
*weaken_specific_list
= NULL
;
180 /* If this is true, we weaken global symbols (set BSF_WEAK). */
182 static boolean weaken
= false;
184 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
186 #define OPTION_ADD_SECTION 150
187 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
188 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
189 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
190 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
191 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
192 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
193 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
194 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
195 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
196 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
197 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
198 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
199 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
200 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
201 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
202 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
204 /* Options to handle if running as "strip". */
206 static struct option strip_options
[] =
208 {"discard-all", no_argument
, 0, 'x'},
209 {"discard-locals", no_argument
, 0, 'X'},
210 {"format", required_argument
, 0, 'F'}, /* Obsolete */
211 {"help", no_argument
, 0, 'h'},
212 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
213 {"input-target", required_argument
, 0, 'I'},
214 {"keep-symbol", required_argument
, 0, 'K'},
215 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
216 {"output-target", required_argument
, 0, 'O'},
217 {"preserve-dates", no_argument
, 0, 'p'},
218 {"remove-section", required_argument
, 0, 'R'},
219 {"strip-all", no_argument
, 0, 's'},
220 {"strip-debug", no_argument
, 0, 'S'},
221 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
222 {"strip-symbol", required_argument
, 0, 'N'},
223 {"target", required_argument
, 0, 'F'},
224 {"verbose", no_argument
, 0, 'v'},
225 {"version", no_argument
, 0, 'V'},
226 {0, no_argument
, 0, 0}
229 /* Options to handle if running as "objcopy". */
231 static struct option copy_options
[] =
233 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
234 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
235 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
236 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
237 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
238 {"byte", required_argument
, 0, 'b'},
239 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
240 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
241 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
242 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
243 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
244 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
245 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
246 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
247 {"discard-all", no_argument
, 0, 'x'},
248 {"discard-locals", no_argument
, 0, 'X'},
249 {"format", required_argument
, 0, 'F'}, /* Obsolete */
250 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
251 {"help", no_argument
, 0, 'h'},
252 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
253 {"input-target", required_argument
, 0, 'I'},
254 {"interleave", required_argument
, 0, 'i'},
255 {"keep-symbol", required_argument
, 0, 'K'},
256 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
257 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
258 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
259 {"output-target", required_argument
, 0, 'O'},
260 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
261 {"preserve-dates", no_argument
, 0, 'p'},
262 {"localize-symbol", required_argument
, 0, 'L'},
263 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
264 {"remove-section", required_argument
, 0, 'R'},
265 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
266 {"set-start", required_argument
, 0, OPTION_SET_START
},
267 {"strip-all", no_argument
, 0, 'S'},
268 {"strip-debug", no_argument
, 0, 'g'},
269 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
270 {"strip-symbol", required_argument
, 0, 'N'},
271 {"target", required_argument
, 0, 'F'},
272 {"verbose", no_argument
, 0, 'v'},
273 {"version", no_argument
, 0, 'V'},
274 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
275 {"weaken-symbol", required_argument
, 0, 'W'},
276 {0, no_argument
, 0, 0}
280 extern char *program_name
;
282 /* This flag distinguishes between strip and objcopy:
283 1 means this is 'strip'; 0 means this is 'objcopy'.
284 -1 means if we should use argv[0] to decide. */
289 copy_usage (stream
, exit_status
)
293 fprintf (stream
, _("\
294 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
295 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
296 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
297 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
298 [--discard-locals] [--debugging] [--remove-section=section]\n"),
300 fprintf (stream
, _("\
301 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
302 [--set-start=val] \n\
303 [--change-start=incr] [--change-addresses=incr] \n\
304 (--adjust-start and --adjust-vma are aliases for these two) \n\
305 [--change-section-address=section{=,+,-}val]\n\
306 (--adjust-section-vma is an alias for --change-section-address)\n\
307 [--change-section-lma=section{=,+,-}val]\n\
308 [--change-section-vma=section{=,+,-}val]\n\
309 [--adjust-warnings] [--no-adjust-warnings]\n\
310 [--change-warnings] [--no-change-warnings]\n\
311 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
312 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
313 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
314 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
315 [--verbose] [--version] [--help] in-file [out-file]\n"));
316 list_supported_targets (program_name
, stream
);
317 if (exit_status
== 0)
318 fprintf (stream
, _("Report bugs to bug-gnu-utils@gnu.org\n"));
323 strip_usage (stream
, exit_status
)
327 fprintf (stream
, _("\
328 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
329 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
330 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
331 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
332 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
333 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n"),
335 list_supported_targets (program_name
, stream
);
336 if (exit_status
== 0)
337 fprintf (stream
, _("Report bugs to bug-gnu-utils@gnu.org\n"));
341 /* Parse section flags into a flagword, with a fatal error if the
342 string can't be parsed. */
356 snext
= strchr (s
, ',');
366 #define PARSE_FLAG(fname,fval) \
367 else if (strncasecmp (fname, s, len) == 0) ret |= fval
368 PARSE_FLAG ("alloc", SEC_ALLOC
);
369 PARSE_FLAG ("load", SEC_LOAD
);
370 PARSE_FLAG ("readonly", SEC_READONLY
);
371 PARSE_FLAG ("code", SEC_CODE
);
372 PARSE_FLAG ("data", SEC_DATA
);
373 PARSE_FLAG ("rom", SEC_ROM
);
374 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
380 copy
= xmalloc (len
+ 1);
381 strncpy (copy
, s
, len
);
383 non_fatal (_("unrecognized section flag `%s'"), copy
);
384 fatal (_("supported flags: alloc, load, readonly, code, data, rom, contents"));
394 /* Find and optionally add an entry in the change_sections list. */
396 static struct section_list
*
397 find_section_list (name
, add
)
401 register struct section_list
*p
;
403 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
404 if (strcmp (p
->name
, name
) == 0)
410 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
414 p
->change_vma
= CHANGE_IGNORE
;
415 p
->change_lma
= CHANGE_IGNORE
;
418 p
->set_flags
= false;
421 p
->next
= change_sections
;
427 /* Add a symbol to strip_specific_list. */
430 add_specific_symbol (name
, list
)
432 struct symlist
**list
;
434 struct symlist
*tmp_list
;
436 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
437 tmp_list
->name
= name
;
438 tmp_list
->next
= *list
;
442 /* See whether a symbol should be stripped or kept based on
443 strip_specific_list and keep_symbols. */
446 is_specified_symbol (name
, list
)
448 struct symlist
*list
;
450 struct symlist
*tmp_list
;
452 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
454 if (strcmp (name
, tmp_list
->name
) == 0)
460 /* See if a section is being removed. */
463 is_strip_section (abfd
, sec
)
467 struct section_list
*p
;
469 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
470 && (strip_symbols
== STRIP_DEBUG
471 || strip_symbols
== STRIP_UNNEEDED
472 || strip_symbols
== STRIP_ALL
473 || discard_locals
== LOCALS_ALL
474 || convert_debugging
))
477 if (! sections_removed
)
479 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
480 return p
!= NULL
&& p
->remove
? true : false;
483 /* Choose which symbol entries to copy; put the result in OSYMS.
484 We don't copy in place, because that confuses the relocs.
485 Return the number of symbols to print. */
488 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
491 asymbol
**osyms
, **isyms
;
494 register asymbol
**from
= isyms
, **to
= osyms
;
495 long src_count
= 0, dst_count
= 0;
497 for (; src_count
< symcount
; src_count
++)
499 asymbol
*sym
= from
[src_count
];
500 flagword flags
= sym
->flags
;
501 const char *name
= bfd_asymbol_name (sym
);
504 if (change_leading_char
505 && (bfd_get_symbol_leading_char (abfd
)
506 != bfd_get_symbol_leading_char (obfd
))
507 && (bfd_get_symbol_leading_char (abfd
) == '\0'
508 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
510 if (bfd_get_symbol_leading_char (obfd
) == '\0')
511 name
= bfd_asymbol_name (sym
) = name
+ 1;
516 n
= xmalloc (strlen (name
) + 2);
517 n
[0] = bfd_get_symbol_leading_char (obfd
);
518 if (bfd_get_symbol_leading_char (abfd
) == '\0')
519 strcpy (n
+ 1, name
);
521 strcpy (n
+ 1, name
+ 1);
522 name
= bfd_asymbol_name (sym
) = n
;
526 if (remove_leading_char
527 && ((flags
& BSF_GLOBAL
) != 0
528 || (flags
& BSF_WEAK
) != 0
529 || bfd_is_und_section (bfd_get_section (sym
))
530 || bfd_is_com_section (bfd_get_section (sym
)))
531 && name
[0] == bfd_get_symbol_leading_char (abfd
))
532 name
= bfd_asymbol_name (sym
) = name
+ 1;
534 if (strip_symbols
== STRIP_ALL
)
536 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
537 || ((flags
& BSF_SECTION_SYM
) != 0
538 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
541 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
542 || (flags
& BSF_WEAK
) != 0
543 || bfd_is_und_section (bfd_get_section (sym
))
544 || bfd_is_com_section (bfd_get_section (sym
)))
545 keep
= strip_symbols
!= STRIP_UNNEEDED
;
546 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
547 keep
= (strip_symbols
!= STRIP_DEBUG
548 && strip_symbols
!= STRIP_UNNEEDED
549 && ! convert_debugging
);
550 else /* Local symbol. */
551 keep
= (strip_symbols
!= STRIP_UNNEEDED
552 && (discard_locals
!= LOCALS_ALL
553 && (discard_locals
!= LOCALS_START_L
554 || ! bfd_is_local_label (abfd
, sym
))));
556 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
558 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
560 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
563 if (keep
&& (flags
& BSF_GLOBAL
) != 0
564 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
566 sym
->flags
&=~ BSF_GLOBAL
;
567 sym
->flags
|= BSF_WEAK
;
569 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
570 && is_specified_symbol (name
, localize_specific_list
))
572 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
573 sym
->flags
|= BSF_LOCAL
;
577 to
[dst_count
++] = sym
;
580 to
[dst_count
] = NULL
;
585 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
589 filter_bytes (memhunk
, size
)
593 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
595 for (; from
< end
; from
+= interleave
)
597 if (*size
% interleave
> copy_byte
)
598 *size
= (*size
/ interleave
) + 1;
603 /* Copy object file IBFD onto OBFD. */
606 copy_object (ibfd
, obfd
)
612 asection
**osections
= NULL
;
613 bfd_size_type
*gaps
= NULL
;
614 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
));
652 if (osympp
!= isympp
)
655 /* BFD mandates that all output sections be created and sizes set before
656 any output is done. Thus, we traverse all sections multiple times. */
657 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
659 if (add_sections
!= NULL
)
661 struct section_add
*padd
;
662 struct section_list
*pset
;
664 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
666 padd
->section
= bfd_make_section (obfd
, padd
->name
);
667 if (padd
->section
== NULL
)
669 non_fatal (_("can't create section `%s': %s"),
670 padd
->name
, bfd_errmsg (bfd_get_error ()));
678 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
679 RETURN_NONFATAL (bfd_get_filename (obfd
));
681 pset
= find_section_list (padd
->name
, false);
685 if (pset
!= NULL
&& pset
->set_flags
)
686 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
688 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
690 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
691 RETURN_NONFATAL (bfd_get_filename (obfd
));
695 if (pset
->change_vma
!= CHANGE_IGNORE
)
696 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
697 RETURN_NONFATAL (bfd_get_filename (obfd
));
699 if (pset
->change_lma
!= CHANGE_IGNORE
)
701 padd
->section
->lma
= pset
->lma_val
;
703 if (! bfd_set_section_alignment
704 (obfd
, padd
->section
,
705 bfd_section_alignment (obfd
, padd
->section
)))
706 RETURN_NONFATAL (bfd_get_filename (obfd
));
713 if (gap_fill_set
|| pad_to_set
)
718 /* We must fill in gaps between the sections and/or we must pad
719 the last section to a specified address. We do this by
720 grabbing a list of the sections, sorting them by VMA, and
721 increasing the section sizes as required to fill the gaps.
722 We write out the gap contents below. */
724 c
= bfd_count_sections (obfd
);
725 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
727 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
729 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
731 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
732 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
736 for (i
= 0; i
< c
- 1; i
++)
740 bfd_vma gap_start
, gap_stop
;
742 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
743 if ((flags
& SEC_HAS_CONTENTS
) == 0
744 || (flags
& SEC_LOAD
) == 0)
747 size
= bfd_section_size (obfd
, osections
[i
]);
748 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
749 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
750 if (gap_start
< gap_stop
)
752 if (! bfd_set_section_size (obfd
, osections
[i
],
753 size
+ (gap_stop
- gap_start
)))
755 non_fatal (_("Can't fill gap after %s: %s"),
756 bfd_get_section_name (obfd
, osections
[i
]),
757 bfd_errmsg (bfd_get_error ()));
761 gaps
[i
] = gap_stop
- gap_start
;
762 if (max_gap
< gap_stop
- gap_start
)
763 max_gap
= gap_stop
- gap_start
;
773 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
774 size
= bfd_section_size (obfd
, osections
[c
- 1]);
775 if (lma
+ size
< pad_to
)
777 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
780 non_fatal (_("Can't add padding to %s: %s"),
781 bfd_get_section_name (obfd
, osections
[c
- 1]),
782 bfd_errmsg (bfd_get_error ()));
787 gaps
[c
- 1] = pad_to
- (lma
+ size
);
788 if (max_gap
< pad_to
- (lma
+ size
))
789 max_gap
= pad_to
- (lma
+ size
);
795 /* Symbol filtering must happen after the output sections have
796 been created, but before their contents are set. */
798 symsize
= bfd_get_symtab_upper_bound (ibfd
);
800 RETURN_NONFATAL (bfd_get_filename (ibfd
));
802 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
803 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
805 RETURN_NONFATAL (bfd_get_filename (ibfd
));
807 if (convert_debugging
)
808 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
810 if (strip_symbols
== STRIP_DEBUG
811 || strip_symbols
== STRIP_ALL
812 || strip_symbols
== STRIP_UNNEEDED
813 || discard_locals
!= LOCALS_UNDEF
814 || strip_specific_list
!= NULL
815 || keep_specific_list
!= NULL
816 || localize_specific_list
!= NULL
817 || weaken_specific_list
!= NULL
820 || change_leading_char
821 || remove_leading_char
824 /* Mark symbols used in output relocations so that they
825 are kept, even if they are local labels or static symbols.
827 Note we iterate over the input sections examining their
828 relocations since the relocations for the output sections
829 haven't been set yet. mark_symbols_used_in_relocations will
830 ignore input sections which have no corresponding output
832 if (strip_symbols
!= STRIP_ALL
)
833 bfd_map_over_sections (ibfd
,
834 mark_symbols_used_in_relocations
,
836 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
837 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
840 if (convert_debugging
&& dhandle
!= NULL
)
842 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
849 bfd_set_symtab (obfd
, osympp
, symcount
);
851 /* This has to happen after the symbol table has been set. */
852 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
854 if (add_sections
!= NULL
)
856 struct section_add
*padd
;
858 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
860 if (! bfd_set_section_contents (obfd
, padd
->section
,
861 (PTR
) padd
->contents
,
863 (bfd_size_type
) padd
->size
))
864 RETURN_NONFATAL (bfd_get_filename (obfd
));
868 if (gap_fill_set
|| pad_to_set
)
873 /* Fill in the gaps. */
877 buf
= (bfd_byte
*) xmalloc (max_gap
);
878 memset (buf
, gap_fill
, (size_t) max_gap
);
880 c
= bfd_count_sections (obfd
);
881 for (i
= 0; i
< c
; i
++)
889 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
899 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
901 RETURN_NONFATAL (bfd_get_filename (obfd
));
910 /* Allow the BFD backend to copy any private data it understands
911 from the input BFD to the output BFD. This is done last to
912 permit the routine to look at the filtered symbol table, which is
913 important for the ECOFF code at least. */
914 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
916 non_fatal (_("%s: error copying private BFD data: %s"),
917 bfd_get_filename (obfd
),
918 bfd_errmsg (bfd_get_error ()));
924 /* Read each archive element in turn from IBFD, copy the
925 contents to temp file, and keep the temp file handle. */
928 copy_archive (ibfd
, obfd
, output_target
)
931 const char *output_target
;
935 struct name_list
*next
;
939 bfd
**ptr
= &obfd
->archive_head
;
941 char *dir
= make_tempname (bfd_get_filename (obfd
));
943 /* Make a temp directory to hold the contents. */
944 #if defined (_WIN32) && !defined (__CYGWIN32__)
945 if (mkdir (dir
) != 0)
947 if (mkdir (dir
, 0700) != 0)
950 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
951 dir
, strerror (errno
));
953 obfd
->has_armap
= ibfd
->has_armap
;
957 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
958 while (!status
&& this_element
!= (bfd
*) NULL
)
960 /* Create an output file for this member. */
961 char *output_name
= concat (dir
, "/", bfd_get_filename (this_element
),
963 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
970 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
971 if (stat_status
!= 0)
972 non_fatal (_("internal stat error on %s"),
973 bfd_get_filename (this_element
));
976 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
977 l
->name
= output_name
;
981 if (output_bfd
== (bfd
*) NULL
)
982 RETURN_NONFATAL (output_name
);
984 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
985 RETURN_NONFATAL (bfd_get_filename (obfd
));
987 if (bfd_check_format (this_element
, bfd_object
) == true)
988 copy_object (this_element
, output_bfd
);
990 if (!bfd_close (output_bfd
))
992 bfd_nonfatal (bfd_get_filename (output_bfd
));
993 /* Error in new object file. Don't change archive. */
997 if (preserve_dates
&& stat_status
== 0)
998 set_times (output_name
, &buf
);
1000 /* Open the newly output file and attach to our list. */
1001 output_bfd
= bfd_openr (output_name
, output_target
);
1003 l
->obfd
= output_bfd
;
1006 ptr
= &output_bfd
->next
;
1008 last_element
= this_element
;
1010 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1012 bfd_close (last_element
);
1014 *ptr
= (bfd
*) NULL
;
1016 if (!bfd_close (obfd
))
1017 RETURN_NONFATAL (bfd_get_filename (obfd
));
1019 if (!bfd_close (ibfd
))
1020 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1022 /* Delete all the files that we opened. */
1023 for (l
= list
; l
!= NULL
; l
= l
->next
)
1025 bfd_close (l
->obfd
);
1031 /* The top-level control. */
1034 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1035 const char *input_filename
;
1036 const char *output_filename
;
1037 const char *input_target
;
1038 const char *output_target
;
1043 /* To allow us to do "strip *" without dying on the first
1044 non-object file, failures are nonfatal. */
1046 ibfd
= bfd_openr (input_filename
, input_target
);
1048 RETURN_NONFATAL (input_filename
);
1050 if (bfd_check_format (ibfd
, bfd_archive
))
1054 /* bfd_get_target does not return the correct value until
1055 bfd_check_format succeeds. */
1056 if (output_target
== NULL
)
1057 output_target
= bfd_get_target (ibfd
);
1059 obfd
= bfd_openw (output_filename
, output_target
);
1061 RETURN_NONFATAL (output_filename
);
1063 copy_archive (ibfd
, obfd
, output_target
);
1065 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1069 /* bfd_get_target does not return the correct value until
1070 bfd_check_format succeeds. */
1071 if (output_target
== NULL
)
1072 output_target
= bfd_get_target (ibfd
);
1074 obfd
= bfd_openw (output_filename
, output_target
);
1076 RETURN_NONFATAL (output_filename
);
1078 copy_object (ibfd
, obfd
);
1080 if (!bfd_close (obfd
))
1081 RETURN_NONFATAL (output_filename
);
1083 if (!bfd_close (ibfd
))
1084 RETURN_NONFATAL (input_filename
);
1088 bfd_nonfatal (input_filename
);
1090 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1092 list_matching_formats (matching
);
1100 /* Create a section in OBFD with the same name and attributes
1101 as ISECTION in IBFD. */
1104 setup_section (ibfd
, isection
, obfdarg
)
1109 bfd
*obfd
= (bfd
*) obfdarg
;
1110 struct section_list
*p
;
1118 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1119 && (strip_symbols
== STRIP_DEBUG
1120 || strip_symbols
== STRIP_UNNEEDED
1121 || strip_symbols
== STRIP_ALL
1122 || discard_locals
== LOCALS_ALL
1123 || convert_debugging
))
1126 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1130 if (p
!= NULL
&& p
->remove
)
1133 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1135 if (osection
== NULL
)
1141 size
= bfd_section_size (ibfd
, isection
);
1143 size
= (size
+ interleave
- 1) / interleave
;
1144 if (! bfd_set_section_size (obfd
, osection
, size
))
1150 vma
= bfd_section_vma (ibfd
, isection
);
1151 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1153 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1156 vma
+= change_section_address
;
1158 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1164 lma
= isection
->lma
;
1165 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1167 if (p
->change_lma
== CHANGE_MODIFY
)
1169 else if (p
->change_lma
== CHANGE_SET
)
1175 lma
+= change_section_address
;
1177 osection
->lma
= lma
;
1179 /* FIXME: This is probably not enough. If we change the LMA we
1180 may have to recompute the header for the file as well. */
1181 if (bfd_set_section_alignment (obfd
,
1183 bfd_section_alignment (ibfd
, isection
))
1190 flags
= bfd_get_section_flags (ibfd
, isection
);
1191 if (p
!= NULL
&& p
->set_flags
)
1192 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1193 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1199 /* This used to be mangle_section; we do here to avoid using
1200 bfd_get_section_by_name since some formats allow multiple
1201 sections with the same name. */
1202 isection
->output_section
= osection
;
1203 isection
->output_offset
= 0;
1205 /* Allow the BFD backend to copy any private data it understands
1206 from the input section to the output section. */
1207 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1209 err
= "private data";
1217 non_fatal (_("%s: section `%s': error in %s: %s"),
1218 bfd_get_filename (ibfd
),
1219 bfd_section_name (ibfd
, isection
),
1220 err
, bfd_errmsg (bfd_get_error ()));
1224 /* Copy the data of input section ISECTION of IBFD
1225 to an output section with the same name in OBFD.
1226 If stripping then don't copy any relocation info. */
1229 copy_section (ibfd
, isection
, obfdarg
)
1234 bfd
*obfd
= (bfd
*) obfdarg
;
1235 struct section_list
*p
;
1242 /* If we have already failed earlier on, do not keep on generating
1247 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1248 && (strip_symbols
== STRIP_DEBUG
1249 || strip_symbols
== STRIP_UNNEEDED
1250 || strip_symbols
== STRIP_ALL
1251 || discard_locals
== LOCALS_ALL
1252 || convert_debugging
))
1257 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1259 if (p
!= NULL
&& p
->remove
)
1262 osection
= isection
->output_section
;
1263 size
= bfd_get_section_size_before_reloc (isection
);
1265 if (size
== 0 || osection
== 0)
1269 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1271 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1274 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1277 relpp
= (arelent
**) xmalloc (relsize
);
1278 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1280 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1282 if (strip_symbols
== STRIP_ALL
)
1284 /* Remove relocations which are not in
1285 keep_strip_specific_list. */
1286 arelent
**temp_relpp
;
1287 long temp_relcount
= 0;
1290 temp_relpp
= (arelent
**) xmalloc (relsize
);
1291 for (i
= 0; i
< relcount
; i
++)
1292 if (is_specified_symbol
1293 (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1294 keep_specific_list
))
1295 temp_relpp
[temp_relcount
++] = relpp
[i
];
1296 relcount
= temp_relcount
;
1300 bfd_set_reloc (obfd
, osection
,
1301 (relcount
== 0 ? (arelent
**) NULL
: relpp
), relcount
);
1304 isection
->_cooked_size
= isection
->_raw_size
;
1305 isection
->reloc_done
= true;
1307 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1309 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1311 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1313 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1316 filter_bytes (memhunk
, &size
);
1318 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1320 RETURN_NONFATAL (bfd_get_filename (obfd
));
1324 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1326 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1328 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1329 flag--they can just remove the section entirely and add it
1330 back again. However, we do permit them to turn on the
1331 SEC_HAS_CONTENTS flag, and take it to mean that the section
1332 contents should be zeroed out. */
1334 memset (memhunk
, 0, size
);
1335 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1337 RETURN_NONFATAL (bfd_get_filename (obfd
));
1342 /* Get all the sections. This is used when --gap-fill or --pad-to is
1346 get_sections (obfd
, osection
, secppparg
)
1351 asection
***secppp
= (asection
***) secppparg
;
1353 **secppp
= osection
;
1357 /* Sort sections by VMA. This is called via qsort, and is used when
1358 --gap-fill or --pad-to is used. We force non loadable or empty
1359 sections to the front, where they are easier to ignore. */
1362 compare_section_lma (arg1
, arg2
)
1366 const asection
**sec1
= (const asection
**) arg1
;
1367 const asection
**sec2
= (const asection
**) arg2
;
1368 flagword flags1
, flags2
;
1370 /* Sort non loadable sections to the front. */
1371 flags1
= (*sec1
)->flags
;
1372 flags2
= (*sec2
)->flags
;
1373 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1374 || (flags1
& SEC_LOAD
) == 0)
1376 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1377 && (flags2
& SEC_LOAD
) != 0)
1382 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1383 || (flags2
& SEC_LOAD
) == 0)
1387 /* Sort sections by LMA. */
1388 if ((*sec1
)->lma
> (*sec2
)->lma
)
1390 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1393 /* Sort sections with the same LMA by size. */
1394 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1396 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1402 /* Mark all the symbols which will be used in output relocations with
1403 the BSF_KEEP flag so that those symbols will not be stripped.
1405 Ignore relocations which will not appear in the output file. */
1408 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1413 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1418 /* Ignore an input section with no corresponding output section. */
1419 if (isection
->output_section
== NULL
)
1422 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1424 bfd_fatal (bfd_get_filename (ibfd
));
1429 relpp
= (arelent
**) xmalloc (relsize
);
1430 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1432 bfd_fatal (bfd_get_filename (ibfd
));
1434 /* Examine each symbol used in a relocation. If it's not one of the
1435 special bfd section symbols, then mark it with BSF_KEEP. */
1436 for (i
= 0; i
< relcount
; i
++)
1438 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1439 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1440 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1441 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1448 /* Write out debugging information. */
1451 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1457 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1458 return write_ieee_debugging_info (obfd
, dhandle
);
1460 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1461 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1463 bfd_byte
*syms
, *strings
;
1464 bfd_size_type symsize
, stringsize
;
1465 asection
*stabsec
, *stabstrsec
;
1467 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1472 stabsec
= bfd_make_section (obfd
, ".stab");
1473 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1475 || stabstrsec
== NULL
1476 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1477 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1478 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1479 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1480 || ! bfd_set_section_flags (obfd
, stabsec
,
1484 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1489 non_fatal (_("%s: can't create debugging section: %s"),
1490 bfd_get_filename (obfd
),
1491 bfd_errmsg (bfd_get_error ()));
1495 /* We can get away with setting the section contents now because
1496 the next thing the caller is going to do is copy over the
1497 real sections. We may someday have to split the contents
1498 setting out of this function. */
1499 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1501 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1502 (file_ptr
) 0, stringsize
))
1504 non_fatal (_("%s: can't set debugging section contents: %s"),
1505 bfd_get_filename (obfd
),
1506 bfd_errmsg (bfd_get_error ()));
1513 non_fatal (_("%s: don't know how to write debugging information for %s"),
1514 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1519 strip_main (argc
, argv
)
1523 char *input_target
= NULL
, *output_target
= NULL
;
1524 boolean show_version
= false;
1526 struct section_list
*p
;
1527 char *output_file
= NULL
;
1529 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1530 strip_options
, (int *) 0)) != EOF
)
1535 input_target
= optarg
;
1538 output_target
= optarg
;
1541 input_target
= output_target
= optarg
;
1544 p
= find_section_list (optarg
, true);
1546 sections_removed
= true;
1549 strip_symbols
= STRIP_ALL
;
1553 strip_symbols
= STRIP_DEBUG
;
1555 case OPTION_STRIP_UNNEEDED
:
1556 strip_symbols
= STRIP_UNNEEDED
;
1559 add_specific_symbol (optarg
, &keep_specific_list
);
1562 add_specific_symbol (optarg
, &strip_specific_list
);
1565 output_file
= optarg
;
1568 preserve_dates
= true;
1571 discard_locals
= LOCALS_ALL
;
1574 discard_locals
= LOCALS_START_L
;
1580 show_version
= true;
1583 break; /* we've been given a long option */
1585 strip_usage (stdout
, 0);
1587 strip_usage (stderr
, 1);
1592 print_version ("strip");
1594 /* Default is to strip all symbols. */
1595 if (strip_symbols
== STRIP_UNDEF
1596 && discard_locals
== LOCALS_UNDEF
1597 && strip_specific_list
== NULL
)
1598 strip_symbols
= STRIP_ALL
;
1600 if (output_target
== (char *) NULL
)
1601 output_target
= input_target
;
1605 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1606 strip_usage (stderr
, 1);
1608 for (; i
< argc
; i
++)
1610 int hold_status
= status
;
1611 struct stat statbuf
;
1616 if (stat (argv
[i
], &statbuf
) < 0)
1618 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
1623 if (output_file
!= NULL
)
1624 tmpname
= output_file
;
1626 tmpname
= make_tempname (argv
[i
]);
1629 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1633 set_times (tmpname
, &statbuf
);
1634 if (output_file
== NULL
)
1635 smart_rename (tmpname
, argv
[i
], preserve_dates
);
1636 status
= hold_status
;
1640 if (output_file
== NULL
)
1648 copy_main (argc
, argv
)
1652 char *input_filename
= NULL
, *output_filename
= NULL
;
1653 char *input_target
= NULL
, *output_target
= NULL
;
1654 boolean show_version
= false;
1655 boolean change_warn
= true;
1657 struct section_list
*p
;
1658 struct stat statbuf
;
1660 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1661 copy_options
, (int *) 0)) != EOF
)
1666 copy_byte
= atoi (optarg
);
1668 fatal (_("byte number must be non-negative"));
1671 interleave
= atoi (optarg
);
1673 fatal (_("interleave must be positive"));
1676 case 's': /* "source" - 'I' is preferred */
1677 input_target
= optarg
;
1680 case 'd': /* "destination" - 'O' is preferred */
1681 output_target
= optarg
;
1684 input_target
= output_target
= optarg
;
1687 p
= find_section_list (optarg
, true);
1689 sections_removed
= true;
1692 strip_symbols
= STRIP_ALL
;
1695 strip_symbols
= STRIP_DEBUG
;
1697 case OPTION_STRIP_UNNEEDED
:
1698 strip_symbols
= STRIP_UNNEEDED
;
1701 add_specific_symbol (optarg
, &keep_specific_list
);
1704 add_specific_symbol (optarg
, &strip_specific_list
);
1707 add_specific_symbol (optarg
, &localize_specific_list
);
1710 add_specific_symbol (optarg
, &weaken_specific_list
);
1713 preserve_dates
= true;
1716 discard_locals
= LOCALS_ALL
;
1719 discard_locals
= LOCALS_START_L
;
1725 show_version
= true;
1730 case OPTION_ADD_SECTION
:
1734 struct section_add
*pa
;
1739 s
= strchr (optarg
, '=');
1742 fatal (_("bad format for --add-section NAME=FILENAME"));
1744 if (stat (s
+ 1, & st
) < 0)
1745 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
1747 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1750 name
= (char *) xmalloc (len
+ 1);
1751 strncpy (name
, optarg
, len
);
1755 pa
->filename
= s
+ 1;
1757 pa
->size
= st
.st_size
;
1759 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1760 f
= fopen (pa
->filename
, FOPEN_RB
);
1763 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
1765 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1767 fatal (_("%s: fread failed"), pa
->filename
);
1771 pa
->next
= add_sections
;
1775 case OPTION_CHANGE_START
:
1776 change_start
= parse_vma (optarg
, "--change-start");
1778 case OPTION_CHANGE_SECTION_ADDRESS
:
1779 case OPTION_CHANGE_SECTION_LMA
:
1780 case OPTION_CHANGE_SECTION_VMA
:
1787 enum change_action what
;
1791 case OPTION_CHANGE_SECTION_ADDRESS
: option
= "--change-section-address"; break;
1792 case OPTION_CHANGE_SECTION_LMA
: option
= "--change-section-lma"; break;
1793 case OPTION_CHANGE_SECTION_VMA
: option
= "--change-section-vma"; break;
1796 s
= strchr (optarg
, '=');
1799 s
= strchr (optarg
, '+');
1802 s
= strchr (optarg
, '-');
1804 fatal (_("bad format for %s"), option
);
1809 name
= (char *) xmalloc (len
+ 1);
1810 strncpy (name
, optarg
, len
);
1813 p
= find_section_list (name
, true);
1815 val
= parse_vma (s
+ 1, option
);
1819 case '=': what
= CHANGE_SET
; break;
1820 case '-': val
= - val
; /* Drop through. */
1821 case '+': what
= CHANGE_MODIFY
; break;
1826 case OPTION_CHANGE_SECTION_ADDRESS
:
1827 p
->change_vma
= what
;
1831 case OPTION_CHANGE_SECTION_LMA
:
1832 p
->change_lma
= what
;
1836 case OPTION_CHANGE_SECTION_VMA
:
1837 p
->change_vma
= what
;
1843 case OPTION_CHANGE_ADDRESSES
:
1844 change_section_address
= parse_vma (optarg
, "--change-addresses");
1845 change_start
= change_section_address
;
1847 case OPTION_CHANGE_WARNINGS
:
1850 case OPTION_CHANGE_LEADING_CHAR
:
1851 change_leading_char
= true;
1853 case OPTION_DEBUGGING
:
1854 convert_debugging
= true;
1856 case OPTION_GAP_FILL
:
1858 bfd_vma gap_fill_vma
;
1860 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
1861 gap_fill
= (bfd_byte
) gap_fill_vma
;
1862 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
1866 sprintf_vma (buff
, gap_fill_vma
);
1868 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
1871 gap_fill_set
= true;
1874 case OPTION_NO_CHANGE_WARNINGS
:
1875 change_warn
= false;
1878 pad_to
= parse_vma (optarg
, "--pad-to");
1881 case OPTION_REMOVE_LEADING_CHAR
:
1882 remove_leading_char
= true;
1884 case OPTION_SET_SECTION_FLAGS
:
1890 s
= strchr (optarg
, '=');
1892 fatal (_("bad format for --set-section-flags"));
1895 name
= (char *) xmalloc (len
+ 1);
1896 strncpy (name
, optarg
, len
);
1899 p
= find_section_list (name
, true);
1901 p
->set_flags
= true;
1902 p
->flags
= parse_flags (s
+ 1);
1905 case OPTION_SET_START
:
1906 set_start
= parse_vma (optarg
, "--set-start");
1907 set_start_set
= true;
1910 break; /* we've been given a long option */
1912 copy_usage (stdout
, 0);
1914 copy_usage (stderr
, 1);
1919 print_version ("objcopy");
1921 if (copy_byte
>= interleave
)
1922 fatal (_("byte number must be less than interleave"));
1924 if (optind
== argc
|| optind
+ 2 < argc
)
1925 copy_usage (stderr
, 1);
1927 input_filename
= argv
[optind
];
1928 if (optind
+ 1 < argc
)
1929 output_filename
= argv
[optind
+ 1];
1931 /* Default is to strip no symbols. */
1932 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
1933 strip_symbols
= STRIP_NONE
;
1935 if (output_target
== (char *) NULL
)
1936 output_target
= input_target
;
1940 if (stat (input_filename
, &statbuf
) < 0)
1941 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
1944 /* If there is no destination file then create a temp and rename
1945 the result into the input. */
1947 if (output_filename
== (char *) NULL
)
1949 char *tmpname
= make_tempname (input_filename
);
1951 copy_file (input_filename
, tmpname
, input_target
, output_target
);
1955 set_times (tmpname
, &statbuf
);
1956 smart_rename (tmpname
, input_filename
, preserve_dates
);
1963 copy_file (input_filename
, output_filename
, input_target
, output_target
);
1964 if (status
== 0 && preserve_dates
)
1965 set_times (output_filename
, &statbuf
);
1970 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
1974 if (p
->change_vma
!= CHANGE_IGNORE
)
1978 sprintf_vma (buff
, p
->vma_val
);
1980 /* xgettext:c-format */
1981 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
1983 p
->change_vma
== CHANGE_SET
? '=' : '+',
1987 if (p
->change_lma
!= CHANGE_IGNORE
)
1991 sprintf_vma (buff
, p
->lma_val
);
1993 /* xgettext:c-format */
1994 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
1996 p
->change_lma
== CHANGE_SET
? '=' : '+',
2011 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2012 setlocale (LC_MESSAGES
, "");
2014 bindtextdomain (PACKAGE
, LOCALEDIR
);
2015 textdomain (PACKAGE
);
2017 program_name
= argv
[0];
2018 xmalloc_set_program_name (program_name
);
2020 START_PROGRESS (program_name
, 0);
2022 strip_symbols
= STRIP_UNDEF
;
2023 discard_locals
= LOCALS_UNDEF
;
2026 set_default_bfd_target ();
2030 int i
= strlen (program_name
);
2031 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2035 strip_main (argc
, argv
);
2037 copy_main (argc
, argv
);
2039 END_PROGRESS (program_name
);