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"
11 #include "lldb/lldb-private-log.h"
12 #include "lldb/Core/DataBuffer.h"
13 #include "lldb/Core/DataBufferHeap.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/RegularExpression.h"
18 #include "lldb/Core/Timer.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/ObjectContainer.h"
21 #include "lldb/Symbol/SymbolFile.h"
22 #include "lldb/Target/Process.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 ObjectFileSP
28 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp, const FileSpec* file, addr_t file_offset, addr_t file_size, DataBufferSP &file_data_sp)
29 {
30     ObjectFileSP object_file_sp;
31 
32     if (module_sp)
33     {
34         Timer scoped_timer (__PRETTY_FUNCTION__,
35                             "ObjectFile::FindPlugin (module = %s/%s, file = %p, file_offset = 0x%z8.8x, file_size = 0x%z8.8x)",
36                             module_sp->GetFileSpec().GetDirectory().AsCString(),
37                             module_sp->GetFileSpec().GetFilename().AsCString(),
38                             file, file_offset, file_size);
39         if (file)
40         {
41             // Memory map the entire file contents
42             if (!file_data_sp)
43             {
44                 assert (file_offset == 0);
45                 file_data_sp = file->MemoryMapFileContents(file_offset, file_size);
46             }
47 
48             if (!file_data_sp || file_data_sp->GetByteSize() == 0)
49             {
50                 // Check for archive file with format "/path/to/archive.a(object.o)"
51                 char path_with_object[PATH_MAX*2];
52                 module_sp->GetFileSpec().GetPath(path_with_object, sizeof(path_with_object));
53 
54                 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$");
55                 if (g_object_regex.Execute (path_with_object, 2))
56                 {
57                     FileSpec archive_file;
58                     std::string path;
59                     std::string object;
60                     if (g_object_regex.GetMatchAtIndex (path_with_object, 1, path) &&
61                         g_object_regex.GetMatchAtIndex (path_with_object, 2, object))
62                     {
63                         archive_file.SetFile (path.c_str(), false);
64                         file_size = archive_file.GetByteSize();
65                         if (file_size > 0)
66                         {
67                             module_sp->SetFileSpecAndObjectName (archive_file, ConstString(object.c_str()));
68                             file_data_sp = archive_file.MemoryMapFileContents(file_offset, file_size);
69                         }
70                     }
71                 }
72             }
73 
74             if (file_data_sp && file_data_sp->GetByteSize() > 0)
75             {
76                 uint32_t idx;
77 
78                 // Check if this is a normal object file by iterating through
79                 // all object file plugin instances.
80                 ObjectFileCreateInstance create_object_file_callback;
81                 for (idx = 0; (create_object_file_callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != NULL; ++idx)
82                 {
83                     object_file_sp.reset (create_object_file_callback(module_sp, file_data_sp, file, file_offset, file_size));
84                     if (object_file_sp.get())
85                         return object_file_sp;
86                 }
87 
88                 // Check if this is a object container by iterating through
89                 // all object container plugin instances and then trying to get
90                 // an object file from the container.
91                 ObjectContainerCreateInstance create_object_container_callback;
92                 for (idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != NULL; ++idx)
93                 {
94                     std::auto_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, file_data_sp, file, file_offset, file_size));
95 
96                     if (object_container_ap.get())
97                         object_file_sp = object_container_ap->GetObjectFile(file);
98 
99                     if (object_file_sp.get())
100                         return object_file_sp;
101                 }
102             }
103         }
104     }
105     // We didn't find it, so clear our shared pointer in case it
106     // contains anything and return an empty shared pointer
107     object_file_sp.reset();
108     return object_file_sp;
109 }
110 
111 ObjectFileSP
112 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp,
113                         const ProcessSP &process_sp,
114                         lldb::addr_t header_addr,
115                         DataBufferSP &file_data_sp)
116 {
117     ObjectFileSP object_file_sp;
118 
119     if (module_sp)
120     {
121         Timer scoped_timer (__PRETTY_FUNCTION__,
122                             "ObjectFile::FindPlugin (module = %s/%s, process = %p, header_addr = 0x%llx)",
123                             module_sp->GetFileSpec().GetDirectory().AsCString(),
124                             module_sp->GetFileSpec().GetFilename().AsCString(),
125                             process_sp.get(), header_addr);
126         uint32_t idx;
127 
128         // Check if this is a normal object file by iterating through
129         // all object file plugin instances.
130         ObjectFileCreateMemoryInstance create_callback;
131         for (idx = 0; (create_callback = PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != NULL; ++idx)
132         {
133             object_file_sp.reset (create_callback(module_sp, file_data_sp, process_sp, header_addr));
134             if (object_file_sp.get())
135                 return object_file_sp;
136         }
137 
138     }
139     // We didn't find it, so clear our shared pointer in case it
140     // contains anything and return an empty shared pointer
141     object_file_sp.reset();
142     return object_file_sp;
143 }
144 
145 ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp,
146                         const FileSpec *file_spec_ptr,
147                         addr_t file_offset,
148                         addr_t file_size,
149                         DataBufferSP& file_data_sp) :
150     ModuleChild (module_sp),
151     m_file (),  // This file could be different from the original module's file
152     m_type (eTypeInvalid),
153     m_strata (eStrataInvalid),
154     m_offset (file_offset),
155     m_length (file_size),
156     m_data (),
157     m_unwind_table (*this),
158     m_process_wp(),
159     m_memory_addr (LLDB_INVALID_ADDRESS)
160 {
161     if (file_spec_ptr)
162         m_file = *file_spec_ptr;
163     if (file_data_sp)
164         m_data.SetData (file_data_sp, file_offset, file_size);
165     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
166     if (log)
167     {
168         if (m_file)
169         {
170             log->Printf ("%p ObjectFile::ObjectFile () module = %s/%s, file = %s/%s, offset = 0x%8.8llx, size = %llu\n",
171                          this,
172                          module_sp->GetFileSpec().GetDirectory().AsCString(),
173                          module_sp->GetFileSpec().GetFilename().AsCString(),
174                          m_file.GetDirectory().AsCString(),
175                          m_file.GetFilename().AsCString(),
176                          m_offset,
177                          m_length);
178         }
179         else
180         {
181             log->Printf ("%p ObjectFile::ObjectFile () module = %s/%s, file = <NULL>, offset = 0x%8.8llx, size = %llu\n",
182                          this,
183                          module_sp->GetFileSpec().GetDirectory().AsCString(),
184                          module_sp->GetFileSpec().GetFilename().AsCString(),
185                          m_offset,
186                          m_length);
187         }
188     }
189 }
190 
191 
192 ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp,
193                         const ProcessSP &process_sp,
194                         lldb::addr_t header_addr,
195                         DataBufferSP& header_data_sp) :
196     ModuleChild (module_sp),
197     m_file (),
198     m_type (eTypeInvalid),
199     m_strata (eStrataInvalid),
200     m_offset (header_addr),
201     m_length (0),
202     m_data (),
203     m_unwind_table (*this),
204     m_process_wp (process_sp),
205     m_memory_addr (header_addr)
206 {
207     if (header_data_sp)
208         m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize());
209     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
210     if (log)
211     {
212         log->Printf ("%p ObjectFile::ObjectFile () module = %s/%s, process = %p, header_addr = 0x%llx\n",
213                      this,
214                      module_sp->GetFileSpec().GetDirectory().AsCString(),
215                      module_sp->GetFileSpec().GetFilename().AsCString(),
216                      process_sp.get(),
217                      m_offset);
218     }
219 }
220 
221 
222 ObjectFile::~ObjectFile()
223 {
224     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
225     if (log)
226     {
227         ModuleSP module_sp (GetModule());
228         if (m_file)
229         {
230             log->Printf ("%p ObjectFile::~ObjectFile () module = %s/%s, file = %s/%s, offset = 0x%8.8llx, size = %llu\n",
231                          this,
232                          module_sp->GetFileSpec().GetDirectory().AsCString(),
233                          module_sp->GetFileSpec().GetFilename().AsCString(),
234                          m_file.GetDirectory().AsCString(),
235                          m_file.GetFilename().AsCString(),
236                          m_offset,
237                          m_length);
238         }
239         else
240         {
241             log->Printf ("%p ObjectFile::~ObjectFile () module = %s/%s, file = <NULL>, offset = 0x%8.8llx, size = %llu\n",
242                          this,
243                          module_sp->GetFileSpec().GetDirectory().AsCString(),
244                          module_sp->GetFileSpec().GetFilename().AsCString(),
245                          m_offset,
246                          m_length);
247         }
248     }
249 }
250 
251 bool
252 ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch)
253 {
254     ModuleSP module_sp (GetModule());
255     if (module_sp)
256         return module_sp->SetArchitecture (new_arch);
257     return false;
258 }
259 
260 AddressClass
261 ObjectFile::GetAddressClass (addr_t file_addr)
262 {
263     Symtab *symtab = GetSymtab();
264     if (symtab)
265     {
266         Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
267         if (symbol)
268         {
269             const AddressRange *range_ptr = symbol->GetAddressRangePtr();
270             if (range_ptr)
271             {
272                 const SectionSP section_sp (range_ptr->GetBaseAddress().GetSection());
273                 if (section_sp)
274                 {
275                     const SectionType section_type = section_sp->GetType();
276                     switch (section_type)
277                     {
278                     case eSectionTypeInvalid:               return eAddressClassUnknown;
279                     case eSectionTypeCode:                  return eAddressClassCode;
280                     case eSectionTypeContainer:             return eAddressClassUnknown;
281                     case eSectionTypeData:
282                     case eSectionTypeDataCString:
283                     case eSectionTypeDataCStringPointers:
284                     case eSectionTypeDataSymbolAddress:
285                     case eSectionTypeData4:
286                     case eSectionTypeData8:
287                     case eSectionTypeData16:
288                     case eSectionTypeDataPointers:
289                     case eSectionTypeZeroFill:
290                     case eSectionTypeDataObjCMessageRefs:
291                     case eSectionTypeDataObjCCFStrings:
292                         return eAddressClassData;
293                     case eSectionTypeDebug:
294                     case eSectionTypeDWARFDebugAbbrev:
295                     case eSectionTypeDWARFDebugAranges:
296                     case eSectionTypeDWARFDebugFrame:
297                     case eSectionTypeDWARFDebugInfo:
298                     case eSectionTypeDWARFDebugLine:
299                     case eSectionTypeDWARFDebugLoc:
300                     case eSectionTypeDWARFDebugMacInfo:
301                     case eSectionTypeDWARFDebugPubNames:
302                     case eSectionTypeDWARFDebugPubTypes:
303                     case eSectionTypeDWARFDebugRanges:
304                     case eSectionTypeDWARFDebugStr:
305                     case eSectionTypeDWARFAppleNames:
306                     case eSectionTypeDWARFAppleTypes:
307                     case eSectionTypeDWARFAppleNamespaces:
308                     case eSectionTypeDWARFAppleObjC:
309                         return eAddressClassDebug;
310                     case eSectionTypeEHFrame:               return eAddressClassRuntime;
311                     case eSectionTypeOther:                 return eAddressClassUnknown;
312                     }
313                 }
314             }
315 
316             const SymbolType symbol_type = symbol->GetType();
317             switch (symbol_type)
318             {
319             case eSymbolTypeAny:            return eAddressClassUnknown;
320             case eSymbolTypeAbsolute:       return eAddressClassUnknown;
321             case eSymbolTypeCode:           return eAddressClassCode;
322             case eSymbolTypeTrampoline:     return eAddressClassCode;
323             case eSymbolTypeData:           return eAddressClassData;
324             case eSymbolTypeRuntime:        return eAddressClassRuntime;
325             case eSymbolTypeException:      return eAddressClassRuntime;
326             case eSymbolTypeSourceFile:     return eAddressClassDebug;
327             case eSymbolTypeHeaderFile:     return eAddressClassDebug;
328             case eSymbolTypeObjectFile:     return eAddressClassDebug;
329             case eSymbolTypeCommonBlock:    return eAddressClassDebug;
330             case eSymbolTypeBlock:          return eAddressClassDebug;
331             case eSymbolTypeLocal:          return eAddressClassData;
332             case eSymbolTypeParam:          return eAddressClassData;
333             case eSymbolTypeVariable:       return eAddressClassData;
334             case eSymbolTypeVariableType:   return eAddressClassDebug;
335             case eSymbolTypeLineEntry:      return eAddressClassDebug;
336             case eSymbolTypeLineHeader:     return eAddressClassDebug;
337             case eSymbolTypeScopeBegin:     return eAddressClassDebug;
338             case eSymbolTypeScopeEnd:       return eAddressClassDebug;
339             case eSymbolTypeAdditional:     return eAddressClassUnknown;
340             case eSymbolTypeCompiler:       return eAddressClassDebug;
341             case eSymbolTypeInstrumentation:return eAddressClassDebug;
342             case eSymbolTypeUndefined:      return eAddressClassUnknown;
343             case eSymbolTypeObjCClass:      return eAddressClassRuntime;
344             case eSymbolTypeObjCMetaClass:  return eAddressClassRuntime;
345             case eSymbolTypeObjCIVar:       return eAddressClassRuntime;
346             }
347         }
348     }
349     return eAddressClassUnknown;
350 }
351 
352 DataBufferSP
353 ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size)
354 {
355     DataBufferSP data_sp;
356     if (process_sp)
357     {
358         std::auto_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0));
359         Error error;
360         const size_t bytes_read = process_sp->ReadMemory (addr,
361                                                           data_ap->GetBytes(),
362                                                           data_ap->GetByteSize(),
363                                                           error);
364         if (bytes_read == byte_size)
365             data_sp.reset (data_ap.release());
366     }
367     return data_sp;
368 }
369 
370 size_t
371 ObjectFile::GetData (off_t offset, size_t length, DataExtractor &data) const
372 {
373     // The entire file has already been mmap'ed into m_data, so just copy from there
374     // as the back mmap buffer will be shared with shared pointers.
375     return data.SetData (m_data, offset, length);
376 }
377 
378 size_t
379 ObjectFile::CopyData (off_t offset, size_t length, void *dst) const
380 {
381     // The entire file has already been mmap'ed into m_data, so just copy from there
382     return m_data.CopyByteOrderedData (offset, length, dst, length, lldb::endian::InlHostByteOrder());
383 }
384 
385 
386 size_t
387 ObjectFile::ReadSectionData (const Section *section, off_t section_offset, void *dst, size_t dst_len) const
388 {
389     if (IsInMemory())
390     {
391         ProcessSP process_sp (m_process_wp.lock());
392         if (process_sp)
393         {
394             Error error;
395             return process_sp->ReadMemory (section->GetLoadBaseAddress (&process_sp->GetTarget()) + section_offset, dst, dst_len, error);
396         }
397     }
398     else
399     {
400         const uint64_t section_file_size = section->GetFileSize();
401         if (section_offset < section_file_size)
402         {
403             const uint64_t section_bytes_left = section_file_size - section_offset;
404             uint64_t section_dst_len = dst_len;
405             if (section_dst_len > section_bytes_left)
406                 section_dst_len = section_bytes_left;
407             return CopyData (section->GetFileOffset() + section_offset, section_dst_len, dst);
408         }
409     }
410     return 0;
411 }
412 
413 //----------------------------------------------------------------------
414 // Get the section data the file on disk
415 //----------------------------------------------------------------------
416 size_t
417 ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const
418 {
419     if (IsInMemory())
420     {
421         ProcessSP process_sp (m_process_wp.lock());
422         if (process_sp)
423         {
424             DataBufferSP data_sp (ReadMemory (process_sp, section->GetLoadBaseAddress (&process_sp->GetTarget()), section->GetByteSize()));
425             if (data_sp)
426             {
427                 section_data.SetData (data_sp, 0, data_sp->GetByteSize());
428                 section_data.SetByteOrder (process_sp->GetByteOrder());
429                 section_data.SetAddressByteSize (process_sp->GetAddressByteSize());
430                 return section_data.GetByteSize();
431             }
432         }
433     }
434     else
435     {
436         // The object file now contains a full mmap'ed copy of the object file data, so just use this
437         return MemoryMapSectionData (section, section_data);
438     }
439     section_data.Clear();
440     return 0;
441 }
442 
443 size_t
444 ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const
445 {
446     if (IsInMemory())
447     {
448         return ReadSectionData (section, section_data);
449     }
450     else
451     {
452         // The object file now contains a full mmap'ed copy of the object file data, so just use this
453         return GetData(section->GetFileOffset(), section->GetByteSize(), section_data);
454     }
455     section_data.Clear();
456     return 0;
457 }
458 
459