LIEF: Library to Instrument Executable Formats Version 1.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>
20#include <vector>
21#include <ostream>
22
23#include "LIEF/span.hpp"
24#include "LIEF/Object.hpp"
25#include "LIEF/visibility.h"
26
27namespace LIEF {
29class LIEF_API Section : public Object {
30 public:
31 static constexpr size_t npos = -1;
32
33 Section() = default;
34 Section(std::string name) :
35 name_(std::move(name)) {}
36
37 ~Section() override = default;
38
39 Section& operator=(const Section&) = default;
40 Section(const Section&) = default;
41
43 virtual std::string name() const {
44 return name_.c_str();
45 }
46
49 virtual const std::string& fullname() const {
50 return name_;
51 }
52
54 virtual span<const uint8_t> content() const {
55 return {};
56 }
57
59 virtual void size(uint64_t size) {
60 size_ = size;
61 }
62
64 virtual uint64_t size() const {
65 return size_;
66 }
67
69 virtual uint64_t offset() const {
70 return offset_;
71 }
72
74 virtual uint64_t virtual_address() const {
75 return virtual_address_;
76 }
77
78 virtual void virtual_address(uint64_t virtual_address) {
79 virtual_address_ = virtual_address;
80 }
81
83 virtual void name(std::string name) {
84 name_ = std::move(name);
85 }
86
88 virtual void content(const std::vector<uint8_t>&) {}
89
90 virtual void offset(uint64_t offset) {
91 offset_ = offset;
92 }
93
95 double entropy() const;
96
97 // Search functions
98 // ================
99 size_t search(uint64_t integer, size_t pos, size_t size) const;
100 size_t search(const std::vector<uint8_t>& pattern, size_t pos = 0) const;
101 size_t search(const std::string& pattern, size_t pos = 0) const;
102 size_t search(uint64_t integer, size_t pos = 0) const;
103
104 // Search all functions
105 // ====================
106 std::vector<size_t> search_all(uint64_t v, size_t size) const;
107
108 std::vector<size_t> search_all(uint64_t v) const;
109
110 std::vector<size_t> search_all(const std::string& v) const;
111
113 void accept(Visitor& visitor) const override;
114
115
116 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& entry);
117
118 protected:
119 std::string name_;
120 uint64_t virtual_address_ = 0;
121 uint64_t size_ = 0;
122 uint64_t offset_ = 0;
123
124 private:
125 template<typename T>
126 std::vector<size_t> search_all_(const T& v) const;
127};
128}
129
130#endif
virtual void offset(uint64_t offset)
Definition Abstract/Section.hpp:90
virtual void content(const std::vector< uint8_t > &)
Change section content.
Definition Abstract/Section.hpp:88
virtual void size(uint64_t size)
Change the section size.
Definition Abstract/Section.hpp:59
static constexpr size_t npos
Definition Abstract/Section.hpp:31
virtual uint64_t size() const
section's size (size in the binary, not the virtual size)
Definition Abstract/Section.hpp:64
std::vector< size_t > search_all(const std::string &v) const
virtual void virtual_address(uint64_t virtual_address)
Definition Abstract/Section.hpp:78
size_t search(uint64_t integer, size_t pos, size_t size) const
Section()=default
Section(const Section &)=default
virtual const std::string & fullname() const
Return the complete section's name which might trailing (0) bytes.
Definition Abstract/Section.hpp:49
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:34
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:83
virtual std::string name() const
section's name
Definition Abstract/Section.hpp:43
friend std::ostream & operator<<(std::ostream &os, const Section &entry)
virtual span< const uint8_t > content() const
section's content
Definition Abstract/Section.hpp:54
~Section() override=default
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:69
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
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:74
Definition Visitor.hpp:212
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Definition string.h:155
#define LIEF_API
Definition visibility.h:43