LLDB  mainline
ObjectContainerUniversalMachO.cpp
Go to the documentation of this file.
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 
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  ObjectContainerMachOArchive)
25 
27  PluginManager::RegisterPlugin(GetPluginNameStatic(),
28  GetPluginDescriptionStatic(), CreateInstance,
29  GetModuleSpecifications);
30 }
31 
33  PluginManager::UnregisterPlugin(CreateInstance);
34 }
35 
37  static ConstString g_name("mach-o");
38  return g_name;
39 }
40 
42  return "Universal mach-o object container reader.";
43 }
44 
46  const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
47  lldb::offset_t data_offset, const FileSpec *file,
48  lldb::offset_t file_offset, lldb::offset_t length) {
49  // We get data when we aren't trying to look for cached container
50  // information, so only try and look for an architecture slice if we get data
51  if (data_sp) {
52  DataExtractor data;
53  data.SetData(data_sp, data_offset, length);
55  std::unique_ptr<ObjectContainerUniversalMachO> container_up(
56  new ObjectContainerUniversalMachO(module_sp, data_sp, data_offset,
57  file, file_offset, length));
58  if (container_up->ParseHeader()) {
59  return container_up.release();
60  }
61  }
62  }
63  return nullptr;
64 }
65 
67  lldb::offset_t offset = 0;
68  uint32_t magic = data.GetU32(&offset);
69  return magic == FAT_MAGIC || magic == FAT_CIGAM;
70 }
71 
73  const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
74  lldb::offset_t data_offset, const FileSpec *file,
75  lldb::offset_t file_offset, lldb::offset_t length)
76  : ObjectContainer(module_sp, file, file_offset, length, data_sp,
77  data_offset),
78  m_header(), m_fat_archs() {
79  memset(&m_header, 0, sizeof(m_header));
80 }
81 
83 
85  bool success = ParseHeader(m_data, m_header, m_fat_archs);
86  // We no longer need any data, we parsed all we needed to parse and cached it
87  // in m_header and m_fat_archs
88  m_data.Clear();
89  return success;
90 }
91 
93  lldb_private::DataExtractor &data, llvm::MachO::fat_header &header,
94  std::vector<llvm::MachO::fat_arch> &fat_archs) {
95  bool success = false;
96  // Store the file offset for this universal file as we could have a universal
97  // .o file in a BSD archive, or be contained in another kind of object.
98  // Universal mach-o files always have their headers in big endian.
99  lldb::offset_t offset = 0;
101  header.magic = data.GetU32(&offset);
102  fat_archs.clear();
103 
104  if (header.magic == FAT_MAGIC) {
105 
106  data.SetAddressByteSize(4);
107 
108  header.nfat_arch = data.GetU32(&offset);
109 
110  // Now we should have enough data for all of the fat headers, so lets index
111  // them so we know how many architectures that this universal binary
112  // contains.
113  uint32_t arch_idx = 0;
114  for (arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) {
115  if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) {
116  fat_arch arch;
117  if (data.GetU32(&offset, &arch, sizeof(fat_arch) / sizeof(uint32_t)))
118  fat_archs.push_back(arch);
119  }
120  }
121  success = true;
122  } else {
123  memset(&header, 0, sizeof(header));
124  }
125  return success;
126 }
127 
129  s->Printf("%p: ", static_cast<const void *>(this));
130  s->Indent();
131  const size_t num_archs = GetNumArchitectures();
132  const size_t num_objects = GetNumObjects();
133  s->Printf("ObjectContainerUniversalMachO, num_archs = %zu, num_objects = %zu",
134  num_archs, num_objects);
135  uint32_t i;
136  ArchSpec arch;
137  s->IndentMore();
138  for (i = 0; i < num_archs; i++) {
139  s->Indent();
140  GetArchitectureAtIndex(i, arch);
141  s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
142  }
143  for (i = 0; i < num_objects; i++) {
144  s->Indent();
145  s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
146  }
147  s->IndentLess();
148  s->EOL();
149 }
150 
152  return m_header.nfat_arch;
153 }
154 
156  uint32_t idx, ArchSpec &arch) const {
157  if (idx < m_header.nfat_arch) {
158  arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].cputype,
159  m_fat_archs[idx].cpusubtype);
160  return true;
161  }
162  return false;
163 }
164 
165 ObjectFileSP
167  uint32_t arch_idx = 0;
168  ArchSpec arch;
169  // If the module hasn't specified an architecture yet, set it to the default
170  // architecture:
171  ModuleSP module_sp(GetModule());
172  if (module_sp) {
173  if (!module_sp->GetArchitecture().IsValid()) {
174  arch = Target::GetDefaultArchitecture();
175  if (!arch.IsValid())
177  } else
178  arch = module_sp->GetArchitecture();
179 
180  ArchSpec curr_arch;
181  // First, try to find an exact match for the Arch of the Target.
182  for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
183  if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
184  arch.IsExactMatch(curr_arch))
185  break;
186  }
187 
188  // Failing an exact match, try to find a compatible Arch of the Target.
189  if (arch_idx >= m_header.nfat_arch) {
190  for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) {
191  if (GetArchitectureAtIndex(arch_idx, curr_arch) &&
192  arch.IsCompatibleMatch(curr_arch))
193  break;
194  }
195  }
196 
197  if (arch_idx < m_header.nfat_arch) {
198  DataBufferSP data_sp;
199  lldb::offset_t data_offset = 0;
200  return ObjectFile::FindPlugin(
201  module_sp, file, m_offset + m_fat_archs[arch_idx].offset,
202  m_fat_archs[arch_idx].size, data_sp, data_offset);
203  }
204  }
205  return ObjectFileSP();
206 }
207 
208 // PluginInterface protocol
210  return GetPluginNameStatic();
211 }
212 
214 
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();
220 
221  DataExtractor data;
222  data.SetData(data_sp, data_offset, data_sp->GetByteSize());
223 
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);
233  }
234  }
235  }
236  }
237  return specs.GetSize() - initial_count;
238 }
bool IsExactMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring an exact cpu type match between them...
Definition: ArchSpec.cpp:968
bool ParseHeader() override
Attempts to parse the object header.
An data extractor class.
Definition: DataExtractor.h:46
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:167
A class that represents a running process on the host machine.
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:786
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:170
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
A file utility class.
Definition: FileSpec.h:56
An architecture specification class.
Definition: ArchSpec.h:33
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:332
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:24
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:585
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:129
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.
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:40
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
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:881
bool IsCompatibleMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring a compatible cpu type match between them...
Definition: ArchSpec.cpp:972
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
LLDB_PLUGIN_DEFINE_ADV(ObjectContainerUniversalMachO, ObjectContainerMachOArchive) void ObjectContainerUniversalMachO