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_SECTION_SEGMENT_LINKS = 1000000;
57 static constexpr uint32_t NB_MAX_RELOCATIONS = 3000000;
58 static constexpr uint32_t NB_MAX_DYNAMIC_ENTRIES = 1000;
59 static constexpr uint32_t MAX_SEGMENT_SIZE = 3_GB;
60
66
76 static std::unique_ptr<Binary>
77 parse(std::string_view file, const ParserConfig& conf = ParserConfig::all());
78
81 template<class PathT, enable_if_path_t<PathT> = 0>
82 static std::unique_ptr<Binary>
83 parse(const PathT& file, const ParserConfig& conf = ParserConfig::all()) {
84 return parse(file.string(), conf);
85 }
86
97 static std::unique_ptr<Binary>
98 parse(const std::vector<uint8_t>& data,
99 const ParserConfig& conf = ParserConfig::all());
100
111 static std::unique_ptr<Binary>
112 parse(std::unique_ptr<BinaryStream> stream,
113 const ParserConfig& conf = ParserConfig::all());
114
121 static std::unique_ptr<Binary>
122 parse_from_memory(uintptr_t address,
123 const ParserConfig& conf = ParserConfig::all());
124
132 static std::unique_ptr<Binary>
133 parse_from_memory(uintptr_t address, size_t size,
134 const ParserConfig& conf = ParserConfig::all());
135
145 static std::unique_ptr<Binary>
146 parse_from_dump(std::string_view filepath, uint64_t addr,
147 const ParserConfig& conf = ParserConfig::all());
148
151 template<class PathT, enable_if_path_t<PathT> = 0>
152 static std::unique_ptr<Binary>
153 parse_from_dump(const PathT& filepath, uint64_t addr,
154 const ParserConfig& conf = ParserConfig::all()) {
155 return parse_from_dump(filepath.string(), addr, conf);
156 }
157
160 static std::unique_ptr<Binary>
161 parse_from_dump(BinaryStream& stream, uint64_t addr,
162 const ParserConfig& conf = ParserConfig::all());
163
166 static std::unique_ptr<Binary>
167 parse_from_dump(std::unique_ptr<BinaryStream> stream, uint64_t addr,
168 const ParserConfig& conf = ParserConfig::all());
169
170 Parser& operator=(const Parser&) = delete;
171 Parser(const Parser&) = delete;
172
174
175 protected:
177 LIEF_LOCAL Parser(std::unique_ptr<BinaryStream> stream, ParserConfig config);
178 LIEF_LOCAL Parser(std::string_view file, ParserConfig config);
179 LIEF_LOCAL Parser(const std::vector<uint8_t>& data, ParserConfig config);
180
181 LIEF_LOCAL ok_error_t init();
182
183 LIEF_LOCAL bool should_swap() const;
184
185 // map, dynamic_symbol.version <----> symbol_version
186 // symbol_version comes from symbol_version table
187 LIEF_LOCAL void link_symbol_version();
188
189 LIEF_LOCAL ok_error_t link_symbol_section(Symbol& sym);
190
191 template<typename ELF_T>
192 LIEF_LOCAL ok_error_t parse_binary();
193
194 template<typename ELF_T>
195 LIEF_LOCAL ok_error_t parse_header();
196
197 template<typename ELF_T>
198 LIEF_LOCAL ok_error_t parse_sections();
199
200 template<typename ELF_T>
201 LIEF_LOCAL ok_error_t parse_segments();
202
203 LIEF_LOCAL uint64_t
204 get_dynamic_string_table(BinaryStream* stream = nullptr) const;
205
207 get_dynamic_string_table_from_segments(BinaryStream* stream = nullptr) const;
208
209 LIEF_LOCAL uint64_t get_dynamic_string_table_from_sections() const;
210
212 template<typename ELF_T>
214 get_numberof_dynamic_symbols(ParserConfig::DYNSYM_COUNT mtd) const;
215
217 template<typename ELF_T>
218 LIEF_LOCAL result<uint32_t> nb_dynsym_hash() const;
219
221 template<typename ELF_T>
222 LIEF_LOCAL result<uint32_t> nb_dynsym_sysv_hash() const;
223
225 template<typename ELF_T>
226 LIEF_LOCAL result<uint32_t> nb_dynsym_gnu_hash() const;
227
229 template<typename ELF_T>
230 LIEF_LOCAL result<uint32_t> nb_dynsym_section() const;
231
233 template<typename ELF_T>
234 LIEF_LOCAL result<uint32_t> nb_dynsym_relocations() const;
235
236 template<typename ELF_T>
237 LIEF_LOCAL ok_error_t parse_dynamic_entries(BinaryStream& stream);
238
239 template<typename ELF_T>
240 LIEF_LOCAL ok_error_t parse_dynamic_symbols(uint64_t offset);
241
251 template<typename ELF_T>
252 LIEF_LOCAL ok_error_t parse_symtab_symbols(const Section& symtab_section,
253 const Section& string_section);
254
258 template<typename ELF_T, typename REL_T>
259 LIEF_LOCAL ok_error_t parse_dynamic_relocations(uint64_t relocations_offset,
260 uint64_t size);
261
267 template<typename ELF_T, typename REL_T>
268 LIEF_LOCAL ok_error_t parse_pltgot_relocations(uint64_t offset, uint64_t size);
269
270
272 template<typename ELF_T>
273 LIEF_LOCAL ok_error_t parse_relative_relocations(uint64_t offset, uint64_t size);
274
276 template<typename ELF_T>
277 LIEF_LOCAL ok_error_t parse_packed_relocations(uint64_t offset, uint64_t size);
278
279 template<typename ELF_T>
280 LIEF_LOCAL ok_error_t process_dynamic_table();
281
284 template<typename ELF_T, typename REL_T>
285 LIEF_LOCAL ok_error_t parse_section_relocations(const Section& section);
286
292 template<typename ELF_T>
293 LIEF_LOCAL ok_error_t parse_symbol_version_requirement(uint64_t offset,
294 uint32_t nb_entries);
295
296
302 template<typename ELF_T>
303 LIEF_LOCAL ok_error_t parse_symbol_version_definition(uint64_t offset,
304 uint32_t nb_entries);
305
306
313 LIEF_LOCAL ok_error_t parse_symbol_version(uint64_t symbol_version_offset);
314
318 template<typename ELF_T>
319 LIEF_LOCAL ok_error_t parse_symbol_gnu_hash(uint64_t offset);
320
322 LIEF_LOCAL ok_error_t parse_notes(uint64_t offset, uint64_t size);
323
324 LIEF_LOCAL std::unique_ptr<Note> get_note(uint32_t type, std::string name,
325 std::vector<uint8_t> desc_bytes);
326
328 LIEF_LOCAL ok_error_t parse_symbol_sysv_hash(uint64_t offset);
329
330 LIEF_LOCAL ok_error_t parse_overlay();
331
332 template<typename ELF_T, typename REL_T>
333 LIEF_LOCAL uint32_t max_relocation_index(uint64_t relocations_offset,
334 uint64_t size) const;
335
337 LIEF_LOCAL static bool check_section_in_segment(const Section& section,
338 const Segment& segment);
339
340 LIEF_LOCAL bool bind_symbol(Relocation& R);
341 LIEF_LOCAL Relocation& insert_relocation(std::unique_ptr<Relocation> R);
342
343 template<class ELF_T>
344 LIEF_LOCAL ok_error_t parse_dyn_table(Segment& pt_dyn);
345
346 std::unique_ptr<BinaryStream> stream_;
347 std::unique_ptr<Binary> binary_;
348 ParserConfig config_;
349 /*
350 * parse_sections() may skip some sections so that
351 * binary_->sections_ is not contiguous based on the index of the sections.
352 *
353 * On the other hand, we need these indexes to bind symbols that
354 * reference sections. That's why we have this unordered_map.
355 */
356 std::unordered_map<size_t, Section*> sections_idx_;
357 uint64_t memory_address_ = 0;
358 std::unordered_set<uint64_t> notes_offset_;
359};
360
361}
362}
363#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:61
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:83
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
static constexpr uint32_t NB_MAX_SECTION_SEGMENT_LINKS
Definition ELF/Parser.hpp:56
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:153
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:57
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:61
@ ELF64
Definition ELF/Parser.hpp:64
@ ELF_UNKNOWN
Definition ELF/Parser.hpp:62
@ ELF32
Definition ELF/Parser.hpp:63
static constexpr uint32_t MAX_SEGMENT_SIZE
Definition ELF/Parser.hpp:59
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:58
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:43
Class which represents an ELF Section.
Definition ELF/Section.hpp:50
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:42
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