* "objcopy -O binary" warning tweak, suggested by dmoseley
[binutils-gdb.git] / binutils / objcopy.c
blob194478af97352d1ac1e16bf9d39bd9edc5d8d255
1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
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 #include "elf/internal.h"
31 #include "elf-bfd.h"
33 #ifdef HAVE_GOOD_UTIME_H
34 #include <utime.h>
35 #else /* ! HAVE_GOOD_UTIME_H */
36 #ifdef HAVE_UTIMES
37 #include <sys/time.h>
38 #endif /* HAVE_UTIMES */
39 #endif /* ! HAVE_GOOD_UTIME_H */
41 /* A list of symbols to explicitly strip out, or to keep. A linked
42 list is good enough for a small number from the command line, but
43 this will slow things down a lot if many symbols are being
44 deleted. */
46 struct symlist
48 const char *name;
49 struct symlist *next;
52 static void copy_usage PARAMS ((FILE *, int));
53 static void strip_usage PARAMS ((FILE *, int));
54 static flagword parse_flags PARAMS ((const char *));
55 static struct section_list *find_section_list PARAMS ((const char *, boolean));
56 static void setup_section PARAMS ((bfd *, asection *, PTR));
57 static void copy_section PARAMS ((bfd *, asection *, PTR));
58 static void get_sections PARAMS ((bfd *, asection *, PTR));
59 static int compare_section_lma PARAMS ((const PTR, const PTR));
60 static void add_specific_symbol PARAMS ((const char *, struct symlist **));
61 static boolean is_specified_symbol PARAMS ((const char *, struct symlist *));
62 static boolean is_strip_section PARAMS ((bfd *, asection *));
63 static unsigned int filter_symbols
64 PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long));
65 static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
66 static void filter_bytes PARAMS ((char *, bfd_size_type *));
67 static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***));
68 static void copy_object PARAMS ((bfd *, bfd *));
69 static void copy_archive PARAMS ((bfd *, bfd *, const char *));
70 static void copy_file
71 PARAMS ((const char *, const char *, const char *, const char *));
72 static int simple_copy PARAMS ((const char *, const char *));
73 static int smart_rename PARAMS ((const char *, const char *));
74 static void set_times PARAMS ((const char *, const struct stat *));
75 static int strip_main PARAMS ((int, char **));
76 static int copy_main PARAMS ((int, char **));
78 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
80 static asymbol **isympp = NULL; /* Input symbols */
81 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
83 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
84 static int copy_byte = -1;
85 static int interleave = 4;
87 static boolean verbose; /* Print file and target names. */
88 static boolean preserve_dates; /* Preserve input file timestamp. */
89 static int status = 0; /* Exit status. */
91 enum strip_action
93 STRIP_UNDEF,
94 STRIP_NONE, /* don't strip */
95 STRIP_DEBUG, /* strip all debugger symbols */
96 STRIP_UNNEEDED, /* strip unnecessary symbols */
97 STRIP_ALL /* strip all symbols */
100 /* Which symbols to remove. */
101 static enum strip_action strip_symbols;
103 enum locals_action
105 LOCALS_UNDEF,
106 LOCALS_START_L, /* discard locals starting with L */
107 LOCALS_ALL /* discard all locals */
110 /* Which local symbols to remove. Overrides STRIP_ALL. */
111 static enum locals_action discard_locals;
113 /* What kind of change to perform. */
114 enum change_action
116 CHANGE_IGNORE,
117 CHANGE_MODIFY,
118 CHANGE_SET
121 /* Structure used to hold lists of sections and actions to take. */
122 struct section_list
124 struct section_list * next; /* Next section to change. */
125 const char * name; /* Section name. */
126 boolean used; /* Whether this entry was used. */
127 boolean remove; /* Whether to remove this section. */
128 enum change_action change_vma;/* Whether to change or set VMA. */
129 bfd_vma vma_val; /* Amount to change by or set to. */
130 enum change_action change_lma;/* Whether to change or set LMA. */
131 bfd_vma lma_val; /* Amount to change by or set to. */
132 boolean set_flags; /* Whether to set the section flags. */
133 flagword flags; /* What to set the section flags to. */
136 static struct section_list *change_sections;
137 static boolean sections_removed;
139 /* Changes to the start address. */
140 static bfd_vma change_start = 0;
141 static boolean set_start_set = false;
142 static bfd_vma set_start;
144 /* Changes to section addresses. */
145 static bfd_vma change_section_address = 0;
147 /* Filling gaps between sections. */
148 static boolean gap_fill_set = false;
149 static bfd_byte gap_fill = 0;
151 /* Pad to a given address. */
152 static boolean pad_to_set = false;
153 static bfd_vma pad_to;
155 /* List of sections to add. */
157 struct section_add
159 /* Next section to add. */
160 struct section_add *next;
161 /* Name of section to add. */
162 const char *name;
163 /* Name of file holding section contents. */
164 const char *filename;
165 /* Size of file. */
166 size_t size;
167 /* Contents of file. */
168 bfd_byte *contents;
169 /* BFD section, after it has been added. */
170 asection *section;
173 static struct section_add *add_sections;
175 /* Whether to convert debugging information. */
177 static boolean convert_debugging = false;
179 /* Whether to change the leading character in symbol names. */
181 static boolean change_leading_char = false;
183 /* Whether to remove the leading character from global symbol names. */
185 static boolean remove_leading_char = false;
187 /* List of symbols to strip, keep, localize, and weaken. */
189 static struct symlist *strip_specific_list = NULL;
190 static struct symlist *keep_specific_list = NULL;
191 static struct symlist *localize_specific_list = NULL;
192 static struct symlist *weaken_specific_list = NULL;
194 /* If this is true, we weaken global symbols (set BSF_WEAK). */
196 static boolean weaken = false;
198 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
200 #define OPTION_ADD_SECTION 150
201 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
202 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
203 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
204 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
205 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
206 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
207 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
208 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
209 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
210 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
211 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
212 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
213 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
214 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
215 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
216 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
218 /* Options to handle if running as "strip". */
220 static struct option strip_options[] =
222 {"discard-all", no_argument, 0, 'x'},
223 {"discard-locals", no_argument, 0, 'X'},
224 {"format", required_argument, 0, 'F'}, /* Obsolete */
225 {"help", no_argument, 0, 'h'},
226 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
227 {"input-target", required_argument, 0, 'I'},
228 {"keep-symbol", required_argument, 0, 'K'},
229 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
230 {"output-target", required_argument, 0, 'O'},
231 {"preserve-dates", no_argument, 0, 'p'},
232 {"remove-section", required_argument, 0, 'R'},
233 {"strip-all", no_argument, 0, 's'},
234 {"strip-debug", no_argument, 0, 'S'},
235 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
236 {"strip-symbol", required_argument, 0, 'N'},
237 {"target", required_argument, 0, 'F'},
238 {"verbose", no_argument, 0, 'v'},
239 {"version", no_argument, 0, 'V'},
240 {0, no_argument, 0, 0}
243 /* Options to handle if running as "objcopy". */
245 static struct option copy_options[] =
247 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
248 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
249 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
250 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
251 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
252 {"byte", required_argument, 0, 'b'},
253 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
254 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
255 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
256 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
257 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
258 {"change-start", required_argument, 0, OPTION_CHANGE_START},
259 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
260 {"debugging", no_argument, 0, OPTION_DEBUGGING},
261 {"discard-all", no_argument, 0, 'x'},
262 {"discard-locals", no_argument, 0, 'X'},
263 {"format", required_argument, 0, 'F'}, /* Obsolete */
264 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
265 {"help", no_argument, 0, 'h'},
266 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
267 {"input-target", required_argument, 0, 'I'},
268 {"interleave", required_argument, 0, 'i'},
269 {"keep-symbol", required_argument, 0, 'K'},
270 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
271 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
272 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
273 {"output-target", required_argument, 0, 'O'},
274 {"pad-to", required_argument, 0, OPTION_PAD_TO},
275 {"preserve-dates", no_argument, 0, 'p'},
276 {"localize-symbol", required_argument, 0, 'L'},
277 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
278 {"remove-section", required_argument, 0, 'R'},
279 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
280 {"set-start", required_argument, 0, OPTION_SET_START},
281 {"strip-all", no_argument, 0, 'S'},
282 {"strip-debug", no_argument, 0, 'g'},
283 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
284 {"strip-symbol", required_argument, 0, 'N'},
285 {"target", required_argument, 0, 'F'},
286 {"verbose", no_argument, 0, 'v'},
287 {"version", no_argument, 0, 'V'},
288 {"weaken", no_argument, 0, OPTION_WEAKEN},
289 {"weaken-symbol", required_argument, 0, 'W'},
290 {0, no_argument, 0, 0}
293 /* IMPORTS */
294 extern char *program_name;
296 /* This flag distinguishes between strip and objcopy:
297 1 means this is 'strip'; 0 means this is 'objcopy'.
298 -1 means if we should use argv[0] to decide. */
299 extern int is_strip;
302 static void
303 copy_usage (stream, exit_status)
304 FILE *stream;
305 int exit_status;
307 fprintf (stream, _("\
308 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
309 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
310 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
311 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
312 [--discard-locals] [--debugging] [--remove-section=section]\n"),
313 program_name);
314 fprintf (stream, _("\
315 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
316 [--set-start=val] \n\
317 [--change-start=incr] [--change-addresses=incr] \n\
318 (--adjust-start and --adjust-vma are aliases for these two) \n\
319 [--change-section-address=section{=,+,-}val]\n\
320 (--adjust-section-vma is an alias for --change-section-address)\n\
321 [--change-section-lma=section{=,+,-}val]\n\
322 [--change-section-vma=section{=,+,-}val]\n\
323 [--adjust-warnings] [--no-adjust-warnings]\n\
324 [--change-warnings] [--no-change-warnings]\n\
325 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
326 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
327 [--localize-symbol symbol] [-L symbol] [--weaken-symbol symbol]\n\
328 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
329 [--verbose] [--version] [--help] in-file [out-file]\n"));
330 list_supported_targets (program_name, stream);
331 if (exit_status == 0)
332 fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
333 exit (exit_status);
336 static void
337 strip_usage (stream, exit_status)
338 FILE *stream;
339 int exit_status;
341 fprintf (stream, _("\
342 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
343 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
344 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
345 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
346 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
347 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n"),
348 program_name);
349 list_supported_targets (program_name, stream);
350 if (exit_status == 0)
351 fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
352 exit (exit_status);
355 /* Parse section flags into a flagword, with a fatal error if the
356 string can't be parsed. */
358 static flagword
359 parse_flags (s)
360 const char *s;
362 flagword ret;
363 const char *snext;
364 int len;
366 ret = SEC_NO_FLAGS;
370 snext = strchr (s, ',');
371 if (snext == NULL)
372 len = strlen (s);
373 else
375 len = snext - s;
376 ++snext;
379 if (0) ;
380 #define PARSE_FLAG(fname,fval) \
381 else if (strncasecmp (fname, s, len) == 0) ret |= fval
382 PARSE_FLAG ("alloc", SEC_ALLOC);
383 PARSE_FLAG ("load", SEC_LOAD);
384 PARSE_FLAG ("readonly", SEC_READONLY);
385 PARSE_FLAG ("code", SEC_CODE);
386 PARSE_FLAG ("data", SEC_DATA);
387 PARSE_FLAG ("rom", SEC_ROM);
388 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
389 #undef PARSE_FLAG
390 else
392 char *copy;
394 copy = xmalloc (len + 1);
395 strncpy (copy, s, len);
396 copy[len] = '\0';
397 non_fatal (_("unrecognized section flag `%s'"), copy);
398 fatal (_("supported flags: alloc, load, readonly, code, data, rom, contents"));
401 s = snext;
403 while (s != NULL);
405 return ret;
408 /* Find and optionally add an entry in the change_sections list. */
410 static struct section_list *
411 find_section_list (name, add)
412 const char *name;
413 boolean add;
415 register struct section_list *p;
417 for (p = change_sections; p != NULL; p = p->next)
418 if (strcmp (p->name, name) == 0)
419 return p;
421 if (! add)
422 return NULL;
424 p = (struct section_list *) xmalloc (sizeof (struct section_list));
425 p->name = name;
426 p->used = false;
427 p->remove = false;
428 p->change_vma = CHANGE_IGNORE;
429 p->change_lma = CHANGE_IGNORE;
430 p->vma_val = 0;
431 p->lma_val = 0;
432 p->set_flags = false;
433 p->flags = 0;
435 p->next = change_sections;
436 change_sections = p;
438 return p;
441 /* Add a symbol to strip_specific_list. */
443 static void
444 add_specific_symbol (name, list)
445 const char *name;
446 struct symlist **list;
448 struct symlist *tmp_list;
450 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
451 tmp_list->name = name;
452 tmp_list->next = *list;
453 *list = tmp_list;
456 /* See whether a symbol should be stripped or kept based on
457 strip_specific_list and keep_symbols. */
459 static boolean
460 is_specified_symbol (name, list)
461 const char *name;
462 struct symlist *list;
464 struct symlist *tmp_list;
466 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
468 if (strcmp (name, tmp_list->name) == 0)
469 return true;
471 return false;
474 /* See if a section is being removed. */
476 static boolean
477 is_strip_section (abfd, sec)
478 bfd *abfd;
479 asection *sec;
481 struct section_list *p;
483 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
484 && (strip_symbols == STRIP_DEBUG
485 || strip_symbols == STRIP_UNNEEDED
486 || strip_symbols == STRIP_ALL
487 || discard_locals == LOCALS_ALL
488 || convert_debugging))
489 return true;
491 if (! sections_removed)
492 return false;
493 p = find_section_list (bfd_get_section_name (abfd, sec), false);
494 return p != NULL && p->remove ? true : false;
497 /* Choose which symbol entries to copy; put the result in OSYMS.
498 We don't copy in place, because that confuses the relocs.
499 Return the number of symbols to print. */
501 static unsigned int
502 filter_symbols (abfd, obfd, osyms, isyms, symcount)
503 bfd *abfd;
504 bfd *obfd;
505 asymbol **osyms, **isyms;
506 long symcount;
508 register asymbol **from = isyms, **to = osyms;
509 long src_count = 0, dst_count = 0;
511 for (; src_count < symcount; src_count++)
513 asymbol *sym = from[src_count];
514 flagword flags = sym->flags;
515 const char *name = bfd_asymbol_name (sym);
516 int keep;
518 if (change_leading_char
519 && (bfd_get_symbol_leading_char (abfd)
520 != bfd_get_symbol_leading_char (obfd))
521 && (bfd_get_symbol_leading_char (abfd) == '\0'
522 || (name[0] == bfd_get_symbol_leading_char (abfd))))
524 if (bfd_get_symbol_leading_char (obfd) == '\0')
525 name = bfd_asymbol_name (sym) = name + 1;
526 else
528 char *n;
530 n = xmalloc (strlen (name) + 2);
531 n[0] = bfd_get_symbol_leading_char (obfd);
532 if (bfd_get_symbol_leading_char (abfd) == '\0')
533 strcpy (n + 1, name);
534 else
535 strcpy (n + 1, name + 1);
536 name = bfd_asymbol_name (sym) = n;
540 if (remove_leading_char
541 && ((flags & BSF_GLOBAL) != 0
542 || (flags & BSF_WEAK) != 0
543 || bfd_is_und_section (bfd_get_section (sym))
544 || bfd_is_com_section (bfd_get_section (sym)))
545 && name[0] == bfd_get_symbol_leading_char (abfd))
546 name = bfd_asymbol_name (sym) = name + 1;
548 if ((flags & BSF_KEEP) != 0) /* Used in relocation. */
549 keep = 1;
550 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
551 || (flags & BSF_WEAK) != 0
552 || bfd_is_und_section (bfd_get_section (sym))
553 || bfd_is_com_section (bfd_get_section (sym)))
554 keep = strip_symbols != STRIP_UNNEEDED;
555 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
556 keep = (strip_symbols != STRIP_DEBUG
557 && strip_symbols != STRIP_UNNEEDED
558 && ! convert_debugging);
559 else /* Local symbol. */
560 keep = (strip_symbols != STRIP_UNNEEDED
561 && (discard_locals != LOCALS_ALL
562 && (discard_locals != LOCALS_START_L
563 || ! bfd_is_local_label (abfd, sym))));
565 if (keep && is_specified_symbol (name, strip_specific_list))
566 keep = 0;
567 if (!keep && is_specified_symbol (name, keep_specific_list))
568 keep = 1;
569 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
570 keep = 0;
572 if (keep && (flags & BSF_GLOBAL) != 0
573 && (weaken || is_specified_symbol (name, weaken_specific_list)))
575 sym->flags &=~ BSF_GLOBAL;
576 sym->flags |= BSF_WEAK;
578 if (keep && (flags & (BSF_GLOBAL | BSF_WEAK))
579 && is_specified_symbol (name, localize_specific_list))
581 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
582 sym->flags |= BSF_LOCAL;
585 if (keep)
586 to[dst_count++] = sym;
589 to[dst_count] = NULL;
591 return dst_count;
594 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
595 Adjust *SIZE. */
597 static void
598 filter_bytes (memhunk, size)
599 char *memhunk;
600 bfd_size_type *size;
602 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
604 for (; from < end; from += interleave)
605 *to++ = *from;
606 *size /= interleave;
609 /* Copy object file IBFD onto OBFD. */
611 static void
612 copy_object (ibfd, obfd)
613 bfd *ibfd;
614 bfd *obfd;
616 bfd_vma start;
617 long symcount;
618 asection **osections = NULL;
619 bfd_size_type *gaps = NULL;
620 bfd_size_type max_gap = 0;
622 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
623 RETURN_NONFATAL (bfd_get_filename (obfd));
625 if (verbose)
626 printf (_("copy from %s(%s) to %s(%s)\n"),
627 bfd_get_filename (ibfd), bfd_get_target (ibfd),
628 bfd_get_filename (obfd), bfd_get_target (obfd));
630 if (set_start_set)
631 start = set_start;
632 else
633 start = bfd_get_start_address (ibfd);
634 start += change_start;
636 if (!bfd_set_start_address (obfd, start)
637 || !bfd_set_file_flags (obfd,
638 (bfd_get_file_flags (ibfd)
639 & bfd_applicable_file_flags (obfd))))
640 RETURN_NONFATAL (bfd_get_filename (ibfd));
642 /* Copy architecture of input file to output file */
643 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
644 bfd_get_mach (ibfd)))
645 non_fatal (_("Warning: Output file cannot represent architecture %s"),
646 bfd_printable_arch_mach (bfd_get_arch (ibfd),
647 bfd_get_mach (ibfd)));
649 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
650 RETURN_NONFATAL (bfd_get_filename (ibfd));
652 if (isympp)
653 free (isympp);
655 if (osympp != isympp)
656 free (osympp);
658 /* BFD mandates that all output sections be created and sizes set before
659 any output is done. Thus, we traverse all sections multiple times. */
660 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
662 if (add_sections != NULL)
664 struct section_add *padd;
665 struct section_list *pset;
667 for (padd = add_sections; padd != NULL; padd = padd->next)
669 padd->section = bfd_make_section (obfd, padd->name);
670 if (padd->section == NULL)
672 non_fatal (_("can't create section `%s': %s"),
673 padd->name, bfd_errmsg (bfd_get_error ()));
674 status = 1;
675 return;
677 else
679 flagword flags;
681 if (! bfd_set_section_size (obfd, padd->section, padd->size))
682 RETURN_NONFATAL (bfd_get_filename (obfd));
684 pset = find_section_list (padd->name, false);
685 if (pset != NULL)
686 pset->used = true;
688 if (pset != NULL && pset->set_flags)
689 flags = pset->flags | SEC_HAS_CONTENTS;
690 else
691 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
693 if (! bfd_set_section_flags (obfd, padd->section, flags))
694 RETURN_NONFATAL (bfd_get_filename (obfd));
696 if (pset != NULL)
698 if (pset->change_vma != CHANGE_IGNORE)
699 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
700 RETURN_NONFATAL (bfd_get_filename (obfd));
702 if (pset->change_lma != CHANGE_IGNORE)
704 padd->section->lma = pset->lma_val;
706 if (! bfd_set_section_alignment
707 (obfd, padd->section,
708 bfd_section_alignment (obfd, padd->section)))
709 RETURN_NONFATAL (bfd_get_filename (obfd));
716 if (gap_fill_set || pad_to_set)
718 asection **set;
719 unsigned int c, i;
721 /* We must fill in gaps between the sections and/or we must pad
722 the last section to a specified address. We do this by
723 grabbing a list of the sections, sorting them by VMA, and
724 increasing the section sizes as required to fill the gaps.
725 We write out the gap contents below. */
727 c = bfd_count_sections (obfd);
728 osections = (asection **) xmalloc (c * sizeof (asection *));
729 set = osections;
730 bfd_map_over_sections (obfd, get_sections, (void *) &set);
732 qsort (osections, c, sizeof (asection *), compare_section_lma);
734 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
735 memset (gaps, 0, c * sizeof (bfd_size_type));
737 if (gap_fill_set)
739 for (i = 0; i < c - 1; i++)
741 flagword flags;
742 bfd_size_type size;
743 bfd_vma gap_start, gap_stop;
745 flags = bfd_get_section_flags (obfd, osections[i]);
746 if ((flags & SEC_HAS_CONTENTS) == 0
747 || (flags & SEC_LOAD) == 0)
748 continue;
750 size = bfd_section_size (obfd, osections[i]);
751 gap_start = bfd_section_lma (obfd, osections[i]) + size;
752 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
753 if (gap_start < gap_stop)
755 if (! bfd_set_section_size (obfd, osections[i],
756 size + (gap_stop - gap_start)))
758 non_fatal (_("Can't fill gap after %s: %s"),
759 bfd_get_section_name (obfd, osections[i]),
760 bfd_errmsg (bfd_get_error ()));
761 status = 1;
762 break;
764 gaps[i] = gap_stop - gap_start;
765 if (max_gap < gap_stop - gap_start)
766 max_gap = gap_stop - gap_start;
771 if (pad_to_set)
773 bfd_vma lma;
774 bfd_size_type size;
776 lma = bfd_section_lma (obfd, osections[c - 1]);
777 size = bfd_section_size (obfd, osections[c - 1]);
778 if (lma + size < pad_to)
780 if (! bfd_set_section_size (obfd, osections[c - 1],
781 pad_to - lma))
783 non_fatal (_("Can't add padding to %s: %s"),
784 bfd_get_section_name (obfd, osections[c - 1]),
785 bfd_errmsg (bfd_get_error ()));
786 status = 1;
788 else
790 gaps[c - 1] = pad_to - (lma + size);
791 if (max_gap < pad_to - (lma + size))
792 max_gap = pad_to - (lma + size);
798 /* Symbol filtering must happen after the output sections have
799 been created, but before their contents are set. */
800 if (strip_symbols == STRIP_ALL)
802 osympp = isympp = NULL;
803 symcount = 0;
805 else
807 long symsize;
808 PTR dhandle = NULL;
810 symsize = bfd_get_symtab_upper_bound (ibfd);
811 if (symsize < 0)
812 RETURN_NONFATAL (bfd_get_filename (ibfd));
814 osympp = isympp = (asymbol **) xmalloc (symsize);
815 symcount = bfd_canonicalize_symtab (ibfd, isympp);
816 if (symcount < 0)
817 RETURN_NONFATAL (bfd_get_filename (ibfd));
819 if (convert_debugging)
820 dhandle = read_debugging_info (ibfd, isympp, symcount);
822 if (strip_symbols == STRIP_DEBUG
823 || strip_symbols == STRIP_UNNEEDED
824 || discard_locals != LOCALS_UNDEF
825 || strip_specific_list != NULL
826 || keep_specific_list != NULL
827 || localize_specific_list != NULL
828 || weaken_specific_list != NULL
829 || sections_removed
830 || convert_debugging
831 || change_leading_char
832 || remove_leading_char
833 || weaken)
835 /* Mark symbols used in output relocations so that they
836 are kept, even if they are local labels or static symbols.
838 Note we iterate over the input sections examining their
839 relocations since the relocations for the output sections
840 haven't been set yet. mark_symbols_used_in_relocations will
841 ignore input sections which have no corresponding output
842 section. */
843 bfd_map_over_sections (ibfd,
844 mark_symbols_used_in_relocations,
845 (PTR)isympp);
846 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
847 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
850 if (convert_debugging && dhandle != NULL)
852 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
854 status = 1;
855 return;
860 bfd_set_symtab (obfd, osympp, symcount);
862 /* This has to happen after the symbol table has been set. */
863 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
865 if (add_sections != NULL)
867 struct section_add *padd;
869 for (padd = add_sections; padd != NULL; padd = padd->next)
871 if (! bfd_set_section_contents (obfd, padd->section,
872 (PTR) padd->contents,
873 (file_ptr) 0,
874 (bfd_size_type) padd->size))
875 RETURN_NONFATAL (bfd_get_filename (obfd));
879 if (gap_fill_set || pad_to_set)
881 bfd_byte *buf;
882 int c, i;
884 /* Fill in the gaps. */
886 if (max_gap > 8192)
887 max_gap = 8192;
888 buf = (bfd_byte *) xmalloc (max_gap);
889 memset (buf, gap_fill, (size_t) max_gap);
891 c = bfd_count_sections (obfd);
892 for (i = 0; i < c; i++)
894 if (gaps[i] != 0)
896 bfd_size_type left;
897 file_ptr off;
899 left = gaps[i];
900 off = bfd_section_size (obfd, osections[i]) - left;
901 while (left > 0)
903 bfd_size_type now;
905 if (left > 8192)
906 now = 8192;
907 else
908 now = left;
910 if (! bfd_set_section_contents (obfd, osections[i], buf,
911 off, now))
912 RETURN_NONFATAL (bfd_get_filename (obfd));
914 left -= now;
915 off += now;
921 /* Allow the BFD backend to copy any private data it understands
922 from the input BFD to the output BFD. This is done last to
923 permit the routine to look at the filtered symbol table, which is
924 important for the ECOFF code at least. */
925 if (!bfd_copy_private_bfd_data (ibfd, obfd))
927 non_fatal (_("%s: error copying private BFD data: %s"),
928 bfd_get_filename (obfd),
929 bfd_errmsg (bfd_get_error ()));
930 status = 1;
931 return;
935 /* Read each archive element in turn from IBFD, copy the
936 contents to temp file, and keep the temp file handle. */
938 static void
939 copy_archive (ibfd, obfd, output_target)
940 bfd *ibfd;
941 bfd *obfd;
942 const char *output_target;
944 struct name_list
946 struct name_list *next;
947 char *name;
948 bfd *obfd;
949 } *list, *l;
950 bfd **ptr = &obfd->archive_head;
951 bfd *this_element;
952 char *dir = make_tempname (bfd_get_filename (obfd));
954 /* Make a temp directory to hold the contents. */
955 #if defined (_WIN32) && !defined (__CYGWIN32__)
956 if (mkdir (dir) != 0)
957 #else
958 if (mkdir (dir, 0700) != 0)
959 #endif
961 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
962 dir, strerror (errno));
964 obfd->has_armap = ibfd->has_armap;
966 list = NULL;
968 this_element = bfd_openr_next_archived_file (ibfd, NULL);
969 while (!status && this_element != (bfd *) NULL)
971 /* Create an output file for this member. */
972 char *output_name = concat (dir, "/", bfd_get_filename (this_element),
973 (char *) NULL);
974 bfd *output_bfd = bfd_openw (output_name, output_target);
975 bfd *last_element;
977 l = (struct name_list *) xmalloc (sizeof (struct name_list));
978 l->name = output_name;
979 l->next = list;
980 list = l;
982 if (output_bfd == (bfd *) NULL)
983 RETURN_NONFATAL (output_name);
985 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
986 RETURN_NONFATAL (bfd_get_filename (obfd));
988 if (bfd_check_format (this_element, bfd_object) == true)
989 copy_object (this_element, output_bfd);
991 if (!bfd_close (output_bfd))
993 bfd_nonfatal (bfd_get_filename (output_bfd));
994 /* Error in new object file. Don't change archive. */
995 status = 1;
998 /* Open the newly output file and attach to our list. */
999 output_bfd = bfd_openr (output_name, output_target);
1001 l->obfd = output_bfd;
1003 *ptr = output_bfd;
1004 ptr = &output_bfd->next;
1006 last_element = this_element;
1008 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1010 bfd_close (last_element);
1012 *ptr = (bfd *) NULL;
1014 if (!bfd_close (obfd))
1015 RETURN_NONFATAL (bfd_get_filename (obfd));
1017 if (!bfd_close (ibfd))
1018 RETURN_NONFATAL (bfd_get_filename (ibfd));
1020 /* Delete all the files that we opened. */
1021 for (l = list; l != NULL; l = l->next)
1023 bfd_close (l->obfd);
1024 unlink (l->name);
1026 rmdir (dir);
1029 /* The top-level control. */
1031 static void
1032 copy_file (input_filename, output_filename, input_target, output_target)
1033 const char *input_filename;
1034 const char *output_filename;
1035 const char *input_target;
1036 const char *output_target;
1038 bfd *ibfd;
1039 char **matching;
1041 /* To allow us to do "strip *" without dying on the first
1042 non-object file, failures are nonfatal. */
1044 ibfd = bfd_openr (input_filename, input_target);
1045 if (ibfd == NULL)
1046 RETURN_NONFATAL (input_filename);
1048 if (bfd_check_format (ibfd, bfd_archive))
1050 bfd *obfd;
1052 /* bfd_get_target does not return the correct value until
1053 bfd_check_format succeeds. */
1054 if (output_target == NULL)
1055 output_target = bfd_get_target (ibfd);
1057 obfd = bfd_openw (output_filename, output_target);
1058 if (obfd == NULL)
1059 RETURN_NONFATAL (output_filename);
1061 copy_archive (ibfd, obfd, output_target);
1063 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1065 bfd *obfd;
1067 /* bfd_get_target does not return the correct value until
1068 bfd_check_format succeeds. */
1069 if (output_target == NULL)
1070 output_target = bfd_get_target (ibfd);
1072 obfd = bfd_openw (output_filename, output_target);
1073 if (obfd == NULL)
1074 RETURN_NONFATAL (output_filename);
1076 copy_object (ibfd, obfd);
1078 if (!bfd_close (obfd))
1079 RETURN_NONFATAL (output_filename);
1081 if (!bfd_close (ibfd))
1082 RETURN_NONFATAL (input_filename);
1084 else
1086 bfd_nonfatal (input_filename);
1088 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1090 list_matching_formats (matching);
1091 free (matching);
1094 status = 1;
1098 /* Create a section in OBFD with the same name and attributes
1099 as ISECTION in IBFD. */
1101 static void
1102 setup_section (ibfd, isection, obfdarg)
1103 bfd *ibfd;
1104 sec_ptr isection;
1105 PTR obfdarg;
1107 bfd *obfd = (bfd *) obfdarg;
1108 struct section_list *p;
1109 sec_ptr osection;
1110 bfd_size_type size;
1111 bfd_vma vma;
1112 bfd_vma lma;
1113 flagword flags;
1114 char *err;
1116 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1117 && (strip_symbols == STRIP_DEBUG
1118 || strip_symbols == STRIP_UNNEEDED
1119 || strip_symbols == STRIP_ALL
1120 || discard_locals == LOCALS_ALL
1121 || convert_debugging))
1122 return;
1124 p = find_section_list (bfd_section_name (ibfd, isection), false);
1125 if (p != NULL)
1126 p->used = true;
1128 if (p != NULL && p->remove)
1129 return;
1131 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1133 if (osection == NULL)
1135 err = "making";
1136 goto loser;
1139 size = bfd_section_size (ibfd, isection);
1140 if (copy_byte >= 0)
1141 size = (size + interleave - 1) / interleave;
1142 if (! bfd_set_section_size (obfd, osection, size))
1144 err = "size";
1145 goto loser;
1148 vma = bfd_section_vma (ibfd, isection);
1149 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1150 vma += p->vma_val;
1151 else if (p != NULL && p->change_vma == CHANGE_SET)
1152 vma = p->vma_val;
1153 else
1154 vma += change_section_address;
1156 if (! bfd_set_section_vma (obfd, osection, vma))
1158 err = "vma";
1159 goto loser;
1162 lma = isection->lma;
1163 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1165 if (p->change_lma == CHANGE_MODIFY)
1166 lma += p->lma_val;
1167 else if (p->change_lma == CHANGE_SET)
1168 lma = p->lma_val;
1169 else
1170 abort ();
1172 else
1173 lma += change_section_address;
1175 osection->lma = lma;
1177 /* FIXME: This is probably not enough. If we change the LMA we
1178 may have to recompute the header for the file as well. */
1179 if (bfd_set_section_alignment (obfd,
1180 osection,
1181 bfd_section_alignment (ibfd, isection))
1182 == false)
1184 err = "alignment";
1185 goto loser;
1188 flags = bfd_get_section_flags (ibfd, isection);
1189 if (p != NULL && p->set_flags)
1190 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1191 if (!bfd_set_section_flags (obfd, osection, flags))
1193 err = "flags";
1194 goto loser;
1197 /* This used to be mangle_section; we do here to avoid using
1198 bfd_get_section_by_name since some formats allow multiple
1199 sections with the same name. */
1200 isection->output_section = osection;
1201 isection->output_offset = 0;
1203 /* Allow the BFD backend to copy any private data it understands
1204 from the input section to the output section. */
1205 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1207 err = "private data";
1208 goto loser;
1211 /* All went well */
1212 return;
1214 loser:
1215 non_fatal (_("%s: section `%s': error in %s: %s"),
1216 bfd_get_filename (ibfd),
1217 bfd_section_name (ibfd, isection),
1218 err, bfd_errmsg (bfd_get_error ()));
1219 status = 1;
1222 /* Copy the data of input section ISECTION of IBFD
1223 to an output section with the same name in OBFD.
1224 If stripping then don't copy any relocation info. */
1226 static void
1227 copy_section (ibfd, isection, obfdarg)
1228 bfd *ibfd;
1229 sec_ptr isection;
1230 PTR obfdarg;
1232 bfd *obfd = (bfd *) obfdarg;
1233 struct section_list *p;
1234 arelent **relpp;
1235 long relcount;
1236 sec_ptr osection;
1237 bfd_size_type size;
1239 /* If we have already failed earlier on, do not keep on generating
1240 complaints now. */
1241 if (status != 0)
1242 return;
1244 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1245 && (strip_symbols == STRIP_DEBUG
1246 || strip_symbols == STRIP_UNNEEDED
1247 || strip_symbols == STRIP_ALL
1248 || discard_locals == LOCALS_ALL
1249 || convert_debugging))
1251 return;
1254 p = find_section_list (bfd_section_name (ibfd, isection), false);
1256 if (p != NULL && p->remove)
1257 return;
1259 osection = isection->output_section;
1260 size = bfd_get_section_size_before_reloc (isection);
1262 if (size == 0 || osection == 0)
1263 return;
1265 if (strip_symbols == STRIP_ALL)
1266 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1267 else
1269 long relsize;
1271 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1272 if (relsize < 0)
1273 RETURN_NONFATAL (bfd_get_filename (ibfd));
1275 if (relsize == 0)
1276 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1277 else
1279 relpp = (arelent **) xmalloc (relsize);
1280 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1281 if (relcount < 0)
1282 RETURN_NONFATAL (bfd_get_filename (ibfd));
1284 bfd_set_reloc (obfd, osection, relpp, relcount);
1288 isection->_cooked_size = isection->_raw_size;
1289 isection->reloc_done = true;
1291 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1293 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1295 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1296 size))
1297 RETURN_NONFATAL (bfd_get_filename (ibfd));
1299 if (copy_byte >= 0)
1300 filter_bytes (memhunk, &size);
1302 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1303 size))
1304 RETURN_NONFATAL (bfd_get_filename (obfd));
1306 free (memhunk);
1308 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1310 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1312 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1313 flag--they can just remove the section entirely and add it
1314 back again. However, we do permit them to turn on the
1315 SEC_HAS_CONTENTS flag, and take it to mean that the section
1316 contents should be zeroed out. */
1318 memset (memhunk, 0, size);
1319 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1320 size))
1321 RETURN_NONFATAL (bfd_get_filename (obfd));
1322 free (memhunk);
1326 /* Get all the sections. This is used when --gap-fill or --pad-to is
1327 used. */
1329 static void
1330 get_sections (obfd, osection, secppparg)
1331 bfd *obfd;
1332 asection *osection;
1333 PTR secppparg;
1335 asection ***secppp = (asection ***) secppparg;
1337 **secppp = osection;
1338 ++(*secppp);
1341 /* Sort sections by VMA. This is called via qsort, and is used when
1342 --gap-fill or --pad-to is used. We force non loadable or empty
1343 sections to the front, where they are easier to ignore. */
1345 static int
1346 compare_section_lma (arg1, arg2)
1347 const PTR arg1;
1348 const PTR arg2;
1350 const asection **sec1 = (const asection **) arg1;
1351 const asection **sec2 = (const asection **) arg2;
1352 flagword flags1, flags2;
1354 /* Sort non loadable sections to the front. */
1355 flags1 = (*sec1)->flags;
1356 flags2 = (*sec2)->flags;
1357 if ((flags1 & SEC_HAS_CONTENTS) == 0
1358 || (flags1 & SEC_LOAD) == 0)
1360 if ((flags2 & SEC_HAS_CONTENTS) != 0
1361 && (flags2 & SEC_LOAD) != 0)
1362 return -1;
1364 else
1366 if ((flags2 & SEC_HAS_CONTENTS) == 0
1367 || (flags2 & SEC_LOAD) == 0)
1368 return 1;
1371 /* Sort sections by LMA. */
1372 if ((*sec1)->lma > (*sec2)->lma)
1373 return 1;
1374 else if ((*sec1)->lma < (*sec2)->lma)
1375 return -1;
1377 /* Sort sections with the same LMA by size. */
1378 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1379 return 1;
1380 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1381 return -1;
1383 return 0;
1386 /* Mark all the symbols which will be used in output relocations with
1387 the BSF_KEEP flag so that those symbols will not be stripped.
1389 Ignore relocations which will not appear in the output file. */
1391 static void
1392 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1393 bfd *ibfd;
1394 sec_ptr isection;
1395 PTR symbolsarg;
1397 asymbol **symbols = (asymbol **) symbolsarg;
1398 long relsize;
1399 arelent **relpp;
1400 long relcount, i;
1402 /* Ignore an input section with no corresponding output section. */
1403 if (isection->output_section == NULL)
1404 return;
1406 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1407 if (relsize < 0)
1408 bfd_fatal (bfd_get_filename (ibfd));
1410 if (relsize == 0)
1411 return;
1413 relpp = (arelent **) xmalloc (relsize);
1414 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1415 if (relcount < 0)
1416 bfd_fatal (bfd_get_filename (ibfd));
1418 /* Examine each symbol used in a relocation. If it's not one of the
1419 special bfd section symbols, then mark it with BSF_KEEP. */
1420 for (i = 0; i < relcount; i++)
1422 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1423 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1424 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1425 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1428 if (relpp != NULL)
1429 free (relpp);
1432 /* Write out debugging information. */
1434 static boolean
1435 write_debugging_info (obfd, dhandle, symcountp, symppp)
1436 bfd *obfd;
1437 PTR dhandle;
1438 long *symcountp;
1439 asymbol ***symppp;
1441 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1442 return write_ieee_debugging_info (obfd, dhandle);
1444 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1445 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1447 bfd_byte *syms, *strings;
1448 bfd_size_type symsize, stringsize;
1449 asection *stabsec, *stabstrsec;
1451 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1452 &symsize, &strings,
1453 &stringsize))
1454 return false;
1456 stabsec = bfd_make_section (obfd, ".stab");
1457 stabstrsec = bfd_make_section (obfd, ".stabstr");
1458 if (stabsec == NULL
1459 || stabstrsec == NULL
1460 || ! bfd_set_section_size (obfd, stabsec, symsize)
1461 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1462 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1463 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1464 || ! bfd_set_section_flags (obfd, stabsec,
1465 (SEC_HAS_CONTENTS
1466 | SEC_READONLY
1467 | SEC_DEBUGGING))
1468 || ! bfd_set_section_flags (obfd, stabstrsec,
1469 (SEC_HAS_CONTENTS
1470 | SEC_READONLY
1471 | SEC_DEBUGGING)))
1473 non_fatal (_("%s: can't create debugging section: %s"),
1474 bfd_get_filename (obfd),
1475 bfd_errmsg (bfd_get_error ()));
1476 return false;
1479 /* We can get away with setting the section contents now because
1480 the next thing the caller is going to do is copy over the
1481 real sections. We may someday have to split the contents
1482 setting out of this function. */
1483 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1484 symsize)
1485 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1486 (file_ptr) 0, stringsize))
1488 non_fatal (_("%s: can't set debugging section contents: %s"),
1489 bfd_get_filename (obfd),
1490 bfd_errmsg (bfd_get_error ()));
1491 return false;
1494 return true;
1497 non_fatal (_("%s: don't know how to write debugging information for %s"),
1498 bfd_get_filename (obfd), bfd_get_target (obfd));
1499 return false;
1502 /* The number of bytes to copy at once. */
1503 #define COPY_BUF 8192
1505 /* Copy file FROM to file TO, performing no translations.
1506 Return 0 if ok, -1 if error. */
1508 static int
1509 simple_copy (from, to)
1510 const char *from;
1511 const char *to;
1513 int fromfd, tofd, nread;
1514 int saved;
1515 char buf[COPY_BUF];
1517 fromfd = open (from, O_RDONLY);
1518 if (fromfd < 0)
1519 return -1;
1520 tofd = creat (to, 0777);
1521 if (tofd < 0)
1523 saved = errno;
1524 close (fromfd);
1525 errno = saved;
1526 return -1;
1528 while ((nread = read (fromfd, buf, sizeof buf)) > 0)
1530 if (write (tofd, buf, nread) != nread)
1532 saved = errno;
1533 close (fromfd);
1534 close (tofd);
1535 errno = saved;
1536 return -1;
1539 saved = errno;
1540 close (fromfd);
1541 close (tofd);
1542 if (nread < 0)
1544 errno = saved;
1545 return -1;
1547 return 0;
1550 #ifndef S_ISLNK
1551 #ifdef S_IFLNK
1552 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1553 #else
1554 #define S_ISLNK(m) 0
1555 #define lstat stat
1556 #endif
1557 #endif
1559 /* Rename FROM to TO, copying if TO is a link.
1560 Assumes that TO already exists, because FROM is a temp file.
1561 Return 0 if ok, -1 if error. */
1563 static int
1564 smart_rename (from, to)
1565 const char *from;
1566 const char *to;
1568 struct stat s;
1569 int ret = 0;
1571 if (lstat (to, &s))
1572 return -1;
1574 #if defined (_WIN32) && !defined (__CYGWIN32__)
1575 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1576 fail instead. Also, chown is not present. */
1578 if (stat (to, &s) == 0)
1579 remove (to);
1581 ret = rename (from, to);
1582 if (ret != 0)
1584 /* We have to clean up here. */
1586 non_fatal (_("%s: rename: %s"), to, strerror (errno));
1587 unlink (from);
1589 #else
1590 /* Use rename only if TO is not a symbolic link and has
1591 only one hard link. */
1592 if (!S_ISLNK (s.st_mode) && s.st_nlink == 1)
1594 ret = rename (from, to);
1595 if (ret == 0)
1597 /* Try to preserve the permission bits and ownership of TO.
1598 First get the mode right except for the setuid bit. Then
1599 change the ownership. Then fix the setuid bit. We do
1600 the chmod before the chown because if the chown succeeds,
1601 and we are a normal user, we won't be able to do the
1602 chmod afterward. We don't bother to fix the setuid bit
1603 first because that might introduce a fleeting security
1604 problem, and because the chown will clear the setuid bit
1605 anyhow. We only fix the setuid bit if the chown
1606 succeeds, because we don't want to introduce an
1607 unexpected setuid file owned by the user running objcopy. */
1608 chmod (to, s.st_mode & 0777);
1609 if (chown (to, s.st_uid, s.st_gid) >= 0)
1610 chmod (to, s.st_mode & 07777);
1612 else
1614 /* We have to clean up here. */
1615 non_fatal (_("%s: rename: %s"), to, strerror (errno));
1616 unlink (from);
1619 else
1621 ret = simple_copy (from, to);
1622 if (ret != 0)
1623 non_fatal (_("%s: simple_copy: %s"), to, strerror (errno));
1625 if (preserve_dates)
1626 set_times (to, &s);
1627 unlink (from);
1629 #endif /* _WIN32 && !__CYGWIN32__ */
1631 return ret;
1634 /* Set the times of the file DESTINATION to be the same as those in
1635 STATBUF. */
1637 static void
1638 set_times (destination, statbuf)
1639 const char *destination;
1640 const struct stat *statbuf;
1642 int result;
1645 #ifdef HAVE_GOOD_UTIME_H
1646 struct utimbuf tb;
1648 tb.actime = statbuf->st_atime;
1649 tb.modtime = statbuf->st_mtime;
1650 result = utime (destination, &tb);
1651 #else /* ! HAVE_GOOD_UTIME_H */
1652 #ifndef HAVE_UTIMES
1653 long tb[2];
1655 tb[0] = statbuf->st_atime;
1656 tb[1] = statbuf->st_mtime;
1657 result = utime (destination, tb);
1658 #else /* HAVE_UTIMES */
1659 struct timeval tv[2];
1661 tv[0].tv_sec = statbuf->st_atime;
1662 tv[0].tv_usec = 0;
1663 tv[1].tv_sec = statbuf->st_mtime;
1664 tv[1].tv_usec = 0;
1665 result = utimes (destination, tv);
1666 #endif /* HAVE_UTIMES */
1667 #endif /* ! HAVE_GOOD_UTIME_H */
1670 if (result != 0)
1671 non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
1674 static int
1675 strip_main (argc, argv)
1676 int argc;
1677 char *argv[];
1679 char *input_target = NULL, *output_target = NULL;
1680 boolean show_version = false;
1681 int c, i;
1682 struct section_list *p;
1683 char *output_file = NULL;
1685 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv",
1686 strip_options, (int *) 0)) != EOF)
1688 switch (c)
1690 case 'I':
1691 input_target = optarg;
1692 break;
1693 case 'O':
1694 output_target = optarg;
1695 break;
1696 case 'F':
1697 input_target = output_target = optarg;
1698 break;
1699 case 'R':
1700 p = find_section_list (optarg, true);
1701 p->remove = true;
1702 sections_removed = true;
1703 break;
1704 case 's':
1705 strip_symbols = STRIP_ALL;
1706 break;
1707 case 'S':
1708 case 'g':
1709 strip_symbols = STRIP_DEBUG;
1710 break;
1711 case OPTION_STRIP_UNNEEDED:
1712 strip_symbols = STRIP_UNNEEDED;
1713 break;
1714 case 'K':
1715 add_specific_symbol (optarg, &keep_specific_list);
1716 break;
1717 case 'N':
1718 add_specific_symbol (optarg, &strip_specific_list);
1719 break;
1720 case 'o':
1721 output_file = optarg;
1722 break;
1723 case 'p':
1724 preserve_dates = true;
1725 break;
1726 case 'x':
1727 discard_locals = LOCALS_ALL;
1728 break;
1729 case 'X':
1730 discard_locals = LOCALS_START_L;
1731 break;
1732 case 'v':
1733 verbose = true;
1734 break;
1735 case 'V':
1736 show_version = true;
1737 break;
1738 case 0:
1739 break; /* we've been given a long option */
1740 case 'h':
1741 strip_usage (stdout, 0);
1742 default:
1743 strip_usage (stderr, 1);
1747 if (show_version)
1748 print_version ("strip");
1750 /* Default is to strip all symbols. */
1751 if (strip_symbols == STRIP_UNDEF
1752 && discard_locals == LOCALS_UNDEF
1753 && strip_specific_list == NULL
1754 && keep_specific_list == NULL)
1755 strip_symbols = STRIP_ALL;
1757 if (output_target == (char *) NULL)
1758 output_target = input_target;
1760 i = optind;
1761 if (i == argc
1762 || (output_file != NULL && (i + 1) < argc))
1763 strip_usage (stderr, 1);
1765 for (; i < argc; i++)
1767 int hold_status = status;
1768 struct stat statbuf;
1769 char *tmpname;
1771 if (preserve_dates)
1773 if (stat (argv[i], &statbuf) < 0)
1775 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
1776 continue;
1780 if (output_file != NULL)
1781 tmpname = output_file;
1782 else
1783 tmpname = make_tempname (argv[i]);
1784 status = 0;
1786 copy_file (argv[i], tmpname, input_target, output_target);
1787 if (status == 0)
1789 if (preserve_dates)
1790 set_times (tmpname, &statbuf);
1791 if (output_file == NULL)
1792 smart_rename (tmpname, argv[i]);
1793 status = hold_status;
1795 else
1796 unlink (tmpname);
1797 if (output_file == NULL)
1798 free (tmpname);
1801 return 0;
1804 static int
1805 copy_main (argc, argv)
1806 int argc;
1807 char *argv[];
1809 char *input_filename = NULL, *output_filename = NULL;
1810 char *input_target = NULL, *output_target = NULL;
1811 boolean show_version = false;
1812 boolean change_warn = true;
1813 int c;
1814 struct section_list *p;
1815 struct stat statbuf;
1817 while ((c = getopt_long (argc, argv, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1818 copy_options, (int *) 0)) != EOF)
1820 switch (c)
1822 case 'b':
1823 copy_byte = atoi (optarg);
1824 if (copy_byte < 0)
1825 fatal (_("byte number must be non-negative"));
1826 break;
1827 case 'i':
1828 interleave = atoi (optarg);
1829 if (interleave < 1)
1830 fatal (_("interleave must be positive"));
1831 break;
1832 case 'I':
1833 case 's': /* "source" - 'I' is preferred */
1834 input_target = optarg;
1835 break;
1836 case 'O':
1837 case 'd': /* "destination" - 'O' is preferred */
1838 output_target = optarg;
1839 break;
1840 case 'F':
1841 input_target = output_target = optarg;
1842 break;
1843 case 'R':
1844 p = find_section_list (optarg, true);
1845 p->remove = true;
1846 sections_removed = true;
1847 break;
1848 case 'S':
1849 strip_symbols = STRIP_ALL;
1850 break;
1851 case 'g':
1852 strip_symbols = STRIP_DEBUG;
1853 break;
1854 case OPTION_STRIP_UNNEEDED:
1855 strip_symbols = STRIP_UNNEEDED;
1856 break;
1857 case 'K':
1858 add_specific_symbol (optarg, &keep_specific_list);
1859 break;
1860 case 'N':
1861 add_specific_symbol (optarg, &strip_specific_list);
1862 break;
1863 case 'L':
1864 add_specific_symbol (optarg, &localize_specific_list);
1865 break;
1866 case 'W':
1867 add_specific_symbol (optarg, &weaken_specific_list);
1868 break;
1869 case 'p':
1870 preserve_dates = true;
1871 break;
1872 case 'x':
1873 discard_locals = LOCALS_ALL;
1874 break;
1875 case 'X':
1876 discard_locals = LOCALS_START_L;
1877 break;
1878 case 'v':
1879 verbose = true;
1880 break;
1881 case 'V':
1882 show_version = true;
1883 break;
1884 case OPTION_WEAKEN:
1885 weaken = true;
1886 break;
1887 case OPTION_ADD_SECTION:
1889 const char *s;
1890 struct stat st;
1891 struct section_add *pa;
1892 int len;
1893 char *name;
1894 FILE *f;
1896 s = strchr (optarg, '=');
1898 if (s == NULL)
1899 fatal (_("bad format for --add-section NAME=FILENAME"));
1901 if (stat (s + 1, & st) < 0)
1902 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
1904 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1906 len = s - optarg;
1907 name = (char *) xmalloc (len + 1);
1908 strncpy (name, optarg, len);
1909 name[len] = '\0';
1910 pa->name = name;
1912 pa->filename = s + 1;
1914 pa->size = st.st_size;
1916 pa->contents = (bfd_byte *) xmalloc (pa->size);
1917 f = fopen (pa->filename, FOPEN_RB);
1919 if (f == NULL)
1920 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
1922 if (fread (pa->contents, 1, pa->size, f) == 0
1923 || ferror (f))
1924 fatal (_("%s: fread failed"), pa->filename);
1926 fclose (f);
1928 pa->next = add_sections;
1929 add_sections = pa;
1931 break;
1932 case OPTION_CHANGE_START:
1933 change_start = parse_vma (optarg, "--change-start");
1934 break;
1935 case OPTION_CHANGE_SECTION_ADDRESS:
1936 case OPTION_CHANGE_SECTION_LMA:
1937 case OPTION_CHANGE_SECTION_VMA:
1939 const char *s;
1940 int len;
1941 char *name;
1942 char *option;
1943 bfd_vma val;
1944 enum change_action what;
1946 switch (c)
1948 case OPTION_CHANGE_SECTION_ADDRESS: option = "--change-section-address"; break;
1949 case OPTION_CHANGE_SECTION_LMA: option = "--change-section-lma"; break;
1950 case OPTION_CHANGE_SECTION_VMA: option = "--change-section-vma"; break;
1953 s = strchr (optarg, '=');
1954 if (s == NULL)
1956 s = strchr (optarg, '+');
1957 if (s == NULL)
1959 s = strchr (optarg, '-');
1960 if (s == NULL)
1961 fatal (_("bad format for %s"), option);
1965 len = s - optarg;
1966 name = (char *) xmalloc (len + 1);
1967 strncpy (name, optarg, len);
1968 name[len] = '\0';
1970 p = find_section_list (name, true);
1972 val = parse_vma (s + 1, option);
1974 switch (*s)
1976 case '=': what = CHANGE_SET; break;
1977 case '-': val = - val; /* Drop through. */
1978 case '+': what = CHANGE_MODIFY; break;
1981 switch (c)
1983 case OPTION_CHANGE_SECTION_ADDRESS:
1984 p->change_vma = what;
1985 p->vma_val = val;
1986 /* Drop through. */
1988 case OPTION_CHANGE_SECTION_LMA:
1989 p->change_lma = what;
1990 p->lma_val = val;
1991 break;
1993 case OPTION_CHANGE_SECTION_VMA:
1994 p->change_vma = what;
1995 p->vma_val = val;
1996 break;
1999 break;
2000 case OPTION_CHANGE_ADDRESSES:
2001 change_section_address = parse_vma (optarg, "--change-addresses");
2002 change_start = change_section_address;
2003 break;
2004 case OPTION_CHANGE_WARNINGS:
2005 change_warn = true;
2006 break;
2007 case OPTION_CHANGE_LEADING_CHAR:
2008 change_leading_char = true;
2009 break;
2010 case OPTION_DEBUGGING:
2011 convert_debugging = true;
2012 break;
2013 case OPTION_GAP_FILL:
2015 bfd_vma gap_fill_vma;
2017 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2018 gap_fill = (bfd_byte) gap_fill_vma;
2019 if ((bfd_vma) gap_fill != gap_fill_vma)
2021 char buff[20];
2023 sprintf_vma (buff, gap_fill_vma);
2025 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2026 buff, gap_fill);
2028 gap_fill_set = true;
2030 break;
2031 case OPTION_NO_CHANGE_WARNINGS:
2032 change_warn = false;
2033 break;
2034 case OPTION_PAD_TO:
2035 pad_to = parse_vma (optarg, "--pad-to");
2036 pad_to_set = true;
2037 break;
2038 case OPTION_REMOVE_LEADING_CHAR:
2039 remove_leading_char = true;
2040 break;
2041 case OPTION_SET_SECTION_FLAGS:
2043 const char *s;
2044 int len;
2045 char *name;
2047 s = strchr (optarg, '=');
2048 if (s == NULL)
2049 fatal (_("bad format for --set-section-flags"));
2051 len = s - optarg;
2052 name = (char *) xmalloc (len + 1);
2053 strncpy (name, optarg, len);
2054 name[len] = '\0';
2056 p = find_section_list (name, true);
2058 p->set_flags = true;
2059 p->flags = parse_flags (s + 1);
2061 break;
2062 case OPTION_SET_START:
2063 set_start = parse_vma (optarg, "--set-start");
2064 set_start_set = true;
2065 break;
2066 case 0:
2067 break; /* we've been given a long option */
2068 case 'h':
2069 copy_usage (stdout, 0);
2070 default:
2071 copy_usage (stderr, 1);
2075 if (show_version)
2076 print_version ("objcopy");
2078 if (copy_byte >= interleave)
2079 fatal (_("byte number must be less than interleave"));
2081 if (optind == argc || optind + 2 < argc)
2082 copy_usage (stderr, 1);
2084 input_filename = argv[optind];
2085 if (optind + 1 < argc)
2086 output_filename = argv[optind + 1];
2088 /* Default is to strip no symbols. */
2089 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2090 strip_symbols = STRIP_NONE;
2092 if (output_target == (char *) NULL)
2093 output_target = input_target;
2095 if (preserve_dates)
2097 if (stat (input_filename, &statbuf) < 0)
2098 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2101 /* If there is no destination file then create a temp and rename
2102 the result into the input. */
2104 if (output_filename == (char *) NULL)
2106 char *tmpname = make_tempname (input_filename);
2108 copy_file (input_filename, tmpname, input_target, output_target);
2109 if (status == 0)
2111 if (preserve_dates)
2112 set_times (tmpname, &statbuf);
2113 smart_rename (tmpname, input_filename);
2115 else
2116 unlink (tmpname);
2118 else
2120 copy_file (input_filename, output_filename, input_target, output_target);
2121 if (status == 0 && preserve_dates)
2122 set_times (output_filename, &statbuf);
2125 if (change_warn)
2127 for (p = change_sections; p != NULL; p = p->next)
2129 if (! p->used)
2131 if (p->change_vma != CHANGE_IGNORE)
2133 char buff [20];
2135 sprintf_vma (buff, p->vma_val);
2137 /* xgettext:c-format */
2138 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
2139 p->name,
2140 p->change_vma == CHANGE_SET ? '=' : '+',
2141 buff);
2144 if (p->change_lma != CHANGE_IGNORE)
2146 char buff [20];
2148 sprintf_vma (buff, p->lma_val);
2150 /* xgettext:c-format */
2151 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
2152 p->name,
2153 p->change_lma == CHANGE_SET ? '=' : '+',
2154 buff);
2160 return 0;
2164 main (argc, argv)
2165 int argc;
2166 char *argv[];
2168 #ifdef HAVE_SETLOCALE
2169 setlocale (LC_MESSAGES, "");
2170 #endif
2171 bindtextdomain (PACKAGE, LOCALEDIR);
2172 textdomain (PACKAGE);
2174 program_name = argv[0];
2175 xmalloc_set_program_name (program_name);
2177 START_PROGRESS (program_name, 0);
2179 strip_symbols = STRIP_UNDEF;
2180 discard_locals = LOCALS_UNDEF;
2182 bfd_init ();
2183 set_default_bfd_target ();
2185 if (is_strip < 0)
2187 int i = strlen (program_name);
2188 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
2191 if (is_strip)
2192 strip_main (argc, argv);
2193 else
2194 copy_main (argc, argv);
2196 END_PROGRESS (program_name);
2198 return status;