Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ObjectContainerBSDArchive.cpp ---------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ObjectContainerBSDArchive.h" |
| 10 | |
Pavel Labath | e705c8b | 2016-12-02 11:15:15 +0000 | [diff] [blame] | 11 | #if defined(_WIN32) || defined(__ANDROID__) |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 12 | // Defines from ar, missing on Windows |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #define ARMAG "!<arch>\n" |
| 14 | #define SARMAG 8 |
| 15 | #define ARFMAG "`\n" |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 16 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | typedef struct ar_hdr { |
| 18 | char ar_name[16]; |
| 19 | char ar_date[12]; |
| 20 | char ar_uid[6], ar_gid[6]; |
| 21 | char ar_mode[8]; |
| 22 | char ar_size[10]; |
| 23 | char ar_fmag[2]; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 24 | } ar_hdr; |
| 25 | #else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include <ar.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 27 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Core/Module.h" |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 30 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Core/PluginManager.h" |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 32 | #include "lldb/Host/FileSystem.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Symbol/ObjectFile.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 34 | #include "lldb/Utility/ArchSpec.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 35 | #include "lldb/Utility/Stream.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 36 | #include "lldb/Utility/Timer.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" |
| 39 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 90 | // If the name is longer than 16 bytes, or contains an embedded space then |
| 91 | // it will use this format where the length of the name is here and the |
| 92 | // name characters are after this header. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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... |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 159 | m_object_name_to_index_map.Append(obj.ar_name, 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 = |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 175 | m_object_name_to_index_map.FindFirstValueForName(object_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | if (match) { |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 177 | if (object_mod_time != llvm::sys::TimePoint<>()) { |
| 178 | const uint64_t object_date = llvm::sys::toTimeT(object_mod_time); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | if (m_objects[match->value].ar_date == object_date) |
| 180 | return &m_objects[match->value]; |
| 181 | const ObjectNameToIndexMap::Entry *next_match = |
| 182 | m_object_name_to_index_map.FindNextValueForName(match); |
| 183 | while (next_match) { |
| 184 | if (m_objects[next_match->value].ar_date == object_date) |
| 185 | return &m_objects[next_match->value]; |
| 186 | next_match = |
| 187 | m_object_name_to_index_map.FindNextValueForName(next_match); |
| 188 | } |
| 189 | } else { |
| 190 | return &m_objects[match->value]; |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 191 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 192 | } |
| 193 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | ObjectContainerBSDArchive::Archive::shared_ptr |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | ObjectContainerBSDArchive::Archive::FindCachedArchive( |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 198 | const FileSpec &file, const ArchSpec &arch, |
| 199 | const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex()); |
| 201 | shared_ptr archive_sp; |
| 202 | Archive::Map &archive_map = Archive::GetArchiveCache(); |
| 203 | Archive::Map::iterator pos = archive_map.find(file); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 204 | // Don't cache a value for "archive_map.end()" below since we might delete an |
| 205 | // archive entry... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | while (pos != archive_map.end() && pos->first == file) { |
| 207 | bool match = true; |
| 208 | if (arch.IsValid() && |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 209 | !pos->second->GetArchitecture().IsCompatibleMatch(arch)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 210 | match = false; |
| 211 | else if (file_offset != LLDB_INVALID_OFFSET && |
| 212 | pos->second->GetFileOffset() != file_offset) |
| 213 | match = false; |
| 214 | if (match) { |
| 215 | if (pos->second->GetModificationTime() == time) { |
| 216 | return pos->second; |
| 217 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 218 | // We have a file at the same path with the same architecture whose |
| 219 | // modification time doesn't match. It doesn't make sense for us to |
| 220 | // continue to use this BSD archive since we cache only the object info |
| 221 | // which consists of file time info and also the file offset and file |
| 222 | // size of any contained objects. Since this information is now out of |
| 223 | // date, we won't get the correct information if we go and extract the |
| 224 | // file data, so we should remove the old and outdated entry. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | archive_map.erase(pos); |
| 226 | pos = archive_map.find(file); |
| 227 | continue; // Continue to next iteration so we don't increment pos |
| 228 | // below... |
| 229 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | ++pos; |
| 232 | } |
| 233 | return archive_sp; |
| 234 | } |
| 235 | |
| 236 | ObjectContainerBSDArchive::Archive::shared_ptr |
| 237 | ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile( |
Pavel Labath | 5cc7967 | 2016-11-09 15:05:45 +0000 | [diff] [blame] | 238 | const FileSpec &file, const ArchSpec &arch, |
| 239 | const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset, |
| 240 | DataExtractor &data) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | shared_ptr archive_sp(new Archive(arch, time, file_offset, data)); |
| 242 | if (archive_sp) { |
| 243 | const size_t num_objects = archive_sp->ParseObjects(); |
| 244 | if (num_objects > 0) { |
| 245 | std::lock_guard<std::recursive_mutex> guard( |
| 246 | Archive::GetArchiveCacheMutex()); |
| 247 | Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp)); |
| 248 | } else { |
| 249 | archive_sp.reset(); |
| 250 | } |
| 251 | } |
| 252 | return archive_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | ObjectContainerBSDArchive::Archive::Map & |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | ObjectContainerBSDArchive::Archive::GetArchiveCache() { |
| 257 | static Archive::Map g_archive_map; |
| 258 | return g_archive_map; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 261 | std::recursive_mutex & |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() { |
| 263 | static std::recursive_mutex g_archive_map_mutex; |
| 264 | return g_archive_map_mutex; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | void ObjectContainerBSDArchive::Initialize() { |
| 268 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 269 | GetPluginDescriptionStatic(), CreateInstance, |
| 270 | GetModuleSpecifications); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | void ObjectContainerBSDArchive::Terminate() { |
| 274 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | lldb_private::ConstString ObjectContainerBSDArchive::GetPluginNameStatic() { |
| 278 | static ConstString g_name("bsd-archive"); |
| 279 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | const char *ObjectContainerBSDArchive::GetPluginDescriptionStatic() { |
| 283 | return "BSD Archive object container reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 286 | ObjectContainer *ObjectContainerBSDArchive::CreateInstance( |
| 287 | const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, |
| 288 | lldb::offset_t data_offset, const FileSpec *file, |
| 289 | lldb::offset_t file_offset, lldb::offset_t length) { |
| 290 | ConstString object_name(module_sp->GetObjectName()); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 291 | if (!object_name) |
| 292 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 294 | if (data_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 295 | // We have data, which means this is the first 512 bytes of the file Check |
| 296 | // to see if the magic bytes match and if they do, read the entire table of |
| 297 | // contents for the archive and cache it |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 298 | DataExtractor data; |
| 299 | data.SetData(data_sp, data_offset, length); |
| 300 | if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 301 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 302 | Timer scoped_timer( |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 303 | func_cat, |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 304 | "ObjectContainerBSDArchive::CreateInstance (module = %s, file = " |
| 305 | "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")", |
| 306 | module_sp->GetFileSpec().GetPath().c_str(), |
| 307 | static_cast<const void *>(file), static_cast<uint64_t>(file_offset), |
| 308 | static_cast<uint64_t>(length)); |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 309 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 310 | // Map the entire .a file to be sure that we don't lose any data if the |
| 311 | // file gets updated by a new build while this .a file is being used for |
| 312 | // debugging |
| 313 | DataBufferSP archive_data_sp = |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 314 | FileSystem::Instance().CreateDataBuffer(*file, length, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 315 | if (!archive_data_sp) |
| 316 | return nullptr; |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 317 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 318 | lldb::offset_t archive_data_offset = 0; |
| 319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | Archive::shared_ptr archive_sp(Archive::FindCachedArchive( |
| 321 | *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(), |
| 322 | file_offset)); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 323 | std::unique_ptr<ObjectContainerBSDArchive> container_up( |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 324 | new ObjectContainerBSDArchive(module_sp, archive_data_sp, |
| 325 | archive_data_offset, file, file_offset, |
| 326 | length)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 327 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 328 | if (container_up) { |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 329 | if (archive_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | // We already have this archive in our cache, use it |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 331 | container_up->SetArchive(archive_sp); |
| 332 | return container_up.release(); |
| 333 | } else if (container_up->ParseHeader()) |
| 334 | return container_up.release(); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | } else { |
| 338 | // No data, just check for a cached archive |
| 339 | Archive::shared_ptr archive_sp(Archive::FindCachedArchive( |
| 340 | *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(), |
| 341 | file_offset)); |
| 342 | if (archive_sp) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 343 | std::unique_ptr<ObjectContainerBSDArchive> container_up( |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 344 | new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file, |
| 345 | file_offset, length)); |
| 346 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 347 | if (container_up) { |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 348 | // We already have this archive in our cache, use it |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 349 | container_up->SetArchive(archive_sp); |
| 350 | return container_up.release(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 352 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | } |
| 354 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) { |
| 358 | uint32_t offset = 0; |
| 359 | const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr)); |
| 360 | if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) { |
| 361 | armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG; |
| 362 | if (strncmp(armag, ARFMAG, 2) == 0) |
| 363 | return true; |
| 364 | } |
| 365 | return false; |
| 366 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 367 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | ObjectContainerBSDArchive::ObjectContainerBSDArchive( |
| 369 | const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, |
| 370 | lldb::offset_t data_offset, const lldb_private::FileSpec *file, |
| 371 | lldb::offset_t file_offset, lldb::offset_t size) |
| 372 | : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset), |
| 373 | m_archive_sp() {} |
| 374 | void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) { |
| 375 | m_archive_sp = archive_sp; |
| 376 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 377 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 378 | ObjectContainerBSDArchive::~ObjectContainerBSDArchive() {} |
| 379 | |
| 380 | bool ObjectContainerBSDArchive::ParseHeader() { |
| 381 | if (m_archive_sp.get() == NULL) { |
| 382 | if (m_data.GetByteSize() > 0) { |
| 383 | ModuleSP module_sp(GetModule()); |
| 384 | if (module_sp) { |
| 385 | m_archive_sp = Archive::ParseAndCacheArchiveForFile( |
| 386 | m_file, module_sp->GetArchitecture(), |
| 387 | module_sp->GetModificationTime(), m_offset, m_data); |
| 388 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 389 | // Clear the m_data that contains the entire archive data and let our |
| 390 | // m_archive_sp hold onto the data. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 391 | m_data.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 392 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | } |
| 394 | return m_archive_sp.get() != NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | void ObjectContainerBSDArchive::Dump(Stream *s) const { |
| 398 | s->Printf("%p: ", static_cast<const void *>(this)); |
| 399 | s->Indent(); |
| 400 | const size_t num_archs = GetNumArchitectures(); |
| 401 | const size_t num_objects = GetNumObjects(); |
| 402 | s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64 |
| 403 | ", num_objects = %" PRIu64 "", |
| 404 | (uint64_t)num_archs, (uint64_t)num_objects); |
| 405 | uint32_t i; |
| 406 | ArchSpec arch; |
| 407 | s->IndentMore(); |
| 408 | for (i = 0; i < num_archs; i++) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | s->Indent(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | GetArchitectureAtIndex(i, arch); |
| 411 | s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName()); |
| 412 | } |
| 413 | for (i = 0; i < num_objects; i++) { |
| 414 | s->Indent(); |
| 415 | s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i)); |
| 416 | } |
| 417 | s->IndentLess(); |
| 418 | s->EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) { |
| 422 | ModuleSP module_sp(GetModule()); |
| 423 | if (module_sp) { |
| 424 | if (module_sp->GetObjectName() && m_archive_sp) { |
| 425 | Object *object = m_archive_sp->FindObject( |
| 426 | module_sp->GetObjectName(), module_sp->GetObjectModificationTime()); |
| 427 | if (object) { |
| 428 | lldb::offset_t data_offset = object->ar_file_offset; |
| 429 | return ObjectFile::FindPlugin( |
| 430 | module_sp, file, m_offset + object->ar_file_offset, |
| 431 | object->ar_file_size, m_archive_sp->GetData().GetSharedDataBuffer(), |
| 432 | data_offset); |
| 433 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 434 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 435 | } |
| 436 | return ObjectFileSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | //------------------------------------------------------------------ |
| 440 | // PluginInterface protocol |
| 441 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | lldb_private::ConstString ObjectContainerBSDArchive::GetPluginName() { |
| 443 | return GetPluginNameStatic(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | uint32_t ObjectContainerBSDArchive::GetPluginVersion() { return 1; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 448 | size_t ObjectContainerBSDArchive::GetModuleSpecifications( |
| 449 | const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, |
| 450 | lldb::offset_t data_offset, lldb::offset_t file_offset, |
| 451 | lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 452 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 453 | // We have data, which means this is the first 512 bytes of the file Check to |
| 454 | // see if the magic bytes match and if they do, read the entire table of |
| 455 | // contents for the archive and cache it |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 456 | DataExtractor data; |
| 457 | data.SetData(data_sp, data_offset, data_sp->GetByteSize()); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 458 | if (!file || !data_sp || !ObjectContainerBSDArchive::MagicBytesMatch(data)) |
| 459 | return 0; |
| 460 | |
| 461 | const size_t initial_count = specs.GetSize(); |
Jonas Devlieghere | 4637696 | 2018-10-31 21:49:27 +0000 | [diff] [blame] | 462 | llvm::sys::TimePoint<> file_mod_time = FileSystem::Instance().GetModificationTime(file); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 463 | Archive::shared_ptr archive_sp( |
| 464 | Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset)); |
| 465 | bool set_archive_arch = false; |
| 466 | if (!archive_sp) { |
| 467 | set_archive_arch = true; |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 468 | data_sp = |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 469 | FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 470 | if (data_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 471 | data.SetData(data_sp, 0, data_sp->GetByteSize()); |
| 472 | archive_sp = Archive::ParseAndCacheArchiveForFile( |
| 473 | file, ArchSpec(), file_mod_time, file_offset, data); |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 474 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 475 | } |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 476 | |
| 477 | if (archive_sp) { |
| 478 | const size_t num_objects = archive_sp->GetNumObjects(); |
| 479 | for (size_t idx = 0; idx < num_objects; ++idx) { |
| 480 | const Object *object = archive_sp->GetObjectAtIndex(idx); |
| 481 | if (object) { |
| 482 | const lldb::offset_t object_file_offset = |
| 483 | file_offset + object->ar_file_offset; |
| 484 | if (object->ar_file_offset < file_size && |
| 485 | file_size > object_file_offset) { |
| 486 | if (ObjectFile::GetModuleSpecifications( |
| 487 | file, object_file_offset, file_size - object_file_offset, |
| 488 | specs)) { |
| 489 | ModuleSpec &spec = |
| 490 | specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1); |
| 491 | llvm::sys::TimePoint<> object_mod_time( |
| 492 | std::chrono::seconds(object->ar_date)); |
| 493 | spec.GetObjectName() = object->ar_name; |
| 494 | spec.SetObjectOffset(object_file_offset); |
| 495 | spec.SetObjectSize(file_size - object_file_offset); |
| 496 | spec.GetObjectModificationTime() = object_mod_time; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | const size_t end_count = specs.GetSize(); |
| 503 | size_t num_specs_added = end_count - initial_count; |
| 504 | if (set_archive_arch && num_specs_added > 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 505 | // The archive was created but we didn't have an architecture so we need to |
| 506 | // set it |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 507 | for (size_t i = initial_count; i < end_count; ++i) { |
| 508 | ModuleSpec module_spec; |
| 509 | if (specs.GetModuleSpecAtIndex(i, module_spec)) { |
| 510 | if (module_spec.GetArchitecture().IsValid()) { |
| 511 | archive_sp->SetArchitecture(module_spec.GetArchitecture()); |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | return num_specs_added; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 518 | } |