LLDB mainline
ObjectContainerBSDArchive.cpp
Go to the documentation of this file.
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
10
11#if defined(_WIN32) || defined(__ANDROID__)
12// Defines from ar, missing on Windows
13#define SARMAG 8
14#define ARFMAG "`\n"
15
16typedef struct ar_hdr {
17 char ar_name[16];
18 char ar_date[12];
19 char ar_uid[6], ar_gid[6];
20 char ar_mode[8];
21 char ar_size[10];
22 char ar_fmag[2];
23} ar_hdr;
24#else
25#include <ar.h>
26#endif
27
28#include "lldb/Core/Module.h"
35#include "lldb/Utility/Stream.h"
36#include "lldb/Utility/Timer.h"
37
38#include "llvm/Object/Archive.h"
39#include "llvm/Support/MemoryBuffer.h"
40
41using namespace lldb;
42using namespace lldb_private;
43
44using namespace llvm::object;
45
47
49
51 ar_name.Clear();
53 size = 0;
54 file_offset = 0;
55 file_size = 0;
56}
57
59 printf("name = \"%s\"\n", ar_name.GetCString());
60 printf("mtime = 0x%8.8" PRIx32 "\n", modification_time);
61 printf("size = 0x%8.8" PRIx32 " (%" PRIu32 ")\n", size, size);
62 printf("file_offset = 0x%16.16" PRIx64 " (%" PRIu64 ")\n", file_offset,
63 file_offset);
64 printf("file_size = 0x%16.16" PRIx64 " (%" PRIu64 ")\n\n", file_size,
65 file_size);
66}
67
69 const llvm::sys::TimePoint<> &time,
70 lldb::offset_t file_offset,
72 ArchiveType archive_type)
73 : m_arch(arch), m_modification_time(time), m_file_offset(file_offset),
74 m_objects(), m_data(data), m_archive_type(archive_type) {}
75
76Log *l = GetLog(LLDBLog::Object);
78
80 DataExtractor &data = m_data;
81
82 std::unique_ptr<llvm::MemoryBuffer> mem_buffer =
83 llvm::MemoryBuffer::getMemBuffer(
84 llvm::StringRef((const char *)data.GetDataStart(),
85 data.GetByteSize()),
86 llvm::StringRef(),
87 /*RequiresNullTerminator=*/false);
88
89 auto exp_ar = llvm::object::Archive::create(mem_buffer->getMemBufferRef());
90 if (!exp_ar) {
91 LLDB_LOG_ERROR(l, exp_ar.takeError(), "failed to create archive: {0}");
92 return 0;
93 }
94 auto llvm_archive = std::move(exp_ar.get());
95
96 llvm::Error iter_err = llvm::Error::success();
97 Object obj;
98 for (const auto &child : llvm_archive->children(iter_err)) {
99 obj.Clear();
100 auto exp_name = child.getName();
101 if (exp_name) {
102 obj.ar_name = ConstString(exp_name.get());
103 } else {
104 LLDB_LOG_ERROR(l, exp_name.takeError(),
105 "failed to get archive object name: {0}");
106 continue;
107 }
108
109 auto exp_mtime = child.getLastModified();
110 if (exp_mtime) {
112 std::chrono::duration_cast<std::chrono::seconds>(
113 std::chrono::time_point_cast<std::chrono::seconds>(
114 exp_mtime.get())
115 .time_since_epoch())
116 .count();
117 } else {
118 LLDB_LOG_ERROR(l, exp_mtime.takeError(),
119 "failed to get archive object time: {0}");
120 continue;
121 }
122
123 auto exp_size = child.getRawSize();
124 if (exp_size) {
125 obj.size = exp_size.get();
126 } else {
127 LLDB_LOG_ERROR(l, exp_size.takeError(),
128 "failed to get archive object size: {0}");
129 continue;
130 }
131
132 obj.file_offset = child.getDataOffset();
133
134 auto exp_file_size = child.getSize();
135 if (exp_file_size) {
136 obj.file_size = exp_file_size.get();
137 } else {
138 LLDB_LOG_ERROR(l, exp_file_size.takeError(),
139 "failed to get archive object file size: {0}");
140 continue;
141 }
142 m_object_name_to_index_map.Append(obj.ar_name, m_objects.size());
143 m_objects.push_back(obj);
144 }
145 if (iter_err) {
146 LLDB_LOG_ERROR(l, std::move(iter_err),
147 "failed to iterate over archive objects: {0}");
148 }
149 // Now sort all of the object name pointers
150 m_object_name_to_index_map.Sort();
151 return m_objects.size();
152}
153
156 ConstString object_name, const llvm::sys::TimePoint<> &object_mod_time) {
157 const ObjectNameToIndexMap::Entry *match =
158 m_object_name_to_index_map.FindFirstValueForName(object_name);
159 if (!match)
160 return nullptr;
161 if (object_mod_time == llvm::sys::TimePoint<>())
162 return &m_objects[match->value];
163
164 const uint64_t object_modification_date = llvm::sys::toTimeT(object_mod_time);
165 if (m_objects[match->value].modification_time == object_modification_date)
166 return &m_objects[match->value];
167
168 const ObjectNameToIndexMap::Entry *next_match =
169 m_object_name_to_index_map.FindNextValueForName(match);
170 while (next_match) {
171 if (m_objects[next_match->value].modification_time ==
172 object_modification_date)
173 return &m_objects[next_match->value];
174 next_match = m_object_name_to_index_map.FindNextValueForName(next_match);
175 }
176
177 return nullptr;
178}
179
182 const FileSpec &file, const ArchSpec &arch,
183 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) {
184 std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex());
185 shared_ptr archive_sp;
186 Archive::Map &archive_map = Archive::GetArchiveCache();
187 Archive::Map::iterator pos = archive_map.find(file);
188 // Don't cache a value for "archive_map.end()" below since we might delete an
189 // archive entry...
190 while (pos != archive_map.end() && pos->first == file) {
191 bool match = true;
192 if (arch.IsValid() &&
193 !pos->second->GetArchitecture().IsCompatibleMatch(arch))
194 match = false;
195 else if (file_offset != LLDB_INVALID_OFFSET &&
196 pos->second->GetFileOffset() != file_offset)
197 match = false;
198 if (match) {
199 if (pos->second->GetModificationTime() == time) {
200 return pos->second;
201 } else {
202 // We have a file at the same path with the same architecture whose
203 // modification time doesn't match. It doesn't make sense for us to
204 // continue to use this BSD archive since we cache only the object info
205 // which consists of file time info and also the file offset and file
206 // size of any contained objects. Since this information is now out of
207 // date, we won't get the correct information if we go and extract the
208 // file data, so we should remove the old and outdated entry.
209 archive_map.erase(pos);
210 pos = archive_map.find(file);
211 continue; // Continue to next iteration so we don't increment pos
212 // below...
213 }
214 }
215 ++pos;
216 }
217 return archive_sp;
218}
219
222 const FileSpec &file, const ArchSpec &arch,
223 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
224 DataExtractor &data, ArchiveType archive_type) {
225 shared_ptr archive_sp(
226 new Archive(arch, time, file_offset, data, archive_type));
227 if (archive_sp) {
228 const size_t num_objects = archive_sp->ParseObjects();
229 if (num_objects > 0) {
230 std::lock_guard<std::recursive_mutex> guard(
232 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
233 } else {
234 archive_sp.reset();
235 }
236 }
237 return archive_sp;
238}
239
242 static Archive::Map g_archive_map;
243 return g_archive_map;
244}
245
246std::recursive_mutex &
248 static std::recursive_mutex g_archive_map_mutex;
249 return g_archive_map_mutex;
250}
251
256}
257
260}
261
263 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
264 lldb::offset_t data_offset, const FileSpec *file,
265 lldb::offset_t file_offset, lldb::offset_t length) {
266 ConstString object_name(module_sp->GetObjectName());
267 if (!object_name)
268 return nullptr;
269
270 if (data_sp) {
271 // We have data, which means this is the first 512 bytes of the file Check
272 // to see if the magic bytes match and if they do, read the entire table of
273 // contents for the archive and cache it
274 DataExtractor data;
275 data.SetData(data_sp, data_offset, length);
277 if (file && data_sp && archive_type != ArchiveType::Invalid) {
279 "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
280 "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
281 module_sp->GetFileSpec().GetPath().c_str(),
282 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
283 static_cast<uint64_t>(length));
284
285 // Map the entire .a file to be sure that we don't lose any data if the
286 // file gets updated by a new build while this .a file is being used for
287 // debugging
288 DataBufferSP archive_data_sp =
289 FileSystem::Instance().CreateDataBuffer(*file, length, file_offset);
290 if (!archive_data_sp)
291 return nullptr;
292
293 lldb::offset_t archive_data_offset = 0;
294
296 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
297 file_offset));
298 std::unique_ptr<ObjectContainerBSDArchive> container_up(
299 new ObjectContainerBSDArchive(module_sp, archive_data_sp,
300 archive_data_offset, file, file_offset,
301 length, archive_type));
302
303 if (container_up) {
304 if (archive_sp) {
305 // We already have this archive in our cache, use it
306 container_up->SetArchive(archive_sp);
307 return container_up.release();
308 } else if (container_up->ParseHeader())
309 return container_up.release();
310 }
311 }
312 } else {
313 // No data, just check for a cached archive
315 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
316 file_offset));
317 if (archive_sp) {
318 std::unique_ptr<ObjectContainerBSDArchive> container_up(
319 new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
320 file_offset, length,
321 archive_sp->GetArchiveType()));
322
323 if (container_up) {
324 // We already have this archive in our cache, use it
325 container_up->SetArchive(archive_sp);
326 return container_up.release();
327 }
328 }
329 }
330 return nullptr;
331}
332
335 uint32_t offset = 0;
336 const char *armag =
337 (const char *)data.PeekData(offset, sizeof(ar_hdr) + SARMAG);
338 if (armag == nullptr)
339 return ArchiveType::Invalid;
340 ArchiveType result = ArchiveType::Invalid;
341 if (strncmp(armag, ArchiveMagic, SARMAG) == 0)
342 result = ArchiveType::Archive;
343 else if (strncmp(armag, ThinArchiveMagic, SARMAG) == 0)
344 result = ArchiveType::ThinArchive;
345 else
346 return ArchiveType::Invalid;
347
348 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
349 if (strncmp(armag, ARFMAG, 2) == 0)
350 return result;
351 return ArchiveType::Invalid;
352}
353
355 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
356 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
357 lldb::offset_t file_offset, lldb::offset_t size, ArchiveType archive_type)
358 : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
359 m_archive_sp() {
360 m_archive_type = archive_type;
361}
362
364 m_archive_sp = archive_sp;
365}
366
368
370 if (m_archive_sp.get() == nullptr) {
371 if (m_data.GetByteSize() > 0) {
372 ModuleSP module_sp(GetModule());
373 if (module_sp) {
375 m_file, module_sp->GetArchitecture(),
376 module_sp->GetModificationTime(), m_offset, m_data, m_archive_type);
377 }
378 // Clear the m_data that contains the entire archive data and let our
379 // m_archive_sp hold onto the data.
380 m_data.Clear();
381 }
382 }
383 return m_archive_sp.get() != nullptr;
384}
385
387 const FileSpec &parentFileSpec) {
388 llvm::SmallString<128> FullPath;
389 if (llvm::sys::path::is_absolute(childPath)) {
390 FullPath = childPath;
391 } else {
392 FullPath = parentFileSpec.GetDirectory().GetStringRef();
393 llvm::sys::path::append(FullPath, childPath);
394 }
395 FileSpec child = FileSpec(FullPath.str(), llvm::sys::path::Style::posix);
396 return child;
397}
398
400 ModuleSP module_sp(GetModule());
401 if (module_sp) {
402 if (module_sp->GetObjectName() && m_archive_sp) {
403 Object *object = m_archive_sp->FindObject(
404 module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
405 if (object) {
406 if (m_archive_type == ArchiveType::ThinArchive) {
407 // Set file to child object file
409 object->ar_name.GetStringRef(), m_file);
410 lldb::offset_t file_offset = 0;
411 lldb::offset_t file_size = object->size;
412 std::shared_ptr<DataBuffer> child_data_sp =
413 FileSystem::Instance().CreateDataBuffer(child, file_size,
414 file_offset);
415 if (!child_data_sp ||
416 child_data_sp->GetByteSize() != object->file_size)
417 return ObjectFileSP();
418 lldb::offset_t data_offset = 0;
420 module_sp, &child, m_offset + object->file_offset,
421 object->file_size, child_data_sp, data_offset);
422 }
423 lldb::offset_t data_offset = object->file_offset;
425 module_sp, file, m_offset + object->file_offset, object->file_size,
426 m_archive_sp->GetData().GetSharedDataBuffer(), data_offset);
427 }
428 }
429 }
430 return ObjectFileSP();
431}
432
434 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
435 lldb::offset_t data_offset, lldb::offset_t file_offset,
437
438 // We have data, which means this is the first 512 bytes of the file Check to
439 // see if the magic bytes match and if they do, read the entire table of
440 // contents for the archive and cache it
441 DataExtractor data;
442 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
444 if (!file || !data_sp || archive_type == ArchiveType::Invalid)
445 return 0;
446
447 const size_t initial_count = specs.GetSize();
448 llvm::sys::TimePoint<> file_mod_time =
450 Archive::shared_ptr archive_sp(
451 Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
452 bool set_archive_arch = false;
453 if (!archive_sp) {
454 set_archive_arch = true;
455 data_sp =
456 FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
457 if (data_sp) {
458 data.SetData(data_sp, 0, data_sp->GetByteSize());
460 file, ArchSpec(), file_mod_time, file_offset, data, archive_type);
461 }
462 }
463
464 if (archive_sp) {
465 const size_t num_objects = archive_sp->GetNumObjects();
466 for (size_t idx = 0; idx < num_objects; ++idx) {
467 const Object *object = archive_sp->GetObjectAtIndex(idx);
468 if (object) {
469 if (archive_sp->GetArchiveType() == ArchiveType::ThinArchive) {
470 if (object->ar_name.IsEmpty())
471 continue;
473 object->ar_name.GetStringRef(), file);
475 specs)) {
476 ModuleSpec &spec =
477 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
478 llvm::sys::TimePoint<> object_mod_time(
479 std::chrono::seconds(object->modification_time));
480 spec.GetObjectName() = object->ar_name;
481 spec.SetObjectOffset(0);
482 spec.SetObjectSize(object->file_size);
483 spec.GetObjectModificationTime() = object_mod_time;
484 }
485 continue;
486 }
487 const lldb::offset_t object_file_offset =
488 file_offset + object->file_offset;
489 if (object->file_offset < file_size && file_size > object_file_offset) {
491 file, object_file_offset, file_size - object_file_offset,
492 specs)) {
493 ModuleSpec &spec =
494 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
495 llvm::sys::TimePoint<> object_mod_time(
496 std::chrono::seconds(object->modification_time));
497 spec.GetObjectName() = object->ar_name;
498 spec.SetObjectOffset(object_file_offset);
499 spec.SetObjectSize(object->file_size);
500 spec.GetObjectModificationTime() = object_mod_time;
501 }
502 }
503 }
504 }
505 }
506 const size_t end_count = specs.GetSize();
507 size_t num_specs_added = end_count - initial_count;
508 if (set_archive_arch && num_specs_added > 0) {
509 // The archive was created but we didn't have an architecture so we need to
510 // set it
511 for (size_t i = initial_count; i < end_count; ++i) {
512 ModuleSpec module_spec;
513 if (specs.GetModuleSpecAtIndex(i, module_spec)) {
514 if (module_spec.GetArchitecture().IsValid()) {
515 archive_sp->SetArchitecture(module_spec.GetArchitecture());
516 break;
517 }
518 }
519 }
520 }
521 return num_specs_added;
522}
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:392
FileSpec GetChildFileSpecificationsFromThin(llvm::StringRef childPath, const FileSpec &parentFileSpec)
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
#define LLDB_SCOPED_TIMERF(...)
Definition: Timer.h:86
Object * FindObject(lldb_private::ConstString object_name, const llvm::sys::TimePoint<> &object_mod_time)
Archive(const lldb_private::ArchSpec &arch, const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset, lldb_private::DataExtractor &data, ArchiveType archive_type)
static std::recursive_mutex & GetArchiveCacheMutex()
static Archive::shared_ptr ParseAndCacheArchiveForFile(const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch, const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset, lldb_private::DataExtractor &data, ArchiveType archive_type)
std::multimap< lldb_private::FileSpec, shared_ptr > Map
static Archive::shared_ptr FindCachedArchive(const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch, const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset)
static lldb_private::ObjectContainer * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
static llvm::StringRef GetPluginNameStatic()
lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override
Selects an architecture in an object file.
bool ParseHeader() override
Attempts to parse the object header.
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
static ArchiveType MagicBytesMatch(const lldb_private::DataExtractor &data)
static llvm::StringRef GetPluginDescriptionStatic()
~ObjectContainerBSDArchive() override
void SetArchive(Archive::shared_ptr &archive_sp)
ObjectContainerBSDArchive(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t offset, lldb::offset_t length, ArchiveType archive_type)
An architecture specification class.
Definition: ArchSpec.h:31
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:359
A uniqued constant string class.
Definition: ConstString.h:40
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:304
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:197
void Clear()
Clear this object's state.
Definition: ConstString.h:232
An data extractor class.
Definition: DataExtractor.h:48
const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
void Clear()
Clears the object state.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
const uint8_t * GetDataStart() const
Get the data start pointer.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
A file utility class.
Definition: FileSpec.h:56
const ConstString & GetDirectory() const
Directory string const get accessor.
Definition: FileSpec.h:223
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
static FileSystem & Instance()
std::shared_ptr< DataBuffer > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
bool GetModuleSpecAtIndex(size_t i, ModuleSpec &module_spec) const
Definition: ModuleSpec.h:323
ModuleSpec & GetModuleSpecRefAtIndex(size_t i)
Definition: ModuleSpec.h:321
void SetObjectSize(uint64_t object_size)
Definition: ModuleSpec.h:115
ConstString & GetObjectName()
Definition: ModuleSpec.h:103
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:89
void SetObjectOffset(uint64_t object_offset)
Definition: ModuleSpec.h:109
llvm::sys::TimePoint & GetObjectModificationTime()
Definition: ModuleSpec.h:117
A plug-in interface definition class for object containers.
DataExtractor m_data
The data for this object file so things can be parsed lazily.
FileSpec m_file
The file that represents this container objects (which can be different from the module's file).
lldb::addr_t m_offset
The offset in bytes into the file, or the address in memory.
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataBufferSP &data_sp, lldb::offset_t &data_offset)
Find a ObjectFile plug-in that can parse file_spec.
Definition: ObjectFile.cpp:53
static size_t GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs, lldb::DataBufferSP data_sp=lldb::DataBufferSP())
Definition: ObjectFile.cpp:196
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
#define LLDB_INVALID_OFFSET
Definition: lldb-defines.h:93
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:332
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
Definition: lldb-forward.h:375
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:336
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:373
uint32_t size
Object size in bytes in the archive.
uint32_t modification_time
Object modification time in the archive.
lldb_private::ConstString ar_name
Object name in the archive.
lldb::offset_t file_size
Length of the object data.
lldb::offset_t file_offset
File offset in bytes from the beginning of the file of the object data.