blob: a412254e1eb0c53583f41aba6959bc219243f325 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ObjectContainerUniversalMachO.cpp -----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectContainerUniversalMachO.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Core/ArchSpec.h"
Greg Clayton5ce9c562013-02-06 17:22:03 +000012#include "lldb/Core/DataBuffer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000014#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/PluginManager.h"
Greg Clayton5ce9c562013-02-06 17:22:03 +000016#include "lldb/Core/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/ObjectFile.h"
Caroline Ticedaccaa92010-09-20 20:44:43 +000018#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb;
21using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000022using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
Kate Stoneb9c1b512016-09-06 20:57:50 +000024void ObjectContainerUniversalMachO::Initialize() {
25 PluginManager::RegisterPlugin(GetPluginNameStatic(),
26 GetPluginDescriptionStatic(), CreateInstance,
27 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028}
29
Kate Stoneb9c1b512016-09-06 20:57:50 +000030void ObjectContainerUniversalMachO::Terminate() {
31 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032}
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginNameStatic() {
35 static ConstString g_name("mach-o");
36 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039const char *ObjectContainerUniversalMachO::GetPluginDescriptionStatic() {
40 return "Universal mach-o object container reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041}
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043ObjectContainer *ObjectContainerUniversalMachO::CreateInstance(
44 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
45 lldb::offset_t data_offset, const FileSpec *file,
46 lldb::offset_t file_offset, lldb::offset_t length) {
47 // We get data when we aren't trying to look for cached container information,
48 // so only try and look for an architecture slice if we get data
49 if (data_sp) {
50 DataExtractor data;
51 data.SetData(data_sp, data_offset, length);
52 if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) {
53 std::unique_ptr<ObjectContainerUniversalMachO> container_ap(
54 new ObjectContainerUniversalMachO(module_sp, data_sp, data_offset,
55 file, file_offset, length));
56 if (container_ap->ParseHeader()) {
57 return container_ap.release();
58 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 }
61 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062}
63
Kate Stoneb9c1b512016-09-06 20:57:50 +000064bool ObjectContainerUniversalMachO::MagicBytesMatch(const DataExtractor &data) {
65 lldb::offset_t offset = 0;
66 uint32_t magic = data.GetU32(&offset);
67 return magic == FAT_MAGIC || magic == FAT_CIGAM;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068}
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070ObjectContainerUniversalMachO::ObjectContainerUniversalMachO(
71 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
72 lldb::offset_t data_offset, const FileSpec *file,
73 lldb::offset_t file_offset, lldb::offset_t length)
74 : ObjectContainer(module_sp, file, file_offset, length, data_sp,
75 data_offset),
76 m_header(), m_fat_archs() {
77 memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080ObjectContainerUniversalMachO::~ObjectContainerUniversalMachO() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082bool ObjectContainerUniversalMachO::ParseHeader() {
83 bool success = ParseHeader(m_data, m_header, m_fat_archs);
84 // We no longer need any data, we parsed all we needed to parse
85 // and cached it in m_header and m_fat_archs
86 m_data.Clear();
87 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090bool ObjectContainerUniversalMachO::ParseHeader(
91 lldb_private::DataExtractor &data, llvm::MachO::fat_header &header,
92 std::vector<llvm::MachO::fat_arch> &fat_archs) {
93 bool success = false;
94 // Store the file offset for this universal file as we could have a universal
95 // .o file
96 // in a BSD archive, or be contained in another kind of object.
97 // Universal mach-o files always have their headers in big endian.
98 lldb::offset_t offset = 0;
99 data.SetByteOrder(eByteOrderBig);
100 header.magic = data.GetU32(&offset);
101 fat_archs.clear();
Greg Claytonf4d6de62013-04-24 22:29:28 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (header.magic == FAT_MAGIC) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 data.SetAddressByteSize(4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 header.nfat_arch = data.GetU32(&offset);
108
109 // Now we should have enough data for all of the fat headers, so lets index
110 // them so we know how many architectures that this universal binary
111 // contains.
112 uint32_t arch_idx = 0;
113 for (arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) {
114 if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) {
115 fat_arch arch;
116 if (data.GetU32(&offset, &arch, sizeof(fat_arch) / sizeof(uint32_t)))
117 fat_archs.push_back(arch);
118 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 success = true;
121 } else {
122 memset(&header, 0, sizeof(header));
123 }
124 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127void ObjectContainerUniversalMachO::Dump(Stream *s) const {
128 s->Printf("%p: ", static_cast<const void *>(this));
129 s->Indent();
130 const size_t num_archs = GetNumArchitectures();
131 const size_t num_objects = GetNumObjects();
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000132 s->Printf("ObjectContainerUniversalMachO, num_archs = %zu, num_objects = %zu",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 num_archs, num_objects);
134 uint32_t i;
135 ArchSpec arch;
136 s->IndentMore();
137 for (i = 0; i < num_archs; i++) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 s->Indent();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 GetArchitectureAtIndex(i, arch);
140 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
141 }
142 for (i = 0; i < num_objects; i++) {
143 s->Indent();
144 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
145 }
146 s->IndentLess();
147 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148}
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150size_t ObjectContainerUniversalMachO::GetNumArchitectures() const {
151 return m_header.nfat_arch;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152}
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154bool ObjectContainerUniversalMachO::GetArchitectureAtIndex(
155 uint32_t idx, ArchSpec &arch) const {
156 if (idx < m_header.nfat_arch) {
157 arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].cputype,
158 m_fat_archs[idx].cpusubtype);
159 return true;
160 }
161 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162}
163
Greg Clayton762f7132011-09-18 18:59:15 +0000164ObjectFileSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165ObjectContainerUniversalMachO::GetObjectFile(const FileSpec *file) {
166 uint32_t arch_idx = 0;
167 ArchSpec arch;
168 // If the module hasn't specified an architecture yet, set it to the default
169 // architecture:
170 ModuleSP module_sp(GetModule());
171 if (module_sp) {
172 if (!module_sp->GetArchitecture().IsValid()) {
173 arch = Target::GetDefaultArchitecture();
174 if (!arch.IsValid())
175 arch.SetTriple(LLDB_ARCH_DEFAULT);
176 } else
177 arch = module_sp->GetArchitecture();
Jason Molendaba813dc2012-11-04 03:20:05 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 ArchSpec curr_arch;
180 // First, try to find an exact match for the Arch of the Target.
181 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
182 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
183 arch.IsExactMatch(curr_arch))
184 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 // Failing an exact match, try to find a compatible Arch of the Target.
188 if (arch_idx >= m_header.nfat_arch) {
189 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
190 if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
191 arch.IsCompatibleMatch(curr_arch))
192 break;
193 }
194 }
195
196 if (arch_idx < m_header.nfat_arch) {
197 DataBufferSP data_sp;
198 lldb::offset_t data_offset = 0;
199 return ObjectFile::FindPlugin(
200 module_sp, file, m_offset + m_fat_archs[arch_idx].offset,
201 m_fat_archs[arch_idx].size, data_sp, data_offset);
202 }
203 }
204 return ObjectFileSP();
205}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206
207//------------------------------------------------------------------
208// PluginInterface protocol
209//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginName() {
211 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212}
213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214uint32_t ObjectContainerUniversalMachO::GetPluginVersion() { return 1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216size_t ObjectContainerUniversalMachO::GetModuleSpecifications(
217 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
218 lldb::offset_t data_offset, lldb::offset_t file_offset,
219 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
220 const size_t initial_count = specs.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 DataExtractor data;
223 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
Greg Claytonf4d6de62013-04-24 22:29:28 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) {
226 llvm::MachO::fat_header header;
227 std::vector<llvm::MachO::fat_arch> fat_archs;
228 if (ParseHeader(data, header, fat_archs)) {
229 for (const llvm::MachO::fat_arch &fat_arch : fat_archs) {
230 const lldb::offset_t slice_file_offset = fat_arch.offset + file_offset;
231 if (fat_arch.offset < file_size && file_size > slice_file_offset) {
232 ObjectFile::GetModuleSpecifications(
233 file, slice_file_offset, file_size - slice_file_offset, specs);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000234 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000236 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 }
238 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000239}