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 9 #include "ObjectContainerUniversalMachO.h" 10 #include "lldb/Core/Module.h" 11 #include "lldb/Core/ModuleSpec.h" 12 #include "lldb/Core/PluginManager.h" 13 #include "lldb/Symbol/ObjectFile.h" 14 #include "lldb/Target/Target.h" 15 #include "lldb/Utility/ArchSpec.h" 16 #include "lldb/Utility/DataBuffer.h" 17 #include "lldb/Utility/Stream.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 using namespace llvm::MachO; 22 23 LLDB_PLUGIN_DEFINE_ADV(ObjectContainerUniversalMachO, 24 ObjectContainerMachOArchive) 25 26 void ObjectContainerUniversalMachO::Initialize() { 27 PluginManager::RegisterPlugin(GetPluginNameStatic(), 28 GetPluginDescriptionStatic(), CreateInstance, 29 GetModuleSpecifications); 30 } 31 32 void ObjectContainerUniversalMachO::Terminate() { 33 PluginManager::UnregisterPlugin(CreateInstance); 34 } 35 36 ObjectContainer *ObjectContainerUniversalMachO::CreateInstance( 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); 45 if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) { 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 57 bool ObjectContainerUniversalMachO::MagicBytesMatch(const DataExtractor &data) { 58 lldb::offset_t offset = 0; 59 uint32_t magic = data.GetU32(&offset); 60 return magic == FAT_MAGIC || magic == FAT_CIGAM; 61 } 62 63 ObjectContainerUniversalMachO::ObjectContainerUniversalMachO( 64 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, 65 lldb::offset_t data_offset, const FileSpec *file, 66 lldb::offset_t file_offset, lldb::offset_t length) 67 : ObjectContainer(module_sp, file, file_offset, length, data_sp, 68 data_offset), 69 m_header(), m_fat_archs() { 70 memset(&m_header, 0, sizeof(m_header)); 71 } 72 73 ObjectContainerUniversalMachO::~ObjectContainerUniversalMachO() = default; 74 75 bool ObjectContainerUniversalMachO::ParseHeader() { 76 bool success = ParseHeader(m_data, m_header, m_fat_archs); 77 // We no longer need any data, we parsed all we needed to parse and cached it 78 // in m_header and m_fat_archs 79 m_data.Clear(); 80 return success; 81 } 82 83 bool ObjectContainerUniversalMachO::ParseHeader( 84 lldb_private::DataExtractor &data, llvm::MachO::fat_header &header, 85 std::vector<llvm::MachO::fat_arch> &fat_archs) { 86 bool success = false; 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 // Universal mach-o files always have their headers in big endian. 90 lldb::offset_t offset = 0; 91 data.SetByteOrder(eByteOrderBig); 92 header.magic = data.GetU32(&offset); 93 fat_archs.clear(); 94 95 if (header.magic == FAT_MAGIC) { 96 97 data.SetAddressByteSize(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 uint32_t arch_idx = 0; 105 for (arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) { 106 if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) { 107 fat_arch arch; 108 if (data.GetU32(&offset, &arch, sizeof(fat_arch) / sizeof(uint32_t))) 109 fat_archs.push_back(arch); 110 } 111 } 112 success = true; 113 } else { 114 memset(&header, 0, sizeof(header)); 115 } 116 return success; 117 } 118 119 void ObjectContainerUniversalMachO::Dump(Stream *s) const { 120 s->Printf("%p: ", static_cast<const void *>(this)); 121 s->Indent(); 122 const size_t num_archs = GetNumArchitectures(); 123 const size_t num_objects = GetNumObjects(); 124 s->Printf("ObjectContainerUniversalMachO, num_archs = %zu, num_objects = %zu", 125 num_archs, num_objects); 126 uint32_t i; 127 ArchSpec arch; 128 s->IndentMore(); 129 for (i = 0; i < num_archs; i++) { 130 s->Indent(); 131 GetArchitectureAtIndex(i, arch); 132 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); 133 } 134 for (i = 0; i < num_objects; i++) { 135 s->Indent(); 136 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i)); 137 } 138 s->IndentLess(); 139 s->EOL(); 140 } 141 142 size_t ObjectContainerUniversalMachO::GetNumArchitectures() const { 143 return m_header.nfat_arch; 144 } 145 146 bool ObjectContainerUniversalMachO::GetArchitectureAtIndex( 147 uint32_t idx, ArchSpec &arch) const { 148 if (idx < m_header.nfat_arch) { 149 arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].cputype, 150 m_fat_archs[idx].cpusubtype); 151 return true; 152 } 153 return false; 154 } 155 156 ObjectFileSP 157 ObjectContainerUniversalMachO::GetObjectFile(const FileSpec *file) { 158 uint32_t arch_idx = 0; 159 ArchSpec arch; 160 // If the module hasn't specified an architecture yet, set it to the default 161 // architecture: 162 ModuleSP module_sp(GetModule()); 163 if (module_sp) { 164 if (!module_sp->GetArchitecture().IsValid()) { 165 arch = Target::GetDefaultArchitecture(); 166 if (!arch.IsValid()) 167 arch.SetTriple(LLDB_ARCH_DEFAULT); 168 } else 169 arch = module_sp->GetArchitecture(); 170 171 ArchSpec curr_arch; 172 // First, try to find an exact match for the Arch of the Target. 173 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) { 174 if (GetArchitectureAtIndex(arch_idx, curr_arch) && 175 arch.IsExactMatch(curr_arch)) 176 break; 177 } 178 179 // Failing an exact match, try to find a compatible Arch of the Target. 180 if (arch_idx >= m_header.nfat_arch) { 181 for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) { 182 if (GetArchitectureAtIndex(arch_idx, curr_arch) && 183 arch.IsCompatibleMatch(curr_arch)) 184 break; 185 } 186 } 187 188 if (arch_idx < m_header.nfat_arch) { 189 DataBufferSP data_sp; 190 lldb::offset_t data_offset = 0; 191 return ObjectFile::FindPlugin( 192 module_sp, file, m_offset + m_fat_archs[arch_idx].offset, 193 m_fat_archs[arch_idx].size, data_sp, data_offset); 194 } 195 } 196 return ObjectFileSP(); 197 } 198 199 size_t ObjectContainerUniversalMachO::GetModuleSpecifications( 200 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 201 lldb::offset_t data_offset, lldb::offset_t file_offset, 202 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { 203 const size_t initial_count = specs.GetSize(); 204 205 DataExtractor data; 206 data.SetData(data_sp, data_offset, data_sp->GetByteSize()); 207 208 if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) { 209 llvm::MachO::fat_header header; 210 std::vector<llvm::MachO::fat_arch> fat_archs; 211 if (ParseHeader(data, header, fat_archs)) { 212 for (const llvm::MachO::fat_arch &fat_arch : fat_archs) { 213 const lldb::offset_t slice_file_offset = fat_arch.offset + file_offset; 214 if (fat_arch.offset < file_size && file_size > slice_file_offset) { 215 ObjectFile::GetModuleSpecifications( 216 file, slice_file_offset, file_size - slice_file_offset, specs); 217 } 218 } 219 } 220 } 221 return specs.GetSize() - initial_count; 222 } 223