LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
DynamicEntry.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_ELF_DYNAMIC_ENTRY_H
17#define LIEF_ELF_DYNAMIC_ENTRY_H
18
19#include <ostream>
20#include <memory>
21#include <cstdint>
22
23#include "LIEF/visibility.h"
24#include "LIEF/Object.hpp"
25#include "LIEF/ELF/enums.hpp"
26
27namespace LIEF {
28namespace ELF {
29namespace details {
30struct Elf64_Dyn;
31struct Elf32_Dyn;
32}
33
37 public:
38 static constexpr uint64_t MIPS_DISC = 0x100000000;
39 static constexpr uint64_t AARCH64_DISC = 0x200000000;
40 static constexpr uint64_t HEXAGON_DISC = 0x300000000;
41 static constexpr uint64_t PPC_DISC = 0x400000000;
42 static constexpr uint64_t PPC64_DISC = 0x500000000;
43 static constexpr uint64_t RISCV_DISC = 0x600000000;
44 static constexpr uint64_t X86_64_DISC = 0x700000000;
45 static constexpr uint64_t IA_64_DISC = 0x800000000;
46
47 enum class TAG : uint64_t {
48 UNKNOWN = uint64_t(-1),
49 DT_NULL_ = 0,
50 NEEDED = 1,
51 PLTRELSZ = 2,
52 PLTGOT = 3,
53 HASH = 4,
54 STRTAB = 5,
55 SYMTAB = 6,
56 RELA = 7,
57 RELASZ = 8,
58 RELAENT = 9,
59 STRSZ = 10,
60 SYMENT = 11,
61 INIT = 12,
62 FINI = 13,
63 SONAME = 14,
64 RPATH = 15,
65 SYMBOLIC = 16,
66 REL = 17,
67 RELSZ = 18,
68 RELENT = 19,
69 PLTREL = 20,
70 DEBUG_TAG = 21,
71 TEXTREL = 22,
72 JMPREL = 23,
73 BIND_NOW = 24,
74 INIT_ARRAY = 25,
75 FINI_ARRAY = 26,
76 INIT_ARRAYSZ = 27,
77 FINI_ARRAYSZ = 28,
78 RUNPATH = 29,
79 FLAGS = 30,
80 PREINIT_ARRAY = 32,
81 PREINIT_ARRAYSZ = 33,
82 SYMTAB_SHNDX = 34,
83 RELRSZ = 35,
84 RELR = 36,
85 RELRENT = 37,
86
87 // GNU Extensions
88 GNU_HASH = 0x6FFFFEF5,
89 TLSDESC_PLT = 0x6FFFFEF6,
90 TLSDESC_GOT = 0x6FFFFEF7,
91 RELACOUNT = 0x6FFFFFF9,
92 RELCOUNT = 0x6FFFFFFA,
93 FLAGS_1 = 0x6FFFFFFB,
94 VERSYM = 0x6FFFFFF0,
95 VERDEF = 0x6FFFFFFC,
96 VERDEFNUM = 0x6FFFFFFD,
97 VERNEED = 0x6FFFFFFE,
98 VERNEEDNUM = 0x6FFFFFFF,
99
100 AUXILIARY = 0x7ffffffd,
101 FILTER = 0x7fffffff,
102
103 // Android Extensions
104 ANDROID_REL_OFFSET = 0x6000000D,
105 ANDROID_REL_SIZE = 0x6000000E,
106 ANDROID_REL = 0x6000000F,
107 ANDROID_RELSZ = 0x60000010,
108 ANDROID_RELA = 0x60000011,
109 ANDROID_RELASZ = 0x60000012,
110 ANDROID_RELR = 0x6FFFE000,
111 ANDROID_RELRSZ = 0x6FFFE001,
112 ANDROID_RELRENT = 0x6FFFE003,
113 ANDROID_RELRCOUNT = 0x6FFFE005,
114
115 /* Mips specific dynamic table entry tags. */
116 MIPS_RLD_VERSION = MIPS_DISC + 0x70000001,
117 MIPS_TIME_STAMP = MIPS_DISC + 0x70000002,
118 MIPS_ICHECKSUM = MIPS_DISC + 0x70000003,
119 MIPS_IVERSION = MIPS_DISC + 0x70000004,
120 MIPS_FLAGS = MIPS_DISC + 0x70000005,
121 MIPS_BASE_ADDRESS = MIPS_DISC + 0x70000006,
122 MIPS_MSYM = MIPS_DISC + 0x70000007,
123 MIPS_CONFLICT = MIPS_DISC + 0x70000008,
124 MIPS_LIBLIST = MIPS_DISC + 0x70000009,
125 MIPS_LOCAL_GOTNO = MIPS_DISC + 0x7000000a,
126 MIPS_CONFLICTNO = MIPS_DISC + 0x7000000b,
127 MIPS_LIBLISTNO = MIPS_DISC + 0x70000010,
128 MIPS_SYMTABNO = MIPS_DISC + 0x70000011,
129 MIPS_UNREFEXTNO = MIPS_DISC + 0x70000012,
130 MIPS_GOTSYM = MIPS_DISC + 0x70000013,
131 MIPS_HIPAGENO = MIPS_DISC + 0x70000014,
132 MIPS_RLD_MAP = MIPS_DISC + 0x70000016,
133 MIPS_DELTA_CLASS = MIPS_DISC + 0x70000017,
134 MIPS_DELTA_CLASS_NO = MIPS_DISC + 0x70000018,
135 MIPS_DELTA_INSTANCE = MIPS_DISC + 0x70000019,
136 MIPS_DELTA_INSTANCE_NO = MIPS_DISC + 0x7000001A,
137 MIPS_DELTA_RELOC = MIPS_DISC + 0x7000001B,
138 MIPS_DELTA_RELOC_NO = MIPS_DISC + 0x7000001C,
139 MIPS_DELTA_SYM = MIPS_DISC + 0x7000001D,
140 MIPS_DELTA_SYM_NO = MIPS_DISC + 0x7000001E,
141 MIPS_DELTA_CLASSSYM = MIPS_DISC + 0x70000020,
142 MIPS_DELTA_CLASSSYM_NO = MIPS_DISC + 0x70000021,
143 MIPS_CXX_FLAGS = MIPS_DISC + 0x70000022,
144 MIPS_PIXIE_INIT = MIPS_DISC + 0x70000023,
145 MIPS_SYMBOL_LIB = MIPS_DISC + 0x70000024,
146 MIPS_LOCALPAGE_GOTIDX = MIPS_DISC + 0x70000025,
147 MIPS_LOCAL_GOTIDX = MIPS_DISC + 0x70000026,
148 MIPS_HIDDEN_GOTIDX = MIPS_DISC + 0x70000027,
149 MIPS_PROTECTED_GOTIDX = MIPS_DISC + 0x70000028,
150 MIPS_OPTIONS = MIPS_DISC + 0x70000029,
151 MIPS_INTERFACE = MIPS_DISC + 0x7000002A,
152 MIPS_DYNSTR_ALIGN = MIPS_DISC + 0x7000002B,
153 MIPS_INTERFACE_SIZE = MIPS_DISC + 0x7000002C,
154 MIPS_RLD_TEXT_RESOLVE_ADDR = MIPS_DISC + 0x7000002D,
155 MIPS_PERF_SUFFIX = MIPS_DISC + 0x7000002E,
156 MIPS_COMPACT_SIZE = MIPS_DISC + 0x7000002F,
157 MIPS_GP_VALUE = MIPS_DISC + 0x70000030,
158 MIPS_AUX_DYNAMIC = MIPS_DISC + 0x70000031,
159 MIPS_PLTGOT = MIPS_DISC + 0x70000032,
160 MIPS_RWPLT = MIPS_DISC + 0x70000034,
161 MIPS_RLD_MAP_REL = MIPS_DISC + 0x70000035,
162 MIPS_XHASH = MIPS_DISC + 0x70000036,
163
164 AARCH64_BTI_PLT = AARCH64_DISC + 0x70000001,
165 AARCH64_PAC_PLT = AARCH64_DISC + 0x70000003,
166 AARCH64_VARIANT_PCS = AARCH64_DISC + 0x70000005,
167 AARCH64_MEMTAG_MODE = AARCH64_DISC + 0x70000009,
168 AARCH64_MEMTAG_HEAP = AARCH64_DISC + 0x7000000b,
169 AARCH64_MEMTAG_STACK = AARCH64_DISC + 0x7000000c,
170 AARCH64_MEMTAG_GLOBALS = AARCH64_DISC + 0x7000000d,
171 AARCH64_MEMTAG_GLOBALSSZ = AARCH64_DISC + 0x7000000f,
172
173 HEXAGON_SYMSZ = HEXAGON_DISC + 0x70000000,
174 HEXAGON_VER = HEXAGON_DISC + 0x70000001,
175 HEXAGON_PLT = HEXAGON_DISC + 0x70000002,
176
177 PPC_GOT = PPC_DISC + 0x70000000,
178 PPC_OPT = PPC_DISC + 0x70000001,
179
180 PPC64_GLINK = PPC64_DISC + 0x70000000,
181 PPC64_OPT = PPC64_DISC + 0x70000003,
182
183 RISCV_VARIANT_CC = RISCV_DISC + 0x70000003,
184
185 X86_64_PLT = X86_64_DISC + 0x70000000,
186 X86_64_PLTSZ = X86_64_DISC + 0x70000001,
187 X86_64_PLTENT = X86_64_DISC + 0x70000003,
188
189 IA_64_PLT_RESERVE = IA_64_DISC + (0x70000000 + 0),
190 IA_64_VMS_SUBTYPE = IA_64_DISC + (0x60000000 + 0),
191 IA_64_VMS_IMGIOCNT = IA_64_DISC + (0x60000000 + 2),
192 IA_64_VMS_LNKFLAGS = IA_64_DISC + (0x60000000 + 8),
193 IA_64_VMS_VIR_MEM_BLK_SIZ = IA_64_DISC + (0x60000000 + 10),
194 IA_64_VMS_IDENT = IA_64_DISC + (0x60000000 + 12),
195 IA_64_VMS_NEEDED_IDENT = IA_64_DISC + (0x60000000 + 16),
196 IA_64_VMS_IMG_RELA_CNT = IA_64_DISC + (0x60000000 + 18),
197 IA_64_VMS_SEG_RELA_CNT = IA_64_DISC + (0x60000000 + 20),
198 IA_64_VMS_FIXUP_RELA_CNT = IA_64_DISC + (0x60000000 + 22),
199 IA_64_VMS_FIXUP_NEEDED = IA_64_DISC + (0x60000000 + 24),
200 IA_64_VMS_SYMVEC_CNT = IA_64_DISC + (0x60000000 + 26),
201 IA_64_VMS_XLATED = IA_64_DISC + (0x60000000 + 30),
202 IA_64_VMS_STACKSIZE = IA_64_DISC + (0x60000000 + 32),
203 IA_64_VMS_UNWINDSZ = IA_64_DISC + (0x60000000 + 34),
204 IA_64_VMS_UNWIND_CODSEG = IA_64_DISC + (0x60000000 + 36),
205 IA_64_VMS_UNWIND_INFOSEG = IA_64_DISC + (0x60000000 + 38),
206 IA_64_VMS_LINKTIME = IA_64_DISC + (0x60000000 + 40),
207 IA_64_VMS_SEG_NO = IA_64_DISC + (0x60000000 + 42),
208 IA_64_VMS_SYMVEC_OFFSET = IA_64_DISC + (0x60000000 + 44),
209 IA_64_VMS_SYMVEC_SEG = IA_64_DISC + (0x60000000 + 46),
210 IA_64_VMS_UNWIND_OFFSET = IA_64_DISC + (0x60000000 + 48),
211 IA_64_VMS_UNWIND_SEG = IA_64_DISC + (0x60000000 + 50),
212 IA_64_VMS_STRTAB_OFFSET = IA_64_DISC + (0x60000000 + 52),
213 IA_64_VMS_SYSVER_OFFSET = IA_64_DISC + (0x60000000 + 54),
214 IA_64_VMS_IMG_RELA_OFF = IA_64_DISC + (0x60000000 + 56),
215 IA_64_VMS_SEG_RELA_OFF = IA_64_DISC + (0x60000000 + 58),
216 IA_64_VMS_FIXUP_RELA_OFF = IA_64_DISC + (0x60000000 + 60),
217 IA_64_VMS_PLTGOT_OFFSET = IA_64_DISC + (0x60000000 + 62),
218 IA_64_VMS_PLTGOT_SEG = IA_64_DISC + (0x60000000 + 64),
219 IA_64_VMS_FPMODE = IA_64_DISC + (0x60000000 + 66),
220 };
221
222 static TAG from_value(uint64_t value, ARCH arch);
223 static uint64_t to_value(TAG tag);
224
225 DynamicEntry() = default;
226 DynamicEntry(const details::Elf64_Dyn& header, ARCH arch);
227 DynamicEntry(const details::Elf32_Dyn& header, ARCH arch);
228
230 tag_(tag), value_(value)
231 {}
232
234 DynamicEntry(const DynamicEntry&) = default;
235 ~DynamicEntry() override = default;
236
237 static std::unique_ptr<DynamicEntry> create(TAG tag, uint64_t value);
238
239 static std::unique_ptr<DynamicEntry> create(TAG tag) {
240 return create(tag, /*value=*/0);
241 }
242
243 virtual std::unique_ptr<DynamicEntry> clone() const {
244 return std::unique_ptr<DynamicEntry>(new DynamicEntry(*this));
245 }
246
249 TAG tag() const {
250 return tag_;
251 }
252
257 uint64_t value() const {
258 return value_;
259 }
260
261 void tag(TAG tag) {
262 tag_ = tag;
263 }
264
265 void value(uint64_t value) {
266 value_ = value;
267 }
268
269 void accept(Visitor& visitor) const override;
270
271 virtual std::ostream& print(std::ostream& os) const;
272
273 std::string to_string() const;
274
275 LIEF_API friend
276 std::ostream& operator<<(std::ostream& os, const DynamicEntry& entry) {
277 return entry.print(os);
278 }
279
280 template<class T>
281 const T* cast() const {
282 static_assert(std::is_base_of<DynamicEntry, T>::value,
283 "Require DynamicEntry inheritance");
284 if (T::classof(this)) {
285 return static_cast<const T*>(this);
286 }
287 return nullptr;
288 }
289
290 template<class T>
291 T* cast() {
292 return const_cast<T*>(static_cast<const DynamicEntry*>(this)->cast<T>());
293 }
294
295 protected:
296 TAG tag_ = TAG::DT_NULL_;
297 uint64_t value_ = 0;
298};
299
301
302}
303}
304#endif
static constexpr uint64_t IA_64_DISC
Definition DynamicEntry.hpp:45
static constexpr uint64_t MIPS_DISC
Definition DynamicEntry.hpp:38
static constexpr uint64_t PPC_DISC
Definition DynamicEntry.hpp:41
DynamicEntry(const details::Elf32_Dyn &header, ARCH arch)
DynamicEntry(const details::Elf64_Dyn &header, ARCH arch)
static constexpr uint64_t AARCH64_DISC
Definition DynamicEntry.hpp:39
TAG
Definition DynamicEntry.hpp:47
const T * cast() const
Definition DynamicEntry.hpp:281
virtual std::ostream & print(std::ostream &os) const
void tag(TAG tag)
Definition DynamicEntry.hpp:261
static constexpr uint64_t HEXAGON_DISC
Definition DynamicEntry.hpp:40
virtual std::unique_ptr< DynamicEntry > clone() const
Definition DynamicEntry.hpp:243
~DynamicEntry() override=default
DynamicEntry & operator=(const DynamicEntry &)=default
void accept(Visitor &visitor) const override
static std::unique_ptr< DynamicEntry > create(TAG tag, uint64_t value)
static uint64_t to_value(TAG tag)
static constexpr uint64_t RISCV_DISC
Definition DynamicEntry.hpp:43
std::string to_string() const
TAG tag() const
Tag of the current entry. The most common tags are: DT_NEEDED, DT_INIT, ...
Definition DynamicEntry.hpp:249
void value(uint64_t value)
Definition DynamicEntry.hpp:265
T * cast()
Definition DynamicEntry.hpp:291
uint64_t value() const
Return the entry's value.
Definition DynamicEntry.hpp:257
static constexpr uint64_t PPC64_DISC
Definition DynamicEntry.hpp:42
friend std::ostream & operator<<(std::ostream &os, const DynamicEntry &entry)
Definition DynamicEntry.hpp:276
static std::unique_ptr< DynamicEntry > create(TAG tag)
Definition DynamicEntry.hpp:239
static TAG from_value(uint64_t value, ARCH arch)
DynamicEntry(const DynamicEntry &)=default
static constexpr uint64_t X86_64_DISC
Definition DynamicEntry.hpp:44
DynamicEntry(TAG tag, uint64_t value)
Definition DynamicEntry.hpp:229
Definition Visitor.hpp:212
Definition DynamicEntry.hpp:29
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:41