LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
FatBinary.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_MACHO_FAT_BINARY_H
17#define LIEF_MACHO_FAT_BINARY_H
18#include <string>
19#include <vector>
20#include <memory>
21
22#include "LIEF/visibility.h"
23#include "LIEF/iterators.hpp"
24#include "LIEF/MachO/Header.hpp"
25
26namespace LIEF {
27class Parser;
28namespace MachO {
29
30class Parser;
31class Builder;
32class Binary;
33
37 friend class LIEF::Parser;
38 friend class Parser;
39 friend class Builder;
40
41 public:
43 using binaries_t = std::vector<std::unique_ptr<Binary>>;
44
47
50
51 FatBinary(const FatBinary&) = delete;
52 FatBinary& operator=(const FatBinary&) = delete;
53
59 static std::unique_ptr<FatBinary> create(binaries_t binaries);
60
61 virtual ~FatBinary();
62
64 size_t size() const {
65 return binaries_.size();
66 }
67
69 bool empty() const {
70 return binaries_.empty();
71 }
72
74 return binaries_;
75 }
77 return binaries_;
78 }
79
81 return it_binaries(binaries_).end();
82 }
83
85 return it_const_binaries(binaries_).end();
86 }
87
89 for (auto& bin : binaries_) {
90 bin.release(); // NOLINT(bugprone-unused-return-value)
91 }
92 }
93
96 std::unique_ptr<Binary> pop_back();
97
100 Binary* at(size_t index) {
101 return const_cast<Binary*>(static_cast<const FatBinary*>(this)->at(index));
102 }
103
104 const Binary* at(size_t index) const {
105 if (index >= size()) {
106 return nullptr;
107 }
108 return binaries_[index].get();
109 }
110
112 return const_cast<Binary*>(static_cast<const FatBinary*>(this)->back());
113 }
114 const Binary* back() const {
115 return binaries_.empty() ? nullptr : binaries_.back().get();
116 }
117
119 return const_cast<Binary*>(static_cast<const FatBinary*>(this)->front());
120 }
121
122 const Binary* front() const {
123 return binaries_.empty() ? nullptr : binaries_.front().get();
124 }
125
126 Binary* operator[](size_t index) {
127 return at(index);
128 }
129 const Binary* operator[](size_t index) const {
130 return at(index);
131 }
132
137 std::unique_ptr<Binary> take(size_t index);
138
141 std::unique_ptr<Binary> take(Header::CPU_TYPE cpu);
142
145 void write(const std::string& filename);
146
148 std::vector<uint8_t> raw();
149
151 return const_cast<Binary*>(static_cast<const FatBinary*>(this)->get(cpu));
152 }
153
155 const Binary* get(Header::CPU_TYPE cpu) const;
156
158 return get(cpu);
159 }
160
162 return get(cpu);
163 }
164
165 LIEF_API friend std::ostream& operator<<(std::ostream& os,
166 const FatBinary& fatbinary);
167
168 private:
171 binaries_t binaries_;
172};
173
174} // namespace MachO
175} // namespace LIEF
176#endif
Class which represents a MachO binary.
Definition MachO/Binary.hpp:88
const LoadCommand * get(LoadCommand::TYPE type) const
Return the LoadCommand associated with the given LoadCommand::TYPE or a nullptr if the command can't ...
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:63
it_binaries end()
Definition FatBinary.hpp:80
Binary * front()
Definition FatBinary.hpp:118
std::unique_ptr< Binary > pop_back()
Get a pointer to the last MachO::Binary object presents in this Fat Binary. It returns a nullptr if n...
bool empty() const
Checks whether this object contains MachO::Binary.
Definition FatBinary.hpp:69
std::unique_ptr< Binary > take(Header::CPU_TYPE cpu)
Take the underlying MachO::Binary that matches the given architecture If no binary with the architect...
std::vector< uint8_t > raw()
Reconstruct the Fat binary object and return his content as bytes.
FatBinary(const FatBinary &)=delete
friend std::ostream & operator<<(std::ostream &os, const FatBinary &fatbinary)
const Binary * operator[](Header::CPU_TYPE cpu) const
Definition FatBinary.hpp:161
ref_iterator< binaries_t &, Binary * > it_binaries
Iterator that outputs Binary&.
Definition FatBinary.hpp:46
std::unique_ptr< Binary > take(size_t index)
Extract a MachO::Binary object. Gives ownership to the caller, and remove it from this FatBinary obje...
const Binary * at(size_t index) const
Definition FatBinary.hpp:104
const Binary * back() const
Definition FatBinary.hpp:114
Binary * operator[](size_t index)
Definition FatBinary.hpp:126
it_binaries begin()
Definition FatBinary.hpp:73
Binary * get(Header::CPU_TYPE cpu)
Definition FatBinary.hpp:150
friend class Builder
Definition FatBinary.hpp:39
it_const_binaries begin() const
Definition FatBinary.hpp:76
std::vector< std::unique_ptr< Binary > > binaries_t
Internal container used to store Binary objects within a Fat Mach-O.
Definition FatBinary.hpp:43
static std::unique_ptr< FatBinary > create(binaries_t binaries)
Create a FatBinary object from the provided list of Binary objects.
const Binary * get(Header::CPU_TYPE cpu) const
Gets a pointer to the MachO::Binary that matches the given architecture.
Binary * back()
Definition FatBinary.hpp:111
const Binary * front() const
Definition FatBinary.hpp:122
void write(const std::string &filename)
Reconstruct the Fat binary object and write it in filename.
FatBinary & operator=(const FatBinary &)=delete
friend class LIEF::Parser
Definition FatBinary.hpp:37
it_const_binaries end() const
Definition FatBinary.hpp:84
const_ref_iterator< const binaries_t &, Binary * > it_const_binaries
Iterator that outputs const Binary&.
Definition FatBinary.hpp:49
Binary * at(size_t index)
Get a pointer to the MachO::Binary specified by the index. It returns a nullptr if the binary does no...
Definition FatBinary.hpp:100
Binary * operator[](Header::CPU_TYPE cpu)
Definition FatBinary.hpp:157
size_t size() const
Number of MachO::Binary wrapped by this object.
Definition FatBinary.hpp:64
const Binary * operator[](size_t index) const
Definition FatBinary.hpp:129
void release_all_binaries()
Definition FatBinary.hpp:88
CPU_TYPE
Definition MachO/Header.hpp:107
The main interface to parse a Mach-O binary.
Definition MachO/Parser.hpp:42
Main interface to parse an executable regardless of its format.
Definition Abstract/Parser.hpp:30
Iterator which returns reference on container's values.
Definition iterators.hpp:45
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:40
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:286
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44