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 }
126 }
127 return true;
128 }
129
130 memset(&header, 0, sizeof(header));
131 return true;
132}
133
135 return m_header.nfat_arch;
136}
137
139 uint32_t idx, ArchSpec &arch) const {
140 if (idx < m_header.nfat_arch) {
141 arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].GetCPUType(),
142 m_fat_archs[idx].GetCPUSubType());
143 return true;
144 }
145 return false;
146}
147
150 uint32_t arch_idx = 0;
151 ArchSpec arch;
152 // If the module hasn't specified an architecture yet, set it to the default
153 // architecture:
154 ModuleSP module_sp(GetModule());
155 if (module_sp) {
156 if (!module_sp->GetArchitecture().IsValid()) {
158 if (!arch.IsValid())
160 } else
161 arch = module_sp->GetArchitecture();
162
163 ArchSpec curr_arch;
164 // First, try to find an exact match for the Arch of the Target.
165 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
166 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
167 arch.IsExactMatch(curr_arch))
168 break;
169 }
170
171 // Failing an exact match, try to find a compatible Arch of the Target.
172 if (arch_idx >= m_header.nfat_arch) {
173 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
174 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
175 arch.IsCompatibleMatch(curr_arch))
176 break;
177 }
178 }
179
180 if (arch_idx < m_header.nfat_arch) {
181 DataExtractorSP extractor_sp;
182 lldb::offset_t data_offset = 0;
184 module_sp, file, m_offset + m_fat_archs[arch_idx].GetOffset(),
185 m_fat_archs[arch_idx].GetSize(), extractor_sp, data_offset);
186 }
187 }
188 return ObjectFileSP();
189}
190
192 const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp,
193 lldb::offset_t data_offset, lldb::offset_t file_offset,
195 const size_t initial_count = specs.GetSize();
196 if (!extractor_sp)
197 return initial_count;
198
199 DataExtractorSP data_extractor_sp =
200 extractor_sp->GetSubsetExtractorSP(data_offset);
201 if (ObjectContainerUniversalMachO::MagicBytesMatch(*data_extractor_sp)) {
202 llvm::MachO::fat_header header;
203 std::vector<FatArch> fat_archs;
204 if (ParseHeader(*data_extractor_sp, header, fat_archs)) {
205 for (const FatArch &fat_arch : fat_archs) {
206 const lldb::offset_t slice_file_offset =
207 fat_arch.GetOffset() + file_offset;
208 if (fat_arch.GetOffset() < file_size && file_size > slice_file_offset) {
210 file, slice_file_offset, file_size - slice_file_offset, specs);
211 }
212 }
213 }
214 }
215 return specs.GetSize() - initial_count;
216}
#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 size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
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:367
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition ArchSpec.cpp:739
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:843
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition ArchSpec.h:509
bool IsExactMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, ExactMatch).
Definition ArchSpec.h:504
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.
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 size_t GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs, 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:2807
#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