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#include "LIEF/ELF/enums.hpp"
26
28
29namespace LIEF {
30class BinaryStream;
31
32namespace OAT {
33class Parser;
34}
35namespace ELF {
36
37class Section;
38class Binary;
39class Segment;
40class Symbol;
41class Note;
42class Relocation;
43
46 friend class OAT::Parser;
47
48 public:
49 static constexpr uint32_t NB_MAX_SYMBOLS = 1000000;
50 static constexpr uint32_t DELTA_NB_SYMBOLS = 3000;
51 static constexpr uint32_t NB_MAX_BUCKETS = NB_MAX_SYMBOLS;
52 static constexpr uint32_t NB_MAX_CHAINS = 1000000;
53 static constexpr uint32_t NB_MAX_SEGMENTS = 10000;
54 static constexpr uint32_t NB_MAX_RELOCATIONS = 3000000;
55 static constexpr uint32_t NB_MAX_DYNAMIC_ENTRIES = 1000;
56 static constexpr uint32_t MAX_SEGMENT_SIZE = 3_GB;
57
63
73 static std::unique_ptr<Binary>
74 parse(const std::string& file,
75 const ParserConfig& conf = ParserConfig::all());
76
87 static std::unique_ptr<Binary>
88 parse(const std::vector<uint8_t>& data,
89 const ParserConfig& conf = ParserConfig::all());
90
101 static std::unique_ptr<Binary>
102 parse(std::unique_ptr<BinaryStream> stream,
103 const ParserConfig& conf = ParserConfig::all());
104
105 Parser& operator=(const Parser&) = delete;
106 Parser(const Parser&) = delete;
107
108 ~Parser() override;
109
110 protected:
112 LIEF_LOCAL Parser(std::unique_ptr<BinaryStream> stream, ParserConfig config);
113 LIEF_LOCAL Parser(const std::string& file, ParserConfig config);
114 LIEF_LOCAL Parser(const std::vector<uint8_t>& data, ParserConfig config);
115
116 LIEF_LOCAL ok_error_t init();
117
118 LIEF_LOCAL bool should_swap() const;
119
120 // map, dynamic_symbol.version <----> symbol_version
121 // symbol_version comes from symbol_version table
122 LIEF_LOCAL void link_symbol_version();
123
124 LIEF_LOCAL ok_error_t link_symbol_section(Symbol& sym);
125
126 template<typename ELF_T>
127 LIEF_LOCAL ok_error_t parse_binary();
128
129 template<typename ELF_T>
130 LIEF_LOCAL ok_error_t parse_header();
131
132 template<typename ELF_T>
133 LIEF_LOCAL ok_error_t parse_sections();
134
135 template<typename ELF_T>
136 LIEF_LOCAL ok_error_t parse_segments();
137
138 LIEF_LOCAL uint64_t
139 get_dynamic_string_table(BinaryStream* stream = nullptr) const;
140
142 get_dynamic_string_table_from_segments(BinaryStream* stream = nullptr) const;
143
144 LIEF_LOCAL uint64_t get_dynamic_string_table_from_sections() const;
145
147 template<typename ELF_T>
149 get_numberof_dynamic_symbols(ParserConfig::DYNSYM_COUNT mtd) const;
150
152 template<typename ELF_T>
153 LIEF_LOCAL result<uint32_t> nb_dynsym_hash() const;
154
156 template<typename ELF_T>
157 LIEF_LOCAL result<uint32_t> nb_dynsym_sysv_hash() const;
158
160 template<typename ELF_T>
161 LIEF_LOCAL result<uint32_t> nb_dynsym_gnu_hash() const;
162
164 template<typename ELF_T>
165 LIEF_LOCAL result<uint32_t> nb_dynsym_section() const;
166
168 template<typename ELF_T>
169 LIEF_LOCAL result<uint32_t> nb_dynsym_relocations() const;
170
171 template<typename ELF_T>
172 LIEF_LOCAL ok_error_t parse_dynamic_entries(BinaryStream& stream);
173
174 template<typename ELF_T>
175 LIEF_LOCAL ok_error_t parse_dynamic_symbols(uint64_t offset);
176
186 template<typename ELF_T>
187 LIEF_LOCAL ok_error_t parse_symtab_symbols(const Section& symtab_section,
188 const Section& string_section);
189
193 template<typename ELF_T, typename REL_T>
194 LIEF_LOCAL ok_error_t parse_dynamic_relocations(uint64_t relocations_offset,
195 uint64_t size);
196
202 template<typename ELF_T, typename REL_T>
203 LIEF_LOCAL ok_error_t parse_pltgot_relocations(uint64_t offset, uint64_t size);
204
205
207 template<typename ELF_T>
208 LIEF_LOCAL ok_error_t parse_relative_relocations(uint64_t offset, uint64_t size);
209
211 template<typename ELF_T>
212 LIEF_LOCAL ok_error_t parse_packed_relocations(uint64_t offset, uint64_t size);
213
214 template<typename ELF_T>
215 LIEF_LOCAL ok_error_t process_dynamic_table();
216
219 template<typename ELF_T, typename REL_T>
220 LIEF_LOCAL ok_error_t parse_section_relocations(const Section& section);
221
227 template<typename ELF_T>
228 LIEF_LOCAL ok_error_t parse_symbol_version_requirement(uint64_t offset,
229 uint32_t nb_entries);
230
231
237 template<typename ELF_T>
238 LIEF_LOCAL ok_error_t parse_symbol_version_definition(uint64_t offset,
239 uint32_t nb_entries);
240
241
248 LIEF_LOCAL ok_error_t parse_symbol_version(uint64_t symbol_version_offset);
249
253 template<typename ELF_T>
254 LIEF_LOCAL ok_error_t parse_symbol_gnu_hash(uint64_t offset);
255
257 LIEF_LOCAL ok_error_t parse_notes(uint64_t offset, uint64_t size);
258
259 LIEF_LOCAL std::unique_ptr<Note> get_note(uint32_t type, std::string name,
260 std::vector<uint8_t> desc_bytes);
261
263 LIEF_LOCAL ok_error_t parse_symbol_sysv_hash(uint64_t offset);
264
265 LIEF_LOCAL ok_error_t parse_overlay();
266
267 template<typename ELF_T, typename REL_T>
268 LIEF_LOCAL uint32_t max_relocation_index(uint64_t relocations_offset,
269 uint64_t size) const;
270
272 LIEF_LOCAL static bool check_section_in_segment(const Section& section,
273 const Segment& segment);
274
275 LIEF_LOCAL bool bind_symbol(Relocation& R);
276 LIEF_LOCAL Relocation& insert_relocation(std::unique_ptr<Relocation> R);
277
278 template<class ELF_T>
279 LIEF_LOCAL ok_error_t parse_dyn_table(Segment& pt_dyn);
280
281 std::unique_ptr<BinaryStream> stream_;
282 std::unique_ptr<Binary> binary_;
283 ParserConfig config_;
284 /*
285 * parse_sections() may skip some sections so that
286 * binary_->sections_ is not contiguous based on the index of the sections.
287 *
288 * On the other hand, we need these indexes to bind symbols that
289 * reference sections. That's why we have this unordered_map.
290 */
291 std::unordered_map<size_t, Section*> sections_idx_;
292};
293
294} // namespace ELF
295} // namespace LIEF
296#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:59
static constexpr uint32_t NB_MAX_BUCKETS
Definition ELF/Parser.hpp:51
friend class OAT::Parser
Definition ELF/Parser.hpp:46
static constexpr uint32_t NB_MAX_SYMBOLS
Definition ELF/Parser.hpp:49
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 constexpr uint32_t DELTA_NB_SYMBOLS
Definition ELF/Parser.hpp:50
static constexpr uint32_t NB_MAX_RELOCATIONS
Definition ELF/Parser.hpp:54
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(const std::string &file, const ParserConfig &conf=ParserConfig::all())
Parse an ELF file and return a LIEF::ELF::Binary object.
static constexpr uint32_t NB_MAX_CHAINS
Definition ELF/Parser.hpp:52
~Parser() override
ELF_TYPE
Definition ELF/Parser.hpp:58
@ ELF64
Definition ELF/Parser.hpp:61
@ ELF_UNKNOWN
Definition ELF/Parser.hpp:59
@ ELF32
Definition ELF/Parser.hpp:60
static constexpr uint32_t MAX_SEGMENT_SIZE
Definition ELF/Parser.hpp:56
Parser(const Parser &)=delete
Parser & operator=(const Parser &)=delete
static constexpr uint32_t NB_MAX_SEGMENTS
Definition ELF/Parser.hpp:53
static constexpr uint32_t NB_MAX_DYNAMIC_ENTRIES
Definition ELF/Parser.hpp:55
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
Class which represents an ELF Section.
Definition ELF/Section.hpp:48
Class which represents the ELF segments.
Definition Segment.hpp:48
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
Class to parse an OAT file to produce an OAT::Binary.
Definition OAT/Parser.hpp:38
Main interface to parse an executable regardless of its format.
Definition Abstract/Parser.hpp:30
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
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:114
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:77
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
Definition ELF/Parser.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:40
This structure is used to tweak the ELF Parser (ELF::Parser).
Definition ELF/ParserConfig.hpp:25
DYNSYM_COUNT
Definition ELF/ParserConfig.hpp:28
static ParserConfig all()
This returns a ParserConfig object configured to process all the ELF elements.
Definition ELF/ParserConfig.hpp:38
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44