-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathobjcopy.c
5721 lines (4908 loc) · 158 KB
/
objcopy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* objcopy.c -- copy object file from input to output, optionally massaging it.
Copyright (C) 1991-2019 Free Software Foundation, Inc.
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
the Free Software Foundation; either version 3 of the License, or
(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
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
#include "sysdep.h"
#include "bfd.h"
#include "progress.h"
#include "getopt.h"
#include "libiberty.h"
#include "bucomm.h"
#include "budbg.h"
#include "filenames.h"
#include "fnmatch.h"
#include "elf-bfd.h"
#include "coff/internal.h"
#include "libcoff.h"
#include "safe-ctype.h"
/* 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;
struct is_specified_symbol_predicate_data
{
const char * name;
bfd_boolean found;
};
/* A node includes symbol name mapping to support redefine_sym. */
struct redefine_node
{
char *source;
char *target;
};
struct addsym_node
{
struct addsym_node *next;
char * symdef;
long symval;
flagword flags;
char * section;
char * othersym;
};
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. */
static section_rename *section_rename_list;
static asymbol **isympp = NULL; /* Input symbols. */
static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
/* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
static int copy_byte = -1;
static int interleave = 0; /* Initialised to 4 in copy_main(). */
static int copy_width = 1;
static bfd_boolean verbose; /* Print file and target names. */
static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
static int deterministic = -1; /* Enable deterministic archives. */
static int status = 0; /* Exit status. */
static bfd_boolean merge_notes = FALSE; /* Merge note sections. */
static bfd_byte * merged_notes = NULL; /* Contents on note section undergoing a merge. */
static bfd_size_type merged_size = 0; /* New, smaller size of the merged note section. */
enum strip_action
{
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. */
};
/* Which symbols to remove. */
static enum strip_action strip_symbols = STRIP_UNDEF;
enum locals_action
{
LOCALS_UNDEF,
LOCALS_START_L, /* Discard locals starting with L. */
LOCALS_ALL /* Discard all locals. */
};
/* 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
{
struct section_list * next; /* Next section to change. */
const char * pattern; /* Section name pattern. */
bfd_boolean used; /* Whether this entry was used. */
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. */
#define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */
#define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */
#define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */
#define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */
#define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */
#define SECTION_CONTEXT_REMOVE_RELOCS (1 << 7) /* Remove relocations for this section. */
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. */
};
static struct section_list *change_sections;
/* TRUE if some sections are to be removed. */
static bfd_boolean sections_removed;
/* TRUE if only some sections are to be copied. */
static bfd_boolean sections_copied;
/* Changes to the start address. */
static bfd_vma change_start = 0;
static bfd_boolean set_start_set = FALSE;
static bfd_vma set_start;
/* Changes to section addresses. */
static bfd_vma change_section_address = 0;
/* Filling gaps between sections. */
static bfd_boolean gap_fill_set = FALSE;
static bfd_byte gap_fill = 0;
/* Pad to a given address. */
static bfd_boolean pad_to_set = FALSE;
static bfd_vma pad_to;
/* Use alternative machine code? */
static unsigned long use_alt_mach_code = 0;
/* Output BFD flags user wants to set or clear */
static flagword bfd_flags_to_set;
static flagword bfd_flags_to_clear;
/* 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;
};
/* List of sections to add to the output BFD. */
static struct section_add *add_sections;
/* List of sections to update in the output BFD. */
static struct section_add *update_sections;
/* List of sections to dump from the output BFD. */
static struct section_add *dump_sections;
/* 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;
/* Whether to convert debugging information. */
static bfd_boolean convert_debugging = FALSE;
/* Whether to compress/decompress DWARF debug sections. */
static enum
{
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
} do_debug_sections = nothing;
/* Whether to generate ELF common symbols with the STT_COMMON type. */
static enum bfd_link_elf_stt_common do_elf_stt_common = unchanged;
/* Whether to change the leading character in symbol names. */
static bfd_boolean change_leading_char = FALSE;
/* Whether to remove the leading character from global symbol names. */
static bfd_boolean remove_leading_char = FALSE;
/* Whether to permit wildcard in symbol comparison. */
static bfd_boolean wildcard = FALSE;
/* True if --localize-hidden is in effect. */
static bfd_boolean localize_hidden = FALSE;
/* List of symbols to strip, keep, localize, keep-global, weaken,
or redefine. */
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;
static htab_t redefine_specific_htab = NULL;
static htab_t redefine_specific_reverse_htab = NULL;
static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
static int add_symbols = 0;
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;
/* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
static bfd_boolean weaken = FALSE;
/* If this is TRUE, we retain BSF_FILE symbols. */
static bfd_boolean keep_file_symbols = FALSE;
/* Prefix symbols/sections. */
static char *prefix_symbols_string = 0;
static char *prefix_sections_string = 0;
static char *prefix_alloc_sections_string = 0;
/* True if --extract-symbol was passed on the command line. */
static bfd_boolean extract_symbol = FALSE;
/* 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;
/* 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
{
DISABLE,
ENABLE,
KEEP
};
/* 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;
/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
enum command_line_switch
{
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,
OPTION_ELF_STT_COMMON,
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,
OPTION_KEEP_SYMBOLS,
OPTION_LOCALIZE_HIDDEN,
OPTION_LOCALIZE_SYMBOLS,
OPTION_LONG_SECTION_NAMES,
OPTION_MERGE_NOTES,
OPTION_NO_MERGE_NOTES,
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,
OPTION_REMOVE_RELOCS,
OPTION_RENAME_SECTION,
OPTION_REVERSE_BYTES,
OPTION_SECTION_ALIGNMENT,
OPTION_SET_SECTION_FLAGS,
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,
OPTION_WEAKEN,
OPTION_WEAKEN_SYMBOLS,
OPTION_WRITABLE_TEXT
};
/* Options to handle if running as "strip". */
static struct option strip_options[] =
{
{"disable-deterministic-archives", no_argument, 0, 'U'},
{"discard-all", no_argument, 0, 'x'},
{"discard-locals", no_argument, 0, 'X'},
{"enable-deterministic-archives", no_argument, 0, 'D'},
{"format", required_argument, 0, 'F'}, /* Obsolete */
{"help", no_argument, 0, 'h'},
{"info", no_argument, 0, OPTION_FORMATS_INFO},
{"input-format", required_argument, 0, 'I'}, /* Obsolete */
{"input-target", required_argument, 0, 'I'},
{"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
{"keep-symbol", required_argument, 0, 'K'},
{"merge-notes", no_argument, 0, 'M'},
{"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
{"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
{"output-file", required_argument, 0, 'o'},
{"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'},
{"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
{"strip-all", no_argument, 0, 's'},
{"strip-debug", no_argument, 0, 'S'},
{"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
{"strip-symbol", required_argument, 0, 'N'},
{"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
{"target", required_argument, 0, 'F'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"wildcard", no_argument, 0, 'w'},
{0, no_argument, 0, 0}
};
/* Options to handle if running as "objcopy". */
static struct option copy_options[] =
{
{"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
{"add-section", required_argument, 0, OPTION_ADD_SECTION},
{"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
{"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
{"adjust-start", required_argument, 0, OPTION_CHANGE_START},
{"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
{"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
{"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
{"binary-architecture", required_argument, 0, 'B'},
{"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},
{"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
{"debugging", no_argument, 0, OPTION_DEBUGGING},
{"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
{"disable-deterministic-archives", no_argument, 0, 'U'},
{"discard-all", no_argument, 0, 'x'},
{"discard-locals", no_argument, 0, 'X'},
{"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
{"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
{"enable-deterministic-archives", no_argument, 0, 'D'},
{"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
{"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
{"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
{"format", required_argument, 0, 'F'}, /* Obsolete */
{"gap-fill", required_argument, 0, OPTION_GAP_FILL},
{"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
{"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
{"heap", required_argument, 0, OPTION_HEAP},
{"help", no_argument, 0, 'h'},
{"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
{"impure", no_argument, 0, OPTION_IMPURE},
{"info", no_argument, 0, OPTION_FORMATS_INFO},
{"input-format", required_argument, 0, 'I'}, /* Obsolete */
{"input-target", required_argument, 0, 'I'},
{"interleave", optional_argument, 0, 'i'},
{"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
{"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
{"keep-global-symbol", required_argument, 0, 'G'},
{"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
{"keep-symbol", required_argument, 0, 'K'},
{"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
{"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
{"localize-symbol", required_argument, 0, 'L'},
{"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
{"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
{"merge-notes", no_argument, 0, 'M'},
{"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
{"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
{"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
{"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
{"only-section", required_argument, 0, 'j'},
{"output-format", required_argument, 0, 'O'}, /* Obsolete */
{"output-target", required_argument, 0, 'O'},
{"pad-to", required_argument, 0, OPTION_PAD_TO},
{"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
{"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
{"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
{"preserve-dates", no_argument, 0, 'p'},
{"pure", no_argument, 0, OPTION_PURE},
{"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
{"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
{"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
{"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
{"remove-section", required_argument, 0, 'R'},
{"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
{"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
{"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
{"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
{"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
{"set-start", required_argument, 0, OPTION_SET_START},
{"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
{"srec-len", required_argument, 0, OPTION_SREC_LEN},
{"stack", required_argument, 0, OPTION_STACK},
{"strip-all", no_argument, 0, 'S'},
{"strip-debug", no_argument, 0, 'g'},
{"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
{"strip-symbol", required_argument, 0, 'N'},
{"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
{"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
{"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
{"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
{"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
{"target", required_argument, 0, 'F'},
{"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"weaken", no_argument, 0, OPTION_WEAKEN},
{"weaken-symbol", required_argument, 0, 'W'},
{"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
{"wildcard", no_argument, 0, 'w'},
{"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
{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'.
-1 means if we should use argv[0] to decide. */
extern int is_strip;
/* The maximum length of an S record. This variable is defined in srec.c
and can be modified by the --srec-len parameter. */
extern unsigned int _bfd_srec_len;
/* Restrict the generation of Srecords to type S3 only.
This variable is defined in bfd/srec.c and can be toggled
on by the --srec-forceS3 command line switch. */
extern bfd_boolean _bfd_srec_forceS3;
/* Forward declarations. */
static void setup_section (bfd *, asection *, void *);
static void setup_bfd_headers (bfd *, bfd *);
static void copy_relocations_in_section (bfd *, asection *, void *);
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 *);
static const char *find_section_rename (const char *, flagword *);
ATTRIBUTE_NORETURN static void
copy_usage (FILE *stream, int exit_status)
{
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"));
fprintf (stream, _(" The options are:\n"));
fprintf (stream, _("\
-I --input-target <bfdname> Assume input file is in format <bfdname>\n\
-O --output-target <bfdname> Create an output file in format <bfdname>\n\
-B --binary-architecture <arch> Set output arch, when input is arch-less\n\
-F --target <bfdname> Set both input and output format to <bfdname>\n\
--debugging Convert debugging information, if possible\n\
-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, _("\
-D --enable-deterministic-archives\n\
Produce deterministic output when stripping archives\n\
-U --disable-deterministic-archives\n\
Disable -D behavior (default)\n"));
fprintf (stream, _("\
-j --only-section <name> Only copy section <name> into the output\n\
--add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
-R --remove-section <name> Remove section <name> from the output\n\
--remove-relocations <name> Remove relocations from section <name>\n\
-S --strip-all Remove all symbol and relocation information\n\
-g --strip-debug Remove all debugging symbols & sections\n\
--strip-dwo Remove all DWO sections\n\
--strip-unneeded Remove all symbols not needed by relocations\n\
-N --strip-symbol <name> Do not copy symbol <name>\n\
--strip-unneeded-symbol <name>\n\
Do not copy symbol <name> unless needed by\n\
relocations\n\
--only-keep-debug Strip everything but the debug information\n\
--extract-dwo Copy only DWO sections\n\
--extract-symbol Remove section contents but keep symbols\n\
-K --keep-symbol <name> Do not strip symbol <name>\n\
--keep-file-symbols Do not strip file symbol(s)\n\
--localize-hidden Turn all ELF hidden symbols into locals\n\
-L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
--globalize-symbol <name> Force symbol <name> to be marked as a global\n\
-G --keep-global-symbol <name> Localize all symbols except <name>\n\
-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\
-w --wildcard Permit wildcard in symbol comparison\n\
-x --discard-all Remove all non-global symbols\n\
-X --discard-locals Remove any compiler-generated symbols\n\
-i --interleave[=<number>] Only copy N out of every <number> bytes\n\
--interleave-width <number> Set N for --interleave\n\
-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\
--add-section <name>=<file> Add section <name> found in <file> to output\n\
--update-section <name>=<file>\n\
Update contents of section <name> with\n\
contents found in <file>\n\
--dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
--rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
--long-section-names {enable|disable|keep}\n\
Handle long section names in Coff objects.\n\
--change-leading-char Force output format's leading character style\n\
--remove-leading-char Remove leading character from global symbols\n\
--reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
--redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
--redefine-syms <file> --redefine-sym for all symbol pairs \n\
listed in <file>\n\
--srec-len <number> Restrict the length of generated Srecords\n\
--srec-forceS3 Restrict the type of generated Srecords to S3\n\
--strip-symbols <file> -N for all symbols listed in <file>\n\
--strip-unneeded-symbols <file>\n\
--strip-unneeded-symbol for all symbols listed\n\
in <file>\n\
--keep-symbols <file> -K for all symbols listed in <file>\n\
--localize-symbols <file> -L for all symbols listed in <file>\n\
--globalize-symbols <file> --globalize-symbol for all in <file>\n\
--keep-global-symbols <file> -G for all symbols listed in <file>\n\
--weaken-symbols <file> -W for all symbols listed in <file>\n\
--add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
--alt-machine-code <index> Use the target's <index>'th alternative machine\n\
--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\
--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\
--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\
Set PE subsystem to <name> [& <version>]\n\
--compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
Compress DWARF debug sections using zlib\n\
--decompress-debug-sections Decompress DWARF debug sections using zlib\n\
--elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
type\n\
-M --merge-notes Remove redundant entries in note sections\n\
--no-merge-notes Do not attempt to remove redundant notes (default)\n\
-v --verbose List all object files modified\n\
@<file> Read options from <file>\n\
-V --version Display this program's version number\n\
-h --help Display this output\n\
--info List object formats & architectures supported\n\
"));
list_supported_targets (program_name, stream);
if (REPORT_BUGS_TO[0] && exit_status == 0)
fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
exit (exit_status);
}
ATTRIBUTE_NORETURN static void
strip_usage (FILE *stream, int exit_status)
{
fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
fprintf (stream, _(" Removes symbols and sections from files\n"));
fprintf (stream, _(" The options are:\n"));
fprintf (stream, _("\
-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\
-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, _("\
-D --enable-deterministic-archives\n\
Produce deterministic output when stripping archives\n\
-U --disable-deterministic-archives\n\
Disable -D behavior (default)\n"));
fprintf (stream, _("\
-R --remove-section=<name> Also remove section <name> from the output\n\
--remove-relocations <name> Remove relocations from section <name>\n\
-s --strip-all Remove all symbol and relocation information\n\
-g -S -d --strip-debug Remove all debugging symbols & sections\n\
--strip-dwo Remove all DWO sections\n\
--strip-unneeded Remove all symbols not needed by relocations\n\
--only-keep-debug Strip everything but the debug information\n\
-M --merge-notes Remove redundant entries in note sections (default)\n\
--no-merge-notes Do not attempt to remove redundant notes\n\
-N --strip-symbol=<name> Do not copy symbol <name>\n\
-K --keep-symbol=<name> Do not strip symbol <name>\n\
--keep-file-symbols Do not strip file symbol(s)\n\
-w --wildcard Permit wildcard in symbol comparison\n\
-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\
--info List object formats & architectures supported\n\
-o <file> Place stripped output into <file>\n\
"));
list_supported_targets (program_name, stream);
if (REPORT_BUGS_TO[0] && exit_status == 0)
fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
exit (exit_status);
}
/* Parse section flags into a flagword, with a fatal error if the
string can't be parsed. */
static flagword
parse_flags (const char *s)
{
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) ;
#define PARSE_FLAG(fname,fval) \
else if (strncasecmp (fname, s, len) == 0) ret |= fval
PARSE_FLAG ("alloc", SEC_ALLOC);
PARSE_FLAG ("load", SEC_LOAD);
PARSE_FLAG ("noload", SEC_NEVER_LOAD);
PARSE_FLAG ("readonly", SEC_READONLY);
PARSE_FLAG ("debug", SEC_DEBUGGING);
PARSE_FLAG ("code", SEC_CODE);
PARSE_FLAG ("data", SEC_DATA);
PARSE_FLAG ("rom", SEC_ROM);
PARSE_FLAG ("share", SEC_COFF_SHARED);
PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
PARSE_FLAG ("merge", SEC_MERGE);
PARSE_FLAG ("strings", SEC_STRINGS);
#undef PARSE_FLAG
else
{
char *copy;
copy = (char *) xmalloc (len + 1);
strncpy (copy, s, len);
copy[len] = '\0';
non_fatal (_("unrecognized section flag `%s'"), copy);
fatal (_("supported flags: %s"),
"alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
}
s = snext;
}
while (s != NULL);
return ret;
}
/* Parse symbol flags into a flagword, with a fatal error if the
string can't be parsed. */
static flagword
parse_symflags (const char *s, char **other)
{
flagword ret;
const char *snext;
size_t len;
ret = BSF_NO_FLAGS;
do
{
snext = strchr (s, ',');
if (snext == NULL)
len = strlen (s);
else
{
len = snext - s;
++snext;
}
#define PARSE_FLAG(fname, fval) \
else if (len == sizeof fname - 1 \
&& strncasecmp (fname, s, len) == 0) \
ret |= fval
#define PARSE_OTHER(fname, fval) \
else if (len >= sizeof fname \
&& strncasecmp (fname, s, sizeof fname - 1) == 0) \
fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
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"),
"local, global, export, debug, function, weak, section, "
"constructor, warning, indirect, file, object, synthetic, "
"indirect-function, unique-object, before=<othersym>");
}
s = snext;
}
while (s != NULL);
return ret;
}
/* 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:
--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. */
static struct section_list *
find_section_list (const char *name, bfd_boolean add, unsigned int context)
{
struct section_list *p, *match = NULL;
/* assert ((context & ((1 << 7) - 1)) != 0); */
for (p = change_sections; p != NULL; p = p->next)
{
if (add)
{
if (strcmp (p->pattern, name) == 0)
{
/* Check for context conflicts. */
if (((p->context & SECTION_CONTEXT_REMOVE)
&& (context & SECTION_CONTEXT_COPY))
|| ((context & SECTION_CONTEXT_REMOVE)
&& (p->context & SECTION_CONTEXT_COPY)))
fatal (_("error: %s both copied and removed"), name);
if (((p->context & SECTION_CONTEXT_SET_VMA)
&& (context & SECTION_CONTEXT_ALTER_VMA))
|| ((context & SECTION_CONTEXT_SET_VMA)
&& (context & SECTION_CONTEXT_ALTER_VMA)))
fatal (_("error: %s both sets and alters VMA"), name);
if (((p->context & SECTION_CONTEXT_SET_LMA)
&& (context & SECTION_CONTEXT_ALTER_LMA))
|| ((context & SECTION_CONTEXT_SET_LMA)
&& (context & SECTION_CONTEXT_ALTER_LMA)))
fatal (_("error: %s both sets and alters LMA"), name);
/* Extend the context. */
p->context |= context;
return p;
}
}
/* If we are not adding a new name/pattern then
only check for a match if the context applies. */
else if (p->context & context)
{
/* We could check for the presence of wildchar characters
first and choose between calling strcmp and fnmatch,
but is that really worth it ? */
if (p->pattern [0] == '!')
{
if (fnmatch (p->pattern + 1, name, 0) == 0)
{
p->used = TRUE;
return NULL;
}
}
else
{
if (fnmatch (p->pattern, name, 0) == 0)
{
if (match == NULL)
match = p;
}
}
}
}
if (! add)
{
if (match != NULL)
match->used = TRUE;
return match;
}
p = (struct section_list *) xmalloc (sizeof (struct section_list));
p->pattern = name;
p->used = FALSE;
p->context = context;
p->vma_val = 0;
p->lma_val = 0;
p->flags = 0;
p->next = change_sections;
change_sections = p;
return p;
}
/* S1 is the entry node already in the table, S2 is the key node. */
static int
eq_string_redefnode (const void *s1, const void *s2)
{
struct redefine_node *node1 = (struct redefine_node *) s1;
struct redefine_node *node2 = (struct redefine_node *) s2;
return !strcmp ((const char *) node1->source, (const char *) node2->source);
}
/* P is redefine node. Hash value is generated from its "source" filed. */
static hashval_t
htab_hash_redefnode (const void *p)
{
struct redefine_node *redefnode = (struct redefine_node *) p;
return htab_hash_string (redefnode->source);
}
/* Create hashtab used for redefine node. */
static htab_t
create_symbol2redef_htab (void)
{
return htab_create_alloc (16, htab_hash_redefnode, eq_string_redefnode, NULL,
xcalloc, free);
}
/* There is htab_hash_string but no htab_eq_string. Makes sense. */
static int
eq_string (const void *s1, const void *s2)
{
return strcmp ((const char *) s1, (const char *) s2) == 0;
}