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