blob: e8a963e5767e344c8c5820ced8f8a6d782330f0f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Clayton44435ed2012-01-12 05:25:17 +000011#include "lldb/Core/DataBuffer.h"
Greg Claytonc9660542012-02-05 02:38:54 +000012#include "lldb/Core/DataBufferHeap.h"
Greg Clayton762f7132011-09-18 18:59:15 +000013#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000015#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/PluginManager.h"
17#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Timer.h"
20#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Symbol/ObjectContainer.h"
22#include "lldb/Symbol/SymbolFile.h"
Greg Claytonc9660542012-02-05 02:38:54 +000023#include "lldb/Target/Process.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
Greg Clayton762f7132011-09-18 18:59:15 +000029ObjectFileSP
Greg Clayton5ce9c562013-02-06 17:22:03 +000030ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp,
31 const FileSpec* file,
32 lldb::offset_t file_offset,
33 lldb::offset_t file_size,
34 DataBufferSP &data_sp,
35 lldb::offset_t &data_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036{
Greg Clayton762f7132011-09-18 18:59:15 +000037 ObjectFileSP object_file_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
Greg Claytone72dfb32012-02-24 01:59:29 +000039 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 {
Greg Claytone72dfb32012-02-24 01:59:29 +000041 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000042 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
43 module_sp->GetFileSpec().GetPath().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000044 static_cast<const void*>(file),
45 static_cast<uint64_t>(file_offset),
46 static_cast<uint64_t>(file_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047 if (file)
48 {
Greg Clayton5ce9c562013-02-06 17:22:03 +000049 FileSpec archive_file;
50 ObjectContainerCreateInstance create_object_container_callback;
51
52 const bool file_exists = file->Exists();
53 if (!data_sp)
Greg Clayton44435ed2012-01-12 05:25:17 +000054 {
Greg Clayton5ce9c562013-02-06 17:22:03 +000055 // We have an object name which most likely means we have
56 // a .o file in a static archive (.a file). Try and see if
57 // we have a cached archive first without reading any data
58 // first
59 if (file_exists && module_sp->GetObjectName())
60 {
Ed Masted4612ad2014-04-20 13:17:36 +000061 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton5ce9c562013-02-06 17:22:03 +000062 {
Greg Clayton7b0992d2013-04-18 22:45:39 +000063 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000064
Greg Clayton5ce9c562013-02-06 17:22:03 +000065 if (object_container_ap.get())
66 object_file_sp = object_container_ap->GetObjectFile(file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000067
Greg Clayton5ce9c562013-02-06 17:22:03 +000068 if (object_file_sp.get())
69 return object_file_sp;
70 }
71 }
72 // Ok, we didn't find any containers that have a named object, now
73 // lets read the first 512 bytes from the file so the object file
74 // and object container plug-ins can use these bytes to see if they
75 // can parse this file.
76 if (file_size > 0)
77 {
78 data_sp = file->ReadFileContents(file_offset, std::min<size_t>(512, file_size));
79 data_offset = 0;
80 }
Greg Clayton44435ed2012-01-12 05:25:17 +000081 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
Greg Clayton5ce9c562013-02-06 17:22:03 +000083 if (!data_sp || data_sp->GetByteSize() == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 {
85 // Check for archive file with format "/path/to/archive.a(object.o)"
86 char path_with_object[PATH_MAX*2];
Greg Claytone72dfb32012-02-24 01:59:29 +000087 module_sp->GetFileSpec().GetPath(path_with_object, sizeof(path_with_object));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088
Greg Clayton1f746072012-08-29 21:13:06 +000089 ConstString archive_object;
Greg Clayton906ba472013-02-06 00:38:25 +000090 const bool must_exist = true;
91 if (ObjectFile::SplitArchivePathWithObject (path_with_object, archive_file, archive_object, must_exist))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 {
Greg Clayton1f746072012-08-29 21:13:06 +000093 file_size = archive_file.GetByteSize();
94 if (file_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 {
Greg Clayton5ce9c562013-02-06 17:22:03 +000096 file = &archive_file;
Greg Clayton1f746072012-08-29 21:13:06 +000097 module_sp->SetFileSpecAndObjectName (archive_file, archive_object);
Greg Clayton5ce9c562013-02-06 17:22:03 +000098 // Check if this is a object container by iterating through all object
99 // container plugin instances and then trying to get an object file
100 // from the container plugins since we had a name. Also, don't read
101 // ANY data in case there is data cached in the container plug-ins
102 // (like BSD archives caching the contained objects within an file).
Ed Masted4612ad2014-04-20 13:17:36 +0000103 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton5ce9c562013-02-06 17:22:03 +0000104 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000105 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000106
Greg Clayton5ce9c562013-02-06 17:22:03 +0000107 if (object_container_ap.get())
108 object_file_sp = object_container_ap->GetObjectFile(file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000109
Greg Clayton5ce9c562013-02-06 17:22:03 +0000110 if (object_file_sp.get())
111 return object_file_sp;
112 }
113 // We failed to find any cached object files in the container
114 // plug-ins, so lets read the first 512 bytes and try again below...
115 data_sp = archive_file.ReadFileContents(file_offset, 512);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 }
117 }
118 }
119
Greg Clayton5ce9c562013-02-06 17:22:03 +0000120 if (data_sp && data_sp->GetByteSize() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121 {
Greg Clayton44435ed2012-01-12 05:25:17 +0000122 // Check if this is a normal object file by iterating through
123 // all object file plugin instances.
124 ObjectFileCreateInstance create_object_file_callback;
Ed Masted4612ad2014-04-20 13:17:36 +0000125 for (uint32_t idx = 0; (create_object_file_callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton44435ed2012-01-12 05:25:17 +0000126 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000127 object_file_sp.reset (create_object_file_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
Greg Clayton44435ed2012-01-12 05:25:17 +0000128 if (object_file_sp.get())
129 return object_file_sp;
130 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131
Greg Clayton44435ed2012-01-12 05:25:17 +0000132 // Check if this is a object container by iterating through
133 // all object container plugin instances and then trying to get
134 // an object file from the container.
Ed Masted4612ad2014-04-20 13:17:36 +0000135 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton44435ed2012-01-12 05:25:17 +0000136 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000137 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138
Greg Clayton44435ed2012-01-12 05:25:17 +0000139 if (object_container_ap.get())
140 object_file_sp = object_container_ap->GetObjectFile(file);
141
142 if (object_file_sp.get())
143 return object_file_sp;
144 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145 }
146 }
147 }
Greg Clayton762f7132011-09-18 18:59:15 +0000148 // We didn't find it, so clear our shared pointer in case it
149 // contains anything and return an empty shared pointer
150 object_file_sp.reset();
151 return object_file_sp;
152}
153
Greg Claytonc9660542012-02-05 02:38:54 +0000154ObjectFileSP
Greg Claytone72dfb32012-02-24 01:59:29 +0000155ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000156 const ProcessSP &process_sp,
157 lldb::addr_t header_addr,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000158 DataBufferSP &data_sp)
Greg Claytonc9660542012-02-05 02:38:54 +0000159{
Greg Claytonc9660542012-02-05 02:38:54 +0000160 ObjectFileSP object_file_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000161
Greg Claytone72dfb32012-02-24 01:59:29 +0000162 if (module_sp)
Greg Claytonc9660542012-02-05 02:38:54 +0000163 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000164 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000165 "ObjectFile::FindPlugin (module = %s, process = %p, header_addr = 0x%" PRIx64 ")",
166 module_sp->GetFileSpec().GetPath().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000167 static_cast<void*>(process_sp.get()), header_addr);
Greg Claytonc9660542012-02-05 02:38:54 +0000168 uint32_t idx;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000169
Greg Claytonc9660542012-02-05 02:38:54 +0000170 // Check if this is a normal object file by iterating through
171 // all object file plugin instances.
172 ObjectFileCreateMemoryInstance create_callback;
Ed Masted4612ad2014-04-20 13:17:36 +0000173 for (idx = 0; (create_callback = PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Claytonc9660542012-02-05 02:38:54 +0000174 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000175 object_file_sp.reset (create_callback(module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +0000176 if (object_file_sp.get())
177 return object_file_sp;
178 }
Greg Claytonc9660542012-02-05 02:38:54 +0000179 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000180
Greg Claytonc9660542012-02-05 02:38:54 +0000181 // We didn't find it, so clear our shared pointer in case it
182 // contains anything and return an empty shared pointer
183 object_file_sp.reset();
184 return object_file_sp;
185}
186
Greg Claytonf4d6de62013-04-24 22:29:28 +0000187size_t
188ObjectFile::GetModuleSpecifications (const FileSpec &file,
189 lldb::offset_t file_offset,
Greg Clayton2540a8a2013-07-12 22:07:46 +0000190 lldb::offset_t file_size,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000191 ModuleSpecList &specs)
192{
193 DataBufferSP data_sp (file.ReadFileContents(file_offset, 512));
194 if (data_sp)
Greg Clayton2540a8a2013-07-12 22:07:46 +0000195 {
196 if (file_size == 0)
197 {
198 const lldb::offset_t actual_file_size = file.GetByteSize();
199 if (actual_file_size > file_offset)
200 file_size = actual_file_size - file_offset;
201 }
202 return ObjectFile::GetModuleSpecifications (file, // file spec
203 data_sp, // data bytes
204 0, // data offset
205 file_offset,// file offset
206 file_size, // file length
Greg Claytonf4d6de62013-04-24 22:29:28 +0000207 specs);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000208 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000209 return 0;
210}
211
212size_t
213ObjectFile::GetModuleSpecifications (const lldb_private::FileSpec& file,
214 lldb::DataBufferSP& data_sp,
215 lldb::offset_t data_offset,
216 lldb::offset_t file_offset,
Greg Clayton2540a8a2013-07-12 22:07:46 +0000217 lldb::offset_t file_size,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000218 lldb_private::ModuleSpecList &specs)
219{
220 const size_t initial_count = specs.GetSize();
221 ObjectFileGetModuleSpecifications callback;
222 uint32_t i;
223 // Try the ObjectFile plug-ins
Ed Masted4612ad2014-04-20 13:17:36 +0000224 for (i = 0; (callback = PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000225 {
Greg Clayton2540a8a2013-07-12 22:07:46 +0000226 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000227 return specs.GetSize() - initial_count;
228 }
229
230 // Try the ObjectContainer plug-ins
Ed Masted4612ad2014-04-20 13:17:36 +0000231 for (i = 0; (callback = PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000232 {
Greg Clayton2540a8a2013-07-12 22:07:46 +0000233 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000234 return specs.GetSize() - initial_count;
235 }
236 return 0;
237}
238
239ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp,
240 const FileSpec *file_spec_ptr,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000241 lldb::offset_t file_offset,
242 lldb::offset_t length,
Greg Clayton23f8c952014-03-24 23:10:19 +0000243 const lldb::DataBufferSP& data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000244 lldb::offset_t data_offset
245) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000246 ModuleChild (module_sp),
Greg Clayton762f7132011-09-18 18:59:15 +0000247 m_file (), // This file could be different from the original module's file
248 m_type (eTypeInvalid),
249 m_strata (eStrataInvalid),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000250 m_file_offset (file_offset),
251 m_length (length),
Greg Clayton44435ed2012-01-12 05:25:17 +0000252 m_data (),
Greg Claytonc9660542012-02-05 02:38:54 +0000253 m_unwind_table (*this),
254 m_process_wp(),
Greg Clayton9422dd62013-03-04 21:46:16 +0000255 m_memory_addr (LLDB_INVALID_ADDRESS),
Greg Clayton3046e662013-07-10 01:23:25 +0000256 m_sections_ap(),
257 m_symtab_ap ()
Greg Clayton9422dd62013-03-04 21:46:16 +0000258{
Greg Clayton762f7132011-09-18 18:59:15 +0000259 if (file_spec_ptr)
260 m_file = *file_spec_ptr;
Greg Clayton5ce9c562013-02-06 17:22:03 +0000261 if (data_sp)
262 m_data.SetData (data_sp, data_offset, length);
Greg Clayton5160ce52013-03-27 23:08:40 +0000263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton762f7132011-09-18 18:59:15 +0000264 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000265 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
266 static_cast<void*>(this),
267 static_cast<void*>(module_sp.get()),
268 module_sp->GetSpecificationDescription().c_str(),
269 m_file ? m_file.GetPath().c_str() : "<NULL>",
270 m_file_offset, m_length);
Greg Clayton762f7132011-09-18 18:59:15 +0000271}
272
Greg Claytonc9660542012-02-05 02:38:54 +0000273
Greg Claytone72dfb32012-02-24 01:59:29 +0000274ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000275 const ProcessSP &process_sp,
276 lldb::addr_t header_addr,
277 DataBufferSP& header_data_sp) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000278 ModuleChild (module_sp),
Greg Claytonc9660542012-02-05 02:38:54 +0000279 m_file (),
280 m_type (eTypeInvalid),
281 m_strata (eStrataInvalid),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000282 m_file_offset (0),
Greg Claytonc9660542012-02-05 02:38:54 +0000283 m_length (0),
284 m_data (),
285 m_unwind_table (*this),
286 m_process_wp (process_sp),
Greg Clayton9422dd62013-03-04 21:46:16 +0000287 m_memory_addr (header_addr),
Greg Clayton3046e662013-07-10 01:23:25 +0000288 m_sections_ap(),
289 m_symtab_ap ()
Greg Clayton9422dd62013-03-04 21:46:16 +0000290{
Greg Claytonc9660542012-02-05 02:38:54 +0000291 if (header_data_sp)
292 m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize());
Greg Clayton5160ce52013-03-27 23:08:40 +0000293 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Claytonc9660542012-02-05 02:38:54 +0000294 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000295 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, header_addr = 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000296 static_cast<void*>(this),
297 static_cast<void*>(module_sp.get()),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000298 module_sp->GetSpecificationDescription().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000299 static_cast<void*>(process_sp.get()), m_memory_addr);
Greg Claytonc9660542012-02-05 02:38:54 +0000300}
301
302
Greg Clayton762f7132011-09-18 18:59:15 +0000303ObjectFile::~ObjectFile()
304{
Greg Clayton5160ce52013-03-27 23:08:40 +0000305 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton762f7132011-09-18 18:59:15 +0000306 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000307 log->Printf ("%p ObjectFile::~ObjectFile ()\n",
308 static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309}
Jim Ingham5aee1622010-08-09 23:31:02 +0000310
311bool
312ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch)
313{
Greg Claytone72dfb32012-02-24 01:59:29 +0000314 ModuleSP module_sp (GetModule());
315 if (module_sp)
316 return module_sp->SetArchitecture (new_arch);
317 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +0000318}
319
Greg Claytone0d378b2011-03-24 21:19:54 +0000320AddressClass
Greg Clayton762f7132011-09-18 18:59:15 +0000321ObjectFile::GetAddressClass (addr_t file_addr)
Greg Claytonded470d2011-03-19 01:12:21 +0000322{
Greg Clayton3046e662013-07-10 01:23:25 +0000323 Symtab *symtab = GetSymtab();
Greg Claytonded470d2011-03-19 01:12:21 +0000324 if (symtab)
325 {
326 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
327 if (symbol)
328 {
Greg Claytone7612132012-03-07 21:03:09 +0000329 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000330 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000331 const SectionSP section_sp (symbol->GetAddressRef().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000332 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000333 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000334 const SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000335 switch (section_type)
336 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000337 case eSectionTypeInvalid:
338 return eAddressClassUnknown;
339 case eSectionTypeCode:
340 return eAddressClassCode;
341 case eSectionTypeContainer:
342 return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000343 case eSectionTypeData:
344 case eSectionTypeDataCString:
345 case eSectionTypeDataCStringPointers:
346 case eSectionTypeDataSymbolAddress:
347 case eSectionTypeData4:
348 case eSectionTypeData8:
349 case eSectionTypeData16:
350 case eSectionTypeDataPointers:
351 case eSectionTypeZeroFill:
352 case eSectionTypeDataObjCMessageRefs:
353 case eSectionTypeDataObjCCFStrings:
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000354 case eSectionTypeGoSymtab:
Greg Clayton5009f9d2011-10-27 17:55:14 +0000355 return eAddressClassData;
356 case eSectionTypeDebug:
357 case eSectionTypeDWARFDebugAbbrev:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000358 case eSectionTypeDWARFDebugAddr:
Greg Clayton5009f9d2011-10-27 17:55:14 +0000359 case eSectionTypeDWARFDebugAranges:
360 case eSectionTypeDWARFDebugFrame:
361 case eSectionTypeDWARFDebugInfo:
362 case eSectionTypeDWARFDebugLine:
363 case eSectionTypeDWARFDebugLoc:
364 case eSectionTypeDWARFDebugMacInfo:
365 case eSectionTypeDWARFDebugPubNames:
366 case eSectionTypeDWARFDebugPubTypes:
367 case eSectionTypeDWARFDebugRanges:
368 case eSectionTypeDWARFDebugStr:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000369 case eSectionTypeDWARFDebugStrOffsets:
Greg Clayton5009f9d2011-10-27 17:55:14 +0000370 case eSectionTypeDWARFAppleNames:
371 case eSectionTypeDWARFAppleTypes:
372 case eSectionTypeDWARFAppleNamespaces:
373 case eSectionTypeDWARFAppleObjC:
374 return eAddressClassDebug;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000375 case eSectionTypeEHFrame:
Jason Molendae589e7e2014-12-08 03:09:00 +0000376 case eSectionTypeCompactUnwind:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000377 return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000378 case eSectionTypeELFSymbolTable:
379 case eSectionTypeELFDynamicSymbols:
380 case eSectionTypeELFRelocationEntries:
381 case eSectionTypeELFDynamicLinkInfo:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000382 case eSectionTypeOther:
383 return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +0000384 }
385 }
386 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000387
Greg Claytone0d378b2011-03-24 21:19:54 +0000388 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000389 switch (symbol_type)
390 {
391 case eSymbolTypeAny: return eAddressClassUnknown;
392 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +0000393 case eSymbolTypeCode: return eAddressClassCode;
394 case eSymbolTypeTrampoline: return eAddressClassCode;
Greg Clayton059f7242013-02-27 21:16:04 +0000395 case eSymbolTypeResolver: return eAddressClassCode;
Greg Claytonded470d2011-03-19 01:12:21 +0000396 case eSymbolTypeData: return eAddressClassData;
397 case eSymbolTypeRuntime: return eAddressClassRuntime;
398 case eSymbolTypeException: return eAddressClassRuntime;
399 case eSymbolTypeSourceFile: return eAddressClassDebug;
400 case eSymbolTypeHeaderFile: return eAddressClassDebug;
401 case eSymbolTypeObjectFile: return eAddressClassDebug;
402 case eSymbolTypeCommonBlock: return eAddressClassDebug;
403 case eSymbolTypeBlock: return eAddressClassDebug;
404 case eSymbolTypeLocal: return eAddressClassData;
405 case eSymbolTypeParam: return eAddressClassData;
406 case eSymbolTypeVariable: return eAddressClassData;
407 case eSymbolTypeVariableType: return eAddressClassDebug;
408 case eSymbolTypeLineEntry: return eAddressClassDebug;
409 case eSymbolTypeLineHeader: return eAddressClassDebug;
410 case eSymbolTypeScopeBegin: return eAddressClassDebug;
411 case eSymbolTypeScopeEnd: return eAddressClassDebug;
412 case eSymbolTypeAdditional: return eAddressClassUnknown;
413 case eSymbolTypeCompiler: return eAddressClassDebug;
414 case eSymbolTypeInstrumentation:return eAddressClassDebug;
415 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000416 case eSymbolTypeObjCClass: return eAddressClassRuntime;
417 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
418 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +0000419 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000420 }
421 }
422 }
423 return eAddressClassUnknown;
424}
425
Greg Claytonc9660542012-02-05 02:38:54 +0000426DataBufferSP
427ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size)
428{
429 DataBufferSP data_sp;
430 if (process_sp)
431 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000432 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0));
Greg Claytonc9660542012-02-05 02:38:54 +0000433 Error error;
434 const size_t bytes_read = process_sp->ReadMemory (addr,
435 data_ap->GetBytes(),
436 data_ap->GetByteSize(),
437 error);
438 if (bytes_read == byte_size)
439 data_sp.reset (data_ap.release());
440 }
441 return data_sp;
442}
443
Greg Clayton44435ed2012-01-12 05:25:17 +0000444size_t
Zachary Turnera746e8e2014-07-02 17:24:07 +0000445ObjectFile::GetData (lldb::offset_t offset, size_t length, DataExtractor &data) const
Greg Clayton44435ed2012-01-12 05:25:17 +0000446{
447 // The entire file has already been mmap'ed into m_data, so just copy from there
448 // as the back mmap buffer will be shared with shared pointers.
449 return data.SetData (m_data, offset, length);
450}
451
452size_t
Zachary Turnera746e8e2014-07-02 17:24:07 +0000453ObjectFile::CopyData (lldb::offset_t offset, size_t length, void *dst) const
Greg Clayton44435ed2012-01-12 05:25:17 +0000454{
455 // The entire file has already been mmap'ed into m_data, so just copy from there
Ed Masteb0e33d42013-10-09 20:34:25 +0000456 // Note that the data remains in target byte order.
457 return m_data.CopyData (offset, length, dst);
Greg Clayton44435ed2012-01-12 05:25:17 +0000458}
Greg Claytonded470d2011-03-19 01:12:21 +0000459
Greg Claytonc9660542012-02-05 02:38:54 +0000460
461size_t
Zachary Turnera746e8e2014-07-02 17:24:07 +0000462ObjectFile::ReadSectionData (const Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) const
Greg Claytonc9660542012-02-05 02:38:54 +0000463{
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000464 assert(section);
465 section_offset *= section->GetTargetByteSize();
466
Michael Sartaina7499c92013-07-01 19:45:50 +0000467 // If some other objectfile owns this data, pass this to them.
468 if (section->GetObjectFile() != this)
469 return section->GetObjectFile()->ReadSectionData (section, section_offset, dst, dst_len);
470
Greg Claytonc3776bf2012-02-09 06:16:32 +0000471 if (IsInMemory())
Greg Claytonc9660542012-02-05 02:38:54 +0000472 {
473 ProcessSP process_sp (m_process_wp.lock());
474 if (process_sp)
475 {
476 Error error;
Greg Clayton39f7ee82013-02-01 21:38:35 +0000477 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget());
478 if (base_load_addr != LLDB_INVALID_ADDRESS)
479 return process_sp->ReadMemory (base_load_addr + section_offset, dst, dst_len, error);
Greg Claytonc9660542012-02-05 02:38:54 +0000480 }
481 }
482 else
483 {
Zachary Turnera746e8e2014-07-02 17:24:07 +0000484 const lldb::offset_t section_file_size = section->GetFileSize();
485 if (section_offset < section_file_size)
Greg Claytonee212e22012-02-21 17:34:25 +0000486 {
Zachary Turnera746e8e2014-07-02 17:24:07 +0000487 const size_t section_bytes_left = section_file_size - section_offset;
488 size_t section_dst_len = dst_len;
Greg Claytonee212e22012-02-21 17:34:25 +0000489 if (section_dst_len > section_bytes_left)
490 section_dst_len = section_bytes_left;
491 return CopyData (section->GetFileOffset() + section_offset, section_dst_len, dst);
492 }
Sean Callananecda2b22013-01-04 23:20:01 +0000493 else
494 {
495 if (section->GetType() == eSectionTypeZeroFill)
496 {
497 const uint64_t section_size = section->GetByteSize();
498 const uint64_t section_bytes_left = section_size - section_offset;
499 uint64_t section_dst_len = dst_len;
500 if (section_dst_len > section_bytes_left)
501 section_dst_len = section_bytes_left;
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000502 memset(dst, 0, section_dst_len);
Sean Callananecda2b22013-01-04 23:20:01 +0000503 return section_dst_len;
504 }
505 }
Greg Claytonc9660542012-02-05 02:38:54 +0000506 }
507 return 0;
508}
509
510//----------------------------------------------------------------------
511// Get the section data the file on disk
512//----------------------------------------------------------------------
513size_t
514ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const
515{
Michael Sartaina7499c92013-07-01 19:45:50 +0000516 // If some other objectfile owns this data, pass this to them.
517 if (section->GetObjectFile() != this)
518 return section->GetObjectFile()->ReadSectionData (section, section_data);
519
Greg Claytonc3776bf2012-02-09 06:16:32 +0000520 if (IsInMemory())
Greg Claytonc9660542012-02-05 02:38:54 +0000521 {
522 ProcessSP process_sp (m_process_wp.lock());
523 if (process_sp)
524 {
Greg Clayton39f7ee82013-02-01 21:38:35 +0000525 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget());
526 if (base_load_addr != LLDB_INVALID_ADDRESS)
Greg Claytonc9660542012-02-05 02:38:54 +0000527 {
Greg Clayton39f7ee82013-02-01 21:38:35 +0000528 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize()));
529 if (data_sp)
530 {
531 section_data.SetData (data_sp, 0, data_sp->GetByteSize());
532 section_data.SetByteOrder (process_sp->GetByteOrder());
533 section_data.SetAddressByteSize (process_sp->GetAddressByteSize());
534 return section_data.GetByteSize();
535 }
Greg Claytonc9660542012-02-05 02:38:54 +0000536 }
537 }
538 }
539 else
540 {
541 // The object file now contains a full mmap'ed copy of the object file data, so just use this
542 return MemoryMapSectionData (section, section_data);
543 }
544 section_data.Clear();
545 return 0;
546}
547
548size_t
549ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const
550{
Michael Sartaina7499c92013-07-01 19:45:50 +0000551 // If some other objectfile owns this data, pass this to them.
552 if (section->GetObjectFile() != this)
553 return section->GetObjectFile()->MemoryMapSectionData (section, section_data);
554
Greg Claytonc3776bf2012-02-09 06:16:32 +0000555 if (IsInMemory())
Greg Claytonc9660542012-02-05 02:38:54 +0000556 {
557 return ReadSectionData (section, section_data);
558 }
559 else
560 {
561 // The object file now contains a full mmap'ed copy of the object file data, so just use this
Greg Clayton47037bc2012-03-27 02:40:46 +0000562 return GetData(section->GetFileOffset(), section->GetFileSize(), section_data);
Greg Claytonc9660542012-02-05 02:38:54 +0000563 }
Greg Claytonc9660542012-02-05 02:38:54 +0000564}
565
Greg Clayton1f746072012-08-29 21:13:06 +0000566
567bool
Greg Clayton906ba472013-02-06 00:38:25 +0000568ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist)
Greg Clayton1f746072012-08-29 21:13:06 +0000569{
570 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$");
Greg Claytonbc43cab2013-04-03 21:37:16 +0000571 RegularExpression::Match regex_match(2);
572 if (g_object_regex.Execute (path_with_object, &regex_match))
Greg Clayton1f746072012-08-29 21:13:06 +0000573 {
574 std::string path;
575 std::string obj;
Greg Claytonbc43cab2013-04-03 21:37:16 +0000576 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) &&
577 regex_match.GetMatchAtIndex (path_with_object, 2, obj))
Greg Clayton1f746072012-08-29 21:13:06 +0000578 {
579 archive_file.SetFile (path.c_str(), false);
580 archive_object.SetCString(obj.c_str());
Greg Clayton906ba472013-02-06 00:38:25 +0000581 if (must_exist && !archive_file.Exists())
582 return false;
Greg Clayton1f746072012-08-29 21:13:06 +0000583 return true;
584 }
585 }
586 return false;
587}
588
Greg Clayton9422dd62013-03-04 21:46:16 +0000589void
Greg Clayton3046e662013-07-10 01:23:25 +0000590ObjectFile::ClearSymtab ()
Greg Clayton9422dd62013-03-04 21:46:16 +0000591{
592 ModuleSP module_sp(GetModule());
593 if (module_sp)
594 {
595 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton5160ce52013-03-27 23:08:40 +0000596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton9422dd62013-03-04 21:46:16 +0000597 if (log)
Greg Clayton3046e662013-07-10 01:23:25 +0000598 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000599 static_cast<void*>(this),
600 static_cast<void*>(m_symtab_ap.get()));
Greg Clayton3046e662013-07-10 01:23:25 +0000601 m_symtab_ap.reset();
Greg Clayton9422dd62013-03-04 21:46:16 +0000602 }
603}
Greg Clayton3046e662013-07-10 01:23:25 +0000604
605SectionList *
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000606ObjectFile::GetSectionList(bool update_module_section_list)
Greg Clayton3046e662013-07-10 01:23:25 +0000607{
Ed Masted4612ad2014-04-20 13:17:36 +0000608 if (m_sections_ap.get() == nullptr)
Greg Clayton3046e662013-07-10 01:23:25 +0000609 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000610 if (update_module_section_list)
Greg Claytonc72f7132014-06-16 19:44:24 +0000611 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000612 ModuleSP module_sp(GetModule());
613 if (module_sp)
614 {
615 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
616 CreateSections(*module_sp->GetUnifiedSectionList());
617 }
618 }
619 else
620 {
621 SectionList unified_section_list;
622 CreateSections(unified_section_list);
Greg Claytonc72f7132014-06-16 19:44:24 +0000623 }
Greg Clayton3046e662013-07-10 01:23:25 +0000624 }
625 return m_sections_ap.get();
626}