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
19#include <algorithm>
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace llvm::MachO;
24
33
37
39 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
40 lldb::offset_t data_offset, const FileSpec *file,
41 lldb::offset_t file_offset, lldb::offset_t length) {
42 // We get data when we aren't trying to look for cached container
43 // information, so only try and look for an architecture slice if we get data
44 if (data_sp) {
45 DataExtractor data;
46 data.SetData(data_sp, data_offset, length);
48 std::unique_ptr<ObjectContainerUniversalMachO> container_up(
49 new ObjectContainerUniversalMachO(module_sp, data_sp, data_offset,
50 file, file_offset, length));
51 if (container_up->ParseHeader()) {
52 return container_up.release();
53 }
54 }
55 }
56 return nullptr;
57}
58
60 lldb::offset_t offset = 0;
61 uint32_t magic = data.GetU32(&offset);
62 return magic == FAT_MAGIC || magic == FAT_CIGAM || magic == FAT_MAGIC_64 ||
63 magic == FAT_CIGAM_64;
64}
65
67 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
68 lldb::offset_t data_offset, const FileSpec *file,
69 lldb::offset_t file_offset, lldb::offset_t length)
70 : ObjectContainer(module_sp, file, file_offset, length, data_sp,
71 data_offset),
73 memset(&m_header, 0, sizeof(m_header));
74}
75
77
80 // We no longer need any data, we parsed all we needed to parse and cached it
81 // in m_header and m_fat_archs. Need to have an empty DataExtractor for code
82 // that assumes it is non-null.
83 m_extractor_sp = std::make_shared<DataExtractor>();
84 return success;
85}
86
88 lldb_private::DataExtractor &extractor, llvm::MachO::fat_header &header,
89 std::vector<FatArch> &fat_archs) {
90 // Store the file offset for this universal file as we could have a universal
91 // .o file in a BSD archive, or be contained in another kind of object.
92 lldb::offset_t offset = 0;
93 extractor.SetByteOrder(eByteOrderBig);
94 header.magic = extractor.GetU32(&offset);
95 fat_archs.clear();
96
97 // Universal mach-o files always have their headers in big endian.
98 if (header.magic == FAT_MAGIC || header.magic == FAT_MAGIC_64) {
99 const bool is_fat64 = header.magic == FAT_MAGIC_64;
100 extractor.SetAddressByteSize(is_fat64 ? 8 : 4);
101
102 header.nfat_arch = extractor.GetU32(&offset);
103
104 // Now we should have enough data for all of the fat headers, so lets index
105 // them so we know how many architectures that this universal binary
106 // contains.
107 for (uint32_t arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) {
108 if (extractor.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) {
109 if (is_fat64) {
110 fat_arch_64 arch;
111 arch.cputype = extractor.GetU32(&offset);
112 arch.cpusubtype = extractor.GetU32(&offset);
113 arch.offset = extractor.GetU64(&offset);
114 arch.size = extractor.GetU64(&offset);
115 arch.align = extractor.GetU32(&offset);
116 arch.reserved = extractor.GetU32(&offset);
117 fat_archs.emplace_back(arch);
118 } else {
119 fat_arch arch;
120 arch.cputype = extractor.GetU32(&offset);
121 arch.cpusubtype = extractor.GetU32(&offset);
122 arch.offset = extractor.GetU32(&offset);
123 arch.size = extractor.GetU32(&offset);
124 arch.align = extractor.GetU32(&offset);
125 fat_archs.emplace_back(arch);
126 }
127 } else {
128 // nfat_arch is read from the file and is untrusted (up to 0xFFFFFFFF).
129 // Once the data is exhausted the offset can no longer advance, so the
130 // remaining iterations would spin uselessly; stop here.
131 break;
132 }
133 }
134 return true;
135 }
136
137 memset(&header, 0, sizeof(header));
138 return true;
139}
140
142 return m_header.nfat_arch;
143}
144
146 uint32_t idx, ArchSpec &arch) const {
147 if (idx < m_fat_archs.size()) {
150 return true;
151 }
152 return false;
153}
154
157 uint32_t arch_idx = 0;
158 ArchSpec arch;
159 // If the module hasn't specified an architecture yet, set it to the default
160 // architecture:
161 ModuleSP module_sp(GetModule());
162 if (module_sp) {
163 if (!module_sp->GetArchitecture().IsValid()) {
165 if (!arch.IsValid())
167 } else
168 arch = module_sp->GetArchitecture();
169
170 ArchSpec curr_arch;
171 // First, try to find an exact match for the Arch of the Target.
172 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
173 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
174 arch.IsExactMatch(curr_arch))
175 break;
176 }
177
178 // Failing an exact match, try to find a compatible Arch of the Target.
179 if (arch_idx >= m_header.nfat_arch) {
180 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
181 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
182 arch.IsCompatibleMatch(curr_arch))
183 break;
184 }
185 }
186
187 if (arch_idx < m_header.nfat_arch) {
188 const FatArch &fat_arch = m_fat_archs[arch_idx];
189 const uint64_t byte_size = GetByteSize();
190 // The slice must start strictly past this container's own bytes and
191 // fit within them.
192 if (fat_arch.GetOffset() > 0 && fat_arch.GetOffset() < byte_size) {
193 DataExtractorSP extractor_sp;
194 lldb::offset_t data_offset = 0;
195 const uint64_t slice_size =
196 std::min(fat_arch.GetSize(), byte_size - fat_arch.GetOffset());
197 return ObjectFile::FindPlugin(module_sp, file,
198 m_offset + fat_arch.GetOffset(),
199 slice_size, extractor_sp, data_offset);
200 }
201 }
202 }
203 return ObjectFileSP();
204}
205
207 const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp,
208 lldb::offset_t file_offset, lldb::offset_t file_size) {
209 if (!extractor_sp)
210 return {};
211
212 ModuleSpecList specs;
214 llvm::MachO::fat_header header;
215 std::vector<FatArch> fat_archs;
216 if (ParseHeader(*extractor_sp, header, fat_archs)) {
217 for (const FatArch &fat_arch : fat_archs) {
218 const lldb::offset_t slice_file_offset =
219 fat_arch.GetOffset() + file_offset;
220 // The slice must start strictly past the current offset. A slice whose
221 // offset is 0 (or otherwise does not advance) is self-referential: the
222 // recursive call below would re-parse the same range and recurse
223 // without bound. Skip such slices instead of overflowing the stack.
224 if (slice_file_offset > file_offset &&
225 fat_arch.GetOffset() < file_size && file_size > slice_file_offset) {
227 file, slice_file_offset, file_size - slice_file_offset);
228 specs.Append(arch_specs);
229 }
230 }
231 }
232 }
233 return specs;
234}
static uint32_t GetCPUType(const ArchDefinition *def, ArchSpec::Core core)
Definition ArchSpec.cpp:857
static uint32_t GetCPUSubType(const ArchDefinition *def, ArchSpec::Core core)
Definition ArchSpec.cpp:864
#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:453
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition ArchSpec.cpp:949
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.
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition ArchSpec.h:597
bool IsExactMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, ExactMatch).
Definition ArchSpec.h:592
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:56
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
void Append(const ModuleSpec &spec)
Definition ModuleSpec.h:371
virtual lldb::addr_t GetByteSize() const
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.
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:2909
#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:86
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