blob: 1ec5f3d733a03695f60c98b4898ad92b5206c79a [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
12#include <ar.h>
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/ArchSpec.h"
Greg Clayton2540a8a2013-07-12 22:07:46 +000015#include "lldb/Core/DataBuffer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Module.h"
Greg Clayton2540a8a2013-07-12 22:07:46 +000017#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/PluginManager.h"
Greg Clayton2540a8a2013-07-12 22:07:46 +000019#include "lldb/Core/Stream.h"
Greg Claytonaecb12b2012-01-09 23:59:30 +000020#include "lldb/Core/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Host/Mutex.h"
22#include "lldb/Symbol/ObjectFile.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27
28
29ObjectContainerBSDArchive::Object::Object() :
30 ar_name(),
31 ar_date(0),
32 ar_uid(0),
33 ar_gid(0),
34 ar_mode(0),
35 ar_size(0),
36 ar_file_offset(0),
37 ar_file_size(0)
38{
39}
40
41void
42ObjectContainerBSDArchive::Object::Clear()
43{
44 ar_name.Clear();
45 ar_date = 0;
46 ar_uid = 0;
47 ar_gid = 0;
48 ar_mode = 0;
49 ar_size = 0;
50 ar_file_offset = 0;
51 ar_file_size = 0;
52}
53
Greg Claytonc7bece562013-01-25 18:06:21 +000054lldb::offset_t
55ObjectContainerBSDArchive::Object::Extract (const DataExtractor& data, lldb::offset_t offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056{
57 size_t ar_name_len = 0;
58 std::string str;
59 char *err;
60 str.assign ((const char *)data.GetData(&offset, 16), 16);
Eli Friedman04e6ba72010-06-10 04:56:00 +000061 if (str.find("#1/") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 {
63 // If the name is longer than 16 bytes, or contains an embedded space
64 // then it will use this format where the length of the name is
65 // here and the name characters are after this header.
66 ar_name_len = strtoul(str.c_str() + 3, &err, 10);
67 }
68 else
69 {
70 // Strip off any spaces (if the object file name contains spaces it
71 // will use the extended format above).
72 str.erase (str.find(' '));
73 ar_name.SetCString(str.c_str());
74 }
75
76 str.assign ((const char *)data.GetData(&offset, 12), 12);
77 ar_date = strtoul(str.c_str(), &err, 10);
78
79 str.assign ((const char *)data.GetData(&offset, 6), 6);
80 ar_uid = strtoul(str.c_str(), &err, 10);
81
82 str.assign ((const char *)data.GetData(&offset, 6), 6);
83 ar_gid = strtoul(str.c_str(), &err, 10);
84
85 str.assign ((const char *)data.GetData(&offset, 8), 8);
86 ar_mode = strtoul(str.c_str(), &err, 8);
87
88 str.assign ((const char *)data.GetData(&offset, 10), 10);
89 ar_size = strtoul(str.c_str(), &err, 10);
90
91 str.assign ((const char *)data.GetData(&offset, 2), 2);
92 if (str == ARFMAG)
93 {
94 if (ar_name_len > 0)
95 {
96 str.assign ((const char *)data.GetData(&offset, ar_name_len), ar_name_len);
97 ar_name.SetCString (str.c_str());
98 }
99 ar_file_offset = offset;
100 ar_file_size = ar_size - ar_name_len;
101 return offset;
102 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000103 return LLDB_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104}
105
106ObjectContainerBSDArchive::Archive::Archive
107(
108 const lldb_private::ArchSpec &arch,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000109 const lldb_private::TimeValue &time,
Greg Clayton2540a8a2013-07-12 22:07:46 +0000110 lldb::offset_t file_offset,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000111 lldb_private::DataExtractor &data
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112) :
113 m_arch (arch),
114 m_time (time),
Greg Clayton2540a8a2013-07-12 22:07:46 +0000115 m_file_offset (file_offset),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000116 m_objects(),
117 m_data (data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118{
119}
120
121ObjectContainerBSDArchive::Archive::~Archive ()
122{
123}
124
125size_t
Greg Clayton5ce9c562013-02-06 17:22:03 +0000126ObjectContainerBSDArchive::Archive::ParseObjects ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000128 DataExtractor &data = m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 std::string str;
Greg Claytonc7bece562013-01-25 18:06:21 +0000130 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
132 if (str == ARMAG)
133 {
134 Object obj;
135 do
136 {
137 offset = obj.Extract (data, offset);
Greg Claytonc7bece562013-01-25 18:06:21 +0000138 if (offset == LLDB_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 break;
Greg Claytonc7bece562013-01-25 18:06:21 +0000140 size_t obj_idx = m_objects.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 m_objects.push_back(obj);
142 // Insert all of the C strings out of order for now...
143 m_object_name_to_index_map.Append (obj.ar_name.GetCString(), obj_idx);
144 offset += obj.ar_file_size;
145 obj.Clear();
146 } while (data.ValidOffset(offset));
147
148 // Now sort all of the object name pointers
149 m_object_name_to_index_map.Sort ();
150 }
151 return m_objects.size();
152}
153
154ObjectContainerBSDArchive::Object *
Greg Clayton57abc5d2013-05-10 21:47:16 +0000155ObjectContainerBSDArchive::Archive::FindObject (const ConstString &object_name, const TimeValue &object_mod_time)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156{
Greg Claytonc7bece562013-01-25 18:06:21 +0000157 const ObjectNameToIndexMap::Entry *match = m_object_name_to_index_map.FindFirstValueForName (object_name.GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 if (match)
Greg Clayton57abc5d2013-05-10 21:47:16 +0000159 {
160 if (object_mod_time.IsValid())
161 {
162 const uint64_t object_date = object_mod_time.GetAsSecondsSinceJan1_1970();
163 if (m_objects[match->value].ar_date == object_date)
164 return &m_objects[match->value];
165 const ObjectNameToIndexMap::Entry *next_match = m_object_name_to_index_map.FindNextValueForName (match);
166 while (next_match)
167 {
168 if (m_objects[next_match->value].ar_date == object_date)
169 return &m_objects[next_match->value];
170 next_match = m_object_name_to_index_map.FindNextValueForName (next_match);
171 }
172 }
173 else
174 {
175 return &m_objects[match->value];
176 }
177 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 return NULL;
179}
180
181
182ObjectContainerBSDArchive::Archive::shared_ptr
Greg Clayton2540a8a2013-07-12 22:07:46 +0000183ObjectContainerBSDArchive::Archive::FindCachedArchive (const FileSpec &file, const ArchSpec &arch, const TimeValue &time, lldb::offset_t file_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184{
185 Mutex::Locker locker(Archive::GetArchiveCacheMutex ());
186 shared_ptr archive_sp;
187 Archive::Map &archive_map = Archive::GetArchiveCache ();
Greg Clayton762f7132011-09-18 18:59:15 +0000188 Archive::Map::iterator pos = archive_map.find (file);
189 // Don't cache a value for "archive_map.end()" below since we might
190 // delete an archive entry...
191 while (pos != archive_map.end() && pos->first == file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 {
Greg Clayton2540a8a2013-07-12 22:07:46 +0000193 bool match = true;
194 if (arch.IsValid() && pos->second->GetArchitecture().IsCompatibleMatch(arch) == false)
195 match = false;
196 else if (file_offset != LLDB_INVALID_OFFSET && pos->second->GetFileOffset() != file_offset)
197 match = false;
198 if (match)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 {
Greg Clayton762f7132011-09-18 18:59:15 +0000200 if (pos->second->GetModificationTime() == time)
201 {
202 return pos->second;
203 }
204 else
205 {
206 // We have a file at the same path with the same architecture
207 // whose modification time doesn't match. It doesn't make sense
208 // for us to continue to use this BSD archive since we cache only
209 // the object info which consists of file time info and also the
210 // file offset and file size of any contianed objects. Since
211 // this information is now out of date, we won't get the correct
212 // information if we go and extract the file data, so we should
213 // remove the old and outdated entry.
214 archive_map.erase (pos);
215 pos = archive_map.find (file);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000216 continue; // Continue to next iteration so we don't increment pos below...
Greg Clayton762f7132011-09-18 18:59:15 +0000217 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218 }
Greg Clayton762f7132011-09-18 18:59:15 +0000219 ++pos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 }
221 return archive_sp;
222}
223
224ObjectContainerBSDArchive::Archive::shared_ptr
225ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile
226(
227 const FileSpec &file,
228 const ArchSpec &arch,
229 const TimeValue &time,
Greg Clayton2540a8a2013-07-12 22:07:46 +0000230 lldb::offset_t file_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231 DataExtractor &data
232)
233{
Greg Clayton2540a8a2013-07-12 22:07:46 +0000234 shared_ptr archive_sp(new Archive (arch, time, file_offset, data));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235 if (archive_sp)
236 {
Greg Clayton2540a8a2013-07-12 22:07:46 +0000237 const size_t num_objects = archive_sp->ParseObjects ();
238 if (num_objects > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 {
240 Mutex::Locker locker(Archive::GetArchiveCacheMutex ());
241 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
242 }
243 else
244 {
245 archive_sp.reset();
246 }
247 }
248 return archive_sp;
249}
250
251ObjectContainerBSDArchive::Archive::Map &
252ObjectContainerBSDArchive::Archive::GetArchiveCache ()
253{
254 static Archive::Map g_archive_map;
255 return g_archive_map;
256}
257
258Mutex &
259ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex ()
260{
261 static Mutex g_archive_map_mutex (Mutex::eMutexTypeRecursive);
262 return g_archive_map_mutex;
263}
264
265
266void
267ObjectContainerBSDArchive::Initialize()
268{
269 PluginManager::RegisterPlugin (GetPluginNameStatic(),
270 GetPluginDescriptionStatic(),
Greg Claytonf4d6de62013-04-24 22:29:28 +0000271 CreateInstance,
272 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273}
274
275void
276ObjectContainerBSDArchive::Terminate()
277{
278 PluginManager::UnregisterPlugin (CreateInstance);
279}
280
281
Greg Clayton57abc5d2013-05-10 21:47:16 +0000282lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283ObjectContainerBSDArchive::GetPluginNameStatic()
284{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000285 static ConstString g_name("bsd-archive");
286 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287}
288
289const char *
290ObjectContainerBSDArchive::GetPluginDescriptionStatic()
291{
292 return "BSD Archive object container reader.";
293}
294
295
296ObjectContainer *
297ObjectContainerBSDArchive::CreateInstance
298(
Greg Claytone72dfb32012-02-24 01:59:29 +0000299 const lldb::ModuleSP &module_sp,
Greg Claytonfad9eef2011-08-03 04:39:36 +0000300 DataBufferSP& data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000301 lldb::offset_t data_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 const FileSpec *file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000303 lldb::offset_t file_offset,
304 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000306 ConstString object_name (module_sp->GetObjectName());
307 if (object_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000309 if (data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000311 // We have data, which means this is the first 512 bytes of the file
312 // Check to see if the magic bytes match and if they do, read the entire
313 // table of contents for the archive and cache it
314 DataExtractor data;
315 data.SetData (data_sp, data_offset, length);
316 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data))
317 {
318 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000319 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = %p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
320 module_sp->GetFileSpec().GetPath().c_str(),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000321 file, (uint64_t) file_offset, (uint64_t) length);
322
323 // Map the entire .a file to be sure that we don't lose any data if the file
324 // gets updated by a new build while this .a file is being used for debugging
325 DataBufferSP archive_data_sp (file->MemoryMapFileContents(file_offset, length));
326 lldb::offset_t archive_data_offset = 0;
327
Greg Clayton2540a8a2013-07-12 22:07:46 +0000328 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file,
329 module_sp->GetArchitecture(),
330 module_sp->GetModificationTime(),
331 file_offset));
Greg Clayton7b0992d2013-04-18 22:45:39 +0000332 std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp,
Greg Clayton57abc5d2013-05-10 21:47:16 +0000333 archive_data_sp,
334 archive_data_offset,
335 file,
336 file_offset,
337 length));
Greg Clayton5ce9c562013-02-06 17:22:03 +0000338
339 if (container_ap.get())
340 {
341 if (archive_sp)
342 {
343 // We already have this archive in our cache, use it
344 container_ap->SetArchive (archive_sp);
345 return container_ap.release();
346 }
347 else if (container_ap->ParseHeader())
348 return container_ap.release();
349 }
350 }
351 }
352 else
353 {
354 // No data, just check for a cached archive
Greg Clayton2540a8a2013-07-12 22:07:46 +0000355 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file,
356 module_sp->GetArchitecture(),
357 module_sp->GetModificationTime(),
358 file_offset));
Greg Clayton44435ed2012-01-12 05:25:17 +0000359 if (archive_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000361 std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp, data_sp, data_offset, file, file_offset, length));
Greg Clayton5ce9c562013-02-06 17:22:03 +0000362
363 if (container_ap.get())
364 {
365 // We already have this archive in our cache, use it
366 container_ap->SetArchive (archive_sp);
367 return container_ap.release();
368 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 }
370 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 }
372 return NULL;
373}
374
375
376
377bool
Greg Clayton44435ed2012-01-12 05:25:17 +0000378ObjectContainerBSDArchive::MagicBytesMatch (const DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 uint32_t offset = 0;
381 const char* armag = (const char* )data.PeekData (offset, sizeof(ar_hdr));
382 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0)
383 {
384 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
385 if (strncmp(armag, ARFMAG, 2) == 0)
386 return true;
387 }
388 return false;
389}
390
391ObjectContainerBSDArchive::ObjectContainerBSDArchive
392(
Greg Claytone72dfb32012-02-24 01:59:29 +0000393 const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000394 DataBufferSP& data_sp,
395 lldb::offset_t data_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 const lldb_private::FileSpec *file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000397 lldb::offset_t file_offset,
398 lldb::offset_t size
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399) :
Greg Clayton5ce9c562013-02-06 17:22:03 +0000400 ObjectContainer (module_sp, file, file_offset, size, data_sp, data_offset),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 m_archive_sp ()
402{
403}
404void
405ObjectContainerBSDArchive::SetArchive (Archive::shared_ptr &archive_sp)
406{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000407 m_archive_sp = archive_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408}
409
410
411
412ObjectContainerBSDArchive::~ObjectContainerBSDArchive()
413{
414}
415
416bool
417ObjectContainerBSDArchive::ParseHeader ()
418{
419 if (m_archive_sp.get() == NULL)
420 {
421 if (m_data.GetByteSize() > 0)
422 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000423 ModuleSP module_sp (GetModule());
424 if (module_sp)
425 {
426 m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file,
427 module_sp->GetArchitecture(),
428 module_sp->GetModificationTime(),
Greg Clayton2540a8a2013-07-12 22:07:46 +0000429 m_offset,
Greg Claytone72dfb32012-02-24 01:59:29 +0000430 m_data);
431 }
Greg Clayton5ce9c562013-02-06 17:22:03 +0000432 // Clear the m_data that contains the entire archive
433 // data and let our m_archive_sp hold onto the data.
434 m_data.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 }
436 }
437 return m_archive_sp.get() != NULL;
438}
439
440void
441ObjectContainerBSDArchive::Dump (Stream *s) const
442{
Jason Molendafd54b362011-09-20 21:44:10 +0000443 s->Printf("%p: ", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 s->Indent();
445 const size_t num_archs = GetNumArchitectures();
446 const size_t num_objects = GetNumObjects();
Jason Molendafd54b362011-09-20 21:44:10 +0000447 s->Printf("ObjectContainerBSDArchive, num_archs = %lu, num_objects = %lu", num_archs, num_objects);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 uint32_t i;
449 ArchSpec arch;
450 s->IndentMore();
451 for (i=0; i<num_archs; i++)
452 {
453 s->Indent();
454 GetArchitectureAtIndex(i, arch);
Jason Molendafd54b362011-09-20 21:44:10 +0000455 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 }
457 for (i=0; i<num_objects; i++)
458 {
459 s->Indent();
Jason Molendafd54b362011-09-20 21:44:10 +0000460 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex (i));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 }
462 s->IndentLess();
463 s->EOL();
464}
465
Greg Clayton762f7132011-09-18 18:59:15 +0000466ObjectFileSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file)
468{
Greg Claytone72dfb32012-02-24 01:59:29 +0000469 ModuleSP module_sp (GetModule());
470 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000472 if (module_sp->GetObjectName() && m_archive_sp)
473 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000474 Object *object = m_archive_sp->FindObject (module_sp->GetObjectName(),
475 module_sp->GetObjectModificationTime());
Greg Claytone72dfb32012-02-24 01:59:29 +0000476 if (object)
Greg Clayton5ce9c562013-02-06 17:22:03 +0000477 {
Greg Clayton80e6c042013-05-21 23:36:34 +0000478 lldb::offset_t data_offset = object->ar_file_offset;
Greg Clayton5ce9c562013-02-06 17:22:03 +0000479 return ObjectFile::FindPlugin (module_sp,
Greg Claytone72dfb32012-02-24 01:59:29 +0000480 file,
Greg Clayton824e7c02012-11-15 19:37:18 +0000481 m_offset + object->ar_file_offset,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000482 object->ar_file_size,
483 m_archive_sp->GetData().GetSharedDataBuffer(),
484 data_offset);
485 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000486 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 }
Greg Clayton762f7132011-09-18 18:59:15 +0000488 return ObjectFileSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
490
491
492//------------------------------------------------------------------
493// PluginInterface protocol
494//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000495lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496ObjectContainerBSDArchive::GetPluginName()
497{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 return GetPluginNameStatic();
499}
500
501uint32_t
502ObjectContainerBSDArchive::GetPluginVersion()
503{
504 return 1;
505}
506
Greg Claytonf4d6de62013-04-24 22:29:28 +0000507
508size_t
509ObjectContainerBSDArchive::GetModuleSpecifications (const lldb_private::FileSpec& file,
510 lldb::DataBufferSP& data_sp,
511 lldb::offset_t data_offset,
512 lldb::offset_t file_offset,
Greg Clayton2540a8a2013-07-12 22:07:46 +0000513 lldb::offset_t file_size,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000514 lldb_private::ModuleSpecList &specs)
515{
Greg Clayton2540a8a2013-07-12 22:07:46 +0000516
517 // We have data, which means this is the first 512 bytes of the file
518 // Check to see if the magic bytes match and if they do, read the entire
519 // table of contents for the archive and cache it
520 DataExtractor data;
521 data.SetData (data_sp, data_offset, data_sp->GetByteSize());
522 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data))
523 {
524 const size_t initial_count = specs.GetSize();
525 TimeValue file_mod_time = file.GetModificationTime();
526 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (file, ArchSpec(), file_mod_time, file_offset));
527 bool set_archive_arch = false;
528 if (!archive_sp)
529 {
530 set_archive_arch = true;
531 DataBufferSP data_sp (file.MemoryMapFileContents(file_offset, file_size));
532 data.SetData (data_sp, 0, data_sp->GetByteSize());
533 archive_sp = Archive::ParseAndCacheArchiveForFile(file, ArchSpec(), file_mod_time, file_offset, data);
534 }
535
536 if (archive_sp)
537 {
538 const size_t num_objects = archive_sp->GetNumObjects();
539 for (size_t idx = 0; idx < num_objects; ++idx)
540 {
541 const Object *object = archive_sp->GetObjectAtIndex (idx);
542 if (object)
543 {
544 const lldb::offset_t object_file_offset = file_offset + object->ar_file_offset;
545 if (object->ar_file_offset < file_size && file_size > object_file_offset)
546 {
547 if (ObjectFile::GetModuleSpecifications(file,
548 object_file_offset,
549 file_size - object_file_offset,
550 specs))
551 {
552 ModuleSpec &spec = specs.GetModuleSpecRefAtIndex (specs.GetSize() - 1);
553 TimeValue object_mod_time;
554 object_mod_time.OffsetWithSeconds(object->ar_date);
555 spec.GetObjectName () = object->ar_name;
556 spec.SetObjectOffset(object_file_offset);
557 spec.GetObjectModificationTime () = object_mod_time;
558 }
559 }
560 }
561 }
562 }
563 const size_t end_count = specs.GetSize();
564 size_t num_specs_added = end_count - initial_count;
565 if (set_archive_arch && num_specs_added > 0)
566 {
567 // The archive was created but we didn't have an architecture
568 // so we need to set it
569 for (size_t i=initial_count; i<end_count; ++ i)
570 {
571 ModuleSpec module_spec;
572 if (specs.GetModuleSpecAtIndex(i, module_spec))
573 {
574 if (module_spec.GetArchitecture().IsValid())
575 {
576 archive_sp->SetArchitecture (module_spec.GetArchitecture());
577 break;
578 }
579 }
580 }
581 }
582 return num_specs_added;
583 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000584 return 0;
585}