1 //===-- 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 #if defined(_WIN32) || defined(__ANDROID__)
13 // Defines from ar, missing on Windows
14 #define ARMAG "!<arch>\n"
15 #define SARMAG 8
16 #define ARFMAG "`\n"
17 
18 typedef struct ar_hdr {
19   char ar_name[16];
20   char ar_date[12];
21   char ar_uid[6], ar_gid[6];
22   char ar_mode[8];
23   char ar_size[10];
24   char ar_fmag[2];
25 } ar_hdr;
26 #else
27 #include <ar.h>
28 #endif
29 
30 #include "lldb/Core/Module.h"
31 #include "lldb/Core/ModuleSpec.h"
32 #include "lldb/Core/PluginManager.h"
33 #include "lldb/Host/FileSystem.h"
34 #include "lldb/Symbol/ObjectFile.h"
35 #include "lldb/Utility/ArchSpec.h"
36 #include "lldb/Utility/Stream.h"
37 #include "lldb/Utility/Timer.h"
38 
39 #include "llvm/Support/MemoryBuffer.h"
40 
41 using namespace lldb;
42 using namespace lldb_private;
43 
44 ObjectContainerBSDArchive::Object::Object()
45     : ar_name(), ar_date(0), ar_uid(0), ar_gid(0), ar_mode(0), ar_size(0),
46       ar_file_offset(0), ar_file_size(0) {}
47 
48 void ObjectContainerBSDArchive::Object::Clear() {
49   ar_name.Clear();
50   ar_date = 0;
51   ar_uid = 0;
52   ar_gid = 0;
53   ar_mode = 0;
54   ar_size = 0;
55   ar_file_offset = 0;
56   ar_file_size = 0;
57 }
58 
59 lldb::offset_t
60 ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data,
61                                            lldb::offset_t offset) {
62   size_t ar_name_len = 0;
63   std::string str;
64   char *err;
65 
66   // File header
67   //
68   // The common format is as follows.
69   //
70   //  Offset  Length	Name            Format
71   //  0       16      File name       ASCII right padded with spaces (no spaces
72   //  allowed in file name)
73   //  16      12      File mod        Decimal as cstring right padded with
74   //  spaces
75   //  28      6       Owner ID        Decimal as cstring right padded with
76   //  spaces
77   //  34      6       Group ID        Decimal as cstring right padded with
78   //  spaces
79   //  40      8       File mode       Octal   as cstring right padded with
80   //  spaces
81   //  48      10      File byte size  Decimal as cstring right padded with
82   //  spaces
83   //  58      2       File magic      0x60 0x0A
84 
85   // Make sure there is enough data for the file header and bail if not
86   if (!data.ValidOffsetForDataOfSize(offset, 60))
87     return LLDB_INVALID_OFFSET;
88 
89   str.assign((const char *)data.GetData(&offset, 16), 16);
90   if (str.find("#1/") == 0) {
91     // If the name is longer than 16 bytes, or contains an embedded space then
92     // it will use this format where the length of the name is here and the
93     // name characters are after this header.
94     ar_name_len = strtoul(str.c_str() + 3, &err, 10);
95   } else {
96     // Strip off any trailing spaces.
97     const size_t last_pos = str.find_last_not_of(' ');
98     if (last_pos != std::string::npos) {
99       if (last_pos + 1 < 16)
100         str.erase(last_pos + 1);
101     }
102     ar_name.SetCString(str.c_str());
103   }
104 
105   str.assign((const char *)data.GetData(&offset, 12), 12);
106   ar_date = strtoul(str.c_str(), &err, 10);
107 
108   str.assign((const char *)data.GetData(&offset, 6), 6);
109   ar_uid = strtoul(str.c_str(), &err, 10);
110 
111   str.assign((const char *)data.GetData(&offset, 6), 6);
112   ar_gid = strtoul(str.c_str(), &err, 10);
113 
114   str.assign((const char *)data.GetData(&offset, 8), 8);
115   ar_mode = strtoul(str.c_str(), &err, 8);
116 
117   str.assign((const char *)data.GetData(&offset, 10), 10);
118   ar_size = strtoul(str.c_str(), &err, 10);
119 
120   str.assign((const char *)data.GetData(&offset, 2), 2);
121   if (str == ARFMAG) {
122     if (ar_name_len > 0) {
123       const void *ar_name_ptr = data.GetData(&offset, ar_name_len);
124       // Make sure there was enough data for the string value and bail if not
125       if (ar_name_ptr == NULL)
126         return LLDB_INVALID_OFFSET;
127       str.assign((const char *)ar_name_ptr, ar_name_len);
128       ar_name.SetCString(str.c_str());
129     }
130     ar_file_offset = offset;
131     ar_file_size = ar_size - ar_name_len;
132     return offset;
133   }
134   return LLDB_INVALID_OFFSET;
135 }
136 
137 ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
138                                             const llvm::sys::TimePoint<> &time,
139                                             lldb::offset_t file_offset,
140                                             lldb_private::DataExtractor &data)
141     : m_arch(arch), m_time(time), m_file_offset(file_offset), m_objects(),
142       m_data(data) {}
143 
144 ObjectContainerBSDArchive::Archive::~Archive() {}
145 
146 size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
147   DataExtractor &data = m_data;
148   std::string str;
149   lldb::offset_t offset = 0;
150   str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
151   if (str == ARMAG) {
152     Object obj;
153     do {
154       offset = obj.Extract(data, offset);
155       if (offset == LLDB_INVALID_OFFSET)
156         break;
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, obj_idx);
161       offset += obj.ar_file_size;
162       obj.Clear();
163     } while (data.ValidOffset(offset));
164 
165     // Now sort all of the object name pointers
166     m_object_name_to_index_map.Sort();
167   }
168   return m_objects.size();
169 }
170 
171 ObjectContainerBSDArchive::Object *
172 ObjectContainerBSDArchive::Archive::FindObject(
173     const ConstString &object_name,
174     const llvm::sys::TimePoint<> &object_mod_time) {
175   const ObjectNameToIndexMap::Entry *match =
176       m_object_name_to_index_map.FindFirstValueForName(object_name);
177   if (match) {
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);
184       while (next_match) {
185         if (m_objects[next_match->value].ar_date == object_date)
186           return &m_objects[next_match->value];
187         next_match =
188             m_object_name_to_index_map.FindNextValueForName(next_match);
189       }
190     } else {
191       return &m_objects[match->value];
192     }
193   }
194   return NULL;
195 }
196 
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 delete an
206   // archive entry...
207   while (pos != archive_map.end() && pos->first == file) {
208     bool match = true;
209     if (arch.IsValid() &&
210         pos->second->GetArchitecture().IsCompatibleMatch(arch) == false)
211       match = false;
212     else if (file_offset != LLDB_INVALID_OFFSET &&
213              pos->second->GetFileOffset() != file_offset)
214       match = false;
215     if (match) {
216       if (pos->second->GetModificationTime() == time) {
217         return pos->second;
218       } else {
219         // We have a file at the same path with the same architecture whose
220         // modification time doesn't match. It doesn't make sense for us to
221         // continue to use this BSD archive since we cache only the object info
222         // which consists of file time info and also the file offset and file
223         // size of any contained objects. Since this information is now out of
224         // date, we won't get the correct information if we go and extract the
225         // file data, so we should remove the old and outdated entry.
226         archive_map.erase(pos);
227         pos = archive_map.find(file);
228         continue; // Continue to next iteration so we don't increment pos
229                   // below...
230       }
231     }
232     ++pos;
233   }
234   return archive_sp;
235 }
236 
237 ObjectContainerBSDArchive::Archive::shared_ptr
238 ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
239     const FileSpec &file, const ArchSpec &arch,
240     const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
241     DataExtractor &data) {
242   shared_ptr archive_sp(new Archive(arch, time, file_offset, data));
243   if (archive_sp) {
244     const size_t num_objects = archive_sp->ParseObjects();
245     if (num_objects > 0) {
246       std::lock_guard<std::recursive_mutex> guard(
247           Archive::GetArchiveCacheMutex());
248       Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
249     } else {
250       archive_sp.reset();
251     }
252   }
253   return archive_sp;
254 }
255 
256 ObjectContainerBSDArchive::Archive::Map &
257 ObjectContainerBSDArchive::Archive::GetArchiveCache() {
258   static Archive::Map g_archive_map;
259   return g_archive_map;
260 }
261 
262 std::recursive_mutex &
263 ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
264   static std::recursive_mutex g_archive_map_mutex;
265   return g_archive_map_mutex;
266 }
267 
268 void ObjectContainerBSDArchive::Initialize() {
269   PluginManager::RegisterPlugin(GetPluginNameStatic(),
270                                 GetPluginDescriptionStatic(), CreateInstance,
271                                 GetModuleSpecifications);
272 }
273 
274 void ObjectContainerBSDArchive::Terminate() {
275   PluginManager::UnregisterPlugin(CreateInstance);
276 }
277 
278 lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() {
279   static ConstString g_name("bsd-archive");
280   return g_name;
281 }
282 
283 const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() {
284   return "BSD Archive object container reader.";
285 }
286 
287 ObjectContainer *ObjectContainerBSDArchive::CreateInstance(
288     const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
289     lldb::offset_t data_offset, const FileSpec *file,
290     lldb::offset_t file_offset, lldb::offset_t length) {
291   ConstString object_name(module_sp->GetObjectName());
292   if (!object_name)
293     return nullptr;
294 
295   if (data_sp) {
296     // We have data, which means this is the first 512 bytes of the file Check
297     // to see if the magic bytes match and if they do, read the entire table of
298     // contents for the archive and cache it
299     DataExtractor data;
300     data.SetData(data_sp, data_offset, length);
301     if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
302       static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
303       Timer scoped_timer(
304           func_cat,
305           "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
306           "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
307           module_sp->GetFileSpec().GetPath().c_str(),
308           static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
309           static_cast<uint64_t>(length));
310 
311       // Map the entire .a file to be sure that we don't lose any data if the
312       // file gets updated by a new build while this .a file is being used for
313       // debugging
314       DataBufferSP archive_data_sp =
315           FileSystem::Instance().CreateDataBuffer(*file, length, file_offset);
316       if (!archive_data_sp)
317         return nullptr;
318 
319       lldb::offset_t archive_data_offset = 0;
320 
321       Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
322           *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
323           file_offset));
324       std::unique_ptr<ObjectContainerBSDArchive> container_ap(
325           new ObjectContainerBSDArchive(module_sp, archive_data_sp,
326                                         archive_data_offset, file, file_offset,
327                                         length));
328 
329       if (container_ap.get()) {
330         if (archive_sp) {
331           // We already have this archive in our cache, use it
332           container_ap->SetArchive(archive_sp);
333           return container_ap.release();
334         } else if (container_ap->ParseHeader())
335           return container_ap.release();
336       }
337     }
338   } else {
339     // No data, just check for a cached archive
340     Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
341         *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
342         file_offset));
343     if (archive_sp) {
344       std::unique_ptr<ObjectContainerBSDArchive> container_ap(
345           new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
346                                         file_offset, length));
347 
348       if (container_ap.get()) {
349         // We already have this archive in our cache, use it
350         container_ap->SetArchive(archive_sp);
351         return container_ap.release();
352       }
353     }
354   }
355   return NULL;
356 }
357 
358 bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
359   uint32_t offset = 0;
360   const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr));
361   if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) {
362     armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
363     if (strncmp(armag, ARFMAG, 2) == 0)
364       return true;
365   }
366   return false;
367 }
368 
369 ObjectContainerBSDArchive::ObjectContainerBSDArchive(
370     const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
371     lldb::offset_t data_offset, const lldb_private::FileSpec *file,
372     lldb::offset_t file_offset, lldb::offset_t size)
373     : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
374       m_archive_sp() {}
375 void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
376   m_archive_sp = archive_sp;
377 }
378 
379 ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {}
380 
381 bool ObjectContainerBSDArchive::ParseHeader() {
382   if (m_archive_sp.get() == NULL) {
383     if (m_data.GetByteSize() > 0) {
384       ModuleSP module_sp(GetModule());
385       if (module_sp) {
386         m_archive_sp = Archive::ParseAndCacheArchiveForFile(
387             m_file, module_sp->GetArchitecture(),
388             module_sp->GetModificationTime(), m_offset, m_data);
389       }
390       // Clear the m_data that contains the entire archive data and let our
391       // m_archive_sp hold onto the data.
392       m_data.Clear();
393     }
394   }
395   return m_archive_sp.get() != NULL;
396 }
397 
398 void ObjectContainerBSDArchive::Dump(Stream *s) const {
399   s->Printf("%p: ", static_cast<const void *>(this));
400   s->Indent();
401   const size_t num_archs = GetNumArchitectures();
402   const size_t num_objects = GetNumObjects();
403   s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64
404             ", num_objects = %" PRIu64 "",
405             (uint64_t)num_archs, (uint64_t)num_objects);
406   uint32_t i;
407   ArchSpec arch;
408   s->IndentMore();
409   for (i = 0; i < num_archs; i++) {
410     s->Indent();
411     GetArchitectureAtIndex(i, arch);
412     s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
413   }
414   for (i = 0; i < num_objects; i++) {
415     s->Indent();
416     s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
417   }
418   s->IndentLess();
419   s->EOL();
420 }
421 
422 ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
423   ModuleSP module_sp(GetModule());
424   if (module_sp) {
425     if (module_sp->GetObjectName() && m_archive_sp) {
426       Object *object = m_archive_sp->FindObject(
427           module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
428       if (object) {
429         lldb::offset_t data_offset = object->ar_file_offset;
430         return ObjectFile::FindPlugin(
431             module_sp, file, m_offset + object->ar_file_offset,
432             object->ar_file_size, m_archive_sp->GetData().GetSharedDataBuffer(),
433             data_offset);
434       }
435     }
436   }
437   return ObjectFileSP();
438 }
439 
440 //------------------------------------------------------------------
441 // PluginInterface protocol
442 //------------------------------------------------------------------
443 lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() {
444   return GetPluginNameStatic();
445 }
446 
447 uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; }
448 
449 size_t ObjectContainerBSDArchive::GetModuleSpecifications(
450     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
451     lldb::offset_t data_offset, lldb::offset_t file_offset,
452     lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
453 
454   // We have data, which means this is the first 512 bytes of the file Check to
455   // see if the magic bytes match and if they do, read the entire table of
456   // contents for the archive and cache it
457   DataExtractor data;
458   data.SetData(data_sp, data_offset, data_sp->GetByteSize());
459   if (!file || !data_sp || !ObjectContainerBSDArchive::MagicBytesMatch(data))
460     return 0;
461 
462   const size_t initial_count = specs.GetSize();
463   llvm::sys::TimePoint<> file_mod_time = FileSystem::Instance().GetModificationTime(file);
464   Archive::shared_ptr archive_sp(
465       Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
466   bool set_archive_arch = false;
467   if (!archive_sp) {
468     set_archive_arch = true;
469     data_sp =
470         FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
471     if (data_sp) {
472       data.SetData(data_sp, 0, data_sp->GetByteSize());
473       archive_sp = Archive::ParseAndCacheArchiveForFile(
474           file, ArchSpec(), file_mod_time, file_offset, data);
475     }
476   }
477 
478   if (archive_sp) {
479     const size_t num_objects = archive_sp->GetNumObjects();
480     for (size_t idx = 0; idx < num_objects; ++idx) {
481       const Object *object = archive_sp->GetObjectAtIndex(idx);
482       if (object) {
483         const lldb::offset_t object_file_offset =
484             file_offset + object->ar_file_offset;
485         if (object->ar_file_offset < file_size &&
486             file_size > object_file_offset) {
487           if (ObjectFile::GetModuleSpecifications(
488                   file, object_file_offset, file_size - object_file_offset,
489                   specs)) {
490             ModuleSpec &spec =
491                 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
492             llvm::sys::TimePoint<> object_mod_time(
493                 std::chrono::seconds(object->ar_date));
494             spec.GetObjectName() = object->ar_name;
495             spec.SetObjectOffset(object_file_offset);
496             spec.SetObjectSize(file_size - object_file_offset);
497             spec.GetObjectModificationTime() = object_mod_time;
498           }
499         }
500       }
501     }
502   }
503   const size_t end_count = specs.GetSize();
504   size_t num_specs_added = end_count - initial_count;
505   if (set_archive_arch && num_specs_added > 0) {
506     // The archive was created but we didn't have an architecture so we need to
507     // set it
508     for (size_t i = initial_count; i < end_count; ++i) {
509       ModuleSpec module_spec;
510       if (specs.GetModuleSpecAtIndex(i, module_spec)) {
511         if (module_spec.GetArchitecture().IsValid()) {
512           archive_sp->SetArchitecture(module_spec.GetArchitecture());
513           break;
514         }
515       }
516     }
517   }
518   return num_specs_added;
519 }
520