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