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