blob: a5e87fc358db42a2208ab6bb938ee1c368780245 [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
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/Module.h"
17#include "lldb/Core/PluginManager.h"
Greg Claytonaecb12b2012-01-09 23:59:30 +000018#include "lldb/Core/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Host/Mutex.h"
20#include "lldb/Symbol/ObjectFile.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25
26
27ObjectContainerBSDArchive::Object::Object() :
28 ar_name(),
29 ar_date(0),
30 ar_uid(0),
31 ar_gid(0),
32 ar_mode(0),
33 ar_size(0),
34 ar_file_offset(0),
35 ar_file_size(0)
36{
37}
38
39void
40ObjectContainerBSDArchive::Object::Clear()
41{
42 ar_name.Clear();
43 ar_date = 0;
44 ar_uid = 0;
45 ar_gid = 0;
46 ar_mode = 0;
47 ar_size = 0;
48 ar_file_offset = 0;
49 ar_file_size = 0;
50}
51
Greg Claytonc7bece562013-01-25 18:06:21 +000052lldb::offset_t
53ObjectContainerBSDArchive::Object::Extract (const DataExtractor& data, lldb::offset_t offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054{
55 size_t ar_name_len = 0;
56 std::string str;
57 char *err;
58 str.assign ((const char *)data.GetData(&offset, 16), 16);
Eli Friedman04e6ba72010-06-10 04:56:00 +000059 if (str.find("#1/") == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 {
61 // If the name is longer than 16 bytes, or contains an embedded space
62 // then it will use this format where the length of the name is
63 // here and the name characters are after this header.
64 ar_name_len = strtoul(str.c_str() + 3, &err, 10);
65 }
66 else
67 {
68 // Strip off any spaces (if the object file name contains spaces it
69 // will use the extended format above).
70 str.erase (str.find(' '));
71 ar_name.SetCString(str.c_str());
72 }
73
74 str.assign ((const char *)data.GetData(&offset, 12), 12);
75 ar_date = strtoul(str.c_str(), &err, 10);
76
77 str.assign ((const char *)data.GetData(&offset, 6), 6);
78 ar_uid = strtoul(str.c_str(), &err, 10);
79
80 str.assign ((const char *)data.GetData(&offset, 6), 6);
81 ar_gid = strtoul(str.c_str(), &err, 10);
82
83 str.assign ((const char *)data.GetData(&offset, 8), 8);
84 ar_mode = strtoul(str.c_str(), &err, 8);
85
86 str.assign ((const char *)data.GetData(&offset, 10), 10);
87 ar_size = strtoul(str.c_str(), &err, 10);
88
89 str.assign ((const char *)data.GetData(&offset, 2), 2);
90 if (str == ARFMAG)
91 {
92 if (ar_name_len > 0)
93 {
94 str.assign ((const char *)data.GetData(&offset, ar_name_len), ar_name_len);
95 ar_name.SetCString (str.c_str());
96 }
97 ar_file_offset = offset;
98 ar_file_size = ar_size - ar_name_len;
99 return offset;
100 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000101 return LLDB_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102}
103
104ObjectContainerBSDArchive::Archive::Archive
105(
106 const lldb_private::ArchSpec &arch,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000107 const lldb_private::TimeValue &time,
108 lldb_private::DataExtractor &data
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109) :
110 m_arch (arch),
111 m_time (time),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000112 m_objects(),
113 m_data (data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114{
115}
116
117ObjectContainerBSDArchive::Archive::~Archive ()
118{
119}
120
121size_t
Greg Clayton5ce9c562013-02-06 17:22:03 +0000122ObjectContainerBSDArchive::Archive::ParseObjects ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000124 DataExtractor &data = m_data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125 std::string str;
Greg Claytonc7bece562013-01-25 18:06:21 +0000126 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
128 if (str == ARMAG)
129 {
130 Object obj;
131 do
132 {
133 offset = obj.Extract (data, offset);
Greg Claytonc7bece562013-01-25 18:06:21 +0000134 if (offset == LLDB_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 break;
Greg Claytonc7bece562013-01-25 18:06:21 +0000136 size_t obj_idx = m_objects.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 m_objects.push_back(obj);
138 // Insert all of the C strings out of order for now...
139 m_object_name_to_index_map.Append (obj.ar_name.GetCString(), obj_idx);
140 offset += obj.ar_file_size;
141 obj.Clear();
142 } while (data.ValidOffset(offset));
143
144 // Now sort all of the object name pointers
145 m_object_name_to_index_map.Sort ();
146 }
147 return m_objects.size();
148}
149
150ObjectContainerBSDArchive::Object *
151ObjectContainerBSDArchive::Archive::FindObject (const ConstString &object_name)
152{
Greg Claytonc7bece562013-01-25 18:06:21 +0000153 const ObjectNameToIndexMap::Entry *match = m_object_name_to_index_map.FindFirstValueForName (object_name.GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 if (match)
155 return &m_objects[match->value];
156 return NULL;
157}
158
159
160ObjectContainerBSDArchive::Archive::shared_ptr
161ObjectContainerBSDArchive::Archive::FindCachedArchive (const FileSpec &file, const ArchSpec &arch, const TimeValue &time)
162{
163 Mutex::Locker locker(Archive::GetArchiveCacheMutex ());
164 shared_ptr archive_sp;
165 Archive::Map &archive_map = Archive::GetArchiveCache ();
Greg Clayton762f7132011-09-18 18:59:15 +0000166 Archive::Map::iterator pos = archive_map.find (file);
167 // Don't cache a value for "archive_map.end()" below since we might
168 // delete an archive entry...
169 while (pos != archive_map.end() && pos->first == file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000171 if (pos->second->GetArchitecture().IsCompatibleMatch(arch))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 {
Greg Clayton762f7132011-09-18 18:59:15 +0000173 if (pos->second->GetModificationTime() == time)
174 {
175 return pos->second;
176 }
177 else
178 {
179 // We have a file at the same path with the same architecture
180 // whose modification time doesn't match. It doesn't make sense
181 // for us to continue to use this BSD archive since we cache only
182 // the object info which consists of file time info and also the
183 // file offset and file size of any contianed objects. Since
184 // this information is now out of date, we won't get the correct
185 // information if we go and extract the file data, so we should
186 // remove the old and outdated entry.
187 archive_map.erase (pos);
188 pos = archive_map.find (file);
189 continue;
190 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 }
Greg Clayton762f7132011-09-18 18:59:15 +0000192 ++pos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 }
194 return archive_sp;
195}
196
197ObjectContainerBSDArchive::Archive::shared_ptr
198ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile
199(
200 const FileSpec &file,
201 const ArchSpec &arch,
202 const TimeValue &time,
203 DataExtractor &data
204)
205{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000206 shared_ptr archive_sp(new Archive (arch, time, data));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 if (archive_sp)
208 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000209 if (archive_sp->ParseObjects () > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 {
211 Mutex::Locker locker(Archive::GetArchiveCacheMutex ());
212 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
213 }
214 else
215 {
216 archive_sp.reset();
217 }
218 }
219 return archive_sp;
220}
221
222ObjectContainerBSDArchive::Archive::Map &
223ObjectContainerBSDArchive::Archive::GetArchiveCache ()
224{
225 static Archive::Map g_archive_map;
226 return g_archive_map;
227}
228
229Mutex &
230ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex ()
231{
232 static Mutex g_archive_map_mutex (Mutex::eMutexTypeRecursive);
233 return g_archive_map_mutex;
234}
235
236
237void
238ObjectContainerBSDArchive::Initialize()
239{
240 PluginManager::RegisterPlugin (GetPluginNameStatic(),
241 GetPluginDescriptionStatic(),
Greg Claytonf4d6de62013-04-24 22:29:28 +0000242 CreateInstance,
243 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244}
245
246void
247ObjectContainerBSDArchive::Terminate()
248{
249 PluginManager::UnregisterPlugin (CreateInstance);
250}
251
252
253const char *
254ObjectContainerBSDArchive::GetPluginNameStatic()
255{
256 return "object-container.bsd-archive";
257}
258
259const char *
260ObjectContainerBSDArchive::GetPluginDescriptionStatic()
261{
262 return "BSD Archive object container reader.";
263}
264
265
266ObjectContainer *
267ObjectContainerBSDArchive::CreateInstance
268(
Greg Claytone72dfb32012-02-24 01:59:29 +0000269 const lldb::ModuleSP &module_sp,
Greg Claytonfad9eef2011-08-03 04:39:36 +0000270 DataBufferSP& data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000271 lldb::offset_t data_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 const FileSpec *file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000273 lldb::offset_t file_offset,
274 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000276 ConstString object_name (module_sp->GetObjectName());
277 if (object_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000279 if (data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000281 // We have data, which means this is the first 512 bytes of the file
282 // Check to see if the magic bytes match and if they do, read the entire
283 // table of contents for the archive and cache it
284 DataExtractor data;
285 data.SetData (data_sp, data_offset, length);
286 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data))
287 {
288 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000289 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = %p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
290 module_sp->GetFileSpec().GetPath().c_str(),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000291 file, (uint64_t) file_offset, (uint64_t) length);
292
293 // Map the entire .a file to be sure that we don't lose any data if the file
294 // gets updated by a new build while this .a file is being used for debugging
295 DataBufferSP archive_data_sp (file->MemoryMapFileContents(file_offset, length));
296 lldb::offset_t archive_data_offset = 0;
297
298 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, module_sp->GetArchitecture(), module_sp->GetModificationTime()));
Greg Clayton7b0992d2013-04-18 22:45:39 +0000299 std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp,
Greg Claytone01e07b2013-04-18 18:10:51 +0000300 archive_data_sp,
301 archive_data_offset,
302 file,
303 file_offset,
304 length));
Greg Clayton5ce9c562013-02-06 17:22:03 +0000305
306 if (container_ap.get())
307 {
308 if (archive_sp)
309 {
310 // We already have this archive in our cache, use it
311 container_ap->SetArchive (archive_sp);
312 return container_ap.release();
313 }
314 else if (container_ap->ParseHeader())
315 return container_ap.release();
316 }
317 }
318 }
319 else
320 {
321 // No data, just check for a cached archive
322 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, module_sp->GetArchitecture(), module_sp->GetModificationTime()));
Greg Clayton44435ed2012-01-12 05:25:17 +0000323 if (archive_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000325 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 +0000326
327 if (container_ap.get())
328 {
329 // We already have this archive in our cache, use it
330 container_ap->SetArchive (archive_sp);
331 return container_ap.release();
332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 }
334 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 }
336 return NULL;
337}
338
339
340
341bool
Greg Clayton44435ed2012-01-12 05:25:17 +0000342ObjectContainerBSDArchive::MagicBytesMatch (const DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 uint32_t offset = 0;
345 const char* armag = (const char* )data.PeekData (offset, sizeof(ar_hdr));
346 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0)
347 {
348 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
349 if (strncmp(armag, ARFMAG, 2) == 0)
350 return true;
351 }
352 return false;
353}
354
355ObjectContainerBSDArchive::ObjectContainerBSDArchive
356(
Greg Claytone72dfb32012-02-24 01:59:29 +0000357 const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000358 DataBufferSP& data_sp,
359 lldb::offset_t data_offset,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 const lldb_private::FileSpec *file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000361 lldb::offset_t file_offset,
362 lldb::offset_t size
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363) :
Greg Clayton5ce9c562013-02-06 17:22:03 +0000364 ObjectContainer (module_sp, file, file_offset, size, data_sp, data_offset),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 m_archive_sp ()
366{
367}
368void
369ObjectContainerBSDArchive::SetArchive (Archive::shared_ptr &archive_sp)
370{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000371 m_archive_sp = archive_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372}
373
374
375
376ObjectContainerBSDArchive::~ObjectContainerBSDArchive()
377{
378}
379
380bool
381ObjectContainerBSDArchive::ParseHeader ()
382{
383 if (m_archive_sp.get() == NULL)
384 {
385 if (m_data.GetByteSize() > 0)
386 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000387 ModuleSP module_sp (GetModule());
388 if (module_sp)
389 {
390 m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file,
391 module_sp->GetArchitecture(),
392 module_sp->GetModificationTime(),
393 m_data);
394 }
Greg Clayton5ce9c562013-02-06 17:22:03 +0000395 // Clear the m_data that contains the entire archive
396 // data and let our m_archive_sp hold onto the data.
397 m_data.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398 }
399 }
400 return m_archive_sp.get() != NULL;
401}
402
403void
404ObjectContainerBSDArchive::Dump (Stream *s) const
405{
Jason Molendafd54b362011-09-20 21:44:10 +0000406 s->Printf("%p: ", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 s->Indent();
408 const size_t num_archs = GetNumArchitectures();
409 const size_t num_objects = GetNumObjects();
Jason Molendafd54b362011-09-20 21:44:10 +0000410 s->Printf("ObjectContainerBSDArchive, num_archs = %lu, num_objects = %lu", num_archs, num_objects);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 uint32_t i;
412 ArchSpec arch;
413 s->IndentMore();
414 for (i=0; i<num_archs; i++)
415 {
416 s->Indent();
417 GetArchitectureAtIndex(i, arch);
Jason Molendafd54b362011-09-20 21:44:10 +0000418 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 }
420 for (i=0; i<num_objects; i++)
421 {
422 s->Indent();
Jason Molendafd54b362011-09-20 21:44:10 +0000423 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex (i));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 }
425 s->IndentLess();
426 s->EOL();
427}
428
Greg Clayton762f7132011-09-18 18:59:15 +0000429ObjectFileSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file)
431{
Greg Claytone72dfb32012-02-24 01:59:29 +0000432 ModuleSP module_sp (GetModule());
433 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000435 if (module_sp->GetObjectName() && m_archive_sp)
436 {
437 Object *object = m_archive_sp->FindObject (module_sp->GetObjectName());
438 if (object)
Greg Clayton5ce9c562013-02-06 17:22:03 +0000439 {
440 lldb::offset_t data_offset = m_offset + object->ar_file_offset;
441 return ObjectFile::FindPlugin (module_sp,
Greg Claytone72dfb32012-02-24 01:59:29 +0000442 file,
Greg Clayton824e7c02012-11-15 19:37:18 +0000443 m_offset + object->ar_file_offset,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000444 object->ar_file_size,
445 m_archive_sp->GetData().GetSharedDataBuffer(),
446 data_offset);
447 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000448 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449 }
Greg Clayton762f7132011-09-18 18:59:15 +0000450 return ObjectFileSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451}
452
453
454//------------------------------------------------------------------
455// PluginInterface protocol
456//------------------------------------------------------------------
457const char *
458ObjectContainerBSDArchive::GetPluginName()
459{
460 return "object-container.bsd-archive";
461}
462
463const char *
464ObjectContainerBSDArchive::GetShortPluginName()
465{
466 return GetPluginNameStatic();
467}
468
469uint32_t
470ObjectContainerBSDArchive::GetPluginVersion()
471{
472 return 1;
473}
474
Greg Claytonf4d6de62013-04-24 22:29:28 +0000475
476size_t
477ObjectContainerBSDArchive::GetModuleSpecifications (const lldb_private::FileSpec& file,
478 lldb::DataBufferSP& data_sp,
479 lldb::offset_t data_offset,
480 lldb::offset_t file_offset,
481 lldb::offset_t length,
482 lldb_private::ModuleSpecList &specs)
483{
484 return 0;
485}