LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Note.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_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;
36class LIEF_API Note : public Object {
40 friend class Parser;
41 friend class Builder;
42 friend class Binary;
43
44 public: using description_t = std::vector<uint8_t>;
47 enum class TYPE {
50 UNKNOWN = 0, GNU_ABI_TAG, GNU_HWCAP, GNU_BUILD_ID, GNU_GOLD_VERSION, GNU_PROPERTY_TYPE_0,
64
65 GNU_BUILD_ATTRIBUTE_OPEN,
66 GNU_BUILD_ATTRIBUTE_FUNC,
67 CRASHPAD,
70 CORE_PRSTATUS,
73 CORE_FPREGSET, CORE_PRPSINFO,
78 CORE_TASKSTRUCT, CORE_AUXV,
83 CORE_PSTATUS, CORE_FPREGS, 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 ANDROID_IDENT,
117 ANDROID_MEMTAG,
118 ANDROID_KUSER,
119 GO_BUILDID, STAPSDT, QNX_STACK
126 };
127
128 public: static result<TYPE> convert_type(Header::FILE_TYPE ftype, uint32_t type,
131 const std::string& name);
132 static result<const char*> type_to_section(TYPE type);
136
137 static result<std::string> note_to_section(const Note& note) {
138 const std::string& sec_name = note.section_name();
139 if (sec_name.empty()) {
140 return type_to_section(note.type());
141 }
142 return sec_name;
143 }
144 static result<const char*> type_owner(TYPE type);
147 static std::unique_ptr<Note> create(
152 const std::string& name, uint32_t type, description_t description,
153 std::string section_name,
154 Header::FILE_TYPE ftype = Header::FILE_TYPE::NONE, ARCH arch = ARCH::NONE,
155 Header::CLASS cls = Header::CLASS::NONE);
156 static std::unique_ptr<Note> create(
161 const std::string& name, TYPE type, description_t description,
162 std::string section_name,
163 ARCH arch = ARCH::NONE, Header::CLASS cls = Header::CLASS::NONE);
164 static std::unique_ptr<Note> create(BinaryStream& stream,
169 std::string section_name,
170 Header::FILE_TYPE ftype = Header::FILE_TYPE::NONE, ARCH arch = ARCH::NONE,
171 Header::CLASS cls = Header::CLASS::NONE);
172
173 Note& operator=(const Note& copy) = default;
174 Note(const Note& copy) = default;
175
176 ~Note() override = default;
177 virtual std::unique_ptr<Note> clone() const {
180 return std::unique_ptr<Note>(new Note(*this));
181 }
182 const std::string& name() const {
185 return name_;
186 }
187 const std::string& section_name() const {
190 return section_name_;
191 }
192 TYPE type() const {
196 return type_;
197 }
198 uint32_t original_type() const {
202 return original_type_;
203 }
204 span<const uint8_t> description() const {
207 return description_;
208 }
209
210 span<uint8_t> description() {
211 return description_;
212 }
213
214 void name(std::string name) {
215 name_ = std::move(name);
216 }
217 void description(description_t description) {
220 description_ = std::move(description);
221 }
222 uint64_t size() const;
225
226 virtual void dump(std::ostream& os) const;
227
228 void accept(Visitor& visitor) const override;
229
230 LIEF_API friend
231 std::ostream& operator<<(std::ostream& os, const Note& note) {
232 note.dump(os);
233 return os;
234 }
235
236 template<class T>
237 const T* cast() const {
238 static_assert(std::is_base_of<Note, T>::value,
239 "Require Note inheritance");
240 if (T::classof(this)) {
241 return static_cast<const T*>(this);
242 }
243 return nullptr;
244 }
245
246 template<class T>
247 T* cast() {
248 return const_cast<T*>(static_cast<const Note*>(this)->cast<T>());
249 }
250
251 protected:
252 Note() = default;
253 Note(std::string name, TYPE type, uint32_t original_type,
254 description_t description, std::string section) :
255 name_(std::move(name)),
256 type_(type),
257 original_type_(original_type),
258 description_(std::move(description)),
259 section_name_(std::move(section))
260 {}
261
262 template<class T>
263 LIEF_LOCAL result<T> read_at(size_t offset) const;
264
265 template<class T>
266 LIEF_LOCAL ok_error_t write_at(size_t offset, const T& value);
267
268 LIEF_LOCAL ok_error_t write_string_at(size_t offset, const std::string& value);
269
270 LIEF_LOCAL result<std::string>
271 read_string_at(size_t offset, size_t maxsize = 0) const;
272
273 std::string name_;
274 TYPE type_ = TYPE::UNKNOWN;
275 uint32_t original_type_ = 0;
276 description_t description_;
277 std::string section_name_;
278};
279
280LIEF_API const char* to_string(Note::TYPE type);
281
282
283} // namepsace ELF
284} // namespace LIEF
285#endif
Header.hpp
Object.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::ELF::Binary
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
LIEF::ELF::Builder
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
LIEF::ELF::Note
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:39
LIEF::ELF::Note::TYPE
TYPE
LIEF representation of the ELF NT_ values.
Definition Note.hpp:49
LIEF::ELF::Note::cast
const T * cast() const
Definition Note.hpp:237
LIEF::ELF::Note::type
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:195
LIEF::ELF::Note::type_owner
static result< const char * > type_owner(TYPE type)
Try to determine the owner's name of the TYPE provided in parameter.
LIEF::ELF::Note::create
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...
LIEF::ELF::Note::operator<<
friend std::ostream & operator<<(std::ostream &os, const Note &note)
Definition Note.hpp:231
LIEF::ELF::Note::note_to_section
static result< std::string > note_to_section(const Note &note)
Definition Note.hpp:137
LIEF::ELF::Note::create
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...
LIEF::ELF::Note::original_type
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:201
LIEF::ELF::Note::create
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...
LIEF::ELF::Note::dump
virtual void dump(std::ostream &os) const
LIEF::ELF::Note::~Note
~Note() override=default
LIEF::ELF::Note::type_to_section
static result< const char * > type_to_section(TYPE type)
Try to determine the ELF section name associated with the TYPE provided in parameter.
LIEF::ELF::Note::cast
T * cast()
Definition Note.hpp:247
LIEF::ELF::Note::description
void description(description_t description)
Change the description of the note.
Definition Note.hpp:219
LIEF::ELF::Note::size
uint64_t size() const
Size of the raw note which includes padding.
LIEF::ELF::Note::accept
void accept(Visitor &visitor) const override
LIEF::ELF::Note::convert_type
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.
LIEF::ELF::Note::section_name
const std::string & section_name() const
Return the section name in which the note is or should be stored.
Definition Note.hpp:189
LIEF::ELF::Note::name
const std::string & name() const
Return the name of the note (also known as 'owner' )
Definition Note.hpp:184
LIEF::ELF::Note::clone
virtual std::unique_ptr< Note > clone() const
Clone the current note and keep its polymorphic type.
Definition Note.hpp:179
LIEF::ELF::Note::operator=
Note & operator=(const Note &copy)=default
LIEF::ELF::Note::Note
Note(const Note &copy)=default
LIEF::ELF::Note::description
span< const uint8_t > description() const
Return the description associated with the note.
Definition Note.hpp:206
LIEF::ELF::Note::description
span< uint8_t > description()
Definition Note.hpp:210
LIEF::ELF::Note::name
void name(std::string name)
Definition Note.hpp:214
LIEF::ELF::Parser
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
errors.hpp
LIEF::ELF
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF::ELF::to_string
const char * to_string(DynamicEntry::TAG e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
LIEF::ok_error_t
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
span.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42