LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ELF/Parser.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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;
43class LIEF_API Parser : public LIEF::Parser {
46 friend class OAT::Parser;
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 NB_MAX_MASKWORD = 512;
56 static constexpr uint32_t MAX_SEGMENT_SIZE = 3_GB;
57
58 enum ELF_TYPE {
59 ELF_UNKNOWN,
60 ELF32, ELF64
61 };
62 static std::unique_ptr<Binary> parse(const std::string& file,
72 const ParserConfig& conf = ParserConfig::all());
73 static std::unique_ptr<Binary> parse(const std::vector<uint8_t>& data,
83 const ParserConfig& conf = ParserConfig::all());
84 static std::unique_ptr<Binary> parse(std::unique_ptr<BinaryStream> stream,
94 const ParserConfig& conf = ParserConfig::all());
95
96 Parser& operator=(const Parser&) = delete;
97 Parser(const Parser&) = delete;
98
99 ~Parser() override;
100
101 protected:
102 LIEF_LOCAL Parser();
103 LIEF_LOCAL Parser(std::unique_ptr<BinaryStream> stream, ParserConfig config);
104 LIEF_LOCAL Parser(const std::string& file, ParserConfig config);
105 LIEF_LOCAL Parser(const std::vector<uint8_t>& data, ParserConfig config);
106
107 LIEF_LOCAL ok_error_t init();
108
109 LIEF_LOCAL bool should_swap() const;
110
111 // map, dynamic_symbol.version <----> symbol_version
112 // symbol_version comes from symbol_version table
113 LIEF_LOCAL void link_symbol_version();
114
115 LIEF_LOCAL ok_error_t link_symbol_section(Symbol& sym);
116
117 template<typename ELF_T>
118 LIEF_LOCAL ok_error_t parse_binary();
119
120 template<typename ELF_T>
121 LIEF_LOCAL ok_error_t parse_header();
122
123 template<typename ELF_T>
124 LIEF_LOCAL ok_error_t parse_sections();
125
126 template<typename ELF_T>
127 LIEF_LOCAL ok_error_t parse_segments();
128
129 LIEF_LOCAL uint64_t get_dynamic_string_table() const;
130
131 LIEF_LOCAL result<uint64_t> get_dynamic_string_table_from_segments() const;
132
133 LIEF_LOCAL uint64_t get_dynamic_string_table_from_sections() const;
134 template<typename ELF_T>
137 LIEF_LOCAL result<uint32_t>
138 get_numberof_dynamic_symbols(ParserConfig::DYNSYM_COUNT mtd) const;
139 template<typename ELF_T>
142 LIEF_LOCAL result<uint32_t> nb_dynsym_hash() const;
143 template<typename ELF_T>
146 LIEF_LOCAL result<uint32_t> nb_dynsym_sysv_hash() const;
147 template<typename ELF_T>
150 LIEF_LOCAL result<uint32_t> nb_dynsym_gnu_hash() const;
151 template<typename ELF_T>
154 LIEF_LOCAL result<uint32_t> nb_dynsym_section() const;
155 template<typename ELF_T>
158 LIEF_LOCAL result<uint32_t> nb_dynsym_relocations() const;
159
160 template<typename ELF_T>
161 LIEF_LOCAL ok_error_t parse_dynamic_entries(uint64_t offset, uint64_t size);
162
163 template<typename ELF_T>
164 LIEF_LOCAL ok_error_t parse_dynamic_symbols(uint64_t offset);
165 template<typename ELF_T>
176 parse_symtab_symbols(uint64_t offset, uint32_t nb_symbols,
177 const Section& string_section);
178 template<typename ELF_T, typename REL_T>
184 parse_dynamic_relocations(uint64_t relocations_offset, uint64_t size);
185 template<typename ELF_T, typename REL_T>
192 LIEF_LOCAL ok_error_t parse_pltgot_relocations(uint64_t offset, uint64_t size);
193
194 template<typename ELF_T>
197 LIEF_LOCAL ok_error_t parse_relative_relocations(uint64_t offset, uint64_t size);
198 template<typename ELF_T>
201 LIEF_LOCAL ok_error_t parse_packed_relocations(uint64_t offset, uint64_t size);
202
203 template<typename ELF_T>
204 LIEF_LOCAL ok_error_t process_dynamic_table();
205 template<typename ELF_T, typename REL_T>
210 parse_section_relocations(const Section& section);
211 template<typename ELF_T>
219 parse_symbol_version_requirement(uint64_t offset, uint32_t nb_entries);
220
221 template<typename ELF_T>
229 parse_symbol_version_definition(uint64_t offset, uint32_t nb_entries);
230
231 LIEF_LOCAL ok_error_t parse_symbol_version(uint64_t symbol_version_offset);
239 template<typename ELF_T>
244 LIEF_LOCAL ok_error_t parse_symbol_gnu_hash(uint64_t offset);
245 LIEF_LOCAL ok_error_t parse_notes(uint64_t offset, uint64_t size);
248
249 LIEF_LOCAL std::unique_ptr<Note>
250 get_note(uint32_t type, std::string name, std::vector<uint8_t> desc_bytes);
251 LIEF_LOCAL ok_error_t parse_symbol_sysv_hash(uint64_t offset);
254
255 LIEF_LOCAL ok_error_t parse_overlay();
256
257 template<typename ELF_T, typename REL_T>
258 LIEF_LOCAL uint32_t max_relocation_index(uint64_t relocations_offset, uint64_t size) const;
259 LIEF_LOCAL static bool check_section_in_segment(const Section& section, const Segment& segment);
262
263 LIEF_LOCAL bool bind_symbol(Relocation& R);
264 LIEF_LOCAL Relocation& insert_relocation(std::unique_ptr<Relocation> R);
265
266 std::unique_ptr<BinaryStream> stream_;
267 std::unique_ptr<Binary> binary_;
268 ParserConfig config_;
269 /*
270 * parse_sections() may skip some sections so that
271 * binary_->sections_ is not contiguous based on the index of the sections.
272 *
273 * On the other hand, we need these indexes to bind symbols that
274 * reference sections. That's why we have this unordered_map.
275 */
276 std::unordered_map<size_t, Section*> sections_idx_;
277};
278
279} // namespace ELF
280} // namespace LIEF
281#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:39
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
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(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.
~Parser() override
Parser(const Parser &)=delete
Parser & operator=(const Parser &)=delete
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
Class wich represents an ELF Section.
Definition ELF/Section.hpp:48
Class which represents the ELF segments.
Definition Segment.hpp:46
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
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
Definition ELF/Parser.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42