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
|
.\" Copyright (c) 1999 Jeroen Ruigrok van der Werven
.\" 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.
.\"
.\" $FreeBSD$
.\"
.Dd December 18, 2005
.Dt ELF 5
.Os
.Sh NAME
.Nm elf
.Nd format of ELF executable binary files
.Sh SYNOPSIS
.In elf.h
.Sh DESCRIPTION
The header file
.In elf.h
defines the format of ELF executable binary files.
Amongst these files are
normal executable files, relocatable object files, core files and shared
libraries.
.Pp
An executable file using the ELF file format consists of an ELF header,
followed by a program header table or a section header table, or both.
The ELF header is always at offset zero of the file.
The program header
table and the section header table's offset in the file are defined in the
ELF header.
The two tables describe the rest of the particularities of
the file.
.Pp
Applications which wish to process ELF binary files for their native
architecture only should include
.In elf.h
in their source code.
These applications should need to refer to
all the types and structures by their generic names
.Dq Elf_xxx
and to the macros by
.Dq ELF_xxx .
Applications written this way can be compiled on any architecture,
regardless whether the host is 32-bit or 64-bit.
.Pp
Should an application need to process ELF files of an unknown
architecture then the application needs to include both
.In sys/elf32.h
and
.In sys/elf64.h
instead of
.In elf.h .
Furthermore, all types and structures need to be identified by either
.Dq Elf32_xxx
or
.Dq Elf64_xxx .
The macros need to be identified by
.Dq ELF32_xxx
or
.Dq ELF64_xxx .
.Pp
Whatever the system's architecture is, it will always include
.In sys/elf_common.h
as well as
.In sys/elf_generic.h .
.Pp
These header files describe the above mentioned headers as C structures
and also include structures for dynamic sections, relocation sections and
symbol tables.
.Pp
The following types are being used for 32-bit architectures:
.Bd -literal -offset indent
Elf32_Addr Unsigned 32-bit program address
Elf32_Half Unsigned 16-bit field
Elf32_Lword Unsigned 64-bit field
Elf32_Off Unsigned 32-bit file offset
Elf32_Sword Signed 32-bit field or integer
Elf32_Word Unsigned 32-bit field or integer
.Ed
.Pp
For 64-bit architectures we have the following types:
.Bd -literal -offset indent
Elf64_Addr Unsigned 64-bit program address
Elf64_Half Unsigned 16-bit field
Elf64_Lword Unsigned 64-bit field
Elf64_Off Unsigned 64-bit file offset
Elf64_Sword Signed 32-bit field
Elf64_Sxword Signed 64-bit field or integer
Elf64_Word Unsigned 32-bit field
Elf64_Xword Unsigned 64-bit field or integer
.Ed
.Pp
All data structures that the file format defines follow the
.Dq natural
size and alignment guidelines for the relevant class.
If necessary,
data structures contain explicit padding to ensure 4-byte alignment
for 4-byte objects, to force structure sizes to a multiple of 4, etc.
.Pp
The ELF header is described by the type Elf32_Ehdr or Elf64_Ehdr:
.Bd -literal -offset indent
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;
.Ed
.Pp
.Bd -literal -offset indent
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Elf64_Addr e_entry;
Elf64_Off e_phoff;
Elf64_Off e_shoff;
Elf64_Word e_flags;
Elf64_Half e_ehsize;
Elf64_Half e_phentsize;
Elf64_Half e_phnum;
Elf64_Half e_shentsize;
Elf64_Half e_shnum;
Elf64_Half e_shstrndx;
} Elf64_Ehdr;
.Ed
.Pp
The fields have the following meanings:
.Pp
.Bl -tag -width "e_phentsize" -compact -offset indent
.It Dv e_ident
This array of bytes specifies to interpret the file,
independent of the processor or the file's remaining contents.
Within this array everything is named by macros, which start with
the prefix
.Sy EI_
and may contain values which start with the prefix
.Sy ELF .
The following macros are defined:
.Pp
.Bl -tag -width "EI_ABIVERSION" -compact
.It Dv EI_MAG0
The first byte of the magic number.
It must be filled with
.Sy ELFMAG0 .
.It Dv EI_MAG1
The second byte of the magic number.
It must be filled with
.Sy ELFMAG1 .
.It Dv EI_MAG2
The third byte of the magic number.
It must be filled with
.Sy ELFMAG2 .
.It Dv EI_MAG3
The fourth byte of the magic number.
It must be filled with
.Sy ELFMAG3 .
.It Dv EI_CLASS
The fifth byte identifies the architecture for this binary:
.Pp
.Bl -tag -width "ELFCLASSNONE" -compact
.It Dv ELFCLASSNONE
This class is invalid.
.It Dv ELFCLASS32
This defines the 32-bit architecture.
It supports machines with files
and virtual address spaces up to 4 Gigabytes.
.It Dv ELFCLASS64
This defines the 64-bit architecture.
.El
.It Dv EI_DATA
The sixth byte specifies the data encoding of the processor-specific
data in the file.
Currently these encodings are supported:
.Pp
.Bl -tag -width "ELFDATA2LSB" -compact
.It Dv ELFDATANONE
Unknown data format.
.It Dv ELFDATA2LSB
Two's complement, little-endian.
.It Dv ELFDATA2MSB
Two's complement, big-endian.
.El
.It Dv EI_VERSION
The version number of the ELF specification:
.Pp
.Bl -tag -width "EV_CURRENT" -compact
.It Dv EV_NONE
Invalid version.
.It Dv EV_CURRENT
Current version.
.El
.It Dv EI_OSABI
This byte identifies the operating system
and ABI to which the object is targeted.
Some fields in other ELF structures have flags
and values that have platform specific meanings;
the interpretation of those fields is determined by the value of this byte.
The following values are currently defined:
.Pp
.Bl -tag -width "ELFOSABI_STANDALONE" -compact
.It Dv ELFOSABI_SYSV
UNIX System V ABI.
.It Dv ELFOSABI_HPUX
HP-UX operating system ABI.
.It Dv ELFOSABI_NETBSD
.Nx
operating system ABI.
.It Dv ELFOSABI_LINUX
GNU/Linux operating system ABI.
.It Dv ELFOSABI_HURD
GNU/Hurd operating system ABI.
.It Dv ELFOSABI_86OPEN
86Open Common IA32 ABI.
.It Dv ELFOSABI_SOLARIS
Solaris operating system ABI.
.It Dv ELFOSABI_MONTEREY
Monterey project ABI.
.It Dv ELFOSABI_IRIX
IRIX operating system ABI.
.It Dv ELFOSABI_FREEBSD
.Fx
operating system ABI.
.It Dv ELFOSABI_TRU64
TRU64 UNIX operating system ABI.
.It Dv ELFOSABI_ARM
ARM architecture ABI.
.It Dv ELFOSABI_STANDALONE
Standalone (embedded) ABI.
.El
.It Dv EI_ABIVERSION
This byte identifies the version of the ABI
to which the object is targeted.
This field is used to distinguish among incompatible versions of an ABI.
The interpretation of this version number
is dependent on the ABI identified by the EI_OSABI field.
Applications conforming to this specification use the value 0.
.It Dv EI_PAD
Start of padding.
These bytes are reserved and set to zero.
Programs
which read them should ignore them.
The value for EI_PAD will change in
the future if currently unused bytes are given meanings.
.It Dv EI_BRAND
Start of architecture identification.
.It Dv EI_NIDENT
The size of the e_ident array.
.El
.Pp
.It Dv e_type
This member of the structure identifies the object file type:
.Pp
.Bl -tag -width "ET_NONE" -compact
.It Dv ET_NONE
An unknown type.
.It Dv ET_REL
A relocatable file.
.It Dv ET_EXEC
An executable file.
.It Dv ET_DYN
A shared object.
.It Dv ET_CORE
A core file.
.El
.Pp
.It Dv e_machine
This member specifies the required architecture for an individual file:
.Pp
.Bl -tag -width "EM_MIPS_RS4_BE" -compact
.It Dv EM_NONE
An unknown machine.
.It Dv EM_M32
AT&T WE 32100.
.It Dv EM_SPARC
Sun Microsystems SPARC.
.It Dv EM_386
Intel 80386.
.It Dv EM_68K
Motorola 68000.
.It Dv EM_88K
Motorola 88000.
.It Dv EM_486
Intel 80486.
.It Dv EM_860
Intel 80860.
.It Dv EM_MIPS
MIPS RS3000 (big-endian only).
.It Dv EM_MIPS_RS4_BE
MIPS RS4000 (big-endian only).
.It Dv EM_SPARC64
SPARC v9 64-bit unofficial.
.It Dv EM_PARISC
HPPA.
.It Dv EM_PPC
PowerPC.
.It Dv EM_ALPHA
Compaq [DEC] Alpha.
.El
.Pp
.It Dv e_version
This member identifies the file version:
.Pp
.Bl -tag -width "EV_CURRENT" -compact
.It Dv EV_NONE
Invalid version
.It Dv EV_CURRENT
Current version
.El
.It Dv e_entry
This member gives the virtual address to which the system first transfers
control, thus starting the process.
If the file has no associated entry
point, this member holds zero.
.It Dv e_phoff
This member holds the program header table's file offset in bytes.
If
the file has no program header table, this member holds zero.
.It Dv e_shoff
This member holds the section header table's file offset in bytes.
If the
file has no section header table this member holds zero.
.It Dv e_flags
This member holds processor-specific flags associated with the file.
Flag
names take the form EF_`machine_flag'.
Currently no flags have been defined.
.It Dv e_ehsize
This member holds the ELF header's size in bytes.
.It Dv e_phentsize
This member holds the size in bytes of one entry in the file's program header
table; all entries are the same size.
.It Dv e_phnum
This member holds the number of entries in the program header
table.
If the file is using extended program header numbering, then the
.Sy e_phnum
member will contain the value
.Dv PN_XNUM
and the actual number of program header table entries will be stored
in the
.Sy sh_info
member of the section header at index
.Dv SHN_UNDEF .
The product of
.Sy e_phentsize
and the number of program header table entries gives the program
header table's size in bytes.
If a file has no program header,
.Sy e_phnum
holds the value zero.
.It Dv e_shentsize
This member holds a sections header's size in bytes.
A section header is one
entry in the section header table; all entries are the same size.
.It Dv e_shnum
This member holds the number of entries in the section header table.
If the file is using extended section numbering, then the
.Sy e_shnum
member will be zero and the actual section number will be stored in the
.Sy sh_size
member of the section header at index
.Dv SHN_UNDEF .
If a file has no section header table, both the
.Sy e_shnum
and the
.Sy e_shoff
fields of the ELF header will be zero.
The product of
.Sy e_shentsize
and the number of sections in the file gives the section header
table's size in bytes.
.It Dv e_shstrndx
This member holds the section header table index of the entry associated
with the section name string table.
If extended section numbering is being used, this field will hold the
value
.Sy SHN_XINDEX ,
and the actual section header table index will be present in the
.Sy sh_link
field of the section header entry at index
.Dv SHN_UNDEF .
If the file has no section name string
table, this member holds the value
.Sy SHN_UNDEF .
.El
.Pp
An executable or shared object file's program header table is an array of
structures, each describing a segment or other information the system needs
to prepare the program for execution.
An object file
.Em segment
contains one or more
.Em sections .
Program headers are meaningful only for executable and shared object files.
A file specifies its own program header size with the ELF header's
.Sy e_phentsize
and
.Sy e_phnum
members.
As with the Elf executable header, the program header
also has different versions depending on the architecture:
.Pp
.Bd -literal -offset indent
typedef struct {
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} Elf32_Phdr;
.Ed
.Pp
.Bd -literal -offset indent
typedef struct {
Elf64_Word p_type;
Elf64_Word p_flags;
Elf64_Off p_offset;
Elf64_Addr p_vaddr;
Elf64_Addr p_paddr;
Elf64_Xword p_filesz;
Elf64_Xword p_memsz;
Elf64_Xword p_align;
} Elf64_Phdr;
.Ed
.Pp
The main difference between the 32-bit and the 64-bit program header lies
only in the location of a
.Sy p_flags
member in the total struct.
.Pp
.Bl -tag -width "p_offset" -compact -offset indent
.It Dv p_type
This member of the Phdr struct tells what kind of segment this array
element describes or how to interpret the array element's information.
.Bl -tag -width "PT_DYNAMIC" -compact
.Pp
.It Dv PT_NULL
The array element is unused and the other members' values are undefined.
This lets the program header have ignored entries.
.It Dv PT_LOAD
The array element specifies a loadable segment, described by
.Sy p_filesz
and
.Sy p_memsz .
The bytes from the file are mapped to the beginning of the memory
segment.
If the segment's memory size
.Pq Sy p_memsz
is larger than the file size
.Pq Sy p_filesz ,
the
.Dq extra
bytes are defined to hold the value 0 and to follow the segment's
initialized area.
The file size may not be larger than the memory size.
Loadable segment entries in the program header table appear in ascending
order, sorted on the
.Sy p_vaddr
member.
.It Dv PT_DYNAMIC
The array element specifies dynamic linking information.
.It Dv PT_INTERP
The array element specifies the location and size of a null-terminated
path name to invoke as an interpreter.
This segment type is meaningful
only for executable files (though it may occur for shared objects).
However
it may not occur more than once in a file.
If it is present it must precede
any loadable segment entry.
.It Dv PT_NOTE
The array element specifies the location and size for auxiliary information.
.It Dv PT_SHLIB
This segment type is reserved but has unspecified semantics.
Programs that
contain an array element of this type do not conform to the ABI.
.It Dv PT_PHDR
The array element, if present, specifies the location and size of the program
header table itself, both in the file and in the memory image of the program.
This segment type may not occur more than once in a file.
Moreover, it may
only occur if the program header table is part of the memory image of the
program.
If it is present it must precede any loadable segment entry.
.It Dv PT_LOPROC
This value up to and including
.Sy PT_HIPROC
are reserved for processor-specific semantics.
.It Dv PT_HIPROC
This value down to and including
.Sy PT_LOPROC
are reserved for processor-specific semantics.
.El
.Pp
.It Dv p_offset
This member holds the offset from the beginning of the file at which
the first byte of the segment resides.
.It Dv p_vaddr
This member holds the virtual address at which the first byte of the
segment resides in memory.
.It Dv p_paddr
On systems for which physical addressing is relevant, this member is
reserved for the segment's physical address.
Under
.Bx
this member is
not used and must be zero.
.It Dv p_filesz
This member holds the number of bytes in the file image of the segment.
It may be zero.
.It Dv p_memsz
This member holds the number of bytes in the memory image of the segment.
It may be zero.
.It Dv p_flags
This member holds flags relevant to the segment:
.Pp
.Bl -tag -width "PF_X" -compact
.It Dv PF_X
An executable segment.
.It Dv PF_W
A writable segment.
.It Dv PF_R
A readable segment.
.El
.Pp
A text segment commonly has the flags
.Sy PF_X
and
.Sy PF_R .
A data segment commonly has
.Sy PF_X ,
.Sy PF_W
and
.Sy PF_R .
.It Dv p_align
This member holds the value to which the segments are aligned in memory
and in the file.
Loadable process segments must have congruent values for
.Sy p_vaddr
and
.Sy p_offset ,
modulo the page size.
Values of zero and one mean no alignment is required.
Otherwise,
.Sy p_align
should be a positive, integral power of two, and
.Sy p_vaddr
should equal
.Sy p_offset ,
modulo
.Sy p_align .
.El
.Pp
An file's section header table lets one locate all the file's sections.
The
section header table is an array of Elf32_Shdr or Elf64_Shdr structures.
The
ELF header's
.Sy e_shoff
member gives the byte offset from the beginning of the file to the section
header table.
.Sy e_shnum
holds the number of entries the section header table contains.
.Sy e_shentsize
holds the size in bytes of each entry.
.Pp
A section header table index is a subscript into this array.
Some section
header table indices are reserved.
An object file does not have sections for
these special indices:
.Pp
.Bl -tag -width "SHN_LORESERVE" -compact
.It Dv SHN_UNDEF
This value marks an undefined, missing, irrelevant, or otherwise meaningless
section reference.
For example, a symbol
.Dq defined
relative to section number
.Sy SHN_UNDEF
is an undefined symbol.
.It Dv SHN_LORESERVE
This value specifies the lower bound of the range of reserved indices.
.It Dv SHN_LOPROC
This value up to and including
.Sy SHN_HIPROC
are reserved for processor-specific semantics.
.It Dv SHN_HIPROC
This value down to and including
.Sy SHN_LOPROC
are reserved for processor-specific semantics.
.It Dv SHN_ABS
This value specifies absolute values for the corresponding reference.
For
example, symbols defined relative to section number
.Sy SHN_ABS
have absolute values and are not affected by relocation.
.It Dv SHN_COMMON
Symbols defined relative to this section are common symbols, such as FORTRAN
COMMON or unallocated C external variables.
.It Dv SHN_HIRESERVE
This value specifies the upper bound of the range of reserved indices.
The
system reserves indices between
.Sy SHN_LORESERVE
and
.Sy SHN_HIRESERVE ,
inclusive.
The section header table does not contain entries for the
reserved indices.
.El
.Pp
The section header has the following structure:
.Bd -literal -offset indent
typedef struct {
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
.Ed
.Pp
.Bd -literal -offset indent
typedef struct {
Elf64_Word sh_name;
Elf64_Word sh_type;
Elf64_Xword sh_flags;
Elf64_Addr sh_addr;
Elf64_Off sh_offset;
Elf64_Xword sh_size;
Elf64_Word sh_link;
Elf64_Word sh_info;
Elf64_Xword sh_addralign;
Elf64_Xword sh_entsize;
} Elf64_Shdr;
.Ed
.Pp
.Bl -tag -width "sh_addralign" -compact
.It Dv sh_name
This member specifies the name of the section.
Its value is an index
into the section header string table section, giving the location of
a null-terminated string.
.It Dv sh_type
This member categorizes the section's contents and semantics.
.Pp
.Bl -tag -width "SHT_PROGBITS" -compact
.It Dv SHT_NULL
This value marks the section header as inactive.
It does not
have an associated section.
Other members of the section header
have undefined values.
.It Dv SHT_PROGBITS
The section holds information defined by the program, whose
format and meaning are determined solely by the program.
.It Dv SHT_SYMTAB
This section holds a symbol table.
Typically,
.Sy SHT_SYMTAB
provides symbols for link editing, though it may also be used
for dynamic linking.
As a complete symbol table, it may contain
many symbols unnecessary for dynamic linking.
An object file can
also contain a
.Sy SHN_DYNSYM
section.
.It Dv SHT_STRTAB
This section holds a string table.
An object file may have multiple
string table sections.
.It Dv SHT_RELA
This section holds relocation entries with explicit addends, such
as type
.Sy Elf32_Rela
for the 32-bit class of object files.
An object may have multiple
relocation sections.
.It Dv SHT_HASH
This section holds a symbol hash table.
All object participating in
dynamic linking must contain a symbol hash table.
An object file may
have only one hash table.
.It Dv SHT_DYNAMIC
This section holds information for dynamic linking.
An object file may
have only one dynamic section.
.It Dv SHT_NOTE
This section holds information that marks the file in some way.
.It Dv SHT_NOBITS
A section of this type occupies no space in the file but otherwise
resembles
.Sy SHN_PROGBITS .
Although this section contains no bytes, the
.Sy sh_offset
member contains the conceptual file offset.
.It Dv SHT_REL
This section holds relocation offsets without explicit addends, such
as type
.Sy Elf32_Rel
for the 32-bit class of object files.
An object file may have multiple
relocation sections.
.It Dv SHT_SHLIB
This section is reserved but has unspecified semantics.
.It Dv SHT_DYNSYM
This section holds a minimal set of dynamic linking symbols.
An
object file can also contain a
.Sy SHN_SYMTAB
section.
.It Dv SHT_LOPROC
This value up to and including
.Sy SHT_HIPROC
are reserved for processor-specific semantics.
.It Dv SHT_HIPROC
This value down to and including
.Sy SHT_LOPROC
are reserved for processor-specific semantics.
.It Dv SHT_LOUSER
This value specifies the lower bound of the range of indices reserved for
application programs.
.It Dv SHT_HIUSER
This value specifies the upper bound of the range of indices reserved for
application programs.
Section types between
.Sy SHT_LOUSER
and
.Sy SHT_HIUSER
may be used by the application, without conflicting with current or future
system-defined section types.
.El
.Pp
.It Dv sh_flags
Sections support one-bit flags that describe miscellaneous attributes.
If a flag bit is set in
.Sy sh_flags ,
the attribute is
.Dq on
for the section.
Otherwise, the attribute is
.Dq off
or does not apply.
Undefined attributes are set to zero.
.Pp
.Bl -tag -width "SHF_EXECINSTR" -compact
.It Dv SHF_WRITE
This section contains data that should be writable during process
execution.
.It Dv SHF_ALLOC
The section occupies memory during process execution.
Some control
sections do not reside in the memory image of an object file.
This
attribute is off for those sections.
.It Dv SHF_EXECINSTR
The section contains executable machine instructions.
.It Dv SHF_MASKPROC
All bits included in this mask are reserved for processor-specific
semantics.
.El
.Pp
.It Dv sh_addr
If the section will appear in the memory image of a process, this member
holds the address at which the section's first byte should reside.
Otherwise, the member contains zero.
.It Dv sh_offset
This member's value holds the byte offset from the beginning of the file
to the first byte in the section.
One section type,
.Sy SHT_NOBITS ,
occupies no space in the file, and its
.Sy sh_offset
member locates the conceptual placement in the file.
.It Dv sh_size
This member holds the section's size in bytes.
Unless the section type
is
.Sy SHT_NOBITS ,
the section occupies
.Sy sh_size
bytes in the file.
A section of type
.Sy SHT_NOBITS
may have a non-zero size, but it occupies no space in the file.
.It Dv sh_link
This member holds a section header table index link, whose interpretation
depends on the section type.
.It Dv sh_info
This member holds extra information, whose interpretation depends on the
section type.
.It Dv sh_addralign
Some sections have address alignment constraints.
If a section holds a
doubleword, the system must ensure doubleword alignment for the entire
section.
That is, the value of
.Sy sh_addr
must be congruent to zero, modulo the value of
.Sy sh_addralign .
Only zero and positive integral powers of two are allowed.
Values of zero
or one mean the section has no alignment constraints.
.It Dv sh_entsize
Some sections hold a table of fixed-sized entries, such as a symbol table.
For such a section, this member gives the size in bytes for each entry.
This member contains zero if the section does not hold a table of
fixed-size entries.
.El
.Pp
Various sections hold program and control information:
.Bl -tag -width ".shstrtab" -compact
.It .bss
(Block Started by Symbol)
This section holds uninitialized data that contributes to the program's
memory image.
By definition, the system initializes the data with zeros
when the program begins to run.
This section is of type
.Sy SHT_NOBITS .
The attributes types are
.Sy SHF_ALLOC
and
.Sy SHF_WRITE .
.It .comment
This section holds version control information.
This section is of type
.Sy SHT_PROGBITS .
No attribute types are used.
.It .data
This section holds initialized data that contribute to the program's
memory image.
This section is of type
.Sy SHT_PROGBITS .
The attribute types are
.Sy SHF_ALLOC
and
.Sy SHF_WRITE .
.It .data1
This section holds initialized data that contribute to the program's
memory image.
This section is of type
.Sy SHT_PROGBITS .
The attribute types are
.Sy SHF_ALLOC
and
.Sy SHF_WRITE .
.It .debug
This section holds information for symbolic debugging.
The contents
are unspecified.
This section is of type
.Sy SHT_PROGBITS .
No attribute types are used.
.It .dynamic
This section holds dynamic linking information.
The section's attributes
will include the
.Sy SHF_ALLOC
bit.
Whether the
.Sy SHF_WRITE
bit is set is processor-specific.
This section is of type
.Sy SHT_DYNAMIC .
See the attributes above.
.It .dynstr
This section holds strings needed for dynamic linking, most commonly
the strings that represent the names associated with symbol table entries.
This section is of type
.Sy SHT_STRTAB .
The attribute type used is
.Sy SHF_ALLOC .
.It .dynsym
This section holds the dynamic linking symbol table.
This section is of type
.Sy SHT_DYNSYM .
The attribute used is
.Sy SHF_ALLOC .
.It .fini
This section holds executable instructions that contribute to the process
termination code.
When a program exits normally the system arranges to
execute the code in this section.
This section is of type
.Sy SHT_PROGBITS .
The attributes used are
.Sy SHF_ALLOC
and
.Sy SHF_EXECINSTR .
.It .got
This section holds the global offset table.
This section is of type
.Sy SHT_PROGBITS .
The attributes are processor-specific.
.It .hash
This section holds a symbol hash table.
This section is of type
.Sy SHT_HASH .
The attribute used is
.Sy SHF_ALLOC .
.It .init
This section holds executable instructions that contribute to the process
initialization code.
When a program starts to run the system arranges to
execute the code in this section before calling the main program entry point.
This section is of type
.Sy SHT_PROGBITS .
The attributes used are
.Sy SHF_ALLOC
and
.Sy SHF_EXECINSTR .
.It .interp
This section holds the pathname of a program interpreter.
If the file has
a loadable segment that includes the section, the section's attributes will
include the
.Sy SHF_ALLOC
bit.
Otherwise, that bit will be off.
This section is of type
.Sy SHT_PROGBITS .
.It .line
This section holds line number information for symbolic debugging, which
describes the correspondence between the program source and the machine code.
The contents are unspecified.
This section is of type
.Sy SHT_PROGBITS .
No attribute types are used.
.It .note
This section holds information in the
.Dq Note Section
format described below.
This section is of type
.Sy SHT_NOTE .
No attribute types are used.
.It .plt
This section holds the procedure linkage table.
This section is of type
.Sy SHT_PROGBITS .
The attributes are processor-specific.
.It .relNAME
This section holds relocation information as described below.
If the file
has a loadable segment that includes relocation, the section's attributes
will include the
.Sy SHF_ALLOC
bit.
Otherwise the bit will be off.
By convention,
.Dq NAME
is supplied by the section to which the relocations apply.
Thus a relocation
section for
.Sy .text
normally would have the name
.Sy .rel.text .
This section is of type
.Sy SHT_REL .
.It .relaNAME
This section holds relocation information as described below.
If the file
has a loadable segment that includes relocation, the section's attributes
will include the
.Sy SHF_ALLOC
bit.
Otherwise the bit will be off.
By convention,
.Dq NAME
is supplied by the section to which the relocations apply.
Thus a relocation
section for
.Sy .text
normally would have the name
.Sy .rela.text .
This section is of type
.Sy SHT_RELA .
.It .rodata
This section holds read-only data that typically contributes to a
non-writable segment in the process image.
This section is of type
.Sy SHT_PROGBITS .
The attribute used is
.Sy SHF_ALLOC .
.It .rodata1
This section hold read-only data that typically contributes to a
non-writable segment in the process image.
This section is of type
.Sy SHT_PROGBITS .
The attribute used is
.Sy SHF_ALLOC .
.It .shstrtab
This section holds section names.
This section is of type
.Sy SHT_STRTAB .
No attribute types are used.
.It .strtab
This section holds strings, most commonly the strings that represent the
names associated with symbol table entries.
If the file has a loadable
segment that includes the symbol string table, the section's attributes
will include the
.Sy SHF_ALLOC
bit.
Otherwise the bit will be off.
This section is of type
.Sy SHT_STRTAB .
.It .symtab
This section holds a symbol table.
If the file has a loadable segment
that includes the symbol table, the section's attributes will include
the
.Sy SHF_ALLOC
bit.
Otherwise the bit will be off.
This section is of type
.Sy SHT_SYMTAB .
.It .text
This section holds the
.Dq text ,
or executable instructions, of a program.
This section is of type
.Sy SHT_PROGBITS .
The attributes used are
.Sy SHF_ALLOC
and
.Sy SHF_EXECINSTR .
.It .jcr
This section holds information about Java classes that must
be registered.
.It .eh_frame
This section holds information used for C++ exception-handling.
.El
.Pp
String table sections hold null-terminated character sequences, commonly
called strings.
The object file uses these strings to represent symbol
and section names.
One references a string as an index into the string
table section.
The first byte, which is index zero, is defined to hold
a null character.
Similarly, a string table's last byte is defined to
hold a null character, ensuring null termination for all strings.
.Pp
An object file's symbol table holds information needed to locate and
relocate a program's symbolic definitions and references.
A symbol table
index is a subscript into this array.
.Pp
.Bd -literal -offset indent
typedef struct {
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
} Elf32_Sym;
.Ed
.Pp
.Bd -literal -offset indent
typedef struct {
Elf64_Word st_name;
unsigned char st_info;
unsigned char st_other;
Elf64_Half st_shndx;
Elf64_Addr st_value;
Elf64_Xword st_size;
} Elf64_Sym;
.Ed
.Pp
.Bl -tag -width "st_value" -compact
.It Dv st_name
This member holds an index into the object file's symbol string table,
which holds character representations of the symbol names.
If the value
is non-zero, it represents a string table index that gives the symbol
name.
Otherwise, the symbol table has no name.
.It Dv st_value
This member gives the value of the associated symbol.
.It Dv st_size
Many symbols have associated sizes.
This member holds zero if the symbol
has no size or an unknown size.
.It Dv st_info
This member specifies the symbol's type and binding attributes:
.Pp
.Bl -tag -width "STT_SECTION" -compact
.It Dv STT_NOTYPE
The symbol's type is not defined.
.It Dv STT_OBJECT
The symbol is associated with a data object.
.It Dv STT_FUNC
The symbol is associated with a function or other executable code.
.It Dv STT_SECTION
The symbol is associated with a section.
Symbol table entries of
this type exist primarily for relocation and normally have
.Sy STB_LOCAL
bindings.
.It Dv STT_FILE
By convention the symbol's name gives the name of the source file
associated with the object file.
A file symbol has
.Sy STB_LOCAL
bindings, its section index is
.Sy SHN_ABS ,
and it precedes the other
.Sy STB_LOCAL
symbols of the file, if it is present.
.It Dv STT_LOPROC
This value up to and including
.Sy STT_HIPROC
are reserved for processor-specific semantics.
.It Dv STT_HIPROC
This value down to and including
.Sy STT_LOPROC
are reserved for processor-specific semantics.
.El
.Pp
.Bl -tag -width "STB_GLOBAL" -compact
.It Dv STB_LOCAL
Local symbols are not visible outside the object file containing their
definition.
Local symbols of the same name may exist in multiple file
without interfering with each other.
.It Dv STB_GLOBAL
Global symbols are visible to all object files being combined.
One file's
definition of a global symbol will satisfy another file's undefined
reference to the same symbol.
.It Dv STB_WEAK
Weak symbols resemble global symbols, but their definitions have lower
precedence.
.It Dv STB_LOPROC
This value up to and including
.Sy STB_HIPROC
are reserved for processor-specific semantics.
.It Dv STB_HIPROC
This value down to and including
.Sy STB_LOPROC
are reserved for processor-specific semantics.
.Pp
There are macros for packing and unpacking the binding and type fields:
.Pp
.Bl -tag -width "ELF32_ST_INFO(bind, type)" -compact
.It Xo
.Fn ELF32_ST_BIND info
.Xc
or
.Fn ELF64_ST_BIND info
extract a binding from an st_info value.
.It Xo
.Fn ELF64_ST_TYPE info
.Xc
or
.Fn ELF32_ST_TYPE info
extract a type from an st_info value.
.It Xo
.Fn ELF32_ST_INFO bind type
.Xc
or
.Fn ELF64_ST_INFO bind type
convert a binding and a type into an st_info value.
.El
.El
.Pp
.It Dv st_other
This member currently holds zero and has no defined meaning.
.It Dv st_shndx
Every symbol table entry is
.Dq defined
in relation to some section.
This member holds the relevant section
header table index.
.El
.Pp
Relocation is the process of connecting symbolic references with
symbolic definitions.
Relocatable files must have information that
describes how to modify their section contents, thus allowing executable
and shared object files to hold the right information for a process'
program image.
Relocation entries are these data.
.Pp
Relocation structures that do not need an addend:
.Pp
.Bd -literal -offset indent
typedef struct {
Elf32_Addr r_offset;
Elf32_Word r_info;
} Elf32_Rel;
.Ed
.Bd -literal -offset indent
typedef struct {
Elf64_Addr r_offset;
Elf64_Xword r_info;
} Elf64_Rel;
.Ed
.Pp
Relocation structures that need an addend:
.Pp
.Bd -literal -offset indent
typedef struct {
Elf32_Addr r_offset;
Elf32_Word r_info;
Elf32_Sword r_addend;
} Elf32_Rela;
.Ed
.Bd -literal -offset indent
typedef struct {
Elf64_Addr r_offset;
Elf64_Xword r_info;
Elf64_Sxword r_addend;
} Elf64_Rela;
.Ed
.Pp
.Bl -tag -width "r_offset" -compact
.It Dv r_offset
This member gives the location at which to apply the relocation action.
For a relocatable file, the value is the byte offset from the beginning
of the section to the storage unit affected by the relocation.
For an
executable file or shared object, the value is the virtual address of
the storage unit affected by the relocation.
.It Dv r_info
This member gives both the symbol table index with respect to which the
relocation must be made and the type of relocation to apply.
Relocation
types are processor-specific.
When the text refers to a relocation
entry's relocation type or symbol table index, it means the result of
applying
.Sy ELF_[32|64]_R_TYPE
or
.Sy ELF[32|64]_R_SYM ,
respectively to the entry's
.Sy r_info
member.
.It Dv r_addend
This member specifies a constant addend used to compute the value to be
stored into the relocatable field.
.El
.Sh SEE ALSO
.Xr as 1 ,
.Xr gdb 1 ,
.Xr ld 1 ,
.Xr objdump 1 ,
.Xr execve 2 ,
.Xr ar 5 ,
.Xr core 5
.Rs
.%A Hewlett Packard
.%B Elf-64 Object File Format
.Re
.Rs
.%A Santa Cruz Operation
.%B System V Application Binary Interface
.Re
.Rs
.%A Unix System Laboratories
.%T Object Files
.%B "Executable and Linking Format (ELF)"
.Re
.Sh HISTORY
The ELF header files made their appearance in
.Fx 2.2.6 .
ELF in itself first appeared in
.At V .
The ELF format is an adopted standard.
.Sh AUTHORS
This manual page was written by
.An Jeroen Ruigrok van der Werven
.Aq asmodai@FreeBSD.org
with inspiration from BSDi's
.Bsx
.Xr elf 5
manpage.
|