Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h
39645 views
//===-- ObjectContainerBSDArchive.h -----------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H9#define LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H1011#include "lldb/Core/UniqueCStringMap.h"12#include "lldb/Symbol/ObjectContainer.h"13#include "lldb/Utility/ArchSpec.h"14#include "lldb/Utility/ConstString.h"15#include "lldb/Utility/FileSpec.h"1617#include "llvm/Object/Archive.h"18#include "llvm/Support/Chrono.h"19#include "llvm/Support/Path.h"2021#include <map>22#include <memory>23#include <mutex>2425enum class ArchiveType { Invalid, Archive, ThinArchive };2627class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {28public:29ObjectContainerBSDArchive(const lldb::ModuleSP &module_sp,30lldb::DataBufferSP &data_sp,31lldb::offset_t data_offset,32const lldb_private::FileSpec *file,33lldb::offset_t offset, lldb::offset_t length,34ArchiveType archive_type);3536~ObjectContainerBSDArchive() override;3738// Static Functions39static void Initialize();4041static void Terminate();4243static llvm::StringRef GetPluginNameStatic() { return "bsd-archive"; }4445static llvm::StringRef GetPluginDescriptionStatic() {46return "BSD Archive object container reader.";47}4849static lldb_private::ObjectContainer *50CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,51lldb::offset_t data_offset, const lldb_private::FileSpec *file,52lldb::offset_t offset, lldb::offset_t length);5354static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,55lldb::DataBufferSP &data_sp,56lldb::offset_t data_offset,57lldb::offset_t file_offset,58lldb::offset_t length,59lldb_private::ModuleSpecList &specs);6061static ArchiveType MagicBytesMatch(const lldb_private::DataExtractor &data);6263// Member Functions64bool ParseHeader() override;6566size_t GetNumObjects() const override {67if (m_archive_sp)68return m_archive_sp->GetNumObjects();69return 0;70}7172lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override;7374// PluginInterface protocol75llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }7677protected:78struct Object {79Object();8081void Clear();8283lldb::offset_t ExtractFromThin(const lldb_private::DataExtractor &data,84lldb::offset_t offset,85llvm::StringRef stringTable);8687lldb::offset_t Extract(const lldb_private::DataExtractor &data,88lldb::offset_t offset);89/// Object name in the archive.90lldb_private::ConstString ar_name;9192/// Object modification time in the archive.93uint32_t modification_time = 0;9495/// Object size in bytes in the archive.96uint32_t size = 0;9798/// File offset in bytes from the beginning of the file of the object data.99lldb::offset_t file_offset = 0;100101/// Length of the object data.102lldb::offset_t file_size = 0;103104void Dump() const;105};106107class Archive {108public:109typedef std::shared_ptr<Archive> shared_ptr;110typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map;111112Archive(const lldb_private::ArchSpec &arch,113const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,114lldb_private::DataExtractor &data, ArchiveType archive_type);115116~Archive();117118static Map &GetArchiveCache();119120static std::recursive_mutex &GetArchiveCacheMutex();121122static Archive::shared_ptr FindCachedArchive(123const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,124const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset);125126static Archive::shared_ptr ParseAndCacheArchiveForFile(127const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,128const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,129lldb_private::DataExtractor &data, ArchiveType archive_type);130131size_t GetNumObjects() const { return m_objects.size(); }132133const Object *GetObjectAtIndex(size_t idx) {134if (idx < m_objects.size())135return &m_objects[idx];136return nullptr;137}138139size_t ParseObjects();140141Object *FindObject(lldb_private::ConstString object_name,142const llvm::sys::TimePoint<> &object_mod_time);143144lldb::offset_t GetFileOffset() const { return m_file_offset; }145146const llvm::sys::TimePoint<> &GetModificationTime() {147return m_modification_time;148}149150const lldb_private::ArchSpec &GetArchitecture() const { return m_arch; }151152void SetArchitecture(const lldb_private::ArchSpec &arch) { m_arch = arch; }153154bool HasNoExternalReferences() const;155156lldb_private::DataExtractor &GetData() { return m_data; }157158ArchiveType GetArchiveType() { return m_archive_type; }159160protected:161typedef lldb_private::UniqueCStringMap<uint32_t> ObjectNameToIndexMap;162// Member Variables163lldb_private::ArchSpec m_arch;164llvm::sys::TimePoint<> m_modification_time;165lldb::offset_t m_file_offset;166std::vector<Object> m_objects;167ObjectNameToIndexMap m_object_name_to_index_map;168lldb_private::DataExtractor m_data; ///< The data for this object container169///so we don't lose data if the .a files170///gets modified171ArchiveType m_archive_type;172};173174void SetArchive(Archive::shared_ptr &archive_sp);175176Archive::shared_ptr m_archive_sp;177178ArchiveType m_archive_type;179};180181#endif // LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H182183184