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"
12 #if defined(_WIN32) || defined(__ANDROID_NDK__)
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/Symbol/ObjectFile.h"
41 using namespace lldb_private;
45 ObjectContainerBSDArchive::Object::Object() :
58 ObjectContainerBSDArchive::Object::Clear()
71 ObjectContainerBSDArchive::Object::Extract (const DataExtractor& data, lldb::offset_t offset)
73 size_t ar_name_len = 0;
80 // The common format is as follows.
82 // Offset Length Name Format
83 // 0 16 File name ASCII right padded with spaces (no spaces allowed in file name)
84 // 16 12 File mod Decimal as cstring right padded with spaces
85 // 28 6 Owner ID Decimal as cstring right padded with spaces
86 // 34 6 Group ID Decimal as cstring right padded with spaces
87 // 40 8 File mode Octal as cstring right padded with spaces
88 // 48 10 File byte size Decimal as cstring right padded with spaces
89 // 58 2 File magic 0x60 0x0A
91 // Make sure there is enough data for the file header and bail if not
92 if (!data.ValidOffsetForDataOfSize(offset, 60))
93 return LLDB_INVALID_OFFSET;
95 str.assign ((const char *)data.GetData(&offset, 16), 16);
96 if (str.find("#1/") == 0)
98 // If the name is longer than 16 bytes, or contains an embedded space
99 // then it will use this format where the length of the name is
100 // here and the name characters are after this header.
101 ar_name_len = strtoul(str.c_str() + 3, &err, 10);
105 // Strip off any trailing spaces.
106 const size_t last_pos = str.find_last_not_of(' ');
107 if (last_pos != std::string::npos)
109 if (last_pos + 1 < 16)
110 str.erase (last_pos + 1);
112 ar_name.SetCString(str.c_str());
115 str.assign ((const char *)data.GetData(&offset, 12), 12);
116 ar_date = strtoul(str.c_str(), &err, 10);
118 str.assign ((const char *)data.GetData(&offset, 6), 6);
119 ar_uid = strtoul(str.c_str(), &err, 10);
121 str.assign ((const char *)data.GetData(&offset, 6), 6);
122 ar_gid = strtoul(str.c_str(), &err, 10);
124 str.assign ((const char *)data.GetData(&offset, 8), 8);
125 ar_mode = strtoul(str.c_str(), &err, 8);
127 str.assign ((const char *)data.GetData(&offset, 10), 10);
128 ar_size = strtoul(str.c_str(), &err, 10);
130 str.assign ((const char *)data.GetData(&offset, 2), 2);
135 const void *ar_name_ptr = data.GetData(&offset, ar_name_len);
136 // Make sure there was enough data for the string value and bail if not
137 if (ar_name_ptr == NULL)
138 return LLDB_INVALID_OFFSET;
139 str.assign ((const char *)ar_name_ptr, ar_name_len);
140 ar_name.SetCString (str.c_str());
142 ar_file_offset = offset;
143 ar_file_size = ar_size - ar_name_len;
146 return LLDB_INVALID_OFFSET;
149 ObjectContainerBSDArchive::Archive::Archive
151 const lldb_private::ArchSpec &arch,
152 const lldb_private::TimeValue &time,
153 lldb::offset_t file_offset,
154 lldb_private::DataExtractor &data
158 m_file_offset (file_offset),
164 ObjectContainerBSDArchive::Archive::~Archive ()
169 ObjectContainerBSDArchive::Archive::ParseObjects ()
171 DataExtractor &data = m_data;
173 lldb::offset_t offset = 0;
174 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
180 offset = obj.Extract (data, offset);
181 if (offset == LLDB_INVALID_OFFSET)
183 size_t obj_idx = m_objects.size();
184 m_objects.push_back(obj);
185 // Insert all of the C strings out of order for now...
186 m_object_name_to_index_map.Append (obj.ar_name.GetCString(), obj_idx);
187 offset += obj.ar_file_size;
189 } while (data.ValidOffset(offset));
191 // Now sort all of the object name pointers
192 m_object_name_to_index_map.Sort ();
194 return m_objects.size();
197 ObjectContainerBSDArchive::Object *
198 ObjectContainerBSDArchive::Archive::FindObject (const ConstString &object_name, const TimeValue &object_mod_time)
200 const ObjectNameToIndexMap::Entry *match = m_object_name_to_index_map.FindFirstValueForName (object_name.GetCString());
203 if (object_mod_time.IsValid())
205 const uint64_t object_date = object_mod_time.GetAsSecondsSinceJan1_1970();
206 if (m_objects[match->value].ar_date == object_date)
207 return &m_objects[match->value];
208 const ObjectNameToIndexMap::Entry *next_match = m_object_name_to_index_map.FindNextValueForName (match);
211 if (m_objects[next_match->value].ar_date == object_date)
212 return &m_objects[next_match->value];
213 next_match = m_object_name_to_index_map.FindNextValueForName (next_match);
218 return &m_objects[match->value];
225 ObjectContainerBSDArchive::Archive::shared_ptr
226 ObjectContainerBSDArchive::Archive::FindCachedArchive (const FileSpec &file, const ArchSpec &arch, const TimeValue &time, lldb::offset_t file_offset)
228 std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex());
229 shared_ptr archive_sp;
230 Archive::Map &archive_map = Archive::GetArchiveCache ();
231 Archive::Map::iterator pos = archive_map.find (file);
232 // Don't cache a value for "archive_map.end()" below since we might
233 // delete an archive entry...
234 while (pos != archive_map.end() && pos->first == file)
237 if (arch.IsValid() && pos->second->GetArchitecture().IsCompatibleMatch(arch) == false)
239 else if (file_offset != LLDB_INVALID_OFFSET && pos->second->GetFileOffset() != file_offset)
243 if (pos->second->GetModificationTime() == time)
249 // We have a file at the same path with the same architecture
250 // whose modification time doesn't match. It doesn't make sense
251 // for us to continue to use this BSD archive since we cache only
252 // the object info which consists of file time info and also the
253 // file offset and file size of any contained objects. Since
254 // this information is now out of date, we won't get the correct
255 // information if we go and extract the file data, so we should
256 // remove the old and outdated entry.
257 archive_map.erase (pos);
258 pos = archive_map.find (file);
259 continue; // Continue to next iteration so we don't increment pos below...
267 ObjectContainerBSDArchive::Archive::shared_ptr
268 ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile
270 const FileSpec &file,
271 const ArchSpec &arch,
272 const TimeValue &time,
273 lldb::offset_t file_offset,
277 shared_ptr archive_sp(new Archive (arch, time, file_offset, data));
280 const size_t num_objects = archive_sp->ParseObjects ();
283 std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex());
284 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
294 ObjectContainerBSDArchive::Archive::Map &
295 ObjectContainerBSDArchive::Archive::GetArchiveCache ()
297 static Archive::Map g_archive_map;
298 return g_archive_map;
301 std::recursive_mutex &
302 ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex()
304 static std::recursive_mutex g_archive_map_mutex;
305 return g_archive_map_mutex;
309 ObjectContainerBSDArchive::Initialize()
311 PluginManager::RegisterPlugin (GetPluginNameStatic(),
312 GetPluginDescriptionStatic(),
314 GetModuleSpecifications);
318 ObjectContainerBSDArchive::Terminate()
320 PluginManager::UnregisterPlugin (CreateInstance);
324 lldb_private::ConstString
325 ObjectContainerBSDArchive::GetPluginNameStatic()
327 static ConstString g_name("bsd-archive");
332 ObjectContainerBSDArchive::GetPluginDescriptionStatic()
334 return "BSD Archive object container reader.";
339 ObjectContainerBSDArchive::CreateInstance
341 const lldb::ModuleSP &module_sp,
342 DataBufferSP& data_sp,
343 lldb::offset_t data_offset,
344 const FileSpec *file,
345 lldb::offset_t file_offset,
346 lldb::offset_t length)
348 ConstString object_name (module_sp->GetObjectName());
353 // We have data, which means this is the first 512 bytes of the file
354 // Check to see if the magic bytes match and if they do, read the entire
355 // table of contents for the archive and cache it
357 data.SetData (data_sp, data_offset, length);
358 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data))
360 Timer scoped_timer (__PRETTY_FUNCTION__,
361 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = %p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
362 module_sp->GetFileSpec().GetPath().c_str(),
363 static_cast<const void*>(file),
364 static_cast<uint64_t>(file_offset),
365 static_cast<uint64_t>(length));
367 // Map the entire .a file to be sure that we don't lose any data if the file
368 // gets updated by a new build while this .a file is being used for debugging
369 DataBufferSP archive_data_sp (file->MemoryMapFileContentsIfLocal(file_offset, length));
370 lldb::offset_t archive_data_offset = 0;
372 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file,
373 module_sp->GetArchitecture(),
374 module_sp->GetModificationTime(),
376 std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp,
383 if (container_ap.get())
387 // We already have this archive in our cache, use it
388 container_ap->SetArchive (archive_sp);
389 return container_ap.release();
391 else if (container_ap->ParseHeader())
392 return container_ap.release();
398 // No data, just check for a cached archive
399 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file,
400 module_sp->GetArchitecture(),
401 module_sp->GetModificationTime(),
405 std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp, data_sp, data_offset, file, file_offset, length));
407 if (container_ap.get())
409 // We already have this archive in our cache, use it
410 container_ap->SetArchive (archive_sp);
411 return container_ap.release();
422 ObjectContainerBSDArchive::MagicBytesMatch (const DataExtractor &data)
425 const char* armag = (const char* )data.PeekData (offset, sizeof(ar_hdr));
426 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0)
428 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
429 if (strncmp(armag, ARFMAG, 2) == 0)
435 ObjectContainerBSDArchive::ObjectContainerBSDArchive
437 const lldb::ModuleSP &module_sp,
438 DataBufferSP& data_sp,
439 lldb::offset_t data_offset,
440 const lldb_private::FileSpec *file,
441 lldb::offset_t file_offset,
444 ObjectContainer (module_sp, file, file_offset, size, data_sp, data_offset),
449 ObjectContainerBSDArchive::SetArchive (Archive::shared_ptr &archive_sp)
451 m_archive_sp = archive_sp;
456 ObjectContainerBSDArchive::~ObjectContainerBSDArchive()
461 ObjectContainerBSDArchive::ParseHeader ()
463 if (m_archive_sp.get() == NULL)
465 if (m_data.GetByteSize() > 0)
467 ModuleSP module_sp (GetModule());
470 m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file,
471 module_sp->GetArchitecture(),
472 module_sp->GetModificationTime(),
476 // Clear the m_data that contains the entire archive
477 // data and let our m_archive_sp hold onto the data.
481 return m_archive_sp.get() != NULL;
485 ObjectContainerBSDArchive::Dump (Stream *s) const
487 s->Printf("%p: ", static_cast<const void*>(this));
489 const size_t num_archs = GetNumArchitectures();
490 const size_t num_objects = GetNumObjects();
491 s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64 ", num_objects = %" PRIu64 "", (uint64_t)num_archs, (uint64_t)num_objects);
495 for (i=0; i<num_archs; i++)
498 GetArchitectureAtIndex(i, arch);
499 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
501 for (i=0; i<num_objects; i++)
504 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex (i));
511 ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file)
513 ModuleSP module_sp (GetModule());
516 if (module_sp->GetObjectName() && m_archive_sp)
518 Object *object = m_archive_sp->FindObject (module_sp->GetObjectName(),
519 module_sp->GetObjectModificationTime());
522 lldb::offset_t data_offset = object->ar_file_offset;
523 return ObjectFile::FindPlugin (module_sp,
525 m_offset + object->ar_file_offset,
526 object->ar_file_size,
527 m_archive_sp->GetData().GetSharedDataBuffer(),
532 return ObjectFileSP();
536 //------------------------------------------------------------------
537 // PluginInterface protocol
538 //------------------------------------------------------------------
539 lldb_private::ConstString
540 ObjectContainerBSDArchive::GetPluginName()
542 return GetPluginNameStatic();
546 ObjectContainerBSDArchive::GetPluginVersion()
553 ObjectContainerBSDArchive::GetModuleSpecifications (const lldb_private::FileSpec& file,
554 lldb::DataBufferSP& data_sp,
555 lldb::offset_t data_offset,
556 lldb::offset_t file_offset,
557 lldb::offset_t file_size,
558 lldb_private::ModuleSpecList &specs)
561 // We have data, which means this is the first 512 bytes of the file
562 // Check to see if the magic bytes match and if they do, read the entire
563 // table of contents for the archive and cache it
565 data.SetData (data_sp, data_offset, data_sp->GetByteSize());
566 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data))
568 const size_t initial_count = specs.GetSize();
569 TimeValue file_mod_time = file.GetModificationTime();
570 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (file, ArchSpec(), file_mod_time, file_offset));
571 bool set_archive_arch = false;
574 set_archive_arch = true;
575 DataBufferSP data_sp (file.MemoryMapFileContentsIfLocal(file_offset, file_size));
576 data.SetData (data_sp, 0, data_sp->GetByteSize());
577 archive_sp = Archive::ParseAndCacheArchiveForFile(file, ArchSpec(), file_mod_time, file_offset, data);
582 const size_t num_objects = archive_sp->GetNumObjects();
583 for (size_t idx = 0; idx < num_objects; ++idx)
585 const Object *object = archive_sp->GetObjectAtIndex (idx);
588 const lldb::offset_t object_file_offset = file_offset + object->ar_file_offset;
589 if (object->ar_file_offset < file_size && file_size > object_file_offset)
591 if (ObjectFile::GetModuleSpecifications(file,
593 file_size - object_file_offset,
596 ModuleSpec &spec = specs.GetModuleSpecRefAtIndex (specs.GetSize() - 1);
597 TimeValue object_mod_time;
598 object_mod_time.OffsetWithSeconds(object->ar_date);
599 spec.GetObjectName () = object->ar_name;
600 spec.SetObjectOffset (object_file_offset);
601 spec.SetObjectSize (file_size - object_file_offset);
602 spec.GetObjectModificationTime () = object_mod_time;
608 const size_t end_count = specs.GetSize();
609 size_t num_specs_added = end_count - initial_count;
610 if (set_archive_arch && num_specs_added > 0)
612 // The archive was created but we didn't have an architecture
613 // so we need to set it
614 for (size_t i=initial_count; i<end_count; ++ i)
616 ModuleSpec module_spec;
617 if (specs.GetModuleSpecAtIndex(i, module_spec))
619 if (module_spec.GetArchitecture().IsValid())
621 archive_sp->SetArchitecture (module_spec.GetArchitecture());
627 return num_specs_added;