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