* configure.in: If frexp is not available, check in -lm.
[deliverable/binutils-gdb.git] / binutils / objcopy.c
CommitLineData
252b5132
RH
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.
4
5 This file is part of GNU Binutils.
6
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.
11
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.
16
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. */
21\f
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>
29
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. */
34
35struct symlist
36{
37 const char *name;
38 struct symlist *next;
39};
40
41static void copy_usage PARAMS ((FILE *, int));
42static void strip_usage PARAMS ((FILE *, int));
43static flagword parse_flags PARAMS ((const char *));
44static struct section_list *find_section_list PARAMS ((const char *, boolean));
45static void setup_section PARAMS ((bfd *, asection *, PTR));
46static void copy_section PARAMS ((bfd *, asection *, PTR));
47static void get_sections PARAMS ((bfd *, asection *, PTR));
48static int compare_section_lma PARAMS ((const PTR, const PTR));
49static void add_specific_symbol PARAMS ((const char *, struct symlist **));
50static boolean is_specified_symbol PARAMS ((const char *, struct symlist *));
51static boolean is_strip_section PARAMS ((bfd *, asection *));
52static unsigned int filter_symbols
53 PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long));
54static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
55static void filter_bytes PARAMS ((char *, bfd_size_type *));
56static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***));
57static void copy_object PARAMS ((bfd *, bfd *));
58static void copy_archive PARAMS ((bfd *, bfd *, const char *));
59static void copy_file
60 PARAMS ((const char *, const char *, const char *, const char *));
61static int strip_main PARAMS ((int, char **));
62static int copy_main PARAMS ((int, char **));
63
64#define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
65
66static asymbol **isympp = NULL; /* Input symbols */
67static asymbol **osympp = NULL; /* Output symbols that survive stripping */
68
69/* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
70static int copy_byte = -1;
71static int interleave = 4;
72
73static boolean verbose; /* Print file and target names. */
74static boolean preserve_dates; /* Preserve input file timestamp. */
75static int status = 0; /* Exit status. */
76
77enum strip_action
78 {
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 */
84 };
85
86/* Which symbols to remove. */
87static enum strip_action strip_symbols;
88
89enum locals_action
90 {
91 LOCALS_UNDEF,
92 LOCALS_START_L, /* discard locals starting with L */
93 LOCALS_ALL /* discard all locals */
94 };
95
96/* Which local symbols to remove. Overrides STRIP_ALL. */
97static enum locals_action discard_locals;
98
99/* What kind of change to perform. */
100enum change_action
101{
102 CHANGE_IGNORE,
103 CHANGE_MODIFY,
104 CHANGE_SET
105};
106
107/* Structure used to hold lists of sections and actions to take. */
108struct section_list
109{
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. */
120};
121
122static struct section_list *change_sections;
123static boolean sections_removed;
124
125/* Changes to the start address. */
126static bfd_vma change_start = 0;
127static boolean set_start_set = false;
128static bfd_vma set_start;
129
130/* Changes to section addresses. */
131static bfd_vma change_section_address = 0;
132
133/* Filling gaps between sections. */
134static boolean gap_fill_set = false;
135static bfd_byte gap_fill = 0;
136
137/* Pad to a given address. */
138static boolean pad_to_set = false;
139static bfd_vma pad_to;
140
141/* List of sections to add. */
142
143struct section_add
144{
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;
157};
158
159static struct section_add *add_sections;
160
161/* Whether to convert debugging information. */
162
163static boolean convert_debugging = false;
164
165/* Whether to change the leading character in symbol names. */
166
167static boolean change_leading_char = false;
168
169/* Whether to remove the leading character from global symbol names. */
170
171static boolean remove_leading_char = false;
172
173/* List of symbols to strip, keep, localize, and weaken. */
174
175static struct symlist *strip_specific_list = NULL;
176static struct symlist *keep_specific_list = NULL;
177static struct symlist *localize_specific_list = NULL;
178static struct symlist *weaken_specific_list = NULL;
179
180/* If this is true, we weaken global symbols (set BSF_WEAK). */
181
182static boolean weaken = false;
183
184/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
185
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)
203
204/* Options to handle if running as "strip". */
205
206static struct option strip_options[] =
207{
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}
227};
228
229/* Options to handle if running as "objcopy". */
230
231static struct option copy_options[] =
232{
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}
277};
278
279/* IMPORTS */
280extern char *program_name;
281
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. */
285extern int is_strip;
286
287
288static void
289copy_usage (stream, exit_status)
290 FILE *stream;
291 int exit_status;
292{
293 fprintf (stream, _("\
294Usage: %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);
320}
321
322static void
323strip_usage (stream, exit_status)
324 FILE *stream;
325 int exit_status;
326{
327 fprintf (stream, _("\
328Usage: %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);
339}
340
341/* Parse section flags into a flagword, with a fatal error if the
342 string can't be parsed. */
343
344static flagword
345parse_flags (s)
346 const char *s;
347{
348 flagword ret;
349 const char *snext;
350 int len;
351
352 ret = SEC_NO_FLAGS;
353
354 do
355 {
356 snext = strchr (s, ',');
357 if (snext == NULL)
358 len = strlen (s);
359 else
360 {
361 len = snext - s;
362 ++snext;
363 }
364
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
377 {
378 char *copy;
379
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"));
385 }
386
387 s = snext;
388 }
389 while (s != NULL);
390
391 return ret;
392}
393
394/* Find and optionally add an entry in the change_sections list. */
395
396static struct section_list *
397find_section_list (name, add)
398 const char *name;
399 boolean add;
400{
401 register struct section_list *p;
402
403 for (p = change_sections; p != NULL; p = p->next)
404 if (strcmp (p->name, name) == 0)
405 return p;
406
407 if (! add)
408 return NULL;
409
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;
420
421 p->next = change_sections;
422 change_sections = p;
423
424 return p;
425}
426
427/* Add a symbol to strip_specific_list. */
428
429static void
430add_specific_symbol (name, list)
431 const char *name;
432 struct symlist **list;
433{
434 struct symlist *tmp_list;
435
436 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
437 tmp_list->name = name;
438 tmp_list->next = *list;
439 *list = tmp_list;
440}
441
442/* See whether a symbol should be stripped or kept based on
443 strip_specific_list and keep_symbols. */
444
445static boolean
446is_specified_symbol (name, list)
447 const char *name;
448 struct symlist *list;
449{
450 struct symlist *tmp_list;
451
452 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
453 {
454 if (strcmp (name, tmp_list->name) == 0)
455 return true;
456 }
457 return false;
458}
459
460/* See if a section is being removed. */
461
462static boolean
463is_strip_section (abfd, sec)
464 bfd *abfd;
465 asection *sec;
466{
467 struct section_list *p;
468
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;
476
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;
481}
482
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. */
486
487static unsigned int
488filter_symbols (abfd, obfd, osyms, isyms, symcount)
489 bfd *abfd;
490 bfd *obfd;
491 asymbol **osyms, **isyms;
492 long symcount;
493{
494 register asymbol **from = isyms, **to = osyms;
495 long src_count = 0, dst_count = 0;
496
497 for (; src_count < symcount; src_count++)
498 {
499 asymbol *sym = from[src_count];
500 flagword flags = sym->flags;
501 const char *name = bfd_asymbol_name (sym);
502 int keep;
503
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))))
509 {
510 if (bfd_get_symbol_leading_char (obfd) == '\0')
511 name = bfd_asymbol_name (sym) = name + 1;
512 else
513 {
514 char *n;
515
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;
523 }
524 }
525
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;
533
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))));
555
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;
562
563 if (keep && (flags & BSF_GLOBAL) != 0
564 && (weaken || is_specified_symbol (name, weaken_specific_list)))
565 {
566 sym->flags &=~ BSF_GLOBAL;
567 sym->flags |= BSF_WEAK;
568 }
569 if (keep && (flags & (BSF_GLOBAL | BSF_WEAK))
570 && is_specified_symbol (name, localize_specific_list))
571 {
572 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
573 sym->flags |= BSF_LOCAL;
574 }
575
576 if (keep)
577 to[dst_count++] = sym;
578 }
579
580 to[dst_count] = NULL;
581
582 return dst_count;
583}
584
585/* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
586 Adjust *SIZE. */
587
588static void
589filter_bytes (memhunk, size)
590 char *memhunk;
591 bfd_size_type *size;
592{
593 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
594
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;
601}
602
603/* Copy object file IBFD onto OBFD. */
604
605static void
606copy_object (ibfd, obfd)
607 bfd *ibfd;
608 bfd *obfd;
609{
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;
617
618
619 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
620 RETURN_NONFATAL (bfd_get_filename (obfd));
621
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));
626
627 if (set_start_set)
628 start = set_start;
629 else
630 start = bfd_get_start_address (ibfd);
631 start += change_start;
632
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));
638
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)));
645
646 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
647 RETURN_NONFATAL (bfd_get_filename (ibfd));
648
649 if (isympp)
650 free (isympp);
651
652 if (osympp != isympp)
653 free (osympp);
654
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);
658
659 if (add_sections != NULL)
660 {
661 struct section_add *padd;
662 struct section_list *pset;
663
664 for (padd = add_sections; padd != NULL; padd = padd->next)
665 {
666 padd->section = bfd_make_section (obfd, padd->name);
667 if (padd->section == NULL)
668 {
669 non_fatal (_("can't create section `%s': %s"),
670 padd->name, bfd_errmsg (bfd_get_error ()));
671 status = 1;
672 return;
673 }
674 else
675 {
676 flagword flags;
677
678 if (! bfd_set_section_size (obfd, padd->section, padd->size))
679 RETURN_NONFATAL (bfd_get_filename (obfd));
680
681 pset = find_section_list (padd->name, false);
682 if (pset != NULL)
683 pset->used = true;
684
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;
689
690 if (! bfd_set_section_flags (obfd, padd->section, flags))
691 RETURN_NONFATAL (bfd_get_filename (obfd));
692
693 if (pset != NULL)
694 {
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));
698
699 if (pset->change_lma != CHANGE_IGNORE)
700 {
701 padd->section->lma = pset->lma_val;
702
703 if (! bfd_set_section_alignment
704 (obfd, padd->section,
705 bfd_section_alignment (obfd, padd->section)))
706 RETURN_NONFATAL (bfd_get_filename (obfd));
707 }
708 }
709 }
710 }
711 }
712
713 if (gap_fill_set || pad_to_set)
714 {
715 asection **set;
716 unsigned int c, i;
717
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. */
723
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);
728
729 qsort (osections, c, sizeof (asection *), compare_section_lma);
730
731 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
732 memset (gaps, 0, c * sizeof (bfd_size_type));
733
734 if (gap_fill_set)
735 {
736 for (i = 0; i < c - 1; i++)
737 {
738 flagword flags;
739 bfd_size_type size;
740 bfd_vma gap_start, gap_stop;
741
742 flags = bfd_get_section_flags (obfd, osections[i]);
743 if ((flags & SEC_HAS_CONTENTS) == 0
744 || (flags & SEC_LOAD) == 0)
745 continue;
746
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)
751 {
752 if (! bfd_set_section_size (obfd, osections[i],
753 size + (gap_stop - gap_start)))
754 {
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;
760 }
761 gaps[i] = gap_stop - gap_start;
762 if (max_gap < gap_stop - gap_start)
763 max_gap = gap_stop - gap_start;
764 }
765 }
766 }
767
768 if (pad_to_set)
769 {
770 bfd_vma lma;
771 bfd_size_type size;
772
773 lma = bfd_section_lma (obfd, osections[c - 1]);
774 size = bfd_section_size (obfd, osections[c - 1]);
775 if (lma + size < pad_to)
776 {
777 if (! bfd_set_section_size (obfd, osections[c - 1],
778 pad_to - lma))
779 {
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;
784 }
785 else
786 {
787 gaps[c - 1] = pad_to - (lma + size);
788 if (max_gap < pad_to - (lma + size))
789 max_gap = pad_to - (lma + size);
790 }
791 }
792 }
793 }
794
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));
801
802 osympp = isympp = (asymbol **) xmalloc (symsize);
803 symcount = bfd_canonicalize_symtab (ibfd, isympp);
804 if (symcount < 0)
805 RETURN_NONFATAL (bfd_get_filename (ibfd));
806
807 if (convert_debugging)
808 dhandle = read_debugging_info (ibfd, isympp, symcount);
809
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)
823 {
824 /* Mark symbols used in output relocations so that they
825 are kept, even if they are local labels or static symbols.
826
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);
838 }
839
840 if (convert_debugging && dhandle != NULL)
841 {
842 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
843 {
844 status = 1;
845 return;
846 }
847 }
848
849 bfd_set_symtab (obfd, osympp, symcount);
850
851 /* This has to happen after the symbol table has been set. */
852 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
853
854 if (add_sections != NULL)
855 {
856 struct section_add *padd;
857
858 for (padd = add_sections; padd != NULL; padd = padd->next)
859 {
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));
865 }
866 }
867
868 if (gap_fill_set || pad_to_set)
869 {
870 bfd_byte *buf;
871 int c, i;
872
873 /* Fill in the gaps. */
874
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);
879
880 c = bfd_count_sections (obfd);
881 for (i = 0; i < c; i++)
882 {
883 if (gaps[i] != 0)
884 {
885 bfd_size_type left;
886 file_ptr off;
887
888 left = gaps[i];
889 off = bfd_section_size (obfd, osections[i]) - left;
890 while (left > 0)
891 {
892 bfd_size_type now;
893
894 if (left > 8192)
895 now = 8192;
896 else
897 now = left;
898
899 if (! bfd_set_section_contents (obfd, osections[i], buf,
900 off, now))
901 RETURN_NONFATAL (bfd_get_filename (obfd));
902
903 left -= now;
904 off += now;
905 }
906 }
907 }
908 }
909
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))
915 {
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;
921 }
922}
923
924/* Read each archive element in turn from IBFD, copy the
925 contents to temp file, and keep the temp file handle. */
926
927static void
928copy_archive (ibfd, obfd, output_target)
929 bfd *ibfd;
930 bfd *obfd;
931 const char *output_target;
932{
933 struct name_list
934 {
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));
942
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
949 {
950 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
951 dir, strerror (errno));
952 }
953 obfd->has_armap = ibfd->has_armap;
954
955 list = NULL;
956
957 this_element = bfd_openr_next_archived_file (ibfd, NULL);
958 while (!status && this_element != (bfd *) NULL)
959 {
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;
8066d1a2
AS
965 struct stat buf;
966 int stat_status = 0;
967
968 if (preserve_dates)
969 {
970 stat_status = bfd_stat_arch_elt (this_element, &buf);
971 if (stat_status != 0)
972 non_fatal (_("internal stat error on %s"),
973 bfd_get_filename (this_element));
974 }
252b5132
RH
975
976 l = (struct name_list *) xmalloc (sizeof (struct name_list));
977 l->name = output_name;
978 l->next = list;
979 list = l;
980
981 if (output_bfd == (bfd *) NULL)
982 RETURN_NONFATAL (output_name);
983
984 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
985 RETURN_NONFATAL (bfd_get_filename (obfd));
986
987 if (bfd_check_format (this_element, bfd_object) == true)
988 copy_object (this_element, output_bfd);
989
990 if (!bfd_close (output_bfd))
991 {
992 bfd_nonfatal (bfd_get_filename (output_bfd));
993 /* Error in new object file. Don't change archive. */
994 status = 1;
995 }
996
8066d1a2
AS
997 if (preserve_dates && stat_status == 0)
998 set_times (output_name, &buf);
999
252b5132
RH
1000 /* Open the newly output file and attach to our list. */
1001 output_bfd = bfd_openr (output_name, output_target);
1002
1003 l->obfd = output_bfd;
1004
1005 *ptr = output_bfd;
1006 ptr = &output_bfd->next;
1007
1008 last_element = this_element;
1009
1010 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1011
1012 bfd_close (last_element);
1013 }
1014 *ptr = (bfd *) NULL;
1015
1016 if (!bfd_close (obfd))
1017 RETURN_NONFATAL (bfd_get_filename (obfd));
1018
1019 if (!bfd_close (ibfd))
1020 RETURN_NONFATAL (bfd_get_filename (ibfd));
1021
1022 /* Delete all the files that we opened. */
1023 for (l = list; l != NULL; l = l->next)
1024 {
1025 bfd_close (l->obfd);
1026 unlink (l->name);
1027 }
1028 rmdir (dir);
1029}
1030
1031/* The top-level control. */
1032
1033static void
1034copy_file (input_filename, output_filename, input_target, output_target)
1035 const char *input_filename;
1036 const char *output_filename;
1037 const char *input_target;
1038 const char *output_target;
1039{
1040 bfd *ibfd;
1041 char **matching;
1042
1043 /* To allow us to do "strip *" without dying on the first
1044 non-object file, failures are nonfatal. */
1045
1046 ibfd = bfd_openr (input_filename, input_target);
1047 if (ibfd == NULL)
1048 RETURN_NONFATAL (input_filename);
1049
1050 if (bfd_check_format (ibfd, bfd_archive))
1051 {
1052 bfd *obfd;
1053
1054 /* bfd_get_target does not return the correct value until
1055 bfd_check_format succeeds. */
1056 if (output_target == NULL)
1057 output_target = bfd_get_target (ibfd);
1058
1059 obfd = bfd_openw (output_filename, output_target);
1060 if (obfd == NULL)
1061 RETURN_NONFATAL (output_filename);
1062
1063 copy_archive (ibfd, obfd, output_target);
1064 }
1065 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1066 {
1067 bfd *obfd;
1068
1069 /* bfd_get_target does not return the correct value until
1070 bfd_check_format succeeds. */
1071 if (output_target == NULL)
1072 output_target = bfd_get_target (ibfd);
1073
1074 obfd = bfd_openw (output_filename, output_target);
1075 if (obfd == NULL)
1076 RETURN_NONFATAL (output_filename);
1077
1078 copy_object (ibfd, obfd);
1079
1080 if (!bfd_close (obfd))
1081 RETURN_NONFATAL (output_filename);
1082
1083 if (!bfd_close (ibfd))
1084 RETURN_NONFATAL (input_filename);
1085 }
1086 else
1087 {
1088 bfd_nonfatal (input_filename);
1089
1090 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1091 {
1092 list_matching_formats (matching);
1093 free (matching);
1094 }
1095
1096 status = 1;
1097 }
1098}
1099
1100/* Create a section in OBFD with the same name and attributes
1101 as ISECTION in IBFD. */
1102
1103static void
1104setup_section (ibfd, isection, obfdarg)
1105 bfd *ibfd;
1106 sec_ptr isection;
1107 PTR obfdarg;
1108{
1109 bfd *obfd = (bfd *) obfdarg;
1110 struct section_list *p;
1111 sec_ptr osection;
1112 bfd_size_type size;
1113 bfd_vma vma;
1114 bfd_vma lma;
1115 flagword flags;
1116 char *err;
1117
1118 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1119 && (strip_symbols == STRIP_DEBUG
1120 || strip_symbols == STRIP_UNNEEDED
1121 || strip_symbols == STRIP_ALL
1122 || discard_locals == LOCALS_ALL
1123 || convert_debugging))
1124 return;
1125
1126 p = find_section_list (bfd_section_name (ibfd, isection), false);
1127 if (p != NULL)
1128 p->used = true;
1129
1130 if (p != NULL && p->remove)
1131 return;
1132
1133 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1134
1135 if (osection == NULL)
1136 {
1137 err = "making";
1138 goto loser;
1139 }
1140
1141 size = bfd_section_size (ibfd, isection);
1142 if (copy_byte >= 0)
1143 size = (size + interleave - 1) / interleave;
1144 if (! bfd_set_section_size (obfd, osection, size))
1145 {
1146 err = "size";
1147 goto loser;
1148 }
1149
1150 vma = bfd_section_vma (ibfd, isection);
1151 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1152 vma += p->vma_val;
1153 else if (p != NULL && p->change_vma == CHANGE_SET)
1154 vma = p->vma_val;
1155 else
1156 vma += change_section_address;
1157
1158 if (! bfd_set_section_vma (obfd, osection, vma))
1159 {
1160 err = "vma";
1161 goto loser;
1162 }
1163
1164 lma = isection->lma;
1165 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1166 {
1167 if (p->change_lma == CHANGE_MODIFY)
1168 lma += p->lma_val;
1169 else if (p->change_lma == CHANGE_SET)
1170 lma = p->lma_val;
1171 else
1172 abort ();
1173 }
1174 else
1175 lma += change_section_address;
1176
1177 osection->lma = lma;
1178
1179 /* FIXME: This is probably not enough. If we change the LMA we
1180 may have to recompute the header for the file as well. */
1181 if (bfd_set_section_alignment (obfd,
1182 osection,
1183 bfd_section_alignment (ibfd, isection))
1184 == false)
1185 {
1186 err = "alignment";
1187 goto loser;
1188 }
1189
1190 flags = bfd_get_section_flags (ibfd, isection);
1191 if (p != NULL && p->set_flags)
1192 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1193 if (!bfd_set_section_flags (obfd, osection, flags))
1194 {
1195 err = "flags";
1196 goto loser;
1197 }
1198
1199 /* This used to be mangle_section; we do here to avoid using
1200 bfd_get_section_by_name since some formats allow multiple
1201 sections with the same name. */
1202 isection->output_section = osection;
1203 isection->output_offset = 0;
1204
1205 /* Allow the BFD backend to copy any private data it understands
1206 from the input section to the output section. */
1207 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1208 {
1209 err = "private data";
1210 goto loser;
1211 }
1212
1213 /* All went well */
1214 return;
1215
1216loser:
1217 non_fatal (_("%s: section `%s': error in %s: %s"),
1218 bfd_get_filename (ibfd),
1219 bfd_section_name (ibfd, isection),
1220 err, bfd_errmsg (bfd_get_error ()));
1221 status = 1;
1222}
1223
1224/* Copy the data of input section ISECTION of IBFD
1225 to an output section with the same name in OBFD.
1226 If stripping then don't copy any relocation info. */
1227
1228static void
1229copy_section (ibfd, isection, obfdarg)
1230 bfd *ibfd;
1231 sec_ptr isection;
1232 PTR obfdarg;
1233{
1234 bfd *obfd = (bfd *) obfdarg;
1235 struct section_list *p;
1236 arelent **relpp;
1237 long relcount;
1238 sec_ptr osection;
1239 bfd_size_type size;
1240 long relsize;
1241
1242 /* If we have already failed earlier on, do not keep on generating
1243 complaints now. */
1244 if (status != 0)
1245 return;
1246
1247 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1248 && (strip_symbols == STRIP_DEBUG
1249 || strip_symbols == STRIP_UNNEEDED
1250 || strip_symbols == STRIP_ALL
1251 || discard_locals == LOCALS_ALL
1252 || convert_debugging))
1253 {
1254 return;
1255 }
1256
1257 p = find_section_list (bfd_section_name (ibfd, isection), false);
1258
1259 if (p != NULL && p->remove)
1260 return;
1261
1262 osection = isection->output_section;
1263 size = bfd_get_section_size_before_reloc (isection);
1264
1265 if (size == 0 || osection == 0)
1266 return;
1267
1268
1269 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1270 if (relsize < 0)
1271 RETURN_NONFATAL (bfd_get_filename (ibfd));
1272
1273 if (relsize == 0)
1274 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1275 else
1276 {
1277 relpp = (arelent **) xmalloc (relsize);
1278 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1279 if (relcount < 0)
1280 RETURN_NONFATAL (bfd_get_filename (ibfd));
1281
1282 if (strip_symbols == STRIP_ALL)
1283 {
1284 /* Remove relocations which are not in
1285 keep_strip_specific_list. */
1286 arelent **temp_relpp;
1287 long temp_relcount = 0;
1288 long i;
1289
1290 temp_relpp = (arelent **) xmalloc (relsize);
1291 for (i = 0; i < relcount; i++)
1292 if (is_specified_symbol
1293 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1294 keep_specific_list))
1295 temp_relpp [temp_relcount++] = relpp [i];
1296 relcount = temp_relcount;
1297 free (relpp);
1298 relpp = temp_relpp;
1299 }
1300 bfd_set_reloc (obfd, osection,
1301 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1302 }
1303
1304 isection->_cooked_size = isection->_raw_size;
1305 isection->reloc_done = true;
1306
1307 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1308 {
1309 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1310
1311 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1312 size))
1313 RETURN_NONFATAL (bfd_get_filename (ibfd));
1314
1315 if (copy_byte >= 0)
1316 filter_bytes (memhunk, &size);
1317
1318 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1319 size))
1320 RETURN_NONFATAL (bfd_get_filename (obfd));
1321
1322 free (memhunk);
1323 }
1324 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1325 {
1326 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1327
1328 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1329 flag--they can just remove the section entirely and add it
1330 back again. However, we do permit them to turn on the
1331 SEC_HAS_CONTENTS flag, and take it to mean that the section
1332 contents should be zeroed out. */
1333
1334 memset (memhunk, 0, size);
1335 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1336 size))
1337 RETURN_NONFATAL (bfd_get_filename (obfd));
1338 free (memhunk);
1339 }
1340}
1341
1342/* Get all the sections. This is used when --gap-fill or --pad-to is
1343 used. */
1344
1345static void
1346get_sections (obfd, osection, secppparg)
1347 bfd *obfd;
1348 asection *osection;
1349 PTR secppparg;
1350{
1351 asection ***secppp = (asection ***) secppparg;
1352
1353 **secppp = osection;
1354 ++(*secppp);
1355}
1356
1357/* Sort sections by VMA. This is called via qsort, and is used when
1358 --gap-fill or --pad-to is used. We force non loadable or empty
1359 sections to the front, where they are easier to ignore. */
1360
1361static int
1362compare_section_lma (arg1, arg2)
1363 const PTR arg1;
1364 const PTR arg2;
1365{
1366 const asection **sec1 = (const asection **) arg1;
1367 const asection **sec2 = (const asection **) arg2;
1368 flagword flags1, flags2;
1369
1370 /* Sort non loadable sections to the front. */
1371 flags1 = (*sec1)->flags;
1372 flags2 = (*sec2)->flags;
1373 if ((flags1 & SEC_HAS_CONTENTS) == 0
1374 || (flags1 & SEC_LOAD) == 0)
1375 {
1376 if ((flags2 & SEC_HAS_CONTENTS) != 0
1377 && (flags2 & SEC_LOAD) != 0)
1378 return -1;
1379 }
1380 else
1381 {
1382 if ((flags2 & SEC_HAS_CONTENTS) == 0
1383 || (flags2 & SEC_LOAD) == 0)
1384 return 1;
1385 }
1386
1387 /* Sort sections by LMA. */
1388 if ((*sec1)->lma > (*sec2)->lma)
1389 return 1;
1390 else if ((*sec1)->lma < (*sec2)->lma)
1391 return -1;
1392
1393 /* Sort sections with the same LMA by size. */
1394 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1395 return 1;
1396 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1397 return -1;
1398
1399 return 0;
1400}
1401
1402/* Mark all the symbols which will be used in output relocations with
1403 the BSF_KEEP flag so that those symbols will not be stripped.
1404
1405 Ignore relocations which will not appear in the output file. */
1406
1407static void
1408mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1409 bfd *ibfd;
1410 sec_ptr isection;
1411 PTR symbolsarg;
1412{
1413 asymbol **symbols = (asymbol **) symbolsarg;
1414 long relsize;
1415 arelent **relpp;
1416 long relcount, i;
1417
1418 /* Ignore an input section with no corresponding output section. */
1419 if (isection->output_section == NULL)
1420 return;
1421
1422 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1423 if (relsize < 0)
1424 bfd_fatal (bfd_get_filename (ibfd));
1425
1426 if (relsize == 0)
1427 return;
1428
1429 relpp = (arelent **) xmalloc (relsize);
1430 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1431 if (relcount < 0)
1432 bfd_fatal (bfd_get_filename (ibfd));
1433
1434 /* Examine each symbol used in a relocation. If it's not one of the
1435 special bfd section symbols, then mark it with BSF_KEEP. */
1436 for (i = 0; i < relcount; i++)
1437 {
1438 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1439 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1440 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1441 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1442 }
1443
1444 if (relpp != NULL)
1445 free (relpp);
1446}
1447
1448/* Write out debugging information. */
1449
1450static boolean
1451write_debugging_info (obfd, dhandle, symcountp, symppp)
1452 bfd *obfd;
1453 PTR dhandle;
1454 long *symcountp;
1455 asymbol ***symppp;
1456{
1457 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1458 return write_ieee_debugging_info (obfd, dhandle);
1459
1460 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1461 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1462 {
1463 bfd_byte *syms, *strings;
1464 bfd_size_type symsize, stringsize;
1465 asection *stabsec, *stabstrsec;
1466
1467 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1468 &symsize, &strings,
1469 &stringsize))
1470 return false;
1471
1472 stabsec = bfd_make_section (obfd, ".stab");
1473 stabstrsec = bfd_make_section (obfd, ".stabstr");
1474 if (stabsec == NULL
1475 || stabstrsec == NULL
1476 || ! bfd_set_section_size (obfd, stabsec, symsize)
1477 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1478 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1479 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1480 || ! bfd_set_section_flags (obfd, stabsec,
1481 (SEC_HAS_CONTENTS
1482 | SEC_READONLY
1483 | SEC_DEBUGGING))
1484 || ! bfd_set_section_flags (obfd, stabstrsec,
1485 (SEC_HAS_CONTENTS
1486 | SEC_READONLY
1487 | SEC_DEBUGGING)))
1488 {
1489 non_fatal (_("%s: can't create debugging section: %s"),
1490 bfd_get_filename (obfd),
1491 bfd_errmsg (bfd_get_error ()));
1492 return false;
1493 }
1494
1495 /* We can get away with setting the section contents now because
1496 the next thing the caller is going to do is copy over the
1497 real sections. We may someday have to split the contents
1498 setting out of this function. */
1499 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1500 symsize)
1501 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1502 (file_ptr) 0, stringsize))
1503 {
1504 non_fatal (_("%s: can't set debugging section contents: %s"),
1505 bfd_get_filename (obfd),
1506 bfd_errmsg (bfd_get_error ()));
1507 return false;
1508 }
1509
1510 return true;
1511 }
1512
1513 non_fatal (_("%s: don't know how to write debugging information for %s"),
1514 bfd_get_filename (obfd), bfd_get_target (obfd));
1515 return false;
1516}
1517
1518static int
1519strip_main (argc, argv)
1520 int argc;
1521 char *argv[];
1522{
1523 char *input_target = NULL, *output_target = NULL;
1524 boolean show_version = false;
1525 int c, i;
1526 struct section_list *p;
1527 char *output_file = NULL;
1528
1529 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv",
1530 strip_options, (int *) 0)) != EOF)
1531 {
1532 switch (c)
1533 {
1534 case 'I':
1535 input_target = optarg;
1536 break;
1537 case 'O':
1538 output_target = optarg;
1539 break;
1540 case 'F':
1541 input_target = output_target = optarg;
1542 break;
1543 case 'R':
1544 p = find_section_list (optarg, true);
1545 p->remove = true;
1546 sections_removed = true;
1547 break;
1548 case 's':
1549 strip_symbols = STRIP_ALL;
1550 break;
1551 case 'S':
1552 case 'g':
1553 strip_symbols = STRIP_DEBUG;
1554 break;
1555 case OPTION_STRIP_UNNEEDED:
1556 strip_symbols = STRIP_UNNEEDED;
1557 break;
1558 case 'K':
1559 add_specific_symbol (optarg, &keep_specific_list);
1560 break;
1561 case 'N':
1562 add_specific_symbol (optarg, &strip_specific_list);
1563 break;
1564 case 'o':
1565 output_file = optarg;
1566 break;
1567 case 'p':
1568 preserve_dates = true;
1569 break;
1570 case 'x':
1571 discard_locals = LOCALS_ALL;
1572 break;
1573 case 'X':
1574 discard_locals = LOCALS_START_L;
1575 break;
1576 case 'v':
1577 verbose = true;
1578 break;
1579 case 'V':
1580 show_version = true;
1581 break;
1582 case 0:
1583 break; /* we've been given a long option */
1584 case 'h':
1585 strip_usage (stdout, 0);
1586 default:
1587 strip_usage (stderr, 1);
1588 }
1589 }
1590
1591 if (show_version)
1592 print_version ("strip");
1593
1594 /* Default is to strip all symbols. */
1595 if (strip_symbols == STRIP_UNDEF
1596 && discard_locals == LOCALS_UNDEF
1597 && strip_specific_list == NULL)
1598 strip_symbols = STRIP_ALL;
1599
1600 if (output_target == (char *) NULL)
1601 output_target = input_target;
1602
1603 i = optind;
1604 if (i == argc
1605 || (output_file != NULL && (i + 1) < argc))
1606 strip_usage (stderr, 1);
1607
1608 for (; i < argc; i++)
1609 {
1610 int hold_status = status;
1611 struct stat statbuf;
1612 char *tmpname;
1613
1614 if (preserve_dates)
1615 {
1616 if (stat (argv[i], &statbuf) < 0)
1617 {
1618 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
1619 continue;
1620 }
1621 }
1622
1623 if (output_file != NULL)
1624 tmpname = output_file;
1625 else
1626 tmpname = make_tempname (argv[i]);
1627 status = 0;
1628
1629 copy_file (argv[i], tmpname, input_target, output_target);
1630 if (status == 0)
1631 {
1632 if (preserve_dates)
1633 set_times (tmpname, &statbuf);
1634 if (output_file == NULL)
1635 smart_rename (tmpname, argv[i], preserve_dates);
1636 status = hold_status;
1637 }
1638 else
1639 unlink (tmpname);
1640 if (output_file == NULL)
1641 free (tmpname);
1642 }
1643
1644 return 0;
1645}
1646
1647static int
1648copy_main (argc, argv)
1649 int argc;
1650 char *argv[];
1651{
1652 char *input_filename = NULL, *output_filename = NULL;
1653 char *input_target = NULL, *output_target = NULL;
1654 boolean show_version = false;
1655 boolean change_warn = true;
1656 int c;
1657 struct section_list *p;
1658 struct stat statbuf;
1659
1660 while ((c = getopt_long (argc, argv, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1661 copy_options, (int *) 0)) != EOF)
1662 {
1663 switch (c)
1664 {
1665 case 'b':
1666 copy_byte = atoi (optarg);
1667 if (copy_byte < 0)
1668 fatal (_("byte number must be non-negative"));
1669 break;
1670 case 'i':
1671 interleave = atoi (optarg);
1672 if (interleave < 1)
1673 fatal (_("interleave must be positive"));
1674 break;
1675 case 'I':
1676 case 's': /* "source" - 'I' is preferred */
1677 input_target = optarg;
1678 break;
1679 case 'O':
1680 case 'd': /* "destination" - 'O' is preferred */
1681 output_target = optarg;
1682 break;
1683 case 'F':
1684 input_target = output_target = optarg;
1685 break;
1686 case 'R':
1687 p = find_section_list (optarg, true);
1688 p->remove = true;
1689 sections_removed = true;
1690 break;
1691 case 'S':
1692 strip_symbols = STRIP_ALL;
1693 break;
1694 case 'g':
1695 strip_symbols = STRIP_DEBUG;
1696 break;
1697 case OPTION_STRIP_UNNEEDED:
1698 strip_symbols = STRIP_UNNEEDED;
1699 break;
1700 case 'K':
1701 add_specific_symbol (optarg, &keep_specific_list);
1702 break;
1703 case 'N':
1704 add_specific_symbol (optarg, &strip_specific_list);
1705 break;
1706 case 'L':
1707 add_specific_symbol (optarg, &localize_specific_list);
1708 break;
1709 case 'W':
1710 add_specific_symbol (optarg, &weaken_specific_list);
1711 break;
1712 case 'p':
1713 preserve_dates = true;
1714 break;
1715 case 'x':
1716 discard_locals = LOCALS_ALL;
1717 break;
1718 case 'X':
1719 discard_locals = LOCALS_START_L;
1720 break;
1721 case 'v':
1722 verbose = true;
1723 break;
1724 case 'V':
1725 show_version = true;
1726 break;
1727 case OPTION_WEAKEN:
1728 weaken = true;
1729 break;
1730 case OPTION_ADD_SECTION:
1731 {
1732 const char *s;
1733 struct stat st;
1734 struct section_add *pa;
1735 int len;
1736 char *name;
1737 FILE *f;
1738
1739 s = strchr (optarg, '=');
1740
1741 if (s == NULL)
1742 fatal (_("bad format for --add-section NAME=FILENAME"));
1743
1744 if (stat (s + 1, & st) < 0)
1745 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
1746
1747 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1748
1749 len = s - optarg;
1750 name = (char *) xmalloc (len + 1);
1751 strncpy (name, optarg, len);
1752 name[len] = '\0';
1753 pa->name = name;
1754
1755 pa->filename = s + 1;
1756
1757 pa->size = st.st_size;
1758
1759 pa->contents = (bfd_byte *) xmalloc (pa->size);
1760 f = fopen (pa->filename, FOPEN_RB);
1761
1762 if (f == NULL)
1763 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
1764
1765 if (fread (pa->contents, 1, pa->size, f) == 0
1766 || ferror (f))
1767 fatal (_("%s: fread failed"), pa->filename);
1768
1769 fclose (f);
1770
1771 pa->next = add_sections;
1772 add_sections = pa;
1773 }
1774 break;
1775 case OPTION_CHANGE_START:
1776 change_start = parse_vma (optarg, "--change-start");
1777 break;
1778 case OPTION_CHANGE_SECTION_ADDRESS:
1779 case OPTION_CHANGE_SECTION_LMA:
1780 case OPTION_CHANGE_SECTION_VMA:
1781 {
1782 const char *s;
1783 int len;
1784 char *name;
1785 char *option;
1786 bfd_vma val;
1787 enum change_action what;
1788
1789 switch (c)
1790 {
1791 case OPTION_CHANGE_SECTION_ADDRESS: option = "--change-section-address"; break;
1792 case OPTION_CHANGE_SECTION_LMA: option = "--change-section-lma"; break;
1793 case OPTION_CHANGE_SECTION_VMA: option = "--change-section-vma"; break;
1794 }
1795
1796 s = strchr (optarg, '=');
1797 if (s == NULL)
1798 {
1799 s = strchr (optarg, '+');
1800 if (s == NULL)
1801 {
1802 s = strchr (optarg, '-');
1803 if (s == NULL)
1804 fatal (_("bad format for %s"), option);
1805 }
1806 }
1807
1808 len = s - optarg;
1809 name = (char *) xmalloc (len + 1);
1810 strncpy (name, optarg, len);
1811 name[len] = '\0';
1812
1813 p = find_section_list (name, true);
1814
1815 val = parse_vma (s + 1, option);
1816
1817 switch (*s)
1818 {
1819 case '=': what = CHANGE_SET; break;
1820 case '-': val = - val; /* Drop through. */
1821 case '+': what = CHANGE_MODIFY; break;
1822 }
1823
1824 switch (c)
1825 {
1826 case OPTION_CHANGE_SECTION_ADDRESS:
1827 p->change_vma = what;
1828 p->vma_val = val;
1829 /* Drop through. */
1830
1831 case OPTION_CHANGE_SECTION_LMA:
1832 p->change_lma = what;
1833 p->lma_val = val;
1834 break;
1835
1836 case OPTION_CHANGE_SECTION_VMA:
1837 p->change_vma = what;
1838 p->vma_val = val;
1839 break;
1840 }
1841 }
1842 break;
1843 case OPTION_CHANGE_ADDRESSES:
1844 change_section_address = parse_vma (optarg, "--change-addresses");
1845 change_start = change_section_address;
1846 break;
1847 case OPTION_CHANGE_WARNINGS:
1848 change_warn = true;
1849 break;
1850 case OPTION_CHANGE_LEADING_CHAR:
1851 change_leading_char = true;
1852 break;
1853 case OPTION_DEBUGGING:
1854 convert_debugging = true;
1855 break;
1856 case OPTION_GAP_FILL:
1857 {
1858 bfd_vma gap_fill_vma;
1859
1860 gap_fill_vma = parse_vma (optarg, "--gap-fill");
1861 gap_fill = (bfd_byte) gap_fill_vma;
1862 if ((bfd_vma) gap_fill != gap_fill_vma)
1863 {
1864 char buff[20];
1865
1866 sprintf_vma (buff, gap_fill_vma);
1867
1868 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
1869 buff, gap_fill);
1870 }
1871 gap_fill_set = true;
1872 }
1873 break;
1874 case OPTION_NO_CHANGE_WARNINGS:
1875 change_warn = false;
1876 break;
1877 case OPTION_PAD_TO:
1878 pad_to = parse_vma (optarg, "--pad-to");
1879 pad_to_set = true;
1880 break;
1881 case OPTION_REMOVE_LEADING_CHAR:
1882 remove_leading_char = true;
1883 break;
1884 case OPTION_SET_SECTION_FLAGS:
1885 {
1886 const char *s;
1887 int len;
1888 char *name;
1889
1890 s = strchr (optarg, '=');
1891 if (s == NULL)
1892 fatal (_("bad format for --set-section-flags"));
1893
1894 len = s - optarg;
1895 name = (char *) xmalloc (len + 1);
1896 strncpy (name, optarg, len);
1897 name[len] = '\0';
1898
1899 p = find_section_list (name, true);
1900
1901 p->set_flags = true;
1902 p->flags = parse_flags (s + 1);
1903 }
1904 break;
1905 case OPTION_SET_START:
1906 set_start = parse_vma (optarg, "--set-start");
1907 set_start_set = true;
1908 break;
1909 case 0:
1910 break; /* we've been given a long option */
1911 case 'h':
1912 copy_usage (stdout, 0);
1913 default:
1914 copy_usage (stderr, 1);
1915 }
1916 }
1917
1918 if (show_version)
1919 print_version ("objcopy");
1920
1921 if (copy_byte >= interleave)
1922 fatal (_("byte number must be less than interleave"));
1923
1924 if (optind == argc || optind + 2 < argc)
1925 copy_usage (stderr, 1);
1926
1927 input_filename = argv[optind];
1928 if (optind + 1 < argc)
1929 output_filename = argv[optind + 1];
1930
1931 /* Default is to strip no symbols. */
1932 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
1933 strip_symbols = STRIP_NONE;
1934
1935 if (output_target == (char *) NULL)
1936 output_target = input_target;
1937
1938 if (preserve_dates)
1939 {
1940 if (stat (input_filename, &statbuf) < 0)
1941 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
1942 }
1943
1944 /* If there is no destination file then create a temp and rename
1945 the result into the input. */
1946
1947 if (output_filename == (char *) NULL)
1948 {
1949 char *tmpname = make_tempname (input_filename);
1950
1951 copy_file (input_filename, tmpname, input_target, output_target);
1952 if (status == 0)
1953 {
1954 if (preserve_dates)
1955 set_times (tmpname, &statbuf);
1956 smart_rename (tmpname, input_filename, preserve_dates);
1957 }
1958 else
1959 unlink (tmpname);
1960 }
1961 else
1962 {
1963 copy_file (input_filename, output_filename, input_target, output_target);
1964 if (status == 0 && preserve_dates)
1965 set_times (output_filename, &statbuf);
1966 }
1967
1968 if (change_warn)
1969 {
1970 for (p = change_sections; p != NULL; p = p->next)
1971 {
1972 if (! p->used)
1973 {
1974 if (p->change_vma != CHANGE_IGNORE)
1975 {
1976 char buff [20];
1977
1978 sprintf_vma (buff, p->vma_val);
1979
1980 /* xgettext:c-format */
1981 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
1982 p->name,
1983 p->change_vma == CHANGE_SET ? '=' : '+',
1984 buff);
1985 }
1986
1987 if (p->change_lma != CHANGE_IGNORE)
1988 {
1989 char buff [20];
1990
1991 sprintf_vma (buff, p->lma_val);
1992
1993 /* xgettext:c-format */
1994 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
1995 p->name,
1996 p->change_lma == CHANGE_SET ? '=' : '+',
1997 buff);
1998 }
1999 }
2000 }
2001 }
2002
2003 return 0;
2004}
2005
2006int
2007main (argc, argv)
2008 int argc;
2009 char *argv[];
2010{
2011#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2012 setlocale (LC_MESSAGES, "");
2013#endif
2014 bindtextdomain (PACKAGE, LOCALEDIR);
2015 textdomain (PACKAGE);
2016
2017 program_name = argv[0];
2018 xmalloc_set_program_name (program_name);
2019
2020 START_PROGRESS (program_name, 0);
2021
2022 strip_symbols = STRIP_UNDEF;
2023 discard_locals = LOCALS_UNDEF;
2024
2025 bfd_init ();
2026 set_default_bfd_target ();
2027
2028 if (is_strip < 0)
2029 {
2030 int i = strlen (program_name);
2031 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
2032 }
2033
2034 if (is_strip)
2035 strip_main (argc, argv);
2036 else
2037 copy_main (argc, argv);
2038
2039 END_PROGRESS (program_name);
2040
2041 return status;
2042}
This page took 0.098129 seconds and 4 git commands to generate.