LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
Abstract/Section.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_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
38 ~Section() override = default;
39
40 Section& operator=(const Section&) = default;
41 Section(const Section&) = default;
42
44 virtual std::string name() const {
45 return name_.c_str();
46 }
47
50 virtual const std::string& fullname() const {
51 return name_;
52 }
53
55 virtual span<const uint8_t> content() const {
56 return {};
57 }
58
60 virtual void size(uint64_t size) {
61 size_ = size;
62 }
63
65 virtual uint64_t size() const {
66 return size_;
67 }
68
70 virtual uint64_t offset() const {
71 return offset_;
72 }
73
75 virtual uint64_t virtual_address() const {
76 return virtual_address_;
77 }
78
79 virtual void virtual_address(uint64_t virtual_address) {
80 virtual_address_ = virtual_address;
81 }
82
84 virtual void name(std::string name) {
85 name_ = std::move(name);
86 }
87
89 virtual void content(const std::vector<uint8_t>&) {}
90
91 virtual void offset(uint64_t offset) {
92 offset_ = offset;
93 }
94
96 double entropy() const;
97
98 // Search functions
99 // ================
100 size_t search(uint64_t integer, size_t pos, size_t size) const;
101 size_t search(const std::vector<uint8_t>& pattern, size_t pos = 0) const;
102 size_t search(const std::string& pattern, size_t pos = 0) const;
103 size_t search(uint64_t integer, size_t pos = 0) const;
104
105 // Search all functions
106 // ====================
107 std::vector<size_t> search_all(uint64_t v, size_t size) const;
108
109 std::vector<size_t> search_all(uint64_t v) const;
110
111 std::vector<size_t> search_all(const std::string& v) const;
112
114 void accept(Visitor& visitor) const override;
115
116
117 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& entry);
118
119 protected:
120 std::string name_;
121 uint64_t virtual_address_ = 0;
122 uint64_t size_ = 0;
123 uint64_t offset_ = 0;
124
125 private:
126 template<typename T>
127 std::vector<size_t> search_all_(const T& v) const;
128};
129}
130
131#endif
Definition Object.hpp:25
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
virtual void offset(uint64_t offset)
Definition Abstract/Section.hpp:91
virtual void content(const std::vector< uint8_t > &)
Change section content.
Definition Abstract/Section.hpp:89
virtual void size(uint64_t size)
Change the section size.
Definition Abstract/Section.hpp:60
virtual uint64_t size() const
section's size (size in the binary, not the virtual size)
Definition Abstract/Section.hpp:65
std::vector< size_t > search_all(const std::string &v) const
virtual void virtual_address(uint64_t virtual_address)
Definition Abstract/Section.hpp:79
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:50
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:84
virtual std::string name() const
section's name
Definition Abstract/Section.hpp:44
friend std::ostream & operator<<(std::ostream &os, const Section &entry)
virtual span< const uint8_t > content() const
section's content
Definition Abstract/Section.hpp:55
~Section() override=default
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:70
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:75
Definition Visitor.hpp:224
LIEF namespace.
Definition Abstract/Binary.hpp:32
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:41