LIEF: Library to Instrument Executable Formats Version
Loading...
Searching...
No Matches
MachO/Section.hpp
1/* Copyright 2017 - 2023 R. Thomas
2 * Copyright 2017 - 2023 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_MACHO_SECTION_H
17#define LIEF_MACHO_SECTION_H
18#include <string>
19#include <vector>
20#include <ostream>
21#include <set>
22#include <memory>
23
24#include "LIEF/types.hpp"
25#include "LIEF/visibility.h"
26
27#include "LIEF/Abstract/Section.hpp"
28
29#include "LIEF/MachO/LoadCommand.hpp"
30#include "LIEF/iterators.hpp"
31
32namespace LIEF {
33namespace MachO {
34
35class BinaryParser;
36class SegmentCommand;
37class Binary;
38class Relocation;
39
40namespace details {
41struct section_32;
42struct section_64;
43}
44
46class LIEF_API Section : public LIEF::Section {
47
48 friend class BinaryParser;
49 friend class Binary;
50 friend class SegmentCommand;
51
52 public:
53
54 using content_t = std::vector<uint8_t>;
55 using flag_list_t = std::set<MACHO_SECTION_FLAGS>;
56
58 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
59
62
65
66 public:
67 Section();
68 Section(const details::section_32& section_cmd);
69 Section(const details::section_64& section_cmd);
70
71 Section(std::string name);
72 Section(std::string name, content_t content);
73
74 Section& operator=(Section copy);
75 Section(const Section& copy);
76
77 void swap(Section& other);
78
79 ~Section() override;
80
81 span<const uint8_t> content() const override;
82
84 void content(const content_t& data) override;
85
87 const std::string& segment_name() const;
88
90 uint64_t address() const;
91
93 uint32_t alignment() const;
94
102 uint32_t relocation_offset() const;
103
105 uint32_t numberof_relocations() const;
106
110 uint32_t flags() const;
111
115
118 uint32_t reserved1() const;
119
122 uint32_t reserved2() const;
123
126 uint32_t reserved3() const;
127
131 flag_list_t flags_list() const;
132
134 uint32_t raw_flags() const;
135
137 bool has_segment() const;
138
142 const SegmentCommand* segment() const;
143
146 void clear(uint8_t v);
147
153 it_const_relocations relocations() const;
154
155 void segment_name(const std::string& name);
156 void address(uint64_t address);
157 void alignment(uint32_t align);
158 void relocation_offset(uint32_t relocOffset);
159 void numberof_relocations(uint32_t nbReloc);
160 void flags(uint32_t flags);
161 void flags(flag_list_t flags);
162 void type(MACHO_SECTION_TYPES type);
163 void reserved1(uint32_t reserved1);
164 void reserved2(uint32_t reserved2);
165 void reserved3(uint32_t reserved3);
166
168 bool has(MACHO_SECTION_FLAGS flag) const;
169
172
175
176 Section& operator+=(MACHO_SECTION_FLAGS flag);
177 Section& operator-=(MACHO_SECTION_FLAGS flag);
178
179 void accept(Visitor& visitor) const override;
180
181
182 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& section);
183
184 private:
185 std::string segment_name_;
186 uint64_t original_size_ = 0;
187 uint32_t align_ = 0;
188 uint32_t relocations_offset_ = 0;
189 uint32_t nbof_relocations_ = 0;
190 uint32_t flags_ = 0;
191 uint32_t reserved1_ = 0;
192 uint32_t reserved2_ = 0;
193 uint32_t reserved3_ = 0;
194 content_t content_;
195 SegmentCommand *segment_ = nullptr;
196 relocations_t relocations_;
197};
198
199}
200}
201#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:73
Class which represents a MachO binary.
Definition MachO/Binary.hpp:74
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
span< const uint8_t > content() const override
section's content
uint32_t reserved1() const
According to the official loader.h file, this value is reserved for offset or index
void remove(MACHO_SECTION_FLAGS flag)
Remove a MACHO_SECTION_FLAGS to the current section.
uint32_t raw_flags() const
Section flags without applying the SECTION_FLAGS_MASK mask.
SegmentCommand * segment()
The segment associated with this section or a nullptr if not present.
void add(MACHO_SECTION_FLAGS flag)
Append a MACHO_SECTION_FLAGS to the current section.
uint32_t flags() const
Section's flags masked with SECTION_FLAGS_MASK (see: MACHO_SECTION_FLAGS)
uint32_t relocation_offset() const
Offset of the relocation table. This value should be 0 for executable and libraries as the relocation...
const std::string & segment_name() const
Return the name of the segment linked to this section.
flag_list_t flags_list() const
Return the Section::flags as an std::set of MACHO_SECTION_FLAGS.
void clear(uint8_t v)
Clear the content of this section by filling its values with the byte provided in parameter.
uint64_t address() const
Virtual base address of the section.
bool has_segment() const
Check if this section is correctly linked with a MachO::SegmentCommand.
uint32_t reserved3() const
This value is only present for 64 bits Mach-O files. In that case, the value is reserved.
void content(const content_t &data) override
Update the content of the section.
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition MachO/Section.hpp:58
MACHO_SECTION_TYPES type() const
Type of the section. This value can help to determine the purpose of the section (e....
uint32_t alignment() const
Section alignment as a power of 2.
it_relocations relocations()
Return an iterator over the MachO::Relocation associated with this section.
uint32_t numberof_relocations() const
Number of relocations associated with this section.
bool has(MACHO_SECTION_FLAGS flag) const
Check if the section has the given MACHO_SECTION_FLAGS flag.
uint32_t reserved2() const
According to the official loader.h file, this value is reserved for count or sizeof
Class which represents a LOAD_COMMAND_TYPES::LC_SEGMENT / LOAD_COMMAND_TYPES::LC_SEGMENT_64 command.
Definition SegmentCommand.hpp:48
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
MACHO_SECTION_FLAGS
Definition MachO/enums.hpp:193
MACHO_SECTION_TYPES
These are the section type and attributes fields. A MachO section can have only one Type,...
Definition MachO/enums.hpp:164
LIEF namespace.
Definition Abstract/Binary.hpp:32