diff options
Diffstat (limited to 'lldb/source/Plugins/ObjectContainer')
4 files changed, 789 insertions, 1054 deletions
diff --git a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp index 21435a99636..50961e933ba 100644 --- a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp +++ b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp @@ -11,18 +11,17 @@ #if defined(_WIN32) || defined(__ANDROID_NDK__) // Defines from ar, missing on Windows -#define ARMAG "!<arch>\n" -#define SARMAG 8 -#define ARFMAG "`\n" - -typedef struct ar_hdr -{ - char ar_name[16]; - char ar_date[12]; - char ar_uid[6], ar_gid[6]; - char ar_mode[8]; - char ar_size[10]; - char ar_fmag[2]; +#define ARMAG "!<arch>\n" +#define SARMAG 8 +#define ARFMAG "`\n" + +typedef struct ar_hdr { + char ar_name[16]; + char ar_date[12]; + char ar_uid[6], ar_gid[6]; + char ar_mode[8]; + char ar_size[10]; + char ar_fmag[2]; } ar_hdr; #else #include <ar.h> @@ -40,591 +39,473 @@ typedef struct ar_hdr using namespace lldb; using namespace lldb_private; - - -ObjectContainerBSDArchive::Object::Object() : - ar_name(), - ar_date(0), - ar_uid(0), - ar_gid(0), - ar_mode(0), - ar_size(0), - ar_file_offset(0), - ar_file_size(0) -{ -} - -void -ObjectContainerBSDArchive::Object::Clear() -{ - ar_name.Clear(); - ar_date = 0; - ar_uid = 0; - ar_gid = 0; - ar_mode = 0; - ar_size = 0; - ar_file_offset = 0; - ar_file_size = 0; +ObjectContainerBSDArchive::Object::Object() + : ar_name(), ar_date(0), ar_uid(0), ar_gid(0), ar_mode(0), ar_size(0), + ar_file_offset(0), ar_file_size(0) {} + +void ObjectContainerBSDArchive::Object::Clear() { + ar_name.Clear(); + ar_date = 0; + ar_uid = 0; + ar_gid = 0; + ar_mode = 0; + ar_size = 0; + ar_file_offset = 0; + ar_file_size = 0; } lldb::offset_t -ObjectContainerBSDArchive::Object::Extract (const DataExtractor& data, lldb::offset_t offset) -{ - size_t ar_name_len = 0; - std::string str; - char *err; - - - // File header - // - // The common format is as follows. - // - // Offset Length Name Format - // 0 16 File name ASCII right padded with spaces (no spaces allowed in file name) - // 16 12 File mod Decimal as cstring right padded with spaces - // 28 6 Owner ID Decimal as cstring right padded with spaces - // 34 6 Group ID Decimal as cstring right padded with spaces - // 40 8 File mode Octal as cstring right padded with spaces - // 48 10 File byte size Decimal as cstring right padded with spaces - // 58 2 File magic 0x60 0x0A - - // Make sure there is enough data for the file header and bail if not - if (!data.ValidOffsetForDataOfSize(offset, 60)) - return LLDB_INVALID_OFFSET; +ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data, + lldb::offset_t offset) { + size_t ar_name_len = 0; + std::string str; + char *err; + + // File header + // + // The common format is as follows. + // + // Offset Length Name Format + // 0 16 File name ASCII right padded with spaces (no spaces + // allowed in file name) + // 16 12 File mod Decimal as cstring right padded with + // spaces + // 28 6 Owner ID Decimal as cstring right padded with + // spaces + // 34 6 Group ID Decimal as cstring right padded with + // spaces + // 40 8 File mode Octal as cstring right padded with + // spaces + // 48 10 File byte size Decimal as cstring right padded with + // spaces + // 58 2 File magic 0x60 0x0A + + // Make sure there is enough data for the file header and bail if not + if (!data.ValidOffsetForDataOfSize(offset, 60)) + return LLDB_INVALID_OFFSET; - str.assign ((const char *)data.GetData(&offset, 16), 16); - if (str.find("#1/") == 0) - { - // If the name is longer than 16 bytes, or contains an embedded space - // then it will use this format where the length of the name is - // here and the name characters are after this header. - ar_name_len = strtoul(str.c_str() + 3, &err, 10); - } - else - { - // Strip off any trailing spaces. - const size_t last_pos = str.find_last_not_of(' '); - if (last_pos != std::string::npos) - { - if (last_pos + 1 < 16) - str.erase (last_pos + 1); - } - ar_name.SetCString(str.c_str()); + str.assign((const char *)data.GetData(&offset, 16), 16); + if (str.find("#1/") == 0) { + // If the name is longer than 16 bytes, or contains an embedded space + // then it will use this format where the length of the name is + // here and the name characters are after this header. + ar_name_len = strtoul(str.c_str() + 3, &err, 10); + } else { + // Strip off any trailing spaces. + const size_t last_pos = str.find_last_not_of(' '); + if (last_pos != std::string::npos) { + if (last_pos + 1 < 16) + str.erase(last_pos + 1); } + ar_name.SetCString(str.c_str()); + } - str.assign ((const char *)data.GetData(&offset, 12), 12); - ar_date = strtoul(str.c_str(), &err, 10); + str.assign((const char *)data.GetData(&offset, 12), 12); + ar_date = strtoul(str.c_str(), &err, 10); - str.assign ((const char *)data.GetData(&offset, 6), 6); - ar_uid = strtoul(str.c_str(), &err, 10); + str.assign((const char *)data.GetData(&offset, 6), 6); + ar_uid = strtoul(str.c_str(), &err, 10); - str.assign ((const char *)data.GetData(&offset, 6), 6); - ar_gid = strtoul(str.c_str(), &err, 10); + str.assign((const char *)data.GetData(&offset, 6), 6); + ar_gid = strtoul(str.c_str(), &err, 10); - str.assign ((const char *)data.GetData(&offset, 8), 8); - ar_mode = strtoul(str.c_str(), &err, 8); + str.assign((const char *)data.GetData(&offset, 8), 8); + ar_mode = strtoul(str.c_str(), &err, 8); - str.assign ((const char *)data.GetData(&offset, 10), 10); - ar_size = strtoul(str.c_str(), &err, 10); + str.assign((const char *)data.GetData(&offset, 10), 10); + ar_size = strtoul(str.c_str(), &err, 10); - str.assign ((const char *)data.GetData(&offset, 2), 2); - if (str == ARFMAG) - { - if (ar_name_len > 0) - { - const void *ar_name_ptr = data.GetData(&offset, ar_name_len); - // Make sure there was enough data for the string value and bail if not - if (ar_name_ptr == NULL) - return LLDB_INVALID_OFFSET; - str.assign ((const char *)ar_name_ptr, ar_name_len); - ar_name.SetCString (str.c_str()); - } - ar_file_offset = offset; - ar_file_size = ar_size - ar_name_len; - return offset; + str.assign((const char *)data.GetData(&offset, 2), 2); + if (str == ARFMAG) { + if (ar_name_len > 0) { + const void *ar_name_ptr = data.GetData(&offset, ar_name_len); + // Make sure there was enough data for the string value and bail if not + if (ar_name_ptr == NULL) + return LLDB_INVALID_OFFSET; + str.assign((const char *)ar_name_ptr, ar_name_len); + ar_name.SetCString(str.c_str()); } - return LLDB_INVALID_OFFSET; -} - -ObjectContainerBSDArchive::Archive::Archive -( - const lldb_private::ArchSpec &arch, - const lldb_private::TimeValue &time, - lldb::offset_t file_offset, - lldb_private::DataExtractor &data -) : - m_arch (arch), - m_time (time), - m_file_offset (file_offset), - m_objects(), - m_data (data) -{ + ar_file_offset = offset; + ar_file_size = ar_size - ar_name_len; + return offset; + } + return LLDB_INVALID_OFFSET; } -ObjectContainerBSDArchive::Archive::~Archive () -{ -} - -size_t -ObjectContainerBSDArchive::Archive::ParseObjects () -{ - DataExtractor &data = m_data; - std::string str; - lldb::offset_t offset = 0; - str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG); - if (str == ARMAG) - { - Object obj; - do - { - offset = obj.Extract (data, offset); - if (offset == LLDB_INVALID_OFFSET) - break; - size_t obj_idx = m_objects.size(); - m_objects.push_back(obj); - // Insert all of the C strings out of order for now... - m_object_name_to_index_map.Append (obj.ar_name.GetCString(), obj_idx); - offset += obj.ar_file_size; - obj.Clear(); - } while (data.ValidOffset(offset)); - - // Now sort all of the object name pointers - m_object_name_to_index_map.Sort (); - } - return m_objects.size(); +ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch, + const lldb_private::TimeValue &time, + lldb::offset_t file_offset, + lldb_private::DataExtractor &data) + : m_arch(arch), m_time(time), m_file_offset(file_offset), m_objects(), + m_data(data) {} + +ObjectContainerBSDArchive::Archive::~Archive() {} + +size_t ObjectContainerBSDArchive::Archive::ParseObjects() { + DataExtractor &data = m_data; + std::string str; + lldb::offset_t offset = 0; + str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG); + if (str == ARMAG) { + Object obj; + do { + offset = obj.Extract(data, offset); + if (offset == LLDB_INVALID_OFFSET) + break; + size_t obj_idx = m_objects.size(); + m_objects.push_back(obj); + // Insert all of the C strings out of order for now... + m_object_name_to_index_map.Append(obj.ar_name.GetCString(), obj_idx); + offset += obj.ar_file_size; + obj.Clear(); + } while (data.ValidOffset(offset)); + + // Now sort all of the object name pointers + m_object_name_to_index_map.Sort(); + } + return m_objects.size(); } ObjectContainerBSDArchive::Object * -ObjectContainerBSDArchive::Archive::FindObject (const ConstString &object_name, const TimeValue &object_mod_time) -{ - const ObjectNameToIndexMap::Entry *match = m_object_name_to_index_map.FindFirstValueForName (object_name.GetCString()); - if (match) - { - if (object_mod_time.IsValid()) - { - const uint64_t object_date = object_mod_time.GetAsSecondsSinceJan1_1970(); - if (m_objects[match->value].ar_date == object_date) - return &m_objects[match->value]; - const ObjectNameToIndexMap::Entry *next_match = m_object_name_to_index_map.FindNextValueForName (match); - while (next_match) - { - if (m_objects[next_match->value].ar_date == object_date) - return &m_objects[next_match->value]; - next_match = m_object_name_to_index_map.FindNextValueForName (next_match); - } - } - else - { - return &m_objects[match->value]; - } +ObjectContainerBSDArchive::Archive::FindObject( + const ConstString &object_name, const TimeValue &object_mod_time) { + const ObjectNameToIndexMap::Entry *match = + m_object_name_to_index_map.FindFirstValueForName( + object_name.GetCString()); + if (match) { + if (object_mod_time.IsValid()) { + const uint64_t object_date = object_mod_time.GetAsSecondsSinceJan1_1970(); + if (m_objects[match->value].ar_date == object_date) + return &m_objects[match->value]; + const ObjectNameToIndexMap::Entry *next_match = + m_object_name_to_index_map.FindNextValueForName(match); + while (next_match) { + if (m_objects[next_match->value].ar_date == object_date) + return &m_objects[next_match->value]; + next_match = + m_object_name_to_index_map.FindNextValueForName(next_match); + } + } else { + return &m_objects[match->value]; } - return NULL; + } + return NULL; } - ObjectContainerBSDArchive::Archive::shared_ptr -ObjectContainerBSDArchive::Archive::FindCachedArchive (const FileSpec &file, const ArchSpec &arch, const TimeValue &time, lldb::offset_t file_offset) -{ - std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex()); - shared_ptr archive_sp; - Archive::Map &archive_map = Archive::GetArchiveCache (); - Archive::Map::iterator pos = archive_map.find (file); - // Don't cache a value for "archive_map.end()" below since we might - // delete an archive entry... - while (pos != archive_map.end() && pos->first == file) - { - bool match = true; - if (arch.IsValid() && pos->second->GetArchitecture().IsCompatibleMatch(arch) == false) - match = false; - else if (file_offset != LLDB_INVALID_OFFSET && pos->second->GetFileOffset() != file_offset) - match = false; - if (match) - { - if (pos->second->GetModificationTime() == time) - { - return pos->second; - } - else - { - // We have a file at the same path with the same architecture - // whose modification time doesn't match. It doesn't make sense - // for us to continue to use this BSD archive since we cache only - // the object info which consists of file time info and also the - // file offset and file size of any contained objects. Since - // this information is now out of date, we won't get the correct - // information if we go and extract the file data, so we should - // remove the old and outdated entry. - archive_map.erase (pos); - pos = archive_map.find (file); - continue; // Continue to next iteration so we don't increment pos below... - } - } - ++pos; +ObjectContainerBSDArchive::Archive::FindCachedArchive( + const FileSpec &file, const ArchSpec &arch, const TimeValue &time, + lldb::offset_t file_offset) { + std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex()); + shared_ptr archive_sp; + Archive::Map &archive_map = Archive::GetArchiveCache(); + Archive::Map::iterator pos = archive_map.find(file); + // Don't cache a value for "archive_map.end()" below since we might + // delete an archive entry... + while (pos != archive_map.end() && pos->first == file) { + bool match = true; + if (arch.IsValid() && + pos->second->GetArchitecture().IsCompatibleMatch(arch) == false) + match = false; + else if (file_offset != LLDB_INVALID_OFFSET && + pos->second->GetFileOffset() != file_offset) + match = false; + if (match) { + if (pos->second->GetModificationTime() == time) { + return pos->second; + } else { + // We have a file at the same path with the same architecture + // whose modification time doesn't match. It doesn't make sense + // for us to continue to use this BSD archive since we cache only + // the object info which consists of file time info and also the + // file offset and file size of any contained objects. Since + // this information is now out of date, we won't get the correct + // information if we go and extract the file data, so we should + // remove the old and outdated entry. + archive_map.erase(pos); + pos = archive_map.find(file); + continue; // Continue to next iteration so we don't increment pos + // below... + } } - return archive_sp; + ++pos; + } + return archive_sp; } ObjectContainerBSDArchive::Archive::shared_ptr -ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile -( - const FileSpec &file, - const ArchSpec &arch, - const TimeValue &time, - lldb::offset_t file_offset, - DataExtractor &data -) -{ - shared_ptr archive_sp(new Archive (arch, time, file_offset, data)); - if (archive_sp) - { - const size_t num_objects = archive_sp->ParseObjects (); - if (num_objects > 0) - { - std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex()); - Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp)); - } - else - { - archive_sp.reset(); - } +ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile( + const FileSpec &file, const ArchSpec &arch, const TimeValue &time, + lldb::offset_t file_offset, DataExtractor &data) { + shared_ptr archive_sp(new Archive(arch, time, file_offset, data)); + if (archive_sp) { + const size_t num_objects = archive_sp->ParseObjects(); + if (num_objects > 0) { + std::lock_guard<std::recursive_mutex> guard( + Archive::GetArchiveCacheMutex()); + Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp)); + } else { + archive_sp.reset(); } - return archive_sp; + } + return archive_sp; } ObjectContainerBSDArchive::Archive::Map & -ObjectContainerBSDArchive::Archive::GetArchiveCache () -{ - static Archive::Map g_archive_map; - return g_archive_map; +ObjectContainerBSDArchive::Archive::GetArchiveCache() { + static Archive::Map g_archive_map; + return g_archive_map; } std::recursive_mutex & -ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() -{ - static std::recursive_mutex g_archive_map_mutex; - return g_archive_map_mutex; +ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() { + static std::recursive_mutex g_archive_map_mutex; + return g_archive_map_mutex; } -void -ObjectContainerBSDArchive::Initialize() -{ - PluginManager::RegisterPlugin (GetPluginNameStatic(), - GetPluginDescriptionStatic(), - CreateInstance, - GetModuleSpecifications); +void ObjectContainerBSDArchive::Initialize() { + PluginManager::RegisterPlugin(GetPluginNameStatic(), + GetPluginDescriptionStatic(), CreateInstance, + GetModuleSpecifications); } -void -ObjectContainerBSDArchive::Terminate() -{ - PluginManager::UnregisterPlugin (CreateInstance); +void ObjectContainerBSDArchive::Terminate() { + PluginManager::UnregisterPlugin(CreateInstance); } - -lldb_private::ConstString -ObjectContainerBSDArchive::GetPluginNameStatic() -{ - static ConstString g_name("bsd-archive"); - return g_name; +lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() { + static ConstString g_name("bsd-archive"); + return g_name; } -const char * -ObjectContainerBSDArchive::GetPluginDescriptionStatic() -{ - return "BSD Archive object container reader."; +const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() { + return "BSD Archive object container reader."; } - -ObjectContainer * -ObjectContainerBSDArchive::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) -{ - ConstString object_name (module_sp->GetObjectName()); - if (object_name) - { - if (data_sp) - { - // We have data, which means this is the first 512 bytes of the file - // Check to see if the magic bytes match and if they do, read the entire - // table of contents for the archive and cache it - DataExtractor data; - data.SetData (data_sp, data_offset, length); - if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) - { - Timer scoped_timer (LLVM_PRETTY_FUNCTION, - "ObjectContainerBSDArchive::CreateInstance (module = %s, file = %p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")", - module_sp->GetFileSpec().GetPath().c_str(), - static_cast<const void*>(file), - static_cast<uint64_t>(file_offset), - static_cast<uint64_t>(length)); - - // Map the entire .a file to be sure that we don't lose any data if the file - // gets updated by a new build while this .a file is being used for debugging - DataBufferSP archive_data_sp (file->MemoryMapFileContentsIfLocal(file_offset, length)); - lldb::offset_t archive_data_offset = 0; - - Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, - module_sp->GetArchitecture(), - module_sp->GetModificationTime(), - file_offset)); - std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp, - archive_data_sp, - archive_data_offset, - file, - file_offset, - length)); - - if (container_ap.get()) - { - if (archive_sp) - { - // We already have this archive in our cache, use it - container_ap->SetArchive (archive_sp); - return container_ap.release(); - } - else if (container_ap->ParseHeader()) - return container_ap.release(); - } - } +ObjectContainer *ObjectContainerBSDArchive::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) { + ConstString object_name(module_sp->GetObjectName()); + if (object_name) { + if (data_sp) { + // We have data, which means this is the first 512 bytes of the file + // Check to see if the magic bytes match and if they do, read the entire + // table of contents for the archive and cache it + DataExtractor data; + data.SetData(data_sp, data_offset, length); + if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) { + Timer scoped_timer( + LLVM_PRETTY_FUNCTION, + "ObjectContainerBSDArchive::CreateInstance (module = %s, file = " + "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")", + module_sp->GetFileSpec().GetPath().c_str(), + static_cast<const void *>(file), static_cast<uint64_t>(file_offset), + static_cast<uint64_t>(length)); + + // Map the entire .a file to be sure that we don't lose any data if the + // file + // gets updated by a new build while this .a file is being used for + // debugging + DataBufferSP archive_data_sp( + file->MemoryMapFileContentsIfLocal(file_offset, length)); + lldb::offset_t archive_data_offset = 0; + + Archive::shared_ptr archive_sp(Archive::FindCachedArchive( + *file, module_sp->GetArchitecture(), + module_sp->GetModificationTime(), file_offset)); + std::unique_ptr<ObjectContainerBSDArchive> container_ap( + new ObjectContainerBSDArchive(module_sp, archive_data_sp, + archive_data_offset, file, + file_offset, length)); + + if (container_ap.get()) { + if (archive_sp) { + // We already have this archive in our cache, use it + container_ap->SetArchive(archive_sp); + return container_ap.release(); + } else if (container_ap->ParseHeader()) + return container_ap.release(); } - else - { - // No data, just check for a cached archive - Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, - module_sp->GetArchitecture(), - module_sp->GetModificationTime(), - file_offset)); - if (archive_sp) - { - std::unique_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp, data_sp, data_offset, file, file_offset, length)); - - if (container_ap.get()) - { - // We already have this archive in our cache, use it - container_ap->SetArchive (archive_sp); - return container_ap.release(); - } - } + } + } else { + // No data, just check for a cached archive + Archive::shared_ptr archive_sp(Archive::FindCachedArchive( + *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(), + file_offset)); + if (archive_sp) { + std::unique_ptr<ObjectContainerBSDArchive> container_ap( + new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file, + file_offset, length)); + + if (container_ap.get()) { + // We already have this archive in our cache, use it + container_ap->SetArchive(archive_sp); + return container_ap.release(); } + } } - return NULL; + } + return NULL; } - - -bool -ObjectContainerBSDArchive::MagicBytesMatch (const DataExtractor &data) -{ - uint32_t offset = 0; - const char* armag = (const char* )data.PeekData (offset, sizeof(ar_hdr)); - if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) - { - armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG; - if (strncmp(armag, ARFMAG, 2) == 0) - return true; - } - return false; +bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) { + uint32_t offset = 0; + const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr)); + if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) { + armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG; + if (strncmp(armag, ARFMAG, 2) == 0) + return true; + } + return false; } -ObjectContainerBSDArchive::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 -) : - ObjectContainer (module_sp, file, file_offset, size, data_sp, data_offset), - m_archive_sp () -{ -} -void -ObjectContainerBSDArchive::SetArchive (Archive::shared_ptr &archive_sp) -{ - m_archive_sp = archive_sp; +ObjectContainerBSDArchive::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) + : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset), + m_archive_sp() {} +void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) { + m_archive_sp = archive_sp; } - - -ObjectContainerBSDArchive::~ObjectContainerBSDArchive() -{ -} - -bool -ObjectContainerBSDArchive::ParseHeader () -{ - if (m_archive_sp.get() == NULL) - { - if (m_data.GetByteSize() > 0) - { - ModuleSP module_sp (GetModule()); - if (module_sp) - { - m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file, - module_sp->GetArchitecture(), - module_sp->GetModificationTime(), - m_offset, - m_data); - } - // Clear the m_data that contains the entire archive - // data and let our m_archive_sp hold onto the data. - m_data.Clear(); - } +ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {} + +bool ObjectContainerBSDArchive::ParseHeader() { + if (m_archive_sp.get() == NULL) { + if (m_data.GetByteSize() > 0) { + ModuleSP module_sp(GetModule()); + if (module_sp) { + m_archive_sp = Archive::ParseAndCacheArchiveForFile( + m_file, module_sp->GetArchitecture(), + module_sp->GetModificationTime(), m_offset, m_data); + } + // Clear the m_data that contains the entire archive + // data and let our m_archive_sp hold onto the data. + m_data.Clear(); } - return m_archive_sp.get() != NULL; + } + return m_archive_sp.get() != NULL; } -void -ObjectContainerBSDArchive::Dump (Stream *s) const -{ - s->Printf("%p: ", static_cast<const void*>(this)); +void ObjectContainerBSDArchive::Dump(Stream *s) const { + s->Printf("%p: ", static_cast<const void *>(this)); + s->Indent(); + const size_t num_archs = GetNumArchitectures(); + const size_t num_objects = GetNumObjects(); + s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64 + ", num_objects = %" PRIu64 "", + (uint64_t)num_archs, (uint64_t)num_objects); + uint32_t i; + ArchSpec arch; + s->IndentMore(); + for (i = 0; i < num_archs; i++) { s->Indent(); - const size_t num_archs = GetNumArchitectures(); - const size_t num_objects = GetNumObjects(); - s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64 ", num_objects = %" PRIu64 "", (uint64_t)num_archs, (uint64_t)num_objects); - uint32_t i; - ArchSpec arch; - s->IndentMore(); - for (i=0; i<num_archs; i++) - { - s->Indent(); - GetArchitectureAtIndex(i, arch); - s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); - } - for (i=0; i<num_objects; i++) - { - s->Indent(); - s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex (i)); - } - s->IndentLess(); - s->EOL(); + GetArchitectureAtIndex(i, arch); + s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); + } + for (i = 0; i < num_objects; i++) { + s->Indent(); + s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i)); + } + s->IndentLess(); + s->EOL(); } -ObjectFileSP -ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file) -{ - ModuleSP module_sp (GetModule()); - if (module_sp) - { - if (module_sp->GetObjectName() && m_archive_sp) - { - Object *object = m_archive_sp->FindObject (module_sp->GetObjectName(), - module_sp->GetObjectModificationTime()); - if (object) - { - lldb::offset_t data_offset = object->ar_file_offset; - return ObjectFile::FindPlugin (module_sp, - file, - m_offset + object->ar_file_offset, - object->ar_file_size, - m_archive_sp->GetData().GetSharedDataBuffer(), - data_offset); - } - } +ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) { + ModuleSP module_sp(GetModule()); + if (module_sp) { + if (module_sp->GetObjectName() && m_archive_sp) { + Object *object = m_archive_sp->FindObject( + module_sp->GetObjectName(), module_sp->GetObjectModificationTime()); + if (object) { + lldb::offset_t data_offset = object->ar_file_offset; + return ObjectFile::FindPlugin( + module_sp, file, m_offset + object->ar_file_offset, + object->ar_file_size, m_archive_sp->GetData().GetSharedDataBuffer(), + data_offset); + } } - return ObjectFileSP(); + } + return ObjectFileSP(); } - //------------------------------------------------------------------ // PluginInterface protocol //------------------------------------------------------------------ -lldb_private::ConstString -ObjectContainerBSDArchive::GetPluginName() -{ - return GetPluginNameStatic(); -} - -uint32_t -ObjectContainerBSDArchive::GetPluginVersion() -{ - return 1; +lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() { + return GetPluginNameStatic(); } +uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; } + +size_t ObjectContainerBSDArchive::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) { + + // We have data, which means this is the first 512 bytes of the file + // Check to see if the magic bytes match and if they do, read the entire + // table of contents for the archive and cache it + DataExtractor data; + data.SetData(data_sp, data_offset, data_sp->GetByteSize()); + if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) { + const size_t initial_count = specs.GetSize(); + TimeValue file_mod_time = file.GetModificationTime(); + Archive::shared_ptr archive_sp(Archive::FindCachedArchive( + file, ArchSpec(), file_mod_time, file_offset)); + bool set_archive_arch = false; + if (!archive_sp) { + set_archive_arch = true; + DataBufferSP data_sp( + file.MemoryMapFileContentsIfLocal(file_offset, file_size)); + data.SetData(data_sp, 0, data_sp->GetByteSize()); + archive_sp = Archive::ParseAndCacheArchiveForFile( + file, ArchSpec(), file_mod_time, file_offset, data); + } -size_t -ObjectContainerBSDArchive::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) -{ - - // We have data, which means this is the first 512 bytes of the file - // Check to see if the magic bytes match and if they do, read the entire - // table of contents for the archive and cache it - DataExtractor data; - data.SetData (data_sp, data_offset, data_sp->GetByteSize()); - if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) - { - const size_t initial_count = specs.GetSize(); - TimeValue file_mod_time = file.GetModificationTime(); - Archive::shared_ptr archive_sp (Archive::FindCachedArchive (file, ArchSpec(), file_mod_time, file_offset)); - bool set_archive_arch = false; - if (!archive_sp) - { - set_archive_arch = true; - DataBufferSP data_sp (file.MemoryMapFileContentsIfLocal(file_offset, file_size)); - data.SetData (data_sp, 0, data_sp->GetByteSize()); - archive_sp = Archive::ParseAndCacheArchiveForFile(file, ArchSpec(), file_mod_time, file_offset, data); - } - - if (archive_sp) - { - const size_t num_objects = archive_sp->GetNumObjects(); - for (size_t idx = 0; idx < num_objects; ++idx) - { - const Object *object = archive_sp->GetObjectAtIndex (idx); - if (object) - { - const lldb::offset_t object_file_offset = file_offset + object->ar_file_offset; - if (object->ar_file_offset < file_size && file_size > object_file_offset) - { - if (ObjectFile::GetModuleSpecifications(file, - object_file_offset, - file_size - object_file_offset, - specs)) - { - ModuleSpec &spec = specs.GetModuleSpecRefAtIndex (specs.GetSize() - 1); - TimeValue object_mod_time; - object_mod_time.OffsetWithSeconds(object->ar_date); - spec.GetObjectName () = object->ar_name; - spec.SetObjectOffset (object_file_offset); - spec.SetObjectSize (file_size - object_file_offset); - spec.GetObjectModificationTime () = object_mod_time; - } - } - } + if (archive_sp) { + const size_t num_objects = archive_sp->GetNumObjects(); + for (size_t idx = 0; idx < num_objects; ++idx) { + const Object *object = archive_sp->GetObjectAtIndex(idx); + if (object) { + const lldb::offset_t object_file_offset = + file_offset + object->ar_file_offset; + if (object->ar_file_offset < file_size && + file_size > object_file_offset) { + if (ObjectFile::GetModuleSpecifications( + file, object_file_offset, file_size - object_file_offset, + specs)) { + ModuleSpec &spec = + specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1); + TimeValue object_mod_time; + object_mod_time.OffsetWithSeconds(object->ar_date); + spec.GetObjectName() = object->ar_name; + spec.SetObjectOffset(object_file_offset); + spec.SetObjectSize(file_size - object_file_offset); + spec.GetObjectModificationTime() = object_mod_time; } + } } - const size_t end_count = specs.GetSize(); - size_t num_specs_added = end_count - initial_count; - if (set_archive_arch && num_specs_added > 0) - { - // The archive was created but we didn't have an architecture - // so we need to set it - for (size_t i=initial_count; i<end_count; ++ i) - { - ModuleSpec module_spec; - if (specs.GetModuleSpecAtIndex(i, module_spec)) - { - if (module_spec.GetArchitecture().IsValid()) - { - archive_sp->SetArchitecture (module_spec.GetArchitecture()); - break; - } - } - } + } + } + const size_t end_count = specs.GetSize(); + size_t num_specs_added = end_count - initial_count; + if (set_archive_arch && num_specs_added > 0) { + // The archive was created but we didn't have an architecture + // so we need to set it + for (size_t i = initial_count; i < end_count; ++i) { + ModuleSpec module_spec; + if (specs.GetModuleSpecAtIndex(i, module_spec)) { + if (module_spec.GetArchitecture().IsValid()) { + archive_sp->SetArchitecture(module_spec.GetArchitecture()); + break; + } } - return num_specs_added; + } } - return 0; + return num_specs_added; + } + return 0; } diff --git a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h index 03b0bf3e2f0..0212772a4c3 100644 --- a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h +++ b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h @@ -16,217 +16,161 @@ // Other libraries and framework includes // Project includes -#include "lldb/Symbol/ObjectContainer.h" #include "lldb/Core/ArchSpec.h" #include "lldb/Core/ConstString.h" -#include "lldb/Host/FileSpec.h" #include "lldb/Core/UniqueCStringMap.h" +#include "lldb/Host/FileSpec.h" #include "lldb/Host/TimeValue.h" +#include "lldb/Symbol/ObjectContainer.h" -class ObjectContainerBSDArchive : - public lldb_private::ObjectContainer -{ +class ObjectContainerBSDArchive : public lldb_private::ObjectContainer { public: - 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); - - ~ObjectContainerBSDArchive() override; - - //------------------------------------------------------------------ - // Static Functions - //------------------------------------------------------------------ - static void - Initialize(); - - static void - Terminate(); - - static lldb_private::ConstString - GetPluginNameStatic(); - - static const char * - GetPluginDescriptionStatic(); - - 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 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 bool - MagicBytesMatch (const lldb_private::DataExtractor &data); - - //------------------------------------------------------------------ - // Member Functions - //------------------------------------------------------------------ - bool - ParseHeader() override; - - size_t - GetNumObjects() const override - { - if (m_archive_sp) - return m_archive_sp->GetNumObjects(); - return 0; - } + 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); + + ~ObjectContainerBSDArchive() override; + + //------------------------------------------------------------------ + // Static Functions + //------------------------------------------------------------------ + static void Initialize(); + + static void Terminate(); + + static lldb_private::ConstString GetPluginNameStatic(); + + static const char *GetPluginDescriptionStatic(); - void - Dump(lldb_private::Stream *s) const override; + 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); - lldb::ObjectFileSP - GetObjectFile(const lldb_private::FileSpec *file) override; + 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); - //------------------------------------------------------------------ - // PluginInterface protocol - //------------------------------------------------------------------ - lldb_private::ConstString - GetPluginName() override; + static bool MagicBytesMatch(const lldb_private::DataExtractor &data); - uint32_t - GetPluginVersion() override; + //------------------------------------------------------------------ + // Member Functions + //------------------------------------------------------------------ + bool ParseHeader() override; + + size_t GetNumObjects() const override { + if (m_archive_sp) + return m_archive_sp->GetNumObjects(); + return 0; + } + + void Dump(lldb_private::Stream *s) const override; + + lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override; + + //------------------------------------------------------------------ + // PluginInterface protocol + //------------------------------------------------------------------ + lldb_private::ConstString GetPluginName() override; + + uint32_t GetPluginVersion() override; protected: - struct Object - { - Object(); - - void - Clear(); - - lldb::offset_t - Extract (const lldb_private::DataExtractor& data, lldb::offset_t offset); - - lldb_private::ConstString ar_name; // name - uint32_t ar_date; // modification time - uint16_t ar_uid; // user id - uint16_t ar_gid; // group id - uint16_t ar_mode; // octal file permissions - uint32_t ar_size; // size in bytes - lldb::offset_t ar_file_offset; // file offset in bytes from the beginning of the file of the object data - lldb::offset_t ar_file_size; // length of the object data - - typedef std::vector<Object> collection; - typedef collection::iterator iterator; - typedef collection::const_iterator const_iterator; - }; - - class Archive - { - public: - typedef std::shared_ptr<Archive> shared_ptr; - typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map; - - Archive(const lldb_private::ArchSpec &arch, - const lldb_private::TimeValue &mod_time, - lldb::offset_t file_offset, - lldb_private::DataExtractor &data); - - ~Archive(); - - static Map & - GetArchiveCache (); - - static std::recursive_mutex & - GetArchiveCacheMutex(); - - static Archive::shared_ptr - FindCachedArchive (const lldb_private::FileSpec &file, - const lldb_private::ArchSpec &arch, - const lldb_private::TimeValue &mod_time, - lldb::offset_t file_offset); - - static Archive::shared_ptr - ParseAndCacheArchiveForFile (const lldb_private::FileSpec &file, - const lldb_private::ArchSpec &arch, - const lldb_private::TimeValue &mod_time, - lldb::offset_t file_offset, - lldb_private::DataExtractor &data); - - size_t - GetNumObjects () const - { - return m_objects.size(); - } - - const Object * - GetObjectAtIndex (size_t idx) - { - if (idx < m_objects.size()) - return &m_objects[idx]; - return NULL; - } - - size_t - ParseObjects (); - - Object * - FindObject (const lldb_private::ConstString &object_name, - const lldb_private::TimeValue &object_mod_time); - - lldb::offset_t - GetFileOffset () const - { - return m_file_offset; - } - - const lldb_private::TimeValue & - GetModificationTime() - { - return m_time; - } - - const lldb_private::ArchSpec & - GetArchitecture () const - { - return m_arch; - } - - void - SetArchitecture (const lldb_private::ArchSpec &arch) - { - m_arch = arch; - } - - bool - HasNoExternalReferences() const; - - lldb_private::DataExtractor & - GetData () - { - return m_data; - } - - protected: - typedef lldb_private::UniqueCStringMap<uint32_t> ObjectNameToIndexMap; - //---------------------------------------------------------------------- - // Member Variables - //---------------------------------------------------------------------- - lldb_private::ArchSpec m_arch; - lldb_private::TimeValue m_time; - lldb::offset_t m_file_offset; - Object::collection m_objects; - ObjectNameToIndexMap m_object_name_to_index_map; - lldb_private::DataExtractor m_data; ///< The data for this object container so we don't lose data if the .a files gets modified - }; - - void - SetArchive (Archive::shared_ptr &archive_sp); - - Archive::shared_ptr m_archive_sp; + struct Object { + Object(); + + void Clear(); + + lldb::offset_t Extract(const lldb_private::DataExtractor &data, + lldb::offset_t offset); + + lldb_private::ConstString ar_name; // name + uint32_t ar_date; // modification time + uint16_t ar_uid; // user id + uint16_t ar_gid; // group id + uint16_t ar_mode; // octal file permissions + uint32_t ar_size; // size in bytes + lldb::offset_t ar_file_offset; // file offset in bytes from the beginning of + // the file of the object data + lldb::offset_t ar_file_size; // length of the object data + + typedef std::vector<Object> collection; + typedef collection::iterator iterator; + typedef collection::const_iterator const_iterator; + }; + + class Archive { + public: + typedef std::shared_ptr<Archive> shared_ptr; + typedef std::multimap<lldb_private::FileSpec, shared_ptr> Map; + + Archive(const lldb_private::ArchSpec &arch, + const lldb_private::TimeValue &mod_time, lldb::offset_t file_offset, + lldb_private::DataExtractor &data); + + ~Archive(); + + static Map &GetArchiveCache(); + + static std::recursive_mutex &GetArchiveCacheMutex(); + + static Archive::shared_ptr FindCachedArchive( + const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch, + const lldb_private::TimeValue &mod_time, lldb::offset_t file_offset); + + static Archive::shared_ptr ParseAndCacheArchiveForFile( + const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch, + const lldb_private::TimeValue &mod_time, lldb::offset_t file_offset, + lldb_private::DataExtractor &data); + + size_t GetNumObjects() const { return m_objects.size(); } + + const Object *GetObjectAtIndex(size_t idx) { + if (idx < m_objects.size()) + return &m_objects[idx]; + return NULL; + } + + size_t ParseObjects(); + + Object *FindObject(const lldb_private::ConstString &object_name, + const lldb_private::TimeValue &object_mod_time); + + lldb::offset_t GetFileOffset() const { return m_file_offset; } + + const lldb_private::TimeValue &GetModificationTime() { return m_time; } + + const lldb_private::ArchSpec &GetArchitecture() const { return m_arch; } + + void SetArchitecture(const lldb_private::ArchSpec &arch) { m_arch = arch; } + + bool HasNoExternalReferences() const; + + lldb_private::DataExtractor &GetData() { return m_data; } + + protected: + typedef lldb_private::UniqueCStringMap<uint32_t> ObjectNameToIndexMap; + //---------------------------------------------------------------------- + // Member Variables + //---------------------------------------------------------------------- + lldb_private::ArchSpec m_arch; + lldb_private::TimeValue m_time; + lldb::offset_t m_file_offset; + Object::collection m_objects; + ObjectNameToIndexMap m_object_name_to_index_map; + lldb_private::DataExtractor m_data; ///< The data for this object container + ///so we don't lose data if the .a files + ///gets modified + }; + + void SetArchive(Archive::shared_ptr &archive_sp); + + Archive::shared_ptr m_archive_sp; }; #endif // liblldb_ObjectContainerBSDArchive_h_ diff --git a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp index 7497b987eba..7d9e30ef031 100644 --- a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp +++ b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp @@ -21,290 +21,219 @@ using namespace lldb; using namespace lldb_private; using namespace llvm::MachO; -void -ObjectContainerUniversalMachO::Initialize() -{ - PluginManager::RegisterPlugin (GetPluginNameStatic(), - GetPluginDescriptionStatic(), - CreateInstance, - GetModuleSpecifications); +void ObjectContainerUniversalMachO::Initialize() { + PluginManager::RegisterPlugin(GetPluginNameStatic(), + GetPluginDescriptionStatic(), CreateInstance, + GetModuleSpecifications); } -void -ObjectContainerUniversalMachO::Terminate() -{ - PluginManager::UnregisterPlugin (CreateInstance); +void ObjectContainerUniversalMachO::Terminate() { + PluginManager::UnregisterPlugin(CreateInstance); } - -lldb_private::ConstString -ObjectContainerUniversalMachO::GetPluginNameStatic() -{ - static ConstString g_name("mach-o"); - return g_name; +lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginNameStatic() { + static ConstString g_name("mach-o"); + return g_name; } -const char * -ObjectContainerUniversalMachO::GetPluginDescriptionStatic() -{ - return "Universal mach-o object container reader."; +const char *ObjectContainerUniversalMachO::GetPluginDescriptionStatic() { + return "Universal mach-o object container reader."; } - -ObjectContainer * -ObjectContainerUniversalMachO::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 -) -{ - // We get data when we aren't trying to look for cached container information, - // so only try and look for an architecture slice if we get data - if (data_sp) - { - DataExtractor data; - data.SetData (data_sp, data_offset, length); - if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) - { - std::unique_ptr<ObjectContainerUniversalMachO> container_ap(new ObjectContainerUniversalMachO (module_sp, data_sp, data_offset, file, file_offset, length)); - if (container_ap->ParseHeader()) - { - return container_ap.release(); - } - } +ObjectContainer *ObjectContainerUniversalMachO::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) { + // We get data when we aren't trying to look for cached container information, + // so only try and look for an architecture slice if we get data + if (data_sp) { + DataExtractor data; + data.SetData(data_sp, data_offset, length); + if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) { + std::unique_ptr<ObjectContainerUniversalMachO> container_ap( + new ObjectContainerUniversalMachO(module_sp, data_sp, data_offset, + file, file_offset, length)); + if (container_ap->ParseHeader()) { + return container_ap.release(); + } } - return NULL; + } + return NULL; } -bool -ObjectContainerUniversalMachO::MagicBytesMatch (const DataExtractor &data) -{ - lldb::offset_t offset = 0; - uint32_t magic = data.GetU32(&offset); - return magic == FAT_MAGIC || magic == FAT_CIGAM; +bool ObjectContainerUniversalMachO::MagicBytesMatch(const DataExtractor &data) { + lldb::offset_t offset = 0; + uint32_t magic = data.GetU32(&offset); + return magic == FAT_MAGIC || magic == FAT_CIGAM; } -ObjectContainerUniversalMachO::ObjectContainerUniversalMachO -( - 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 -) : - ObjectContainer (module_sp, file, file_offset, length, data_sp, data_offset), - m_header(), - m_fat_archs() -{ - memset(&m_header, 0, sizeof(m_header)); +ObjectContainerUniversalMachO::ObjectContainerUniversalMachO( + 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) + : ObjectContainer(module_sp, file, file_offset, length, data_sp, + data_offset), + m_header(), m_fat_archs() { + memset(&m_header, 0, sizeof(m_header)); } +ObjectContainerUniversalMachO::~ObjectContainerUniversalMachO() {} -ObjectContainerUniversalMachO::~ObjectContainerUniversalMachO() -{ +bool ObjectContainerUniversalMachO::ParseHeader() { + bool success = ParseHeader(m_data, m_header, m_fat_archs); + // We no longer need any data, we parsed all we needed to parse + // and cached it in m_header and m_fat_archs + m_data.Clear(); + return success; } -bool -ObjectContainerUniversalMachO::ParseHeader () -{ - bool success = ParseHeader (m_data, m_header, m_fat_archs); - // We no longer need any data, we parsed all we needed to parse - // and cached it in m_header and m_fat_archs - m_data.Clear(); - return success; -} +bool ObjectContainerUniversalMachO::ParseHeader( + lldb_private::DataExtractor &data, llvm::MachO::fat_header &header, + std::vector<llvm::MachO::fat_arch> &fat_archs) { + bool success = false; + // Store the file offset for this universal file as we could have a universal + // .o file + // in a BSD archive, or be contained in another kind of object. + // Universal mach-o files always have their headers in big endian. + lldb::offset_t offset = 0; + data.SetByteOrder(eByteOrderBig); + header.magic = data.GetU32(&offset); + fat_archs.clear(); -bool -ObjectContainerUniversalMachO::ParseHeader (lldb_private::DataExtractor &data, - llvm::MachO::fat_header &header, - std::vector<llvm::MachO::fat_arch> &fat_archs) -{ - bool success = false; - // Store the file offset for this universal file as we could have a universal .o file - // in a BSD archive, or be contained in another kind of object. - // Universal mach-o files always have their headers in big endian. - lldb::offset_t offset = 0; - data.SetByteOrder (eByteOrderBig); - header.magic = data.GetU32(&offset); - fat_archs.clear(); + if (header.magic == FAT_MAGIC) { - if (header.magic == FAT_MAGIC) - { + data.SetAddressByteSize(4); - data.SetAddressByteSize(4); - - header.nfat_arch = data.GetU32(&offset); - - // Now we should have enough data for all of the fat headers, so lets index - // them so we know how many architectures that this universal binary contains. - uint32_t arch_idx = 0; - for (arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) - { - if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) - { - fat_arch arch; - if (data.GetU32(&offset, &arch, sizeof(fat_arch)/sizeof(uint32_t))) - fat_archs.push_back(arch); - } - } - success = true; - } - else - { - memset(&header, 0, sizeof(header)); - } - return success; -} + header.nfat_arch = data.GetU32(&offset); -void -ObjectContainerUniversalMachO::Dump (Stream *s) const -{ - s->Printf("%p: ", static_cast<const void*>(this)); - s->Indent(); - const size_t num_archs = GetNumArchitectures(); - const size_t num_objects = GetNumObjects(); - s->Printf("ObjectContainerUniversalMachO, num_archs = %lu, num_objects = %lu", num_archs, num_objects); - uint32_t i; - ArchSpec arch; - s->IndentMore(); - for (i=0; i<num_archs; i++) - { - s->Indent(); - GetArchitectureAtIndex(i, arch); - s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); - } - for (i=0; i<num_objects; i++) - { - s->Indent(); - s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex (i)); + // Now we should have enough data for all of the fat headers, so lets index + // them so we know how many architectures that this universal binary + // contains. + uint32_t arch_idx = 0; + for (arch_idx = 0; arch_idx < header.nfat_arch; ++arch_idx) { + if (data.ValidOffsetForDataOfSize(offset, sizeof(fat_arch))) { + fat_arch arch; + if (data.GetU32(&offset, &arch, sizeof(fat_arch) / sizeof(uint32_t))) + fat_archs.push_back(arch); + } } - s->IndentLess(); - s->EOL(); + success = true; + } else { + memset(&header, 0, sizeof(header)); + } + return success; +} + +void ObjectContainerUniversalMachO::Dump(Stream *s) const { + s->Printf("%p: ", static_cast<const void *>(this)); + s->Indent(); + const size_t num_archs = GetNumArchitectures(); + const size_t num_objects = GetNumObjects(); + s->Printf("ObjectContainerUniversalMachO, num_archs = %lu, num_objects = %lu", + num_archs, num_objects); + uint32_t i; + ArchSpec arch; + s->IndentMore(); + for (i = 0; i < num_archs; i++) { + s->Indent(); + GetArchitectureAtIndex(i, arch); + s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); + } + for (i = 0; i < num_objects; i++) { + s->Indent(); + s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i)); + } + s->IndentLess(); + s->EOL(); } -size_t -ObjectContainerUniversalMachO::GetNumArchitectures () const -{ - return m_header.nfat_arch; +size_t ObjectContainerUniversalMachO::GetNumArchitectures() const { + return m_header.nfat_arch; } -bool -ObjectContainerUniversalMachO::GetArchitectureAtIndex (uint32_t idx, ArchSpec& arch) const -{ - if (idx < m_header.nfat_arch) - { - arch.SetArchitecture (eArchTypeMachO, m_fat_archs[idx].cputype, m_fat_archs[idx].cpusubtype); - return true; - } - return false; +bool ObjectContainerUniversalMachO::GetArchitectureAtIndex( + uint32_t idx, ArchSpec &arch) const { + if (idx < m_header.nfat_arch) { + arch.SetArchitecture(eArchTypeMachO, m_fat_archs[idx].cputype, + m_fat_archs[idx].cpusubtype); + return true; + } + return false; } ObjectFileSP -ObjectContainerUniversalMachO::GetObjectFile (const FileSpec *file) -{ - uint32_t arch_idx = 0; - ArchSpec arch; - // If the module hasn't specified an architecture yet, set it to the default - // architecture: - ModuleSP module_sp (GetModule()); - if (module_sp) - { - if (!module_sp->GetArchitecture().IsValid()) - { - arch = Target::GetDefaultArchitecture (); - if (!arch.IsValid()) - arch.SetTriple (LLDB_ARCH_DEFAULT); - } - else - arch = module_sp->GetArchitecture(); - - ArchSpec curr_arch; - // First, try to find an exact match for the Arch of the Target. - for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) - { - if (GetArchitectureAtIndex (arch_idx, curr_arch) && arch.IsExactMatch(curr_arch)) - break; - } +ObjectContainerUniversalMachO::GetObjectFile(const FileSpec *file) { + uint32_t arch_idx = 0; + ArchSpec arch; + // If the module hasn't specified an architecture yet, set it to the default + // architecture: + ModuleSP module_sp(GetModule()); + if (module_sp) { + if (!module_sp->GetArchitecture().IsValid()) { + arch = Target::GetDefaultArchitecture(); + if (!arch.IsValid()) + arch.SetTriple(LLDB_ARCH_DEFAULT); + } else + arch = module_sp->GetArchitecture(); + + ArchSpec curr_arch; + // First, try to find an exact match for the Arch of the Target. + for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) { + if (GetArchitectureAtIndex(arch_idx, curr_arch) && + arch.IsExactMatch(curr_arch)) + break; + } - // Failing an exact match, try to find a compatible Arch of the Target. - if (arch_idx >= m_header.nfat_arch) - { - for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) - { - if (GetArchitectureAtIndex (arch_idx, curr_arch) && arch.IsCompatibleMatch(curr_arch)) - break; - } - } + // Failing an exact match, try to find a compatible Arch of the Target. + if (arch_idx >= m_header.nfat_arch) { + for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx) { + if (GetArchitectureAtIndex(arch_idx, curr_arch) && + arch.IsCompatibleMatch(curr_arch)) + break; + } + } - if (arch_idx < m_header.nfat_arch) - { - DataBufferSP data_sp; - lldb::offset_t data_offset = 0; - return ObjectFile::FindPlugin (module_sp, - file, - m_offset + m_fat_archs[arch_idx].offset, - m_fat_archs[arch_idx].size, - data_sp, - data_offset); - } + if (arch_idx < m_header.nfat_arch) { + DataBufferSP data_sp; + lldb::offset_t data_offset = 0; + return ObjectFile::FindPlugin( + module_sp, file, m_offset + m_fat_archs[arch_idx].offset, + m_fat_archs[arch_idx].size, data_sp, data_offset); } - return ObjectFileSP(); + } + return ObjectFileSP(); } - //------------------------------------------------------------------ // PluginInterface protocol //------------------------------------------------------------------ -lldb_private::ConstString -ObjectContainerUniversalMachO::GetPluginName() -{ - return GetPluginNameStatic(); -} - -uint32_t -ObjectContainerUniversalMachO::GetPluginVersion() -{ - return 1; -} - - -size_t -ObjectContainerUniversalMachO::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) -{ - const size_t initial_count = specs.GetSize(); - - DataExtractor data; - data.SetData (data_sp, data_offset, data_sp->GetByteSize()); - - if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) - { - llvm::MachO::fat_header header; - std::vector<llvm::MachO::fat_arch> fat_archs; - if (ParseHeader (data, header, fat_archs)) - { - for (const llvm::MachO::fat_arch &fat_arch : fat_archs) - { - const lldb::offset_t slice_file_offset = fat_arch.offset + file_offset; - if (fat_arch.offset < file_size && file_size > slice_file_offset) - { - ObjectFile::GetModuleSpecifications (file, - slice_file_offset, - file_size - slice_file_offset, - specs); - } - } +lldb_private::ConstString ObjectContainerUniversalMachO::GetPluginName() { + return GetPluginNameStatic(); +} + +uint32_t ObjectContainerUniversalMachO::GetPluginVersion() { return 1; } + +size_t ObjectContainerUniversalMachO::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) { + const size_t initial_count = specs.GetSize(); + + DataExtractor data; + data.SetData(data_sp, data_offset, data_sp->GetByteSize()); + + if (ObjectContainerUniversalMachO::MagicBytesMatch(data)) { + llvm::MachO::fat_header header; + std::vector<llvm::MachO::fat_arch> fat_archs; + if (ParseHeader(data, header, fat_archs)) { + for (const llvm::MachO::fat_arch &fat_arch : fat_archs) { + const lldb::offset_t slice_file_offset = fat_arch.offset + file_offset; + if (fat_arch.offset < file_size && file_size > slice_file_offset) { + ObjectFile::GetModuleSpecifications( + file, slice_file_offset, file_size - slice_file_offset, specs); } + } } - return specs.GetSize() - initial_count; + } + return specs.GetSize() - initial_count; } - diff --git a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h index 162402e4b2b..36542559e08 100644 --- a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h +++ b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h @@ -14,92 +14,73 @@ // C++ Includes // Other libraries and framework includes // Project includes -#include "lldb/Symbol/ObjectContainer.h" #include "lldb/Host/FileSpec.h" +#include "lldb/Symbol/ObjectContainer.h" #include "lldb/Utility/SafeMachO.h" -class ObjectContainerUniversalMachO : - public lldb_private::ObjectContainer -{ +class ObjectContainerUniversalMachO : public lldb_private::ObjectContainer { public: - ObjectContainerUniversalMachO(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); - - ~ObjectContainerUniversalMachO() override; - - //------------------------------------------------------------------ - // Static Functions - //------------------------------------------------------------------ - static void - Initialize(); - - static void - Terminate(); - - static lldb_private::ConstString - GetPluginNameStatic(); - - static const char * - GetPluginDescriptionStatic(); - - 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 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 bool - MagicBytesMatch (const lldb_private::DataExtractor &data); - - //------------------------------------------------------------------ - // Member Functions - //------------------------------------------------------------------ - bool - ParseHeader() override; - - void - Dump(lldb_private::Stream *s) const override; - - size_t - GetNumArchitectures() const override; - - bool - GetArchitectureAtIndex(uint32_t cpu_idx, lldb_private::ArchSpec& arch) const override; - - lldb::ObjectFileSP - GetObjectFile(const lldb_private::FileSpec *file) override; - - //------------------------------------------------------------------ - // PluginInterface protocol - //------------------------------------------------------------------ - lldb_private::ConstString - GetPluginName() override; - - uint32_t - GetPluginVersion() override; + ObjectContainerUniversalMachO(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); + + ~ObjectContainerUniversalMachO() override; + + //------------------------------------------------------------------ + // Static Functions + //------------------------------------------------------------------ + static void Initialize(); + + static void Terminate(); + + static lldb_private::ConstString GetPluginNameStatic(); + + static const char *GetPluginDescriptionStatic(); + + 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 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 bool MagicBytesMatch(const lldb_private::DataExtractor &data); + + //------------------------------------------------------------------ + // Member Functions + //------------------------------------------------------------------ + bool ParseHeader() override; + + void Dump(lldb_private::Stream *s) const override; + + size_t GetNumArchitectures() const override; + + bool GetArchitectureAtIndex(uint32_t cpu_idx, + lldb_private::ArchSpec &arch) const override; + + lldb::ObjectFileSP GetObjectFile(const lldb_private::FileSpec *file) override; + + //------------------------------------------------------------------ + // PluginInterface protocol + //------------------------------------------------------------------ + lldb_private::ConstString GetPluginName() override; + + uint32_t GetPluginVersion() override; protected: - llvm::MachO::fat_header m_header; - std::vector<llvm::MachO::fat_arch> m_fat_archs; - - static bool - ParseHeader (lldb_private::DataExtractor &data, - llvm::MachO::fat_header &header, - std::vector<llvm::MachO::fat_arch> &fat_archs); + llvm::MachO::fat_header m_header; + std::vector<llvm::MachO::fat_arch> m_fat_archs; + + static bool ParseHeader(lldb_private::DataExtractor &data, + llvm::MachO::fat_header &header, + std::vector<llvm::MachO::fat_arch> &fat_archs); }; #endif // liblldb_ObjectContainerUniversalMachO_h_ |