LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ELF/Parser.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_PARSER_H
17#define LIEF_ELF_PARSER_H
18#include <string_view>
19#include <unordered_map>
20#include <unordered_set>
21
22#include "LIEF/path.hpp"
23#include "LIEF/utils.hpp"
24#include "LIEF/visibility.h"
25
27#include "LIEF/errors.hpp"
28
30
31namespace LIEF {
32class BinaryStream;
33
34namespace OAT {
35class Parser;
36}
37namespace ELF {
38
39class Section;
40class Binary;
41class Segment;
42class Symbol;
43class Note;
44class Relocation;
45
48 friend class OAT::Parser;
49
50 public:
51 static constexpr uint32_t NB_MAX_SYMBOLS = 1000000;
52 static constexpr uint32_t DELTA_NB_SYMBOLS = 3000;
53 static constexpr uint32_t NB_MAX_BUCKETS = NB_MAX_SYMBOLS;
54 static constexpr uint32_t NB_MAX_CHAINS = 1000000;
55 static constexpr uint32_t NB_MAX_SEGMENTS = 10000;
56 static constexpr uint32_t NB_MAX_RELOCATIONS = 3000000;
57 static constexpr uint32_t NB_MAX_DYNAMIC_ENTRIES = 1000;
58 static constexpr uint32_t MAX_SEGMENT_SIZE = 3_GB;
59
65
75 static std::unique_ptr<Binary>
76 parse(std::string_view file, const ParserConfig& conf = ParserConfig::all());
77
80 template<class PathT, enable_if_path_t<PathT> = 0>
81 static std::unique_ptr<Binary>
82 parse(const PathT& file, const ParserConfig& conf = ParserConfig::all()) {
83 return parse(file.string(), conf);
84 }
85
96 static std::unique_ptr<Binary>
97 parse(const std::vector<uint8_t>& data,
98 const ParserConfig& conf = ParserConfig::all());
99
110 static std::unique_ptr<Binary>
111 parse(std::unique_ptr<BinaryStream> stream,
112 const ParserConfig& conf = ParserConfig::all());
113
120 static std::unique_ptr<Binary>
121 parse_from_memory(uintptr_t address,
122 const ParserConfig& conf = ParserConfig::all());
123
131 static std::unique_ptr<Binary>
132 parse_from_memory(uintptr_t address, size_t size,
133 const ParserConfig& conf = ParserConfig::all());
134
144 static std::unique_ptr<Binary>
145 parse_from_dump(std::string_view filepath, uint64_t addr,
146 const ParserConfig& conf = ParserConfig::all());
147
150 template<class PathT, enable_if_path_t<PathT> = 0>
151 static std::unique_ptr<Binary>
152 parse_from_dump(const PathT& filepath, uint64_t addr,
153 const ParserConfig& conf = ParserConfig::all()) {
154 return parse_from_dump(filepath.string(), addr, conf);
155 }
156
159 static std::unique_ptr<Binary>
160 parse_from_dump(BinaryStream& stream, uint64_t addr,
161 const ParserConfig& conf = ParserConfig::all());
162
165 static std::unique_ptr<Binary>
166 parse_from_dump(std::unique_ptr<BinaryStream> stream, uint64_t addr,
167 const ParserConfig& conf = ParserConfig::all());
168
169 Parser& operator=(const Parser&) = delete;
170 Parser(const Parser&) = delete;
171
173
174 protected:
176 LIEF_LOCAL Parser(std::unique_ptr<BinaryStream> stream, ParserConfig config);
177 LIEF_LOCAL Parser(std::string_view file, ParserConfig config);
178 LIEF_LOCAL Parser(const std::vector<uint8_t>& data, ParserConfig config);
179
180 LIEF_LOCAL ok_error_t init();
181
182 LIEF_LOCAL bool should_swap() const;
183
184 // map, dynamic_symbol.version <----> symbol_version
185 // symbol_version comes from symbol_version table
186 LIEF_LOCAL void link_symbol_version();
187
188 LIEF_LOCAL ok_error_t link_symbol_section(Symbol& sym);
189
190 template<typename ELF_T>
191 LIEF_LOCAL ok_error_t parse_binary();
192
193 template<typename ELF_T>
194 LIEF_LOCAL ok_error_t parse_header();
195
196 template<typename ELF_T>
197 LIEF_LOCAL ok_error_t parse_sections();
198
199 template<typename ELF_T>
200 LIEF_LOCAL ok_error_t parse_segments();
201
202 LIEF_LOCAL uint64_t
203 get_dynamic_string_table(BinaryStream* stream = nullptr) const;
204
206 get_dynamic_string_table_from_segments(BinaryStream* stream = nullptr) const;
207
208 LIEF_LOCAL uint64_t get_dynamic_string_table_from_sections() const;
209
211 template<typename ELF_T>
213 get_numberof_dynamic_symbols(ParserConfig::DYNSYM_COUNT mtd) const;
214
216 template<typename ELF_T>
217 LIEF_LOCAL result<uint32_t> nb_dynsym_hash() const;
218
220 template<typename ELF_T>
221 LIEF_LOCAL result<uint32_t> nb_dynsym_sysv_hash() const;
222
224 template<typename ELF_T>
225 LIEF_LOCAL result<uint32_t> nb_dynsym_gnu_hash() const;
226
228 template<typename ELF_T>
229 LIEF_LOCAL result<uint32_t> nb_dynsym_section() const;
230
232 template<typename ELF_T>
233 LIEF_LOCAL result<uint32_t> nb_dynsym_relocations() const;
234
235 template<typename ELF_T>
236 LIEF_LOCAL ok_error_t parse_dynamic_entries(BinaryStream& stream);
237
238 template<typename ELF_T>
239 LIEF_LOCAL ok_error_t parse_dynamic_symbols(uint64_t offset);
240
250 template<typename ELF_T>
251 LIEF_LOCAL ok_error_t parse_symtab_symbols(const Section& symtab_section,
252 const Section& string_section);
253
257 template<typename ELF_T, typename REL_T>
258 LIEF_LOCAL ok_error_t parse_dynamic_relocations(uint64_t relocations_offset,
259 uint64_t size);
260
266 template<typename ELF_T, typename REL_T>
267 LIEF_LOCAL ok_error_t parse_pltgot_relocations(uint64_t offset, uint64_t size);
268
269
271 template<typename ELF_T>
272 LIEF_LOCAL ok_error_t parse_relative_relocations(uint64_t offset, uint64_t size);
273
275 template<typename ELF_T>
276 LIEF_LOCAL ok_error_t parse_packed_relocations(uint64_t offset, uint64_t size);
277
278 template<typename ELF_T>
279 LIEF_LOCAL ok_error_t process_dynamic_table();
280
283 template<typename ELF_T, typename REL_T>
284 LIEF_LOCAL ok_error_t parse_section_relocations(const Section& section);
285
291 template<typename ELF_T>
292 LIEF_LOCAL ok_error_t parse_symbol_version_requirement(uint64_t offset,
293 uint32_t nb_entries);
294
295
301 template<typename ELF_T>
302 LIEF_LOCAL ok_error_t parse_symbol_version_definition(uint64_t offset,
303 uint32_t nb_entries);
304
305
312 LIEF_LOCAL ok_error_t parse_symbol_version(uint64_t symbol_version_offset);
313
317 template<typename ELF_T>
318 LIEF_LOCAL ok_error_t parse_symbol_gnu_hash(uint64_t offset);
319
321 LIEF_LOCAL ok_error_t parse_notes(uint64_t offset, uint64_t size);
322
323 LIEF_LOCAL std::unique_ptr<Note> get_note(uint32_t type, std::string name,
324 std::vector<uint8_t> desc_bytes);
325
327 LIEF_LOCAL ok_error_t parse_symbol_sysv_hash(uint64_t offset);
328
329 LIEF_LOCAL ok_error_t parse_overlay();
330
331 template<typename ELF_T, typename REL_T>
332 LIEF_LOCAL uint32_t max_relocation_index(uint64_t relocations_offset,
333 uint64_t size) const;
334
336 LIEF_LOCAL static bool check_section_in_segment(const Section& section,
337 const Segment& segment);
338
339 LIEF_LOCAL bool bind_symbol(Relocation& R);
340 LIEF_LOCAL Relocation& insert_relocation(std::unique_ptr<Relocation> R);
341
342 template<class ELF_T>
343 LIEF_LOCAL ok_error_t parse_dyn_table(Segment& pt_dyn);
344
345 std::unique_ptr<BinaryStream> stream_;
346 std::unique_ptr<Binary> binary_;
347 ParserConfig config_;
348 /*
349 * parse_sections() may skip some sections so that
350 * binary_->sections_ is not contiguous based on the index of the sections.
351 *
352 * On the other hand, we need these indexes to bind symbols that
353 * reference sections. That's why we have this unordered_map.
354 */
355 std::unordered_map<size_t, Section*> sections_idx_;
356 uint64_t memory_address_ = 0;
357 std::unordered_set<uint64_t> notes_offset_;
358};
359
360}
361}
362#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
static std::unique_ptr< Binary > parse_from_dump(std::string_view filepath, uint64_t addr, const ParserConfig &conf=ParserConfig::all())
Parse an ELF binary from a memory dump located on disk.
static std::unique_ptr< Binary > parse(std::string_view file, const ParserConfig &conf=ParserConfig::all())
Parse an ELF file and return a LIEF::ELF::Binary object.
static std::unique_ptr< Binary > parse(const PathT &file, const ParserConfig &conf=ParserConfig::all())
Same as parse(std::string_view, const ParserConfig&) but the file is given as a std::filesystem::path...
Definition ELF/Parser.hpp:82
static std::unique_ptr< Binary > parse_from_dump(BinaryStream &stream, uint64_t addr, const ParserConfig &conf=ParserConfig::all())
Same as parse_from_dump(std::string_view, uint64_t, const ParserConfig&) but the dump is wrapped in t...
static constexpr uint32_t NB_MAX_BUCKETS
Definition ELF/Parser.hpp:53
friend class OAT::Parser
Definition ELF/Parser.hpp:48
static constexpr uint32_t NB_MAX_SYMBOLS
Definition ELF/Parser.hpp:51
static std::unique_ptr< Binary > parse_from_dump(const PathT &filepath, uint64_t addr, const ParserConfig &conf=ParserConfig::all())
Same as parse_from_dump(std::string_view, uint64_t, const ParserConfig&) but the dump file is given a...
Definition ELF/Parser.hpp:152
static std::unique_ptr< Binary > parse(std::unique_ptr< BinaryStream > stream, const ParserConfig &conf=ParserConfig::all())
Parse the ELF binary from the given stream and return a LIEF::ELF::Binary object.
static std::unique_ptr< Binary > parse_from_dump(std::unique_ptr< BinaryStream > stream, uint64_t addr, const ParserConfig &conf=ParserConfig::all())
Same as parse_from_dump(std::string_view, uint64_t, const ParserConfig&) but the dump is wrapped in t...
static constexpr uint32_t DELTA_NB_SYMBOLS
Definition ELF/Parser.hpp:52
static constexpr uint32_t NB_MAX_RELOCATIONS
Definition ELF/Parser.hpp:56
static std::unique_ptr< Binary > parse(const std::vector< uint8_t > &data, const ParserConfig &conf=ParserConfig::all())
Parse the given raw data as an ELF binary and return a LIEF::ELF::Binary object.
static std::unique_ptr< Binary > parse_from_memory(uintptr_t address, const ParserConfig &conf=ParserConfig::all())
Parse the ELF binary from the given memory address.
static std::unique_ptr< Binary > parse_from_memory(uintptr_t address, size_t size, const ParserConfig &conf=ParserConfig::all())
Parse the ELF binary from the given memory address with the given size.
static constexpr uint32_t NB_MAX_CHAINS
Definition ELF/Parser.hpp:54
ELF_TYPE
Definition ELF/Parser.hpp:60
@ ELF64
Definition ELF/Parser.hpp:63
@ ELF_UNKNOWN
Definition ELF/Parser.hpp:61
@ ELF32
Definition ELF/Parser.hpp:62
static constexpr uint32_t MAX_SEGMENT_SIZE
Definition ELF/Parser.hpp:58
Parser(const Parser &)=delete
Parser & operator=(const Parser &)=delete
static constexpr uint32_t NB_MAX_SEGMENTS
Definition ELF/Parser.hpp:55
static constexpr uint32_t NB_MAX_DYNAMIC_ENTRIES
Definition ELF/Parser.hpp:57
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:43
Class which represents an ELF Section.
Definition ELF/Section.hpp:49
Class which represents the ELF segments.
Definition Segment.hpp:49
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:34
Class to parse an OAT file to produce an OAT::Binary.
Definition OAT/Parser.hpp:42
Main interface to parse an executable regardless of its format.
Definition Abstract/Parser.hpp:32
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
Class which represents an abstracted section.
Definition Abstract/Section.hpp:31
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:30
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:119
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
Definition ELF/Parser.hpp:34
LIEF namespace.
Definition Abstract/Binary.hpp:41
This structure is used to tweak the ELF Parser (ELF::Parser).
Definition ELF/ParserConfig.hpp:26
DYNSYM_COUNT
Methods that can be used by the LIEF::ELF::Parser to count the number of dynamic symbols.
Definition ELF/ParserConfig.hpp:29
static ParserConfig all()
This returns a ParserConfig object configured to process all the ELF elements.
Definition ELF/ParserConfig.hpp:45
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46