LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
Note.hpp
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_NOTE_H
17#define LIEF_ELF_NOTE_H
18
19#include <vector>
20#include <ostream>
21#include <memory>
22
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/errors.hpp"
26#include "LIEF/span.hpp"
27
28#include "LIEF/ELF/Header.hpp"
29
30namespace LIEF {
31class BinaryStream;
32namespace ELF {
33class Binary;
34class Parser;
35class Builder;
36
39class LIEF_API Note : public Object {
40 friend class Parser;
41 friend class Builder;
42 friend class Binary;
43
44 public:
46 using description_t = std::vector<uint8_t>;
47
49 enum class TYPE {
50 UNKNOWN = 0,
54 GNU_ABI_TAG,
56 GNU_HWCAP,
58 GNU_BUILD_ID,
60 GNU_GOLD_VERSION,
63 GNU_PROPERTY_TYPE_0,
64
65 GNU_BUILD_ATTRIBUTE_OPEN,
66 GNU_BUILD_ATTRIBUTE_FUNC,
67
69 CRASHPAD,
70
72 CORE_PRSTATUS,
73 CORE_FPREGSET,
77 CORE_PRPSINFO,
78 CORE_TASKSTRUCT,
82 CORE_AUXV,
83 CORE_PSTATUS,
85 CORE_FPREGS,
87 CORE_PSINFO,
88 CORE_LWPSTATUS,
89 CORE_LWPSINFO,
90 CORE_WIN32PSTATUS,
91 CORE_FILE,
92 CORE_PRXFPREG,
93 CORE_SIGINFO,
94
95 CORE_ARM_VFP,
96 CORE_ARM_TLS,
97 CORE_ARM_HW_BREAK,
98 CORE_ARM_HW_WATCH,
99 CORE_ARM_SYSTEM_CALL,
100 CORE_ARM_SVE,
101 CORE_ARM_PAC_MASK,
102 CORE_ARM_PACA_KEYS,
103 CORE_ARM_PACG_KEYS,
104 CORE_TAGGED_ADDR_CTRL,
105 CORE_PAC_ENABLED_KEYS,
106
107 CORE_X86_TLS,
108 CORE_X86_IOPERM,
109 CORE_X86_XSTATE,
110 CORE_X86_CET,
111
116 ANDROID_IDENT,
117 ANDROID_MEMTAG,
118 ANDROID_KUSER,
119
121 GO_BUILDID,
123 STAPSDT,
124 };
125
126 public:
128 static result<TYPE> convert_type(Header::FILE_TYPE ftype, uint32_t type,
129 const std::string& name);
130
134
135 static result<const char*> note_to_section(const Note& note) {
136 return type_to_section(note.type());
137 }
138
141
145 static std::unique_ptr<Note> create(
146 const std::string& name, uint32_t type, description_t description,
147 Header::FILE_TYPE ftype = Header::FILE_TYPE::NONE, ARCH arch = ARCH::NONE,
148 Header::CLASS cls = Header::CLASS::NONE);
149
153 static std::unique_ptr<Note> create(
154 const std::string& name, TYPE type, description_t description,
155 ARCH arch = ARCH::NONE, Header::CLASS cls = Header::CLASS::NONE);
156
160 static std::unique_ptr<Note> create(BinaryStream& stream,
161 Header::FILE_TYPE ftype = Header::FILE_TYPE::NONE, ARCH arch = ARCH::NONE,
162 Header::CLASS cls = Header::CLASS::NONE);
163
164 Note& operator=(const Note& copy) = default;
165 Note(const Note& copy) = default;
166
167 ~Note() override = default;
168
170 virtual std::unique_ptr<Note> clone() const {
171 return std::unique_ptr<Note>(new Note(*this));
172 }
173
175 const std::string& name() const {
176 return name_;
177 }
178
181 TYPE type() const {
182 return type_;
183 }
184
187 uint32_t original_type() const {
188 return original_type_;
189 }
190
192 span<const uint8_t> description() const {
193 return description_;
194 }
195
196 span<uint8_t> description() {
197 return description_;
198 }
199
200 void name(std::string name) {
201 name_ = std::move(name);
202 }
203
205 void description(description_t description) {
206 description_ = std::move(description);
207 }
208
210 uint64_t size() const;
211
212 virtual void dump(std::ostream& os) const;
213
214 void accept(Visitor& visitor) const override;
215
216 LIEF_API friend
217 std::ostream& operator<<(std::ostream& os, const Note& note) {
218 note.dump(os);
219 return os;
220 }
221
222 protected:
223 Note() = default;
224 Note(std::string name, TYPE type, uint32_t original_type,
225 description_t description) :
226 name_(std::move(name)),
227 type_(type),
228 original_type_(original_type),
229 description_(std::move(description))
230 {}
231
232 template<class T>
233 LIEF_LOCAL result<T> read_at(size_t offset) const;
234
235 template<class T>
236 LIEF_LOCAL ok_error_t write_at(size_t offset, const T& value);
237
238 LIEF_LOCAL ok_error_t write_string_at(size_t offset, const std::string& value);
239
240 LIEF_LOCAL result<std::string>
241 read_string_at(size_t offset, size_t maxsize = 0) const;
242
243 std::string name_;
244 TYPE type_ = TYPE::UNKNOWN;
245 uint32_t original_type_ = 0;
246 description_t description_;
247};
248
249LIEF_API const char* to_string(Note::TYPE type);
250
251
252} // namepsace ELF
253} // namespace LIEF
254#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:51
FILE_TYPE
The type of the underlying ELF file. This enum matches the semantic of ET_NONE, ET_REL,...
Definition ELF/Header.hpp:61
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS]
Definition ELF/Header.hpp:76
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:39
static std::unique_ptr< Note > create(const std::string &name, TYPE type, description_t description, ARCH arch=ARCH::NONE, Header::CLASS cls=Header::CLASS::NONE)
Create a new note from the given parameters. Additional information such as the architecture or the E...
std::vector< uint8_t > description_t
Container used to handle the description data.
Definition Note.hpp:46
TYPE
LIEF representation of the ELF NT_ values.
Definition Note.hpp:49
TYPE type() const
Return the type of the note. This type does not match the NT_ type value. For accessing the original ...
Definition Note.hpp:181
static result< const char * > type_owner(TYPE type)
Try to determine the owner's name of the TYPE provided in parameter.
uint32_t original_type() const
The original NT_xxx integer value. The meaning of this value likely depends on the owner of the note.
Definition Note.hpp:187
static result< const char * > type_to_section(TYPE type)
Try to determine the ELF section name associated with the TYPE provided in parameter.
void description(description_t description)
Change the description of the note.
Definition Note.hpp:205
uint64_t size() const
Size of the raw note which includes padding.
static result< TYPE > convert_type(Header::FILE_TYPE ftype, uint32_t type, const std::string &name)
Convert the raw integer note type into a TYPE according to the owner.
static std::unique_ptr< Note > create(BinaryStream &stream, Header::FILE_TYPE ftype=Header::FILE_TYPE::NONE, ARCH arch=ARCH::NONE, Header::CLASS cls=Header::CLASS::NONE)
Create a new note from the given stream. Additional information such as the architecture or the ELF c...
const std::string & name() const
Return the name of the note (also known as 'owner' )
Definition Note.hpp:175
virtual std::unique_ptr< Note > clone() const
Clone the current note and keep its polymorphic type.
Definition Note.hpp:170
static std::unique_ptr< Note > create(const std::string &name, uint32_t type, description_t description, Header::FILE_TYPE ftype=Header::FILE_TYPE::NONE, ARCH arch=ARCH::NONE, Header::CLASS cls=Header::CLASS::NONE)
Create a new note from the given parameters. Additional information such as the architecture or the E...
span< const uint8_t > description() const
Return the description associated with the note.
Definition Note.hpp:192
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Definition Object.hpp:25
Definition Visitor.hpp:219
ARCH
Machine architectures See current registered ELF machine architectures at: http://www....
Definition ELF/enums.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:106
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:72