blob: d7bef836d9d87064da01903e890fe78096484bbb [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 Turner666cc0b2017-03-04 01:30:05 +000036#include "lldb/Utility/DataBufferLLVM.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000037#include "lldb/Utility/Stream.h"
Pavel Labath38d06322017-06-29 14:32:17 +000038#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Zachary Turner3f4a4b32017-02-24 18:56:49 +000040#include "llvm/Support/MemoryBuffer.h"
41
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042using namespace lldb;
43using namespace lldb_private;
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045ObjectContainerBSDArchive::Object::Object()
46 : ar_name(), ar_date(0), ar_uid(0), ar_gid(0), ar_mode(0), ar_size(0),
47 ar_file_offset(0), ar_file_size(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049void ObjectContainerBSDArchive::Object::Clear() {
50 ar_name.Clear();
51 ar_date = 0;
52 ar_uid = 0;
53 ar_gid = 0;
54 ar_mode = 0;
55 ar_size = 0;
56 ar_file_offset = 0;
57 ar_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058}
59
Greg Claytonc7bece562013-01-25 18:06:21 +000060lldb::offset_t
Kate Stoneb9c1b512016-09-06 20:57:50 +000061ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data,
62 lldb::offset_t offset) {
63 size_t ar_name_len = 0;
64 std::string str;
65 char *err;
Greg Clayton745b6682014-05-02 22:25:51 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 // File header
68 //
69 // The common format is as follows.
70 //
71 // Offset Length Name Format
72 // 0 16 File name ASCII right padded with spaces (no spaces
73 // allowed in file name)
74 // 16 12 File mod Decimal as cstring right padded with
75 // spaces
76 // 28 6 Owner ID Decimal as cstring right padded with
77 // spaces
78 // 34 6 Group ID Decimal as cstring right padded with
79 // spaces
80 // 40 8 File mode Octal as cstring right padded with
81 // spaces
82 // 48 10 File byte size Decimal as cstring right padded with
83 // spaces
84 // 58 2 File magic 0x60 0x0A
Greg Clayton745b6682014-05-02 22:25:51 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 // Make sure there is enough data for the file header and bail if not
87 if (!data.ValidOffsetForDataOfSize(offset, 60))
Greg Claytonc7bece562013-01-25 18:06:21 +000088 return LLDB_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 str.assign((const char *)data.GetData(&offset, 16), 16);
91 if (str.find("#1/") == 0) {
92 // If the name is longer than 16 bytes, or contains an embedded space
93 // then it will use this format where the length of the name is
94 // here and the name characters are after this header.
95 ar_name_len = strtoul(str.c_str() + 3, &err, 10);
96 } else {
97 // Strip off any trailing spaces.
98 const size_t last_pos = str.find_last_not_of(' ');
99 if (last_pos != std::string::npos) {
100 if (last_pos + 1 < 16)
101 str.erase(last_pos + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 ar_name.SetCString(str.c_str());
104 }
105
106 str.assign((const char *)data.GetData(&offset, 12), 12);
107 ar_date = strtoul(str.c_str(), &err, 10);
108
109 str.assign((const char *)data.GetData(&offset, 6), 6);
110 ar_uid = strtoul(str.c_str(), &err, 10);
111
112 str.assign((const char *)data.GetData(&offset, 6), 6);
113 ar_gid = strtoul(str.c_str(), &err, 10);
114
115 str.assign((const char *)data.GetData(&offset, 8), 8);
116 ar_mode = strtoul(str.c_str(), &err, 8);
117
118 str.assign((const char *)data.GetData(&offset, 10), 10);
119 ar_size = strtoul(str.c_str(), &err, 10);
120
121 str.assign((const char *)data.GetData(&offset, 2), 2);
122 if (str == ARFMAG) {
123 if (ar_name_len > 0) {
124 const void *ar_name_ptr = data.GetData(&offset, ar_name_len);
125 // Make sure there was enough data for the string value and bail if not
126 if (ar_name_ptr == NULL)
127 return LLDB_INVALID_OFFSET;
128 str.assign((const char *)ar_name_ptr, ar_name_len);
129 ar_name.SetCString(str.c_str());
130 }
131 ar_file_offset = offset;
132 ar_file_size = ar_size - ar_name_len;
133 return offset;
134 }
135 return LLDB_INVALID_OFFSET;
136}
137
138ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
Pavel Labath5cc79672016-11-09 15:05:45 +0000139 const llvm::sys::TimePoint<> &time,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 lldb::offset_t file_offset,
141 lldb_private::DataExtractor &data)
142 : m_arch(arch), m_time(time), m_file_offset(file_offset), m_objects(),
143 m_data(data) {}
144
145ObjectContainerBSDArchive::Archive::~Archive() {}
146
147size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
148 DataExtractor &data = m_data;
149 std::string str;
150 lldb::offset_t offset = 0;
151 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
152 if (str == ARMAG) {
153 Object obj;
154 do {
155 offset = obj.Extract(data, offset);
156 if (offset == LLDB_INVALID_OFFSET)
157 break;
158 size_t obj_idx = m_objects.size();
159 m_objects.push_back(obj);
160 // Insert all of the C strings out of order for now...
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000161 m_object_name_to_index_map.Append(obj.ar_name, obj_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 offset += obj.ar_file_size;
163 obj.Clear();
164 } while (data.ValidOffset(offset));
165
166 // Now sort all of the object name pointers
167 m_object_name_to_index_map.Sort();
168 }
169 return m_objects.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170}
171
172ObjectContainerBSDArchive::Object *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173ObjectContainerBSDArchive::Archive::FindObject(
Pavel Labath5cc79672016-11-09 15:05:45 +0000174 const ConstString &object_name,
175 const llvm::sys::TimePoint<> &object_mod_time) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 const ObjectNameToIndexMap::Entry *match =
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000177 m_object_name_to_index_map.FindFirstValueForName(object_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 if (match) {
Pavel Labath5cc79672016-11-09 15:05:45 +0000179 if (object_mod_time != llvm::sys::TimePoint<>()) {
180 const uint64_t object_date = llvm::sys::toTimeT(object_mod_time);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 if (m_objects[match->value].ar_date == object_date)
182 return &m_objects[match->value];
183 const ObjectNameToIndexMap::Entry *next_match =
184 m_object_name_to_index_map.FindNextValueForName(match);
185 while (next_match) {
186 if (m_objects[next_match->value].ar_date == object_date)
187 return &m_objects[next_match->value];
188 next_match =
189 m_object_name_to_index_map.FindNextValueForName(next_match);
190 }
191 } else {
192 return &m_objects[match->value];
Greg Clayton57abc5d2013-05-10 21:47:16 +0000193 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 }
195 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
198ObjectContainerBSDArchive::Archive::shared_ptr
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199ObjectContainerBSDArchive::Archive::FindCachedArchive(
Pavel Labath5cc79672016-11-09 15:05:45 +0000200 const FileSpec &file, const ArchSpec &arch,
201 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex());
203 shared_ptr archive_sp;
204 Archive::Map &archive_map = Archive::GetArchiveCache();
205 Archive::Map::iterator pos = archive_map.find(file);
206 // Don't cache a value for "archive_map.end()" below since we might
207 // delete an archive entry...
208 while (pos != archive_map.end() && pos->first == file) {
209 bool match = true;
210 if (arch.IsValid() &&
211 pos->second->GetArchitecture().IsCompatibleMatch(arch) == false)
212 match = false;
213 else if (file_offset != LLDB_INVALID_OFFSET &&
214 pos->second->GetFileOffset() != file_offset)
215 match = false;
216 if (match) {
217 if (pos->second->GetModificationTime() == time) {
218 return pos->second;
219 } else {
220 // We have a file at the same path with the same architecture
221 // whose modification time doesn't match. It doesn't make sense
222 // for us to continue to use this BSD archive since we cache only
223 // the object info which consists of file time info and also the
224 // file offset and file size of any contained objects. Since
225 // this information is now out of date, we won't get the correct
226 // information if we go and extract the file data, so we should
227 // remove the old and outdated entry.
228 archive_map.erase(pos);
229 pos = archive_map.find(file);
230 continue; // Continue to next iteration so we don't increment pos
231 // below...
232 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 ++pos;
235 }
236 return archive_sp;
237}
238
239ObjectContainerBSDArchive::Archive::shared_ptr
240ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
Pavel Labath5cc79672016-11-09 15:05:45 +0000241 const FileSpec &file, const ArchSpec &arch,
242 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
243 DataExtractor &data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 shared_ptr archive_sp(new Archive(arch, time, file_offset, data));
245 if (archive_sp) {
246 const size_t num_objects = archive_sp->ParseObjects();
247 if (num_objects > 0) {
248 std::lock_guard<std::recursive_mutex> guard(
249 Archive::GetArchiveCacheMutex());
250 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
251 } else {
252 archive_sp.reset();
253 }
254 }
255 return archive_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256}
257
258ObjectContainerBSDArchive::Archive::Map &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259ObjectContainerBSDArchive::Archive::GetArchiveCache() {
260 static Archive::Map g_archive_map;
261 return g_archive_map;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000264std::recursive_mutex &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
266 static std::recursive_mutex g_archive_map_mutex;
267 return g_archive_map_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270void ObjectContainerBSDArchive::Initialize() {
271 PluginManager::RegisterPlugin(GetPluginNameStatic(),
272 GetPluginDescriptionStatic(), CreateInstance,
273 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274}
275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276void ObjectContainerBSDArchive::Terminate() {
277 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278}
279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() {
281 static ConstString g_name("bsd-archive");
282 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283}
284
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() {
286 return "BSD Archive object container reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287}
288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289ObjectContainer *ObjectContainerBSDArchive::CreateInstance(
290 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
291 lldb::offset_t data_offset, const FileSpec *file,
292 lldb::offset_t file_offset, lldb::offset_t length) {
293 ConstString object_name(module_sp->GetObjectName());
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000294 if (!object_name)
295 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000297 if (data_sp) {
298 // We have data, which means this is the first 512 bytes of the file
299 // Check to see if the magic bytes match and if they do, read the entire
300 // table of contents for the archive and cache it
301 DataExtractor data;
302 data.SetData(data_sp, data_offset, length);
303 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000304 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000305 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +0000306 func_cat,
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000307 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
308 "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
309 module_sp->GetFileSpec().GetPath().c_str(),
310 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
311 static_cast<uint64_t>(length));
Greg Clayton5ce9c562013-02-06 17:22:03 +0000312
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000313 // Map the entire .a file to be sure that we don't lose any data if the
314 // file gets updated by a new build while this .a file is being used for
315 // debugging
316 DataBufferSP archive_data_sp =
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000317 DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000318 if (!archive_data_sp)
319 return nullptr;
Greg Clayton5ce9c562013-02-06 17:22:03 +0000320
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000321 lldb::offset_t archive_data_offset = 0;
322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
324 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
325 file_offset));
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000326 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
327 new ObjectContainerBSDArchive(module_sp, archive_data_sp,
328 archive_data_offset, file, file_offset,
329 length));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000331 if (container_ap.get()) {
332 if (archive_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 // We already have this archive in our cache, use it
334 container_ap->SetArchive(archive_sp);
335 return container_ap.release();
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000336 } else if (container_ap->ParseHeader())
337 return container_ap.release();
338 }
339 }
340 } else {
341 // No data, just check for a cached archive
342 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
343 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
344 file_offset));
345 if (archive_sp) {
346 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
347 new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
348 file_offset, length));
349
350 if (container_ap.get()) {
351 // We already have this archive in our cache, use it
352 container_ap->SetArchive(archive_sp);
353 return container_ap.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 }
357 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
361 uint32_t offset = 0;
362 const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr));
363 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) {
364 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
365 if (strncmp(armag, ARFMAG, 2) == 0)
366 return true;
367 }
368 return false;
369}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371ObjectContainerBSDArchive::ObjectContainerBSDArchive(
372 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
373 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
374 lldb::offset_t file_offset, lldb::offset_t size)
375 : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
376 m_archive_sp() {}
377void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
378 m_archive_sp = archive_sp;
379}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {}
382
383bool ObjectContainerBSDArchive::ParseHeader() {
384 if (m_archive_sp.get() == NULL) {
385 if (m_data.GetByteSize() > 0) {
386 ModuleSP module_sp(GetModule());
387 if (module_sp) {
388 m_archive_sp = Archive::ParseAndCacheArchiveForFile(
389 m_file, module_sp->GetArchitecture(),
390 module_sp->GetModificationTime(), m_offset, m_data);
391 }
392 // Clear the m_data that contains the entire archive
393 // data and let our m_archive_sp hold onto the data.
394 m_data.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 }
397 return m_archive_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398}
399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400void ObjectContainerBSDArchive::Dump(Stream *s) const {
401 s->Printf("%p: ", static_cast<const void *>(this));
402 s->Indent();
403 const size_t num_archs = GetNumArchitectures();
404 const size_t num_objects = GetNumObjects();
405 s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64
406 ", num_objects = %" PRIu64 "",
407 (uint64_t)num_archs, (uint64_t)num_objects);
408 uint32_t i;
409 ArchSpec arch;
410 s->IndentMore();
411 for (i = 0; i < num_archs; i++) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 s->Indent();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 GetArchitectureAtIndex(i, arch);
414 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
415 }
416 for (i = 0; i < num_objects; i++) {
417 s->Indent();
418 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
419 }
420 s->IndentLess();
421 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422}
423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
425 ModuleSP module_sp(GetModule());
426 if (module_sp) {
427 if (module_sp->GetObjectName() && m_archive_sp) {
428 Object *object = m_archive_sp->FindObject(
429 module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
430 if (object) {
431 lldb::offset_t data_offset = object->ar_file_offset;
432 return ObjectFile::FindPlugin(
433 module_sp, file, m_offset + object->ar_file_offset,
434 object->ar_file_size, m_archive_sp->GetData().GetSharedDataBuffer(),
435 data_offset);
436 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 }
439 return ObjectFileSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440}
441
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442//------------------------------------------------------------------
443// PluginInterface protocol
444//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() {
446 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451size_t ObjectContainerBSDArchive::GetModuleSpecifications(
452 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
453 lldb::offset_t data_offset, lldb::offset_t file_offset,
454 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
Greg Claytonf4d6de62013-04-24 22:29:28 +0000455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 // We have data, which means this is the first 512 bytes of the file
457 // Check to see if the magic bytes match and if they do, read the entire
458 // table of contents for the archive and cache it
459 DataExtractor data;
460 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000461 if (!file || !data_sp || !ObjectContainerBSDArchive::MagicBytesMatch(data))
462 return 0;
463
464 const size_t initial_count = specs.GetSize();
465 llvm::sys::TimePoint<> file_mod_time = FileSystem::GetModificationTime(file);
466 Archive::shared_ptr archive_sp(
467 Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
468 bool set_archive_arch = false;
469 if (!archive_sp) {
470 set_archive_arch = true;
Zachary Turner666cc0b2017-03-04 01:30:05 +0000471 data_sp =
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000472 DataBufferLLVM::CreateSliceFromPath(file.GetPath(), file_size, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000473 if (data_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 data.SetData(data_sp, 0, data_sp->GetByteSize());
475 archive_sp = Archive::ParseAndCacheArchiveForFile(
476 file, ArchSpec(), file_mod_time, file_offset, data);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000477 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 }
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000479
480 if (archive_sp) {
481 const size_t num_objects = archive_sp->GetNumObjects();
482 for (size_t idx = 0; idx < num_objects; ++idx) {
483 const Object *object = archive_sp->GetObjectAtIndex(idx);
484 if (object) {
485 const lldb::offset_t object_file_offset =
486 file_offset + object->ar_file_offset;
487 if (object->ar_file_offset < file_size &&
488 file_size > object_file_offset) {
489 if (ObjectFile::GetModuleSpecifications(
490 file, object_file_offset, file_size - object_file_offset,
491 specs)) {
492 ModuleSpec &spec =
493 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
494 llvm::sys::TimePoint<> object_mod_time(
495 std::chrono::seconds(object->ar_date));
496 spec.GetObjectName() = object->ar_name;
497 spec.SetObjectOffset(object_file_offset);
498 spec.SetObjectSize(file_size - object_file_offset);
499 spec.GetObjectModificationTime() = object_mod_time;
500 }
501 }
502 }
503 }
504 }
505 const size_t end_count = specs.GetSize();
506 size_t num_specs_added = end_count - initial_count;
507 if (set_archive_arch && num_specs_added > 0) {
508 // The archive was created but we didn't have an architecture
509 // so we need to set it
510 for (size_t i = initial_count; i < end_count; ++i) {
511 ModuleSpec module_spec;
512 if (specs.GetModuleSpecAtIndex(i, module_spec)) {
513 if (module_spec.GetArchitecture().IsValid()) {
514 archive_sp->SetArchitecture(module_spec.GetArchitecture());
515 break;
516 }
517 }
518 }
519 }
520 return num_specs_added;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000521}