Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ObjectFile.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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9 | #include "lldb/Symbol/ObjectFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 11 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Section.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/ObjectContainer.h" |
| 15 | #include "lldb/Symbol/SymbolFile.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Process.h" |
Hafiz Abid Qadeer | 4687db0 | 2017-01-19 17:32:50 +0000 | [diff] [blame] | 17 | #include "lldb/Target/SectionLoadList.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/DataBuffer.h" |
| 20 | #include "lldb/Utility/DataBufferHeap.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 21 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 22 | #include "lldb/Utility/RegularExpression.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 23 | #include "lldb/Utility/Timer.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) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 36 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | Timer scoped_timer( |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 38 | func_cat, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = " |
| 40 | "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")", |
| 41 | module_sp->GetFileSpec().GetPath().c_str(), |
| 42 | static_cast<const void *>(file), static_cast<uint64_t>(file_offset), |
| 43 | static_cast<uint64_t>(file_size)); |
| 44 | if (file) { |
| 45 | FileSpec archive_file; |
| 46 | ObjectContainerCreateInstance create_object_container_callback; |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 47 | |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 48 | const bool file_exists = FileSystem::Instance().Exists(*file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | if (!data_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 50 | // We have an object name which most likely means we have a .o file in |
| 51 | // a static archive (.a file). Try and see if we have a cached archive |
| 52 | // first without reading any data first |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 59 | std::unique_ptr<ObjectContainer> object_container_up( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 64 | if (object_container_up) |
| 65 | object_file_sp = object_container_up->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 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 71 | // Ok, we didn't find any containers that have a named object, now lets |
| 72 | // read the first 512 bytes from the file so the object file and object |
| 73 | // container plug-ins can use these bytes to see if they can parse this |
| 74 | // file. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | if (file_size > 0) { |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 76 | data_sp = FileSystem::Instance().CreateDataBuffer(file->GetPath(), |
| 77 | 512, file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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)) { |
Jonas Devlieghere | 59b78bc | 2018-11-01 04:45:28 +0000 | [diff] [blame] | 92 | file_size = FileSystem::Instance().GetByteSize(archive_file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 97 | // object container plugin instances and then trying to get an |
| 98 | // object file from the container plugins since we had a name. |
| 99 | // Also, don't read |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | // ANY data in case there is data cached in the container plug-ins |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 101 | // (like BSD archives caching the contained objects within an |
| 102 | // file). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | for (uint32_t idx = 0; |
| 104 | (create_object_container_callback = |
| 105 | PluginManager::GetObjectContainerCreateCallbackAtIndex( |
| 106 | idx)) != nullptr; |
| 107 | ++idx) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 108 | std::unique_ptr<ObjectContainer> object_container_up( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | create_object_container_callback(module_sp, data_sp, |
| 110 | data_offset, file, |
| 111 | file_offset, file_size)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 112 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 113 | if (object_container_up) |
| 114 | object_file_sp = object_container_up->GetObjectFile(file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | |
| 116 | if (object_file_sp.get()) |
| 117 | return object_file_sp; |
| 118 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 119 | // We failed to find any cached object files in the container plug- |
| 120 | // ins, so lets read the first 512 bytes and try again below... |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 121 | data_sp = FileSystem::Instance().CreateDataBuffer( |
| 122 | archive_file.GetPath(), 512, file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (data_sp && data_sp->GetByteSize() > 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 128 | // Check if this is a normal object file by iterating through all |
| 129 | // object file plugin instances. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | ObjectFileCreateInstance create_object_file_callback; |
| 131 | for (uint32_t idx = 0; |
| 132 | (create_object_file_callback = |
| 133 | PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != |
| 134 | nullptr; |
| 135 | ++idx) { |
| 136 | object_file_sp.reset(create_object_file_callback( |
| 137 | module_sp, data_sp, data_offset, file, file_offset, file_size)); |
| 138 | if (object_file_sp.get()) |
| 139 | return object_file_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 140 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 141 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 142 | // Check if this is a object container by iterating through all object |
| 143 | // container plugin instances and then trying to get an object file |
| 144 | // from the container. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | for (uint32_t idx = 0; |
| 146 | (create_object_container_callback = |
| 147 | PluginManager::GetObjectContainerCreateCallbackAtIndex( |
| 148 | idx)) != nullptr; |
| 149 | ++idx) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 150 | std::unique_ptr<ObjectContainer> object_container_up( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | create_object_container_callback(module_sp, data_sp, data_offset, |
| 152 | file, file_offset, file_size)); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 153 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 154 | if (object_container_up) |
| 155 | object_file_sp = object_container_up->GetObjectFile(file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | |
| 157 | if (object_file_sp.get()) |
| 158 | return object_file_sp; |
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 | } |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 161 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 163 | // We didn't find it, so clear our shared pointer in case it contains |
| 164 | // anything and return an empty shared pointer |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | object_file_sp.reset(); |
| 166 | return object_file_sp; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, |
| 170 | const ProcessSP &process_sp, |
| 171 | lldb::addr_t header_addr, |
| 172 | DataBufferSP &data_sp) { |
| 173 | ObjectFileSP object_file_sp; |
| 174 | |
| 175 | if (module_sp) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 176 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
| 177 | Timer scoped_timer(func_cat, |
| 178 | "ObjectFile::FindPlugin (module = " |
| 179 | "%s, process = %p, header_addr = " |
| 180 | "0x%" PRIx64 ")", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | module_sp->GetFileSpec().GetPath().c_str(), |
| 182 | static_cast<void *>(process_sp.get()), header_addr); |
| 183 | uint32_t idx; |
| 184 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 185 | // Check if this is a normal object file by iterating through all object |
| 186 | // file plugin instances. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | ObjectFileCreateMemoryInstance create_callback; |
| 188 | for (idx = 0; |
| 189 | (create_callback = |
| 190 | PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != |
| 191 | nullptr; |
| 192 | ++idx) { |
| 193 | object_file_sp.reset( |
| 194 | create_callback(module_sp, data_sp, process_sp, header_addr)); |
| 195 | if (object_file_sp.get()) |
| 196 | return object_file_sp; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 197 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 199 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 200 | // We didn't find it, so clear our shared pointer in case it contains |
| 201 | // anything and return an empty shared pointer |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | object_file_sp.reset(); |
| 203 | return object_file_sp; |
| 204 | } |
| 205 | |
| 206 | size_t ObjectFile::GetModuleSpecifications(const FileSpec &file, |
| 207 | lldb::offset_t file_offset, |
| 208 | lldb::offset_t file_size, |
| 209 | ModuleSpecList &specs) { |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 210 | DataBufferSP data_sp = |
| 211 | FileSystem::Instance().CreateDataBuffer(file.GetPath(), 512, file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | if (data_sp) { |
| 213 | if (file_size == 0) { |
Jonas Devlieghere | 59b78bc | 2018-11-01 04:45:28 +0000 | [diff] [blame] | 214 | const lldb::offset_t actual_file_size = |
| 215 | FileSystem::Instance().GetByteSize(file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | if (actual_file_size > file_offset) |
| 217 | file_size = actual_file_size - file_offset; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 218 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | return ObjectFile::GetModuleSpecifications(file, // file spec |
| 220 | data_sp, // data bytes |
| 221 | 0, // data offset |
| 222 | file_offset, // file offset |
| 223 | file_size, // file length |
| 224 | specs); |
| 225 | } |
| 226 | return 0; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | size_t ObjectFile::GetModuleSpecifications( |
| 230 | const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, |
| 231 | lldb::offset_t data_offset, lldb::offset_t file_offset, |
| 232 | lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { |
| 233 | const size_t initial_count = specs.GetSize(); |
| 234 | ObjectFileGetModuleSpecifications callback; |
| 235 | uint32_t i; |
| 236 | // Try the ObjectFile plug-ins |
| 237 | for (i = 0; |
| 238 | (callback = |
| 239 | PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex( |
| 240 | i)) != nullptr; |
| 241 | ++i) { |
| 242 | if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) |
| 243 | return specs.GetSize() - initial_count; |
| 244 | } |
| 245 | |
| 246 | // Try the ObjectContainer plug-ins |
| 247 | for (i = 0; |
| 248 | (callback = PluginManager:: |
| 249 | GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != |
| 250 | nullptr; |
| 251 | ++i) { |
| 252 | if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) |
| 253 | return specs.GetSize() - initial_count; |
| 254 | } |
| 255 | return 0; |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, |
| 259 | const FileSpec *file_spec_ptr, |
| 260 | lldb::offset_t file_offset, lldb::offset_t length, |
| 261 | const lldb::DataBufferSP &data_sp, |
| 262 | lldb::offset_t data_offset) |
| 263 | : ModuleChild(module_sp), |
| 264 | m_file(), // This file could be different from the original module's file |
| 265 | m_type(eTypeInvalid), m_strata(eStrataInvalid), |
Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 266 | m_file_offset(file_offset), m_length(length), m_data(), m_process_wp(), |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 267 | m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_up(), m_symtab_up(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | m_synthetic_symbol_idx(0) { |
| 269 | if (file_spec_ptr) |
| 270 | m_file = *file_spec_ptr; |
| 271 | if (data_sp) |
| 272 | m_data.SetData(data_sp, data_offset, length); |
| 273 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 274 | LLDB_LOGF(log, |
| 275 | "%p ObjectFile::ObjectFile() module = %p (%s), file = %s, " |
| 276 | "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, |
| 277 | static_cast<void *>(this), static_cast<void *>(module_sp.get()), |
| 278 | module_sp->GetSpecificationDescription().c_str(), |
| 279 | m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset, |
| 280 | m_length); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, |
| 284 | const ProcessSP &process_sp, lldb::addr_t header_addr, |
| 285 | DataBufferSP &header_data_sp) |
| 286 | : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid), |
| 287 | m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(), |
Pavel Labath | 66d8832 | 2019-02-14 14:40:10 +0000 | [diff] [blame] | 288 | m_process_wp(process_sp), m_memory_addr(header_addr), m_sections_up(), |
| 289 | m_symtab_up(), m_synthetic_symbol_idx(0) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 290 | if (header_data_sp) |
| 291 | m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize()); |
| 292 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 293 | LLDB_LOGF(log, |
| 294 | "%p ObjectFile::ObjectFile() module = %p (%s), process = %p, " |
| 295 | "header_addr = 0x%" PRIx64, |
| 296 | static_cast<void *>(this), static_cast<void *>(module_sp.get()), |
| 297 | module_sp->GetSpecificationDescription().c_str(), |
| 298 | static_cast<void *>(process_sp.get()), m_memory_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 300 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 301 | ObjectFile::~ObjectFile() { |
| 302 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 303 | LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this)); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) { |
| 307 | ModuleSP module_sp(GetModule()); |
| 308 | if (module_sp) |
| 309 | return module_sp->SetArchitecture(new_arch); |
| 310 | return false; |
| 311 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | AddressClass ObjectFile::GetAddressClass(addr_t file_addr) { |
| 314 | Symtab *symtab = GetSymtab(); |
| 315 | if (symtab) { |
| 316 | Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); |
| 317 | if (symbol) { |
| 318 | if (symbol->ValueIsAddress()) { |
| 319 | const SectionSP section_sp(symbol->GetAddressRef().GetSection()); |
| 320 | if (section_sp) { |
| 321 | const SectionType section_type = section_sp->GetType(); |
| 322 | switch (section_type) { |
| 323 | case eSectionTypeInvalid: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 324 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 325 | case eSectionTypeCode: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 326 | return AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 327 | case eSectionTypeContainer: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 328 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | case eSectionTypeData: |
| 330 | case eSectionTypeDataCString: |
| 331 | case eSectionTypeDataCStringPointers: |
| 332 | case eSectionTypeDataSymbolAddress: |
| 333 | case eSectionTypeData4: |
| 334 | case eSectionTypeData8: |
| 335 | case eSectionTypeData16: |
| 336 | case eSectionTypeDataPointers: |
| 337 | case eSectionTypeZeroFill: |
| 338 | case eSectionTypeDataObjCMessageRefs: |
| 339 | case eSectionTypeDataObjCCFStrings: |
| 340 | case eSectionTypeGoSymtab: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 341 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | case eSectionTypeDebug: |
| 343 | case eSectionTypeDWARFDebugAbbrev: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 344 | case eSectionTypeDWARFDebugAbbrevDwo: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | case eSectionTypeDWARFDebugAddr: |
| 346 | case eSectionTypeDWARFDebugAranges: |
Tamas Berghammer | 963ce48 | 2017-08-25 13:56:14 +0000 | [diff] [blame] | 347 | case eSectionTypeDWARFDebugCuIndex: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | case eSectionTypeDWARFDebugFrame: |
| 349 | case eSectionTypeDWARFDebugInfo: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 350 | case eSectionTypeDWARFDebugInfoDwo: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | case eSectionTypeDWARFDebugLine: |
George Rimar | c6c7bfc | 2018-09-13 17:06:47 +0000 | [diff] [blame] | 352 | case eSectionTypeDWARFDebugLineStr: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | case eSectionTypeDWARFDebugLoc: |
George Rimar | e4dee26 | 2018-10-23 09:46:15 +0000 | [diff] [blame] | 354 | case eSectionTypeDWARFDebugLocLists: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | case eSectionTypeDWARFDebugMacInfo: |
| 356 | case eSectionTypeDWARFDebugMacro: |
Pavel Labath | a041d84 | 2018-06-01 12:06:45 +0000 | [diff] [blame] | 357 | case eSectionTypeDWARFDebugNames: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 358 | case eSectionTypeDWARFDebugPubNames: |
| 359 | case eSectionTypeDWARFDebugPubTypes: |
| 360 | case eSectionTypeDWARFDebugRanges: |
George Rimar | 6e35712 | 2018-10-10 08:11:15 +0000 | [diff] [blame] | 361 | case eSectionTypeDWARFDebugRngLists: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 362 | case eSectionTypeDWARFDebugStr: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 363 | case eSectionTypeDWARFDebugStrDwo: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | case eSectionTypeDWARFDebugStrOffsets: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 365 | case eSectionTypeDWARFDebugStrOffsetsDwo: |
Greg Clayton | 2550ca1 | 2018-05-08 17:19:24 +0000 | [diff] [blame] | 366 | case eSectionTypeDWARFDebugTypes: |
Pavel Labath | ad805ef | 2019-06-12 11:42:42 +0000 | [diff] [blame] | 367 | case eSectionTypeDWARFDebugTypesDwo: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | case eSectionTypeDWARFAppleNames: |
| 369 | case eSectionTypeDWARFAppleTypes: |
| 370 | case eSectionTypeDWARFAppleNamespaces: |
| 371 | case eSectionTypeDWARFAppleObjC: |
Jan Kratochvil | e4777a9 | 2018-04-29 19:47:48 +0000 | [diff] [blame] | 372 | case eSectionTypeDWARFGNUDebugAltLink: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 373 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | case eSectionTypeEHFrame: |
| 375 | case eSectionTypeARMexidx: |
| 376 | case eSectionTypeARMextab: |
| 377 | case eSectionTypeCompactUnwind: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 378 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | case eSectionTypeELFSymbolTable: |
| 380 | case eSectionTypeELFDynamicSymbols: |
| 381 | case eSectionTypeELFRelocationEntries: |
| 382 | case eSectionTypeELFDynamicLinkInfo: |
| 383 | case eSectionTypeOther: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 384 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | case eSectionTypeAbsoluteAddress: |
| 386 | // In case of absolute sections decide the address class based on |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 387 | // the symbol type because the section type isn't specify if it is |
| 388 | // a code or a data section. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | break; |
| 390 | } |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 391 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | const SymbolType symbol_type = symbol->GetType(); |
| 395 | switch (symbol_type) { |
| 396 | case eSymbolTypeAny: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 397 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | case eSymbolTypeAbsolute: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 399 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | case eSymbolTypeCode: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 401 | return AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | case eSymbolTypeTrampoline: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 403 | return AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 404 | case eSymbolTypeResolver: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 405 | return AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 406 | case eSymbolTypeData: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 407 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 408 | case eSymbolTypeRuntime: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 409 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | case eSymbolTypeException: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 411 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 412 | case eSymbolTypeSourceFile: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 413 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 414 | case eSymbolTypeHeaderFile: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 415 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | case eSymbolTypeObjectFile: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 417 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | case eSymbolTypeCommonBlock: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 419 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 420 | case eSymbolTypeBlock: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 421 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 422 | case eSymbolTypeLocal: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 423 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 424 | case eSymbolTypeParam: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 425 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 426 | case eSymbolTypeVariable: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 427 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | case eSymbolTypeVariableType: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 429 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | case eSymbolTypeLineEntry: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 431 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 432 | case eSymbolTypeLineHeader: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 433 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 434 | case eSymbolTypeScopeBegin: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 435 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 436 | case eSymbolTypeScopeEnd: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 437 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | case eSymbolTypeAdditional: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 439 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | case eSymbolTypeCompiler: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 441 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | case eSymbolTypeInstrumentation: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 443 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | case eSymbolTypeUndefined: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 445 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | case eSymbolTypeObjCClass: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 447 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 448 | case eSymbolTypeObjCMetaClass: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 449 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | case eSymbolTypeObjCIVar: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 451 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 452 | case eSymbolTypeReExported: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 453 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 454 | } |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 455 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 456 | } |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 457 | return AddressClass::eUnknown; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 460 | DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp, |
| 461 | lldb::addr_t addr, size_t byte_size) { |
| 462 | DataBufferSP data_sp; |
| 463 | if (process_sp) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 464 | std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 465 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 466 | const size_t bytes_read = process_sp->ReadMemory( |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 467 | addr, data_up->GetBytes(), data_up->GetByteSize(), error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 468 | if (bytes_read == byte_size) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 469 | data_sp.reset(data_up.release()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 470 | } |
| 471 | return data_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 474 | size_t ObjectFile::GetData(lldb::offset_t offset, size_t length, |
| 475 | DataExtractor &data) const { |
| 476 | // The entire file has already been mmap'ed into m_data, so just copy from |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 477 | // there as the back mmap buffer will be shared with shared pointers. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 478 | return data.SetData(m_data, offset, length); |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length, |
| 482 | void *dst) const { |
| 483 | // The entire file has already been mmap'ed into m_data, so just copy from |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 484 | // there Note that the data remains in target byte order. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 485 | return m_data.CopyData(offset, length, dst); |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 486 | } |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 487 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 488 | size_t ObjectFile::ReadSectionData(Section *section, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | lldb::offset_t section_offset, void *dst, |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 490 | size_t dst_len) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 491 | assert(section); |
| 492 | section_offset *= section->GetTargetByteSize(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 493 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 494 | // If some other objectfile owns this data, pass this to them. |
| 495 | if (section->GetObjectFile() != this) |
| 496 | return section->GetObjectFile()->ReadSectionData(section, section_offset, |
| 497 | dst, dst_len); |
Matthew Gardiner | f03e6d84 | 2014-09-29 08:02:24 +0000 | [diff] [blame] | 498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | if (IsInMemory()) { |
| 500 | ProcessSP process_sp(m_process_wp.lock()); |
| 501 | if (process_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 502 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 503 | const addr_t base_load_addr = |
| 504 | section->GetLoadBaseAddress(&process_sp->GetTarget()); |
| 505 | if (base_load_addr != LLDB_INVALID_ADDRESS) |
| 506 | return process_sp->ReadMemory(base_load_addr + section_offset, dst, |
| 507 | dst_len, error); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 508 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 509 | } else { |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 510 | if (!section->IsRelocated()) |
| 511 | RelocateSection(section); |
| 512 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 513 | const lldb::offset_t section_file_size = section->GetFileSize(); |
| 514 | if (section_offset < section_file_size) { |
| 515 | const size_t section_bytes_left = section_file_size - section_offset; |
| 516 | size_t section_dst_len = dst_len; |
| 517 | if (section_dst_len > section_bytes_left) |
| 518 | section_dst_len = section_bytes_left; |
| 519 | return CopyData(section->GetFileOffset() + section_offset, |
| 520 | section_dst_len, dst); |
| 521 | } else { |
| 522 | if (section->GetType() == eSectionTypeZeroFill) { |
| 523 | const uint64_t section_size = section->GetByteSize(); |
| 524 | const uint64_t section_bytes_left = section_size - section_offset; |
| 525 | uint64_t section_dst_len = dst_len; |
| 526 | if (section_dst_len > section_bytes_left) |
| 527 | section_dst_len = section_bytes_left; |
| 528 | memset(dst, 0, section_dst_len); |
| 529 | return section_dst_len; |
| 530 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 531 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | } |
| 533 | return 0; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 536 | // Get the section data the file on disk |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 537 | size_t ObjectFile::ReadSectionData(Section *section, |
| 538 | DataExtractor §ion_data) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | // If some other objectfile owns this data, pass this to them. |
| 540 | if (section->GetObjectFile() != this) |
| 541 | return section->GetObjectFile()->ReadSectionData(section, section_data); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 542 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 543 | if (IsInMemory()) { |
| 544 | ProcessSP process_sp(m_process_wp.lock()); |
| 545 | if (process_sp) { |
| 546 | const addr_t base_load_addr = |
| 547 | section->GetLoadBaseAddress(&process_sp->GetTarget()); |
| 548 | if (base_load_addr != LLDB_INVALID_ADDRESS) { |
| 549 | DataBufferSP data_sp( |
| 550 | ReadMemory(process_sp, base_load_addr, section->GetByteSize())); |
| 551 | if (data_sp) { |
| 552 | section_data.SetData(data_sp, 0, data_sp->GetByteSize()); |
| 553 | section_data.SetByteOrder(process_sp->GetByteOrder()); |
| 554 | section_data.SetAddressByteSize(process_sp->GetAddressByteSize()); |
| 555 | return section_data.GetByteSize(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 556 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 557 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 558 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 559 | return GetData(section->GetFileOffset(), section->GetFileSize(), |
| 560 | section_data); |
| 561 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 562 | // The object file now contains a full mmap'ed copy of the object file |
| 563 | // data, so just use this |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 564 | if (!section->IsRelocated()) |
| 565 | RelocateSection(section); |
| 566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 567 | return GetData(section->GetFileOffset(), section->GetFileSize(), |
| 568 | section_data); |
| 569 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 572 | bool ObjectFile::SplitArchivePathWithObject(const char *path_with_object, |
| 573 | FileSpec &archive_file, |
| 574 | ConstString &archive_object, |
| 575 | bool must_exist) { |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 576 | RegularExpression g_object_regex(llvm::StringRef("(.*)\\(([^\\)]+)\\)$")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | RegularExpression::Match regex_match(2); |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 578 | if (g_object_regex.Execute(llvm::StringRef::withNullAsEmpty(path_with_object), |
| 579 | ®ex_match)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | std::string path; |
| 581 | std::string obj; |
| 582 | if (regex_match.GetMatchAtIndex(path_with_object, 1, path) && |
| 583 | regex_match.GetMatchAtIndex(path_with_object, 2, obj)) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 584 | archive_file.SetFile(path, FileSpec::Style::native); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 585 | archive_object.SetCString(obj.c_str()); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 586 | return !(must_exist && !FileSystem::Instance().Exists(archive_file)); |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 587 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | } |
| 589 | return false; |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 592 | void ObjectFile::ClearSymtab() { |
| 593 | ModuleSP module_sp(GetModule()); |
| 594 | if (module_sp) { |
| 595 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 596 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 597 | LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p", |
| 598 | static_cast<void *>(this), |
| 599 | static_cast<void *>(m_symtab_up.get())); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 600 | m_symtab_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | SectionList *ObjectFile::GetSectionList(bool update_module_section_list) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 605 | if (m_sections_up == nullptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 606 | if (update_module_section_list) { |
| 607 | ModuleSP module_sp(GetModule()); |
| 608 | if (module_sp) { |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 609 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 610 | CreateSections(*module_sp->GetUnifiedSectionList()); |
| 611 | } |
| 612 | } else { |
| 613 | SectionList unified_section_list; |
| 614 | CreateSections(unified_section_list); |
Greg Clayton | 9422dd6 | 2013-03-04 21:46:16 +0000 | [diff] [blame] | 615 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 616 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 617 | return m_sections_up.get(); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 618 | } |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 619 | |
| 620 | lldb::SymbolType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 621 | ObjectFile::GetSymbolTypeFromName(llvm::StringRef name, |
| 622 | lldb::SymbolType symbol_type_hint) { |
| 623 | if (!name.empty()) { |
| 624 | if (name.startswith("_OBJC_")) { |
| 625 | // ObjC |
| 626 | if (name.startswith("_OBJC_CLASS_$_")) |
| 627 | return lldb::eSymbolTypeObjCClass; |
| 628 | if (name.startswith("_OBJC_METACLASS_$_")) |
| 629 | return lldb::eSymbolTypeObjCMetaClass; |
| 630 | if (name.startswith("_OBJC_IVAR_$_")) |
| 631 | return lldb::eSymbolTypeObjCIVar; |
| 632 | } else if (name.startswith(".objc_class_name_")) { |
| 633 | // ObjC v1 |
| 634 | return lldb::eSymbolTypeObjCClass; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 635 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 636 | } |
| 637 | return symbol_type_hint; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 638 | } |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 639 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 640 | ConstString ObjectFile::GetNextSyntheticSymbolName() { |
| 641 | StreamString ss; |
| 642 | ConstString file_name = GetModule()->GetFileSpec().GetFilename(); |
| 643 | ss.Printf("___lldb_unnamed_symbol%u$$%s", ++m_synthetic_symbol_idx, |
| 644 | file_name.GetCString()); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 645 | return ConstString(ss.GetString()); |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 646 | } |
Hafiz Abid Qadeer | 4687db0 | 2017-01-19 17:32:50 +0000 | [diff] [blame] | 647 | |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 648 | std::vector<ObjectFile::LoadableData> |
| 649 | ObjectFile::GetLoadableData(Target &target) { |
| 650 | std::vector<LoadableData> loadables; |
Hafiz Abid Qadeer | 4687db0 | 2017-01-19 17:32:50 +0000 | [diff] [blame] | 651 | SectionList *section_list = GetSectionList(); |
| 652 | if (!section_list) |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 653 | return loadables; |
| 654 | // Create a list of loadable data from loadable sections |
Hafiz Abid Qadeer | 4687db0 | 2017-01-19 17:32:50 +0000 | [diff] [blame] | 655 | size_t section_count = section_list->GetNumSections(0); |
| 656 | for (size_t i = 0; i < section_count; ++i) { |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 657 | LoadableData loadable; |
Hafiz Abid Qadeer | 4687db0 | 2017-01-19 17:32:50 +0000 | [diff] [blame] | 658 | SectionSP section_sp = section_list->GetSectionAtIndex(i); |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 659 | loadable.Dest = |
| 660 | target.GetSectionLoadList().GetSectionLoadAddress(section_sp); |
| 661 | if (loadable.Dest == LLDB_INVALID_ADDRESS) |
| 662 | continue; |
| 663 | // We can skip sections like bss |
| 664 | if (section_sp->GetFileSize() == 0) |
| 665 | continue; |
| 666 | DataExtractor section_data; |
| 667 | section_sp->GetSectionData(section_data); |
| 668 | loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(), |
| 669 | section_data.GetByteSize()); |
| 670 | loadables.push_back(loadable); |
Hafiz Abid Qadeer | 4687db0 | 2017-01-19 17:32:50 +0000 | [diff] [blame] | 671 | } |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 672 | return loadables; |
Hafiz Abid Qadeer | 4687db0 | 2017-01-19 17:32:50 +0000 | [diff] [blame] | 673 | } |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 674 | |
| 675 | void ObjectFile::RelocateSection(lldb_private::Section *section) |
| 676 | { |
| 677 | } |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 678 | |
| 679 | DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size, |
| 680 | uint64_t Offset) { |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 681 | return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset); |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 682 | } |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 683 | |
| 684 | void llvm::format_provider<ObjectFile::Type>::format( |
| 685 | const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) { |
| 686 | switch (type) { |
| 687 | case ObjectFile::eTypeInvalid: |
| 688 | OS << "invalid"; |
| 689 | break; |
| 690 | case ObjectFile::eTypeCoreFile: |
| 691 | OS << "core file"; |
| 692 | break; |
| 693 | case ObjectFile::eTypeExecutable: |
| 694 | OS << "executable"; |
| 695 | break; |
| 696 | case ObjectFile::eTypeDebugInfo: |
| 697 | OS << "debug info"; |
| 698 | break; |
| 699 | case ObjectFile::eTypeDynamicLinker: |
| 700 | OS << "dynamic linker"; |
| 701 | break; |
| 702 | case ObjectFile::eTypeObjectFile: |
| 703 | OS << "object file"; |
| 704 | break; |
| 705 | case ObjectFile::eTypeSharedLibrary: |
| 706 | OS << "shared library"; |
| 707 | break; |
| 708 | case ObjectFile::eTypeStubLibrary: |
| 709 | OS << "stub library"; |
| 710 | break; |
| 711 | case ObjectFile::eTypeJIT: |
| 712 | OS << "jit"; |
| 713 | break; |
| 714 | case ObjectFile::eTypeUnknown: |
| 715 | OS << "unknown"; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | void llvm::format_provider<ObjectFile::Strata>::format( |
| 721 | const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) { |
| 722 | switch (strata) { |
| 723 | case ObjectFile::eStrataInvalid: |
| 724 | OS << "invalid"; |
| 725 | break; |
| 726 | case ObjectFile::eStrataUnknown: |
| 727 | OS << "unknown"; |
| 728 | break; |
| 729 | case ObjectFile::eStrataUser: |
| 730 | OS << "user"; |
| 731 | break; |
| 732 | case ObjectFile::eStrataKernel: |
| 733 | OS << "kernel"; |
| 734 | break; |
| 735 | case ObjectFile::eStrataRawImage: |
| 736 | OS << "raw image"; |
| 737 | break; |
| 738 | case ObjectFile::eStrataJIT: |
| 739 | OS << "jit"; |
| 740 | break; |
| 741 | } |
| 742 | } |