LLDB  mainline
ObjectContainerUniversalMachO.cpp
Go to the documentation of this file.
1 //===-- ObjectContainerUniversalMachO.cpp -----------------------*- C++ -*-===//
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"
11 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Symbol/ObjectFile.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Utility/ArchSpec.h"
17 #include "lldb/Utility/Stream.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 using namespace llvm::MachO;
22 
24  PluginManager::RegisterPlugin(GetPluginNameStatic(),
25  GetPluginDescriptionStatic(), CreateInstance,
26  GetModuleSpecifications);
27 }
28 
30  PluginManager::UnregisterPlugin(CreateInstance);
31 }
32 
34  static ConstString g_name("mach-o");
35  return g_name;
36 }
37 
39  return "Universal mach-o object container reader.";
40 }
41 
43  const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
44  lldb::offset_t data_offset, const FileSpec *file,
45  lldb::offset_t file_offset, lldb::offset_t length) {
46  // We get data when we aren't trying to look for cached container
47  // information, so only try and look for an architecture slice if we get data
48  if (data_sp) {
49  DataExtractor data;
50  data.SetData(data_sp, data_offset, length);
52  std::unique_ptr<ObjectContainerUniversalMachO> container_up(
53  new ObjectContainerUniversalMachO(module_sp, data_sp, data_offset,
54  file, file_offset, length));
55  if (container_up->ParseHeader()) {
56  return container_up.release();
57  }
58  }
59  }
60  return NULL;
61 }
62 
64  lldb::offset_t offset = 0;
65  uint32_t magic = data.GetU32(&offset);
66  return magic == FAT_MAGIC || magic == FAT_CIGAM;
67 }
68 
70  const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
71  lldb::offset_t data_offset, const FileSpec *file,
72  lldb::offset_t file_offset, lldb::offset_t length)
73  : ObjectContainer(module_sp, file, file_offset, length, data_sp,
74  data_offset),
75  m_header(), m_fat_archs() {
76  memset(&m_header, 0, sizeof(m_header));
77 }
78 
80 
82  bool success = ParseHeader(m_data, m_header, m_fat_archs);
83  // We no longer need any data, we parsed all we needed to parse and cached it
84  // in m_header and m_fat_archs
85  m_data.Clear();
86  return success;
87 }
88 
90  lldb_private::DataExtractor &data, llvm::MachO::fat_header &header,
91  std::vector<llvm::MachO::fat_arch> &fat_archs) {
92  bool success = false;
93  // Store the file offset for this universal file as we could have a universal
94  // .o file in a BSD archive, or be contained in another kind of object.
95  // Universal mach-o files always have their headers in big endian.
96  lldb::offset_t offset = 0;
98  header.magic = data.GetU32(&offset);
99  fat_archs.clear();
100 
101  if (header.magic == FAT_MAGIC) {
102 
103  data.SetAddressByteSize(4);
104 
105  header.nfat_arch = data.GetU32(&offset);
106 
107  // Now we should have enough data for all of the fat headers, so lets index
108  // them so we know how many architectures that this universal binary
109  // contains.
110  uint32_t arch_idx = 0;
111  for (arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) {
112  if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) {
113  fat_arch arch;
114  if (data.GetU32(&offset, &arch, sizeof(fat_arch) / sizeof(uint32_t)))
115  fat_archs.push_back(arch);
116  }
117  }
118  success = true;
119  } else {
120  memset(&header, 0, sizeof(header));
121  }
122  return success;
123 }
124 
126  s->Printf("%p: ", static_cast<const void *>(this));
127  s->Indent();
128  const size_t num_archs = GetNumArchitectures();
129  const size_t num_objects = GetNumObjects();
130  s->Printf("ObjectContainerUniversalMachO, num_archs = %zu, num_objects = %zu",
131  num_archs, num_objects);
132  uint32_t i;
133  ArchSpec arch;
134  s->IndentMore();
135  for (i = 0; i < num_archs; i++) {
136  s->Indent();
137  GetArchitectureAtIndex(i, arch);
138  s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
139  }
140  for (i = 0; i < num_objects; i++) {
141  s->Indent();
142  s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
143  }
144  s->IndentLess();
145  s->EOL();
146 }
147 
149  return m_header.nfat_arch;
150 }
151 
153  uint32_t idx, ArchSpec &arch) const {
154  if (idx < m_header.nfat_arch) {
155  arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].cputype,
156  m_fat_archs[idx].cpusubtype);
157  return true;
158  }
159  return false;
160 }
161 
162 ObjectFileSP
164  uint32_t arch_idx = 0;
165  ArchSpec arch;
166  // If the module hasn't specified an architecture yet, set it to the default
167  // architecture:
168  ModuleSP module_sp(GetModule());
169  if (module_sp) {
170  if (!module_sp->GetArchitecture().IsValid()) {
171  arch = Target::GetDefaultArchitecture();
172  if (!arch.IsValid())
174  } else
175  arch = module_sp->GetArchitecture();
176 
177  ArchSpec curr_arch;
178  // First, try to find an exact match for the Arch of the Target.
179  for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
180  if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
181  arch.IsExactMatch(curr_arch))
182  break;
183  }
184 
185  // Failing an exact match, try to find a compatible Arch of the Target.
186  if (arch_idx >= m_header.nfat_arch) {
187  for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
188  if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
189  arch.IsCompatibleMatch(curr_arch))
190  break;
191  }
192  }
193 
194  if (arch_idx < m_header.nfat_arch) {
195  DataBufferSP data_sp;
196  lldb::offset_t data_offset = 0;
197  return ObjectFile::FindPlugin(
198  module_sp, file, m_offset + m_fat_archs[arch_idx].offset,
199  m_fat_archs[arch_idx].size, data_sp, data_offset);
200  }
201  }
202  return ObjectFileSP();
203 }
204 
205 // PluginInterface protocol
207  return GetPluginNameStatic();
208 }
209 
211 
213  const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
214  lldb::offset_t data_offset, lldb::offset_t file_offset,
215  lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
216  const size_t initial_count = specs.GetSize();
217 
218  DataExtractor data;
219  data.SetData(data_sp, data_offset, data_sp->GetByteSize());
220 
222  llvm::MachO::fat_header header;
223  std::vector<llvm::MachO::fat_arch> fat_archs;
224  if (ParseHeader(data, header, fat_archs)) {
225  for (const llvm::MachO::fat_arch &fat_arch : fat_archs) {
226  const lldb::offset_t slice_file_offset = fat_arch.offset + file_offset;
227  if (fat_arch.offset < file_size && file_size > slice_file_offset) {
228  ObjectFile::GetModuleSpecifications(
229  file, slice_file_offset, file_size - slice_file_offset, specs);
230  }
231  }
232  }
233  }
234  return specs.GetSize() - initial_count;
235 }
bool IsExactMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring an exact cpu type match between them...
Definition: ArchSpec.cpp:977
bool ParseHeader() override
Attempts to parse the object header.
An data extractor class.
Definition: DataExtractor.h:47
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:797
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
A file utility class.
Definition: FileSpec.h:55
An architecture specification class.
Definition: ArchSpec.h:32
lldb::addr_t m_offset
The offset in bytes into the file, or the address in memory.
size_t GetNumArchitectures() const override
Get the number of architectures in this object file.
A plug-in interface definition class for object containers.
#define LLDB_ARCH_DEFAULT
CPU Type definitions.
Definition: lldb-defines.h:101
virtual const char * GetObjectNameAtIndex(uint32_t object_idx) const
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:329
void Dump(lldb_private::Stream *s) const override
Dump a description of this object to a Stream.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:27
std::vector< llvm::MachO::fat_arch > m_fat_archs
lldb_private::ConstString GetPluginName() override
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition: ArchSpec.cpp:591
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
uint64_t offset_t
Definition: lldb-types.h:87
void Clear()
Clears the object state.
static bool MagicBytesMatch(const lldb_private::DataExtractor &data)
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
static lldb_private::ConstString GetPluginNameStatic()
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)
bool GetArchitectureAtIndex(uint32_t cpu_idx, lldb_private::ArchSpec &arch) const override
Gets the architecture given an index.
void IndentLess(int amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:221
DataExtractor m_data
The data for this object file so things can be parsed lazily.
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
virtual size_t GetNumObjects() const
Get the number of objects within this object file (archives).
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override
Selects an architecture in an object file.
A uniqued constant string class.
Definition: ConstString.h:38
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)
Definition: SBAddress.h:15
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
void IndentMore(int amount=2)
Increment the current indentation level.
Definition: Stream.cpp:218
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:892
bool IsCompatibleMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring a compatible cpu type match between them...
Definition: ArchSpec.cpp:981
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.