Initial revision
[binutils.git] / binutils / objcopy.c
blob5fd77775d701a6c2af2e65eb2d8a5f7cc04e3d2f
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
20 02111-1307, USA. */
22 #include "bfd.h"
23 #include "progress.h"
24 #include "bucomm.h"
25 #include "getopt.h"
26 #include "libiberty.h"
27 #include "budbg.h"
28 #include <sys/stat.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
33 deleted. */
35 struct symlist
37 const char *name;
38 struct symlist *next;
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 *));
59 static void copy_file
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. */
77 enum strip_action
79 STRIP_UNDEF,
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;
89 enum locals_action
91 LOCALS_UNDEF,
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. */
100 enum change_action
102 CHANGE_IGNORE,
103 CHANGE_MODIFY,
104 CHANGE_SET
107 /* Structure used to hold lists of sections and actions to take. */
108 struct section_list
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. */
143 struct section_add
145 /* Next section to add. */
146 struct section_add *next;
147 /* Name of section to add. */
148 const char *name;
149 /* Name of file holding section contents. */
150 const char *filename;
151 /* Size of file. */
152 size_t size;
153 /* Contents of file. */
154 bfd_byte *contents;
155 /* BFD section, after it has been added. */
156 asection *section;
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}
279 /* IMPORTS */
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. */
285 extern int is_strip;
288 static void
289 copy_usage (stream, exit_status)
290 FILE *stream;
291 int 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"),
299 program_name);
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"));
319 exit (exit_status);
322 static void
323 strip_usage (stream, exit_status)
324 FILE *stream;
325 int 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"),
334 program_name);
335 list_supported_targets (program_name, stream);
336 if (exit_status == 0)
337 fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
338 exit (exit_status);
341 /* Parse section flags into a flagword, with a fatal error if the
342 string can't be parsed. */
344 static flagword
345 parse_flags (s)
346 const char *s;
348 flagword ret;
349 const char *snext;
350 int len;
352 ret = SEC_NO_FLAGS;
356 snext = strchr (s, ',');
357 if (snext == NULL)
358 len = strlen (s);
359 else
361 len = snext - s;
362 ++snext;
365 if (0) ;
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);
375 #undef PARSE_FLAG
376 else
378 char *copy;
380 copy = xmalloc (len + 1);
381 strncpy (copy, s, len);
382 copy[len] = '\0';
383 non_fatal (_("unrecognized section flag `%s'"), copy);
384 fatal (_("supported flags: alloc, load, readonly, code, data, rom, contents"));
387 s = snext;
389 while (s != NULL);
391 return ret;
394 /* Find and optionally add an entry in the change_sections list. */
396 static struct section_list *
397 find_section_list (name, add)
398 const char *name;
399 boolean add;
401 register struct section_list *p;
403 for (p = change_sections; p != NULL; p = p->next)
404 if (strcmp (p->name, name) == 0)
405 return p;
407 if (! add)
408 return NULL;
410 p = (struct section_list *) xmalloc (sizeof (struct section_list));
411 p->name = name;
412 p->used = false;
413 p->remove = false;
414 p->change_vma = CHANGE_IGNORE;
415 p->change_lma = CHANGE_IGNORE;
416 p->vma_val = 0;
417 p->lma_val = 0;
418 p->set_flags = false;
419 p->flags = 0;
421 p->next = change_sections;
422 change_sections = p;
424 return p;
427 /* Add a symbol to strip_specific_list. */
429 static void
430 add_specific_symbol (name, list)
431 const char *name;
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;
439 *list = tmp_list;
442 /* See whether a symbol should be stripped or kept based on
443 strip_specific_list and keep_symbols. */
445 static boolean
446 is_specified_symbol (name, list)
447 const char *name;
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)
455 return true;
457 return false;
460 /* See if a section is being removed. */
462 static boolean
463 is_strip_section (abfd, sec)
464 bfd *abfd;
465 asection *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))
475 return true;
477 if (! sections_removed)
478 return false;
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. */
487 static unsigned int
488 filter_symbols (abfd, obfd, osyms, isyms, symcount)
489 bfd *abfd;
490 bfd *obfd;
491 asymbol **osyms, **isyms;
492 long symcount;
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);
502 int keep;
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;
512 else
514 char *n;
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);
520 else
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)
535 keep = 0;
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
539 & BSF_KEEP) != 0))
540 keep = 1;
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))
557 keep = 0;
558 if (!keep && is_specified_symbol (name, keep_specific_list))
559 keep = 1;
560 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
561 keep = 0;
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;
576 if (keep)
577 to[dst_count++] = sym;
580 to[dst_count] = NULL;
582 return dst_count;
585 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
586 Adjust *SIZE. */
588 static void
589 filter_bytes (memhunk, size)
590 char *memhunk;
591 bfd_size_type *size;
593 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
595 for (; from < end; from += interleave)
596 *to++ = *from;
597 if (*size % interleave > copy_byte)
598 *size = (*size / interleave) + 1;
599 else
600 *size /= interleave;
603 /* Copy object file IBFD onto OBFD. */
605 static void
606 copy_object (ibfd, obfd)
607 bfd *ibfd;
608 bfd *obfd;
610 bfd_vma start;
611 long symcount;
612 asection **osections = NULL;
613 bfd_size_type *gaps = NULL;
614 bfd_size_type max_gap = 0;
615 long symsize;
616 PTR dhandle;
619 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
620 RETURN_NONFATAL (bfd_get_filename (obfd));
622 if (verbose)
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));
627 if (set_start_set)
628 start = set_start;
629 else
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));
649 if (isympp)
650 free (isympp);
652 if (osympp != isympp)
653 free (osympp);
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 ()));
671 status = 1;
672 return;
674 else
676 flagword flags;
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);
682 if (pset != NULL)
683 pset->used = true;
685 if (pset != NULL && pset->set_flags)
686 flags = pset->flags | SEC_HAS_CONTENTS;
687 else
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));
693 if (pset != NULL)
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)
715 asection **set;
716 unsigned int c, i;
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 *));
726 set = osections;
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));
734 if (gap_fill_set)
736 for (i = 0; i < c - 1; i++)
738 flagword flags;
739 bfd_size_type size;
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)
745 continue;
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 ()));
758 status = 1;
759 break;
761 gaps[i] = gap_stop - gap_start;
762 if (max_gap < gap_stop - gap_start)
763 max_gap = gap_stop - gap_start;
768 if (pad_to_set)
770 bfd_vma lma;
771 bfd_size_type size;
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],
778 pad_to - lma))
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 ()));
783 status = 1;
785 else
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. */
797 dhandle = NULL;
798 symsize = bfd_get_symtab_upper_bound (ibfd);
799 if (symsize < 0)
800 RETURN_NONFATAL (bfd_get_filename (ibfd));
802 osympp = isympp = (asymbol **) xmalloc (symsize);
803 symcount = bfd_canonicalize_symtab (ibfd, isympp);
804 if (symcount < 0)
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
818 || sections_removed
819 || convert_debugging
820 || change_leading_char
821 || remove_leading_char
822 || weaken)
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
831 section. */
832 if (strip_symbols != STRIP_ALL)
833 bfd_map_over_sections (ibfd,
834 mark_symbols_used_in_relocations,
835 (PTR)isympp);
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))
844 status = 1;
845 return;
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,
862 (file_ptr) 0,
863 (bfd_size_type) padd->size))
864 RETURN_NONFATAL (bfd_get_filename (obfd));
868 if (gap_fill_set || pad_to_set)
870 bfd_byte *buf;
871 int c, i;
873 /* Fill in the gaps. */
875 if (max_gap > 8192)
876 max_gap = 8192;
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++)
883 if (gaps[i] != 0)
885 bfd_size_type left;
886 file_ptr off;
888 left = gaps[i];
889 off = bfd_section_size (obfd, osections[i]) - left;
890 while (left > 0)
892 bfd_size_type now;
894 if (left > 8192)
895 now = 8192;
896 else
897 now = left;
899 if (! bfd_set_section_contents (obfd, osections[i], buf,
900 off, now))
901 RETURN_NONFATAL (bfd_get_filename (obfd));
903 left -= now;
904 off += now;
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 ()));
919 status = 1;
920 return;
924 /* Read each archive element in turn from IBFD, copy the
925 contents to temp file, and keep the temp file handle. */
927 static void
928 copy_archive (ibfd, obfd, output_target)
929 bfd *ibfd;
930 bfd *obfd;
931 const char *output_target;
933 struct name_list
935 struct name_list *next;
936 char *name;
937 bfd *obfd;
938 } *list, *l;
939 bfd **ptr = &obfd->archive_head;
940 bfd *this_element;
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)
946 #else
947 if (mkdir (dir, 0700) != 0)
948 #endif
950 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
951 dir, strerror (errno));
953 obfd->has_armap = ibfd->has_armap;
955 list = NULL;
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),
962 (char *) NULL);
963 bfd *output_bfd = bfd_openw (output_name, output_target);
964 bfd *last_element;
966 l = (struct name_list *) xmalloc (sizeof (struct name_list));
967 l->name = output_name;
968 l->next = list;
969 list = l;
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. */
984 status = 1;
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;
992 *ptr = 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);
1013 unlink (l->name);
1015 rmdir (dir);
1018 /* The top-level control. */
1020 static void
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;
1027 bfd *ibfd;
1028 char **matching;
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);
1034 if (ibfd == NULL)
1035 RETURN_NONFATAL (input_filename);
1037 if (bfd_check_format (ibfd, bfd_archive))
1039 bfd *obfd;
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);
1047 if (obfd == NULL)
1048 RETURN_NONFATAL (output_filename);
1050 copy_archive (ibfd, obfd, output_target);
1052 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1054 bfd *obfd;
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);
1062 if (obfd == NULL)
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);
1073 else
1075 bfd_nonfatal (input_filename);
1077 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1079 list_matching_formats (matching);
1080 free (matching);
1083 status = 1;
1087 /* Create a section in OBFD with the same name and attributes
1088 as ISECTION in IBFD. */
1090 static void
1091 setup_section (ibfd, isection, obfdarg)
1092 bfd *ibfd;
1093 sec_ptr isection;
1094 PTR obfdarg;
1096 bfd *obfd = (bfd *) obfdarg;
1097 struct section_list *p;
1098 sec_ptr osection;
1099 bfd_size_type size;
1100 bfd_vma vma;
1101 bfd_vma lma;
1102 flagword flags;
1103 char *err;
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))
1111 return;
1113 p = find_section_list (bfd_section_name (ibfd, isection), false);
1114 if (p != NULL)
1115 p->used = true;
1117 if (p != NULL && p->remove)
1118 return;
1120 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1122 if (osection == NULL)
1124 err = "making";
1125 goto loser;
1128 size = bfd_section_size (ibfd, isection);
1129 if (copy_byte >= 0)
1130 size = (size + interleave - 1) / interleave;
1131 if (! bfd_set_section_size (obfd, osection, size))
1133 err = "size";
1134 goto loser;
1137 vma = bfd_section_vma (ibfd, isection);
1138 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1139 vma += p->vma_val;
1140 else if (p != NULL && p->change_vma == CHANGE_SET)
1141 vma = p->vma_val;
1142 else
1143 vma += change_section_address;
1145 if (! bfd_set_section_vma (obfd, osection, vma))
1147 err = "vma";
1148 goto loser;
1151 lma = isection->lma;
1152 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1154 if (p->change_lma == CHANGE_MODIFY)
1155 lma += p->lma_val;
1156 else if (p->change_lma == CHANGE_SET)
1157 lma = p->lma_val;
1158 else
1159 abort ();
1161 else
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,
1169 osection,
1170 bfd_section_alignment (ibfd, isection))
1171 == false)
1173 err = "alignment";
1174 goto loser;
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))
1182 err = "flags";
1183 goto loser;
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";
1197 goto loser;
1200 /* All went well */
1201 return;
1203 loser:
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 ()));
1208 status = 1;
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. */
1215 static void
1216 copy_section (ibfd, isection, obfdarg)
1217 bfd *ibfd;
1218 sec_ptr isection;
1219 PTR obfdarg;
1221 bfd *obfd = (bfd *) obfdarg;
1222 struct section_list *p;
1223 arelent **relpp;
1224 long relcount;
1225 sec_ptr osection;
1226 bfd_size_type size;
1227 long relsize;
1229 /* If we have already failed earlier on, do not keep on generating
1230 complaints now. */
1231 if (status != 0)
1232 return;
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))
1241 return;
1244 p = find_section_list (bfd_section_name (ibfd, isection), false);
1246 if (p != NULL && p->remove)
1247 return;
1249 osection = isection->output_section;
1250 size = bfd_get_section_size_before_reloc (isection);
1252 if (size == 0 || osection == 0)
1253 return;
1256 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1257 if (relsize < 0)
1258 RETURN_NONFATAL (bfd_get_filename (ibfd));
1260 if (relsize == 0)
1261 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1262 else
1264 relpp = (arelent **) xmalloc (relsize);
1265 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1266 if (relcount < 0)
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;
1275 long i;
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;
1284 free (relpp);
1285 relpp = temp_relpp;
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,
1299 size))
1300 RETURN_NONFATAL (bfd_get_filename (ibfd));
1302 if (copy_byte >= 0)
1303 filter_bytes (memhunk, &size);
1305 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1306 size))
1307 RETURN_NONFATAL (bfd_get_filename (obfd));
1309 free (memhunk);
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,
1323 size))
1324 RETURN_NONFATAL (bfd_get_filename (obfd));
1325 free (memhunk);
1329 /* Get all the sections. This is used when --gap-fill or --pad-to is
1330 used. */
1332 static void
1333 get_sections (obfd, osection, secppparg)
1334 bfd *obfd;
1335 asection *osection;
1336 PTR secppparg;
1338 asection ***secppp = (asection ***) secppparg;
1340 **secppp = osection;
1341 ++(*secppp);
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. */
1348 static int
1349 compare_section_lma (arg1, arg2)
1350 const PTR arg1;
1351 const PTR 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)
1365 return -1;
1367 else
1369 if ((flags2 & SEC_HAS_CONTENTS) == 0
1370 || (flags2 & SEC_LOAD) == 0)
1371 return 1;
1374 /* Sort sections by LMA. */
1375 if ((*sec1)->lma > (*sec2)->lma)
1376 return 1;
1377 else if ((*sec1)->lma < (*sec2)->lma)
1378 return -1;
1380 /* Sort sections with the same LMA by size. */
1381 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1382 return 1;
1383 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1384 return -1;
1386 return 0;
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. */
1394 static void
1395 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1396 bfd *ibfd;
1397 sec_ptr isection;
1398 PTR symbolsarg;
1400 asymbol **symbols = (asymbol **) symbolsarg;
1401 long relsize;
1402 arelent **relpp;
1403 long relcount, i;
1405 /* Ignore an input section with no corresponding output section. */
1406 if (isection->output_section == NULL)
1407 return;
1409 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1410 if (relsize < 0)
1411 bfd_fatal (bfd_get_filename (ibfd));
1413 if (relsize == 0)
1414 return;
1416 relpp = (arelent **) xmalloc (relsize);
1417 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1418 if (relcount < 0)
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;
1431 if (relpp != NULL)
1432 free (relpp);
1435 /* Write out debugging information. */
1437 static boolean
1438 write_debugging_info (obfd, dhandle, symcountp, symppp)
1439 bfd *obfd;
1440 PTR dhandle;
1441 long *symcountp;
1442 asymbol ***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,
1455 &symsize, &strings,
1456 &stringsize))
1457 return false;
1459 stabsec = bfd_make_section (obfd, ".stab");
1460 stabstrsec = bfd_make_section (obfd, ".stabstr");
1461 if (stabsec == NULL
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,
1468 (SEC_HAS_CONTENTS
1469 | SEC_READONLY
1470 | SEC_DEBUGGING))
1471 || ! bfd_set_section_flags (obfd, stabstrsec,
1472 (SEC_HAS_CONTENTS
1473 | SEC_READONLY
1474 | SEC_DEBUGGING)))
1476 non_fatal (_("%s: can't create debugging section: %s"),
1477 bfd_get_filename (obfd),
1478 bfd_errmsg (bfd_get_error ()));
1479 return false;
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,
1487 symsize)
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 ()));
1494 return false;
1497 return true;
1500 non_fatal (_("%s: don't know how to write debugging information for %s"),
1501 bfd_get_filename (obfd), bfd_get_target (obfd));
1502 return false;
1505 static int
1506 strip_main (argc, argv)
1507 int argc;
1508 char *argv[];
1510 char *input_target = NULL, *output_target = NULL;
1511 boolean show_version = false;
1512 int c, i;
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)
1519 switch (c)
1521 case 'I':
1522 input_target = optarg;
1523 break;
1524 case 'O':
1525 output_target = optarg;
1526 break;
1527 case 'F':
1528 input_target = output_target = optarg;
1529 break;
1530 case 'R':
1531 p = find_section_list (optarg, true);
1532 p->remove = true;
1533 sections_removed = true;
1534 break;
1535 case 's':
1536 strip_symbols = STRIP_ALL;
1537 break;
1538 case 'S':
1539 case 'g':
1540 strip_symbols = STRIP_DEBUG;
1541 break;
1542 case OPTION_STRIP_UNNEEDED:
1543 strip_symbols = STRIP_UNNEEDED;
1544 break;
1545 case 'K':
1546 add_specific_symbol (optarg, &keep_specific_list);
1547 break;
1548 case 'N':
1549 add_specific_symbol (optarg, &strip_specific_list);
1550 break;
1551 case 'o':
1552 output_file = optarg;
1553 break;
1554 case 'p':
1555 preserve_dates = true;
1556 break;
1557 case 'x':
1558 discard_locals = LOCALS_ALL;
1559 break;
1560 case 'X':
1561 discard_locals = LOCALS_START_L;
1562 break;
1563 case 'v':
1564 verbose = true;
1565 break;
1566 case 'V':
1567 show_version = true;
1568 break;
1569 case 0:
1570 break; /* we've been given a long option */
1571 case 'h':
1572 strip_usage (stdout, 0);
1573 default:
1574 strip_usage (stderr, 1);
1578 if (show_version)
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;
1590 i = optind;
1591 if (i == argc
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;
1599 char *tmpname;
1601 if (preserve_dates)
1603 if (stat (argv[i], &statbuf) < 0)
1605 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
1606 continue;
1610 if (output_file != NULL)
1611 tmpname = output_file;
1612 else
1613 tmpname = make_tempname (argv[i]);
1614 status = 0;
1616 copy_file (argv[i], tmpname, input_target, output_target);
1617 if (status == 0)
1619 if (preserve_dates)
1620 set_times (tmpname, &statbuf);
1621 if (output_file == NULL)
1622 smart_rename (tmpname, argv[i], preserve_dates);
1623 status = hold_status;
1625 else
1626 unlink (tmpname);
1627 if (output_file == NULL)
1628 free (tmpname);
1631 return 0;
1634 static int
1635 copy_main (argc, argv)
1636 int argc;
1637 char *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;
1643 int c;
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)
1650 switch (c)
1652 case 'b':
1653 copy_byte = atoi (optarg);
1654 if (copy_byte < 0)
1655 fatal (_("byte number must be non-negative"));
1656 break;
1657 case 'i':
1658 interleave = atoi (optarg);
1659 if (interleave < 1)
1660 fatal (_("interleave must be positive"));
1661 break;
1662 case 'I':
1663 case 's': /* "source" - 'I' is preferred */
1664 input_target = optarg;
1665 break;
1666 case 'O':
1667 case 'd': /* "destination" - 'O' is preferred */
1668 output_target = optarg;
1669 break;
1670 case 'F':
1671 input_target = output_target = optarg;
1672 break;
1673 case 'R':
1674 p = find_section_list (optarg, true);
1675 p->remove = true;
1676 sections_removed = true;
1677 break;
1678 case 'S':
1679 strip_symbols = STRIP_ALL;
1680 break;
1681 case 'g':
1682 strip_symbols = STRIP_DEBUG;
1683 break;
1684 case OPTION_STRIP_UNNEEDED:
1685 strip_symbols = STRIP_UNNEEDED;
1686 break;
1687 case 'K':
1688 add_specific_symbol (optarg, &keep_specific_list);
1689 break;
1690 case 'N':
1691 add_specific_symbol (optarg, &strip_specific_list);
1692 break;
1693 case 'L':
1694 add_specific_symbol (optarg, &localize_specific_list);
1695 break;
1696 case 'W':
1697 add_specific_symbol (optarg, &weaken_specific_list);
1698 break;
1699 case 'p':
1700 preserve_dates = true;
1701 break;
1702 case 'x':
1703 discard_locals = LOCALS_ALL;
1704 break;
1705 case 'X':
1706 discard_locals = LOCALS_START_L;
1707 break;
1708 case 'v':
1709 verbose = true;
1710 break;
1711 case 'V':
1712 show_version = true;
1713 break;
1714 case OPTION_WEAKEN:
1715 weaken = true;
1716 break;
1717 case OPTION_ADD_SECTION:
1719 const char *s;
1720 struct stat st;
1721 struct section_add *pa;
1722 int len;
1723 char *name;
1724 FILE *f;
1726 s = strchr (optarg, '=');
1728 if (s == NULL)
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));
1736 len = s - optarg;
1737 name = (char *) xmalloc (len + 1);
1738 strncpy (name, optarg, len);
1739 name[len] = '\0';
1740 pa->name = name;
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);
1749 if (f == NULL)
1750 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
1752 if (fread (pa->contents, 1, pa->size, f) == 0
1753 || ferror (f))
1754 fatal (_("%s: fread failed"), pa->filename);
1756 fclose (f);
1758 pa->next = add_sections;
1759 add_sections = pa;
1761 break;
1762 case OPTION_CHANGE_START:
1763 change_start = parse_vma (optarg, "--change-start");
1764 break;
1765 case OPTION_CHANGE_SECTION_ADDRESS:
1766 case OPTION_CHANGE_SECTION_LMA:
1767 case OPTION_CHANGE_SECTION_VMA:
1769 const char *s;
1770 int len;
1771 char *name;
1772 char *option;
1773 bfd_vma val;
1774 enum change_action what;
1776 switch (c)
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, '=');
1784 if (s == NULL)
1786 s = strchr (optarg, '+');
1787 if (s == NULL)
1789 s = strchr (optarg, '-');
1790 if (s == NULL)
1791 fatal (_("bad format for %s"), option);
1795 len = s - optarg;
1796 name = (char *) xmalloc (len + 1);
1797 strncpy (name, optarg, len);
1798 name[len] = '\0';
1800 p = find_section_list (name, true);
1802 val = parse_vma (s + 1, option);
1804 switch (*s)
1806 case '=': what = CHANGE_SET; break;
1807 case '-': val = - val; /* Drop through. */
1808 case '+': what = CHANGE_MODIFY; break;
1811 switch (c)
1813 case OPTION_CHANGE_SECTION_ADDRESS:
1814 p->change_vma = what;
1815 p->vma_val = val;
1816 /* Drop through. */
1818 case OPTION_CHANGE_SECTION_LMA:
1819 p->change_lma = what;
1820 p->lma_val = val;
1821 break;
1823 case OPTION_CHANGE_SECTION_VMA:
1824 p->change_vma = what;
1825 p->vma_val = val;
1826 break;
1829 break;
1830 case OPTION_CHANGE_ADDRESSES:
1831 change_section_address = parse_vma (optarg, "--change-addresses");
1832 change_start = change_section_address;
1833 break;
1834 case OPTION_CHANGE_WARNINGS:
1835 change_warn = true;
1836 break;
1837 case OPTION_CHANGE_LEADING_CHAR:
1838 change_leading_char = true;
1839 break;
1840 case OPTION_DEBUGGING:
1841 convert_debugging = true;
1842 break;
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)
1851 char buff[20];
1853 sprintf_vma (buff, gap_fill_vma);
1855 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
1856 buff, gap_fill);
1858 gap_fill_set = true;
1860 break;
1861 case OPTION_NO_CHANGE_WARNINGS:
1862 change_warn = false;
1863 break;
1864 case OPTION_PAD_TO:
1865 pad_to = parse_vma (optarg, "--pad-to");
1866 pad_to_set = true;
1867 break;
1868 case OPTION_REMOVE_LEADING_CHAR:
1869 remove_leading_char = true;
1870 break;
1871 case OPTION_SET_SECTION_FLAGS:
1873 const char *s;
1874 int len;
1875 char *name;
1877 s = strchr (optarg, '=');
1878 if (s == NULL)
1879 fatal (_("bad format for --set-section-flags"));
1881 len = s - optarg;
1882 name = (char *) xmalloc (len + 1);
1883 strncpy (name, optarg, len);
1884 name[len] = '\0';
1886 p = find_section_list (name, true);
1888 p->set_flags = true;
1889 p->flags = parse_flags (s + 1);
1891 break;
1892 case OPTION_SET_START:
1893 set_start = parse_vma (optarg, "--set-start");
1894 set_start_set = true;
1895 break;
1896 case 0:
1897 break; /* we've been given a long option */
1898 case 'h':
1899 copy_usage (stdout, 0);
1900 default:
1901 copy_usage (stderr, 1);
1905 if (show_version)
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;
1925 if (preserve_dates)
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);
1939 if (status == 0)
1941 if (preserve_dates)
1942 set_times (tmpname, &statbuf);
1943 smart_rename (tmpname, input_filename, preserve_dates);
1945 else
1946 unlink (tmpname);
1948 else
1950 copy_file (input_filename, output_filename, input_target, output_target);
1951 if (status == 0 && preserve_dates)
1952 set_times (output_filename, &statbuf);
1955 if (change_warn)
1957 for (p = change_sections; p != NULL; p = p->next)
1959 if (! p->used)
1961 if (p->change_vma != CHANGE_IGNORE)
1963 char buff [20];
1965 sprintf_vma (buff, p->vma_val);
1967 /* xgettext:c-format */
1968 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
1969 p->name,
1970 p->change_vma == CHANGE_SET ? '=' : '+',
1971 buff);
1974 if (p->change_lma != CHANGE_IGNORE)
1976 char buff [20];
1978 sprintf_vma (buff, p->lma_val);
1980 /* xgettext:c-format */
1981 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
1982 p->name,
1983 p->change_lma == CHANGE_SET ? '=' : '+',
1984 buff);
1990 return 0;
1994 main (argc, argv)
1995 int argc;
1996 char *argv[];
1998 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1999 setlocale (LC_MESSAGES, "");
2000 #endif
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;
2012 bfd_init ();
2013 set_default_bfd_target ();
2015 if (is_strip < 0)
2017 int i = strlen (program_name);
2018 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
2021 if (is_strip)
2022 strip_main (argc, argv);
2023 else
2024 copy_main (argc, argv);
2026 END_PROGRESS (program_name);
2028 return status;