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
38 public:
39 static constexpr uint64_t MIPS_DISC = 0x100000000;
40 static constexpr uint64_t AARCH64_DISC = 0x200000000;
41 static constexpr uint64_t HEXAGON_DISC = 0x300000000;
42 static constexpr uint64_t PPC_DISC = 0x400000000;
43 static constexpr uint64_t PPC64_DISC = 0x500000000;
44 static constexpr uint64_t RISCV_DISC = 0x600000000;
45 static constexpr uint64_t X86_64_DISC = 0x700000000;
46 static constexpr uint64_t IA_64_DISC = 0x800000000;
47
48 enum class TAG : uint64_t {
49 UNKNOWN = uint64_t(-1),
50 DT_NULL_ = 0,
51 NEEDED = 1,
52 PLTRELSZ = 2,
53 PLTGOT = 3,
54 HASH = 4,
55 STRTAB = 5,
56 SYMTAB = 6,
57 RELA = 7,
58 RELASZ = 8,
59 RELAENT = 9,
60 STRSZ = 10,
61 SYMENT = 11,
62 INIT = 12,
63 FINI = 13,
64 SONAME = 14,
65 RPATH = 15,
66 SYMBOLIC = 16,
67 REL = 17,
68 RELSZ = 18,
69 RELENT = 19,
70 PLTREL = 20,
71 DEBUG_TAG = 21,
72 TEXTREL = 22,
73 JMPREL = 23,
74 BIND_NOW = 24,
75 INIT_ARRAY = 25,
76 FINI_ARRAY = 26,
77 INIT_ARRAYSZ = 27,
78 FINI_ARRAYSZ = 28,
79 RUNPATH = 29,
80 FLAGS = 30,
81 PREINIT_ARRAY = 32,
82 PREINIT_ARRAYSZ = 33,
83 SYMTAB_SHNDX = 34,
84 RELRSZ = 35,
85 RELR = 36,
86 RELRENT = 37,
87
88 // GNU Extensions
89 GNU_HASH = 0x6FFFFEF5,
90 TLSDESC_PLT = 0x6FFFFEF6,
92 TLSDESC_GOT = 0x6FFFFEF7,
94 RELACOUNT = 0x6FFFFFF9,
95 RELCOUNT = 0x6FFFFFFA,
96 FLAGS_1 = 0x6FFFFFFB,
97 VERSYM = 0x6FFFFFF0,
98 VERDEF = 0x6FFFFFFC,
99 VERDEFNUM = 0x6FFFFFFD,
100 VERNEED = 0x6FFFFFFE,
101 VERNEEDNUM = 0x6FFFFFFF,
102
103 AUXILIARY = 0x7ffffffd,
104 FILTER = 0x7fffffff,
105
106 // Android Extensions
107 ANDROID_REL_OFFSET = 0x6000000D,
109 ANDROID_REL_SIZE = 0x6000000E,
111 ANDROID_REL = 0x6000000F,
113 ANDROID_RELSZ = 0x60000010,
115 ANDROID_RELA = 0x60000011,
117 ANDROID_RELASZ = 0x60000012,
119 ANDROID_RELR = 0x6FFFE000,
121 ANDROID_RELRSZ = 0x6FFFE001,
123 ANDROID_RELRENT = 0x6FFFE003,
125 ANDROID_RELRCOUNT = 0x6FFFE005,
127
128 /* Mips specific dynamic table entry tags. */
129 MIPS_RLD_VERSION =
130 MIPS_DISC +
131 0x70000001,
132 MIPS_TIME_STAMP = MIPS_DISC + 0x70000002,
133 MIPS_ICHECKSUM =
134 MIPS_DISC +
135 0x70000003,
136 MIPS_IVERSION =
137 MIPS_DISC + 0x70000004,
138 MIPS_FLAGS = MIPS_DISC + 0x70000005,
139 MIPS_BASE_ADDRESS =
140 MIPS_DISC + 0x70000006,
141 MIPS_MSYM = MIPS_DISC + 0x70000007,
142 MIPS_CONFLICT = MIPS_DISC + 0x70000008,
143 MIPS_LIBLIST = MIPS_DISC + 0x70000009,
144 MIPS_LOCAL_GOTNO =
145 MIPS_DISC +
146 0x7000000a,
147 MIPS_CONFLICTNO =
148 MIPS_DISC + 0x7000000b,
149 MIPS_LIBLISTNO =
150 MIPS_DISC + 0x70000010,
151 MIPS_SYMTABNO =
152 MIPS_DISC + 0x70000011,
153 MIPS_UNREFEXTNO = MIPS_DISC + 0x70000012,
155 MIPS_GOTSYM =
156 MIPS_DISC +
157 0x70000013,
158 MIPS_HIPAGENO =
159 MIPS_DISC +
160 0x70000014,
161 MIPS_RLD_MAP =
162 MIPS_DISC +
163 0x70000016,
164 MIPS_DELTA_CLASS = MIPS_DISC + 0x70000017,
165 MIPS_DELTA_CLASS_NO =
166 MIPS_DISC + 0x70000018,
167 MIPS_DELTA_INSTANCE =
168 MIPS_DISC + 0x70000019,
169 MIPS_DELTA_INSTANCE_NO =
170 MIPS_DISC +
171 0x7000001A,
172 MIPS_DELTA_RELOC = MIPS_DISC + 0x7000001B,
173 MIPS_DELTA_RELOC_NO =
174 MIPS_DISC + 0x7000001C,
175 MIPS_DELTA_SYM =
176 MIPS_DISC +
177 0x7000001D,
178 MIPS_DELTA_SYM_NO =
179 MIPS_DISC + 0x7000001E,
180 MIPS_DELTA_CLASSSYM =
181 MIPS_DISC + 0x70000020,
182 MIPS_DELTA_CLASSSYM_NO =
183 MIPS_DISC +
184 0x70000021,
185 MIPS_CXX_FLAGS =
186 MIPS_DISC +
187 0x70000022,
188 MIPS_PIXIE_INIT = MIPS_DISC + 0x70000023,
189 MIPS_SYMBOL_LIB = MIPS_DISC + 0x70000024,
190 MIPS_LOCALPAGE_GOTIDX =
191 MIPS_DISC +
192 0x70000025,
193 MIPS_LOCAL_GOTIDX =
194 MIPS_DISC +
195 0x70000026,
196 MIPS_HIDDEN_GOTIDX =
197 MIPS_DISC +
198 0x70000027,
199 MIPS_PROTECTED_GOTIDX =
200 MIPS_DISC +
201 0x70000028,
202 MIPS_OPTIONS = MIPS_DISC + 0x70000029,
203 MIPS_INTERFACE = MIPS_DISC + 0x7000002A,
204 MIPS_DYNSTR_ALIGN = MIPS_DISC + 0x7000002B,
205 MIPS_INTERFACE_SIZE =
206 MIPS_DISC + 0x7000002C,
207 MIPS_RLD_TEXT_RESOLVE_ADDR =
208 MIPS_DISC +
209 0x7000002D,
210 MIPS_PERF_SUFFIX =
211 MIPS_DISC + 0x7000002E,
213 MIPS_COMPACT_SIZE =
214 MIPS_DISC + 0x7000002F,
215 MIPS_GP_VALUE = MIPS_DISC + 0x70000030,
216 MIPS_AUX_DYNAMIC =
217 MIPS_DISC + 0x70000031,
218 MIPS_PLTGOT =
219 MIPS_DISC + 0x70000032,
220 MIPS_RWPLT = MIPS_DISC + 0x70000034,
221 MIPS_RLD_MAP_REL = MIPS_DISC + 0x70000035,
222 MIPS_XHASH = MIPS_DISC + 0x70000036,
223
224 AARCH64_BTI_PLT = AARCH64_DISC + 0x70000001,
225 AARCH64_PAC_PLT = AARCH64_DISC + 0x70000003,
226 AARCH64_VARIANT_PCS = AARCH64_DISC + 0x70000005,
227 AARCH64_MEMTAG_MODE = AARCH64_DISC + 0x70000009,
228 AARCH64_MEMTAG_HEAP = AARCH64_DISC + 0x7000000b,
229 AARCH64_MEMTAG_STACK = AARCH64_DISC + 0x7000000c,
230 AARCH64_MEMTAG_GLOBALS = AARCH64_DISC + 0x7000000d,
231 AARCH64_MEMTAG_GLOBALSSZ = AARCH64_DISC + 0x7000000f,
232
233 HEXAGON_SYMSZ = HEXAGON_DISC + 0x70000000,
234 HEXAGON_VER = HEXAGON_DISC + 0x70000001,
235 HEXAGON_PLT = HEXAGON_DISC + 0x70000002,
236
237 PPC_GOT = PPC_DISC + 0x70000000,
238 PPC_OPT = PPC_DISC + 0x70000001,
239
240 PPC64_GLINK = PPC64_DISC + 0x70000000,
241 PPC64_OPT = PPC64_DISC + 0x70000003,
242
243 RISCV_VARIANT_CC = RISCV_DISC + 0x70000003,
244
245 X86_64_PLT = X86_64_DISC + 0x70000000,
246 X86_64_PLTSZ = X86_64_DISC + 0x70000001,
247 X86_64_PLTENT = X86_64_DISC + 0x70000003,
248
249 IA_64_PLT_RESERVE = IA_64_DISC + (0x70000000 + 0),
250 IA_64_VMS_SUBTYPE = IA_64_DISC + (0x60000000 + 0),
251 IA_64_VMS_IMGIOCNT = IA_64_DISC + (0x60000000 + 2),
252 IA_64_VMS_LNKFLAGS = IA_64_DISC + (0x60000000 + 8),
253 IA_64_VMS_VIR_MEM_BLK_SIZ = IA_64_DISC + (0x60000000 + 10),
254 IA_64_VMS_IDENT = IA_64_DISC + (0x60000000 + 12),
255 IA_64_VMS_NEEDED_IDENT = IA_64_DISC + (0x60000000 + 16),
256 IA_64_VMS_IMG_RELA_CNT = IA_64_DISC + (0x60000000 + 18),
257 IA_64_VMS_SEG_RELA_CNT = IA_64_DISC + (0x60000000 + 20),
258 IA_64_VMS_FIXUP_RELA_CNT = IA_64_DISC + (0x60000000 + 22),
259 IA_64_VMS_FIXUP_NEEDED = IA_64_DISC + (0x60000000 + 24),
260 IA_64_VMS_SYMVEC_CNT = IA_64_DISC + (0x60000000 + 26),
261 IA_64_VMS_XLATED = IA_64_DISC + (0x60000000 + 30),
262 IA_64_VMS_STACKSIZE = IA_64_DISC + (0x60000000 + 32),
263 IA_64_VMS_UNWINDSZ = IA_64_DISC + (0x60000000 + 34),
264 IA_64_VMS_UNWIND_CODSEG = IA_64_DISC + (0x60000000 + 36),
265 IA_64_VMS_UNWIND_INFOSEG = IA_64_DISC + (0x60000000 + 38),
266 IA_64_VMS_LINKTIME = IA_64_DISC + (0x60000000 + 40),
267 IA_64_VMS_SEG_NO = IA_64_DISC + (0x60000000 + 42),
268 IA_64_VMS_SYMVEC_OFFSET = IA_64_DISC + (0x60000000 + 44),
269 IA_64_VMS_SYMVEC_SEG = IA_64_DISC + (0x60000000 + 46),
270 IA_64_VMS_UNWIND_OFFSET = IA_64_DISC + (0x60000000 + 48),
271 IA_64_VMS_UNWIND_SEG = IA_64_DISC + (0x60000000 + 50),
272 IA_64_VMS_STRTAB_OFFSET = IA_64_DISC + (0x60000000 + 52),
273 IA_64_VMS_SYSVER_OFFSET = IA_64_DISC + (0x60000000 + 54),
274 IA_64_VMS_IMG_RELA_OFF = IA_64_DISC + (0x60000000 + 56),
275 IA_64_VMS_SEG_RELA_OFF = IA_64_DISC + (0x60000000 + 58),
276 IA_64_VMS_FIXUP_RELA_OFF = IA_64_DISC + (0x60000000 + 60),
277 IA_64_VMS_PLTGOT_OFFSET = IA_64_DISC + (0x60000000 + 62),
278 IA_64_VMS_PLTGOT_SEG = IA_64_DISC + (0x60000000 + 64),
279 IA_64_VMS_FPMODE = IA_64_DISC + (0x60000000 + 66),
280 };
281
282 static TAG from_value(uint64_t value, ARCH arch);
283 static uint64_t to_value(TAG tag);
284
285 DynamicEntry() = default;
286 DynamicEntry(const details::Elf64_Dyn& header, ARCH arch);
287 DynamicEntry(const details::Elf32_Dyn& header, ARCH arch);
288
290 tag_(tag),
291 value_(value) {}
292
294 DynamicEntry(const DynamicEntry&) = default;
295 ~DynamicEntry() override = default;
296
297 static std::unique_ptr<DynamicEntry> create(TAG tag, uint64_t value);
298
299 static std::unique_ptr<DynamicEntry> create(TAG tag) {
300 return create(tag, /*value=*/0);
301 }
302
303 virtual std::unique_ptr<DynamicEntry> clone() const {
304 return std::unique_ptr<DynamicEntry>(new DynamicEntry(*this));
305 }
306
309 TAG tag() const {
310 return tag_;
311 }
312
317 uint64_t value() const {
318 return value_;
319 }
320
321 void tag(TAG tag) {
322 tag_ = tag;
323 }
324
325 void value(uint64_t value) {
326 value_ = value;
327 }
328
329 void accept(Visitor& visitor) const override;
330
331 virtual std::ostream& print(std::ostream& os) const;
332
333 std::string to_string() const;
334
335 LIEF_API friend std::ostream& operator<<(std::ostream& os,
336 const DynamicEntry& entry) {
337 return entry.print(os);
338 }
339
340 template<class T>
341 const T* cast() const {
342 static_assert(std::is_base_of<DynamicEntry, T>::value,
343 "Require DynamicEntry inheritance");
344 if (T::classof(this)) {
345 return static_cast<const T*>(this);
346 }
347 return nullptr;
348 }
349
350 template<class T>
351 T* cast() {
352 return const_cast<T*>(static_cast<const DynamicEntry*>(this)->cast<T>());
353 }
354
355 protected:
356 TAG tag_ = TAG::DT_NULL_;
357 uint64_t value_ = 0;
358};
359
361
362}
363}
364#endif
static constexpr uint64_t IA_64_DISC
Definition DynamicEntry.hpp:46
static constexpr uint64_t MIPS_DISC
Definition DynamicEntry.hpp:39
static constexpr uint64_t PPC_DISC
Definition DynamicEntry.hpp:42
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:40
TAG
Definition DynamicEntry.hpp:48
const T * cast() const
Definition DynamicEntry.hpp:341
virtual std::ostream & print(std::ostream &os) const
void tag(TAG tag)
Definition DynamicEntry.hpp:321
static constexpr uint64_t HEXAGON_DISC
Definition DynamicEntry.hpp:41
virtual std::unique_ptr< DynamicEntry > clone() const
Definition DynamicEntry.hpp:303
~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:44
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:309
void value(uint64_t value)
Definition DynamicEntry.hpp:325
T * cast()
Definition DynamicEntry.hpp:351
uint64_t value() const
Return the entry's value.
Definition DynamicEntry.hpp:317
static constexpr uint64_t PPC64_DISC
Definition DynamicEntry.hpp:43
friend std::ostream & operator<<(std::ostream &os, const DynamicEntry &entry)
Definition DynamicEntry.hpp:335
static std::unique_ptr< DynamicEntry > create(TAG tag)
Definition DynamicEntry.hpp:299
static TAG from_value(uint64_t value, ARCH arch)
DynamicEntry(const DynamicEntry &)=default
static constexpr uint64_t X86_64_DISC
Definition DynamicEntry.hpp:45
DynamicEntry(TAG tag, uint64_t value)
Definition DynamicEntry.hpp:289
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:43