blob: 275f1fa2f70b389f25d42d75345c5a743fcffbe6 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Pavel Labathe705c8b2016-12-02 11:15:15 +000012#if defined(_WIN32) || defined(__ANDROID__)
Virgile Bellob2f1fb22013-08-23 12:44:05 +000013// Defines from ar, missing on Windows
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#define ARMAG "!<arch>\n"
15#define SARMAG 8
16#define ARFMAG "`\n"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000017
Kate Stoneb9c1b512016-09-06 20:57:50 +000018typedef 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];
Virgile Bellob2f1fb22013-08-23 12:44:05 +000025} ar_hdr;
26#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include <ar.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000028#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Module.h"
Greg Clayton2540a8a2013-07-12 22:07:46 +000031#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Core/PluginManager.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000033#include "lldb/Host/FileSystem.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/ObjectFile.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000035#include "lldb/Utility/ArchSpec.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000036#include "lldb/Utility/DataBufferLLVM.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000037#include "lldb/Utility/Stream.h"
Pavel Labath38d06322017-06-29 14:32:17 +000038#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Zachary Turner3f4a4b32017-02-24 18:56:49 +000040#include "llvm/Support/MemoryBuffer.h"
41
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042using namespace lldb;
43using namespace lldb_private;
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045ObjectContainerBSDArchive::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) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049void 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058}
59
Greg Claytonc7bece562013-01-25 18:06:21 +000060lldb::offset_t
Kate Stoneb9c1b512016-09-06 20:57:50 +000061ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data,
62 lldb::offset_t offset) {
63 size_t ar_name_len = 0;
64 std::string str;
65 char *err;
Greg Clayton745b6682014-05-02 22:25:51 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 // 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
Greg Clayton745b6682014-05-02 22:25:51 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 // Make sure there is enough data for the file header and bail if not
87 if (!data.ValidOffsetForDataOfSize(offset, 60))
Greg Claytonc7bece562013-01-25 18:06:21 +000088 return LLDB_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 str.assign((const char *)data.GetData(&offset, 16), 16);
91 if (str.find("#1/") == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +000092 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 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
138ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
Pavel Labath5cc79672016-11-09 15:05:45 +0000139 const llvm::sys::TimePoint<> &time,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 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
145ObjectContainerBSDArchive::Archive::~Archive() {}
146
147size_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...
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000161 m_object_name_to_index_map.Append(obj.ar_name, obj_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170}
171
172ObjectContainerBSDArchive::Object *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173ObjectContainerBSDArchive::Archive::FindObject(
Pavel Labath5cc79672016-11-09 15:05:45 +0000174 const ConstString &object_name,
175 const llvm::sys::TimePoint<> &object_mod_time) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 const ObjectNameToIndexMap::Entry *match =
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000177 m_object_name_to_index_map.FindFirstValueForName(object_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 if (match) {
Pavel Labath5cc79672016-11-09 15:05:45 +0000179 if (object_mod_time != llvm::sys::TimePoint<>()) {
180 const uint64_t object_date = llvm::sys::toTimeT(object_mod_time);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 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];
Greg Clayton57abc5d2013-05-10 21:47:16 +0000193 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 }
195 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
198ObjectContainerBSDArchive::Archive::shared_ptr
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199ObjectContainerBSDArchive::Archive::FindCachedArchive(
Pavel Labath5cc79672016-11-09 15:05:45 +0000200 const FileSpec &file, const ArchSpec &arch,
201 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 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);
Adrian Prantl05097242018-04-30 16:49:04 +0000206 // Don't cache a value for "archive_map.end()" below since we might delete an
207 // archive entry...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 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 {
Adrian Prantl05097242018-04-30 16:49:04 +0000220 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 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 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 ++pos;
234 }
235 return archive_sp;
236}
237
238ObjectContainerBSDArchive::Archive::shared_ptr
239ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
Pavel Labath5cc79672016-11-09 15:05:45 +0000240 const FileSpec &file, const ArchSpec &arch,
241 const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
242 DataExtractor &data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
257ObjectContainerBSDArchive::Archive::Map &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258ObjectContainerBSDArchive::Archive::GetArchiveCache() {
259 static Archive::Map g_archive_map;
260 return g_archive_map;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261}
262
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000263std::recursive_mutex &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
265 static std::recursive_mutex g_archive_map_mutex;
266 return g_archive_map_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269void ObjectContainerBSDArchive::Initialize() {
270 PluginManager::RegisterPlugin(GetPluginNameStatic(),
271 GetPluginDescriptionStatic(), CreateInstance,
272 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273}
274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275void ObjectContainerBSDArchive::Terminate() {
276 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277}
278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() {
280 static ConstString g_name("bsd-archive");
281 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282}
283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() {
285 return "BSD Archive object container reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286}
287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288ObjectContainer *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());
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000293 if (!object_name)
294 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000296 if (data_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000297 // 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
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000300 DataExtractor data;
301 data.SetData(data_sp, data_offset, length);
302 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000303 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000304 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +0000305 func_cat,
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000306 "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));
Greg Clayton5ce9c562013-02-06 17:22:03 +0000311
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000312 // 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 =
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000316 DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000317 if (!archive_data_sp)
318 return nullptr;
Greg Clayton5ce9c562013-02-06 17:22:03 +0000319
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000320 lldb::offset_t archive_data_offset = 0;
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
323 *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
324 file_offset));
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000325 std::unique_ptr<ObjectContainerBSDArchive> container_ap(
326 new ObjectContainerBSDArchive(module_sp, archive_data_sp,
327 archive_data_offset, file, file_offset,
328 length));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000330 if (container_ap.get()) {
331 if (archive_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 // We already have this archive in our cache, use it
333 container_ap->SetArchive(archive_sp);
334 return container_ap.release();
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000335 } else if (container_ap->ParseHeader())
336 return container_ap.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_ap(
346 new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
347 file_offset, length));
348
349 if (container_ap.get()) {
350 // We already have this archive in our cache, use it
351 container_ap->SetArchive(archive_sp);
352 return container_ap.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 }
356 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357}
358
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359bool 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}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370ObjectContainerBSDArchive::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() {}
376void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
377 m_archive_sp = archive_sp;
378}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {}
381
382bool ObjectContainerBSDArchive::ParseHeader() {
383 if (m_archive_sp.get() == NULL) {
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 }
Adrian Prantl05097242018-04-30 16:49:04 +0000391 // Clear the m_data that contains the entire archive data and let our
392 // m_archive_sp hold onto the data.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 m_data.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 }
396 return m_archive_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397}
398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399void 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++) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 s->Indent();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421}
422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423ObjectFileSP 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->ar_file_offset;
431 return ObjectFile::FindPlugin(
432 module_sp, file, m_offset + object->ar_file_offset,
433 object->ar_file_size, m_archive_sp->GetData().GetSharedDataBuffer(),
434 data_offset);
435 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 }
438 return ObjectFileSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439}
440
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441//------------------------------------------------------------------
442// PluginInterface protocol
443//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() {
445 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446}
447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450size_t ObjectContainerBSDArchive::GetModuleSpecifications(
451 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
452 lldb::offset_t data_offset, lldb::offset_t file_offset,
453 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
Greg Claytonf4d6de62013-04-24 22:29:28 +0000454
Adrian Prantl05097242018-04-30 16:49:04 +0000455 // We have data, which means this is the first 512 bytes of the file Check to
456 // see if the magic bytes match and if they do, read the entire table of
457 // contents for the archive and cache it
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 DataExtractor data;
459 data.SetData(data_sp, data_offset, data_sp->GetByteSize());
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000460 if (!file || !data_sp || !ObjectContainerBSDArchive::MagicBytesMatch(data))
461 return 0;
462
463 const size_t initial_count = specs.GetSize();
464 llvm::sys::TimePoint<> file_mod_time = FileSystem::GetModificationTime(file);
465 Archive::shared_ptr archive_sp(
466 Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
467 bool set_archive_arch = false;
468 if (!archive_sp) {
469 set_archive_arch = true;
Zachary Turner666cc0b2017-03-04 01:30:05 +0000470 data_sp =
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000471 DataBufferLLVM::CreateSliceFromPath(file.GetPath(), file_size, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000472 if (data_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 data.SetData(data_sp, 0, data_sp->GetByteSize());
474 archive_sp = Archive::ParseAndCacheArchiveForFile(
475 file, ArchSpec(), file_mod_time, file_offset, data);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 }
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000478
479 if (archive_sp) {
480 const size_t num_objects = archive_sp->GetNumObjects();
481 for (size_t idx = 0; idx < num_objects; ++idx) {
482 const Object *object = archive_sp->GetObjectAtIndex(idx);
483 if (object) {
484 const lldb::offset_t object_file_offset =
485 file_offset + object->ar_file_offset;
486 if (object->ar_file_offset < file_size &&
487 file_size > object_file_offset) {
488 if (ObjectFile::GetModuleSpecifications(
489 file, object_file_offset, file_size - object_file_offset,
490 specs)) {
491 ModuleSpec &spec =
492 specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
493 llvm::sys::TimePoint<> object_mod_time(
494 std::chrono::seconds(object->ar_date));
495 spec.GetObjectName() = object->ar_name;
496 spec.SetObjectOffset(object_file_offset);
497 spec.SetObjectSize(file_size - object_file_offset);
498 spec.GetObjectModificationTime() = object_mod_time;
499 }
500 }
501 }
502 }
503 }
504 const size_t end_count = specs.GetSize();
505 size_t num_specs_added = end_count - initial_count;
506 if (set_archive_arch && num_specs_added > 0) {
Adrian Prantl05097242018-04-30 16:49:04 +0000507 // The archive was created but we didn't have an architecture so we need to
508 // set it
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000509 for (size_t i = initial_count; i < end_count; ++i) {
510 ModuleSpec module_spec;
511 if (specs.GetModuleSpecAtIndex(i, module_spec)) {
512 if (module_spec.GetArchitecture().IsValid()) {
513 archive_sp->SetArchitecture(module_spec.GetArchitecture());
514 break;
515 }
516 }
517 }
518 }
519 return num_specs_added;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000520}