blob: 391ab75469674534bb8cea331a230b588a42caed [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ObjectContainerBSDArchive.cpp ---------------------------*- 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#include "ObjectContainerBSDArchive.h"
11
Pavel Labathe705c8b2016-12-02 11:15:15 +000012#if defined(_WIN32) || defined(__ANDROID__)
Virgile Bellob2f1fb22013-08-23 12:44:05 +000013// Defines from ar, missing on Windows
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#define ARMAG "!<arch>\n"
15#define SARMAG 8
16#define ARFMAG "`\n"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000017
Kate Stoneb9c1b512016-09-06 20:57:50 +000018typedef struct ar_hdr {
19 char ar_name[16];
20 char ar_date[12];
21 char ar_uid[6], ar_gid[6];
22 char ar_mode[8];
23 char ar_size[10];
24 char ar_fmag[2];
Virgile Bellob2f1fb22013-08-23 12:44:05 +000025} ar_hdr;
26#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include <ar.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000028#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Module.h"
Greg Clayton2540a8a2013-07-12 22:07:46 +000031#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Core/PluginManager.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000033#include "lldb/Host/FileSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/ObjectFile.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000035#include "lldb/Utility/ArchSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000036#include "lldb/Utility/Stream.h"
Pavel Labath38d06322017-06-29 14:32:17 +000037#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
Zachary Turner3f4a4b32017-02-24 18:56:49 +000039#include "llvm/Support/MemoryBuffer.h"
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041using namespace lldb;
42using namespace lldb_private;
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044ObjectContainerBSDArchive::Object::Object()
45 : ar_name(), ar_date(0), ar_uid(0), ar_gid(0), ar_mode(0), ar_size(0),
46 ar_file_offset(0), ar_file_size(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048void ObjectContainerBSDArchive::Object::Clear() {
49 ar_name.Clear();
50 ar_date = 0;
51 ar_uid = 0;
52 ar_gid = 0;
53 ar_mode = 0;
54 ar_size = 0;
55 ar_file_offset = 0;
56 ar_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057}
58
Greg Claytonc7bece562013-01-25 18:06:21 +000059lldb::offset_t
Kate Stoneb9c1b512016-09-06 20:57:50 +000060ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data,
61 lldb::offset_t offset) {
62 size_t ar_name_len = 0;
63 std::string str;
64 char *err;
Greg Clayton745b6682014-05-02 22:25:51 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 // File header
67 //
68 // The common format is as follows.
69 //
70 // Offset Length Name Format
71 // 0 16 File name ASCII right padded with spaces (no spaces
72 // allowed in file name)
73 // 16 12 File mod Decimal as cstring right padded with
74 // spaces
75 // 28 6 Owner ID Decimal as cstring right padded with
76 // spaces
77 // 34 6 Group ID Decimal as cstring right padded with
78 // spaces
79 // 40 8 File mode Octal as cstring right padded with
80 // spaces
81 // 48 10 File byte size Decimal as cstring right padded with
82 // spaces
83 // 58 2 File magic 0x60 0x0A
Greg Clayton745b6682014-05-02 22:25:51 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 // Make sure there is enough data for the file header and bail if not
86 if (!data.ValidOffsetForDataOfSize(offset, 60))
Greg Claytonc7bece562013-01-25 18:06:21 +000087 return LLDB_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 str.assign((const char *)data.GetData(&offset, 16), 16);
90 if (str.find("#1/") == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +000091 // If the name is longer than 16 bytes, or contains an embedded space then
92 // it will use this format where the length of the name is here and the
93 // name characters are after this header.
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 ar_name_len = strtoul(str.c_str() + 3, &err, 10);
95 } else {
96 // Strip off any trailing spaces.
97 const size_t last_pos = str.find_last_not_of(' ');
98 if (last_pos != std::string::npos) {
99 if (last_pos + 1 < 16)
100 str.erase(last_pos + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 ar_name.SetCString(str.c_str());
103 }
104
105 str.assign((const char *)data.GetData(&offset, 12), 12);
106 ar_date = strtoul(str.c_str(), &err, 10);
107
108 str.assign((const char *)data.GetData(&offset, 6), 6);
109 ar_uid = strtoul(str.c_str(), &err, 10);
110
111 str.assign((const char *)data.GetData(&offset, 6), 6);
112 ar_gid = strtoul(str.c_str(), &err, 10);
113
114 str.assign((const char *)data.GetData(&offset, 8), 8);
115 ar_mode = strtoul(str.c_str(), &err, 8);
116
117 str.assign((const char *)data.GetData(&offset, 10), 10);
118 ar_size = strtoul(str.c_str(), &err, 10);
119
120 str.assign((const char *)data.GetData(&offset, 2), 2);
121 if (str == ARFMAG) {
122 if (ar_name_len > 0) {
123 const void *ar_name_ptr = data.GetData(&offset, ar_name_len);
124 // Make sure there was enough data for the string value and bail if not
125 if (ar_name_ptr == NULL)
126 return LLDB_INVALID_OFFSET;
127 str.assign((const char *)ar_name_ptr, ar_name_len);
128 ar_name.SetCString(str.c_str());
129 }
130 ar_file_offset = offset;
131 ar_file_size = ar_size - ar_name_len;
132 return offset;
133 }
134 return LLDB_INVALID_OFFSET;
135}
136
137ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
Pavel Labath5cc79672016-11-09 15:05:45 +0000138 const llvm::sys::TimePoint<> &time,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 lldb::offset_t file_offset,
140 lldb_private::DataExtractor &data)
141 : m_arch(arch), m_time(time), m_file_offset(file_offset), m_objects(),
142 m_data(data) {}
143
144ObjectContainerBSDArchive::Archive::~Archive() {}
145
146size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
147 DataExtractor &data = m_data;
148 std::string str;
149 lldb::offset_t offset = 0;
150 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
151 if (str == ARMAG) {
152 Object obj;
153 do {
154 offset = obj.Extract(data, offset);
155 if (offset == LLDB_INVALID_OFFSET)
156 break;
157 size_t obj_idx = m_objects.size();
158 m_objects.push_back(obj);
159 // Insert all of the C strings out of order for now...
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000160 m_object_name_to_index_map.Append(obj.ar_name, obj_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 offset += obj.ar_file_size;
162 obj.Clear();
163 } while (data.ValidOffset(offset));
164
165 // Now sort all of the object name pointers
166 m_object_name_to_index_map.Sort();
167 }
168 return m_objects.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169}
170
171ObjectContainerBSDArchive::Object *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172ObjectContainerBSDArchive::Archive::FindObject(
Pavel Labath5cc79672016-11-09 15:05:45 +0000173 const ConstString &object_name,
174 const llvm::sys::TimePoint<> &object_mod_time) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 const ObjectNameToIndexMap::Entry *match =
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000176 m_object_name_to_index_map.FindFirstValueForName(object_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 if (match) {
Pavel Labath5cc79672016-11-09 15:05:45 +0000178 if (object_mod_time != llvm::sys::TimePoint<>()) {
179 const uint64_t object_date = llvm::sys::toTimeT(object_mod_time);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 if (m_objects[match->value].ar_date == object_date)
181 return &m_objects[match->value];
182 const ObjectNameToIndexMap::Entry *next_match =
183 m_object_name_to_index_map.FindNextValueForName(match);
184 while (next_match) {
185 if (m_objects[next_match->value].ar_date == object_date)
186 return &m_objects[next_match->value];
187 next_match =
188 m_object_name_to_index_map.FindNextValueForName(next_match);
189 }
190 } else {
191 return &m_objects[match->value];
Greg Clayton57abc5d2013-05-10 21:47:16 +0000192 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 }
194 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195}
196
197ObjectContainerBSDArchive::Archive::shared_ptr
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198ObjectContainerBSDArchive::Archive::FindCachedArchive(
Pavel Labath5cc79672016-11-09 15:05:45 +0000199 const FileSpec &file, const ArchSpec &arch,
200 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex());
202 shared_ptr archive_sp;
203 Archive::Map &archive_map = Archive::GetArchiveCache();
204 Archive::Map::iterator pos = archive_map.find(file);
Adrian Prantl05097242018-04-30 16:49:04 +0000205 // Don't cache a value for "archive_map.end()" below since we might delete an
206 // archive entry...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 while (pos != archive_map.end() && pos->first == file) {
208 bool match = true;
209 if (arch.IsValid() &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000210 !pos->second->GetArchitecture().IsCompatibleMatch(arch))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 match = false;
212 else if (file_offset != LLDB_INVALID_OFFSET &&
213 pos->second->GetFileOffset() != file_offset)
214 match = false;
215 if (match) {
216 if (pos->second->GetModificationTime() == time) {
217 return pos->second;
218 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000219 // We have a file at the same path with the same architecture whose
220 // modification time doesn't match. It doesn't make sense for us to
221 // continue to use this BSD archive since we cache only the object info
222 // which consists of file time info and also the file offset and file
223 // size of any contained objects. Since this information is now out of
224 // date, we won't get the correct information if we go and extract the
225 // file data, so we should remove the old and outdated entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 archive_map.erase(pos);
227 pos = archive_map.find(file);
228 continue; // Continue to next iteration so we don't increment pos
229 // below...
230 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 ++pos;
233 }
234 return archive_sp;
235}
236
237ObjectContainerBSDArchive::Archive::shared_ptr
238ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
Pavel Labath5cc79672016-11-09 15:05:45 +0000239 const FileSpec &file, const ArchSpec &arch,
240 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
241 DataExtractor &data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 shared_ptr archive_sp(new Archive(arch, time, file_offset, data));
243 if (archive_sp) {
244 const size_t num_objects = archive_sp->ParseObjects();
245 if (num_objects > 0) {
246 std::lock_guard<std::recursive_mutex> guard(
247 Archive::GetArchiveCacheMutex());
248 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
249 } else {
250 archive_sp.reset();
251 }
252 }
253 return archive_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
256ObjectContainerBSDArchive::Archive::Map &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257ObjectContainerBSDArchive::Archive::GetArchiveCache() {
258 static Archive::Map g_archive_map;
259 return g_archive_map;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260}
261
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000262std::recursive_mutex &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
264 static std::recursive_mutex g_archive_map_mutex;
265 return g_archive_map_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266}
267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268void ObjectContainerBSDArchive::Initialize() {
269 PluginManager::RegisterPlugin(GetPluginNameStatic(),
270 GetPluginDescriptionStatic(), CreateInstance,
271 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272}
273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274void ObjectContainerBSDArchive::Terminate() {
275 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276}
277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() {
279 static ConstString g_name("bsd-archive");
280 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281}
282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() {
284 return "BSD Archive object container reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285}
286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287ObjectContainer *ObjectContainerBSDArchive::CreateInstance(
288 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
289 lldb::offset_t data_offset, const FileSpec *file,
290 lldb::offset_t file_offset, lldb::offset_t length) {
291 ConstString object_name(module_sp->GetObjectName());
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000292 if (!object_name)
293 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000295 if (data_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000296 // We have data, which means this is the first 512 bytes of the file Check
297 // to see if the magic bytes match and if they do, read the entire table of
298 // contents for the archive and cache it
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000299 DataExtractor data;
300 data.SetData(data_sp, data_offset, length);
301 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000302 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000303 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +0000304 func_cat,
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000305 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
306 "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
307 module_sp->GetFileSpec().GetPath().c_str(),
308 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
309 static_cast<uint64_t>(length));
Greg Clayton5ce9c562013-02-06 17:22:03 +0000310
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000311 // Map the entire .a file to be sure that we don't lose any data if the
312 // file gets updated by a new build while this .a file is being used for
313 // debugging
314 DataBufferSP archive_data_sp =
Jonas Devlieghere87e403a2018-11-12 21:24:50 +0000315 FileSystem::Instance().CreateDataBuffer(*file, length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000316 if (!archive_data_sp)
317 return nullptr;
Greg Clayton5ce9c562013-02-06 17:22:03 +0000318
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000319 lldb::offset_t archive_data_offset = 0;
320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
322 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
323 file_offset));
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000324 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
325 new ObjectContainerBSDArchive(module_sp, archive_data_sp,
326 archive_data_offset, file, file_offset,
327 length));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000329 if (container_ap.get()) {
330 if (archive_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 // We already have this archive in our cache, use it
332 container_ap->SetArchive(archive_sp);
333 return container_ap.release();
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000334 } else if (container_ap->ParseHeader())
335 return container_ap.release();
336 }
337 }
338 } else {
339 // No data, just check for a cached archive
340 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
341 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
342 file_offset));
343 if (archive_sp) {
344 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
345 new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
346 file_offset, length));
347
348 if (container_ap.get()) {
349 // We already have this archive in our cache, use it
350 container_ap->SetArchive(archive_sp);
351 return container_ap.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 }
355 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356}
357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
359 uint32_t offset = 0;
360 const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr));
361 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) {
362 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
363 if (strncmp(armag, ARFMAG, 2) == 0)
364 return true;
365 }
366 return false;
367}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369ObjectContainerBSDArchive::ObjectContainerBSDArchive(
370 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
371 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
372 lldb::offset_t file_offset, lldb::offset_t size)
373 : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
374 m_archive_sp() {}
375void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
376 m_archive_sp = archive_sp;
377}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {}
380
381bool ObjectContainerBSDArchive::ParseHeader() {
382 if (m_archive_sp.get() == NULL) {
383 if (m_data.GetByteSize() > 0) {
384 ModuleSP module_sp(GetModule());
385 if (module_sp) {
386 m_archive_sp = Archive::ParseAndCacheArchiveForFile(
387 m_file, module_sp->GetArchitecture(),
388 module_sp->GetModificationTime(), m_offset, m_data);
389 }
Adrian Prantl05097242018-04-30 16:49:04 +0000390 // Clear the m_data that contains the entire archive data and let our
391 // m_archive_sp hold onto the data.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 m_data.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 }
395 return m_archive_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398void ObjectContainerBSDArchive::Dump(Stream *s) const {
399 s->Printf("%p: ", static_cast<const void *>(this));
400 s->Indent();
401 const size_t num_archs = GetNumArchitectures();
402 const size_t num_objects = GetNumObjects();
403 s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64
404 ", num_objects = %" PRIu64 "",
405 (uint64_t)num_archs, (uint64_t)num_objects);
406 uint32_t i;
407 ArchSpec arch;
408 s->IndentMore();
409 for (i = 0; i < num_archs; i++) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 s->Indent();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 GetArchitectureAtIndex(i, arch);
412 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
413 }
414 for (i = 0; i < num_objects; i++) {
415 s->Indent();
416 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
417 }
418 s->IndentLess();
419 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420}
421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
423 ModuleSP module_sp(GetModule());
424 if (module_sp) {
425 if (module_sp->GetObjectName() && m_archive_sp) {
426 Object *object = m_archive_sp->FindObject(
427 module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
428 if (object) {
429 lldb::offset_t data_offset = object->ar_file_offset;
430 return ObjectFile::FindPlugin(
431 module_sp, file, m_offset + object->ar_file_offset,
432 object->ar_file_size, m_archive_sp->GetData().GetSharedDataBuffer(),
433 data_offset);
434 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 }
437 return ObjectFileSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438}
439
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440//------------------------------------------------------------------
441// PluginInterface protocol
442//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() {
444 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445}
446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449size_t ObjectContainerBSDArchive::GetModuleSpecifications(
450 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
451 lldb::offset_t data_offset, lldb::offset_t file_offset,
452 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
Greg Claytonf4d6de62013-04-24 22:29:28 +0000453
Adrian Prantl05097242018-04-30 16:49:04 +0000454 // We have data, which means this is the first 512 bytes of the file Check to
455 // see if the magic bytes match and if they do, read the entire table of
456 // contents for the archive and cache it
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 DataExtractor data;
458 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000459 if (!file || !data_sp || !ObjectContainerBSDArchive::MagicBytesMatch(data))
460 return 0;
461
462 const size_t initial_count = specs.GetSize();
Jonas Devlieghere46376962018-10-31 21:49:27 +0000463 llvm::sys::TimePoint<> file_mod_time = FileSystem::Instance().GetModificationTime(file);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000464 Archive::shared_ptr archive_sp(
465 Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
466 bool set_archive_arch = false;
467 if (!archive_sp) {
468 set_archive_arch = true;
Zachary Turner666cc0b2017-03-04 01:30:05 +0000469 data_sp =
Jonas Devlieghere87e403a2018-11-12 21:24:50 +0000470 FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000471 if (data_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472 data.SetData(data_sp, 0, data_sp->GetByteSize());
473 archive_sp = Archive::ParseAndCacheArchiveForFile(
474 file, ArchSpec(), file_mod_time, file_offset, data);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000475 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 }
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000477
478 if (archive_sp) {
479 const size_t num_objects = archive_sp->GetNumObjects();
480 for (size_t idx = 0; idx < num_objects; ++idx) {
481 const Object *object = archive_sp->GetObjectAtIndex(idx);
482 if (object) {
483 const lldb::offset_t object_file_offset =
484 file_offset + object->ar_file_offset;
485 if (object->ar_file_offset < file_size &&
486 file_size > object_file_offset) {
487 if (ObjectFile::GetModuleSpecifications(
488 file, object_file_offset, file_size - object_file_offset,
489 specs)) {
490 ModuleSpec &spec =
491 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
492 llvm::sys::TimePoint<> object_mod_time(
493 std::chrono::seconds(object->ar_date));
494 spec.GetObjectName() = object->ar_name;
495 spec.SetObjectOffset(object_file_offset);
496 spec.SetObjectSize(file_size - object_file_offset);
497 spec.GetObjectModificationTime() = object_mod_time;
498 }
499 }
500 }
501 }
502 }
503 const size_t end_count = specs.GetSize();
504 size_t num_specs_added = end_count - initial_count;
505 if (set_archive_arch && num_specs_added > 0) {
Adrian Prantl05097242018-04-30 16:49:04 +0000506 // The archive was created but we didn't have an architecture so we need to
507 // set it
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000508 for (size_t i = initial_count; i < end_count; ++i) {
509 ModuleSpec module_spec;
510 if (specs.GetModuleSpecAtIndex(i, module_spec)) {
511 if (module_spec.GetArchitecture().IsValid()) {
512 archive_sp->SetArchitecture(module_spec.GetArchitecture());
513 break;
514 }
515 }
516 }
517 }
518 return num_specs_added;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000519}