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