blob: ef27184b03f45c7f8bbccd0bc7cef900214e5b67 [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/ArchSpec.h"
Greg Clayton2540a8a2013-07-12 22:07:46 +000031#include "lldb/Core/DataBuffer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Core/Module.h"
Greg Clayton2540a8a2013-07-12 22:07:46 +000033#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/PluginManager.h"
Greg Claytonaecb12b2012-01-09 23:59:30 +000035#include "lldb/Core/Timer.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000036#include "lldb/Host/FileSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Symbol/ObjectFile.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000038#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043ObjectContainerBSDArchive::Object::Object()
44 : ar_name(), ar_date(0), ar_uid(0), ar_gid(0), ar_mode(0), ar_size(0),
45 ar_file_offset(0), ar_file_size(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047void ObjectContainerBSDArchive::Object::Clear() {
48 ar_name.Clear();
49 ar_date = 0;
50 ar_uid = 0;
51 ar_gid = 0;
52 ar_mode = 0;
53 ar_size = 0;
54 ar_file_offset = 0;
55 ar_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Greg Claytonc7bece562013-01-25 18:06:21 +000058lldb::offset_t
Kate Stoneb9c1b512016-09-06 20:57:50 +000059ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data,
60 lldb::offset_t offset) {
61 size_t ar_name_len = 0;
62 std::string str;
63 char *err;
Greg Clayton745b6682014-05-02 22:25:51 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 // File header
66 //
67 // The common format is as follows.
68 //
69 // Offset Length Name Format
70 // 0 16 File name ASCII right padded with spaces (no spaces
71 // allowed in file name)
72 // 16 12 File mod Decimal as cstring right padded with
73 // spaces
74 // 28 6 Owner ID Decimal as cstring right padded with
75 // spaces
76 // 34 6 Group ID Decimal as cstring right padded with
77 // spaces
78 // 40 8 File mode Octal as cstring right padded with
79 // spaces
80 // 48 10 File byte size Decimal as cstring right padded with
81 // spaces
82 // 58 2 File magic 0x60 0x0A
Greg Clayton745b6682014-05-02 22:25:51 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 // Make sure there is enough data for the file header and bail if not
85 if (!data.ValidOffsetForDataOfSize(offset, 60))
Greg Claytonc7bece562013-01-25 18:06:21 +000086 return LLDB_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 str.assign((const char *)data.GetData(&offset, 16), 16);
89 if (str.find("#1/") == 0) {
90 // If the name is longer than 16 bytes, or contains an embedded space
91 // then it will use this format where the length of the name is
92 // here and the name characters are after this header.
93 ar_name_len = strtoul(str.c_str() + 3, &err, 10);
94 } else {
95 // Strip off any trailing spaces.
96 const size_t last_pos = str.find_last_not_of(' ');
97 if (last_pos != std::string::npos) {
98 if (last_pos + 1 < 16)
99 str.erase(last_pos + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 ar_name.SetCString(str.c_str());
102 }
103
104 str.assign((const char *)data.GetData(&offset, 12), 12);
105 ar_date = strtoul(str.c_str(), &err, 10);
106
107 str.assign((const char *)data.GetData(&offset, 6), 6);
108 ar_uid = strtoul(str.c_str(), &err, 10);
109
110 str.assign((const char *)data.GetData(&offset, 6), 6);
111 ar_gid = strtoul(str.c_str(), &err, 10);
112
113 str.assign((const char *)data.GetData(&offset, 8), 8);
114 ar_mode = strtoul(str.c_str(), &err, 8);
115
116 str.assign((const char *)data.GetData(&offset, 10), 10);
117 ar_size = strtoul(str.c_str(), &err, 10);
118
119 str.assign((const char *)data.GetData(&offset, 2), 2);
120 if (str == ARFMAG) {
121 if (ar_name_len > 0) {
122 const void *ar_name_ptr = data.GetData(&offset, ar_name_len);
123 // Make sure there was enough data for the string value and bail if not
124 if (ar_name_ptr == NULL)
125 return LLDB_INVALID_OFFSET;
126 str.assign((const char *)ar_name_ptr, ar_name_len);
127 ar_name.SetCString(str.c_str());
128 }
129 ar_file_offset = offset;
130 ar_file_size = ar_size - ar_name_len;
131 return offset;
132 }
133 return LLDB_INVALID_OFFSET;
134}
135
136ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
Pavel Labath5cc79672016-11-09 15:05:45 +0000137 const llvm::sys::TimePoint<> &time,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 lldb::offset_t file_offset,
139 lldb_private::DataExtractor &data)
140 : m_arch(arch), m_time(time), m_file_offset(file_offset), m_objects(),
141 m_data(data) {}
142
143ObjectContainerBSDArchive::Archive::~Archive() {}
144
145size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
146 DataExtractor &data = m_data;
147 std::string str;
148 lldb::offset_t offset = 0;
149 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
150 if (str == ARMAG) {
151 Object obj;
152 do {
153 offset = obj.Extract(data, offset);
154 if (offset == LLDB_INVALID_OFFSET)
155 break;
156 size_t obj_idx = m_objects.size();
157 m_objects.push_back(obj);
158 // Insert all of the C strings out of order for now...
Zachary Turner4fa098a2016-10-06 21:22:44 +0000159 m_object_name_to_index_map.Append(obj.ar_name.GetStringRef(), obj_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 offset += obj.ar_file_size;
161 obj.Clear();
162 } while (data.ValidOffset(offset));
163
164 // Now sort all of the object name pointers
165 m_object_name_to_index_map.Sort();
166 }
167 return m_objects.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168}
169
170ObjectContainerBSDArchive::Object *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171ObjectContainerBSDArchive::Archive::FindObject(
Pavel Labath5cc79672016-11-09 15:05:45 +0000172 const ConstString &object_name,
173 const llvm::sys::TimePoint<> &object_mod_time) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 const ObjectNameToIndexMap::Entry *match =
175 m_object_name_to_index_map.FindFirstValueForName(
Zachary Turner4fa098a2016-10-06 21:22:44 +0000176 object_name.GetStringRef());
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);
205 // Don't cache a value for "archive_map.end()" below since we might
206 // delete an archive entry...
207 while (pos != archive_map.end() && pos->first == file) {
208 bool match = true;
209 if (arch.IsValid() &&
210 pos->second->GetArchitecture().IsCompatibleMatch(arch) == false)
211 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 {
219 // We have a file at the same path with the same architecture
220 // whose modification time doesn't match. It doesn't make sense
221 // for us to continue to use this BSD archive since we cache only
222 // the object info which consists of file time info and also the
223 // file offset and file size of any contained objects. Since
224 // this information is now out of date, we won't get the correct
225 // information if we go and extract the file data, so we should
226 // remove the old and outdated entry.
227 archive_map.erase(pos);
228 pos = archive_map.find(file);
229 continue; // Continue to next iteration so we don't increment pos
230 // below...
231 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 ++pos;
234 }
235 return archive_sp;
236}
237
238ObjectContainerBSDArchive::Archive::shared_ptr
239ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
Pavel Labath5cc79672016-11-09 15:05:45 +0000240 const FileSpec &file, const ArchSpec &arch,
241 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
242 DataExtractor &data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 shared_ptr archive_sp(new Archive(arch, time, file_offset, data));
244 if (archive_sp) {
245 const size_t num_objects = archive_sp->ParseObjects();
246 if (num_objects > 0) {
247 std::lock_guard<std::recursive_mutex> guard(
248 Archive::GetArchiveCacheMutex());
249 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
250 } else {
251 archive_sp.reset();
252 }
253 }
254 return archive_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
257ObjectContainerBSDArchive::Archive::Map &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258ObjectContainerBSDArchive::Archive::GetArchiveCache() {
259 static Archive::Map g_archive_map;
260 return g_archive_map;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261}
262
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000263std::recursive_mutex &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
265 static std::recursive_mutex g_archive_map_mutex;
266 return g_archive_map_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269void ObjectContainerBSDArchive::Initialize() {
270 PluginManager::RegisterPlugin(GetPluginNameStatic(),
271 GetPluginDescriptionStatic(), CreateInstance,
272 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273}
274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275void ObjectContainerBSDArchive::Terminate() {
276 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277}
278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() {
280 static ConstString g_name("bsd-archive");
281 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282}
283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() {
285 return "BSD Archive object container reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286}
287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288ObjectContainer *ObjectContainerBSDArchive::CreateInstance(
289 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
290 lldb::offset_t data_offset, const FileSpec *file,
291 lldb::offset_t file_offset, lldb::offset_t length) {
292 ConstString object_name(module_sp->GetObjectName());
293 if (object_name) {
294 if (data_sp) {
295 // We have data, which means this is the first 512 bytes of the file
296 // Check to see if the magic bytes match and if they do, read the entire
297 // table of contents for the archive and cache it
298 DataExtractor data;
299 data.SetData(data_sp, data_offset, length);
300 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
301 Timer scoped_timer(
302 LLVM_PRETTY_FUNCTION,
303 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
304 "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
305 module_sp->GetFileSpec().GetPath().c_str(),
306 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
307 static_cast<uint64_t>(length));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 // Map the entire .a file to be sure that we don't lose any data if the
310 // file
311 // gets updated by a new build while this .a file is being used for
312 // debugging
313 DataBufferSP archive_data_sp(
314 file->MemoryMapFileContentsIfLocal(file_offset, length));
315 lldb::offset_t archive_data_offset = 0;
Greg Clayton5ce9c562013-02-06 17:22:03 +0000316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
318 *file, module_sp->GetArchitecture(),
319 module_sp->GetModificationTime(), file_offset));
320 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
321 new ObjectContainerBSDArchive(module_sp, archive_data_sp,
322 archive_data_offset, file,
323 file_offset, length));
Greg Clayton5ce9c562013-02-06 17:22:03 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 if (container_ap.get()) {
326 if (archive_sp) {
327 // We already have this archive in our cache, use it
328 container_ap->SetArchive(archive_sp);
329 return container_ap.release();
330 } else if (container_ap->ParseHeader())
331 return container_ap.release();
Greg Clayton5ce9c562013-02-06 17:22:03 +0000332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 }
334 } else {
335 // No data, just check for a cached archive
336 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
337 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
338 file_offset));
339 if (archive_sp) {
340 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
341 new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
342 file_offset, length));
343
344 if (container_ap.get()) {
345 // We already have this archive in our cache, use it
346 container_ap->SetArchive(archive_sp);
347 return container_ap.release();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 }
352 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353}
354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
356 uint32_t offset = 0;
357 const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr));
358 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) {
359 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
360 if (strncmp(armag, ARFMAG, 2) == 0)
361 return true;
362 }
363 return false;
364}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366ObjectContainerBSDArchive::ObjectContainerBSDArchive(
367 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
368 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
369 lldb::offset_t file_offset, lldb::offset_t size)
370 : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
371 m_archive_sp() {}
372void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
373 m_archive_sp = archive_sp;
374}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {}
377
378bool ObjectContainerBSDArchive::ParseHeader() {
379 if (m_archive_sp.get() == NULL) {
380 if (m_data.GetByteSize() > 0) {
381 ModuleSP module_sp(GetModule());
382 if (module_sp) {
383 m_archive_sp = Archive::ParseAndCacheArchiveForFile(
384 m_file, module_sp->GetArchitecture(),
385 module_sp->GetModificationTime(), m_offset, m_data);
386 }
387 // Clear the m_data that contains the entire archive
388 // data and let our m_archive_sp hold onto the data.
389 m_data.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 }
392 return m_archive_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393}
394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395void ObjectContainerBSDArchive::Dump(Stream *s) const {
396 s->Printf("%p: ", static_cast<const void *>(this));
397 s->Indent();
398 const size_t num_archs = GetNumArchitectures();
399 const size_t num_objects = GetNumObjects();
400 s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64
401 ", num_objects = %" PRIu64 "",
402 (uint64_t)num_archs, (uint64_t)num_objects);
403 uint32_t i;
404 ArchSpec arch;
405 s->IndentMore();
406 for (i = 0; i < num_archs; i++) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 s->Indent();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 GetArchitectureAtIndex(i, arch);
409 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
410 }
411 for (i = 0; i < num_objects; i++) {
412 s->Indent();
413 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
414 }
415 s->IndentLess();
416 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417}
418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
420 ModuleSP module_sp(GetModule());
421 if (module_sp) {
422 if (module_sp->GetObjectName() && m_archive_sp) {
423 Object *object = m_archive_sp->FindObject(
424 module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
425 if (object) {
426 lldb::offset_t data_offset = object->ar_file_offset;
427 return ObjectFile::FindPlugin(
428 module_sp, file, m_offset + object->ar_file_offset,
429 object->ar_file_size, m_archive_sp->GetData().GetSharedDataBuffer(),
430 data_offset);
431 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 }
434 return ObjectFileSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435}
436
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437//------------------------------------------------------------------
438// PluginInterface protocol
439//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() {
441 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442}
443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446size_t ObjectContainerBSDArchive::GetModuleSpecifications(
447 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
448 lldb::offset_t data_offset, lldb::offset_t file_offset,
449 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
Greg Claytonf4d6de62013-04-24 22:29:28 +0000450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 // We have data, which means this is the first 512 bytes of the file
452 // Check to see if the magic bytes match and if they do, read the entire
453 // table of contents for the archive and cache it
454 DataExtractor data;
455 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
456 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
457 const size_t initial_count = specs.GetSize();
Pavel Labath5cc79672016-11-09 15:05:45 +0000458 llvm::sys::TimePoint<> file_mod_time =
459 FileSystem::GetModificationTime(file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
461 file, ArchSpec(), file_mod_time, file_offset));
462 bool set_archive_arch = false;
463 if (!archive_sp) {
464 set_archive_arch = true;
465 DataBufferSP data_sp(
466 file.MemoryMapFileContentsIfLocal(file_offset, file_size));
467 data.SetData(data_sp, 0, data_sp->GetByteSize());
468 archive_sp = Archive::ParseAndCacheArchiveForFile(
469 file, ArchSpec(), file_mod_time, file_offset, data);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000470 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471
472 if (archive_sp) {
473 const size_t num_objects = archive_sp->GetNumObjects();
474 for (size_t idx = 0; idx < num_objects; ++idx) {
475 const Object *object = archive_sp->GetObjectAtIndex(idx);
476 if (object) {
477 const lldb::offset_t object_file_offset =
478 file_offset + object->ar_file_offset;
479 if (object->ar_file_offset < file_size &&
480 file_size > object_file_offset) {
481 if (ObjectFile::GetModuleSpecifications(
482 file, object_file_offset, file_size - object_file_offset,
483 specs)) {
484 ModuleSpec &spec =
485 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
Pavel Labath5cc79672016-11-09 15:05:45 +0000486 llvm::sys::TimePoint<> object_mod_time(
487 std::chrono::seconds(object->ar_date));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 spec.GetObjectName() = object->ar_name;
489 spec.SetObjectOffset(object_file_offset);
490 spec.SetObjectSize(file_size - object_file_offset);
491 spec.GetObjectModificationTime() = object_mod_time;
492 }
493 }
494 }
495 }
496 }
497 const size_t end_count = specs.GetSize();
498 size_t num_specs_added = end_count - initial_count;
499 if (set_archive_arch && num_specs_added > 0) {
500 // The archive was created but we didn't have an architecture
501 // so we need to set it
502 for (size_t i = initial_count; i < end_count; ++i) {
503 ModuleSpec module_spec;
504 if (specs.GetModuleSpecAtIndex(i, module_spec)) {
505 if (module_spec.GetArchitecture().IsValid()) {
506 archive_sp->SetArchitecture(module_spec.GetArchitecture());
507 break;
508 }
509 }
510 }
511 }
512 return num_specs_added;
513 }
514 return 0;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000515}