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 {
33
36class LIEF_API Note : public Object {
37 friend class Parser;
38 friend class Builder;
39 friend class Binary;
40
41 public:
43 using description_t = std::vector<uint8_t>;
44
46 enum class TYPE {
47 UNKNOWN = 0,
51 GNU_ABI_TAG,
53 GNU_HWCAP,
55 GNU_BUILD_ID,
57 GNU_GOLD_VERSION,
60 GNU_PROPERTY_TYPE_0,
61
62 GNU_BUILD_ATTRIBUTE_OPEN,
63 GNU_BUILD_ATTRIBUTE_FUNC,
64
66 CRASHPAD,
67
69 CORE_PRSTATUS,
70 CORE_FPREGSET,
74 CORE_PRPSINFO,
75 CORE_TASKSTRUCT,
79 CORE_AUXV,
80 CORE_PSTATUS,
82 CORE_FPREGS,
84 CORE_PSINFO,
85 CORE_LWPSTATUS,
86 CORE_LWPSINFO,
87 CORE_WIN32PSTATUS,
88 CORE_FILE,
89 CORE_PRXFPREG,
90 CORE_SIGINFO,
91
92 CORE_ARM_VFP,
93 CORE_ARM_TLS,
94 CORE_ARM_HW_BREAK,
95 CORE_ARM_HW_WATCH,
96 CORE_ARM_SYSTEM_CALL,
97 CORE_ARM_SVE,
98 CORE_ARM_PAC_MASK,
99 CORE_ARM_PACA_KEYS,
100 CORE_ARM_PACG_KEYS,
101 CORE_TAGGED_ADDR_CTRL,
102 CORE_PAC_ENABLED_KEYS,
103
104 CORE_X86_TLS,
105 CORE_X86_IOPERM,
106 CORE_X86_XSTATE,
107 CORE_X86_CET,
108
113 ANDROID_IDENT,
114 ANDROID_MEMTAG,
115 ANDROID_KUSER,
116
118 GO_BUILDID,
120 STAPSDT,
121 };
122
123 public:
125 static result<TYPE> convert_type(Header::FILE_TYPE ftype, uint32_t type,
126 const std::string& name);
127
131
132 static result<const char*> note_to_section(const Note& note) {
133 return type_to_section(note.type());
134 }
135
138
142 static std::unique_ptr<Note> create(
143 const std::string& name, uint32_t type, description_t description,
144 Header::FILE_TYPE ftype = Header::FILE_TYPE::NONE, ARCH arch = ARCH::NONE,
145 Header::CLASS cls = Header::CLASS::NONE);
146
150 static std::unique_ptr<Note> create(
151 const std::string& name, TYPE type, description_t description,
152 ARCH arch = ARCH::NONE, Header::CLASS cls = Header::CLASS::NONE);
153
157 static std::unique_ptr<Note> create(BinaryStream& stream,
158 Header::FILE_TYPE ftype = Header::FILE_TYPE::NONE, ARCH arch = ARCH::NONE,
159 Header::CLASS cls = Header::CLASS::NONE);
160
161 Note& operator=(const Note& copy) = default;
162 Note(const Note& copy) = default;
163
164 ~Note() override = default;
165
167 virtual std::unique_ptr<Note> clone() const {
168 return std::unique_ptr<Note>(new Note(*this));
169 }
170
172 const std::string& name() const {
173 return name_;
174 }
175
178 TYPE type() const {
179 return type_;
180 }
181
184 uint32_t original_type() const {
185 return original_type_;
186 }
187
189 span<const uint8_t> description() const {
190 return description_;
191 }
192
193 span<uint8_t> description() {
194 return description_;
195 }
196
197 void name(std::string name) {
198 name_ = std::move(name);
199 }
200
202 void description(description_t description) {
203 description_ = std::move(description);
204 }
205
207 uint64_t size() const;
208
209 virtual void dump(std::ostream& os) const;
210
211 void accept(Visitor& visitor) const override;
212
213 LIEF_API friend
214 std::ostream& operator<<(std::ostream& os, const Note& note) {
215 note.dump(os);
216 return os;
217 }
218
219 protected:
220 Note() = default;
221 Note(std::string name, TYPE type, uint32_t original_type,
222 description_t description) :
223 name_(std::move(name)),
224 type_(type),
225 original_type_(original_type),
226 description_(std::move(description))
227 {}
228
229 template<class T>
230 LIEF_LOCAL result<T> read_at(size_t offset) const;
231
232 template<class T>
233 LIEF_LOCAL ok_error_t write_at(size_t offset, const T& value);
234
235 LIEF_LOCAL ok_error_t write_string_at(size_t offset, const std::string& value);
236
237 LIEF_LOCAL result<std::string>
238 read_string_at(size_t offset, size_t maxsize = 0) const;
239
240 std::string name_;
241 TYPE type_ = TYPE::UNKNOWN;
242 uint32_t original_type_ = 0;
243 description_t description_;
244};
245
246LIEF_API const char* to_string(Note::TYPE type);
247
248
249} // namepsace ELF
250} // namespace LIEF
251#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:36
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:43
TYPE
LIEF representation of the ELF NT_ values.
Definition Note.hpp:46
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:178
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:184
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:202
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:172
virtual std::unique_ptr< Note > clone() const
Clone the current note and keep its polymorphic type.
Definition Note.hpp:167
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:189
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