1 //===-- ObjectContainerBSDArchive.cpp -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "ObjectContainerBSDArchive.h"
10
11 #if defined(_WIN32) || defined(__ANDROID__)
12 // Defines from ar, missing on Windows
13 #define ARMAG "!<arch>\n"
14 #define SARMAG 8
15 #define ARFMAG "`\n"
16
17 typedef struct ar_hdr {
18 char ar_name[16];
19 char ar_date[12];
20 char ar_uid[6], ar_gid[6];
21 char ar_mode[8];
22 char ar_size[10];
23 char ar_fmag[2];
24 } ar_hdr;
25 #else
26 #include <ar.h>
27 #endif
28
29 #include "lldb/Core/Module.h"
30 #include "lldb/Core/ModuleSpec.h"
31 #include "lldb/Core/PluginManager.h"
32 #include "lldb/Host/FileSystem.h"
33 #include "lldb/Symbol/ObjectFile.h"
34 #include "lldb/Utility/ArchSpec.h"
35 #include "lldb/Utility/Stream.h"
36 #include "lldb/Utility/Timer.h"
37
38 #include "llvm/Support/MemoryBuffer.h"
39
40 using namespace lldb;
41 using namespace lldb_private;
42
LLDB_PLUGIN_DEFINE(ObjectContainerBSDArchive)43 LLDB_PLUGIN_DEFINE(ObjectContainerBSDArchive)
44
45 ObjectContainerBSDArchive::Object::Object()
46 : ar_name(), modification_time(0), uid(0), gid(0), mode(0), size(0),
47 file_offset(0), file_size(0) {}
48
Clear()49 void ObjectContainerBSDArchive::Object::Clear() {
50 ar_name.Clear();
51 modification_time = 0;
52 uid = 0;
53 gid = 0;
54 mode = 0;
55 size = 0;
56 file_offset = 0;
57 file_size = 0;
58 }
59
60 lldb::offset_t
Extract(const DataExtractor & data,lldb::offset_t offset)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 (llvm::StringRef(str).startswith("#1/")) {
92 // If the name is longer than 16 bytes, or contains an embedded space then
93 // it will use this format where the length of the name is here and the
94 // 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 modification_time = strtoul(str.c_str(), &err, 10);
108
109 str.assign((const char *)data.GetData(&offset, 6), 6);
110 uid = strtoul(str.c_str(), &err, 10);
111
112 str.assign((const char *)data.GetData(&offset, 6), 6);
113 gid = strtoul(str.c_str(), &err, 10);
114
115 str.assign((const char *)data.GetData(&offset, 8), 8);
116 mode = strtoul(str.c_str(), &err, 8);
117
118 str.assign((const char *)data.GetData(&offset, 10), 10);
119 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 == nullptr)
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 file_offset = offset;
132 file_size = size - ar_name_len;
133 return offset;
134 }
135 return LLDB_INVALID_OFFSET;
136 }
137
Archive(const lldb_private::ArchSpec & arch,const llvm::sys::TimePoint<> & time,lldb::offset_t file_offset,lldb_private::DataExtractor & data)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_modification_time(time), m_file_offset(file_offset),
143 m_objects(), m_data(data) {}
144
~Archive()145 ObjectContainerBSDArchive::Archive::~Archive() {}
146
ParseObjects()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.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 *
FindObject(ConstString object_name,const llvm::sys::TimePoint<> & object_mod_time)173 ObjectContainerBSDArchive::Archive::FindObject(
174 ConstString object_name, 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 return nullptr;
179 if (object_mod_time == llvm::sys::TimePoint<>())
180 return &m_objects[match->value];
181
182 const uint64_t object_modification_date = llvm::sys::toTimeT(object_mod_time);
183 if (m_objects[match->value].modification_time == object_modification_date)
184 return &m_objects[match->value];
185
186 const ObjectNameToIndexMap::Entry *next_match =
187 m_object_name_to_index_map.FindNextValueForName(match);
188 while (next_match) {
189 if (m_objects[next_match->value].modification_time ==
190 object_modification_date)
191 return &m_objects[next_match->value];
192 next_match = m_object_name_to_index_map.FindNextValueForName(next_match);
193 }
194
195 return nullptr;
196 }
197
198 ObjectContainerBSDArchive::Archive::shared_ptr
FindCachedArchive(const FileSpec & file,const ArchSpec & arch,const llvm::sys::TimePoint<> & time,lldb::offset_t file_offset)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 delete an
207 // 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))
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 whose
221 // modification time doesn't match. It doesn't make sense for us to
222 // continue to use this BSD archive since we cache only the object info
223 // which consists of file time info and also the file offset and file
224 // size of any contained objects. Since this information is now out of
225 // date, we won't get the correct information if we go and extract the
226 // file data, so we should 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
230 // below...
231 }
232 }
233 ++pos;
234 }
235 return archive_sp;
236 }
237
238 ObjectContainerBSDArchive::Archive::shared_ptr
ParseAndCacheArchiveForFile(const FileSpec & file,const ArchSpec & arch,const llvm::sys::TimePoint<> & time,lldb::offset_t file_offset,DataExtractor & data)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));
244 if (archive_sp) {
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));
250 } else {
251 archive_sp.reset();
252 }
253 }
254 return archive_sp;
255 }
256
257 ObjectContainerBSDArchive::Archive::Map &
GetArchiveCache()258 ObjectContainerBSDArchive::Archive::GetArchiveCache() {
259 static Archive::Map g_archive_map;
260 return g_archive_map;
261 }
262
263 std::recursive_mutex &
GetArchiveCacheMutex()264 ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
265 static std::recursive_mutex g_archive_map_mutex;
266 return g_archive_map_mutex;
267 }
268
Initialize()269 void ObjectContainerBSDArchive::Initialize() {
270 PluginManager::RegisterPlugin(GetPluginNameStatic(),
271 GetPluginDescriptionStatic(), CreateInstance,
272 GetModuleSpecifications);
273 }
274
Terminate()275 void ObjectContainerBSDArchive::Terminate() {
276 PluginManager::UnregisterPlugin(CreateInstance);
277 }
278
GetPluginNameStatic()279 lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() {
280 static ConstString g_name("bsd-archive");
281 return g_name;
282 }
283
GetPluginDescriptionStatic()284 const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() {
285 return "BSD Archive object container reader.";
286 }
287
CreateInstance(const lldb::ModuleSP & module_sp,DataBufferSP & data_sp,lldb::offset_t data_offset,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t length)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());
293 if (!object_name)
294 return nullptr;
295
296 if (data_sp) {
297 // We have data, which means this is the first 512 bytes of the file Check
298 // to see if the magic bytes match and if they do, read the entire table of
299 // contents for the archive and cache it
300 DataExtractor data;
301 data.SetData(data_sp, data_offset, length);
302 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
303 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
304 Timer scoped_timer(
305 func_cat,
306 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
307 "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
308 module_sp->GetFileSpec().GetPath().c_str(),
309 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
310 static_cast<uint64_t>(length));
311
312 // Map the entire .a file to be sure that we don't lose any data if the
313 // file gets updated by a new build while this .a file is being used for
314 // debugging
315 DataBufferSP archive_data_sp =
316 FileSystem::Instance().CreateDataBuffer(*file, length, file_offset);
317 if (!archive_data_sp)
318 return nullptr;
319
320 lldb::offset_t archive_data_offset = 0;
321
322 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
323 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
324 file_offset));
325 std::unique_ptr<ObjectContainerBSDArchive> container_up(
326 new ObjectContainerBSDArchive(module_sp, archive_data_sp,
327 archive_data_offset, file, file_offset,
328 length));
329
330 if (container_up) {
331 if (archive_sp) {
332 // We already have this archive in our cache, use it
333 container_up->SetArchive(archive_sp);
334 return container_up.release();
335 } else if (container_up->ParseHeader())
336 return container_up.release();
337 }
338 }
339 } else {
340 // No data, just check for a cached archive
341 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
342 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
343 file_offset));
344 if (archive_sp) {
345 std::unique_ptr<ObjectContainerBSDArchive> container_up(
346 new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
347 file_offset, length));
348
349 if (container_up) {
350 // We already have this archive in our cache, use it
351 container_up->SetArchive(archive_sp);
352 return container_up.release();
353 }
354 }
355 }
356 return nullptr;
357 }
358
MagicBytesMatch(const DataExtractor & data)359 bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
360 uint32_t offset = 0;
361 const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr));
362 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) {
363 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
364 if (strncmp(armag, ARFMAG, 2) == 0)
365 return true;
366 }
367 return false;
368 }
369
ObjectContainerBSDArchive(const lldb::ModuleSP & module_sp,DataBufferSP & data_sp,lldb::offset_t data_offset,const lldb_private::FileSpec * file,lldb::offset_t file_offset,lldb::offset_t size)370 ObjectContainerBSDArchive::ObjectContainerBSDArchive(
371 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
372 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
373 lldb::offset_t file_offset, lldb::offset_t size)
374 : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
375 m_archive_sp() {}
SetArchive(Archive::shared_ptr & archive_sp)376 void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
377 m_archive_sp = archive_sp;
378 }
379
~ObjectContainerBSDArchive()380 ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {}
381
ParseHeader()382 bool ObjectContainerBSDArchive::ParseHeader() {
383 if (m_archive_sp.get() == nullptr) {
384 if (m_data.GetByteSize() > 0) {
385 ModuleSP module_sp(GetModule());
386 if (module_sp) {
387 m_archive_sp = Archive::ParseAndCacheArchiveForFile(
388 m_file, module_sp->GetArchitecture(),
389 module_sp->GetModificationTime(), m_offset, m_data);
390 }
391 // Clear the m_data that contains the entire archive data and let our
392 // m_archive_sp hold onto the data.
393 m_data.Clear();
394 }
395 }
396 return m_archive_sp.get() != nullptr;
397 }
398
Dump(Stream * s) const399 void ObjectContainerBSDArchive::Dump(Stream *s) const {
400 s->Printf("%p: ", static_cast<const void *>(this));
401 s->Indent();
402 const size_t num_archs = GetNumArchitectures();
403 const size_t num_objects = GetNumObjects();
404 s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64
405 ", num_objects = %" PRIu64 "",
406 (uint64_t)num_archs, (uint64_t)num_objects);
407 uint32_t i;
408 ArchSpec arch;
409 s->IndentMore();
410 for (i = 0; i < num_archs; i++) {
411 s->Indent();
412 GetArchitectureAtIndex(i, arch);
413 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
414 }
415 for (i = 0; i < num_objects; i++) {
416 s->Indent();
417 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
418 }
419 s->IndentLess();
420 s->EOL();
421 }
422
GetObjectFile(const FileSpec * file)423 ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
424 ModuleSP module_sp(GetModule());
425 if (module_sp) {
426 if (module_sp->GetObjectName() && m_archive_sp) {
427 Object *object = m_archive_sp->FindObject(
428 module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
429 if (object) {
430 lldb::offset_t data_offset = object->file_offset;
431 return ObjectFile::FindPlugin(
432 module_sp, file, m_offset + object->file_offset, object->file_size,
433 m_archive_sp->GetData().GetSharedDataBuffer(), data_offset);
434 }
435 }
436 }
437 return ObjectFileSP();
438 }
439
440 // PluginInterface protocol
GetPluginName()441 lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() {
442 return GetPluginNameStatic();
443 }
444
GetPluginVersion()445 uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; }
446
GetModuleSpecifications(const lldb_private::FileSpec & file,lldb::DataBufferSP & data_sp,lldb::offset_t data_offset,lldb::offset_t file_offset,lldb::offset_t file_size,lldb_private::ModuleSpecList & specs)447 size_t ObjectContainerBSDArchive::GetModuleSpecifications(
448 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
449 lldb::offset_t data_offset, lldb::offset_t file_offset,
450 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
451
452 // We have data, which means this is the first 512 bytes of the file Check to
453 // see if the magic bytes match and if they do, read the entire table of
454 // contents for the archive and cache it
455 DataExtractor data;
456 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
457 if (!file || !data_sp || !ObjectContainerBSDArchive::MagicBytesMatch(data))
458 return 0;
459
460 const size_t initial_count = specs.GetSize();
461 llvm::sys::TimePoint<> file_mod_time = FileSystem::Instance().GetModificationTime(file);
462 Archive::shared_ptr archive_sp(
463 Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
464 bool set_archive_arch = false;
465 if (!archive_sp) {
466 set_archive_arch = true;
467 data_sp =
468 FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
469 if (data_sp) {
470 data.SetData(data_sp, 0, data_sp->GetByteSize());
471 archive_sp = Archive::ParseAndCacheArchiveForFile(
472 file, ArchSpec(), file_mod_time, file_offset, data);
473 }
474 }
475
476 if (archive_sp) {
477 const size_t num_objects = archive_sp->GetNumObjects();
478 for (size_t idx = 0; idx < num_objects; ++idx) {
479 const Object *object = archive_sp->GetObjectAtIndex(idx);
480 if (object) {
481 const lldb::offset_t object_file_offset =
482 file_offset + object->file_offset;
483 if (object->file_offset < file_size && file_size > object_file_offset) {
484 if (ObjectFile::GetModuleSpecifications(
485 file, object_file_offset, file_size - object_file_offset,
486 specs)) {
487 ModuleSpec &spec =
488 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
489 llvm::sys::TimePoint<> object_mod_time(
490 std::chrono::seconds(object->modification_time));
491 spec.GetObjectName() = object->ar_name;
492 spec.SetObjectOffset(object_file_offset);
493 spec.SetObjectSize(file_size - object_file_offset);
494 spec.GetObjectModificationTime() = object_mod_time;
495 }
496 }
497 }
498 }
499 }
500 const size_t end_count = specs.GetSize();
501 size_t num_specs_added = end_count - initial_count;
502 if (set_archive_arch && num_specs_added > 0) {
503 // The archive was created but we didn't have an architecture so we need to
504 // set it
505 for (size_t i = initial_count; i < end_count; ++i) {
506 ModuleSpec module_spec;
507 if (specs.GetModuleSpecAtIndex(i, module_spec)) {
508 if (module_spec.GetArchitecture().IsValid()) {
509 archive_sp->SetArchitecture(module_spec.GetArchitecture());
510 break;
511 }
512 }
513 }
514 }
515 return num_specs_added;
516 }
517