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
);
966 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
967 l
->name
= output_name
;
971 if (output_bfd
== (bfd
*) NULL
)
972 RETURN_NONFATAL (output_name
);
974 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
975 RETURN_NONFATAL (bfd_get_filename (obfd
));
977 if (bfd_check_format (this_element
, bfd_object
) == true)
978 copy_object (this_element
, output_bfd
);
980 if (!bfd_close (output_bfd
))
982 bfd_nonfatal (bfd_get_filename (output_bfd
));
983 /* Error in new object file. Don't change archive. */
987 /* Open the newly output file and attach to our list. */
988 output_bfd
= bfd_openr (output_name
, output_target
);
990 l
->obfd
= output_bfd
;
993 ptr
= &output_bfd
->next
;
995 last_element
= this_element
;
997 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
999 bfd_close (last_element
);
1001 *ptr
= (bfd
*) NULL
;
1003 if (!bfd_close (obfd
))
1004 RETURN_NONFATAL (bfd_get_filename (obfd
));
1006 if (!bfd_close (ibfd
))
1007 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1009 /* Delete all the files that we opened. */
1010 for (l
= list
; l
!= NULL
; l
= l
->next
)
1012 bfd_close (l
->obfd
);
1018 /* The top-level control. */
1021 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1022 const char *input_filename
;
1023 const char *output_filename
;
1024 const char *input_target
;
1025 const char *output_target
;
1030 /* To allow us to do "strip *" without dying on the first
1031 non-object file, failures are nonfatal. */
1033 ibfd
= bfd_openr (input_filename
, input_target
);
1035 RETURN_NONFATAL (input_filename
);
1037 if (bfd_check_format (ibfd
, bfd_archive
))
1041 /* bfd_get_target does not return the correct value until
1042 bfd_check_format succeeds. */
1043 if (output_target
== NULL
)
1044 output_target
= bfd_get_target (ibfd
);
1046 obfd
= bfd_openw (output_filename
, output_target
);
1048 RETURN_NONFATAL (output_filename
);
1050 copy_archive (ibfd
, obfd
, output_target
);
1052 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1056 /* bfd_get_target does not return the correct value until
1057 bfd_check_format succeeds. */
1058 if (output_target
== NULL
)
1059 output_target
= bfd_get_target (ibfd
);
1061 obfd
= bfd_openw (output_filename
, output_target
);
1063 RETURN_NONFATAL (output_filename
);
1065 copy_object (ibfd
, obfd
);
1067 if (!bfd_close (obfd
))
1068 RETURN_NONFATAL (output_filename
);
1070 if (!bfd_close (ibfd
))
1071 RETURN_NONFATAL (input_filename
);
1075 bfd_nonfatal (input_filename
);
1077 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1079 list_matching_formats (matching
);
1087 /* Create a section in OBFD with the same name and attributes
1088 as ISECTION in IBFD. */
1091 setup_section (ibfd
, isection
, obfdarg
)
1096 bfd
*obfd
= (bfd
*) obfdarg
;
1097 struct section_list
*p
;
1105 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1106 && (strip_symbols
== STRIP_DEBUG
1107 || strip_symbols
== STRIP_UNNEEDED
1108 || strip_symbols
== STRIP_ALL
1109 || discard_locals
== LOCALS_ALL
1110 || convert_debugging
))
1113 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1117 if (p
!= NULL
&& p
->remove
)
1120 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1122 if (osection
== NULL
)
1128 size
= bfd_section_size (ibfd
, isection
);
1130 size
= (size
+ interleave
- 1) / interleave
;
1131 if (! bfd_set_section_size (obfd
, osection
, size
))
1137 vma
= bfd_section_vma (ibfd
, isection
);
1138 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1140 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1143 vma
+= change_section_address
;
1145 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1151 lma
= isection
->lma
;
1152 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1154 if (p
->change_lma
== CHANGE_MODIFY
)
1156 else if (p
->change_lma
== CHANGE_SET
)
1162 lma
+= change_section_address
;
1164 osection
->lma
= lma
;
1166 /* FIXME: This is probably not enough. If we change the LMA we
1167 may have to recompute the header for the file as well. */
1168 if (bfd_set_section_alignment (obfd
,
1170 bfd_section_alignment (ibfd
, isection
))
1177 flags
= bfd_get_section_flags (ibfd
, isection
);
1178 if (p
!= NULL
&& p
->set_flags
)
1179 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1180 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1186 /* This used to be mangle_section; we do here to avoid using
1187 bfd_get_section_by_name since some formats allow multiple
1188 sections with the same name. */
1189 isection
->output_section
= osection
;
1190 isection
->output_offset
= 0;
1192 /* Allow the BFD backend to copy any private data it understands
1193 from the input section to the output section. */
1194 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1196 err
= "private data";
1204 non_fatal (_("%s: section `%s': error in %s: %s"),
1205 bfd_get_filename (ibfd
),
1206 bfd_section_name (ibfd
, isection
),
1207 err
, bfd_errmsg (bfd_get_error ()));
1211 /* Copy the data of input section ISECTION of IBFD
1212 to an output section with the same name in OBFD.
1213 If stripping then don't copy any relocation info. */
1216 copy_section (ibfd
, isection
, obfdarg
)
1221 bfd
*obfd
= (bfd
*) obfdarg
;
1222 struct section_list
*p
;
1229 /* If we have already failed earlier on, do not keep on generating
1234 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1235 && (strip_symbols
== STRIP_DEBUG
1236 || strip_symbols
== STRIP_UNNEEDED
1237 || strip_symbols
== STRIP_ALL
1238 || discard_locals
== LOCALS_ALL
1239 || convert_debugging
))
1244 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1246 if (p
!= NULL
&& p
->remove
)
1249 osection
= isection
->output_section
;
1250 size
= bfd_get_section_size_before_reloc (isection
);
1252 if (size
== 0 || osection
== 0)
1256 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1258 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1261 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1264 relpp
= (arelent
**) xmalloc (relsize
);
1265 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1267 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1269 if (strip_symbols
== STRIP_ALL
)
1271 /* Remove relocations which are not in
1272 keep_strip_specific_list. */
1273 arelent
**temp_relpp
;
1274 long temp_relcount
= 0;
1277 temp_relpp
= (arelent
**) xmalloc (relsize
);
1278 for (i
= 0; i
< relcount
; i
++)
1279 if (is_specified_symbol
1280 (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1281 keep_specific_list
))
1282 temp_relpp
[temp_relcount
++] = relpp
[i
];
1283 relcount
= temp_relcount
;
1287 bfd_set_reloc (obfd
, osection
,
1288 (relcount
== 0 ? (arelent
**) NULL
: relpp
), relcount
);
1291 isection
->_cooked_size
= isection
->_raw_size
;
1292 isection
->reloc_done
= true;
1294 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1296 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1298 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1300 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1303 filter_bytes (memhunk
, &size
);
1305 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1307 RETURN_NONFATAL (bfd_get_filename (obfd
));
1311 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1313 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1315 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1316 flag--they can just remove the section entirely and add it
1317 back again. However, we do permit them to turn on the
1318 SEC_HAS_CONTENTS flag, and take it to mean that the section
1319 contents should be zeroed out. */
1321 memset (memhunk
, 0, size
);
1322 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1324 RETURN_NONFATAL (bfd_get_filename (obfd
));
1329 /* Get all the sections. This is used when --gap-fill or --pad-to is
1333 get_sections (obfd
, osection
, secppparg
)
1338 asection
***secppp
= (asection
***) secppparg
;
1340 **secppp
= osection
;
1344 /* Sort sections by VMA. This is called via qsort, and is used when
1345 --gap-fill or --pad-to is used. We force non loadable or empty
1346 sections to the front, where they are easier to ignore. */
1349 compare_section_lma (arg1
, arg2
)
1353 const asection
**sec1
= (const asection
**) arg1
;
1354 const asection
**sec2
= (const asection
**) arg2
;
1355 flagword flags1
, flags2
;
1357 /* Sort non loadable sections to the front. */
1358 flags1
= (*sec1
)->flags
;
1359 flags2
= (*sec2
)->flags
;
1360 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1361 || (flags1
& SEC_LOAD
) == 0)
1363 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1364 && (flags2
& SEC_LOAD
) != 0)
1369 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1370 || (flags2
& SEC_LOAD
) == 0)
1374 /* Sort sections by LMA. */
1375 if ((*sec1
)->lma
> (*sec2
)->lma
)
1377 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1380 /* Sort sections with the same LMA by size. */
1381 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1383 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1389 /* Mark all the symbols which will be used in output relocations with
1390 the BSF_KEEP flag so that those symbols will not be stripped.
1392 Ignore relocations which will not appear in the output file. */
1395 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1400 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1405 /* Ignore an input section with no corresponding output section. */
1406 if (isection
->output_section
== NULL
)
1409 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1411 bfd_fatal (bfd_get_filename (ibfd
));
1416 relpp
= (arelent
**) xmalloc (relsize
);
1417 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1419 bfd_fatal (bfd_get_filename (ibfd
));
1421 /* Examine each symbol used in a relocation. If it's not one of the
1422 special bfd section symbols, then mark it with BSF_KEEP. */
1423 for (i
= 0; i
< relcount
; i
++)
1425 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1426 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1427 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1428 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1435 /* Write out debugging information. */
1438 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1444 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1445 return write_ieee_debugging_info (obfd
, dhandle
);
1447 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1448 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1450 bfd_byte
*syms
, *strings
;
1451 bfd_size_type symsize
, stringsize
;
1452 asection
*stabsec
, *stabstrsec
;
1454 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1459 stabsec
= bfd_make_section (obfd
, ".stab");
1460 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1462 || stabstrsec
== NULL
1463 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1464 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1465 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1466 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1467 || ! bfd_set_section_flags (obfd
, stabsec
,
1471 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1476 non_fatal (_("%s: can't create debugging section: %s"),
1477 bfd_get_filename (obfd
),
1478 bfd_errmsg (bfd_get_error ()));
1482 /* We can get away with setting the section contents now because
1483 the next thing the caller is going to do is copy over the
1484 real sections. We may someday have to split the contents
1485 setting out of this function. */
1486 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1488 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1489 (file_ptr
) 0, stringsize
))
1491 non_fatal (_("%s: can't set debugging section contents: %s"),
1492 bfd_get_filename (obfd
),
1493 bfd_errmsg (bfd_get_error ()));
1500 non_fatal (_("%s: don't know how to write debugging information for %s"),
1501 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1506 strip_main (argc
, argv
)
1510 char *input_target
= NULL
, *output_target
= NULL
;
1511 boolean show_version
= false;
1513 struct section_list
*p
;
1514 char *output_file
= NULL
;
1516 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpgxXVv",
1517 strip_options
, (int *) 0)) != EOF
)
1522 input_target
= optarg
;
1525 output_target
= optarg
;
1528 input_target
= output_target
= optarg
;
1531 p
= find_section_list (optarg
, true);
1533 sections_removed
= true;
1536 strip_symbols
= STRIP_ALL
;
1540 strip_symbols
= STRIP_DEBUG
;
1542 case OPTION_STRIP_UNNEEDED
:
1543 strip_symbols
= STRIP_UNNEEDED
;
1546 add_specific_symbol (optarg
, &keep_specific_list
);
1549 add_specific_symbol (optarg
, &strip_specific_list
);
1552 output_file
= optarg
;
1555 preserve_dates
= true;
1558 discard_locals
= LOCALS_ALL
;
1561 discard_locals
= LOCALS_START_L
;
1567 show_version
= true;
1570 break; /* we've been given a long option */
1572 strip_usage (stdout
, 0);
1574 strip_usage (stderr
, 1);
1579 print_version ("strip");
1581 /* Default is to strip all symbols. */
1582 if (strip_symbols
== STRIP_UNDEF
1583 && discard_locals
== LOCALS_UNDEF
1584 && strip_specific_list
== NULL
)
1585 strip_symbols
= STRIP_ALL
;
1587 if (output_target
== (char *) NULL
)
1588 output_target
= input_target
;
1592 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1593 strip_usage (stderr
, 1);
1595 for (; i
< argc
; i
++)
1597 int hold_status
= status
;
1598 struct stat statbuf
;
1603 if (stat (argv
[i
], &statbuf
) < 0)
1605 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
1610 if (output_file
!= NULL
)
1611 tmpname
= output_file
;
1613 tmpname
= make_tempname (argv
[i
]);
1616 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1620 set_times (tmpname
, &statbuf
);
1621 if (output_file
== NULL
)
1622 smart_rename (tmpname
, argv
[i
], preserve_dates
);
1623 status
= hold_status
;
1627 if (output_file
== NULL
)
1635 copy_main (argc
, argv
)
1639 char *input_filename
= NULL
, *output_filename
= NULL
;
1640 char *input_target
= NULL
, *output_target
= NULL
;
1641 boolean show_version
= false;
1642 boolean change_warn
= true;
1644 struct section_list
*p
;
1645 struct stat statbuf
;
1647 while ((c
= getopt_long (argc
, argv
, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1648 copy_options
, (int *) 0)) != EOF
)
1653 copy_byte
= atoi (optarg
);
1655 fatal (_("byte number must be non-negative"));
1658 interleave
= atoi (optarg
);
1660 fatal (_("interleave must be positive"));
1663 case 's': /* "source" - 'I' is preferred */
1664 input_target
= optarg
;
1667 case 'd': /* "destination" - 'O' is preferred */
1668 output_target
= optarg
;
1671 input_target
= output_target
= optarg
;
1674 p
= find_section_list (optarg
, true);
1676 sections_removed
= true;
1679 strip_symbols
= STRIP_ALL
;
1682 strip_symbols
= STRIP_DEBUG
;
1684 case OPTION_STRIP_UNNEEDED
:
1685 strip_symbols
= STRIP_UNNEEDED
;
1688 add_specific_symbol (optarg
, &keep_specific_list
);
1691 add_specific_symbol (optarg
, &strip_specific_list
);
1694 add_specific_symbol (optarg
, &localize_specific_list
);
1697 add_specific_symbol (optarg
, &weaken_specific_list
);
1700 preserve_dates
= true;
1703 discard_locals
= LOCALS_ALL
;
1706 discard_locals
= LOCALS_START_L
;
1712 show_version
= true;
1717 case OPTION_ADD_SECTION
:
1721 struct section_add
*pa
;
1726 s
= strchr (optarg
, '=');
1729 fatal (_("bad format for --add-section NAME=FILENAME"));
1731 if (stat (s
+ 1, & st
) < 0)
1732 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
1734 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
1737 name
= (char *) xmalloc (len
+ 1);
1738 strncpy (name
, optarg
, len
);
1742 pa
->filename
= s
+ 1;
1744 pa
->size
= st
.st_size
;
1746 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
1747 f
= fopen (pa
->filename
, FOPEN_RB
);
1750 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
1752 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
1754 fatal (_("%s: fread failed"), pa
->filename
);
1758 pa
->next
= add_sections
;
1762 case OPTION_CHANGE_START
:
1763 change_start
= parse_vma (optarg
, "--change-start");
1765 case OPTION_CHANGE_SECTION_ADDRESS
:
1766 case OPTION_CHANGE_SECTION_LMA
:
1767 case OPTION_CHANGE_SECTION_VMA
:
1774 enum change_action what
;
1778 case OPTION_CHANGE_SECTION_ADDRESS
: option
= "--change-section-address"; break;
1779 case OPTION_CHANGE_SECTION_LMA
: option
= "--change-section-lma"; break;
1780 case OPTION_CHANGE_SECTION_VMA
: option
= "--change-section-vma"; break;
1783 s
= strchr (optarg
, '=');
1786 s
= strchr (optarg
, '+');
1789 s
= strchr (optarg
, '-');
1791 fatal (_("bad format for %s"), option
);
1796 name
= (char *) xmalloc (len
+ 1);
1797 strncpy (name
, optarg
, len
);
1800 p
= find_section_list (name
, true);
1802 val
= parse_vma (s
+ 1, option
);
1806 case '=': what
= CHANGE_SET
; break;
1807 case '-': val
= - val
; /* Drop through. */
1808 case '+': what
= CHANGE_MODIFY
; break;
1813 case OPTION_CHANGE_SECTION_ADDRESS
:
1814 p
->change_vma
= what
;
1818 case OPTION_CHANGE_SECTION_LMA
:
1819 p
->change_lma
= what
;
1823 case OPTION_CHANGE_SECTION_VMA
:
1824 p
->change_vma
= what
;
1830 case OPTION_CHANGE_ADDRESSES
:
1831 change_section_address
= parse_vma (optarg
, "--change-addresses");
1832 change_start
= change_section_address
;
1834 case OPTION_CHANGE_WARNINGS
:
1837 case OPTION_CHANGE_LEADING_CHAR
:
1838 change_leading_char
= true;
1840 case OPTION_DEBUGGING
:
1841 convert_debugging
= true;
1843 case OPTION_GAP_FILL
:
1845 bfd_vma gap_fill_vma
;
1847 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
1848 gap_fill
= (bfd_byte
) gap_fill_vma
;
1849 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
1853 sprintf_vma (buff
, gap_fill_vma
);
1855 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
1858 gap_fill_set
= true;
1861 case OPTION_NO_CHANGE_WARNINGS
:
1862 change_warn
= false;
1865 pad_to
= parse_vma (optarg
, "--pad-to");
1868 case OPTION_REMOVE_LEADING_CHAR
:
1869 remove_leading_char
= true;
1871 case OPTION_SET_SECTION_FLAGS
:
1877 s
= strchr (optarg
, '=');
1879 fatal (_("bad format for --set-section-flags"));
1882 name
= (char *) xmalloc (len
+ 1);
1883 strncpy (name
, optarg
, len
);
1886 p
= find_section_list (name
, true);
1888 p
->set_flags
= true;
1889 p
->flags
= parse_flags (s
+ 1);
1892 case OPTION_SET_START
:
1893 set_start
= parse_vma (optarg
, "--set-start");
1894 set_start_set
= true;
1897 break; /* we've been given a long option */
1899 copy_usage (stdout
, 0);
1901 copy_usage (stderr
, 1);
1906 print_version ("objcopy");
1908 if (copy_byte
>= interleave
)
1909 fatal (_("byte number must be less than interleave"));
1911 if (optind
== argc
|| optind
+ 2 < argc
)
1912 copy_usage (stderr
, 1);
1914 input_filename
= argv
[optind
];
1915 if (optind
+ 1 < argc
)
1916 output_filename
= argv
[optind
+ 1];
1918 /* Default is to strip no symbols. */
1919 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
1920 strip_symbols
= STRIP_NONE
;
1922 if (output_target
== (char *) NULL
)
1923 output_target
= input_target
;
1927 if (stat (input_filename
, &statbuf
) < 0)
1928 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
1931 /* If there is no destination file then create a temp and rename
1932 the result into the input. */
1934 if (output_filename
== (char *) NULL
)
1936 char *tmpname
= make_tempname (input_filename
);
1938 copy_file (input_filename
, tmpname
, input_target
, output_target
);
1942 set_times (tmpname
, &statbuf
);
1943 smart_rename (tmpname
, input_filename
, preserve_dates
);
1950 copy_file (input_filename
, output_filename
, input_target
, output_target
);
1951 if (status
== 0 && preserve_dates
)
1952 set_times (output_filename
, &statbuf
);
1957 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
1961 if (p
->change_vma
!= CHANGE_IGNORE
)
1965 sprintf_vma (buff
, p
->vma_val
);
1967 /* xgettext:c-format */
1968 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
1970 p
->change_vma
== CHANGE_SET
? '=' : '+',
1974 if (p
->change_lma
!= CHANGE_IGNORE
)
1978 sprintf_vma (buff
, p
->lma_val
);
1980 /* xgettext:c-format */
1981 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
1983 p
->change_lma
== CHANGE_SET
? '=' : '+',
1998 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1999 setlocale (LC_MESSAGES
, "");
2001 bindtextdomain (PACKAGE
, LOCALEDIR
);
2002 textdomain (PACKAGE
);
2004 program_name
= argv
[0];
2005 xmalloc_set_program_name (program_name
);
2007 START_PROGRESS (program_name
, 0);
2009 strip_symbols
= STRIP_UNDEF
;
2010 discard_locals
= LOCALS_UNDEF
;
2013 set_default_bfd_target ();
2017 int i
= strlen (program_name
);
2018 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip") == 0);
2022 strip_main (argc
, argv
);
2024 copy_main (argc
, argv
);
2026 END_PROGRESS (program_name
);