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__)
13 // Defines from ar, missing on Windows
14 #define ARMAG "!<arch>\n"
18 typedef struct ar_hdr {
21 char ar_uid[6], ar_gid[6];
30 #include "lldb/Core/ArchSpec.h"
31 #include "lldb/Core/DataBuffer.h"
32 #include "lldb/Core/Module.h"
33 #include "lldb/Core/ModuleSpec.h"
34 #include "lldb/Core/PluginManager.h"
35 #include "lldb/Core/Stream.h"
36 #include "lldb/Core/Timer.h"
37 #include "lldb/Host/FileSystem.h"
38 #include "lldb/Symbol/ObjectFile.h"
41 using namespace lldb_private;
43 ObjectContainerBSDArchive::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) {}
47 void ObjectContainerBSDArchive::Object::Clear() {
59 ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data,
60 lldb::offset_t offset) {
61 size_t ar_name_len = 0;
67 // The common format is as follows.
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
74 // 28 6 Owner ID Decimal as cstring right padded with
76 // 34 6 Group ID Decimal as cstring right padded with
78 // 40 8 File mode Octal as cstring right padded with
80 // 48 10 File byte size Decimal as cstring right padded with
82 // 58 2 File magic 0x60 0x0A
84 // Make sure there is enough data for the file header and bail if not
85 if (!data.ValidOffsetForDataOfSize(offset, 60))
86 return LLDB_INVALID_OFFSET;
88 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);
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);
101 ar_name.SetCString(str.c_str());
104 str.assign((const char *)data.GetData(&offset, 12), 12);
105 ar_date = strtoul(str.c_str(), &err, 10);
107 str.assign((const char *)data.GetData(&offset, 6), 6);
108 ar_uid = strtoul(str.c_str(), &err, 10);
110 str.assign((const char *)data.GetData(&offset, 6), 6);
111 ar_gid = strtoul(str.c_str(), &err, 10);
113 str.assign((const char *)data.GetData(&offset, 8), 8);
114 ar_mode = strtoul(str.c_str(), &err, 8);
116 str.assign((const char *)data.GetData(&offset, 10), 10);
117 ar_size = strtoul(str.c_str(), &err, 10);
119 str.assign((const char *)data.GetData(&offset, 2), 2);
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());
129 ar_file_offset = offset;
130 ar_file_size = ar_size - ar_name_len;
133 return LLDB_INVALID_OFFSET;
136 ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
137 const llvm::sys::TimePoint<> &time,
138 lldb::offset_t file_offset,
139 lldb_private::DataExtractor &data)
140 : m_arch(arch), m_time(time), m_file_offset(file_offset), m_objects(),
143 ObjectContainerBSDArchive::Archive::~Archive() {}
145 size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
146 DataExtractor &data = m_data;
148 lldb::offset_t offset = 0;
149 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
153 offset = obj.Extract(data, offset);
154 if (offset == LLDB_INVALID_OFFSET)
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...
159 m_object_name_to_index_map.Append(obj.ar_name.GetStringRef(), obj_idx);
160 offset += obj.ar_file_size;
162 } while (data.ValidOffset(offset));
164 // Now sort all of the object name pointers
165 m_object_name_to_index_map.Sort();
167 return m_objects.size();
170 ObjectContainerBSDArchive::Object *
171 ObjectContainerBSDArchive::Archive::FindObject(
172 const ConstString &object_name,
173 const llvm::sys::TimePoint<> &object_mod_time) {
174 const ObjectNameToIndexMap::Entry *match =
175 m_object_name_to_index_map.FindFirstValueForName(
176 object_name.GetStringRef());
178 if (object_mod_time != llvm::sys::TimePoint<>()) {
179 const uint64_t object_date = llvm::sys::toTimeT(object_mod_time);
180 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);
185 if (m_objects[next_match->value].ar_date == object_date)
186 return &m_objects[next_match->value];
188 m_object_name_to_index_map.FindNextValueForName(next_match);
191 return &m_objects[match->value];
197 ObjectContainerBSDArchive::Archive::shared_ptr
198 ObjectContainerBSDArchive::Archive::FindCachedArchive(
199 const FileSpec &file, const ArchSpec &arch,
200 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) {
201 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) {
209 if (arch.IsValid() &&
210 pos->second->GetArchitecture().IsCompatibleMatch(arch) == false)
212 else if (file_offset != LLDB_INVALID_OFFSET &&
213 pos->second->GetFileOffset() != file_offset)
216 if (pos->second->GetModificationTime() == time) {
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
238 ObjectContainerBSDArchive::Archive::shared_ptr
239 ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
240 const FileSpec &file, const ArchSpec &arch,
241 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
242 DataExtractor &data) {
243 shared_ptr archive_sp(new Archive(arch, time, file_offset, data));
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));
257 ObjectContainerBSDArchive::Archive::Map &
258 ObjectContainerBSDArchive::Archive::GetArchiveCache() {
259 static Archive::Map g_archive_map;
260 return g_archive_map;
263 std::recursive_mutex &
264 ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
265 static std::recursive_mutex g_archive_map_mutex;
266 return g_archive_map_mutex;
269 void ObjectContainerBSDArchive::Initialize() {
270 PluginManager::RegisterPlugin(GetPluginNameStatic(),
271 GetPluginDescriptionStatic(), CreateInstance,
272 GetModuleSpecifications);
275 void ObjectContainerBSDArchive::Terminate() {
276 PluginManager::UnregisterPlugin(CreateInstance);
279 lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() {
280 static ConstString g_name("bsd-archive");
284 const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() {
285 return "BSD Archive object container reader.";
288 ObjectContainer *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());
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
299 data.SetData(data_sp, data_offset, length);
300 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
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));
309 // Map the entire .a file to be sure that we don't lose any data if the
311 // gets updated by a new build while this .a file is being used for
313 DataBufferSP archive_data_sp(
314 file->MemoryMapFileContentsIfLocal(file_offset, length));
315 lldb::offset_t archive_data_offset = 0;
317 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));
325 if (container_ap.get()) {
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();
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(),
340 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
341 new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
342 file_offset, length));
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();
355 bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
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)
366 ObjectContainerBSDArchive::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),
372 void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
373 m_archive_sp = archive_sp;
376 ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {}
378 bool ObjectContainerBSDArchive::ParseHeader() {
379 if (m_archive_sp.get() == NULL) {
380 if (m_data.GetByteSize() > 0) {
381 ModuleSP module_sp(GetModule());
383 m_archive_sp = Archive::ParseAndCacheArchiveForFile(
384 m_file, module_sp->GetArchitecture(),
385 module_sp->GetModificationTime(), m_offset, m_data);
387 // Clear the m_data that contains the entire archive
388 // data and let our m_archive_sp hold onto the data.
392 return m_archive_sp.get() != NULL;
395 void ObjectContainerBSDArchive::Dump(Stream *s) const {
396 s->Printf("%p: ", static_cast<const void *>(this));
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);
406 for (i = 0; i < num_archs; i++) {
408 GetArchitectureAtIndex(i, arch);
409 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
411 for (i = 0; i < num_objects; i++) {
413 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
419 ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
420 ModuleSP module_sp(GetModule());
422 if (module_sp->GetObjectName() && m_archive_sp) {
423 Object *object = m_archive_sp->FindObject(
424 module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
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(),
434 return ObjectFileSP();
437 //------------------------------------------------------------------
438 // PluginInterface protocol
439 //------------------------------------------------------------------
440 lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() {
441 return GetPluginNameStatic();
444 uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; }
446 size_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) {
451 // 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
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();
458 llvm::sys::TimePoint<> file_mod_time =
459 FileSystem::GetModificationTime(file);
460 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
461 file, ArchSpec(), file_mod_time, file_offset));
462 bool set_archive_arch = false;
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);
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);
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,
485 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
486 llvm::sys::TimePoint<> object_mod_time(
487 std::chrono::seconds(object->ar_date));
488 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;
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());
512 return num_specs_added;