Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ObjectFile.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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10 | #include "lldb/Symbol/ObjectFile.h" |
| 11 | #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h" |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 12 | #include "lldb/Core/DataBuffer.h" |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 13 | #include "lldb/Core/DataBufferHeap.h" |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Log.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/PluginManager.h" |
| 18 | #include "lldb/Core/RegularExpression.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Section.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Timer.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/ObjectContainer.h" |
| 22 | #include "lldb/Symbol/SymbolFile.h" |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | #include "lldb/lldb-private.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 29 | ObjectFileSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file, |
| 31 | lldb::offset_t file_offset, lldb::offset_t file_size, |
| 32 | DataBufferSP &data_sp, lldb::offset_t &data_offset) { |
| 33 | ObjectFileSP object_file_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | if (module_sp) { |
| 36 | Timer scoped_timer( |
| 37 | LLVM_PRETTY_FUNCTION, |
| 38 | "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = " |
| 39 | "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")", |
| 40 | module_sp->GetFileSpec().GetPath().c_str(), |
| 41 | static_cast<const void *>(file), static_cast<uint64_t>(file_offset), |
| 42 | static_cast<uint64_t>(file_size)); |
| 43 | if (file) { |
| 44 | FileSpec archive_file; |
| 45 | ObjectContainerCreateInstance create_object_container_callback; |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | const bool file_exists = file->Exists(); |
| 48 | if (!data_sp) { |
| 49 | // We have an object name which most likely means we have |
| 50 | // a .o file in a static archive (.a file). Try and see if |
| 51 | // we have a cached archive first without reading any data |
| 52 | // first |
| 53 | if (file_exists && module_sp->GetObjectName()) { |
| 54 | for (uint32_t idx = 0; |
| 55 | (create_object_container_callback = |
| 56 | PluginManager::GetObjectContainerCreateCallbackAtIndex( |
| 57 | idx)) != nullptr; |
| 58 | ++idx) { |
| 59 | std::unique_ptr<ObjectContainer> object_container_ap( |
| 60 | create_object_container_callback(module_sp, data_sp, |
| 61 | data_offset, file, file_offset, |
| 62 | file_size)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | if (object_container_ap.get()) |
| 65 | object_file_sp = object_container_ap->GetObjectFile(file); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | if (object_file_sp.get()) |
| 68 | return object_file_sp; |
| 69 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | // Ok, we didn't find any containers that have a named object, now |
| 72 | // lets read the first 512 bytes from the file so the object file |
| 73 | // and object container plug-ins can use these bytes to see if they |
| 74 | // can parse this file. |
| 75 | if (file_size > 0) { |
| 76 | data_sp = file->ReadFileContents(file_offset, |
| 77 | std::min<size_t>(512, file_size)); |
| 78 | data_offset = 0; |
| 79 | } |
| 80 | } |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | if (!data_sp || data_sp->GetByteSize() == 0) { |
| 83 | // Check for archive file with format "/path/to/archive.a(object.o)" |
| 84 | char path_with_object[PATH_MAX * 2]; |
| 85 | module_sp->GetFileSpec().GetPath(path_with_object, |
| 86 | sizeof(path_with_object)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | ConstString archive_object; |
| 89 | const bool must_exist = true; |
| 90 | if (ObjectFile::SplitArchivePathWithObject( |
| 91 | path_with_object, archive_file, archive_object, must_exist)) { |
| 92 | file_size = archive_file.GetByteSize(); |
| 93 | if (file_size > 0) { |
| 94 | file = &archive_file; |
| 95 | module_sp->SetFileSpecAndObjectName(archive_file, archive_object); |
| 96 | // Check if this is a object container by iterating through all |
| 97 | // object |
| 98 | // container plugin instances and then trying to get an object file |
| 99 | // from the container plugins since we had a name. Also, don't read |
| 100 | // ANY data in case there is data cached in the container plug-ins |
| 101 | // (like BSD archives caching the contained objects within an file). |
| 102 | for (uint32_t idx = 0; |
| 103 | (create_object_container_callback = |
| 104 | PluginManager::GetObjectContainerCreateCallbackAtIndex( |
| 105 | idx)) != nullptr; |
| 106 | ++idx) { |
| 107 | std::unique_ptr<ObjectContainer> object_container_ap( |
| 108 | create_object_container_callback(module_sp, data_sp, |
| 109 | data_offset, file, |
| 110 | file_offset, file_size)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | if (object_container_ap.get()) |
| 113 | object_file_sp = object_container_ap->GetObjectFile(file); |
| 114 | |
| 115 | if (object_file_sp.get()) |
| 116 | return object_file_sp; |
| 117 | } |
| 118 | // We failed to find any cached object files in the container |
| 119 | // plug-ins, so lets read the first 512 bytes and try again below... |
| 120 | data_sp = archive_file.ReadFileContents(file_offset, 512); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (data_sp && data_sp->GetByteSize() > 0) { |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 126 | // Check if this is a normal object file by iterating through |
| 127 | // all object file plugin instances. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | ObjectFileCreateInstance create_object_file_callback; |
| 129 | for (uint32_t idx = 0; |
| 130 | (create_object_file_callback = |
| 131 | PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != |
| 132 | nullptr; |
| 133 | ++idx) { |
| 134 | object_file_sp.reset(create_object_file_callback( |
| 135 | module_sp, data_sp, data_offset, file, file_offset, file_size)); |
| 136 | if (object_file_sp.get()) |
| 137 | return object_file_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 138 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | // Check if this is a object container by iterating through |
| 141 | // all object container plugin instances and then trying to get |
| 142 | // an object file from the container. |
| 143 | for (uint32_t idx = 0; |
| 144 | (create_object_container_callback = |
| 145 | PluginManager::GetObjectContainerCreateCallbackAtIndex( |
| 146 | idx)) != nullptr; |
| 147 | ++idx) { |
| 148 | std::unique_ptr<ObjectContainer> object_container_ap( |
| 149 | create_object_container_callback(module_sp, data_sp, data_offset, |
| 150 | file, file_offset, file_size)); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | if (object_container_ap.get()) |
| 153 | object_file_sp = object_container_ap->GetObjectFile(file); |
| 154 | |
| 155 | if (object_file_sp.get()) |
| 156 | return object_file_sp; |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 157 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | } |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 159 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | } |
| 161 | // We didn't find it, so clear our shared pointer in case it |
| 162 | // contains anything and return an empty shared pointer |
| 163 | object_file_sp.reset(); |
| 164 | return object_file_sp; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, |
| 168 | const ProcessSP &process_sp, |
| 169 | lldb::addr_t header_addr, |
| 170 | DataBufferSP &data_sp) { |
| 171 | ObjectFileSP object_file_sp; |
| 172 | |
| 173 | if (module_sp) { |
| 174 | Timer scoped_timer(LLVM_PRETTY_FUNCTION, "ObjectFile::FindPlugin (module = " |
| 175 | "%s, process = %p, header_addr = " |
| 176 | "0x%" PRIx64 ")", |
| 177 | module_sp->GetFileSpec().GetPath().c_str(), |
| 178 | static_cast<void *>(process_sp.get()), header_addr); |
| 179 | uint32_t idx; |
| 180 | |
| 181 | // Check if this is a normal object file by iterating through |
| 182 | // all object file plugin instances. |
| 183 | ObjectFileCreateMemoryInstance create_callback; |
| 184 | for (idx = 0; |
| 185 | (create_callback = |
| 186 | PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != |
| 187 | nullptr; |
| 188 | ++idx) { |
| 189 | object_file_sp.reset( |
| 190 | create_callback(module_sp, data_sp, process_sp, header_addr)); |
| 191 | if (object_file_sp.get()) |
| 192 | return object_file_sp; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 193 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | // We didn't find it, so clear our shared pointer in case it |
| 197 | // contains anything and return an empty shared pointer |
| 198 | object_file_sp.reset(); |
| 199 | return object_file_sp; |
| 200 | } |
| 201 | |
| 202 | size_t ObjectFile::GetModuleSpecifications(const FileSpec &file, |
| 203 | lldb::offset_t file_offset, |
| 204 | lldb::offset_t file_size, |
| 205 | ModuleSpecList &specs) { |
| 206 | DataBufferSP data_sp(file.ReadFileContents(file_offset, 512)); |
| 207 | if (data_sp) { |
| 208 | if (file_size == 0) { |
| 209 | const lldb::offset_t actual_file_size = file.GetByteSize(); |
| 210 | if (actual_file_size > file_offset) |
| 211 | file_size = actual_file_size - file_offset; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 212 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | return ObjectFile::GetModuleSpecifications(file, // file spec |
| 214 | data_sp, // data bytes |
| 215 | 0, // data offset |
| 216 | file_offset, // file offset |
| 217 | file_size, // file length |
| 218 | specs); |
| 219 | } |
| 220 | return 0; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | size_t ObjectFile::GetModuleSpecifications( |
| 224 | const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, |
| 225 | lldb::offset_t data_offset, lldb::offset_t file_offset, |
| 226 | lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { |
| 227 | const size_t initial_count = specs.GetSize(); |
| 228 | ObjectFileGetModuleSpecifications callback; |
| 229 | uint32_t i; |
| 230 | // Try the ObjectFile plug-ins |
| 231 | for (i = 0; |
| 232 | (callback = |
| 233 | PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex( |
| 234 | i)) != nullptr; |
| 235 | ++i) { |
| 236 | if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) |
| 237 | return specs.GetSize() - initial_count; |
| 238 | } |
| 239 | |
| 240 | // Try the ObjectContainer plug-ins |
| 241 | for (i = 0; |
| 242 | (callback = PluginManager:: |
| 243 | GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != |
| 244 | nullptr; |
| 245 | ++i) { |
| 246 | if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) |
| 247 | return specs.GetSize() - initial_count; |
| 248 | } |
| 249 | return 0; |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, |
| 253 | const FileSpec *file_spec_ptr, |
| 254 | lldb::offset_t file_offset, lldb::offset_t length, |
| 255 | const lldb::DataBufferSP &data_sp, |
| 256 | lldb::offset_t data_offset) |
| 257 | : ModuleChild(module_sp), |
| 258 | m_file(), // This file could be different from the original module's file |
| 259 | m_type(eTypeInvalid), m_strata(eStrataInvalid), |
| 260 | m_file_offset(file_offset), m_length(length), m_data(), |
| 261 | m_unwind_table(*this), m_process_wp(), |
| 262 | m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_ap(), m_symtab_ap(), |
| 263 | m_synthetic_symbol_idx(0) { |
| 264 | if (file_spec_ptr) |
| 265 | m_file = *file_spec_ptr; |
| 266 | if (data_sp) |
| 267 | m_data.SetData(data_sp, data_offset, length); |
| 268 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
| 269 | if (log) |
| 270 | log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, " |
| 271 | "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, |
| 272 | static_cast<void *>(this), static_cast<void *>(module_sp.get()), |
| 273 | module_sp->GetSpecificationDescription().c_str(), |
| 274 | m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset, |
| 275 | m_length); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, |
| 279 | const ProcessSP &process_sp, lldb::addr_t header_addr, |
| 280 | DataBufferSP &header_data_sp) |
| 281 | : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid), |
| 282 | m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(), |
| 283 | m_unwind_table(*this), m_process_wp(process_sp), |
| 284 | m_memory_addr(header_addr), m_sections_ap(), m_symtab_ap(), |
| 285 | m_synthetic_symbol_idx(0) { |
| 286 | if (header_data_sp) |
| 287 | m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize()); |
| 288 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
| 289 | if (log) |
| 290 | log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, " |
| 291 | "header_addr = 0x%" PRIx64, |
| 292 | static_cast<void *>(this), static_cast<void *>(module_sp.get()), |
| 293 | module_sp->GetSpecificationDescription().c_str(), |
| 294 | static_cast<void *>(process_sp.get()), m_memory_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 297 | ObjectFile::~ObjectFile() { |
| 298 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
| 299 | if (log) |
| 300 | log->Printf("%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this)); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) { |
| 304 | ModuleSP module_sp(GetModule()); |
| 305 | if (module_sp) |
| 306 | return module_sp->SetArchitecture(new_arch); |
| 307 | return false; |
| 308 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 309 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | AddressClass ObjectFile::GetAddressClass(addr_t file_addr) { |
| 311 | Symtab *symtab = GetSymtab(); |
| 312 | if (symtab) { |
| 313 | Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); |
| 314 | if (symbol) { |
| 315 | if (symbol->ValueIsAddress()) { |
| 316 | const SectionSP section_sp(symbol->GetAddressRef().GetSection()); |
| 317 | if (section_sp) { |
| 318 | const SectionType section_type = section_sp->GetType(); |
| 319 | switch (section_type) { |
| 320 | case eSectionTypeInvalid: |
| 321 | return eAddressClassUnknown; |
| 322 | case eSectionTypeCode: |
| 323 | return eAddressClassCode; |
| 324 | case eSectionTypeContainer: |
| 325 | return eAddressClassUnknown; |
| 326 | case eSectionTypeData: |
| 327 | case eSectionTypeDataCString: |
| 328 | case eSectionTypeDataCStringPointers: |
| 329 | case eSectionTypeDataSymbolAddress: |
| 330 | case eSectionTypeData4: |
| 331 | case eSectionTypeData8: |
| 332 | case eSectionTypeData16: |
| 333 | case eSectionTypeDataPointers: |
| 334 | case eSectionTypeZeroFill: |
| 335 | case eSectionTypeDataObjCMessageRefs: |
| 336 | case eSectionTypeDataObjCCFStrings: |
| 337 | case eSectionTypeGoSymtab: |
| 338 | return eAddressClassData; |
| 339 | case eSectionTypeDebug: |
| 340 | case eSectionTypeDWARFDebugAbbrev: |
| 341 | case eSectionTypeDWARFDebugAddr: |
| 342 | case eSectionTypeDWARFDebugAranges: |
| 343 | case eSectionTypeDWARFDebugFrame: |
| 344 | case eSectionTypeDWARFDebugInfo: |
| 345 | case eSectionTypeDWARFDebugLine: |
| 346 | case eSectionTypeDWARFDebugLoc: |
| 347 | case eSectionTypeDWARFDebugMacInfo: |
| 348 | case eSectionTypeDWARFDebugMacro: |
| 349 | case eSectionTypeDWARFDebugPubNames: |
| 350 | case eSectionTypeDWARFDebugPubTypes: |
| 351 | case eSectionTypeDWARFDebugRanges: |
| 352 | case eSectionTypeDWARFDebugStr: |
| 353 | case eSectionTypeDWARFDebugStrOffsets: |
| 354 | case eSectionTypeDWARFAppleNames: |
| 355 | case eSectionTypeDWARFAppleTypes: |
| 356 | case eSectionTypeDWARFAppleNamespaces: |
| 357 | case eSectionTypeDWARFAppleObjC: |
| 358 | return eAddressClassDebug; |
| 359 | case eSectionTypeEHFrame: |
| 360 | case eSectionTypeARMexidx: |
| 361 | case eSectionTypeARMextab: |
| 362 | case eSectionTypeCompactUnwind: |
| 363 | return eAddressClassRuntime; |
| 364 | case eSectionTypeELFSymbolTable: |
| 365 | case eSectionTypeELFDynamicSymbols: |
| 366 | case eSectionTypeELFRelocationEntries: |
| 367 | case eSectionTypeELFDynamicLinkInfo: |
| 368 | case eSectionTypeOther: |
| 369 | return eAddressClassUnknown; |
| 370 | case eSectionTypeAbsoluteAddress: |
| 371 | // In case of absolute sections decide the address class based on |
| 372 | // the symbol |
| 373 | // type because the section type isn't specify if it is a code or a |
| 374 | // data |
| 375 | // section. |
| 376 | break; |
| 377 | } |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 378 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | const SymbolType symbol_type = symbol->GetType(); |
| 382 | switch (symbol_type) { |
| 383 | case eSymbolTypeAny: |
| 384 | return eAddressClassUnknown; |
| 385 | case eSymbolTypeAbsolute: |
| 386 | return eAddressClassUnknown; |
| 387 | case eSymbolTypeCode: |
| 388 | return eAddressClassCode; |
| 389 | case eSymbolTypeTrampoline: |
| 390 | return eAddressClassCode; |
| 391 | case eSymbolTypeResolver: |
| 392 | return eAddressClassCode; |
| 393 | case eSymbolTypeData: |
| 394 | return eAddressClassData; |
| 395 | case eSymbolTypeRuntime: |
| 396 | return eAddressClassRuntime; |
| 397 | case eSymbolTypeException: |
| 398 | return eAddressClassRuntime; |
| 399 | case eSymbolTypeSourceFile: |
| 400 | return eAddressClassDebug; |
| 401 | case eSymbolTypeHeaderFile: |
| 402 | return eAddressClassDebug; |
| 403 | case eSymbolTypeObjectFile: |
| 404 | return eAddressClassDebug; |
| 405 | case eSymbolTypeCommonBlock: |
| 406 | return eAddressClassDebug; |
| 407 | case eSymbolTypeBlock: |
| 408 | return eAddressClassDebug; |
| 409 | case eSymbolTypeLocal: |
| 410 | return eAddressClassData; |
| 411 | case eSymbolTypeParam: |
| 412 | return eAddressClassData; |
| 413 | case eSymbolTypeVariable: |
| 414 | return eAddressClassData; |
| 415 | case eSymbolTypeVariableType: |
| 416 | return eAddressClassDebug; |
| 417 | case eSymbolTypeLineEntry: |
| 418 | return eAddressClassDebug; |
| 419 | case eSymbolTypeLineHeader: |
| 420 | return eAddressClassDebug; |
| 421 | case eSymbolTypeScopeBegin: |
| 422 | return eAddressClassDebug; |
| 423 | case eSymbolTypeScopeEnd: |
| 424 | return eAddressClassDebug; |
| 425 | case eSymbolTypeAdditional: |
| 426 | return eAddressClassUnknown; |
| 427 | case eSymbolTypeCompiler: |
| 428 | return eAddressClassDebug; |
| 429 | case eSymbolTypeInstrumentation: |
| 430 | return eAddressClassDebug; |
| 431 | case eSymbolTypeUndefined: |
| 432 | return eAddressClassUnknown; |
| 433 | case eSymbolTypeObjCClass: |
| 434 | return eAddressClassRuntime; |
| 435 | case eSymbolTypeObjCMetaClass: |
| 436 | return eAddressClassRuntime; |
| 437 | case eSymbolTypeObjCIVar: |
| 438 | return eAddressClassRuntime; |
| 439 | case eSymbolTypeReExported: |
| 440 | return eAddressClassRuntime; |
| 441 | } |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 442 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | } |
| 444 | return eAddressClassUnknown; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 447 | DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp, |
| 448 | lldb::addr_t addr, size_t byte_size) { |
| 449 | DataBufferSP data_sp; |
| 450 | if (process_sp) { |
| 451 | std::unique_ptr<DataBufferHeap> data_ap(new DataBufferHeap(byte_size, 0)); |
| 452 | Error error; |
| 453 | const size_t bytes_read = process_sp->ReadMemory( |
| 454 | addr, data_ap->GetBytes(), data_ap->GetByteSize(), error); |
| 455 | if (bytes_read == byte_size) |
| 456 | data_sp.reset(data_ap.release()); |
| 457 | } |
| 458 | return data_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | size_t ObjectFile::GetData(lldb::offset_t offset, size_t length, |
| 462 | DataExtractor &data) const { |
| 463 | // The entire file has already been mmap'ed into m_data, so just copy from |
| 464 | // there |
| 465 | // as the back mmap buffer will be shared with shared pointers. |
| 466 | return data.SetData(m_data, offset, length); |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length, |
| 470 | void *dst) const { |
| 471 | // The entire file has already been mmap'ed into m_data, so just copy from |
| 472 | // there |
| 473 | // Note that the data remains in target byte order. |
| 474 | return m_data.CopyData(offset, length, dst); |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 475 | } |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 477 | size_t ObjectFile::ReadSectionData(const Section *section, |
| 478 | lldb::offset_t section_offset, void *dst, |
| 479 | size_t dst_len) const { |
| 480 | assert(section); |
| 481 | section_offset *= section->GetTargetByteSize(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 482 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 483 | // If some other objectfile owns this data, pass this to them. |
| 484 | if (section->GetObjectFile() != this) |
| 485 | return section->GetObjectFile()->ReadSectionData(section, section_offset, |
| 486 | dst, dst_len); |
Matthew Gardiner | f03e6d84 | 2014-09-29 08:02:24 +0000 | [diff] [blame] | 487 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 488 | if (IsInMemory()) { |
| 489 | ProcessSP process_sp(m_process_wp.lock()); |
| 490 | if (process_sp) { |
| 491 | Error error; |
| 492 | const addr_t base_load_addr = |
| 493 | section->GetLoadBaseAddress(&process_sp->GetTarget()); |
| 494 | if (base_load_addr != LLDB_INVALID_ADDRESS) |
| 495 | return process_sp->ReadMemory(base_load_addr + section_offset, dst, |
| 496 | dst_len, error); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 497 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 498 | } else { |
| 499 | const lldb::offset_t section_file_size = section->GetFileSize(); |
| 500 | if (section_offset < section_file_size) { |
| 501 | const size_t section_bytes_left = section_file_size - section_offset; |
| 502 | size_t section_dst_len = dst_len; |
| 503 | if (section_dst_len > section_bytes_left) |
| 504 | section_dst_len = section_bytes_left; |
| 505 | return CopyData(section->GetFileOffset() + section_offset, |
| 506 | section_dst_len, dst); |
| 507 | } else { |
| 508 | if (section->GetType() == eSectionTypeZeroFill) { |
| 509 | const uint64_t section_size = section->GetByteSize(); |
| 510 | const uint64_t section_bytes_left = section_size - section_offset; |
| 511 | uint64_t section_dst_len = dst_len; |
| 512 | if (section_dst_len > section_bytes_left) |
| 513 | section_dst_len = section_bytes_left; |
| 514 | memset(dst, 0, section_dst_len); |
| 515 | return section_dst_len; |
| 516 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 517 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 518 | } |
| 519 | return 0; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | //---------------------------------------------------------------------- |
| 523 | // Get the section data the file on disk |
| 524 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 525 | size_t ObjectFile::ReadSectionData(const Section *section, |
| 526 | DataExtractor §ion_data) const { |
| 527 | // If some other objectfile owns this data, pass this to them. |
| 528 | if (section->GetObjectFile() != this) |
| 529 | return section->GetObjectFile()->ReadSectionData(section, section_data); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 530 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 531 | if (IsInMemory()) { |
| 532 | ProcessSP process_sp(m_process_wp.lock()); |
| 533 | if (process_sp) { |
| 534 | const addr_t base_load_addr = |
| 535 | section->GetLoadBaseAddress(&process_sp->GetTarget()); |
| 536 | if (base_load_addr != LLDB_INVALID_ADDRESS) { |
| 537 | DataBufferSP data_sp( |
| 538 | ReadMemory(process_sp, base_load_addr, section->GetByteSize())); |
| 539 | if (data_sp) { |
| 540 | section_data.SetData(data_sp, 0, data_sp->GetByteSize()); |
| 541 | section_data.SetByteOrder(process_sp->GetByteOrder()); |
| 542 | section_data.SetAddressByteSize(process_sp->GetAddressByteSize()); |
| 543 | return section_data.GetByteSize(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 544 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 546 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 547 | return GetData(section->GetFileOffset(), section->GetFileSize(), |
| 548 | section_data); |
| 549 | } else { |
| 550 | // The object file now contains a full mmap'ed copy of the object file data, |
| 551 | // so just use this |
| 552 | return MemoryMapSectionData(section, section_data); |
| 553 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | size_t ObjectFile::MemoryMapSectionData(const Section *section, |
| 557 | DataExtractor §ion_data) const { |
| 558 | // If some other objectfile owns this data, pass this to them. |
| 559 | if (section->GetObjectFile() != this) |
| 560 | return section->GetObjectFile()->MemoryMapSectionData(section, |
| 561 | section_data); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 562 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 563 | if (IsInMemory()) { |
| 564 | return ReadSectionData(section, section_data); |
| 565 | } else { |
| 566 | // The object file now contains a full mmap'ed copy of the object file data, |
| 567 | // so just use this |
| 568 | return GetData(section->GetFileOffset(), section->GetFileSize(), |
| 569 | section_data); |
| 570 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 573 | bool ObjectFile::SplitArchivePathWithObject(const char *path_with_object, |
| 574 | FileSpec &archive_file, |
| 575 | ConstString &archive_object, |
| 576 | bool must_exist) { |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 577 | RegularExpression g_object_regex(llvm::StringRef("(.*)\\(([^\\)]+)\\)$")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 578 | RegularExpression::Match regex_match(2); |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 579 | if (g_object_regex.Execute(llvm::StringRef::withNullAsEmpty(path_with_object), |
| 580 | ®ex_match)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 581 | std::string path; |
| 582 | std::string obj; |
| 583 | if (regex_match.GetMatchAtIndex(path_with_object, 1, path) && |
| 584 | regex_match.GetMatchAtIndex(path_with_object, 2, obj)) { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 585 | archive_file.SetFile(path, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | archive_object.SetCString(obj.c_str()); |
| 587 | if (must_exist && !archive_file.Exists()) |
| 588 | return false; |
| 589 | return true; |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 590 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 591 | } |
| 592 | return false; |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 595 | void ObjectFile::ClearSymtab() { |
| 596 | ModuleSP module_sp(GetModule()); |
| 597 | if (module_sp) { |
| 598 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 599 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
| 600 | if (log) |
| 601 | log->Printf("%p ObjectFile::ClearSymtab () symtab = %p", |
| 602 | static_cast<void *>(this), |
| 603 | static_cast<void *>(m_symtab_ap.get())); |
| 604 | m_symtab_ap.reset(); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | SectionList *ObjectFile::GetSectionList(bool update_module_section_list) { |
| 609 | if (m_sections_ap.get() == nullptr) { |
| 610 | if (update_module_section_list) { |
| 611 | ModuleSP module_sp(GetModule()); |
| 612 | if (module_sp) { |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 613 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 614 | CreateSections(*module_sp->GetUnifiedSectionList()); |
| 615 | } |
| 616 | } else { |
| 617 | SectionList unified_section_list; |
| 618 | CreateSections(unified_section_list); |
Greg Clayton | 9422dd6 | 2013-03-04 21:46:16 +0000 | [diff] [blame] | 619 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 620 | } |
| 621 | return m_sections_ap.get(); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 622 | } |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 623 | |
| 624 | lldb::SymbolType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 625 | ObjectFile::GetSymbolTypeFromName(llvm::StringRef name, |
| 626 | lldb::SymbolType symbol_type_hint) { |
| 627 | if (!name.empty()) { |
| 628 | if (name.startswith("_OBJC_")) { |
| 629 | // ObjC |
| 630 | if (name.startswith("_OBJC_CLASS_$_")) |
| 631 | return lldb::eSymbolTypeObjCClass; |
| 632 | if (name.startswith("_OBJC_METACLASS_$_")) |
| 633 | return lldb::eSymbolTypeObjCMetaClass; |
| 634 | if (name.startswith("_OBJC_IVAR_$_")) |
| 635 | return lldb::eSymbolTypeObjCIVar; |
| 636 | } else if (name.startswith(".objc_class_name_")) { |
| 637 | // ObjC v1 |
| 638 | return lldb::eSymbolTypeObjCClass; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 639 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 640 | } |
| 641 | return symbol_type_hint; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 642 | } |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 643 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 644 | ConstString ObjectFile::GetNextSyntheticSymbolName() { |
| 645 | StreamString ss; |
| 646 | ConstString file_name = GetModule()->GetFileSpec().GetFilename(); |
| 647 | ss.Printf("___lldb_unnamed_symbol%u$$%s", ++m_synthetic_symbol_idx, |
| 648 | file_name.GetCString()); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame^] | 649 | return ConstString(ss.GetString()); |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 650 | } |