xref: /llvm-project/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp (revision 736d4d85db80d2583ac12daa1eac59b0c010e1af)
1 //===-- ObjectFileELF.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 "ObjectFileELF.h"
11 
12 #include <cassert>
13 #include <algorithm>
14 
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataBuffer.h"
17 #include "lldb/Core/Error.h"
18 #include "lldb/Core/FileSpecList.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/ModuleSpec.h"
22 #include "lldb/Core/PluginManager.h"
23 #include "lldb/Core/Section.h"
24 #include "lldb/Core/Stream.h"
25 #include "lldb/Core/Timer.h"
26 #include "lldb/Symbol/DWARFCallFrameInfo.h"
27 #include "lldb/Symbol/SymbolContext.h"
28 #include "lldb/Target/SectionLoadList.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Host/Host.h"
31 
32 #include "llvm/ADT/PointerUnion.h"
33 #include "llvm/Support/MathExtras.h"
34 
35 #define CASE_AND_STREAM(s, def, width)                  \
36     case def: s->Printf("%-*s", width, #def); break;
37 
38 using namespace lldb;
39 using namespace lldb_private;
40 using namespace elf;
41 using namespace llvm::ELF;
42 
43 namespace {
44 //===----------------------------------------------------------------------===//
45 /// @class ELFRelocation
46 /// @brief Generic wrapper for ELFRel and ELFRela.
47 ///
48 /// This helper class allows us to parse both ELFRel and ELFRela relocation
49 /// entries in a generic manner.
50 class ELFRelocation
51 {
52 public:
53 
54     /// Constructs an ELFRelocation entry with a personality as given by @p
55     /// type.
56     ///
57     /// @param type Either DT_REL or DT_RELA.  Any other value is invalid.
58     ELFRelocation(unsigned type);
59 
60     ~ELFRelocation();
61 
62     bool
63     Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
64 
65     static unsigned
66     RelocType32(const ELFRelocation &rel);
67 
68     static unsigned
69     RelocType64(const ELFRelocation &rel);
70 
71     static unsigned
72     RelocSymbol32(const ELFRelocation &rel);
73 
74     static unsigned
75     RelocSymbol64(const ELFRelocation &rel);
76 
77     static unsigned
78     RelocOffset32(const ELFRelocation &rel);
79 
80     static unsigned
81     RelocOffset64(const ELFRelocation &rel);
82 
83     static unsigned
84     RelocAddend32(const ELFRelocation &rel);
85 
86     static unsigned
87     RelocAddend64(const ELFRelocation &rel);
88 
89 private:
90     typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
91 
92     RelocUnion reloc;
93 };
94 
95 ELFRelocation::ELFRelocation(unsigned type)
96 {
97     if (type == DT_REL || type == SHT_REL)
98         reloc = new ELFRel();
99     else if (type == DT_RELA || type == SHT_RELA)
100         reloc = new ELFRela();
101     else {
102         assert(false && "unexpected relocation type");
103         reloc = static_cast<ELFRel*>(NULL);
104     }
105 }
106 
107 ELFRelocation::~ELFRelocation()
108 {
109     if (reloc.is<ELFRel*>())
110         delete reloc.get<ELFRel*>();
111     else
112         delete reloc.get<ELFRela*>();
113 }
114 
115 bool
116 ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
117 {
118     if (reloc.is<ELFRel*>())
119         return reloc.get<ELFRel*>()->Parse(data, offset);
120     else
121         return reloc.get<ELFRela*>()->Parse(data, offset);
122 }
123 
124 unsigned
125 ELFRelocation::RelocType32(const ELFRelocation &rel)
126 {
127     if (rel.reloc.is<ELFRel*>())
128         return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
129     else
130         return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
131 }
132 
133 unsigned
134 ELFRelocation::RelocType64(const ELFRelocation &rel)
135 {
136     if (rel.reloc.is<ELFRel*>())
137         return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
138     else
139         return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
140 }
141 
142 unsigned
143 ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
144 {
145     if (rel.reloc.is<ELFRel*>())
146         return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
147     else
148         return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
149 }
150 
151 unsigned
152 ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
153 {
154     if (rel.reloc.is<ELFRel*>())
155         return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
156     else
157         return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
158 }
159 
160 unsigned
161 ELFRelocation::RelocOffset32(const ELFRelocation &rel)
162 {
163     if (rel.reloc.is<ELFRel*>())
164         return rel.reloc.get<ELFRel*>()->r_offset;
165     else
166         return rel.reloc.get<ELFRela*>()->r_offset;
167 }
168 
169 unsigned
170 ELFRelocation::RelocOffset64(const ELFRelocation &rel)
171 {
172     if (rel.reloc.is<ELFRel*>())
173         return rel.reloc.get<ELFRel*>()->r_offset;
174     else
175         return rel.reloc.get<ELFRela*>()->r_offset;
176 }
177 
178 unsigned
179 ELFRelocation::RelocAddend32(const ELFRelocation &rel)
180 {
181     if (rel.reloc.is<ELFRel*>())
182         return 0;
183     else
184         return rel.reloc.get<ELFRela*>()->r_addend;
185 }
186 
187 unsigned
188 ELFRelocation::RelocAddend64(const ELFRelocation &rel)
189 {
190     if (rel.reloc.is<ELFRel*>())
191         return 0;
192     else
193         return rel.reloc.get<ELFRela*>()->r_addend;
194 }
195 
196 } // end anonymous namespace
197 
198 bool
199 ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset)
200 {
201     // Read all fields.
202     if (data.GetU32(offset, &n_namesz, 3) == NULL)
203         return false;
204 
205     // The name field is required to be nul-terminated, and n_namesz
206     // includes the terminating nul in observed implementations (contrary
207     // to the ELF-64 spec).  A special case is needed for cores generated
208     // by some older Linux versions, which write a note named "CORE"
209     // without a nul terminator and n_namesz = 4.
210     if (n_namesz == 4)
211     {
212         char buf[4];
213         if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4)
214             return false;
215         if (strncmp (buf, "CORE", 4) == 0)
216         {
217             n_name = "CORE";
218             *offset += 4;
219             return true;
220         }
221     }
222 
223     const char *cstr = data.GetCStr(offset, llvm::RoundUpToAlignment (n_namesz, 4));
224     if (cstr == NULL)
225     {
226         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
227         if (log)
228             log->Printf("Failed to parse note name lacking nul terminator");
229 
230         return false;
231     }
232     n_name = cstr;
233     return true;
234 }
235 
236 // Arbitrary constant used as UUID prefix for core files.
237 const uint32_t
238 ObjectFileELF::g_core_uuid_magic(0xE210C);
239 
240 //------------------------------------------------------------------
241 // Static methods.
242 //------------------------------------------------------------------
243 void
244 ObjectFileELF::Initialize()
245 {
246     PluginManager::RegisterPlugin(GetPluginNameStatic(),
247                                   GetPluginDescriptionStatic(),
248                                   CreateInstance,
249                                   CreateMemoryInstance,
250                                   GetModuleSpecifications);
251 }
252 
253 void
254 ObjectFileELF::Terminate()
255 {
256     PluginManager::UnregisterPlugin(CreateInstance);
257 }
258 
259 lldb_private::ConstString
260 ObjectFileELF::GetPluginNameStatic()
261 {
262     static ConstString g_name("elf");
263     return g_name;
264 }
265 
266 const char *
267 ObjectFileELF::GetPluginDescriptionStatic()
268 {
269     return "ELF object file reader.";
270 }
271 
272 ObjectFile *
273 ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
274                                DataBufferSP &data_sp,
275                                lldb::offset_t data_offset,
276                                const lldb_private::FileSpec* file,
277                                lldb::offset_t file_offset,
278                                lldb::offset_t length)
279 {
280     if (!data_sp)
281     {
282         data_sp = file->MemoryMapFileContents(file_offset, length);
283         data_offset = 0;
284     }
285 
286     if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
287     {
288         const uint8_t *magic = data_sp->GetBytes() + data_offset;
289         if (ELFHeader::MagicBytesMatch(magic))
290         {
291             // Update the data to contain the entire file if it doesn't already
292             if (data_sp->GetByteSize() < length) {
293                 data_sp = file->MemoryMapFileContents(file_offset, length);
294                 data_offset = 0;
295                 magic = data_sp->GetBytes();
296             }
297             unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
298             if (address_size == 4 || address_size == 8)
299             {
300                 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length));
301                 ArchSpec spec;
302                 if (objfile_ap->GetArchitecture(spec) &&
303                     objfile_ap->SetModulesArchitecture(spec))
304                     return objfile_ap.release();
305             }
306         }
307     }
308     return NULL;
309 }
310 
311 
312 ObjectFile*
313 ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
314                                      DataBufferSP& data_sp,
315                                      const lldb::ProcessSP &process_sp,
316                                      lldb::addr_t header_addr)
317 {
318     if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT))
319     {
320         const uint8_t *magic = data_sp->GetBytes();
321         if (ELFHeader::MagicBytesMatch(magic))
322         {
323             unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
324             if (address_size == 4 || address_size == 8)
325             {
326                 std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
327                 ArchSpec spec;
328                 if (objfile_ap->GetArchitecture(spec) &&
329                     objfile_ap->SetModulesArchitecture(spec))
330                     return objfile_ap.release();
331             }
332         }
333     }
334     return NULL;
335 }
336 
337 bool
338 ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp,
339                                   lldb::addr_t data_offset,
340                                   lldb::addr_t data_length)
341 {
342     if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
343     {
344         const uint8_t *magic = data_sp->GetBytes() + data_offset;
345         return ELFHeader::MagicBytesMatch(magic);
346     }
347     return false;
348 }
349 
350 /*
351  * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c
352  *
353  *   COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
354  *   code or tables extracted from it, as desired without restriction.
355  */
356 static uint32_t
357 calc_crc32(uint32_t crc, const void *buf, size_t size)
358 {
359     static const uint32_t g_crc32_tab[] =
360     {
361         0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
362         0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
363         0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
364         0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
365         0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
366         0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
367         0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
368         0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
369         0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
370         0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
371         0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
372         0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
373         0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
374         0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
375         0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
376         0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
377         0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
378         0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
379         0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
380         0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
381         0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
382         0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
383         0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
384         0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
385         0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
386         0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
387         0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
388         0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
389         0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
390         0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
391         0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
392         0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
393         0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
394         0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
395         0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
396         0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
397         0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
398         0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
399         0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
400         0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
401         0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
402         0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
403         0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
404     };
405     const uint8_t *p = (const uint8_t *)buf;
406 
407     crc = crc ^ ~0U;
408     while (size--)
409         crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
410     return crc ^ ~0U;
411 }
412 
413 static uint32_t
414 calc_gnu_debuglink_crc32(const void *buf, size_t size)
415 {
416     return calc_crc32(0U, buf, size);
417 }
418 
419 uint32_t
420 ObjectFileELF::CalculateELFNotesSegmentsCRC32 (const ProgramHeaderColl& program_headers,
421                                                DataExtractor& object_data)
422 {
423     typedef ProgramHeaderCollConstIter Iter;
424 
425     uint32_t core_notes_crc = 0;
426 
427     for (Iter I = program_headers.begin(); I != program_headers.end(); ++I)
428     {
429         if (I->p_type == llvm::ELF::PT_NOTE)
430         {
431             const elf_off ph_offset = I->p_offset;
432             const size_t ph_size = I->p_filesz;
433 
434             DataExtractor segment_data;
435             if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size)
436             {
437                 // The ELF program header contained incorrect data,
438                 // prabably corefile is incomplete or corrupted.
439                 break;
440             }
441 
442             core_notes_crc = calc_crc32(core_notes_crc,
443                                         segment_data.GetDataStart(),
444                                         segment_data.GetByteSize());
445         }
446     }
447 
448     return core_notes_crc;
449 }
450 
451 size_t
452 ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file,
453                                         lldb::DataBufferSP& data_sp,
454                                         lldb::offset_t data_offset,
455                                         lldb::offset_t file_offset,
456                                         lldb::offset_t length,
457                                         lldb_private::ModuleSpecList &specs)
458 {
459     const size_t initial_count = specs.GetSize();
460 
461     if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
462     {
463         DataExtractor data;
464         data.SetData(data_sp);
465         elf::ELFHeader header;
466         if (header.Parse(data, &data_offset))
467         {
468             if (data_sp)
469             {
470                 ModuleSpec spec;
471                 spec.GetFileSpec() = file;
472                 spec.GetArchitecture().SetArchitecture(eArchTypeELF,
473                                                        header.e_machine,
474                                                        LLDB_INVALID_CPUTYPE);
475                 if (spec.GetArchitecture().IsValid())
476                 {
477                     // We could parse the ABI tag information (in .note, .notes, or .note.ABI-tag) to get the
478                     // machine information. However, this info isn't guaranteed to exist or be correct. Details:
479                     //  http://refspecs.linuxfoundation.org/LSB_1.2.0/gLSB/noteabitag.html
480                     // Instead of passing potentially incorrect information down the pipeline, grab
481                     // the host information and use it.
482                     spec.GetArchitecture().GetTriple().setOSName (Host::GetOSString().GetCString());
483                     spec.GetArchitecture().GetTriple().setVendorName(Host::GetVendorString().GetCString());
484 
485                     // Try to get the UUID from the section list. Usually that's at the end, so
486                     // map the file in if we don't have it already.
487                     size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize;
488                     if (section_header_end > data_sp->GetByteSize())
489                     {
490                         data_sp = file.MemoryMapFileContents (file_offset, section_header_end);
491                         data.SetData(data_sp);
492                     }
493 
494                     uint32_t gnu_debuglink_crc = 0;
495                     std::string gnu_debuglink_file;
496                     SectionHeaderColl section_headers;
497                     lldb_private::UUID &uuid = spec.GetUUID();
498                     GetSectionHeaderInfo(section_headers, data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc);
499 
500 
501                     if (!uuid.IsValid())
502                     {
503                         uint32_t core_notes_crc = 0;
504 
505                         if (!gnu_debuglink_crc)
506                         {
507                             lldb_private::Timer scoped_timer (__PRETTY_FUNCTION__,
508                                                               "Calculating module crc32 %s with size %" PRIu64 " KiB",
509                                                               file.GetLastPathComponent().AsCString(),
510                                                               (file.GetByteSize()-file_offset)/1024);
511 
512                             // For core files - which usually don't happen to have a gnu_debuglink,
513                             // and are pretty bulky - calulating whole contents crc32 would be too much of luxury.
514                             // Thus we will need to fallback to something simpler.
515                             if (header.e_type == llvm::ELF::ET_CORE)
516                             {
517                                 size_t program_headers_end = header.e_phoff + header.e_phnum * header.e_phentsize;
518                                 if (program_headers_end > data_sp->GetByteSize())
519                                 {
520                                     data_sp = file.MemoryMapFileContents(file_offset, program_headers_end);
521                                     data.SetData(data_sp);
522                                 }
523                                 ProgramHeaderColl program_headers;
524                                 GetProgramHeaderInfo(program_headers, data, header);
525 
526                                 size_t segment_data_end = 0;
527                                 for (ProgramHeaderCollConstIter I = program_headers.begin();
528                                      I != program_headers.end(); ++I)
529                                 {
530                                      segment_data_end = std::max<unsigned long long> (I->p_offset + I->p_filesz, segment_data_end);
531                                 }
532 
533                                 if (segment_data_end > data_sp->GetByteSize())
534                                 {
535                                     data_sp = file.MemoryMapFileContents(file_offset, segment_data_end);
536                                     data.SetData(data_sp);
537                                 }
538 
539                                 core_notes_crc = CalculateELFNotesSegmentsCRC32 (program_headers, data);
540                             }
541                             else
542                             {
543                                 // Need to map entire file into memory to calculate the crc.
544                                 data_sp = file.MemoryMapFileContents (file_offset, SIZE_MAX);
545                                 data.SetData(data_sp);
546                                 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize());
547                             }
548                         }
549                         if (gnu_debuglink_crc)
550                         {
551                             // Use 4 bytes of crc from the .gnu_debuglink section.
552                             uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 };
553                             uuid.SetBytes (uuidt, sizeof(uuidt));
554                         }
555                         else if (core_notes_crc)
556                         {
557                             // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it look different form
558                             // .gnu_debuglink crc followed by 4 bytes of note segments crc.
559                             uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
560                             uuid.SetBytes (uuidt, sizeof(uuidt));
561                         }
562                     }
563 
564                     specs.Append(spec);
565                 }
566             }
567         }
568     }
569 
570     return specs.GetSize() - initial_count;
571 }
572 
573 //------------------------------------------------------------------
574 // PluginInterface protocol
575 //------------------------------------------------------------------
576 lldb_private::ConstString
577 ObjectFileELF::GetPluginName()
578 {
579     return GetPluginNameStatic();
580 }
581 
582 uint32_t
583 ObjectFileELF::GetPluginVersion()
584 {
585     return m_plugin_version;
586 }
587 //------------------------------------------------------------------
588 // ObjectFile protocol
589 //------------------------------------------------------------------
590 
591 ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
592                               DataBufferSP& data_sp,
593                               lldb::offset_t data_offset,
594                               const FileSpec* file,
595                               lldb::offset_t file_offset,
596                               lldb::offset_t length) :
597     ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
598     m_header(),
599     m_program_headers(),
600     m_section_headers(),
601     m_filespec_ap()
602 {
603     if (file)
604         m_file = *file;
605     ::memset(&m_header, 0, sizeof(m_header));
606     m_gnu_debuglink_crc = 0;
607     m_gnu_debuglink_file.clear();
608 }
609 
610 ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
611                               DataBufferSP& data_sp,
612                               const lldb::ProcessSP &process_sp,
613                               addr_t header_addr) :
614     ObjectFile(module_sp, process_sp, LLDB_INVALID_ADDRESS, data_sp),
615     m_header(),
616     m_program_headers(),
617     m_section_headers(),
618     m_filespec_ap()
619 {
620     ::memset(&m_header, 0, sizeof(m_header));
621 }
622 
623 ObjectFileELF::~ObjectFileELF()
624 {
625 }
626 
627 bool
628 ObjectFileELF::IsExecutable() const
629 {
630     return m_header.e_entry != 0;
631 }
632 
633 bool
634 ObjectFileELF::SetLoadAddress (Target &target,
635                                lldb::addr_t value,
636                                bool value_is_offset)
637 {
638     ModuleSP module_sp = GetModule();
639     if (module_sp)
640     {
641         size_t num_loaded_sections = 0;
642         SectionList *section_list = GetSectionList ();
643         if (section_list)
644         {
645             if (value_is_offset)
646             {
647                 const size_t num_sections = section_list->GetSize();
648                 size_t sect_idx = 0;
649 
650                 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
651                 {
652                     // Iterate through the object file sections to find all
653                     // of the sections that have SHF_ALLOC in their flag bits.
654                     SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
655                     // if (section_sp && !section_sp->IsThreadSpecific())
656                     if (section_sp && section_sp->Test(SHF_ALLOC))
657                     {
658                         if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
659                             ++num_loaded_sections;
660                     }
661                 }
662                 return num_loaded_sections > 0;
663             }
664             else
665             {
666                 // Not sure how to slide an ELF file given the base address
667                 // of the ELF file in memory
668             }
669         }
670     }
671     return false; // If it changed
672 }
673 
674 ByteOrder
675 ObjectFileELF::GetByteOrder() const
676 {
677     if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
678         return eByteOrderBig;
679     if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
680         return eByteOrderLittle;
681     return eByteOrderInvalid;
682 }
683 
684 uint32_t
685 ObjectFileELF::GetAddressByteSize() const
686 {
687     return m_data.GetAddressByteSize();
688 }
689 
690 size_t
691 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
692 {
693     return std::distance(m_section_headers.begin(), I) + 1u;
694 }
695 
696 size_t
697 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
698 {
699     return std::distance(m_section_headers.begin(), I) + 1u;
700 }
701 
702 bool
703 ObjectFileELF::ParseHeader()
704 {
705     lldb::offset_t offset = 0;
706     return m_header.Parse(m_data, &offset);
707 }
708 
709 bool
710 ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
711 {
712     // Need to parse the section list to get the UUIDs, so make sure that's been done.
713     if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
714         return false;
715 
716     if (m_uuid.IsValid())
717     {
718         // We have the full build id uuid.
719         *uuid = m_uuid;
720         return true;
721     }
722     else if (GetType() == ObjectFile::eTypeCoreFile)
723     {
724         uint32_t core_notes_crc = 0;
725 
726         if (!ParseProgramHeaders())
727             return false;
728 
729         core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
730 
731         if (core_notes_crc)
732         {
733             // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
734             // look different form .gnu_debuglink crc - followed by 4 bytes of note
735             // segments crc.
736             uint32_t uuidt[4] = { g_core_uuid_magic, core_notes_crc, 0, 0 };
737             m_uuid.SetBytes (uuidt, sizeof(uuidt));
738         }
739     }
740     else
741     {
742         if (!m_gnu_debuglink_crc)
743             m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize());
744         if (m_gnu_debuglink_crc)
745         {
746             // Use 4 bytes of crc from the .gnu_debuglink section.
747             uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 };
748             m_uuid.SetBytes (uuidt, sizeof(uuidt));
749         }
750     }
751 
752     if (m_uuid.IsValid())
753     {
754         *uuid = m_uuid;
755         return true;
756     }
757 
758     return false;
759 }
760 
761 lldb_private::FileSpecList
762 ObjectFileELF::GetDebugSymbolFilePaths()
763 {
764     FileSpecList file_spec_list;
765 
766     if (!m_gnu_debuglink_file.empty())
767     {
768         FileSpec file_spec (m_gnu_debuglink_file.c_str(), false);
769         file_spec_list.Append (file_spec);
770     }
771     return file_spec_list;
772 }
773 
774 uint32_t
775 ObjectFileELF::GetDependentModules(FileSpecList &files)
776 {
777     size_t num_modules = ParseDependentModules();
778     uint32_t num_specs = 0;
779 
780     for (unsigned i = 0; i < num_modules; ++i)
781     {
782         if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
783             num_specs++;
784     }
785 
786     return num_specs;
787 }
788 
789 Address
790 ObjectFileELF::GetImageInfoAddress(Target *target)
791 {
792     if (!ParseDynamicSymbols())
793         return Address();
794 
795     SectionList *section_list = GetSectionList();
796     if (!section_list)
797         return Address();
798 
799     // Find the SHT_DYNAMIC (.dynamic) section.
800     SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true));
801     if (!dynsym_section_sp)
802         return Address();
803     assert (dynsym_section_sp->GetObjectFile() == this);
804 
805     user_id_t dynsym_id = dynsym_section_sp->GetID();
806     const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
807     if (!dynsym_hdr)
808         return Address();
809 
810     for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
811     {
812         ELFDynamic &symbol = m_dynamic_symbols[i];
813 
814         if (symbol.d_tag == DT_DEBUG)
815         {
816             // Compute the offset as the number of previous entries plus the
817             // size of d_tag.
818             addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
819             return Address(dynsym_section_sp, offset);
820         }
821         else if (symbol.d_tag == DT_MIPS_RLD_MAP && target)
822         {
823             addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
824             addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
825             if (dyn_base == LLDB_INVALID_ADDRESS)
826                 return Address();
827             Address addr;
828             Error error;
829             if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr))
830                 return addr;
831         }
832     }
833 
834     return Address();
835 }
836 
837 lldb_private::Address
838 ObjectFileELF::GetEntryPointAddress ()
839 {
840     if (m_entry_point_address.IsValid())
841         return m_entry_point_address;
842 
843     if (!ParseHeader() || !IsExecutable())
844         return m_entry_point_address;
845 
846     SectionList *section_list = GetSectionList();
847     addr_t offset = m_header.e_entry;
848 
849     if (!section_list)
850         m_entry_point_address.SetOffset(offset);
851     else
852         m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
853     return m_entry_point_address;
854 }
855 
856 //----------------------------------------------------------------------
857 // ParseDependentModules
858 //----------------------------------------------------------------------
859 size_t
860 ObjectFileELF::ParseDependentModules()
861 {
862     if (m_filespec_ap.get())
863         return m_filespec_ap->GetSize();
864 
865     m_filespec_ap.reset(new FileSpecList());
866 
867     if (!ParseSectionHeaders())
868         return 0;
869 
870     SectionList *section_list = GetSectionList();
871     if (!section_list)
872         return 0;
873 
874     // Find the SHT_DYNAMIC section.
875     Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
876     if (!dynsym)
877         return 0;
878     assert (dynsym->GetObjectFile() == this);
879 
880     const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID());
881     if (!header)
882         return 0;
883     // sh_link: section header index of string table used by entries in the section.
884     Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get();
885     if (!dynstr)
886         return 0;
887 
888     DataExtractor dynsym_data;
889     DataExtractor dynstr_data;
890     if (ReadSectionData(dynsym, dynsym_data) &&
891         ReadSectionData(dynstr, dynstr_data))
892     {
893         ELFDynamic symbol;
894         const lldb::offset_t section_size = dynsym_data.GetByteSize();
895         lldb::offset_t offset = 0;
896 
897         // The only type of entries we are concerned with are tagged DT_NEEDED,
898         // yielding the name of a required library.
899         while (offset < section_size)
900         {
901             if (!symbol.Parse(dynsym_data, &offset))
902                 break;
903 
904             if (symbol.d_tag != DT_NEEDED)
905                 continue;
906 
907             uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
908             const char *lib_name = dynstr_data.PeekCStr(str_index);
909             m_filespec_ap->Append(FileSpec(lib_name, true));
910         }
911     }
912 
913     return m_filespec_ap->GetSize();
914 }
915 
916 //----------------------------------------------------------------------
917 // GetProgramHeaderInfo
918 //----------------------------------------------------------------------
919 size_t
920 ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
921                                     DataExtractor &object_data,
922                                     const ELFHeader &header)
923 {
924     // We have already parsed the program headers
925     if (!program_headers.empty())
926         return program_headers.size();
927 
928     // If there are no program headers to read we are done.
929     if (header.e_phnum == 0)
930         return 0;
931 
932     program_headers.resize(header.e_phnum);
933     if (program_headers.size() != header.e_phnum)
934         return 0;
935 
936     const size_t ph_size = header.e_phnum * header.e_phentsize;
937     const elf_off ph_offset = header.e_phoff;
938     DataExtractor data;
939     if (data.SetData(object_data, ph_offset, ph_size) != ph_size)
940         return 0;
941 
942     uint32_t idx;
943     lldb::offset_t offset;
944     for (idx = 0, offset = 0; idx < header.e_phnum; ++idx)
945     {
946         if (program_headers[idx].Parse(data, &offset) == false)
947             break;
948     }
949 
950     if (idx < program_headers.size())
951         program_headers.resize(idx);
952 
953     return program_headers.size();
954 
955 }
956 
957 //----------------------------------------------------------------------
958 // ParseProgramHeaders
959 //----------------------------------------------------------------------
960 size_t
961 ObjectFileELF::ParseProgramHeaders()
962 {
963     return GetProgramHeaderInfo(m_program_headers, m_data, m_header);
964 }
965 
966 static bool
967 ParseNoteGNUBuildID(DataExtractor &data, lldb_private::UUID &uuid)
968 {
969     // Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id.
970     // BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId
971     lldb::offset_t offset = 0;
972     static const uint32_t g_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h
973 
974     while (true)
975     {
976         ELFNote note = ELFNote();
977         if (!note.Parse(data, &offset))
978             return false;
979 
980         // 16 bytes is UUID|MD5, 20 bytes is SHA1
981         if (note.n_name == "GNU" && (note.n_type == g_gnu_build_id) &&
982             (note.n_descsz == 16 || note.n_descsz == 20))
983         {
984             uint8_t uuidbuf[20];
985             if (data.GetU8 (&offset, &uuidbuf, note.n_descsz) == NULL)
986                 return false;
987             uuid.SetBytes (uuidbuf, note.n_descsz);
988             return true;
989         }
990         offset += llvm::RoundUpToAlignment(note.n_descsz, 4);
991     }
992     return false;
993 }
994 
995 //----------------------------------------------------------------------
996 // GetSectionHeaderInfo
997 //----------------------------------------------------------------------
998 size_t
999 ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
1000                                     lldb_private::DataExtractor &object_data,
1001                                     const elf::ELFHeader &header,
1002                                     lldb_private::UUID &uuid,
1003                                     std::string &gnu_debuglink_file,
1004                                     uint32_t &gnu_debuglink_crc)
1005 {
1006     // We have already parsed the section headers
1007     if (!section_headers.empty())
1008         return section_headers.size();
1009 
1010     // If there are no section headers we are done.
1011     if (header.e_shnum == 0)
1012         return 0;
1013 
1014     section_headers.resize(header.e_shnum);
1015     if (section_headers.size() != header.e_shnum)
1016         return 0;
1017 
1018     const size_t sh_size = header.e_shnum * header.e_shentsize;
1019     const elf_off sh_offset = header.e_shoff;
1020     DataExtractor sh_data;
1021     if (sh_data.SetData (object_data, sh_offset, sh_size) != sh_size)
1022         return 0;
1023 
1024     uint32_t idx;
1025     lldb::offset_t offset;
1026     for (idx = 0, offset = 0; idx < header.e_shnum; ++idx)
1027     {
1028         if (section_headers[idx].Parse(sh_data, &offset) == false)
1029             break;
1030     }
1031     if (idx < section_headers.size())
1032         section_headers.resize(idx);
1033 
1034     const unsigned strtab_idx = header.e_shstrndx;
1035     if (strtab_idx && strtab_idx < section_headers.size())
1036     {
1037         const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
1038         const size_t byte_size = sheader.sh_size;
1039         const Elf64_Off offset = sheader.sh_offset;
1040         lldb_private::DataExtractor shstr_data;
1041 
1042         if (shstr_data.SetData (object_data, offset, byte_size) == byte_size)
1043         {
1044             for (SectionHeaderCollIter I = section_headers.begin();
1045                  I != section_headers.end(); ++I)
1046             {
1047                 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink");
1048                 const ELFSectionHeaderInfo &header = *I;
1049                 const uint64_t section_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1050                 ConstString name(shstr_data.PeekCStr(I->sh_name));
1051 
1052                 I->section_name = name;
1053 
1054                 if (name == g_sect_name_gnu_debuglink)
1055                 {
1056                     DataExtractor data;
1057                     if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size))
1058                     {
1059                         lldb::offset_t gnu_debuglink_offset = 0;
1060                         gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset);
1061                         gnu_debuglink_offset = llvm::RoundUpToAlignment (gnu_debuglink_offset, 4);
1062                         data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
1063                     }
1064                 }
1065 
1066                 if (header.sh_type == SHT_NOTE && !uuid.IsValid())
1067                 {
1068                     DataExtractor data;
1069                     if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size))
1070                     {
1071                         ParseNoteGNUBuildID (data, uuid);
1072                     }
1073                 }
1074             }
1075 
1076             return section_headers.size();
1077         }
1078     }
1079 
1080     section_headers.clear();
1081     return 0;
1082 }
1083 
1084 size_t
1085 ObjectFileELF::GetProgramHeaderCount()
1086 {
1087     return ParseProgramHeaders();
1088 }
1089 
1090 const elf::ELFProgramHeader *
1091 ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id)
1092 {
1093     if (!id || !ParseProgramHeaders())
1094         return NULL;
1095 
1096     if (--id < m_program_headers.size())
1097         return &m_program_headers[id];
1098 
1099     return NULL;
1100 }
1101 
1102 DataExtractor
1103 ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id)
1104 {
1105     const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id);
1106     if (segment_header == NULL)
1107         return DataExtractor();
1108     return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz);
1109 }
1110 
1111 //----------------------------------------------------------------------
1112 // ParseSectionHeaders
1113 //----------------------------------------------------------------------
1114 size_t
1115 ObjectFileELF::ParseSectionHeaders()
1116 {
1117     return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc);
1118 }
1119 
1120 const ObjectFileELF::ELFSectionHeaderInfo *
1121 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
1122 {
1123     if (!id || !ParseSectionHeaders())
1124         return NULL;
1125 
1126     if (--id < m_section_headers.size())
1127         return &m_section_headers[id];
1128 
1129     return NULL;
1130 }
1131 
1132 void
1133 ObjectFileELF::CreateSections(SectionList &unified_section_list)
1134 {
1135     if (!m_sections_ap.get() && ParseSectionHeaders())
1136     {
1137         m_sections_ap.reset(new SectionList());
1138 
1139         for (SectionHeaderCollIter I = m_section_headers.begin();
1140              I != m_section_headers.end(); ++I)
1141         {
1142             const ELFSectionHeaderInfo &header = *I;
1143 
1144             ConstString& name = I->section_name;
1145             const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1146             const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
1147 
1148             static ConstString g_sect_name_text (".text");
1149             static ConstString g_sect_name_data (".data");
1150             static ConstString g_sect_name_bss (".bss");
1151             static ConstString g_sect_name_tdata (".tdata");
1152             static ConstString g_sect_name_tbss (".tbss");
1153             static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
1154             static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
1155             static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
1156             static ConstString g_sect_name_dwarf_debug_info (".debug_info");
1157             static ConstString g_sect_name_dwarf_debug_line (".debug_line");
1158             static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
1159             static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
1160             static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
1161             static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
1162             static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
1163             static ConstString g_sect_name_dwarf_debug_str (".debug_str");
1164             static ConstString g_sect_name_eh_frame (".eh_frame");
1165 
1166             SectionType sect_type = eSectionTypeOther;
1167 
1168             bool is_thread_specific = false;
1169 
1170             if      (name == g_sect_name_text)                  sect_type = eSectionTypeCode;
1171             else if (name == g_sect_name_data)                  sect_type = eSectionTypeData;
1172             else if (name == g_sect_name_bss)                   sect_type = eSectionTypeZeroFill;
1173             else if (name == g_sect_name_tdata)
1174             {
1175                 sect_type = eSectionTypeData;
1176                 is_thread_specific = true;
1177             }
1178             else if (name == g_sect_name_tbss)
1179             {
1180                 sect_type = eSectionTypeZeroFill;
1181                 is_thread_specific = true;
1182             }
1183             // .debug_abbrev – Abbreviations used in the .debug_info section
1184             // .debug_aranges – Lookup table for mapping addresses to compilation units
1185             // .debug_frame – Call frame information
1186             // .debug_info – The core DWARF information section
1187             // .debug_line – Line number information
1188             // .debug_loc – Location lists used in DW_AT_location attributes
1189             // .debug_macinfo – Macro information
1190             // .debug_pubnames – Lookup table for mapping object and function names to compilation units
1191             // .debug_pubtypes – Lookup table for mapping type names to compilation units
1192             // .debug_ranges – Address ranges used in DW_AT_ranges attributes
1193             // .debug_str – String table used in .debug_info
1194             // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html
1195             // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644
1196             // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo
1197             else if (name == g_sect_name_dwarf_debug_abbrev)    sect_type = eSectionTypeDWARFDebugAbbrev;
1198             else if (name == g_sect_name_dwarf_debug_aranges)   sect_type = eSectionTypeDWARFDebugAranges;
1199             else if (name == g_sect_name_dwarf_debug_frame)     sect_type = eSectionTypeDWARFDebugFrame;
1200             else if (name == g_sect_name_dwarf_debug_info)      sect_type = eSectionTypeDWARFDebugInfo;
1201             else if (name == g_sect_name_dwarf_debug_line)      sect_type = eSectionTypeDWARFDebugLine;
1202             else if (name == g_sect_name_dwarf_debug_loc)       sect_type = eSectionTypeDWARFDebugLoc;
1203             else if (name == g_sect_name_dwarf_debug_macinfo)   sect_type = eSectionTypeDWARFDebugMacInfo;
1204             else if (name == g_sect_name_dwarf_debug_pubnames)  sect_type = eSectionTypeDWARFDebugPubNames;
1205             else if (name == g_sect_name_dwarf_debug_pubtypes)  sect_type = eSectionTypeDWARFDebugPubTypes;
1206             else if (name == g_sect_name_dwarf_debug_ranges)    sect_type = eSectionTypeDWARFDebugRanges;
1207             else if (name == g_sect_name_dwarf_debug_str)       sect_type = eSectionTypeDWARFDebugStr;
1208             else if (name == g_sect_name_eh_frame)              sect_type = eSectionTypeEHFrame;
1209 
1210             switch (header.sh_type)
1211             {
1212                 case SHT_SYMTAB:
1213                     assert (sect_type == eSectionTypeOther);
1214                     sect_type = eSectionTypeELFSymbolTable;
1215                     break;
1216                 case SHT_DYNSYM:
1217                     assert (sect_type == eSectionTypeOther);
1218                     sect_type = eSectionTypeELFDynamicSymbols;
1219                     break;
1220                 case SHT_RELA:
1221                 case SHT_REL:
1222                     assert (sect_type == eSectionTypeOther);
1223                     sect_type = eSectionTypeELFRelocationEntries;
1224                     break;
1225                 case SHT_DYNAMIC:
1226                     assert (sect_type == eSectionTypeOther);
1227                     sect_type = eSectionTypeELFDynamicLinkInfo;
1228                     break;
1229             }
1230 
1231             elf::elf_xword log2align = (header.sh_addralign==0)
1232                                         ? 0
1233                                         : llvm::Log2_64(header.sh_addralign);
1234             SectionSP section_sp (new Section(GetModule(),        // Module to which this section belongs.
1235                                               this,               // ObjectFile to which this section belongs and should read section data from.
1236                                               SectionIndex(I),    // Section ID.
1237                                               name,               // Section name.
1238                                               sect_type,          // Section type.
1239                                               header.sh_addr,     // VM address.
1240                                               vm_size,            // VM size in bytes of this section.
1241                                               header.sh_offset,   // Offset of this section in the file.
1242                                               file_size,          // Size of the section as found in the file.
1243                                               log2align,          // Alignment of the section
1244                                               header.sh_flags));  // Flags for this section.
1245 
1246             if (is_thread_specific)
1247                 section_sp->SetIsThreadSpecific (is_thread_specific);
1248             m_sections_ap->AddSection(section_sp);
1249         }
1250     }
1251 
1252     if (m_sections_ap.get())
1253     {
1254         if (GetType() == eTypeDebugInfo)
1255         {
1256             static const SectionType g_sections[] =
1257             {
1258                 eSectionTypeDWARFDebugAranges,
1259                 eSectionTypeDWARFDebugInfo,
1260                 eSectionTypeDWARFDebugAbbrev,
1261                 eSectionTypeDWARFDebugFrame,
1262                 eSectionTypeDWARFDebugLine,
1263                 eSectionTypeDWARFDebugStr,
1264                 eSectionTypeDWARFDebugLoc,
1265                 eSectionTypeDWARFDebugMacInfo,
1266                 eSectionTypeDWARFDebugPubNames,
1267                 eSectionTypeDWARFDebugPubTypes,
1268                 eSectionTypeDWARFDebugRanges,
1269                 eSectionTypeELFSymbolTable,
1270             };
1271             SectionList *elf_section_list = m_sections_ap.get();
1272             for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx)
1273             {
1274                 SectionType section_type = g_sections[idx];
1275                 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true));
1276                 if (section_sp)
1277                 {
1278                     SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true));
1279                     if (module_section_sp)
1280                         unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp);
1281                     else
1282                         unified_section_list.AddSection (section_sp);
1283                 }
1284             }
1285         }
1286         else
1287         {
1288             unified_section_list = *m_sections_ap;
1289         }
1290     }
1291 }
1292 
1293 // private
1294 unsigned
1295 ObjectFileELF::ParseSymbols (Symtab *symtab,
1296                              user_id_t start_id,
1297                              SectionList *section_list,
1298                              const size_t num_symbols,
1299                              const DataExtractor &symtab_data,
1300                              const DataExtractor &strtab_data)
1301 {
1302     ELFSymbol symbol;
1303     lldb::offset_t offset = 0;
1304 
1305     static ConstString text_section_name(".text");
1306     static ConstString init_section_name(".init");
1307     static ConstString fini_section_name(".fini");
1308     static ConstString ctors_section_name(".ctors");
1309     static ConstString dtors_section_name(".dtors");
1310 
1311     static ConstString data_section_name(".data");
1312     static ConstString rodata_section_name(".rodata");
1313     static ConstString rodata1_section_name(".rodata1");
1314     static ConstString data2_section_name(".data1");
1315     static ConstString bss_section_name(".bss");
1316 
1317     //StreamFile strm(stdout, false);
1318     unsigned i;
1319     for (i = 0; i < num_symbols; ++i)
1320     {
1321         if (symbol.Parse(symtab_data, &offset) == false)
1322             break;
1323 
1324         const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
1325 
1326         // No need to add non-section symbols that have no names
1327         if (symbol.getType() != STT_SECTION &&
1328             (symbol_name == NULL || symbol_name[0] == '\0'))
1329             continue;
1330 
1331         //symbol.Dump (&strm, i, &strtab_data, section_list);
1332 
1333         SectionSP symbol_section_sp;
1334         SymbolType symbol_type = eSymbolTypeInvalid;
1335         Elf64_Half symbol_idx = symbol.st_shndx;
1336 
1337         switch (symbol_idx)
1338         {
1339         case SHN_ABS:
1340             symbol_type = eSymbolTypeAbsolute;
1341             break;
1342         case SHN_UNDEF:
1343             symbol_type = eSymbolTypeUndefined;
1344             break;
1345         default:
1346             symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
1347             break;
1348         }
1349 
1350         // If a symbol is undefined do not process it further even if it has a STT type
1351         if (symbol_type != eSymbolTypeUndefined)
1352         {
1353             switch (symbol.getType())
1354             {
1355             default:
1356             case STT_NOTYPE:
1357                 // The symbol's type is not specified.
1358                 break;
1359 
1360             case STT_OBJECT:
1361                 // The symbol is associated with a data object, such as a variable,
1362                 // an array, etc.
1363                 symbol_type = eSymbolTypeData;
1364                 break;
1365 
1366             case STT_FUNC:
1367                 // The symbol is associated with a function or other executable code.
1368                 symbol_type = eSymbolTypeCode;
1369                 break;
1370 
1371             case STT_SECTION:
1372                 // The symbol is associated with a section. Symbol table entries of
1373                 // this type exist primarily for relocation and normally have
1374                 // STB_LOCAL binding.
1375                 break;
1376 
1377             case STT_FILE:
1378                 // Conventionally, the symbol's name gives the name of the source
1379                 // file associated with the object file. A file symbol has STB_LOCAL
1380                 // binding, its section index is SHN_ABS, and it precedes the other
1381                 // STB_LOCAL symbols for the file, if it is present.
1382                 symbol_type = eSymbolTypeSourceFile;
1383                 break;
1384 
1385             case STT_GNU_IFUNC:
1386                 // The symbol is associated with an indirect function. The actual
1387                 // function will be resolved if it is referenced.
1388                 symbol_type = eSymbolTypeResolver;
1389                 break;
1390             }
1391         }
1392 
1393         if (symbol_type == eSymbolTypeInvalid)
1394         {
1395             if (symbol_section_sp)
1396             {
1397                 const ConstString &sect_name = symbol_section_sp->GetName();
1398                 if (sect_name == text_section_name ||
1399                     sect_name == init_section_name ||
1400                     sect_name == fini_section_name ||
1401                     sect_name == ctors_section_name ||
1402                     sect_name == dtors_section_name)
1403                 {
1404                     symbol_type = eSymbolTypeCode;
1405                 }
1406                 else if (sect_name == data_section_name ||
1407                          sect_name == data2_section_name ||
1408                          sect_name == rodata_section_name ||
1409                          sect_name == rodata1_section_name ||
1410                          sect_name == bss_section_name)
1411                 {
1412                     symbol_type = eSymbolTypeData;
1413                 }
1414             }
1415         }
1416 
1417         // If the symbol section we've found has no data (SHT_NOBITS), then check the module section
1418         // list. This can happen if we're parsing the debug file and it has no .text section, for example.
1419         if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0))
1420         {
1421             ModuleSP module_sp(GetModule());
1422             if (module_sp)
1423             {
1424                 SectionList *module_section_list = module_sp->GetSectionList();
1425                 if (module_section_list && module_section_list != section_list)
1426                 {
1427                     const ConstString &sect_name = symbol_section_sp->GetName();
1428                     lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name));
1429                     if (section_sp && section_sp->GetFileSize())
1430                     {
1431                         symbol_section_sp = section_sp;
1432                     }
1433                 }
1434             }
1435         }
1436 
1437         uint64_t symbol_value = symbol.st_value;
1438         if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile)
1439             symbol_value -= symbol_section_sp->GetFileAddress();
1440         bool is_global = symbol.getBinding() == STB_GLOBAL;
1441         uint32_t flags = symbol.st_other << 8 | symbol.st_info;
1442         bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
1443         Symbol dc_symbol(
1444             i + start_id,       // ID is the original symbol table index.
1445             symbol_name,        // Symbol name.
1446             is_mangled,         // Is the symbol name mangled?
1447             symbol_type,        // Type of this symbol
1448             is_global,          // Is this globally visible?
1449             false,              // Is this symbol debug info?
1450             false,              // Is this symbol a trampoline?
1451             false,              // Is this symbol artificial?
1452             symbol_section_sp,  // Section in which this symbol is defined or null.
1453             symbol_value,       // Offset in section or symbol value.
1454             symbol.st_size,     // Size in bytes of this symbol.
1455             true,               // Size is valid
1456             flags);             // Symbol flags.
1457         symtab->AddSymbol(dc_symbol);
1458     }
1459 
1460     return i;
1461 }
1462 
1463 unsigned
1464 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab)
1465 {
1466     if (symtab->GetObjectFile() != this)
1467     {
1468         // If the symbol table section is owned by a different object file, have it do the
1469         // parsing.
1470         ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile());
1471         return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab);
1472     }
1473 
1474     // Get section list for this object file.
1475     SectionList *section_list = m_sections_ap.get();
1476     if (!section_list)
1477         return 0;
1478 
1479     user_id_t symtab_id = symtab->GetID();
1480     const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
1481     assert(symtab_hdr->sh_type == SHT_SYMTAB ||
1482            symtab_hdr->sh_type == SHT_DYNSYM);
1483 
1484     // sh_link: section header index of associated string table.
1485     // Section ID's are ones based.
1486     user_id_t strtab_id = symtab_hdr->sh_link + 1;
1487     Section *strtab = section_list->FindSectionByID(strtab_id).get();
1488 
1489     if (symtab && strtab)
1490     {
1491         assert (symtab->GetObjectFile() == this);
1492         assert (strtab->GetObjectFile() == this);
1493 
1494         DataExtractor symtab_data;
1495         DataExtractor strtab_data;
1496         if (ReadSectionData(symtab, symtab_data) &&
1497             ReadSectionData(strtab, strtab_data))
1498         {
1499             size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
1500 
1501             return ParseSymbols(symbol_table, start_id, section_list,
1502                                 num_symbols, symtab_data, strtab_data);
1503         }
1504     }
1505 
1506     return 0;
1507 }
1508 
1509 size_t
1510 ObjectFileELF::ParseDynamicSymbols()
1511 {
1512     if (m_dynamic_symbols.size())
1513         return m_dynamic_symbols.size();
1514 
1515     SectionList *section_list = GetSectionList();
1516     if (!section_list)
1517         return 0;
1518 
1519     // Find the SHT_DYNAMIC section.
1520     Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
1521     if (!dynsym)
1522         return 0;
1523     assert (dynsym->GetObjectFile() == this);
1524 
1525     ELFDynamic symbol;
1526     DataExtractor dynsym_data;
1527     if (ReadSectionData(dynsym, dynsym_data))
1528     {
1529         const lldb::offset_t section_size = dynsym_data.GetByteSize();
1530         lldb::offset_t cursor = 0;
1531 
1532         while (cursor < section_size)
1533         {
1534             if (!symbol.Parse(dynsym_data, &cursor))
1535                 break;
1536 
1537             m_dynamic_symbols.push_back(symbol);
1538         }
1539     }
1540 
1541     return m_dynamic_symbols.size();
1542 }
1543 
1544 const ELFDynamic *
1545 ObjectFileELF::FindDynamicSymbol(unsigned tag)
1546 {
1547     if (!ParseDynamicSymbols())
1548         return NULL;
1549 
1550     DynamicSymbolCollIter I = m_dynamic_symbols.begin();
1551     DynamicSymbolCollIter E = m_dynamic_symbols.end();
1552     for ( ; I != E; ++I)
1553     {
1554         ELFDynamic *symbol = &*I;
1555 
1556         if (symbol->d_tag == tag)
1557             return symbol;
1558     }
1559 
1560     return NULL;
1561 }
1562 
1563 unsigned
1564 ObjectFileELF::PLTRelocationType()
1565 {
1566     // DT_PLTREL
1567     //  This member specifies the type of relocation entry to which the
1568     //  procedure linkage table refers. The d_val member holds DT_REL or
1569     //  DT_RELA, as appropriate. All relocations in a procedure linkage table
1570     //  must use the same relocation.
1571     const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
1572 
1573     if (symbol)
1574         return symbol->d_val;
1575 
1576     return 0;
1577 }
1578 
1579 static unsigned
1580 ParsePLTRelocations(Symtab *symbol_table,
1581                     user_id_t start_id,
1582                     unsigned rel_type,
1583                     const ELFHeader *hdr,
1584                     const ELFSectionHeader *rel_hdr,
1585                     const ELFSectionHeader *plt_hdr,
1586                     const ELFSectionHeader *sym_hdr,
1587                     const lldb::SectionSP &plt_section_sp,
1588                     DataExtractor &rel_data,
1589                     DataExtractor &symtab_data,
1590                     DataExtractor &strtab_data)
1591 {
1592     ELFRelocation rel(rel_type);
1593     ELFSymbol symbol;
1594     lldb::offset_t offset = 0;
1595     // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes.
1596     // So round the entsize up by the alignment if addralign is set.
1597     const elf_xword plt_entsize = plt_hdr->sh_addralign ?
1598         llvm::RoundUpToAlignment (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize;
1599     const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
1600 
1601     typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1602     reloc_info_fn reloc_type;
1603     reloc_info_fn reloc_symbol;
1604 
1605     if (hdr->Is32Bit())
1606     {
1607         reloc_type = ELFRelocation::RelocType32;
1608         reloc_symbol = ELFRelocation::RelocSymbol32;
1609     }
1610     else
1611     {
1612         reloc_type = ELFRelocation::RelocType64;
1613         reloc_symbol = ELFRelocation::RelocSymbol64;
1614     }
1615 
1616     unsigned slot_type = hdr->GetRelocationJumpSlotType();
1617     unsigned i;
1618     for (i = 0; i < num_relocations; ++i)
1619     {
1620         if (rel.Parse(rel_data, &offset) == false)
1621             break;
1622 
1623         if (reloc_type(rel) != slot_type)
1624             continue;
1625 
1626         lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
1627         uint64_t plt_index = (i + 1) * plt_entsize;
1628 
1629         if (!symbol.Parse(symtab_data, &symbol_offset))
1630             break;
1631 
1632         const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
1633         bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
1634 
1635         Symbol jump_symbol(
1636             i + start_id,    // Symbol table index
1637             symbol_name,     // symbol name.
1638             is_mangled,      // is the symbol name mangled?
1639             eSymbolTypeTrampoline, // Type of this symbol
1640             false,           // Is this globally visible?
1641             false,           // Is this symbol debug info?
1642             true,            // Is this symbol a trampoline?
1643             true,            // Is this symbol artificial?
1644             plt_section_sp,  // Section in which this symbol is defined or null.
1645             plt_index,       // Offset in section or symbol value.
1646             plt_entsize,     // Size in bytes of this symbol.
1647             true,            // Size is valid
1648             0);              // Symbol flags.
1649 
1650         symbol_table->AddSymbol(jump_symbol);
1651     }
1652 
1653     return i;
1654 }
1655 
1656 unsigned
1657 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1658                                       user_id_t start_id,
1659                                       const ELFSectionHeaderInfo *rel_hdr,
1660                                       user_id_t rel_id)
1661 {
1662     assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1663 
1664     // The link field points to the associated symbol table. The info field
1665     // points to the section holding the plt.
1666     user_id_t symtab_id = rel_hdr->sh_link;
1667     user_id_t plt_id = rel_hdr->sh_info;
1668 
1669     if (!symtab_id || !plt_id)
1670         return 0;
1671 
1672     // Section ID's are ones based;
1673     symtab_id++;
1674     plt_id++;
1675 
1676     const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
1677     if (!plt_hdr)
1678         return 0;
1679 
1680     const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1681     if (!sym_hdr)
1682         return 0;
1683 
1684     SectionList *section_list = m_sections_ap.get();
1685     if (!section_list)
1686         return 0;
1687 
1688     Section *rel_section = section_list->FindSectionByID(rel_id).get();
1689     if (!rel_section)
1690         return 0;
1691 
1692     SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1693     if (!plt_section_sp)
1694         return 0;
1695 
1696     Section *symtab = section_list->FindSectionByID(symtab_id).get();
1697     if (!symtab)
1698         return 0;
1699 
1700     // sh_link points to associated string table.
1701     Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1702     if (!strtab)
1703         return 0;
1704 
1705     DataExtractor rel_data;
1706     if (!ReadSectionData(rel_section, rel_data))
1707         return 0;
1708 
1709     DataExtractor symtab_data;
1710     if (!ReadSectionData(symtab, symtab_data))
1711         return 0;
1712 
1713     DataExtractor strtab_data;
1714     if (!ReadSectionData(strtab, strtab_data))
1715         return 0;
1716 
1717     unsigned rel_type = PLTRelocationType();
1718     if (!rel_type)
1719         return 0;
1720 
1721     return ParsePLTRelocations (symbol_table,
1722                                 start_id,
1723                                 rel_type,
1724                                 &m_header,
1725                                 rel_hdr,
1726                                 plt_hdr,
1727                                 sym_hdr,
1728                                 plt_section_sp,
1729                                 rel_data,
1730                                 symtab_data,
1731                                 strtab_data);
1732 }
1733 
1734 unsigned
1735 ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
1736                 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
1737                 DataExtractor &rel_data, DataExtractor &symtab_data,
1738                 DataExtractor &debug_data, Section* rel_section)
1739 {
1740     ELFRelocation rel(rel_hdr->sh_type);
1741     lldb::addr_t offset = 0;
1742     const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
1743     typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1744     reloc_info_fn reloc_type;
1745     reloc_info_fn reloc_symbol;
1746 
1747     if (hdr->Is32Bit())
1748     {
1749         reloc_type = ELFRelocation::RelocType32;
1750         reloc_symbol = ELFRelocation::RelocSymbol32;
1751     }
1752     else
1753     {
1754         reloc_type = ELFRelocation::RelocType64;
1755         reloc_symbol = ELFRelocation::RelocSymbol64;
1756     }
1757 
1758     for (unsigned i = 0; i < num_relocations; ++i)
1759     {
1760         if (rel.Parse(rel_data, &offset) == false)
1761             break;
1762 
1763         Symbol* symbol = NULL;
1764 
1765         if (hdr->Is32Bit())
1766         {
1767             switch (reloc_type(rel)) {
1768             case R_386_32:
1769             case R_386_PC32:
1770             default:
1771                 assert(false && "unexpected relocation type");
1772             }
1773         } else {
1774             switch (reloc_type(rel)) {
1775             case R_X86_64_64:
1776             {
1777                 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
1778                 if (symbol)
1779                 {
1780                     addr_t value = symbol->GetAddress().GetFileAddress();
1781                     DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
1782                     uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel));
1783                     *dst = value + ELFRelocation::RelocAddend64(rel);
1784                 }
1785                 break;
1786             }
1787             case R_X86_64_32:
1788             case R_X86_64_32S:
1789             {
1790                 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
1791                 if (symbol)
1792                 {
1793                     addr_t value = symbol->GetAddress().GetFileAddress();
1794                     value += ELFRelocation::RelocAddend32(rel);
1795                     assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) ||
1796                            (reloc_type(rel) == R_X86_64_32S &&
1797                             ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN)));
1798                     uint32_t truncated_addr = (value & 0xFFFFFFFF);
1799                     DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer();
1800                     uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel));
1801                     *dst = truncated_addr;
1802                 }
1803                 break;
1804             }
1805             case R_X86_64_PC32:
1806             default:
1807                 assert(false && "unexpected relocation type");
1808             }
1809         }
1810     }
1811 
1812     return 0;
1813 }
1814 
1815 unsigned
1816 ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id)
1817 {
1818     assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1819 
1820     // Parse in the section list if needed.
1821     SectionList *section_list = GetSectionList();
1822     if (!section_list)
1823         return 0;
1824 
1825     // Section ID's are ones based.
1826     user_id_t symtab_id = rel_hdr->sh_link + 1;
1827     user_id_t debug_id = rel_hdr->sh_info + 1;
1828 
1829     const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
1830     if (!symtab_hdr)
1831         return 0;
1832 
1833     const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
1834     if (!debug_hdr)
1835         return 0;
1836 
1837     Section *rel = section_list->FindSectionByID(rel_id).get();
1838     if (!rel)
1839         return 0;
1840 
1841     Section *symtab = section_list->FindSectionByID(symtab_id).get();
1842     if (!symtab)
1843         return 0;
1844 
1845     Section *debug = section_list->FindSectionByID(debug_id).get();
1846     if (!debug)
1847         return 0;
1848 
1849     DataExtractor rel_data;
1850     DataExtractor symtab_data;
1851     DataExtractor debug_data;
1852 
1853     if (ReadSectionData(rel, rel_data) &&
1854         ReadSectionData(symtab, symtab_data) &&
1855         ReadSectionData(debug, debug_data))
1856     {
1857         RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr,
1858                         rel_data, symtab_data, debug_data, debug);
1859     }
1860 
1861     return 0;
1862 }
1863 
1864 Symtab *
1865 ObjectFileELF::GetSymtab()
1866 {
1867     ModuleSP module_sp(GetModule());
1868     if (!module_sp)
1869         return NULL;
1870 
1871     // We always want to use the main object file so we (hopefully) only have one cached copy
1872     // of our symtab, dynamic sections, etc.
1873     ObjectFile *module_obj_file = module_sp->GetObjectFile();
1874     if (module_obj_file && module_obj_file != this)
1875         return module_obj_file->GetSymtab();
1876 
1877     if (m_symtab_ap.get() == NULL)
1878     {
1879         SectionList *section_list = GetSectionList();
1880         if (!section_list)
1881             return NULL;
1882 
1883         uint64_t symbol_id = 0;
1884         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1885 
1886         m_symtab_ap.reset(new Symtab(this));
1887 
1888         // Sharable objects and dynamic executables usually have 2 distinct symbol
1889         // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller
1890         // version of the symtab that only contains global symbols. The information found
1891         // in the dynsym is therefore also found in the symtab, while the reverse is not
1892         // necessarily true.
1893         Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get();
1894         if (!symtab)
1895         {
1896             // The symtab section is non-allocable and can be stripped, so if it doesn't exist
1897             // then use the dynsym section which should always be there.
1898             symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get();
1899         }
1900         if (symtab)
1901             symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab);
1902 
1903         // DT_JMPREL
1904         //      If present, this entry's d_ptr member holds the address of relocation
1905         //      entries associated solely with the procedure linkage table. Separating
1906         //      these relocation entries lets the dynamic linker ignore them during
1907         //      process initialization, if lazy binding is enabled. If this entry is
1908         //      present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
1909         //      also be present.
1910         const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
1911         if (symbol)
1912         {
1913             // Synthesize trampoline symbols to help navigate the PLT.
1914             addr_t addr = symbol->d_ptr;
1915             Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get();
1916             if (reloc_section)
1917             {
1918                 user_id_t reloc_id = reloc_section->GetID();
1919                 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id);
1920                 assert(reloc_header);
1921 
1922                 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id);
1923             }
1924         }
1925     }
1926 
1927     for (SectionHeaderCollIter I = m_section_headers.begin();
1928          I != m_section_headers.end(); ++I)
1929     {
1930         if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL)
1931         {
1932             if (CalculateType() == eTypeObjectFile)
1933             {
1934                 const char *section_name = I->section_name.AsCString("");
1935                 if (strstr(section_name, ".rela.debug") ||
1936                     strstr(section_name, ".rel.debug"))
1937                 {
1938                     const ELFSectionHeader &reloc_header = *I;
1939                     user_id_t reloc_id = SectionIndex(I);
1940                     RelocateDebugSections(&reloc_header, reloc_id);
1941                 }
1942             }
1943         }
1944     }
1945     return m_symtab_ap.get();
1946 }
1947 
1948 Symbol *
1949 ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique)
1950 {
1951     if (!m_symtab_ap.get())
1952         return nullptr; // GetSymtab() should be called first.
1953 
1954     const SectionList *section_list = GetSectionList();
1955     if (!section_list)
1956         return nullptr;
1957 
1958     if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo())
1959     {
1960         AddressRange range;
1961         if (eh_frame->GetAddressRange (so_addr, range))
1962         {
1963             const addr_t file_addr = range.GetBaseAddress().GetFileAddress();
1964             Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr;
1965             if (symbol)
1966                 return symbol;
1967 
1968             // Note that a (stripped) symbol won't be found by GetSymtab()...
1969             lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr);
1970             if (eh_sym_section_sp.get())
1971             {
1972                 addr_t section_base = eh_sym_section_sp->GetFileAddress();
1973                 addr_t offset = file_addr - section_base;
1974                 uint64_t symbol_id = m_symtab_ap->GetNumSymbols();
1975 
1976                 Symbol eh_symbol(
1977                         symbol_id,            // Symbol table index.
1978                         "???",                // Symbol name.
1979                         false,                // Is the symbol name mangled?
1980                         eSymbolTypeCode,      // Type of this symbol.
1981                         true,                 // Is this globally visible?
1982                         false,                // Is this symbol debug info?
1983                         false,                // Is this symbol a trampoline?
1984                         true,                 // Is this symbol artificial?
1985                         eh_sym_section_sp,    // Section in which this symbol is defined or null.
1986                         offset,               // Offset in section or symbol value.
1987                         range.GetByteSize(),  // Size in bytes of this symbol.
1988                         true,                 // Size is valid.
1989                         0);                   // Symbol flags.
1990                 if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol))
1991                     return m_symtab_ap->SymbolAtIndex(symbol_id);
1992             }
1993         }
1994     }
1995     return nullptr;
1996 }
1997 
1998 
1999 bool
2000 ObjectFileELF::IsStripped ()
2001 {
2002     // TODO: determine this for ELF
2003     return false;
2004 }
2005 
2006 //===----------------------------------------------------------------------===//
2007 // Dump
2008 //
2009 // Dump the specifics of the runtime file container (such as any headers
2010 // segments, sections, etc).
2011 //----------------------------------------------------------------------
2012 void
2013 ObjectFileELF::Dump(Stream *s)
2014 {
2015     DumpELFHeader(s, m_header);
2016     s->EOL();
2017     DumpELFProgramHeaders(s);
2018     s->EOL();
2019     DumpELFSectionHeaders(s);
2020     s->EOL();
2021     SectionList *section_list = GetSectionList();
2022     if (section_list)
2023         section_list->Dump(s, NULL, true, UINT32_MAX);
2024     Symtab *symtab = GetSymtab();
2025     if (symtab)
2026         symtab->Dump(s, NULL, eSortOrderNone);
2027     s->EOL();
2028     DumpDependentModules(s);
2029     s->EOL();
2030 }
2031 
2032 //----------------------------------------------------------------------
2033 // DumpELFHeader
2034 //
2035 // Dump the ELF header to the specified output stream
2036 //----------------------------------------------------------------------
2037 void
2038 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
2039 {
2040     s->PutCString("ELF Header\n");
2041     s->Printf("e_ident[EI_MAG0   ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
2042     s->Printf("e_ident[EI_MAG1   ] = 0x%2.2x '%c'\n",
2043               header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
2044     s->Printf("e_ident[EI_MAG2   ] = 0x%2.2x '%c'\n",
2045               header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
2046     s->Printf("e_ident[EI_MAG3   ] = 0x%2.2x '%c'\n",
2047               header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
2048 
2049     s->Printf("e_ident[EI_CLASS  ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
2050     s->Printf("e_ident[EI_DATA   ] = 0x%2.2x ", header.e_ident[EI_DATA]);
2051     DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
2052     s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
2053     s->Printf ("e_ident[EI_PAD    ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
2054 
2055     s->Printf("e_type      = 0x%4.4x ", header.e_type);
2056     DumpELFHeader_e_type(s, header.e_type);
2057     s->Printf("\ne_machine   = 0x%4.4x\n", header.e_machine);
2058     s->Printf("e_version   = 0x%8.8x\n", header.e_version);
2059     s->Printf("e_entry     = 0x%8.8" PRIx64 "\n", header.e_entry);
2060     s->Printf("e_phoff     = 0x%8.8" PRIx64 "\n", header.e_phoff);
2061     s->Printf("e_shoff     = 0x%8.8" PRIx64 "\n", header.e_shoff);
2062     s->Printf("e_flags     = 0x%8.8x\n", header.e_flags);
2063     s->Printf("e_ehsize    = 0x%4.4x\n", header.e_ehsize);
2064     s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
2065     s->Printf("e_phnum     = 0x%4.4x\n", header.e_phnum);
2066     s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
2067     s->Printf("e_shnum     = 0x%4.4x\n", header.e_shnum);
2068     s->Printf("e_shstrndx  = 0x%4.4x\n", header.e_shstrndx);
2069 }
2070 
2071 //----------------------------------------------------------------------
2072 // DumpELFHeader_e_type
2073 //
2074 // Dump an token value for the ELF header member e_type
2075 //----------------------------------------------------------------------
2076 void
2077 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
2078 {
2079     switch (e_type)
2080     {
2081     case ET_NONE:   *s << "ET_NONE"; break;
2082     case ET_REL:    *s << "ET_REL"; break;
2083     case ET_EXEC:   *s << "ET_EXEC"; break;
2084     case ET_DYN:    *s << "ET_DYN"; break;
2085     case ET_CORE:   *s << "ET_CORE"; break;
2086     default:
2087         break;
2088     }
2089 }
2090 
2091 //----------------------------------------------------------------------
2092 // DumpELFHeader_e_ident_EI_DATA
2093 //
2094 // Dump an token value for the ELF header member e_ident[EI_DATA]
2095 //----------------------------------------------------------------------
2096 void
2097 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
2098 {
2099     switch (ei_data)
2100     {
2101     case ELFDATANONE:   *s << "ELFDATANONE"; break;
2102     case ELFDATA2LSB:   *s << "ELFDATA2LSB - Little Endian"; break;
2103     case ELFDATA2MSB:   *s << "ELFDATA2MSB - Big Endian"; break;
2104     default:
2105         break;
2106     }
2107 }
2108 
2109 
2110 //----------------------------------------------------------------------
2111 // DumpELFProgramHeader
2112 //
2113 // Dump a single ELF program header to the specified output stream
2114 //----------------------------------------------------------------------
2115 void
2116 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
2117 {
2118     DumpELFProgramHeader_p_type(s, ph.p_type);
2119     s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
2120     s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags);
2121 
2122     DumpELFProgramHeader_p_flags(s, ph.p_flags);
2123     s->Printf(") %8.8" PRIx64, ph.p_align);
2124 }
2125 
2126 //----------------------------------------------------------------------
2127 // DumpELFProgramHeader_p_type
2128 //
2129 // Dump an token value for the ELF program header member p_type which
2130 // describes the type of the program header
2131 // ----------------------------------------------------------------------
2132 void
2133 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
2134 {
2135     const int kStrWidth = 15;
2136     switch (p_type)
2137     {
2138     CASE_AND_STREAM(s, PT_NULL        , kStrWidth);
2139     CASE_AND_STREAM(s, PT_LOAD        , kStrWidth);
2140     CASE_AND_STREAM(s, PT_DYNAMIC     , kStrWidth);
2141     CASE_AND_STREAM(s, PT_INTERP      , kStrWidth);
2142     CASE_AND_STREAM(s, PT_NOTE        , kStrWidth);
2143     CASE_AND_STREAM(s, PT_SHLIB       , kStrWidth);
2144     CASE_AND_STREAM(s, PT_PHDR        , kStrWidth);
2145     CASE_AND_STREAM(s, PT_TLS         , kStrWidth);
2146     CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
2147     default:
2148         s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
2149         break;
2150     }
2151 }
2152 
2153 
2154 //----------------------------------------------------------------------
2155 // DumpELFProgramHeader_p_flags
2156 //
2157 // Dump an token value for the ELF program header member p_flags
2158 //----------------------------------------------------------------------
2159 void
2160 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
2161 {
2162     *s  << ((p_flags & PF_X) ? "PF_X" : "    ")
2163         << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
2164         << ((p_flags & PF_W) ? "PF_W" : "    ")
2165         << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
2166         << ((p_flags & PF_R) ? "PF_R" : "    ");
2167 }
2168 
2169 //----------------------------------------------------------------------
2170 // DumpELFProgramHeaders
2171 //
2172 // Dump all of the ELF program header to the specified output stream
2173 //----------------------------------------------------------------------
2174 void
2175 ObjectFileELF::DumpELFProgramHeaders(Stream *s)
2176 {
2177     if (ParseProgramHeaders())
2178     {
2179         s->PutCString("Program Headers\n");
2180         s->PutCString("IDX  p_type          p_offset p_vaddr  p_paddr  "
2181                       "p_filesz p_memsz  p_flags                   p_align\n");
2182         s->PutCString("==== --------------- -------- -------- -------- "
2183                       "-------- -------- ------------------------- --------\n");
2184 
2185         uint32_t idx = 0;
2186         for (ProgramHeaderCollConstIter I = m_program_headers.begin();
2187              I != m_program_headers.end(); ++I, ++idx)
2188         {
2189             s->Printf("[%2u] ", idx);
2190             ObjectFileELF::DumpELFProgramHeader(s, *I);
2191             s->EOL();
2192         }
2193     }
2194 }
2195 
2196 //----------------------------------------------------------------------
2197 // DumpELFSectionHeader
2198 //
2199 // Dump a single ELF section header to the specified output stream
2200 //----------------------------------------------------------------------
2201 void
2202 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh)
2203 {
2204     s->Printf("%8.8x ", sh.sh_name);
2205     DumpELFSectionHeader_sh_type(s, sh.sh_type);
2206     s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
2207     DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
2208     s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size);
2209     s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
2210     s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
2211 }
2212 
2213 //----------------------------------------------------------------------
2214 // DumpELFSectionHeader_sh_type
2215 //
2216 // Dump an token value for the ELF section header member sh_type which
2217 // describes the type of the section
2218 //----------------------------------------------------------------------
2219 void
2220 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
2221 {
2222     const int kStrWidth = 12;
2223     switch (sh_type)
2224     {
2225     CASE_AND_STREAM(s, SHT_NULL     , kStrWidth);
2226     CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
2227     CASE_AND_STREAM(s, SHT_SYMTAB   , kStrWidth);
2228     CASE_AND_STREAM(s, SHT_STRTAB   , kStrWidth);
2229     CASE_AND_STREAM(s, SHT_RELA     , kStrWidth);
2230     CASE_AND_STREAM(s, SHT_HASH     , kStrWidth);
2231     CASE_AND_STREAM(s, SHT_DYNAMIC  , kStrWidth);
2232     CASE_AND_STREAM(s, SHT_NOTE     , kStrWidth);
2233     CASE_AND_STREAM(s, SHT_NOBITS   , kStrWidth);
2234     CASE_AND_STREAM(s, SHT_REL      , kStrWidth);
2235     CASE_AND_STREAM(s, SHT_SHLIB    , kStrWidth);
2236     CASE_AND_STREAM(s, SHT_DYNSYM   , kStrWidth);
2237     CASE_AND_STREAM(s, SHT_LOPROC   , kStrWidth);
2238     CASE_AND_STREAM(s, SHT_HIPROC   , kStrWidth);
2239     CASE_AND_STREAM(s, SHT_LOUSER   , kStrWidth);
2240     CASE_AND_STREAM(s, SHT_HIUSER   , kStrWidth);
2241     default:
2242         s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
2243         break;
2244     }
2245 }
2246 
2247 //----------------------------------------------------------------------
2248 // DumpELFSectionHeader_sh_flags
2249 //
2250 // Dump an token value for the ELF section header member sh_flags
2251 //----------------------------------------------------------------------
2252 void
2253 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
2254 {
2255     *s  << ((sh_flags & SHF_WRITE) ? "WRITE" : "     ")
2256         << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
2257         << ((sh_flags & SHF_ALLOC) ? "ALLOC" : "     ")
2258         << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
2259         << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : "         ");
2260 }
2261 
2262 //----------------------------------------------------------------------
2263 // DumpELFSectionHeaders
2264 //
2265 // Dump all of the ELF section header to the specified output stream
2266 //----------------------------------------------------------------------
2267 void
2268 ObjectFileELF::DumpELFSectionHeaders(Stream *s)
2269 {
2270     if (!ParseSectionHeaders())
2271         return;
2272 
2273     s->PutCString("Section Headers\n");
2274     s->PutCString("IDX  name     type         flags                            "
2275                   "addr     offset   size     link     info     addralgn "
2276                   "entsize  Name\n");
2277     s->PutCString("==== -------- ------------ -------------------------------- "
2278                   "-------- -------- -------- -------- -------- -------- "
2279                   "-------- ====================\n");
2280 
2281     uint32_t idx = 0;
2282     for (SectionHeaderCollConstIter I = m_section_headers.begin();
2283          I != m_section_headers.end(); ++I, ++idx)
2284     {
2285         s->Printf("[%2u] ", idx);
2286         ObjectFileELF::DumpELFSectionHeader(s, *I);
2287         const char* section_name = I->section_name.AsCString("");
2288         if (section_name)
2289             *s << ' ' << section_name << "\n";
2290     }
2291 }
2292 
2293 void
2294 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
2295 {
2296     size_t num_modules = ParseDependentModules();
2297 
2298     if (num_modules > 0)
2299     {
2300         s->PutCString("Dependent Modules:\n");
2301         for (unsigned i = 0; i < num_modules; ++i)
2302         {
2303             const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
2304             s->Printf("   %s\n", spec.GetFilename().GetCString());
2305         }
2306     }
2307 }
2308 
2309 bool
2310 ObjectFileELF::GetArchitecture (ArchSpec &arch)
2311 {
2312     if (!ParseHeader())
2313         return false;
2314 
2315     arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
2316     arch.GetTriple().setOSName (Host::GetOSString().GetCString());
2317     arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
2318     return true;
2319 }
2320 
2321 ObjectFile::Type
2322 ObjectFileELF::CalculateType()
2323 {
2324     switch (m_header.e_type)
2325     {
2326         case llvm::ELF::ET_NONE:
2327             // 0 - No file type
2328             return eTypeUnknown;
2329 
2330         case llvm::ELF::ET_REL:
2331             // 1 - Relocatable file
2332             return eTypeObjectFile;
2333 
2334         case llvm::ELF::ET_EXEC:
2335             // 2 - Executable file
2336             return eTypeExecutable;
2337 
2338         case llvm::ELF::ET_DYN:
2339             // 3 - Shared object file
2340             return eTypeSharedLibrary;
2341 
2342         case ET_CORE:
2343             // 4 - Core file
2344             return eTypeCoreFile;
2345 
2346         default:
2347             break;
2348     }
2349     return eTypeUnknown;
2350 }
2351 
2352 ObjectFile::Strata
2353 ObjectFileELF::CalculateStrata()
2354 {
2355     switch (m_header.e_type)
2356     {
2357         case llvm::ELF::ET_NONE:
2358             // 0 - No file type
2359             return eStrataUnknown;
2360 
2361         case llvm::ELF::ET_REL:
2362             // 1 - Relocatable file
2363             return eStrataUnknown;
2364 
2365         case llvm::ELF::ET_EXEC:
2366             // 2 - Executable file
2367             // TODO: is there any way to detect that an executable is a kernel
2368             // related executable by inspecting the program headers, section
2369             // headers, symbols, or any other flag bits???
2370             return eStrataUser;
2371 
2372         case llvm::ELF::ET_DYN:
2373             // 3 - Shared object file
2374             // TODO: is there any way to detect that an shared library is a kernel
2375             // related executable by inspecting the program headers, section
2376             // headers, symbols, or any other flag bits???
2377             return eStrataUnknown;
2378 
2379         case ET_CORE:
2380             // 4 - Core file
2381             // TODO: is there any way to detect that an core file is a kernel
2382             // related executable by inspecting the program headers, section
2383             // headers, symbols, or any other flag bits???
2384             return eStrataUnknown;
2385 
2386         default:
2387             break;
2388     }
2389     return eStrataUnknown;
2390 }
2391 
2392