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