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  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);
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 
58  lldb::offset_t offset = 0;
59  uint32_t magic = data.GetU32(&offset);
60  return magic == FAT_MAGIC || magic == FAT_CIGAM;
61 }
62 
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 
74 
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 
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;
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 
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 
143  return m_header.nfat_arch;
144 }
145 
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
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())
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 
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 
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 }
ObjectContainerUniversalMachO::Initialize
static void Initialize()
lldb_private::Stream::IndentLess
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:171
ObjectContainerUniversalMachO::m_header
llvm::MachO::fat_header m_header
Definition: ObjectContainerUniversalMachO.h:67
lldb_private::ArchSpec
Definition: ArchSpec.h:33
ModuleSpec.h
lldb_private::ModuleSpecList
Definition: ModuleSpec.h:275
LLDB_ARCH_DEFAULT
#define LLDB_ARCH_DEFAULT
CPU Type definitions.
Definition: lldb-defines.h:91
lldb_private::ObjectContainer
Definition: ObjectContainer.h:30
ObjectContainerUniversalMachO::~ObjectContainerUniversalMachO
~ObjectContainerUniversalMachO() override
Module.h
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::ObjectContainer::m_data
DataExtractor m_data
The data for this object file so things can be parsed lazily.
Definition: ObjectContainer.h:167
ObjectContainerUniversalMachO::GetObjectFile
lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override
Selects an architecture in an object file.
Definition: ObjectContainerUniversalMachO.cpp:157
lldb_private::Stream
Definition: Stream.h:28
lldb_private::DataExtractor::SetData
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
Definition: DataExtractor.cpp:223
LLDB_PLUGIN_DEFINE_ADV
LLDB_PLUGIN_DEFINE_ADV(ObjectContainerUniversalMachO, ObjectContainerMachOArchive) void ObjectContainerUniversalMachO
Definition: ObjectContainerUniversalMachO.cpp:23
ObjectContainerUniversalMachO
Definition: ObjectContainerUniversalMachO.h:16
lldb_private::eArchTypeMachO
@ eArchTypeMachO
Definition: lldb-private-enumerations.h:63
lldb_private::ArchSpec::IsCompatibleMatch
bool IsCompatibleMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring a compatible cpu type match between them.
Definition: ArchSpec.cpp:935
ObjectContainerUniversalMachO::ObjectContainerUniversalMachO
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)
Definition: ObjectContainerUniversalMachO.cpp:63
Target.h
lldb_private::DataExtractor::SetByteOrder
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
Definition: DataExtractor.h:931
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::Stream::Indent
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:130
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::ObjectContainer::m_offset
lldb::addr_t m_offset
The offset in bytes into the file, or the address in memory.
Definition: ObjectContainer.h:164
lldb_private::ArchSpec::SetTriple
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:740
lldb_private::DataExtractor::SetAddressByteSize
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
Definition: DataExtractor.h:845
lldb_private::ObjectContainer::GetObjectNameAtIndex
virtual const char * GetObjectNameAtIndex(uint32_t object_idx) const
Definition: ObjectContainer.h:155
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:353
ObjectContainerUniversalMachO::Terminate
static void Terminate()
Definition: ObjectContainerUniversalMachO.cpp:32
ObjectContainerUniversalMachO::MagicBytesMatch
static bool MagicBytesMatch(const lldb_private::DataExtractor &data)
Definition: ObjectContainerUniversalMachO.cpp:57
ObjectFile.h
lldb_private::ArchSpec::SetArchitecture
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:844
lldb_private::ArchSpec::GetArchitectureName
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition: ArchSpec.cpp:539
uint32_t
lldb_private::Stream::IndentMore
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:168
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
ObjectContainerUniversalMachO::ParseHeader
bool ParseHeader() override
Attempts to parse the object header.
Definition: ObjectContainerUniversalMachO.cpp:75
ObjectContainerUniversalMachO::CreateInstance
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: ObjectContainerUniversalMachO.cpp:36
ArchSpec.h
lldb_private::ModuleChild::GetModule
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
ObjectContainerUniversalMachO.h
lldb_private::DataExtractor::GetU32
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
Definition: DataExtractor.cpp:425
PluginManager.h
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
ObjectContainerUniversalMachO::GetModuleSpecifications
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)
Definition: ObjectContainerUniversalMachO.cpp:199
ObjectContainerUniversalMachO::GetNumArchitectures
size_t GetNumArchitectures() const override
Get the number of architectures in this object file.
Definition: ObjectContainerUniversalMachO.cpp:142
lldb::eByteOrderBig
@ eByteOrderBig
Definition: lldb-enumerations.h:140
lldb_private::DataExtractor::Clear
void Clear()
Clears the object state.
Definition: DataExtractor.cpp:192
lldb_private::ObjectContainer::GetNumObjects
virtual size_t GetNumObjects() const
Get the number of objects within this object file (archives).
Definition: ObjectContainer.h:102
lldb_private::ModuleSpecList::GetSize
size_t GetSize() const
Definition: ModuleSpec.h:298
Stream.h
DataBuffer.h
lldb_private::DataExtractor::ValidOffsetForDataOfSize
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
Definition: DataExtractor.h:965
lldb
Definition: SBAddress.h:15
ObjectContainerUniversalMachO::GetArchitectureAtIndex
bool GetArchitectureAtIndex(uint32_t cpu_idx, lldb_private::ArchSpec &arch) const override
Gets the architecture given an index.
Definition: ObjectContainerUniversalMachO.cpp:146
lldb_private::ArchSpec::IsExactMatch
bool IsExactMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring an exact cpu type match between them.
Definition: ArchSpec.cpp:931
ObjectContainerUniversalMachO::Dump
void Dump(lldb_private::Stream *s) const override
Dump a description of this object to a Stream.
Definition: ObjectContainerUniversalMachO.cpp:119
ObjectContainerUniversalMachO::m_fat_archs
std::vector< llvm::MachO::fat_arch > m_fat_archs
Definition: ObjectContainerUniversalMachO.h:68