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