Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Pavel Labath | e705c8b | 2016-12-02 11:15:15 +0000 | [diff] [blame] | 12 | #if defined(_WIN32) || defined(__ANDROID__) |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 13 | // Defines from ar, missing on Windows |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 14 | #define ARMAG "!<arch>\n" |
| 15 | #define SARMAG 8 |
| 16 | #define ARFMAG "`\n" |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 17 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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]; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 25 | } ar_hdr; |
| 26 | #else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include <ar.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 28 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | #include "lldb/Core/ArchSpec.h" |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 31 | #include "lldb/Core/DataBuffer.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "lldb/Core/Module.h" |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 33 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | aecb12b | 2012-01-09 23:59:30 +0000 | [diff] [blame] | 35 | #include "lldb/Core/Timer.h" |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 36 | #include "lldb/Host/FileSystem.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame^] | 38 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace lldb; |
| 41 | using namespace lldb_private; |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 58 | lldb::offset_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 745b668 | 2014-05-02 22:25:51 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Greg Clayton | 745b668 | 2014-05-02 22:25:51 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | // Make sure there is enough data for the file header and bail if not |
| 85 | if (!data.ValidOffsetForDataOfSize(offset, 60)) |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 86 | return LLDB_INVALID_OFFSET; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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, |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 137 | const llvm::sys::TimePoint<> &time, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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... |
Zachary Turner | 4fa098a | 2016-10-06 21:22:44 +0000 | [diff] [blame] | 159 | m_object_name_to_index_map.Append(obj.ar_name.GetStringRef(), obj_idx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ObjectContainerBSDArchive::Object * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | ObjectContainerBSDArchive::Archive::FindObject( |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 172 | const ConstString &object_name, |
| 173 | const llvm::sys::TimePoint<> &object_mod_time) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | const ObjectNameToIndexMap::Entry *match = |
| 175 | m_object_name_to_index_map.FindFirstValueForName( |
Zachary Turner | 4fa098a | 2016-10-06 21:22:44 +0000 | [diff] [blame] | 176 | object_name.GetStringRef()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | if (match) { |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 178 | if (object_mod_time != llvm::sys::TimePoint<>()) { |
| 179 | const uint64_t object_date = llvm::sys::toTimeT(object_mod_time); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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]; |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 192 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | } |
| 194 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | ObjectContainerBSDArchive::Archive::shared_ptr |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | ObjectContainerBSDArchive::Archive::FindCachedArchive( |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 199 | const FileSpec &file, const ArchSpec &arch, |
| 200 | const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | ++pos; |
| 234 | } |
| 235 | return archive_sp; |
| 236 | } |
| 237 | |
| 238 | ObjectContainerBSDArchive::Archive::shared_ptr |
| 239 | ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile( |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 240 | const FileSpec &file, const ArchSpec &arch, |
| 241 | const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset, |
| 242 | DataExtractor &data) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | ObjectContainerBSDArchive::Archive::Map & |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | ObjectContainerBSDArchive::Archive::GetArchiveCache() { |
| 259 | static Archive::Map g_archive_map; |
| 260 | return g_archive_map; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 263 | std::recursive_mutex & |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() { |
| 265 | static std::recursive_mutex g_archive_map_mutex; |
| 266 | return g_archive_map_mutex; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | void ObjectContainerBSDArchive::Initialize() { |
| 270 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 271 | GetPluginDescriptionStatic(), CreateInstance, |
| 272 | GetModuleSpecifications); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 275 | void ObjectContainerBSDArchive::Terminate() { |
| 276 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 279 | lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() { |
| 280 | static ConstString g_name("bsd-archive"); |
| 281 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() { |
| 285 | return "BSD Archive object container reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 316 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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)); |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 324 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 332 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 348 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | } |
| 352 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 391 | } |
| 392 | return m_archive_sp.get() != NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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++) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 407 | s->Indent(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 432 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 433 | } |
| 434 | return ObjectFileSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 437 | //------------------------------------------------------------------ |
| 438 | // PluginInterface protocol |
| 439 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() { |
| 441 | return GetPluginNameStatic(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 445 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) { |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 450 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 458 | llvm::sys::TimePoint<> file_mod_time = |
| 459 | FileSystem::GetModificationTime(file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 470 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 486 | llvm::sys::TimePoint<> object_mod_time( |
| 487 | std::chrono::seconds(object->ar_date)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 515 | } |