1 //===-- ObjectContainerBSDArchive.h -----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_ObjectContainerBSDArchive_h_
11 #define liblldb_ObjectContainerBSDArchive_h_
12 
13 // Project includes
14 #include "lldb/Core/ArchSpec.h"
15 #include "lldb/Core/UniqueCStringMap.h"
16 #include "lldb/Symbol/ObjectContainer.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/Utility/FileSpec.h"
19 
20 // Other libraries and framework includes
21 #include "llvm/Support/Chrono.h"
22 
23 // C Includes
24 // C++ Includes
25 #include <map>
26 #include <memory>
27 #include <mutex>
28 
29 class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
30 public:
31   ObjectContainerBSDArchive(const lldb::ModuleSP &module_sp,
32                             lldb::DataBufferSP &data_sp,
33                             lldb::offset_t data_offset,
34                             const lldb_private::FileSpec *file,
35                             lldb::offset_t offset, lldb::offset_t length);
36 
37   ~ObjectContainerBSDArchive() override;
38 
39   //------------------------------------------------------------------
40   // Static Functions
41   //------------------------------------------------------------------
42   static void Initialize();
43 
44   static void Terminate();
45 
46   static lldb_private::ConstString GetPluginNameStatic();
47 
48   static const char *GetPluginDescriptionStatic();
49 
50   static lldb_private::ObjectContainer *
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,
60                                         lldb_private::ModuleSpecList &specs);
61 
62   static bool MagicBytesMatch(const lldb_private::DataExtractor &data);
63 
64   //------------------------------------------------------------------
65   // Member Functions
66   //------------------------------------------------------------------
67   bool ParseHeader() override;
68 
69   size_t GetNumObjects() const override {
70     if (m_archive_sp)
71       return m_archive_sp->GetNumObjects();
72     return 0;
73   }
74 
75   void Dump(lldb_private::Stream *s) const override;
76 
77   lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override;
78 
79   //------------------------------------------------------------------
80   // PluginInterface protocol
81   //------------------------------------------------------------------
82   lldb_private::ConstString GetPluginName() override;
83 
84   uint32_t GetPluginVersion() override;
85 
86 protected:
87   struct Object {
88     Object();
89 
90     void Clear();
91 
92     lldb::offset_t Extract(const lldb_private::DataExtractor &data,
93                            lldb::offset_t offset);
94 
95     lldb_private::ConstString ar_name; // name
96     uint32_t ar_date;                  // modification time
97     uint16_t ar_uid;                   // user id
98     uint16_t ar_gid;                   // group id
99     uint16_t ar_mode;                  // octal file permissions
100     uint32_t ar_size;                  // size in bytes
101     lldb::offset_t ar_file_offset; // file offset in bytes from the beginning of
102                                    // the file of the object data
103     lldb::offset_t ar_file_size;   // length of the object data
104 
105     typedef std::vector<Object> collection;
106     typedef collection::iterator iterator;
107     typedef collection::const_iterator const_iterator;
108   };
109 
110   class Archive {
111   public:
112     typedef std::shared_ptr<Archive> shared_ptr;
113     typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map;
114 
115     Archive(const lldb_private::ArchSpec &arch,
116             const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
117             lldb_private::DataExtractor &data);
118 
119     ~Archive();
120 
121     static Map &GetArchiveCache();
122 
123     static std::recursive_mutex &GetArchiveCacheMutex();
124 
125     static Archive::shared_ptr FindCachedArchive(
126         const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
127         const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset);
128 
129     static Archive::shared_ptr ParseAndCacheArchiveForFile(
130         const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
131         const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
132         lldb_private::DataExtractor &data);
133 
134     size_t GetNumObjects() const { return m_objects.size(); }
135 
136     const Object *GetObjectAtIndex(size_t idx) {
137       if (idx < m_objects.size())
138         return &m_objects[idx];
139       return NULL;
140     }
141 
142     size_t ParseObjects();
143 
144     Object *FindObject(const lldb_private::ConstString &object_name,
145                        const llvm::sys::TimePoint<> &object_mod_time);
146 
147     lldb::offset_t GetFileOffset() const { return m_file_offset; }
148 
149     const llvm::sys::TimePoint<> &GetModificationTime() { return m_time; }
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 
157     lldb_private::DataExtractor &GetData() { return m_data; }
158 
159   protected:
160     typedef lldb_private::UniqueCStringMap<uint32_t> ObjectNameToIndexMap;
161     //----------------------------------------------------------------------
162     // Member Variables
163     //----------------------------------------------------------------------
164     lldb_private::ArchSpec m_arch;
165     llvm::sys::TimePoint<> m_time;
166     lldb::offset_t m_file_offset;
167     Object::collection m_objects;
168     ObjectNameToIndexMap m_object_name_to_index_map;
169     lldb_private::DataExtractor m_data; ///< The data for this object container
170                                         ///so we don't lose data if the .a files
171                                         ///gets modified
172   };
173 
174   void SetArchive(Archive::shared_ptr &archive_sp);
175 
176   Archive::shared_ptr m_archive_sp;
177 };
178 
179 #endif // liblldb_ObjectContainerBSDArchive_h_
180