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
24 ObjectContainerMachOArchive)
25
30}
31
34}
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),
70 m_header(), m_fat_archs() {
71 memset(&m_header, 0, sizeof(m_header));
72}
73
75
77 bool success = ParseHeader(m_data, m_header, m_fat_archs);
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
80 m_data.Clear();
81 return success;
82}
83
85 lldb_private::DataExtractor &data, llvm::MachO::fat_header &header,
86 std::vector<FatArch> &fat_archs) {
87 // Store the file offset for this universal file as we could have a universal
88 // .o file in a BSD archive, or be contained in another kind of object.
89 lldb::offset_t offset = 0;
91 header.magic = data.GetU32(&offset);
92 fat_archs.clear();
93
94 // Universal mach-o files always have their headers in big endian.
95 if (header.magic == FAT_MAGIC || header.magic == FAT_MAGIC_64) {
96 const bool is_fat64 = header.magic == FAT_MAGIC_64;
97 data.SetAddressByteSize(is_fat64 ? 8 : 4);
98
99 header.nfat_arch = data.GetU32(&offset);
100
101 // Now we should have enough data for all of the fat headers, so lets index
102 // them so we know how many architectures that this universal binary
103 // contains.
104 for (uint32_t arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) {
105 if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) {
106 if (is_fat64) {
107 fat_arch_64 arch;
108 arch.cputype = data.GetU32(&offset);
109 arch.cpusubtype = data.GetU32(&offset);
110 arch.offset = data.GetU64(&offset);
111 arch.size = data.GetU64(&offset);
112 arch.align = data.GetU32(&offset);
113 arch.reserved = data.GetU32(&offset);
114 fat_archs.emplace_back(arch);
115 } else {
116 fat_arch arch;
117 arch.cputype = data.GetU32(&offset);
118 arch.cpusubtype = data.GetU32(&offset);
119 arch.offset = data.GetU32(&offset);
120 arch.size = data.GetU32(&offset);
121 arch.align = data.GetU32(&offset);
122 fat_archs.emplace_back(arch);
123 }
124 }
125 }
126 return true;
127 }
128
129 memset(&header, 0, sizeof(header));
130 return true;
131}
132
134 return m_header.nfat_arch;
135}
136
138 uint32_t idx, ArchSpec &arch) const {
139 if (idx < m_header.nfat_arch) {
140 arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].GetCPUType(),
141 m_fat_archs[idx].GetCPUSubType());
142 return true;
143 }
144 return false;
145}
146
149 uint32_t arch_idx = 0;
150 ArchSpec arch;
151 // If the module hasn't specified an architecture yet, set it to the default
152 // architecture:
153 ModuleSP module_sp(GetModule());
154 if (module_sp) {
155 if (!module_sp->GetArchitecture().IsValid()) {
157 if (!arch.IsValid())
159 } else
160 arch = module_sp->GetArchitecture();
161
162 ArchSpec curr_arch;
163 // First, try to find an exact match for the Arch of the Target.
164 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
165 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
166 arch.IsExactMatch(curr_arch))
167 break;
168 }
169
170 // Failing an exact match, try to find a compatible Arch of the Target.
171 if (arch_idx >= m_header.nfat_arch) {
172 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
173 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
174 arch.IsCompatibleMatch(curr_arch))
175 break;
176 }
177 }
178
179 if (arch_idx < m_header.nfat_arch) {
180 DataBufferSP data_sp;
181 lldb::offset_t data_offset = 0;
183 module_sp, file, m_offset + m_fat_archs[arch_idx].GetOffset(),
184 m_fat_archs[arch_idx].GetSize(), data_sp, data_offset);
185 }
186 }
187 return ObjectFileSP();
188}
189
191 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
192 lldb::offset_t data_offset, lldb::offset_t file_offset,
194 const size_t initial_count = specs.GetSize();
195
196 DataExtractor data;
197 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
198
200 llvm::MachO::fat_header header;
201 std::vector<FatArch> fat_archs;
202 if (ParseHeader(data, header, fat_archs)) {
203 for (const FatArch &fat_arch : fat_archs) {
204 const lldb::offset_t slice_file_offset =
205 fat_arch.GetOffset() + file_offset;
206 if (fat_arch.GetOffset() < file_size && file_size > slice_file_offset) {
208 file, slice_file_offset, file_size - slice_file_offset, specs);
209 }
210 }
211 }
212 }
213 return specs.GetSize() - initial_count;
214}
#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName)
Definition: PluginManager.h:25
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 size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
static llvm::StringRef GetPluginNameStatic()
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:31
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:348
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:747
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:851
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition: ArchSpec.h:502
bool IsExactMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, ExactMatch).
Definition: ArchSpec.h:497
An data extractor class.
Definition: DataExtractor.h:48
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 Clear()
Clears the object state.
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.
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.
Definition: ModuleChild.cpp:24
A plug-in interface definition class for object containers.
DataExtractor m_data
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.
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::DataBufferSP &data_sp, lldb::offset_t &data_offset)
Find a ObjectFile plug-in that can parse file_spec.
Definition: ObjectFile.cpp:53
static size_t GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs, lldb::DataBufferSP data_sp=lldb::DataBufferSP())
Definition: ObjectFile.cpp:187
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static ArchSpec GetDefaultArchitecture()
Definition: Target.cpp:2605
#define LLDB_ARCH_DEFAULT
CPU Type definitions.
Definition: lldb-defines.h:101
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
Definition: lldb-forward.h:370
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:331
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:368