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(ObjectContainerUniversalMachO) 24 25 void ObjectContainerUniversalMachO::Initialize() { 26 PluginManager::RegisterPlugin(GetPluginNameStatic(), 27 GetPluginDescriptionStatic(), CreateInstance, 28 GetModuleSpecifications); 29 } 30 31 void ObjectContainerUniversalMachO::Terminate() { 32 PluginManager::UnregisterPlugin(CreateInstance); 33 } 34 35 lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginNameStatic() { 36 static ConstString g_name("mach-o"); 37 return g_name; 38 } 39 40 const char *ObjectContainerUniversalMachO::GetPluginDescriptionStatic() { 41 return "Universal mach-o object container reader."; 42 } 43 44 ObjectContainer *ObjectContainerUniversalMachO::CreateInstance( 45 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, 46 lldb::offset_t data_offset, const FileSpec *file, 47 lldb::offset_t file_offset, lldb::offset_t length) { 48 // We get data when we aren't trying to look for cached container 49 // information, so only try and look for an architecture slice if we get data 50 if (data_sp) { 51 DataExtractor data; 52 data.SetData(data_sp, data_offset, length); 53 if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) { 54 std::unique_ptr<ObjectContainerUniversalMachO> container_up( 55 new ObjectContainerUniversalMachO(module_sp, data_sp, data_offset, 56 file, file_offset, length)); 57 if (container_up->ParseHeader()) { 58 return container_up.release(); 59 } 60 } 61 } 62 return nullptr; 63 } 64 65 bool ObjectContainerUniversalMachO::MagicBytesMatch(const DataExtractor &data) { 66 lldb::offset_t offset = 0; 67 uint32_t magic = data.GetU32(&offset); 68 return magic == FAT_MAGIC || magic == FAT_CIGAM; 69 } 70 71 ObjectContainerUniversalMachO::ObjectContainerUniversalMachO( 72 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, 73 lldb::offset_t data_offset, const FileSpec *file, 74 lldb::offset_t file_offset, lldb::offset_t length) 75 : ObjectContainer(module_sp, file, file_offset, length, data_sp, 76 data_offset), 77 m_header(), m_fat_archs() { 78 memset(&m_header, 0, sizeof(m_header)); 79 } 80 81 ObjectContainerUniversalMachO::~ObjectContainerUniversalMachO() {} 82 83 bool ObjectContainerUniversalMachO::ParseHeader() { 84 bool success = ParseHeader(m_data, m_header, m_fat_archs); 85 // We no longer need any data, we parsed all we needed to parse and cached it 86 // in m_header and m_fat_archs 87 m_data.Clear(); 88 return success; 89 } 90 91 bool ObjectContainerUniversalMachO::ParseHeader( 92 lldb_private::DataExtractor &data, llvm::MachO::fat_header &header, 93 std::vector<llvm::MachO::fat_arch> &fat_archs) { 94 bool success = false; 95 // Store the file offset for this universal file as we could have a universal 96 // .o file 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(); 102 103 if (header.magic == FAT_MAGIC) { 104 105 data.SetAddressByteSize(4); 106 107 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 } 119 } 120 success = true; 121 } else { 122 memset(&header, 0, sizeof(header)); 123 } 124 return success; 125 } 126 127 void 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(); 132 s->Printf("ObjectContainerUniversalMachO, num_archs = %zu, num_objects = %zu", 133 num_archs, num_objects); 134 uint32_t i; 135 ArchSpec arch; 136 s->IndentMore(); 137 for (i = 0; i < num_archs; i++) { 138 s->Indent(); 139 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(); 148 } 149 150 size_t ObjectContainerUniversalMachO::GetNumArchitectures() const { 151 return m_header.nfat_arch; 152 } 153 154 bool 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; 162 } 163 164 ObjectFileSP 165 ObjectContainerUniversalMachO::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(); 178 179 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; 185 } 186 187 // 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 } 206 207 // PluginInterface protocol 208 lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginName() { 209 return GetPluginNameStatic(); 210 } 211 212 uint32_t ObjectContainerUniversalMachO::GetPluginVersion() { return 1; } 213 214 size_t ObjectContainerUniversalMachO::GetModuleSpecifications( 215 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 216 lldb::offset_t data_offset, lldb::offset_t file_offset, 217 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { 218 const size_t initial_count = specs.GetSize(); 219 220 DataExtractor data; 221 data.SetData(data_sp, data_offset, data_sp->GetByteSize()); 222 223 if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) { 224 llvm::MachO::fat_header header; 225 std::vector<llvm::MachO::fat_arch> fat_archs; 226 if (ParseHeader(data, header, fat_archs)) { 227 for (const llvm::MachO::fat_arch &fat_arch : fat_archs) { 228 const lldb::offset_t slice_file_offset = fat_arch.offset + file_offset; 229 if (fat_arch.offset < file_size && file_size > slice_file_offset) { 230 ObjectFile::GetModuleSpecifications( 231 file, slice_file_offset, file_size - slice_file_offset, specs); 232 } 233 } 234 } 235 } 236 return specs.GetSize() - initial_count; 237 } 238