LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
Abstract/Section.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_ABSTRACT_SECTION_H
17#define LIEF_ABSTRACT_SECTION_H
18
19#include <string_view>
20#include <ostream>
21#include <string>
22#include <vector>
23
24#include "LIEF/Object.hpp"
26#include "LIEF/span.hpp"
27#include "LIEF/visibility.h"
28
29namespace LIEF {
31class LIEF_API Section : public Object {
32 public:
33 static constexpr size_t npos = -1;
34
35 Section() = default;
36 Section(std::string name) :
37 name_(std::move(name)) {}
38
39 ~Section() override = default;
40
41 Section& operator=(const Section&) = default;
42 Section(const Section&) = default;
43
45 virtual std::string_view name() const LIEF_LIFETIMEBOUND {
46 return fullname().substr(0, fullname().find_first_of('\0'));
47 }
48
51 virtual std::string_view fullname() const LIEF_LIFETIMEBOUND {
52 return name_;
53 }
54
57 return {};
58 }
59
61 virtual void size(uint64_t size) {
62 size_ = size;
63 }
64
66 virtual uint64_t size() const {
67 return size_;
68 }
69
71 virtual uint64_t offset() const {
72 return offset_;
73 }
74
76 virtual uint64_t virtual_address() const {
77 return virtual_address_;
78 }
79
80 virtual void virtual_address(uint64_t virtual_address) {
81 virtual_address_ = virtual_address;
82 }
83
85 virtual void name(std::string name) {
86 name_ = std::move(name);
87 }
88
90 virtual void content(const std::vector<uint8_t>&) {}
91
92 virtual void offset(uint64_t offset) {
93 offset_ = offset;
94 }
95
97 double entropy() const;
98
99 // Search functions
100 // ================
101 size_t search(uint64_t integer, size_t pos, size_t size) const;
102 size_t search(const std::vector<uint8_t>& pattern, size_t pos = 0) const;
103 size_t search(const std::string& pattern, size_t pos = 0) const;
104 size_t search(uint64_t integer, size_t pos = 0) const;
105
106 // Search all functions
107 // ====================
108 std::vector<size_t> search_all(uint64_t v, size_t size) const;
109
110 std::vector<size_t> search_all(uint64_t v) const;
111
112 std::vector<size_t> search_all(const std::string& v) const;
113
115 void accept(Visitor& visitor) const override;
116
117
118 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& entry);
119
120 protected:
121 std::string name_;
122 uint64_t virtual_address_ = 0;
123 uint64_t size_ = 0;
124 uint64_t offset_ = 0;
125
126 private:
127 template<typename T>
128 std::vector<size_t> search_all_(const T& v) const;
129};
130}
131
132#endif
virtual void offset(uint64_t offset)
Definition Abstract/Section.hpp:92
virtual void content(const std::vector< uint8_t > &)
Change section content.
Definition Abstract/Section.hpp:90
virtual void size(uint64_t size)
Change the section size.
Definition Abstract/Section.hpp:61
static constexpr size_t npos
Definition Abstract/Section.hpp:33
virtual uint64_t size() const
Section's size (size in the binary, not the virtual size).
Definition Abstract/Section.hpp:66
std::vector< size_t > search_all(const std::string &v) const
virtual void virtual_address(uint64_t virtual_address)
Definition Abstract/Section.hpp:80
size_t search(uint64_t integer, size_t pos, size_t size) const
Section()=default
Section(const Section &)=default
virtual std::string_view fullname() const
Return the complete section's name which might include trailing (0) bytes.
Definition Abstract/Section.hpp:51
size_t search(const std::vector< uint8_t > &pattern, size_t pos=0) const
std::vector< size_t > search_all(uint64_t v) const
Section(std::string name)
Definition Abstract/Section.hpp:36
size_t search(uint64_t integer, size_t pos=0) const
double entropy() const
Section's entropy.
virtual void name(std::string name)
Change the section's name.
Definition Abstract/Section.hpp:85
friend std::ostream & operator<<(std::ostream &os, const Section &entry)
virtual span< const uint8_t > content() const
Section's content.
Definition Abstract/Section.hpp:56
~Section() override=default
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:71
size_t search(const std::string &pattern, size_t pos=0) const
std::vector< size_t > search_all(uint64_t v, size_t size) const
virtual std::string_view name() const
Section's name.
Definition Abstract/Section.hpp:45
void accept(Visitor &visitor) const override
Method so that the visitor can visit us.
Section & operator=(const Section &)=default
virtual uint64_t virtual_address() const
Address where the section should be mapped.
Definition Abstract/Section.hpp:76
Definition Visitor.hpp:212
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:45