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 protected:
74  struct Object {
75  Object();
76 
77  void Clear();
78 
80  lldb::offset_t offset);
81  /// Object name in the archive.
83 
84  /// Object modification time in the archive.
86 
87  /// Object user id in the archive.
89 
90  /// Object group id in the archive.
92 
93  /// Object octal file permissions in the archive.
95 
96  /// Object size in bytes in the archive.
98 
99  /// File offset in bytes from the beginning of the file of the object data.
101 
102  /// Length of the object data.
104  };
105 
106  class Archive {
107  public:
108  typedef std::shared_ptr<Archive> shared_ptr;
109  typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map;
110 
111  Archive(const lldb_private::ArchSpec &arch,
112  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
114 
115  ~Archive();
116 
117  static Map &GetArchiveCache();
118 
119  static std::recursive_mutex &GetArchiveCacheMutex();
120 
122  const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
123  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset);
124 
126  const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
127  const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
129 
130  size_t GetNumObjects() const { return m_objects.size(); }
131 
132  const Object *GetObjectAtIndex(size_t idx) {
133  if (idx < m_objects.size())
134  return &m_objects[idx];
135  return nullptr;
136  }
137 
138  size_t ParseObjects();
139 
141  const llvm::sys::TimePoint<> &object_mod_time);
142 
144 
145  const llvm::sys::TimePoint<> &GetModificationTime() {
146  return m_modification_time;
147  }
148 
149  const lldb_private::ArchSpec &GetArchitecture() const { return m_arch; }
150 
151  void SetArchitecture(const lldb_private::ArchSpec &arch) { m_arch = arch; }
152 
153  bool HasNoExternalReferences() const;
154 
156 
157  protected:
159  // Member Variables
161  llvm::sys::TimePoint<> m_modification_time;
163  std::vector<Object> m_objects;
165  lldb_private::DataExtractor m_data; ///< The data for this object container
166  ///so we don't lose data if the .a files
167  ///gets modified
168  };
169 
170  void SetArchive(Archive::shared_ptr &archive_sp);
171 
173 };
174 
175 #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:85
lldb_private::ArchSpec
Definition: ArchSpec.h:33
ObjectContainerBSDArchive::Archive::GetNumObjects
size_t GetNumObjects() const
Definition: ObjectContainerBSDArchive.h:130
ObjectContainerBSDArchive::Archive::m_file_offset
lldb::offset_t m_file_offset
Definition: ObjectContainerBSDArchive.h:162
ObjectContainerBSDArchive::Archive
Definition: ObjectContainerBSDArchive.h:106
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:441
ObjectContainerBSDArchive::MagicBytesMatch
static bool MagicBytesMatch(const lldb_private::DataExtractor &data)
Definition: ObjectContainerBSDArchive.cpp:355
ObjectContainerBSDArchive::Archive::m_objects
std::vector< Object > m_objects
Definition: ObjectContainerBSDArchive.h:163
ObjectContainerBSDArchive::Object::Clear
void Clear()
Definition: ObjectContainerBSDArchive.cpp:47
lldb_private::ModuleSpecList
Definition: ModuleSpec.h:280
ObjectContainerBSDArchive::Terminate
static void Terminate()
Definition: ObjectContainerBSDArchive.cpp:273
lldb_private::ObjectContainer
Definition: ObjectContainer.h:30
ObjectContainerBSDArchive::SetArchive
void SetArchive(Archive::shared_ptr &archive_sp)
Definition: ObjectContainerBSDArchive.cpp:372
ObjectContainerBSDArchive
Definition: ObjectContainerBSDArchive.h:24
ObjectContainerBSDArchive::GetPluginName
lldb_private::ConstString GetPluginName() override
Definition: ObjectContainerBSDArchive.cpp:437
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:256
ObjectContainerBSDArchive::Object
Definition: ObjectContainerBSDArchive.h:74
ObjectContainerBSDArchive::Archive::FindObject
Object * FindObject(lldb_private::ConstString object_name, const llvm::sys::TimePoint<> &object_mod_time)
Definition: ObjectContainerBSDArchive.cpp:171
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:197
UniqueCStringMap.h
ObjectContainerBSDArchive::Archive::m_modification_time
llvm::sys::TimePoint m_modification_time
Definition: ObjectContainerBSDArchive.h:161
ObjectContainerBSDArchive::Archive::SetArchitecture
void SetArchitecture(const lldb_private::ArchSpec &arch)
Definition: ObjectContainerBSDArchive.h:151
lldb_private::FileSpec
Definition: FileSpec.h:56
ObjectContainerBSDArchive::Archive::m_arch
lldb_private::ArchSpec m_arch
Definition: ObjectContainerBSDArchive.h:160
lldb_private::DataExtractor
Definition: DataExtractor.h:48
ObjectContainerBSDArchive::Archive::GetData
lldb_private::DataExtractor & GetData()
Definition: ObjectContainerBSDArchive.h:155
ObjectContainerBSDArchive::Archive::ParseObjects
size_t ParseObjects()
Definition: ObjectContainerBSDArchive.cpp:145
ObjectContainerBSDArchive::Archive::shared_ptr
std::shared_ptr< Archive > shared_ptr
Definition: ObjectContainerBSDArchive.h:108
ObjectContainerBSDArchive::Object::Extract
lldb::offset_t Extract(const lldb_private::DataExtractor &data, lldb::offset_t offset)
Definition: ObjectContainerBSDArchive.cpp:59
ObjectContainerBSDArchive::Archive::GetArchitecture
const lldb_private::ArchSpec & GetArchitecture() const
Definition: ObjectContainerBSDArchive.h:149
ObjectContainerBSDArchive::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: ObjectContainerBSDArchive.cpp:282
ObjectContainerBSDArchive::Archive::m_object_name_to_index_map
ObjectNameToIndexMap m_object_name_to_index_map
Definition: ObjectContainerBSDArchive.h:164
ObjectContainerBSDArchive::ParseHeader
bool ParseHeader() override
Attempts to parse the object header.
Definition: ObjectContainerBSDArchive.cpp:378
lldb_private::ConstString
Definition: ConstString.h:40
ObjectContainerBSDArchive::~ObjectContainerBSDArchive
~ObjectContainerBSDArchive() override
ObjectContainerBSDArchive::Archive::GetFileOffset
lldb::offset_t GetFileOffset() const
Definition: ObjectContainerBSDArchive.h:143
ObjectContainerBSDArchive::Archive::GetModificationTime
const llvm::sys::TimePoint & GetModificationTime()
Definition: ObjectContainerBSDArchive.h:145
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:366
ObjectContainerBSDArchive::Dump
void Dump(lldb_private::Stream *s) const override
Dump a description of this object to a Stream.
Definition: ObjectContainerBSDArchive.cpp:395
ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex
static std::recursive_mutex & GetArchiveCacheMutex()
Definition: ObjectContainerBSDArchive.cpp:262
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:136
ObjectContainerBSDArchive::Object::ar_name
lldb_private::ConstString ar_name
Object name in the archive.
Definition: ObjectContainerBSDArchive.h:82
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:165
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:267
ObjectContainerBSDArchive::m_archive_sp
Archive::shared_ptr m_archive_sp
Definition: ObjectContainerBSDArchive.h:172
ObjectContainerBSDArchive::Archive::ObjectNameToIndexMap
lldb_private::UniqueCStringMap< uint32_t > ObjectNameToIndexMap
Definition: ObjectContainerBSDArchive.h:158
ObjectContainerBSDArchive::Object::size
uint32_t size
Object size in bytes in the archive.
Definition: ObjectContainerBSDArchive.h:97
ArchSpec.h
uint16_t
ObjectContainerBSDArchive::Object::uid
uint16_t uid
Object user id in the archive.
Definition: ObjectContainerBSDArchive.h:88
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:237
ObjectContainerBSDArchive::Object::gid
uint16_t gid
Object group id in the archive.
Definition: ObjectContainerBSDArchive.h:91
ObjectContainerBSDArchive::GetObjectFile
lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override
Selects an architecture in an object file.
Definition: ObjectContainerBSDArchive.cpp:419
ObjectContainerBSDArchive::Archive::Map
std::multimap< lldb_private::FileSpec, shared_ptr > Map
Definition: ObjectContainerBSDArchive.h:109
ObjectContainerBSDArchive::Object::file_size
lldb::offset_t file_size
Length of the object data.
Definition: ObjectContainerBSDArchive.h:103
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:100
FileSpec.h
ObjectContainerBSDArchive::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: ObjectContainerBSDArchive.cpp:277
ConstString.h
ObjectContainer.h
ObjectContainerBSDArchive::Archive::GetObjectAtIndex
const Object * GetObjectAtIndex(size_t idx)
Definition: ObjectContainerBSDArchive.h:132
ObjectContainerBSDArchive::Archive::~Archive
~Archive()
ObjectContainerBSDArchive::Object::mode
uint16_t mode
Object octal file permissions in the archive.
Definition: ObjectContainerBSDArchive.h:94
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:286