LLDB  mainline
ObjectContainerBSDArchive.h
Go to the documentation of this file.
1 //===-- ObjectContainerBSDArchive.h -----------------------------*- 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 
9 #ifndef LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H
10 #define LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H
11 
14 #include "lldb/Utility/ArchSpec.h"
16 #include "lldb/Utility/FileSpec.h"
17 
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Support/Chrono.h"
20 #include "llvm/Support/Path.h"
21 
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 
27 
29 public:
30  ObjectContainerBSDArchive(const lldb::ModuleSP &module_sp,
31  lldb::DataBufferSP &data_sp,
32  lldb::offset_t data_offset,
33  const lldb_private::FileSpec *file,
34  lldb::offset_t offset, lldb::offset_t length,
35  ArchiveType archive_type);
36 
37  ~ObjectContainerBSDArchive() override;
38 
39  // Static Functions
40  static void Initialize();
41 
42  static void Terminate();
43 
44  static llvm::StringRef GetPluginNameStatic() { return "bsd-archive"; }
45 
46  static llvm::StringRef GetPluginDescriptionStatic() {
47  return "BSD Archive object container reader.";
48  }
49 
51  CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
52  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
53  lldb::offset_t offset, lldb::offset_t length);
54 
55  static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
56  lldb::DataBufferSP &data_sp,
57  lldb::offset_t data_offset,
58  lldb::offset_t file_offset,
59  lldb::offset_t length,
61 
63 
64  // Member Functions
65  bool ParseHeader() override;
66 
67  size_t GetNumObjects() const override {
68  if (m_archive_sp)
69  return m_archive_sp->GetNumObjects();
70  return 0;
71  }
72 
73  lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override;
74 
75  // PluginInterface protocol
76  llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
77 
78 protected:
79  struct Object {
80  Object();
81 
82  void Clear();
83 
85  lldb::offset_t offset,
86  llvm::StringRef stringTable);
87 
89  lldb::offset_t offset);
90  /// Object name in the archive.
92 
93  /// Object modification time in the archive.
95 
96  /// Object user id in the archive.
98 
99  /// Object group id in the archive.
101 
102  /// Object octal file permissions in the archive.
104 
105  /// Object size in bytes in the archive.
107 
108  /// File offset in bytes from the beginning of the file of the object data.
110 
111  /// Length of the object data.
113  };
114 
115  class Archive {
116  public:
117  typedef std::shared_ptr<Archive> shared_ptr;
118  typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map;
119 
120  Archive(const lldb_private::ArchSpec &arch,
121  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
122  lldb_private::DataExtractor &data, ArchiveType archive_type);
123 
124  ~Archive();
125 
126  static Map &GetArchiveCache();
127 
128  static std::recursive_mutex &GetArchiveCacheMutex();
129 
131  const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
132  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset);
133 
135  const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
136  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
137  lldb_private::DataExtractor &data, ArchiveType archive_type);
138 
139  size_t GetNumObjects() const { return m_objects.size(); }
140 
141  const Object *GetObjectAtIndex(size_t idx) {
142  if (idx < m_objects.size())
143  return &m_objects[idx];
144  return nullptr;
145  }
146 
147  size_t ParseObjects();
148 
150  const llvm::sys::TimePoint<> &object_mod_time);
151 
153 
154  const llvm::sys::TimePoint<> &GetModificationTime() {
155  return m_modification_time;
156  }
157 
158  const lldb_private::ArchSpec &GetArchitecture() const { return m_arch; }
159 
160  void SetArchitecture(const lldb_private::ArchSpec &arch) { m_arch = arch; }
161 
162  bool HasNoExternalReferences() const;
163 
165 
167 
168  protected:
170  // Member Variables
172  llvm::sys::TimePoint<> m_modification_time;
174  std::vector<Object> m_objects;
176  lldb_private::DataExtractor m_data; ///< The data for this object container
177  ///so we don't lose data if the .a files
178  ///gets modified
180  };
181 
182  void SetArchive(Archive::shared_ptr &archive_sp);
183 
185 
187 };
188 
189 #endif // LLDB_SOURCE_PLUGINS_OBJECTCONTAINER_BSD_ARCHIVE_OBJECTCONTAINERBSDARCHIVE_H
ObjectContainerBSDArchive::Object::modification_time
uint32_t modification_time
Object modification time in the archive.
Definition: ObjectContainerBSDArchive.h:94
lldb_private::ArchSpec
Definition: ArchSpec.h:32
ObjectContainerBSDArchive::Archive::GetNumObjects
size_t GetNumObjects() const
Definition: ObjectContainerBSDArchive.h:139
ObjectContainerBSDArchive::Archive::m_file_offset
lldb::offset_t m_file_offset
Definition: ObjectContainerBSDArchive.h:173
ObjectContainerBSDArchive::Archive
Definition: ObjectContainerBSDArchive.h:115
ObjectContainerBSDArchive::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: ObjectContainerBSDArchive.cpp:560
ObjectContainerBSDArchive::Archive::m_objects
std::vector< Object > m_objects
Definition: ObjectContainerBSDArchive.h:174
ObjectContainerBSDArchive::Object::Clear
void Clear()
Definition: ObjectContainerBSDArchive.cpp:49
lldb_private::ModuleSpecList
Definition: ModuleSpec.h:275
ObjectContainerBSDArchive::Terminate
static void Terminate()
Definition: ObjectContainerBSDArchive.cpp:387
lldb_private::ObjectContainer
Definition: ObjectContainer.h:30
ObjectContainerBSDArchive::SetArchive
void SetArchive(Archive::shared_ptr &archive_sp)
Definition: ObjectContainerBSDArchive.cpp:491
ObjectContainerBSDArchive
Definition: ObjectContainerBSDArchive.h:28
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
ObjectContainerBSDArchive::Archive::GetArchiveCache
static Map & GetArchiveCache()
Definition: ObjectContainerBSDArchive.cpp:370
ObjectContainerBSDArchive::Object
Definition: ObjectContainerBSDArchive.h:79
ObjectContainerBSDArchive::GetPluginName
llvm::StringRef GetPluginName() override
Definition: ObjectContainerBSDArchive.h:76
ObjectContainerBSDArchive::Archive::FindObject
Object * FindObject(lldb_private::ConstString object_name, const llvm::sys::TimePoint<> &object_mod_time)
Definition: ObjectContainerBSDArchive.cpp:284
ObjectContainerBSDArchive::Archive::FindCachedArchive
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)
Definition: ObjectContainerBSDArchive.cpp:310
UniqueCStringMap.h
ObjectContainerBSDArchive::Archive::m_modification_time
llvm::sys::TimePoint m_modification_time
Definition: ObjectContainerBSDArchive.h:172
ObjectContainerBSDArchive::Archive::SetArchitecture
void SetArchitecture(const lldb_private::ArchSpec &arch)
Definition: ObjectContainerBSDArchive.h:160
lldb_private::FileSpec
Definition: FileSpec.h:55
ObjectContainerBSDArchive::Archive::m_arch
lldb_private::ArchSpec m_arch
Definition: ObjectContainerBSDArchive.h:171
ArchiveType::Archive
@ Archive
ArchiveType::Invalid
@ Invalid
lldb_private::DataExtractor
Definition: DataExtractor.h:48
ObjectContainerBSDArchive::Archive::GetData
lldb_private::DataExtractor & GetData()
Definition: ObjectContainerBSDArchive.h:164
ObjectContainerBSDArchive::Archive::GetArchiveType
ArchiveType GetArchiveType()
Definition: ObjectContainerBSDArchive.h:166
ObjectContainerBSDArchive::Archive::ParseObjects
size_t ParseObjects()
Definition: ObjectContainerBSDArchive.cpp:216
ObjectContainerBSDArchive::Archive::shared_ptr
std::shared_ptr< Archive > shared_ptr
Definition: ObjectContainerBSDArchive.h:117
ObjectContainerBSDArchive::m_archive_type
ArchiveType m_archive_type
Definition: ObjectContainerBSDArchive.h:186
ObjectContainerBSDArchive::Object::Extract
lldb::offset_t Extract(const lldb_private::DataExtractor &data, lldb::offset_t offset)
Definition: ObjectContainerBSDArchive.cpp:129
ObjectContainerBSDArchive::Archive::GetArchitecture
const lldb_private::ArchSpec & GetArchitecture() const
Definition: ObjectContainerBSDArchive.h:158
ObjectContainerBSDArchive::Archive::m_object_name_to_index_map
ObjectNameToIndexMap m_object_name_to_index_map
Definition: ObjectContainerBSDArchive.h:175
ObjectContainerBSDArchive::ParseHeader
bool ParseHeader() override
Attempts to parse the object header.
Definition: ObjectContainerBSDArchive.cpp:497
lldb_private::ConstString
Definition: ConstString.h:39
ObjectContainerBSDArchive::~ObjectContainerBSDArchive
~ObjectContainerBSDArchive() override
ObjectContainerBSDArchive::Archive::GetFileOffset
lldb::offset_t GetFileOffset() const
Definition: ObjectContainerBSDArchive.h:152
ObjectContainerBSDArchive::Archive::GetModificationTime
const llvm::sys::TimePoint & GetModificationTime()
Definition: ObjectContainerBSDArchive.h:154
lldb_private::UniqueCStringMap< uint32_t >
ObjectContainerBSDArchive::Archive::m_archive_type
ArchiveType m_archive_type
Definition: ObjectContainerBSDArchive.h:179
ObjectContainerBSDArchive::Object::Object
Object()
Definition: ObjectContainerBSDArchive.cpp:47
ObjectContainerBSDArchive::Archive::HasNoExternalReferences
bool HasNoExternalReferences() const
ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex
static std::recursive_mutex & GetArchiveCacheMutex()
Definition: ObjectContainerBSDArchive.cpp:376
ObjectContainerBSDArchive::Object::ar_name
lldb_private::ConstString ar_name
Object name in the archive.
Definition: ObjectContainerBSDArchive.h:91
ObjectContainerBSDArchive::Archive::m_data
lldb_private::DataExtractor m_data
The data for this object container so we don't lose data if the .a files gets modified.
Definition: ObjectContainerBSDArchive.h:176
uint32_t
ObjectContainerBSDArchive::GetNumObjects
size_t GetNumObjects() const override
Get the number of objects within this object file (archives).
Definition: ObjectContainerBSDArchive.h:67
ObjectContainerBSDArchive::Initialize
static void Initialize()
Definition: ObjectContainerBSDArchive.cpp:381
ObjectContainerBSDArchive::m_archive_sp
Archive::shared_ptr m_archive_sp
Definition: ObjectContainerBSDArchive.h:184
ObjectContainerBSDArchive::Archive::ObjectNameToIndexMap
lldb_private::UniqueCStringMap< uint32_t > ObjectNameToIndexMap
Definition: ObjectContainerBSDArchive.h:169
ObjectContainerBSDArchive::Object::size
uint32_t size
Object size in bytes in the archive.
Definition: ObjectContainerBSDArchive.h:106
ArchSpec.h
ObjectContainerBSDArchive::ObjectContainerBSDArchive
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)
Definition: ObjectContainerBSDArchive.cpp:482
uint16_t
ObjectContainerBSDArchive::Object::uid
uint16_t uid
Object user id in the archive.
Definition: ObjectContainerBSDArchive.h:97
ObjectContainerBSDArchive::Object::gid
uint16_t gid
Object group id in the archive.
Definition: ObjectContainerBSDArchive.h:100
ObjectContainerBSDArchive::GetObjectFile
lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override
Selects an architecture in an object file.
Definition: ObjectContainerBSDArchive.cpp:527
ObjectContainerBSDArchive::Archive::Map
std::multimap< lldb_private::FileSpec, shared_ptr > Map
Definition: ObjectContainerBSDArchive.h:118
ObjectContainerBSDArchive::Object::file_size
lldb::offset_t file_size
Length of the object data.
Definition: ObjectContainerBSDArchive.h:112
ObjectContainerBSDArchive::Object::file_offset
lldb::offset_t file_offset
File offset in bytes from the beginning of the file of the object data.
Definition: ObjectContainerBSDArchive.h:109
ArchiveType::ThinArchive
@ ThinArchive
ArchiveType
ArchiveType
Definition: ObjectContainerBSDArchive.h:26
FileSpec.h
ObjectContainerBSDArchive::Object::ExtractFromThin
lldb::offset_t ExtractFromThin(const lldb_private::DataExtractor &data, lldb::offset_t offset, llvm::StringRef stringTable)
Definition: ObjectContainerBSDArchive.cpp:60
ConstString.h
ObjectContainer.h
ObjectContainerBSDArchive::Archive::Archive
Archive(const lldb_private::ArchSpec &arch, const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset, lldb_private::DataExtractor &data, ArchiveType archive_type)
Definition: ObjectContainerBSDArchive.cpp:206
ObjectContainerBSDArchive::GetPluginDescriptionStatic
static llvm::StringRef GetPluginDescriptionStatic()
Definition: ObjectContainerBSDArchive.h:46
ObjectContainerBSDArchive::Archive::GetObjectAtIndex
const Object * GetObjectAtIndex(size_t idx)
Definition: ObjectContainerBSDArchive.h:141
ObjectContainerBSDArchive::Archive::~Archive
~Archive()
ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile
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)
Definition: ObjectContainerBSDArchive.cpp:350
ObjectContainerBSDArchive::GetPluginNameStatic
static llvm::StringRef GetPluginNameStatic()
Definition: ObjectContainerBSDArchive.h:44
ObjectContainerBSDArchive::MagicBytesMatch
static ArchiveType MagicBytesMatch(const lldb_private::DataExtractor &data)
Definition: ObjectContainerBSDArchive.cpp:463
ObjectContainerBSDArchive::Object::mode
uint16_t mode
Object octal file permissions in the archive.
Definition: ObjectContainerBSDArchive.h:103
ObjectContainerBSDArchive::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: ObjectContainerBSDArchive.cpp:391