LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
Note.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_ELF_NOTE_H
16#define LIEF_ELF_NOTE_H
17
18#include <string_view>
19#include <memory>
20#include <ostream>
21#include <vector>
22
23#include "LIEF/Object.hpp"
25#include "LIEF/errors.hpp"
26#include "LIEF/span.hpp"
27#include "LIEF/visibility.h"
28
29#include "LIEF/ELF/Header.hpp"
30
31namespace LIEF {
32class BinaryStream;
33namespace ELF {
34class Binary;
35class Parser;
36class Builder;
37
40class LIEF_API Note : public Object {
41 friend class Parser;
42 friend class Builder;
43 friend class Binary;
44
45 public:
47 using description_t = std::vector<uint8_t>;
48
50 enum class TYPE {
51 UNKNOWN = 0,
55 GNU_ABI_TAG,
56
58 GNU_HWCAP,
59
61 GNU_BUILD_ID,
62
64 GNU_GOLD_VERSION,
65
68 GNU_PROPERTY_TYPE_0,
69
70 GNU_BUILD_ATTRIBUTE_OPEN,
71 GNU_BUILD_ATTRIBUTE_FUNC,
72
74 CRASHPAD,
75
77 CORE_PRSTATUS,
78 CORE_FPREGSET,
79
83 CORE_PRPSINFO,
84 CORE_TASKSTRUCT,
85
89 CORE_AUXV,
90 CORE_PSTATUS,
92 CORE_FPREGS,
94 CORE_PSINFO,
95 CORE_LWPSTATUS,
96 CORE_LWPSINFO,
97 CORE_WIN32PSTATUS,
98 CORE_FILE,
99 CORE_PRXFPREG,
100 CORE_SIGINFO,
101
102 CORE_ARM_VFP,
103 CORE_ARM_TLS,
104 CORE_ARM_HW_BREAK,
105 CORE_ARM_HW_WATCH,
106 CORE_ARM_SYSTEM_CALL,
107 CORE_ARM_SVE,
108 CORE_ARM_PAC_MASK,
109 CORE_ARM_PACA_KEYS,
110 CORE_ARM_PACG_KEYS,
111 CORE_TAGGED_ADDR_CTRL,
112 CORE_PAC_ENABLED_KEYS,
113
114 CORE_X86_TLS,
115 CORE_X86_IOPERM,
116 CORE_X86_XSTATE,
117 CORE_X86_CET,
118
123 ANDROID_IDENT,
124 ANDROID_MEMTAG,
125 ANDROID_KUSER,
126
128 GO_BUILDID,
129
131 STAPSDT,
132
134 QNX_STACK,
135 };
136
137 public:
140 std::string_view name);
141
145
147 std::string_view sec_name = note.section_name();
148 if (sec_name.empty()) {
149 return type_to_section(note.type());
150 }
151 return std::string(sec_name);
152 }
153
156
160 static std::unique_ptr<Note>
161 create(std::string_view name, uint32_t type, description_t description,
162 std::string section_name,
166
170 static std::unique_ptr<Note>
172 std::string section_name, ARCH arch = ARCH::NONE,
175
179 static std::unique_ptr<Note>
180 create(BinaryStream& stream, std::string section_name,
183
184 Note& operator=(const Note& copy) = default;
185 Note(const Note& copy) = default;
186
187 ~Note() override = default;
188
190 virtual std::unique_ptr<Note> clone() const {
191 return std::make_unique<Note>(*this);
192 }
193
195 std::string_view name() const LIEF_LIFETIMEBOUND {
196 return name_;
197 }
198
200 std::string_view section_name() const LIEF_LIFETIMEBOUND {
201 return section_name_;
202 }
203
206 TYPE type() const {
207 return type_;
208 }
209
212 uint32_t original_type() const {
213 return original_type_;
214 }
215
218 return description_;
219 }
220
222 return description_;
223 }
224
225 void name(std::string name) {
226 name_ = std::move(name);
227 }
228
231 description_ = std::move(description);
232 }
233
235 uint64_t size() const;
236
237 virtual void dump(std::ostream& os) const;
238
239 void accept(Visitor& visitor) const override;
240
241 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Note& note) {
242 note.dump(os);
243 return os;
244 }
245
246 template<class T>
247 const T* cast() const {
248 static_assert(std::is_base_of_v<Note, T>, "Require Note inheritance");
249 if (T::classof(this)) {
250 return static_cast<const T*>(this);
251 }
252 return nullptr;
253 }
254
255 template<class T>
256 T* cast() {
257 return const_cast<T*>(static_cast<const Note*>(this)->cast<T>());
258 }
259
260 protected:
261 Note() = default;
262 Note(std::string name, TYPE type, uint32_t original_type,
263 description_t description, std::string section,
265 name_(std::move(name)),
266 type_(type),
267 original_type_(original_type),
268 description_(std::move(description)),
269 section_name_(std::move(section)),
270 endianness_(endianness) {}
271
272 LIEF_LOCAL bool should_swap() const;
273
274 template<class T>
275 LIEF_LOCAL result<T> read_at(size_t offset) const;
276
277 template<class T>
278 LIEF_LOCAL ok_error_t write_at(size_t offset, const T& value);
279
280 LIEF_LOCAL ok_error_t write_string_at(size_t offset, std::string_view value);
281
282 LIEF_LOCAL result<std::string> read_string_at(size_t offset,
283 size_t maxsize = 0) const;
284
285 std::string name_;
286 TYPE type_ = TYPE::UNKNOWN;
287 uint32_t original_type_ = 0;
288 description_t description_;
289 std::string section_name_;
290 Header::ELF_DATA endianness_ = Header::ELF_DATA::LSB;
291};
292
293LIEF_API const char* to_string(Note::TYPE type);
294
295
296}
297}
298#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:64
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:47
ELF_DATA
Match the result Elfxx_Ehdr.e_ident[EI_DATA].
Definition ELF/Header.hpp:108
@ LSB
2's complement, little endian
Definition ELF/Header.hpp:110
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:40
std::vector< uint8_t > description_t
Container used to handle the description data.
Definition Note.hpp:47
TYPE
LIEF representation of the ELF NT_ values.
Definition Note.hpp:50
const T * cast() const
Definition Note.hpp:247
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:206
static result< const char * > type_owner(TYPE type)
Try to determine the owner's name of the TYPE provided in parameter.
friend std::ostream & operator<<(std::ostream &os, const Note &note)
Definition Note.hpp:241
static result< std::string > note_to_section(const Note &note)
Definition Note.hpp:146
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:212
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
static result< TYPE > convert_type(Header::FILE_TYPE ftype, uint32_t type, std::string_view name)
Convert the raw integer note type into a TYPE according to the owner.
std::string_view name() const
Return the name of the note (also known as 'owner' ).
Definition Note.hpp:195
friend class Builder
Definition Note.hpp:42
~Note() override=default
static std::unique_ptr< Note > create(std::string_view 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, Header::ELF_DATA endianness=Header::ELF_DATA::LSB)
Create a new note from the given parameters. Additional information such as the architecture or the E...
static result< const char * > type_to_section(TYPE type)
Try to determine the ELF section name associated with the TYPE provided in parameter.
static std::unique_ptr< Note > create(std::string_view name, TYPE type, description_t description, std::string section_name, ARCH arch=ARCH::NONE, Header::CLASS cls=Header::CLASS::NONE, Header::ELF_DATA endianness=Header::ELF_DATA::LSB)
Create a new note from the given parameters. Additional information such as the architecture or the E...
T * cast()
Definition Note.hpp:256
void description(description_t description)
Change the description of the note.
Definition Note.hpp:230
friend class Binary
Definition Note.hpp:43
uint64_t size() const
Size of the raw note which includes padding.
void accept(Visitor &visitor) const override
friend class Parser
Definition Note.hpp:41
virtual std::unique_ptr< Note > clone() const
Clone the current note and keep its polymorphic type.
Definition Note.hpp:190
Note & operator=(const Note &copy)=default
std::string_view section_name() const
Return the section name in which the note is or should be stored.
Definition Note.hpp:200
Note(const Note &copy)=default
span< const uint8_t > description() const
Return the description associated with the note.
Definition Note.hpp:217
span< uint8_t > description()
Definition Note.hpp:221
void name(std::string name)
Definition Note.hpp:225
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:46
Definition Visitor.hpp:211
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:27
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:27
@ NONE
Definition ELF/enums.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45