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/Support/Chrono.h"
19 
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 
25 public:
26  ObjectContainerBSDArchive(const lldb::ModuleSP &module_sp,
27  lldb::DataBufferSP &data_sp,
28  lldb::offset_t data_offset,
29  const lldb_private::FileSpec *file,
30  lldb::offset_t offset, lldb::offset_t length);
31 
32  ~ObjectContainerBSDArchive() override;
33 
34  // Static Functions
35  static void Initialize();
36 
37  static void Terminate();
38 
40 
41  static const char *GetPluginDescriptionStatic();
42 
44  CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
45  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
46  lldb::offset_t offset, lldb::offset_t length);
47 
48  static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
49  lldb::DataBufferSP &data_sp,
50  lldb::offset_t data_offset,
51  lldb::offset_t file_offset,
52  lldb::offset_t length,
54 
55  static bool MagicBytesMatch(const lldb_private::DataExtractor &data);
56 
57  // Member Functions
58  bool ParseHeader() override;
59 
60  size_t GetNumObjects() const override {
61  if (m_archive_sp)
62  return m_archive_sp->GetNumObjects();
63  return 0;
64  }
65 
66  void Dump(lldb_private::Stream *s) const override;
67 
68  lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override;
69 
70  // PluginInterface protocol
72 
73  uint32_t GetPluginVersion() override;
74 
75 protected:
76  struct Object {
77  Object();
78 
79  void Clear();
80 
82  lldb::offset_t offset);
83  /// Object name in the archive.
85 
86  /// Object modification time in the archive.
88 
89  /// Object user id in the archive.
91 
92  /// Object group id in the archive.
94 
95  /// Object octal file permissions in the archive.
97 
98  /// Object size in bytes in the archive.
100 
101  /// File offset in bytes from the beginning of the file of the object data.
103 
104  /// Length of the object data.
106  };
107 
108  class Archive {
109  public:
110  typedef std::shared_ptr<Archive> shared_ptr;
111  typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map;
112 
113  Archive(const lldb_private::ArchSpec &arch,
114  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
116 
117  ~Archive();
118 
119  static Map &GetArchiveCache();
120 
121  static std::recursive_mutex &GetArchiveCacheMutex();
122 
124  const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
125  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset);
126 
128  const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
129  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
131 
132  size_t GetNumObjects() const { return m_objects.size(); }
133 
134  const Object *GetObjectAtIndex(size_t idx) {
135  if (idx < m_objects.size())
136  return &m_objects[idx];
137  return nullptr;
138  }
139 
140  size_t ParseObjects();
141 
143  const llvm::sys::TimePoint<> &object_mod_time);
144 
146 
147  const llvm::sys::TimePoint<> &GetModificationTime() {
148  return m_modification_time;
149  }
150 
151  const lldb_private::ArchSpec &GetArchitecture() const { return m_arch; }
152 
153  void SetArchitecture(const lldb_private::ArchSpec &arch) { m_arch = arch; }
154 
155  bool HasNoExternalReferences() const;
156 
158 
159  protected:
161  // Member Variables
163  llvm::sys::TimePoint<> m_modification_time;
165  std::vector<Object> m_objects;
167  lldb_private::DataExtractor m_data; ///< The data for this object container
168  ///so we don't lose data if the .a files
169  ///gets modified
170  };
171 
172  void SetArchive(Archive::shared_ptr &archive_sp);
173 
175 };
176 
177 #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:87
lldb_private::ArchSpec
Definition: ArchSpec.h:33
ObjectContainerBSDArchive::Archive::GetNumObjects
size_t GetNumObjects() const
Definition: ObjectContainerBSDArchive.h:132
ObjectContainerBSDArchive::Archive::m_file_offset
lldb::offset_t m_file_offset
Definition: ObjectContainerBSDArchive.h:164
ObjectContainerBSDArchive::Archive
Definition: ObjectContainerBSDArchive.h:108
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:445
ObjectContainerBSDArchive::MagicBytesMatch
static bool MagicBytesMatch(const lldb_private::DataExtractor &data)
Definition: ObjectContainerBSDArchive.cpp:357
ObjectContainerBSDArchive::Archive::m_objects
std::vector< Object > m_objects
Definition: ObjectContainerBSDArchive.h:165
ObjectContainerBSDArchive::Object::Clear
void Clear()
Definition: ObjectContainerBSDArchive.cpp:49
lldb_private::ModuleSpecList
Definition: ModuleSpec.h:281
ObjectContainerBSDArchive::Terminate
static void Terminate()
Definition: ObjectContainerBSDArchive.cpp:275
lldb_private::ObjectContainer
Definition: ObjectContainer.h:30
ObjectContainerBSDArchive::SetArchive
void SetArchive(Archive::shared_ptr &archive_sp)
Definition: ObjectContainerBSDArchive.cpp:374
ObjectContainerBSDArchive
Definition: ObjectContainerBSDArchive.h:24
ObjectContainerBSDArchive::GetPluginName
lldb_private::ConstString GetPluginName() override
Definition: ObjectContainerBSDArchive.cpp:439
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::Stream
Definition: Stream.h:28
ObjectContainerBSDArchive::Archive::GetArchiveCache
static Map & GetArchiveCache()
Definition: ObjectContainerBSDArchive.cpp:258
ObjectContainerBSDArchive::Object
Definition: ObjectContainerBSDArchive.h:76
ObjectContainerBSDArchive::Archive::FindObject
Object * FindObject(lldb_private::ConstString object_name, const llvm::sys::TimePoint<> &object_mod_time)
Definition: ObjectContainerBSDArchive.cpp:173
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:199
UniqueCStringMap.h
ObjectContainerBSDArchive::Archive::m_modification_time
llvm::sys::TimePoint m_modification_time
Definition: ObjectContainerBSDArchive.h:163
ObjectContainerBSDArchive::Archive::SetArchitecture
void SetArchitecture(const lldb_private::ArchSpec &arch)
Definition: ObjectContainerBSDArchive.h:153
lldb_private::FileSpec
Definition: FileSpec.h:56
ObjectContainerBSDArchive::Archive::m_arch
lldb_private::ArchSpec m_arch
Definition: ObjectContainerBSDArchive.h:162
lldb_private::DataExtractor
Definition: DataExtractor.h:48
ObjectContainerBSDArchive::Archive::GetData
lldb_private::DataExtractor & GetData()
Definition: ObjectContainerBSDArchive.h:157
ObjectContainerBSDArchive::Archive::ParseObjects
size_t ParseObjects()
Definition: ObjectContainerBSDArchive.cpp:147
ObjectContainerBSDArchive::Archive::shared_ptr
std::shared_ptr< Archive > shared_ptr
Definition: ObjectContainerBSDArchive.h:110
ObjectContainerBSDArchive::GetPluginVersion
uint32_t GetPluginVersion() override
Definition: ObjectContainerBSDArchive.cpp:443
ObjectContainerBSDArchive::Object::Extract
lldb::offset_t Extract(const lldb_private::DataExtractor &data, lldb::offset_t offset)
Definition: ObjectContainerBSDArchive.cpp:61
ObjectContainerBSDArchive::Archive::GetArchitecture
const lldb_private::ArchSpec & GetArchitecture() const
Definition: ObjectContainerBSDArchive.h:151
ObjectContainerBSDArchive::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: ObjectContainerBSDArchive.cpp:284
ObjectContainerBSDArchive::Archive::m_object_name_to_index_map
ObjectNameToIndexMap m_object_name_to_index_map
Definition: ObjectContainerBSDArchive.h:166
ObjectContainerBSDArchive::ParseHeader
bool ParseHeader() override
Attempts to parse the object header.
Definition: ObjectContainerBSDArchive.cpp:380
lldb_private::ConstString
Definition: ConstString.h:40
ObjectContainerBSDArchive::~ObjectContainerBSDArchive
~ObjectContainerBSDArchive() override
Definition: ObjectContainerBSDArchive.cpp:378
ObjectContainerBSDArchive::Archive::GetFileOffset
lldb::offset_t GetFileOffset() const
Definition: ObjectContainerBSDArchive.h:145
ObjectContainerBSDArchive::Archive::GetModificationTime
const llvm::sys::TimePoint & GetModificationTime()
Definition: ObjectContainerBSDArchive.h:147
lldb_private::UniqueCStringMap< uint32_t >
ObjectContainerBSDArchive::Object::Object
Object()
Definition: ObjectContainerBSDArchive.cpp:45
ObjectContainerBSDArchive::Archive::HasNoExternalReferences
bool HasNoExternalReferences() const
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)
Definition: ObjectContainerBSDArchive.cpp:368
ObjectContainerBSDArchive::Dump
void Dump(lldb_private::Stream *s) const override
Dump a description of this object to a Stream.
Definition: ObjectContainerBSDArchive.cpp:397
ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex
static std::recursive_mutex & GetArchiveCacheMutex()
Definition: ObjectContainerBSDArchive.cpp:264
ObjectContainerBSDArchive::Archive::Archive
Archive(const lldb_private::ArchSpec &arch, const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset, lldb_private::DataExtractor &data)
Definition: ObjectContainerBSDArchive.cpp:138
ObjectContainerBSDArchive::Object::ar_name
lldb_private::ConstString ar_name
Object name in the archive.
Definition: ObjectContainerBSDArchive.h:84
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:167
uint32_t
ObjectContainerBSDArchive::GetNumObjects
size_t GetNumObjects() const override
Get the number of objects within this object file (archives).
Definition: ObjectContainerBSDArchive.h:60
ObjectContainerBSDArchive::Initialize
static void Initialize()
Definition: ObjectContainerBSDArchive.cpp:269
ObjectContainerBSDArchive::m_archive_sp
Archive::shared_ptr m_archive_sp
Definition: ObjectContainerBSDArchive.h:174
ObjectContainerBSDArchive::Archive::ObjectNameToIndexMap
lldb_private::UniqueCStringMap< uint32_t > ObjectNameToIndexMap
Definition: ObjectContainerBSDArchive.h:160
ObjectContainerBSDArchive::Object::size
uint32_t size
Object size in bytes in the archive.
Definition: ObjectContainerBSDArchive.h:99
ArchSpec.h
uint16_t
ObjectContainerBSDArchive::Object::uid
uint16_t uid
Object user id in the archive.
Definition: ObjectContainerBSDArchive.h:90
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)
Definition: ObjectContainerBSDArchive.cpp:239
ObjectContainerBSDArchive::Object::gid
uint16_t gid
Object group id in the archive.
Definition: ObjectContainerBSDArchive.h:93
ObjectContainerBSDArchive::GetObjectFile
lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override
Selects an architecture in an object file.
Definition: ObjectContainerBSDArchive.cpp:421
ObjectContainerBSDArchive::Archive::Map
std::multimap< lldb_private::FileSpec, shared_ptr > Map
Definition: ObjectContainerBSDArchive.h:111
ObjectContainerBSDArchive::Object::file_size
lldb::offset_t file_size
Length of the object data.
Definition: ObjectContainerBSDArchive.h:105
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:102
FileSpec.h
ObjectContainerBSDArchive::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: ObjectContainerBSDArchive.cpp:279
ConstString.h
ObjectContainer.h
ObjectContainerBSDArchive::Archive::GetObjectAtIndex
const Object * GetObjectAtIndex(size_t idx)
Definition: ObjectContainerBSDArchive.h:134
ObjectContainerBSDArchive::Archive::~Archive
~Archive()
Definition: ObjectContainerBSDArchive.cpp:145
ObjectContainerBSDArchive::Object::mode
uint16_t mode
Object octal file permissions in the archive.
Definition: ObjectContainerBSDArchive.h:96
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:288