1// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_PLATFORM_ELF_H_
6#define RUNTIME_PLATFORM_ELF_H_
7
8#include "platform/globals.h"
9
10namespace dart {
11namespace elf {
12
13#pragma pack(push, 1)
14
15struct ElfHeader {
16 uint8_t ident[16];
17 uint16_t type;
18 uint16_t machine;
19 uint32_t version;
20#if defined(TARGET_ARCH_IS_32_BIT)
21 uint32_t entry_point;
22 uint32_t program_table_offset;
23 uint32_t section_table_offset;
24#else
25 uint64_t entry_point;
26 uint64_t program_table_offset;
27 uint64_t section_table_offset;
28#endif
29 uint32_t flags;
30 uint16_t header_size;
31 uint16_t program_table_entry_size;
32 uint16_t num_program_headers;
33 uint16_t section_table_entry_size;
34 uint16_t num_section_headers;
35 uint16_t shstrtab_section_index;
36};
37
38enum class ProgramHeaderType : uint32_t {
39 PT_NULL = 0,
40 PT_LOAD = 1,
41 PT_DYNAMIC = 2,
42 PT_NOTE = 4,
43 PT_PHDR = 6,
44};
45
46struct ProgramHeader {
47#if defined(TARGET_ARCH_IS_32_BIT)
48 ProgramHeaderType type;
49 uint32_t file_offset;
50 uint32_t memory_offset;
51 uint32_t physical_memory_offset;
52 uint32_t file_size;
53 uint32_t memory_size;
54 uint32_t flags;
55 uint32_t alignment;
56#else
57 ProgramHeaderType type;
58 uint32_t flags;
59 uint64_t file_offset;
60 uint64_t memory_offset;
61 uint64_t physical_memory_offset;
62 uint64_t file_size;
63 uint64_t memory_size;
64 uint64_t alignment;
65#endif
66};
67
68enum class SectionHeaderType : uint32_t {
69 SHT_NULL = 0,
70 SHT_PROGBITS = 1,
71 SHT_SYMTAB = 2,
72 SHT_STRTAB = 3,
73 SHT_HASH = 5,
74 SHT_NOTE = 7,
75 SHT_NOBITS = 8,
76 SHT_DYNAMIC = 6,
77 SHT_DYNSYM = 11,
78};
79
80struct SectionHeader {
81#if defined(TARGET_ARCH_IS_32_BIT)
82 uint32_t name;
83 SectionHeaderType type;
84 uint32_t flags;
85 uint32_t memory_offset;
86 uint32_t file_offset;
87 uint32_t file_size;
88 uint32_t link;
89 uint32_t info;
90 uint32_t alignment;
91 uint32_t entry_size;
92#else
93 uint32_t name;
94 SectionHeaderType type;
95 uint64_t flags;
96 uint64_t memory_offset;
97 uint64_t file_offset;
98 uint64_t file_size;
99 uint32_t link;
100 uint32_t info;
101 uint64_t alignment;
102 uint64_t entry_size;
103#endif
104};
105
106struct Symbol {
107#if defined(TARGET_ARCH_IS_32_BIT)
108 uint32_t name;
109 uint32_t value;
110 uint32_t size;
111 uint8_t info;
112 uint8_t other; // Reserved by ELF.
113 uint16_t section;
114#else
115 uint32_t name;
116 uint8_t info;
117 uint8_t other; // Reserved by ELF.
118 uint16_t section;
119 uint64_t value;
120 uint64_t size;
121#endif
122};
123
124enum class DynamicEntryType : uint32_t {
125 DT_NULL = 0,
126 DT_HASH = 4,
127 DT_STRTAB = 5,
128 DT_SYMTAB = 6,
129 DT_STRSZ = 10,
130 DT_SYMENT = 11,
131};
132
133struct DynamicEntry {
134#if defined(TARGET_ARCH_IS_32_BIT)
135 uint32_t tag;
136 uint32_t value;
137#else
138 uint64_t tag;
139 uint64_t value;
140#endif
141};
142
143enum class NoteType : uint32_t {
144 NT_GNU_BUILD_ID = 3,
145};
146
147struct Note {
148 uint32_t name_size;
149 uint32_t description_size;
150 NoteType type;
151 uint8_t data[];
152};
153
154#pragma pack(pop)
155
156static constexpr intptr_t ELFCLASS32 = 1;
157static constexpr intptr_t ELFCLASS64 = 2;
158
159static const intptr_t EI_DATA = 5;
160static const intptr_t ELFDATA2LSB = 1;
161
162static const intptr_t ELFOSABI_SYSV = 0;
163
164static const intptr_t ET_DYN = 3;
165
166static constexpr intptr_t EF_ARM_ABI_FLOAT_HARD = 0x00000400;
167static constexpr intptr_t EF_ARM_ABI_FLOAT_SOFT = 0x00000200;
168static constexpr intptr_t EF_ARM_ABI = 0x05000000;
169
170static constexpr intptr_t EM_386 = 3;
171static constexpr intptr_t EM_ARM = 40;
172static constexpr intptr_t EM_X86_64 = 62;
173static constexpr intptr_t EM_AARCH64 = 183;
174
175static const intptr_t EV_CURRENT = 1;
176
177static const intptr_t PF_X = 1;
178static const intptr_t PF_W = 2;
179static const intptr_t PF_R = 4;
180
181static const intptr_t SHF_WRITE = 0x1;
182static const intptr_t SHF_ALLOC = 0x2;
183static const intptr_t SHF_EXECINSTR = 0x4;
184
185static const intptr_t SHN_UNDEF = 0;
186
187static const intptr_t STN_UNDEF = 0;
188
189static const intptr_t STB_LOCAL = 0;
190static const intptr_t STB_GLOBAL = 1;
191
192static const intptr_t STT_OBJECT = 1; // I.e., data.
193static const intptr_t STT_FUNC = 2;
194static const intptr_t STT_SECTION = 3;
195
196static constexpr const char* ELF_NOTE_GNU = "GNU";
197
198} // namespace elf
199} // namespace dart
200
201#endif // RUNTIME_PLATFORM_ELF_H_
202