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 #include <ar.h> 13 14 #include "lldb/Core/Stream.h" 15 #include "lldb/Core/ArchSpec.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/RegularExpression.h" 19 #include "lldb/Host/Mutex.h" 20 #include "lldb/Symbol/ObjectFile.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 26 27 ObjectContainerBSDArchive::Object::Object() : 28 ar_name(), 29 ar_date(0), 30 ar_uid(0), 31 ar_gid(0), 32 ar_mode(0), 33 ar_size(0), 34 ar_file_offset(0), 35 ar_file_size(0) 36 { 37 } 38 39 void 40 ObjectContainerBSDArchive::Object::Clear() 41 { 42 ar_name.Clear(); 43 ar_date = 0; 44 ar_uid = 0; 45 ar_gid = 0; 46 ar_mode = 0; 47 ar_size = 0; 48 ar_file_offset = 0; 49 ar_file_size = 0; 50 } 51 52 uint32_t 53 ObjectContainerBSDArchive::Object::Extract (const DataExtractor& data, uint32_t offset) 54 { 55 size_t ar_name_len = 0; 56 std::string str; 57 char *err; 58 str.assign ((const char *)data.GetData(&offset, 16), 16); 59 if (str.find("#1/") == 0) 60 { 61 // If the name is longer than 16 bytes, or contains an embedded space 62 // then it will use this format where the length of the name is 63 // here and the name characters are after this header. 64 ar_name_len = strtoul(str.c_str() + 3, &err, 10); 65 } 66 else 67 { 68 // Strip off any spaces (if the object file name contains spaces it 69 // will use the extended format above). 70 str.erase (str.find(' ')); 71 ar_name.SetCString(str.c_str()); 72 } 73 74 str.assign ((const char *)data.GetData(&offset, 12), 12); 75 ar_date = strtoul(str.c_str(), &err, 10); 76 77 str.assign ((const char *)data.GetData(&offset, 6), 6); 78 ar_uid = strtoul(str.c_str(), &err, 10); 79 80 str.assign ((const char *)data.GetData(&offset, 6), 6); 81 ar_gid = strtoul(str.c_str(), &err, 10); 82 83 str.assign ((const char *)data.GetData(&offset, 8), 8); 84 ar_mode = strtoul(str.c_str(), &err, 8); 85 86 str.assign ((const char *)data.GetData(&offset, 10), 10); 87 ar_size = strtoul(str.c_str(), &err, 10); 88 89 str.assign ((const char *)data.GetData(&offset, 2), 2); 90 if (str == ARFMAG) 91 { 92 if (ar_name_len > 0) 93 { 94 str.assign ((const char *)data.GetData(&offset, ar_name_len), ar_name_len); 95 ar_name.SetCString (str.c_str()); 96 } 97 ar_file_offset = offset; 98 ar_file_size = ar_size - ar_name_len; 99 return offset; 100 } 101 return LLDB_INVALID_INDEX32; 102 } 103 104 ObjectContainerBSDArchive::Archive::Archive 105 ( 106 const lldb_private::ArchSpec &arch, 107 const lldb_private::TimeValue &time 108 ) : 109 m_arch (arch), 110 m_time (time), 111 m_objects() 112 { 113 } 114 115 ObjectContainerBSDArchive::Archive::~Archive () 116 { 117 } 118 119 size_t 120 ObjectContainerBSDArchive::Archive::ParseObjects (DataExtractor &data) 121 { 122 std::string str; 123 uint32_t offset = 0; 124 str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG); 125 if (str == ARMAG) 126 { 127 Object obj; 128 do 129 { 130 offset = obj.Extract (data, offset); 131 if (offset == LLDB_INVALID_INDEX32) 132 break; 133 uint32_t obj_idx = m_objects.size(); 134 m_objects.push_back(obj); 135 // Insert all of the C strings out of order for now... 136 m_object_name_to_index_map.Append (obj.ar_name.GetCString(), obj_idx); 137 offset += obj.ar_file_size; 138 obj.Clear(); 139 } while (data.ValidOffset(offset)); 140 141 // Now sort all of the object name pointers 142 m_object_name_to_index_map.Sort (); 143 } 144 return m_objects.size(); 145 } 146 147 ObjectContainerBSDArchive::Object * 148 ObjectContainerBSDArchive::Archive::FindObject (const ConstString &object_name) 149 { 150 const UniqueCStringMap<uint32_t>::Entry *match = m_object_name_to_index_map.FindFirstValueForName (object_name.GetCString()); 151 if (match) 152 return &m_objects[match->value]; 153 return NULL; 154 } 155 156 157 ObjectContainerBSDArchive::Archive::shared_ptr 158 ObjectContainerBSDArchive::Archive::FindCachedArchive (const FileSpec &file, const ArchSpec &arch, const TimeValue &time) 159 { 160 Mutex::Locker locker(Archive::GetArchiveCacheMutex ()); 161 shared_ptr archive_sp; 162 Archive::Map &archive_map = Archive::GetArchiveCache (); 163 Archive::Map::iterator pos = archive_map.find (file); 164 // Don't cache a value for "archive_map.end()" below since we might 165 // delete an archive entry... 166 while (pos != archive_map.end() && pos->first == file) 167 { 168 if (pos->second->GetArchitecture() == arch) 169 { 170 if (pos->second->GetModificationTime() == time) 171 { 172 return pos->second; 173 } 174 else 175 { 176 // We have a file at the same path with the same architecture 177 // whose modification time doesn't match. It doesn't make sense 178 // for us to continue to use this BSD archive since we cache only 179 // the object info which consists of file time info and also the 180 // file offset and file size of any contianed objects. Since 181 // this information is now out of date, we won't get the correct 182 // information if we go and extract the file data, so we should 183 // remove the old and outdated entry. 184 archive_map.erase (pos); 185 pos = archive_map.find (file); 186 continue; 187 } 188 } 189 ++pos; 190 } 191 return archive_sp; 192 } 193 194 ObjectContainerBSDArchive::Archive::shared_ptr 195 ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile 196 ( 197 const FileSpec &file, 198 const ArchSpec &arch, 199 const TimeValue &time, 200 DataExtractor &data 201 ) 202 { 203 shared_ptr archive_sp(new Archive (arch, time)); 204 if (archive_sp) 205 { 206 if (archive_sp->ParseObjects (data) > 0) 207 { 208 Mutex::Locker locker(Archive::GetArchiveCacheMutex ()); 209 Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp)); 210 } 211 else 212 { 213 archive_sp.reset(); 214 } 215 } 216 return archive_sp; 217 } 218 219 ObjectContainerBSDArchive::Archive::Map & 220 ObjectContainerBSDArchive::Archive::GetArchiveCache () 221 { 222 static Archive::Map g_archive_map; 223 return g_archive_map; 224 } 225 226 Mutex & 227 ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex () 228 { 229 static Mutex g_archive_map_mutex (Mutex::eMutexTypeRecursive); 230 return g_archive_map_mutex; 231 } 232 233 234 void 235 ObjectContainerBSDArchive::Initialize() 236 { 237 PluginManager::RegisterPlugin (GetPluginNameStatic(), 238 GetPluginDescriptionStatic(), 239 CreateInstance); 240 } 241 242 void 243 ObjectContainerBSDArchive::Terminate() 244 { 245 PluginManager::UnregisterPlugin (CreateInstance); 246 } 247 248 249 const char * 250 ObjectContainerBSDArchive::GetPluginNameStatic() 251 { 252 return "object-container.bsd-archive"; 253 } 254 255 const char * 256 ObjectContainerBSDArchive::GetPluginDescriptionStatic() 257 { 258 return "BSD Archive object container reader."; 259 } 260 261 262 ObjectContainer * 263 ObjectContainerBSDArchive::CreateInstance 264 ( 265 Module* module, 266 DataBufferSP& data_sp, 267 const FileSpec *file, 268 addr_t offset, 269 addr_t length) 270 { 271 if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data_sp)) 272 { 273 Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, module->GetArchitecture(), module->GetModificationTime())); 274 275 if (archive_sp) 276 { 277 // We already have this archive in our cache, use it 278 std::auto_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module, data_sp, file, offset, length)); 279 if (container_ap.get()) 280 { 281 container_ap->SetArchive (archive_sp); 282 return container_ap.release(); 283 } 284 } 285 286 // Read everything since we need that in order to index all the 287 // objects in the archive 288 data_sp = file->MemoryMapFileContents (offset, length); 289 290 std::auto_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module, data_sp, file, offset, length)); 291 if (container_ap->ParseHeader()) 292 return container_ap.release(); 293 } 294 return NULL; 295 } 296 297 298 299 bool 300 ObjectContainerBSDArchive::MagicBytesMatch (DataBufferSP& dataSP) 301 { 302 DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4); 303 uint32_t offset = 0; 304 const char* armag = (const char* )data.PeekData (offset, sizeof(ar_hdr)); 305 if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) 306 { 307 armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG; 308 if (strncmp(armag, ARFMAG, 2) == 0) 309 return true; 310 } 311 return false; 312 } 313 314 ObjectContainerBSDArchive::ObjectContainerBSDArchive 315 ( 316 Module* module, 317 DataBufferSP& dataSP, 318 const lldb_private::FileSpec *file, 319 lldb::addr_t offset, 320 lldb::addr_t size 321 ) : 322 ObjectContainer (module, file, offset, size, dataSP), 323 m_archive_sp () 324 { 325 } 326 void 327 ObjectContainerBSDArchive::SetArchive (Archive::shared_ptr &archive_sp) 328 { 329 m_archive_sp = archive_sp; 330 } 331 332 333 334 ObjectContainerBSDArchive::~ObjectContainerBSDArchive() 335 { 336 } 337 338 bool 339 ObjectContainerBSDArchive::ParseHeader () 340 { 341 if (m_archive_sp.get() == NULL) 342 { 343 if (m_data.GetByteSize() > 0) 344 { 345 m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file, 346 m_module->GetArchitecture(), 347 m_module->GetModificationTime(), 348 m_data); 349 // The archive might be huge, so clear "m_data" to free up the 350 // memory since it will contain the entire file (possibly more than 351 // one architecture slice). We already have an index of all objects 352 // in the file, so we will be ready to serve up those objects. 353 m_data.Clear(); 354 } 355 } 356 return m_archive_sp.get() != NULL; 357 } 358 359 void 360 ObjectContainerBSDArchive::Dump (Stream *s) const 361 { 362 s->Printf("%p: ", this); 363 s->Indent(); 364 const size_t num_archs = GetNumArchitectures(); 365 const size_t num_objects = GetNumObjects(); 366 s->Printf("ObjectContainerBSDArchive, num_archs = %lu, num_objects = %lu", num_archs, num_objects); 367 uint32_t i; 368 ArchSpec arch; 369 s->IndentMore(); 370 for (i=0; i<num_archs; i++) 371 { 372 s->Indent(); 373 GetArchitectureAtIndex(i, arch); 374 s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); 375 } 376 for (i=0; i<num_objects; i++) 377 { 378 s->Indent(); 379 s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex (i)); 380 } 381 s->IndentLess(); 382 s->EOL(); 383 } 384 385 ObjectFileSP 386 ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file) 387 { 388 if (m_module->GetObjectName() && m_archive_sp) 389 { 390 Object *object = m_archive_sp->FindObject (m_module->GetObjectName()); 391 if (object) 392 return ObjectFile::FindPlugin (m_module, file, m_offset + object->ar_file_offset, object->ar_file_size); 393 } 394 return ObjectFileSP(); 395 } 396 397 398 //------------------------------------------------------------------ 399 // PluginInterface protocol 400 //------------------------------------------------------------------ 401 const char * 402 ObjectContainerBSDArchive::GetPluginName() 403 { 404 return "object-container.bsd-archive"; 405 } 406 407 const char * 408 ObjectContainerBSDArchive::GetShortPluginName() 409 { 410 return GetPluginNameStatic(); 411 } 412 413 uint32_t 414 ObjectContainerBSDArchive::GetPluginVersion() 415 { 416 return 1; 417 } 418 419