aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/lldb/source/Plugins/ObjectContainer
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/lldb/source/Plugins/ObjectContainer')
-rw-r--r--contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp519
-rw-r--r--contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h182
-rw-r--r--contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.cpp299
-rw-r--r--contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.h94
4 files changed, 1094 insertions, 0 deletions
diff --git a/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
new file mode 100644
index 000000000000..7aa5b8d81890
--- /dev/null
+++ b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
@@ -0,0 +1,519 @@
+//===-- ObjectContainerBSDArchive.cpp -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "ObjectContainerBSDArchive.h"
+
+#if defined(_WIN32) || defined(__ANDROID__)
+// Defines from ar, missing on Windows
+#define SARMAG 8
+#define ARFMAG "`\n"
+
+typedef struct ar_hdr {
+ char ar_name[16];
+ char ar_date[12];
+ char ar_uid[6], ar_gid[6];
+ char ar_mode[8];
+ char ar_size[10];
+ char ar_fmag[2];
+} ar_hdr;
+#else
+#include <ar.h>
+#endif
+
+#include "lldb/Core/Module.h"
+#include "lldb/Core/ModuleSpec.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Symbol/ObjectFile.h"
+#include "lldb/Utility/ArchSpec.h"
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Stream.h"
+#include "lldb/Utility/Timer.h"
+
+#include "llvm/Object/Archive.h"
+#include "llvm/Support/MemoryBuffer.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+using namespace llvm::object;
+
+LLDB_PLUGIN_DEFINE(ObjectContainerBSDArchive)
+
+ObjectContainerBSDArchive::Object::Object() : ar_name() {}
+
+void ObjectContainerBSDArchive::Object::Clear() {
+ ar_name.Clear();
+ modification_time = 0;
+ size = 0;
+ file_offset = 0;
+ file_size = 0;
+}
+
+void ObjectContainerBSDArchive::Object::Dump() const {
+ printf("name = \"%s\"\n", ar_name.GetCString());
+ printf("mtime = 0x%8.8" PRIx32 "\n", modification_time);
+ printf("size = 0x%8.8" PRIx32 " (%" PRIu32 ")\n", size, size);
+ printf("file_offset = 0x%16.16" PRIx64 " (%" PRIu64 ")\n", file_offset,
+ file_offset);
+ printf("file_size = 0x%16.16" PRIx64 " (%" PRIu64 ")\n\n", file_size,
+ file_size);
+}
+
+ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
+ const llvm::sys::TimePoint<> &time,
+ lldb::offset_t file_offset,
+ lldb_private::DataExtractor &data,
+ ArchiveType archive_type)
+ : m_arch(arch), m_modification_time(time), m_file_offset(file_offset),
+ m_objects(), m_data(data), m_archive_type(archive_type) {}
+
+Log *l = GetLog(LLDBLog::Object);
+ObjectContainerBSDArchive::Archive::~Archive() = default;
+
+size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
+ DataExtractor &data = m_data;
+
+ std::unique_ptr<llvm::MemoryBuffer> mem_buffer =
+ llvm::MemoryBuffer::getMemBuffer(
+ llvm::StringRef((const char *)data.GetDataStart(),
+ data.GetByteSize()),
+ llvm::StringRef(),
+ /*RequiresNullTerminator=*/false);
+
+ auto exp_ar = llvm::object::Archive::create(mem_buffer->getMemBufferRef());
+ if (!exp_ar) {
+ LLDB_LOG_ERROR(l, exp_ar.takeError(), "failed to create archive: {0}");
+ return 0;
+ }
+ auto llvm_archive = std::move(exp_ar.get());
+
+ llvm::Error iter_err = llvm::Error::success();
+ Object obj;
+ for (const auto &child: llvm_archive->children(iter_err)) {
+ obj.Clear();
+ auto exp_name = child.getName();
+ if (exp_name) {
+ obj.ar_name = ConstString(exp_name.get());
+ } else {
+ LLDB_LOG_ERROR(l, exp_name.takeError(),
+ "failed to get archive object name: {0}");
+ continue;
+ }
+
+ auto exp_mtime = child.getLastModified();
+ if (exp_mtime) {
+ obj.modification_time =
+ std::chrono::duration_cast<std::chrono::seconds>(
+ std::chrono::time_point_cast<std::chrono::seconds>(
+ exp_mtime.get()).time_since_epoch()).count();
+ } else {
+ LLDB_LOG_ERROR(l, exp_mtime.takeError(),
+ "failed to get archive object time: {0}");
+ continue;
+ }
+
+ auto exp_size = child.getRawSize();
+ if (exp_size) {
+ obj.size = exp_size.get();
+ } else {
+ LLDB_LOG_ERROR(l, exp_size.takeError(),
+ "failed to get archive object size: {0}");
+ continue;
+ }
+
+ obj.file_offset = child.getDataOffset();
+
+ auto exp_file_size = child.getSize();
+ if (exp_file_size) {
+ obj.file_size = exp_file_size.get();
+ } else {
+ LLDB_LOG_ERROR(l, exp_file_size.takeError(),
+ "failed to get archive object file size: {0}");
+ continue;
+ }
+ m_object_name_to_index_map.Append(obj.ar_name, m_objects.size());
+ m_objects.push_back(obj);
+ }
+ if (iter_err) {
+ LLDB_LOG_ERROR(l, std::move(iter_err),
+ "failed to iterate over archive objects: {0}");
+ }
+ // Now sort all of the object name pointers
+ m_object_name_to_index_map.Sort();
+ return m_objects.size();
+}
+
+ObjectContainerBSDArchive::Object *
+ObjectContainerBSDArchive::Archive::FindObject(
+ ConstString object_name, const llvm::sys::TimePoint<> &object_mod_time) {
+ const ObjectNameToIndexMap::Entry *match =
+ m_object_name_to_index_map.FindFirstValueForName(object_name);
+ if (!match)
+ return nullptr;
+ if (object_mod_time == llvm::sys::TimePoint<>())
+ return &m_objects[match->value];
+
+ const uint64_t object_modification_date = llvm::sys::toTimeT(object_mod_time);
+ if (m_objects[match->value].modification_time == object_modification_date)
+ return &m_objects[match->value];
+
+ const ObjectNameToIndexMap::Entry *next_match =
+ m_object_name_to_index_map.FindNextValueForName(match);
+ while (next_match) {
+ if (m_objects[next_match->value].modification_time ==
+ object_modification_date)
+ return &m_objects[next_match->value];
+ next_match = m_object_name_to_index_map.FindNextValueForName(next_match);
+ }
+
+ return nullptr;
+}
+
+ObjectContainerBSDArchive::Archive::shared_ptr
+ObjectContainerBSDArchive::Archive::FindCachedArchive(
+ const FileSpec &file, const ArchSpec &arch,
+ const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) {
+ std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex());
+ shared_ptr archive_sp;
+ Archive::Map &archive_map = Archive::GetArchiveCache();
+ Archive::Map::iterator pos = archive_map.find(file);
+ // Don't cache a value for "archive_map.end()" below since we might delete an
+ // archive entry...
+ while (pos != archive_map.end() && pos->first == file) {
+ bool match = true;
+ if (arch.IsValid() &&
+ !pos->second->GetArchitecture().IsCompatibleMatch(arch))
+ match = false;
+ else if (file_offset != LLDB_INVALID_OFFSET &&
+ pos->second->GetFileOffset() != file_offset)
+ match = false;
+ if (match) {
+ if (pos->second->GetModificationTime() == time) {
+ return pos->second;
+ } else {
+ // We have a file at the same path with the same architecture whose
+ // modification time doesn't match. It doesn't make sense for us to
+ // continue to use this BSD archive since we cache only the object info
+ // which consists of file time info and also the file offset and file
+ // size of any contained objects. Since this information is now out of
+ // date, we won't get the correct information if we go and extract the
+ // file data, so we should remove the old and outdated entry.
+ archive_map.erase(pos);
+ pos = archive_map.find(file);
+ continue; // Continue to next iteration so we don't increment pos
+ // below...
+ }
+ }
+ ++pos;
+ }
+ return archive_sp;
+}
+
+ObjectContainerBSDArchive::Archive::shared_ptr
+ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
+ const FileSpec &file, const ArchSpec &arch,
+ const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
+ DataExtractor &data, ArchiveType archive_type) {
+ shared_ptr archive_sp(
+ new Archive(arch, time, file_offset, data, archive_type));
+ if (archive_sp) {
+ const size_t num_objects = archive_sp->ParseObjects();
+ if (num_objects > 0) {
+ std::lock_guard<std::recursive_mutex> guard(
+ Archive::GetArchiveCacheMutex());
+ Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
+ } else {
+ archive_sp.reset();
+ }
+ }
+ return archive_sp;
+}
+
+ObjectContainerBSDArchive::Archive::Map &
+ObjectContainerBSDArchive::Archive::GetArchiveCache() {
+ static Archive::Map g_archive_map;
+ return g_archive_map;
+}
+
+std::recursive_mutex &
+ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
+ static std::recursive_mutex g_archive_map_mutex;
+ return g_archive_map_mutex;
+}
+
+void ObjectContainerBSDArchive::Initialize() {
+ PluginManager::RegisterPlugin(GetPluginNameStatic(),
+ GetPluginDescriptionStatic(), CreateInstance,
+ GetModuleSpecifications);
+}
+
+void ObjectContainerBSDArchive::Terminate() {
+ PluginManager::UnregisterPlugin(CreateInstance);
+}
+
+ObjectContainer *ObjectContainerBSDArchive::CreateInstance(
+ const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
+ lldb::offset_t data_offset, const FileSpec *file,
+ lldb::offset_t file_offset, lldb::offset_t length) {
+ ConstString object_name(module_sp->GetObjectName());
+ if (!object_name)
+ return nullptr;
+
+ if (data_sp) {
+ // We have data, which means this is the first 512 bytes of the file Check
+ // to see if the magic bytes match and if they do, read the entire table of
+ // contents for the archive and cache it
+ DataExtractor data;
+ data.SetData(data_sp, data_offset, length);
+ ArchiveType archive_type = ObjectContainerBSDArchive::MagicBytesMatch(data);
+ if (file && data_sp && archive_type != ArchiveType::Invalid) {
+ LLDB_SCOPED_TIMERF(
+ "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
+ "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
+ module_sp->GetFileSpec().GetPath().c_str(),
+ static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
+ static_cast<uint64_t>(length));
+
+ // Map the entire .a file to be sure that we don't lose any data if the
+ // file gets updated by a new build while this .a file is being used for
+ // debugging
+ DataBufferSP archive_data_sp =
+ FileSystem::Instance().CreateDataBuffer(*file, length, file_offset);
+ if (!archive_data_sp)
+ return nullptr;
+
+ lldb::offset_t archive_data_offset = 0;
+
+ Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
+ *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
+ file_offset));
+ std::unique_ptr<ObjectContainerBSDArchive> container_up(
+ new ObjectContainerBSDArchive(module_sp, archive_data_sp,
+ archive_data_offset, file, file_offset,
+ length, archive_type));
+
+ if (container_up) {
+ if (archive_sp) {
+ // We already have this archive in our cache, use it
+ container_up->SetArchive(archive_sp);
+ return container_up.release();
+ } else if (container_up->ParseHeader())
+ return container_up.release();
+ }
+ }
+ } else {
+ // No data, just check for a cached archive
+ Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
+ *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
+ file_offset));
+ if (archive_sp) {
+ std::unique_ptr<ObjectContainerBSDArchive> container_up(
+ new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
+ file_offset, length,
+ archive_sp->GetArchiveType()));
+
+ if (container_up) {
+ // We already have this archive in our cache, use it
+ container_up->SetArchive(archive_sp);
+ return container_up.release();
+ }
+ }
+ }
+ return nullptr;
+}
+
+ArchiveType
+ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
+ uint32_t offset = 0;
+ const char *armag = (const char *)data.PeekData(offset,
+ sizeof(ar_hdr) + SARMAG);
+ if (armag == nullptr)
+ return ArchiveType::Invalid;
+ ArchiveType result = ArchiveType::Invalid;
+ if (strncmp(armag, ArchiveMagic, SARMAG) == 0)
+ result = ArchiveType::Archive;
+ else if (strncmp(armag, ThinArchiveMagic, SARMAG) == 0)
+ result = ArchiveType::ThinArchive;
+ else
+ return ArchiveType::Invalid;
+
+ armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
+ if (strncmp(armag, ARFMAG, 2) == 0)
+ return result;
+ return ArchiveType::Invalid;
+}
+
+ObjectContainerBSDArchive::ObjectContainerBSDArchive(
+ const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
+ lldb::offset_t data_offset, const lldb_private::FileSpec *file,
+ lldb::offset_t file_offset, lldb::offset_t size, ArchiveType archive_type)
+ : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
+ m_archive_sp() {
+ m_archive_type = archive_type;
+}
+
+void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
+ m_archive_sp = archive_sp;
+}
+
+ObjectContainerBSDArchive::~ObjectContainerBSDArchive() = default;
+
+bool ObjectContainerBSDArchive::ParseHeader() {
+ if (m_archive_sp.get() == nullptr) {
+ if (m_data.GetByteSize() > 0) {
+ ModuleSP module_sp(GetModule());
+ if (module_sp) {
+ m_archive_sp = Archive::ParseAndCacheArchiveForFile(
+ m_file, module_sp->GetArchitecture(),
+ module_sp->GetModificationTime(), m_offset, m_data, m_archive_type);
+ }
+ // Clear the m_data that contains the entire archive data and let our
+ // m_archive_sp hold onto the data.
+ m_data.Clear();
+ }
+ }
+ return m_archive_sp.get() != nullptr;
+}
+
+FileSpec GetChildFileSpecificationsFromThin(llvm::StringRef childPath,
+ const FileSpec &parentFileSpec) {
+ llvm::SmallString<128> FullPath;
+ if (llvm::sys::path::is_absolute(childPath)) {
+ FullPath = childPath;
+ } else {
+ FullPath = parentFileSpec.GetDirectory().GetStringRef();
+ llvm::sys::path::append(FullPath, childPath);
+ }
+ FileSpec child = FileSpec(FullPath.str(), llvm::sys::path::Style::posix);
+ return child;
+}
+
+ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
+ ModuleSP module_sp(GetModule());
+ if (module_sp) {
+ if (module_sp->GetObjectName() && m_archive_sp) {
+ Object *object = m_archive_sp->FindObject(
+ module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
+ if (object) {
+ if (m_archive_type == ArchiveType::ThinArchive) {
+ // Set file to child object file
+ FileSpec child = GetChildFileSpecificationsFromThin(
+ object->ar_name.GetStringRef(), m_file);
+ lldb::offset_t file_offset = 0;
+ lldb::offset_t file_size = object->size;
+ std::shared_ptr<DataBuffer> child_data_sp =
+ FileSystem::Instance().CreateDataBuffer(child, file_size,
+ file_offset);
+ if (!child_data_sp ||
+ child_data_sp->GetByteSize() != object->file_size)
+ return ObjectFileSP();
+ lldb::offset_t data_offset = 0;
+ return ObjectFile::FindPlugin(
+ module_sp, &child, m_offset + object->file_offset,
+ object->file_size, child_data_sp, data_offset);
+ }
+ lldb::offset_t data_offset = object->file_offset;
+ return ObjectFile::FindPlugin(
+ module_sp, file, m_offset + object->file_offset, object->file_size,
+ m_archive_sp->GetData().GetSharedDataBuffer(), data_offset);
+ }
+ }
+ }
+ return ObjectFileSP();
+}
+
+size_t ObjectContainerBSDArchive::GetModuleSpecifications(
+ const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
+ lldb::offset_t data_offset, lldb::offset_t file_offset,
+ lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
+
+ // We have data, which means this is the first 512 bytes of the file Check to
+ // see if the magic bytes match and if they do, read the entire table of
+ // contents for the archive and cache it
+ DataExtractor data;
+ data.SetData(data_sp, data_offset, data_sp->GetByteSize());
+ ArchiveType archive_type = ObjectContainerBSDArchive::MagicBytesMatch(data);
+ if (!file || !data_sp || archive_type == ArchiveType::Invalid)
+ return 0;
+
+ const size_t initial_count = specs.GetSize();
+ llvm::sys::TimePoint<> file_mod_time = FileSystem::Instance().GetModificationTime(file);
+ Archive::shared_ptr archive_sp(
+ Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
+ bool set_archive_arch = false;
+ if (!archive_sp) {
+ set_archive_arch = true;
+ data_sp =
+ FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
+ if (data_sp) {
+ data.SetData(data_sp, 0, data_sp->GetByteSize());
+ archive_sp = Archive::ParseAndCacheArchiveForFile(
+ file, ArchSpec(), file_mod_time, file_offset, data, archive_type);
+ }
+ }
+
+ if (archive_sp) {
+ const size_t num_objects = archive_sp->GetNumObjects();
+ for (size_t idx = 0; idx < num_objects; ++idx) {
+ const Object *object = archive_sp->GetObjectAtIndex(idx);
+ if (object) {
+ if (archive_sp->GetArchiveType() == ArchiveType::ThinArchive) {
+ if (object->ar_name.IsEmpty())
+ continue;
+ FileSpec child = GetChildFileSpecificationsFromThin(
+ object->ar_name.GetStringRef(), file);
+ if (ObjectFile::GetModuleSpecifications(child, 0, object->file_size,
+ specs)) {
+ ModuleSpec &spec =
+ specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
+ llvm::sys::TimePoint<> object_mod_time(
+ std::chrono::seconds(object->modification_time));
+ spec.GetObjectName() = object->ar_name;
+ spec.SetObjectOffset(0);
+ spec.SetObjectSize(object->file_size);
+ spec.GetObjectModificationTime() = object_mod_time;
+ }
+ continue;
+ }
+ const lldb::offset_t object_file_offset =
+ file_offset + object->file_offset;
+ if (object->file_offset < file_size && file_size > object_file_offset) {
+ if (ObjectFile::GetModuleSpecifications(
+ file, object_file_offset, file_size - object_file_offset,
+ specs)) {
+ ModuleSpec &spec =
+ specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
+ llvm::sys::TimePoint<> object_mod_time(
+ std::chrono::seconds(object->modification_time));
+ spec.GetObjectName() = object->ar_name;
+ spec.SetObjectOffset(object_file_offset);
+ spec.SetObjectSize(object->file_size);
+ spec.GetObjectModificationTime() = object_mod_time;
+ }
+ }
+ }
+ }
+ }
+ const size_t end_count = specs.GetSize();
+ size_t num_specs_added = end_count - initial_count;
+ if (set_archive_arch && num_specs_added > 0) {
+ // The archive was created but we didn't have an architecture so we need to
+ // set it
+ for (size_t i = initial_count; i < end_count; ++i) {
+ ModuleSpec module_spec;
+ if (specs.GetModuleSpecAtIndex(i, module_spec)) {
+ if (module_spec.GetArchitecture().IsValid()) {
+ archive_sp->SetArchitecture(module_spec.GetArchitecture());
+ break;
+ }
+ }
+ }
+ }
+ return num_specs_added;
+}
diff --git a/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h
new file mode 100644
index 000000000000..fbecd1d27063
--- /dev/null
+++ b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h
@@ -0,0 +1,182 @@
+//===-- ObjectContainerBSDArchive.h -----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H
+#define LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H
+
+#include "lldb/Core/UniqueCStringMap.h"
+#include "lldb/Symbol/ObjectContainer.h"
+#include "lldb/Utility/ArchSpec.h"
+#include "lldb/Utility/ConstString.h"
+#include "lldb/Utility/FileSpec.h"
+
+#include "llvm/Object/Archive.h"
+#include "llvm/Support/Chrono.h"
+#include "llvm/Support/Path.h"
+
+#include <map>
+#include <memory>
+#include <mutex>
+
+enum class ArchiveType { Invalid, Archive, ThinArchive };
+
+class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
+public:
+ ObjectContainerBSDArchive(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,
+ ArchiveType archive_type);
+
+ ~ObjectContainerBSDArchive() override;
+
+ // Static Functions
+ static void Initialize();
+
+ static void Terminate();
+
+ static llvm::StringRef GetPluginNameStatic() { return "bsd-archive"; }
+
+ static llvm::StringRef GetPluginDescriptionStatic() {
+ return "BSD Archive object container reader.";
+ }
+
+ 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);
+
+ 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);
+
+ static ArchiveType MagicBytesMatch(const lldb_private::DataExtractor &data);
+
+ // Member Functions
+ bool ParseHeader() override;
+
+ size_t GetNumObjects() const override {
+ if (m_archive_sp)
+ return m_archive_sp->GetNumObjects();
+ return 0;
+ }
+
+ lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override;
+
+ // PluginInterface protocol
+ llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
+
+protected:
+ struct Object {
+ Object();
+
+ void Clear();
+
+ lldb::offset_t ExtractFromThin(const lldb_private::DataExtractor &data,
+ lldb::offset_t offset,
+ llvm::StringRef stringTable);
+
+ lldb::offset_t Extract(const lldb_private::DataExtractor &data,
+ lldb::offset_t offset);
+ /// Object name in the archive.
+ lldb_private::ConstString ar_name;
+
+ /// Object modification time in the archive.
+ uint32_t modification_time = 0;
+
+ /// Object size in bytes in the archive.
+ uint32_t size = 0;
+
+ /// File offset in bytes from the beginning of the file of the object data.
+ lldb::offset_t file_offset = 0;
+
+ /// Length of the object data.
+ lldb::offset_t file_size = 0;
+
+ void Dump() const;
+ };
+
+ class Archive {
+ public:
+ typedef std::shared_ptr<Archive> shared_ptr;
+ typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map;
+
+ Archive(const lldb_private::ArchSpec &arch,
+ const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
+ lldb_private::DataExtractor &data, ArchiveType archive_type);
+
+ ~Archive();
+
+ static Map &GetArchiveCache();
+
+ static std::recursive_mutex &GetArchiveCacheMutex();
+
+ static Archive::shared_ptr FindCachedArchive(
+ const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
+ const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset);
+
+ static Archive::shared_ptr ParseAndCacheArchiveForFile(
+ const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
+ const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
+ lldb_private::DataExtractor &data, ArchiveType archive_type);
+
+ size_t GetNumObjects() const { return m_objects.size(); }
+
+ const Object *GetObjectAtIndex(size_t idx) {
+ if (idx < m_objects.size())
+ return &m_objects[idx];
+ return nullptr;
+ }
+
+ size_t ParseObjects();
+
+ Object *FindObject(lldb_private::ConstString object_name,
+ const llvm::sys::TimePoint<> &object_mod_time);
+
+ lldb::offset_t GetFileOffset() const { return m_file_offset; }
+
+ const llvm::sys::TimePoint<> &GetModificationTime() {
+ return m_modification_time;
+ }
+
+ const lldb_private::ArchSpec &GetArchitecture() const { return m_arch; }
+
+ void SetArchitecture(const lldb_private::ArchSpec &arch) { m_arch = arch; }
+
+ bool HasNoExternalReferences() const;
+
+ lldb_private::DataExtractor &GetData() { return m_data; }
+
+ ArchiveType GetArchiveType() { return m_archive_type; }
+
+ protected:
+ typedef lldb_private::UniqueCStringMap<uint32_t> ObjectNameToIndexMap;
+ // Member Variables
+ lldb_private::ArchSpec m_arch;
+ llvm::sys::TimePoint<> m_modification_time;
+ lldb::offset_t m_file_offset;
+ std::vector<Object> m_objects;
+ ObjectNameToIndexMap m_object_name_to_index_map;
+ lldb_private::DataExtractor m_data; ///< The data for this object container
+ ///so we don't lose data if the .a files
+ ///gets modified
+ ArchiveType m_archive_type;
+ };
+
+ void SetArchive(Archive::shared_ptr &archive_sp);
+
+ Archive::shared_ptr m_archive_sp;
+
+ ArchiveType m_archive_type;
+};
+
+#endif // LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H
diff --git a/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.cpp b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.cpp
new file mode 100644
index 000000000000..76141f797641
--- /dev/null
+++ b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.cpp
@@ -0,0 +1,299 @@
+//===-- ObjectContainerMachOFileset.cpp -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "ObjectContainerMachOFileset.h"
+#include "lldb/Core/Module.h"
+#include "lldb/Core/ModuleSpec.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Symbol/ObjectFile.h"
+#include "lldb/Target/Target.h"
+#include "lldb/Utility/ArchSpec.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/Stream.h"
+#include <optional>
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace llvm::MachO;
+
+LLDB_PLUGIN_DEFINE(ObjectContainerMachOFileset)
+
+void ObjectContainerMachOFileset::Initialize() {
+ PluginManager::RegisterPlugin(GetPluginNameStatic(),
+ GetPluginDescriptionStatic(), CreateInstance,
+ GetModuleSpecifications, CreateMemoryInstance);
+}
+
+void ObjectContainerMachOFileset::Terminate() {
+ PluginManager::UnregisterPlugin(CreateInstance);
+}
+
+ObjectContainerMachOFileset::ObjectContainerMachOFileset(
+ 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)
+ : ObjectContainer(module_sp, file, offset, length, data_sp, data_offset),
+ m_memory_addr(LLDB_INVALID_ADDRESS) {}
+
+ObjectContainerMachOFileset::ObjectContainerMachOFileset(
+ const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
+ const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
+ : ObjectContainer(module_sp, nullptr, 0, data_sp->GetByteSize(), data_sp,
+ 0),
+ m_process_wp(process_sp), m_memory_addr(header_addr) {}
+
+ObjectContainer *ObjectContainerMachOFileset::CreateInstance(
+ const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
+ lldb::offset_t data_offset, const FileSpec *file,
+ lldb::offset_t file_offset, lldb::offset_t length) {
+ if (!data_sp)
+ return {};
+
+ DataExtractor data;
+ data.SetData(data_sp, data_offset, length);
+ if (!MagicBytesMatch(data))
+ return {};
+
+ auto container_up = std::make_unique<ObjectContainerMachOFileset>(
+ module_sp, data_sp, data_offset, file, file_offset, length);
+ if (!container_up->ParseHeader())
+ return {};
+
+ return container_up.release();
+}
+
+ObjectContainer *ObjectContainerMachOFileset::CreateMemoryInstance(
+ const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
+ const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
+ if (!MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
+ return {};
+
+ auto container_up = std::make_unique<ObjectContainerMachOFileset>(
+ module_sp, data_sp, process_sp, header_addr);
+ if (!container_up->ParseHeader())
+ return {};
+
+ return container_up.release();
+}
+
+ObjectContainerMachOFileset::~ObjectContainerMachOFileset() = default;
+
+static uint32_t MachHeaderSizeFromMagic(uint32_t magic) {
+ switch (magic) {
+ case MH_MAGIC:
+ case MH_CIGAM:
+ return sizeof(struct mach_header);
+ case MH_MAGIC_64:
+ case MH_CIGAM_64:
+ return sizeof(struct mach_header_64);
+ default:
+ return 0;
+ }
+}
+
+static std::optional<mach_header> ParseMachOHeader(DataExtractor &data) {
+ lldb::offset_t offset = 0;
+ mach_header header;
+ header.magic = data.GetU32(&offset);
+ switch (header.magic) {
+ case MH_MAGIC:
+ data.SetByteOrder(endian::InlHostByteOrder());
+ data.SetAddressByteSize(4);
+ break;
+ case MH_MAGIC_64:
+ data.SetByteOrder(endian::InlHostByteOrder());
+ data.SetAddressByteSize(8);
+ break;
+ case MH_CIGAM:
+ data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig
+ ? eByteOrderLittle
+ : eByteOrderBig);
+ data.SetAddressByteSize(4);
+ break;
+ case MH_CIGAM_64:
+ data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig
+ ? eByteOrderLittle
+ : eByteOrderBig);
+ data.SetAddressByteSize(8);
+ break;
+ default:
+ return {};
+ }
+
+ header.cputype = data.GetU32(&offset);
+ header.cpusubtype = data.GetU32(&offset);
+ header.filetype = data.GetU32(&offset);
+ header.ncmds = data.GetU32(&offset);
+ header.sizeofcmds = data.GetU32(&offset);
+ return header;
+}
+
+static bool
+ParseFileset(DataExtractor &data, mach_header header,
+ std::vector<ObjectContainerMachOFileset::Entry> &entries,
+ std::optional<lldb::addr_t> load_addr = std::nullopt) {
+ lldb::offset_t offset = MachHeaderSizeFromMagic(header.magic);
+ lldb::offset_t slide = 0;
+ for (uint32_t i = 0; i < header.ncmds; ++i) {
+ const lldb::offset_t load_cmd_offset = offset;
+ load_command lc = {};
+ if (data.GetU32(&offset, &lc.cmd, 2) == nullptr)
+ break;
+
+ // If we know the load address we can compute the slide.
+ if (load_addr) {
+ if (lc.cmd == llvm::MachO::LC_SEGMENT_64) {
+ segment_command_64 segment;
+ data.CopyData(load_cmd_offset, sizeof(segment_command_64), &segment);
+ if (llvm::StringRef(segment.segname) == "__TEXT")
+ slide = *load_addr - segment.vmaddr;
+ }
+ }
+
+ if (lc.cmd == LC_FILESET_ENTRY) {
+ fileset_entry_command entry;
+ data.CopyData(load_cmd_offset, sizeof(fileset_entry_command), &entry);
+ lldb::offset_t entry_id_offset = load_cmd_offset + entry.entry_id.offset;
+ if (const char *id = data.GetCStr(&entry_id_offset))
+ entries.emplace_back(entry.vmaddr + slide, entry.fileoff,
+ std::string(id));
+ }
+
+ offset = load_cmd_offset + lc.cmdsize;
+ }
+
+ return true;
+}
+
+bool ObjectContainerMachOFileset::ParseHeader(
+ DataExtractor &data, const lldb_private::FileSpec &file,
+ lldb::offset_t file_offset, std::vector<Entry> &entries) {
+ std::optional<mach_header> header = ParseMachOHeader(data);
+
+ if (!header)
+ return false;
+
+ const size_t header_size = MachHeaderSizeFromMagic(header->magic);
+ const size_t header_and_lc_size = header_size + header->sizeofcmds;
+
+ if (data.GetByteSize() < header_and_lc_size) {
+ DataBufferSP data_sp =
+ ObjectFile::MapFileData(file, header_and_lc_size, file_offset);
+ data.SetData(data_sp);
+ }
+
+ return ParseFileset(data, *header, entries);
+}
+
+bool ObjectContainerMachOFileset::ParseHeader() {
+ ModuleSP module_sp(GetModule());
+ if (!module_sp)
+ return false;
+
+ std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
+
+ std::optional<mach_header> header = ParseMachOHeader(m_data);
+ if (!header)
+ return false;
+
+ const size_t header_size = MachHeaderSizeFromMagic(header->magic);
+ const size_t header_and_lc_size = header_size + header->sizeofcmds;
+
+ if (m_data.GetByteSize() < header_and_lc_size) {
+ ProcessSP process_sp(m_process_wp.lock());
+ DataBufferSP data_sp =
+ process_sp
+ ? ObjectFile::ReadMemory(process_sp, m_memory_addr,
+ header_and_lc_size)
+ : ObjectFile::MapFileData(m_file, header_and_lc_size, m_offset);
+ m_data.SetData(data_sp);
+ }
+
+ return ParseFileset(m_data, *header, m_entries, m_memory_addr);
+}
+
+size_t ObjectContainerMachOFileset::GetModuleSpecifications(
+ const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
+ lldb::offset_t data_offset, lldb::offset_t file_offset,
+ lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
+ const size_t initial_count = specs.GetSize();
+
+ DataExtractor data;
+ data.SetData(data_sp, data_offset, data_sp->GetByteSize());
+
+ if (MagicBytesMatch(data)) {
+ std::vector<Entry> entries;
+ if (ParseHeader(data, file, file_offset, entries)) {
+ for (const Entry &entry : entries) {
+ const lldb::offset_t entry_offset = entry.fileoff + file_offset;
+ if (ObjectFile::GetModuleSpecifications(
+ file, entry_offset, file_size - entry_offset, specs)) {
+ ModuleSpec &spec = specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
+ spec.GetObjectName() = ConstString(entry.id);
+ }
+ }
+ }
+ }
+ return specs.GetSize() - initial_count;
+}
+
+bool ObjectContainerMachOFileset::MagicBytesMatch(DataBufferSP data_sp,
+ lldb::addr_t data_offset,
+ lldb::addr_t data_length) {
+ DataExtractor data;
+ data.SetData(data_sp, data_offset, data_length);
+ return MagicBytesMatch(data);
+}
+
+bool ObjectContainerMachOFileset::MagicBytesMatch(const DataExtractor &data) {
+ lldb::offset_t offset = 0;
+ uint32_t magic = data.GetU32(&offset);
+ switch (magic) {
+ case MH_MAGIC:
+ case MH_CIGAM:
+ case MH_MAGIC_64:
+ case MH_CIGAM_64:
+ break;
+ default:
+ return false;
+ }
+ offset += 4; // cputype
+ offset += 4; // cpusubtype
+ uint32_t filetype = data.GetU32(&offset);
+ return filetype == MH_FILESET;
+}
+
+ObjectFileSP
+ObjectContainerMachOFileset::GetObjectFile(const lldb_private::FileSpec *file) {
+ ModuleSP module_sp(GetModule());
+ if (!module_sp)
+ return {};
+
+ ConstString object_name = module_sp->GetObjectName();
+ if (!object_name)
+ return {};
+
+ Entry *entry = FindEntry(object_name.GetCString());
+ if (!entry)
+ return {};
+
+ DataBufferSP data_sp;
+ lldb::offset_t data_offset = 0;
+ return ObjectFile::FindPlugin(module_sp, file, m_offset + entry->fileoff,
+ m_data.GetByteSize() - entry->fileoff, data_sp,
+ data_offset);
+}
+
+ObjectContainerMachOFileset::Entry *
+ObjectContainerMachOFileset::FindEntry(llvm::StringRef id) {
+ for (Entry &entry : m_entries) {
+ if (entry.id == id)
+ return &entry;
+ }
+ return nullptr;
+}
diff --git a/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.h b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.h
new file mode 100644
index 000000000000..134c503aac91
--- /dev/null
+++ b/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/Mach-O-Fileset/ObjectContainerMachOFileset.h
@@ -0,0 +1,94 @@
+//===-- ObjectContainerMachOFileset.h ---------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_MACH_O_FILESET_OBJECTCONTAINERMADCHOFILESET_H
+#define LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_MACH_O_FILESET_OBJECTCONTAINERMADCHOFILESET_H
+
+#include "lldb/Host/SafeMachO.h"
+#include "lldb/Symbol/ObjectContainer.h"
+#include "lldb/Utility/FileSpec.h"
+
+namespace lldb_private {
+
+class ObjectContainerMachOFileset : public lldb_private::ObjectContainer {
+public:
+ ObjectContainerMachOFileset(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);
+
+ ObjectContainerMachOFileset(const lldb::ModuleSP &module_sp,
+ lldb::WritableDataBufferSP data_sp,
+ const lldb::ProcessSP &process_sp,
+ lldb::addr_t header_addr);
+
+ ~ObjectContainerMachOFileset() override;
+
+ static void Initialize();
+ static void Terminate();
+
+ static llvm::StringRef GetPluginNameStatic() { return "mach-o-fileset"; }
+
+ static llvm::StringRef GetPluginDescriptionStatic() {
+ return "Mach-O Fileset container reader.";
+ }
+
+ 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);
+
+ static lldb_private::ObjectContainer *CreateMemoryInstance(
+ const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
+ const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
+
+ 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);
+
+ static bool MagicBytesMatch(const lldb_private::DataExtractor &data);
+ static bool MagicBytesMatch(lldb::DataBufferSP data_sp,
+ lldb::addr_t data_offset,
+ lldb::addr_t data_length);
+
+ bool ParseHeader() override;
+
+ size_t GetNumObjects() const override { return m_entries.size(); }
+
+ lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override;
+
+ llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
+
+ struct Entry {
+ Entry(uint64_t vmaddr, uint64_t fileoff, std::string id)
+ : vmaddr(vmaddr), fileoff(fileoff), id(id) {}
+ uint64_t vmaddr;
+ uint64_t fileoff;
+ std::string id;
+ };
+
+ Entry *FindEntry(llvm::StringRef id);
+
+private:
+ static bool ParseHeader(lldb_private::DataExtractor &data,
+ const lldb_private::FileSpec &file,
+ lldb::offset_t file_offset,
+ std::vector<Entry> &entries);
+
+ std::vector<Entry> m_entries;
+ lldb::ProcessWP m_process_wp;
+ const lldb::addr_t m_memory_addr;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_MACH_O_FILESET_OBJECTCONTAINERMADCHOFILESET_H