1*56bb7041Schristos /* objcopy.c -- copy object file from input to output, optionally massaging it.
2*56bb7041Schristos    Copyright (C) 1991-2020 Free Software Foundation, Inc.
3*56bb7041Schristos 
4*56bb7041Schristos    This file is part of GNU Binutils.
5*56bb7041Schristos 
6*56bb7041Schristos    This program is free software; you can redistribute it and/or modify
7*56bb7041Schristos    it under the terms of the GNU General Public License as published by
8*56bb7041Schristos    the Free Software Foundation; either version 3 of the License, or
9*56bb7041Schristos    (at your option) any later version.
10*56bb7041Schristos 
11*56bb7041Schristos    This program is distributed in the hope that it will be useful,
12*56bb7041Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*56bb7041Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*56bb7041Schristos    GNU General Public License for more details.
15*56bb7041Schristos 
16*56bb7041Schristos    You should have received a copy of the GNU General Public License
17*56bb7041Schristos    along with this program; if not, write to the Free Software
18*56bb7041Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19*56bb7041Schristos    02110-1301, USA.  */
20*56bb7041Schristos 
21*56bb7041Schristos #include "sysdep.h"
22*56bb7041Schristos #include "bfd.h"
23*56bb7041Schristos #include "progress.h"
24*56bb7041Schristos #include "getopt.h"
25*56bb7041Schristos #include "libiberty.h"
26*56bb7041Schristos #include "bucomm.h"
27*56bb7041Schristos #include "budbg.h"
28*56bb7041Schristos #include "filenames.h"
29*56bb7041Schristos #include "fnmatch.h"
30*56bb7041Schristos #include "elf-bfd.h"
31*56bb7041Schristos #include "coff/internal.h"
32*56bb7041Schristos #include "libcoff.h"
33*56bb7041Schristos #include "safe-ctype.h"
34*56bb7041Schristos 
35*56bb7041Schristos /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
36*56bb7041Schristos    header in generic PE code.  */
37*56bb7041Schristos #include "coff/i386.h"
38*56bb7041Schristos #include "coff/pe.h"
39*56bb7041Schristos 
40*56bb7041Schristos static bfd_vma pe_file_alignment = (bfd_vma) -1;
41*56bb7041Schristos static bfd_vma pe_heap_commit = (bfd_vma) -1;
42*56bb7041Schristos static bfd_vma pe_heap_reserve = (bfd_vma) -1;
43*56bb7041Schristos static bfd_vma pe_image_base = (bfd_vma) -1;
44*56bb7041Schristos static bfd_vma pe_section_alignment = (bfd_vma) -1;
45*56bb7041Schristos static bfd_vma pe_stack_commit = (bfd_vma) -1;
46*56bb7041Schristos static bfd_vma pe_stack_reserve = (bfd_vma) -1;
47*56bb7041Schristos static short pe_subsystem = -1;
48*56bb7041Schristos static short pe_major_subsystem_version = -1;
49*56bb7041Schristos static short pe_minor_subsystem_version = -1;
50*56bb7041Schristos 
51*56bb7041Schristos struct is_specified_symbol_predicate_data
52*56bb7041Schristos {
53*56bb7041Schristos   const char *  name;
54*56bb7041Schristos   bfd_boolean	found;
55*56bb7041Schristos };
56*56bb7041Schristos 
57*56bb7041Schristos /* A node includes symbol name mapping to support redefine_sym.  */
58*56bb7041Schristos struct redefine_node
59*56bb7041Schristos {
60*56bb7041Schristos   char *source;
61*56bb7041Schristos   char *target;
62*56bb7041Schristos };
63*56bb7041Schristos 
64*56bb7041Schristos struct addsym_node
65*56bb7041Schristos {
66*56bb7041Schristos   struct addsym_node *next;
67*56bb7041Schristos   char *    symdef;
68*56bb7041Schristos   long      symval;
69*56bb7041Schristos   flagword  flags;
70*56bb7041Schristos   char *    section;
71*56bb7041Schristos   const char *  othersym;
72*56bb7041Schristos };
73*56bb7041Schristos 
74*56bb7041Schristos typedef struct section_rename
75*56bb7041Schristos {
76*56bb7041Schristos   const char *            old_name;
77*56bb7041Schristos   const char *            new_name;
78*56bb7041Schristos   flagword                flags;
79*56bb7041Schristos   struct section_rename * next;
80*56bb7041Schristos }
81*56bb7041Schristos section_rename;
82*56bb7041Schristos 
83*56bb7041Schristos /* List of sections to be renamed.  */
84*56bb7041Schristos static section_rename *section_rename_list;
85*56bb7041Schristos 
86*56bb7041Schristos static asymbol **isympp = NULL;	/* Input symbols.  */
87*56bb7041Schristos static asymbol **osympp = NULL;	/* Output symbols that survive stripping.  */
88*56bb7041Schristos 
89*56bb7041Schristos /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes.  */
90*56bb7041Schristos static int copy_byte = -1;
91*56bb7041Schristos static int interleave = 0; /* Initialised to 4 in copy_main().  */
92*56bb7041Schristos static int copy_width = 1;
93*56bb7041Schristos 
94*56bb7041Schristos static bfd_boolean verbose;		/* Print file and target names.  */
95*56bb7041Schristos static bfd_boolean preserve_dates;	/* Preserve input file timestamp.  */
96*56bb7041Schristos static int deterministic = -1;		/* Enable deterministic archives.  */
97*56bb7041Schristos static int status = 0;			/* Exit status.  */
98*56bb7041Schristos 
99*56bb7041Schristos static bfd_boolean    merge_notes = FALSE;	/* Merge note sections.  */
100*56bb7041Schristos 
101*56bb7041Schristos typedef struct merged_note_section
102*56bb7041Schristos {
103*56bb7041Schristos   asection *                    sec;	 /* The section that is being merged.  */
104*56bb7041Schristos   bfd_byte *                    contents;/* New contents of the section.  */
105*56bb7041Schristos   bfd_size_type                 size;	 /* New size of the section.  */
106*56bb7041Schristos   struct merged_note_section *  next;  	 /* Link to next merged note section.  */
107*56bb7041Schristos } merged_note_section;
108*56bb7041Schristos 
109*56bb7041Schristos enum strip_action
110*56bb7041Schristos {
111*56bb7041Schristos   STRIP_UNDEF,
112*56bb7041Schristos   STRIP_NONE,		/* Don't strip.  */
113*56bb7041Schristos   STRIP_DEBUG,		/* Strip all debugger symbols.  */
114*56bb7041Schristos   STRIP_UNNEEDED,	/* Strip unnecessary symbols.  */
115*56bb7041Schristos   STRIP_NONDEBUG,	/* Strip everything but debug info.  */
116*56bb7041Schristos   STRIP_DWO,		/* Strip all DWO info.  */
117*56bb7041Schristos   STRIP_NONDWO,		/* Strip everything but DWO info.  */
118*56bb7041Schristos   STRIP_ALL		/* Strip all symbols.  */
119*56bb7041Schristos };
120*56bb7041Schristos 
121*56bb7041Schristos /* Which symbols to remove.  */
122*56bb7041Schristos static enum strip_action strip_symbols = STRIP_UNDEF;
123*56bb7041Schristos 
124*56bb7041Schristos enum locals_action
125*56bb7041Schristos {
126*56bb7041Schristos   LOCALS_UNDEF,
127*56bb7041Schristos   LOCALS_START_L,	/* Discard locals starting with L.  */
128*56bb7041Schristos   LOCALS_ALL		/* Discard all locals.  */
129*56bb7041Schristos };
130*56bb7041Schristos 
131*56bb7041Schristos /* Which local symbols to remove.  Overrides STRIP_ALL.  */
132*56bb7041Schristos static enum locals_action discard_locals;
133*56bb7041Schristos 
134*56bb7041Schristos /* Structure used to hold lists of sections and actions to take.  */
135*56bb7041Schristos struct section_list
136*56bb7041Schristos {
137*56bb7041Schristos   struct section_list * next;	   /* Next section to change.  */
138*56bb7041Schristos   const char *		pattern;   /* Section name pattern.  */
139*56bb7041Schristos   bfd_boolean		used;	   /* Whether this entry was used.  */
140*56bb7041Schristos 
141*56bb7041Schristos   unsigned int          context;   /* What to do with matching sections.  */
142*56bb7041Schristos   /* Flag bits used in the context field.
143*56bb7041Schristos      COPY and REMOVE are mutually exlusive.  SET and ALTER are mutually exclusive.  */
144*56bb7041Schristos #define SECTION_CONTEXT_REMOVE    (1 << 0) /* Remove this section.  */
145*56bb7041Schristos #define SECTION_CONTEXT_COPY      (1 << 1) /* Copy this section, delete all non-copied section.  */
146*56bb7041Schristos #define SECTION_CONTEXT_KEEP      (1 << 2) /* Keep this section.  */
147*56bb7041Schristos #define SECTION_CONTEXT_SET_VMA   (1 << 3) /* Set the sections' VMA address.  */
148*56bb7041Schristos #define SECTION_CONTEXT_ALTER_VMA (1 << 4) /* Increment or decrement the section's VMA address.  */
149*56bb7041Schristos #define SECTION_CONTEXT_SET_LMA   (1 << 5) /* Set the sections' LMA address.  */
150*56bb7041Schristos #define SECTION_CONTEXT_ALTER_LMA (1 << 6) /* Increment or decrement the section's LMA address.  */
151*56bb7041Schristos #define SECTION_CONTEXT_SET_FLAGS (1 << 7) /* Set the section's flags.  */
152*56bb7041Schristos #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 8) /* Remove relocations for this section.  */
153*56bb7041Schristos #define SECTION_CONTEXT_SET_ALIGNMENT (1 << 9) /* Set alignment for section.  */
154*56bb7041Schristos 
155*56bb7041Schristos   bfd_vma		vma_val;   /* Amount to change by or set to.  */
156*56bb7041Schristos   bfd_vma		lma_val;   /* Amount to change by or set to.  */
157*56bb7041Schristos   flagword		flags;	   /* What to set the section flags to.	 */
158*56bb7041Schristos   unsigned int	        alignment; /* Alignment of output section.  */
159*56bb7041Schristos };
160*56bb7041Schristos 
161*56bb7041Schristos static struct section_list *change_sections;
162*56bb7041Schristos 
163*56bb7041Schristos /* TRUE if some sections are to be removed.  */
164*56bb7041Schristos static bfd_boolean sections_removed;
165*56bb7041Schristos 
166*56bb7041Schristos /* TRUE if only some sections are to be copied.  */
167*56bb7041Schristos static bfd_boolean sections_copied;
168*56bb7041Schristos 
169*56bb7041Schristos /* Changes to the start address.  */
170*56bb7041Schristos static bfd_vma change_start = 0;
171*56bb7041Schristos static bfd_boolean set_start_set = FALSE;
172*56bb7041Schristos static bfd_vma set_start;
173*56bb7041Schristos 
174*56bb7041Schristos /* Changes to section addresses.  */
175*56bb7041Schristos static bfd_vma change_section_address = 0;
176*56bb7041Schristos 
177*56bb7041Schristos /* Filling gaps between sections.  */
178*56bb7041Schristos static bfd_boolean gap_fill_set = FALSE;
179*56bb7041Schristos static bfd_byte gap_fill = 0;
180*56bb7041Schristos 
181*56bb7041Schristos /* Pad to a given address.  */
182*56bb7041Schristos static bfd_boolean pad_to_set = FALSE;
183*56bb7041Schristos static bfd_vma pad_to;
184*56bb7041Schristos 
185*56bb7041Schristos /* Use alternative machine code?  */
186*56bb7041Schristos static unsigned long use_alt_mach_code = 0;
187*56bb7041Schristos 
188*56bb7041Schristos /* Output BFD flags user wants to set or clear */
189*56bb7041Schristos static flagword bfd_flags_to_set;
190*56bb7041Schristos static flagword bfd_flags_to_clear;
191*56bb7041Schristos 
192*56bb7041Schristos /* List of sections to add.  */
193*56bb7041Schristos struct section_add
194*56bb7041Schristos {
195*56bb7041Schristos   /* Next section to add.  */
196*56bb7041Schristos   struct section_add *next;
197*56bb7041Schristos   /* Name of section to add.  */
198*56bb7041Schristos   const char *name;
199*56bb7041Schristos   /* Name of file holding section contents.  */
200*56bb7041Schristos   const char *filename;
201*56bb7041Schristos   /* Size of file.  */
202*56bb7041Schristos   size_t size;
203*56bb7041Schristos   /* Contents of file.  */
204*56bb7041Schristos   bfd_byte *contents;
205*56bb7041Schristos   /* BFD section, after it has been added.  */
206*56bb7041Schristos   asection *section;
207*56bb7041Schristos };
208*56bb7041Schristos 
209*56bb7041Schristos /* List of sections to add to the output BFD.  */
210*56bb7041Schristos static struct section_add *add_sections;
211*56bb7041Schristos 
212*56bb7041Schristos /* List of sections to update in the output BFD.  */
213*56bb7041Schristos static struct section_add *update_sections;
214*56bb7041Schristos 
215*56bb7041Schristos /* List of sections to dump from the output BFD.  */
216*56bb7041Schristos static struct section_add *dump_sections;
217*56bb7041Schristos 
218*56bb7041Schristos /* If non-NULL the argument to --add-gnu-debuglink.
219*56bb7041Schristos    This should be the filename to store in the .gnu_debuglink section.  */
220*56bb7041Schristos static const char * gnu_debuglink_filename = NULL;
221*56bb7041Schristos 
222*56bb7041Schristos /* Whether to convert debugging information.  */
223*56bb7041Schristos static bfd_boolean convert_debugging = FALSE;
224*56bb7041Schristos 
225*56bb7041Schristos /* Whether to compress/decompress DWARF debug sections.  */
226*56bb7041Schristos static enum
227*56bb7041Schristos {
228*56bb7041Schristos   nothing = 0,
229*56bb7041Schristos   compress = 1 << 0,
230*56bb7041Schristos   compress_zlib = compress | 1 << 1,
231*56bb7041Schristos   compress_gnu_zlib = compress | 1 << 2,
232*56bb7041Schristos   compress_gabi_zlib = compress | 1 << 3,
233*56bb7041Schristos   decompress = 1 << 4
234*56bb7041Schristos } do_debug_sections = nothing;
235*56bb7041Schristos 
236*56bb7041Schristos /* Whether to generate ELF common symbols with the STT_COMMON type.  */
237*56bb7041Schristos static enum bfd_link_elf_stt_common do_elf_stt_common = unchanged;
238*56bb7041Schristos 
239*56bb7041Schristos /* Whether to change the leading character in symbol names.  */
240*56bb7041Schristos static bfd_boolean change_leading_char = FALSE;
241*56bb7041Schristos 
242*56bb7041Schristos /* Whether to remove the leading character from global symbol names.  */
243*56bb7041Schristos static bfd_boolean remove_leading_char = FALSE;
244*56bb7041Schristos 
245*56bb7041Schristos /* Whether to permit wildcard in symbol comparison.  */
246*56bb7041Schristos static bfd_boolean wildcard = FALSE;
247*56bb7041Schristos 
248*56bb7041Schristos /* True if --localize-hidden is in effect.  */
249*56bb7041Schristos static bfd_boolean localize_hidden = FALSE;
250*56bb7041Schristos 
251*56bb7041Schristos /* List of symbols to strip, keep, localize, keep-global, weaken,
252*56bb7041Schristos    or redefine.  */
253*56bb7041Schristos static htab_t strip_specific_htab = NULL;
254*56bb7041Schristos static htab_t strip_unneeded_htab = NULL;
255*56bb7041Schristos static htab_t keep_specific_htab = NULL;
256*56bb7041Schristos static htab_t localize_specific_htab = NULL;
257*56bb7041Schristos static htab_t globalize_specific_htab = NULL;
258*56bb7041Schristos static htab_t keepglobal_specific_htab = NULL;
259*56bb7041Schristos static htab_t weaken_specific_htab = NULL;
260*56bb7041Schristos static htab_t redefine_specific_htab = NULL;
261*56bb7041Schristos static htab_t redefine_specific_reverse_htab = NULL;
262*56bb7041Schristos static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
263*56bb7041Schristos static int add_symbols = 0;
264*56bb7041Schristos 
265*56bb7041Schristos static char *strip_specific_buffer = NULL;
266*56bb7041Schristos static char *strip_unneeded_buffer = NULL;
267*56bb7041Schristos static char *keep_specific_buffer = NULL;
268*56bb7041Schristos static char *localize_specific_buffer = NULL;
269*56bb7041Schristos static char *globalize_specific_buffer = NULL;
270*56bb7041Schristos static char *keepglobal_specific_buffer = NULL;
271*56bb7041Schristos static char *weaken_specific_buffer = NULL;
272*56bb7041Schristos 
273*56bb7041Schristos /* If this is TRUE, we weaken global symbols (set BSF_WEAK).  */
274*56bb7041Schristos static bfd_boolean weaken = FALSE;
275*56bb7041Schristos 
276*56bb7041Schristos /* If this is TRUE, we retain BSF_FILE symbols.  */
277*56bb7041Schristos static bfd_boolean keep_file_symbols = FALSE;
278*56bb7041Schristos 
279*56bb7041Schristos /* Prefix symbols/sections.  */
280*56bb7041Schristos static char *prefix_symbols_string = 0;
281*56bb7041Schristos static char *prefix_sections_string = 0;
282*56bb7041Schristos static char *prefix_alloc_sections_string = 0;
283*56bb7041Schristos 
284*56bb7041Schristos /* True if --extract-symbol was passed on the command line.  */
285*56bb7041Schristos static bfd_boolean extract_symbol = FALSE;
286*56bb7041Schristos 
287*56bb7041Schristos /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
288*56bb7041Schristos    of <reverse_bytes> bytes within each output section.  */
289*56bb7041Schristos static int reverse_bytes = 0;
290*56bb7041Schristos 
291*56bb7041Schristos /* For Coff objects, we may want to allow or disallow long section names,
292*56bb7041Schristos    or preserve them where found in the inputs.  Debug info relies on them.  */
293*56bb7041Schristos enum long_section_name_handling
294*56bb7041Schristos {
295*56bb7041Schristos   DISABLE,
296*56bb7041Schristos   ENABLE,
297*56bb7041Schristos   KEEP
298*56bb7041Schristos };
299*56bb7041Schristos 
300*56bb7041Schristos /* The default long section handling mode is to preserve them.
301*56bb7041Schristos    This is also the only behaviour for 'strip'.  */
302*56bb7041Schristos static enum long_section_name_handling long_section_names = KEEP;
303*56bb7041Schristos 
304*56bb7041Schristos /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
305*56bb7041Schristos enum command_line_switch
306*56bb7041Schristos {
307*56bb7041Schristos   OPTION_ADD_SECTION=150,
308*56bb7041Schristos   OPTION_ADD_GNU_DEBUGLINK,
309*56bb7041Schristos   OPTION_ADD_SYMBOL,
310*56bb7041Schristos   OPTION_ALT_MACH_CODE,
311*56bb7041Schristos   OPTION_CHANGE_ADDRESSES,
312*56bb7041Schristos   OPTION_CHANGE_LEADING_CHAR,
313*56bb7041Schristos   OPTION_CHANGE_SECTION_ADDRESS,
314*56bb7041Schristos   OPTION_CHANGE_SECTION_LMA,
315*56bb7041Schristos   OPTION_CHANGE_SECTION_VMA,
316*56bb7041Schristos   OPTION_CHANGE_START,
317*56bb7041Schristos   OPTION_CHANGE_WARNINGS,
318*56bb7041Schristos   OPTION_COMPRESS_DEBUG_SECTIONS,
319*56bb7041Schristos   OPTION_DEBUGGING,
320*56bb7041Schristos   OPTION_DECOMPRESS_DEBUG_SECTIONS,
321*56bb7041Schristos   OPTION_DUMP_SECTION,
322*56bb7041Schristos   OPTION_ELF_STT_COMMON,
323*56bb7041Schristos   OPTION_EXTRACT_DWO,
324*56bb7041Schristos   OPTION_EXTRACT_SYMBOL,
325*56bb7041Schristos   OPTION_FILE_ALIGNMENT,
326*56bb7041Schristos   OPTION_FORMATS_INFO,
327*56bb7041Schristos   OPTION_GAP_FILL,
328*56bb7041Schristos   OPTION_GLOBALIZE_SYMBOL,
329*56bb7041Schristos   OPTION_GLOBALIZE_SYMBOLS,
330*56bb7041Schristos   OPTION_HEAP,
331*56bb7041Schristos   OPTION_IMAGE_BASE,
332*56bb7041Schristos   OPTION_IMPURE,
333*56bb7041Schristos   OPTION_INTERLEAVE_WIDTH,
334*56bb7041Schristos   OPTION_KEEPGLOBAL_SYMBOLS,
335*56bb7041Schristos   OPTION_KEEP_FILE_SYMBOLS,
336*56bb7041Schristos   OPTION_KEEP_SECTION,
337*56bb7041Schristos   OPTION_KEEP_SYMBOLS,
338*56bb7041Schristos   OPTION_LOCALIZE_HIDDEN,
339*56bb7041Schristos   OPTION_LOCALIZE_SYMBOLS,
340*56bb7041Schristos   OPTION_LONG_SECTION_NAMES,
341*56bb7041Schristos   OPTION_MERGE_NOTES,
342*56bb7041Schristos   OPTION_NO_MERGE_NOTES,
343*56bb7041Schristos   OPTION_NO_CHANGE_WARNINGS,
344*56bb7041Schristos   OPTION_ONLY_KEEP_DEBUG,
345*56bb7041Schristos   OPTION_PAD_TO,
346*56bb7041Schristos   OPTION_PREFIX_ALLOC_SECTIONS,
347*56bb7041Schristos   OPTION_PREFIX_SECTIONS,
348*56bb7041Schristos   OPTION_PREFIX_SYMBOLS,
349*56bb7041Schristos   OPTION_PURE,
350*56bb7041Schristos   OPTION_READONLY_TEXT,
351*56bb7041Schristos   OPTION_REDEFINE_SYM,
352*56bb7041Schristos   OPTION_REDEFINE_SYMS,
353*56bb7041Schristos   OPTION_REMOVE_LEADING_CHAR,
354*56bb7041Schristos   OPTION_REMOVE_RELOCS,
355*56bb7041Schristos   OPTION_RENAME_SECTION,
356*56bb7041Schristos   OPTION_REVERSE_BYTES,
357*56bb7041Schristos   OPTION_PE_SECTION_ALIGNMENT,
358*56bb7041Schristos   OPTION_SET_SECTION_FLAGS,
359*56bb7041Schristos   OPTION_SET_SECTION_ALIGNMENT,
360*56bb7041Schristos   OPTION_SET_START,
361*56bb7041Schristos   OPTION_SREC_FORCES3,
362*56bb7041Schristos   OPTION_SREC_LEN,
363*56bb7041Schristos   OPTION_STACK,
364*56bb7041Schristos   OPTION_STRIP_DWO,
365*56bb7041Schristos   OPTION_STRIP_SYMBOLS,
366*56bb7041Schristos   OPTION_STRIP_UNNEEDED,
367*56bb7041Schristos   OPTION_STRIP_UNNEEDED_SYMBOL,
368*56bb7041Schristos   OPTION_STRIP_UNNEEDED_SYMBOLS,
369*56bb7041Schristos   OPTION_SUBSYSTEM,
370*56bb7041Schristos   OPTION_UPDATE_SECTION,
371*56bb7041Schristos   OPTION_VERILOG_DATA_WIDTH,
372*56bb7041Schristos   OPTION_WEAKEN,
373*56bb7041Schristos   OPTION_WEAKEN_SYMBOLS,
374*56bb7041Schristos   OPTION_WRITABLE_TEXT
375*56bb7041Schristos };
376*56bb7041Schristos 
377*56bb7041Schristos /* Options to handle if running as "strip".  */
378*56bb7041Schristos 
379*56bb7041Schristos static struct option strip_options[] =
380*56bb7041Schristos {
381*56bb7041Schristos   {"disable-deterministic-archives", no_argument, 0, 'U'},
382*56bb7041Schristos   {"discard-all", no_argument, 0, 'x'},
383*56bb7041Schristos   {"discard-locals", no_argument, 0, 'X'},
384*56bb7041Schristos   {"enable-deterministic-archives", no_argument, 0, 'D'},
385*56bb7041Schristos   {"format", required_argument, 0, 'F'}, /* Obsolete */
386*56bb7041Schristos   {"help", no_argument, 0, 'h'},
387*56bb7041Schristos   {"info", no_argument, 0, OPTION_FORMATS_INFO},
388*56bb7041Schristos   {"input-format", required_argument, 0, 'I'}, /* Obsolete */
389*56bb7041Schristos   {"input-target", required_argument, 0, 'I'},
390*56bb7041Schristos   {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
391*56bb7041Schristos   {"keep-section", required_argument, 0, OPTION_KEEP_SECTION},
392*56bb7041Schristos   {"keep-symbol", required_argument, 0, 'K'},
393*56bb7041Schristos   {"merge-notes", no_argument, 0, 'M'},
394*56bb7041Schristos   {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
395*56bb7041Schristos   {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
396*56bb7041Schristos   {"output-file", required_argument, 0, 'o'},
397*56bb7041Schristos   {"output-format", required_argument, 0, 'O'},	/* Obsolete */
398*56bb7041Schristos   {"output-target", required_argument, 0, 'O'},
399*56bb7041Schristos   {"preserve-dates", no_argument, 0, 'p'},
400*56bb7041Schristos   {"remove-section", required_argument, 0, 'R'},
401*56bb7041Schristos   {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
402*56bb7041Schristos   {"strip-all", no_argument, 0, 's'},
403*56bb7041Schristos   {"strip-debug", no_argument, 0, 'S'},
404*56bb7041Schristos   {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
405*56bb7041Schristos   {"strip-symbol", required_argument, 0, 'N'},
406*56bb7041Schristos   {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
407*56bb7041Schristos   {"target", required_argument, 0, 'F'},
408*56bb7041Schristos   {"verbose", no_argument, 0, 'v'},
409*56bb7041Schristos   {"version", no_argument, 0, 'V'},
410*56bb7041Schristos   {"wildcard", no_argument, 0, 'w'},
411*56bb7041Schristos   {0, no_argument, 0, 0}
412*56bb7041Schristos };
413*56bb7041Schristos 
414*56bb7041Schristos /* Options to handle if running as "objcopy".  */
415*56bb7041Schristos 
416*56bb7041Schristos static struct option copy_options[] =
417*56bb7041Schristos {
418*56bb7041Schristos   {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
419*56bb7041Schristos   {"add-section", required_argument, 0, OPTION_ADD_SECTION},
420*56bb7041Schristos   {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
421*56bb7041Schristos   {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
422*56bb7041Schristos   {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
423*56bb7041Schristos   {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
424*56bb7041Schristos   {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
425*56bb7041Schristos   {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
426*56bb7041Schristos   {"binary-architecture", required_argument, 0, 'B'},
427*56bb7041Schristos   {"byte", required_argument, 0, 'b'},
428*56bb7041Schristos   {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
429*56bb7041Schristos   {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
430*56bb7041Schristos   {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
431*56bb7041Schristos   {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
432*56bb7041Schristos   {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
433*56bb7041Schristos   {"change-start", required_argument, 0, OPTION_CHANGE_START},
434*56bb7041Schristos   {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
435*56bb7041Schristos   {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
436*56bb7041Schristos   {"debugging", no_argument, 0, OPTION_DEBUGGING},
437*56bb7041Schristos   {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
438*56bb7041Schristos   {"disable-deterministic-archives", no_argument, 0, 'U'},
439*56bb7041Schristos   {"discard-all", no_argument, 0, 'x'},
440*56bb7041Schristos   {"discard-locals", no_argument, 0, 'X'},
441*56bb7041Schristos   {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
442*56bb7041Schristos   {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
443*56bb7041Schristos   {"enable-deterministic-archives", no_argument, 0, 'D'},
444*56bb7041Schristos   {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
445*56bb7041Schristos   {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
446*56bb7041Schristos   {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
447*56bb7041Schristos   {"format", required_argument, 0, 'F'}, /* Obsolete */
448*56bb7041Schristos   {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
449*56bb7041Schristos   {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
450*56bb7041Schristos   {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
451*56bb7041Schristos   {"heap", required_argument, 0, OPTION_HEAP},
452*56bb7041Schristos   {"help", no_argument, 0, 'h'},
453*56bb7041Schristos   {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
454*56bb7041Schristos   {"impure", no_argument, 0, OPTION_IMPURE},
455*56bb7041Schristos   {"info", no_argument, 0, OPTION_FORMATS_INFO},
456*56bb7041Schristos   {"input-format", required_argument, 0, 'I'}, /* Obsolete */
457*56bb7041Schristos   {"input-target", required_argument, 0, 'I'},
458*56bb7041Schristos   {"interleave", optional_argument, 0, 'i'},
459*56bb7041Schristos   {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
460*56bb7041Schristos   {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
461*56bb7041Schristos   {"keep-global-symbol", required_argument, 0, 'G'},
462*56bb7041Schristos   {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
463*56bb7041Schristos   {"keep-section", required_argument, 0, OPTION_KEEP_SECTION},
464*56bb7041Schristos   {"keep-symbol", required_argument, 0, 'K'},
465*56bb7041Schristos   {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
466*56bb7041Schristos   {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
467*56bb7041Schristos   {"localize-symbol", required_argument, 0, 'L'},
468*56bb7041Schristos   {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
469*56bb7041Schristos   {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
470*56bb7041Schristos   {"merge-notes", no_argument, 0, 'M'},
471*56bb7041Schristos   {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
472*56bb7041Schristos   {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
473*56bb7041Schristos   {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
474*56bb7041Schristos   {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
475*56bb7041Schristos   {"only-section", required_argument, 0, 'j'},
476*56bb7041Schristos   {"output-format", required_argument, 0, 'O'},	/* Obsolete */
477*56bb7041Schristos   {"output-target", required_argument, 0, 'O'},
478*56bb7041Schristos   {"pad-to", required_argument, 0, OPTION_PAD_TO},
479*56bb7041Schristos   {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
480*56bb7041Schristos   {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
481*56bb7041Schristos   {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
482*56bb7041Schristos   {"preserve-dates", no_argument, 0, 'p'},
483*56bb7041Schristos   {"pure", no_argument, 0, OPTION_PURE},
484*56bb7041Schristos   {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
485*56bb7041Schristos   {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
486*56bb7041Schristos   {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
487*56bb7041Schristos   {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
488*56bb7041Schristos   {"remove-section", required_argument, 0, 'R'},
489*56bb7041Schristos   {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
490*56bb7041Schristos   {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
491*56bb7041Schristos   {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
492*56bb7041Schristos   {"section-alignment", required_argument, 0, OPTION_PE_SECTION_ALIGNMENT},
493*56bb7041Schristos   {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
494*56bb7041Schristos   {"set-section-alignment", required_argument, 0, OPTION_SET_SECTION_ALIGNMENT},
495*56bb7041Schristos   {"set-start", required_argument, 0, OPTION_SET_START},
496*56bb7041Schristos   {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
497*56bb7041Schristos   {"srec-len", required_argument, 0, OPTION_SREC_LEN},
498*56bb7041Schristos   {"stack", required_argument, 0, OPTION_STACK},
499*56bb7041Schristos   {"strip-all", no_argument, 0, 'S'},
500*56bb7041Schristos   {"strip-debug", no_argument, 0, 'g'},
501*56bb7041Schristos   {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
502*56bb7041Schristos   {"strip-symbol", required_argument, 0, 'N'},
503*56bb7041Schristos   {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
504*56bb7041Schristos   {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
505*56bb7041Schristos   {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
506*56bb7041Schristos   {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
507*56bb7041Schristos   {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
508*56bb7041Schristos   {"target", required_argument, 0, 'F'},
509*56bb7041Schristos   {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
510*56bb7041Schristos   {"verbose", no_argument, 0, 'v'},
511*56bb7041Schristos   {"verilog-data-width", required_argument, 0, OPTION_VERILOG_DATA_WIDTH},
512*56bb7041Schristos   {"version", no_argument, 0, 'V'},
513*56bb7041Schristos   {"weaken", no_argument, 0, OPTION_WEAKEN},
514*56bb7041Schristos   {"weaken-symbol", required_argument, 0, 'W'},
515*56bb7041Schristos   {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
516*56bb7041Schristos   {"wildcard", no_argument, 0, 'w'},
517*56bb7041Schristos   {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
518*56bb7041Schristos   {0, no_argument, 0, 0}
519*56bb7041Schristos };
520*56bb7041Schristos 
521*56bb7041Schristos /* IMPORTS */
522*56bb7041Schristos extern char *program_name;
523*56bb7041Schristos 
524*56bb7041Schristos /* This flag distinguishes between strip and objcopy:
525*56bb7041Schristos    1 means this is 'strip'; 0 means this is 'objcopy'.
526*56bb7041Schristos    -1 means if we should use argv[0] to decide.  */
527*56bb7041Schristos extern int is_strip;
528*56bb7041Schristos 
529*56bb7041Schristos /* The maximum length of an S record.  This variable is defined in srec.c
530*56bb7041Schristos    and can be modified by the --srec-len parameter.  */
531*56bb7041Schristos extern unsigned int _bfd_srec_len;
532*56bb7041Schristos 
533*56bb7041Schristos /* Restrict the generation of Srecords to type S3 only.
534*56bb7041Schristos    This variable is defined in bfd/srec.c and can be toggled
535*56bb7041Schristos    on by the --srec-forceS3 command line switch.  */
536*56bb7041Schristos extern bfd_boolean _bfd_srec_forceS3;
537*56bb7041Schristos 
538*56bb7041Schristos /* Width of data in bytes for verilog output.
539*56bb7041Schristos    This variable is declared in bfd/verilog.c and can be modified by
540*56bb7041Schristos    the --verilog-data-width parameter.  */
541*56bb7041Schristos extern unsigned int VerilogDataWidth;
542*56bb7041Schristos 
543*56bb7041Schristos /* Forward declarations.  */
544*56bb7041Schristos static void setup_section (bfd *, asection *, void *);
545*56bb7041Schristos static void setup_bfd_headers (bfd *, bfd *);
546*56bb7041Schristos static void copy_relocations_in_section (bfd *, asection *, void *);
547*56bb7041Schristos static void copy_section (bfd *, asection *, void *);
548*56bb7041Schristos static void get_sections (bfd *, asection *, void *);
549*56bb7041Schristos static int compare_section_lma (const void *, const void *);
550*56bb7041Schristos static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
551*56bb7041Schristos static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
552*56bb7041Schristos static const char *lookup_sym_redefinition (const char *);
553*56bb7041Schristos static const char *find_section_rename (const char *, flagword *);
554*56bb7041Schristos 
555*56bb7041Schristos ATTRIBUTE_NORETURN static void
copy_usage(FILE * stream,int exit_status)556*56bb7041Schristos copy_usage (FILE *stream, int exit_status)
557*56bb7041Schristos {
558*56bb7041Schristos   fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
559*56bb7041Schristos   fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
560*56bb7041Schristos   fprintf (stream, _(" The options are:\n"));
561*56bb7041Schristos   fprintf (stream, _("\
562*56bb7041Schristos   -I --input-target <bfdname>      Assume input file is in format <bfdname>\n\
563*56bb7041Schristos   -O --output-target <bfdname>     Create an output file in format <bfdname>\n\
564*56bb7041Schristos   -B --binary-architecture <arch>  Set output arch, when input is arch-less\n\
565*56bb7041Schristos   -F --target <bfdname>            Set both input and output format to <bfdname>\n\
566*56bb7041Schristos      --debugging                   Convert debugging information, if possible\n\
567*56bb7041Schristos   -p --preserve-dates              Copy modified/access timestamps to the output\n"));
568*56bb7041Schristos   if (DEFAULT_AR_DETERMINISTIC)
569*56bb7041Schristos     fprintf (stream, _("\
570*56bb7041Schristos   -D --enable-deterministic-archives\n\
571*56bb7041Schristos                                    Produce deterministic output when stripping archives (default)\n\
572*56bb7041Schristos   -U --disable-deterministic-archives\n\
573*56bb7041Schristos                                    Disable -D behavior\n"));
574*56bb7041Schristos   else
575*56bb7041Schristos     fprintf (stream, _("\
576*56bb7041Schristos   -D --enable-deterministic-archives\n\
577*56bb7041Schristos                                    Produce deterministic output when stripping archives\n\
578*56bb7041Schristos   -U --disable-deterministic-archives\n\
579*56bb7041Schristos                                    Disable -D behavior (default)\n"));
580*56bb7041Schristos   fprintf (stream, _("\
581*56bb7041Schristos   -j --only-section <name>         Only copy section <name> into the output\n\
582*56bb7041Schristos      --add-gnu-debuglink=<file>    Add section .gnu_debuglink linking to <file>\n\
583*56bb7041Schristos   -R --remove-section <name>       Remove section <name> from the output\n\
584*56bb7041Schristos      --remove-relocations <name>   Remove relocations from section <name>\n\
585*56bb7041Schristos   -S --strip-all                   Remove all symbol and relocation information\n\
586*56bb7041Schristos   -g --strip-debug                 Remove all debugging symbols & sections\n\
587*56bb7041Schristos      --strip-dwo                   Remove all DWO sections\n\
588*56bb7041Schristos      --strip-unneeded              Remove all symbols not needed by relocations\n\
589*56bb7041Schristos   -N --strip-symbol <name>         Do not copy symbol <name>\n\
590*56bb7041Schristos      --strip-unneeded-symbol <name>\n\
591*56bb7041Schristos                                    Do not copy symbol <name> unless needed by\n\
592*56bb7041Schristos                                      relocations\n\
593*56bb7041Schristos      --only-keep-debug             Strip everything but the debug information\n\
594*56bb7041Schristos      --extract-dwo                 Copy only DWO sections\n\
595*56bb7041Schristos      --extract-symbol              Remove section contents but keep symbols\n\
596*56bb7041Schristos      --keep-section <name>         Do not strip section <name>\n\
597*56bb7041Schristos   -K --keep-symbol <name>          Do not strip symbol <name>\n\
598*56bb7041Schristos      --keep-file-symbols           Do not strip file symbol(s)\n\
599*56bb7041Schristos      --localize-hidden             Turn all ELF hidden symbols into locals\n\
600*56bb7041Schristos   -L --localize-symbol <name>      Force symbol <name> to be marked as a local\n\
601*56bb7041Schristos      --globalize-symbol <name>     Force symbol <name> to be marked as a global\n\
602*56bb7041Schristos   -G --keep-global-symbol <name>   Localize all symbols except <name>\n\
603*56bb7041Schristos   -W --weaken-symbol <name>        Force symbol <name> to be marked as a weak\n\
604*56bb7041Schristos      --weaken                      Force all global symbols to be marked as weak\n\
605*56bb7041Schristos   -w --wildcard                    Permit wildcard in symbol comparison\n\
606*56bb7041Schristos   -x --discard-all                 Remove all non-global symbols\n\
607*56bb7041Schristos   -X --discard-locals              Remove any compiler-generated symbols\n\
608*56bb7041Schristos   -i --interleave[=<number>]       Only copy N out of every <number> bytes\n\
609*56bb7041Schristos      --interleave-width <number>   Set N for --interleave\n\
610*56bb7041Schristos   -b --byte <num>                  Select byte <num> in every interleaved block\n\
611*56bb7041Schristos      --gap-fill <val>              Fill gaps between sections with <val>\n\
612*56bb7041Schristos      --pad-to <addr>               Pad the last section up to address <addr>\n\
613*56bb7041Schristos      --set-start <addr>            Set the start address to <addr>\n\
614*56bb7041Schristos     {--change-start|--adjust-start} <incr>\n\
615*56bb7041Schristos                                    Add <incr> to the start address\n\
616*56bb7041Schristos     {--change-addresses|--adjust-vma} <incr>\n\
617*56bb7041Schristos                                    Add <incr> to LMA, VMA and start addresses\n\
618*56bb7041Schristos     {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
619*56bb7041Schristos                                    Change LMA and VMA of section <name> by <val>\n\
620*56bb7041Schristos      --change-section-lma <name>{=|+|-}<val>\n\
621*56bb7041Schristos                                    Change the LMA of section <name> by <val>\n\
622*56bb7041Schristos      --change-section-vma <name>{=|+|-}<val>\n\
623*56bb7041Schristos                                    Change the VMA of section <name> by <val>\n\
624*56bb7041Schristos     {--[no-]change-warnings|--[no-]adjust-warnings}\n\
625*56bb7041Schristos                                    Warn if a named section does not exist\n\
626*56bb7041Schristos      --set-section-flags <name>=<flags>\n\
627*56bb7041Schristos                                    Set section <name>'s properties to <flags>\n\
628*56bb7041Schristos      --set-section-alignment <name>=<align>\n\
629*56bb7041Schristos                                    Set section <name>'s alignment to <align> bytes\n\
630*56bb7041Schristos      --add-section <name>=<file>   Add section <name> found in <file> to output\n\
631*56bb7041Schristos      --update-section <name>=<file>\n\
632*56bb7041Schristos                                    Update contents of section <name> with\n\
633*56bb7041Schristos                                    contents found in <file>\n\
634*56bb7041Schristos      --dump-section <name>=<file>  Dump the contents of section <name> into <file>\n\
635*56bb7041Schristos      --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
636*56bb7041Schristos      --long-section-names {enable|disable|keep}\n\
637*56bb7041Schristos                                    Handle long section names in Coff objects.\n\
638*56bb7041Schristos      --change-leading-char         Force output format's leading character style\n\
639*56bb7041Schristos      --remove-leading-char         Remove leading character from global symbols\n\
640*56bb7041Schristos      --reverse-bytes=<num>         Reverse <num> bytes at a time, in output sections with content\n\
641*56bb7041Schristos      --redefine-sym <old>=<new>    Redefine symbol name <old> to <new>\n\
642*56bb7041Schristos      --redefine-syms <file>        --redefine-sym for all symbol pairs \n\
643*56bb7041Schristos                                      listed in <file>\n\
644*56bb7041Schristos      --srec-len <number>           Restrict the length of generated Srecords\n\
645*56bb7041Schristos      --srec-forceS3                Restrict the type of generated Srecords to S3\n\
646*56bb7041Schristos      --strip-symbols <file>        -N for all symbols listed in <file>\n\
647*56bb7041Schristos      --strip-unneeded-symbols <file>\n\
648*56bb7041Schristos                                    --strip-unneeded-symbol for all symbols listed\n\
649*56bb7041Schristos                                      in <file>\n\
650*56bb7041Schristos      --keep-symbols <file>         -K for all symbols listed in <file>\n\
651*56bb7041Schristos      --localize-symbols <file>     -L for all symbols listed in <file>\n\
652*56bb7041Schristos      --globalize-symbols <file>    --globalize-symbol for all in <file>\n\
653*56bb7041Schristos      --keep-global-symbols <file>  -G for all symbols listed in <file>\n\
654*56bb7041Schristos      --weaken-symbols <file>       -W for all symbols listed in <file>\n\
655*56bb7041Schristos      --add-symbol <name>=[<section>:]<value>[,<flags>]  Add a symbol\n\
656*56bb7041Schristos      --alt-machine-code <index>    Use the target's <index>'th alternative machine\n\
657*56bb7041Schristos      --writable-text               Mark the output text as writable\n\
658*56bb7041Schristos      --readonly-text               Make the output text write protected\n\
659*56bb7041Schristos      --pure                        Mark the output file as demand paged\n\
660*56bb7041Schristos      --impure                      Mark the output file as impure\n\
661*56bb7041Schristos      --prefix-symbols <prefix>     Add <prefix> to start of every symbol name\n\
662*56bb7041Schristos      --prefix-sections <prefix>    Add <prefix> to start of every section name\n\
663*56bb7041Schristos      --prefix-alloc-sections <prefix>\n\
664*56bb7041Schristos                                    Add <prefix> to start of every allocatable\n\
665*56bb7041Schristos                                      section name\n\
666*56bb7041Schristos      --file-alignment <num>        Set PE file alignment to <num>\n\
667*56bb7041Schristos      --heap <reserve>[,<commit>]   Set PE reserve/commit heap to <reserve>/\n\
668*56bb7041Schristos                                    <commit>\n\
669*56bb7041Schristos      --image-base <address>        Set PE image base to <address>\n\
670*56bb7041Schristos      --section-alignment <num>     Set PE section alignment to <num>\n\
671*56bb7041Schristos      --stack <reserve>[,<commit>]  Set PE reserve/commit stack to <reserve>/\n\
672*56bb7041Schristos                                    <commit>\n\
673*56bb7041Schristos      --subsystem <name>[:<version>]\n\
674*56bb7041Schristos                                    Set PE subsystem to <name> [& <version>]\n\
675*56bb7041Schristos      --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
676*56bb7041Schristos                                    Compress DWARF debug sections using zlib\n\
677*56bb7041Schristos      --decompress-debug-sections   Decompress DWARF debug sections using zlib\n\
678*56bb7041Schristos      --elf-stt-common=[yes|no]     Generate ELF common symbols with STT_COMMON\n\
679*56bb7041Schristos                                      type\n\
680*56bb7041Schristos      --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
681*56bb7041Schristos   -M  --merge-notes                Remove redundant entries in note sections\n\
682*56bb7041Schristos       --no-merge-notes             Do not attempt to remove redundant notes (default)\n\
683*56bb7041Schristos   -v --verbose                     List all object files modified\n\
684*56bb7041Schristos   @<file>                          Read options from <file>\n\
685*56bb7041Schristos   -V --version                     Display this program's version number\n\
686*56bb7041Schristos   -h --help                        Display this output\n\
687*56bb7041Schristos      --info                        List object formats & architectures supported\n\
688*56bb7041Schristos "));
689*56bb7041Schristos   list_supported_targets (program_name, stream);
690*56bb7041Schristos   if (REPORT_BUGS_TO[0] && exit_status == 0)
691*56bb7041Schristos     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
692*56bb7041Schristos   exit (exit_status);
693*56bb7041Schristos }
694*56bb7041Schristos 
695*56bb7041Schristos ATTRIBUTE_NORETURN static void
strip_usage(FILE * stream,int exit_status)696*56bb7041Schristos strip_usage (FILE *stream, int exit_status)
697*56bb7041Schristos {
698*56bb7041Schristos   fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
699*56bb7041Schristos   fprintf (stream, _(" Removes symbols and sections from files\n"));
700*56bb7041Schristos   fprintf (stream, _(" The options are:\n"));
701*56bb7041Schristos   fprintf (stream, _("\
702*56bb7041Schristos   -I --input-target=<bfdname>      Assume input file is in format <bfdname>\n\
703*56bb7041Schristos   -O --output-target=<bfdname>     Create an output file in format <bfdname>\n\
704*56bb7041Schristos   -F --target=<bfdname>            Set both input and output format to <bfdname>\n\
705*56bb7041Schristos   -p --preserve-dates              Copy modified/access timestamps to the output\n\
706*56bb7041Schristos "));
707*56bb7041Schristos   if (DEFAULT_AR_DETERMINISTIC)
708*56bb7041Schristos     fprintf (stream, _("\
709*56bb7041Schristos   -D --enable-deterministic-archives\n\
710*56bb7041Schristos                                    Produce deterministic output when stripping archives (default)\n\
711*56bb7041Schristos   -U --disable-deterministic-archives\n\
712*56bb7041Schristos                                    Disable -D behavior\n"));
713*56bb7041Schristos   else
714*56bb7041Schristos     fprintf (stream, _("\
715*56bb7041Schristos   -D --enable-deterministic-archives\n\
716*56bb7041Schristos                                    Produce deterministic output when stripping archives\n\
717*56bb7041Schristos   -U --disable-deterministic-archives\n\
718*56bb7041Schristos                                    Disable -D behavior (default)\n"));
719*56bb7041Schristos   fprintf (stream, _("\
720*56bb7041Schristos   -R --remove-section=<name>       Also remove section <name> from the output\n\
721*56bb7041Schristos      --remove-relocations <name>   Remove relocations from section <name>\n\
722*56bb7041Schristos   -s --strip-all                   Remove all symbol and relocation information\n\
723*56bb7041Schristos   -g -S -d --strip-debug           Remove all debugging symbols & sections\n\
724*56bb7041Schristos      --strip-dwo                   Remove all DWO sections\n\
725*56bb7041Schristos      --strip-unneeded              Remove all symbols not needed by relocations\n\
726*56bb7041Schristos      --only-keep-debug             Strip everything but the debug information\n\
727*56bb7041Schristos   -M  --merge-notes                Remove redundant entries in note sections (default)\n\
728*56bb7041Schristos       --no-merge-notes             Do not attempt to remove redundant notes\n\
729*56bb7041Schristos   -N --strip-symbol=<name>         Do not copy symbol <name>\n\
730*56bb7041Schristos      --keep-section=<name>         Do not strip section <name>\n\
731*56bb7041Schristos   -K --keep-symbol=<name>          Do not strip symbol <name>\n\
732*56bb7041Schristos      --keep-file-symbols           Do not strip file symbol(s)\n\
733*56bb7041Schristos   -w --wildcard                    Permit wildcard in symbol comparison\n\
734*56bb7041Schristos   -x --discard-all                 Remove all non-global symbols\n\
735*56bb7041Schristos   -X --discard-locals              Remove any compiler-generated symbols\n\
736*56bb7041Schristos   -v --verbose                     List all object files modified\n\
737*56bb7041Schristos   -V --version                     Display this program's version number\n\
738*56bb7041Schristos   -h --help                        Display this output\n\
739*56bb7041Schristos      --info                        List object formats & architectures supported\n\
740*56bb7041Schristos   -o <file>                        Place stripped output into <file>\n\
741*56bb7041Schristos "));
742*56bb7041Schristos 
743*56bb7041Schristos   list_supported_targets (program_name, stream);
744*56bb7041Schristos   if (REPORT_BUGS_TO[0] && exit_status == 0)
745*56bb7041Schristos     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
746*56bb7041Schristos   exit (exit_status);
747*56bb7041Schristos }
748*56bb7041Schristos 
749*56bb7041Schristos /* Parse section flags into a flagword, with a fatal error if the
750*56bb7041Schristos    string can't be parsed.  */
751*56bb7041Schristos 
752*56bb7041Schristos static flagword
parse_flags(const char * s)753*56bb7041Schristos parse_flags (const char *s)
754*56bb7041Schristos {
755*56bb7041Schristos   flagword ret;
756*56bb7041Schristos   const char *snext;
757*56bb7041Schristos   int len;
758*56bb7041Schristos 
759*56bb7041Schristos   ret = SEC_NO_FLAGS;
760*56bb7041Schristos 
761*56bb7041Schristos   do
762*56bb7041Schristos     {
763*56bb7041Schristos       snext = strchr (s, ',');
764*56bb7041Schristos       if (snext == NULL)
765*56bb7041Schristos 	len = strlen (s);
766*56bb7041Schristos       else
767*56bb7041Schristos 	{
768*56bb7041Schristos 	  len = snext - s;
769*56bb7041Schristos 	  ++snext;
770*56bb7041Schristos 	}
771*56bb7041Schristos 
772*56bb7041Schristos       if (0) ;
773*56bb7041Schristos #define PARSE_FLAG(fname,fval)					\
774*56bb7041Schristos       else if (strncasecmp (fname, s, len) == 0) ret |= fval
775*56bb7041Schristos       PARSE_FLAG ("alloc", SEC_ALLOC);
776*56bb7041Schristos       PARSE_FLAG ("load", SEC_LOAD);
777*56bb7041Schristos       PARSE_FLAG ("noload", SEC_NEVER_LOAD);
778*56bb7041Schristos       PARSE_FLAG ("readonly", SEC_READONLY);
779*56bb7041Schristos       PARSE_FLAG ("debug", SEC_DEBUGGING);
780*56bb7041Schristos       PARSE_FLAG ("code", SEC_CODE);
781*56bb7041Schristos       PARSE_FLAG ("data", SEC_DATA);
782*56bb7041Schristos       PARSE_FLAG ("rom", SEC_ROM);
783*56bb7041Schristos       PARSE_FLAG ("exclude", SEC_EXCLUDE);
784*56bb7041Schristos       PARSE_FLAG ("share", SEC_COFF_SHARED);
785*56bb7041Schristos       PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
786*56bb7041Schristos       PARSE_FLAG ("merge", SEC_MERGE);
787*56bb7041Schristos       PARSE_FLAG ("strings", SEC_STRINGS);
788*56bb7041Schristos #undef PARSE_FLAG
789*56bb7041Schristos       else
790*56bb7041Schristos 	{
791*56bb7041Schristos 	  char *copy;
792*56bb7041Schristos 
793*56bb7041Schristos 	  copy = (char *) xmalloc (len + 1);
794*56bb7041Schristos 	  strncpy (copy, s, len);
795*56bb7041Schristos 	  copy[len] = '\0';
796*56bb7041Schristos 	  non_fatal (_("unrecognized section flag `%s'"), copy);
797*56bb7041Schristos 	  fatal (_("supported flags: %s"),
798*56bb7041Schristos 		 "alloc, load, noload, readonly, debug, code, data, rom, exclude, share, contents, merge, strings");
799*56bb7041Schristos 	}
800*56bb7041Schristos 
801*56bb7041Schristos       s = snext;
802*56bb7041Schristos     }
803*56bb7041Schristos   while (s != NULL);
804*56bb7041Schristos 
805*56bb7041Schristos   return ret;
806*56bb7041Schristos }
807*56bb7041Schristos 
808*56bb7041Schristos /* Parse symbol flags into a flagword, with a fatal error if the
809*56bb7041Schristos    string can't be parsed.  */
810*56bb7041Schristos 
811*56bb7041Schristos static flagword
parse_symflags(const char * s,const char ** other)812*56bb7041Schristos parse_symflags (const char *s, const char **other)
813*56bb7041Schristos {
814*56bb7041Schristos   flagword ret;
815*56bb7041Schristos   const char *snext;
816*56bb7041Schristos   size_t len;
817*56bb7041Schristos 
818*56bb7041Schristos   ret = BSF_NO_FLAGS;
819*56bb7041Schristos 
820*56bb7041Schristos   do
821*56bb7041Schristos     {
822*56bb7041Schristos       snext = strchr (s, ',');
823*56bb7041Schristos       if (snext == NULL)
824*56bb7041Schristos 	len = strlen (s);
825*56bb7041Schristos       else
826*56bb7041Schristos 	{
827*56bb7041Schristos 	  len = snext - s;
828*56bb7041Schristos 	  ++snext;
829*56bb7041Schristos 	}
830*56bb7041Schristos 
831*56bb7041Schristos #define PARSE_FLAG(fname, fval)						\
832*56bb7041Schristos       else if (len == sizeof fname - 1					\
833*56bb7041Schristos 	       && strncasecmp (fname, s, len) == 0)			\
834*56bb7041Schristos 	ret |= fval
835*56bb7041Schristos 
836*56bb7041Schristos #define PARSE_OTHER(fname, fval)					\
837*56bb7041Schristos       else if (len >= sizeof fname					\
838*56bb7041Schristos 	       && strncasecmp (fname, s, sizeof fname - 1) == 0)	\
839*56bb7041Schristos 	fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
840*56bb7041Schristos 
841*56bb7041Schristos       if (0) ;
842*56bb7041Schristos       PARSE_FLAG ("local", BSF_LOCAL);
843*56bb7041Schristos       PARSE_FLAG ("global", BSF_GLOBAL);
844*56bb7041Schristos       PARSE_FLAG ("export", BSF_EXPORT);
845*56bb7041Schristos       PARSE_FLAG ("debug", BSF_DEBUGGING);
846*56bb7041Schristos       PARSE_FLAG ("function", BSF_FUNCTION);
847*56bb7041Schristos       PARSE_FLAG ("weak", BSF_WEAK);
848*56bb7041Schristos       PARSE_FLAG ("section", BSF_SECTION_SYM);
849*56bb7041Schristos       PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
850*56bb7041Schristos       PARSE_FLAG ("warning", BSF_WARNING);
851*56bb7041Schristos       PARSE_FLAG ("indirect", BSF_INDIRECT);
852*56bb7041Schristos       PARSE_FLAG ("file", BSF_FILE);
853*56bb7041Schristos       PARSE_FLAG ("object", BSF_OBJECT);
854*56bb7041Schristos       PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
855*56bb7041Schristos       PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
856*56bb7041Schristos       PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
857*56bb7041Schristos       PARSE_OTHER ("before=", *other);
858*56bb7041Schristos 
859*56bb7041Schristos #undef PARSE_FLAG
860*56bb7041Schristos #undef PARSE_OTHER
861*56bb7041Schristos       else
862*56bb7041Schristos 	{
863*56bb7041Schristos 	  char *copy;
864*56bb7041Schristos 
865*56bb7041Schristos 	  copy = (char *) xmalloc (len + 1);
866*56bb7041Schristos 	  strncpy (copy, s, len);
867*56bb7041Schristos 	  copy[len] = '\0';
868*56bb7041Schristos 	  non_fatal (_("unrecognized symbol flag `%s'"), copy);
869*56bb7041Schristos 	  fatal (_("supported flags: %s"),
870*56bb7041Schristos 		 "local, global, export, debug, function, weak, section, "
871*56bb7041Schristos 		 "constructor, warning, indirect, file, object, synthetic, "
872*56bb7041Schristos 		 "indirect-function, unique-object, before=<othersym>");
873*56bb7041Schristos 	}
874*56bb7041Schristos 
875*56bb7041Schristos       s = snext;
876*56bb7041Schristos     }
877*56bb7041Schristos   while (s != NULL);
878*56bb7041Schristos 
879*56bb7041Schristos   return ret;
880*56bb7041Schristos }
881*56bb7041Schristos 
882*56bb7041Schristos /* Find and optionally add an entry in the change_sections list.
883*56bb7041Schristos 
884*56bb7041Schristos    We need to be careful in how we match section names because of the support
885*56bb7041Schristos    for wildcard characters.  For example suppose that the user has invoked
886*56bb7041Schristos    objcopy like this:
887*56bb7041Schristos 
888*56bb7041Schristos        --set-section-flags .debug_*=debug
889*56bb7041Schristos        --set-section-flags .debug_str=readonly,debug
890*56bb7041Schristos        --change-section-address .debug_*ranges=0x1000
891*56bb7041Schristos 
892*56bb7041Schristos    With the idea that all debug sections will receive the DEBUG flag, the
893*56bb7041Schristos    .debug_str section will also receive the READONLY flag and the
894*56bb7041Schristos    .debug_ranges and .debug_aranges sections will have their address set to
895*56bb7041Schristos    0x1000.  (This may not make much sense, but it is just an example).
896*56bb7041Schristos 
897*56bb7041Schristos    When adding the section name patterns to the section list we need to make
898*56bb7041Schristos    sure that previous entries do not match with the new entry, unless the
899*56bb7041Schristos    match is exact.  (In which case we assume that the user is overriding
900*56bb7041Schristos    the previous entry with the new context).
901*56bb7041Schristos 
902*56bb7041Schristos    When matching real section names to the section list we make use of the
903*56bb7041Schristos    wildcard characters, but we must do so in context.  Eg if we are setting
904*56bb7041Schristos    section addresses then we match for .debug_ranges but not for .debug_info.
905*56bb7041Schristos 
906*56bb7041Schristos    Finally, if ADD is false and we do find a match, we mark the section list
907*56bb7041Schristos    entry as used.  */
908*56bb7041Schristos 
909*56bb7041Schristos static struct section_list *
find_section_list(const char * name,bfd_boolean add,unsigned int context)910*56bb7041Schristos find_section_list (const char *name, bfd_boolean add, unsigned int context)
911*56bb7041Schristos {
912*56bb7041Schristos   struct section_list *p, *match = NULL;
913*56bb7041Schristos 
914*56bb7041Schristos   /* assert ((context & ((1 << 7) - 1)) != 0); */
915*56bb7041Schristos 
916*56bb7041Schristos   for (p = change_sections; p != NULL; p = p->next)
917*56bb7041Schristos     {
918*56bb7041Schristos       if (add)
919*56bb7041Schristos 	{
920*56bb7041Schristos 	  if (strcmp (p->pattern, name) == 0)
921*56bb7041Schristos 	    {
922*56bb7041Schristos 	      /* Check for context conflicts.  */
923*56bb7041Schristos 	      if (((p->context & SECTION_CONTEXT_REMOVE)
924*56bb7041Schristos 		   && (context & SECTION_CONTEXT_COPY))
925*56bb7041Schristos 		  || ((context & SECTION_CONTEXT_REMOVE)
926*56bb7041Schristos 		      && (p->context & SECTION_CONTEXT_COPY)))
927*56bb7041Schristos 		fatal (_("error: %s both copied and removed"), name);
928*56bb7041Schristos 
929*56bb7041Schristos 	      if (((p->context & SECTION_CONTEXT_SET_VMA)
930*56bb7041Schristos 		  && (context & SECTION_CONTEXT_ALTER_VMA))
931*56bb7041Schristos 		  || ((context & SECTION_CONTEXT_SET_VMA)
932*56bb7041Schristos 		      && (context & SECTION_CONTEXT_ALTER_VMA)))
933*56bb7041Schristos 		fatal (_("error: %s both sets and alters VMA"), name);
934*56bb7041Schristos 
935*56bb7041Schristos 	      if (((p->context & SECTION_CONTEXT_SET_LMA)
936*56bb7041Schristos 		  && (context & SECTION_CONTEXT_ALTER_LMA))
937*56bb7041Schristos 		  || ((context & SECTION_CONTEXT_SET_LMA)
938*56bb7041Schristos 		      && (context & SECTION_CONTEXT_ALTER_LMA)))
939*56bb7041Schristos 		fatal (_("error: %s both sets and alters LMA"), name);
940*56bb7041Schristos 
941*56bb7041Schristos 	      /* Extend the context.  */
942*56bb7041Schristos 	      p->context |= context;
943*56bb7041Schristos 	      return p;
944*56bb7041Schristos 	    }
945*56bb7041Schristos 	}
946*56bb7041Schristos       /* If we are not adding a new name/pattern then
947*56bb7041Schristos 	 only check for a match if the context applies.  */
948*56bb7041Schristos       else if (p->context & context)
949*56bb7041Schristos         {
950*56bb7041Schristos           /* We could check for the presence of wildchar characters
951*56bb7041Schristos              first and choose between calling strcmp and fnmatch,
952*56bb7041Schristos              but is that really worth it ?  */
953*56bb7041Schristos           if (p->pattern [0] == '!')
954*56bb7041Schristos             {
955*56bb7041Schristos               if (fnmatch (p->pattern + 1, name, 0) == 0)
956*56bb7041Schristos                 {
957*56bb7041Schristos                   p->used = TRUE;
958*56bb7041Schristos                   return NULL;
959*56bb7041Schristos                 }
960*56bb7041Schristos             }
961*56bb7041Schristos           else
962*56bb7041Schristos             {
963*56bb7041Schristos               if (fnmatch (p->pattern, name, 0) == 0)
964*56bb7041Schristos                 {
965*56bb7041Schristos                   if (match == NULL)
966*56bb7041Schristos                     match = p;
967*56bb7041Schristos                 }
968*56bb7041Schristos             }
969*56bb7041Schristos         }
970*56bb7041Schristos     }
971*56bb7041Schristos 
972*56bb7041Schristos   if (! add)
973*56bb7041Schristos     {
974*56bb7041Schristos       if (match != NULL)
975*56bb7041Schristos         match->used = TRUE;
976*56bb7041Schristos       return match;
977*56bb7041Schristos     }
978*56bb7041Schristos 
979*56bb7041Schristos   p = (struct section_list *) xmalloc (sizeof (struct section_list));
980*56bb7041Schristos   p->pattern = name;
981*56bb7041Schristos   p->used = FALSE;
982*56bb7041Schristos   p->context = context;
983*56bb7041Schristos   p->vma_val = 0;
984*56bb7041Schristos   p->lma_val = 0;
985*56bb7041Schristos   p->flags = 0;
986*56bb7041Schristos   p->alignment = 0;
987*56bb7041Schristos   p->next = change_sections;
988*56bb7041Schristos   change_sections = p;
989*56bb7041Schristos 
990*56bb7041Schristos   return p;
991*56bb7041Schristos }
992*56bb7041Schristos 
993*56bb7041Schristos /* S1 is the entry node already in the table, S2 is the key node.  */
994*56bb7041Schristos 
995*56bb7041Schristos static int
eq_string_redefnode(const void * s1,const void * s2)996*56bb7041Schristos eq_string_redefnode (const void *s1, const void *s2)
997*56bb7041Schristos {
998*56bb7041Schristos   struct redefine_node *node1 = (struct redefine_node *) s1;
999*56bb7041Schristos   struct redefine_node *node2 = (struct redefine_node *) s2;
1000*56bb7041Schristos   return !strcmp ((const char *) node1->source, (const char *) node2->source);
1001*56bb7041Schristos }
1002*56bb7041Schristos 
1003*56bb7041Schristos /* P is redefine node.  Hash value is generated from its "source" filed.  */
1004*56bb7041Schristos 
1005*56bb7041Schristos static hashval_t
htab_hash_redefnode(const void * p)1006*56bb7041Schristos htab_hash_redefnode (const void *p)
1007*56bb7041Schristos {
1008*56bb7041Schristos   struct redefine_node *redefnode = (struct redefine_node *) p;
1009*56bb7041Schristos   return htab_hash_string (redefnode->source);
1010*56bb7041Schristos }
1011*56bb7041Schristos 
1012*56bb7041Schristos /* Create hashtab used for redefine node.  */
1013*56bb7041Schristos 
1014*56bb7041Schristos static htab_t
create_symbol2redef_htab(void)1015*56bb7041Schristos create_symbol2redef_htab (void)
1016*56bb7041Schristos {
1017*56bb7041Schristos   return htab_create_alloc (16, htab_hash_redefnode, eq_string_redefnode, NULL,
1018*56bb7041Schristos 			    xcalloc, free);
1019*56bb7041Schristos }
1020*56bb7041Schristos 
1021*56bb7041Schristos /* There is htab_hash_string but no htab_eq_string. Makes sense.  */
1022*56bb7041Schristos 
1023*56bb7041Schristos static int
eq_string(const void * s1,const void * s2)1024*56bb7041Schristos eq_string (const void *s1, const void *s2)
1025*56bb7041Schristos {
1026*56bb7041Schristos   return strcmp ((const char *) s1, (const char *) s2) == 0;
1027*56bb7041Schristos }
1028*56bb7041Schristos 
1029*56bb7041Schristos static htab_t
create_symbol_htab(void)1030*56bb7041Schristos create_symbol_htab (void)
1031*56bb7041Schristos {
1032*56bb7041Schristos   return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
1033*56bb7041Schristos }
1034*56bb7041Schristos 
1035*56bb7041Schristos static void
create_symbol_htabs(void)1036*56bb7041Schristos create_symbol_htabs (void)
1037*56bb7041Schristos {
1038*56bb7041Schristos   strip_specific_htab = create_symbol_htab ();
1039*56bb7041Schristos   strip_unneeded_htab = create_symbol_htab ();
1040*56bb7041Schristos   keep_specific_htab = create_symbol_htab ();
1041*56bb7041Schristos   localize_specific_htab = create_symbol_htab ();
1042*56bb7041Schristos   globalize_specific_htab = create_symbol_htab ();
1043*56bb7041Schristos   keepglobal_specific_htab = create_symbol_htab ();
1044*56bb7041Schristos   weaken_specific_htab = create_symbol_htab ();
1045*56bb7041Schristos   redefine_specific_htab = create_symbol2redef_htab ();
1046*56bb7041Schristos   /* As there is no bidirectional hash table in libiberty, need a reverse table
1047*56bb7041Schristos      to check duplicated target string.  */
1048*56bb7041Schristos   redefine_specific_reverse_htab = create_symbol_htab ();
1049*56bb7041Schristos }
1050*56bb7041Schristos 
1051*56bb7041Schristos /* Add a symbol to strip_specific_list.  */
1052*56bb7041Schristos 
1053*56bb7041Schristos static void
add_specific_symbol(const char * name,htab_t htab)1054*56bb7041Schristos add_specific_symbol (const char *name, htab_t htab)
1055*56bb7041Schristos {
1056*56bb7041Schristos   *htab_find_slot (htab, name, INSERT) = (char *) name;
1057*56bb7041Schristos }
1058*56bb7041Schristos 
1059*56bb7041Schristos /* Like add_specific_symbol, but the element type is void *.  */
1060*56bb7041Schristos 
1061*56bb7041Schristos static void
add_specific_symbol_node(const void * node,htab_t htab)1062*56bb7041Schristos add_specific_symbol_node (const void *node, htab_t htab)
1063*56bb7041Schristos {
1064*56bb7041Schristos   *htab_find_slot (htab, node, INSERT) = (void *) node;
1065*56bb7041Schristos }
1066*56bb7041Schristos 
1067*56bb7041Schristos /* Add symbols listed in `filename' to strip_specific_list.  */
1068*56bb7041Schristos 
1069*56bb7041Schristos #define IS_WHITESPACE(c)      ((c) == ' ' || (c) == '\t')
1070*56bb7041Schristos #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
1071*56bb7041Schristos 
1072*56bb7041Schristos static void
add_specific_symbols(const char * filename,htab_t htab,char ** buffer_p)1073*56bb7041Schristos add_specific_symbols (const char *filename, htab_t htab, char **buffer_p)
1074*56bb7041Schristos {
1075*56bb7041Schristos   off_t  size;
1076*56bb7041Schristos   FILE * f;
1077*56bb7041Schristos   char * line;
1078*56bb7041Schristos   char * buffer;
1079*56bb7041Schristos   unsigned int line_count;
1080*56bb7041Schristos 
1081*56bb7041Schristos   size = get_file_size (filename);
1082*56bb7041Schristos   if (size == 0)
1083*56bb7041Schristos     {
1084*56bb7041Schristos       status = 1;
1085*56bb7041Schristos       return;
1086*56bb7041Schristos     }
1087*56bb7041Schristos 
1088*56bb7041Schristos   buffer = (char *) xmalloc (size + 2);
1089*56bb7041Schristos   f = fopen (filename, FOPEN_RT);
1090*56bb7041Schristos   if (f == NULL)
1091*56bb7041Schristos     fatal (_("cannot open '%s': %s"), filename, strerror (errno));
1092*56bb7041Schristos 
1093*56bb7041Schristos   if (fread (buffer, 1, size, f) == 0 || ferror (f))
1094*56bb7041Schristos     fatal (_("%s: fread failed"), filename);
1095*56bb7041Schristos 
1096*56bb7041Schristos   fclose (f);
1097*56bb7041Schristos   buffer [size] = '\n';
1098*56bb7041Schristos   buffer [size + 1] = '\0';
1099*56bb7041Schristos 
1100*56bb7041Schristos   line_count = 1;
1101*56bb7041Schristos 
1102*56bb7041Schristos   for (line = buffer; * line != '\0'; line ++)
1103*56bb7041Schristos     {
1104*56bb7041Schristos       char * eol;
1105*56bb7041Schristos       char * name;
1106*56bb7041Schristos       char * name_end;
1107*56bb7041Schristos       int finished = FALSE;
1108*56bb7041Schristos 
1109*56bb7041Schristos       for (eol = line;; eol ++)
1110*56bb7041Schristos 	{
1111*56bb7041Schristos 	  switch (* eol)
1112*56bb7041Schristos 	    {
1113*56bb7041Schristos 	    case '\n':
1114*56bb7041Schristos 	      * eol = '\0';
1115*56bb7041Schristos 	      /* Cope with \n\r.  */
1116*56bb7041Schristos 	      if (eol[1] == '\r')
1117*56bb7041Schristos 		++ eol;
1118*56bb7041Schristos 	      finished = TRUE;
1119*56bb7041Schristos 	      break;
1120*56bb7041Schristos 
1121*56bb7041Schristos 	    case '\r':
1122*56bb7041Schristos 	      * eol = '\0';
1123*56bb7041Schristos 	      /* Cope with \r\n.  */
1124*56bb7041Schristos 	      if (eol[1] == '\n')
1125*56bb7041Schristos 		++ eol;
1126*56bb7041Schristos 	      finished = TRUE;
1127*56bb7041Schristos 	      break;
1128*56bb7041Schristos 
1129*56bb7041Schristos 	    case 0:
1130*56bb7041Schristos 	      finished = TRUE;
1131*56bb7041Schristos 	      break;
1132*56bb7041Schristos 
1133*56bb7041Schristos 	    case '#':
1134*56bb7041Schristos 	      /* Line comment, Terminate the line here, in case a
1135*56bb7041Schristos 		 name is present and then allow the rest of the
1136*56bb7041Schristos 		 loop to find the real end of the line.  */
1137*56bb7041Schristos 	      * eol = '\0';
1138*56bb7041Schristos 	      break;
1139*56bb7041Schristos 
1140*56bb7041Schristos 	    default:
1141*56bb7041Schristos 	      break;
1142*56bb7041Schristos 	    }
1143*56bb7041Schristos 
1144*56bb7041Schristos 	  if (finished)
1145*56bb7041Schristos 	    break;
1146*56bb7041Schristos 	}
1147*56bb7041Schristos 
1148*56bb7041Schristos       /* A name may now exist somewhere between 'line' and 'eol'.
1149*56bb7041Schristos 	 Strip off leading whitespace and trailing whitespace,
1150*56bb7041Schristos 	 then add it to the list.  */
1151*56bb7041Schristos       for (name = line; IS_WHITESPACE (* name); name ++)
1152*56bb7041Schristos 	;
1153*56bb7041Schristos       for (name_end = name;
1154*56bb7041Schristos 	   (! IS_WHITESPACE (* name_end))
1155*56bb7041Schristos 	   && (! IS_LINE_TERMINATOR (* name_end));
1156*56bb7041Schristos 	   name_end ++)
1157*56bb7041Schristos 	;
1158*56bb7041Schristos 
1159*56bb7041Schristos       if (! IS_LINE_TERMINATOR (* name_end))
1160*56bb7041Schristos 	{
1161*56bb7041Schristos 	  char * extra;
1162*56bb7041Schristos 
1163*56bb7041Schristos 	  for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
1164*56bb7041Schristos 	    ;
1165*56bb7041Schristos 
1166*56bb7041Schristos 	  if (! IS_LINE_TERMINATOR (* extra))
1167*56bb7041Schristos 	    non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1168*56bb7041Schristos 		       filename, line_count);
1169*56bb7041Schristos 	}
1170*56bb7041Schristos 
1171*56bb7041Schristos       * name_end = '\0';
1172*56bb7041Schristos 
1173*56bb7041Schristos       if (name_end > name)
1174*56bb7041Schristos 	add_specific_symbol (name, htab);
1175*56bb7041Schristos 
1176*56bb7041Schristos       /* Advance line pointer to end of line.  The 'eol ++' in the for
1177*56bb7041Schristos 	 loop above will then advance us to the start of the next line.  */
1178*56bb7041Schristos       line = eol;
1179*56bb7041Schristos       line_count ++;
1180*56bb7041Schristos     }
1181*56bb7041Schristos 
1182*56bb7041Schristos   /* Do not free the buffer.  Parts of it will have been referenced
1183*56bb7041Schristos      in the calls to add_specific_symbol.  */
1184*56bb7041Schristos   *buffer_p = buffer;
1185*56bb7041Schristos }
1186*56bb7041Schristos 
1187*56bb7041Schristos /* See whether a symbol should be stripped or kept
1188*56bb7041Schristos    based on strip_specific_list and keep_symbols.  */
1189*56bb7041Schristos 
1190*56bb7041Schristos static int
is_specified_symbol_predicate(void ** slot,void * data)1191*56bb7041Schristos is_specified_symbol_predicate (void **slot, void *data)
1192*56bb7041Schristos {
1193*56bb7041Schristos   struct is_specified_symbol_predicate_data *d =
1194*56bb7041Schristos       (struct is_specified_symbol_predicate_data *) data;
1195*56bb7041Schristos   const char *slot_name = (char *) *slot;
1196*56bb7041Schristos 
1197*56bb7041Schristos   if (*slot_name != '!')
1198*56bb7041Schristos     {
1199*56bb7041Schristos       if (! fnmatch (slot_name, d->name, 0))
1200*56bb7041Schristos 	{
1201*56bb7041Schristos 	  d->found = TRUE;
1202*56bb7041Schristos 	  /* Continue traversal, there might be a non-match rule.  */
1203*56bb7041Schristos 	  return 1;
1204*56bb7041Schristos 	}
1205*56bb7041Schristos     }
1206*56bb7041Schristos   else
1207*56bb7041Schristos     {
1208*56bb7041Schristos       if (! fnmatch (slot_name + 1, d->name, 0))
1209*56bb7041Schristos 	{
1210*56bb7041Schristos 	  d->found = FALSE;
1211*56bb7041Schristos 	  /* Stop traversal.  */
1212*56bb7041Schristos 	  return 0;
1213*56bb7041Schristos 	}
1214*56bb7041Schristos     }
1215*56bb7041Schristos 
1216*56bb7041Schristos   /* Continue traversal.  */
1217*56bb7041Schristos   return 1;
1218*56bb7041Schristos }
1219*56bb7041Schristos 
1220*56bb7041Schristos static bfd_boolean
is_specified_symbol(const char * name,htab_t htab)1221*56bb7041Schristos is_specified_symbol (const char *name, htab_t htab)
1222*56bb7041Schristos {
1223*56bb7041Schristos   if (wildcard)
1224*56bb7041Schristos     {
1225*56bb7041Schristos       struct is_specified_symbol_predicate_data data;
1226*56bb7041Schristos 
1227*56bb7041Schristos       data.name = name;
1228*56bb7041Schristos       data.found = FALSE;
1229*56bb7041Schristos 
1230*56bb7041Schristos       htab_traverse (htab, is_specified_symbol_predicate, &data);
1231*56bb7041Schristos 
1232*56bb7041Schristos       return data.found;
1233*56bb7041Schristos     }
1234*56bb7041Schristos 
1235*56bb7041Schristos   return htab_find (htab, name) != NULL;
1236*56bb7041Schristos }
1237*56bb7041Schristos 
1238*56bb7041Schristos /* Return a pointer to the symbol used as a signature for GROUP.  */
1239*56bb7041Schristos 
1240*56bb7041Schristos static asymbol *
group_signature(asection * group)1241*56bb7041Schristos group_signature (asection *group)
1242*56bb7041Schristos {
1243*56bb7041Schristos   bfd *abfd = group->owner;
1244*56bb7041Schristos   Elf_Internal_Shdr *ghdr;
1245*56bb7041Schristos 
1246*56bb7041Schristos   /* PR 20089: An earlier error may have prevented us from loading the symbol table.  */
1247*56bb7041Schristos   if (isympp == NULL)
1248*56bb7041Schristos     return NULL;
1249*56bb7041Schristos 
1250*56bb7041Schristos   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1251*56bb7041Schristos     return NULL;
1252*56bb7041Schristos 
1253*56bb7041Schristos   ghdr = &elf_section_data (group)->this_hdr;
1254*56bb7041Schristos   if (ghdr->sh_link == elf_onesymtab (abfd))
1255*56bb7041Schristos     {
1256*56bb7041Schristos       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1257*56bb7041Schristos       Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
1258*56bb7041Schristos 
1259*56bb7041Schristos       if (ghdr->sh_info > 0
1260*56bb7041Schristos 	  && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
1261*56bb7041Schristos 	return isympp[ghdr->sh_info - 1];
1262*56bb7041Schristos     }
1263*56bb7041Schristos   return NULL;
1264*56bb7041Schristos }
1265*56bb7041Schristos 
1266*56bb7041Schristos /* Return TRUE if the section is a DWO section.  */
1267*56bb7041Schristos 
1268*56bb7041Schristos static bfd_boolean
is_dwo_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)1269*56bb7041Schristos is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1270*56bb7041Schristos {
1271*56bb7041Schristos   const char *name = bfd_section_name (sec);
1272*56bb7041Schristos   int len = strlen (name);
1273*56bb7041Schristos 
1274*56bb7041Schristos   return strncmp (name + len - 4, ".dwo", 4) == 0;
1275*56bb7041Schristos }
1276*56bb7041Schristos 
1277*56bb7041Schristos /* Return TRUE if section SEC is in the update list.  */
1278*56bb7041Schristos 
1279*56bb7041Schristos static bfd_boolean
is_update_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)1280*56bb7041Schristos is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1281*56bb7041Schristos {
1282*56bb7041Schristos   if (update_sections != NULL)
1283*56bb7041Schristos     {
1284*56bb7041Schristos       struct section_add *pupdate;
1285*56bb7041Schristos 
1286*56bb7041Schristos       for (pupdate = update_sections;
1287*56bb7041Schristos 	   pupdate != NULL;
1288*56bb7041Schristos 	   pupdate = pupdate->next)
1289*56bb7041Schristos 	{
1290*56bb7041Schristos 	  if (strcmp (sec->name, pupdate->name) == 0)
1291*56bb7041Schristos 	    return TRUE;
1292*56bb7041Schristos 	}
1293*56bb7041Schristos     }
1294*56bb7041Schristos 
1295*56bb7041Schristos   return FALSE;
1296*56bb7041Schristos }
1297*56bb7041Schristos 
1298*56bb7041Schristos static bfd_boolean
is_mergeable_note_section(bfd * abfd,asection * sec)1299*56bb7041Schristos is_mergeable_note_section (bfd * abfd, asection * sec)
1300*56bb7041Schristos {
1301*56bb7041Schristos   if (merge_notes
1302*56bb7041Schristos       && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1303*56bb7041Schristos       && elf_section_data (sec)->this_hdr.sh_type == SHT_NOTE
1304*56bb7041Schristos       /* FIXME: We currently only support merging GNU_BUILD_NOTEs.
1305*56bb7041Schristos 	 We should add support for more note types.  */
1306*56bb7041Schristos       && ((elf_section_data (sec)->this_hdr.sh_flags & SHF_GNU_BUILD_NOTE) != 0
1307*56bb7041Schristos 	  /* Old versions of GAS (prior to 2.27) could not set the section
1308*56bb7041Schristos 	     flags to OS-specific values, so we also accept sections that
1309*56bb7041Schristos 	     start with the expected name.  */
1310*56bb7041Schristos 	  || (CONST_STRNEQ (sec->name, GNU_BUILD_ATTRS_SECTION_NAME))))
1311*56bb7041Schristos     return TRUE;
1312*56bb7041Schristos 
1313*56bb7041Schristos   return FALSE;
1314*56bb7041Schristos }
1315*56bb7041Schristos 
1316*56bb7041Schristos /* See if a non-group section is being removed.  */
1317*56bb7041Schristos 
1318*56bb7041Schristos static bfd_boolean
is_strip_section_1(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)1319*56bb7041Schristos is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1320*56bb7041Schristos {
1321*56bb7041Schristos   if (find_section_list (bfd_section_name (sec), FALSE, SECTION_CONTEXT_KEEP)
1322*56bb7041Schristos       != NULL)
1323*56bb7041Schristos     return FALSE;
1324*56bb7041Schristos 
1325*56bb7041Schristos   if (sections_removed || sections_copied)
1326*56bb7041Schristos     {
1327*56bb7041Schristos       struct section_list *p;
1328*56bb7041Schristos       struct section_list *q;
1329*56bb7041Schristos 
1330*56bb7041Schristos       p = find_section_list (bfd_section_name (sec), FALSE,
1331*56bb7041Schristos 			     SECTION_CONTEXT_REMOVE);
1332*56bb7041Schristos       q = find_section_list (bfd_section_name (sec), FALSE,
1333*56bb7041Schristos 			     SECTION_CONTEXT_COPY);
1334*56bb7041Schristos 
1335*56bb7041Schristos       if (p && q)
1336*56bb7041Schristos 	fatal (_("error: section %s matches both remove and copy options"),
1337*56bb7041Schristos 	       bfd_section_name (sec));
1338*56bb7041Schristos       if (p && is_update_section (abfd, sec))
1339*56bb7041Schristos 	fatal (_("error: section %s matches both update and remove options"),
1340*56bb7041Schristos 	       bfd_section_name (sec));
1341*56bb7041Schristos 
1342*56bb7041Schristos       if (p != NULL)
1343*56bb7041Schristos 	return TRUE;
1344*56bb7041Schristos       if (sections_copied && q == NULL)
1345*56bb7041Schristos 	return TRUE;
1346*56bb7041Schristos     }
1347*56bb7041Schristos 
1348*56bb7041Schristos   if ((bfd_section_flags (sec) & SEC_DEBUGGING) != 0)
1349*56bb7041Schristos     {
1350*56bb7041Schristos       if (strip_symbols == STRIP_DEBUG
1351*56bb7041Schristos 	  || strip_symbols == STRIP_UNNEEDED
1352*56bb7041Schristos 	  || strip_symbols == STRIP_ALL
1353*56bb7041Schristos 	  || discard_locals == LOCALS_ALL
1354*56bb7041Schristos 	  || convert_debugging)
1355*56bb7041Schristos 	{
1356*56bb7041Schristos 	  /* By default we don't want to strip .reloc section.
1357*56bb7041Schristos 	     This section has for pe-coff special meaning.   See
1358*56bb7041Schristos 	     pe-dll.c file in ld, and peXXigen.c in bfd for details.  */
1359*56bb7041Schristos 	  if (strcmp (bfd_section_name (sec), ".reloc") != 0)
1360*56bb7041Schristos 	    return TRUE;
1361*56bb7041Schristos 	}
1362*56bb7041Schristos 
1363*56bb7041Schristos       if (strip_symbols == STRIP_DWO)
1364*56bb7041Schristos 	return is_dwo_section (abfd, sec);
1365*56bb7041Schristos 
1366*56bb7041Schristos       if (strip_symbols == STRIP_NONDEBUG)
1367*56bb7041Schristos 	return FALSE;
1368*56bb7041Schristos     }
1369*56bb7041Schristos 
1370*56bb7041Schristos   if (strip_symbols == STRIP_NONDWO)
1371*56bb7041Schristos     return !is_dwo_section (abfd, sec);
1372*56bb7041Schristos 
1373*56bb7041Schristos   return FALSE;
1374*56bb7041Schristos }
1375*56bb7041Schristos 
1376*56bb7041Schristos /* See if a section is being removed.  */
1377*56bb7041Schristos 
1378*56bb7041Schristos static bfd_boolean
is_strip_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)1379*56bb7041Schristos is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1380*56bb7041Schristos {
1381*56bb7041Schristos   if (is_strip_section_1 (abfd, sec))
1382*56bb7041Schristos     return TRUE;
1383*56bb7041Schristos 
1384*56bb7041Schristos   if ((bfd_section_flags (sec) & SEC_GROUP) != 0)
1385*56bb7041Schristos     {
1386*56bb7041Schristos       asymbol *gsym;
1387*56bb7041Schristos       const char *gname;
1388*56bb7041Schristos       asection *elt, *first;
1389*56bb7041Schristos 
1390*56bb7041Schristos       gsym = group_signature (sec);
1391*56bb7041Schristos       /* Strip groups without a valid signature.  */
1392*56bb7041Schristos       if (gsym == NULL)
1393*56bb7041Schristos 	return TRUE;
1394*56bb7041Schristos 
1395*56bb7041Schristos       /* PR binutils/3181
1396*56bb7041Schristos 	 If we are going to strip the group signature symbol, then
1397*56bb7041Schristos 	 strip the group section too.  */
1398*56bb7041Schristos       gname = gsym->name;
1399*56bb7041Schristos       if ((strip_symbols == STRIP_ALL
1400*56bb7041Schristos 	   && !is_specified_symbol (gname, keep_specific_htab))
1401*56bb7041Schristos 	  || is_specified_symbol (gname, strip_specific_htab))
1402*56bb7041Schristos 	return TRUE;
1403*56bb7041Schristos 
1404*56bb7041Schristos       /* Remove the group section if all members are removed.  */
1405*56bb7041Schristos       first = elt = elf_next_in_group (sec);
1406*56bb7041Schristos       while (elt != NULL)
1407*56bb7041Schristos 	{
1408*56bb7041Schristos 	  if (!is_strip_section_1 (abfd, elt))
1409*56bb7041Schristos 	    return FALSE;
1410*56bb7041Schristos 	  elt = elf_next_in_group (elt);
1411*56bb7041Schristos 	  if (elt == first)
1412*56bb7041Schristos 	    break;
1413*56bb7041Schristos 	}
1414*56bb7041Schristos 
1415*56bb7041Schristos       return TRUE;
1416*56bb7041Schristos     }
1417*56bb7041Schristos 
1418*56bb7041Schristos   return FALSE;
1419*56bb7041Schristos }
1420*56bb7041Schristos 
1421*56bb7041Schristos static bfd_boolean
is_nondebug_keep_contents_section(bfd * ibfd,asection * isection)1422*56bb7041Schristos is_nondebug_keep_contents_section (bfd *ibfd, asection *isection)
1423*56bb7041Schristos {
1424*56bb7041Schristos   /* Always keep ELF note sections.  */
1425*56bb7041Schristos   if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour)
1426*56bb7041Schristos     return elf_section_type (isection) == SHT_NOTE;
1427*56bb7041Schristos 
1428*56bb7041Schristos   /* Always keep the .buildid section for PE/COFF.
1429*56bb7041Schristos 
1430*56bb7041Schristos      Strictly, this should be written "always keep the section storing the debug
1431*56bb7041Schristos      directory", but that may be the .text section for objects produced by some
1432*56bb7041Schristos      tools, which it is not sensible to keep.  */
1433*56bb7041Schristos   if (bfd_get_flavour (ibfd) == bfd_target_coff_flavour)
1434*56bb7041Schristos     return strcmp (bfd_section_name (isection), ".buildid") == 0;
1435*56bb7041Schristos 
1436*56bb7041Schristos   return FALSE;
1437*56bb7041Schristos }
1438*56bb7041Schristos 
1439*56bb7041Schristos /* Return true if SYM is a hidden symbol.  */
1440*56bb7041Schristos 
1441*56bb7041Schristos static bfd_boolean
is_hidden_symbol(asymbol * sym)1442*56bb7041Schristos is_hidden_symbol (asymbol *sym)
1443*56bb7041Schristos {
1444*56bb7041Schristos   elf_symbol_type *elf_sym;
1445*56bb7041Schristos 
1446*56bb7041Schristos   elf_sym = elf_symbol_from (sym->the_bfd, sym);
1447*56bb7041Schristos   if (elf_sym != NULL)
1448*56bb7041Schristos     switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
1449*56bb7041Schristos       {
1450*56bb7041Schristos       case STV_HIDDEN:
1451*56bb7041Schristos       case STV_INTERNAL:
1452*56bb7041Schristos 	return TRUE;
1453*56bb7041Schristos       }
1454*56bb7041Schristos   return FALSE;
1455*56bb7041Schristos }
1456*56bb7041Schristos 
1457*56bb7041Schristos /* Empty name is hopefully never a valid symbol name.  */
1458*56bb7041Schristos static const char * empty_name = "";
1459*56bb7041Schristos 
1460*56bb7041Schristos static bfd_boolean
need_sym_before(struct addsym_node ** node,const char * sym)1461*56bb7041Schristos need_sym_before (struct addsym_node **node, const char *sym)
1462*56bb7041Schristos {
1463*56bb7041Schristos   int count;
1464*56bb7041Schristos   struct addsym_node *ptr = add_sym_list;
1465*56bb7041Schristos 
1466*56bb7041Schristos   /* 'othersym' symbols are at the front of the list.  */
1467*56bb7041Schristos   for (count = 0; count < add_symbols; count++)
1468*56bb7041Schristos     {
1469*56bb7041Schristos       if (!ptr->othersym)
1470*56bb7041Schristos 	break;
1471*56bb7041Schristos       if (ptr->othersym == empty_name)
1472*56bb7041Schristos 	continue;
1473*56bb7041Schristos       else if (strcmp (ptr->othersym, sym) == 0)
1474*56bb7041Schristos 	{
1475*56bb7041Schristos 	  free ((char *) ptr->othersym);
1476*56bb7041Schristos 	  ptr->othersym = empty_name;
1477*56bb7041Schristos 	  *node = ptr;
1478*56bb7041Schristos 	  return TRUE;
1479*56bb7041Schristos 	}
1480*56bb7041Schristos       ptr = ptr->next;
1481*56bb7041Schristos     }
1482*56bb7041Schristos   return FALSE;
1483*56bb7041Schristos }
1484*56bb7041Schristos 
1485*56bb7041Schristos static asymbol *
create_new_symbol(struct addsym_node * ptr,bfd * obfd)1486*56bb7041Schristos create_new_symbol (struct addsym_node *ptr, bfd *obfd)
1487*56bb7041Schristos {
1488*56bb7041Schristos   asymbol *sym = bfd_make_empty_symbol (obfd);
1489*56bb7041Schristos 
1490*56bb7041Schristos   bfd_set_asymbol_name (sym, ptr->symdef);
1491*56bb7041Schristos   sym->value = ptr->symval;
1492*56bb7041Schristos   sym->flags = ptr->flags;
1493*56bb7041Schristos   if (ptr->section)
1494*56bb7041Schristos     {
1495*56bb7041Schristos       asection *sec = bfd_get_section_by_name (obfd, ptr->section);
1496*56bb7041Schristos       if (!sec)
1497*56bb7041Schristos 	fatal (_("Section %s not found"), ptr->section);
1498*56bb7041Schristos       sym->section = sec;
1499*56bb7041Schristos     }
1500*56bb7041Schristos   else
1501*56bb7041Schristos     sym->section = bfd_abs_section_ptr;
1502*56bb7041Schristos   return sym;
1503*56bb7041Schristos }
1504*56bb7041Schristos 
1505*56bb7041Schristos /* Choose which symbol entries to copy; put the result in OSYMS.
1506*56bb7041Schristos    We don't copy in place, because that confuses the relocs.
1507*56bb7041Schristos    Return the number of symbols to print.  */
1508*56bb7041Schristos 
1509*56bb7041Schristos static unsigned int
filter_symbols(bfd * abfd,bfd * obfd,asymbol ** osyms,asymbol ** isyms,long symcount)1510*56bb7041Schristos filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
1511*56bb7041Schristos 		asymbol **isyms, long symcount)
1512*56bb7041Schristos {
1513*56bb7041Schristos   asymbol **from = isyms, **to = osyms;
1514*56bb7041Schristos   long src_count = 0, dst_count = 0;
1515*56bb7041Schristos   int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1516*56bb7041Schristos 
1517*56bb7041Schristos   for (; src_count < symcount; src_count++)
1518*56bb7041Schristos     {
1519*56bb7041Schristos       asymbol *sym = from[src_count];
1520*56bb7041Schristos       flagword flags = sym->flags;
1521*56bb7041Schristos       char *name = (char *) bfd_asymbol_name (sym);
1522*56bb7041Schristos       bfd_boolean keep;
1523*56bb7041Schristos       bfd_boolean used_in_reloc = FALSE;
1524*56bb7041Schristos       bfd_boolean undefined;
1525*56bb7041Schristos       bfd_boolean rem_leading_char;
1526*56bb7041Schristos       bfd_boolean add_leading_char;
1527*56bb7041Schristos 
1528*56bb7041Schristos       undefined = bfd_is_und_section (bfd_asymbol_section (sym));
1529*56bb7041Schristos 
1530*56bb7041Schristos       if (add_sym_list)
1531*56bb7041Schristos 	{
1532*56bb7041Schristos 	  struct addsym_node *ptr;
1533*56bb7041Schristos 
1534*56bb7041Schristos 	  if (need_sym_before (&ptr, name))
1535*56bb7041Schristos 	    to[dst_count++] = create_new_symbol (ptr, obfd);
1536*56bb7041Schristos 	}
1537*56bb7041Schristos 
1538*56bb7041Schristos       if (htab_elements (redefine_specific_htab) || section_rename_list)
1539*56bb7041Schristos 	{
1540*56bb7041Schristos 	  char *new_name;
1541*56bb7041Schristos 
1542*56bb7041Schristos 	  new_name = (char *) lookup_sym_redefinition (name);
1543*56bb7041Schristos 	  if (new_name == name
1544*56bb7041Schristos 	      && (flags & BSF_SECTION_SYM) != 0)
1545*56bb7041Schristos 	    new_name = (char *) find_section_rename (name, NULL);
1546*56bb7041Schristos 	  bfd_set_asymbol_name (sym, new_name);
1547*56bb7041Schristos 	  name = new_name;
1548*56bb7041Schristos 	}
1549*56bb7041Schristos 
1550*56bb7041Schristos       /* Check if we will remove the current leading character.  */
1551*56bb7041Schristos       rem_leading_char =
1552*56bb7041Schristos 	(name[0] != '\0'
1553*56bb7041Schristos 	 && name[0] == bfd_get_symbol_leading_char (abfd)
1554*56bb7041Schristos 	 && (change_leading_char
1555*56bb7041Schristos 	     || (remove_leading_char
1556*56bb7041Schristos 		 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1557*56bb7041Schristos 		     || undefined
1558*56bb7041Schristos 		     || bfd_is_com_section (bfd_asymbol_section (sym))))));
1559*56bb7041Schristos 
1560*56bb7041Schristos       /* Check if we will add a new leading character.  */
1561*56bb7041Schristos       add_leading_char =
1562*56bb7041Schristos 	change_leading_char
1563*56bb7041Schristos 	&& (bfd_get_symbol_leading_char (obfd) != '\0')
1564*56bb7041Schristos 	&& (bfd_get_symbol_leading_char (abfd) == '\0'
1565*56bb7041Schristos 	    || (name[0] == bfd_get_symbol_leading_char (abfd)));
1566*56bb7041Schristos 
1567*56bb7041Schristos       /* Short circuit for change_leading_char if we can do it in-place.  */
1568*56bb7041Schristos       if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1569*56bb7041Schristos 	{
1570*56bb7041Schristos 	  name[0] = bfd_get_symbol_leading_char (obfd);
1571*56bb7041Schristos 	  bfd_set_asymbol_name (sym, name);
1572*56bb7041Schristos 	  rem_leading_char = FALSE;
1573*56bb7041Schristos 	  add_leading_char = FALSE;
1574*56bb7041Schristos 	}
1575*56bb7041Schristos 
1576*56bb7041Schristos       /* Remove leading char.  */
1577*56bb7041Schristos       if (rem_leading_char)
1578*56bb7041Schristos 	bfd_set_asymbol_name (sym, ++name);
1579*56bb7041Schristos 
1580*56bb7041Schristos       /* Add new leading char and/or prefix.  */
1581*56bb7041Schristos       if (add_leading_char || prefix_symbols_string)
1582*56bb7041Schristos 	{
1583*56bb7041Schristos 	  char *n, *ptr;
1584*56bb7041Schristos 	  size_t len = strlen (name) + 1;
1585*56bb7041Schristos 
1586*56bb7041Schristos 	  if (add_leading_char)
1587*56bb7041Schristos 	    len++;
1588*56bb7041Schristos 	  if (prefix_symbols_string)
1589*56bb7041Schristos 	    len += strlen (prefix_symbols_string);
1590*56bb7041Schristos 
1591*56bb7041Schristos 	  ptr = n = (char *) xmalloc (len);
1592*56bb7041Schristos 	  if (add_leading_char)
1593*56bb7041Schristos 	    *ptr++ = bfd_get_symbol_leading_char (obfd);
1594*56bb7041Schristos 
1595*56bb7041Schristos 	  if (prefix_symbols_string)
1596*56bb7041Schristos 	    {
1597*56bb7041Schristos 	      strcpy (ptr, prefix_symbols_string);
1598*56bb7041Schristos 	      ptr += strlen (prefix_symbols_string);
1599*56bb7041Schristos 	    }
1600*56bb7041Schristos 
1601*56bb7041Schristos 	  strcpy (ptr, name);
1602*56bb7041Schristos 	  bfd_set_asymbol_name (sym, n);
1603*56bb7041Schristos 	  name = n;
1604*56bb7041Schristos 	}
1605*56bb7041Schristos 
1606*56bb7041Schristos       if (strip_symbols == STRIP_ALL)
1607*56bb7041Schristos 	keep = FALSE;
1608*56bb7041Schristos       else if ((flags & BSF_KEEP) != 0		/* Used in relocation.  */
1609*56bb7041Schristos 	       || ((flags & BSF_SECTION_SYM) != 0
1610*56bb7041Schristos 		   && ((*bfd_asymbol_section (sym)->symbol_ptr_ptr)->flags
1611*56bb7041Schristos 		       & BSF_KEEP) != 0))
1612*56bb7041Schristos 	{
1613*56bb7041Schristos 	  keep = TRUE;
1614*56bb7041Schristos 	  used_in_reloc = TRUE;
1615*56bb7041Schristos 	}
1616*56bb7041Schristos       else if (relocatable			/* Relocatable file.  */
1617*56bb7041Schristos 	       && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1618*56bb7041Schristos 		   || bfd_is_com_section (bfd_asymbol_section (sym))))
1619*56bb7041Schristos 	keep = TRUE;
1620*56bb7041Schristos       else if (bfd_decode_symclass (sym) == 'I')
1621*56bb7041Schristos 	/* Global symbols in $idata sections need to be retained
1622*56bb7041Schristos 	   even if relocatable is FALSE.  External users of the
1623*56bb7041Schristos 	   library containing the $idata section may reference these
1624*56bb7041Schristos 	   symbols.  */
1625*56bb7041Schristos 	keep = TRUE;
1626*56bb7041Schristos       else if ((flags & BSF_GLOBAL) != 0	/* Global symbol.  */
1627*56bb7041Schristos 	       || (flags & BSF_WEAK) != 0
1628*56bb7041Schristos 	       || undefined
1629*56bb7041Schristos 	       || bfd_is_com_section (bfd_asymbol_section (sym)))
1630*56bb7041Schristos 	keep = strip_symbols != STRIP_UNNEEDED;
1631*56bb7041Schristos       else if ((flags & BSF_DEBUGGING) != 0)	/* Debugging symbol.  */
1632*56bb7041Schristos 	keep = (strip_symbols != STRIP_DEBUG
1633*56bb7041Schristos 		&& strip_symbols != STRIP_UNNEEDED
1634*56bb7041Schristos 		&& ! convert_debugging);
1635*56bb7041Schristos       else if (bfd_coff_get_comdat_section (abfd, bfd_asymbol_section (sym)))
1636*56bb7041Schristos 	/* COMDAT sections store special information in local
1637*56bb7041Schristos 	   symbols, so we cannot risk stripping any of them.  */
1638*56bb7041Schristos 	keep = TRUE;
1639*56bb7041Schristos       else			/* Local symbol.  */
1640*56bb7041Schristos 	keep = (strip_symbols != STRIP_UNNEEDED
1641*56bb7041Schristos 		&& (discard_locals != LOCALS_ALL
1642*56bb7041Schristos 		    && (discard_locals != LOCALS_START_L
1643*56bb7041Schristos 			|| ! bfd_is_local_label (abfd, sym))));
1644*56bb7041Schristos 
1645*56bb7041Schristos       if (keep && is_specified_symbol (name, strip_specific_htab))
1646*56bb7041Schristos 	{
1647*56bb7041Schristos 	  /* There are multiple ways to set 'keep' above, but if it
1648*56bb7041Schristos 	     was the relocatable symbol case, then that's an error.  */
1649*56bb7041Schristos 	  if (used_in_reloc)
1650*56bb7041Schristos 	    {
1651*56bb7041Schristos 	      non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1652*56bb7041Schristos 	      status = 1;
1653*56bb7041Schristos 	    }
1654*56bb7041Schristos 	  else
1655*56bb7041Schristos 	    keep = FALSE;
1656*56bb7041Schristos 	}
1657*56bb7041Schristos 
1658*56bb7041Schristos       if (keep
1659*56bb7041Schristos 	  && !(flags & BSF_KEEP)
1660*56bb7041Schristos 	  && is_specified_symbol (name, strip_unneeded_htab))
1661*56bb7041Schristos 	keep = FALSE;
1662*56bb7041Schristos 
1663*56bb7041Schristos       if (!keep
1664*56bb7041Schristos 	  && ((keep_file_symbols && (flags & BSF_FILE))
1665*56bb7041Schristos 	      || is_specified_symbol (name, keep_specific_htab)))
1666*56bb7041Schristos 	keep = TRUE;
1667*56bb7041Schristos 
1668*56bb7041Schristos       if (keep && is_strip_section (abfd, bfd_asymbol_section (sym)))
1669*56bb7041Schristos 	keep = FALSE;
1670*56bb7041Schristos 
1671*56bb7041Schristos       if (keep)
1672*56bb7041Schristos 	{
1673*56bb7041Schristos 	  if ((flags & BSF_GLOBAL) != 0
1674*56bb7041Schristos 	      && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1675*56bb7041Schristos 	    {
1676*56bb7041Schristos 	      sym->flags &= ~ BSF_GLOBAL;
1677*56bb7041Schristos 	      sym->flags |= BSF_WEAK;
1678*56bb7041Schristos 	    }
1679*56bb7041Schristos 
1680*56bb7041Schristos 	  if (!undefined
1681*56bb7041Schristos 	      && (flags & (BSF_GLOBAL | BSF_WEAK))
1682*56bb7041Schristos 	      && (is_specified_symbol (name, localize_specific_htab)
1683*56bb7041Schristos 		  || (htab_elements (keepglobal_specific_htab) != 0
1684*56bb7041Schristos 		      && ! is_specified_symbol (name, keepglobal_specific_htab))
1685*56bb7041Schristos 		  || (localize_hidden && is_hidden_symbol (sym))))
1686*56bb7041Schristos 	    {
1687*56bb7041Schristos 	      sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1688*56bb7041Schristos 	      sym->flags |= BSF_LOCAL;
1689*56bb7041Schristos 	    }
1690*56bb7041Schristos 
1691*56bb7041Schristos 	  if (!undefined
1692*56bb7041Schristos 	      && (flags & BSF_LOCAL)
1693*56bb7041Schristos 	      && is_specified_symbol (name, globalize_specific_htab))
1694*56bb7041Schristos 	    {
1695*56bb7041Schristos 	      sym->flags &= ~ BSF_LOCAL;
1696*56bb7041Schristos 	      sym->flags |= BSF_GLOBAL;
1697*56bb7041Schristos 	    }
1698*56bb7041Schristos 
1699*56bb7041Schristos 	  to[dst_count++] = sym;
1700*56bb7041Schristos 	}
1701*56bb7041Schristos     }
1702*56bb7041Schristos   if (add_sym_list)
1703*56bb7041Schristos     {
1704*56bb7041Schristos       struct addsym_node *ptr = add_sym_list;
1705*56bb7041Schristos 
1706*56bb7041Schristos       for (src_count = 0; src_count < add_symbols; src_count++)
1707*56bb7041Schristos 	{
1708*56bb7041Schristos 	  if (ptr->othersym)
1709*56bb7041Schristos 	    {
1710*56bb7041Schristos 	      if (ptr->othersym != empty_name)
1711*56bb7041Schristos 		fatal (_("'before=%s' not found"), ptr->othersym);
1712*56bb7041Schristos 	    }
1713*56bb7041Schristos 	  else
1714*56bb7041Schristos 	    to[dst_count++] = create_new_symbol (ptr, obfd);
1715*56bb7041Schristos 
1716*56bb7041Schristos 	  ptr = ptr->next;
1717*56bb7041Schristos 	}
1718*56bb7041Schristos     }
1719*56bb7041Schristos 
1720*56bb7041Schristos   to[dst_count] = NULL;
1721*56bb7041Schristos 
1722*56bb7041Schristos   return dst_count;
1723*56bb7041Schristos }
1724*56bb7041Schristos 
1725*56bb7041Schristos /* Find the redefined name of symbol SOURCE.  */
1726*56bb7041Schristos 
1727*56bb7041Schristos static const char *
lookup_sym_redefinition(const char * source)1728*56bb7041Schristos lookup_sym_redefinition (const char *source)
1729*56bb7041Schristos {
1730*56bb7041Schristos   struct redefine_node key_node = {(char *) source, NULL};
1731*56bb7041Schristos   struct redefine_node *redef_node
1732*56bb7041Schristos     = (struct redefine_node *) htab_find (redefine_specific_htab, &key_node);
1733*56bb7041Schristos 
1734*56bb7041Schristos   return redef_node == NULL ? source : redef_node->target;
1735*56bb7041Schristos }
1736*56bb7041Schristos 
1737*56bb7041Schristos /* Insert a node into symbol redefine hash tabel.  */
1738*56bb7041Schristos 
1739*56bb7041Schristos static void
add_redefine_and_check(const char * cause,const char * source,const char * target)1740*56bb7041Schristos add_redefine_and_check (const char *cause, const char *source,
1741*56bb7041Schristos 			const char *target)
1742*56bb7041Schristos {
1743*56bb7041Schristos   struct redefine_node *new_node
1744*56bb7041Schristos     = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1745*56bb7041Schristos 
1746*56bb7041Schristos   new_node->source = strdup (source);
1747*56bb7041Schristos   new_node->target = strdup (target);
1748*56bb7041Schristos 
1749*56bb7041Schristos   if (htab_find (redefine_specific_htab, new_node) != HTAB_EMPTY_ENTRY)
1750*56bb7041Schristos     fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1751*56bb7041Schristos 	   cause, source);
1752*56bb7041Schristos 
1753*56bb7041Schristos   if (htab_find (redefine_specific_reverse_htab, target) != HTAB_EMPTY_ENTRY)
1754*56bb7041Schristos     fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1755*56bb7041Schristos 	   cause, target);
1756*56bb7041Schristos 
1757*56bb7041Schristos   /* Insert the NEW_NODE into hash table for quick search.  */
1758*56bb7041Schristos   add_specific_symbol_node (new_node, redefine_specific_htab);
1759*56bb7041Schristos 
1760*56bb7041Schristos   /* Insert the target string into the reverse hash table, this is needed for
1761*56bb7041Schristos      duplicated target string check.  */
1762*56bb7041Schristos   add_specific_symbol (new_node->target, redefine_specific_reverse_htab);
1763*56bb7041Schristos 
1764*56bb7041Schristos }
1765*56bb7041Schristos 
1766*56bb7041Schristos /* Handle the --redefine-syms option.  Read lines containing "old new"
1767*56bb7041Schristos    from the file, and add them to the symbol redefine list.  */
1768*56bb7041Schristos 
1769*56bb7041Schristos static void
add_redefine_syms_file(const char * filename)1770*56bb7041Schristos add_redefine_syms_file (const char *filename)
1771*56bb7041Schristos {
1772*56bb7041Schristos   FILE *file;
1773*56bb7041Schristos   char *buf;
1774*56bb7041Schristos   size_t bufsize;
1775*56bb7041Schristos   size_t len;
1776*56bb7041Schristos   size_t outsym_off;
1777*56bb7041Schristos   int c, lineno;
1778*56bb7041Schristos 
1779*56bb7041Schristos   file = fopen (filename, "r");
1780*56bb7041Schristos   if (file == NULL)
1781*56bb7041Schristos     fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1782*56bb7041Schristos 	   filename, strerror (errno));
1783*56bb7041Schristos 
1784*56bb7041Schristos   bufsize = 100;
1785*56bb7041Schristos   buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL.  */);
1786*56bb7041Schristos 
1787*56bb7041Schristos   lineno = 1;
1788*56bb7041Schristos   c = getc (file);
1789*56bb7041Schristos   len = 0;
1790*56bb7041Schristos   outsym_off = 0;
1791*56bb7041Schristos   while (c != EOF)
1792*56bb7041Schristos     {
1793*56bb7041Schristos       /* Collect the input symbol name.  */
1794*56bb7041Schristos       while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1795*56bb7041Schristos 	{
1796*56bb7041Schristos 	  if (c == '#')
1797*56bb7041Schristos 	    goto comment;
1798*56bb7041Schristos 	  buf[len++] = c;
1799*56bb7041Schristos 	  if (len >= bufsize)
1800*56bb7041Schristos 	    {
1801*56bb7041Schristos 	      bufsize *= 2;
1802*56bb7041Schristos 	      buf = (char *) xrealloc (buf, bufsize + 1);
1803*56bb7041Schristos 	    }
1804*56bb7041Schristos 	  c = getc (file);
1805*56bb7041Schristos 	}
1806*56bb7041Schristos       buf[len++] = '\0';
1807*56bb7041Schristos       if (c == EOF)
1808*56bb7041Schristos 	break;
1809*56bb7041Schristos 
1810*56bb7041Schristos       /* Eat white space between the symbol names.  */
1811*56bb7041Schristos       while (IS_WHITESPACE (c))
1812*56bb7041Schristos 	c = getc (file);
1813*56bb7041Schristos       if (c == '#' || IS_LINE_TERMINATOR (c))
1814*56bb7041Schristos 	goto comment;
1815*56bb7041Schristos       if (c == EOF)
1816*56bb7041Schristos 	break;
1817*56bb7041Schristos 
1818*56bb7041Schristos       /* Collect the output symbol name.  */
1819*56bb7041Schristos       outsym_off = len;
1820*56bb7041Schristos       while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1821*56bb7041Schristos 	{
1822*56bb7041Schristos 	  if (c == '#')
1823*56bb7041Schristos 	    goto comment;
1824*56bb7041Schristos 	  buf[len++] = c;
1825*56bb7041Schristos 	  if (len >= bufsize)
1826*56bb7041Schristos 	    {
1827*56bb7041Schristos 	      bufsize *= 2;
1828*56bb7041Schristos 	      buf = (char *) xrealloc (buf, bufsize + 1);
1829*56bb7041Schristos 	    }
1830*56bb7041Schristos 	  c = getc (file);
1831*56bb7041Schristos 	}
1832*56bb7041Schristos       buf[len++] = '\0';
1833*56bb7041Schristos       if (c == EOF)
1834*56bb7041Schristos 	break;
1835*56bb7041Schristos 
1836*56bb7041Schristos       /* Eat white space at end of line.  */
1837*56bb7041Schristos       while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1838*56bb7041Schristos 	c = getc (file);
1839*56bb7041Schristos       if (c == '#')
1840*56bb7041Schristos 	goto comment;
1841*56bb7041Schristos       /* Handle \r\n.  */
1842*56bb7041Schristos       if ((c == '\r' && (c = getc (file)) == '\n')
1843*56bb7041Schristos 	  || c == '\n' || c == EOF)
1844*56bb7041Schristos 	{
1845*56bb7041Schristos 	end_of_line:
1846*56bb7041Schristos 	  /* Append the redefinition to the list.  */
1847*56bb7041Schristos 	  if (buf[0] != '\0')
1848*56bb7041Schristos 	    add_redefine_and_check (filename, &buf[0], &buf[outsym_off]);
1849*56bb7041Schristos 
1850*56bb7041Schristos 	  lineno++;
1851*56bb7041Schristos 	  len = 0;
1852*56bb7041Schristos 	  outsym_off = 0;
1853*56bb7041Schristos 	  if (c == EOF)
1854*56bb7041Schristos 	    break;
1855*56bb7041Schristos 	  c = getc (file);
1856*56bb7041Schristos 	  continue;
1857*56bb7041Schristos 	}
1858*56bb7041Schristos       else
1859*56bb7041Schristos 	fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1860*56bb7041Schristos     comment:
1861*56bb7041Schristos       if (len != 0 && (outsym_off == 0 || outsym_off == len))
1862*56bb7041Schristos 	fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1863*56bb7041Schristos       buf[len++] = '\0';
1864*56bb7041Schristos 
1865*56bb7041Schristos       /* Eat the rest of the line and finish it.  */
1866*56bb7041Schristos       while (c != '\n' && c != EOF)
1867*56bb7041Schristos 	c = getc (file);
1868*56bb7041Schristos       goto end_of_line;
1869*56bb7041Schristos     }
1870*56bb7041Schristos 
1871*56bb7041Schristos   if (len != 0)
1872*56bb7041Schristos     fatal (_("%s:%d: premature end of file"), filename, lineno);
1873*56bb7041Schristos 
1874*56bb7041Schristos   free (buf);
1875*56bb7041Schristos   fclose (file);
1876*56bb7041Schristos }
1877*56bb7041Schristos 
1878*56bb7041Schristos /* Copy unknown object file IBFD onto OBFD.
1879*56bb7041Schristos    Returns TRUE upon success, FALSE otherwise.  */
1880*56bb7041Schristos 
1881*56bb7041Schristos static bfd_boolean
copy_unknown_object(bfd * ibfd,bfd * obfd)1882*56bb7041Schristos copy_unknown_object (bfd *ibfd, bfd *obfd)
1883*56bb7041Schristos {
1884*56bb7041Schristos   char *cbuf;
1885*56bb7041Schristos   int tocopy;
1886*56bb7041Schristos   long ncopied;
1887*56bb7041Schristos   long size;
1888*56bb7041Schristos   struct stat buf;
1889*56bb7041Schristos 
1890*56bb7041Schristos   if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1891*56bb7041Schristos     {
1892*56bb7041Schristos       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1893*56bb7041Schristos       return FALSE;
1894*56bb7041Schristos     }
1895*56bb7041Schristos 
1896*56bb7041Schristos   size = buf.st_size;
1897*56bb7041Schristos   if (size < 0)
1898*56bb7041Schristos     {
1899*56bb7041Schristos       non_fatal (_("stat returns negative size for `%s'"),
1900*56bb7041Schristos 		 bfd_get_archive_filename (ibfd));
1901*56bb7041Schristos       return FALSE;
1902*56bb7041Schristos     }
1903*56bb7041Schristos 
1904*56bb7041Schristos   if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1905*56bb7041Schristos     {
1906*56bb7041Schristos       bfd_nonfatal (bfd_get_archive_filename (ibfd));
1907*56bb7041Schristos       return FALSE;
1908*56bb7041Schristos     }
1909*56bb7041Schristos 
1910*56bb7041Schristos   if (verbose)
1911*56bb7041Schristos     printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1912*56bb7041Schristos 	    bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1913*56bb7041Schristos 
1914*56bb7041Schristos   cbuf = (char *) xmalloc (BUFSIZE);
1915*56bb7041Schristos   ncopied = 0;
1916*56bb7041Schristos   while (ncopied < size)
1917*56bb7041Schristos     {
1918*56bb7041Schristos       tocopy = size - ncopied;
1919*56bb7041Schristos       if (tocopy > BUFSIZE)
1920*56bb7041Schristos 	tocopy = BUFSIZE;
1921*56bb7041Schristos 
1922*56bb7041Schristos       if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1923*56bb7041Schristos 	  != (bfd_size_type) tocopy)
1924*56bb7041Schristos 	{
1925*56bb7041Schristos 	  bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1926*56bb7041Schristos 	  free (cbuf);
1927*56bb7041Schristos 	  return FALSE;
1928*56bb7041Schristos 	}
1929*56bb7041Schristos 
1930*56bb7041Schristos       if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1931*56bb7041Schristos 	  != (bfd_size_type) tocopy)
1932*56bb7041Schristos 	{
1933*56bb7041Schristos 	  bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1934*56bb7041Schristos 	  free (cbuf);
1935*56bb7041Schristos 	  return FALSE;
1936*56bb7041Schristos 	}
1937*56bb7041Schristos 
1938*56bb7041Schristos       ncopied += tocopy;
1939*56bb7041Schristos     }
1940*56bb7041Schristos 
1941*56bb7041Schristos   /* We should at least to be able to read it back when copying an
1942*56bb7041Schristos      unknown object in an archive.  */
1943*56bb7041Schristos   chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
1944*56bb7041Schristos   free (cbuf);
1945*56bb7041Schristos   return TRUE;
1946*56bb7041Schristos }
1947*56bb7041Schristos 
1948*56bb7041Schristos typedef struct objcopy_internal_note
1949*56bb7041Schristos {
1950*56bb7041Schristos   Elf_Internal_Note  note;
1951*56bb7041Schristos   unsigned long      padded_namesz;
1952*56bb7041Schristos   bfd_vma            start;
1953*56bb7041Schristos   bfd_vma            end;
1954*56bb7041Schristos } objcopy_internal_note;
1955*56bb7041Schristos 
1956*56bb7041Schristos #define DEBUG_MERGE 0
1957*56bb7041Schristos 
1958*56bb7041Schristos #if DEBUG_MERGE
1959*56bb7041Schristos #define merge_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
1960*56bb7041Schristos #else
1961*56bb7041Schristos #define merge_debug(format, ...)
1962*56bb7041Schristos #endif
1963*56bb7041Schristos 
1964*56bb7041Schristos /* Returns TRUE iff PNOTE1 overlaps or adjoins PNOTE2.  */
1965*56bb7041Schristos 
1966*56bb7041Schristos static bfd_boolean
overlaps_or_adjoins(objcopy_internal_note * pnote1,objcopy_internal_note * pnote2)1967*56bb7041Schristos overlaps_or_adjoins (objcopy_internal_note * pnote1,
1968*56bb7041Schristos 		     objcopy_internal_note * pnote2)
1969*56bb7041Schristos {
1970*56bb7041Schristos   if (pnote1->end < pnote2->start)
1971*56bb7041Schristos     /* FIXME: Alignment of 16 bytes taken from x86_64 binaries.
1972*56bb7041Schristos        Really we should extract the alignment of the section
1973*56bb7041Schristos        covered by the notes.  */
1974*56bb7041Schristos     return BFD_ALIGN (pnote1->end, 16) < pnote2->start;
1975*56bb7041Schristos 
1976*56bb7041Schristos   if (pnote2->end < pnote2->start)
1977*56bb7041Schristos     return BFD_ALIGN (pnote2->end, 16) < pnote1->start;
1978*56bb7041Schristos 
1979*56bb7041Schristos   if (pnote1->end < pnote2->end)
1980*56bb7041Schristos     return TRUE;
1981*56bb7041Schristos 
1982*56bb7041Schristos   if (pnote2->end < pnote1->end)
1983*56bb7041Schristos     return TRUE;
1984*56bb7041Schristos 
1985*56bb7041Schristos   return FALSE;
1986*56bb7041Schristos }
1987*56bb7041Schristos 
1988*56bb7041Schristos /* Returns TRUE iff NEEDLE is fully contained by HAYSTACK.  */
1989*56bb7041Schristos 
1990*56bb7041Schristos static bfd_boolean
contained_by(objcopy_internal_note * needle,objcopy_internal_note * haystack)1991*56bb7041Schristos contained_by (objcopy_internal_note * needle,
1992*56bb7041Schristos 	      objcopy_internal_note * haystack)
1993*56bb7041Schristos {
1994*56bb7041Schristos   return needle->start >= haystack->start && needle->end <= haystack->end;
1995*56bb7041Schristos }
1996*56bb7041Schristos 
1997*56bb7041Schristos static bfd_boolean
is_open_note(objcopy_internal_note * pnote)1998*56bb7041Schristos is_open_note (objcopy_internal_note * pnote)
1999*56bb7041Schristos {
2000*56bb7041Schristos   return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
2001*56bb7041Schristos }
2002*56bb7041Schristos 
2003*56bb7041Schristos static bfd_boolean
is_func_note(objcopy_internal_note * pnote)2004*56bb7041Schristos is_func_note (objcopy_internal_note * pnote)
2005*56bb7041Schristos {
2006*56bb7041Schristos   return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_FUNC;
2007*56bb7041Schristos }
2008*56bb7041Schristos 
2009*56bb7041Schristos static bfd_boolean
is_deleted_note(objcopy_internal_note * pnote)2010*56bb7041Schristos is_deleted_note (objcopy_internal_note * pnote)
2011*56bb7041Schristos {
2012*56bb7041Schristos   return pnote->note.type == 0;
2013*56bb7041Schristos }
2014*56bb7041Schristos 
2015*56bb7041Schristos static bfd_boolean
is_version_note(objcopy_internal_note * pnote)2016*56bb7041Schristos is_version_note (objcopy_internal_note * pnote)
2017*56bb7041Schristos {
2018*56bb7041Schristos   return (pnote->note.namesz > 4
2019*56bb7041Schristos 	  && pnote->note.namedata[0] == 'G'
2020*56bb7041Schristos 	  && pnote->note.namedata[1] == 'A'
2021*56bb7041Schristos 	  && pnote->note.namedata[2] == '$'
2022*56bb7041Schristos 	  && pnote->note.namedata[3] == GNU_BUILD_ATTRIBUTE_VERSION);
2023*56bb7041Schristos }
2024*56bb7041Schristos 
2025*56bb7041Schristos static bfd_boolean
is_64bit(bfd * abfd)2026*56bb7041Schristos is_64bit (bfd * abfd)
2027*56bb7041Schristos {
2028*56bb7041Schristos   /* Should never happen, but let's be paranoid.  */
2029*56bb7041Schristos   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2030*56bb7041Schristos     return FALSE;
2031*56bb7041Schristos 
2032*56bb7041Schristos   return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64;
2033*56bb7041Schristos }
2034*56bb7041Schristos 
2035*56bb7041Schristos /* This sorting function is used to get the notes into an order
2036*56bb7041Schristos    that makes merging easy.  */
2037*56bb7041Schristos 
2038*56bb7041Schristos static int
compare_gnu_build_notes(const void * data1,const void * data2)2039*56bb7041Schristos compare_gnu_build_notes (const void * data1, const void * data2)
2040*56bb7041Schristos {
2041*56bb7041Schristos   objcopy_internal_note * pnote1 = (objcopy_internal_note *) data1;
2042*56bb7041Schristos   objcopy_internal_note * pnote2 = (objcopy_internal_note *) data2;
2043*56bb7041Schristos 
2044*56bb7041Schristos   /* Sort notes based upon the attribute they record.  */
2045*56bb7041Schristos   int cmp = memcmp (pnote1->note.namedata + 3,
2046*56bb7041Schristos 		    pnote2->note.namedata + 3,
2047*56bb7041Schristos 		    pnote1->note.namesz < pnote2->note.namesz ?
2048*56bb7041Schristos 		    pnote1->note.namesz - 3 : pnote2->note.namesz - 3);
2049*56bb7041Schristos   if (cmp)
2050*56bb7041Schristos     return cmp;
2051*56bb7041Schristos 
2052*56bb7041Schristos   if (pnote1->end < pnote2->start)
2053*56bb7041Schristos     return -1;
2054*56bb7041Schristos   if (pnote1->start > pnote2->end)
2055*56bb7041Schristos     return 1;
2056*56bb7041Schristos 
2057*56bb7041Schristos   /* Overlaps - we should merge the two ranges.  */
2058*56bb7041Schristos   if (pnote1->start < pnote2->start)
2059*56bb7041Schristos     return -1;
2060*56bb7041Schristos   if (pnote1->end > pnote2->end)
2061*56bb7041Schristos     return 1;
2062*56bb7041Schristos   if (pnote1->end < pnote2->end)
2063*56bb7041Schristos     return -1;
2064*56bb7041Schristos 
2065*56bb7041Schristos   /* Put OPEN notes before function notes.  */
2066*56bb7041Schristos   if (is_open_note (pnote1) && ! is_open_note (pnote2))
2067*56bb7041Schristos     return -1;
2068*56bb7041Schristos   if (! is_open_note (pnote1) && is_open_note (pnote2))
2069*56bb7041Schristos     return 1;
2070*56bb7041Schristos 
2071*56bb7041Schristos   return 0;
2072*56bb7041Schristos }
2073*56bb7041Schristos 
2074*56bb7041Schristos /* This sorting function is used to get the notes into an order
2075*56bb7041Schristos    that makes eliminating address ranges easier.  */
2076*56bb7041Schristos 
2077*56bb7041Schristos static int
sort_gnu_build_notes(const void * data1,const void * data2)2078*56bb7041Schristos sort_gnu_build_notes (const void * data1, const void * data2)
2079*56bb7041Schristos {
2080*56bb7041Schristos   objcopy_internal_note * pnote1 = (objcopy_internal_note *) data1;
2081*56bb7041Schristos   objcopy_internal_note * pnote2 = (objcopy_internal_note *) data2;
2082*56bb7041Schristos 
2083*56bb7041Schristos   if (pnote1->note.type != pnote2->note.type)
2084*56bb7041Schristos     {
2085*56bb7041Schristos       /* Move deleted notes to the end.  */
2086*56bb7041Schristos       if (is_deleted_note (pnote1))     /* 1: OFD 2: OFD */
2087*56bb7041Schristos 	return 1;
2088*56bb7041Schristos 
2089*56bb7041Schristos       /* Move OPEN notes to the start.  */
2090*56bb7041Schristos       if (is_open_note (pnote1))	/* 1: OF  2: OFD */
2091*56bb7041Schristos 	return -1;
2092*56bb7041Schristos 
2093*56bb7041Schristos       if (is_deleted_note (pnote2))	/* 1: F   2: O D */
2094*56bb7041Schristos 	return -1;
2095*56bb7041Schristos 
2096*56bb7041Schristos       return 1;				/* 1: F   2: O   */
2097*56bb7041Schristos     }
2098*56bb7041Schristos 
2099*56bb7041Schristos   /* Sort by starting address.  */
2100*56bb7041Schristos   if (pnote1->start < pnote2->start)
2101*56bb7041Schristos     return -1;
2102*56bb7041Schristos   if (pnote1->start > pnote2->start)
2103*56bb7041Schristos     return 1;
2104*56bb7041Schristos 
2105*56bb7041Schristos   /* Then by end address (bigger range first).  */
2106*56bb7041Schristos   if (pnote1->end > pnote2->end)
2107*56bb7041Schristos     return -1;
2108*56bb7041Schristos   if (pnote1->end < pnote2->end)
2109*56bb7041Schristos     return 1;
2110*56bb7041Schristos 
2111*56bb7041Schristos   /* Then by attribute type.  */
2112*56bb7041Schristos   if (pnote1->note.namesz > 4
2113*56bb7041Schristos       && pnote2->note.namesz > 4
2114*56bb7041Schristos       && pnote1->note.namedata[3] != pnote2->note.namedata[3])
2115*56bb7041Schristos     return pnote1->note.namedata[3] - pnote2->note.namedata[3];
2116*56bb7041Schristos 
2117*56bb7041Schristos   return 0;
2118*56bb7041Schristos }
2119*56bb7041Schristos 
2120*56bb7041Schristos /* Merge the notes on SEC, removing redundant entries.
2121*56bb7041Schristos    Returns the new, smaller size of the section upon success.  */
2122*56bb7041Schristos 
2123*56bb7041Schristos static bfd_size_type
merge_gnu_build_notes(bfd * abfd,asection * sec,bfd_size_type size,bfd_byte * contents)2124*56bb7041Schristos merge_gnu_build_notes (bfd *          abfd,
2125*56bb7041Schristos 		       asection *     sec,
2126*56bb7041Schristos 		       bfd_size_type  size,
2127*56bb7041Schristos 		       bfd_byte *     contents)
2128*56bb7041Schristos {
2129*56bb7041Schristos   objcopy_internal_note *  pnotes_end;
2130*56bb7041Schristos   objcopy_internal_note *  pnotes = NULL;
2131*56bb7041Schristos   objcopy_internal_note *  pnote;
2132*56bb7041Schristos   bfd_size_type       remain = size;
2133*56bb7041Schristos   unsigned            version_1_seen = 0;
2134*56bb7041Schristos   unsigned            version_2_seen = 0;
2135*56bb7041Schristos   unsigned            version_3_seen = 0;
2136*56bb7041Schristos   const char *        err = NULL;
2137*56bb7041Schristos   bfd_byte *          in = contents;
2138*56bb7041Schristos   unsigned long       previous_func_start = 0;
2139*56bb7041Schristos   unsigned long       previous_open_start = 0;
2140*56bb7041Schristos   unsigned long       previous_func_end = 0;
2141*56bb7041Schristos   unsigned long       previous_open_end = 0;
2142*56bb7041Schristos   long                relsize;
2143*56bb7041Schristos 
2144*56bb7041Schristos   relsize = bfd_get_reloc_upper_bound (abfd, sec);
2145*56bb7041Schristos   if (relsize > 0)
2146*56bb7041Schristos     {
2147*56bb7041Schristos       arelent **  relpp;
2148*56bb7041Schristos       long        relcount;
2149*56bb7041Schristos 
2150*56bb7041Schristos       /* If there are relocs associated with this section then we
2151*56bb7041Schristos 	 cannot safely merge it.  */
2152*56bb7041Schristos       relpp = (arelent **) xmalloc (relsize);
2153*56bb7041Schristos       relcount = bfd_canonicalize_reloc (abfd, sec, relpp, isympp);
2154*56bb7041Schristos       free (relpp);
2155*56bb7041Schristos       if (relcount != 0)
2156*56bb7041Schristos 	{
2157*56bb7041Schristos 	  if (! is_strip)
2158*56bb7041Schristos 	    non_fatal (_("%s[%s]: Cannot merge - there are relocations against this section"),
2159*56bb7041Schristos 		       bfd_get_filename (abfd), bfd_section_name (sec));
2160*56bb7041Schristos 	  goto done;
2161*56bb7041Schristos 	}
2162*56bb7041Schristos     }
2163*56bb7041Schristos 
2164*56bb7041Schristos   /* Make a copy of the notes and convert to our internal format.
2165*56bb7041Schristos      Minimum size of a note is 12 bytes.  Also locate the version
2166*56bb7041Schristos      notes and check them.  */
2167*56bb7041Schristos   pnote = pnotes = (objcopy_internal_note *)
2168*56bb7041Schristos     xcalloc ((size / 12), sizeof (* pnote));
2169*56bb7041Schristos   while (remain >= 12)
2170*56bb7041Schristos     {
2171*56bb7041Schristos       bfd_vma start, end;
2172*56bb7041Schristos 
2173*56bb7041Schristos       pnote->note.namesz   = bfd_get_32 (abfd, in);
2174*56bb7041Schristos       pnote->note.descsz   = bfd_get_32 (abfd, in + 4);
2175*56bb7041Schristos       pnote->note.type     = bfd_get_32 (abfd, in + 8);
2176*56bb7041Schristos       pnote->padded_namesz = (pnote->note.namesz + 3) & ~3;
2177*56bb7041Schristos 
2178*56bb7041Schristos       if (((pnote->note.descsz + 3) & ~3) != pnote->note.descsz)
2179*56bb7041Schristos 	{
2180*56bb7041Schristos 	  err = _("corrupt GNU build attribute note: description size not a factor of 4");
2181*56bb7041Schristos 	  goto done;
2182*56bb7041Schristos 	}
2183*56bb7041Schristos 
2184*56bb7041Schristos       if (pnote->note.type    != NT_GNU_BUILD_ATTRIBUTE_OPEN
2185*56bb7041Schristos 	  && pnote->note.type != NT_GNU_BUILD_ATTRIBUTE_FUNC)
2186*56bb7041Schristos 	{
2187*56bb7041Schristos 	  err = _("corrupt GNU build attribute note: wrong note type");
2188*56bb7041Schristos 	  goto done;
2189*56bb7041Schristos 	}
2190*56bb7041Schristos 
2191*56bb7041Schristos       if (pnote->padded_namesz + pnote->note.descsz + 12 > remain)
2192*56bb7041Schristos 	{
2193*56bb7041Schristos 	  err = _("corrupt GNU build attribute note: note too big");
2194*56bb7041Schristos 	  goto done;
2195*56bb7041Schristos 	}
2196*56bb7041Schristos 
2197*56bb7041Schristos       if (pnote->note.namesz < 2)
2198*56bb7041Schristos 	{
2199*56bb7041Schristos 	  err = _("corrupt GNU build attribute note: name too small");
2200*56bb7041Schristos 	  goto done;
2201*56bb7041Schristos 	}
2202*56bb7041Schristos 
2203*56bb7041Schristos       pnote->note.namedata = (char *)(in + 12);
2204*56bb7041Schristos       pnote->note.descdata = (char *)(in + 12 + pnote->padded_namesz);
2205*56bb7041Schristos 
2206*56bb7041Schristos       remain -= 12 + pnote->padded_namesz + pnote->note.descsz;
2207*56bb7041Schristos       in     += 12 + pnote->padded_namesz + pnote->note.descsz;
2208*56bb7041Schristos 
2209*56bb7041Schristos       if (pnote->note.namesz > 2
2210*56bb7041Schristos 	  && pnote->note.namedata[0] == '$'
2211*56bb7041Schristos 	  && pnote->note.namedata[1] == GNU_BUILD_ATTRIBUTE_VERSION
2212*56bb7041Schristos 	  && pnote->note.namedata[2] == '1')
2213*56bb7041Schristos 	++ version_1_seen;
2214*56bb7041Schristos       else if (is_version_note (pnote))
2215*56bb7041Schristos 	{
2216*56bb7041Schristos 	  if (pnote->note.namedata[4] == '2')
2217*56bb7041Schristos 	    ++ version_2_seen;
2218*56bb7041Schristos 	  else if (pnote->note.namedata[4] == '3')
2219*56bb7041Schristos 	    ++ version_3_seen;
2220*56bb7041Schristos 	  else
2221*56bb7041Schristos 	    {
2222*56bb7041Schristos 	      err = _("corrupt GNU build attribute note: unsupported version");
2223*56bb7041Schristos 	      goto done;
2224*56bb7041Schristos 	    }
2225*56bb7041Schristos 	}
2226*56bb7041Schristos 
2227*56bb7041Schristos       switch (pnote->note.descsz)
2228*56bb7041Schristos 	{
2229*56bb7041Schristos 	case 0:
2230*56bb7041Schristos 	  start = end = 0;
2231*56bb7041Schristos 	  break;
2232*56bb7041Schristos 
2233*56bb7041Schristos 	case 4:
2234*56bb7041Schristos 	  start = bfd_get_32 (abfd, pnote->note.descdata);
2235*56bb7041Schristos 	  /* FIXME: For version 1 and 2 notes we should try to
2236*56bb7041Schristos 	     calculate the end address by finding a symbol whose
2237*56bb7041Schristos 	     value is START, and then adding in its size.
2238*56bb7041Schristos 
2239*56bb7041Schristos 	     For now though, since v1 and v2 was not intended to
2240*56bb7041Schristos 	     handle gaps, we chose an artificially large end
2241*56bb7041Schristos 	     address.  */
2242*56bb7041Schristos 	  end = (bfd_vma) -1;
2243*56bb7041Schristos 	  break;
2244*56bb7041Schristos 
2245*56bb7041Schristos 	case 8:
2246*56bb7041Schristos 	  if (! is_64bit (abfd))
2247*56bb7041Schristos 	    {
2248*56bb7041Schristos 	      start = bfd_get_32 (abfd, pnote->note.descdata);
2249*56bb7041Schristos 	      end = bfd_get_32 (abfd, pnote->note.descdata + 4);
2250*56bb7041Schristos 	    }
2251*56bb7041Schristos 	  else
2252*56bb7041Schristos 	    {
2253*56bb7041Schristos 	      start = bfd_get_64 (abfd, pnote->note.descdata);
2254*56bb7041Schristos 	      /* FIXME: For version 1 and 2 notes we should try to
2255*56bb7041Schristos 		 calculate the end address by finding a symbol whose
2256*56bb7041Schristos 		 value is START, and then adding in its size.
2257*56bb7041Schristos 
2258*56bb7041Schristos 		 For now though, since v1 and v2 was not intended to
2259*56bb7041Schristos 		 handle gaps, we chose an artificially large end
2260*56bb7041Schristos 		 address.  */
2261*56bb7041Schristos 	      end = (bfd_vma) -1;
2262*56bb7041Schristos 	    }
2263*56bb7041Schristos 	  break;
2264*56bb7041Schristos 
2265*56bb7041Schristos 	case 16:
2266*56bb7041Schristos 	  start = bfd_get_64 (abfd, pnote->note.descdata);
2267*56bb7041Schristos 	  end = bfd_get_64 (abfd, pnote->note.descdata + 8);
2268*56bb7041Schristos 	  break;
2269*56bb7041Schristos 
2270*56bb7041Schristos 	default:
2271*56bb7041Schristos 	  err = _("corrupt GNU build attribute note: bad description size");
2272*56bb7041Schristos 	  goto done;
2273*56bb7041Schristos 	}
2274*56bb7041Schristos 
2275*56bb7041Schristos       if (is_open_note (pnote))
2276*56bb7041Schristos 	{
2277*56bb7041Schristos 	  if (start)
2278*56bb7041Schristos 	    previous_open_start = start;
2279*56bb7041Schristos 
2280*56bb7041Schristos 	  pnote->start = previous_open_start;
2281*56bb7041Schristos 
2282*56bb7041Schristos 	  if (end)
2283*56bb7041Schristos 	    previous_open_end = end;
2284*56bb7041Schristos 
2285*56bb7041Schristos 	  pnote->end = previous_open_end;
2286*56bb7041Schristos 	}
2287*56bb7041Schristos       else
2288*56bb7041Schristos 	{
2289*56bb7041Schristos 	  if (start)
2290*56bb7041Schristos 	    previous_func_start = start;
2291*56bb7041Schristos 
2292*56bb7041Schristos 	  pnote->start = previous_func_start;
2293*56bb7041Schristos 
2294*56bb7041Schristos 	  if (end)
2295*56bb7041Schristos 	    previous_func_end = end;
2296*56bb7041Schristos 
2297*56bb7041Schristos 	  pnote->end = previous_func_end;
2298*56bb7041Schristos 	}
2299*56bb7041Schristos 
2300*56bb7041Schristos       if (pnote->note.namedata[pnote->note.namesz - 1] != 0)
2301*56bb7041Schristos 	{
2302*56bb7041Schristos 	  err = _("corrupt GNU build attribute note: name not NUL terminated");
2303*56bb7041Schristos 	  goto done;
2304*56bb7041Schristos 	}
2305*56bb7041Schristos 
2306*56bb7041Schristos       pnote ++;
2307*56bb7041Schristos     }
2308*56bb7041Schristos 
2309*56bb7041Schristos   pnotes_end = pnote;
2310*56bb7041Schristos 
2311*56bb7041Schristos   /* Check that the notes are valid.  */
2312*56bb7041Schristos   if (remain != 0)
2313*56bb7041Schristos     {
2314*56bb7041Schristos       err = _("corrupt GNU build attribute notes: excess data at end");
2315*56bb7041Schristos       goto done;
2316*56bb7041Schristos     }
2317*56bb7041Schristos 
2318*56bb7041Schristos   if (version_1_seen == 0 && version_2_seen == 0 && version_3_seen == 0)
2319*56bb7041Schristos     {
2320*56bb7041Schristos #if 0
2321*56bb7041Schristos       err = _("bad GNU build attribute notes: no known versions detected");
2322*56bb7041Schristos       goto done;
2323*56bb7041Schristos #else
2324*56bb7041Schristos       /* This happens with glibc.  No idea why.  */
2325*56bb7041Schristos       non_fatal (_("%s[%s]: Warning: version note missing - assuming version 3"),
2326*56bb7041Schristos 		 bfd_get_filename (abfd), bfd_section_name (sec));
2327*56bb7041Schristos       version_3_seen = 2;
2328*56bb7041Schristos #endif
2329*56bb7041Schristos     }
2330*56bb7041Schristos 
2331*56bb7041Schristos   if (   (version_1_seen > 0 && version_2_seen > 0)
2332*56bb7041Schristos       || (version_1_seen > 0 && version_3_seen > 0)
2333*56bb7041Schristos       || (version_2_seen > 0 && version_3_seen > 0))
2334*56bb7041Schristos     {
2335*56bb7041Schristos       err = _("bad GNU build attribute notes: multiple different versions");
2336*56bb7041Schristos       goto done;
2337*56bb7041Schristos     }
2338*56bb7041Schristos 
2339*56bb7041Schristos   /* We are now only supporting the merging v3+ notes
2340*56bb7041Schristos      - it makes things much simpler.  */
2341*56bb7041Schristos   if (version_3_seen == 0)
2342*56bb7041Schristos     {
2343*56bb7041Schristos       merge_debug ("%s: skipping merge - not using v3 notes", bfd_section_name (sec));
2344*56bb7041Schristos       goto done;
2345*56bb7041Schristos     }
2346*56bb7041Schristos 
2347*56bb7041Schristos   merge_debug ("Merging section %s which contains %ld notes\n",
2348*56bb7041Schristos 	       sec->name, pnotes_end - pnotes);
2349*56bb7041Schristos 
2350*56bb7041Schristos   /* Sort the notes.  */
2351*56bb7041Schristos   qsort (pnotes, pnotes_end - pnotes, sizeof (* pnotes),
2352*56bb7041Schristos 	 compare_gnu_build_notes);
2353*56bb7041Schristos 
2354*56bb7041Schristos #if DEBUG_MERGE
2355*56bb7041Schristos   merge_debug ("Results of initial sort:\n");
2356*56bb7041Schristos   for (pnote = pnotes; pnote < pnotes_end; pnote ++)
2357*56bb7041Schristos     merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2358*56bb7041Schristos 		 (pnote->note.namedata - (char *) contents) - 12,
2359*56bb7041Schristos 		 pnote->start, pnote->end,
2360*56bb7041Schristos 		 pnote->note.type,
2361*56bb7041Schristos 		 pnote->note.namedata[3],
2362*56bb7041Schristos 		 pnote->note.namesz
2363*56bb7041Schristos 		 );
2364*56bb7041Schristos #endif
2365*56bb7041Schristos 
2366*56bb7041Schristos   /* Now merge the notes.  The rules are:
2367*56bb7041Schristos      1. If a note has a zero range, it can be eliminated.
2368*56bb7041Schristos      2. If two notes have the same namedata then:
2369*56bb7041Schristos         2a. If one note's range is fully covered by the other note
2370*56bb7041Schristos 	    then it can be deleted.
2371*56bb7041Schristos 	2b. If one note's range partially overlaps or adjoins the
2372*56bb7041Schristos 	    other note then if they are both of the same type (open
2373*56bb7041Schristos 	    or func) then they can be merged and one deleted.  If
2374*56bb7041Schristos 	    they are of different types then they cannot be merged.  */
2375*56bb7041Schristos   for (pnote = pnotes; pnote < pnotes_end; pnote ++)
2376*56bb7041Schristos     {
2377*56bb7041Schristos       /* Skip already deleted notes.
2378*56bb7041Schristos 	 FIXME: Can this happen ?  We are scanning forwards and
2379*56bb7041Schristos 	 deleting backwards after all.  */
2380*56bb7041Schristos       if (is_deleted_note (pnote))
2381*56bb7041Schristos 	continue;
2382*56bb7041Schristos 
2383*56bb7041Schristos       /* Rule 1 - delete 0-range notes.  */
2384*56bb7041Schristos       if (pnote->start == pnote->end)
2385*56bb7041Schristos 	{
2386*56bb7041Schristos 	  merge_debug ("Delete note at offset %#08lx - empty range\n",
2387*56bb7041Schristos 		       (pnote->note.namedata - (char *) contents) - 12);
2388*56bb7041Schristos 	  pnote->note.type = 0;
2389*56bb7041Schristos 	  continue;
2390*56bb7041Schristos 	}
2391*56bb7041Schristos 
2392*56bb7041Schristos       int iter;
2393*56bb7041Schristos       objcopy_internal_note * back;
2394*56bb7041Schristos 
2395*56bb7041Schristos       /* Rule 2: Check to see if there is an identical previous note.  */
2396*56bb7041Schristos       for (iter = 0, back = pnote - 1; back >= pnotes; back --)
2397*56bb7041Schristos 	{
2398*56bb7041Schristos 	  if (is_deleted_note (back))
2399*56bb7041Schristos 	    continue;
2400*56bb7041Schristos 
2401*56bb7041Schristos 	  /* Our sorting function should have placed all identically
2402*56bb7041Schristos 	     attributed notes together, so if we see a note of a different
2403*56bb7041Schristos 	     attribute type stop searching.  */
2404*56bb7041Schristos 	  if (back->note.namesz != pnote->note.namesz
2405*56bb7041Schristos 	      || memcmp (back->note.namedata,
2406*56bb7041Schristos 			 pnote->note.namedata, pnote->note.namesz) != 0)
2407*56bb7041Schristos 	    break;
2408*56bb7041Schristos 
2409*56bb7041Schristos 	  if (back->start == pnote->start
2410*56bb7041Schristos 	      && back->end == pnote->end)
2411*56bb7041Schristos 	    {
2412*56bb7041Schristos 	      merge_debug ("Delete note at offset %#08lx - duplicate of note at offset %#08lx\n",
2413*56bb7041Schristos 			   (pnote->note.namedata - (char *) contents) - 12,
2414*56bb7041Schristos 			   (back->note.namedata - (char *) contents) - 12);
2415*56bb7041Schristos 	      pnote->note.type = 0;
2416*56bb7041Schristos 	      break;
2417*56bb7041Schristos 	    }
2418*56bb7041Schristos 
2419*56bb7041Schristos 	  /* Rule 2a.  */
2420*56bb7041Schristos 	  if (contained_by (pnote, back))
2421*56bb7041Schristos 	    {
2422*56bb7041Schristos 	      merge_debug ("Delete note at offset %#08lx - fully contained by note at %#08lx\n",
2423*56bb7041Schristos 			   (pnote->note.namedata - (char *) contents) - 12,
2424*56bb7041Schristos 			   (back->note.namedata - (char *) contents) - 12);
2425*56bb7041Schristos 	      pnote->note.type = 0;
2426*56bb7041Schristos 	      break;
2427*56bb7041Schristos 	    }
2428*56bb7041Schristos 
2429*56bb7041Schristos #if DEBUG_MERGE
2430*56bb7041Schristos 	  /* This should not happen as we have sorted the
2431*56bb7041Schristos 	     notes with earlier starting addresses first.  */
2432*56bb7041Schristos 	  if (contained_by (back, pnote))
2433*56bb7041Schristos 	    merge_debug ("ERROR: UNEXPECTED CONTAINMENT\n");
2434*56bb7041Schristos #endif
2435*56bb7041Schristos 
2436*56bb7041Schristos 	  /* Rule 2b.  */
2437*56bb7041Schristos 	  if (overlaps_or_adjoins (back, pnote)
2438*56bb7041Schristos 	      && is_func_note (back) == is_func_note (pnote))
2439*56bb7041Schristos 	    {
2440*56bb7041Schristos 	      merge_debug ("Delete note at offset %#08lx - merge into note at %#08lx\n",
2441*56bb7041Schristos 			   (pnote->note.namedata - (char *) contents) - 12,
2442*56bb7041Schristos 			   (back->note.namedata - (char *) contents) - 12);
2443*56bb7041Schristos 
2444*56bb7041Schristos 	      back->end   = back->end > pnote->end ? back->end : pnote->end;
2445*56bb7041Schristos 	      back->start = back->start < pnote->start ? back->start : pnote->start;
2446*56bb7041Schristos 	      pnote->note.type = 0;
2447*56bb7041Schristos 	      break;
2448*56bb7041Schristos 	    }
2449*56bb7041Schristos 
2450*56bb7041Schristos 	  /* Don't scan too far back however.  */
2451*56bb7041Schristos 	  if (iter ++ > 16)
2452*56bb7041Schristos 	    {
2453*56bb7041Schristos 	      /* FIXME: Not sure if this can ever be triggered.  */
2454*56bb7041Schristos 	      merge_debug ("ITERATION LIMIT REACHED\n");
2455*56bb7041Schristos 	      break;
2456*56bb7041Schristos 	    }
2457*56bb7041Schristos 	}
2458*56bb7041Schristos #if DEBUG_MERGE
2459*56bb7041Schristos       if (! is_deleted_note (pnote))
2460*56bb7041Schristos 	merge_debug ("Unable to do anything with note at %#08lx\n",
2461*56bb7041Schristos 		     (pnote->note.namedata - (char *) contents) - 12);
2462*56bb7041Schristos #endif
2463*56bb7041Schristos     }
2464*56bb7041Schristos 
2465*56bb7041Schristos   /* Resort the notes.  */
2466*56bb7041Schristos   merge_debug ("Final sorting of notes\n");
2467*56bb7041Schristos   qsort (pnotes, pnotes_end - pnotes, sizeof (* pnotes), sort_gnu_build_notes);
2468*56bb7041Schristos 
2469*56bb7041Schristos   /* Reconstruct the ELF notes.  */
2470*56bb7041Schristos   bfd_byte *     new_contents;
2471*56bb7041Schristos   bfd_byte *     old;
2472*56bb7041Schristos   bfd_byte *     new;
2473*56bb7041Schristos   bfd_size_type  new_size;
2474*56bb7041Schristos   bfd_vma        prev_start = 0;
2475*56bb7041Schristos   bfd_vma        prev_end = 0;
2476*56bb7041Schristos 
2477*56bb7041Schristos   /* Not sure how, but the notes might grow in size.
2478*56bb7041Schristos      (eg see PR 1774507).  Allow for this here.  */
2479*56bb7041Schristos   new = new_contents = xmalloc (size * 2);
2480*56bb7041Schristos   for (pnote = pnotes, old = contents;
2481*56bb7041Schristos        pnote < pnotes_end;
2482*56bb7041Schristos        pnote ++)
2483*56bb7041Schristos     {
2484*56bb7041Schristos       bfd_size_type note_size = 12 + pnote->padded_namesz + pnote->note.descsz;
2485*56bb7041Schristos 
2486*56bb7041Schristos       if (! is_deleted_note (pnote))
2487*56bb7041Schristos 	{
2488*56bb7041Schristos 	  /* Create the note, potentially using the
2489*56bb7041Schristos 	     address range of the previous note.  */
2490*56bb7041Schristos 	  if (pnote->start == prev_start && pnote->end == prev_end)
2491*56bb7041Schristos 	    {
2492*56bb7041Schristos 	      bfd_put_32 (abfd, pnote->note.namesz, new);
2493*56bb7041Schristos 	      bfd_put_32 (abfd, 0, new + 4);
2494*56bb7041Schristos 	      bfd_put_32 (abfd, pnote->note.type, new + 8);
2495*56bb7041Schristos 	      new += 12;
2496*56bb7041Schristos 	      memcpy (new, pnote->note.namedata, pnote->note.namesz);
2497*56bb7041Schristos 	      if (pnote->note.namesz < pnote->padded_namesz)
2498*56bb7041Schristos 		memset (new + pnote->note.namesz, 0, pnote->padded_namesz - pnote->note.namesz);
2499*56bb7041Schristos 	      new += pnote->padded_namesz;
2500*56bb7041Schristos 	    }
2501*56bb7041Schristos 	  else
2502*56bb7041Schristos 	    {
2503*56bb7041Schristos 	      bfd_put_32 (abfd, pnote->note.namesz, new);
2504*56bb7041Schristos 	      bfd_put_32 (abfd, is_64bit (abfd) ? 16 : 8, new + 4);
2505*56bb7041Schristos 	      bfd_put_32 (abfd, pnote->note.type, new + 8);
2506*56bb7041Schristos 	      new += 12;
2507*56bb7041Schristos 	      memcpy (new, pnote->note.namedata, pnote->note.namesz);
2508*56bb7041Schristos 	      if (pnote->note.namesz < pnote->padded_namesz)
2509*56bb7041Schristos 		memset (new + pnote->note.namesz, 0, pnote->padded_namesz - pnote->note.namesz);
2510*56bb7041Schristos 	      new += pnote->padded_namesz;
2511*56bb7041Schristos 	      if (is_64bit (abfd))
2512*56bb7041Schristos 		{
2513*56bb7041Schristos 		  bfd_put_64 (abfd, pnote->start, new);
2514*56bb7041Schristos 		  bfd_put_64 (abfd, pnote->end, new + 8);
2515*56bb7041Schristos 		  new += 16;
2516*56bb7041Schristos 		}
2517*56bb7041Schristos 	      else
2518*56bb7041Schristos 		{
2519*56bb7041Schristos 		  bfd_put_32 (abfd, pnote->start, new);
2520*56bb7041Schristos 		  bfd_put_32 (abfd, pnote->end, new + 4);
2521*56bb7041Schristos 		  new += 8;
2522*56bb7041Schristos 		}
2523*56bb7041Schristos 
2524*56bb7041Schristos 	      prev_start = pnote->start;
2525*56bb7041Schristos 	      prev_end = pnote->end;
2526*56bb7041Schristos 	    }
2527*56bb7041Schristos 	}
2528*56bb7041Schristos 
2529*56bb7041Schristos       old += note_size;
2530*56bb7041Schristos     }
2531*56bb7041Schristos 
2532*56bb7041Schristos #if DEBUG_MERGE
2533*56bb7041Schristos   merge_debug ("Results of merge:\n");
2534*56bb7041Schristos   for (pnote = pnotes; pnote < pnotes_end; pnote ++)
2535*56bb7041Schristos     if (! is_deleted_note (pnote))
2536*56bb7041Schristos       merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2537*56bb7041Schristos 		   (pnote->note.namedata - (char *) contents) - 12,
2538*56bb7041Schristos 		   pnote->start, pnote->end,
2539*56bb7041Schristos 		   pnote->note.type,
2540*56bb7041Schristos 		   pnote->note.namedata[3],
2541*56bb7041Schristos 		   pnote->note.namesz
2542*56bb7041Schristos 		   );
2543*56bb7041Schristos #endif
2544*56bb7041Schristos 
2545*56bb7041Schristos   new_size = new - new_contents;
2546*56bb7041Schristos   if (new_size < size)
2547*56bb7041Schristos     {
2548*56bb7041Schristos       memcpy (contents, new_contents, new_size);
2549*56bb7041Schristos       size = new_size;
2550*56bb7041Schristos     }
2551*56bb7041Schristos   free (new_contents);
2552*56bb7041Schristos 
2553*56bb7041Schristos  done:
2554*56bb7041Schristos   if (err)
2555*56bb7041Schristos     {
2556*56bb7041Schristos       bfd_set_error (bfd_error_bad_value);
2557*56bb7041Schristos       bfd_nonfatal_message (NULL, abfd, sec, err);
2558*56bb7041Schristos       status = 1;
2559*56bb7041Schristos     }
2560*56bb7041Schristos 
2561*56bb7041Schristos   free (pnotes);
2562*56bb7041Schristos   return size;
2563*56bb7041Schristos }
2564*56bb7041Schristos 
2565*56bb7041Schristos static flagword
check_new_section_flags(flagword flags,bfd * abfd,const char * secname)2566*56bb7041Schristos check_new_section_flags (flagword flags, bfd * abfd, const char * secname)
2567*56bb7041Schristos {
2568*56bb7041Schristos   /* Only set the SEC_COFF_SHARED flag on COFF files.
2569*56bb7041Schristos      The same bit value is used by ELF targets to indicate
2570*56bb7041Schristos      compressed sections, and setting that flag here breaks
2571*56bb7041Schristos      things.  */
2572*56bb7041Schristos   if ((flags & SEC_COFF_SHARED)
2573*56bb7041Schristos       && bfd_get_flavour (abfd) != bfd_target_coff_flavour)
2574*56bb7041Schristos     {
2575*56bb7041Schristos       non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"),
2576*56bb7041Schristos 		 bfd_get_filename (abfd), secname);
2577*56bb7041Schristos       flags &= ~ SEC_COFF_SHARED;
2578*56bb7041Schristos     }
2579*56bb7041Schristos   return flags;
2580*56bb7041Schristos }
2581*56bb7041Schristos 
2582*56bb7041Schristos /* Copy object file IBFD onto OBFD.
2583*56bb7041Schristos    Returns TRUE upon success, FALSE otherwise.  */
2584*56bb7041Schristos 
2585*56bb7041Schristos static bfd_boolean
copy_object(bfd * ibfd,bfd * obfd,const bfd_arch_info_type * input_arch)2586*56bb7041Schristos copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
2587*56bb7041Schristos {
2588*56bb7041Schristos   bfd_vma start;
2589*56bb7041Schristos   long symcount;
2590*56bb7041Schristos   asection **osections = NULL;
2591*56bb7041Schristos   asection *osec;
2592*56bb7041Schristos   asection *gnu_debuglink_section = NULL;
2593*56bb7041Schristos   bfd_size_type *gaps = NULL;
2594*56bb7041Schristos   bfd_size_type max_gap = 0;
2595*56bb7041Schristos   long symsize;
2596*56bb7041Schristos   void *dhandle;
2597*56bb7041Schristos   enum bfd_architecture iarch;
2598*56bb7041Schristos   unsigned int imach;
2599*56bb7041Schristos   unsigned int num_sec, i;
2600*56bb7041Schristos 
2601*56bb7041Schristos   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2602*56bb7041Schristos       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2603*56bb7041Schristos       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2604*56bb7041Schristos     {
2605*56bb7041Schristos       /* PR 17636: Call non-fatal so that we return to our parent who
2606*56bb7041Schristos 	 may need to tidy temporary files.  */
2607*56bb7041Schristos       non_fatal (_("unable to change endianness of '%s'"),
2608*56bb7041Schristos 		 bfd_get_archive_filename (ibfd));
2609*56bb7041Schristos       return FALSE;
2610*56bb7041Schristos     }
2611*56bb7041Schristos 
2612*56bb7041Schristos   if (ibfd->read_only)
2613*56bb7041Schristos     {
2614*56bb7041Schristos       non_fatal (_("unable to modify '%s' due to errors"),
2615*56bb7041Schristos 		 bfd_get_archive_filename (ibfd));
2616*56bb7041Schristos       return FALSE;
2617*56bb7041Schristos     }
2618*56bb7041Schristos 
2619*56bb7041Schristos   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2620*56bb7041Schristos     {
2621*56bb7041Schristos       bfd_nonfatal_message (NULL, obfd, NULL, NULL);
2622*56bb7041Schristos       return FALSE;
2623*56bb7041Schristos     }
2624*56bb7041Schristos 
2625*56bb7041Schristos   if (ibfd->sections == NULL)
2626*56bb7041Schristos     {
2627*56bb7041Schristos       non_fatal (_("error: the input file '%s' has no sections"),
2628*56bb7041Schristos 		 bfd_get_archive_filename (ibfd));
2629*56bb7041Schristos       return FALSE;
2630*56bb7041Schristos     }
2631*56bb7041Schristos 
2632*56bb7041Schristos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2633*56bb7041Schristos     {
2634*56bb7041Schristos       if ((do_debug_sections & compress) != 0
2635*56bb7041Schristos 	  && do_debug_sections != compress)
2636*56bb7041Schristos 	{
2637*56bb7041Schristos 	  non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
2638*56bb7041Schristos 		     bfd_get_archive_filename (ibfd));
2639*56bb7041Schristos 	  return FALSE;
2640*56bb7041Schristos 	}
2641*56bb7041Schristos 
2642*56bb7041Schristos       if (do_elf_stt_common)
2643*56bb7041Schristos 	{
2644*56bb7041Schristos 	  non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
2645*56bb7041Schristos 		     bfd_get_archive_filename (ibfd));
2646*56bb7041Schristos 	  return FALSE;
2647*56bb7041Schristos 	}
2648*56bb7041Schristos     }
2649*56bb7041Schristos 
2650*56bb7041Schristos   if (verbose)
2651*56bb7041Schristos     printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
2652*56bb7041Schristos 	    bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
2653*56bb7041Schristos 	    bfd_get_filename (obfd), bfd_get_target (obfd));
2654*56bb7041Schristos 
2655*56bb7041Schristos   if (extract_symbol)
2656*56bb7041Schristos     start = 0;
2657*56bb7041Schristos   else
2658*56bb7041Schristos     {
2659*56bb7041Schristos       if (set_start_set)
2660*56bb7041Schristos 	start = set_start;
2661*56bb7041Schristos       else
2662*56bb7041Schristos 	start = bfd_get_start_address (ibfd);
2663*56bb7041Schristos       start += change_start;
2664*56bb7041Schristos     }
2665*56bb7041Schristos 
2666*56bb7041Schristos   /* Neither the start address nor the flags
2667*56bb7041Schristos      need to be set for a core file.  */
2668*56bb7041Schristos   if (bfd_get_format (obfd) != bfd_core)
2669*56bb7041Schristos     {
2670*56bb7041Schristos       flagword flags;
2671*56bb7041Schristos 
2672*56bb7041Schristos       flags = bfd_get_file_flags (ibfd);
2673*56bb7041Schristos       flags |= bfd_flags_to_set;
2674*56bb7041Schristos       flags &= ~bfd_flags_to_clear;
2675*56bb7041Schristos       flags &= bfd_applicable_file_flags (obfd);
2676*56bb7041Schristos 
2677*56bb7041Schristos       if (strip_symbols == STRIP_ALL)
2678*56bb7041Schristos 	flags &= ~HAS_RELOC;
2679*56bb7041Schristos 
2680*56bb7041Schristos       if (!bfd_set_start_address (obfd, start)
2681*56bb7041Schristos 	  || !bfd_set_file_flags (obfd, flags))
2682*56bb7041Schristos 	{
2683*56bb7041Schristos 	  bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2684*56bb7041Schristos 	  return FALSE;
2685*56bb7041Schristos 	}
2686*56bb7041Schristos     }
2687*56bb7041Schristos 
2688*56bb7041Schristos   /* Copy architecture of input file to output file.  */
2689*56bb7041Schristos   iarch = bfd_get_arch (ibfd);
2690*56bb7041Schristos   imach = bfd_get_mach (ibfd);
2691*56bb7041Schristos   if (input_arch)
2692*56bb7041Schristos     {
2693*56bb7041Schristos       if (iarch == bfd_arch_unknown)
2694*56bb7041Schristos 	{
2695*56bb7041Schristos 	  iarch = input_arch->arch;
2696*56bb7041Schristos 	  imach = input_arch->mach;
2697*56bb7041Schristos 	}
2698*56bb7041Schristos       else
2699*56bb7041Schristos 	non_fatal (_("Input file `%s' ignores binary architecture parameter."),
2700*56bb7041Schristos 		   bfd_get_archive_filename (ibfd));
2701*56bb7041Schristos     }
2702*56bb7041Schristos   if (iarch == bfd_arch_unknown
2703*56bb7041Schristos       && bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2704*56bb7041Schristos       && bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2705*56bb7041Schristos     {
2706*56bb7041Schristos       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
2707*56bb7041Schristos       iarch = bed->arch;
2708*56bb7041Schristos       imach = 0;
2709*56bb7041Schristos     }
2710*56bb7041Schristos   if (!bfd_set_arch_mach (obfd, iarch, imach)
2711*56bb7041Schristos       && (ibfd->target_defaulted
2712*56bb7041Schristos 	  || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
2713*56bb7041Schristos     {
2714*56bb7041Schristos       if (bfd_get_arch (ibfd) == bfd_arch_unknown)
2715*56bb7041Schristos 	non_fatal (_("Unable to recognise the format of the input file `%s'"),
2716*56bb7041Schristos 		   bfd_get_archive_filename (ibfd));
2717*56bb7041Schristos       else
2718*56bb7041Schristos 	non_fatal (_("Output file cannot represent architecture `%s'"),
2719*56bb7041Schristos 		   bfd_printable_arch_mach (bfd_get_arch (ibfd),
2720*56bb7041Schristos 					    bfd_get_mach (ibfd)));
2721*56bb7041Schristos       return FALSE;
2722*56bb7041Schristos     }
2723*56bb7041Schristos 
2724*56bb7041Schristos   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2725*56bb7041Schristos     {
2726*56bb7041Schristos       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2727*56bb7041Schristos       return FALSE;
2728*56bb7041Schristos     }
2729*56bb7041Schristos 
2730*56bb7041Schristos   if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2731*56bb7041Schristos       && bfd_pei_p (obfd))
2732*56bb7041Schristos     {
2733*56bb7041Schristos       /* Set up PE parameters.  */
2734*56bb7041Schristos       pe_data_type *pe = pe_data (obfd);
2735*56bb7041Schristos 
2736*56bb7041Schristos       /* Copy PE parameters before changing them.  */
2737*56bb7041Schristos       if (bfd_get_flavour (ibfd) == bfd_target_coff_flavour
2738*56bb7041Schristos 	  && bfd_pei_p (ibfd))
2739*56bb7041Schristos 	pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
2740*56bb7041Schristos 
2741*56bb7041Schristos       if (pe_file_alignment != (bfd_vma) -1)
2742*56bb7041Schristos 	pe->pe_opthdr.FileAlignment = pe_file_alignment;
2743*56bb7041Schristos       else
2744*56bb7041Schristos 	pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
2745*56bb7041Schristos 
2746*56bb7041Schristos       if (pe_heap_commit != (bfd_vma) -1)
2747*56bb7041Schristos 	pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
2748*56bb7041Schristos 
2749*56bb7041Schristos       if (pe_heap_reserve != (bfd_vma) -1)
2750*56bb7041Schristos 	pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
2751*56bb7041Schristos 
2752*56bb7041Schristos       if (pe_image_base != (bfd_vma) -1)
2753*56bb7041Schristos 	pe->pe_opthdr.ImageBase = pe_image_base;
2754*56bb7041Schristos 
2755*56bb7041Schristos       if (pe_section_alignment != (bfd_vma) -1)
2756*56bb7041Schristos 	pe->pe_opthdr.SectionAlignment = pe_section_alignment;
2757*56bb7041Schristos       else
2758*56bb7041Schristos 	pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
2759*56bb7041Schristos 
2760*56bb7041Schristos       if (pe_stack_commit != (bfd_vma) -1)
2761*56bb7041Schristos 	pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
2762*56bb7041Schristos 
2763*56bb7041Schristos       if (pe_stack_reserve != (bfd_vma) -1)
2764*56bb7041Schristos 	pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
2765*56bb7041Schristos 
2766*56bb7041Schristos       if (pe_subsystem != -1)
2767*56bb7041Schristos 	pe->pe_opthdr.Subsystem = pe_subsystem;
2768*56bb7041Schristos 
2769*56bb7041Schristos       if (pe_major_subsystem_version != -1)
2770*56bb7041Schristos 	pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
2771*56bb7041Schristos 
2772*56bb7041Schristos       if (pe_minor_subsystem_version != -1)
2773*56bb7041Schristos 	pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
2774*56bb7041Schristos 
2775*56bb7041Schristos       if (pe_file_alignment > pe_section_alignment)
2776*56bb7041Schristos 	{
2777*56bb7041Schristos 	  char file_alignment[20], section_alignment[20];
2778*56bb7041Schristos 
2779*56bb7041Schristos 	  sprintf_vma (file_alignment, pe_file_alignment);
2780*56bb7041Schristos 	  sprintf_vma (section_alignment, pe_section_alignment);
2781*56bb7041Schristos 	  non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
2782*56bb7041Schristos 
2783*56bb7041Schristos 		     file_alignment, section_alignment);
2784*56bb7041Schristos 	}
2785*56bb7041Schristos 
2786*56bb7041Schristos       if (preserve_dates
2787*56bb7041Schristos 	  && bfd_get_flavour (ibfd) == bfd_target_coff_flavour
2788*56bb7041Schristos 	  && bfd_pei_p (ibfd))
2789*56bb7041Schristos 	pe->timestamp = pe_data (ibfd)->coff.timestamp;
2790*56bb7041Schristos     }
2791*56bb7041Schristos 
2792*56bb7041Schristos   if (isympp)
2793*56bb7041Schristos     free (isympp);
2794*56bb7041Schristos 
2795*56bb7041Schristos   if (osympp != isympp)
2796*56bb7041Schristos     free (osympp);
2797*56bb7041Schristos 
2798*56bb7041Schristos   isympp = NULL;
2799*56bb7041Schristos   osympp = NULL;
2800*56bb7041Schristos 
2801*56bb7041Schristos   symsize = bfd_get_symtab_upper_bound (ibfd);
2802*56bb7041Schristos   if (symsize < 0)
2803*56bb7041Schristos     {
2804*56bb7041Schristos       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2805*56bb7041Schristos       return FALSE;
2806*56bb7041Schristos     }
2807*56bb7041Schristos 
2808*56bb7041Schristos   osympp = isympp = (asymbol **) xmalloc (symsize);
2809*56bb7041Schristos   symcount = bfd_canonicalize_symtab (ibfd, isympp);
2810*56bb7041Schristos   if (symcount < 0)
2811*56bb7041Schristos     {
2812*56bb7041Schristos       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2813*56bb7041Schristos       return FALSE;
2814*56bb7041Schristos     }
2815*56bb7041Schristos   /* PR 17512: file:  d6323821
2816*56bb7041Schristos      If the symbol table could not be loaded do not pretend that we have
2817*56bb7041Schristos      any symbols.  This trips us up later on when we load the relocs.  */
2818*56bb7041Schristos   if (symcount == 0)
2819*56bb7041Schristos     {
2820*56bb7041Schristos       free (isympp);
2821*56bb7041Schristos       osympp = isympp = NULL;
2822*56bb7041Schristos     }
2823*56bb7041Schristos 
2824*56bb7041Schristos   /* BFD mandates that all output sections be created and sizes set before
2825*56bb7041Schristos      any output is done.  Thus, we traverse all sections multiple times.  */
2826*56bb7041Schristos   bfd_map_over_sections (ibfd, setup_section, obfd);
2827*56bb7041Schristos 
2828*56bb7041Schristos   if (!extract_symbol)
2829*56bb7041Schristos     setup_bfd_headers (ibfd, obfd);
2830*56bb7041Schristos 
2831*56bb7041Schristos   if (add_sections != NULL)
2832*56bb7041Schristos     {
2833*56bb7041Schristos       struct section_add *padd;
2834*56bb7041Schristos       struct section_list *pset;
2835*56bb7041Schristos 
2836*56bb7041Schristos       for (padd = add_sections; padd != NULL; padd = padd->next)
2837*56bb7041Schristos 	{
2838*56bb7041Schristos 	  flagword flags;
2839*56bb7041Schristos 
2840*56bb7041Schristos 	  pset = find_section_list (padd->name, FALSE,
2841*56bb7041Schristos 				    SECTION_CONTEXT_SET_FLAGS);
2842*56bb7041Schristos 	  if (pset != NULL)
2843*56bb7041Schristos 	    {
2844*56bb7041Schristos 	      flags = pset->flags | SEC_HAS_CONTENTS;
2845*56bb7041Schristos 	      flags = check_new_section_flags (flags, obfd, padd->name);
2846*56bb7041Schristos 	    }
2847*56bb7041Schristos 	  else
2848*56bb7041Schristos 	    flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
2849*56bb7041Schristos 
2850*56bb7041Schristos 	  /* bfd_make_section_with_flags() does not return very helpful
2851*56bb7041Schristos 	     error codes, so check for the most likely user error first.  */
2852*56bb7041Schristos 	  if (bfd_get_section_by_name (obfd, padd->name))
2853*56bb7041Schristos 	    {
2854*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, NULL,
2855*56bb7041Schristos 				    _("can't add section '%s'"), padd->name);
2856*56bb7041Schristos 	      return FALSE;
2857*56bb7041Schristos 	    }
2858*56bb7041Schristos 	  else
2859*56bb7041Schristos 	    {
2860*56bb7041Schristos 	      /* We use LINKER_CREATED here so that the backend hooks
2861*56bb7041Schristos 		 will create any special section type information,
2862*56bb7041Schristos 		 instead of presuming we know what we're doing merely
2863*56bb7041Schristos 		 because we set the flags.  */
2864*56bb7041Schristos 	      padd->section = bfd_make_section_with_flags
2865*56bb7041Schristos 		(obfd, padd->name, flags | SEC_LINKER_CREATED);
2866*56bb7041Schristos 	      if (padd->section == NULL)
2867*56bb7041Schristos 		{
2868*56bb7041Schristos 		  bfd_nonfatal_message (NULL, obfd, NULL,
2869*56bb7041Schristos 					_("can't create section `%s'"),
2870*56bb7041Schristos 					padd->name);
2871*56bb7041Schristos 		  return FALSE;
2872*56bb7041Schristos 		}
2873*56bb7041Schristos 	    }
2874*56bb7041Schristos 
2875*56bb7041Schristos 	  if (!bfd_set_section_size (padd->section, padd->size))
2876*56bb7041Schristos 	    {
2877*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2878*56bb7041Schristos 	      return FALSE;
2879*56bb7041Schristos 	    }
2880*56bb7041Schristos 
2881*56bb7041Schristos 	  pset = find_section_list (padd->name, FALSE,
2882*56bb7041Schristos 				    SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
2883*56bb7041Schristos 	  if (pset != NULL
2884*56bb7041Schristos 	      && !bfd_set_section_vma (padd->section, pset->vma_val))
2885*56bb7041Schristos 	    {
2886*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2887*56bb7041Schristos 	      return FALSE;
2888*56bb7041Schristos 	    }
2889*56bb7041Schristos 
2890*56bb7041Schristos 	  pset = find_section_list (padd->name, FALSE,
2891*56bb7041Schristos 				    SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
2892*56bb7041Schristos 	  if (pset != NULL)
2893*56bb7041Schristos 	    {
2894*56bb7041Schristos 	      padd->section->lma = pset->lma_val;
2895*56bb7041Schristos 
2896*56bb7041Schristos 	      if (!bfd_set_section_alignment
2897*56bb7041Schristos 		  (padd->section, bfd_section_alignment (padd->section)))
2898*56bb7041Schristos 		{
2899*56bb7041Schristos 		  bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2900*56bb7041Schristos 		  return FALSE;
2901*56bb7041Schristos 		}
2902*56bb7041Schristos 	    }
2903*56bb7041Schristos 	}
2904*56bb7041Schristos     }
2905*56bb7041Schristos 
2906*56bb7041Schristos   if (update_sections != NULL)
2907*56bb7041Schristos     {
2908*56bb7041Schristos       struct section_add *pupdate;
2909*56bb7041Schristos 
2910*56bb7041Schristos       for (pupdate = update_sections;
2911*56bb7041Schristos 	   pupdate != NULL;
2912*56bb7041Schristos 	   pupdate = pupdate->next)
2913*56bb7041Schristos 	{
2914*56bb7041Schristos 	  pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name);
2915*56bb7041Schristos 	  if (pupdate->section == NULL)
2916*56bb7041Schristos 	    {
2917*56bb7041Schristos 	      non_fatal (_("error: %s not found, can't be updated"), pupdate->name);
2918*56bb7041Schristos 	      return FALSE;
2919*56bb7041Schristos 	    }
2920*56bb7041Schristos 
2921*56bb7041Schristos 	  osec = pupdate->section->output_section;
2922*56bb7041Schristos 	  if (!bfd_set_section_size (osec, pupdate->size))
2923*56bb7041Schristos 	    {
2924*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, osec, NULL);
2925*56bb7041Schristos 	      return FALSE;
2926*56bb7041Schristos 	    }
2927*56bb7041Schristos 	}
2928*56bb7041Schristos     }
2929*56bb7041Schristos 
2930*56bb7041Schristos   merged_note_section * merged_note_sections = NULL;
2931*56bb7041Schristos   if (merge_notes)
2932*56bb7041Schristos     {
2933*56bb7041Schristos       /* This palaver is necessary because we must set the output
2934*56bb7041Schristos 	 section size first, before its contents are ready.  */
2935*56bb7041Schristos       for (osec = ibfd->sections; osec != NULL; osec = osec->next)
2936*56bb7041Schristos 	{
2937*56bb7041Schristos 	  if (! is_mergeable_note_section (ibfd, osec))
2938*56bb7041Schristos 	    continue;
2939*56bb7041Schristos 
2940*56bb7041Schristos 	  /* If the section is going to be completly deleted then
2941*56bb7041Schristos 	     do not bother to merge it.  */
2942*56bb7041Schristos 	  if (osec->output_section == NULL)
2943*56bb7041Schristos 	    continue;
2944*56bb7041Schristos 
2945*56bb7041Schristos 	  bfd_size_type size = bfd_section_size (osec);
2946*56bb7041Schristos 
2947*56bb7041Schristos 	  if (size == 0)
2948*56bb7041Schristos 	    {
2949*56bb7041Schristos 	      bfd_nonfatal_message (NULL, ibfd, osec,
2950*56bb7041Schristos 				    _("warning: note section is empty"));
2951*56bb7041Schristos 	      continue;
2952*56bb7041Schristos 	    }
2953*56bb7041Schristos 
2954*56bb7041Schristos 	  merged_note_section * merged = xmalloc (sizeof * merged);
2955*56bb7041Schristos 	  merged->contents = NULL;
2956*56bb7041Schristos 	  if (! bfd_get_full_section_contents (ibfd, osec, & merged->contents))
2957*56bb7041Schristos 	    {
2958*56bb7041Schristos 	      bfd_nonfatal_message (NULL, ibfd, osec,
2959*56bb7041Schristos 				    _("warning: could not load note section"));
2960*56bb7041Schristos 	      free (merged);
2961*56bb7041Schristos 	      continue;
2962*56bb7041Schristos 	    }
2963*56bb7041Schristos 
2964*56bb7041Schristos 	  merged->size = merge_gnu_build_notes (ibfd, osec, size,
2965*56bb7041Schristos 						merged->contents);
2966*56bb7041Schristos 
2967*56bb7041Schristos 	  /* FIXME: Once we have read the contents in, we must write
2968*56bb7041Schristos 	     them out again.  So even if the mergeing has achieved
2969*56bb7041Schristos 	     nothing we still add this entry to the merge list.  */
2970*56bb7041Schristos 
2971*56bb7041Schristos 	  if (size != merged->size
2972*56bb7041Schristos 	      && !bfd_set_section_size (osec->output_section, merged->size))
2973*56bb7041Schristos 	    {
2974*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, osec,
2975*56bb7041Schristos 				    _("warning: failed to set merged notes size"));
2976*56bb7041Schristos 	      free (merged->contents);
2977*56bb7041Schristos 	      free (merged);
2978*56bb7041Schristos 	      continue;
2979*56bb7041Schristos 	    }
2980*56bb7041Schristos 
2981*56bb7041Schristos 	  /* Add section to list of merged sections.  */
2982*56bb7041Schristos 	  merged->sec  = osec;
2983*56bb7041Schristos 	  merged->next = merged_note_sections;
2984*56bb7041Schristos 	  merged_note_sections = merged;
2985*56bb7041Schristos 	}
2986*56bb7041Schristos     }
2987*56bb7041Schristos 
2988*56bb7041Schristos   if (dump_sections != NULL)
2989*56bb7041Schristos     {
2990*56bb7041Schristos       struct section_add * pdump;
2991*56bb7041Schristos 
2992*56bb7041Schristos       for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
2993*56bb7041Schristos 	{
2994*56bb7041Schristos 	  FILE * f;
2995*56bb7041Schristos 	  bfd_byte *contents;
2996*56bb7041Schristos 
2997*56bb7041Schristos 	  osec = bfd_get_section_by_name (ibfd, pdump->name);
2998*56bb7041Schristos 	  if (osec == NULL)
2999*56bb7041Schristos 	    {
3000*56bb7041Schristos 	      bfd_nonfatal_message (NULL, ibfd, NULL,
3001*56bb7041Schristos 				    _("can't dump section '%s' - it does not exist"),
3002*56bb7041Schristos 				    pdump->name);
3003*56bb7041Schristos 	      continue;
3004*56bb7041Schristos 	    }
3005*56bb7041Schristos 
3006*56bb7041Schristos 	  if ((bfd_section_flags (osec) & SEC_HAS_CONTENTS) == 0)
3007*56bb7041Schristos 	    {
3008*56bb7041Schristos 	      bfd_nonfatal_message (NULL, ibfd, osec,
3009*56bb7041Schristos 				    _("can't dump section - it has no contents"));
3010*56bb7041Schristos 	      continue;
3011*56bb7041Schristos 	    }
3012*56bb7041Schristos 
3013*56bb7041Schristos 	  bfd_size_type size = bfd_section_size (osec);
3014*56bb7041Schristos 	  /* Note - we allow the dumping of zero-sized sections,
3015*56bb7041Schristos 	     creating an empty file.  */
3016*56bb7041Schristos 
3017*56bb7041Schristos 	  f = fopen (pdump->filename, FOPEN_WB);
3018*56bb7041Schristos 	  if (f == NULL)
3019*56bb7041Schristos 	    {
3020*56bb7041Schristos 	      bfd_nonfatal_message (pdump->filename, NULL, NULL,
3021*56bb7041Schristos 				    _("could not open section dump file"));
3022*56bb7041Schristos 	      continue;
3023*56bb7041Schristos 	    }
3024*56bb7041Schristos 
3025*56bb7041Schristos 	  if (bfd_malloc_and_get_section (ibfd, osec, &contents))
3026*56bb7041Schristos 	    {
3027*56bb7041Schristos 	      if (size != 0 && fwrite (contents, 1, size, f) != size)
3028*56bb7041Schristos 		{
3029*56bb7041Schristos 		  non_fatal (_("error writing section contents to %s (error: %s)"),
3030*56bb7041Schristos 			     pdump->filename,
3031*56bb7041Schristos 			     strerror (errno));
3032*56bb7041Schristos 		  free (contents);
3033*56bb7041Schristos 		  fclose (f);
3034*56bb7041Schristos 		  return FALSE;
3035*56bb7041Schristos 		}
3036*56bb7041Schristos 	    }
3037*56bb7041Schristos 	  else
3038*56bb7041Schristos 	    bfd_nonfatal_message (NULL, ibfd, osec,
3039*56bb7041Schristos 				  _("could not retrieve section contents"));
3040*56bb7041Schristos 
3041*56bb7041Schristos 	  fclose (f);
3042*56bb7041Schristos 	  free (contents);
3043*56bb7041Schristos 	}
3044*56bb7041Schristos     }
3045*56bb7041Schristos 
3046*56bb7041Schristos   if (gnu_debuglink_filename != NULL)
3047*56bb7041Schristos     {
3048*56bb7041Schristos       /* PR 15125: Give a helpful warning message if
3049*56bb7041Schristos 	 the debuglink section already exists, and
3050*56bb7041Schristos 	 allow the rest of the copy to complete.  */
3051*56bb7041Schristos       if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
3052*56bb7041Schristos 	{
3053*56bb7041Schristos 	  non_fatal (_("%s: debuglink section already exists"),
3054*56bb7041Schristos 		     bfd_get_filename (obfd));
3055*56bb7041Schristos 	  gnu_debuglink_filename = NULL;
3056*56bb7041Schristos 	}
3057*56bb7041Schristos       else
3058*56bb7041Schristos 	{
3059*56bb7041Schristos 	  gnu_debuglink_section = bfd_create_gnu_debuglink_section
3060*56bb7041Schristos 	    (obfd, gnu_debuglink_filename);
3061*56bb7041Schristos 
3062*56bb7041Schristos 	  if (gnu_debuglink_section == NULL)
3063*56bb7041Schristos 	    {
3064*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, NULL,
3065*56bb7041Schristos 				    _("cannot create debug link section `%s'"),
3066*56bb7041Schristos 				    gnu_debuglink_filename);
3067*56bb7041Schristos 	      return FALSE;
3068*56bb7041Schristos 	    }
3069*56bb7041Schristos 
3070*56bb7041Schristos 	  /* Special processing for PE format files.  We
3071*56bb7041Schristos 	     have no way to distinguish PE from COFF here.  */
3072*56bb7041Schristos 	  if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
3073*56bb7041Schristos 	    {
3074*56bb7041Schristos 	      bfd_vma debuglink_vma;
3075*56bb7041Schristos 	      asection * highest_section;
3076*56bb7041Schristos 
3077*56bb7041Schristos 	      /* The PE spec requires that all sections be adjacent and sorted
3078*56bb7041Schristos 		 in ascending order of VMA.  It also specifies that debug
3079*56bb7041Schristos 		 sections should be last.  This is despite the fact that debug
3080*56bb7041Schristos 		 sections are not loaded into memory and so in theory have no
3081*56bb7041Schristos 		 use for a VMA.
3082*56bb7041Schristos 
3083*56bb7041Schristos 		 This means that the debuglink section must be given a non-zero
3084*56bb7041Schristos 		 VMA which makes it contiguous with other debug sections.  So
3085*56bb7041Schristos 		 walk the current section list, find the section with the
3086*56bb7041Schristos 		 highest VMA and start the debuglink section after that one.  */
3087*56bb7041Schristos 	      for (osec = obfd->sections, highest_section = NULL;
3088*56bb7041Schristos 		   osec != NULL;
3089*56bb7041Schristos 		   osec = osec->next)
3090*56bb7041Schristos 		if (osec->vma > 0
3091*56bb7041Schristos 		    && (highest_section == NULL
3092*56bb7041Schristos 			|| osec->vma > highest_section->vma))
3093*56bb7041Schristos 		  highest_section = osec;
3094*56bb7041Schristos 
3095*56bb7041Schristos 	      if (highest_section)
3096*56bb7041Schristos 		debuglink_vma = BFD_ALIGN (highest_section->vma
3097*56bb7041Schristos 					   + highest_section->size,
3098*56bb7041Schristos 					   /* FIXME: We ought to be using
3099*56bb7041Schristos 					      COFF_PAGE_SIZE here or maybe
3100*56bb7041Schristos 					      bfd_section_alignment() (if it
3101*56bb7041Schristos 					      was set) but since this is for PE
3102*56bb7041Schristos 					      and we know the required alignment
3103*56bb7041Schristos 					      it is easier just to hard code it.  */
3104*56bb7041Schristos 					   0x1000);
3105*56bb7041Schristos 	      else
3106*56bb7041Schristos 		/* Umm, not sure what to do in this case.  */
3107*56bb7041Schristos 		debuglink_vma = 0x1000;
3108*56bb7041Schristos 
3109*56bb7041Schristos 	      bfd_set_section_vma (gnu_debuglink_section, debuglink_vma);
3110*56bb7041Schristos 	    }
3111*56bb7041Schristos 	}
3112*56bb7041Schristos     }
3113*56bb7041Schristos 
3114*56bb7041Schristos   num_sec = bfd_count_sections (obfd);
3115*56bb7041Schristos   if (num_sec != 0
3116*56bb7041Schristos       && (gap_fill_set || pad_to_set))
3117*56bb7041Schristos     {
3118*56bb7041Schristos       asection **set;
3119*56bb7041Schristos 
3120*56bb7041Schristos       /* We must fill in gaps between the sections and/or we must pad
3121*56bb7041Schristos 	 the last section to a specified address.  We do this by
3122*56bb7041Schristos 	 grabbing a list of the sections, sorting them by VMA, and
3123*56bb7041Schristos 	 increasing the section sizes as required to fill the gaps.
3124*56bb7041Schristos 	 We write out the gap contents below.  */
3125*56bb7041Schristos 
3126*56bb7041Schristos       osections = xmalloc (num_sec * sizeof (*osections));
3127*56bb7041Schristos       set = osections;
3128*56bb7041Schristos       bfd_map_over_sections (obfd, get_sections, &set);
3129*56bb7041Schristos 
3130*56bb7041Schristos       qsort (osections, num_sec, sizeof (*osections), compare_section_lma);
3131*56bb7041Schristos 
3132*56bb7041Schristos       gaps = xmalloc (num_sec * sizeof (*gaps));
3133*56bb7041Schristos       memset (gaps, 0, num_sec * sizeof (*gaps));
3134*56bb7041Schristos 
3135*56bb7041Schristos       if (gap_fill_set)
3136*56bb7041Schristos 	{
3137*56bb7041Schristos 	  for (i = 0; i < num_sec - 1; i++)
3138*56bb7041Schristos 	    {
3139*56bb7041Schristos 	      flagword flags;
3140*56bb7041Schristos 	      bfd_size_type size;           /* Octets.  */
3141*56bb7041Schristos 	      bfd_vma gap_start, gap_stop;  /* Octets.  */
3142*56bb7041Schristos 	      unsigned int opb1 = bfd_octets_per_byte (obfd, osections[i]);
3143*56bb7041Schristos 	      unsigned int opb2 = bfd_octets_per_byte (obfd, osections[i+1]);
3144*56bb7041Schristos 
3145*56bb7041Schristos 	      flags = bfd_section_flags (osections[i]);
3146*56bb7041Schristos 	      if ((flags & SEC_HAS_CONTENTS) == 0
3147*56bb7041Schristos 		  || (flags & SEC_LOAD) == 0)
3148*56bb7041Schristos 		continue;
3149*56bb7041Schristos 
3150*56bb7041Schristos 	      size = bfd_section_size (osections[i]);
3151*56bb7041Schristos 	      gap_start = bfd_section_lma (osections[i]) * opb1 + size;
3152*56bb7041Schristos 	      gap_stop = bfd_section_lma (osections[i + 1]) * opb2;
3153*56bb7041Schristos 	      if (gap_start < gap_stop)
3154*56bb7041Schristos 		{
3155*56bb7041Schristos 		  if (!bfd_set_section_size (osections[i],
3156*56bb7041Schristos 					     size + (gap_stop - gap_start)))
3157*56bb7041Schristos 		    {
3158*56bb7041Schristos 		      bfd_nonfatal_message (NULL, obfd, osections[i],
3159*56bb7041Schristos 					    _("Can't fill gap after section"));
3160*56bb7041Schristos 		      status = 1;
3161*56bb7041Schristos 		      break;
3162*56bb7041Schristos 		    }
3163*56bb7041Schristos 		  gaps[i] = gap_stop - gap_start;
3164*56bb7041Schristos 		  if (max_gap < gap_stop - gap_start)
3165*56bb7041Schristos 		    max_gap = gap_stop - gap_start;
3166*56bb7041Schristos 		}
3167*56bb7041Schristos 	    }
3168*56bb7041Schristos 	}
3169*56bb7041Schristos 
3170*56bb7041Schristos       if (pad_to_set)
3171*56bb7041Schristos 	{
3172*56bb7041Schristos 	  bfd_vma lma;         /* Octets.  */
3173*56bb7041Schristos 	  bfd_size_type size;  /* Octets.  */
3174*56bb7041Schristos 	  unsigned int opb = bfd_octets_per_byte (obfd, osections[num_sec - 1]);
3175*56bb7041Schristos 	  bfd_vma _pad_to = pad_to * opb;
3176*56bb7041Schristos 
3177*56bb7041Schristos 	  lma = bfd_section_lma (osections[num_sec - 1]) * opb;
3178*56bb7041Schristos 	  size = bfd_section_size (osections[num_sec - 1]);
3179*56bb7041Schristos 	  if (lma + size < _pad_to)
3180*56bb7041Schristos 	    {
3181*56bb7041Schristos 	      if (!bfd_set_section_size (osections[num_sec - 1], _pad_to - lma))
3182*56bb7041Schristos 		{
3183*56bb7041Schristos 		  bfd_nonfatal_message (NULL, obfd, osections[num_sec - 1],
3184*56bb7041Schristos 					_("can't add padding"));
3185*56bb7041Schristos 		  status = 1;
3186*56bb7041Schristos 		}
3187*56bb7041Schristos 	      else
3188*56bb7041Schristos 		{
3189*56bb7041Schristos 		  gaps[num_sec - 1] = _pad_to - (lma + size);
3190*56bb7041Schristos 		  if (max_gap < _pad_to - (lma + size))
3191*56bb7041Schristos 		    max_gap = _pad_to - (lma + size);
3192*56bb7041Schristos 		}
3193*56bb7041Schristos 	    }
3194*56bb7041Schristos 	}
3195*56bb7041Schristos     }
3196*56bb7041Schristos 
3197*56bb7041Schristos   /* Symbol filtering must happen after the output sections
3198*56bb7041Schristos      have been created, but before their contents are set.  */
3199*56bb7041Schristos   dhandle = NULL;
3200*56bb7041Schristos   if (convert_debugging)
3201*56bb7041Schristos     dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
3202*56bb7041Schristos 
3203*56bb7041Schristos   if (strip_symbols == STRIP_DEBUG
3204*56bb7041Schristos       || strip_symbols == STRIP_ALL
3205*56bb7041Schristos       || strip_symbols == STRIP_UNNEEDED
3206*56bb7041Schristos       || strip_symbols == STRIP_NONDEBUG
3207*56bb7041Schristos       || strip_symbols == STRIP_DWO
3208*56bb7041Schristos       || strip_symbols == STRIP_NONDWO
3209*56bb7041Schristos       || discard_locals != LOCALS_UNDEF
3210*56bb7041Schristos       || localize_hidden
3211*56bb7041Schristos       || htab_elements (strip_specific_htab) != 0
3212*56bb7041Schristos       || htab_elements (keep_specific_htab) != 0
3213*56bb7041Schristos       || htab_elements (localize_specific_htab) != 0
3214*56bb7041Schristos       || htab_elements (globalize_specific_htab) != 0
3215*56bb7041Schristos       || htab_elements (keepglobal_specific_htab) != 0
3216*56bb7041Schristos       || htab_elements (weaken_specific_htab) != 0
3217*56bb7041Schristos       || htab_elements (redefine_specific_htab) != 0
3218*56bb7041Schristos       || prefix_symbols_string
3219*56bb7041Schristos       || sections_removed
3220*56bb7041Schristos       || sections_copied
3221*56bb7041Schristos       || convert_debugging
3222*56bb7041Schristos       || change_leading_char
3223*56bb7041Schristos       || remove_leading_char
3224*56bb7041Schristos       || section_rename_list
3225*56bb7041Schristos       || weaken
3226*56bb7041Schristos       || add_symbols)
3227*56bb7041Schristos     {
3228*56bb7041Schristos       /* Mark symbols used in output relocations so that they
3229*56bb7041Schristos 	 are kept, even if they are local labels or static symbols.
3230*56bb7041Schristos 
3231*56bb7041Schristos 	 Note we iterate over the input sections examining their
3232*56bb7041Schristos 	 relocations since the relocations for the output sections
3233*56bb7041Schristos 	 haven't been set yet.  mark_symbols_used_in_relocations will
3234*56bb7041Schristos 	 ignore input sections which have no corresponding output
3235*56bb7041Schristos 	 section.  */
3236*56bb7041Schristos       if (strip_symbols != STRIP_ALL)
3237*56bb7041Schristos 	{
3238*56bb7041Schristos 	  bfd_set_error (bfd_error_no_error);
3239*56bb7041Schristos 	  bfd_map_over_sections (ibfd,
3240*56bb7041Schristos 				 mark_symbols_used_in_relocations,
3241*56bb7041Schristos 				 isympp);
3242*56bb7041Schristos 	  if (bfd_get_error () != bfd_error_no_error)
3243*56bb7041Schristos 	    {
3244*56bb7041Schristos 	      status = 1;
3245*56bb7041Schristos 	      return FALSE;
3246*56bb7041Schristos 	    }
3247*56bb7041Schristos 	}
3248*56bb7041Schristos 
3249*56bb7041Schristos       osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *));
3250*56bb7041Schristos       symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
3251*56bb7041Schristos     }
3252*56bb7041Schristos 
3253*56bb7041Schristos   if (convert_debugging && dhandle != NULL)
3254*56bb7041Schristos     {
3255*56bb7041Schristos       bfd_boolean res;
3256*56bb7041Schristos 
3257*56bb7041Schristos       res = write_debugging_info (obfd, dhandle, &symcount, &osympp);
3258*56bb7041Schristos 
3259*56bb7041Schristos       free (dhandle);
3260*56bb7041Schristos       dhandle = NULL; /* Paranoia...  */
3261*56bb7041Schristos 
3262*56bb7041Schristos       if (! res)
3263*56bb7041Schristos 	{
3264*56bb7041Schristos 	  status = 1;
3265*56bb7041Schristos 	  return FALSE;
3266*56bb7041Schristos 	}
3267*56bb7041Schristos     }
3268*56bb7041Schristos 
3269*56bb7041Schristos   bfd_set_symtab (obfd, osympp, symcount);
3270*56bb7041Schristos 
3271*56bb7041Schristos   /* This has to happen before section positions are set.  */
3272*56bb7041Schristos   bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
3273*56bb7041Schristos 
3274*56bb7041Schristos   /* This has to happen after the symbol table has been set.  */
3275*56bb7041Schristos   bfd_map_over_sections (ibfd, copy_section, obfd);
3276*56bb7041Schristos 
3277*56bb7041Schristos   if (add_sections != NULL)
3278*56bb7041Schristos     {
3279*56bb7041Schristos       struct section_add *padd;
3280*56bb7041Schristos 
3281*56bb7041Schristos       for (padd = add_sections; padd != NULL; padd = padd->next)
3282*56bb7041Schristos 	{
3283*56bb7041Schristos 	  if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
3284*56bb7041Schristos 					  0, padd->size))
3285*56bb7041Schristos 	    {
3286*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
3287*56bb7041Schristos 	      return FALSE;
3288*56bb7041Schristos 	    }
3289*56bb7041Schristos 	}
3290*56bb7041Schristos     }
3291*56bb7041Schristos 
3292*56bb7041Schristos   if (update_sections != NULL)
3293*56bb7041Schristos     {
3294*56bb7041Schristos       struct section_add *pupdate;
3295*56bb7041Schristos 
3296*56bb7041Schristos       for (pupdate = update_sections;
3297*56bb7041Schristos 	   pupdate != NULL;
3298*56bb7041Schristos 	   pupdate = pupdate->next)
3299*56bb7041Schristos 	{
3300*56bb7041Schristos 	  osec = pupdate->section->output_section;
3301*56bb7041Schristos 	  if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
3302*56bb7041Schristos 					  0, pupdate->size))
3303*56bb7041Schristos 	    {
3304*56bb7041Schristos 	      bfd_nonfatal_message (NULL, obfd, osec, NULL);
3305*56bb7041Schristos 	      return FALSE;
3306*56bb7041Schristos 	    }
3307*56bb7041Schristos 	}
3308*56bb7041Schristos     }
3309*56bb7041Schristos 
3310*56bb7041Schristos   if (merged_note_sections != NULL)
3311*56bb7041Schristos     {
3312*56bb7041Schristos       merged_note_section * merged = NULL;
3313*56bb7041Schristos 
3314*56bb7041Schristos       for (osec = obfd->sections; osec != NULL; osec = osec->next)
3315*56bb7041Schristos 	{
3316*56bb7041Schristos 	  if (! is_mergeable_note_section (obfd, osec))
3317*56bb7041Schristos 	    continue;
3318*56bb7041Schristos 
3319*56bb7041Schristos 	  if (merged == NULL)
3320*56bb7041Schristos 	    merged = merged_note_sections;
3321*56bb7041Schristos 
3322*56bb7041Schristos 	  /* It is likely that output sections are in the same order
3323*56bb7041Schristos 	     as the input sections, but do not assume that this is
3324*56bb7041Schristos 	     the case.  */
3325*56bb7041Schristos 	  if (strcmp (bfd_section_name (merged->sec),
3326*56bb7041Schristos 		      bfd_section_name (osec)) != 0)
3327*56bb7041Schristos 	    {
3328*56bb7041Schristos 	      for (merged = merged_note_sections;
3329*56bb7041Schristos 		   merged != NULL;
3330*56bb7041Schristos 		   merged = merged->next)
3331*56bb7041Schristos 		if (strcmp (bfd_section_name (merged->sec),
3332*56bb7041Schristos 			    bfd_section_name (osec)) == 0)
3333*56bb7041Schristos 		  break;
3334*56bb7041Schristos 
3335*56bb7041Schristos 	      if (merged == NULL)
3336*56bb7041Schristos 		{
3337*56bb7041Schristos 		  bfd_nonfatal_message
3338*56bb7041Schristos 		    (NULL, obfd, osec,
3339*56bb7041Schristos 		     _("error: failed to locate merged notes"));
3340*56bb7041Schristos 		  continue;
3341*56bb7041Schristos 		}
3342*56bb7041Schristos 	    }
3343*56bb7041Schristos 
3344*56bb7041Schristos 	  if (merged->contents == NULL)
3345*56bb7041Schristos 	    {
3346*56bb7041Schristos 	      bfd_nonfatal_message
3347*56bb7041Schristos 		(NULL, obfd, osec,
3348*56bb7041Schristos 		 _("error: failed to merge notes"));
3349*56bb7041Schristos 	      continue;
3350*56bb7041Schristos 	    }
3351*56bb7041Schristos 
3352*56bb7041Schristos 	  if (! bfd_set_section_contents (obfd, osec, merged->contents, 0,
3353*56bb7041Schristos 					  merged->size))
3354*56bb7041Schristos 	    {
3355*56bb7041Schristos 	      bfd_nonfatal_message
3356*56bb7041Schristos 		(NULL, obfd, osec,
3357*56bb7041Schristos 		 _("error: failed to copy merged notes into output"));
3358*56bb7041Schristos 	      return FALSE;
3359*56bb7041Schristos 	    }
3360*56bb7041Schristos 
3361*56bb7041Schristos 	  merged = merged->next;
3362*56bb7041Schristos 	}
3363*56bb7041Schristos 
3364*56bb7041Schristos       /* Free the memory.  */
3365*56bb7041Schristos       merged_note_section * next;
3366*56bb7041Schristos       for (merged = merged_note_sections; merged != NULL; merged = next)
3367*56bb7041Schristos 	{
3368*56bb7041Schristos 	  next = merged->next;
3369*56bb7041Schristos 	  free (merged->contents);
3370*56bb7041Schristos 	  free (merged);
3371*56bb7041Schristos 	}
3372*56bb7041Schristos     }
3373*56bb7041Schristos   else if (merge_notes && ! is_strip)
3374*56bb7041Schristos     non_fatal (_("%s: Could not find any mergeable note sections"),
3375*56bb7041Schristos 	       bfd_get_filename (ibfd));
3376*56bb7041Schristos 
3377*56bb7041Schristos   if (gnu_debuglink_filename != NULL)
3378*56bb7041Schristos     {
3379*56bb7041Schristos       if (! bfd_fill_in_gnu_debuglink_section
3380*56bb7041Schristos 	  (obfd, gnu_debuglink_section, gnu_debuglink_filename))
3381*56bb7041Schristos 	{
3382*56bb7041Schristos 	  bfd_nonfatal_message (NULL, obfd, NULL,
3383*56bb7041Schristos 				_("cannot fill debug link section `%s'"),
3384*56bb7041Schristos 				gnu_debuglink_filename);
3385*56bb7041Schristos 	  return FALSE;
3386*56bb7041Schristos 	}
3387*56bb7041Schristos     }
3388*56bb7041Schristos 
3389*56bb7041Schristos   if (gaps != NULL)
3390*56bb7041Schristos     {
3391*56bb7041Schristos       bfd_byte *buf;
3392*56bb7041Schristos 
3393*56bb7041Schristos       /* Fill in the gaps.  */
3394*56bb7041Schristos       if (max_gap > 8192)
3395*56bb7041Schristos 	max_gap = 8192;
3396*56bb7041Schristos       buf = (bfd_byte *) xmalloc (max_gap);
3397*56bb7041Schristos       memset (buf, gap_fill, max_gap);
3398*56bb7041Schristos 
3399*56bb7041Schristos       for (i = 0; i < num_sec; i++)
3400*56bb7041Schristos 	{
3401*56bb7041Schristos 	  if (gaps[i] != 0)
3402*56bb7041Schristos 	    {
3403*56bb7041Schristos 	      bfd_size_type left;
3404*56bb7041Schristos 	      file_ptr off;
3405*56bb7041Schristos 
3406*56bb7041Schristos 	      left = gaps[i];
3407*56bb7041Schristos 	      off = bfd_section_size (osections[i]) - left;
3408*56bb7041Schristos 
3409*56bb7041Schristos 	      while (left > 0)
3410*56bb7041Schristos 		{
3411*56bb7041Schristos 		  bfd_size_type now;
3412*56bb7041Schristos 
3413*56bb7041Schristos 		  if (left > 8192)
3414*56bb7041Schristos 		    now = 8192;
3415*56bb7041Schristos 		  else
3416*56bb7041Schristos 		    now = left;
3417*56bb7041Schristos 
3418*56bb7041Schristos 		  if (! bfd_set_section_contents (obfd, osections[i], buf,
3419*56bb7041Schristos 						  off, now))
3420*56bb7041Schristos 		    {
3421*56bb7041Schristos 		      bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
3422*56bb7041Schristos 		      free (buf);
3423*56bb7041Schristos 		      return FALSE;
3424*56bb7041Schristos 		    }
3425*56bb7041Schristos 
3426*56bb7041Schristos 		  left -= now;
3427*56bb7041Schristos 		  off += now;
3428*56bb7041Schristos 		}
3429*56bb7041Schristos 	    }
3430*56bb7041Schristos 	}
3431*56bb7041Schristos 
3432*56bb7041Schristos       free (buf);
3433*56bb7041Schristos       free (gaps);
3434*56bb7041Schristos       gaps = NULL;
3435*56bb7041Schristos     }
3436*56bb7041Schristos 
3437*56bb7041Schristos   /* Allow the BFD backend to copy any private data it understands
3438*56bb7041Schristos      from the input BFD to the output BFD.  This is done last to
3439*56bb7041Schristos      permit the routine to look at the filtered symbol table, which is
3440*56bb7041Schristos      important for the ECOFF code at least.  */
3441*56bb7041Schristos   if (! bfd_copy_private_bfd_data (ibfd, obfd))
3442*56bb7041Schristos     {
3443*56bb7041Schristos       bfd_nonfatal_message (NULL, obfd, NULL,
3444*56bb7041Schristos 			    _("error copying private BFD data"));
3445*56bb7041Schristos       return FALSE;
3446*56bb7041Schristos     }
3447*56bb7041Schristos 
3448*56bb7041Schristos   /* Switch to the alternate machine code.  We have to do this at the
3449*56bb7041Schristos      very end, because we only initialize the header when we create
3450*56bb7041Schristos      the first section.  */
3451*56bb7041Schristos   if (use_alt_mach_code != 0)
3452*56bb7041Schristos     {
3453*56bb7041Schristos       if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
3454*56bb7041Schristos 	{
3455*56bb7041Schristos 	  non_fatal (_("this target does not support %lu alternative machine codes"),
3456*56bb7041Schristos 		     use_alt_mach_code);
3457*56bb7041Schristos 	  if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
3458*56bb7041Schristos 	    {
3459*56bb7041Schristos 	      non_fatal (_("treating that number as an absolute e_machine value instead"));
3460*56bb7041Schristos 	      elf_elfheader (obfd)->e_machine = use_alt_mach_code;
3461*56bb7041Schristos 	    }
3462*56bb7041Schristos 	  else
3463*56bb7041Schristos 	    non_fatal (_("ignoring the alternative value"));
3464*56bb7041Schristos 	}
3465*56bb7041Schristos     }
3466*56bb7041Schristos 
3467*56bb7041Schristos   return TRUE;
3468*56bb7041Schristos }
3469*56bb7041Schristos 
3470*56bb7041Schristos /* Read each archive element in turn from IBFD, copy the
3471*56bb7041Schristos    contents to temp file, and keep the temp file handle.
3472*56bb7041Schristos    If 'force_output_target' is TRUE then make sure that
3473*56bb7041Schristos    all elements in the new archive are of the type
3474*56bb7041Schristos    'output_target'.  */
3475*56bb7041Schristos 
3476*56bb7041Schristos static void
copy_archive(bfd * ibfd,bfd * obfd,const char * output_target,bfd_boolean force_output_target,const bfd_arch_info_type * input_arch)3477*56bb7041Schristos copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
3478*56bb7041Schristos 	      bfd_boolean force_output_target,
3479*56bb7041Schristos 	      const bfd_arch_info_type *input_arch)
3480*56bb7041Schristos {
3481*56bb7041Schristos   struct name_list
3482*56bb7041Schristos     {
3483*56bb7041Schristos       struct name_list *next;
3484*56bb7041Schristos       const char *name;
3485*56bb7041Schristos       bfd *obfd;
3486*56bb7041Schristos     } *list, *l;
3487*56bb7041Schristos   bfd **ptr = &obfd->archive_head;
3488*56bb7041Schristos   bfd *this_element;
3489*56bb7041Schristos   char *dir;
3490*56bb7041Schristos   const char *filename;
3491*56bb7041Schristos 
3492*56bb7041Schristos   /* PR 24281: It is not clear what should happen when copying a thin archive.
3493*56bb7041Schristos      One part is straight forward - if the output archive is in a different
3494*56bb7041Schristos      directory from the input archive then any relative paths in the library
3495*56bb7041Schristos      should be adjusted to the new location.  But if any transformation
3496*56bb7041Schristos      options are active (eg strip, rename, add, etc) then the implication is
3497*56bb7041Schristos      that these should be applied to the files pointed to by the archive.
3498*56bb7041Schristos      But since objcopy is not destructive, this means that new files must be
3499*56bb7041Schristos      created, and there is no guidance for the names of the new files.  (Plus
3500*56bb7041Schristos      this conflicts with one of the goals of thin libraries - only taking up
3501*56bb7041Schristos      a  minimal amount of space in the file system).
3502*56bb7041Schristos 
3503*56bb7041Schristos      So for now we fail if an attempt is made to copy such libraries.  */
3504*56bb7041Schristos   if (ibfd->is_thin_archive)
3505*56bb7041Schristos     {
3506*56bb7041Schristos       status = 1;
3507*56bb7041Schristos       bfd_set_error (bfd_error_invalid_operation);
3508*56bb7041Schristos       bfd_nonfatal_message (NULL, ibfd, NULL,
3509*56bb7041Schristos 			    _("sorry: copying thin archives is not currently supported"));
3510*56bb7041Schristos       return;
3511*56bb7041Schristos     }
3512*56bb7041Schristos 
3513*56bb7041Schristos   /* Make a temp directory to hold the contents.  */
3514*56bb7041Schristos   dir = make_tempdir (bfd_get_filename (obfd));
3515*56bb7041Schristos   if (dir == NULL)
3516*56bb7041Schristos     fatal (_("cannot create tempdir for archive copying (error: %s)"),
3517*56bb7041Schristos 	   strerror (errno));
3518*56bb7041Schristos 
3519*56bb7041Schristos   if (strip_symbols == STRIP_ALL)
3520*56bb7041Schristos     obfd->has_armap = FALSE;
3521*56bb7041Schristos   else
3522*56bb7041Schristos     obfd->has_armap = ibfd->has_armap;
3523*56bb7041Schristos   obfd->is_thin_archive = ibfd->is_thin_archive;
3524*56bb7041Schristos 
3525*56bb7041Schristos   if (deterministic)
3526*56bb7041Schristos     obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
3527*56bb7041Schristos 
3528*56bb7041Schristos   list = NULL;
3529*56bb7041Schristos 
3530*56bb7041Schristos   this_element = bfd_openr_next_archived_file (ibfd, NULL);
3531*56bb7041Schristos 
3532*56bb7041Schristos   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
3533*56bb7041Schristos     {
3534*56bb7041Schristos       status = 1;
3535*56bb7041Schristos       bfd_nonfatal_message (NULL, obfd, NULL, NULL);
3536*56bb7041Schristos       goto cleanup_and_exit;
3537*56bb7041Schristos     }
3538*56bb7041Schristos 
3539*56bb7041Schristos   while (!status && this_element != NULL)
3540*56bb7041Schristos     {
3541*56bb7041Schristos       char *output_name;
3542*56bb7041Schristos       bfd *output_bfd;
3543*56bb7041Schristos       bfd *last_element;
3544*56bb7041Schristos       struct stat buf;
3545*56bb7041Schristos       int stat_status = 0;
3546*56bb7041Schristos       bfd_boolean del = TRUE;
3547*56bb7041Schristos       bfd_boolean ok_object;
3548*56bb7041Schristos 
3549*56bb7041Schristos       /* PR binutils/17533: Do not allow directory traversal
3550*56bb7041Schristos 	 outside of the current directory tree by archive members.  */
3551*56bb7041Schristos       if (! is_valid_archive_path (bfd_get_filename (this_element)))
3552*56bb7041Schristos 	{
3553*56bb7041Schristos 	  non_fatal (_("illegal pathname found in archive member: %s"),
3554*56bb7041Schristos 		     bfd_get_filename (this_element));
3555*56bb7041Schristos 	  status = 1;
3556*56bb7041Schristos 	  goto cleanup_and_exit;
3557*56bb7041Schristos 	}
3558*56bb7041Schristos 
3559*56bb7041Schristos       /* Create an output file for this member.  */
3560*56bb7041Schristos       output_name = concat (dir, "/",
3561*56bb7041Schristos 			    bfd_get_filename (this_element), (char *) 0);
3562*56bb7041Schristos 
3563*56bb7041Schristos       /* If the file already exists, make another temp dir.  */
3564*56bb7041Schristos       if (stat (output_name, &buf) >= 0)
3565*56bb7041Schristos 	{
3566*56bb7041Schristos 	  char * tmpdir = make_tempdir (output_name);
3567*56bb7041Schristos 
3568*56bb7041Schristos 	  free (output_name);
3569*56bb7041Schristos 	  if (tmpdir == NULL)
3570*56bb7041Schristos 	    {
3571*56bb7041Schristos 	      non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
3572*56bb7041Schristos 			 strerror (errno));
3573*56bb7041Schristos 	      status = 1;
3574*56bb7041Schristos 	      goto cleanup_and_exit;
3575*56bb7041Schristos 	    }
3576*56bb7041Schristos 
3577*56bb7041Schristos 	  l = (struct name_list *) xmalloc (sizeof (struct name_list));
3578*56bb7041Schristos 	  l->name = tmpdir;
3579*56bb7041Schristos 	  l->next = list;
3580*56bb7041Schristos 	  l->obfd = NULL;
3581*56bb7041Schristos 	  list = l;
3582*56bb7041Schristos 	  output_name = concat (tmpdir, "/",
3583*56bb7041Schristos 				bfd_get_filename (this_element), (char *) 0);
3584*56bb7041Schristos 	}
3585*56bb7041Schristos 
3586*56bb7041Schristos       if (preserve_dates)
3587*56bb7041Schristos 	{
3588*56bb7041Schristos 	  stat_status = bfd_stat_arch_elt (this_element, &buf);
3589*56bb7041Schristos 
3590*56bb7041Schristos 	  if (stat_status != 0)
3591*56bb7041Schristos 	    non_fatal (_("internal stat error on %s"),
3592*56bb7041Schristos 		       bfd_get_filename (this_element));
3593*56bb7041Schristos 	}
3594*56bb7041Schristos 
3595*56bb7041Schristos       l = (struct name_list *) xmalloc (sizeof (struct name_list));
3596*56bb7041Schristos       l->name = output_name;
3597*56bb7041Schristos       l->next = list;
3598*56bb7041Schristos       l->obfd = NULL;
3599*56bb7041Schristos       list = l;
3600*56bb7041Schristos 
3601*56bb7041Schristos       ok_object = bfd_check_format (this_element, bfd_object);
3602*56bb7041Schristos       if (!ok_object)
3603*56bb7041Schristos 	bfd_nonfatal_message (NULL, this_element, NULL,
3604*56bb7041Schristos 			      _("Unable to recognise the format of file"));
3605*56bb7041Schristos 
3606*56bb7041Schristos       /* PR binutils/3110: Cope with archives
3607*56bb7041Schristos 	 containing multiple target types.  */
3608*56bb7041Schristos       if (force_output_target || !ok_object)
3609*56bb7041Schristos 	output_bfd = bfd_openw (output_name, output_target);
3610*56bb7041Schristos       else
3611*56bb7041Schristos 	output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
3612*56bb7041Schristos 
3613*56bb7041Schristos       if (output_bfd == NULL)
3614*56bb7041Schristos 	{
3615*56bb7041Schristos 	  bfd_nonfatal_message (output_name, NULL, NULL, NULL);
3616*56bb7041Schristos 	  status = 1;
3617*56bb7041Schristos 	  goto cleanup_and_exit;
3618*56bb7041Schristos 	}
3619*56bb7041Schristos 
3620*56bb7041Schristos       if (ok_object)
3621*56bb7041Schristos 	{
3622*56bb7041Schristos 	  del = !copy_object (this_element, output_bfd, input_arch);
3623*56bb7041Schristos 
3624*56bb7041Schristos 	  if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
3625*56bb7041Schristos 	    /* Try again as an unknown object file.  */
3626*56bb7041Schristos 	    ok_object = FALSE;
3627*56bb7041Schristos 	  else if (!bfd_close (output_bfd))
3628*56bb7041Schristos 	    {
3629*56bb7041Schristos 	      bfd_nonfatal_message (output_name, NULL, NULL, NULL);
3630*56bb7041Schristos 	      /* Error in new object file. Don't change archive.  */
3631*56bb7041Schristos 	      status = 1;
3632*56bb7041Schristos 	    }
3633*56bb7041Schristos 	}
3634*56bb7041Schristos 
3635*56bb7041Schristos       if (!ok_object)
3636*56bb7041Schristos 	{
3637*56bb7041Schristos 	  del = !copy_unknown_object (this_element, output_bfd);
3638*56bb7041Schristos 	  if (!bfd_close_all_done (output_bfd))
3639*56bb7041Schristos 	    {
3640*56bb7041Schristos 	      bfd_nonfatal_message (output_name, NULL, NULL, NULL);
3641*56bb7041Schristos 	      /* Error in new object file. Don't change archive.  */
3642*56bb7041Schristos 	      status = 1;
3643*56bb7041Schristos 	    }
3644*56bb7041Schristos 	}
3645*56bb7041Schristos 
3646*56bb7041Schristos       if (del)
3647*56bb7041Schristos 	{
3648*56bb7041Schristos 	  unlink (output_name);
3649*56bb7041Schristos 	  status = 1;
3650*56bb7041Schristos 	}
3651*56bb7041Schristos       else
3652*56bb7041Schristos 	{
3653*56bb7041Schristos 	  if (preserve_dates && stat_status == 0)
3654*56bb7041Schristos 	    set_times (output_name, &buf);
3655*56bb7041Schristos 
3656*56bb7041Schristos 	  /* Open the newly output file and attach to our list.  */
3657*56bb7041Schristos 	  output_bfd = bfd_openr (output_name, output_target);
3658*56bb7041Schristos 
3659*56bb7041Schristos 	  l->obfd = output_bfd;
3660*56bb7041Schristos 
3661*56bb7041Schristos 	  *ptr = output_bfd;
3662*56bb7041Schristos 	  ptr = &output_bfd->archive_next;
3663*56bb7041Schristos 
3664*56bb7041Schristos 	  last_element = this_element;
3665*56bb7041Schristos 
3666*56bb7041Schristos 	  this_element = bfd_openr_next_archived_file (ibfd, last_element);
3667*56bb7041Schristos 
3668*56bb7041Schristos 	  bfd_close (last_element);
3669*56bb7041Schristos 	}
3670*56bb7041Schristos     }
3671*56bb7041Schristos   *ptr = NULL;
3672*56bb7041Schristos 
3673*56bb7041Schristos   filename = bfd_get_filename (obfd);
3674*56bb7041Schristos   if (!bfd_close (obfd))
3675*56bb7041Schristos     {
3676*56bb7041Schristos       status = 1;
3677*56bb7041Schristos       bfd_nonfatal_message (filename, NULL, NULL, NULL);
3678*56bb7041Schristos     }
3679*56bb7041Schristos 
3680*56bb7041Schristos   filename = bfd_get_filename (ibfd);
3681*56bb7041Schristos   if (!bfd_close (ibfd))
3682*56bb7041Schristos     {
3683*56bb7041Schristos       status = 1;
3684*56bb7041Schristos       bfd_nonfatal_message (filename, NULL, NULL, NULL);
3685*56bb7041Schristos     }
3686*56bb7041Schristos 
3687*56bb7041Schristos  cleanup_and_exit:
3688*56bb7041Schristos   /* Delete all the files that we opened.  */
3689*56bb7041Schristos   {
3690*56bb7041Schristos     struct name_list * next;
3691*56bb7041Schristos 
3692*56bb7041Schristos     for (l = list; l != NULL; l = next)
3693*56bb7041Schristos       {
3694*56bb7041Schristos 	if (l->obfd == NULL)
3695*56bb7041Schristos 	  rmdir (l->name);
3696*56bb7041Schristos 	else
3697*56bb7041Schristos 	  {
3698*56bb7041Schristos 	    bfd_close (l->obfd);
3699*56bb7041Schristos 	    unlink (l->name);
3700*56bb7041Schristos 	  }
3701*56bb7041Schristos 	next = l->next;
3702*56bb7041Schristos 	free (l);
3703*56bb7041Schristos       }
3704*56bb7041Schristos   }
3705*56bb7041Schristos 
3706*56bb7041Schristos   rmdir (dir);
3707*56bb7041Schristos }
3708*56bb7041Schristos 
3709*56bb7041Schristos static void
set_long_section_mode(bfd * output_bfd,bfd * input_bfd,enum long_section_name_handling style)3710*56bb7041Schristos set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
3711*56bb7041Schristos {
3712*56bb7041Schristos   /* This is only relevant to Coff targets.  */
3713*56bb7041Schristos   if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
3714*56bb7041Schristos     {
3715*56bb7041Schristos       if (style == KEEP
3716*56bb7041Schristos 	  && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
3717*56bb7041Schristos 	style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
3718*56bb7041Schristos       bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
3719*56bb7041Schristos     }
3720*56bb7041Schristos }
3721*56bb7041Schristos 
3722*56bb7041Schristos /* The top-level control.  */
3723*56bb7041Schristos 
3724*56bb7041Schristos static void
copy_file(const char * input_filename,const char * output_filename,const char * input_target,const char * output_target,const bfd_arch_info_type * input_arch)3725*56bb7041Schristos copy_file (const char *input_filename, const char *output_filename,
3726*56bb7041Schristos 	   const char *input_target,   const char *output_target,
3727*56bb7041Schristos 	   const bfd_arch_info_type *input_arch)
3728*56bb7041Schristos {
3729*56bb7041Schristos   bfd *ibfd;
3730*56bb7041Schristos   char **obj_matching;
3731*56bb7041Schristos   char **core_matching;
3732*56bb7041Schristos   off_t size = get_file_size (input_filename);
3733*56bb7041Schristos 
3734*56bb7041Schristos   if (size < 1)
3735*56bb7041Schristos     {
3736*56bb7041Schristos       if (size == 0)
3737*56bb7041Schristos 	non_fatal (_("error: the input file '%s' is empty"),
3738*56bb7041Schristos 		   input_filename);
3739*56bb7041Schristos       status = 1;
3740*56bb7041Schristos       return;
3741*56bb7041Schristos     }
3742*56bb7041Schristos 
3743*56bb7041Schristos   /* To allow us to do "strip *" without dying on the first
3744*56bb7041Schristos      non-object file, failures are nonfatal.  */
3745*56bb7041Schristos   ibfd = bfd_openr (input_filename, input_target);
3746*56bb7041Schristos   if (ibfd == NULL)
3747*56bb7041Schristos     {
3748*56bb7041Schristos       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
3749*56bb7041Schristos       status = 1;
3750*56bb7041Schristos       return;
3751*56bb7041Schristos     }
3752*56bb7041Schristos 
3753*56bb7041Schristos   switch (do_debug_sections)
3754*56bb7041Schristos     {
3755*56bb7041Schristos     case compress:
3756*56bb7041Schristos     case compress_zlib:
3757*56bb7041Schristos     case compress_gnu_zlib:
3758*56bb7041Schristos     case compress_gabi_zlib:
3759*56bb7041Schristos       ibfd->flags |= BFD_COMPRESS;
3760*56bb7041Schristos       /* Don't check if input is ELF here since this information is
3761*56bb7041Schristos 	 only available after bfd_check_format_matches is called.  */
3762*56bb7041Schristos       if (do_debug_sections != compress_gnu_zlib)
3763*56bb7041Schristos 	ibfd->flags |= BFD_COMPRESS_GABI;
3764*56bb7041Schristos       break;
3765*56bb7041Schristos     case decompress:
3766*56bb7041Schristos       ibfd->flags |= BFD_DECOMPRESS;
3767*56bb7041Schristos       break;
3768*56bb7041Schristos     default:
3769*56bb7041Schristos       break;
3770*56bb7041Schristos     }
3771*56bb7041Schristos 
3772*56bb7041Schristos   switch (do_elf_stt_common)
3773*56bb7041Schristos     {
3774*56bb7041Schristos     case elf_stt_common:
3775*56bb7041Schristos       ibfd->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
3776*56bb7041Schristos       break;
3777*56bb7041Schristos       break;
3778*56bb7041Schristos     case no_elf_stt_common:
3779*56bb7041Schristos       ibfd->flags |= BFD_CONVERT_ELF_COMMON;
3780*56bb7041Schristos       break;
3781*56bb7041Schristos     default:
3782*56bb7041Schristos       break;
3783*56bb7041Schristos     }
3784*56bb7041Schristos 
3785*56bb7041Schristos   if (bfd_check_format (ibfd, bfd_archive))
3786*56bb7041Schristos     {
3787*56bb7041Schristos       bfd_boolean force_output_target;
3788*56bb7041Schristos       bfd *obfd;
3789*56bb7041Schristos 
3790*56bb7041Schristos       /* bfd_get_target does not return the correct value until
3791*56bb7041Schristos 	 bfd_check_format succeeds.  */
3792*56bb7041Schristos       if (output_target == NULL)
3793*56bb7041Schristos 	{
3794*56bb7041Schristos 	  output_target = bfd_get_target (ibfd);
3795*56bb7041Schristos 	  force_output_target = FALSE;
3796*56bb7041Schristos 	}
3797*56bb7041Schristos       else
3798*56bb7041Schristos 	force_output_target = TRUE;
3799*56bb7041Schristos 
3800*56bb7041Schristos       obfd = bfd_openw (output_filename, output_target);
3801*56bb7041Schristos       if (obfd == NULL)
3802*56bb7041Schristos 	{
3803*56bb7041Schristos 	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
3804*56bb7041Schristos 	  status = 1;
3805*56bb7041Schristos 	  return;
3806*56bb7041Schristos 	}
3807*56bb7041Schristos 
3808*56bb7041Schristos       if (gnu_debuglink_filename != NULL)
3809*56bb7041Schristos 	{
3810*56bb7041Schristos 	  non_fatal (_("--add-gnu-debuglink ignored for archive %s"),
3811*56bb7041Schristos 		     bfd_get_filename (ibfd));
3812*56bb7041Schristos 	  gnu_debuglink_filename = NULL;
3813*56bb7041Schristos 	}
3814*56bb7041Schristos 
3815*56bb7041Schristos       /* This is a no-op on non-Coff targets.  */
3816*56bb7041Schristos       set_long_section_mode (obfd, ibfd, long_section_names);
3817*56bb7041Schristos 
3818*56bb7041Schristos       copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
3819*56bb7041Schristos     }
3820*56bb7041Schristos   else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
3821*56bb7041Schristos     {
3822*56bb7041Schristos       bfd *obfd;
3823*56bb7041Schristos     do_copy:
3824*56bb7041Schristos 
3825*56bb7041Schristos       /* bfd_get_target does not return the correct value until
3826*56bb7041Schristos 	 bfd_check_format succeeds.  */
3827*56bb7041Schristos       if (output_target == NULL)
3828*56bb7041Schristos 	output_target = bfd_get_target (ibfd);
3829*56bb7041Schristos 
3830*56bb7041Schristos       obfd = bfd_openw (output_filename, output_target);
3831*56bb7041Schristos       if (obfd == NULL)
3832*56bb7041Schristos  	{
3833*56bb7041Schristos  	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
3834*56bb7041Schristos  	  status = 1;
3835*56bb7041Schristos  	  return;
3836*56bb7041Schristos  	}
3837*56bb7041Schristos       /* This is a no-op on non-Coff targets.  */
3838*56bb7041Schristos       set_long_section_mode (obfd, ibfd, long_section_names);
3839*56bb7041Schristos 
3840*56bb7041Schristos       if (! copy_object (ibfd, obfd, input_arch))
3841*56bb7041Schristos 	status = 1;
3842*56bb7041Schristos 
3843*56bb7041Schristos       /* PR 17512: file: 0f15796a.
3844*56bb7041Schristos 	 If the file could not be copied it may not be in a writeable
3845*56bb7041Schristos 	 state.  So use bfd_close_all_done to avoid the possibility of
3846*56bb7041Schristos 	 writing uninitialised data into the file.  */
3847*56bb7041Schristos       if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd)))
3848*56bb7041Schristos 	{
3849*56bb7041Schristos 	  status = 1;
3850*56bb7041Schristos 	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
3851*56bb7041Schristos 	  return;
3852*56bb7041Schristos 	}
3853*56bb7041Schristos 
3854*56bb7041Schristos       if (!bfd_close (ibfd))
3855*56bb7041Schristos 	{
3856*56bb7041Schristos 	  status = 1;
3857*56bb7041Schristos 	  bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
3858*56bb7041Schristos 	  return;
3859*56bb7041Schristos 	}
3860*56bb7041Schristos     }
3861*56bb7041Schristos   else
3862*56bb7041Schristos     {
3863*56bb7041Schristos       bfd_error_type obj_error = bfd_get_error ();
3864*56bb7041Schristos       bfd_error_type core_error;
3865*56bb7041Schristos 
3866*56bb7041Schristos       if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
3867*56bb7041Schristos 	{
3868*56bb7041Schristos 	  /* This probably can't happen..  */
3869*56bb7041Schristos 	  if (obj_error == bfd_error_file_ambiguously_recognized)
3870*56bb7041Schristos 	    free (obj_matching);
3871*56bb7041Schristos 	  goto do_copy;
3872*56bb7041Schristos 	}
3873*56bb7041Schristos 
3874*56bb7041Schristos       core_error = bfd_get_error ();
3875*56bb7041Schristos       /* Report the object error in preference to the core error.  */
3876*56bb7041Schristos       if (obj_error != core_error)
3877*56bb7041Schristos 	bfd_set_error (obj_error);
3878*56bb7041Schristos 
3879*56bb7041Schristos       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
3880*56bb7041Schristos 
3881*56bb7041Schristos       if (obj_error == bfd_error_file_ambiguously_recognized)
3882*56bb7041Schristos 	{
3883*56bb7041Schristos 	  list_matching_formats (obj_matching);
3884*56bb7041Schristos 	  free (obj_matching);
3885*56bb7041Schristos 	}
3886*56bb7041Schristos       if (core_error == bfd_error_file_ambiguously_recognized)
3887*56bb7041Schristos 	{
3888*56bb7041Schristos 	  list_matching_formats (core_matching);
3889*56bb7041Schristos 	  free (core_matching);
3890*56bb7041Schristos 	}
3891*56bb7041Schristos 
3892*56bb7041Schristos       status = 1;
3893*56bb7041Schristos     }
3894*56bb7041Schristos }
3895*56bb7041Schristos 
3896*56bb7041Schristos /* Add a name to the section renaming list.  */
3897*56bb7041Schristos 
3898*56bb7041Schristos static void
add_section_rename(const char * old_name,const char * new_name,flagword flags)3899*56bb7041Schristos add_section_rename (const char * old_name, const char * new_name,
3900*56bb7041Schristos 		    flagword flags)
3901*56bb7041Schristos {
3902*56bb7041Schristos   section_rename * srename;
3903*56bb7041Schristos 
3904*56bb7041Schristos   /* Check for conflicts first.  */
3905*56bb7041Schristos   for (srename = section_rename_list; srename != NULL; srename = srename->next)
3906*56bb7041Schristos     if (strcmp (srename->old_name, old_name) == 0)
3907*56bb7041Schristos       {
3908*56bb7041Schristos 	/* Silently ignore duplicate definitions.  */
3909*56bb7041Schristos 	if (strcmp (srename->new_name, new_name) == 0
3910*56bb7041Schristos 	    && srename->flags == flags)
3911*56bb7041Schristos 	  return;
3912*56bb7041Schristos 
3913*56bb7041Schristos 	fatal (_("Multiple renames of section %s"), old_name);
3914*56bb7041Schristos       }
3915*56bb7041Schristos 
3916*56bb7041Schristos   srename = (section_rename *) xmalloc (sizeof (* srename));
3917*56bb7041Schristos 
3918*56bb7041Schristos   srename->old_name = old_name;
3919*56bb7041Schristos   srename->new_name = new_name;
3920*56bb7041Schristos   srename->flags    = flags;
3921*56bb7041Schristos   srename->next     = section_rename_list;
3922*56bb7041Schristos 
3923*56bb7041Schristos   section_rename_list = srename;
3924*56bb7041Schristos }
3925*56bb7041Schristos 
3926*56bb7041Schristos /* Check the section rename list for a new name of the input section
3927*56bb7041Schristos    called OLD_NAME.  Returns the new name if one is found and sets
3928*56bb7041Schristos    RETURNED_FLAGS if non-NULL to the flags to be used for this section.  */
3929*56bb7041Schristos 
3930*56bb7041Schristos static const char *
find_section_rename(const char * old_name,flagword * returned_flags)3931*56bb7041Schristos find_section_rename (const char *old_name, flagword *returned_flags)
3932*56bb7041Schristos {
3933*56bb7041Schristos   const section_rename *srename;
3934*56bb7041Schristos 
3935*56bb7041Schristos   for (srename = section_rename_list; srename != NULL; srename = srename->next)
3936*56bb7041Schristos     if (strcmp (srename->old_name, old_name) == 0)
3937*56bb7041Schristos       {
3938*56bb7041Schristos 	if (returned_flags != NULL && srename->flags != (flagword) -1)
3939*56bb7041Schristos 	  *returned_flags = srename->flags;
3940*56bb7041Schristos 
3941*56bb7041Schristos 	return srename->new_name;
3942*56bb7041Schristos       }
3943*56bb7041Schristos 
3944*56bb7041Schristos   return old_name;
3945*56bb7041Schristos }
3946*56bb7041Schristos 
3947*56bb7041Schristos /* Once each of the sections is copied, we may still need to do some
3948*56bb7041Schristos    finalization work for private section headers.  Do that here.  */
3949*56bb7041Schristos 
3950*56bb7041Schristos static void
setup_bfd_headers(bfd * ibfd,bfd * obfd)3951*56bb7041Schristos setup_bfd_headers (bfd *ibfd, bfd *obfd)
3952*56bb7041Schristos {
3953*56bb7041Schristos   /* Allow the BFD backend to copy any private data it understands
3954*56bb7041Schristos      from the input section to the output section.  */
3955*56bb7041Schristos   if (! bfd_copy_private_header_data (ibfd, obfd))
3956*56bb7041Schristos     {
3957*56bb7041Schristos       status = 1;
3958*56bb7041Schristos       bfd_nonfatal_message (NULL, ibfd, NULL,
3959*56bb7041Schristos 			    _("error in private header data"));
3960*56bb7041Schristos       return;
3961*56bb7041Schristos     }
3962*56bb7041Schristos 
3963*56bb7041Schristos   /* All went well.  */
3964*56bb7041Schristos   return;
3965*56bb7041Schristos }
3966*56bb7041Schristos 
3967*56bb7041Schristos /* Create a section in OBFD with the same
3968*56bb7041Schristos    name and attributes as ISECTION in IBFD.  */
3969*56bb7041Schristos 
3970*56bb7041Schristos static void
setup_section(bfd * ibfd,sec_ptr isection,void * obfdarg)3971*56bb7041Schristos setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3972*56bb7041Schristos {
3973*56bb7041Schristos   bfd *obfd = (bfd *) obfdarg;
3974*56bb7041Schristos   struct section_list *p;
3975*56bb7041Schristos   sec_ptr osection;
3976*56bb7041Schristos   bfd_size_type size;
3977*56bb7041Schristos   bfd_vma vma;
3978*56bb7041Schristos   bfd_vma lma;
3979*56bb7041Schristos   flagword flags;
3980*56bb7041Schristos   const char *err;
3981*56bb7041Schristos   const char * name;
3982*56bb7041Schristos   const char * new_name;
3983*56bb7041Schristos   char *prefix = NULL;
3984*56bb7041Schristos   bfd_boolean make_nobits;
3985*56bb7041Schristos   unsigned int alignment;
3986*56bb7041Schristos 
3987*56bb7041Schristos   if (is_strip_section (ibfd, isection))
3988*56bb7041Schristos     return;
3989*56bb7041Schristos 
3990*56bb7041Schristos   /* Get the, possibly new, name of the output section.  */
3991*56bb7041Schristos   name = bfd_section_name (isection);
3992*56bb7041Schristos   flags = bfd_section_flags (isection);
3993*56bb7041Schristos   if (bfd_get_flavour (ibfd) != bfd_get_flavour (obfd))
3994*56bb7041Schristos     {
3995*56bb7041Schristos       flags &= bfd_applicable_section_flags (ibfd);
3996*56bb7041Schristos       flags &= bfd_applicable_section_flags (obfd);
3997*56bb7041Schristos     }
3998*56bb7041Schristos   new_name = find_section_rename (name, &flags);
3999*56bb7041Schristos   if (new_name != name)
4000*56bb7041Schristos     {
4001*56bb7041Schristos       name = new_name;
4002*56bb7041Schristos       flags = check_new_section_flags (flags, obfd, name);
4003*56bb7041Schristos     }
4004*56bb7041Schristos 
4005*56bb7041Schristos   /* Prefix sections.  */
4006*56bb7041Schristos   if (prefix_alloc_sections_string
4007*56bb7041Schristos       && (bfd_section_flags (isection) & SEC_ALLOC) != 0)
4008*56bb7041Schristos     prefix = prefix_alloc_sections_string;
4009*56bb7041Schristos   else if (prefix_sections_string)
4010*56bb7041Schristos     prefix = prefix_sections_string;
4011*56bb7041Schristos 
4012*56bb7041Schristos   if (prefix)
4013*56bb7041Schristos     {
4014*56bb7041Schristos       char *n;
4015*56bb7041Schristos 
4016*56bb7041Schristos       n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
4017*56bb7041Schristos       strcpy (n, prefix);
4018*56bb7041Schristos       strcat (n, name);
4019*56bb7041Schristos       name = n;
4020*56bb7041Schristos     }
4021*56bb7041Schristos 
4022*56bb7041Schristos   make_nobits = FALSE;
4023*56bb7041Schristos 
4024*56bb7041Schristos   p = find_section_list (bfd_section_name (isection), FALSE,
4025*56bb7041Schristos 			 SECTION_CONTEXT_SET_FLAGS);
4026*56bb7041Schristos   if (p != NULL)
4027*56bb7041Schristos     {
4028*56bb7041Schristos       flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
4029*56bb7041Schristos       flags = check_new_section_flags (flags, obfd, bfd_section_name (isection));
4030*56bb7041Schristos     }
4031*56bb7041Schristos   else if (strip_symbols == STRIP_NONDEBUG
4032*56bb7041Schristos 	   && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
4033*56bb7041Schristos 	   && !is_nondebug_keep_contents_section (ibfd, isection))
4034*56bb7041Schristos     {
4035*56bb7041Schristos       flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
4036*56bb7041Schristos       if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
4037*56bb7041Schristos 	{
4038*56bb7041Schristos 	  make_nobits = TRUE;
4039*56bb7041Schristos 
4040*56bb7041Schristos 	  /* Twiddle the input section flags so that it seems to
4041*56bb7041Schristos 	     elf.c:copy_private_bfd_data that section flags have not
4042*56bb7041Schristos 	     changed between input and output sections.  This hack
4043*56bb7041Schristos 	     prevents wholesale rewriting of the program headers.  */
4044*56bb7041Schristos 	  isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
4045*56bb7041Schristos 	}
4046*56bb7041Schristos     }
4047*56bb7041Schristos 
4048*56bb7041Schristos   osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
4049*56bb7041Schristos 
4050*56bb7041Schristos   if (osection == NULL)
4051*56bb7041Schristos     {
4052*56bb7041Schristos       err = _("failed to create output section");
4053*56bb7041Schristos       goto loser;
4054*56bb7041Schristos     }
4055*56bb7041Schristos 
4056*56bb7041Schristos   if (make_nobits)
4057*56bb7041Schristos     elf_section_type (osection) = SHT_NOBITS;
4058*56bb7041Schristos 
4059*56bb7041Schristos   size = bfd_section_size (isection);
4060*56bb7041Schristos   size = bfd_convert_section_size (ibfd, isection, obfd, size);
4061*56bb7041Schristos   if (copy_byte >= 0)
4062*56bb7041Schristos     size = (size + interleave - 1) / interleave * copy_width;
4063*56bb7041Schristos   else if (extract_symbol)
4064*56bb7041Schristos     size = 0;
4065*56bb7041Schristos   if (!bfd_set_section_size (osection, size))
4066*56bb7041Schristos     {
4067*56bb7041Schristos       err = _("failed to set size");
4068*56bb7041Schristos       goto loser;
4069*56bb7041Schristos     }
4070*56bb7041Schristos 
4071*56bb7041Schristos   vma = bfd_section_vma (isection);
4072*56bb7041Schristos   p = find_section_list (bfd_section_name (isection), FALSE,
4073*56bb7041Schristos 			 SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
4074*56bb7041Schristos   if (p != NULL)
4075*56bb7041Schristos     {
4076*56bb7041Schristos       if (p->context & SECTION_CONTEXT_SET_VMA)
4077*56bb7041Schristos 	vma = p->vma_val;
4078*56bb7041Schristos       else
4079*56bb7041Schristos 	vma += p->vma_val;
4080*56bb7041Schristos     }
4081*56bb7041Schristos   else
4082*56bb7041Schristos     vma += change_section_address;
4083*56bb7041Schristos 
4084*56bb7041Schristos   if (!bfd_set_section_vma (osection, vma))
4085*56bb7041Schristos     {
4086*56bb7041Schristos       err = _("failed to set vma");
4087*56bb7041Schristos       goto loser;
4088*56bb7041Schristos     }
4089*56bb7041Schristos 
4090*56bb7041Schristos   lma = isection->lma;
4091*56bb7041Schristos   p = find_section_list (bfd_section_name (isection), FALSE,
4092*56bb7041Schristos 			 SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
4093*56bb7041Schristos   if (p != NULL)
4094*56bb7041Schristos     {
4095*56bb7041Schristos       if (p->context & SECTION_CONTEXT_ALTER_LMA)
4096*56bb7041Schristos 	lma += p->lma_val;
4097*56bb7041Schristos       else
4098*56bb7041Schristos 	lma = p->lma_val;
4099*56bb7041Schristos     }
4100*56bb7041Schristos   else
4101*56bb7041Schristos     lma += change_section_address;
4102*56bb7041Schristos 
4103*56bb7041Schristos   osection->lma = lma;
4104*56bb7041Schristos 
4105*56bb7041Schristos   p = find_section_list (bfd_section_name (isection), FALSE,
4106*56bb7041Schristos 			 SECTION_CONTEXT_SET_ALIGNMENT);
4107*56bb7041Schristos   if (p != NULL)
4108*56bb7041Schristos     alignment = p->alignment;
4109*56bb7041Schristos   else
4110*56bb7041Schristos     alignment = bfd_section_alignment (isection);
4111*56bb7041Schristos 
4112*56bb7041Schristos   /* FIXME: This is probably not enough.  If we change the LMA we
4113*56bb7041Schristos      may have to recompute the header for the file as well.  */
4114*56bb7041Schristos   if (!bfd_set_section_alignment (osection, alignment))
4115*56bb7041Schristos     {
4116*56bb7041Schristos       err = _("failed to set alignment");
4117*56bb7041Schristos       goto loser;
4118*56bb7041Schristos     }
4119*56bb7041Schristos 
4120*56bb7041Schristos   /* Copy merge entity size.  */
4121*56bb7041Schristos   osection->entsize = isection->entsize;
4122*56bb7041Schristos 
4123*56bb7041Schristos   /* Copy compress status.  */
4124*56bb7041Schristos   osection->compress_status = isection->compress_status;
4125*56bb7041Schristos 
4126*56bb7041Schristos   /* This used to be mangle_section; we do here to avoid using
4127*56bb7041Schristos      bfd_get_section_by_name since some formats allow multiple
4128*56bb7041Schristos      sections with the same name.  */
4129*56bb7041Schristos   isection->output_section = osection;
4130*56bb7041Schristos   isection->output_offset = 0;
4131*56bb7041Schristos 
4132*56bb7041Schristos   if ((isection->flags & SEC_GROUP) != 0)
4133*56bb7041Schristos     {
4134*56bb7041Schristos       asymbol *gsym = group_signature (isection);
4135*56bb7041Schristos 
4136*56bb7041Schristos       if (gsym != NULL)
4137*56bb7041Schristos 	{
4138*56bb7041Schristos 	  gsym->flags |= BSF_KEEP;
4139*56bb7041Schristos 	  if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour)
4140*56bb7041Schristos 	    elf_group_id (isection) = gsym;
4141*56bb7041Schristos 	}
4142*56bb7041Schristos     }
4143*56bb7041Schristos 
4144*56bb7041Schristos   /* Allow the BFD backend to copy any private data it understands
4145*56bb7041Schristos      from the input section to the output section.  */
4146*56bb7041Schristos   if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
4147*56bb7041Schristos     {
4148*56bb7041Schristos       err = _("failed to copy private data");
4149*56bb7041Schristos       goto loser;
4150*56bb7041Schristos     }
4151*56bb7041Schristos 
4152*56bb7041Schristos   /* All went well.  */
4153*56bb7041Schristos   return;
4154*56bb7041Schristos 
4155*56bb7041Schristos  loser:
4156*56bb7041Schristos   status = 1;
4157*56bb7041Schristos   bfd_nonfatal_message (NULL, obfd, osection, err);
4158*56bb7041Schristos }
4159*56bb7041Schristos 
4160*56bb7041Schristos /* Return TRUE if input section ISECTION should be skipped.  */
4161*56bb7041Schristos 
4162*56bb7041Schristos static bfd_boolean
skip_section(bfd * ibfd,sec_ptr isection,bfd_boolean skip_copy)4163*56bb7041Schristos skip_section (bfd *ibfd, sec_ptr isection, bfd_boolean skip_copy)
4164*56bb7041Schristos {
4165*56bb7041Schristos   sec_ptr osection;
4166*56bb7041Schristos   bfd_size_type size;
4167*56bb7041Schristos   flagword flags;
4168*56bb7041Schristos 
4169*56bb7041Schristos   /* If we have already failed earlier on,
4170*56bb7041Schristos      do not keep on generating complaints now.  */
4171*56bb7041Schristos   if (status != 0)
4172*56bb7041Schristos     return TRUE;
4173*56bb7041Schristos 
4174*56bb7041Schristos   if (extract_symbol)
4175*56bb7041Schristos     return TRUE;
4176*56bb7041Schristos 
4177*56bb7041Schristos   if (is_strip_section (ibfd, isection))
4178*56bb7041Schristos     return TRUE;
4179*56bb7041Schristos 
4180*56bb7041Schristos   if (is_update_section (ibfd, isection))
4181*56bb7041Schristos     return TRUE;
4182*56bb7041Schristos 
4183*56bb7041Schristos   /* When merging a note section we skip the copying of the contents,
4184*56bb7041Schristos      but not the copying of the relocs associated with the contents.  */
4185*56bb7041Schristos   if (skip_copy && is_mergeable_note_section (ibfd, isection))
4186*56bb7041Schristos     return TRUE;
4187*56bb7041Schristos 
4188*56bb7041Schristos   flags = bfd_section_flags (isection);
4189*56bb7041Schristos   if ((flags & SEC_GROUP) != 0)
4190*56bb7041Schristos     return TRUE;
4191*56bb7041Schristos 
4192*56bb7041Schristos   osection = isection->output_section;
4193*56bb7041Schristos   size = bfd_section_size (isection);
4194*56bb7041Schristos 
4195*56bb7041Schristos   if (size == 0 || osection == 0)
4196*56bb7041Schristos     return TRUE;
4197*56bb7041Schristos 
4198*56bb7041Schristos   return FALSE;
4199*56bb7041Schristos }
4200*56bb7041Schristos 
4201*56bb7041Schristos /* Add section SECTION_PATTERN to the list of sections that will have their
4202*56bb7041Schristos    relocations removed.  */
4203*56bb7041Schristos 
4204*56bb7041Schristos static void
handle_remove_relocations_option(const char * section_pattern)4205*56bb7041Schristos handle_remove_relocations_option (const char *section_pattern)
4206*56bb7041Schristos {
4207*56bb7041Schristos   find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE_RELOCS);
4208*56bb7041Schristos }
4209*56bb7041Schristos 
4210*56bb7041Schristos /* Return TRUE if ISECTION from IBFD should have its relocations removed,
4211*56bb7041Schristos    otherwise return FALSE.  If the user has requested that relocations be
4212*56bb7041Schristos    removed from a section that does not have relocations then this
4213*56bb7041Schristos    function will still return TRUE.  */
4214*56bb7041Schristos 
4215*56bb7041Schristos static bfd_boolean
discard_relocations(bfd * ibfd ATTRIBUTE_UNUSED,asection * isection)4216*56bb7041Schristos discard_relocations (bfd *ibfd ATTRIBUTE_UNUSED, asection *isection)
4217*56bb7041Schristos {
4218*56bb7041Schristos   return (find_section_list (bfd_section_name (isection), FALSE,
4219*56bb7041Schristos 			     SECTION_CONTEXT_REMOVE_RELOCS) != NULL);
4220*56bb7041Schristos }
4221*56bb7041Schristos 
4222*56bb7041Schristos /* Wrapper for dealing with --remove-section (-R) command line arguments.
4223*56bb7041Schristos    A special case is detected here, if the user asks to remove a relocation
4224*56bb7041Schristos    section (one starting with ".rela" or ".rel") then this removal must
4225*56bb7041Schristos    be done using a different technique in a relocatable object.  */
4226*56bb7041Schristos 
4227*56bb7041Schristos static void
handle_remove_section_option(const char * section_pattern)4228*56bb7041Schristos handle_remove_section_option (const char *section_pattern)
4229*56bb7041Schristos {
4230*56bb7041Schristos   find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE);
4231*56bb7041Schristos   if (strncmp (section_pattern, ".rel", 4) == 0)
4232*56bb7041Schristos     {
4233*56bb7041Schristos       section_pattern += 4;
4234*56bb7041Schristos       if (*section_pattern == 'a')
4235*56bb7041Schristos 	section_pattern++;
4236*56bb7041Schristos       if (*section_pattern)
4237*56bb7041Schristos 	handle_remove_relocations_option (section_pattern);
4238*56bb7041Schristos     }
4239*56bb7041Schristos   sections_removed = TRUE;
4240*56bb7041Schristos }
4241*56bb7041Schristos 
4242*56bb7041Schristos /* Copy relocations in input section ISECTION of IBFD to an output
4243*56bb7041Schristos    section with the same name in OBFDARG.  If stripping then don't
4244*56bb7041Schristos    copy any relocation info.  */
4245*56bb7041Schristos 
4246*56bb7041Schristos static void
copy_relocations_in_section(bfd * ibfd,sec_ptr isection,void * obfdarg)4247*56bb7041Schristos copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
4248*56bb7041Schristos {
4249*56bb7041Schristos   bfd *obfd = (bfd *) obfdarg;
4250*56bb7041Schristos   long relsize;
4251*56bb7041Schristos   arelent **relpp;
4252*56bb7041Schristos   long relcount;
4253*56bb7041Schristos   sec_ptr osection;
4254*56bb7041Schristos 
4255*56bb7041Schristos  if (skip_section (ibfd, isection, FALSE))
4256*56bb7041Schristos     return;
4257*56bb7041Schristos 
4258*56bb7041Schristos   osection = isection->output_section;
4259*56bb7041Schristos 
4260*56bb7041Schristos   /* Core files and DWO files do not need to be relocated.  */
4261*56bb7041Schristos   if (bfd_get_format (obfd) == bfd_core
4262*56bb7041Schristos       || strip_symbols == STRIP_NONDWO
4263*56bb7041Schristos       || discard_relocations (ibfd, isection))
4264*56bb7041Schristos     relsize = 0;
4265*56bb7041Schristos   else
4266*56bb7041Schristos     {
4267*56bb7041Schristos       relsize = bfd_get_reloc_upper_bound (ibfd, isection);
4268*56bb7041Schristos 
4269*56bb7041Schristos       if (relsize < 0)
4270*56bb7041Schristos 	{
4271*56bb7041Schristos 	  /* Do not complain if the target does not support relocations.  */
4272*56bb7041Schristos 	  if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
4273*56bb7041Schristos 	    relsize = 0;
4274*56bb7041Schristos 	  else
4275*56bb7041Schristos 	    {
4276*56bb7041Schristos 	      status = 1;
4277*56bb7041Schristos 	      bfd_nonfatal_message (NULL, ibfd, isection, NULL);
4278*56bb7041Schristos 	      return;
4279*56bb7041Schristos 	    }
4280*56bb7041Schristos 	}
4281*56bb7041Schristos     }
4282*56bb7041Schristos 
4283*56bb7041Schristos   if (relsize == 0)
4284*56bb7041Schristos     {
4285*56bb7041Schristos       bfd_set_reloc (obfd, osection, NULL, 0);
4286*56bb7041Schristos       osection->flags &= ~SEC_RELOC;
4287*56bb7041Schristos     }
4288*56bb7041Schristos   else
4289*56bb7041Schristos     {
4290*56bb7041Schristos       if (isection->orelocation != NULL)
4291*56bb7041Schristos 	{
4292*56bb7041Schristos 	  /* Some other function has already set up the output relocs
4293*56bb7041Schristos 	     for us, so scan those instead of the default relocs.  */
4294*56bb7041Schristos 	  relcount = isection->reloc_count;
4295*56bb7041Schristos 	  relpp = isection->orelocation;
4296*56bb7041Schristos 	}
4297*56bb7041Schristos       else
4298*56bb7041Schristos 	{
4299*56bb7041Schristos 	  relpp = (arelent **) xmalloc (relsize);
4300*56bb7041Schristos 	  relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
4301*56bb7041Schristos 	  if (relcount < 0)
4302*56bb7041Schristos 	    {
4303*56bb7041Schristos 	      status = 1;
4304*56bb7041Schristos 	      bfd_nonfatal_message (NULL, ibfd, isection,
4305*56bb7041Schristos 				    _("relocation count is negative"));
4306*56bb7041Schristos 	      free (relpp);
4307*56bb7041Schristos 	      return;
4308*56bb7041Schristos 	    }
4309*56bb7041Schristos 	}
4310*56bb7041Schristos 
4311*56bb7041Schristos       if (strip_symbols == STRIP_ALL)
4312*56bb7041Schristos 	{
4313*56bb7041Schristos 	  /* Remove relocations which are not in
4314*56bb7041Schristos 	     keep_strip_specific_list.  */
4315*56bb7041Schristos 	  arelent **temp_relpp;
4316*56bb7041Schristos 	  long temp_relcount = 0;
4317*56bb7041Schristos 	  long i;
4318*56bb7041Schristos 
4319*56bb7041Schristos 	  temp_relpp = (arelent **) xmalloc (relsize);
4320*56bb7041Schristos 	  for (i = 0; i < relcount; i++)
4321*56bb7041Schristos 	    {
4322*56bb7041Schristos 	      /* PR 17512: file: 9e907e0c.  */
4323*56bb7041Schristos 	      if (relpp[i]->sym_ptr_ptr
4324*56bb7041Schristos 		  /* PR 20096 */
4325*56bb7041Schristos 		  && * relpp[i]->sym_ptr_ptr)
4326*56bb7041Schristos 		if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
4327*56bb7041Schristos 					 keep_specific_htab))
4328*56bb7041Schristos 		  temp_relpp [temp_relcount++] = relpp [i];
4329*56bb7041Schristos 	    }
4330*56bb7041Schristos 	  relcount = temp_relcount;
4331*56bb7041Schristos 	  if (relpp != isection->orelocation)
4332*56bb7041Schristos 	    free (relpp);
4333*56bb7041Schristos 	  relpp = temp_relpp;
4334*56bb7041Schristos 	}
4335*56bb7041Schristos 
4336*56bb7041Schristos       bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
4337*56bb7041Schristos       if (relcount == 0)
4338*56bb7041Schristos 	{
4339*56bb7041Schristos 	  osection->flags &= ~SEC_RELOC;
4340*56bb7041Schristos 	  if (relpp != isection->orelocation)
4341*56bb7041Schristos 	    free (relpp);
4342*56bb7041Schristos 	}
4343*56bb7041Schristos     }
4344*56bb7041Schristos }
4345*56bb7041Schristos 
4346*56bb7041Schristos /* Copy the data of input section ISECTION of IBFD
4347*56bb7041Schristos    to an output section with the same name in OBFD.  */
4348*56bb7041Schristos 
4349*56bb7041Schristos static void
copy_section(bfd * ibfd,sec_ptr isection,void * obfdarg)4350*56bb7041Schristos copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
4351*56bb7041Schristos {
4352*56bb7041Schristos   bfd *obfd = (bfd *) obfdarg;
4353*56bb7041Schristos   struct section_list *p;
4354*56bb7041Schristos   sec_ptr osection;
4355*56bb7041Schristos   bfd_size_type size;
4356*56bb7041Schristos 
4357*56bb7041Schristos   if (skip_section (ibfd, isection, TRUE))
4358*56bb7041Schristos     return;
4359*56bb7041Schristos 
4360*56bb7041Schristos   osection = isection->output_section;
4361*56bb7041Schristos   /* The output SHF_COMPRESSED section size is different from input if
4362*56bb7041Schristos      ELF classes of input and output aren't the same.  We can't use
4363*56bb7041Schristos      the output section size since --interleave will shrink the output
4364*56bb7041Schristos      section.   Size will be updated if the section is converted.   */
4365*56bb7041Schristos   size = bfd_section_size (isection);
4366*56bb7041Schristos 
4367*56bb7041Schristos   if (bfd_section_flags (isection) & SEC_HAS_CONTENTS
4368*56bb7041Schristos       && bfd_section_flags (osection) & SEC_HAS_CONTENTS)
4369*56bb7041Schristos     {
4370*56bb7041Schristos       bfd_byte *memhunk = NULL;
4371*56bb7041Schristos 
4372*56bb7041Schristos       if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)
4373*56bb7041Schristos 	  || !bfd_convert_section_contents (ibfd, isection, obfd,
4374*56bb7041Schristos 					    &memhunk, &size))
4375*56bb7041Schristos 	{
4376*56bb7041Schristos 	  status = 1;
4377*56bb7041Schristos 	  bfd_nonfatal_message (NULL, ibfd, isection, NULL);
4378*56bb7041Schristos 	  free (memhunk);
4379*56bb7041Schristos 	  return;
4380*56bb7041Schristos 	}
4381*56bb7041Schristos 
4382*56bb7041Schristos       if (reverse_bytes)
4383*56bb7041Schristos 	{
4384*56bb7041Schristos 	  /* We don't handle leftover bytes (too many possible behaviors,
4385*56bb7041Schristos 	     and we don't know what the user wants).  The section length
4386*56bb7041Schristos 	     must be a multiple of the number of bytes to swap.  */
4387*56bb7041Schristos 	  if ((size % reverse_bytes) == 0)
4388*56bb7041Schristos 	    {
4389*56bb7041Schristos 	      unsigned long i, j;
4390*56bb7041Schristos 	      bfd_byte b;
4391*56bb7041Schristos 
4392*56bb7041Schristos 	      for (i = 0; i < size; i += reverse_bytes)
4393*56bb7041Schristos 		for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
4394*56bb7041Schristos 		  {
4395*56bb7041Schristos 		    bfd_byte *m = (bfd_byte *) memhunk;
4396*56bb7041Schristos 
4397*56bb7041Schristos 		    b = m[i + j];
4398*56bb7041Schristos 		    m[i + j] = m[(i + reverse_bytes) - (j + 1)];
4399*56bb7041Schristos 		    m[(i + reverse_bytes) - (j + 1)] = b;
4400*56bb7041Schristos 		  }
4401*56bb7041Schristos 	    }
4402*56bb7041Schristos 	  else
4403*56bb7041Schristos 	    /* User must pad the section up in order to do this.  */
4404*56bb7041Schristos 	    fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
4405*56bb7041Schristos 		   bfd_section_name (isection), reverse_bytes);
4406*56bb7041Schristos 	}
4407*56bb7041Schristos 
4408*56bb7041Schristos       if (copy_byte >= 0)
4409*56bb7041Schristos 	{
4410*56bb7041Schristos 	  /* Keep only every `copy_byte'th byte in MEMHUNK.  */
4411*56bb7041Schristos 	  char *from = (char *) memhunk + copy_byte;
4412*56bb7041Schristos 	  char *to = (char *) memhunk;
4413*56bb7041Schristos 	  char *end = (char *) memhunk + size;
4414*56bb7041Schristos 	  int i;
4415*56bb7041Schristos 
4416*56bb7041Schristos 	  /* If the section address is not exactly divisible by the interleave,
4417*56bb7041Schristos 	     then we must bias the from address.  If the copy_byte is less than
4418*56bb7041Schristos 	     the bias, then we must skip forward one interleave, and increment
4419*56bb7041Schristos 	     the final lma.  */
4420*56bb7041Schristos 	  int extra = isection->lma % interleave;
4421*56bb7041Schristos 	  from -= extra;
4422*56bb7041Schristos 	  if (copy_byte < extra)
4423*56bb7041Schristos 	    from += interleave;
4424*56bb7041Schristos 
4425*56bb7041Schristos 	  for (; from < end; from += interleave)
4426*56bb7041Schristos 	    for (i = 0; i < copy_width; i++)
4427*56bb7041Schristos 	      {
4428*56bb7041Schristos 		if (&from[i] >= end)
4429*56bb7041Schristos 		  break;
4430*56bb7041Schristos 		*to++ = from[i];
4431*56bb7041Schristos 	      }
4432*56bb7041Schristos 
4433*56bb7041Schristos 	  size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
4434*56bb7041Schristos 	  osection->lma /= interleave;
4435*56bb7041Schristos 	  if (copy_byte < extra)
4436*56bb7041Schristos 	    osection->lma++;
4437*56bb7041Schristos 	}
4438*56bb7041Schristos 
4439*56bb7041Schristos       if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
4440*56bb7041Schristos 	{
4441*56bb7041Schristos 	  status = 1;
4442*56bb7041Schristos 	  bfd_nonfatal_message (NULL, obfd, osection, NULL);
4443*56bb7041Schristos 	  free (memhunk);
4444*56bb7041Schristos 	  return;
4445*56bb7041Schristos 	}
4446*56bb7041Schristos       free (memhunk);
4447*56bb7041Schristos     }
4448*56bb7041Schristos   else if ((p = find_section_list (bfd_section_name (isection),
4449*56bb7041Schristos 				   FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL
4450*56bb7041Schristos 	   && (p->flags & SEC_HAS_CONTENTS) != 0)
4451*56bb7041Schristos     {
4452*56bb7041Schristos       void *memhunk = xmalloc (size);
4453*56bb7041Schristos 
4454*56bb7041Schristos       /* We don't permit the user to turn off the SEC_HAS_CONTENTS
4455*56bb7041Schristos 	 flag--they can just remove the section entirely and add it
4456*56bb7041Schristos 	 back again.  However, we do permit them to turn on the
4457*56bb7041Schristos 	 SEC_HAS_CONTENTS flag, and take it to mean that the section
4458*56bb7041Schristos 	 contents should be zeroed out.  */
4459*56bb7041Schristos 
4460*56bb7041Schristos       memset (memhunk, 0, size);
4461*56bb7041Schristos       if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
4462*56bb7041Schristos 	{
4463*56bb7041Schristos 	  status = 1;
4464*56bb7041Schristos 	  bfd_nonfatal_message (NULL, obfd, osection, NULL);
4465*56bb7041Schristos 	  free (memhunk);
4466*56bb7041Schristos 	  return;
4467*56bb7041Schristos 	}
4468*56bb7041Schristos       free (memhunk);
4469*56bb7041Schristos     }
4470*56bb7041Schristos }
4471*56bb7041Schristos 
4472*56bb7041Schristos /* Get all the sections.  This is used when --gap-fill or --pad-to is
4473*56bb7041Schristos    used.  */
4474*56bb7041Schristos 
4475*56bb7041Schristos static void
get_sections(bfd * obfd ATTRIBUTE_UNUSED,asection * osection,void * secppparg)4476*56bb7041Schristos get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
4477*56bb7041Schristos {
4478*56bb7041Schristos   asection ***secppp = (asection ***) secppparg;
4479*56bb7041Schristos 
4480*56bb7041Schristos   **secppp = osection;
4481*56bb7041Schristos   ++(*secppp);
4482*56bb7041Schristos }
4483*56bb7041Schristos 
4484*56bb7041Schristos /* Sort sections by LMA.  This is called via qsort, and is used when
4485*56bb7041Schristos    --gap-fill or --pad-to is used.  We force non loadable or empty
4486*56bb7041Schristos    sections to the front, where they are easier to ignore.  */
4487*56bb7041Schristos 
4488*56bb7041Schristos static int
compare_section_lma(const void * arg1,const void * arg2)4489*56bb7041Schristos compare_section_lma (const void *arg1, const void *arg2)
4490*56bb7041Schristos {
4491*56bb7041Schristos   const asection *sec1 = *(const asection **) arg1;
4492*56bb7041Schristos   const asection *sec2 = *(const asection **) arg2;
4493*56bb7041Schristos   flagword flags1, flags2;
4494*56bb7041Schristos 
4495*56bb7041Schristos   /* Sort non loadable sections to the front.  */
4496*56bb7041Schristos   flags1 = sec1->flags;
4497*56bb7041Schristos   flags2 = sec2->flags;
4498*56bb7041Schristos   if ((flags1 & SEC_HAS_CONTENTS) == 0
4499*56bb7041Schristos       || (flags1 & SEC_LOAD) == 0)
4500*56bb7041Schristos     {
4501*56bb7041Schristos       if ((flags2 & SEC_HAS_CONTENTS) != 0
4502*56bb7041Schristos 	  && (flags2 & SEC_LOAD) != 0)
4503*56bb7041Schristos 	return -1;
4504*56bb7041Schristos     }
4505*56bb7041Schristos   else
4506*56bb7041Schristos     {
4507*56bb7041Schristos       if ((flags2 & SEC_HAS_CONTENTS) == 0
4508*56bb7041Schristos 	  || (flags2 & SEC_LOAD) == 0)
4509*56bb7041Schristos 	return 1;
4510*56bb7041Schristos     }
4511*56bb7041Schristos 
4512*56bb7041Schristos   /* Sort sections by LMA.  */
4513*56bb7041Schristos   if (sec1->lma > sec2->lma)
4514*56bb7041Schristos     return 1;
4515*56bb7041Schristos   if (sec1->lma < sec2->lma)
4516*56bb7041Schristos     return -1;
4517*56bb7041Schristos 
4518*56bb7041Schristos   /* Sort sections with the same LMA by size.  */
4519*56bb7041Schristos   if (bfd_section_size (sec1) > bfd_section_size (sec2))
4520*56bb7041Schristos     return 1;
4521*56bb7041Schristos   if (bfd_section_size (sec1) < bfd_section_size (sec2))
4522*56bb7041Schristos     return -1;
4523*56bb7041Schristos 
4524*56bb7041Schristos   if (sec1->id > sec2->id)
4525*56bb7041Schristos     return 1;
4526*56bb7041Schristos   if (sec1->id < sec2->id)
4527*56bb7041Schristos     return -1;
4528*56bb7041Schristos   return 0;
4529*56bb7041Schristos }
4530*56bb7041Schristos 
4531*56bb7041Schristos /* Mark all the symbols which will be used in output relocations with
4532*56bb7041Schristos    the BSF_KEEP flag so that those symbols will not be stripped.
4533*56bb7041Schristos 
4534*56bb7041Schristos    Ignore relocations which will not appear in the output file.  */
4535*56bb7041Schristos 
4536*56bb7041Schristos static void
mark_symbols_used_in_relocations(bfd * ibfd,sec_ptr isection,void * symbolsarg)4537*56bb7041Schristos mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
4538*56bb7041Schristos {
4539*56bb7041Schristos   asymbol **symbols = (asymbol **) symbolsarg;
4540*56bb7041Schristos   long relsize;
4541*56bb7041Schristos   arelent **relpp;
4542*56bb7041Schristos   long relcount, i;
4543*56bb7041Schristos 
4544*56bb7041Schristos   /* Ignore an input section with no corresponding output section.  */
4545*56bb7041Schristos   if (isection->output_section == NULL)
4546*56bb7041Schristos     return;
4547*56bb7041Schristos 
4548*56bb7041Schristos   relsize = bfd_get_reloc_upper_bound (ibfd, isection);
4549*56bb7041Schristos   if (relsize < 0)
4550*56bb7041Schristos     {
4551*56bb7041Schristos       /* Do not complain if the target does not support relocations.  */
4552*56bb7041Schristos       if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
4553*56bb7041Schristos 	return;
4554*56bb7041Schristos       bfd_fatal (bfd_get_filename (ibfd));
4555*56bb7041Schristos     }
4556*56bb7041Schristos 
4557*56bb7041Schristos   if (relsize == 0)
4558*56bb7041Schristos     return;
4559*56bb7041Schristos 
4560*56bb7041Schristos   relpp = (arelent **) xmalloc (relsize);
4561*56bb7041Schristos   relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
4562*56bb7041Schristos   if (relcount < 0)
4563*56bb7041Schristos     bfd_fatal (bfd_get_filename (ibfd));
4564*56bb7041Schristos 
4565*56bb7041Schristos   /* Examine each symbol used in a relocation.  If it's not one of the
4566*56bb7041Schristos      special bfd section symbols, then mark it with BSF_KEEP.  */
4567*56bb7041Schristos   for (i = 0; i < relcount; i++)
4568*56bb7041Schristos     {
4569*56bb7041Schristos       /* See PRs 20923 and 20930 for reproducers for the NULL tests.  */
4570*56bb7041Schristos       if (relpp[i]->sym_ptr_ptr != NULL
4571*56bb7041Schristos 	  && * relpp[i]->sym_ptr_ptr != NULL
4572*56bb7041Schristos 	  && *relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
4573*56bb7041Schristos 	  && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
4574*56bb7041Schristos 	  && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
4575*56bb7041Schristos 	(*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
4576*56bb7041Schristos     }
4577*56bb7041Schristos 
4578*56bb7041Schristos   if (relpp != NULL)
4579*56bb7041Schristos     free (relpp);
4580*56bb7041Schristos }
4581*56bb7041Schristos 
4582*56bb7041Schristos /* Write out debugging information.  */
4583*56bb7041Schristos 
4584*56bb7041Schristos static bfd_boolean
write_debugging_info(bfd * obfd,void * dhandle,long * symcountp ATTRIBUTE_UNUSED,asymbol *** symppp ATTRIBUTE_UNUSED)4585*56bb7041Schristos write_debugging_info (bfd *obfd, void *dhandle,
4586*56bb7041Schristos 		      long *symcountp ATTRIBUTE_UNUSED,
4587*56bb7041Schristos 		      asymbol ***symppp ATTRIBUTE_UNUSED)
4588*56bb7041Schristos {
4589*56bb7041Schristos   if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
4590*56bb7041Schristos       || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
4591*56bb7041Schristos     {
4592*56bb7041Schristos       bfd_byte *syms, *strings = NULL;
4593*56bb7041Schristos       bfd_size_type symsize, stringsize;
4594*56bb7041Schristos       asection *stabsec, *stabstrsec;
4595*56bb7041Schristos       flagword flags;
4596*56bb7041Schristos 
4597*56bb7041Schristos       if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
4598*56bb7041Schristos 						    &symsize, &strings,
4599*56bb7041Schristos 						    &stringsize))
4600*56bb7041Schristos 	return FALSE;
4601*56bb7041Schristos 
4602*56bb7041Schristos       flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
4603*56bb7041Schristos       stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
4604*56bb7041Schristos       stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
4605*56bb7041Schristos       if (stabsec == NULL
4606*56bb7041Schristos 	  || stabstrsec == NULL
4607*56bb7041Schristos 	  || !bfd_set_section_size (stabsec, symsize)
4608*56bb7041Schristos 	  || !bfd_set_section_size (stabstrsec, stringsize)
4609*56bb7041Schristos 	  || !bfd_set_section_alignment (stabsec, 2)
4610*56bb7041Schristos 	  || !bfd_set_section_alignment (stabstrsec, 0))
4611*56bb7041Schristos 	{
4612*56bb7041Schristos 	  bfd_nonfatal_message (NULL, obfd, NULL,
4613*56bb7041Schristos 				_("can't create debugging section"));
4614*56bb7041Schristos 	  free (strings);
4615*56bb7041Schristos 	  return FALSE;
4616*56bb7041Schristos 	}
4617*56bb7041Schristos 
4618*56bb7041Schristos       /* We can get away with setting the section contents now because
4619*56bb7041Schristos 	 the next thing the caller is going to do is copy over the
4620*56bb7041Schristos 	 real sections.  We may someday have to split the contents
4621*56bb7041Schristos 	 setting out of this function.  */
4622*56bb7041Schristos       if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
4623*56bb7041Schristos 	  || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
4624*56bb7041Schristos 					 stringsize))
4625*56bb7041Schristos 	{
4626*56bb7041Schristos 	  bfd_nonfatal_message (NULL, obfd, NULL,
4627*56bb7041Schristos 				_("can't set debugging section contents"));
4628*56bb7041Schristos 	  free (strings);
4629*56bb7041Schristos 	  return FALSE;
4630*56bb7041Schristos 	}
4631*56bb7041Schristos 
4632*56bb7041Schristos       return TRUE;
4633*56bb7041Schristos     }
4634*56bb7041Schristos 
4635*56bb7041Schristos   bfd_nonfatal_message (NULL, obfd, NULL,
4636*56bb7041Schristos 			_("don't know how to write debugging information for %s"),
4637*56bb7041Schristos 			bfd_get_target (obfd));
4638*56bb7041Schristos   return FALSE;
4639*56bb7041Schristos }
4640*56bb7041Schristos 
4641*56bb7041Schristos /* If neither -D nor -U was specified explicitly,
4642*56bb7041Schristos    then use the configured default.  */
4643*56bb7041Schristos static void
default_deterministic(void)4644*56bb7041Schristos default_deterministic (void)
4645*56bb7041Schristos {
4646*56bb7041Schristos   if (deterministic < 0)
4647*56bb7041Schristos     deterministic = DEFAULT_AR_DETERMINISTIC;
4648*56bb7041Schristos }
4649*56bb7041Schristos 
4650*56bb7041Schristos static int
strip_main(int argc,char * argv[])4651*56bb7041Schristos strip_main (int argc, char *argv[])
4652*56bb7041Schristos {
4653*56bb7041Schristos   char *input_target = NULL;
4654*56bb7041Schristos   char *output_target = NULL;
4655*56bb7041Schristos   bfd_boolean show_version = FALSE;
4656*56bb7041Schristos   bfd_boolean formats_info = FALSE;
4657*56bb7041Schristos   int c;
4658*56bb7041Schristos   int i;
4659*56bb7041Schristos   char *output_file = NULL;
4660*56bb7041Schristos   bfd_boolean merge_notes_set = FALSE;
4661*56bb7041Schristos 
4662*56bb7041Schristos   while ((c = getopt_long (argc, argv, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU",
4663*56bb7041Schristos 			   strip_options, (int *) 0)) != EOF)
4664*56bb7041Schristos     {
4665*56bb7041Schristos       switch (c)
4666*56bb7041Schristos 	{
4667*56bb7041Schristos 	case 'I':
4668*56bb7041Schristos 	  input_target = optarg;
4669*56bb7041Schristos 	  break;
4670*56bb7041Schristos 	case 'O':
4671*56bb7041Schristos 	  output_target = optarg;
4672*56bb7041Schristos 	  break;
4673*56bb7041Schristos 	case 'F':
4674*56bb7041Schristos 	  input_target = output_target = optarg;
4675*56bb7041Schristos 	  break;
4676*56bb7041Schristos 	case 'R':
4677*56bb7041Schristos 	  handle_remove_section_option (optarg);
4678*56bb7041Schristos 	  break;
4679*56bb7041Schristos 	case OPTION_KEEP_SECTION:
4680*56bb7041Schristos 	  find_section_list (optarg, TRUE, SECTION_CONTEXT_KEEP);
4681*56bb7041Schristos 	  break;
4682*56bb7041Schristos 	case OPTION_REMOVE_RELOCS:
4683*56bb7041Schristos 	  handle_remove_relocations_option (optarg);
4684*56bb7041Schristos 	  break;
4685*56bb7041Schristos 	case 's':
4686*56bb7041Schristos 	  strip_symbols = STRIP_ALL;
4687*56bb7041Schristos 	  break;
4688*56bb7041Schristos 	case 'S':
4689*56bb7041Schristos 	case 'g':
4690*56bb7041Schristos 	case 'd':	/* Historic BSD alias for -g.  Used by early NetBSD.  */
4691*56bb7041Schristos 	  strip_symbols = STRIP_DEBUG;
4692*56bb7041Schristos 	  break;
4693*56bb7041Schristos 	case OPTION_STRIP_DWO:
4694*56bb7041Schristos 	  strip_symbols = STRIP_DWO;
4695*56bb7041Schristos 	  break;
4696*56bb7041Schristos 	case OPTION_STRIP_UNNEEDED:
4697*56bb7041Schristos 	  strip_symbols = STRIP_UNNEEDED;
4698*56bb7041Schristos 	  break;
4699*56bb7041Schristos 	case 'K':
4700*56bb7041Schristos 	  add_specific_symbol (optarg, keep_specific_htab);
4701*56bb7041Schristos 	  break;
4702*56bb7041Schristos 	case 'M':
4703*56bb7041Schristos 	  merge_notes = TRUE;
4704*56bb7041Schristos 	  merge_notes_set = TRUE;
4705*56bb7041Schristos 	  break;
4706*56bb7041Schristos 	case OPTION_NO_MERGE_NOTES:
4707*56bb7041Schristos 	  merge_notes = FALSE;
4708*56bb7041Schristos 	  merge_notes_set = TRUE;
4709*56bb7041Schristos 	  break;
4710*56bb7041Schristos 	case 'N':
4711*56bb7041Schristos 	  add_specific_symbol (optarg, strip_specific_htab);
4712*56bb7041Schristos 	  break;
4713*56bb7041Schristos 	case 'o':
4714*56bb7041Schristos 	  output_file = optarg;
4715*56bb7041Schristos 	  break;
4716*56bb7041Schristos 	case 'p':
4717*56bb7041Schristos 	  preserve_dates = TRUE;
4718*56bb7041Schristos 	  break;
4719*56bb7041Schristos 	case 'D':
4720*56bb7041Schristos 	  deterministic = TRUE;
4721*56bb7041Schristos 	  break;
4722*56bb7041Schristos 	case 'U':
4723*56bb7041Schristos 	  deterministic = FALSE;
4724*56bb7041Schristos 	  break;
4725*56bb7041Schristos 	case 'x':
4726*56bb7041Schristos 	  discard_locals = LOCALS_ALL;
4727*56bb7041Schristos 	  break;
4728*56bb7041Schristos 	case 'X':
4729*56bb7041Schristos 	  discard_locals = LOCALS_START_L;
4730*56bb7041Schristos 	  break;
4731*56bb7041Schristos 	case 'v':
4732*56bb7041Schristos 	  verbose = TRUE;
4733*56bb7041Schristos 	  break;
4734*56bb7041Schristos 	case 'V':
4735*56bb7041Schristos 	  show_version = TRUE;
4736*56bb7041Schristos 	  break;
4737*56bb7041Schristos 	case OPTION_FORMATS_INFO:
4738*56bb7041Schristos 	  formats_info = TRUE;
4739*56bb7041Schristos 	  break;
4740*56bb7041Schristos 	case OPTION_ONLY_KEEP_DEBUG:
4741*56bb7041Schristos 	  strip_symbols = STRIP_NONDEBUG;
4742*56bb7041Schristos 	  break;
4743*56bb7041Schristos 	case OPTION_KEEP_FILE_SYMBOLS:
4744*56bb7041Schristos 	  keep_file_symbols = 1;
4745*56bb7041Schristos 	  break;
4746*56bb7041Schristos 	case 0:
4747*56bb7041Schristos 	  /* We've been given a long option.  */
4748*56bb7041Schristos 	  break;
4749*56bb7041Schristos 	case 'w':
4750*56bb7041Schristos 	  wildcard = TRUE;
4751*56bb7041Schristos 	  break;
4752*56bb7041Schristos 	case 'H':
4753*56bb7041Schristos 	case 'h':
4754*56bb7041Schristos 	  strip_usage (stdout, 0);
4755*56bb7041Schristos 	default:
4756*56bb7041Schristos 	  strip_usage (stderr, 1);
4757*56bb7041Schristos 	}
4758*56bb7041Schristos     }
4759*56bb7041Schristos 
4760*56bb7041Schristos   /* If the user has not expressly chosen to merge/not-merge ELF notes
4761*56bb7041Schristos      then enable the merging unless we are stripping debug or dwo info.  */
4762*56bb7041Schristos   if (! merge_notes_set
4763*56bb7041Schristos       && (strip_symbols == STRIP_UNDEF
4764*56bb7041Schristos 	  || strip_symbols == STRIP_ALL
4765*56bb7041Schristos 	  || strip_symbols == STRIP_UNNEEDED
4766*56bb7041Schristos 	  || strip_symbols == STRIP_NONDEBUG
4767*56bb7041Schristos 	  || strip_symbols == STRIP_NONDWO))
4768*56bb7041Schristos     merge_notes = TRUE;
4769*56bb7041Schristos 
4770*56bb7041Schristos   if (formats_info)
4771*56bb7041Schristos     {
4772*56bb7041Schristos       display_info ();
4773*56bb7041Schristos       return 0;
4774*56bb7041Schristos     }
4775*56bb7041Schristos 
4776*56bb7041Schristos   if (show_version)
4777*56bb7041Schristos     print_version ("strip");
4778*56bb7041Schristos 
4779*56bb7041Schristos   default_deterministic ();
4780*56bb7041Schristos 
4781*56bb7041Schristos   /* Default is to strip all symbols.  */
4782*56bb7041Schristos   if (strip_symbols == STRIP_UNDEF
4783*56bb7041Schristos       && discard_locals == LOCALS_UNDEF
4784*56bb7041Schristos       && htab_elements (strip_specific_htab) == 0)
4785*56bb7041Schristos     strip_symbols = STRIP_ALL;
4786*56bb7041Schristos 
4787*56bb7041Schristos   if (output_target == NULL)
4788*56bb7041Schristos     output_target = input_target;
4789*56bb7041Schristos 
4790*56bb7041Schristos   i = optind;
4791*56bb7041Schristos   if (i == argc
4792*56bb7041Schristos       || (output_file != NULL && (i + 1) < argc))
4793*56bb7041Schristos     strip_usage (stderr, 1);
4794*56bb7041Schristos 
4795*56bb7041Schristos   for (; i < argc; i++)
4796*56bb7041Schristos     {
4797*56bb7041Schristos       int hold_status = status;
4798*56bb7041Schristos       struct stat statbuf;
4799*56bb7041Schristos       char *tmpname;
4800*56bb7041Schristos 
4801*56bb7041Schristos       if (get_file_size (argv[i]) < 1)
4802*56bb7041Schristos 	{
4803*56bb7041Schristos 	  status = 1;
4804*56bb7041Schristos 	  continue;
4805*56bb7041Schristos 	}
4806*56bb7041Schristos 
4807*56bb7041Schristos       if (preserve_dates)
4808*56bb7041Schristos 	/* No need to check the return value of stat().
4809*56bb7041Schristos 	   It has already been checked in get_file_size().  */
4810*56bb7041Schristos 	stat (argv[i], &statbuf);
4811*56bb7041Schristos 
4812*56bb7041Schristos       if (output_file == NULL
4813*56bb7041Schristos 	  || filename_cmp (argv[i], output_file) == 0)
4814*56bb7041Schristos 	tmpname = make_tempname (argv[i]);
4815*56bb7041Schristos       else
4816*56bb7041Schristos 	tmpname = output_file;
4817*56bb7041Schristos 
4818*56bb7041Schristos       if (tmpname == NULL)
4819*56bb7041Schristos 	{
4820*56bb7041Schristos 	  bfd_nonfatal_message (argv[i], NULL, NULL,
4821*56bb7041Schristos 				_("could not create temporary file to hold stripped copy"));
4822*56bb7041Schristos 	  status = 1;
4823*56bb7041Schristos 	  continue;
4824*56bb7041Schristos 	}
4825*56bb7041Schristos 
4826*56bb7041Schristos       status = 0;
4827*56bb7041Schristos       copy_file (argv[i], tmpname, input_target, output_target, NULL);
4828*56bb7041Schristos       if (status == 0)
4829*56bb7041Schristos 	{
4830*56bb7041Schristos 	  if (preserve_dates)
4831*56bb7041Schristos 	    set_times (tmpname, &statbuf);
4832*56bb7041Schristos 	  if (output_file != tmpname)
4833*56bb7041Schristos 	    status = (smart_rename (tmpname,
4834*56bb7041Schristos 				    output_file ? output_file : argv[i],
4835*56bb7041Schristos 				    preserve_dates) != 0);
4836*56bb7041Schristos 	  if (status == 0)
4837*56bb7041Schristos 	    status = hold_status;
4838*56bb7041Schristos 	}
4839*56bb7041Schristos       else
4840*56bb7041Schristos 	unlink_if_ordinary (tmpname);
4841*56bb7041Schristos       if (output_file != tmpname)
4842*56bb7041Schristos 	free (tmpname);
4843*56bb7041Schristos     }
4844*56bb7041Schristos 
4845*56bb7041Schristos   return status;
4846*56bb7041Schristos }
4847*56bb7041Schristos 
4848*56bb7041Schristos /* Set up PE subsystem.  */
4849*56bb7041Schristos 
4850*56bb7041Schristos static void
set_pe_subsystem(const char * s)4851*56bb7041Schristos set_pe_subsystem (const char *s)
4852*56bb7041Schristos {
4853*56bb7041Schristos   const char *version, *subsystem;
4854*56bb7041Schristos   size_t i;
4855*56bb7041Schristos   static const struct
4856*56bb7041Schristos     {
4857*56bb7041Schristos       const char *name;
4858*56bb7041Schristos       const char set_def;
4859*56bb7041Schristos       const short value;
4860*56bb7041Schristos     }
4861*56bb7041Schristos   v[] =
4862*56bb7041Schristos     {
4863*56bb7041Schristos       { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
4864*56bb7041Schristos       { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
4865*56bb7041Schristos       { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
4866*56bb7041Schristos       { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
4867*56bb7041Schristos       { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
4868*56bb7041Schristos       { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
4869*56bb7041Schristos       { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
4870*56bb7041Schristos       { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
4871*56bb7041Schristos       { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
4872*56bb7041Schristos       { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
4873*56bb7041Schristos     };
4874*56bb7041Schristos   short value;
4875*56bb7041Schristos   char *copy;
4876*56bb7041Schristos   int set_def = -1;
4877*56bb7041Schristos 
4878*56bb7041Schristos   /* Check for the presence of a version number.  */
4879*56bb7041Schristos   version = strchr (s, ':');
4880*56bb7041Schristos   if (version == NULL)
4881*56bb7041Schristos     subsystem = s;
4882*56bb7041Schristos   else
4883*56bb7041Schristos     {
4884*56bb7041Schristos       int len = version - s;
4885*56bb7041Schristos       copy = xstrdup (s);
4886*56bb7041Schristos       subsystem = copy;
4887*56bb7041Schristos       copy[len] = '\0';
4888*56bb7041Schristos       version = copy + 1 + len;
4889*56bb7041Schristos       pe_major_subsystem_version = strtoul (version, &copy, 0);
4890*56bb7041Schristos       if (*copy == '.')
4891*56bb7041Schristos 	pe_minor_subsystem_version = strtoul (copy + 1, &copy, 0);
4892*56bb7041Schristos       if (*copy != '\0')
4893*56bb7041Schristos 	non_fatal (_("%s: bad version in PE subsystem"), s);
4894*56bb7041Schristos     }
4895*56bb7041Schristos 
4896*56bb7041Schristos   /* Check for numeric subsystem.  */
4897*56bb7041Schristos   value = (short) strtol (subsystem, &copy, 0);
4898*56bb7041Schristos   if (*copy == '\0')
4899*56bb7041Schristos     {
4900*56bb7041Schristos       for (i = 0; i < ARRAY_SIZE (v); i++)
4901*56bb7041Schristos 	if (v[i].value == value)
4902*56bb7041Schristos 	  {
4903*56bb7041Schristos 	    pe_subsystem = value;
4904*56bb7041Schristos 	    set_def = v[i].set_def;
4905*56bb7041Schristos 	    break;
4906*56bb7041Schristos 	  }
4907*56bb7041Schristos     }
4908*56bb7041Schristos   else
4909*56bb7041Schristos     {
4910*56bb7041Schristos       /* Search for subsystem by name.  */
4911*56bb7041Schristos       for (i = 0; i < ARRAY_SIZE (v); i++)
4912*56bb7041Schristos 	if (strcmp (subsystem, v[i].name) == 0)
4913*56bb7041Schristos 	  {
4914*56bb7041Schristos 	    pe_subsystem = v[i].value;
4915*56bb7041Schristos 	    set_def = v[i].set_def;
4916*56bb7041Schristos 	    break;
4917*56bb7041Schristos 	  }
4918*56bb7041Schristos     }
4919*56bb7041Schristos 
4920*56bb7041Schristos   switch (set_def)
4921*56bb7041Schristos     {
4922*56bb7041Schristos     case -1:
4923*56bb7041Schristos       fatal (_("unknown PE subsystem: %s"), s);
4924*56bb7041Schristos       break;
4925*56bb7041Schristos     case 0:
4926*56bb7041Schristos       break;
4927*56bb7041Schristos     default:
4928*56bb7041Schristos       if (pe_file_alignment == (bfd_vma) -1)
4929*56bb7041Schristos 	pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
4930*56bb7041Schristos       if (pe_section_alignment == (bfd_vma) -1)
4931*56bb7041Schristos 	pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
4932*56bb7041Schristos       break;
4933*56bb7041Schristos     }
4934*56bb7041Schristos   if (s != subsystem)
4935*56bb7041Schristos     free ((char *) subsystem);
4936*56bb7041Schristos }
4937*56bb7041Schristos 
4938*56bb7041Schristos /* Convert EFI target to PEI target.  */
4939*56bb7041Schristos 
4940*56bb7041Schristos static void
convert_efi_target(char * efi)4941*56bb7041Schristos convert_efi_target (char *efi)
4942*56bb7041Schristos {
4943*56bb7041Schristos   efi[0] = 'p';
4944*56bb7041Schristos   efi[1] = 'e';
4945*56bb7041Schristos   efi[2] = 'i';
4946*56bb7041Schristos 
4947*56bb7041Schristos   if (strcmp (efi + 4, "ia32") == 0)
4948*56bb7041Schristos     {
4949*56bb7041Schristos       /* Change ia32 to i386.  */
4950*56bb7041Schristos       efi[5]= '3';
4951*56bb7041Schristos       efi[6]= '8';
4952*56bb7041Schristos       efi[7]= '6';
4953*56bb7041Schristos     }
4954*56bb7041Schristos   else if (strcmp (efi + 4, "x86_64") == 0)
4955*56bb7041Schristos     {
4956*56bb7041Schristos       /* Change x86_64 to x86-64.  */
4957*56bb7041Schristos       efi[7] = '-';
4958*56bb7041Schristos     }
4959*56bb7041Schristos }
4960*56bb7041Schristos 
4961*56bb7041Schristos /* Allocate and return a pointer to a struct section_add, initializing the
4962*56bb7041Schristos    structure using ARG, a string in the format "sectionname=filename".
4963*56bb7041Schristos    The returned structure will have its next pointer set to NEXT.  The
4964*56bb7041Schristos    OPTION field is the name of the command line option currently being
4965*56bb7041Schristos    parsed, and is only used if an error needs to be reported.  */
4966*56bb7041Schristos 
4967*56bb7041Schristos static struct section_add *
init_section_add(const char * arg,struct section_add * next,const char * option)4968*56bb7041Schristos init_section_add (const char *arg,
4969*56bb7041Schristos 		  struct section_add *next,
4970*56bb7041Schristos 		  const char *option)
4971*56bb7041Schristos {
4972*56bb7041Schristos   struct section_add *pa;
4973*56bb7041Schristos   const char *s;
4974*56bb7041Schristos 
4975*56bb7041Schristos   s = strchr (arg, '=');
4976*56bb7041Schristos   if (s == NULL)
4977*56bb7041Schristos     fatal (_("bad format for %s"), option);
4978*56bb7041Schristos 
4979*56bb7041Schristos   pa = (struct section_add *) xmalloc (sizeof (struct section_add));
4980*56bb7041Schristos   pa->name = xstrndup (arg, s - arg);
4981*56bb7041Schristos   pa->filename = s + 1;
4982*56bb7041Schristos   pa->next = next;
4983*56bb7041Schristos   pa->contents = NULL;
4984*56bb7041Schristos   pa->size = 0;
4985*56bb7041Schristos 
4986*56bb7041Schristos   return pa;
4987*56bb7041Schristos }
4988*56bb7041Schristos 
4989*56bb7041Schristos /* Load the file specified in PA, allocating memory to hold the file
4990*56bb7041Schristos    contents, and store a pointer to the allocated memory in the contents
4991*56bb7041Schristos    field of PA.  The size field of PA is also updated.  All errors call
4992*56bb7041Schristos    FATAL.  */
4993*56bb7041Schristos 
4994*56bb7041Schristos static void
section_add_load_file(struct section_add * pa)4995*56bb7041Schristos section_add_load_file (struct section_add *pa)
4996*56bb7041Schristos {
4997*56bb7041Schristos   size_t off, alloc;
4998*56bb7041Schristos   FILE *f;
4999*56bb7041Schristos 
5000*56bb7041Schristos   /* We don't use get_file_size so that we can do
5001*56bb7041Schristos      --add-section .note.GNU_stack=/dev/null
5002*56bb7041Schristos      get_file_size doesn't work on /dev/null.  */
5003*56bb7041Schristos 
5004*56bb7041Schristos   f = fopen (pa->filename, FOPEN_RB);
5005*56bb7041Schristos   if (f == NULL)
5006*56bb7041Schristos     fatal (_("cannot open: %s: %s"),
5007*56bb7041Schristos 	   pa->filename, strerror (errno));
5008*56bb7041Schristos 
5009*56bb7041Schristos   off = 0;
5010*56bb7041Schristos   alloc = 4096;
5011*56bb7041Schristos   pa->contents = (bfd_byte *) xmalloc (alloc);
5012*56bb7041Schristos   while (!feof (f))
5013*56bb7041Schristos     {
5014*56bb7041Schristos       off_t got;
5015*56bb7041Schristos 
5016*56bb7041Schristos       if (off == alloc)
5017*56bb7041Schristos 	{
5018*56bb7041Schristos 	  alloc <<= 1;
5019*56bb7041Schristos 	  pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
5020*56bb7041Schristos 	}
5021*56bb7041Schristos 
5022*56bb7041Schristos       got = fread (pa->contents + off, 1, alloc - off, f);
5023*56bb7041Schristos       if (ferror (f))
5024*56bb7041Schristos 	fatal (_("%s: fread failed"), pa->filename);
5025*56bb7041Schristos 
5026*56bb7041Schristos       off += got;
5027*56bb7041Schristos     }
5028*56bb7041Schristos 
5029*56bb7041Schristos   pa->size = off;
5030*56bb7041Schristos 
5031*56bb7041Schristos   fclose (f);
5032*56bb7041Schristos }
5033*56bb7041Schristos 
5034*56bb7041Schristos static int
copy_main(int argc,char * argv[])5035*56bb7041Schristos copy_main (int argc, char *argv[])
5036*56bb7041Schristos {
5037*56bb7041Schristos   char *input_filename = NULL;
5038*56bb7041Schristos   char *output_filename = NULL;
5039*56bb7041Schristos   char *tmpname;
5040*56bb7041Schristos   char *input_target = NULL;
5041*56bb7041Schristos   char *output_target = NULL;
5042*56bb7041Schristos   bfd_boolean show_version = FALSE;
5043*56bb7041Schristos   bfd_boolean change_warn = TRUE;
5044*56bb7041Schristos   bfd_boolean formats_info = FALSE;
5045*56bb7041Schristos   bfd_boolean use_globalize = FALSE;
5046*56bb7041Schristos   bfd_boolean use_keep_global = FALSE;
5047*56bb7041Schristos   int c;
5048*56bb7041Schristos   struct stat statbuf;
5049*56bb7041Schristos   const bfd_arch_info_type *input_arch = NULL;
5050*56bb7041Schristos 
5051*56bb7041Schristos   while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
5052*56bb7041Schristos 			   copy_options, (int *) 0)) != EOF)
5053*56bb7041Schristos     {
5054*56bb7041Schristos       switch (c)
5055*56bb7041Schristos 	{
5056*56bb7041Schristos 	case 'b':
5057*56bb7041Schristos 	  copy_byte = atoi (optarg);
5058*56bb7041Schristos 	  if (copy_byte < 0)
5059*56bb7041Schristos 	    fatal (_("byte number must be non-negative"));
5060*56bb7041Schristos 	  break;
5061*56bb7041Schristos 
5062*56bb7041Schristos 	case 'B':
5063*56bb7041Schristos 	  input_arch = bfd_scan_arch (optarg);
5064*56bb7041Schristos 	  if (input_arch == NULL)
5065*56bb7041Schristos 	    fatal (_("architecture %s unknown"), optarg);
5066*56bb7041Schristos 	  break;
5067*56bb7041Schristos 
5068*56bb7041Schristos 	case 'i':
5069*56bb7041Schristos 	  if (optarg)
5070*56bb7041Schristos 	    {
5071*56bb7041Schristos 	      interleave = atoi (optarg);
5072*56bb7041Schristos 	      if (interleave < 1)
5073*56bb7041Schristos 		fatal (_("interleave must be positive"));
5074*56bb7041Schristos 	    }
5075*56bb7041Schristos 	  else
5076*56bb7041Schristos 	    interleave = 4;
5077*56bb7041Schristos 	  break;
5078*56bb7041Schristos 
5079*56bb7041Schristos 	case OPTION_INTERLEAVE_WIDTH:
5080*56bb7041Schristos 	  copy_width = atoi (optarg);
5081*56bb7041Schristos 	  if (copy_width < 1)
5082*56bb7041Schristos 	    fatal(_("interleave width must be positive"));
5083*56bb7041Schristos 	  break;
5084*56bb7041Schristos 
5085*56bb7041Schristos 	case 'I':
5086*56bb7041Schristos 	case 's':		/* "source" - 'I' is preferred */
5087*56bb7041Schristos 	  input_target = optarg;
5088*56bb7041Schristos 	  break;
5089*56bb7041Schristos 
5090*56bb7041Schristos 	case 'O':
5091*56bb7041Schristos 	case 'd':		/* "destination" - 'O' is preferred */
5092*56bb7041Schristos 	  output_target = optarg;
5093*56bb7041Schristos 	  break;
5094*56bb7041Schristos 
5095*56bb7041Schristos 	case 'F':
5096*56bb7041Schristos 	  input_target = output_target = optarg;
5097*56bb7041Schristos 	  break;
5098*56bb7041Schristos 
5099*56bb7041Schristos 	case 'j':
5100*56bb7041Schristos 	  find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY);
5101*56bb7041Schristos 	  sections_copied = TRUE;
5102*56bb7041Schristos 	  break;
5103*56bb7041Schristos 
5104*56bb7041Schristos 	case 'R':
5105*56bb7041Schristos 	  handle_remove_section_option (optarg);
5106*56bb7041Schristos 	  break;
5107*56bb7041Schristos 
5108*56bb7041Schristos 	case OPTION_KEEP_SECTION:
5109*56bb7041Schristos 	  find_section_list (optarg, TRUE, SECTION_CONTEXT_KEEP);
5110*56bb7041Schristos 	  break;
5111*56bb7041Schristos 
5112*56bb7041Schristos         case OPTION_REMOVE_RELOCS:
5113*56bb7041Schristos 	  handle_remove_relocations_option (optarg);
5114*56bb7041Schristos 	  break;
5115*56bb7041Schristos 
5116*56bb7041Schristos 	case 'S':
5117*56bb7041Schristos 	  strip_symbols = STRIP_ALL;
5118*56bb7041Schristos 	  break;
5119*56bb7041Schristos 
5120*56bb7041Schristos 	case 'g':
5121*56bb7041Schristos 	  strip_symbols = STRIP_DEBUG;
5122*56bb7041Schristos 	  break;
5123*56bb7041Schristos 
5124*56bb7041Schristos 	case OPTION_STRIP_DWO:
5125*56bb7041Schristos 	  strip_symbols = STRIP_DWO;
5126*56bb7041Schristos 	  break;
5127*56bb7041Schristos 
5128*56bb7041Schristos 	case OPTION_STRIP_UNNEEDED:
5129*56bb7041Schristos 	  strip_symbols = STRIP_UNNEEDED;
5130*56bb7041Schristos 	  break;
5131*56bb7041Schristos 
5132*56bb7041Schristos 	case OPTION_ONLY_KEEP_DEBUG:
5133*56bb7041Schristos 	  strip_symbols = STRIP_NONDEBUG;
5134*56bb7041Schristos 	  break;
5135*56bb7041Schristos 
5136*56bb7041Schristos 	case OPTION_KEEP_FILE_SYMBOLS:
5137*56bb7041Schristos 	  keep_file_symbols = 1;
5138*56bb7041Schristos 	  break;
5139*56bb7041Schristos 
5140*56bb7041Schristos 	case OPTION_ADD_GNU_DEBUGLINK:
5141*56bb7041Schristos 	  long_section_names = ENABLE ;
5142*56bb7041Schristos 	  gnu_debuglink_filename = optarg;
5143*56bb7041Schristos 	  break;
5144*56bb7041Schristos 
5145*56bb7041Schristos 	case 'K':
5146*56bb7041Schristos 	  add_specific_symbol (optarg, keep_specific_htab);
5147*56bb7041Schristos 	  break;
5148*56bb7041Schristos 
5149*56bb7041Schristos 	case 'M':
5150*56bb7041Schristos 	  merge_notes = TRUE;
5151*56bb7041Schristos 	  break;
5152*56bb7041Schristos 	case OPTION_NO_MERGE_NOTES:
5153*56bb7041Schristos 	  merge_notes = FALSE;
5154*56bb7041Schristos 	  break;
5155*56bb7041Schristos 
5156*56bb7041Schristos 	case 'N':
5157*56bb7041Schristos 	  add_specific_symbol (optarg, strip_specific_htab);
5158*56bb7041Schristos 	  break;
5159*56bb7041Schristos 
5160*56bb7041Schristos 	case OPTION_STRIP_UNNEEDED_SYMBOL:
5161*56bb7041Schristos 	  add_specific_symbol (optarg, strip_unneeded_htab);
5162*56bb7041Schristos 	  break;
5163*56bb7041Schristos 
5164*56bb7041Schristos 	case 'L':
5165*56bb7041Schristos 	  add_specific_symbol (optarg, localize_specific_htab);
5166*56bb7041Schristos 	  break;
5167*56bb7041Schristos 
5168*56bb7041Schristos 	case OPTION_GLOBALIZE_SYMBOL:
5169*56bb7041Schristos 	  use_globalize = TRUE;
5170*56bb7041Schristos 	  add_specific_symbol (optarg, globalize_specific_htab);
5171*56bb7041Schristos 	  break;
5172*56bb7041Schristos 
5173*56bb7041Schristos 	case 'G':
5174*56bb7041Schristos 	  use_keep_global = TRUE;
5175*56bb7041Schristos 	  add_specific_symbol (optarg, keepglobal_specific_htab);
5176*56bb7041Schristos 	  break;
5177*56bb7041Schristos 
5178*56bb7041Schristos 	case 'W':
5179*56bb7041Schristos 	  add_specific_symbol (optarg, weaken_specific_htab);
5180*56bb7041Schristos 	  break;
5181*56bb7041Schristos 
5182*56bb7041Schristos 	case 'p':
5183*56bb7041Schristos 	  preserve_dates = TRUE;
5184*56bb7041Schristos 	  break;
5185*56bb7041Schristos 
5186*56bb7041Schristos 	case 'D':
5187*56bb7041Schristos 	  deterministic = TRUE;
5188*56bb7041Schristos 	  break;
5189*56bb7041Schristos 
5190*56bb7041Schristos 	case 'U':
5191*56bb7041Schristos 	  deterministic = FALSE;
5192*56bb7041Schristos 	  break;
5193*56bb7041Schristos 
5194*56bb7041Schristos 	case 'w':
5195*56bb7041Schristos 	  wildcard = TRUE;
5196*56bb7041Schristos 	  break;
5197*56bb7041Schristos 
5198*56bb7041Schristos 	case 'x':
5199*56bb7041Schristos 	  discard_locals = LOCALS_ALL;
5200*56bb7041Schristos 	  break;
5201*56bb7041Schristos 
5202*56bb7041Schristos 	case 'X':
5203*56bb7041Schristos 	  discard_locals = LOCALS_START_L;
5204*56bb7041Schristos 	  break;
5205*56bb7041Schristos 
5206*56bb7041Schristos 	case 'v':
5207*56bb7041Schristos 	  verbose = TRUE;
5208*56bb7041Schristos 	  break;
5209*56bb7041Schristos 
5210*56bb7041Schristos 	case 'V':
5211*56bb7041Schristos 	  show_version = TRUE;
5212*56bb7041Schristos 	  break;
5213*56bb7041Schristos 
5214*56bb7041Schristos 	case OPTION_FORMATS_INFO:
5215*56bb7041Schristos 	  formats_info = TRUE;
5216*56bb7041Schristos 	  break;
5217*56bb7041Schristos 
5218*56bb7041Schristos 	case OPTION_WEAKEN:
5219*56bb7041Schristos 	  weaken = TRUE;
5220*56bb7041Schristos 	  break;
5221*56bb7041Schristos 
5222*56bb7041Schristos 	case OPTION_ADD_SECTION:
5223*56bb7041Schristos 	  add_sections = init_section_add (optarg, add_sections,
5224*56bb7041Schristos 					   "--add-section");
5225*56bb7041Schristos 	  section_add_load_file (add_sections);
5226*56bb7041Schristos 	  break;
5227*56bb7041Schristos 
5228*56bb7041Schristos 	case OPTION_UPDATE_SECTION:
5229*56bb7041Schristos 	  update_sections = init_section_add (optarg, update_sections,
5230*56bb7041Schristos 					      "--update-section");
5231*56bb7041Schristos 	  section_add_load_file (update_sections);
5232*56bb7041Schristos 	  break;
5233*56bb7041Schristos 
5234*56bb7041Schristos 	case OPTION_DUMP_SECTION:
5235*56bb7041Schristos 	  dump_sections = init_section_add (optarg, dump_sections,
5236*56bb7041Schristos 					    "--dump-section");
5237*56bb7041Schristos 	  break;
5238*56bb7041Schristos 
5239*56bb7041Schristos 	case OPTION_ADD_SYMBOL:
5240*56bb7041Schristos 	  {
5241*56bb7041Schristos 	    char *s, *t;
5242*56bb7041Schristos 	    struct addsym_node *newsym = xmalloc (sizeof *newsym);
5243*56bb7041Schristos 
5244*56bb7041Schristos 	    newsym->next = NULL;
5245*56bb7041Schristos 	    s = strchr (optarg, '=');
5246*56bb7041Schristos 	    if (s == NULL)
5247*56bb7041Schristos 	      fatal (_("bad format for %s"), "--add-symbol");
5248*56bb7041Schristos 	    t = strchr (s + 1, ':');
5249*56bb7041Schristos 
5250*56bb7041Schristos 	    newsym->symdef = xstrndup (optarg, s - optarg);
5251*56bb7041Schristos 	    if (t)
5252*56bb7041Schristos 	      {
5253*56bb7041Schristos 		newsym->section = xstrndup (s + 1, t - (s + 1));
5254*56bb7041Schristos 		newsym->symval = strtol (t + 1, NULL, 0);
5255*56bb7041Schristos 	      }
5256*56bb7041Schristos 	    else
5257*56bb7041Schristos 	      {
5258*56bb7041Schristos 		newsym->section = NULL;
5259*56bb7041Schristos 		newsym->symval = strtol (s + 1, NULL, 0);
5260*56bb7041Schristos 		t = s;
5261*56bb7041Schristos 	      }
5262*56bb7041Schristos 
5263*56bb7041Schristos 	    t = strchr (t + 1, ',');
5264*56bb7041Schristos 	    newsym->othersym = NULL;
5265*56bb7041Schristos 	    if (t)
5266*56bb7041Schristos 	      newsym->flags = parse_symflags (t+1, &newsym->othersym);
5267*56bb7041Schristos 	    else
5268*56bb7041Schristos 	      newsym->flags = BSF_GLOBAL;
5269*56bb7041Schristos 
5270*56bb7041Schristos 	    /* Keep 'othersym' symbols at the front of the list.  */
5271*56bb7041Schristos 	    if (newsym->othersym)
5272*56bb7041Schristos 	      {
5273*56bb7041Schristos 		newsym->next = add_sym_list;
5274*56bb7041Schristos 		if (!add_sym_list)
5275*56bb7041Schristos 		  add_sym_tail = &newsym->next;
5276*56bb7041Schristos 		add_sym_list = newsym;
5277*56bb7041Schristos 	      }
5278*56bb7041Schristos 	    else
5279*56bb7041Schristos 	      {
5280*56bb7041Schristos 		*add_sym_tail = newsym;
5281*56bb7041Schristos 		add_sym_tail = &newsym->next;
5282*56bb7041Schristos 	      }
5283*56bb7041Schristos 	    add_symbols++;
5284*56bb7041Schristos 	  }
5285*56bb7041Schristos 	  break;
5286*56bb7041Schristos 
5287*56bb7041Schristos 	case OPTION_CHANGE_START:
5288*56bb7041Schristos 	  change_start = parse_vma (optarg, "--change-start");
5289*56bb7041Schristos 	  break;
5290*56bb7041Schristos 
5291*56bb7041Schristos 	case OPTION_CHANGE_SECTION_ADDRESS:
5292*56bb7041Schristos 	case OPTION_CHANGE_SECTION_LMA:
5293*56bb7041Schristos 	case OPTION_CHANGE_SECTION_VMA:
5294*56bb7041Schristos 	  {
5295*56bb7041Schristos 	    struct section_list * p;
5296*56bb7041Schristos 	    unsigned int context = 0;
5297*56bb7041Schristos 	    const char *s;
5298*56bb7041Schristos 	    int len;
5299*56bb7041Schristos 	    char *name;
5300*56bb7041Schristos 	    char *option = NULL;
5301*56bb7041Schristos 	    bfd_vma val;
5302*56bb7041Schristos 
5303*56bb7041Schristos 	    switch (c)
5304*56bb7041Schristos 	      {
5305*56bb7041Schristos 	      case OPTION_CHANGE_SECTION_ADDRESS:
5306*56bb7041Schristos 		option = "--change-section-address";
5307*56bb7041Schristos 		context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
5308*56bb7041Schristos 		break;
5309*56bb7041Schristos 	      case OPTION_CHANGE_SECTION_LMA:
5310*56bb7041Schristos 		option = "--change-section-lma";
5311*56bb7041Schristos 		context = SECTION_CONTEXT_ALTER_LMA;
5312*56bb7041Schristos 		break;
5313*56bb7041Schristos 	      case OPTION_CHANGE_SECTION_VMA:
5314*56bb7041Schristos 		option = "--change-section-vma";
5315*56bb7041Schristos 		context = SECTION_CONTEXT_ALTER_VMA;
5316*56bb7041Schristos 		break;
5317*56bb7041Schristos 	      }
5318*56bb7041Schristos 
5319*56bb7041Schristos 	    s = strchr (optarg, '=');
5320*56bb7041Schristos 	    if (s == NULL)
5321*56bb7041Schristos 	      {
5322*56bb7041Schristos 		s = strchr (optarg, '+');
5323*56bb7041Schristos 		if (s == NULL)
5324*56bb7041Schristos 		  {
5325*56bb7041Schristos 		    s = strchr (optarg, '-');
5326*56bb7041Schristos 		    if (s == NULL)
5327*56bb7041Schristos 		      fatal (_("bad format for %s"), option);
5328*56bb7041Schristos 		  }
5329*56bb7041Schristos 	      }
5330*56bb7041Schristos 	    else
5331*56bb7041Schristos 	      {
5332*56bb7041Schristos 		/* Correct the context.  */
5333*56bb7041Schristos 		switch (c)
5334*56bb7041Schristos 		  {
5335*56bb7041Schristos 		  case OPTION_CHANGE_SECTION_ADDRESS:
5336*56bb7041Schristos 		    context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
5337*56bb7041Schristos 		    break;
5338*56bb7041Schristos 		  case OPTION_CHANGE_SECTION_LMA:
5339*56bb7041Schristos 		    context = SECTION_CONTEXT_SET_LMA;
5340*56bb7041Schristos 		    break;
5341*56bb7041Schristos 		  case OPTION_CHANGE_SECTION_VMA:
5342*56bb7041Schristos 		    context = SECTION_CONTEXT_SET_VMA;
5343*56bb7041Schristos 		    break;
5344*56bb7041Schristos 		  }
5345*56bb7041Schristos 	      }
5346*56bb7041Schristos 
5347*56bb7041Schristos 	    len = s - optarg;
5348*56bb7041Schristos 	    name = (char *) xmalloc (len + 1);
5349*56bb7041Schristos 	    strncpy (name, optarg, len);
5350*56bb7041Schristos 	    name[len] = '\0';
5351*56bb7041Schristos 
5352*56bb7041Schristos 	    p = find_section_list (name, TRUE, context);
5353*56bb7041Schristos 
5354*56bb7041Schristos 	    val = parse_vma (s + 1, option);
5355*56bb7041Schristos 	    if (*s == '-')
5356*56bb7041Schristos 	      val = - val;
5357*56bb7041Schristos 
5358*56bb7041Schristos 	    switch (c)
5359*56bb7041Schristos 	      {
5360*56bb7041Schristos 	      case OPTION_CHANGE_SECTION_ADDRESS:
5361*56bb7041Schristos 		p->vma_val = val;
5362*56bb7041Schristos 		/* Fall through.  */
5363*56bb7041Schristos 
5364*56bb7041Schristos 	      case OPTION_CHANGE_SECTION_LMA:
5365*56bb7041Schristos 		p->lma_val = val;
5366*56bb7041Schristos 		break;
5367*56bb7041Schristos 
5368*56bb7041Schristos 	      case OPTION_CHANGE_SECTION_VMA:
5369*56bb7041Schristos 		p->vma_val = val;
5370*56bb7041Schristos 		break;
5371*56bb7041Schristos 	      }
5372*56bb7041Schristos 	  }
5373*56bb7041Schristos 	  break;
5374*56bb7041Schristos 
5375*56bb7041Schristos 	case OPTION_CHANGE_ADDRESSES:
5376*56bb7041Schristos 	  change_section_address = parse_vma (optarg, "--change-addresses");
5377*56bb7041Schristos 	  change_start = change_section_address;
5378*56bb7041Schristos 	  break;
5379*56bb7041Schristos 
5380*56bb7041Schristos 	case OPTION_CHANGE_WARNINGS:
5381*56bb7041Schristos 	  change_warn = TRUE;
5382*56bb7041Schristos 	  break;
5383*56bb7041Schristos 
5384*56bb7041Schristos 	case OPTION_CHANGE_LEADING_CHAR:
5385*56bb7041Schristos 	  change_leading_char = TRUE;
5386*56bb7041Schristos 	  break;
5387*56bb7041Schristos 
5388*56bb7041Schristos 	case OPTION_COMPRESS_DEBUG_SECTIONS:
5389*56bb7041Schristos 	  if (optarg)
5390*56bb7041Schristos 	    {
5391*56bb7041Schristos 	      if (strcasecmp (optarg, "none") == 0)
5392*56bb7041Schristos 		do_debug_sections = decompress;
5393*56bb7041Schristos 	      else if (strcasecmp (optarg, "zlib") == 0)
5394*56bb7041Schristos 		do_debug_sections = compress_zlib;
5395*56bb7041Schristos 	      else if (strcasecmp (optarg, "zlib-gnu") == 0)
5396*56bb7041Schristos 		do_debug_sections = compress_gnu_zlib;
5397*56bb7041Schristos 	      else if (strcasecmp (optarg, "zlib-gabi") == 0)
5398*56bb7041Schristos 		do_debug_sections = compress_gabi_zlib;
5399*56bb7041Schristos 	      else
5400*56bb7041Schristos 		fatal (_("unrecognized --compress-debug-sections type `%s'"),
5401*56bb7041Schristos 		       optarg);
5402*56bb7041Schristos 	    }
5403*56bb7041Schristos 	  else
5404*56bb7041Schristos 	    do_debug_sections = compress;
5405*56bb7041Schristos 	  break;
5406*56bb7041Schristos 
5407*56bb7041Schristos 	case OPTION_DEBUGGING:
5408*56bb7041Schristos 	  convert_debugging = TRUE;
5409*56bb7041Schristos 	  break;
5410*56bb7041Schristos 
5411*56bb7041Schristos 	case OPTION_DECOMPRESS_DEBUG_SECTIONS:
5412*56bb7041Schristos 	  do_debug_sections = decompress;
5413*56bb7041Schristos 	  break;
5414*56bb7041Schristos 
5415*56bb7041Schristos 	case OPTION_ELF_STT_COMMON:
5416*56bb7041Schristos 	  if (strcasecmp (optarg, "yes") == 0)
5417*56bb7041Schristos 	    do_elf_stt_common = elf_stt_common;
5418*56bb7041Schristos 	  else if (strcasecmp (optarg, "no") == 0)
5419*56bb7041Schristos 	    do_elf_stt_common = no_elf_stt_common;
5420*56bb7041Schristos 	  else
5421*56bb7041Schristos 	    fatal (_("unrecognized --elf-stt-common= option `%s'"),
5422*56bb7041Schristos 		   optarg);
5423*56bb7041Schristos 	  break;
5424*56bb7041Schristos 
5425*56bb7041Schristos 	case OPTION_GAP_FILL:
5426*56bb7041Schristos 	  {
5427*56bb7041Schristos 	    bfd_vma gap_fill_vma;
5428*56bb7041Schristos 
5429*56bb7041Schristos 	    gap_fill_vma = parse_vma (optarg, "--gap-fill");
5430*56bb7041Schristos 	    gap_fill = (bfd_byte) gap_fill_vma;
5431*56bb7041Schristos 	    if ((bfd_vma) gap_fill != gap_fill_vma)
5432*56bb7041Schristos 	      {
5433*56bb7041Schristos 		char buff[20];
5434*56bb7041Schristos 
5435*56bb7041Schristos 		sprintf_vma (buff, gap_fill_vma);
5436*56bb7041Schristos 
5437*56bb7041Schristos 		non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
5438*56bb7041Schristos 			   buff, gap_fill);
5439*56bb7041Schristos 	      }
5440*56bb7041Schristos 	    gap_fill_set = TRUE;
5441*56bb7041Schristos 	  }
5442*56bb7041Schristos 	  break;
5443*56bb7041Schristos 
5444*56bb7041Schristos 	case OPTION_NO_CHANGE_WARNINGS:
5445*56bb7041Schristos 	  change_warn = FALSE;
5446*56bb7041Schristos 	  break;
5447*56bb7041Schristos 
5448*56bb7041Schristos 	case OPTION_PAD_TO:
5449*56bb7041Schristos 	  pad_to = parse_vma (optarg, "--pad-to");
5450*56bb7041Schristos 	  pad_to_set = TRUE;
5451*56bb7041Schristos 	  break;
5452*56bb7041Schristos 
5453*56bb7041Schristos 	case OPTION_REMOVE_LEADING_CHAR:
5454*56bb7041Schristos 	  remove_leading_char = TRUE;
5455*56bb7041Schristos 	  break;
5456*56bb7041Schristos 
5457*56bb7041Schristos 	case OPTION_REDEFINE_SYM:
5458*56bb7041Schristos 	  {
5459*56bb7041Schristos 	    /* Insert this redefinition onto redefine_specific_htab.  */
5460*56bb7041Schristos 
5461*56bb7041Schristos 	    int len;
5462*56bb7041Schristos 	    const char *s;
5463*56bb7041Schristos 	    const char *nextarg;
5464*56bb7041Schristos 	    char *source, *target;
5465*56bb7041Schristos 
5466*56bb7041Schristos 	    s = strchr (optarg, '=');
5467*56bb7041Schristos 	    if (s == NULL)
5468*56bb7041Schristos 	      fatal (_("bad format for %s"), "--redefine-sym");
5469*56bb7041Schristos 
5470*56bb7041Schristos 	    len = s - optarg;
5471*56bb7041Schristos 	    source = (char *) xmalloc (len + 1);
5472*56bb7041Schristos 	    strncpy (source, optarg, len);
5473*56bb7041Schristos 	    source[len] = '\0';
5474*56bb7041Schristos 
5475*56bb7041Schristos 	    nextarg = s + 1;
5476*56bb7041Schristos 	    len = strlen (nextarg);
5477*56bb7041Schristos 	    target = (char *) xmalloc (len + 1);
5478*56bb7041Schristos 	    strcpy (target, nextarg);
5479*56bb7041Schristos 
5480*56bb7041Schristos 	    add_redefine_and_check ("--redefine-sym", source, target);
5481*56bb7041Schristos 
5482*56bb7041Schristos 	    free (source);
5483*56bb7041Schristos 	    free (target);
5484*56bb7041Schristos 	  }
5485*56bb7041Schristos 	  break;
5486*56bb7041Schristos 
5487*56bb7041Schristos 	case OPTION_REDEFINE_SYMS:
5488*56bb7041Schristos 	  add_redefine_syms_file (optarg);
5489*56bb7041Schristos 	  break;
5490*56bb7041Schristos 
5491*56bb7041Schristos 	case OPTION_SET_SECTION_FLAGS:
5492*56bb7041Schristos 	  {
5493*56bb7041Schristos 	    struct section_list *p;
5494*56bb7041Schristos 	    const char *s;
5495*56bb7041Schristos 	    int len;
5496*56bb7041Schristos 	    char *name;
5497*56bb7041Schristos 
5498*56bb7041Schristos 	    s = strchr (optarg, '=');
5499*56bb7041Schristos 	    if (s == NULL)
5500*56bb7041Schristos 	      fatal (_("bad format for %s"), "--set-section-flags");
5501*56bb7041Schristos 
5502*56bb7041Schristos 	    len = s - optarg;
5503*56bb7041Schristos 	    name = (char *) xmalloc (len + 1);
5504*56bb7041Schristos 	    strncpy (name, optarg, len);
5505*56bb7041Schristos 	    name[len] = '\0';
5506*56bb7041Schristos 
5507*56bb7041Schristos 	    p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS);
5508*56bb7041Schristos 
5509*56bb7041Schristos 	    p->flags = parse_flags (s + 1);
5510*56bb7041Schristos 	  }
5511*56bb7041Schristos 	  break;
5512*56bb7041Schristos 
5513*56bb7041Schristos 	case OPTION_SET_SECTION_ALIGNMENT:
5514*56bb7041Schristos 	  {
5515*56bb7041Schristos 	    struct section_list *p;
5516*56bb7041Schristos 	    const char *s;
5517*56bb7041Schristos 	    int len;
5518*56bb7041Schristos 	    char *name;
5519*56bb7041Schristos 	    int palign, align;
5520*56bb7041Schristos 
5521*56bb7041Schristos 	    s = strchr (optarg, '=');
5522*56bb7041Schristos 	    if (s == NULL)
5523*56bb7041Schristos 	      fatal (_("bad format for --set-section-alignment: argument needed"));
5524*56bb7041Schristos 
5525*56bb7041Schristos 	    align = atoi (s + 1);
5526*56bb7041Schristos 	    if (align <= 0)
5527*56bb7041Schristos 	      fatal (_("bad format for --set-section-alignment: numeric argument needed"));
5528*56bb7041Schristos 
5529*56bb7041Schristos 	    /* Convert integer alignment into a power-of-two alignment.  */
5530*56bb7041Schristos 	    palign = 0;
5531*56bb7041Schristos 	    while ((align & 1) == 0)
5532*56bb7041Schristos 	      {
5533*56bb7041Schristos 	    	align >>= 1;
5534*56bb7041Schristos 	    	++palign;
5535*56bb7041Schristos 	      }
5536*56bb7041Schristos 
5537*56bb7041Schristos 	    if (align != 1)
5538*56bb7041Schristos 	      /* Number has more than on 1, i.e. wasn't a power of 2.  */
5539*56bb7041Schristos 	      fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
5540*56bb7041Schristos 
5541*56bb7041Schristos 	    /* Add the alignment setting to the section list.  */
5542*56bb7041Schristos 	    len = s - optarg;
5543*56bb7041Schristos 	    name = (char *) xmalloc (len + 1);
5544*56bb7041Schristos 	    strncpy (name, optarg, len);
5545*56bb7041Schristos 	    name[len] = '\0';
5546*56bb7041Schristos 
5547*56bb7041Schristos 	    p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_ALIGNMENT);
5548*56bb7041Schristos 	    if (p)
5549*56bb7041Schristos 	      p->alignment = palign;
5550*56bb7041Schristos 	  }
5551*56bb7041Schristos 	  break;
5552*56bb7041Schristos 
5553*56bb7041Schristos 	case OPTION_RENAME_SECTION:
5554*56bb7041Schristos 	  {
5555*56bb7041Schristos 	    flagword flags;
5556*56bb7041Schristos 	    const char *eq, *fl;
5557*56bb7041Schristos 	    char *old_name;
5558*56bb7041Schristos 	    char *new_name;
5559*56bb7041Schristos 	    unsigned int len;
5560*56bb7041Schristos 
5561*56bb7041Schristos 	    eq = strchr (optarg, '=');
5562*56bb7041Schristos 	    if (eq == NULL)
5563*56bb7041Schristos 	      fatal (_("bad format for %s"), "--rename-section");
5564*56bb7041Schristos 
5565*56bb7041Schristos 	    len = eq - optarg;
5566*56bb7041Schristos 	    if (len == 0)
5567*56bb7041Schristos 	      fatal (_("bad format for %s"), "--rename-section");
5568*56bb7041Schristos 
5569*56bb7041Schristos 	    old_name = (char *) xmalloc (len + 1);
5570*56bb7041Schristos 	    strncpy (old_name, optarg, len);
5571*56bb7041Schristos 	    old_name[len] = 0;
5572*56bb7041Schristos 
5573*56bb7041Schristos 	    eq++;
5574*56bb7041Schristos 	    fl = strchr (eq, ',');
5575*56bb7041Schristos 	    if (fl)
5576*56bb7041Schristos 	      {
5577*56bb7041Schristos 		flags = parse_flags (fl + 1);
5578*56bb7041Schristos 		len = fl - eq;
5579*56bb7041Schristos 	      }
5580*56bb7041Schristos 	    else
5581*56bb7041Schristos 	      {
5582*56bb7041Schristos 		flags = -1;
5583*56bb7041Schristos 		len = strlen (eq);
5584*56bb7041Schristos 	      }
5585*56bb7041Schristos 
5586*56bb7041Schristos 	    if (len == 0)
5587*56bb7041Schristos 	      fatal (_("bad format for %s"), "--rename-section");
5588*56bb7041Schristos 
5589*56bb7041Schristos 	    new_name = (char *) xmalloc (len + 1);
5590*56bb7041Schristos 	    strncpy (new_name, eq, len);
5591*56bb7041Schristos 	    new_name[len] = 0;
5592*56bb7041Schristos 
5593*56bb7041Schristos 	    add_section_rename (old_name, new_name, flags);
5594*56bb7041Schristos 	  }
5595*56bb7041Schristos 	  break;
5596*56bb7041Schristos 
5597*56bb7041Schristos 	case OPTION_SET_START:
5598*56bb7041Schristos 	  set_start = parse_vma (optarg, "--set-start");
5599*56bb7041Schristos 	  set_start_set = TRUE;
5600*56bb7041Schristos 	  break;
5601*56bb7041Schristos 
5602*56bb7041Schristos 	case OPTION_SREC_LEN:
5603*56bb7041Schristos 	  _bfd_srec_len = parse_vma (optarg, "--srec-len");
5604*56bb7041Schristos 	  break;
5605*56bb7041Schristos 
5606*56bb7041Schristos 	case OPTION_SREC_FORCES3:
5607*56bb7041Schristos 	  _bfd_srec_forceS3 = TRUE;
5608*56bb7041Schristos 	  break;
5609*56bb7041Schristos 
5610*56bb7041Schristos 	case OPTION_STRIP_SYMBOLS:
5611*56bb7041Schristos 	  add_specific_symbols (optarg, strip_specific_htab,
5612*56bb7041Schristos 				&strip_specific_buffer);
5613*56bb7041Schristos 	  break;
5614*56bb7041Schristos 
5615*56bb7041Schristos 	case OPTION_STRIP_UNNEEDED_SYMBOLS:
5616*56bb7041Schristos 	  add_specific_symbols (optarg, strip_unneeded_htab,
5617*56bb7041Schristos 				&strip_unneeded_buffer);
5618*56bb7041Schristos 	  break;
5619*56bb7041Schristos 
5620*56bb7041Schristos 	case OPTION_KEEP_SYMBOLS:
5621*56bb7041Schristos 	  add_specific_symbols (optarg, keep_specific_htab,
5622*56bb7041Schristos 				&keep_specific_buffer);
5623*56bb7041Schristos 	  break;
5624*56bb7041Schristos 
5625*56bb7041Schristos 	case OPTION_LOCALIZE_HIDDEN:
5626*56bb7041Schristos 	  localize_hidden = TRUE;
5627*56bb7041Schristos 	  break;
5628*56bb7041Schristos 
5629*56bb7041Schristos 	case OPTION_LOCALIZE_SYMBOLS:
5630*56bb7041Schristos 	  add_specific_symbols (optarg, localize_specific_htab,
5631*56bb7041Schristos 				&localize_specific_buffer);
5632*56bb7041Schristos 	  break;
5633*56bb7041Schristos 
5634*56bb7041Schristos 	case OPTION_LONG_SECTION_NAMES:
5635*56bb7041Schristos 	  if (!strcmp ("enable", optarg))
5636*56bb7041Schristos 	    long_section_names = ENABLE;
5637*56bb7041Schristos 	  else if (!strcmp ("disable", optarg))
5638*56bb7041Schristos 	    long_section_names = DISABLE;
5639*56bb7041Schristos 	  else if (!strcmp ("keep", optarg))
5640*56bb7041Schristos 	    long_section_names = KEEP;
5641*56bb7041Schristos 	  else
5642*56bb7041Schristos 	    fatal (_("unknown long section names option '%s'"), optarg);
5643*56bb7041Schristos 	  break;
5644*56bb7041Schristos 
5645*56bb7041Schristos 	case OPTION_GLOBALIZE_SYMBOLS:
5646*56bb7041Schristos 	  use_globalize = TRUE;
5647*56bb7041Schristos 	  add_specific_symbols (optarg, globalize_specific_htab,
5648*56bb7041Schristos 				&globalize_specific_buffer);
5649*56bb7041Schristos 	  break;
5650*56bb7041Schristos 
5651*56bb7041Schristos 	case OPTION_KEEPGLOBAL_SYMBOLS:
5652*56bb7041Schristos 	  use_keep_global = TRUE;
5653*56bb7041Schristos 	  add_specific_symbols (optarg, keepglobal_specific_htab,
5654*56bb7041Schristos 				&keepglobal_specific_buffer);
5655*56bb7041Schristos 	  break;
5656*56bb7041Schristos 
5657*56bb7041Schristos 	case OPTION_WEAKEN_SYMBOLS:
5658*56bb7041Schristos 	  add_specific_symbols (optarg, weaken_specific_htab,
5659*56bb7041Schristos 				&weaken_specific_buffer);
5660*56bb7041Schristos 	  break;
5661*56bb7041Schristos 
5662*56bb7041Schristos 	case OPTION_ALT_MACH_CODE:
5663*56bb7041Schristos 	  use_alt_mach_code = strtoul (optarg, NULL, 0);
5664*56bb7041Schristos 	  if (use_alt_mach_code == 0)
5665*56bb7041Schristos 	    fatal (_("unable to parse alternative machine code"));
5666*56bb7041Schristos 	  break;
5667*56bb7041Schristos 
5668*56bb7041Schristos 	case OPTION_PREFIX_SYMBOLS:
5669*56bb7041Schristos 	  prefix_symbols_string = optarg;
5670*56bb7041Schristos 	  break;
5671*56bb7041Schristos 
5672*56bb7041Schristos 	case OPTION_PREFIX_SECTIONS:
5673*56bb7041Schristos 	  prefix_sections_string = optarg;
5674*56bb7041Schristos 	  break;
5675*56bb7041Schristos 
5676*56bb7041Schristos 	case OPTION_PREFIX_ALLOC_SECTIONS:
5677*56bb7041Schristos 	  prefix_alloc_sections_string = optarg;
5678*56bb7041Schristos 	  break;
5679*56bb7041Schristos 
5680*56bb7041Schristos 	case OPTION_READONLY_TEXT:
5681*56bb7041Schristos 	  bfd_flags_to_set |= WP_TEXT;
5682*56bb7041Schristos 	  bfd_flags_to_clear &= ~WP_TEXT;
5683*56bb7041Schristos 	  break;
5684*56bb7041Schristos 
5685*56bb7041Schristos 	case OPTION_WRITABLE_TEXT:
5686*56bb7041Schristos 	  bfd_flags_to_clear |= WP_TEXT;
5687*56bb7041Schristos 	  bfd_flags_to_set &= ~WP_TEXT;
5688*56bb7041Schristos 	  break;
5689*56bb7041Schristos 
5690*56bb7041Schristos 	case OPTION_PURE:
5691*56bb7041Schristos 	  bfd_flags_to_set |= D_PAGED;
5692*56bb7041Schristos 	  bfd_flags_to_clear &= ~D_PAGED;
5693*56bb7041Schristos 	  break;
5694*56bb7041Schristos 
5695*56bb7041Schristos 	case OPTION_IMPURE:
5696*56bb7041Schristos 	  bfd_flags_to_clear |= D_PAGED;
5697*56bb7041Schristos 	  bfd_flags_to_set &= ~D_PAGED;
5698*56bb7041Schristos 	  break;
5699*56bb7041Schristos 
5700*56bb7041Schristos 	case OPTION_EXTRACT_DWO:
5701*56bb7041Schristos 	  strip_symbols = STRIP_NONDWO;
5702*56bb7041Schristos 	  break;
5703*56bb7041Schristos 
5704*56bb7041Schristos 	case OPTION_EXTRACT_SYMBOL:
5705*56bb7041Schristos 	  extract_symbol = TRUE;
5706*56bb7041Schristos 	  break;
5707*56bb7041Schristos 
5708*56bb7041Schristos 	case OPTION_REVERSE_BYTES:
5709*56bb7041Schristos 	  {
5710*56bb7041Schristos 	    int prev = reverse_bytes;
5711*56bb7041Schristos 
5712*56bb7041Schristos 	    reverse_bytes = atoi (optarg);
5713*56bb7041Schristos 	    if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
5714*56bb7041Schristos 	      fatal (_("number of bytes to reverse must be positive and even"));
5715*56bb7041Schristos 
5716*56bb7041Schristos 	    if (prev && prev != reverse_bytes)
5717*56bb7041Schristos 	      non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
5718*56bb7041Schristos 			 prev);
5719*56bb7041Schristos 	    break;
5720*56bb7041Schristos 	  }
5721*56bb7041Schristos 
5722*56bb7041Schristos 	case OPTION_FILE_ALIGNMENT:
5723*56bb7041Schristos 	  pe_file_alignment = parse_vma (optarg, "--file-alignment");
5724*56bb7041Schristos 	  break;
5725*56bb7041Schristos 
5726*56bb7041Schristos 	case OPTION_HEAP:
5727*56bb7041Schristos 	  {
5728*56bb7041Schristos 	    char *end;
5729*56bb7041Schristos 	    pe_heap_reserve = strtoul (optarg, &end, 0);
5730*56bb7041Schristos 	    if (end == optarg
5731*56bb7041Schristos 		|| (*end != '.' && *end != '\0'))
5732*56bb7041Schristos 	      non_fatal (_("%s: invalid reserve value for --heap"),
5733*56bb7041Schristos 			 optarg);
5734*56bb7041Schristos 	    else if (*end != '\0')
5735*56bb7041Schristos 	      {
5736*56bb7041Schristos 		pe_heap_commit = strtoul (end + 1, &end, 0);
5737*56bb7041Schristos 		if (*end != '\0')
5738*56bb7041Schristos 		  non_fatal (_("%s: invalid commit value for --heap"),
5739*56bb7041Schristos 			     optarg);
5740*56bb7041Schristos 	      }
5741*56bb7041Schristos 	  }
5742*56bb7041Schristos 	  break;
5743*56bb7041Schristos 
5744*56bb7041Schristos 	case OPTION_IMAGE_BASE:
5745*56bb7041Schristos 	  pe_image_base = parse_vma (optarg, "--image-base");
5746*56bb7041Schristos 	  break;
5747*56bb7041Schristos 
5748*56bb7041Schristos 	case OPTION_PE_SECTION_ALIGNMENT:
5749*56bb7041Schristos 	  pe_section_alignment = parse_vma (optarg,
5750*56bb7041Schristos 					    "--section-alignment");
5751*56bb7041Schristos 	  break;
5752*56bb7041Schristos 
5753*56bb7041Schristos 	case OPTION_SUBSYSTEM:
5754*56bb7041Schristos 	  set_pe_subsystem (optarg);
5755*56bb7041Schristos 	  break;
5756*56bb7041Schristos 
5757*56bb7041Schristos 	case OPTION_STACK:
5758*56bb7041Schristos 	  {
5759*56bb7041Schristos 	    char *end;
5760*56bb7041Schristos 	    pe_stack_reserve = strtoul (optarg, &end, 0);
5761*56bb7041Schristos 	    if (end == optarg
5762*56bb7041Schristos 		|| (*end != '.' && *end != '\0'))
5763*56bb7041Schristos 	      non_fatal (_("%s: invalid reserve value for --stack"),
5764*56bb7041Schristos 			 optarg);
5765*56bb7041Schristos 	    else if (*end != '\0')
5766*56bb7041Schristos 	      {
5767*56bb7041Schristos 		pe_stack_commit = strtoul (end + 1, &end, 0);
5768*56bb7041Schristos 		if (*end != '\0')
5769*56bb7041Schristos 		  non_fatal (_("%s: invalid commit value for --stack"),
5770*56bb7041Schristos 			     optarg);
5771*56bb7041Schristos 	      }
5772*56bb7041Schristos 	  }
5773*56bb7041Schristos 	  break;
5774*56bb7041Schristos 
5775*56bb7041Schristos 	case OPTION_VERILOG_DATA_WIDTH:
5776*56bb7041Schristos 	  VerilogDataWidth = parse_vma (optarg, "--verilog-data-width");
5777*56bb7041Schristos 	  if (VerilogDataWidth < 1)
5778*56bb7041Schristos 	    fatal (_("verilog data width must be at least 1 byte"));
5779*56bb7041Schristos 	  break;
5780*56bb7041Schristos 
5781*56bb7041Schristos 	case 0:
5782*56bb7041Schristos 	  /* We've been given a long option.  */
5783*56bb7041Schristos 	  break;
5784*56bb7041Schristos 
5785*56bb7041Schristos 	case 'H':
5786*56bb7041Schristos 	case 'h':
5787*56bb7041Schristos 	  copy_usage (stdout, 0);
5788*56bb7041Schristos 
5789*56bb7041Schristos 	default:
5790*56bb7041Schristos 	  copy_usage (stderr, 1);
5791*56bb7041Schristos 	}
5792*56bb7041Schristos     }
5793*56bb7041Schristos 
5794*56bb7041Schristos   if (use_globalize && use_keep_global)
5795*56bb7041Schristos     fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)"));
5796*56bb7041Schristos 
5797*56bb7041Schristos   if (formats_info)
5798*56bb7041Schristos     {
5799*56bb7041Schristos       display_info ();
5800*56bb7041Schristos       return 0;
5801*56bb7041Schristos     }
5802*56bb7041Schristos 
5803*56bb7041Schristos   if (show_version)
5804*56bb7041Schristos     print_version ("objcopy");
5805*56bb7041Schristos 
5806*56bb7041Schristos   if (interleave && copy_byte == -1)
5807*56bb7041Schristos     fatal (_("interleave start byte must be set with --byte"));
5808*56bb7041Schristos 
5809*56bb7041Schristos   if (copy_byte >= interleave)
5810*56bb7041Schristos     fatal (_("byte number must be less than interleave"));
5811*56bb7041Schristos 
5812*56bb7041Schristos   if (copy_width > interleave - copy_byte)
5813*56bb7041Schristos     fatal (_("interleave width must be less than or equal to interleave - byte`"));
5814*56bb7041Schristos 
5815*56bb7041Schristos   if (optind == argc || optind + 2 < argc)
5816*56bb7041Schristos     copy_usage (stderr, 1);
5817*56bb7041Schristos 
5818*56bb7041Schristos   input_filename = argv[optind];
5819*56bb7041Schristos   if (optind + 1 < argc)
5820*56bb7041Schristos     output_filename = argv[optind + 1];
5821*56bb7041Schristos 
5822*56bb7041Schristos   default_deterministic ();
5823*56bb7041Schristos 
5824*56bb7041Schristos   /* Default is to strip no symbols.  */
5825*56bb7041Schristos   if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
5826*56bb7041Schristos     strip_symbols = STRIP_NONE;
5827*56bb7041Schristos 
5828*56bb7041Schristos   if (output_target == NULL)
5829*56bb7041Schristos     output_target = input_target;
5830*56bb7041Schristos 
5831*56bb7041Schristos   /* Convert input EFI target to PEI target.  */
5832*56bb7041Schristos   if (input_target != NULL
5833*56bb7041Schristos       && strncmp (input_target, "efi-", 4) == 0)
5834*56bb7041Schristos     {
5835*56bb7041Schristos       char *efi;
5836*56bb7041Schristos 
5837*56bb7041Schristos       efi = xstrdup (output_target + 4);
5838*56bb7041Schristos       if (strncmp (efi, "bsdrv-", 6) == 0
5839*56bb7041Schristos 	  || strncmp (efi, "rtdrv-", 6) == 0)
5840*56bb7041Schristos 	efi += 2;
5841*56bb7041Schristos       else if (strncmp (efi, "app-", 4) != 0)
5842*56bb7041Schristos 	fatal (_("unknown input EFI target: %s"), input_target);
5843*56bb7041Schristos 
5844*56bb7041Schristos       input_target = efi;
5845*56bb7041Schristos       convert_efi_target (efi);
5846*56bb7041Schristos     }
5847*56bb7041Schristos 
5848*56bb7041Schristos   /* Convert output EFI target to PEI target.  */
5849*56bb7041Schristos   if (output_target != NULL
5850*56bb7041Schristos       && strncmp (output_target, "efi-", 4) == 0)
5851*56bb7041Schristos     {
5852*56bb7041Schristos       char *efi;
5853*56bb7041Schristos 
5854*56bb7041Schristos       efi = xstrdup (output_target + 4);
5855*56bb7041Schristos       if (strncmp (efi, "app-", 4) == 0)
5856*56bb7041Schristos 	{
5857*56bb7041Schristos 	  if (pe_subsystem == -1)
5858*56bb7041Schristos 	    pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
5859*56bb7041Schristos 	}
5860*56bb7041Schristos       else if (strncmp (efi, "bsdrv-", 6) == 0)
5861*56bb7041Schristos 	{
5862*56bb7041Schristos 	  if (pe_subsystem == -1)
5863*56bb7041Schristos 	    pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
5864*56bb7041Schristos 	  efi += 2;
5865*56bb7041Schristos 	}
5866*56bb7041Schristos       else if (strncmp (efi, "rtdrv-", 6) == 0)
5867*56bb7041Schristos 	{
5868*56bb7041Schristos 	  if (pe_subsystem == -1)
5869*56bb7041Schristos 	    pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
5870*56bb7041Schristos 	  efi += 2;
5871*56bb7041Schristos 	}
5872*56bb7041Schristos       else
5873*56bb7041Schristos 	fatal (_("unknown output EFI target: %s"), output_target);
5874*56bb7041Schristos 
5875*56bb7041Schristos       if (pe_file_alignment == (bfd_vma) -1)
5876*56bb7041Schristos 	pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
5877*56bb7041Schristos       if (pe_section_alignment == (bfd_vma) -1)
5878*56bb7041Schristos 	pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
5879*56bb7041Schristos 
5880*56bb7041Schristos       output_target = efi;
5881*56bb7041Schristos       convert_efi_target (efi);
5882*56bb7041Schristos     }
5883*56bb7041Schristos 
5884*56bb7041Schristos   if (preserve_dates)
5885*56bb7041Schristos     if (stat (input_filename, & statbuf) < 0)
5886*56bb7041Schristos       fatal (_("warning: could not locate '%s'.  System error message: %s"),
5887*56bb7041Schristos 	     input_filename, strerror (errno));
5888*56bb7041Schristos 
5889*56bb7041Schristos   /* If there is no destination file, or the source and destination files
5890*56bb7041Schristos      are the same, then create a temp and rename the result into the input.  */
5891*56bb7041Schristos   if (output_filename == NULL
5892*56bb7041Schristos       || filename_cmp (input_filename, output_filename) == 0)
5893*56bb7041Schristos     tmpname = make_tempname (input_filename);
5894*56bb7041Schristos   else
5895*56bb7041Schristos     tmpname = output_filename;
5896*56bb7041Schristos 
5897*56bb7041Schristos   if (tmpname == NULL)
5898*56bb7041Schristos     fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
5899*56bb7041Schristos 	   input_filename, strerror (errno));
5900*56bb7041Schristos 
5901*56bb7041Schristos   copy_file (input_filename, tmpname, input_target, output_target, input_arch);
5902*56bb7041Schristos   if (status == 0)
5903*56bb7041Schristos     {
5904*56bb7041Schristos       if (preserve_dates)
5905*56bb7041Schristos 	set_times (tmpname, &statbuf);
5906*56bb7041Schristos       if (tmpname != output_filename)
5907*56bb7041Schristos 	status = (smart_rename (tmpname, input_filename,
5908*56bb7041Schristos 				preserve_dates) != 0);
5909*56bb7041Schristos     }
5910*56bb7041Schristos   else
5911*56bb7041Schristos     unlink_if_ordinary (tmpname);
5912*56bb7041Schristos 
5913*56bb7041Schristos   if (tmpname != output_filename)
5914*56bb7041Schristos     free (tmpname);
5915*56bb7041Schristos 
5916*56bb7041Schristos   if (change_warn)
5917*56bb7041Schristos     {
5918*56bb7041Schristos       struct section_list *p;
5919*56bb7041Schristos 
5920*56bb7041Schristos       for (p = change_sections; p != NULL; p = p->next)
5921*56bb7041Schristos 	{
5922*56bb7041Schristos 	  if (! p->used)
5923*56bb7041Schristos 	    {
5924*56bb7041Schristos 	      if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
5925*56bb7041Schristos 		{
5926*56bb7041Schristos 		  char buff [20];
5927*56bb7041Schristos 
5928*56bb7041Schristos 		  sprintf_vma (buff, p->vma_val);
5929*56bb7041Schristos 
5930*56bb7041Schristos 		  /* xgettext:c-format */
5931*56bb7041Schristos 		  non_fatal (_("%s %s%c0x%s never used"),
5932*56bb7041Schristos 			     "--change-section-vma",
5933*56bb7041Schristos 			     p->pattern,
5934*56bb7041Schristos 			     p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
5935*56bb7041Schristos 			     buff);
5936*56bb7041Schristos 		}
5937*56bb7041Schristos 
5938*56bb7041Schristos 	      if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
5939*56bb7041Schristos 		{
5940*56bb7041Schristos 		  char buff [20];
5941*56bb7041Schristos 
5942*56bb7041Schristos 		  sprintf_vma (buff, p->lma_val);
5943*56bb7041Schristos 
5944*56bb7041Schristos 		  /* xgettext:c-format */
5945*56bb7041Schristos 		  non_fatal (_("%s %s%c0x%s never used"),
5946*56bb7041Schristos 			     "--change-section-lma",
5947*56bb7041Schristos 			     p->pattern,
5948*56bb7041Schristos 			     p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
5949*56bb7041Schristos 			     buff);
5950*56bb7041Schristos 		}
5951*56bb7041Schristos 	    }
5952*56bb7041Schristos 	}
5953*56bb7041Schristos     }
5954*56bb7041Schristos 
5955*56bb7041Schristos   if (strip_specific_buffer)
5956*56bb7041Schristos     free (strip_specific_buffer);
5957*56bb7041Schristos 
5958*56bb7041Schristos   if (strip_unneeded_buffer)
5959*56bb7041Schristos     free (strip_unneeded_buffer);
5960*56bb7041Schristos 
5961*56bb7041Schristos   if (keep_specific_buffer)
5962*56bb7041Schristos     free (keep_specific_buffer);
5963*56bb7041Schristos 
5964*56bb7041Schristos   if (localize_specific_buffer)
5965*56bb7041Schristos     free (globalize_specific_buffer);
5966*56bb7041Schristos 
5967*56bb7041Schristos   if (globalize_specific_buffer)
5968*56bb7041Schristos     free (globalize_specific_buffer);
5969*56bb7041Schristos 
5970*56bb7041Schristos   if (keepglobal_specific_buffer)
5971*56bb7041Schristos     free (keepglobal_specific_buffer);
5972*56bb7041Schristos 
5973*56bb7041Schristos   if (weaken_specific_buffer)
5974*56bb7041Schristos     free (weaken_specific_buffer);
5975*56bb7041Schristos 
5976*56bb7041Schristos   return 0;
5977*56bb7041Schristos }
5978*56bb7041Schristos 
5979*56bb7041Schristos int
main(int argc,char * argv[])5980*56bb7041Schristos main (int argc, char *argv[])
5981*56bb7041Schristos {
5982*56bb7041Schristos #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
5983*56bb7041Schristos   setlocale (LC_MESSAGES, "");
5984*56bb7041Schristos #endif
5985*56bb7041Schristos #if defined (HAVE_SETLOCALE)
5986*56bb7041Schristos   setlocale (LC_CTYPE, "");
5987*56bb7041Schristos #endif
5988*56bb7041Schristos   bindtextdomain (PACKAGE, LOCALEDIR);
5989*56bb7041Schristos   textdomain (PACKAGE);
5990*56bb7041Schristos 
5991*56bb7041Schristos   program_name = argv[0];
5992*56bb7041Schristos   xmalloc_set_program_name (program_name);
5993*56bb7041Schristos 
5994*56bb7041Schristos   START_PROGRESS (program_name, 0);
5995*56bb7041Schristos 
5996*56bb7041Schristos   expandargv (&argc, &argv);
5997*56bb7041Schristos 
5998*56bb7041Schristos   strip_symbols = STRIP_UNDEF;
5999*56bb7041Schristos   discard_locals = LOCALS_UNDEF;
6000*56bb7041Schristos 
6001*56bb7041Schristos   if (bfd_init () != BFD_INIT_MAGIC)
6002*56bb7041Schristos     fatal (_("fatal error: libbfd ABI mismatch"));
6003*56bb7041Schristos   set_default_bfd_target ();
6004*56bb7041Schristos 
6005*56bb7041Schristos   if (is_strip < 0)
6006*56bb7041Schristos     {
6007*56bb7041Schristos       int i = strlen (program_name);
6008*56bb7041Schristos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
6009*56bb7041Schristos       /* Drop the .exe suffix, if any.  */
6010*56bb7041Schristos       if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
6011*56bb7041Schristos 	{
6012*56bb7041Schristos 	  i -= 4;
6013*56bb7041Schristos 	  program_name[i] = '\0';
6014*56bb7041Schristos 	}
6015*56bb7041Schristos #endif
6016*56bb7041Schristos       is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
6017*56bb7041Schristos     }
6018*56bb7041Schristos 
6019*56bb7041Schristos   create_symbol_htabs ();
6020*56bb7041Schristos 
6021*56bb7041Schristos   if (argv != NULL)
6022*56bb7041Schristos     bfd_set_error_program_name (argv[0]);
6023*56bb7041Schristos 
6024*56bb7041Schristos   if (is_strip)
6025*56bb7041Schristos     strip_main (argc, argv);
6026*56bb7041Schristos   else
6027*56bb7041Schristos     copy_main (argc, argv);
6028*56bb7041Schristos 
6029*56bb7041Schristos   END_PROGRESS (program_name);
6030*56bb7041Schristos 
6031*56bb7041Schristos   return status;
6032*56bb7041Schristos }
6033