objcopy.c 168 KB
Newer Older
Richard Henderson's avatar
Richard Henderson committed
1
/* objcopy.c -- copy object file from input to output, optionally massaging it.
2
   Copyright (C) 1991-2020 Free Software Foundation, Inc.
Richard Henderson's avatar
Richard Henderson committed
3 4 5 6 7

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
Richard Henderson's avatar
Richard Henderson committed
9 10 11 12 13 14 15 16 17
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
Nick Clifton's avatar
Nick Clifton committed
18 19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */
Richard Henderson's avatar
Richard Henderson committed
20

Alan Modra's avatar
 
Alan Modra committed
21
#include "sysdep.h"
Richard Henderson's avatar
Richard Henderson committed
22
#include "bfd.h"
23
#include "libbfd.h"
Richard Henderson's avatar
Richard Henderson committed
24 25 26
#include "progress.h"
#include "getopt.h"
#include "libiberty.h"
Alan Modra's avatar
 
Alan Modra committed
27
#include "bucomm.h"
Richard Henderson's avatar
Richard Henderson committed
28
#include "budbg.h"
29
#include "filenames.h"
30
#include "fnmatch.h"
31
#include "elf-bfd.h"
Dave Korn's avatar
Dave Korn committed
32 33
#include "coff/internal.h"
#include "libcoff.h"
34
#include "safe-ctype.h"
Richard Henderson's avatar
Richard Henderson committed
35

H.J. Lu's avatar
bfd/  
H.J. Lu committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* FIXME: See bfd/peXXigen.c for why we include an architecture specific
   header in generic PE code.  */
#include "coff/i386.h"
#include "coff/pe.h"

static bfd_vma pe_file_alignment = (bfd_vma) -1;
static bfd_vma pe_heap_commit = (bfd_vma) -1;
static bfd_vma pe_heap_reserve = (bfd_vma) -1;
static bfd_vma pe_image_base = (bfd_vma) -1;
static bfd_vma pe_section_alignment = (bfd_vma) -1;
static bfd_vma pe_stack_commit = (bfd_vma) -1;
static bfd_vma pe_stack_reserve = (bfd_vma) -1;
static short pe_subsystem = -1;
static short pe_major_subsystem_version = -1;
static short pe_minor_subsystem_version = -1;

Nick Clifton's avatar
Nick Clifton committed
52
struct is_specified_symbol_predicate_data
Richard Henderson's avatar
Richard Henderson committed
53
{
54
  const char *  name;
Nick Clifton's avatar
Nick Clifton committed
55
  bfd_boolean	found;
Richard Henderson's avatar
Richard Henderson committed
56 57
};

58
/* A node includes symbol name mapping to support redefine_sym.  */
Alan Modra's avatar
Alan Modra committed
59 60 61 62 63 64
struct redefine_node
{
  char *source;
  char *target;
};

65 66 67 68 69 70 71
struct addsym_node
{
  struct addsym_node *next;
  char *    symdef;
  long      symval;
  flagword  flags;
  char *    section;
72
  const char *  othersym;
73 74
};

75 76 77 78 79 80 81 82 83 84
typedef struct section_rename
{
  const char *            old_name;
  const char *            new_name;
  flagword                flags;
  struct section_rename * next;
}
section_rename;

/* List of sections to be renamed.  */
Nick Clifton's avatar
Nick Clifton committed
85
static section_rename *section_rename_list;
86

Nick Clifton's avatar
Nick Clifton committed
87 88
static asymbol **isympp = NULL;	/* Input symbols.  */
static asymbol **osympp = NULL;	/* Output symbols that survive stripping.  */
Richard Henderson's avatar
Richard Henderson committed
89

90
/* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes.  */
Richard Henderson's avatar
Richard Henderson committed
91
static int copy_byte = -1;
92 93
static int interleave = 0; /* Initialised to 4 in copy_main().  */
static int copy_width = 1;
Richard Henderson's avatar
Richard Henderson committed
94

95 96
static bfd_boolean verbose;		/* Print file and target names.  */
static bfd_boolean preserve_dates;	/* Preserve input file timestamp.  */
Roland McGrath's avatar
Roland McGrath committed
97
static int deterministic = -1;		/* Enable deterministic archives.  */
98 99 100
static int status = 0;			/* Exit status.  */

static bfd_boolean    merge_notes = FALSE;	/* Merge note sections.  */
101 102 103 104 105 106 107 108

typedef struct merged_note_section
{
  asection *                    sec;	 /* The section that is being merged.  */
  bfd_byte *                    contents;/* New contents of the section.  */
  bfd_size_type                 size;	 /* New size of the section.  */
  struct merged_note_section *  next;  	 /* Link to next merged note section.  */
} merged_note_section;
Richard Henderson's avatar
Richard Henderson committed
109 110

enum strip_action
111 112 113 114 115 116 117 118 119 120
{
  STRIP_UNDEF,
  STRIP_NONE,		/* Don't strip.  */
  STRIP_DEBUG,		/* Strip all debugger symbols.  */
  STRIP_UNNEEDED,	/* Strip unnecessary symbols.  */
  STRIP_NONDEBUG,	/* Strip everything but debug info.  */
  STRIP_DWO,		/* Strip all DWO info.  */
  STRIP_NONDWO,		/* Strip everything but DWO info.  */
  STRIP_ALL		/* Strip all symbols.  */
};
Richard Henderson's avatar
Richard Henderson committed
121

Kazu Hirata's avatar
Kazu Hirata committed
122
/* Which symbols to remove.  */
123
static enum strip_action strip_symbols = STRIP_UNDEF;
Richard Henderson's avatar
Richard Henderson committed
124 125

enum locals_action
126 127 128 129 130
{
  LOCALS_UNDEF,
  LOCALS_START_L,	/* Discard locals starting with L.  */
  LOCALS_ALL		/* Discard all locals.  */
};
Richard Henderson's avatar
Richard Henderson committed
131 132 133 134 135 136 137

/* Which local symbols to remove.  Overrides STRIP_ALL.  */
static enum locals_action discard_locals;

/* Structure used to hold lists of sections and actions to take.  */
struct section_list
{
138
  struct section_list * next;	   /* Next section to change.  */
Nick Clifton's avatar
Nick Clifton committed
139
  const char *		pattern;   /* Section name pattern.  */
140
  bfd_boolean		used;	   /* Whether this entry was used.  */
Nick Clifton's avatar
Nick Clifton committed
141 142 143 144 145 146

  unsigned int          context;   /* What to do with matching sections.  */
  /* Flag bits used in the context field.
     COPY and REMOVE are mutually exlusive.  SET and ALTER are mutually exclusive.  */
#define SECTION_CONTEXT_REMOVE    (1 << 0) /* Remove this section.  */
#define SECTION_CONTEXT_COPY      (1 << 1) /* Copy this section, delete all non-copied section.  */
147 148 149 150 151 152 153 154
#define SECTION_CONTEXT_KEEP      (1 << 2) /* Keep this section.  */
#define SECTION_CONTEXT_SET_VMA   (1 << 3) /* Set the sections' VMA address.  */
#define SECTION_CONTEXT_ALTER_VMA (1 << 4) /* Increment or decrement the section's VMA address.  */
#define SECTION_CONTEXT_SET_LMA   (1 << 5) /* Set the sections' LMA address.  */
#define SECTION_CONTEXT_ALTER_LMA (1 << 6) /* Increment or decrement the section's LMA address.  */
#define SECTION_CONTEXT_SET_FLAGS (1 << 7) /* Set the section's flags.  */
#define SECTION_CONTEXT_REMOVE_RELOCS (1 << 8) /* Remove relocations for this section.  */
#define SECTION_CONTEXT_SET_ALIGNMENT (1 << 9) /* Set alignment for section.  */
Nick Clifton's avatar
Nick Clifton committed
155

156 157 158
  bfd_vma		vma_val;   /* Amount to change by or set to.  */
  bfd_vma		lma_val;   /* Amount to change by or set to.  */
  flagword		flags;	   /* What to set the section flags to.	 */
159
  unsigned int	        alignment; /* Alignment of output section.  */
Richard Henderson's avatar
Richard Henderson committed
160 161 162
};

static struct section_list *change_sections;
163

164 165
/* TRUE if some sections are to be removed.  */
static bfd_boolean sections_removed;
166

167 168
/* TRUE if only some sections are to be copied.  */
static bfd_boolean sections_copied;
Richard Henderson's avatar
Richard Henderson committed
169 170 171

/* Changes to the start address.  */
static bfd_vma change_start = 0;
172
static bfd_boolean set_start_set = FALSE;
Richard Henderson's avatar
Richard Henderson committed
173 174 175 176 177 178
static bfd_vma set_start;

/* Changes to section addresses.  */
static bfd_vma change_section_address = 0;

/* Filling gaps between sections.  */
179
static bfd_boolean gap_fill_set = FALSE;
Richard Henderson's avatar
Richard Henderson committed
180 181 182
static bfd_byte gap_fill = 0;

/* Pad to a given address.  */
183
static bfd_boolean pad_to_set = FALSE;
Richard Henderson's avatar
Richard Henderson committed
184 185
static bfd_vma pad_to;

186 187
/* Use alternative machine code?  */
static unsigned long use_alt_mach_code = 0;
188

189 190 191 192
/* Output BFD flags user wants to set or clear */
static flagword bfd_flags_to_set;
static flagword bfd_flags_to_clear;

Richard Henderson's avatar
Richard Henderson committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/* List of sections to add.  */
struct section_add
{
  /* Next section to add.  */
  struct section_add *next;
  /* Name of section to add.  */
  const char *name;
  /* Name of file holding section contents.  */
  const char *filename;
  /* Size of file.  */
  size_t size;
  /* Contents of file.  */
  bfd_byte *contents;
  /* BFD section, after it has been added.  */
  asection *section;
};

210
/* List of sections to add to the output BFD.  */
Richard Henderson's avatar
Richard Henderson committed
211 212
static struct section_add *add_sections;

213 214 215
/* List of sections to update in the output BFD.  */
static struct section_add *update_sections;

216 217 218
/* List of sections to dump from the output BFD.  */
static struct section_add *dump_sections;

219 220 221 222
/* If non-NULL the argument to --add-gnu-debuglink.
   This should be the filename to store in the .gnu_debuglink section.  */
static const char * gnu_debuglink_filename = NULL;

Richard Henderson's avatar
Richard Henderson committed
223
/* Whether to convert debugging information.  */
224
static bfd_boolean convert_debugging = FALSE;
Richard Henderson's avatar
Richard Henderson committed
225

226 227 228
/* Whether to compress/decompress DWARF debug sections.  */
static enum
{
229 230 231 232 233 234
  nothing = 0,
  compress = 1 << 0,
  compress_zlib = compress | 1 << 1,
  compress_gnu_zlib = compress | 1 << 2,
  compress_gabi_zlib = compress | 1 << 3,
  decompress = 1 << 4
235 236
} do_debug_sections = nothing;

H.J. Lu's avatar
H.J. Lu committed
237
/* Whether to generate ELF common symbols with the STT_COMMON type.  */
238
static enum bfd_link_elf_stt_common do_elf_stt_common = unchanged;
H.J. Lu's avatar
H.J. Lu committed
239

Richard Henderson's avatar
Richard Henderson committed
240
/* Whether to change the leading character in symbol names.  */
241
static bfd_boolean change_leading_char = FALSE;
Richard Henderson's avatar
Richard Henderson committed
242 243

/* Whether to remove the leading character from global symbol names.  */
244
static bfd_boolean remove_leading_char = FALSE;
Richard Henderson's avatar
Richard Henderson committed
245

246
/* Whether to permit wildcard in symbol comparison.  */
247 248
static bfd_boolean wildcard = FALSE;

Richard Sandiford's avatar
Richard Sandiford committed
249 250 251
/* True if --localize-hidden is in effect.  */
static bfd_boolean localize_hidden = FALSE;

252 253
/* List of symbols to strip, keep, localize, keep-global, weaken,
   or redefine.  */
Nick Clifton's avatar
Nick Clifton committed
254 255 256 257 258 259 260
static htab_t strip_specific_htab = NULL;
static htab_t strip_unneeded_htab = NULL;
static htab_t keep_specific_htab = NULL;
static htab_t localize_specific_htab = NULL;
static htab_t globalize_specific_htab = NULL;
static htab_t keepglobal_specific_htab = NULL;
static htab_t weaken_specific_htab = NULL;
261 262
static htab_t redefine_specific_htab = NULL;
static htab_t redefine_specific_reverse_htab = NULL;
263 264
static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
static int add_symbols = 0;
Richard Henderson's avatar
Richard Henderson committed
265

266 267 268 269 270 271 272 273
static char *strip_specific_buffer = NULL;
static char *strip_unneeded_buffer = NULL;
static char *keep_specific_buffer = NULL;
static char *localize_specific_buffer = NULL;
static char *globalize_specific_buffer = NULL;
static char *keepglobal_specific_buffer = NULL;
static char *weaken_specific_buffer = NULL;

274 275
/* If this is TRUE, we weaken global symbols (set BSF_WEAK).  */
static bfd_boolean weaken = FALSE;
Richard Henderson's avatar
Richard Henderson committed
276

Jan Beulich's avatar
Jan Beulich committed
277 278 279
/* If this is TRUE, we retain BSF_FILE symbols.  */
static bfd_boolean keep_file_symbols = FALSE;

280 281 282 283 284
/* Prefix symbols/sections.  */
static char *prefix_symbols_string = 0;
static char *prefix_sections_string = 0;
static char *prefix_alloc_sections_string = 0;

Richard Sandiford's avatar
Richard Sandiford committed
285 286 287
/* True if --extract-symbol was passed on the command line.  */
static bfd_boolean extract_symbol = FALSE;

288 289 290 291
/* If `reverse_bytes' is nonzero, then reverse the order of every chunk
   of <reverse_bytes> bytes within each output section.  */
static int reverse_bytes = 0;

Dave Korn's avatar
Dave Korn committed
292 293 294
/* For Coff objects, we may want to allow or disallow long section names,
   or preserve them where found in the inputs.  Debug info relies on them.  */
enum long_section_name_handling
295 296 297 298 299
{
  DISABLE,
  ENABLE,
  KEEP
};
Dave Korn's avatar
Dave Korn committed
300 301 302 303

/* The default long section handling mode is to preserve them.
   This is also the only behaviour for 'strip'.  */
static enum long_section_name_handling long_section_names = KEEP;
304

Richard Henderson's avatar
Richard Henderson committed
305
/* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
Nick Clifton's avatar
Nick Clifton committed
306
enum command_line_switch
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
{
  OPTION_ADD_SECTION=150,
  OPTION_ADD_GNU_DEBUGLINK,
  OPTION_ADD_SYMBOL,
  OPTION_ALT_MACH_CODE,
  OPTION_CHANGE_ADDRESSES,
  OPTION_CHANGE_LEADING_CHAR,
  OPTION_CHANGE_SECTION_ADDRESS,
  OPTION_CHANGE_SECTION_LMA,
  OPTION_CHANGE_SECTION_VMA,
  OPTION_CHANGE_START,
  OPTION_CHANGE_WARNINGS,
  OPTION_COMPRESS_DEBUG_SECTIONS,
  OPTION_DEBUGGING,
  OPTION_DECOMPRESS_DEBUG_SECTIONS,
  OPTION_DUMP_SECTION,
H.J. Lu's avatar
H.J. Lu committed
323
  OPTION_ELF_STT_COMMON,
324 325 326 327 328 329 330 331 332 333 334 335 336
  OPTION_EXTRACT_DWO,
  OPTION_EXTRACT_SYMBOL,
  OPTION_FILE_ALIGNMENT,
  OPTION_FORMATS_INFO,
  OPTION_GAP_FILL,
  OPTION_GLOBALIZE_SYMBOL,
  OPTION_GLOBALIZE_SYMBOLS,
  OPTION_HEAP,
  OPTION_IMAGE_BASE,
  OPTION_IMPURE,
  OPTION_INTERLEAVE_WIDTH,
  OPTION_KEEPGLOBAL_SYMBOLS,
  OPTION_KEEP_FILE_SYMBOLS,
337
  OPTION_KEEP_SECTION,
338 339 340 341
  OPTION_KEEP_SYMBOLS,
  OPTION_LOCALIZE_HIDDEN,
  OPTION_LOCALIZE_SYMBOLS,
  OPTION_LONG_SECTION_NAMES,
342
  OPTION_MERGE_NOTES,
343
  OPTION_NO_MERGE_NOTES,
344 345 346 347 348 349 350 351 352 353 354
  OPTION_NO_CHANGE_WARNINGS,
  OPTION_ONLY_KEEP_DEBUG,
  OPTION_PAD_TO,
  OPTION_PREFIX_ALLOC_SECTIONS,
  OPTION_PREFIX_SECTIONS,
  OPTION_PREFIX_SYMBOLS,
  OPTION_PURE,
  OPTION_READONLY_TEXT,
  OPTION_REDEFINE_SYM,
  OPTION_REDEFINE_SYMS,
  OPTION_REMOVE_LEADING_CHAR,
355
  OPTION_REMOVE_RELOCS,
356 357
  OPTION_RENAME_SECTION,
  OPTION_REVERSE_BYTES,
358
  OPTION_PE_SECTION_ALIGNMENT,
359
  OPTION_SET_SECTION_FLAGS,
360
  OPTION_SET_SECTION_ALIGNMENT,
361 362 363 364 365 366 367 368 369 370 371
  OPTION_SET_START,
  OPTION_SREC_FORCES3,
  OPTION_SREC_LEN,
  OPTION_STACK,
  OPTION_STRIP_DWO,
  OPTION_STRIP_SYMBOLS,
  OPTION_STRIP_UNNEEDED,
  OPTION_STRIP_UNNEEDED_SYMBOL,
  OPTION_STRIP_UNNEEDED_SYMBOLS,
  OPTION_SUBSYSTEM,
  OPTION_UPDATE_SECTION,
372
  OPTION_VERILOG_DATA_WIDTH,
373 374 375 376
  OPTION_WEAKEN,
  OPTION_WEAKEN_SYMBOLS,
  OPTION_WRITABLE_TEXT
};
Richard Henderson's avatar
Richard Henderson committed
377 378 379 380 381

/* Options to handle if running as "strip".  */

static struct option strip_options[] =
{
Roland McGrath's avatar
Roland McGrath committed
382
  {"disable-deterministic-archives", no_argument, 0, 'U'},
Richard Henderson's avatar
Richard Henderson committed
383 384
  {"discard-all", no_argument, 0, 'x'},
  {"discard-locals", no_argument, 0, 'X'},
385
  {"enable-deterministic-archives", no_argument, 0, 'D'},
Richard Henderson's avatar
Richard Henderson committed
386 387
  {"format", required_argument, 0, 'F'}, /* Obsolete */
  {"help", no_argument, 0, 'h'},
Nick Clifton's avatar
Nick Clifton committed
388
  {"info", no_argument, 0, OPTION_FORMATS_INFO},
Richard Henderson's avatar
Richard Henderson committed
389 390
  {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  {"input-target", required_argument, 0, 'I'},
Jan Beulich's avatar
Jan Beulich committed
391
  {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
392
  {"keep-section", required_argument, 0, OPTION_KEEP_SECTION},
Richard Henderson's avatar
Richard Henderson committed
393
  {"keep-symbol", required_argument, 0, 'K'},
394 395
  {"merge-notes", no_argument, 0, 'M'},
  {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
Nick Clifton's avatar
Nick Clifton committed
396
  {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
397
  {"output-file", required_argument, 0, 'o'},
Richard Henderson's avatar
Richard Henderson committed
398 399 400 401
  {"output-format", required_argument, 0, 'O'},	/* Obsolete */
  {"output-target", required_argument, 0, 'O'},
  {"preserve-dates", no_argument, 0, 'p'},
  {"remove-section", required_argument, 0, 'R'},
402
  {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
Richard Henderson's avatar
Richard Henderson committed
403 404
  {"strip-all", no_argument, 0, 's'},
  {"strip-debug", no_argument, 0, 'S'},
Cary Coutant's avatar
Cary Coutant committed
405
  {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
Richard Henderson's avatar
Richard Henderson committed
406
  {"strip-symbol", required_argument, 0, 'N'},
407
  {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
Richard Henderson's avatar
Richard Henderson committed
408 409 410
  {"target", required_argument, 0, 'F'},
  {"verbose", no_argument, 0, 'v'},
  {"version", no_argument, 0, 'V'},
411
  {"wildcard", no_argument, 0, 'w'},
Richard Henderson's avatar
Richard Henderson committed
412 413 414 415 416 417 418
  {0, no_argument, 0, 0}
};

/* Options to handle if running as "objcopy".  */

static struct option copy_options[] =
{
419
  {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
Richard Henderson's avatar
Richard Henderson committed
420
  {"add-section", required_argument, 0, OPTION_ADD_SECTION},
421 422
  {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
  {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
Richard Henderson's avatar
Richard Henderson committed
423 424 425
  {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
  {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
  {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
426
  {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
427
  {"binary-architecture", required_argument, 0, 'B'},
Richard Henderson's avatar
Richard Henderson committed
428 429 430 431 432 433 434 435
  {"byte", required_argument, 0, 'b'},
  {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
  {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
  {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
  {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
  {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
  {"change-start", required_argument, 0, OPTION_CHANGE_START},
  {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
436
  {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
Richard Henderson's avatar
Richard Henderson committed
437
  {"debugging", no_argument, 0, OPTION_DEBUGGING},
438
  {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
Roland McGrath's avatar
Roland McGrath committed
439
  {"disable-deterministic-archives", no_argument, 0, 'U'},
Richard Henderson's avatar
Richard Henderson committed
440 441
  {"discard-all", no_argument, 0, 'x'},
  {"discard-locals", no_argument, 0, 'X'},
442
  {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
H.J. Lu's avatar
H.J. Lu committed
443
  {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
444
  {"enable-deterministic-archives", no_argument, 0, 'D'},
Cary Coutant's avatar
Cary Coutant committed
445
  {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
Richard Sandiford's avatar
Richard Sandiford committed
446
  {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
447
  {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
Richard Henderson's avatar
Richard Henderson committed
448 449
  {"format", required_argument, 0, 'F'}, /* Obsolete */
  {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
450 451
  {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
  {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
452
  {"heap", required_argument, 0, OPTION_HEAP},
Richard Henderson's avatar
Richard Henderson committed
453
  {"help", no_argument, 0, 'h'},
454
  {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
455
  {"impure", no_argument, 0, OPTION_IMPURE},
Nick Clifton's avatar
Nick Clifton committed
456
  {"info", no_argument, 0, OPTION_FORMATS_INFO},
Richard Henderson's avatar
Richard Henderson committed
457 458
  {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  {"input-target", required_argument, 0, 'I'},
459 460
  {"interleave", optional_argument, 0, 'i'},
  {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
Jan Beulich's avatar
Jan Beulich committed
461
  {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
462 463
  {"keep-global-symbol", required_argument, 0, 'G'},
  {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
464
  {"keep-section", required_argument, 0, OPTION_KEEP_SECTION},
Richard Henderson's avatar
Richard Henderson committed
465
  {"keep-symbol", required_argument, 0, 'K'},
466
  {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
Richard Sandiford's avatar
Richard Sandiford committed
467
  {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
468 469
  {"localize-symbol", required_argument, 0, 'L'},
  {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
Dave Korn's avatar
Dave Korn committed
470
  {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
471
  {"merge-notes", no_argument, 0, 'M'},
472
  {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
Richard Henderson's avatar
Richard Henderson committed
473 474
  {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
  {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
Nick Clifton's avatar
Nick Clifton committed
475
  {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
476
  {"only-section", required_argument, 0, 'j'},
Richard Henderson's avatar
Richard Henderson committed
477 478 479
  {"output-format", required_argument, 0, 'O'},	/* Obsolete */
  {"output-target", required_argument, 0, 'O'},
  {"pad-to", required_argument, 0, OPTION_PAD_TO},
480
  {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
481 482
  {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
  {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
Richard Henderson's avatar
Richard Henderson committed
483
  {"preserve-dates", no_argument, 0, 'p'},
484 485
  {"pure", no_argument, 0, OPTION_PURE},
  {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
486
  {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
487
  {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
Richard Henderson's avatar
Richard Henderson committed
488 489
  {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
  {"remove-section", required_argument, 0, 'R'},
490
  {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
491
  {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
492
  {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
493
  {"section-alignment", required_argument, 0, OPTION_PE_SECTION_ALIGNMENT},
Richard Henderson's avatar
Richard Henderson committed
494
  {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
495
  {"set-section-alignment", required_argument, 0, OPTION_SET_SECTION_ALIGNMENT},
Richard Henderson's avatar
Richard Henderson committed
496
  {"set-start", required_argument, 0, OPTION_SET_START},
497
  {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
498 499
  {"srec-len", required_argument, 0, OPTION_SREC_LEN},
  {"stack", required_argument, 0, OPTION_STACK},
Richard Henderson's avatar
Richard Henderson committed
500 501
  {"strip-all", no_argument, 0, 'S'},
  {"strip-debug", no_argument, 0, 'g'},
Cary Coutant's avatar
Cary Coutant committed
502
  {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
503 504
  {"strip-symbol", required_argument, 0, 'N'},
  {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
Richard Henderson's avatar
Richard Henderson committed
505
  {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
Jan Beulich's avatar
Jan Beulich committed
506 507
  {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
  {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
508
  {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
Richard Henderson's avatar
Richard Henderson committed
509
  {"target", required_argument, 0, 'F'},
510
  {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
Richard Henderson's avatar
Richard Henderson committed
511
  {"verbose", no_argument, 0, 'v'},
512
  {"verilog-data-width", required_argument, 0, OPTION_VERILOG_DATA_WIDTH},
Richard Henderson's avatar
Richard Henderson committed
513 514 515
  {"version", no_argument, 0, 'V'},
  {"weaken", no_argument, 0, OPTION_WEAKEN},
  {"weaken-symbol", required_argument, 0, 'W'},
516
  {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
517
  {"wildcard", no_argument, 0, 'w'},
518
  {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
Richard Henderson's avatar
Richard Henderson committed
519 520 521 522 523 524 525 526
  {0, no_argument, 0, 0}
};

/* IMPORTS */
extern char *program_name;

/* This flag distinguishes between strip and objcopy:
   1 means this is 'strip'; 0 means this is 'objcopy'.
Kazu Hirata's avatar
Kazu Hirata committed
527
   -1 means if we should use argv[0] to decide.  */
Richard Henderson's avatar
Richard Henderson committed
528 529
extern int is_strip;

Pedro Alves's avatar
Pedro Alves committed
530
/* The maximum length of an S record.  This variable is defined in srec.c
531
   and can be modified by the --srec-len parameter.  */
Pedro Alves's avatar
Pedro Alves committed
532
extern unsigned int _bfd_srec_len;
533 534

/* Restrict the generation of Srecords to type S3 only.
Pedro Alves's avatar
Pedro Alves committed
535
   This variable is defined in bfd/srec.c and can be toggled
536
   on by the --srec-forceS3 command line switch.  */
Pedro Alves's avatar
Pedro Alves committed
537
extern bfd_boolean _bfd_srec_forceS3;
Richard Henderson's avatar
Richard Henderson committed
538

539 540 541 542 543
/* Width of data in bytes for verilog output.
   This variable is declared in bfd/verilog.c and can be modified by
   the --verilog-data-width parameter.  */
extern unsigned int VerilogDataWidth;

544 545
/* Forward declarations.  */
static void setup_section (bfd *, asection *, void *);
Bob Wilson's avatar
Bob Wilson committed
546
static void setup_bfd_headers (bfd *, bfd *);
H.J. Lu's avatar
H.J. Lu committed
547
static void copy_relocations_in_section (bfd *, asection *, void *);
548 549 550 551 552 553
static void copy_section (bfd *, asection *, void *);
static void get_sections (bfd *, asection *, void *);
static int compare_section_lma (const void *, const void *);
static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
static const char *lookup_sym_redefinition (const char *);
Alan Modra's avatar
Alan Modra committed
554
static const char *find_section_rename (const char *, flagword *);
555

556
ATTRIBUTE_NORETURN static void
Nick Clifton's avatar
Nick Clifton committed
557
copy_usage (FILE *stream, int exit_status)
Richard Henderson's avatar
Richard Henderson committed
558
{
559 560
  fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
  fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
561
  fprintf (stream, _(" The options are:\n"));
Richard Henderson's avatar
Richard Henderson committed
562
  fprintf (stream, _("\
Nick Clifton's avatar
Nick Clifton committed
563 564
  -I --input-target <bfdname>      Assume input file is in format <bfdname>\n\
  -O --output-target <bfdname>     Create an output file in format <bfdname>\n\
565
  -B --binary-architecture <arch>  Set output arch, when input is arch-less\n\
Nick Clifton's avatar
Nick Clifton committed
566 567
  -F --target <bfdname>            Set both input and output format to <bfdname>\n\
     --debugging                   Convert debugging information, if possible\n\
Roland McGrath's avatar
Roland McGrath committed
568 569 570 571 572 573 574 575 576
  -p --preserve-dates              Copy modified/access timestamps to the output\n"));
  if (DEFAULT_AR_DETERMINISTIC)
    fprintf (stream, _("\
  -D --enable-deterministic-archives\n\
                                   Produce deterministic output when stripping archives (default)\n\
  -U --disable-deterministic-archives\n\
                                   Disable -D behavior\n"));
  else
    fprintf (stream, _("\
577 578
  -D --enable-deterministic-archives\n\
                                   Produce deterministic output when stripping archives\n\
Roland McGrath's avatar
Roland McGrath committed
579 580 581
  -U --disable-deterministic-archives\n\
                                   Disable -D behavior (default)\n"));
  fprintf (stream, _("\
Nick Clifton's avatar
Nick Clifton committed
582
  -j --only-section <name>         Only copy section <name> into the output\n\
583
     --add-gnu-debuglink=<file>    Add section .gnu_debuglink linking to <file>\n\
Nick Clifton's avatar
Nick Clifton committed
584
  -R --remove-section <name>       Remove section <name> from the output\n\
585
     --remove-relocations <name>   Remove relocations from section <name>\n\
Nick Clifton's avatar
Nick Clifton committed
586
  -S --strip-all                   Remove all symbol and relocation information\n\
587
  -g --strip-debug                 Remove all debugging symbols & sections\n\
Cary Coutant's avatar
Cary Coutant committed
588
     --strip-dwo                   Remove all DWO sections\n\
Nick Clifton's avatar
Nick Clifton committed
589 590
     --strip-unneeded              Remove all symbols not needed by relocations\n\
  -N --strip-symbol <name>         Do not copy symbol <name>\n\
Jan Beulich's avatar
Jan Beulich committed
591 592 593
     --strip-unneeded-symbol <name>\n\
                                   Do not copy symbol <name> unless needed by\n\
                                     relocations\n\
594
     --only-keep-debug             Strip everything but the debug information\n\
Cary Coutant's avatar
Cary Coutant committed
595
     --extract-dwo                 Copy only DWO sections\n\
Richard Sandiford's avatar
Richard Sandiford committed
596
     --extract-symbol              Remove section contents but keep symbols\n\
597
     --keep-section <name>         Do not strip section <name>\n\
598
  -K --keep-symbol <name>          Do not strip symbol <name>\n\
Jan Beulich's avatar
Jan Beulich committed
599
     --keep-file-symbols           Do not strip file symbol(s)\n\
Richard Sandiford's avatar
Richard Sandiford committed
600
     --localize-hidden             Turn all ELF hidden symbols into locals\n\
Nick Clifton's avatar
Nick Clifton committed
601
  -L --localize-symbol <name>      Force symbol <name> to be marked as a local\n\
602
     --globalize-symbol <name>     Force symbol <name> to be marked as a global\n\
603
  -G --keep-global-symbol <name>   Localize all symbols except <name>\n\
Nick Clifton's avatar
Nick Clifton committed
604 605
  -W --weaken-symbol <name>        Force symbol <name> to be marked as a weak\n\
     --weaken                      Force all global symbols to be marked as weak\n\
606
  -w --wildcard                    Permit wildcard in symbol comparison\n\
Nick Clifton's avatar
Nick Clifton committed
607 608
  -x --discard-all                 Remove all non-global symbols\n\
  -X --discard-locals              Remove any compiler-generated symbols\n\
H.J. Lu's avatar
H.J. Lu committed
609
  -i --interleave[=<number>]       Only copy N out of every <number> bytes\n\
610
     --interleave-width <number>   Set N for --interleave\n\
Nick Clifton's avatar
Nick Clifton committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
  -b --byte <num>                  Select byte <num> in every interleaved block\n\
     --gap-fill <val>              Fill gaps between sections with <val>\n\
     --pad-to <addr>               Pad the last section up to address <addr>\n\
     --set-start <addr>            Set the start address to <addr>\n\
    {--change-start|--adjust-start} <incr>\n\
                                   Add <incr> to the start address\n\
    {--change-addresses|--adjust-vma} <incr>\n\
                                   Add <incr> to LMA, VMA and start addresses\n\
    {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
                                   Change LMA and VMA of section <name> by <val>\n\
     --change-section-lma <name>{=|+|-}<val>\n\
                                   Change the LMA of section <name> by <val>\n\
     --change-section-vma <name>{=|+|-}<val>\n\
                                   Change the VMA of section <name> by <val>\n\
    {--[no-]change-warnings|--[no-]adjust-warnings}\n\
                                   Warn if a named section does not exist\n\
     --set-section-flags <name>=<flags>\n\
                                   Set section <name>'s properties to <flags>\n\
629
     --set-section-alignment <name>=<align>\n\
630
                                   Set section <name>'s alignment to <align> bytes\n\
Nick Clifton's avatar
Nick Clifton committed
631
     --add-section <name>=<file>   Add section <name> found in <file> to output\n\
632 633 634
     --update-section <name>=<file>\n\
                                   Update contents of section <name> with\n\
                                   contents found in <file>\n\
635
     --dump-section <name>=<file>  Dump the contents of section <name> into <file>\n\
636
     --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
Dave Korn's avatar
Dave Korn committed
637 638
     --long-section-names {enable|disable|keep}\n\
                                   Handle long section names in Coff objects.\n\
Nick Clifton's avatar
Nick Clifton committed
639 640
     --change-leading-char         Force output format's leading character style\n\
     --remove-leading-char         Remove leading character from global symbols\n\
641
     --reverse-bytes=<num>         Reverse <num> bytes at a time, in output sections with content\n\
Alan Modra's avatar
Alan Modra committed
642
     --redefine-sym <old>=<new>    Redefine symbol name <old> to <new>\n\
643 644
     --redefine-syms <file>        --redefine-sym for all symbol pairs \n\
                                     listed in <file>\n\
645 646
     --srec-len <number>           Restrict the length of generated Srecords\n\
     --srec-forceS3                Restrict the type of generated Srecords to S3\n\
647
     --strip-symbols <file>        -N for all symbols listed in <file>\n\
Jan Beulich's avatar
Jan Beulich committed
648 649 650
     --strip-unneeded-symbols <file>\n\
                                   --strip-unneeded-symbol for all symbols listed\n\
                                     in <file>\n\
651 652
     --keep-symbols <file>         -K for all symbols listed in <file>\n\
     --localize-symbols <file>     -L for all symbols listed in <file>\n\
653
     --globalize-symbols <file>    --globalize-symbol for all in <file>\n\
654 655
     --keep-global-symbols <file>  -G for all symbols listed in <file>\n\
     --weaken-symbols <file>       -W for all symbols listed in <file>\n\
656
     --add-symbol <name>=[<section>:]<value>[,<flags>]  Add a symbol\n\
657
     --alt-machine-code <index>    Use the target's <index>'th alternative machine\n\
658 659 660 661
     --writable-text               Mark the output text as writable\n\
     --readonly-text               Make the output text write protected\n\
     --pure                        Mark the output file as demand paged\n\
     --impure                      Mark the output file as impure\n\
662 663 664 665 666
     --prefix-symbols <prefix>     Add <prefix> to start of every symbol name\n\
     --prefix-sections <prefix>    Add <prefix> to start of every section name\n\
     --prefix-alloc-sections <prefix>\n\
                                   Add <prefix> to start of every allocatable\n\
                                     section name\n\
H.J. Lu's avatar
bfd/  
H.J. Lu committed
667 668 669 670 671 672 673 674
     --file-alignment <num>        Set PE file alignment to <num>\n\
     --heap <reserve>[,<commit>]   Set PE reserve/commit heap to <reserve>/\n\
                                   <commit>\n\
     --image-base <address>        Set PE image base to <address>\n\
     --section-alignment <num>     Set PE section alignment to <num>\n\
     --stack <reserve>[,<commit>]  Set PE reserve/commit stack to <reserve>/\n\
                                   <commit>\n\
     --subsystem <name>[:<version>]\n\
675
                                   Set PE subsystem to <name> [& <version>]\n\
676 677
     --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
                                   Compress DWARF debug sections using zlib\n\
678
     --decompress-debug-sections   Decompress DWARF debug sections using zlib\n\
H.J. Lu's avatar
H.J. Lu committed
679 680
     --elf-stt-common=[yes|no]     Generate ELF common symbols with STT_COMMON\n\
                                     type\n\
681
     --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
682
  -M  --merge-notes                Remove redundant entries in note sections\n\
683
      --no-merge-notes             Do not attempt to remove redundant notes (default)\n\
Nick Clifton's avatar
Nick Clifton committed
684
  -v --verbose                     List all object files modified\n\
685
  @<file>                          Read options from <file>\n\
Nick Clifton's avatar
Nick Clifton committed
686 687
  -V --version                     Display this program's version number\n\
  -h --help                        Display this output\n\
Nick Clifton's avatar
Nick Clifton committed
688
     --info                        List object formats & architectures supported\n\
Nick Clifton's avatar
Nick Clifton committed
689
"));
Richard Henderson's avatar
Richard Henderson committed
690
  list_supported_targets (program_name, stream);
Joseph Myers's avatar
Joseph Myers committed
691
  if (REPORT_BUGS_TO[0] && exit_status == 0)
692
    fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
Richard Henderson's avatar
Richard Henderson committed
693 694 695
  exit (exit_status);
}

696
ATTRIBUTE_NORETURN static void
Nick Clifton's avatar
Nick Clifton committed
697
strip_usage (FILE *stream, int exit_status)
Richard Henderson's avatar
Richard Henderson committed
698
{
699 700
  fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
  fprintf (stream, _(" Removes symbols and sections from files\n"));
701
  fprintf (stream, _(" The options are:\n"));
Richard Henderson's avatar
Richard Henderson committed
702
  fprintf (stream, _("\
703 704 705
  -I --input-target=<bfdname>      Assume input file is in format <bfdname>\n\
  -O --output-target=<bfdname>     Create an output file in format <bfdname>\n\
  -F --target=<bfdname>            Set both input and output format to <bfdname>\n\
Nick Clifton's avatar
Nick Clifton committed
706
  -p --preserve-dates              Copy modified/access timestamps to the output\n\
Roland McGrath's avatar
Roland McGrath committed
707 708 709 710 711 712 713 714 715
"));
  if (DEFAULT_AR_DETERMINISTIC)
    fprintf (stream, _("\
  -D --enable-deterministic-archives\n\
                                   Produce deterministic output when stripping archives (default)\n\
  -U --disable-deterministic-archives\n\
                                   Disable -D behavior\n"));
  else
    fprintf (stream, _("\
716 717
  -D --enable-deterministic-archives\n\
                                   Produce deterministic output when stripping archives\n\
Roland McGrath's avatar
Roland McGrath committed
718 719 720
  -U --disable-deterministic-archives\n\
                                   Disable -D behavior (default)\n"));
  fprintf (stream, _("\
Andrew Stubbs's avatar
Andrew Stubbs committed
721
  -R --remove-section=<name>       Also remove section <name> from the output\n\
722
     --remove-relocations <name>   Remove relocations from section <name>\n\
Nick Clifton's avatar
Nick Clifton committed
723
  -s --strip-all                   Remove all symbol and relocation information\n\
724
  -g -S -d --strip-debug           Remove all debugging symbols & sections\n\
Cary Coutant's avatar
Cary Coutant committed
725
     --strip-dwo                   Remove all DWO sections\n\
Nick Clifton's avatar
Nick Clifton committed
726
     --strip-unneeded              Remove all symbols not needed by relocations\n\
727
     --only-keep-debug             Strip everything but the debug information\n\
728 729
  -M  --merge-notes                Remove redundant entries in note sections (default)\n\
      --no-merge-notes             Do not attempt to remove redundant notes\n\
730
  -N --strip-symbol=<name>         Do not copy symbol <name>\n\
731
     --keep-section=<name>         Do not strip section <name>\n\
732
  -K --keep-symbol=<name>          Do not strip symbol <name>\n\
Jan Beulich's avatar
Jan Beulich committed
733
     --keep-file-symbols           Do not strip file symbol(s)\n\
734
  -w --wildcard                    Permit wildcard in symbol comparison\n\
Nick Clifton's avatar
Nick Clifton committed
735 736 737 738 739
  -x --discard-all                 Remove all non-global symbols\n\
  -X --discard-locals              Remove any compiler-generated symbols\n\
  -v --verbose                     List all object files modified\n\
  -V --version                     Display this program's version number\n\
  -h --help                        Display this output\n\
Nick Clifton's avatar
Nick Clifton committed
740
     --info                        List object formats & architectures supported\n\
Nick Clifton's avatar
Nick Clifton committed
741 742 743
  -o <file>                        Place stripped output into <file>\n\
"));

Richard Henderson's avatar
Richard Henderson committed
744
  list_supported_targets (program_name, stream);
Joseph Myers's avatar
Joseph Myers committed
745
  if (REPORT_BUGS_TO[0] && exit_status == 0)
746
    fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
Richard Henderson's avatar
Richard Henderson committed
747 748 749 750 751 752 753
  exit (exit_status);
}

/* Parse section flags into a flagword, with a fatal error if the
   string can't be parsed.  */

static flagword
Nick Clifton's avatar
Nick Clifton committed
754
parse_flags (const char *s)
Richard Henderson's avatar
Richard Henderson committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
{
  flagword ret;
  const char *snext;
  int len;

  ret = SEC_NO_FLAGS;

  do
    {
      snext = strchr (s, ',');
      if (snext == NULL)
	len = strlen (s);
      else
	{
	  len = snext - s;
	  ++snext;
	}

      if (0) ;
774 775
#define PARSE_FLAG(fname,fval)					\
      else if (strncasecmp (fname, s, len) == 0) ret |= fval
Richard Henderson's avatar
Richard Henderson committed
776 777
      PARSE_FLAG ("alloc", SEC_ALLOC);
      PARSE_FLAG ("load", SEC_LOAD);
778
      PARSE_FLAG ("noload", SEC_NEVER_LOAD);
Richard Henderson's avatar
Richard Henderson committed
779
      PARSE_FLAG ("readonly", SEC_READONLY);
780
      PARSE_FLAG ("debug", SEC_DEBUGGING);
Richard Henderson's avatar
Richard Henderson committed
781 782 783
      PARSE_FLAG ("code", SEC_CODE);
      PARSE_FLAG ("data", SEC_DATA);
      PARSE_FLAG ("rom", SEC_ROM);
784
      PARSE_FLAG ("exclude", SEC_EXCLUDE);
H.J. Lu's avatar
bfd/  
H.J. Lu committed
785
      PARSE_FLAG ("share", SEC_COFF_SHARED);
Richard Henderson's avatar
Richard Henderson committed
786
      PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
787 788
      PARSE_FLAG ("merge", SEC_MERGE);
      PARSE_FLAG ("strings", SEC_STRINGS);
Richard Henderson's avatar
Richard Henderson committed
789 790 791 792 793
#undef PARSE_FLAG
      else
	{
	  char *copy;

794
	  copy = (char *) xmalloc (len + 1);
Richard Henderson's avatar
Richard Henderson committed
795 796 797
	  strncpy (copy, s, len);
	  copy[len] = '\0';
	  non_fatal (_("unrecognized section flag `%s'"), copy);
Alan Modra's avatar
Alan Modra committed
798
	  fatal (_("supported flags: %s"),
799
		 "alloc, load, noload, readonly, debug, code, data, rom, exclude, share, contents, merge, strings");
Richard Henderson's avatar
Richard Henderson committed
800 801 802 803 804 805 806 807 808
	}

      s = snext;
    }
  while (s != NULL);

  return ret;
}

809 810 811 812
/* Parse symbol flags into a flagword, with a fatal error if the
   string can't be parsed.  */

static flagword
813
parse_symflags (const char *s, const char **other)
814 815 816
{
  flagword ret;
  const char *snext;
817
  size_t len;
818 819 820 821 822 823 824

  ret = BSF_NO_FLAGS;

  do
    {
      snext = strchr (s, ',');
      if (snext == NULL)
825
	len = strlen (s);
826 827 828 829 830 831
      else
	{
	  len = snext - s;
	  ++snext;
	}

832 833 834
#define PARSE_FLAG(fname, fval)						\
      else if (len == sizeof fname - 1					\
	       && strncasecmp (fname, s, len) == 0)			\
835 836
	ret |= fval

837 838 839
#define PARSE_OTHER(fname, fval)					\
      else if (len >= sizeof fname					\
	       && strncasecmp (fname, s, sizeof fname - 1) == 0)	\
840
	fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
841

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
      if (0) ;
      PARSE_FLAG ("local", BSF_LOCAL);
      PARSE_FLAG ("global", BSF_GLOBAL);
      PARSE_FLAG ("export", BSF_EXPORT);
      PARSE_FLAG ("debug", BSF_DEBUGGING);
      PARSE_FLAG ("function", BSF_FUNCTION);
      PARSE_FLAG ("weak", BSF_WEAK);
      PARSE_FLAG ("section", BSF_SECTION_SYM);
      PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
      PARSE_FLAG ("warning", BSF_WARNING);
      PARSE_FLAG ("indirect", BSF_INDIRECT);
      PARSE_FLAG ("file", BSF_FILE);
      PARSE_FLAG ("object", BSF_OBJECT);
      PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
      PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
      PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
      PARSE_OTHER ("before=", *other);

#undef PARSE_FLAG
#undef PARSE_OTHER
      else
	{
	  char *copy;

	  copy = (char *) xmalloc (len + 1);
	  strncpy (copy, s, len);
	  copy[len] = '\0';
	  non_fatal (_("unrecognized symbol flag `%s'"), copy);
	  fatal (_("supported flags: %s"),
871 872 873
		 "local, global, export, debug, function, weak, section, "
		 "constructor, warning, indirect, file, object, synthetic, "
		 "indirect-function, unique-object, before=<othersym>");
874 875 876 877 878 879 880 881 882
	}

      s = snext;
    }
  while (s != NULL);

  return ret;
}

Nick Clifton's avatar
Nick Clifton committed
883 884 885 886 887
/* Find and optionally add an entry in the change_sections list.

   We need to be careful in how we match section names because of the support
   for wildcard characters.  For example suppose that the user has invoked
   objcopy like this:
H.J. Lu's avatar
H.J. Lu committed
888

Nick Clifton's avatar
Nick Clifton committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
       --set-section-flags .debug_*=debug
       --set-section-flags .debug_str=readonly,debug
       --change-section-address .debug_*ranges=0x1000

   With the idea that all debug sections will receive the DEBUG flag, the
   .debug_str section will also receive the READONLY flag and the
   .debug_ranges and .debug_aranges sections will have their address set to
   0x1000.  (This may not make much sense, but it is just an example).

   When adding the section name patterns to the section list we need to make
   sure that previous entries do not match with the new entry, unless the
   match is exact.  (In which case we assume that the user is overriding
   the previous entry with the new context).

   When matching real section names to the section list we make use of the
   wildcard characters, but we must do so in context.  Eg if we are setting
   section addresses then we match for .debug_ranges but not for .debug_info.

   Finally, if ADD is false and we do find a match, we mark the section list
   entry as used.  */
Richard Henderson's avatar
Richard Henderson committed
909 910

static struct section_list *
Nick Clifton's avatar
Nick Clifton committed
911
find_section_list (const char *name, bfd_boolean add, unsigned int context)
Richard Henderson's avatar
Richard Henderson committed
912
{
913
  struct section_list *p, *match = NULL;
Richard Henderson's avatar
Richard Henderson committed
914

Nick Clifton's avatar
Nick Clifton committed
915
  /* assert ((context & ((1 << 7) - 1)) != 0); */
H.J. Lu's avatar
H.J. Lu committed
916

Richard Henderson's avatar
Richard Henderson committed
917
  for (p = change_sections; p != NULL; p = p->next)
Nick Clifton's avatar
Nick Clifton committed
918 919 920 921 922 923