aboutsummaryrefslogtreecommitdiff
path: root/libiberty/simple-object-elf.c
blob: 7468a1adc3dd1da2584d7324d98b9314a18f91ce (plain)
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
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
1112
1113
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
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
/* simple-object-elf.c -- routines to manipulate ELF object files.
   Copyright (C) 2010-2018 Free Software Foundation, Inc.
   Written by Ian Lance Taylor, Google.

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 2, 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, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  */

#include "config.h"
#include "libiberty.h"
#include "simple-object.h"

#include <errno.h>
#include <stddef.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#include "simple-object-common.h"

/* ELF structures and constants.  */

/* 32-bit ELF file header.  */

typedef struct {
  unsigned char	e_ident[16];		/* ELF "magic number" */
  unsigned char	e_type[2];		/* Identifies object file type */
  unsigned char	e_machine[2];		/* Specifies required architecture */
  unsigned char	e_version[4];		/* Identifies object file version */
  unsigned char	e_entry[4];		/* Entry point virtual address */
  unsigned char	e_phoff[4];		/* Program header table file offset */
  unsigned char	e_shoff[4];		/* Section header table file offset */
  unsigned char	e_flags[4];		/* Processor-specific flags */
  unsigned char	e_ehsize[2];		/* ELF header size in bytes */
  unsigned char	e_phentsize[2];		/* Program header table entry size */
  unsigned char	e_phnum[2];		/* Program header table entry count */
  unsigned char	e_shentsize[2];		/* Section header table entry size */
  unsigned char	e_shnum[2];		/* Section header table entry count */
  unsigned char	e_shstrndx[2];		/* Section header string table index */
} Elf32_External_Ehdr;

/* 64-bit ELF file header.  */

typedef struct {
  unsigned char	e_ident[16];		/* ELF "magic number" */
  unsigned char	e_type[2];		/* Identifies object file type */
  unsigned char	e_machine[2];		/* Specifies required architecture */
  unsigned char	e_version[4];		/* Identifies object file version */
  unsigned char	e_entry[8];		/* Entry point virtual address */
  unsigned char	e_phoff[8];		/* Program header table file offset */
  unsigned char	e_shoff[8];		/* Section header table file offset */
  unsigned char	e_flags[4];		/* Processor-specific flags */
  unsigned char	e_ehsize[2];		/* ELF header size in bytes */
  unsigned char	e_phentsize[2];		/* Program header table entry size */
  unsigned char	e_phnum[2];		/* Program header table entry count */
  unsigned char	e_shentsize[2];		/* Section header table entry size */
  unsigned char	e_shnum[2];		/* Section header table entry count */
  unsigned char	e_shstrndx[2];		/* Section header string table index */
} Elf64_External_Ehdr;

/* Indexes and values in e_ident field of Ehdr.  */

#define EI_MAG0		0	/* File identification byte 0 index */
#define ELFMAG0		   0x7F	/* Magic number byte 0 */

#define EI_MAG1		1	/* File identification byte 1 index */
#define ELFMAG1		    'E'	/* Magic number byte 1 */

#define EI_MAG2		2	/* File identification byte 2 index */
#define ELFMAG2		    'L'	/* Magic number byte 2 */

#define EI_MAG3		3	/* File identification byte 3 index */
#define ELFMAG3		    'F'	/* Magic number byte 3 */

#define EI_CLASS	4	/* File class */
#define ELFCLASSNONE	      0	/* Invalid class */
#define ELFCLASS32	      1	/* 32-bit objects */
#define ELFCLASS64	      2	/* 64-bit objects */

#define EI_DATA		5	/* Data encoding */
#define ELFDATANONE	      0	/* Invalid data encoding */
#define ELFDATA2LSB	      1	/* 2's complement, little endian */
#define ELFDATA2MSB	      2	/* 2's complement, big endian */

#define EI_VERSION	6	/* File version */
#define EV_CURRENT	1		/* Current version */

#define EI_OSABI	7	/* Operating System/ABI indication */

/* Values for e_type field of Ehdr.  */

#define ET_REL		1	/* Relocatable file */

/* Values for e_machine field of Ehdr.  */

#define EM_SPARC	  2	/* SUN SPARC */
#define EM_SPARC32PLUS	 18	/* Sun's "v8plus" */

/* Special section index values.  */

#define SHN_UNDEF	0		/* Undefined section */
#define SHN_LORESERVE	0xFF00		/* Begin range of reserved indices */
#define SHN_COMMON	0xFFF2	/* Associated symbol is in common */
#define SHN_XINDEX	0xFFFF		/* Section index is held elsewhere */
#define SHN_HIRESERVE	0xffff		/* End of reserved indices */


/* 32-bit ELF program header.  */

typedef struct {
  unsigned char	p_type[4];		/* Identifies program segment type */
  unsigned char	p_offset[4];		/* Segment file offset */
  unsigned char	p_vaddr[4];		/* Segment virtual address */
  unsigned char	p_paddr[4];		/* Segment physical address */
  unsigned char	p_filesz[4];		/* Segment size in file */
  unsigned char	p_memsz[4];		/* Segment size in memory */
  unsigned char	p_flags[4];		/* Segment flags */
  unsigned char	p_align[4];		/* Segment alignment, file & memory */
} Elf32_External_Phdr;

/* 64-bit ELF program header.  */

typedef struct {
  unsigned char	p_type[4];		/* Identifies program segment type */
  unsigned char	p_flags[4];		/* Segment flags */
  unsigned char	p_offset[8];		/* Segment file offset */
  unsigned char	p_vaddr[8];		/* Segment virtual address */
  unsigned char	p_paddr[8];		/* Segment physical address */
  unsigned char	p_filesz[8];		/* Segment size in file */
  unsigned char	p_memsz[8];		/* Segment size in memory */
  unsigned char	p_align[8];		/* Segment alignment, file & memory */
} Elf64_External_Phdr;

/* 32-bit ELF section header */

typedef struct {
  unsigned char	sh_name[4];		/* Section name, index in string tbl */
  unsigned char	sh_type[4];		/* Type of section */
  unsigned char	sh_flags[4];		/* Miscellaneous section attributes */
  unsigned char	sh_addr[4];		/* Section virtual addr at execution */
  unsigned char	sh_offset[4];		/* Section file offset */
  unsigned char	sh_size[4];		/* Size of section in bytes */
  unsigned char	sh_link[4];		/* Index of another section */
  unsigned char	sh_info[4];		/* Additional section information */
  unsigned char	sh_addralign[4];	/* Section alignment */
  unsigned char	sh_entsize[4];		/* Entry size if section holds table */
} Elf32_External_Shdr;

/* 64-bit ELF section header.  */

typedef struct {
  unsigned char	sh_name[4];		/* Section name, index in string tbl */
  unsigned char	sh_type[4];		/* Type of section */
  unsigned char	sh_flags[8];		/* Miscellaneous section attributes */
  unsigned char	sh_addr[8];		/* Section virtual addr at execution */
  unsigned char	sh_offset[8];		/* Section file offset */
  unsigned char	sh_size[8];		/* Size of section in bytes */
  unsigned char	sh_link[4];		/* Index of another section */
  unsigned char	sh_info[4];		/* Additional section information */
  unsigned char	sh_addralign[8];	/* Section alignment */
  unsigned char	sh_entsize[8];		/* Entry size if section holds table */
} Elf64_External_Shdr;

/* Values for sh_type field.  */

#define SHT_NULL	0		/* Section header table entry unused */
#define SHT_PROGBITS	1		/* Program data */
#define SHT_SYMTAB	2		/* Link editing symbol table */
#define SHT_STRTAB	3		/* A string table */
#define SHT_RELA	4		/* Relocation entries with addends */
#define SHT_REL		9		/* Relocation entries, no addends */
#define SHT_GROUP	17		/* Section contains a section group */
#define SHT_SYMTAB_SHNDX 18		/* Extended section indeces */

/* Values for sh_flags field.  */

#define SHF_INFO_LINK	0x00000040	/* `sh_info' contains SHT index */
#define SHF_EXECINSTR	0x00000004	/* Executable section.  */
#define SHF_EXCLUDE	0x80000000	/* Link editor is to exclude this
					   section from executable and
					   shared library that it builds
					   when those objects are not to be
					   further relocated.  */
/* Symbol table entry.  */

typedef struct
{
  unsigned char st_name[4];                /* Symbol name (string tbl index) */
  unsigned char st_value[4];               /* Symbol value */
  unsigned char st_size[4];                /* Symbol size */
  unsigned char st_info;                /* Symbol type and binding */
  unsigned char st_other;               /* Symbol visibility */
  unsigned char st_shndx[2];               /* Section index */
} Elf32_External_Sym;

typedef struct
{
  unsigned char st_name[4];                /* Symbol name (string tbl index) */
  unsigned char st_info;                /* Symbol type and binding */
  unsigned char st_other;               /* Symbol visibility */
  unsigned char st_shndx[2];               /* Section index */
  unsigned char st_value[8];               /* Symbol value */
  unsigned char st_size[8];                /* Symbol size */
} Elf64_External_Sym;

#define ELF_ST_BIND(val)              (((unsigned char) (val)) >> 4)
#define ELF_ST_TYPE(val)              ((val) & 0xf)
#define ELF_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))

#define STT_NOTYPE	0	/* Symbol type is unspecified */
#define STT_OBJECT	1	/* Symbol is a data object */
#define STT_FUNC	2	/* Symbol is a code object */
#define STT_TLS		6	/* Thread local data object */
#define STT_GNU_IFUNC	10	/* Symbol is an indirect code object */

#define STB_LOCAL	0	/* Local symbol */
#define STB_GLOBAL	1	/* Global symbol */
#define STB_WEAK	2	/* Weak global */

#define STV_DEFAULT	0	/* Visibility is specified by binding type */
#define STV_HIDDEN	2	/* Can only be seen inside currect component */

/* Functions to fetch and store different ELF types, depending on the
   endianness and size.  */

struct elf_type_functions
{
  unsigned short (*fetch_Elf_Half) (const unsigned char *);
  unsigned int (*fetch_Elf_Word) (const unsigned char *);
  ulong_type (*fetch_Elf_Addr) (const unsigned char *);
  void (*set_Elf_Half) (unsigned char *, unsigned short);
  void (*set_Elf_Word) (unsigned char *, unsigned int);
  void (*set_Elf_Addr) (unsigned char *, ulong_type);
};

static const struct elf_type_functions elf_big_32_functions =
{
  simple_object_fetch_big_16,
  simple_object_fetch_big_32,
  simple_object_fetch_big_32_ulong,
  simple_object_set_big_16,
  simple_object_set_big_32,
  simple_object_set_big_32_ulong
};

static const struct elf_type_functions elf_little_32_functions =
{
  simple_object_fetch_little_16,
  simple_object_fetch_little_32,
  simple_object_fetch_little_32_ulong,
  simple_object_set_little_16,
  simple_object_set_little_32,
  simple_object_set_little_32_ulong
};

#ifdef UNSIGNED_64BIT_TYPE

static const struct elf_type_functions elf_big_64_functions =
{
  simple_object_fetch_big_16,
  simple_object_fetch_big_32,
  simple_object_fetch_big_64,
  simple_object_set_big_16,
  simple_object_set_big_32,
  simple_object_set_big_64
};

static const struct elf_type_functions elf_little_64_functions =
{
  simple_object_fetch_little_16,
  simple_object_fetch_little_32,
  simple_object_fetch_little_64,
  simple_object_set_little_16,
  simple_object_set_little_32,
  simple_object_set_little_64
};

#endif

/* Hideous macro to fetch the value of a field from an external ELF
   struct of some sort.  TYPEFUNCS is the set of type functions.
   BUFFER points to the external data.  STRUCTTYPE is the appropriate
   struct type.  FIELD is a field within the struct.  TYPE is the type
   of the field in the struct: Elf_Half, Elf_Word, or Elf_Addr.  */

#define ELF_FETCH_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE) \
  ((TYPEFUNCS)->fetch_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD)))

/* Even more hideous macro to fetch the value of FIELD from BUFFER.
   SIZE is 32 or 64.  STRUCTTYPE is the name of the struct from
   elf/external.h: Ehdr, Shdr, etc.  FIELD is the name of a field in
   the struct.  TYPE is the type of the field in the struct: Elf_Half,
   Elf_Word, or Elf_Addr.  */

#define ELF_FETCH_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER,	\
			      FIELD, TYPE)				\
  ELF_FETCH_STRUCT_FIELD (TYPEFUNCS,					\
			  Elf ## SIZE ## _External_ ## STRUCTTYPE,	\
			  FIELD, BUFFER, TYPE)

/* Like ELF_FETCH_SIZED_FIELD but taking an ELFCLASS value.  */

#define ELF_FETCH_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER,		\
			FIELD, TYPE)					\
  ((CLASS) == ELFCLASS32						\
    ? ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD,	\
			     TYPE)					\
    : ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD,	\
			     TYPE))

/* Hideous macro to set the value of a field in an external ELF
   structure to VAL.  TYPEFUNCS is the set of type functions.  BUFFER
   points to the external data.  STRUCTTYPE is the appropriate
   structure type.  FIELD is a field within the struct.  TYPE is the
   type of the field in the struct: Elf_Half, Elf_Word, or
   Elf_Addr.  */

#define ELF_SET_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE, VAL) \
  (TYPEFUNCS)->set_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD), (VAL))

/* Even more hideous macro to set the value of FIELD in BUFFER to VAL.
   SIZE is 32 or 64.  STRUCTTYPE is the name of the struct from
   elf/external.h: Ehdr, Shdr, etc.  FIELD is the name of a field in
   the struct.  TYPE is the type of the field in the struct: Elf_Half,
   Elf_Word, or Elf_Addr.  */

#define ELF_SET_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, FIELD, \
			    TYPE, VAL)					\
  ELF_SET_STRUCT_FIELD (TYPEFUNCS,					\
			Elf ## SIZE ## _External_ ## STRUCTTYPE,	\
			FIELD, BUFFER, TYPE, VAL)

/* Like ELF_SET_SIZED_FIELD but taking an ELFCLASS value.  */

#define ELF_SET_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, FIELD,	\
		      TYPE, VAL)					\
  ((CLASS) == ELFCLASS32						\
    ? ELF_SET_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD,	\
			   TYPE, VAL)					\
    : ELF_SET_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD,	\
			   TYPE, VAL))

/* Private data for an simple_object_read.  */

struct simple_object_elf_read
{
  /* Type functions.  */
  const struct elf_type_functions* type_functions;
  /* Elf data.  */
  unsigned char ei_data;
  /* Elf class.  */
  unsigned char ei_class;
  /* ELF OS ABI.  */
  unsigned char ei_osabi;
  /* Elf machine number.  */
  unsigned short machine;
  /* Processor specific flags.  */
  unsigned int flags;
  /* File offset of section headers.  */
  ulong_type shoff;
  /* Number of sections.  */
  unsigned int shnum;
  /* Index of string table section header.  */
  unsigned int shstrndx;
};

/* Private data for an simple_object_attributes.  */

struct simple_object_elf_attributes
{
  /* Type functions.  */
  const struct elf_type_functions* type_functions;
  /* Elf data.  */
  unsigned char ei_data;
  /* Elf class.  */
  unsigned char ei_class;
  /* ELF OS ABI.  */
  unsigned char ei_osabi;
  /* Elf machine number.  */
  unsigned short machine;
  /* Processor specific flags.  */
  unsigned int flags;
};

/* Private data for an simple_object_write.  */

struct simple_object_elf_write
{
  struct simple_object_elf_attributes attrs;
  unsigned char *shdrs;
};

/* See if we have an ELF file.  */

static void *
simple_object_elf_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
			 int descriptor, off_t offset,
			 const char *segment_name ATTRIBUTE_UNUSED,
			 const char **errmsg, int *err)
{
  unsigned char ei_data;
  unsigned char ei_class;
  const struct elf_type_functions *type_functions;
  unsigned char ehdr[sizeof (Elf64_External_Ehdr)];
  struct simple_object_elf_read *eor;

  if (header[EI_MAG0] != ELFMAG0
      || header[EI_MAG1] != ELFMAG1
      || header[EI_MAG2] != ELFMAG2
      || header[EI_MAG3] != ELFMAG3
      || header[EI_VERSION] != EV_CURRENT)
    {
      *errmsg = NULL;
      *err = 0;
      return NULL;
    }

  ei_data = header[EI_DATA];
  if (ei_data != ELFDATA2LSB && ei_data != ELFDATA2MSB)
    {
      *errmsg = "unknown ELF endianness";
      *err = 0;
      return NULL;
    }

  ei_class = header[EI_CLASS];
  switch (ei_class)
    {
    case ELFCLASS32:
      type_functions = (ei_data == ELFDATA2LSB
			? &elf_little_32_functions
			: &elf_big_32_functions);
      break;

    case ELFCLASS64:
#ifndef UNSIGNED_64BIT_TYPE
      *errmsg = "64-bit ELF objects not supported";
      *err = 0;
      return NULL;
#else
      type_functions = (ei_data == ELFDATA2LSB
			? &elf_little_64_functions
			: &elf_big_64_functions);
      break;
#endif

    default:
      *errmsg = "unrecognized ELF size";
      *err = 0;
      return NULL;
    }

  if (!simple_object_internal_read (descriptor, offset, ehdr, sizeof ehdr,
				    errmsg, err))
    return NULL;

  eor = XNEW (struct simple_object_elf_read);
  eor->type_functions = type_functions;
  eor->ei_data = ei_data;
  eor->ei_class = ei_class;
  eor->ei_osabi = header[EI_OSABI];
  eor->machine = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
				  e_machine, Elf_Half);
  eor->flags = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
				e_flags, Elf_Word);
  eor->shoff = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
				e_shoff, Elf_Addr);
  eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
				e_shnum, Elf_Half);
  eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
				   e_shstrndx, Elf_Half);

  if ((eor->shnum == 0 || eor->shstrndx == SHN_XINDEX)
      && eor->shoff != 0)
    {
      unsigned char shdr[sizeof (Elf64_External_Shdr)];

      /* Object file has more than 0xffff sections.  */

      if (!simple_object_internal_read (descriptor, offset + eor->shoff, shdr,
					(ei_class == ELFCLASS32
					 ? sizeof (Elf32_External_Shdr)
					 : sizeof (Elf64_External_Shdr)),
					errmsg, err))
	{
	  XDELETE (eor);
	  return NULL;
	}

      if (eor->shnum == 0)
	eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				      shdr, sh_size, Elf_Addr);

      if (eor->shstrndx == SHN_XINDEX)
	{
	  eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					   shdr, sh_link, Elf_Word);

	  /* Versions of the GNU binutils between 2.12 and 2.18 did
	     not handle objects with more than SHN_LORESERVE sections
	     correctly.  All large section indexes were offset by
	     0x100.  There is more information at
	     http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
	     Fortunately these object files are easy to detect, as the
	     GNU binutils always put the section header string table
	     near the end of the list of sections.  Thus if the
	     section header string table index is larger than the
	     number of sections, then we know we have to subtract
	     0x100 to get the real section index.  */
	  if (eor->shstrndx >= eor->shnum
	      && eor->shstrndx >= SHN_LORESERVE + 0x100)
	    eor->shstrndx -= 0x100;
	}
    }

  if (eor->shstrndx >= eor->shnum)
    {
      *errmsg = "invalid ELF shstrndx >= shnum";
      *err = 0;
      XDELETE (eor);
      return NULL;
    }

  return (void *) eor;
}

/* Find all sections in an ELF file.  */

static const char *
simple_object_elf_find_sections (simple_object_read *sobj,
				 int (*pfn) (void *, const char *,
					     off_t offset, off_t length),
				 void *data,
				 int *err)
{
  struct simple_object_elf_read *eor =
    (struct simple_object_elf_read *) sobj->data;
  const struct elf_type_functions *type_functions = eor->type_functions;
  unsigned char ei_class = eor->ei_class;
  size_t shdr_size;
  unsigned int shnum;
  unsigned char *shdrs;
  const char *errmsg;
  unsigned char *shstrhdr;
  size_t name_size;
  off_t shstroff;
  unsigned char *names;
  unsigned int i;

  shdr_size = (ei_class == ELFCLASS32
	       ? sizeof (Elf32_External_Shdr)
	       : sizeof (Elf64_External_Shdr));

  /* Read the section headers.  We skip section 0, which is not a
     useful section.  */

  shnum = eor->shnum;
  shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));

  if (!simple_object_internal_read (sobj->descriptor,
				    sobj->offset + eor->shoff + shdr_size,
				    shdrs,
				    shdr_size * (shnum - 1),
				    &errmsg, err))
    {
      XDELETEVEC (shdrs);
      return errmsg;
    }

  /* Read the section names.  */

  shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
  name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
			       shstrhdr, sh_size, Elf_Addr);
  shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
			      shstrhdr, sh_offset, Elf_Addr);
  names = XNEWVEC (unsigned char, name_size);
  if (!simple_object_internal_read (sobj->descriptor,
				    sobj->offset + shstroff,
				    names, name_size, &errmsg, err))
    {
      XDELETEVEC (names);
      XDELETEVEC (shdrs);
      return errmsg;
    }

  for (i = 1; i < shnum; ++i)
    {
      unsigned char *shdr;
      unsigned int sh_name;
      const char *name;
      off_t offset;
      off_t length;

      shdr = shdrs + (i - 1) * shdr_size;
      sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				 shdr, sh_name, Elf_Word);
      if (sh_name >= name_size)
	{
	  *err = 0;
	  XDELETEVEC (names);
	  XDELETEVEC (shdrs);
	  return "ELF section name out of range";
	}

      name = (const char *) names + sh_name;
      offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				shdr, sh_offset, Elf_Addr);
      length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				shdr, sh_size, Elf_Addr);

      if (!(*pfn) (data, name, offset, length))
	break;
    }

  XDELETEVEC (names);
  XDELETEVEC (shdrs);

  return NULL;
}

/* Fetch the attributes for an simple_object_read.  */

static void *
simple_object_elf_fetch_attributes (simple_object_read *sobj,
				    const char **errmsg ATTRIBUTE_UNUSED,
				    int *err ATTRIBUTE_UNUSED)
{
  struct simple_object_elf_read *eor =
    (struct simple_object_elf_read *) sobj->data;
  struct simple_object_elf_attributes *ret;

  ret = XNEW (struct simple_object_elf_attributes);
  ret->type_functions = eor->type_functions;
  ret->ei_data = eor->ei_data;
  ret->ei_class = eor->ei_class;
  ret->ei_osabi = eor->ei_osabi;
  ret->machine = eor->machine;
  ret->flags = eor->flags;
  return ret;
}

/* Release the privata data for an simple_object_read.  */

static void
simple_object_elf_release_read (void *data)
{
  XDELETE (data);
}

/* Compare two attributes structures.  */

static const char *
simple_object_elf_attributes_merge (void *todata, void *fromdata, int *err)
{
  struct simple_object_elf_attributes *to =
    (struct simple_object_elf_attributes *) todata;
  struct simple_object_elf_attributes *from =
    (struct simple_object_elf_attributes *) fromdata;

  if (to->ei_data != from->ei_data || to->ei_class != from->ei_class)
    {
      *err = 0;
      return "ELF object format mismatch";
    }

  if (to->machine != from->machine)
    {
      int ok;

      /* EM_SPARC and EM_SPARC32PLUS are compatible and force an
	 output of EM_SPARC32PLUS.  */
      ok = 0;
      switch (to->machine)
	{
	case EM_SPARC:
	  if (from->machine == EM_SPARC32PLUS)
	    {
	      to->machine = from->machine;
	      ok = 1;
	    }
	  break;

	case EM_SPARC32PLUS:
	  if (from->machine == EM_SPARC)
	    ok = 1;
	  break;

	default:
	  break;
	}

      if (!ok)
	{
	  *err = 0;
	  return "ELF machine number mismatch";
	}
    }

  return NULL;
}

/* Release the private data for an attributes structure.  */

static void
simple_object_elf_release_attributes (void *data)
{
  XDELETE (data);
}

/* Prepare to write out a file.  */

static void *
simple_object_elf_start_write (void *attributes_data,
			       const char **errmsg ATTRIBUTE_UNUSED,
			       int *err ATTRIBUTE_UNUSED)
{
  struct simple_object_elf_attributes *attrs =
    (struct simple_object_elf_attributes *) attributes_data;
  struct simple_object_elf_write *ret;

  /* We're just going to record the attributes, but we need to make a
     copy because the user may delete them.  */
  ret = XNEW (struct simple_object_elf_write);
  ret->attrs = *attrs;
  ret->shdrs = NULL;
  return ret;
}

/* Write out an ELF ehdr.  */

static int
simple_object_elf_write_ehdr (simple_object_write *sobj, int descriptor,
			      const char **errmsg, int *err)
{
  struct simple_object_elf_attributes *attrs =
    (struct simple_object_elf_attributes *) sobj->data;
  const struct elf_type_functions* fns;
  unsigned char cl;
  size_t ehdr_size;
  unsigned char buf[sizeof (Elf64_External_Ehdr)];
  simple_object_write_section *section;
  unsigned int shnum;
  unsigned int shstrndx;

  fns = attrs->type_functions;
  cl = attrs->ei_class;

  shnum = 0;
  for (section = sobj->sections; section != NULL; section = section->next)
    ++shnum;
  if (shnum > 0)
    {
      /* Add a section header for the dummy section and one for
	 .shstrtab.  */
      shnum += 2;
    }

  ehdr_size = (cl == ELFCLASS32
	       ? sizeof (Elf32_External_Ehdr)
	       : sizeof (Elf64_External_Ehdr));
  memset (buf, 0, sizeof (Elf64_External_Ehdr));

  buf[EI_MAG0] = ELFMAG0;
  buf[EI_MAG1] = ELFMAG1;
  buf[EI_MAG2] = ELFMAG2;
  buf[EI_MAG3] = ELFMAG3;
  buf[EI_CLASS] = cl;
  buf[EI_DATA] = attrs->ei_data;
  buf[EI_VERSION] = EV_CURRENT;
  buf[EI_OSABI] = attrs->ei_osabi;

  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_type, Elf_Half, ET_REL);
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_machine, Elf_Half, attrs->machine);
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_version, Elf_Word, EV_CURRENT);
  /* e_entry left as zero.  */
  /* e_phoff left as zero.  */
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shoff, Elf_Addr, ehdr_size);
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_flags, Elf_Word, attrs->flags);
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_ehsize, Elf_Half, ehdr_size);
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_phentsize, Elf_Half,
		 (cl == ELFCLASS32
		  ? sizeof (Elf32_External_Phdr)
		  : sizeof (Elf64_External_Phdr)));
  /* e_phnum left as zero.  */
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shentsize, Elf_Half,
		 (cl == ELFCLASS32
		  ? sizeof (Elf32_External_Shdr)
		  : sizeof (Elf64_External_Shdr)));
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shnum, Elf_Half,
		 shnum >= SHN_LORESERVE ? 0 : shnum);
  if (shnum == 0)
    shstrndx = 0;
  else
    {
      shstrndx = shnum - 1;
      if (shstrndx >= SHN_LORESERVE)
	shstrndx = SHN_XINDEX;
    }
  ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shstrndx, Elf_Half, shstrndx);

  return simple_object_internal_write (descriptor, 0, buf, ehdr_size,
				       errmsg, err);
}

/* Write out an ELF shdr.  */

static int
simple_object_elf_write_shdr (simple_object_write *sobj, int descriptor,
			      off_t offset, unsigned int sh_name,
			      unsigned int sh_type, unsigned int sh_flags,
			      off_t sh_addr,
			      unsigned int sh_offset, unsigned int sh_size,
			      unsigned int sh_link, unsigned int sh_info,
			      size_t sh_addralign,
			      size_t sh_entsize,
			      const char **errmsg, int *err)
{
  struct simple_object_elf_attributes *attrs =
    (struct simple_object_elf_attributes *) sobj->data;
  const struct elf_type_functions* fns;
  unsigned char cl;
  size_t shdr_size;
  unsigned char buf[sizeof (Elf64_External_Shdr)];

  fns = attrs->type_functions;
  cl = attrs->ei_class;

  shdr_size = (cl == ELFCLASS32
	       ? sizeof (Elf32_External_Shdr)
	       : sizeof (Elf64_External_Shdr));
  memset (buf, 0, sizeof (Elf64_External_Shdr));

  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_name, Elf_Word, sh_name);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_type, Elf_Word, sh_type);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_flags, Elf_Addr, sh_flags);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addr, Elf_Addr, sh_addr);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_offset, Elf_Addr, sh_offset);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_size, Elf_Addr, sh_size);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_link, Elf_Word, sh_link);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_info, Elf_Word, sh_info);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addralign, Elf_Addr, sh_addralign);
  ELF_SET_FIELD (fns, cl, Shdr, buf, sh_entsize, Elf_Addr, sh_entsize);

  return simple_object_internal_write (descriptor, offset, buf, shdr_size,
				       errmsg, err);
}

/* Write out a complete ELF file.
   Ehdr
   initial dummy Shdr
   user-created Shdrs
   .shstrtab Shdr
   user-created section data
   .shstrtab data  */

static const char *
simple_object_elf_write_to_file (simple_object_write *sobj, int descriptor,
				 int *err)
{
  struct simple_object_elf_write *eow =
    (struct simple_object_elf_write *) sobj->data;
  struct simple_object_elf_attributes *attrs = &eow->attrs;
  unsigned char cl;
  size_t ehdr_size;
  size_t shdr_size;
  const char *errmsg;
  simple_object_write_section *section;
  unsigned int shnum;
  size_t shdr_offset;
  size_t sh_offset;
  unsigned int first_sh_size;
  unsigned int first_sh_link;
  size_t sh_name;
  unsigned char zero;
  unsigned secnum;

  if (!simple_object_elf_write_ehdr (sobj, descriptor, &errmsg, err))
    return errmsg;

  cl = attrs->ei_class;
  if (cl == ELFCLASS32)
    {
      ehdr_size = sizeof (Elf32_External_Ehdr);
      shdr_size = sizeof (Elf32_External_Shdr);
    }
  else
    {
      ehdr_size = sizeof (Elf64_External_Ehdr);
      shdr_size = sizeof (Elf64_External_Shdr);
    }

  shnum = 0;
  for (section = sobj->sections; section != NULL; section = section->next)
    ++shnum;
  if (shnum == 0)
    return NULL;

  /* Add initial dummy Shdr and .shstrtab.  */
  shnum += 2;

  shdr_offset = ehdr_size;
  sh_offset = shdr_offset + shnum * shdr_size;

  if (shnum < SHN_LORESERVE)
    first_sh_size = 0;
  else
    first_sh_size = shnum;
  if (shnum - 1 < SHN_LORESERVE)
    first_sh_link = 0;
  else
    first_sh_link = shnum - 1;
  if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
				     0, 0, 0, 0, 0, first_sh_size, first_sh_link,
				     0, 0, 0, &errmsg, err))
    return errmsg;

  shdr_offset += shdr_size;

  sh_name = 1;
  secnum = 0;
  for (section = sobj->sections; section != NULL; section = section->next)
    {
      size_t mask;
      size_t new_sh_offset;
      size_t sh_size;
      struct simple_object_write_section_buffer *buffer;
      unsigned int sh_type = SHT_PROGBITS;
      unsigned int sh_flags = 0;
      off_t sh_addr = 0;
      unsigned int sh_link = 0;
      unsigned int sh_info = 0;
      size_t sh_addralign = 1U << section->align;
      size_t sh_entsize = 0;
      if (eow->shdrs)
	{
	  sh_type = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
				     eow->shdrs + secnum * shdr_size,
				     sh_type, Elf_Word);
	  sh_flags = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
				      eow->shdrs + secnum * shdr_size,
				      sh_flags, Elf_Addr);
	  sh_addr = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
				     eow->shdrs + secnum * shdr_size,
				     sh_addr, Elf_Addr);
	  sh_link = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
				     eow->shdrs + secnum * shdr_size,
				     sh_link, Elf_Word);
	  sh_info = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
				     eow->shdrs + secnum * shdr_size,
				     sh_info, Elf_Word);
	  sh_addralign = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
					  eow->shdrs + secnum * shdr_size,
					  sh_addralign, Elf_Addr);
	  sh_entsize = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
					eow->shdrs + secnum * shdr_size,
					sh_entsize, Elf_Addr);
	  secnum++;
	}

      mask = sh_addralign - 1;
      new_sh_offset = sh_offset + mask;
      new_sh_offset &= ~ mask;
      while (new_sh_offset > sh_offset)
	{
	  unsigned char zeroes[16];
	  size_t write;

	  memset (zeroes, 0, sizeof zeroes);
	  write = new_sh_offset - sh_offset;
	  if (write > sizeof zeroes)
	    write = sizeof zeroes;
	  if (!simple_object_internal_write (descriptor, sh_offset, zeroes,
					     write, &errmsg, err))
	    return errmsg;
	  sh_offset += write;
	}

      sh_size = 0;
      for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
	{
	  if (!simple_object_internal_write (descriptor, sh_offset + sh_size,
					     ((const unsigned char *)
					      buffer->buffer),
					     buffer->size, &errmsg, err))
	    return errmsg;
	  sh_size += buffer->size;
	}

      if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
					 sh_name, sh_type, sh_flags,
					 sh_addr, sh_offset,
					 sh_size, sh_link, sh_info,
					 sh_addralign, sh_entsize,
					 &errmsg, err))
	return errmsg;

      shdr_offset += shdr_size;
      sh_name += strlen (section->name) + 1;
      sh_offset += sh_size;
    }

  if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
				     sh_name, SHT_STRTAB, 0, 0, sh_offset,
				     sh_name + strlen (".shstrtab") + 1, 0, 0,
				     1, 0, &errmsg, err))
    return errmsg;

  /* .shstrtab has a leading zero byte.  */
  zero = 0;
  if (!simple_object_internal_write (descriptor, sh_offset, &zero, 1,
				     &errmsg, err))
    return errmsg;
  ++sh_offset;

  for (section = sobj->sections; section != NULL; section = section->next)
    {
      size_t len;

      len = strlen (section->name) + 1;
      if (!simple_object_internal_write (descriptor, sh_offset,
					 (const unsigned char *) section->name,
					 len, &errmsg, err))
	return errmsg;
      sh_offset += len;
    }

  if (!simple_object_internal_write (descriptor, sh_offset,
				     (const unsigned char *) ".shstrtab",
				     strlen (".shstrtab") + 1, &errmsg, err))
    return errmsg;

  return NULL;
}

/* Release the private data for an simple_object_write structure.  */

static void
simple_object_elf_release_write (void *data)
{
  struct simple_object_elf_write *eow = (struct simple_object_elf_write *) data;
  if (eow->shdrs)
    XDELETE (eow->shdrs);
  XDELETE (data);
}

/* Copy all sections in an ELF file.  */

static const char *
simple_object_elf_copy_lto_debug_sections (simple_object_read *sobj,
					   simple_object_write *dobj,
					   char *(*pfn) (const char *),
					   int *err)
{
  struct simple_object_elf_read *eor =
    (struct simple_object_elf_read *) sobj->data;
  const struct elf_type_functions *type_functions = eor->type_functions;
  struct simple_object_elf_write *eow =
    (struct simple_object_elf_write *) dobj->data;
  unsigned char ei_class = eor->ei_class;
  size_t shdr_size;
  unsigned int shnum;
  unsigned char *shdrs;
  const char *errmsg;
  unsigned char *shstrhdr;
  size_t name_size;
  off_t shstroff;
  unsigned char *names;
  unsigned int i;
  int changed;
  int *pfnret;
  const char **pfnname;
  unsigned new_i;
  unsigned *sh_map;
  unsigned first_shndx = 0;
  unsigned int *symtab_indices_shndx;

  shdr_size = (ei_class == ELFCLASS32
	       ? sizeof (Elf32_External_Shdr)
	       : sizeof (Elf64_External_Shdr));

  /* Read the section headers.  We skip section 0, which is not a
     useful section.  */

  shnum = eor->shnum;
  shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));

  if (!simple_object_internal_read (sobj->descriptor,
				    sobj->offset + eor->shoff + shdr_size,
				    shdrs,
				    shdr_size * (shnum - 1),
				    &errmsg, err))
    {
      XDELETEVEC (shdrs);
      return errmsg;
    }

  /* Read the section names.  */

  shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
  name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
			       shstrhdr, sh_size, Elf_Addr);
  shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
			      shstrhdr, sh_offset, Elf_Addr);
  names = XNEWVEC (unsigned char, name_size);
  if (!simple_object_internal_read (sobj->descriptor,
				    sobj->offset + shstroff,
				    names, name_size, &errmsg, err))
    {
      XDELETEVEC (names);
      XDELETEVEC (shdrs);
      return errmsg;
    }

  pfnret = XNEWVEC (int, shnum);
  pfnname = XNEWVEC (const char *, shnum);

  /* Map of symtab to index section.  */
  symtab_indices_shndx = XCNEWVEC (unsigned int, shnum - 1);

  /* First perform the callbacks to know which sections to preserve and
     what name to use for those.  */
  for (i = 1; i < shnum; ++i)
    {
      unsigned char *shdr;
      unsigned int sh_name, sh_type;
      const char *name;
      char *ret;

      shdr = shdrs + (i - 1) * shdr_size;
      sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				 shdr, sh_name, Elf_Word);
      if (sh_name >= name_size)
	{
	  *err = 0;
	  XDELETEVEC (names);
	  XDELETEVEC (shdrs);
	  return "ELF section name out of range";
	}

      name = (const char *) names + sh_name;

      ret = (*pfn) (name);
      pfnret[i - 1] = ret == NULL ? -1 : 0;
      pfnname[i - 1] = ret == NULL ? name : ret;
      if (first_shndx == 0
	  && pfnret[i - 1] == 0)
	first_shndx = i;

      /* Remember the indexes of existing SHT_SYMTAB_SHNDX sections.  */
      sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				 shdr, sh_type, Elf_Word);
      if (sh_type == SHT_SYMTAB_SHNDX)
	{
	  unsigned int sh_link;
	  sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				     shdr, sh_link, Elf_Word);
	  symtab_indices_shndx[sh_link - 1] = i;
	  /* Always discard the extended index sections, after
	     copying it will not be needed.  This way we don't need to
	     update it and deal with the ordering constraints of
	     processing the existing symtab and changing the index.  */
	  pfnret[i - 1] = -1;
	}
    }

  /* Mark sections as preserved that are required by to be preserved
     sections.  */
  do
    {
      changed = 0;
      for (i = 1; i < shnum; ++i)
	{
	  unsigned char *shdr;
	  unsigned int sh_type, sh_info, sh_link;
	  off_t offset;
	  off_t length;

	  shdr = shdrs + (i - 1) * shdr_size;
	  sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				     shdr, sh_type, Elf_Word);
	  sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				     shdr, sh_info, Elf_Word);
	  sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				     shdr, sh_link, Elf_Word);
	  if (sh_type == SHT_GROUP)
	    {
	      /* Mark groups containing copied sections.  */
	      unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class,
						  Shdr, shdr, sh_entsize,
						  Elf_Addr);
	      unsigned char *ent, *buf;
	      int keep = 0;
	      offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					shdr, sh_offset, Elf_Addr);
	      length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					shdr, sh_size, Elf_Addr);
	      buf = XNEWVEC (unsigned char, length);
	      if (!simple_object_internal_read (sobj->descriptor,
						sobj->offset + offset, buf,
						(size_t) length, &errmsg, err))
		{
		  XDELETEVEC (buf);
		  XDELETEVEC (names);
		  XDELETEVEC (shdrs);
		  return errmsg;
		}
	      for (ent = buf + entsize; ent < buf + length; ent += entsize)
		{
		  unsigned sec = type_functions->fetch_Elf_Word (ent);
		  if (pfnret[sec - 1] == 0)
		    keep = 1;
		}
	      if (keep)
		{
		  changed |= (pfnret[sh_link - 1] == -1
			      || pfnret[i - 1] == -1);
		  pfnret[sh_link - 1] = 0;
		  pfnret[i - 1] = 0;
		}
	    }
	  if (sh_type == SHT_RELA
	      || sh_type == SHT_REL)
	    {
	      /* Mark relocation sections and symtab of copied sections.  */
	      if (pfnret[sh_info - 1] == 0)
		{
		  changed |= (pfnret[sh_link - 1] == -1
			      || pfnret[i - 1] == -1);
		  pfnret[sh_link - 1] = 0;
		  pfnret[i - 1] = 0;
		}
	    }
	  if (sh_type == SHT_SYMTAB)
	    {
	      /* Mark strings sections of copied symtabs.  */
	      if (pfnret[i - 1] == 0)
		{
		  changed |= pfnret[sh_link - 1] == -1;
		  pfnret[sh_link - 1] = 0;
		}
	    }
	}
    }
  while (changed);

  /* Compute a mapping of old -> new section numbers.  */
  sh_map = XNEWVEC (unsigned, shnum);
  sh_map[0] = 0;
  new_i = 1;
  for (i = 1; i < shnum; ++i)
    {
      if (pfnret[i - 1] == -1)
	sh_map[i] = 0;
      else
	sh_map[i] = new_i++;
    }
  if (new_i - 1 >= SHN_LORESERVE)
    {
      *err = ENOTSUP;
      return "Too many copied sections";
    }
  eow->shdrs = XNEWVEC (unsigned char, shdr_size * (new_i - 1));

  /* Then perform the actual copying.  */
  new_i = 0;
  for (i = 1; i < shnum; ++i)
    {
      unsigned char *shdr;
      unsigned int sh_name, sh_type;
      const char *name;
      off_t offset;
      off_t length;
      simple_object_write_section *dest;
      off_t flags;
      unsigned char *buf;

      if (pfnret[i - 1])
	continue;

      new_i++;
      shdr = shdrs + (i - 1) * shdr_size;
      sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				 shdr, sh_name, Elf_Word);
      if (sh_name >= name_size)
	{
	  *err = 0;
	  XDELETEVEC (names);
	  XDELETEVEC (shdrs);
	  XDELETEVEC (symtab_indices_shndx);
	  return "ELF section name out of range";
	}

      name = pfnname[i - 1];
      offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				shdr, sh_offset, Elf_Addr);
      length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				shdr, sh_size, Elf_Addr);
      sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				 shdr, sh_type, Elf_Word);

      dest = simple_object_write_create_section (dobj, pfnname[i - 1],
						 0, &errmsg, err);
      if (dest == NULL)
	{
	  XDELETEVEC (names);
	  XDELETEVEC (shdrs);
	  XDELETEVEC (symtab_indices_shndx);
	  return errmsg;
	}

      /* Record the SHDR of the source.  */
      memcpy (eow->shdrs + (new_i - 1) * shdr_size, shdr, shdr_size);
      shdr = eow->shdrs + (new_i - 1) * shdr_size;

      /* Copy the data.
	 ???  This is quite wasteful and ideally would be delayed until
	 write_to_file ().  Thus it questions the interfacing
	 which eventually should contain destination creation plus
	 writing.  */
      buf = XNEWVEC (unsigned char, length);
      if (!simple_object_internal_read (sobj->descriptor,
					sobj->offset + offset, buf,
					(size_t) length, &errmsg, err))
	{
	  XDELETEVEC (buf);
	  XDELETEVEC (names);
	  XDELETEVEC (shdrs);
	  XDELETEVEC (symtab_indices_shndx);
	  return errmsg;
	}

      /* If we are processing .symtab purge __gnu_lto_v1 and
	 __gnu_lto_slim symbols from it and any symbols in discarded
	 sections.  */
      if (sh_type == SHT_SYMTAB)
	{
	  unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					      shdr, sh_entsize, Elf_Addr);
	  unsigned strtab = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					     shdr, sh_link, Elf_Word);
	  unsigned char *strshdr = shdrs + (strtab - 1) * shdr_size;
	  off_t stroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					  strshdr, sh_offset, Elf_Addr);
	  size_t strsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					  strshdr, sh_size, Elf_Addr);
	  char *strings = XNEWVEC (char, strsz);
	  char *gnu_lto = strings;
	  unsigned char *ent;
	  unsigned *shndx_table = NULL;
	  simple_object_internal_read (sobj->descriptor,
				       sobj->offset + stroff,
				       (unsigned char *)strings,
				       strsz, &errmsg, err);
	  /* Find gnu_lto_ in strings.  */
	  while ((gnu_lto = (char *) memchr (gnu_lto, 'g',
					     strings + strsz - gnu_lto)))
	    if (strncmp (gnu_lto, "gnu_lto_v1",
			 strings + strsz - gnu_lto) == 0)
	      break;
	    else
	      gnu_lto++;
	  /* Read the section index table if present.  */
	  if (symtab_indices_shndx[i - 1] != 0)
	    {
	      unsigned char *sidxhdr = shdrs + (strtab - 1) * shdr_size;
	      off_t sidxoff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					       sidxhdr, sh_offset, Elf_Addr);
	      size_t sidxsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
					       sidxhdr, sh_size, Elf_Addr);
	      shndx_table = (unsigned *)XNEWVEC (char, sidxsz);
	      simple_object_internal_read (sobj->descriptor,
					   sobj->offset + sidxoff,
					   (unsigned char *)shndx_table,
					   sidxsz, &errmsg, err);
	    }
	  for (ent = buf; ent < buf + length; ent += entsize)
	    {
	      unsigned st_shndx = ELF_FETCH_FIELD (type_functions, ei_class,
						   Sym, ent,
						   st_shndx, Elf_Half);
	      unsigned raw_st_shndx = st_shndx;
	      unsigned char *st_info;
	      unsigned char *st_other;
	      int discard = 0;
	      if (ei_class == ELFCLASS32)
		{
		  st_info = &((Elf32_External_Sym *)ent)->st_info;
		  st_other = &((Elf32_External_Sym *)ent)->st_other;
		}
	      else
		{
		  st_info = &((Elf64_External_Sym *)ent)->st_info;
		  st_other = &((Elf64_External_Sym *)ent)->st_other;
		}
	      if (st_shndx == SHN_XINDEX)
		st_shndx = type_functions->fetch_Elf_Word
		    ((unsigned char *)(shndx_table + (ent - buf) / entsize));
	      /* Eliminate all COMMONs - this includes __gnu_lto_v1
		 and __gnu_lto_slim which otherwise cause endless
		 LTO plugin invocation.  */
	      if (st_shndx == SHN_COMMON)
		discard = 1;
	      /* We also need to remove symbols refering to sections
		 we'll eventually remove as with fat LTO objects
		 we otherwise get duplicate symbols at final link
		 (with GNU ld, gold is fine and ignores symbols in
		 sections marked as EXCLUDE).  ld/20513  */
	      else if (st_shndx != SHN_UNDEF
		       && st_shndx < shnum
		       && pfnret[st_shndx - 1] == -1)
		discard = 1;

	      if (discard)
		{
		  /* Make discarded symbols undefined and unnamed
		     in case it is local.  */
		  int bind = ELF_ST_BIND (*st_info);
		  int other = STV_DEFAULT;
		  if (bind == STB_LOCAL)
		    {
		      /* Make discarded local symbols unnamed and
			 defined in the first prevailing section.  */
		      ELF_SET_FIELD (type_functions, ei_class, Sym,
				     ent, st_name, Elf_Word, 0);
		      ELF_SET_FIELD (type_functions, ei_class, Sym,
				     ent, st_shndx, Elf_Half,
				     sh_map[first_shndx]);
		    }
		  else
		    {
		      /* Make discarded global symbols hidden weak
			 undefined and sharing the gnu_lto_ name.  */
		      bind = STB_WEAK;
		      other = STV_HIDDEN;
		      if (gnu_lto)
			ELF_SET_FIELD (type_functions, ei_class, Sym,
				       ent, st_name, Elf_Word,
				       gnu_lto - strings);
		      ELF_SET_FIELD (type_functions, ei_class, Sym,
				     ent, st_shndx, Elf_Half, SHN_UNDEF);
		    }
		  *st_other = other;
		  *st_info = ELF_ST_INFO (bind, STT_NOTYPE);
		  ELF_SET_FIELD (type_functions, ei_class, Sym,
				 ent, st_value, Elf_Addr, 0);
		  ELF_SET_FIELD (type_functions, ei_class, Sym,
				 ent, st_size, Elf_Word, 0);
		}
	      else if (raw_st_shndx < SHN_LORESERVE
		       || raw_st_shndx == SHN_XINDEX)
		/* Remap the section reference.  */
		ELF_SET_FIELD (type_functions, ei_class, Sym,
			       ent, st_shndx, Elf_Half, sh_map[st_shndx]);
	    }
	  XDELETEVEC (strings);
	  XDELETEVEC (shndx_table);
	}
      else if (sh_type == SHT_GROUP)
	{
	  /* Remap section indices in groups and remove removed members.  */
	  unsigned char *ent, *dst;
	  for (dst = ent = buf + 4; ent < buf + length; ent += 4)
	    {
	      unsigned shndx = type_functions->fetch_Elf_Word (ent);
	      if (pfnret[shndx - 1] == -1)
		;
	      else
		{
		  type_functions->set_Elf_Word (dst, sh_map[shndx]);
		  dst += 4;
		}
	    }
	  /* Adjust the length.  */
	  length = dst - buf;
	}

      errmsg = simple_object_write_add_data (dobj, dest,
					     buf, length, 1, err);
      XDELETEVEC (buf);
      if (errmsg)
	{
	  XDELETEVEC (names);
	  XDELETEVEC (shdrs);
	  XDELETEVEC (symtab_indices_shndx);
	  return errmsg;
	}

      flags = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
			       shdr, sh_flags, Elf_Addr);
      /* Remap the section references.  */
      {
	unsigned int sh_info, sh_link;
	if (flags & SHF_INFO_LINK || sh_type == SHT_REL || sh_type == SHT_RELA)
	  {
	    sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				       shdr, sh_info, Elf_Word);
	    if (sh_info < SHN_LORESERVE
		|| sh_info > SHN_HIRESERVE)
	      sh_info = sh_map[sh_info];
	    ELF_SET_FIELD (type_functions, ei_class, Shdr,
			   shdr, sh_info, Elf_Word, sh_info);
	  }
	sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
				   shdr, sh_link, Elf_Word);
	if (sh_link < SHN_LORESERVE
	    || sh_link > SHN_HIRESERVE)
	  sh_link = sh_map[sh_link];
	ELF_SET_FIELD (type_functions, ei_class, Shdr,
		       shdr, sh_link, Elf_Word, sh_link);
      }
      /* The debugobj doesn't contain any code, thus no trampolines.
	 Even when the original object needs trampolines, debugobj
	 doesn't.  */
      if (strcmp (name, ".note.GNU-stack") == 0)
	flags &= ~SHF_EXECINSTR;
      /* Clear SHF_EXCLUDE on to be preserved sections.  */
      flags &= ~SHF_EXCLUDE;
      ELF_SET_FIELD (type_functions, ei_class, Shdr,
		     shdr, sh_flags, Elf_Addr, flags);
    }

  XDELETEVEC (names);
  XDELETEVEC (shdrs);
  XDELETEVEC (pfnret);
  XDELETEVEC (pfnname);
  XDELETEVEC (symtab_indices_shndx);
  XDELETEVEC (sh_map);

  return NULL;
}


/* The ELF functions.  */

const struct simple_object_functions simple_object_elf_functions =
{
  simple_object_elf_match,
  simple_object_elf_find_sections,
  simple_object_elf_fetch_attributes,
  simple_object_elf_release_read,
  simple_object_elf_attributes_merge,
  simple_object_elf_release_attributes,
  simple_object_elf_start_write,
  simple_object_elf_write_to_file,
  simple_object_elf_release_write,
  simple_object_elf_copy_lto_debug_sections
};