1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "libiberty.h"
26 static bfd_vma parse_vma
PARAMS ((const char *, const char *));
27 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
28 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
29 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
30 static int compare_section_vma
PARAMS ((const PTR
, const PTR
));
31 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
33 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
35 static asymbol
**isympp
= NULL
; /* Input symbols */
36 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
38 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
39 static int copy_byte
= -1;
40 static int interleave
= 4;
42 static boolean verbose
; /* Print file and target names. */
43 static int status
= 0; /* Exit status. */
48 strip_none
, /* don't strip */
49 strip_debug
, /* strip all debugger symbols */
50 strip_all
/* strip all symbols */
53 /* Which symbols to remove. */
54 static enum strip_action strip_symbols
;
59 locals_start_L
, /* discard locals starting with L */
60 locals_all
/* discard all locals */
63 /* Which local symbols to remove. Overrides strip_all. */
64 static enum locals_action discard_locals
;
66 /* Structure used to hold lists of sections and actions to take. */
70 /* Next section to adjust. */
71 struct section_list
*next
;
74 /* Whether this entry was used. */
76 /* Remaining fields only used if not on remove_sections list. */
77 /* Whether to adjust or set VMA. */
79 /* Amount to adjust by or set to. */
83 /* List of sections to remove. */
85 static struct section_list
*remove_sections
;
87 /* Adjustments to the start address. */
88 static bfd_vma adjust_start
= 0;
89 static boolean set_start_set
= false;
90 static bfd_vma set_start
;
92 /* Adjustments to section VMA's. */
93 static bfd_vma adjust_section_vma
= 0;
94 static struct section_list
*adjust_sections
;
96 /* Filling gaps between sections. */
97 static boolean gap_fill_set
= false;
98 static bfd_byte gap_fill
;
100 /* Options to handle if running as "strip". */
102 static struct option strip_options
[] =
104 {"discard-all", no_argument
, 0, 'x'},
105 {"discard-locals", no_argument
, 0, 'X'},
106 {"format", required_argument
, 0, 'F'}, /* Obsolete */
107 {"help", no_argument
, 0, 'h'},
108 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
109 {"input-target", required_argument
, 0, 'I'},
110 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
111 {"output-target", required_argument
, 0, 'O'},
112 {"remove-section", required_argument
, 0, 'R'},
113 {"strip-all", no_argument
, 0, 's'},
114 {"strip-debug", no_argument
, 0, 'S'},
115 {"target", required_argument
, 0, 'F'},
116 {"verbose", no_argument
, 0, 'v'},
117 {"version", no_argument
, 0, 'V'},
118 {0, no_argument
, 0, 0}
121 /* Options to handle if running as "objcopy". */
123 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
125 #define OPTION_ADJUST_START 150
126 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
127 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
128 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
129 #define OPTION_GAP_FILL (OPTION_ADJUST_WARNINGS + 1)
130 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
131 #define OPTION_SET_START (OPTION_NO_ADJUST_WARNINGS + 1)
133 static struct option copy_options
[] =
135 {"adjust-start", required_argument
, 0, OPTION_ADJUST_START
},
136 {"adjust-vma", required_argument
, 0, OPTION_ADJUST_VMA
},
137 {"adjust-section-vma", required_argument
, 0, OPTION_ADJUST_SECTION_VMA
},
138 {"adjust-warnings", no_argument
, 0, OPTION_ADJUST_WARNINGS
},
139 {"byte", required_argument
, 0, 'b'},
140 {"discard-all", no_argument
, 0, 'x'},
141 {"discard-locals", no_argument
, 0, 'X'},
142 {"format", required_argument
, 0, 'F'}, /* Obsolete */
143 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
144 {"help", no_argument
, 0, 'h'},
145 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
146 {"input-target", required_argument
, 0, 'I'},
147 {"interleave", required_argument
, 0, 'i'},
148 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_ADJUST_WARNINGS
},
149 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
150 {"output-target", required_argument
, 0, 'O'},
151 {"remove-section", required_argument
, 0, 'R'},
152 {"set-start", required_argument
, 0, OPTION_SET_START
},
153 {"strip-all", no_argument
, 0, 'S'},
154 {"strip-debug", no_argument
, 0, 'g'},
155 {"target", required_argument
, 0, 'F'},
156 {"verbose", no_argument
, 0, 'v'},
157 {"version", no_argument
, 0, 'V'},
158 {0, no_argument
, 0, 0}
162 extern char *program_name
;
163 extern char *program_version
;
165 /* This flag distinguishes between strip and objcopy:
166 1 means this is 'strip'; 0 means this is 'objcopy'.
167 -1 means if we should use argv[0] to decide. */
172 copy_usage (stream
, status
)
177 Usage: %s [-vVSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
178 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
179 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
180 [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
181 [--remove-section=section] [--gap-fill=val] [--set-start=val]\n\
182 [--adjust-start=incr] [--adjust-vma=incr]\n\
183 [--adjust-section-vma=section{=,+,-}val] [--adjust-warnings]\n\
184 [--no-adjust-warnings] [--verbose] [--version] [--help]\n\
185 in-file [out-file]\n",
191 strip_usage (stream
, status
)
196 Usage: %s [-vVsSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
197 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
198 [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
199 [--remove-section=section] [--verbose] [--version] [--help] file...\n",
204 /* Parse a string into a VMA, with a fatal error if it can't be
215 ret
= bfd_scan_vma (s
, &end
, 0);
218 fprintf (stderr
, "%s: %s: bad number: %s\n", program_name
, arg
, s
);
224 /* Return the name of a temporary file in the same directory as FILENAME. */
227 make_tempname (filename
)
230 static char template[] = "stXXXXXX";
232 char *slash
= strrchr (filename
, '/');
234 if (slash
!= (char *) NULL
)
237 tmpname
= xmalloc (strlen (filename
) + sizeof (template) + 1);
238 strcpy (tmpname
, filename
);
239 strcat (tmpname
, "/");
240 strcat (tmpname
, template);
246 tmpname
= xmalloc (sizeof (template));
247 strcpy (tmpname
, template);
253 /* Choose which symbol entries to copy; put the result in OSYMS.
254 We don't copy in place, because that confuses the relocs.
255 Return the number of symbols to print. */
258 filter_symbols (abfd
, osyms
, isyms
, symcount
)
260 asymbol
**osyms
, **isyms
;
263 register asymbol
**from
= isyms
, **to
= osyms
;
264 long src_count
= 0, dst_count
= 0;
266 for (; src_count
< symcount
; src_count
++)
268 asymbol
*sym
= from
[src_count
];
269 flagword flags
= sym
->flags
;
272 if ((flags
& BSF_GLOBAL
) /* Keep if external. */
273 || (flags
& BSF_KEEP
) /* Keep if used in a relocation. */
274 || bfd_is_und_section (bfd_get_section (sym
))
275 || bfd_is_com_section (bfd_get_section (sym
)))
277 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
278 keep
= strip_symbols
!= strip_debug
;
279 else /* Local symbol. */
280 keep
= discard_locals
!= locals_all
281 && (discard_locals
!= locals_start_L
||
282 ! bfd_is_local_label (abfd
, sym
));
284 to
[dst_count
++] = sym
;
290 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
294 filter_bytes (memhunk
, size
)
298 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
300 for (; from
< end
; from
+= interleave
)
305 /* Copy object file IBFD onto OBFD. */
308 copy_object (ibfd
, obfd
)
314 asection
**osections
= NULL
;
315 bfd_size_type
*gaps
= NULL
;
316 bfd_size_type max_gap
= 0;
318 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
320 nonfatal (bfd_get_filename (obfd
));
324 printf ("copy from %s(%s) to %s(%s)\n",
325 bfd_get_filename(ibfd
), bfd_get_target(ibfd
),
326 bfd_get_filename(obfd
), bfd_get_target(obfd
));
331 start
= bfd_get_start_address (ibfd
);
332 start
+= adjust_start
;
334 if (!bfd_set_start_address (obfd
, start
)
335 || !bfd_set_file_flags (obfd
,
336 (bfd_get_file_flags (ibfd
)
337 & bfd_applicable_file_flags (obfd
))))
339 nonfatal (bfd_get_filename (ibfd
));
342 /* Copy architecture of input file to output file */
343 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
344 bfd_get_mach (ibfd
)))
346 fprintf (stderr
, "Output file cannot represent architecture %s\n",
347 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
348 bfd_get_mach (ibfd
)));
350 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
352 nonfatal (bfd_get_filename(ibfd
));
357 if (osympp
!= isympp
)
360 /* bfd mandates that all output sections be created and sizes set before
361 any output is done. Thus, we traverse all sections multiple times. */
362 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
369 /* We must fill in gaps between the sections. We do this by
370 grabbing a list of the sections, sorting them by VMA, and
371 adding new sections to occupy any gaps. */
373 c
= bfd_count_sections (obfd
);
374 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
376 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
378 qsort (osections
, c
, sizeof (asection
*), compare_section_vma
);
380 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
381 for (i
= 0; i
< c
- 1; i
++)
385 bfd_vma gap_start
, gap_stop
;
387 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
388 if ((flags
& SEC_HAS_CONTENTS
) == 0
389 || (flags
& SEC_LOAD
) == 0)
392 size
= bfd_section_size (obfd
, osections
[i
]);
393 gap_start
= bfd_section_vma (obfd
, osections
[i
]) + size
;
394 gap_stop
= bfd_section_vma (obfd
, osections
[i
+ 1]);
395 if (gap_start
>= gap_stop
)
399 if (! bfd_set_section_size (obfd
, osections
[i
],
400 size
+ (gap_stop
- gap_start
)))
402 fprintf (stderr
, "%s: Can't fill gap after %s: %s\n",
404 bfd_get_section_name (obfd
, osections
[i
]),
405 bfd_errmsg (bfd_get_error()));
409 gaps
[i
] = gap_stop
- gap_start
;
410 if (max_gap
< gap_stop
- gap_start
)
411 max_gap
= gap_stop
- gap_start
;
416 /* Symbol filtering must happen after the output sections have
417 been created, but before their contents are set. */
418 if (strip_symbols
== strip_all
&& discard_locals
== locals_undef
)
420 osympp
= isympp
= NULL
;
427 symsize
= bfd_get_symtab_upper_bound (ibfd
);
430 nonfatal (bfd_get_filename (ibfd
));
433 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
434 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
437 nonfatal (bfd_get_filename (ibfd
));
440 if (strip_symbols
== strip_debug
|| discard_locals
!= locals_undef
)
442 /* Mark symbols used in output relocations so that they
443 are kept, even if they are local labels or static symbols.
445 Note we iterate over the input sections examining their
446 relocations since the relocations for the output sections
447 haven't been set yet. mark_symbols_used_in_relocations will
448 ignore input sections which have no corresponding output
450 bfd_map_over_sections (ibfd
,
451 mark_symbols_used_in_relocations
,
453 osympp
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
454 symcount
= filter_symbols (ibfd
, osympp
, isympp
, symcount
);
458 bfd_set_symtab (obfd
, osympp
, symcount
);
460 /* This has to happen after the symbol table has been set. */
461 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
468 /* Fill in the gaps. */
472 buf
= (bfd_byte
*) xmalloc (max_gap
);
473 memset (buf
, gap_fill
, max_gap
);
475 c
= bfd_count_sections (obfd
);
476 for (i
= 0; i
< c
- 1; i
++)
484 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
493 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
496 nonfatal (bfd_get_filename (obfd
));
506 /* Allow the BFD backend to copy any private data it understands
507 from the input BFD to the output BFD. This is done last to
508 permit the routine to look at the filtered symbol table, which is
509 important for the ECOFF code at least. */
510 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
512 fprintf (stderr
, "%s: %s: error copying private BFD data: %s\n",
513 program_name
, bfd_get_filename (obfd
),
514 bfd_errmsg (bfd_get_error ()));
526 size_t size
= strlen (a
) + strlen (b
) + strlen (c
);
527 char *r
= xmalloc (size
+ 1);
535 /* Read each archive element in turn from IBFD, copy the
536 contents to temp file, and keep the temp file handle. */
539 copy_archive (ibfd
, obfd
, output_target
)
544 bfd
**ptr
= &obfd
->archive_head
;
546 char *dir
= make_tempname (bfd_get_filename (obfd
));
548 /* Make a temp directory to hold the contents. */
550 obfd
->has_armap
= ibfd
->has_armap
;
552 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
553 ibfd
->archive_head
= this_element
;
554 while (this_element
!= (bfd
*) NULL
)
556 /* Create an output file for this member. */
557 char *output_name
= cat (dir
, "/", bfd_get_filename(this_element
));
558 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
560 if (output_bfd
== (bfd
*) NULL
)
562 nonfatal (output_name
);
564 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
566 nonfatal (bfd_get_filename (obfd
));
569 if (bfd_check_format (this_element
, bfd_object
) == true)
571 copy_object (this_element
, output_bfd
);
574 bfd_close (output_bfd
);
575 /* Open the newly output file and attatch to our list. */
576 output_bfd
= bfd_openr (output_name
, output_target
);
578 /* Mark it for deletion. */
580 ptr
= &output_bfd
->next
;
581 this_element
->next
= bfd_openr_next_archived_file (ibfd
, this_element
);
582 this_element
= this_element
->next
;
586 if (!bfd_close (obfd
))
588 nonfatal (bfd_get_filename (obfd
));
591 /* Delete all the files that we opened.
592 Construct their names again, unfortunately, but
593 we're about to exit anyway. */
594 for (this_element
= ibfd
->archive_head
;
595 this_element
!= (bfd
*) NULL
;
596 this_element
= this_element
->next
)
598 unlink (cat (dir
, "/", bfd_get_filename (this_element
)));
601 if (!bfd_close (ibfd
))
603 nonfatal (bfd_get_filename (ibfd
));
607 /* The top-level control. */
610 copy_file (input_filename
, output_filename
, input_target
, output_target
)
611 char *input_filename
;
612 char *output_filename
;
619 /* To allow us to do "strip *" without dying on the first
620 non-object file, failures are nonfatal. */
622 ibfd
= bfd_openr (input_filename
, input_target
);
625 nonfatal (input_filename
);
628 if (bfd_check_format (ibfd
, bfd_archive
))
632 /* bfd_get_target does not return the correct value until
633 bfd_check_format succeeds. */
634 if (output_target
== NULL
)
635 output_target
= bfd_get_target (ibfd
);
637 obfd
= bfd_openw (output_filename
, output_target
);
640 nonfatal (output_filename
);
642 copy_archive (ibfd
, obfd
, output_target
);
644 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
648 /* bfd_get_target does not return the correct value until
649 bfd_check_format succeeds. */
650 if (output_target
== NULL
)
651 output_target
= bfd_get_target (ibfd
);
653 obfd
= bfd_openw (output_filename
, output_target
);
656 nonfatal (output_filename
);
659 copy_object (ibfd
, obfd
);
661 if (!bfd_close (obfd
))
663 nonfatal (output_filename
);
666 if (!bfd_close (ibfd
))
668 nonfatal (input_filename
);
673 bfd_nonfatal (input_filename
);
674 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
676 list_matching_formats (matching
);
683 /* Create a section in OBFD with the same name and attributes
684 as ISECTION in IBFD. */
687 setup_section (ibfd
, isection
, obfdarg
)
692 bfd
*obfd
= (bfd
*) obfdarg
;
693 struct section_list
*p
;
698 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
699 && (strip_symbols
== strip_debug
700 || strip_symbols
== strip_all
701 || discard_locals
== locals_all
))
704 for (p
= remove_sections
; p
!= NULL
; p
= p
->next
)
706 if (strcmp (p
->name
, bfd_section_name (ibfd
, isection
)) == 0)
713 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
714 if (osection
== NULL
)
720 if (!bfd_set_section_size (obfd
,
722 bfd_section_size (ibfd
, isection
)))
728 vma
= bfd_section_vma (ibfd
, isection
);
729 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
731 if (strcmp (p
->name
, bfd_section_name (ibfd
, isection
)) == 0)
742 vma
+= adjust_section_vma
;
744 if (! bfd_set_section_vma (obfd
, osection
, vma
))
750 if (bfd_set_section_alignment (obfd
,
752 bfd_section_alignment (ibfd
, isection
))
759 if (!bfd_set_section_flags (obfd
, osection
,
760 bfd_get_section_flags (ibfd
, isection
)))
766 /* This used to be mangle_section; we do here to avoid using
767 bfd_get_section_by_name since some formats allow multiple
768 sections with the same name. */
769 isection
->output_section
= osection
;
770 isection
->output_offset
= 0;
772 /* Allow the BFD backend to copy any private data it understands
773 from the input section to the output section. */
774 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
776 err
= "private data";
784 fprintf (stderr
, "%s: %s: section `%s': error in %s: %s\n",
786 bfd_get_filename (ibfd
), bfd_section_name (ibfd
, isection
),
787 err
, bfd_errmsg (bfd_get_error ()));
791 /* Copy the data of input section ISECTION of IBFD
792 to an output section with the same name in OBFD.
793 If stripping then don't copy any relocation info. */
796 copy_section (ibfd
, isection
, obfdarg
)
801 bfd
*obfd
= (bfd
*) obfdarg
;
802 struct section_list
*p
;
808 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
809 && (strip_symbols
== strip_debug
810 || strip_symbols
== strip_all
811 || discard_locals
== locals_all
))
816 for (p
= remove_sections
; p
!= NULL
; p
= p
->next
)
817 if (strcmp (p
->name
, bfd_section_name (ibfd
, isection
)) == 0)
820 osection
= isection
->output_section
;
821 size
= bfd_get_section_size_before_reloc (isection
);
823 if (size
== 0 || osection
== 0)
826 if (strip_symbols
== strip_all
)
827 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
832 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
835 nonfatal (bfd_get_filename (ibfd
));
838 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
841 relpp
= (arelent
**) xmalloc (relsize
);
842 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
845 nonfatal (bfd_get_filename (ibfd
));
847 bfd_set_reloc (obfd
, osection
, relpp
, relcount
);
851 isection
->_cooked_size
= isection
->_raw_size
;
852 isection
->reloc_done
= true;
854 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
856 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
858 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
861 nonfatal (bfd_get_filename (ibfd
));
866 filter_bytes (memhunk
, &size
);
867 /* The section has gotten smaller. */
868 if (!bfd_set_section_size (obfd
, osection
, size
))
869 nonfatal (bfd_get_filename (obfd
));
872 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
875 nonfatal (bfd_get_filename (obfd
));
881 /* Get all the sections. This is used when --gap-fill is used. */
884 get_sections (obfd
, osection
, secppparg
)
889 asection
***secppp
= (asection
***) secppparg
;
895 /* Sort sections by VMA. This is called via qsort, and is used when
896 --gap-fill is used. */
899 compare_section_vma (arg1
, arg2
)
903 const asection
**sec1
= (const asection
**) arg1
;
904 const asection
**sec2
= (const asection
**) arg2
;
906 if ((*sec1
)->vma
> (*sec2
)->vma
)
908 else if ((*sec1
)->vma
< (*sec2
)->vma
)
914 /* Mark all the symbols which will be used in output relocations with
915 the BSF_KEEP flag so that those symbols will not be stripped.
917 Ignore relocations which will not appear in the output file. */
920 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
925 asymbol
**symbols
= (asymbol
**) symbolsarg
;
930 /* Ignore an input section with no corresponding output section. */
931 if (isection
->output_section
== NULL
)
934 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
936 bfd_fatal (bfd_get_filename (ibfd
));
938 relpp
= (arelent
**) xmalloc (relsize
);
939 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
941 bfd_fatal (bfd_get_filename (ibfd
));
943 /* Examine each symbol used in a relocation. If it's not one of the
944 special bfd section symbols, then mark it with BSF_KEEP. */
945 for (i
= 0; i
< relcount
; i
++)
947 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
948 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
949 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
950 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
957 /* The number of bytes to copy at once. */
958 #define COPY_BUF 8192
960 /* Copy file FROM to file TO, performing no translations.
961 Return 0 if ok, -1 if error. */
964 simple_copy (from
, to
)
967 int fromfd
, tofd
, nread
;
970 fromfd
= open (from
, O_RDONLY
);
973 tofd
= open (to
, O_WRONLY
| O_CREAT
| O_TRUNC
);
979 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
981 if (write (tofd
, buf
, nread
) != nread
)
997 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1004 /* Rename FROM to TO, copying if TO is a link.
1005 Assumes that TO already exists, because FROM is a temp file.
1006 Return 0 if ok, -1 if error. */
1009 smart_rename (from
, to
)
1018 /* Use rename only if TO is not a symbolic link and has
1019 only one hard link. */
1020 if (!S_ISLNK (s
.st_mode
) && s
.st_nlink
== 1)
1022 ret
= rename (from
, to
);
1025 /* Try to preserve the permission bits and ownership of TO. */
1026 chmod (to
, s
.st_mode
& 07777);
1027 chown (to
, s
.st_uid
, s
.st_gid
);
1032 ret
= simple_copy (from
, to
);
1040 strip_main (argc
, argv
)
1044 char *input_target
= NULL
, *output_target
= NULL
;
1045 boolean show_version
= false;
1048 while ((c
= getopt_long (argc
, argv
, "I:O:F:R:sSgxXVv",
1049 strip_options
, (int *) 0)) != EOF
)
1054 input_target
= optarg
;
1057 output_target
= optarg
;
1060 input_target
= output_target
= optarg
;
1064 struct section_list
*n
;
1066 n
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
1069 n
->next
= remove_sections
;
1070 remove_sections
= n
;
1074 strip_symbols
= strip_all
;
1078 strip_symbols
= strip_debug
;
1081 discard_locals
= locals_all
;
1084 discard_locals
= locals_start_L
;
1090 show_version
= true;
1093 break; /* we've been given a long option */
1095 strip_usage (stdout
, 0);
1097 strip_usage (stderr
, 1);
1103 printf ("GNU %s version %s\n", program_name
, program_version
);
1107 /* Default is to strip all symbols. */
1108 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
1109 strip_symbols
= strip_all
;
1111 if (output_target
== (char *) NULL
)
1112 output_target
= input_target
;
1116 strip_usage (stderr
, 1);
1118 for (; i
< argc
; i
++)
1120 int hold_status
= status
;
1122 char *tmpname
= make_tempname (argv
[i
]);
1124 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1127 smart_rename (tmpname
, argv
[i
]);
1128 status
= hold_status
;
1139 copy_main (argc
, argv
)
1143 char *input_filename
= NULL
, *output_filename
= NULL
;
1144 char *input_target
= NULL
, *output_target
= NULL
;
1145 boolean show_version
= false;
1146 boolean adjust_warn
= true;
1148 struct section_list
*p
;
1150 while ((c
= getopt_long (argc
, argv
, "b:i:I:s:O:d:F:R:SgxXVv",
1151 copy_options
, (int *) 0)) != EOF
)
1156 copy_byte
= atoi(optarg
);
1159 fprintf (stderr
, "%s: byte number must be non-negative\n",
1165 interleave
= atoi(optarg
);
1168 fprintf(stderr
, "%s: interleave must be positive\n",
1174 case 's': /* "source" - 'I' is preferred */
1175 input_target
= optarg
;
1178 case 'd': /* "destination" - 'O' is preferred */
1179 output_target
= optarg
;
1182 input_target
= output_target
= optarg
;
1185 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
1188 p
->next
= remove_sections
;
1189 remove_sections
= p
;
1192 strip_symbols
= strip_all
;
1195 strip_symbols
= strip_debug
;
1198 discard_locals
= locals_all
;
1201 discard_locals
= locals_start_L
;
1207 show_version
= true;
1209 case OPTION_ADJUST_START
:
1210 adjust_start
= parse_vma (optarg
, "--adjust-start");
1212 case OPTION_ADJUST_SECTION_VMA
:
1218 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
1219 s
= strchr (optarg
, '=');
1223 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1227 s
= strchr (optarg
, '+');
1230 s
= strchr (optarg
, '-');
1234 "%s: bad format for --adjust-section-vma\n",
1240 p
->val
= parse_vma (s
+ 1, "--adjust-section-vma");
1246 name
= (char *) xmalloc (len
+ 1);
1247 strncpy (name
, optarg
, len
);
1253 p
->next
= adjust_sections
;
1254 adjust_sections
= p
;
1257 case OPTION_ADJUST_VMA
:
1258 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
1259 adjust_start
= adjust_section_vma
;
1261 case OPTION_ADJUST_WARNINGS
:
1264 case OPTION_GAP_FILL
:
1266 bfd_vma gap_fill_vma
;
1268 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
1269 gap_fill
= (bfd_byte
) gap_fill_vma
;
1270 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
1272 fprintf (stderr
, "%s: warning: truncating gap-fill from 0x",
1274 fprintf_vma (stderr
, gap_fill_vma
);
1275 fprintf (stderr
, "to 0x%x\n", (unsigned int) gap_fill
);
1277 gap_fill_set
= true;
1280 case OPTION_NO_ADJUST_WARNINGS
:
1281 adjust_warn
= false;
1283 case OPTION_SET_START
:
1284 set_start
= parse_vma (optarg
, "--set-start");
1285 set_start_set
= true;
1288 break; /* we've been given a long option */
1290 copy_usage (stdout
, 0);
1292 copy_usage (stderr
, 1);
1298 printf ("GNU %s version %s\n", program_name
, program_version
);
1302 if (copy_byte
>= interleave
)
1304 fprintf (stderr
, "%s: byte number must be less than interleave\n",
1309 if (optind
== argc
|| optind
+ 2 < argc
)
1310 copy_usage (stderr
, 1);
1312 input_filename
= argv
[optind
];
1313 if (optind
+ 1 < argc
)
1314 output_filename
= argv
[optind
+ 1];
1316 /* Default is to strip no symbols. */
1317 if (strip_symbols
== strip_undef
&& discard_locals
== locals_undef
)
1318 strip_symbols
= strip_none
;
1320 if (output_target
== (char *) NULL
)
1321 output_target
= input_target
;
1323 /* If there is no destination file then create a temp and rename
1324 the result into the input. */
1326 if (output_filename
== (char *) NULL
)
1328 char *tmpname
= make_tempname (input_filename
);
1329 copy_file (input_filename
, tmpname
, input_target
, output_target
);
1331 smart_rename (tmpname
, input_filename
);
1337 copy_file (input_filename
, output_filename
, input_target
, output_target
);
1342 for (p
= adjust_sections
; p
!= NULL
; p
= p
->next
)
1346 fprintf (stderr
, "%s: warning: --adjust-section-vma %s%c0x",
1347 program_name
, p
->name
,
1348 p
->adjust
? '=' : '+');
1349 fprintf_vma (stderr
, p
->val
);
1350 fprintf (stderr
, " never used\n");
1355 /* We could issue similar warnings for remove_sections, but I don't
1356 think that would be as useful. */
1366 program_name
= argv
[0];
1367 xmalloc_set_program_name (program_name
);
1368 strip_symbols
= strip_undef
;
1369 discard_locals
= locals_undef
;
1375 int i
= strlen (program_name
);
1376 is_strip
= (i
>= 5 && strcmp (program_name
+ i
- 5, "strip"));
1380 strip_main (argc
, argv
);
1382 copy_main (argc
, argv
);