-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf.h
1671 lines (1145 loc) · 41.4 KB
/
elf.h
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
/*++
Copyright (c) 2012 Minoca Corp.
This file is licensed under the terms of the GNU General Public License
version 3. Alternative licensing terms are available. Contact
[email protected] for details. See the LICENSE file at the root of this
project for complete licensing information.
Module Name:
elf.h
Abstract:
This header contains definitions for the ELF file format.
Author:
Evan Green 13-Oct-2012
--*/
//
// ------------------------------------------------------------------- Includes
//
//
// --------------------------------------------------------------------- Macros
//
//
// These macros manipulate the bind and type information from a symbol's
// information field.
//
#define ELF_GET_SYMBOL_BIND(_Information) ((_Information) >> 4)
#define ELF_GET_SYMBOL_TYPE(_Information) ((_Information) & 0xF)
#define ELF_SYMBOL_INFORMATION(_Bind, _Type) \
(((_Bind) << 4) + ((_Type) & 0x0F))
//
// These macros manipulate the symbol and type fields in a relocation entry's
// information in 32-bit ELF images.
//
#define ELF32_GET_RELOCATION_SYMBOL(_Information) ((_Information) >> 8)
#define ELF32_GET_RELOCATION_TYPE(_Information) ((_Information) & 0xFF)
#define ELF32_RELOCATION_INFORMATION(_Symbol, _Type) \
(((_Symbol) << 8) + ((_Type) & 0xFF))
//
// These macros manipulate the symbol and type fields in a relocation entry's
// information in 64-bit ELF images.
//
#define ELF64_GET_RELOCATION_SYMBOL(_Information) ((_Information) >> 32)
#define ELF64_GET_RELOCATION_TYPE(_Information) ((_Information) & 0xFFFFFFFF)
#define ELF64_RELOCATION_INFORMATION(_Symbol, _Type) \
(((_Symbol) << 32) + ((_Type) & 0xFFFFFFFF))
//
// ---------------------------------------------------------------- Definitions
//
#define ELF32_IDENTIFICATION_LENGTH 16
#define ELF64_IDENTIFICATION_LENGTH 16
#define ELF_MAGIC0 0x7F
#define ELF_MAGIC1 'E'
#define ELF_MAGIC2 'L'
#define ELF_MAGIC3 'F'
#define ELF_MAGIC 0x464C457F
#define ELF_CLASS_OFFSET 4
#define ELF_ENDIANNESS_OFFSET 5
#define ELF_VERSION_OFFSET 6
#define ELF_OS_ABI_OFFSET 7
#define ELF_ABI_VERSION_OFFSET 8
#define ELF_IMAGE_RELOCATABLE 1
#define ELF_IMAGE_EXECUTABLE 2
#define ELF_IMAGE_SHARED_OBJECT 3
#define ELF_IMAGE_CORE 4
#define ELF_MACHINE_I386 3
#define ELF_MACHINE_ARM 40
#define ELF_MACHINE_X86_64 62
#define ELF_MACHINE_AARCH64 183
#define ELF_LITTLE_ENDIAN 0x1
#define ELF_BIG_ENDIAN 0x2
#define ELF_32BIT 0x1
#define ELF_64BIT 0x2
#define ELF_SECTION_TYPE_NULL 0
#define ELF_SECTION_TYPE_PROGRAM 1
#define ELF_SECTION_TYPE_SYMBOLS 2
#define ELF_SECTION_TYPE_STRINGS 3
#define ELF_SECTION_TYPE_RELOCATIONS_ADDENDS 4
#define ELF_SECTION_TYPE_HASH_TABLE 5
#define ELF_SECTION_TYPE_DYNAMIC_LINK 6
#define ELF_SECTION_TYPE_NOTE 7
#define ELF_SECTION_TYPE_NO_BITS 8
#define ELF_SECTION_TYPE_RELOCATIONS_NO_ADDENDS 9
#define ELF_SECTION_TYPE_SHLIB 10
#define ELF_SECTION_TYPE_DYNAMIC_SYMBOLS 11
#define ELF_SECTION_TYPE_OS_LOW 0x60000000
#define ELF_SECTION_TYPE_OS_HIGH 0x6FFFFFFF
#define ELF_SECTION_TYPE_PROCESSOR_LOW 0x70000000
#define ELF_SECTION_TYPE_PROCESSOR_HIGH 0x7FFFFFFF
#define ELF_SECTION_TYPE_USER_LOW 0x80000000
#define ELF_SECTION_TYPE_USER_HIGH 0x8FFFFFFF
#define ELF_SECTION_UNDEFINED 0
#define ELF_SECTION_RESERVED_LOW 0xFF00
#define ELF_SECTION_ABSOLUTE 0xFFF1
#define ELF_SECTION_COMMON 0xFFF2
#define ELF_SECTION_RESERVED_HIGH 0xFFFF
#define ELF_SECTION_FLAG_WRITABLE 0x1
#define ELF_SECTION_FLAG_LOAD 0x2
#define ELF_SECTION_FLAG_EXECUTABLE 0x4
#define ELF_SECTION_FLAG_OS_MASK 0x0F000000
#define ELF_SECTION_FLAG_PROCESSOR_MASK 0xF0000000
#define ELF_SEGMENT_TYPE_NULL 0
#define ELF_SEGMENT_TYPE_LOAD 1
#define ELF_SEGMENT_TYPE_DYNAMIC 2
#define ELF_SEGMENT_TYPE_INTERPRETER 3
#define ELF_SEGMENT_TYPE_NOTE 4
#define ELF_SEGMENT_TYPE_SHLIB 5
#define ELF_SEGMENT_TYPE_PROGRAM_HEADER 6
#define ELF_SEGMENT_TYPE_TLS 7
#define ELF_SEGMENT_PROCESSOR_LOW 0x70000000
#define ELF_SEGMENT_PROCESSOR_HIGH 0x7FFFFFFF
#define ELF_PROGRAM_HEADER_FLAG_EXECUTE 0x00000001
#define ELF_PROGRAM_HEADER_FLAG_WRITE 0x00000002
#define ELF_PROGRAM_HEADER_FLAG_READ 0x00000004
#define ELF_DYNAMIC_NULL 0
#define ELF_DYNAMIC_NEEDED 1
#define ELF_DYNAMIC_PLT_REL_SIZE 2
#define ELF_DYNAMIC_PLT_GOT 3
#define ELF_DYNAMIC_HASH_TABLE 4
#define ELF_DYNAMIC_STRING_TABLE 5
#define ELF_DYNAMIC_SYMBOL_TABLE 6
#define ELF_DYNAMIC_RELA_TABLE 7
#define ELF_DYNAMIC_RELA_TABLE_SIZE 8
#define ELF_DYNAMIC_RELA_ENTRY_SIZE 9
#define ELF_DYNAMIC_STRING_TABLE_SIZE 10
#define ELF_DYNAMIC_SYMBOL_ENTRY_SIZE 11
#define ELF_DYNAMIC_INIT 12
#define ELF_DYNAMIC_FINI 13
#define ELF_DYNAMIC_LIBRARY_NAME 14
#define ELF_DYNAMIC_RPATH 15
#define ELF_DYNAMIC_SYMBOLIC 16
#define ELF_DYNAMIC_REL_TABLE 17
#define ELF_DYNAMIC_REL_TABLE_SIZE 18
#define ELF_DYNAMIC_REL_ENTRY_SIZE 19
#define ELF_DYNAMIC_PLT_RELOCATION_TYPE 20
#define ELF_DYNAMIC_DEBUG 21
#define ELF_DYNAMIC_TEXT_RELOCATIONS 22
#define ELF_DYNAMIC_JUMP_RELOCATIONS 23
#define ELF_DYNAMIC_BIND_NOW 24
#define ELF_DYNAMIC_INIT_ARRAY 25
#define ELF_DYNAMIC_FINI_ARRAY 26
#define ELF_DYNAMIC_INIT_ARRAY_SIZE 27
#define ELF_DYNAMIC_FINI_ARRAY_SIZE 28
#define ELF_DYNAMIC_RUN_PATH 29
#define ELF_DYNAMIC_FLAGS 30
#define ELF_DYNAMIC_PREINIT_ARRAY 32
#define ELF_DYNAMIC_PREINIT_ARRAY_SIZE 33
#define ELF_DYNAMIC_GNU_HASH_TABLE 0x6FFFFEF5
#define ELF_DYNAMIC_PROCESSOR_LOW 0x70000000
#define ELF_DYNAMIC_PROCESSOR_HIGH 0x7FFFFFFF
#define ELF32_WORD_SIZE_SHIFT 5
#define ELF32_WORD_SIZE_MASK ((1 << ELF32_WORD_SIZE_SHIFT) - 1)
#define ELF64_WORD_SIZE_SHIFT 6
#define ELF64_WORD_SIZE_MASK ((1 << ELF64_WORD_SIZE_SHIFT) - 1)
//
// Define ELF dynamic flags.
//
//
// This flag indicates that the $ORIGIN subsitution string may be used.
//
#define ELF_DYNAMIC_FLAG_ORIGIN 0x01
//
// This flag indicates that symbol searches should start from the image itself,
// then start from the executable if the symbol was not found in this image.
//
#define ELF_DYNAMIC_FLAG_SYMBOLIC 0x02
//
// This flag indicates that one or more relocations might require modifying a
// non-writable segment.
//
#define ELF_DYNAMIC_FLAG_TEXT_RELOCATIONS 0x04
//
// This flag indicates that the dynamic linker should process all relocations
// for this object before transferring control to the program. The presence of
// this flag takes precedence over the lazy flag passed to the dynamic load
// routine.
//
#define ELF_DYNAMIC_FLAG_BIND_NOW 0x08
//
// This flag indicates that the image uses the static TLS model, and attempts
// to load this file dynamically should be blocked.
//
#define ELF_DYNAMIC_FLAG_STATIC_TLS 0x10
//
// ------------------------------------------------------ Data Type Definitions
//
typedef ULONG ELF32_ADDR, *PELF32_ADDR;
typedef USHORT ELF32_HALF, *PELF32_HALF;
typedef ULONG ELF32_OFF, *PELF32_OFF;
typedef LONG ELF32_SWORD, *PELF32_SWORD;
typedef ULONG ELF32_WORD, *PELF32_WORD;
typedef ULONGLONG ELF64_ADDR, *PELF64_ADDR;
typedef ULONGLONG ELF64_OFF, *PELF64_OFF;
typedef USHORT ELF64_HALF, *PELF64_HALF;
typedef ULONG ELF64_WORD, *PELF64_WORD;
typedef LONG ELF64_SWORD, *PELF64_SWORD;
typedef ULONGLONG ELF64_XWORD, *PELF64_XWORD;
typedef LONGLONG ELF64_SXWORD, *PELF64_SXWORD;
typedef enum _ELF_SYMBOL_BIND_TYPE {
ElfBindLocal = 0,
ElfBindGlobal = 1,
ElfBindWeak = 2,
} ELF_SYMBOL_BIND_TYPE, *PELF_SYMBOL_BIND_TYPE;
typedef enum _ELF_SYMBOL_TYPE {
ElfSymbolNone = 0,
ElfSymbolObject = 1,
ElfSymbolFunction = 2,
ElfSymbolSection = 3,
ElfSymbolFile = 4,
ElfSymbolCommon = 5,
ElfSymbolTls = 6,
} ELF_SYMBOL_TYPE, *PELF_SYMBOL_TYPE;
typedef enum _ELF_386_RELOCATION_TYPE {
Elf386RelocationNone = 0,
Elf386Relocation32 = 1,
Elf386RelocationPc32 = 2,
Elf386RelocationGot32 = 3,
Elf386RelocationPlt32 = 4,
Elf386RelocationCopy = 5,
Elf386RelocationGlobalData = 6,
Elf386RelocationJumpSlot = 7,
Elf386RelocationRelative = 8,
Elf386RelocationGotOffset = 9,
Elf386RelocationGotPc = 10,
Elf386RelocationTlsTpOff = 14,
Elf386RelocationTlsDtpMod32 = 35,
Elf386RelocationTlsDtpOff32 = 36,
Elf386RelocationTlsTpOff32 = 37,
} ELF_386_RELOCATION_TYPE, *PELF_386_RELOCATION_TYPE;
typedef enum _ELF_X86_64_RELOCATION_TYPE {
ElfX64RelocationNone = 0,
ElfX64Relocation64 = 1,
ElfX64RelocationPc32 = 2,
ElfX64RelocationGot32 = 3,
ElfX64RelocationPlt32 = 4,
ElfX64RelocationCopy = 5,
ElfX64RelocationGlobalData = 6,
ElfX64RelocationJumpSlot = 7,
ElfX64RelocationRelative = 8,
ElfX64RelocationGotPcRelative = 9,
ElfX64Relocation32 = 10,
ElfX64Relocation32S = 11,
ElfX64Relocation16 = 12,
ElfX64RelocationPc16 = 13,
ElfX64Relocation8 = 14,
ElfX64RelocationPc8 = 15,
ElfX64RelocationDtpMod64 = 16,
ElfX64RelocationDtpOff64 = 17,
ElfX64RelocationTpOff64 = 18,
ElfX64RelocationTlsGd = 19,
ElfX64RelocationTlsLd = 20,
ElfX64RelocationDtpOff32 = 21,
ElfX64RelocationGotTpOff = 22,
ElfX64RelocationTpOff32 = 23,
ElfX64RelocationPc64 = 24,
ElfX64RelocationGotOff64 = 25,
ElfX64RelocationGotPc32 = 26,
ElfX64RelocationSize32 = 32,
ElfX64RelocationSize64 = 33,
ElfX64RelocationGotPc32TlsDesc = 34,
ElfX64RelocationTlsDescCall = 35,
ElfX64RelocationTlsDesc = 36,
ElfX64RelocationIRelative = 37,
ElfX64RelocationRelative64 = 38,
} ELF_X86_64_RELOCATION_TYPE, *PELF_X86_64_RELOCATION_TYPE;
typedef enum _ELF_ARM_RELOCATION_TYPE {
ElfArmRelocationNone = 0,
ElfArmRelocationAbsolute32 = 2,
ElfArmRelocationTlsDtpMod32 = 17,
ElfArmRelocationTlsDtpOff32 = 18,
ElfArmRelocationTlsTpOff32 = 19,
ElfArmRelocationCopy = 20,
ElfArmRelocationGlobalData = 21,
ElfArmRelocationJumpSlot = 22,
ElfArmRelocationRelative = 23,
ElfArmRelocationGotOffset = 24,
ElfArmRelocationGotPc = 25,
ElfArmRelocationGot32 = 26,
ElfArmRelocationPlt32 = 27
} ELF_ARM_RELOCATION_TYPE, *PELF_ARM_RELOCATION_TYPE;
//
// 32-bit structures
//
/*++
Structure Description:
This structure stores the main file header for an ELF image. It is located
at offset 0 in the file and stores the locations of other ELF headers.
Members:
Identification - Stores a magic number identifying the file as an ELF file,
as well as several other pieces of information such as the file version,
ELF version, endianness, etc.
ImageType - Stores the type of file this is (relocatable, executable,
shared object, etc.)
Machine - Stores the machine architecture of the code in this image.
Version - Stores the version of the ELF format.
EntryPoint - Stores the virtual address where the system should transfer
control to once the image is loaded.
ProgramHeaderOffset - Stores the offset within the file to the first
program header.
SectionHeaderOffset - Stores the offset within the file to the first
section header.
Flags - Stores processor-specific flags associated with the file.
ElfHeaderSize - Stores the size of this header, in bytes.
ProgramHeaderSize - Stores the size of one program header, in bytes.
ProgramHeaderCount - Stores the number of program headers in the file.
SectionHeaderSize - Stores the size of one section header, in bytes.
SectionHeaderCount - Stores the number of section headers in the file.
StringSectionIndex - Stores the section header table index of the entry
associated with the section name string table.
--*/
#pragma pack(push, 1)
typedef struct _ELF32_HEADER {
UCHAR Identification[ELF32_IDENTIFICATION_LENGTH];
ELF32_HALF ImageType;
ELF32_HALF Machine;
ELF32_WORD Version;
ELF32_ADDR EntryPoint;
ELF32_OFF ProgramHeaderOffset;
ELF32_OFF SectionHeaderOffset;
ELF32_WORD Flags;
ELF32_HALF ElfHeaderSize;
ELF32_HALF ProgramHeaderSize;
ELF32_HALF ProgramHeaderCount;
ELF32_HALF SectionHeaderSize;
ELF32_HALF SectionHeaderCount;
ELF32_HALF StringSectionIndex;
} PACKED ELF32_HEADER, *PELF32_HEADER;
/*++
Structure Description:
This structure stores information about a section of data in an ELF32 file.
Members:
NameOffset - Stores the index into the string table section, giving the
location of a null-terminated string of the name of the section.
Type - Stores a description of the section's contents and semantics. See
the ELF_SECTION_TYPE_* definitions.
Flags - Stores miscellaneous attributes of the section. See the
ELF_SECTION_FLAG_* definitions.
VirtualAddress - Stores the virtual address of the beginning of this
section.
Offset - Stores the byte offset from the beginning of the file to the first
byte in the section. If the section type is NOBITS, and this field
locates the conceptual placement in the file.
Size - Stores the size of the section in bytes both in memory an on disk
(unless the type is NOBITS, in which case the file contains no bytes).
Link - Stores a section header table index link, whose interpretation
depends on the section type.
Information - Stores extra information, whose interpretation depends on the
section type.
Alignment - Stores the alignment constraints of the section. Only 0 and
positive powers of 2 are supported. Values of 0 or 1 indicate no
alignment constraints.
EntrySize - Stores the size of one entry, if the section holds a table of
fixed size entries, such as a symbol table. This field contains 0 if
the section does not hold a table of fixed size entries.
--*/
typedef struct _ELF32_SECTION_HEADER {
ELF32_WORD NameOffset;
ELF32_WORD SectionType;
ELF32_WORD Flags;
ELF32_ADDR VirtualAddress;
ELF32_OFF Offset;
ELF32_WORD Size;
ELF32_WORD Link;
ELF32_WORD Information;
ELF32_WORD Alignment;
ELF32_WORD EntrySize;
} PACKED ELF32_SECTION_HEADER, *PELF32_SECTION_HEADER;
/*++
Structure Description:
This structure stores information about a program segment, used to load
ELF32 files. Segments are distinct from sections. One segment may contain
multiple sections.
Members:
Type - Stores what type of segment this array element describes. See
ELF_SEGMENT_TYPE_* definitions.
Offset - Stores the offset from the beginning of the file, in bytes, at
which the first byte of the segment resides.
VirtualAddress - Stores the virtual address at which the first byte of the
segment resides in memory.
PhysicalAddress - Stores the physical address at which the segment resides.
This field is almost always the same as the virtual address.
FileSize - Stores the number of bytes in the file image of the segment. It
may be zero.
MemorySize - Stores the number of bytes in the memory image of the segment.
It may be zero.
Flags - Stores flags regarding this segment. See ELF_PROGRAM_HEADER_FLAG_*
definitions.
Alignment - Stores the power of two alignment requirement for the segment.
Values of 0 and 1 mean that no alignment is required.
--*/
typedef struct _ELF32_PROGRAM_HEADER {
ELF32_WORD Type;
ELF32_OFF Offset;
ELF32_ADDR VirtualAddress;
ELF32_ADDR PhysicalAddress;
ELF32_WORD FileSize;
ELF32_WORD MemorySize;
ELF32_WORD Flags;
ELF32_WORD Alignment;
} PACKED ELF32_PROGRAM_HEADER, *PELF32_PROGRAM_HEADER;
/*++
Structure Description:
This structure stores information about a relocation entry.
Members:
Offset - Stores the location at which to apply the relocation action. For
an executable or shared file object, the value is the virtual address
of the storage unit affected by the relocation.
Information - Stores both the symbol table index with respect to which
relocation must be made, and the type of relocation to apply.
--*/
typedef struct _ELF32_RELOCATION_ENTRY {
ELF32_ADDR Offset;
ELF32_WORD Information;
} PACKED ELF32_RELOCATION_ENTRY, *PELF32_RELOCATION_ENTRY;
/*++
Structure Description:
This structure stores information about a relocation entry with an addend.
Members:
Offset - Stores the location at which to apply the relocation action. For
an executable or shared file object, the value is the virtual address
of the storage unit affected by the relocation.
Information - Stores both the symbol table index with respect to which
relocation must be made, and the type of relocation to apply.
Addend - Stores a constant addend used to compute the value to be stored in
the relocatable field.
--*/
typedef struct _ELF32_RELOCATION_ADDEND_ENTRY {
ELF32_ADDR Offset;
ELF32_WORD Information;
ELF32_SWORD Addend;
} PACKED ELF32_RELOCATION_ADDEND_ENTRY, *PELF32_RELOCATION_ADDEND_ENTRY;
/*++
Structure Description:
This structure stores a symbol entry in the ELF format.
Members:
NameOffset - Stores an offset into the string table where the name of the
symbol is stored.
Value - Stores the value or address of the symbol.
Size - Stores a size associated with the symbol. For data symbols, this
store the size of the type. This member holds 0 if the symbol is an
unknown size.
Information - Stores the symbols type and binding attributes.
Other - Stores 0 and has no defined meaning.
SectionIndex - Stores the section index of the section related to this
symbol. All symbols are defined in the context of a section.
--*/
typedef struct _ELF32_SYMBOL {
ELF32_WORD NameOffset;
ELF32_ADDR Value;
ELF32_WORD Size;
UCHAR Information;
UCHAR Other;
ELF32_HALF SectionIndex;
} PACKED ELF32_SYMBOL, *PELF32_SYMBOL;
/*++
Structure Description:
This structure stores a single entry located in a dynamic section
Members:
Tag - Stores the type of entry and the interpretation of the Value. See
ELF_DYNAMIC_* definitions.
Value - Stores the entry value, whose meaning varies with the type.
--*/
typedef struct _ELF32_DYNAMIC_ENTRY {
ELF32_SWORD Tag;
ELF32_WORD Value;
} PACKED ELF32_DYNAMIC_ENTRY, *PELF32_DYNAMIC_ENTRY;
//
// 64-bit structures
//
/*++
Structure Description:
This structure stores the main file header for an ELF image. It is located
at offset 0 in the file and stores the locations of other ELF headers.
Members:
Identification - Stores a magic number identifying the file as an ELF file,
as well as several other pieces of information such as the file version,
ELF version, endianness, etc.
ImageType - Stores the type of file this is (relocatable, executable,
shared object, etc.)
Machine - Stores the machine architecture of the code in this image.
Version - Stores the version of the ELF format.
EntryPoint - Stores the virtual address where the system should transfer
control to once the image is loaded.
ProgramHeaderOffset - Stores the offset within the file to the first
program header.
SectionHeaderOffset - Stores the offset within the file to the first
section header.
Flags - Stores processor-specific flags associated with the file.
ElfHeaderSize - Stores the size of this header, in bytes.
ProgramHeaderSize - Stores the size of one program header, in bytes.
ProgramHeaderCount - Stores the number of program headers in the file.
SectionHeaderSize - Stores the size of one section header, in bytes.
SectionHeaderCount - Stores the number of section headers in the file.
StringSectionIndex - Stores the section header table index of the entry
associated with the section name string table.
--*/
typedef struct _ELF64_HEADER {
UCHAR Identification[ELF64_IDENTIFICATION_LENGTH];
ELF64_HALF ImageType;
ELF64_HALF Machine;
ELF64_WORD Version;
ELF64_ADDR EntryPoint;
ELF64_OFF ProgramHeaderOffset;
ELF64_OFF SectionHeaderOffset;
ELF64_WORD Flags;
ELF64_HALF ElfHeaderSize;
ELF64_HALF ProgramHeaderSize;
ELF64_HALF ProgramHeaderCount;
ELF64_HALF SectionHeaderSize;
ELF64_HALF SectionHeaderCount;
ELF64_HALF StringSectionIndex;
} PACKED ELF64_HEADER, *PELF64_HEADER;
/*++
Structure Description:
This structure stores information about a section of data in an ELF32 file.
Members:
NameOffset - Stores the index into the string table section, giving the
location of a null-terminated string of the name of the section.
Type - Stores a description of the section's contents and semantics. See
the ELF_SECTION_TYPE_* definitions.
Flags - Stores miscellaneous attributes of the section. See the
ELF_SECTION_FLAG_* definitions.
VirtualAddress - Stores the virtual address of the beginning of this
section.
Offset - Stores the byte offset from the beginning of the file to the first
byte in the section. If the section type is NOBITS, and this field
locates the conceptual placement in the file.
Size - Stores the size of the section in bytes both in memory an on disk
(unless the type is NOBITS, in which case the file contains no bytes).
Link - Stores a section header table index link, whose interpretation
depends on the section type.
Information - Stores extra information, whose interpretation depends on the
section type.
Alignment - Stores the alignment constraints of the section. Only 0 and
positive powers of 2 are supported. Values of 0 or 1 indicate no
alignment constraints.
EntrySize - Stores the size of one entry, if the section holds a table of
fixed size entries, such as a symbol table. This field contains 0 if
the section does not hold a table of fixed size entries.
--*/
typedef struct _ELF64_SECTION_HEADER {
ELF64_WORD NameOffset;
ELF64_WORD SectionType;
ELF64_XWORD Flags;
ELF64_ADDR VirtualAddress;
ELF64_OFF Offset;
ELF64_XWORD Size;
ELF64_WORD Link;
ELF64_WORD Information;
ELF64_XWORD Alignment;
ELF64_XWORD EntrySize;
} PACKED ELF64_SECTION_HEADER, *PELF64_SECTION_HEADER;
/*++
Structure Description:
This structure stores information about a program segment, used to load
ELF64 files. Segments are distinct from sections. One segment may contain
multiple sections.
Members:
Type - Stores what type of segment this array element describes. See
ELF_SEGMENT_TYPE_* definitions.
Flags - Stores flags regarding this segment. See ELF_PROGRAM_HEADER_FLAG_*
definitions.
Offset - Stores the offset from the beginning of the file, in bytes, at
which the first byte of the segment resides.
VirtualAddress - Stores the virtual address at which the first byte of the
segment resides in memory.
PhysicalAddress - Stores the physical address at which the segment resides.
This field is almost always the same as the virtual address.
FileSize - Stores the number of bytes in the file image of the segment. It
may be zero.
MemorySize - Stores the number of bytes in the memory image of the segment.
It may be zero.
Alignment - Stores the power of two alignment requirement for the segment.
Values of 0 and 1 mean that no alignment is required.
--*/
typedef struct _ELF64_PROGRAM_HEADER {
ELF64_WORD Type;
ELF64_WORD Flags;
ELF64_OFF Offset;
ELF64_ADDR VirtualAddress;
ELF64_ADDR PhysicalAddress;
ELF64_XWORD FileSize;
ELF64_XWORD MemorySize;
ELF64_XWORD Alignment;
} PACKED ELF64_PROGRAM_HEADER, *PELF64_PROGRAM_HEADER;
/*++
Structure Description:
This structure stores information about a relocation entry.
Members:
Offset - Stores the location at which to apply the relocation action. For
an executable or shared file object, the value is the virtual address
of the storage unit affected by the relocation.
Information - Stores both the symbol table index with respect to which
relocation must be made, and the type of relocation to apply.
--*/
typedef struct _ELF64_RELOCATION_ENTRY {
ELF64_ADDR Offset;
ELF64_XWORD Information;
} PACKED ELF64_RELOCATION_ENTRY, *PELF64_RELOCATION_ENTRY;
/*++
Structure Description:
This structure stores information about a relocation entry with an addend.
Members:
Offset - Stores the location at which to apply the relocation action. For
an executable or shared file object, the value is the virtual address
of the storage unit affected by the relocation.
Information - Stores both the symbol table index with respect to which
relocation must be made, and the type of relocation to apply.
Addend - Stores a constant addend used to compute the value to be stored in
the relocatable field.
--*/
typedef struct _ELF64_RELOCATION_ADDEND_ENTRY {
ELF64_ADDR Offset;
ELF64_XWORD Information;
ELF64_SXWORD Addend;
} PACKED ELF64_RELOCATION_ADDEND_ENTRY, *PELF64_RELOCATION_ADDEND_ENTRY;
/*++
Structure Description:
This structure stores a symbol entry in the ELF format.
Members:
NameOffset - Stores an offset into the string table where the name of the
symbol is stored.
Information - Stores the symbols type and binding attributes.
Other - Stores 0 and has no defined meaning.
SectionIndex - Stores the section index of the section related to this
symbol. All symbols are defined in the context of a section.
Value - Stores the value or address of the symbol.
Size - Stores a size associated with the symbol. For data symbols, this
store the size of the type. This member holds 0 if the symbol is an
unknown size.
--*/
typedef struct _ELF64_SYMBOL {
ELF64_WORD NameOffset;
UCHAR Information;
UCHAR Other;
ELF64_HALF SectionIndex;
ELF64_ADDR Value;
ELF64_XWORD Size;
} PACKED ELF64_SYMBOL, *PELF64_SYMBOL;
/*++
Structure Description:
This structure stores a single entry located in a dynamic section
Members:
Tag - Stores the type of entry and the interpretation of the Value. See
ELF_DYNAMIC_* definitions.
Value - Stores the entry value, whose meaning varies with the type.
--*/
typedef struct _ELF64_DYNAMIC_ENTRY {
ELF64_SXWORD Tag;
ELF64_XWORD Value;
} PACKED ELF64_DYNAMIC_ENTRY, *PELF64_DYNAMIC_ENTRY;
#pragma pack(pop)
//
// -------------------------------------------------------------------- Globals
//
//
// -------------------------------------------------------- Function Prototypes
//
//
// 32-bit ELF functions.
//
KSTATUS
ImpElf32OpenLibrary (
PLIST_ENTRY ListHead,
PLOADED_IMAGE Parent,
PCSTR LibraryName,
PIMAGE_FILE_INFORMATION File,
PSTR *Path
);
/*++
Routine Description:
This routine attempts to open a dynamic library.
Arguments:
ListHead - Supplies an optional pointer to the head of the list of loaded
images.
Parent - Supplies a pointer to the parent image requiring this image for
load.
LibraryName - Supplies the name of the library to open.
File - Supplies a pointer where the information for the file including its
open handle will be returned.
Path - Supplies a pointer where the real path to the opened file will be
returned. The caller is responsible for freeing this memory.
Return Value:
Status code.
--*/
KSTATUS
ImpElf32GetImageSize (
PLIST_ENTRY ListHead,
PLOADED_IMAGE Image,
PIMAGE_BUFFER Buffer,
PSTR *InterpreterPath
);
/*++
Routine Description:
This routine determines the size of an ELF executable image. The image size,
preferred lowest address, and relocatable flag will all be filled in.
Arguments:
ListHead - Supplies a pointer to the head of the list of loaded images.
Image - Supplies a pointer to the image to get the size of.
Buffer - Supplies a pointer to the loaded image buffer.
InterpreterPath - Supplies a pointer where the interpreter name will be
returned if the program is requesting an interpreter.
Return Value:
Returns the size of the expanded image in memory on success.
0 on failure.
--*/
KSTATUS
ImpElf32LoadImage (
PLIST_ENTRY ListHead,
PLOADED_IMAGE Image,
PIMAGE_BUFFER Buffer
);
/*++
Routine Description:
This routine loads an ELF image into its executable form.
Arguments:
ListHead - Supplies a pointer to the head of the list of loaded images.
Image - Supplies a pointer to the loaded image. This must be partially
filled out. Notable fields that must be filled out by the caller
include the loaded virtual address and image size. This routine will
fill out many other fields.
Buffer - Supplies a pointer to the image buffer.
Return Value:
STATUS_SUCCESS on success.
STATUS_FILE_CORRUPT if the file headers were corrupt or unexpected.
Other errors on failure.
--*/
KSTATUS
ImpElf32AddImage (
PIMAGE_BUFFER ImageBuffer,