LLDB mainline
ObjectContainerUniversalMachO.cpp
Go to the documentation of this file.
1//===-- ObjectContainerUniversalMachO.cpp ---------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "lldb/Core/Module.h"
14#include "lldb/Target/Target.h"
17#include "lldb/Utility/Stream.h"
18
19using namespace lldb;
20using namespace lldb_private;
21using namespace llvm::MachO;
22
31
35
37 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
38 lldb::offset_t data_offset, const FileSpec *file,
39 lldb::offset_t file_offset, lldb::offset_t length) {
40 // We get data when we aren't trying to look for cached container
41 // information, so only try and look for an architecture slice if we get data
42 if (data_sp) {
43 DataExtractor data;
44 data.SetData(data_sp, data_offset, length);
46 std::unique_ptr<ObjectContainerUniversalMachO> container_up(
47 new ObjectContainerUniversalMachO(module_sp, data_sp, data_offset,
48 file, file_offset, length));
49 if (container_up->ParseHeader()) {
50 return container_up.release();
51 }
52 }
53 }
54 return nullptr;
55}
56
58 lldb::offset_t offset = 0;
59 uint32_t magic = data.GetU32(&offset);
60 return magic == FAT_MAGIC || magic == FAT_CIGAM || magic == FAT_MAGIC_64 ||
61 magic == FAT_CIGAM_64;
62}
63
65 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
66 lldb::offset_t data_offset, const FileSpec *file,
67 lldb::offset_t file_offset, lldb::offset_t length)
68 : ObjectContainer(module_sp, file, file_offset, length, data_sp,
69 data_offset),
71 memset(&m_header, 0, sizeof(m_header));
72}
73
75
78 // We no longer need any data, we parsed all we needed to parse and cached it
79 // in m_header and m_fat_archs. Need to have an empty DataExtractor for code
80 // that assumes it is non-null.
81 m_extractor_sp = std::make_shared<DataExtractor>();
82 return success;
83}
84
86 lldb_private::DataExtractor &extractor, llvm::MachO::fat_header &header,
87 std::vector<FatArch> &fat_archs) {
88 // Store the file offset for this universal file as we could have a universal
89 // .o file in a BSD archive, or be contained in another kind of object.
90 lldb::offset_t offset = 0;
91 extractor.SetByteOrder(eByteOrderBig);
92 header.magic = extractor.GetU32(&offset);
93 fat_archs.clear();
94
95 // Universal mach-o files always have their headers in big endian.
96 if (header.magic == FAT_MAGIC || header.magic == FAT_MAGIC_64) {
97 const bool is_fat64 = header.magic == FAT_MAGIC_64;
98 extractor.SetAddressByteSize(is_fat64 ? 8 : 4);
99
100 header.nfat_arch = extractor.GetU32(&offset);
101
102 // Now we should have enough data for all of the fat headers, so lets index
103 // them so we know how many architectures that this universal binary
104 // contains.
105 for (uint32_t arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) {
106 if (extractor.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) {
107 if (is_fat64) {
108 fat_arch_64 arch;
109 arch.cputype = extractor.GetU32(&offset);
110 arch.cpusubtype = extractor.GetU32(&offset);
111 arch.offset = extractor.GetU64(&offset);
112 arch.size = extractor.GetU64(&offset);
113 arch.align = extractor.GetU32(&offset);
114 arch.reserved = extractor.GetU32(&offset);
115 fat_archs.emplace_back(arch);
116 } else {
117 fat_arch arch;
118 arch.cputype = extractor.GetU32(&offset);
119 arch.cpusubtype = extractor.GetU32(&offset);
120 arch.offset = extractor.GetU32(&offset);
121 arch.size = extractor.GetU32(&offset);
122 arch.align = extractor.GetU32(&offset);
123 fat_archs.emplace_back(arch);
124 }
125 } else {
126 // nfat_arch is read from the file and is untrusted (up to 0xFFFFFFFF).
127 // Once the data is exhausted the offset can no longer advance, so the
128 // remaining iterations would spin uselessly; stop here.
129 break;
130 }
131 }
132 return true;
133 }
134
135 memset(&header, 0, sizeof(header));
136 return true;
137}
138
140 return m_header.nfat_arch;
141}
142
144 uint32_t idx, ArchSpec &arch) const {
145 if (idx < m_header.nfat_arch) {
146 arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].GetCPUType(),
147 m_fat_archs[idx].GetCPUSubType());
148 return true;
149 }
150 return false;
151}
152
155 uint32_t arch_idx = 0;
156 ArchSpec arch;
157 // If the module hasn't specified an architecture yet, set it to the default
158 // architecture:
159 ModuleSP module_sp(GetModule());
160 if (module_sp) {
161 if (!module_sp->GetArchitecture().IsValid()) {
163 if (!arch.IsValid())
165 } else
166 arch = module_sp->GetArchitecture();
167
168 ArchSpec curr_arch;
169 // First, try to find an exact match for the Arch of the Target.
170 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
171 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
172 arch.IsExactMatch(curr_arch))
173 break;
174 }
175
176 // Failing an exact match, try to find a compatible Arch of the Target.
177 if (arch_idx >= m_header.nfat_arch) {
178 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
179 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
180 arch.IsCompatibleMatch(curr_arch))
181 break;
182 }
183 }
184
185 if (arch_idx < m_header.nfat_arch) {
186 DataExtractorSP extractor_sp;
187 lldb::offset_t data_offset = 0;
189 module_sp, file, m_offset + m_fat_archs[arch_idx].GetOffset(),
190 m_fat_archs[arch_idx].GetSize(), extractor_sp, data_offset);
191 }
192 }
193 return ObjectFileSP();
194}
195
197 const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp,
198 lldb::offset_t file_offset, lldb::offset_t file_size) {
199 if (!extractor_sp)
200 return {};
201
202 ModuleSpecList specs;
204 llvm::MachO::fat_header header;
205 std::vector<FatArch> fat_archs;
206 if (ParseHeader(*extractor_sp, header, fat_archs)) {
207 for (const FatArch &fat_arch : fat_archs) {
208 const lldb::offset_t slice_file_offset =
209 fat_arch.GetOffset() + file_offset;
210 // The slice must start strictly past the current offset. A slice whose
211 // offset is 0 (or otherwise does not advance) is self-referential: the
212 // recursive call below would re-parse the same range and recurse
213 // without bound. Skip such slices instead of overflowing the stack.
214 if (slice_file_offset > file_offset &&
215 fat_arch.GetOffset() < file_size && file_size > slice_file_offset) {
217 file, slice_file_offset, file_size - slice_file_offset);
218 specs.Append(arch_specs);
219 }
220 }
221 }
222 }
223 return specs;
224}
#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName)
bool GetArchitectureAtIndex(uint32_t cpu_idx, lldb_private::ArchSpec &arch) const override
Gets the architecture given an index.
size_t GetNumArchitectures() const override
Get the number of architectures in this object file.
static lldb_private::ModuleSpecList GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp, lldb::offset_t file_offset, lldb::offset_t length)
ObjectContainerUniversalMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
~ObjectContainerUniversalMachO() override
static bool MagicBytesMatch(const lldb_private::DataExtractor &data)
static llvm::StringRef GetPluginDescriptionStatic()
lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override
Selects an architecture in an object file.
static lldb_private::ObjectContainer * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
bool ParseHeader() override
Attempts to parse the object header.
An architecture specification class.
Definition ArchSpec.h:32
bool IsValid() const
Tests if this ArchSpec is valid.
Definition ArchSpec.h:370
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition ArchSpec.cpp:748
bool SetArchitecture(ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os=0)
Change the architecture object type, CPU type and OS type.
Definition ArchSpec.cpp:852
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition ArchSpec.h:512
bool IsExactMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, ExactMatch).
Definition ArchSpec.h:507
An data extractor class.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
virtual lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
A file utility class.
Definition FileSpec.h:57
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
void Append(const ModuleSpec &spec)
Definition ModuleSpec.h:341
lldb::DataExtractorSP m_extractor_sp
The data for this object file so things can be parsed lazily.
lldb::addr_t m_offset
The offset in bytes into the file, or the address in memory.
ObjectContainer(const lldb::ModuleSP &module_sp, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataBufferSP data_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
virtual lldb::addr_t GetOffset() const
Returns the offset into a file at which this object resides.
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataExtractorSP extractor_sp, lldb::offset_t &data_offset)
Find a ObjectFile plug-in that can parse file_spec.
static ModuleSpecList GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataExtractorSP=lldb::DataExtractorSP())
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static ArchSpec GetDefaultArchitecture()
Definition Target.cpp:2865
#define LLDB_ARCH_DEFAULT
CPU Type definitions.
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP