1 //===-- ObjectContainerBSDArchive.cpp ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ObjectContainerBSDArchive.h"
13 // Defines from ar, missing on Windows
14 #define ARMAG "!<arch>\n"
22 char ar_uid[6], ar_gid[6];
31 #include "lldb/Core/ArchSpec.h"
32 #include "lldb/Core/DataBuffer.h"
33 #include "lldb/Core/Module.h"
34 #include "lldb/Core/ModuleSpec.h"
35 #include "lldb/Core/PluginManager.h"
36 #include "lldb/Core/Stream.h"
37 #include "lldb/Core/Timer.h"
38 #include "lldb/Host/Mutex.h"
39 #include "lldb/Symbol/ObjectFile.h"
42 using namespace lldb_private;
46 ObjectContainerBSDArchive::Object::Object() :
59 ObjectContainerBSDArchive::Object::Clear()
72 ObjectContainerBSDArchive::Object::Extract (const DataExtractor& data, lldb::offset_t offset)
74 size_t ar_name_len = 0;
77 str.assign ((const char *)data.GetData(&offset, 16), 16);
78 if (str.find("#1/") == 0)
80 // If the name is longer than 16 bytes, or contains an embedded space
81 // then it will use this format where the length of the name is
82 // here and the name characters are after this header.
83 ar_name_len = strtoul(str.c_str() + 3, &err, 10);
87 // Strip off any spaces (if the object file name contains spaces it
88 // will use the extended format above).
89 str.erase (str.find(' '));
90 ar_name.SetCString(str.c_str());
93 str.assign ((const char *)data.GetData(&offset, 12), 12);
94 ar_date = strtoul(str.c_str(), &err, 10);
96 str.assign ((const char *)data.GetData(&offset, 6), 6);
97 ar_uid = strtoul(str.c_str(), &err, 10);
99 str.assign ((const char *)data.GetData(&offset, 6), 6);
100 ar_gid = strtoul(str.c_str(), &err, 10);
102 str.assign ((const char *)data.GetData(&offset, 8), 8);
103 ar_mode = strtoul(str.c_str(), &err, 8);
105 str.assign ((const char *)data.GetData(&offset, 10), 10);
106 ar_size = strtoul(str.c_str(), &err, 10);
108 str.assign ((const char *)data.GetData(&offset, 2), 2);
113 str.assign ((const char *)data.GetData(&offset, ar_name_len), ar_name_len);
114 ar_name.SetCString (str.c_str());
116 ar_file_offset = offset;
117 ar_file_size = ar_size - ar_name_len;
120 return LLDB_INVALID_OFFSET;
123 ObjectContainerBSDArchive::Archive::Archive
125 const lldb_private::ArchSpec &arch,
126 const lldb_private::TimeValue &time,
127 lldb::offset_t file_offset,
128 lldb_private::DataExtractor &data
132 m_file_offset (file_offset),
138 ObjectContainerBSDArchive::Archive::~Archive ()
143 ObjectContainerBSDArchive::Archive::ParseObjects ()
145 DataExtractor &data = m_data;
147 lldb::offset_t offset = 0;
148 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
154 offset = obj.Extract (data, offset);
155 if (offset == LLDB_INVALID_OFFSET)
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...
160 m_object_name_to_index_map.Append (obj.ar_name.GetCString(), obj_idx);
161 offset += obj.ar_file_size;
163 } while (data.ValidOffset(offset));
165 // Now sort all of the object name pointers
166 m_object_name_to_index_map.Sort ();
168 return m_objects.size();
171 ObjectContainerBSDArchive::Object *
172 ObjectContainerBSDArchive::Archive::FindObject (const ConstString &object_name, const TimeValue &object_mod_time)
174 const ObjectNameToIndexMap::Entry *match = m_object_name_to_index_map.FindFirstValueForName (object_name.GetCString());
177 if (object_mod_time.IsValid())
179 const uint64_t object_date = object_mod_time.GetAsSecondsSinceJan1_1970();
180 if (m_objects[match->value].ar_date == object_date)
181 return &m_objects[match->value];
182 const ObjectNameToIndexMap::Entry *next_match = m_object_name_to_index_map.FindNextValueForName (match);
185 if (m_objects[next_match->value].ar_date == object_date)
186 return &m_objects[next_match->value];
187 next_match = m_object_name_to_index_map.FindNextValueForName (next_match);
192 return &m_objects[match->value];
199 ObjectContainerBSDArchive::Archive::shared_ptr
200 ObjectContainerBSDArchive::Archive::FindCachedArchive (const FileSpec &file, const ArchSpec &arch, const TimeValue &time, lldb::offset_t file_offset)
202 Mutex::Locker locker(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)
211 if (arch.IsValid() && pos->second->GetArchitecture().IsCompatibleMatch(arch) == false)
213 else if (file_offset != LLDB_INVALID_OFFSET && pos->second->GetFileOffset() != file_offset)
217 if (pos->second->GetModificationTime() == time)
223 // We have a file at the same path with the same architecture
224 // whose modification time doesn't match. It doesn't make sense
225 // for us to continue to use this BSD archive since we cache only
226 // the object info which consists of file time info and also the
227 // file offset and file size of any contianed objects. Since
228 // this information is now out of date, we won't get the correct
229 // information if we go and extract the file data, so we should
230 // remove the old and outdated entry.
231 archive_map.erase (pos);
232 pos = archive_map.find (file);
233 continue; // Continue to next iteration so we don't increment pos below...
241 ObjectContainerBSDArchive::Archive::shared_ptr
242 ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile
244 const FileSpec &file,
245 const ArchSpec &arch,
246 const TimeValue &time,
247 lldb::offset_t file_offset,
251 shared_ptr archive_sp(new Archive (arch, time, file_offset, data));
254 const size_t num_objects = archive_sp->ParseObjects ();
257 Mutex::Locker locker(Archive::GetArchiveCacheMutex ());
258 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
268 ObjectContainerBSDArchive::Archive::Map &
269 ObjectContainerBSDArchive::Archive::GetArchiveCache ()
271 static Archive::Map g_archive_map;
272 return g_archive_map;
276 ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex ()
278 static Mutex g_archive_map_mutex (Mutex::eMutexTypeRecursive);
279 return g_archive_map_mutex;
284 ObjectContainerBSDArchive::Initialize()
286 PluginManager::RegisterPlugin (GetPluginNameStatic(),
287 GetPluginDescriptionStatic(),
289 GetModuleSpecifications);
293 ObjectContainerBSDArchive::Terminate()
295 PluginManager::UnregisterPlugin (CreateInstance);
299 lldb_private::ConstString
300 ObjectContainerBSDArchive::GetPluginNameStatic()
302 static ConstString g_name("bsd-archive");
307 ObjectContainerBSDArchive::GetPluginDescriptionStatic()
309 return "BSD Archive object container reader.";
314 ObjectContainerBSDArchive::CreateInstance
316 const lldb::ModuleSP &module_sp,
317 DataBufferSP& data_sp,
318 lldb::offset_t data_offset,
319 const FileSpec *file,
320 lldb::offset_t file_offset,
321 lldb::offset_t length)
323 ConstString object_name (module_sp->GetObjectName());
328 // We have data, which means this is the first 512 bytes of the file
329 // Check to see if the magic bytes match and if they do, read the entire
330 // table of contents for the archive and cache it
332 data.SetData (data_sp, data_offset, length);
333 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data))
335 Timer scoped_timer (__PRETTY_FUNCTION__,
336 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = %p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
337 module_sp->GetFileSpec().GetPath().c_str(),
338 file, (uint64_t) file_offset, (uint64_t) length);
340 // Map the entire .a file to be sure that we don't lose any data if the file
341 // gets updated by a new build while this .a file is being used for debugging
342 DataBufferSP archive_data_sp (file->MemoryMapFileContents(file_offset, length));
343 lldb::offset_t archive_data_offset = 0;
345 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file,
346 module_sp->GetArchitecture(),
347 module_sp->GetModificationTime(),
349 std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp,
356 if (container_ap.get())
360 // We already have this archive in our cache, use it
361 container_ap->SetArchive (archive_sp);
362 return container_ap.release();
364 else if (container_ap->ParseHeader())
365 return container_ap.release();
371 // No data, just check for a cached archive
372 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file,
373 module_sp->GetArchitecture(),
374 module_sp->GetModificationTime(),
378 std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp, data_sp, data_offset, file, file_offset, length));
380 if (container_ap.get())
382 // We already have this archive in our cache, use it
383 container_ap->SetArchive (archive_sp);
384 return container_ap.release();
395 ObjectContainerBSDArchive::MagicBytesMatch (const DataExtractor &data)
398 const char* armag = (const char* )data.PeekData (offset, sizeof(ar_hdr));
399 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0)
401 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
402 if (strncmp(armag, ARFMAG, 2) == 0)
408 ObjectContainerBSDArchive::ObjectContainerBSDArchive
410 const lldb::ModuleSP &module_sp,
411 DataBufferSP& data_sp,
412 lldb::offset_t data_offset,
413 const lldb_private::FileSpec *file,
414 lldb::offset_t file_offset,
417 ObjectContainer (module_sp, file, file_offset, size, data_sp, data_offset),
422 ObjectContainerBSDArchive::SetArchive (Archive::shared_ptr &archive_sp)
424 m_archive_sp = archive_sp;
429 ObjectContainerBSDArchive::~ObjectContainerBSDArchive()
434 ObjectContainerBSDArchive::ParseHeader ()
436 if (m_archive_sp.get() == NULL)
438 if (m_data.GetByteSize() > 0)
440 ModuleSP module_sp (GetModule());
443 m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file,
444 module_sp->GetArchitecture(),
445 module_sp->GetModificationTime(),
449 // Clear the m_data that contains the entire archive
450 // data and let our m_archive_sp hold onto the data.
454 return m_archive_sp.get() != NULL;
458 ObjectContainerBSDArchive::Dump (Stream *s) const
460 s->Printf("%p: ", this);
462 const size_t num_archs = GetNumArchitectures();
463 const size_t num_objects = GetNumObjects();
464 s->Printf("ObjectContainerBSDArchive, num_archs = %zu, num_objects = %zu", num_archs, num_objects);
468 for (i=0; i<num_archs; i++)
471 GetArchitectureAtIndex(i, arch);
472 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
474 for (i=0; i<num_objects; i++)
477 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex (i));
484 ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file)
486 ModuleSP module_sp (GetModule());
489 if (module_sp->GetObjectName() && m_archive_sp)
491 Object *object = m_archive_sp->FindObject (module_sp->GetObjectName(),
492 module_sp->GetObjectModificationTime());
495 lldb::offset_t data_offset = object->ar_file_offset;
496 return ObjectFile::FindPlugin (module_sp,
498 m_offset + object->ar_file_offset,
499 object->ar_file_size,
500 m_archive_sp->GetData().GetSharedDataBuffer(),
505 return ObjectFileSP();
509 //------------------------------------------------------------------
510 // PluginInterface protocol
511 //------------------------------------------------------------------
512 lldb_private::ConstString
513 ObjectContainerBSDArchive::GetPluginName()
515 return GetPluginNameStatic();
519 ObjectContainerBSDArchive::GetPluginVersion()
526 ObjectContainerBSDArchive::GetModuleSpecifications (const lldb_private::FileSpec& file,
527 lldb::DataBufferSP& data_sp,
528 lldb::offset_t data_offset,
529 lldb::offset_t file_offset,
530 lldb::offset_t file_size,
531 lldb_private::ModuleSpecList &specs)
534 // We have data, which means this is the first 512 bytes of the file
535 // Check to see if the magic bytes match and if they do, read the entire
536 // table of contents for the archive and cache it
538 data.SetData (data_sp, data_offset, data_sp->GetByteSize());
539 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data))
541 const size_t initial_count = specs.GetSize();
542 TimeValue file_mod_time = file.GetModificationTime();
543 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (file, ArchSpec(), file_mod_time, file_offset));
544 bool set_archive_arch = false;
547 set_archive_arch = true;
548 DataBufferSP data_sp (file.MemoryMapFileContents(file_offset, file_size));
549 data.SetData (data_sp, 0, data_sp->GetByteSize());
550 archive_sp = Archive::ParseAndCacheArchiveForFile(file, ArchSpec(), file_mod_time, file_offset, data);
555 const size_t num_objects = archive_sp->GetNumObjects();
556 for (size_t idx = 0; idx < num_objects; ++idx)
558 const Object *object = archive_sp->GetObjectAtIndex (idx);
561 const lldb::offset_t object_file_offset = file_offset + object->ar_file_offset;
562 if (object->ar_file_offset < file_size && file_size > object_file_offset)
564 if (ObjectFile::GetModuleSpecifications(file,
566 file_size - object_file_offset,
569 ModuleSpec &spec = specs.GetModuleSpecRefAtIndex (specs.GetSize() - 1);
570 TimeValue object_mod_time;
571 object_mod_time.OffsetWithSeconds(object->ar_date);
572 spec.GetObjectName () = object->ar_name;
573 spec.SetObjectOffset(object_file_offset);
574 spec.GetObjectModificationTime () = object_mod_time;
580 const size_t end_count = specs.GetSize();
581 size_t num_specs_added = end_count - initial_count;
582 if (set_archive_arch && num_specs_added > 0)
584 // The archive was created but we didn't have an architecture
585 // so we need to set it
586 for (size_t i=initial_count; i<end_count; ++ i)
588 ModuleSpec module_spec;
589 if (specs.GetModuleSpecAtIndex(i, module_spec))
591 if (module_spec.GetArchitecture().IsValid())
593 archive_sp->SetArchitecture (module_spec.GetArchitecture());
599 return num_specs_added;