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