LIEF: Library to Instrument Executable Formats Version 1.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 <unordered_map>
19
20#include "LIEF/visibility.h"
21#include "LIEF/utils.hpp"
22
24#include "LIEF/errors.hpp"
25
27
28namespace LIEF {
29class BinaryStream;
30
31namespace OAT {
32class Parser;
33}
34namespace ELF {
35
36class Section;
37class Binary;
38class Segment;
39class Symbol;
40class Note;
41class Relocation;
42
45 friend class OAT::Parser;
46
47 public:
48 static constexpr uint32_t NB_MAX_SYMBOLS = 1000000;
49 static constexpr uint32_t DELTA_NB_SYMBOLS = 3000;
50 static constexpr uint32_t NB_MAX_BUCKETS = NB_MAX_SYMBOLS;
51 static constexpr uint32_t NB_MAX_CHAINS = 1000000;
52 static constexpr uint32_t NB_MAX_SEGMENTS = 10000;
53 static constexpr uint32_t NB_MAX_RELOCATIONS = 3000000;
54 static constexpr uint32_t NB_MAX_DYNAMIC_ENTRIES = 1000;
55 static constexpr uint32_t MAX_SEGMENT_SIZE = 3_GB;
56
62
72 static std::unique_ptr<Binary>
73 parse(const std::string& file,
74 const ParserConfig& conf = ParserConfig::all());
75
86 static std::unique_ptr<Binary>
87 parse(const std::vector<uint8_t>& data,
88 const ParserConfig& conf = ParserConfig::all());
89
100 static std::unique_ptr<Binary>
101 parse(std::unique_ptr<BinaryStream> stream,
102 const ParserConfig& conf = ParserConfig::all());
103
110 static std::unique_ptr<Binary>
111 parse_from_memory(uintptr_t address,
112 const ParserConfig& conf = ParserConfig::all());
113
121 static std::unique_ptr<Binary>
122 parse_from_memory(uintptr_t address, size_t size,
123 const ParserConfig& conf = ParserConfig::all());
124
134 static std::unique_ptr<Binary>
135 parse_from_dump(const std::string& filepath, uint64_t addr,
136 const ParserConfig& conf = ParserConfig::all());
137
140 static std::unique_ptr<Binary>
141 parse_from_dump(BinaryStream& stream, uint64_t addr,
142 const ParserConfig& conf = ParserConfig::all());
143
146 static std::unique_ptr<Binary>
147 parse_from_dump(std::unique_ptr<BinaryStream> stream, uint64_t addr,
148 const ParserConfig& conf = ParserConfig::all());
149
150 Parser& operator=(const Parser&) = delete;
151 Parser(const Parser&) = delete;
152
153 ~Parser() override;
154
155 protected:
157 LIEF_LOCAL Parser(std::unique_ptr<BinaryStream> stream, ParserConfig config);
158 LIEF_LOCAL Parser(const std::string& file, ParserConfig config);
159 LIEF_LOCAL Parser(const std::vector<uint8_t>& data, ParserConfig config);
160
161 LIEF_LOCAL ok_error_t init();
162
163 LIEF_LOCAL bool should_swap() const;
164
165 // map, dynamic_symbol.version <----> symbol_version
166 // symbol_version comes from symbol_version table
167 LIEF_LOCAL void link_symbol_version();
168
169 LIEF_LOCAL ok_error_t link_symbol_section(Symbol& sym);
170
171 template<typename ELF_T>
172 LIEF_LOCAL ok_error_t parse_binary();
173
174 template<typename ELF_T>
175 LIEF_LOCAL ok_error_t parse_header();
176
177 template<typename ELF_T>
178 LIEF_LOCAL ok_error_t parse_sections();
179
180 template<typename ELF_T>
181 LIEF_LOCAL ok_error_t parse_segments();
182
183 LIEF_LOCAL uint64_t
184 get_dynamic_string_table(BinaryStream* stream = nullptr) const;
185
187 get_dynamic_string_table_from_segments(BinaryStream* stream = nullptr) const;
188
189 LIEF_LOCAL uint64_t get_dynamic_string_table_from_sections() const;
190
192 template<typename ELF_T>
194 get_numberof_dynamic_symbols(ParserConfig::DYNSYM_COUNT mtd) const;
195
197 template<typename ELF_T>
198 LIEF_LOCAL result<uint32_t> nb_dynsym_hash() const;
199
201 template<typename ELF_T>
202 LIEF_LOCAL result<uint32_t> nb_dynsym_sysv_hash() const;
203
205 template<typename ELF_T>
206 LIEF_LOCAL result<uint32_t> nb_dynsym_gnu_hash() const;
207
209 template<typename ELF_T>
210 LIEF_LOCAL result<uint32_t> nb_dynsym_section() const;
211
213 template<typename ELF_T>
214 LIEF_LOCAL result<uint32_t> nb_dynsym_relocations() const;
215
216 template<typename ELF_T>
217 LIEF_LOCAL ok_error_t parse_dynamic_entries(BinaryStream& stream);
218
219 template<typename ELF_T>
220 LIEF_LOCAL ok_error_t parse_dynamic_symbols(uint64_t offset);
221
231 template<typename ELF_T>
232 LIEF_LOCAL ok_error_t parse_symtab_symbols(const Section& symtab_section,
233 const Section& string_section);
234
238 template<typename ELF_T, typename REL_T>
239 LIEF_LOCAL ok_error_t parse_dynamic_relocations(uint64_t relocations_offset,
240 uint64_t size);
241
247 template<typename ELF_T, typename REL_T>
248 LIEF_LOCAL ok_error_t parse_pltgot_relocations(uint64_t offset, uint64_t size);
249
250
252 template<typename ELF_T>
253 LIEF_LOCAL ok_error_t parse_relative_relocations(uint64_t offset, uint64_t size);
254
256 template<typename ELF_T>
257 LIEF_LOCAL ok_error_t parse_packed_relocations(uint64_t offset, uint64_t size);
258
259 template<typename ELF_T>
260 LIEF_LOCAL ok_error_t process_dynamic_table();
261
264 template<typename ELF_T, typename REL_T>
265 LIEF_LOCAL ok_error_t parse_section_relocations(const Section& section);
266
272 template<typename ELF_T>
273 LIEF_LOCAL ok_error_t parse_symbol_version_requirement(uint64_t offset,
274 uint32_t nb_entries);
275
276
282 template<typename ELF_T>
283 LIEF_LOCAL ok_error_t parse_symbol_version_definition(uint64_t offset,
284 uint32_t nb_entries);
285
286
293 LIEF_LOCAL ok_error_t parse_symbol_version(uint64_t symbol_version_offset);
294
298 template<typename ELF_T>
299 LIEF_LOCAL ok_error_t parse_symbol_gnu_hash(uint64_t offset);
300
302 LIEF_LOCAL ok_error_t parse_notes(uint64_t offset, uint64_t size);
303
304 LIEF_LOCAL std::unique_ptr<Note> get_note(uint32_t type, std::string name,
305 std::vector<uint8_t> desc_bytes);
306
308 LIEF_LOCAL ok_error_t parse_symbol_sysv_hash(uint64_t offset);
309
310 LIEF_LOCAL ok_error_t parse_overlay();
311
312 template<typename ELF_T, typename REL_T>
313 LIEF_LOCAL uint32_t max_relocation_index(uint64_t relocations_offset,
314 uint64_t size) const;
315
317 LIEF_LOCAL static bool check_section_in_segment(const Section& section,
318 const Segment& segment);
319
320 LIEF_LOCAL bool bind_symbol(Relocation& R);
321 LIEF_LOCAL Relocation& insert_relocation(std::unique_ptr<Relocation> R);
322
323 template<class ELF_T>
324 LIEF_LOCAL ok_error_t parse_dyn_table(Segment& pt_dyn);
325
326 std::unique_ptr<BinaryStream> stream_;
327 std::unique_ptr<Binary> binary_;
328 ParserConfig config_;
329 /*
330 * parse_sections() may skip some sections so that
331 * binary_->sections_ is not contiguous based on the index of the sections.
332 *
333 * On the other hand, we need these indexes to bind symbols that
334 * reference sections. That's why we have this unordered_map.
335 */
336 std::unordered_map<size_t, Section*> sections_idx_;
337 uint64_t memory_address_ = 0;
338};
339
340} // namespace ELF
341} // namespace LIEF
342#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(const std::string &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_from_dump(BinaryStream &stream, uint64_t addr, const ParserConfig &conf=ParserConfig::all())
Same as parse_from_dump(const std::string&, uint64_t, const ParserConfig&) but the dump is wrapped in...
static constexpr uint32_t NB_MAX_BUCKETS
Definition ELF/Parser.hpp:50
friend class OAT::Parser
Definition ELF/Parser.hpp:45
static constexpr uint32_t NB_MAX_SYMBOLS
Definition ELF/Parser.hpp:48
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(const std::string&, uint64_t, const ParserConfig&) but the dump is wrapped in...
static constexpr uint32_t DELTA_NB_SYMBOLS
Definition ELF/Parser.hpp:49
static constexpr uint32_t NB_MAX_RELOCATIONS
Definition ELF/Parser.hpp:53
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(const std::string &file, const ParserConfig &conf=ParserConfig::all())
Parse an ELF file and return a LIEF::ELF::Binary object.
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:51
~Parser() override
ELF_TYPE
Definition ELF/Parser.hpp:57
@ ELF64
Definition ELF/Parser.hpp:60
@ ELF_UNKNOWN
Definition ELF/Parser.hpp:58
@ ELF32
Definition ELF/Parser.hpp:59
static constexpr uint32_t MAX_SEGMENT_SIZE
Definition ELF/Parser.hpp:55
Parser(const Parser &)=delete
Parser & operator=(const Parser &)=delete
static constexpr uint32_t NB_MAX_SEGMENTS
Definition ELF/Parser.hpp:52
static constexpr uint32_t NB_MAX_DYNAMIC_ENTRIES
Definition ELF/Parser.hpp:54
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
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:40
Main interface to parse an executable regardless of its format.
Definition Abstract/Parser.hpp:31
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
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:31
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