LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
Note.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_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,
55
57 GNU_HWCAP,
58
60 GNU_BUILD_ID,
61
63 GNU_GOLD_VERSION,
64
67 GNU_PROPERTY_TYPE_0,
68
69 GNU_BUILD_ATTRIBUTE_OPEN,
70 GNU_BUILD_ATTRIBUTE_FUNC,
71
73 CRASHPAD,
74
76 CORE_PRSTATUS,
77 CORE_FPREGSET,
78
82 CORE_PRPSINFO,
83 CORE_TASKSTRUCT,
84
88 CORE_AUXV,
89 CORE_PSTATUS,
91 CORE_FPREGS,
93 CORE_PSINFO,
94 CORE_LWPSTATUS,
95 CORE_LWPSINFO,
96 CORE_WIN32PSTATUS,
97 CORE_FILE,
98 CORE_PRXFPREG,
99 CORE_SIGINFO,
100
101 CORE_ARM_VFP,
102 CORE_ARM_TLS,
103 CORE_ARM_HW_BREAK,
104 CORE_ARM_HW_WATCH,
105 CORE_ARM_SYSTEM_CALL,
106 CORE_ARM_SVE,
107 CORE_ARM_PAC_MASK,
108 CORE_ARM_PACA_KEYS,
109 CORE_ARM_PACG_KEYS,
110 CORE_TAGGED_ADDR_CTRL,
111 CORE_PAC_ENABLED_KEYS,
112
113 CORE_X86_TLS,
114 CORE_X86_IOPERM,
115 CORE_X86_XSTATE,
116 CORE_X86_CET,
117
122 ANDROID_IDENT,
123 ANDROID_MEMTAG,
124 ANDROID_KUSER,
125
127 GO_BUILDID,
128
130 STAPSDT,
131
133 QNX_STACK,
134 };
135
136 public:
139 const std::string& name);
140
144
146 const std::string& sec_name = note.section_name();
147 if (sec_name.empty()) {
148 return type_to_section(note.type());
149 }
150 return sec_name;
151 }
152
155
159 static std::unique_ptr<Note>
160 create(const std::string& name, uint32_t type, description_t description,
161 std::string section_name,
164
168 static std::unique_ptr<Note> create(const std::string& name, TYPE type,
170 std::string section_name,
171 ARCH arch = ARCH::NONE,
173
177 static std::unique_ptr<Note>
178 create(BinaryStream& stream, std::string section_name,
181
182 Note& operator=(const Note& copy) = default;
183 Note(const Note& copy) = default;
184
185 ~Note() override = default;
186
188 virtual std::unique_ptr<Note> clone() const {
189 return std::unique_ptr<Note>(new Note(*this));
190 }
191
193 const std::string& name() const {
194 return name_;
195 }
196
198 const std::string& section_name() const {
199 return section_name_;
200 }
201
204 TYPE type() const {
205 return type_;
206 }
207
210 uint32_t original_type() const {
211 return original_type_;
212 }
213
216 return description_;
217 }
218
220 return description_;
221 }
222
223 void name(std::string name) {
224 name_ = std::move(name);
225 }
226
229 description_ = std::move(description);
230 }
231
233 uint64_t size() const;
234
235 virtual void dump(std::ostream& os) const;
236
237 void accept(Visitor& visitor) const override;
238
239 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Note& note) {
240 note.dump(os);
241 return os;
242 }
243
244 template<class T>
245 const T* cast() const {
246 static_assert(std::is_base_of<Note, T>::value, "Require Note inheritance");
247 if (T::classof(this)) {
248 return static_cast<const T*>(this);
249 }
250 return nullptr;
251 }
252
253 template<class T>
254 T* cast() {
255 return const_cast<T*>(static_cast<const Note*>(this)->cast<T>());
256 }
257
258 protected:
259 Note() = default;
260 Note(std::string name, TYPE type, uint32_t original_type,
261 description_t description, std::string section) :
262 name_(std::move(name)),
263 type_(type),
264 original_type_(original_type),
265 description_(std::move(description)),
266 section_name_(std::move(section)) {}
267
268 template<class T>
269 LIEF_LOCAL result<T> read_at(size_t offset) const;
270
271 template<class T>
272 LIEF_LOCAL ok_error_t write_at(size_t offset, const T& value);
273
274 LIEF_LOCAL ok_error_t write_string_at(size_t offset, const std::string& value);
275
276 LIEF_LOCAL result<std::string> read_string_at(size_t offset,
277 size_t maxsize = 0) const;
278
279 std::string name_;
280 TYPE type_ = TYPE::UNKNOWN;
281 uint32_t original_type_ = 0;
282 description_t description_;
283 std::string section_name_;
284};
285
286LIEF_API const char* to_string(Note::TYPE type);
287
288
289} // namespace ELF
290} // namespace LIEF
291#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 takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
FILE_TYPE
The type of the underlying ELF file. This enum matches the semantic of ET_NONE, ET_REL,...
Definition ELF/Header.hpp:59
@ NONE
Can't be determined.
Definition ELF/Header.hpp:60
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS].
Definition ELF/Header.hpp:74
@ NONE
Invalid class.
Definition ELF/Header.hpp:75
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:39
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
const T * cast() const
Definition Note.hpp:245
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:204
static result< const char * > type_owner(TYPE type)
Try to determine the owner's name of the TYPE provided in parameter.
static std::unique_ptr< Note > create(const std::string &name, TYPE type, description_t description, std::string section_name, 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...
friend std::ostream & operator<<(std::ostream &os, const Note &note)
Definition Note.hpp:239
static result< std::string > note_to_section(const Note &note)
Definition Note.hpp:145
static std::unique_ptr< Note > create(const std::string &name, uint32_t type, description_t description, std::string section_name, 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...
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:210
static std::unique_ptr< Note > create(BinaryStream &stream, std::string section_name, 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...
virtual void dump(std::ostream &os) const
friend class Builder
Definition Note.hpp:41
~Note() override=default
static result< const char * > type_to_section(TYPE type)
Try to determine the ELF section name associated with the TYPE provided in parameter.
T * cast()
Definition Note.hpp:254
void description(description_t description)
Change the description of the note.
Definition Note.hpp:228
friend class Binary
Definition Note.hpp:42
uint64_t size() const
Size of the raw note which includes padding.
void accept(Visitor &visitor) const override
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.
friend class Parser
Definition Note.hpp:40
const std::string & section_name() const
Return the section name in which the note is or should be stored.
Definition Note.hpp:198
const std::string & name() const
Return the name of the note (also known as 'owner' ).
Definition Note.hpp:193
virtual std::unique_ptr< Note > clone() const
Clone the current note and keep its polymorphic type.
Definition Note.hpp:188
Note & operator=(const Note &copy)=default
Note(const Note &copy)=default
span< const uint8_t > description() const
Return the description associated with the note.
Definition Note.hpp:215
span< uint8_t > description()
Definition Note.hpp:219
void name(std::string name)
Definition Note.hpp:223
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:44
Definition Visitor.hpp:212
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:117
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
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:30
@ NONE
Definition ELF/enums.hpp:31
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46