blob: 11b540071208607ca44216a7a2bf5c1107f76c8d [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 Clayton762f7132011-09-18 18:59:15 +000011#include "lldb/lldb-private-log.h"
Greg Clayton44435ed2012-01-12 05:25:17 +000012#include "lldb/Core/DataBuffer.h"
Greg Claytonc9660542012-02-05 02:38:54 +000013#include "lldb/Core/DataBufferHeap.h"
Greg Clayton762f7132011-09-18 18:59:15 +000014#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000016#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/PluginManager.h"
18#include "lldb/Core/RegularExpression.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Timer.h"
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/ObjectContainer.h"
23#include "lldb/Symbol/SymbolFile.h"
Greg Claytonc9660542012-02-05 02:38:54 +000024#include "lldb/Target/Process.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
27using namespace lldb;
28using namespace lldb_private;
29
Greg Clayton762f7132011-09-18 18:59:15 +000030ObjectFileSP
Greg Clayton5ce9c562013-02-06 17:22:03 +000031ObjectFile::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 Lattner30fdc8d2010-06-08 16:52:24 +000037{
Greg Clayton762f7132011-09-18 18:59:15 +000038 ObjectFileSP object_file_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Greg Claytone72dfb32012-02-24 01:59:29 +000040 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041 {
Greg Claytone72dfb32012-02-24 01:59:29 +000042 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000043 "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(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000045 static_cast<const void*>(file),
46 static_cast<uint64_t>(file_offset),
47 static_cast<uint64_t>(file_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048 if (file)
49 {
Greg Clayton5ce9c562013-02-06 17:22:03 +000050 FileSpec archive_file;
51 ObjectContainerCreateInstance create_object_container_callback;
52
53 const bool file_exists = file->Exists();
54 if (!data_sp)
Greg Clayton44435ed2012-01-12 05:25:17 +000055 {
Greg Clayton5ce9c562013-02-06 17:22:03 +000056 // We have an object name which most likely means we have
57 // a .o file in a static archive (.a file). Try and see if
58 // we have a cached archive first without reading any data
59 // first
60 if (file_exists && module_sp->GetObjectName())
61 {
Ed Masted4612ad2014-04-20 13:17:36 +000062 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton5ce9c562013-02-06 17:22:03 +000063 {
Greg Clayton7b0992d2013-04-18 22:45:39 +000064 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 +000065
Greg Clayton5ce9c562013-02-06 17:22:03 +000066 if (object_container_ap.get())
67 object_file_sp = object_container_ap->GetObjectFile(file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000068
Greg Clayton5ce9c562013-02-06 17:22:03 +000069 if (object_file_sp.get())
70 return object_file_sp;
71 }
72 }
73 // Ok, we didn't find any containers that have a named object, now
74 // lets read the first 512 bytes from the file so the object file
75 // and object container plug-ins can use these bytes to see if they
76 // can parse this file.
77 if (file_size > 0)
78 {
79 data_sp = file->ReadFileContents(file_offset, std::min<size_t>(512, file_size));
80 data_offset = 0;
81 }
Greg Clayton44435ed2012-01-12 05:25:17 +000082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083
Greg Clayton5ce9c562013-02-06 17:22:03 +000084 if (!data_sp || data_sp->GetByteSize() == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 {
86 // Check for archive file with format "/path/to/archive.a(object.o)"
87 char path_with_object[PATH_MAX*2];
Greg Claytone72dfb32012-02-24 01:59:29 +000088 module_sp->GetFileSpec().GetPath(path_with_object, sizeof(path_with_object));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089
Greg Clayton1f746072012-08-29 21:13:06 +000090 ConstString archive_object;
Greg Clayton906ba472013-02-06 00:38:25 +000091 const bool must_exist = true;
92 if (ObjectFile::SplitArchivePathWithObject (path_with_object, archive_file, archive_object, must_exist))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 {
Greg Clayton1f746072012-08-29 21:13:06 +000094 file_size = archive_file.GetByteSize();
95 if (file_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096 {
Greg Clayton5ce9c562013-02-06 17:22:03 +000097 file = &archive_file;
Greg Clayton1f746072012-08-29 21:13:06 +000098 module_sp->SetFileSpecAndObjectName (archive_file, archive_object);
Greg Clayton5ce9c562013-02-06 17:22:03 +000099 // Check if this is a object container by iterating through all object
100 // container plugin instances and then trying to get an object file
101 // from the container plugins since we had a name. Also, don't read
102 // ANY data in case there is data cached in the container plug-ins
103 // (like BSD archives caching the contained objects within an file).
Ed Masted4612ad2014-04-20 13:17:36 +0000104 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton5ce9c562013-02-06 17:22:03 +0000105 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000106 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 +0000107
Greg Clayton5ce9c562013-02-06 17:22:03 +0000108 if (object_container_ap.get())
109 object_file_sp = object_container_ap->GetObjectFile(file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000110
Greg Clayton5ce9c562013-02-06 17:22:03 +0000111 if (object_file_sp.get())
112 return object_file_sp;
113 }
114 // We failed to find any cached object files in the container
115 // plug-ins, so lets read the first 512 bytes and try again below...
116 data_sp = archive_file.ReadFileContents(file_offset, 512);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117 }
118 }
119 }
120
Greg Clayton5ce9c562013-02-06 17:22:03 +0000121 if (data_sp && data_sp->GetByteSize() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 {
Greg Clayton44435ed2012-01-12 05:25:17 +0000123 // Check if this is a normal object file by iterating through
124 // all object file plugin instances.
125 ObjectFileCreateInstance create_object_file_callback;
Ed Masted4612ad2014-04-20 13:17:36 +0000126 for (uint32_t idx = 0; (create_object_file_callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton44435ed2012-01-12 05:25:17 +0000127 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000128 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 +0000129 if (object_file_sp.get())
130 return object_file_sp;
131 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132
Greg Clayton44435ed2012-01-12 05:25:17 +0000133 // Check if this is a object container by iterating through
134 // all object container plugin instances and then trying to get
135 // an object file from the container.
Ed Masted4612ad2014-04-20 13:17:36 +0000136 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Clayton44435ed2012-01-12 05:25:17 +0000137 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000138 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 +0000139
Greg Clayton44435ed2012-01-12 05:25:17 +0000140 if (object_container_ap.get())
141 object_file_sp = object_container_ap->GetObjectFile(file);
142
143 if (object_file_sp.get())
144 return object_file_sp;
145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 }
147 }
148 }
Greg Clayton762f7132011-09-18 18:59:15 +0000149 // We didn't find it, so clear our shared pointer in case it
150 // contains anything and return an empty shared pointer
151 object_file_sp.reset();
152 return object_file_sp;
153}
154
Greg Claytonc9660542012-02-05 02:38:54 +0000155ObjectFileSP
Greg Claytone72dfb32012-02-24 01:59:29 +0000156ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000157 const ProcessSP &process_sp,
158 lldb::addr_t header_addr,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000159 DataBufferSP &data_sp)
Greg Claytonc9660542012-02-05 02:38:54 +0000160{
Greg Claytonc9660542012-02-05 02:38:54 +0000161 ObjectFileSP object_file_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000162
Greg Claytone72dfb32012-02-24 01:59:29 +0000163 if (module_sp)
Greg Claytonc9660542012-02-05 02:38:54 +0000164 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000165 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000166 "ObjectFile::FindPlugin (module = %s, process = %p, header_addr = 0x%" PRIx64 ")",
167 module_sp->GetFileSpec().GetPath().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000168 static_cast<void*>(process_sp.get()), header_addr);
Greg Claytonc9660542012-02-05 02:38:54 +0000169 uint32_t idx;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000170
Greg Claytonc9660542012-02-05 02:38:54 +0000171 // Check if this is a normal object file by iterating through
172 // all object file plugin instances.
173 ObjectFileCreateMemoryInstance create_callback;
Ed Masted4612ad2014-04-20 13:17:36 +0000174 for (idx = 0; (create_callback = PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != nullptr; ++idx)
Greg Claytonc9660542012-02-05 02:38:54 +0000175 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000176 object_file_sp.reset (create_callback(module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +0000177 if (object_file_sp.get())
178 return object_file_sp;
179 }
Greg Claytonc9660542012-02-05 02:38:54 +0000180 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000181
Greg Claytonc9660542012-02-05 02:38:54 +0000182 // We didn't find it, so clear our shared pointer in case it
183 // contains anything and return an empty shared pointer
184 object_file_sp.reset();
185 return object_file_sp;
186}
187
Greg Claytonf4d6de62013-04-24 22:29:28 +0000188size_t
189ObjectFile::GetModuleSpecifications (const FileSpec &file,
190 lldb::offset_t file_offset,
Greg Clayton2540a8a2013-07-12 22:07:46 +0000191 lldb::offset_t file_size,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000192 ModuleSpecList &specs)
193{
194 DataBufferSP data_sp (file.ReadFileContents(file_offset, 512));
195 if (data_sp)
Greg Clayton2540a8a2013-07-12 22:07:46 +0000196 {
197 if (file_size == 0)
198 {
199 const lldb::offset_t actual_file_size = file.GetByteSize();
200 if (actual_file_size > file_offset)
201 file_size = actual_file_size - file_offset;
202 }
203 return ObjectFile::GetModuleSpecifications (file, // file spec
204 data_sp, // data bytes
205 0, // data offset
206 file_offset,// file offset
207 file_size, // file length
Greg Claytonf4d6de62013-04-24 22:29:28 +0000208 specs);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000209 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000210 return 0;
211}
212
213size_t
214ObjectFile::GetModuleSpecifications (const lldb_private::FileSpec& file,
215 lldb::DataBufferSP& data_sp,
216 lldb::offset_t data_offset,
217 lldb::offset_t file_offset,
Greg Clayton2540a8a2013-07-12 22:07:46 +0000218 lldb::offset_t file_size,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000219 lldb_private::ModuleSpecList &specs)
220{
221 const size_t initial_count = specs.GetSize();
222 ObjectFileGetModuleSpecifications callback;
223 uint32_t i;
224 // Try the ObjectFile plug-ins
Ed Masted4612ad2014-04-20 13:17:36 +0000225 for (i = 0; (callback = PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000226 {
Greg Clayton2540a8a2013-07-12 22:07:46 +0000227 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000228 return specs.GetSize() - initial_count;
229 }
230
231 // Try the ObjectContainer plug-ins
Ed Masted4612ad2014-04-20 13:17:36 +0000232 for (i = 0; (callback = PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000233 {
Greg Clayton2540a8a2013-07-12 22:07:46 +0000234 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
Greg Claytonf4d6de62013-04-24 22:29:28 +0000235 return specs.GetSize() - initial_count;
236 }
237 return 0;
238}
239
240ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp,
241 const FileSpec *file_spec_ptr,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000242 lldb::offset_t file_offset,
243 lldb::offset_t length,
Greg Clayton23f8c952014-03-24 23:10:19 +0000244 const lldb::DataBufferSP& data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000245 lldb::offset_t data_offset
246) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000247 ModuleChild (module_sp),
Greg Clayton762f7132011-09-18 18:59:15 +0000248 m_file (), // This file could be different from the original module's file
249 m_type (eTypeInvalid),
250 m_strata (eStrataInvalid),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000251 m_file_offset (file_offset),
252 m_length (length),
Greg Clayton44435ed2012-01-12 05:25:17 +0000253 m_data (),
Greg Claytonc9660542012-02-05 02:38:54 +0000254 m_unwind_table (*this),
255 m_process_wp(),
Greg Clayton9422dd62013-03-04 21:46:16 +0000256 m_memory_addr (LLDB_INVALID_ADDRESS),
Greg Clayton3046e662013-07-10 01:23:25 +0000257 m_sections_ap(),
258 m_symtab_ap ()
Greg Clayton9422dd62013-03-04 21:46:16 +0000259{
Greg Clayton762f7132011-09-18 18:59:15 +0000260 if (file_spec_ptr)
261 m_file = *file_spec_ptr;
Greg Clayton5ce9c562013-02-06 17:22:03 +0000262 if (data_sp)
263 m_data.SetData (data_sp, data_offset, length);
Greg Clayton5160ce52013-03-27 23:08:40 +0000264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton762f7132011-09-18 18:59:15 +0000265 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000266 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
267 static_cast<void*>(this),
268 static_cast<void*>(module_sp.get()),
269 module_sp->GetSpecificationDescription().c_str(),
270 m_file ? m_file.GetPath().c_str() : "<NULL>",
271 m_file_offset, m_length);
Greg Clayton762f7132011-09-18 18:59:15 +0000272}
273
Greg Claytonc9660542012-02-05 02:38:54 +0000274
Greg Claytone72dfb32012-02-24 01:59:29 +0000275ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000276 const ProcessSP &process_sp,
277 lldb::addr_t header_addr,
278 DataBufferSP& header_data_sp) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000279 ModuleChild (module_sp),
Greg Claytonc9660542012-02-05 02:38:54 +0000280 m_file (),
281 m_type (eTypeInvalid),
282 m_strata (eStrataInvalid),
Greg Clayton5ce9c562013-02-06 17:22:03 +0000283 m_file_offset (0),
Greg Claytonc9660542012-02-05 02:38:54 +0000284 m_length (0),
285 m_data (),
286 m_unwind_table (*this),
287 m_process_wp (process_sp),
Greg Clayton9422dd62013-03-04 21:46:16 +0000288 m_memory_addr (header_addr),
Greg Clayton3046e662013-07-10 01:23:25 +0000289 m_sections_ap(),
290 m_symtab_ap ()
Greg Clayton9422dd62013-03-04 21:46:16 +0000291{
Greg Claytonc9660542012-02-05 02:38:54 +0000292 if (header_data_sp)
293 m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize());
Greg Clayton5160ce52013-03-27 23:08:40 +0000294 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Claytonc9660542012-02-05 02:38:54 +0000295 if (log)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000296 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, header_addr = 0x%" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000297 static_cast<void*>(this),
298 static_cast<void*>(module_sp.get()),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000299 module_sp->GetSpecificationDescription().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000300 static_cast<void*>(process_sp.get()), m_memory_addr);
Greg Claytonc9660542012-02-05 02:38:54 +0000301}
302
303
Greg Clayton762f7132011-09-18 18:59:15 +0000304ObjectFile::~ObjectFile()
305{
Greg Clayton5160ce52013-03-27 23:08:40 +0000306 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton762f7132011-09-18 18:59:15 +0000307 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000308 log->Printf ("%p ObjectFile::~ObjectFile ()\n",
309 static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310}
Jim Ingham5aee1622010-08-09 23:31:02 +0000311
312bool
313ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch)
314{
Greg Claytone72dfb32012-02-24 01:59:29 +0000315 ModuleSP module_sp (GetModule());
316 if (module_sp)
317 return module_sp->SetArchitecture (new_arch);
318 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +0000319}
320
Greg Claytone0d378b2011-03-24 21:19:54 +0000321AddressClass
Greg Clayton762f7132011-09-18 18:59:15 +0000322ObjectFile::GetAddressClass (addr_t file_addr)
Greg Claytonded470d2011-03-19 01:12:21 +0000323{
Greg Clayton3046e662013-07-10 01:23:25 +0000324 Symtab *symtab = GetSymtab();
Greg Claytonded470d2011-03-19 01:12:21 +0000325 if (symtab)
326 {
327 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
328 if (symbol)
329 {
Greg Claytone7612132012-03-07 21:03:09 +0000330 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000331 {
Greg Claytone7612132012-03-07 21:03:09 +0000332 const SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000333 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000334 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000335 const SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000336 switch (section_type)
337 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000338 case eSectionTypeInvalid:
339 return eAddressClassUnknown;
340 case eSectionTypeCode:
341 return eAddressClassCode;
342 case eSectionTypeContainer:
343 return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000344 case eSectionTypeData:
345 case eSectionTypeDataCString:
346 case eSectionTypeDataCStringPointers:
347 case eSectionTypeDataSymbolAddress:
348 case eSectionTypeData4:
349 case eSectionTypeData8:
350 case eSectionTypeData16:
351 case eSectionTypeDataPointers:
352 case eSectionTypeZeroFill:
353 case eSectionTypeDataObjCMessageRefs:
354 case eSectionTypeDataObjCCFStrings:
355 return eAddressClassData;
356 case eSectionTypeDebug:
357 case eSectionTypeDWARFDebugAbbrev:
358 case eSectionTypeDWARFDebugAranges:
359 case eSectionTypeDWARFDebugFrame:
360 case eSectionTypeDWARFDebugInfo:
361 case eSectionTypeDWARFDebugLine:
362 case eSectionTypeDWARFDebugLoc:
363 case eSectionTypeDWARFDebugMacInfo:
364 case eSectionTypeDWARFDebugPubNames:
365 case eSectionTypeDWARFDebugPubTypes:
366 case eSectionTypeDWARFDebugRanges:
367 case eSectionTypeDWARFDebugStr:
368 case eSectionTypeDWARFAppleNames:
369 case eSectionTypeDWARFAppleTypes:
370 case eSectionTypeDWARFAppleNamespaces:
371 case eSectionTypeDWARFAppleObjC:
372 return eAddressClassDebug;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000373 case eSectionTypeEHFrame:
374 return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000375 case eSectionTypeELFSymbolTable:
376 case eSectionTypeELFDynamicSymbols:
377 case eSectionTypeELFRelocationEntries:
378 case eSectionTypeELFDynamicLinkInfo:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000379 case eSectionTypeOther:
380 return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +0000381 }
382 }
383 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000384
Greg Claytone0d378b2011-03-24 21:19:54 +0000385 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000386 switch (symbol_type)
387 {
388 case eSymbolTypeAny: return eAddressClassUnknown;
389 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +0000390 case eSymbolTypeCode: return eAddressClassCode;
391 case eSymbolTypeTrampoline: return eAddressClassCode;
Greg Clayton059f7242013-02-27 21:16:04 +0000392 case eSymbolTypeResolver: return eAddressClassCode;
Greg Claytonded470d2011-03-19 01:12:21 +0000393 case eSymbolTypeData: return eAddressClassData;
394 case eSymbolTypeRuntime: return eAddressClassRuntime;
395 case eSymbolTypeException: return eAddressClassRuntime;
396 case eSymbolTypeSourceFile: return eAddressClassDebug;
397 case eSymbolTypeHeaderFile: return eAddressClassDebug;
398 case eSymbolTypeObjectFile: return eAddressClassDebug;
399 case eSymbolTypeCommonBlock: return eAddressClassDebug;
400 case eSymbolTypeBlock: return eAddressClassDebug;
401 case eSymbolTypeLocal: return eAddressClassData;
402 case eSymbolTypeParam: return eAddressClassData;
403 case eSymbolTypeVariable: return eAddressClassData;
404 case eSymbolTypeVariableType: return eAddressClassDebug;
405 case eSymbolTypeLineEntry: return eAddressClassDebug;
406 case eSymbolTypeLineHeader: return eAddressClassDebug;
407 case eSymbolTypeScopeBegin: return eAddressClassDebug;
408 case eSymbolTypeScopeEnd: return eAddressClassDebug;
409 case eSymbolTypeAdditional: return eAddressClassUnknown;
410 case eSymbolTypeCompiler: return eAddressClassDebug;
411 case eSymbolTypeInstrumentation:return eAddressClassDebug;
412 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000413 case eSymbolTypeObjCClass: return eAddressClassRuntime;
414 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
415 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +0000416 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000417 }
418 }
419 }
420 return eAddressClassUnknown;
421}
422
Greg Claytonc9660542012-02-05 02:38:54 +0000423DataBufferSP
424ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size)
425{
426 DataBufferSP data_sp;
427 if (process_sp)
428 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000429 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0));
Greg Claytonc9660542012-02-05 02:38:54 +0000430 Error error;
431 const size_t bytes_read = process_sp->ReadMemory (addr,
432 data_ap->GetBytes(),
433 data_ap->GetByteSize(),
434 error);
435 if (bytes_read == byte_size)
436 data_sp.reset (data_ap.release());
437 }
438 return data_sp;
439}
440
Greg Clayton44435ed2012-01-12 05:25:17 +0000441size_t
Zachary Turnera746e8e2014-07-02 17:24:07 +0000442ObjectFile::GetData (lldb::offset_t offset, size_t length, DataExtractor &data) const
Greg Clayton44435ed2012-01-12 05:25:17 +0000443{
444 // The entire file has already been mmap'ed into m_data, so just copy from there
445 // as the back mmap buffer will be shared with shared pointers.
446 return data.SetData (m_data, offset, length);
447}
448
449size_t
Zachary Turnera746e8e2014-07-02 17:24:07 +0000450ObjectFile::CopyData (lldb::offset_t offset, size_t length, void *dst) const
Greg Clayton44435ed2012-01-12 05:25:17 +0000451{
452 // The entire file has already been mmap'ed into m_data, so just copy from there
Ed Masteb0e33d42013-10-09 20:34:25 +0000453 // Note that the data remains in target byte order.
454 return m_data.CopyData (offset, length, dst);
Greg Clayton44435ed2012-01-12 05:25:17 +0000455}
Greg Claytonded470d2011-03-19 01:12:21 +0000456
Greg Claytonc9660542012-02-05 02:38:54 +0000457
458size_t
Zachary Turnera746e8e2014-07-02 17:24:07 +0000459ObjectFile::ReadSectionData (const Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) const
Greg Claytonc9660542012-02-05 02:38:54 +0000460{
Michael Sartaina7499c92013-07-01 19:45:50 +0000461 // If some other objectfile owns this data, pass this to them.
462 if (section->GetObjectFile() != this)
463 return section->GetObjectFile()->ReadSectionData (section, section_offset, dst, dst_len);
464
Greg Claytonc3776bf2012-02-09 06:16:32 +0000465 if (IsInMemory())
Greg Claytonc9660542012-02-05 02:38:54 +0000466 {
467 ProcessSP process_sp (m_process_wp.lock());
468 if (process_sp)
469 {
470 Error error;
Greg Clayton39f7ee82013-02-01 21:38:35 +0000471 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget());
472 if (base_load_addr != LLDB_INVALID_ADDRESS)
473 return process_sp->ReadMemory (base_load_addr + section_offset, dst, dst_len, error);
Greg Claytonc9660542012-02-05 02:38:54 +0000474 }
475 }
476 else
477 {
Zachary Turnera746e8e2014-07-02 17:24:07 +0000478 const lldb::offset_t section_file_size = section->GetFileSize();
479 if (section_offset < section_file_size)
Greg Claytonee212e22012-02-21 17:34:25 +0000480 {
Zachary Turnera746e8e2014-07-02 17:24:07 +0000481 const size_t section_bytes_left = section_file_size - section_offset;
482 size_t section_dst_len = dst_len;
Greg Claytonee212e22012-02-21 17:34:25 +0000483 if (section_dst_len > section_bytes_left)
484 section_dst_len = section_bytes_left;
485 return CopyData (section->GetFileOffset() + section_offset, section_dst_len, dst);
486 }
Sean Callananecda2b22013-01-04 23:20:01 +0000487 else
488 {
489 if (section->GetType() == eSectionTypeZeroFill)
490 {
491 const uint64_t section_size = section->GetByteSize();
492 const uint64_t section_bytes_left = section_size - section_offset;
493 uint64_t section_dst_len = dst_len;
494 if (section_dst_len > section_bytes_left)
495 section_dst_len = section_bytes_left;
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000496 memset(dst, 0, section_dst_len);
Sean Callananecda2b22013-01-04 23:20:01 +0000497 return section_dst_len;
498 }
499 }
Greg Claytonc9660542012-02-05 02:38:54 +0000500 }
501 return 0;
502}
503
504//----------------------------------------------------------------------
505// Get the section data the file on disk
506//----------------------------------------------------------------------
507size_t
508ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const
509{
Michael Sartaina7499c92013-07-01 19:45:50 +0000510 // If some other objectfile owns this data, pass this to them.
511 if (section->GetObjectFile() != this)
512 return section->GetObjectFile()->ReadSectionData (section, section_data);
513
Greg Claytonc3776bf2012-02-09 06:16:32 +0000514 if (IsInMemory())
Greg Claytonc9660542012-02-05 02:38:54 +0000515 {
516 ProcessSP process_sp (m_process_wp.lock());
517 if (process_sp)
518 {
Greg Clayton39f7ee82013-02-01 21:38:35 +0000519 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget());
520 if (base_load_addr != LLDB_INVALID_ADDRESS)
Greg Claytonc9660542012-02-05 02:38:54 +0000521 {
Greg Clayton39f7ee82013-02-01 21:38:35 +0000522 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize()));
523 if (data_sp)
524 {
525 section_data.SetData (data_sp, 0, data_sp->GetByteSize());
526 section_data.SetByteOrder (process_sp->GetByteOrder());
527 section_data.SetAddressByteSize (process_sp->GetAddressByteSize());
528 return section_data.GetByteSize();
529 }
Greg Claytonc9660542012-02-05 02:38:54 +0000530 }
531 }
532 }
533 else
534 {
535 // The object file now contains a full mmap'ed copy of the object file data, so just use this
536 return MemoryMapSectionData (section, section_data);
537 }
538 section_data.Clear();
539 return 0;
540}
541
542size_t
543ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const
544{
Michael Sartaina7499c92013-07-01 19:45:50 +0000545 // If some other objectfile owns this data, pass this to them.
546 if (section->GetObjectFile() != this)
547 return section->GetObjectFile()->MemoryMapSectionData (section, section_data);
548
Greg Claytonc3776bf2012-02-09 06:16:32 +0000549 if (IsInMemory())
Greg Claytonc9660542012-02-05 02:38:54 +0000550 {
551 return ReadSectionData (section, section_data);
552 }
553 else
554 {
555 // 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 +0000556 return GetData(section->GetFileOffset(), section->GetFileSize(), section_data);
Greg Claytonc9660542012-02-05 02:38:54 +0000557 }
558 section_data.Clear();
559 return 0;
560}
561
Greg Clayton1f746072012-08-29 21:13:06 +0000562
563bool
Greg Clayton906ba472013-02-06 00:38:25 +0000564ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist)
Greg Clayton1f746072012-08-29 21:13:06 +0000565{
566 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$");
Greg Claytonbc43cab2013-04-03 21:37:16 +0000567 RegularExpression::Match regex_match(2);
568 if (g_object_regex.Execute (path_with_object, &regex_match))
Greg Clayton1f746072012-08-29 21:13:06 +0000569 {
570 std::string path;
571 std::string obj;
Greg Claytonbc43cab2013-04-03 21:37:16 +0000572 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) &&
573 regex_match.GetMatchAtIndex (path_with_object, 2, obj))
Greg Clayton1f746072012-08-29 21:13:06 +0000574 {
575 archive_file.SetFile (path.c_str(), false);
576 archive_object.SetCString(obj.c_str());
Greg Clayton906ba472013-02-06 00:38:25 +0000577 if (must_exist && !archive_file.Exists())
578 return false;
Greg Clayton1f746072012-08-29 21:13:06 +0000579 return true;
580 }
581 }
582 return false;
583}
584
Greg Clayton9422dd62013-03-04 21:46:16 +0000585void
Greg Clayton3046e662013-07-10 01:23:25 +0000586ObjectFile::ClearSymtab ()
Greg Clayton9422dd62013-03-04 21:46:16 +0000587{
588 ModuleSP module_sp(GetModule());
589 if (module_sp)
590 {
591 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton5160ce52013-03-27 23:08:40 +0000592 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton9422dd62013-03-04 21:46:16 +0000593 if (log)
Greg Clayton3046e662013-07-10 01:23:25 +0000594 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000595 static_cast<void*>(this),
596 static_cast<void*>(m_symtab_ap.get()));
Greg Clayton3046e662013-07-10 01:23:25 +0000597 m_symtab_ap.reset();
Greg Clayton9422dd62013-03-04 21:46:16 +0000598 }
599}
Greg Clayton3046e662013-07-10 01:23:25 +0000600
601SectionList *
602ObjectFile::GetSectionList()
603{
Ed Masted4612ad2014-04-20 13:17:36 +0000604 if (m_sections_ap.get() == nullptr)
Greg Clayton3046e662013-07-10 01:23:25 +0000605 {
606 ModuleSP module_sp(GetModule());
607 if (module_sp)
Greg Claytonc72f7132014-06-16 19:44:24 +0000608 {
609 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +0000610 CreateSections(*module_sp->GetUnifiedSectionList());
Greg Claytonc72f7132014-06-16 19:44:24 +0000611 }
Greg Clayton3046e662013-07-10 01:23:25 +0000612 }
613 return m_sections_ap.get();
614}