Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 12 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/PluginManager.h" |
| 14 | #include "lldb/Symbol/ObjectFile.h" |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 15 | #include "lldb/Target/Target.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/ArchSpec.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/DataBuffer.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 22 | using namespace llvm::MachO; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | void ObjectContainerUniversalMachO::Initialize() { |
| 25 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 26 | GetPluginDescriptionStatic(), CreateInstance, |
| 27 | GetModuleSpecifications); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | void ObjectContainerUniversalMachO::Terminate() { |
| 31 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginNameStatic() { |
| 35 | static ConstString g_name("mach-o"); |
| 36 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | const char *ObjectContainerUniversalMachO::GetPluginDescriptionStatic() { |
| 40 | return "Universal mach-o object container reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | ObjectContainer *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) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 47 | // We get data when we aren't trying to look for cached container |
| 48 | // information, so only try and look for an architecture slice if we get data |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | } |
| 61 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | bool 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | ObjectContainerUniversalMachO::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | ObjectContainerUniversalMachO::~ObjectContainerUniversalMachO() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | bool ObjectContainerUniversalMachO::ParseHeader() { |
| 83 | bool success = ParseHeader(m_data, m_header, m_fat_archs); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 84 | // We no longer need any data, we parsed all we needed to parse and cached it |
| 85 | // in m_header and m_fat_archs |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | m_data.Clear(); |
| 87 | return success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | bool 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 |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 95 | // .o file in a BSD archive, or be contained in another kind of object. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | // Universal mach-o files always have their headers in big endian. |
| 97 | lldb::offset_t offset = 0; |
| 98 | data.SetByteOrder(eByteOrderBig); |
| 99 | header.magic = data.GetU32(&offset); |
| 100 | fat_archs.clear(); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | if (header.magic == FAT_MAGIC) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | data.SetAddressByteSize(4); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | header.nfat_arch = data.GetU32(&offset); |
| 107 | |
| 108 | // Now we should have enough data for all of the fat headers, so lets index |
| 109 | // them so we know how many architectures that this universal binary |
| 110 | // contains. |
| 111 | uint32_t arch_idx = 0; |
| 112 | for (arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) { |
| 113 | if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) { |
| 114 | fat_arch arch; |
| 115 | if (data.GetU32(&offset, &arch, sizeof(fat_arch) / sizeof(uint32_t))) |
| 116 | fat_archs.push_back(arch); |
| 117 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | success = true; |
| 120 | } else { |
| 121 | memset(&header, 0, sizeof(header)); |
| 122 | } |
| 123 | return success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | void ObjectContainerUniversalMachO::Dump(Stream *s) const { |
| 127 | s->Printf("%p: ", static_cast<const void *>(this)); |
| 128 | s->Indent(); |
| 129 | const size_t num_archs = GetNumArchitectures(); |
| 130 | const size_t num_objects = GetNumObjects(); |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 131 | s->Printf("ObjectContainerUniversalMachO, num_archs = %zu, num_objects = %zu", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | num_archs, num_objects); |
| 133 | uint32_t i; |
| 134 | ArchSpec arch; |
| 135 | s->IndentMore(); |
| 136 | for (i = 0; i < num_archs; i++) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | s->Indent(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | GetArchitectureAtIndex(i, arch); |
| 139 | s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); |
| 140 | } |
| 141 | for (i = 0; i < num_objects; i++) { |
| 142 | s->Indent(); |
| 143 | s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i)); |
| 144 | } |
| 145 | s->IndentLess(); |
| 146 | s->EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | size_t ObjectContainerUniversalMachO::GetNumArchitectures() const { |
| 150 | return m_header.nfat_arch; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | bool ObjectContainerUniversalMachO::GetArchitectureAtIndex( |
| 154 | uint32_t idx, ArchSpec &arch) const { |
| 155 | if (idx < m_header.nfat_arch) { |
| 156 | arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].cputype, |
| 157 | m_fat_archs[idx].cpusubtype); |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 163 | ObjectFileSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | ObjectContainerUniversalMachO::GetObjectFile(const FileSpec *file) { |
| 165 | uint32_t arch_idx = 0; |
| 166 | ArchSpec arch; |
| 167 | // If the module hasn't specified an architecture yet, set it to the default |
| 168 | // architecture: |
| 169 | ModuleSP module_sp(GetModule()); |
| 170 | if (module_sp) { |
| 171 | if (!module_sp->GetArchitecture().IsValid()) { |
| 172 | arch = Target::GetDefaultArchitecture(); |
| 173 | if (!arch.IsValid()) |
| 174 | arch.SetTriple(LLDB_ARCH_DEFAULT); |
| 175 | } else |
| 176 | arch = module_sp->GetArchitecture(); |
Jason Molenda | ba813dc | 2012-11-04 03:20:05 +0000 | [diff] [blame] | 177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | ArchSpec curr_arch; |
| 179 | // First, try to find an exact match for the Arch of the Target. |
| 180 | for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) { |
| 181 | if (GetArchitectureAtIndex(arch_idx, curr_arch) && |
| 182 | arch.IsExactMatch(curr_arch)) |
| 183 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | // Failing an exact match, try to find a compatible Arch of the Target. |
| 187 | if (arch_idx >= m_header.nfat_arch) { |
| 188 | for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) { |
| 189 | if (GetArchitectureAtIndex(arch_idx, curr_arch) && |
| 190 | arch.IsCompatibleMatch(curr_arch)) |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (arch_idx < m_header.nfat_arch) { |
| 196 | DataBufferSP data_sp; |
| 197 | lldb::offset_t data_offset = 0; |
| 198 | return ObjectFile::FindPlugin( |
| 199 | module_sp, file, m_offset + m_fat_archs[arch_idx].offset, |
| 200 | m_fat_archs[arch_idx].size, data_sp, data_offset); |
| 201 | } |
| 202 | } |
| 203 | return ObjectFileSP(); |
| 204 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | |
| 206 | //------------------------------------------------------------------ |
| 207 | // PluginInterface protocol |
| 208 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginName() { |
| 210 | return GetPluginNameStatic(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | uint32_t ObjectContainerUniversalMachO::GetPluginVersion() { return 1; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | size_t ObjectContainerUniversalMachO::GetModuleSpecifications( |
| 216 | const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, |
| 217 | lldb::offset_t data_offset, lldb::offset_t file_offset, |
| 218 | lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { |
| 219 | const size_t initial_count = specs.GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | DataExtractor data; |
| 222 | data.SetData(data_sp, data_offset, data_sp->GetByteSize()); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) { |
| 225 | llvm::MachO::fat_header header; |
| 226 | std::vector<llvm::MachO::fat_arch> fat_archs; |
| 227 | if (ParseHeader(data, header, fat_archs)) { |
| 228 | for (const llvm::MachO::fat_arch &fat_arch : fat_archs) { |
| 229 | const lldb::offset_t slice_file_offset = fat_arch.offset + file_offset; |
| 230 | if (fat_arch.offset < file_size && file_size > slice_file_offset) { |
| 231 | ObjectFile::GetModuleSpecifications( |
| 232 | file, slice_file_offset, file_size - slice_file_offset, specs); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 233 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 235 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | } |
| 237 | return specs.GetSize() - initial_count; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 238 | } |