aboutsummaryrefslogblamecommitdiff
path: root/elfcopy/main.c
blob: 0ba4e6864c86c20bd0b40a05f6aa1b45903a5285 (plain) (tree)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501









































                                                                             
                                                                 











































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                

                                                              



































































































































































































































































































































































































                                                                               
/*-
 * Copyright (c) 2007-2013 Kai Wang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libelftc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "elfcopy.h"

ELFTC_VCSID("$Id: main.c 3111 2014-12-20 08:33:01Z kaiwang27 $");

enum options
{
	ECP_ADD_GNU_DEBUGLINK,
	ECP_ADD_SECTION,
	ECP_CHANGE_ADDR,
	ECP_CHANGE_SEC_ADDR,
	ECP_CHANGE_SEC_LMA,
	ECP_CHANGE_SEC_VMA,
	ECP_CHANGE_START,
	ECP_CHANGE_WARN,
	ECP_GAP_FILL,
	ECP_GLOBALIZE_SYMBOL,
	ECP_GLOBALIZE_SYMBOLS,
	ECP_KEEP_SYMBOLS,
	ECP_KEEP_GLOBAL_SYMBOLS,
	ECP_LOCALIZE_SYMBOLS,
	ECP_NO_CHANGE_WARN,
	ECP_ONLY_DEBUG,
	ECP_PAD_TO,
	ECP_PREFIX_ALLOC,
	ECP_PREFIX_SEC,
	ECP_PREFIX_SYM,
	ECP_REDEF_SYMBOL,
	ECP_REDEF_SYMBOLS,
	ECP_RENAME_SECTION,
	ECP_SET_OSABI,
	ECP_SET_SEC_FLAGS,
	ECP_SET_START,
	ECP_SREC_FORCE_S3,
	ECP_SREC_LEN,
	ECP_STRIP_SYMBOLS,
	ECP_STRIP_UNNEEDED,
	ECP_WEAKEN_ALL,
	ECP_WEAKEN_SYMBOLS
};

static struct option mcs_longopts[] =
{
	{ "help", no_argument, NULL, 'h' },
	{ "version", no_argument, NULL, 'V' },
	{ NULL, 0, NULL, 0 }
};

static struct option strip_longopts[] =
{
	{"discard-all", no_argument, NULL, 'x'},
	{"discard-locals", no_argument, NULL, 'X'},
	{"help", no_argument, NULL, 'h'},
	{"input-target", required_argument, NULL, 'I'},
	{"keep-symbol", required_argument, NULL, 'K'},
	{"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG},
	{"output-file", required_argument, NULL, 'o'},
	{"output-target", required_argument, NULL, 'O'},
	{"preserve-dates", no_argument, NULL, 'p'},
	{"remove-section", required_argument, NULL, 'R'},
	{"strip-all", no_argument, NULL, 's'},
	{"strip-debug", no_argument, NULL, 'S'},
	{"strip-symbol", required_argument, NULL, 'N'},
	{"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED},
	{"version", no_argument, NULL, 'V'},
	{"wildcard", no_argument, NULL, 'w'},
	{NULL, 0, NULL, 0}
};

static struct option elfcopy_longopts[] =
{
	{"add-gnu-debuglink", required_argument, NULL, ECP_ADD_GNU_DEBUGLINK},
	{"add-section", required_argument, NULL, ECP_ADD_SECTION},
	{"adjust-section-vma", required_argument, NULL, ECP_CHANGE_SEC_ADDR},
	{"adjust-vma", required_argument, NULL, ECP_CHANGE_ADDR},
	{"adjust-start", required_argument, NULL, ECP_CHANGE_START},
	{"adjust-warnings", no_argument, NULL, ECP_CHANGE_WARN},
	{"binary-architecture", required_argument, NULL, 'B'},
	{"change-addresses", required_argument, NULL, ECP_CHANGE_ADDR},
	{"change-section-address", required_argument, NULL,
	 ECP_CHANGE_SEC_ADDR},
	{"change-section-lma", required_argument, NULL, ECP_CHANGE_SEC_LMA},
	{"change-section-vma", required_argument, NULL, ECP_CHANGE_SEC_VMA},
	{"change-start", required_argument, NULL, ECP_CHANGE_START},
	{"change-warnings", no_argument, NULL, ECP_CHANGE_WARN},
	{"discard-all", no_argument, NULL, 'x'},
	{"discard-locals", no_argument, NULL, 'X'},
	{"gap-fill", required_argument, NULL, ECP_GAP_FILL},
	{"globalize-symbol", required_argument, NULL, ECP_GLOBALIZE_SYMBOL},
	{"globalize-symbols", required_argument, NULL, ECP_GLOBALIZE_SYMBOLS},
	{"help", no_argument, NULL, 'h'},
	{"input-target", required_argument, NULL, 'I'},
	{"keep-symbol", required_argument, NULL, 'K'},
	{"keep-symbols", required_argument, NULL, ECP_KEEP_SYMBOLS},
	{"keep-global-symbol", required_argument, NULL, 'G'},
	{"keep-global-symbols", required_argument, NULL,
	 ECP_KEEP_GLOBAL_SYMBOLS},
	{"localize-symbol", required_argument, NULL, 'L'},
	{"localize-symbols", required_argument, NULL, ECP_LOCALIZE_SYMBOLS},
	{"no-adjust-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN},
	{"no-change-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN},
	{"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG},
	{"only-section", required_argument, NULL, 'j'},
	{"osabi", required_argument, NULL, ECP_SET_OSABI},
	{"output-target", required_argument, NULL, 'O'},
	{"pad-to", required_argument, NULL, ECP_PAD_TO},
	{"preserve-dates", no_argument, NULL, 'p'},
	{"prefix-alloc-sections", required_argument, NULL, ECP_PREFIX_ALLOC},
	{"prefix-sections", required_argument, NULL, ECP_PREFIX_SEC},
	{"prefix-symbols", required_argument, NULL, ECP_PREFIX_SYM},
	{"redefine-sym", required_argument, NULL, ECP_REDEF_SYMBOL},
	{"redefine-syms", required_argument, NULL, ECP_REDEF_SYMBOLS},
	{"remove-section", required_argument, NULL, 'R'},
	{"rename-section", required_argument, NULL, ECP_RENAME_SECTION},
	{"set-section-flags", required_argument, NULL, ECP_SET_SEC_FLAGS},
	{"set-start", required_argument, NULL, ECP_SET_START},
	{"srec-forceS3", no_argument, NULL, ECP_SREC_FORCE_S3},
	{"srec-len", required_argument, NULL, ECP_SREC_LEN},
	{"strip-all", no_argument, NULL, 'S'},
	{"strip-debug", no_argument, 0, 'g'},
	{"strip-symbol", required_argument, NULL, 'N'},
	{"strip-symbols", required_argument, NULL, ECP_STRIP_SYMBOLS},
	{"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED},
	{"version", no_argument, NULL, 'V'},
	{"weaken", no_argument, NULL, ECP_WEAKEN_ALL},
	{"weaken-symbol", required_argument, NULL, 'W'},
	{"weaken-symbols", required_argument, NULL, ECP_WEAKEN_SYMBOLS},
	{"wildcard", no_argument, NULL, 'w'},
	{NULL, 0, NULL, 0}
};

static struct {
	const char *name;
	int value;
} sec_flags[] = {
	{"alloc", SF_ALLOC},
	{"load", SF_LOAD},
	{"noload", SF_NOLOAD},
	{"readonly", SF_READONLY},
	{"debug", SF_DEBUG},
	{"code", SF_CODE},
	{"data", SF_DATA},
	{"rom", SF_ROM},
	{"share", SF_SHARED},
	{"contents", SF_CONTENTS},
	{NULL, 0}
};

static struct {
	const char *name;
	int abi;
} osabis[] = {
	{"sysv", ELFOSABI_SYSV},
	{"hpus", ELFOSABI_HPUX},
	{"netbsd", ELFOSABI_NETBSD},
	{"linux", ELFOSABI_LINUX},
	{"hurd", ELFOSABI_HURD},
	{"86open", ELFOSABI_86OPEN},
	{"solaris", ELFOSABI_SOLARIS},
	{"aix", ELFOSABI_AIX},
	{"irix", ELFOSABI_IRIX},
	{"freebsd", ELFOSABI_FREEBSD},
	{"tru64", ELFOSABI_TRU64},
	{"modesto", ELFOSABI_MODESTO},
	{"openbsd", ELFOSABI_OPENBSD},
	{"openvms", ELFOSABI_OPENVMS},
	{"nsk", ELFOSABI_NSK},
	{"arm", ELFOSABI_ARM},
	{"standalone", ELFOSABI_STANDALONE},
	{NULL, 0}
};

static int	copy_from_tempfile(const char *src, const char *dst,
    int infd, int *outfd);
static void	create_file(struct elfcopy *ecp, const char *src,
    const char *dst);
static void	elfcopy_main(struct elfcopy *ecp, int argc, char **argv);
static void	elfcopy_usage(void);
static void	mcs_main(struct elfcopy *ecp, int argc, char **argv);
static void	mcs_usage(void);
static void	parse_sec_address_op(struct elfcopy *ecp, int optnum,
    const char *optname, char *s);
static void	parse_sec_flags(struct sec_action *sac, char *s);
static void	parse_symlist_file(struct elfcopy *ecp, const char *fn,
    unsigned int op);
static void	print_version(void);
static void	set_input_target(struct elfcopy *ecp, const char *target_name);
static void	set_osabi(struct elfcopy *ecp, const char *abi);
static void	set_output_target(struct elfcopy *ecp, const char *target_name);
static void	strip_main(struct elfcopy *ecp, int argc, char **argv);
static void	strip_usage(void);

/*
 * An ELF object usually has a sturcture described by the
 * diagram below.
 *  _____________
 * |             |
 * |     NULL    | <- always a SHT_NULL section
 * |_____________|
 * |             |
 * |   .interp   |
 * |_____________|
 * |             |
 * |     ...     |
 * |_____________|
 * |             |
 * |    .text    |
 * |_____________|
 * |             |
 * |     ...     |
 * |_____________|
 * |             |
 * |  .comment   | <- above(include) this: normal sections
 * |_____________|
 * |             |
 * | add sections| <- unloadable sections added by --add-section
 * |_____________|
 * |             |
 * |  .shstrtab  | <- section name string table
 * |_____________|
 * |             |
 * |    shdrs    | <- section header table
 * |_____________|
 * |             |
 * |   .symtab   | <- symbol table, if any
 * |_____________|
 * |             |
 * |   .strtab   | <- symbol name string table, if any
 * |_____________|
 * |             |
 * |  .rel.text  | <- relocation info for .o files.
 * |_____________|
 */
void
create_elf(struct elfcopy *ecp)
{
	struct section	*shtab;
	GElf_Ehdr	 ieh;
	GElf_Ehdr	 oeh;
	size_t		 ishnum;

	ecp->flags |= SYMTAB_INTACT;

	/* Create EHDR. */
	if (gelf_getehdr(ecp->ein, &ieh) == NULL)
		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
		    elf_errmsg(-1));
	if ((ecp->iec = gelf_getclass(ecp->ein)) == ELFCLASSNONE)
		errx(EXIT_FAILURE, "getclass() failed: %s",
		    elf_errmsg(-1));

	if (ecp->oec == ELFCLASSNONE)
		ecp->oec = ecp->iec;
	if (ecp->oed == ELFDATANONE)
		ecp->oed = ieh.e_ident[EI_DATA];

	if (gelf_newehdr(ecp->eout, ecp->oec) == NULL)
		errx(EXIT_FAILURE, "gelf_newehdr failed: %s",
		    elf_errmsg(-1));
	if (gelf_getehdr(ecp->eout, &oeh) == NULL)
		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
		    elf_errmsg(-1));

	memcpy(oeh.e_ident, ieh.e_ident, sizeof(ieh.e_ident));
	oeh.e_ident[EI_CLASS] = ecp->oec;
	oeh.e_ident[EI_DATA]  = ecp->oed;
	if (ecp->abi != -1)
		oeh.e_ident[EI_OSABI] = ecp->abi;
	oeh.e_flags	      = ieh.e_flags;
	oeh.e_machine	      = ieh.e_machine;
	oeh.e_type	      = ieh.e_type;
	oeh.e_entry	      = ieh.e_entry;
	oeh.e_version	      = ieh.e_version;

	if (ieh.e_type == ET_EXEC)
		ecp->flags |= EXECUTABLE;
	else if (ieh.e_type == ET_DYN)
		ecp->flags |= DYNAMIC;
	else if (ieh.e_type == ET_REL)
		ecp->flags |= RELOCATABLE;
	else
		errx(EXIT_FAILURE, "unsupported e_type");

	if (!elf_getshnum(ecp->ein, &ishnum))
		errx(EXIT_FAILURE, "elf_getshnum failed: %s",
		    elf_errmsg(-1));
	if (ishnum > 0 && (ecp->secndx = calloc(ishnum,
	    sizeof(*ecp->secndx))) == NULL)
		err(EXIT_FAILURE, "calloc failed");

	/* Read input object program header. */
	setup_phdr(ecp);

	/*
	 * Scan of input sections: we iterate through sections from input
	 * object, skip sections need to be stripped, allot Elf_Scn and
	 * create internal section structure for sections we want.
	 * (i.e., determine output sections)
	 */
	create_scn(ecp);

	/* Apply section address changes, if any. */
	adjust_addr(ecp);

	/*
	 * Determine if the symbol table needs to be changed based on
	 * command line options.
	 */
	if (ecp->strip == STRIP_DEBUG ||
	    ecp->strip == STRIP_UNNEEDED ||
	    ecp->flags & WEAKEN_ALL ||
	    ecp->flags & DISCARD_LOCAL ||
	    ecp->flags & DISCARD_LLABEL ||
	    ecp->prefix_sym != NULL ||
	    !STAILQ_EMPTY(&ecp->v_symop))
		ecp->flags &= ~SYMTAB_INTACT;

	/*
	 * Create symbol table. Symbols are filtered or stripped according to
	 * command line args specified by user, and later updated for the new
	 * layout of sections in the output object.
	 */
	if ((ecp->flags & SYMTAB_EXIST) != 0)
		create_symtab(ecp);

	/*
	 * First processing of output sections: at this stage we copy the
	 * content of each section from input to output object.  Section
	 * content will be modified and printed (mcs) if need. Also content of
	 * relocation section probably will be filtered and updated according
	 * to symbol table changes.
	 */
	copy_content(ecp);

	/*
	 * Write the underlying ehdr. Note that it should be called
	 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
	 */
	if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
		errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
		    elf_errmsg(-1));

	/* Generate section name string table (.shstrtab). */
	set_shstrtab(ecp);

	/*
	 * Second processing of output sections: Update section headers.
	 * At this stage we set name string index, update st_link and st_info
	 * for output sections.
	 */
	update_shdr(ecp, 1);

	/* Renew oeh to get the updated e_shstrndx. */
	if (gelf_getehdr(ecp->eout, &oeh) == NULL)
		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
		    elf_errmsg(-1));

	/*
	 * Insert SHDR table into the internal section list as a "pseudo"
	 * section, so later it will get sorted and resynced just as "normal"
	 * sections.
	 */
	shtab = insert_shtab(ecp, 0);

	/*
	 * Resync section offsets in the output object. This is needed
	 * because probably sections are modified or new sections are added,
	 * as a result overlap/gap might appears.
	 */
	resync_sections(ecp);

	/* Store SHDR offset in EHDR. */
	oeh.e_shoff = shtab->off;

	/* Put program header table immediately after the Elf header. */
	if (ecp->ophnum > 0) {
		oeh.e_phoff = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
		if (oeh.e_phoff == 0)
			errx(EXIT_FAILURE, "gelf_fsize() failed: %s",
			    elf_errmsg(-1));
	}

	/*
	 * Update ELF object entry point if requested.
	 */
	if (ecp->change_addr != 0)
		oeh.e_entry += ecp->change_addr;
	if (ecp->flags & SET_START)
		oeh.e_entry = ecp->set_start;
	if (ecp->change_start != 0)
		oeh.e_entry += ecp->change_start;

	/*
	 * Update ehdr again before we call elf_update(), since we
	 * modified e_shoff and e_phoff.
	 */
	if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
		errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
		    elf_errmsg(-1));

	if (ecp->ophnum > 0)
		copy_phdr(ecp);

	/* Write out the output elf object. */
	if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
		errx(EXIT_FAILURE, "elf_update() failed: %s",
		    elf_errmsg(-1));

	/* Release allocated resource. */
	free_elf(ecp);
}

void
free_elf(struct elfcopy *ecp)
{
	struct segment	*seg, *seg_temp;
	struct section	*sec, *sec_temp;

	/* Free internal segment list. */
	if (!STAILQ_EMPTY(&ecp->v_seg)) {
		STAILQ_FOREACH_SAFE(seg, &ecp->v_seg, seg_list, seg_temp) {
			STAILQ_REMOVE(&ecp->v_seg, seg, segment, seg_list);
			free(seg);
		}
	}

	/* Free symbol table buffers. */
	free_symtab(ecp);

	/* Free internal section list. */
	if (!TAILQ_EMPTY(&ecp->v_sec)) {
		TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) {
			TAILQ_REMOVE(&ecp->v_sec, sec, sec_list);
			if (sec->buf != NULL)
				free(sec->buf);
			if (sec->newname != NULL)
				free(sec->newname);
			if (sec->pad != NULL)
				free(sec->pad);
			free(sec);
		}
	}
}

/* Create a temporary file. */
void
create_tempfile(char **fn, int *fd)
{
	const char	*tmpdir;
	char		*cp, *tmpf;
	size_t		 tlen, plen;

#define	_TEMPFILE "ecp.XXXXXXXX"
#define	_TEMPFILEPATH "/tmp/ecp.XXXXXXXX"

	if (fn == NULL || fd == NULL)
		return;
	/* Repect TMPDIR environment variable. */
	tmpdir = getenv("TMPDIR");
	if (tmpdir != NULL && *tmpdir != '\0') {
		tlen = strlen(tmpdir);
		plen = strlen(_TEMPFILE);
		tmpf = malloc(tlen + plen + 2);
		if (tmpf == NULL)
			err(EXIT_FAILURE, "malloc failed");
		strncpy(tmpf, tmpdir, tlen);
		cp = &tmpf[tlen - 1];
		if (*cp++ != '/')
			*cp++ = '/';
		strncpy(cp, _TEMPFILE, plen);
		cp[plen] = '\0';
	} else {
		tmpf = strdup(_TEMPFILEPATH);
		if (tmpf == NULL)
			err(EXIT_FAILURE, "strdup failed");
	}
	if ((*fd = mkstemp(tmpf)) == -1)
		err(EXIT_FAILURE, "mkstemp %s failed", tmpf);
	if (fchmod(*fd, 0644) == -1)
		err(EXIT_FAILURE, "fchmod %s failed", tmpf);
	*fn = tmpf;

#undef _TEMPFILE
#undef _TEMPFILEPATH
}

static int
copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd)
{
	int tmpfd;

	/*
	 * First, check if we can use rename().
	 */
	if (rename(src, dst) >= 0) {
		*outfd = infd;
		return (0);
	} else if (errno != EXDEV)
		return (-1);

	/*
	 * If the rename() failed due to 'src' and 'dst' residing in
	 * two different file systems, invoke a helper function in
	 * libelftc to do the copy.
	 */

	if (unlink(dst) < 0)
		return (-1);

	if ((tmpfd = open(dst, O_CREAT | O_WRONLY, 0755)) < 0)
		return (-1);

	if (lseek(infd, 0, SEEK_SET) < 0)
		return (-1);

	if (elftc_copyfile(infd, tmpfd) < 0)
		return (-1);

	/*
	 * Remove the temporary file from the file system
	 * namespace, and close its file descriptor.
	 */
	if (unlink(src) < 0)
		return (-1);

	(void) close(infd);

	/*
	 * Return the file descriptor for the destination.
	 */
	*outfd = tmpfd;

	return (0);
}

static void
create_file(struct elfcopy *ecp, const char *src, const char *dst)
{
	struct stat	 sb;
	char		*tempfile, *elftemp;
	int		 efd, ifd, ofd, ofd0, tfd;

	tempfile = NULL;

	if (src == NULL)
		errx(EXIT_FAILURE, "internal: src == NULL");
	if ((ifd = open(src, O_RDONLY)) == -1)
		err(EXIT_FAILURE, "open %s failed", src);

	if (fstat(ifd, &sb) == -1)
		err(EXIT_FAILURE, "fstat %s failed", src);

	if (dst == NULL)
		create_tempfile(&tempfile, &ofd);
	else
		if ((ofd = open(dst, O_RDWR|O_CREAT, 0755)) == -1)
			err(EXIT_FAILURE, "open %s failed", dst);

#ifndef LIBELF_AR
	/* Detect and process ar(1) archive using libarchive. */
	if (ac_detect_ar(ifd)) {
		ac_create_ar(ecp, ifd, ofd);
		goto copy_done;
	}
#endif

	if (lseek(ifd, 0, SEEK_SET) < 0)
		err(EXIT_FAILURE, "lseek failed");

	/*
	 * If input object is not ELF file, convert it to an intermediate
	 * ELF object before processing.
	 */
	if (ecp->itf != ETF_ELF) {
		create_tempfile(&elftemp, &efd);
		if ((ecp->eout = elf_begin(efd, ELF_C_WRITE, NULL)) == NULL)
			errx(EXIT_FAILURE, "elf_begin() failed: %s",
			    elf_errmsg(-1));
		elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);
		if (ecp->itf == ETF_BINARY)
			create_elf_from_binary(ecp, ifd, src);
		else if (ecp->itf == ETF_IHEX)
			create_elf_from_ihex(ecp, ifd);
		else if (ecp->itf == ETF_SREC)
			create_elf_from_srec(ecp, ifd);
		else
			errx(EXIT_FAILURE, "Internal: invalid target flavour");
		elf_end(ecp->eout);

		/* Open intermediate ELF object as new input object. */
		close(ifd);
		if ((ifd = open(elftemp, O_RDONLY)) == -1)
			err(EXIT_FAILURE, "open %s failed", src);
		close(efd);
		free(elftemp);
	}

	if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
		errx(EXIT_FAILURE, "elf_begin() failed: %s",
		    elf_errmsg(-1));

	switch (elf_kind(ecp->ein)) {
	case ELF_K_NONE:
		errx(EXIT_FAILURE, "file format not recognized");
	case ELF_K_ELF:
		if ((ecp->eout = elf_begin(ofd, ELF_C_WRITE, NULL)) == NULL)
			errx(EXIT_FAILURE, "elf_begin() failed: %s",
			    elf_errmsg(-1));

		/* elfcopy(1) manage ELF layout by itself. */
		elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);

		/*
		 * Create output ELF object.
		 */
		create_elf(ecp);
		elf_end(ecp->eout);

		/*
		 * Convert the output ELF object to binary/srec/ihex if need.
		 */
		if (ecp->otf != ETF_ELF) {
			/*
			 * Create (another) tempfile for binary/srec/ihex
			 * output object.
			 */
			if (tempfile != NULL) {
				if (unlink(tempfile) < 0)
					err(EXIT_FAILURE, "unlink %s failed",
					    tempfile);
				free(tempfile);
			}
			create_tempfile(&tempfile, &ofd0);


			/*
			 * Rewind the file descriptor being processed.
			 */
			if (lseek(ofd, 0, SEEK_SET) < 0)
				err(EXIT_FAILURE,
				    "lseek failed for the output object");

			/*
			 * Call flavour-specific conversion routine.
			 */
			switch (ecp->otf) {
			case ETF_BINARY:
				create_binary(ofd, ofd0);
				break;
			case ETF_IHEX:
				create_ihex(ofd, ofd0);
				break;
			case ETF_SREC:
				create_srec(ecp, ofd, ofd0,
				    dst != NULL ? dst : src);
				break;
			default:
				errx(EXIT_FAILURE, "Internal: unsupported"
				    " output flavour %d", ecp->oec);
			}

			close(ofd);
			ofd = ofd0;
		}

		break;

	case ELF_K_AR:
		/* XXX: Not yet supported. */
		break;
	default:
		errx(EXIT_FAILURE, "file format not supported");
	}

	elf_end(ecp->ein);

#ifndef LIBELF_AR
copy_done:
#endif

	if (tempfile != NULL) {
		if (dst == NULL)
			dst = src;

		if (copy_from_tempfile(tempfile, dst, ofd, &tfd) < 0)
			err(EXIT_FAILURE, "creation of %s failed", dst);

		free(tempfile);
		tempfile = NULL;

		ofd = tfd;
	}

	if (strcmp(dst, "/dev/null") && fchmod(ofd, sb.st_mode) == -1)
		err(EXIT_FAILURE, "fchmod %s failed", dst);

	if ((ecp->flags & PRESERVE_DATE) &&
	    elftc_set_timestamps(dst, &sb) < 0)
		err(EXIT_FAILURE, "setting timestamps failed");

	close(ifd);
	close(ofd);
}

static void
elfcopy_main(struct elfcopy *ecp, int argc, char **argv)
{
	struct sec_action	*sac;
	const char		*infile, *outfile;
	char			*fn, *s;
	int			 opt;

	while ((opt = getopt_long(argc, argv, "dB:gG:I:j:K:L:N:O:pR:s:SwW:xXV",
	    elfcopy_longopts, NULL)) != -1) {
		switch(opt) {
		case 'B':
			/* ignored */
			break;
		case 'R':
			sac = lookup_sec_act(ecp, optarg, 1);
			if (sac->copy != 0)
				errx(EXIT_FAILURE,
				    "both copy and remove specified");
			sac->remove = 1;
			ecp->flags |= SEC_REMOVE;
			break;
		case 'S':
			ecp->strip = STRIP_ALL;
			break;
		case 'g':
			ecp->strip = STRIP_DEBUG;
			break;
		case 'G':
			ecp->flags |= KEEP_GLOBAL;
			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEPG);
			break;
		case 'I':
		case 's':
			set_input_target(ecp, optarg);
			break;
		case 'j':
			sac = lookup_sec_act(ecp, optarg, 1);
			if (sac->remove != 0)
				errx(EXIT_FAILURE,
				    "both copy and remove specified");
			sac->copy = 1;
			ecp->flags |= SEC_COPY;
			break;
		case 'K':
			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
			break;
		case 'L':
			add_to_symop_list(ecp, optarg, NULL, SYMOP_LOCALIZE);
			break;
		case 'N':
			add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
			break;
		case 'O':
			set_output_target(ecp, optarg);
			break;
		case 'p':
			ecp->flags |= PRESERVE_DATE;
			break;
		case 'V':
			print_version();
			break;
		case 'w':
			ecp->flags |= WILDCARD;
			break;
		case 'W':
			add_to_symop_list(ecp, optarg, NULL, SYMOP_WEAKEN);
			break;
		case 'x':
			ecp->flags |= DISCARD_LOCAL;
			break;
		case 'X':
			ecp->flags |= DISCARD_LLABEL;
			break;
		case ECP_ADD_GNU_DEBUGLINK:
			ecp->debuglink = optarg;
			break;
		case ECP_ADD_SECTION:
			add_section(ecp, optarg);
			break;
		case ECP_CHANGE_ADDR:
			ecp->change_addr = (int64_t) strtoll(optarg, NULL, 0);
			break;
		case ECP_CHANGE_SEC_ADDR:
			parse_sec_address_op(ecp, opt, "--change-section-addr",
			    optarg);
			break;
		case ECP_CHANGE_SEC_LMA:
			parse_sec_address_op(ecp, opt, "--change-section-lma",
			    optarg);
			break;
		case ECP_CHANGE_SEC_VMA:
			parse_sec_address_op(ecp, opt, "--change-section-vma",
			    optarg);
			break;
		case ECP_CHANGE_START:
			ecp->change_start = (int64_t) strtoll(optarg, NULL, 0);
			break;
		case ECP_CHANGE_WARN:
			/* default */
			break;
		case ECP_GAP_FILL:
			ecp->fill = (uint8_t) strtoul(optarg, NULL, 0);
			ecp->flags |= GAP_FILL;
			break;
		case ECP_GLOBALIZE_SYMBOL:
			add_to_symop_list(ecp, optarg, NULL, SYMOP_GLOBALIZE);
			break;
		case ECP_GLOBALIZE_SYMBOLS:
			parse_symlist_file(ecp, optarg, SYMOP_GLOBALIZE);
			break;
		case ECP_KEEP_SYMBOLS:
			parse_symlist_file(ecp, optarg, SYMOP_KEEP);
			break;
		case ECP_KEEP_GLOBAL_SYMBOLS:
			parse_symlist_file(ecp, optarg, SYMOP_KEEPG);
			break;
		case ECP_LOCALIZE_SYMBOLS:
			parse_symlist_file(ecp, optarg, SYMOP_LOCALIZE);
			break;
		case ECP_NO_CHANGE_WARN:
			ecp->flags |= NO_CHANGE_WARN;
			break;
		case ECP_ONLY_DEBUG:
			ecp->strip = STRIP_NONDEBUG;
			break;
		case ECP_PAD_TO:
			ecp->pad_to = (uint64_t) strtoull(optarg, NULL, 0);
			break;
		case ECP_PREFIX_ALLOC:
			ecp->prefix_alloc = optarg;
			break;
		case ECP_PREFIX_SEC:
			ecp->prefix_sec = optarg;
			break;
		case ECP_PREFIX_SYM:
			ecp->prefix_sym = optarg;
			break;
		case ECP_REDEF_SYMBOL:
			if ((s = strchr(optarg, '=')) == NULL)
				errx(EXIT_FAILURE,
				    "illegal format for --redefine-sym");
			*s++ = '\0';
			add_to_symop_list(ecp, optarg, s, SYMOP_REDEF);
			break;
		case ECP_REDEF_SYMBOLS:
			parse_symlist_file(ecp, optarg, SYMOP_REDEF);
			break;
		case ECP_RENAME_SECTION:
			if ((fn = strchr(optarg, '=')) == NULL)
				errx(EXIT_FAILURE,
				    "illegal format for --rename-section");
			*fn++ = '\0';

			/* Check for optional flags. */
			if ((s = strchr(fn, ',')) != NULL)
				*s++ = '\0';

			sac = lookup_sec_act(ecp, optarg, 1);
			sac->rename = 1;
			sac->newname = fn;
			if (s != NULL)
				parse_sec_flags(sac, s);
			break;
		case ECP_SET_OSABI:
			set_osabi(ecp, optarg);
			break;
		case ECP_SET_SEC_FLAGS:
			if ((s = strchr(optarg, '=')) == NULL)
				errx(EXIT_FAILURE,
				    "illegal format for --set-section-flags");
			*s++ = '\0';
			sac = lookup_sec_act(ecp, optarg, 1);
			parse_sec_flags(sac, s);
			break;
		case ECP_SET_START:
			ecp->flags |= SET_START;
			ecp->set_start = (uint64_t) strtoull(optarg, NULL, 0);
			break;
		case ECP_SREC_FORCE_S3:
			ecp->flags |= SREC_FORCE_S3;
			break;
		case ECP_SREC_LEN:
			ecp->flags |= SREC_FORCE_LEN;
			ecp->srec_len = strtoul(optarg, NULL, 0);
			break;
		case ECP_STRIP_SYMBOLS:
			parse_symlist_file(ecp, optarg, SYMOP_STRIP);
			break;
		case ECP_STRIP_UNNEEDED:
			ecp->strip = STRIP_UNNEEDED;
			break;
		case ECP_WEAKEN_ALL:
			ecp->flags |= WEAKEN_ALL;
			break;
		case ECP_WEAKEN_SYMBOLS:
			parse_symlist_file(ecp, optarg, SYMOP_WEAKEN);
			break;
		default:
			elfcopy_usage();
		}
	}

	if (optind == argc || optind + 2 < argc)
		elfcopy_usage();

	infile = argv[optind];
	outfile = NULL;
	if (optind + 1 < argc)
		outfile = argv[optind + 1];

	create_file(ecp, infile, outfile);
}

static void
mcs_main(struct elfcopy *ecp, int argc, char **argv)
{
	struct sec_action	*sac;
	const char		*string;
	int			 append, delete, compress, name, print;
	int			 opt, i;

	append = delete = compress = name = print = 0;
	string = NULL;
	while ((opt = getopt_long(argc, argv, "a:cdhn:pV", mcs_longopts,
		NULL)) != -1) {
		switch(opt) {
		case 'a':
			append = 1;
			string = optarg; /* XXX multiple -a not supported */
			break;
		case 'c':
			compress = 1;
			break;
		case 'd':
			delete = 1;
			break;
		case 'n':
			name = 1;
			(void)lookup_sec_act(ecp, optarg, 1);
			break;
		case 'p':
			print = 1;
			break;
		case 'V':
			print_version();
			break;
		case 'h':
		default:
			mcs_usage();
		}
	}

	if (optind == argc)
		mcs_usage();

	/* Must specify one operation at least. */
	if (!append && !compress && !delete && !print)
		mcs_usage();

	/*
	 * If we are going to delete, ignore other operations. This is
	 * different from the Solaris implementation, which can print
	 * and delete a section at the same time, for example. Also, this
	 * implementation do not respect the order between operations that
	 * user specified, i.e., "mcs -pc a.out" equals to "mcs -cp a.out".
	 */
	if (delete) {
		append = compress = print = 0;
		ecp->flags |= SEC_REMOVE;
	}
	if (append)
		ecp->flags |= SEC_APPEND;
	if (compress)
		ecp->flags |= SEC_COMPRESS;
	if (print)
		ecp->flags |= SEC_PRINT;

	/* .comment is the default section to operate on. */
	if (!name)
		(void)lookup_sec_act(ecp, ".comment", 1);

	STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) {
		sac->append = append;
		sac->compress = compress;
		sac->print = print;
		sac->remove = delete;
		sac->string = string;
	}

	for (i = optind; i < argc; i++) {
		/* If only -p is specified, output to /dev/null */
		if (print && !append && !compress && !delete)
			create_file(ecp, argv[i], "/dev/null");
		else
			create_file(ecp, argv[i], NULL);
	}
}

static void
strip_main(struct elfcopy *ecp, int argc, char **argv)
{
	struct sec_action	*sac;
	const char		*outfile;
	int			 opt;
	int			 i;

	outfile = NULL;
	while ((opt = getopt_long(argc, argv, "hI:K:N:o:O:pR:sSdgVxXw",
	    strip_longopts, NULL)) != -1) {
		switch(opt) {
		case 'R':
			sac = lookup_sec_act(ecp, optarg, 1);
			sac->remove = 1;
			ecp->flags |= SEC_REMOVE;
			break;
		case 's':
			ecp->strip = STRIP_ALL;
			break;
		case 'S':
		case 'g':
		case 'd':
			ecp->strip = STRIP_DEBUG;
			break;
		case 'I':
			/* ignored */
			break;
		case 'K':
			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
			break;
		case 'N':
			add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
			break;
		case 'o':
			outfile = optarg;
			break;
		case 'O':
			set_output_target(ecp, optarg);
			break;
		case 'p':
			ecp->flags |= PRESERVE_DATE;
			break;
		case 'V':
			print_version();
			break;
		case 'w':
			ecp->flags |= WILDCARD;
			break;
		case 'x':
			ecp->flags |= DISCARD_LOCAL;
			break;
		case 'X':
			ecp->flags |= DISCARD_LLABEL;
			break;
		case ECP_ONLY_DEBUG:
			ecp->strip = STRIP_NONDEBUG;
			break;
		case ECP_STRIP_UNNEEDED:
			ecp->strip = STRIP_UNNEEDED;
			break;
		case 'h':
		default:
			strip_usage();
		}
	}

	if (ecp->strip == 0 &&
	    ((ecp->flags & DISCARD_LOCAL) == 0) &&
	    ((ecp->flags & DISCARD_LLABEL) == 0) &&
	    lookup_symop_list(ecp, NULL, SYMOP_STRIP) == NULL)
		ecp->strip = STRIP_ALL;
	if (optind == argc)
		strip_usage();

	for (i = optind; i < argc; i++)
		create_file(ecp, argv[i], outfile);
}

static void
parse_sec_flags(struct sec_action *sac, char *s)
{
	const char	*flag;
	int		 found, i;

	for (flag = strtok(s, ","); flag; flag = strtok(NULL, ",")) {
		found = 0;
		for (i = 0; sec_flags[i].name != NULL; i++)
			if (strcasecmp(sec_flags[i].name, flag) == 0) {
				sac->flags |= sec_flags[i].value;
				found = 1;
				break;
			}
		if (!found)
			errx(EXIT_FAILURE, "unrecognized section flag %s",
			    flag);
	}
}

static void
parse_sec_address_op(struct elfcopy *ecp, int optnum, const char *optname,
    char *s)
{
	struct sec_action	*sac;
	const char		*name;
	char			*v;
	char			 op;

	name = v = s;
	do {
		v++;
	} while (*v != '\0' && *v != '=' && *v != '+' && *v != '-');
	if (*v == '\0' || *(v + 1) == '\0')
		errx(EXIT_FAILURE, "invalid format for %s", optname);
	op = *v;
	*v++ = '\0';
	sac = lookup_sec_act(ecp, name, 1);
	switch (op) {
	case '=':
		if (optnum == ECP_CHANGE_SEC_LMA ||
		    optnum == ECP_CHANGE_SEC_ADDR) {
			sac->setlma = 1;
			sac->lma = (uint64_t) strtoull(v, NULL, 0);
		}
		if (optnum == ECP_CHANGE_SEC_VMA ||
		    optnum == ECP_CHANGE_SEC_ADDR) {
			sac->setvma = 1;
			sac->vma = (uint64_t) strtoull(v, NULL, 0);
		}
		break;
	case '+':
		if (optnum == ECP_CHANGE_SEC_LMA ||
		    optnum == ECP_CHANGE_SEC_ADDR)
			sac->lma_adjust = (int64_t) strtoll(v, NULL, 0);
		if (optnum == ECP_CHANGE_SEC_VMA ||
		    optnum == ECP_CHANGE_SEC_ADDR)
			sac->vma_adjust = (int64_t) strtoll(v, NULL, 0);
		break;
	case '-':
		if (optnum == ECP_CHANGE_SEC_LMA ||
		    optnum == ECP_CHANGE_SEC_ADDR)
			sac->lma_adjust = (int64_t) -strtoll(v, NULL, 0);
		if (optnum == ECP_CHANGE_SEC_VMA ||
		    optnum == ECP_CHANGE_SEC_ADDR)
			sac->vma_adjust = (int64_t) -strtoll(v, NULL, 0);
		break;
	default:
		break;
	}
}

static void
parse_symlist_file(struct elfcopy *ecp, const char *fn, unsigned int op)
{
	struct symfile	*sf;
	struct stat	 sb;
	FILE		*fp;
	char		*data, *p, *line, *end, *e, *n;

	if (stat(fn, &sb) == -1)
		err(EXIT_FAILURE, "stat %s failed", fn);

	/* Check if we already read and processed this file. */
	STAILQ_FOREACH(sf, &ecp->v_symfile, symfile_list) {
		if (sf->dev == sb.st_dev && sf->ino == sb.st_ino)
			goto process_symfile;
	}

	if ((fp = fopen(fn, "r")) == NULL)
		err(EXIT_FAILURE, "can not open %s", fn);
	if ((data = malloc(sb.st_size + 1)) == NULL)
		err(EXIT_FAILURE, "malloc failed");
	if (fread(data, 1, sb.st_size, fp) == 0 || ferror(fp))
		err(EXIT_FAILURE, "fread failed");
	fclose(fp);
	data[sb.st_size] = '\0';

	if ((sf = calloc(1, sizeof(*sf))) == NULL)
		err(EXIT_FAILURE, "malloc failed");
	sf->dev = sb.st_dev;
	sf->ino = sb.st_ino;
	sf->size = sb.st_size + 1;
	sf->data = data;

process_symfile:

	/*
	 * Basically what we do here is to convert EOL to '\0', and remove
	 * leading and trailing whitespaces for each line.
	 */

	end = sf->data + sf->size;
	line = NULL;
	for(p = sf->data; p < end; p++) {
		if ((*p == '\t' || *p == ' ') && line == NULL)
			continue;
		if (*p == '\r' || *p == '\n' || *p == '\0') {
			*p = '\0';
			if (line == NULL)
				continue;

			/* Skip comment. */
			if (*line == '#') {
				line = NULL;
				continue;
			}

			e = p - 1;
			while(e != line && (*e == '\t' || *e == ' '))
				*e-- = '\0';
			if (op != SYMOP_REDEF)
				add_to_symop_list(ecp, line, NULL, op);
			else {
				if (strlen(line) < 3)
					errx(EXIT_FAILURE,
					    "illegal format for"
					    " --redefine-sym");
				for(n = line + 1; n < e; n++) {
					if (*n == ' ' || *n == '\t') {
						while(*n == ' ' || *n == '\t')
							*n++ = '\0';
						break;
					}
				}
				if (n >= e)
					errx(EXIT_FAILURE,
					    "illegal format for"
					    " --redefine-sym");
				add_to_symop_list(ecp, line, n, op);
			}
			line = NULL;
			continue;
		}

		if (line == NULL)
			line = p;
	}
}

static void
set_input_target(struct elfcopy *ecp, const char *target_name)
{
	Elftc_Bfd_Target *tgt;

	if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
		errx(EXIT_FAILURE, "%s: invalid target name", target_name);
	ecp->itf = elftc_bfd_target_flavor(tgt);
}

static void
set_output_target(struct elfcopy *ecp, const char *target_name)
{
	Elftc_Bfd_Target *tgt;

	if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
		errx(EXIT_FAILURE, "%s: invalid target name", target_name);
	ecp->otf = elftc_bfd_target_flavor(tgt);
	if (ecp->otf == ETF_ELF) {
		ecp->oec = elftc_bfd_target_class(tgt);
		ecp->oed = elftc_bfd_target_byteorder(tgt);
		ecp->oem = elftc_bfd_target_machine(tgt);
	}
	ecp->otgt = target_name;
}

static void
set_osabi(struct elfcopy *ecp, const char *abi)
{
	int i, found;

	found = 0;
	for (i = 0; osabis[i].name != NULL; i++)
		if (strcasecmp(osabis[i].name, abi) == 0) {
			ecp->abi = osabis[i].abi;
			found = 1;
			break;
		}
	if (!found)
		errx(EXIT_FAILURE, "unrecognized OSABI %s", abi);
}

#define	ELFCOPY_USAGE_MESSAGE	"\
Usage: %s [options] infile [outfile]\n\
  Transform an ELF object.\n\n\
  Options:\n\
  -d | -g | --strip-debug      Remove debugging information from the output.\n\
  -j SECTION | --only-section=SECTION\n\
                               Copy only the named section to the output.\n\
  -p | --preserve-dates        Preserve access and modification times.\n\
  -w | --wildcard              Use shell-style patterns to name symbols.\n\
  -x | --discard-all           Do not copy non-globals to the output.\n\
  -I FORMAT | --input-target=FORMAT\n\
                               (Accepted but ignored).\n\
  -K SYM | --keep-symbol=SYM   Copy symbol SYM to the output.\n\
  -L SYM | --localize-symbol=SYM\n\
                               Make symbol SYM local to the output file.\n\
  -N SYM | --strip-symbol=SYM  Do not copy symbol SYM to the output.\n\
  -R NAME | --remove-section=NAME\n\
                               Remove the named section.\n\
  -S | --strip-all             Remove all symbol and relocation information\n\
                               from the output.\n\
  -V | --version               Print a version identifier and exit.\n\
  -W SYM | --weaken-symbol=SYM Mark symbol SYM as weak in the output.\n\
  -X | --discard-locals        Do not copy compiler generated symbols to\n\
                               the output.\n\
  --add-section NAME=FILE      Add the contents of FILE to the ELF object as\n\
                               a new section named NAME.\n\
  --adjust-section-vma SECTION{=,+,-}VAL | \\\n\
    --change-section-address SECTION{=,+,-}VAL\n\
                               Set or adjust the VMA and the LMA of the\n\
                               named section by VAL.\n\
  --adjust-start=INCR | --change-start=INCR\n\
                               Add INCR to the start address for the ELF\n\
                               object.\n\
  --adjust-vma=INCR | --change-addresses=INCR\n\
                               Increase the VMA and LMA of all sections by\n\
                               INCR.\n\
  --adjust-warning | --change-warnings\n\
                               Issue warnings for non-existent sections.\n\
  --change-section-lma SECTION{=,+,-}VAL\n\
                               Set or adjust the LMA address of the named\n\
                               section by VAL.\n\
  --change-section-vma SECTION{=,+,-}VAL\n\
                               Set or adjust the VMA address of the named\n\
                               section by VAL.\n\
  --gap-fill=VAL               Fill the gaps between sections with bytes\n\
                               of value VAL.\n\
  --no-adjust-warning| --no-change-warnings\n\
                               Do not issue warnings for non-existent\n\
                               sections.\n\
  --only-keep-debug            Copy only debugging information.\n\
  --output-target=FORMAT       Use the specified format for the output.\n\
  --pad-to=ADDRESS             Pad the output object upto the given address.\n\
  --prefix-alloc-sections=STRING\n\
                               Prefix the section names of all the allocated\n\
                               sections with STRING.\n\
  --prefix-sections=STRING     Prefix the section names of all the sections\n\
                               with STRING.\n\
  --prefix-symbols=STRING      Prefix the symbol names of all the symbols\n\
                               with STRING.\n\
  --rename-section OLDNAME=NEWNAME[,FLAGS]\n\
                               Rename and optionally change section flags.\n\
  --set-section-flags SECTION=FLAGS\n\
                               Set section flags for the named section.\n\
                               Supported flags are: 'alloc', 'code',\n\
                               'contents', 'data', 'debug', 'load',\n\
                               'noload', 'readonly', 'rom', and 'shared'.\n\
  --set-start=ADDRESS          Set the start address of the ELF object.\n\
  --srec-forceS3               Only generate S3 S-Records.\n\
  --srec-len=LEN               Set the maximum length of a S-Record line.\n\
  --strip-unneeded             Do not copy relocation information.\n"

static void
elfcopy_usage(void)
{
	(void) fprintf(stderr, ELFCOPY_USAGE_MESSAGE, ELFTC_GETPROGNAME());
	exit(EXIT_FAILURE);
}

#define	MCS_USAGE_MESSAGE	"\
Usage: %s [options] file...\n\
  Manipulate the comment section in an ELF object.\n\n\
  Options:\n\
  -a STRING        Append 'STRING' to the comment section.\n\
  -c               Remove duplicate entries from the comment section.\n\
  -d               Delete the comment section.\n\
  -h | --help      Print a help message and exit.\n\
  -n NAME          Operate on the ELF section with name 'NAME'.\n\
  -p               Print the contents of the comment section.\n\
  -V | --version   Print a version identifier and exit.\n"

static void
mcs_usage(void)
{
	(void) fprintf(stderr, MCS_USAGE_MESSAGE, ELFTC_GETPROGNAME());
	exit(EXIT_FAILURE);
}

#define	STRIP_USAGE_MESSAGE	"\
Usage: %s [options] file...\n\
  Discard information from ELF objects.\n\n\
  Options:\n\
  -d | -g | -S | --strip-debug    Remove debugging symbols.\n\
  -h | --help                     Print a help message.\n\
  --only-keep-debug               Keep debugging information only.\n\
  -p | --preserve-dates           Preserve access and modification times.\n\
  -s | --strip-all                Remove all symbols.\n\
  --strip-unneeded                Remove symbols not needed for relocation\n\
                                  processing.\n\
  -w | --wildcard                 Use shell-style patterns to name symbols.\n\
  -x | --discard-all              Discard all non-global symbols.\n\
  -I TGT| --input-target=TGT      (Accepted, but ignored).\n\
  -K SYM | --keep-symbol=SYM      Keep symbol 'SYM' in the output.\n\
  -N SYM | --strip-symbol=SYM     Remove symbol 'SYM' from the output.\n\
  -O TGT | --output-target=TGT    Set the output file format to 'TGT'.\n\
  -R SEC | --remove-section=SEC   Remove the section named 'SEC'.\n\
  -V | --version                  Print a version identifier and exit.\n\
  -X | --discard-locals           Remove compiler-generated local symbols.\n"

static void
strip_usage(void)
{
	(void) fprintf(stderr, STRIP_USAGE_MESSAGE, ELFTC_GETPROGNAME());
	exit(EXIT_FAILURE);
}

static void
print_version(void)
{
	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
	exit(EXIT_SUCCESS);
}

int
main(int argc, char **argv)
{
	struct elfcopy *ecp;

	if (elf_version(EV_CURRENT) == EV_NONE)
		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
		    elf_errmsg(-1));

	ecp = calloc(1, sizeof(*ecp));
	if (ecp == NULL)
		err(EXIT_FAILURE, "calloc failed");
	memset(ecp, 0, sizeof(*ecp));

	ecp->itf = ecp->otf = ETF_ELF;
	ecp->iec = ecp->oec = ELFCLASSNONE;
	ecp->oed = ELFDATANONE;
	ecp->abi = -1;
	/* There is always an empty section. */
	ecp->nos = 1;
	ecp->fill = 0;

	STAILQ_INIT(&ecp->v_seg);
	STAILQ_INIT(&ecp->v_sac);
	STAILQ_INIT(&ecp->v_sadd);
	STAILQ_INIT(&ecp->v_symop);
	STAILQ_INIT(&ecp->v_symfile);
	STAILQ_INIT(&ecp->v_arobj);
	TAILQ_INIT(&ecp->v_sec);

	if ((ecp->progname = ELFTC_GETPROGNAME()) == NULL)
		ecp->progname = "elfcopy";

	if (strcmp(ecp->progname, "strip") == 0)
		strip_main(ecp, argc, argv);
	else if (strcmp(ecp->progname, "mcs") == 0)
		mcs_main(ecp, argc, argv);
	else
		elfcopy_main(ecp, argc, argv);

	free_sec_add(ecp);
	free_sec_act(ecp);
	free(ecp);

	exit(EXIT_SUCCESS);
}