1 //===-- ObjectFileELF.h --------------------------------------- -*- 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 #ifndef liblldb_ObjectFileELF_h_
11 #define liblldb_ObjectFileELF_h_
12 
13 // C Includes
14 #include <stdint.h>
15 
16 // C++ Includes
17 #include <vector>
18 
19 // Other libraries and framework includes
20 // Project includes
21 #include "lldb/lldb-private.h"
22 #include "lldb/Host/FileSpec.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Core/UUID.h"
25 #include "lldb/Core/ArchSpec.h"
26 
27 #include "ELFHeader.h"
28 
29 struct ELFNote
30 {
31     elf::elf_word n_namesz;
32     elf::elf_word n_descsz;
33     elf::elf_word n_type;
34 
35     std::string n_name;
36 
37     ELFNote() : n_namesz(0), n_descsz(0), n_type(0)
38     {
39     }
40 
41     /// Parse an ELFNote entry from the given DataExtractor starting at position
42     /// \p offset.
43     ///
44     /// @param[in] data
45     ///    The DataExtractor to read from.
46     ///
47     /// @param[in,out] offset
48     ///    Pointer to an offset in the data.  On return the offset will be
49     ///    advanced by the number of bytes read.
50     ///
51     /// @return
52     ///    True if the ELFRel entry was successfully read and false otherwise.
53     bool
54     Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
55 };
56 
57 //------------------------------------------------------------------------------
58 /// @class ObjectFileELF
59 /// @brief Generic ELF object file reader.
60 ///
61 /// This class provides a generic ELF (32/64 bit) reader plugin implementing the
62 /// ObjectFile protocol.
63 class ObjectFileELF :
64     public lldb_private::ObjectFile
65 {
66 public:
67     ~ObjectFileELF() override;
68 
69     //------------------------------------------------------------------
70     // Static Functions
71     //------------------------------------------------------------------
72     static void
73     Initialize();
74 
75     static void
76     Terminate();
77 
78     static lldb_private::ConstString
79     GetPluginNameStatic();
80 
81     static const char *
82     GetPluginDescriptionStatic();
83 
84     static lldb_private::ObjectFile *
85     CreateInstance(const lldb::ModuleSP &module_sp,
86                    lldb::DataBufferSP& data_sp,
87                    lldb::offset_t data_offset,
88                    const lldb_private::FileSpec* file,
89                    lldb::offset_t file_offset,
90                    lldb::offset_t length);
91 
92     static lldb_private::ObjectFile *
93     CreateMemoryInstance (const lldb::ModuleSP &module_sp,
94                           lldb::DataBufferSP& data_sp,
95                           const lldb::ProcessSP &process_sp,
96                           lldb::addr_t header_addr);
97 
98     static size_t
99     GetModuleSpecifications (const lldb_private::FileSpec& file,
100                              lldb::DataBufferSP& data_sp,
101                              lldb::offset_t data_offset,
102                              lldb::offset_t file_offset,
103                              lldb::offset_t length,
104                              lldb_private::ModuleSpecList &specs);
105 
106     static bool
107     MagicBytesMatch (lldb::DataBufferSP& data_sp,
108                      lldb::addr_t offset,
109                      lldb::addr_t length);
110 
111     //------------------------------------------------------------------
112     // PluginInterface protocol
113     //------------------------------------------------------------------
114     lldb_private::ConstString
115     GetPluginName() override;
116 
117     uint32_t
118     GetPluginVersion() override;
119 
120     //------------------------------------------------------------------
121     // ObjectFile Protocol.
122     //------------------------------------------------------------------
123     bool
124     ParseHeader() override;
125 
126     bool
127     SetLoadAddress (lldb_private::Target &target,
128                     lldb::addr_t value,
129                     bool value_is_offset) override;
130 
131     lldb::ByteOrder
132     GetByteOrder() const override;
133 
134     bool
135     IsExecutable () const override;
136 
137     uint32_t
138     GetAddressByteSize() const override;
139 
140     lldb::AddressClass
141     GetAddressClass (lldb::addr_t file_addr) override;
142 
143     lldb_private::Symtab *
144     GetSymtab() override;
145 
146     lldb_private::Symbol *
147     ResolveSymbolForAddress(const lldb_private::Address& so_addr, bool verify_unique) override;
148 
149     bool
150     IsStripped () override;
151 
152     void
153     CreateSections (lldb_private::SectionList &unified_section_list) override;
154 
155     void
156     Dump(lldb_private::Stream *s) override;
157 
158     bool
159     GetArchitecture (lldb_private::ArchSpec &arch) override;
160 
161     bool
162     GetUUID(lldb_private::UUID* uuid) override;
163 
164     lldb_private::FileSpecList
165     GetDebugSymbolFilePaths() override;
166 
167     uint32_t
168     GetDependentModules(lldb_private::FileSpecList& files) override;
169 
170     lldb_private::Address
171     GetImageInfoAddress(lldb_private::Target *target) override;
172 
173     lldb_private::Address
174     GetEntryPointAddress () override;
175 
176     ObjectFile::Type
177     CalculateType() override;
178 
179     ObjectFile::Strata
180     CalculateStrata() override;
181 
182     // Returns number of program headers found in the ELF file.
183     size_t
184     GetProgramHeaderCount();
185 
186     // Returns the program header with the given index.
187     const elf::ELFProgramHeader *
188     GetProgramHeaderByIndex(lldb::user_id_t id);
189 
190     // Returns segment data for the given index.
191     lldb_private::DataExtractor
192     GetSegmentDataByIndex(lldb::user_id_t id);
193 
194     std::string
195     StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
196 
197 private:
198     ObjectFileELF(const lldb::ModuleSP &module_sp,
199                   lldb::DataBufferSP& data_sp,
200                   lldb::offset_t data_offset,
201                   const lldb_private::FileSpec* file,
202                   lldb::offset_t offset,
203                   lldb::offset_t length);
204 
205     ObjectFileELF (const lldb::ModuleSP &module_sp,
206                    lldb::DataBufferSP& header_data_sp,
207                    const lldb::ProcessSP &process_sp,
208                    lldb::addr_t header_addr);
209 
210     typedef std::vector<elf::ELFProgramHeader>  ProgramHeaderColl;
211     typedef ProgramHeaderColl::iterator         ProgramHeaderCollIter;
212     typedef ProgramHeaderColl::const_iterator   ProgramHeaderCollConstIter;
213 
214     struct ELFSectionHeaderInfo : public elf::ELFSectionHeader
215     {
216         lldb_private::ConstString section_name;
217     };
218 
219     typedef std::vector<ELFSectionHeaderInfo>   SectionHeaderColl;
220     typedef SectionHeaderColl::iterator         SectionHeaderCollIter;
221     typedef SectionHeaderColl::const_iterator   SectionHeaderCollConstIter;
222 
223     typedef std::vector<elf::ELFDynamic>        DynamicSymbolColl;
224     typedef DynamicSymbolColl::iterator         DynamicSymbolCollIter;
225     typedef DynamicSymbolColl::const_iterator   DynamicSymbolCollConstIter;
226 
227     typedef std::map<lldb::addr_t, lldb::AddressClass> FileAddressToAddressClassMap;
228 
229     /// Version of this reader common to all plugins based on this class.
230     static const uint32_t m_plugin_version = 1;
231     static const uint32_t g_core_uuid_magic;
232 
233     /// ELF file header.
234     elf::ELFHeader m_header;
235 
236     /// ELF build ID.
237     lldb_private::UUID m_uuid;
238 
239     /// ELF .gnu_debuglink file and crc data if available.
240     std::string m_gnu_debuglink_file;
241     uint32_t m_gnu_debuglink_crc;
242 
243     /// Collection of program headers.
244     ProgramHeaderColl m_program_headers;
245 
246     /// Collection of section headers.
247     SectionHeaderColl m_section_headers;
248 
249     /// Collection of symbols from the dynamic table.
250     DynamicSymbolColl m_dynamic_symbols;
251 
252     /// List of file specifications corresponding to the modules (shared
253     /// libraries) on which this object file depends.
254     mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
255 
256     /// Cached value of the entry point for this module.
257     lldb_private::Address  m_entry_point_address;
258 
259     /// The architecture detected from parsing elf file contents.
260     lldb_private::ArchSpec m_arch_spec;
261 
262     /// The address class for each symbol in the elf file
263     FileAddressToAddressClassMap m_address_class_map;
264 
265     /// Returns a 1 based index of the given section header.
266     size_t
267     SectionIndex(const SectionHeaderCollIter &I);
268 
269     /// Returns a 1 based index of the given section header.
270     size_t
271     SectionIndex(const SectionHeaderCollConstIter &I) const;
272 
273     // Parses the ELF program headers.
274     static size_t
275     GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
276                          lldb_private::DataExtractor &data,
277                          const elf::ELFHeader &header);
278 
279     // Finds PT_NOTE segments and calculates their crc sum.
280     static uint32_t
281     CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl& program_headers,
282                                    lldb_private::DataExtractor &data);
283 
284     /// Parses all section headers present in this object file and populates
285     /// m_program_headers.  This method will compute the header list only once.
286     /// Returns the number of headers parsed.
287     size_t
288     ParseProgramHeaders();
289 
290     /// Parses all section headers present in this object file and populates
291     /// m_section_headers.  This method will compute the header list only once.
292     /// Returns the number of headers parsed.
293     size_t
294     ParseSectionHeaders();
295 
296     /// Parses the elf section headers and returns the uuid, debug link name, crc, archspec.
297     static size_t
298     GetSectionHeaderInfo(SectionHeaderColl &section_headers,
299                          lldb_private::DataExtractor &data,
300                          const elf::ELFHeader &header,
301                          lldb_private::UUID &uuid,
302                          std::string &gnu_debuglink_file,
303                          uint32_t &gnu_debuglink_crc,
304                          lldb_private::ArchSpec &arch_spec);
305 
306     /// Scans the dynamic section and locates all dependent modules (shared
307     /// libraries) populating m_filespec_ap.  This method will compute the
308     /// dependent module list only once.  Returns the number of dependent
309     /// modules parsed.
310     size_t
311     ParseDependentModules();
312 
313     /// Parses the dynamic symbol table and populates m_dynamic_symbols.  The
314     /// vector retains the order as found in the object file.  Returns the
315     /// number of dynamic symbols parsed.
316     size_t
317     ParseDynamicSymbols();
318 
319     /// Populates m_symtab_ap will all non-dynamic linker symbols.  This method
320     /// will parse the symbols only once.  Returns the number of symbols parsed.
321     unsigned
322     ParseSymbolTable(lldb_private::Symtab *symbol_table,
323                      lldb::user_id_t start_id,
324                      lldb_private::Section *symtab);
325 
326     /// Helper routine for ParseSymbolTable().
327     unsigned
328     ParseSymbols(lldb_private::Symtab *symbol_table,
329                  lldb::user_id_t start_id,
330                  lldb_private::SectionList *section_list,
331                  const size_t num_symbols,
332                  const lldb_private::DataExtractor &symtab_data,
333                  const lldb_private::DataExtractor &strtab_data);
334 
335     /// Scans the relocation entries and adds a set of artificial symbols to the
336     /// given symbol table for each PLT slot.  Returns the number of symbols
337     /// added.
338     unsigned
339     ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
340                            lldb::user_id_t start_id,
341                            const ELFSectionHeaderInfo *rela_hdr,
342                            lldb::user_id_t section_id);
343 
344     /// Relocates debug sections
345     unsigned
346     RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr, lldb::user_id_t rel_id);
347 
348     unsigned
349     RelocateSection(lldb_private::Symtab* symtab, const elf::ELFHeader *hdr, const elf::ELFSectionHeader *rel_hdr,
350                     const elf::ELFSectionHeader *symtab_hdr, const elf::ELFSectionHeader *debug_hdr,
351                     lldb_private::DataExtractor &rel_data, lldb_private::DataExtractor &symtab_data,
352                     lldb_private::DataExtractor &debug_data, lldb_private::Section* rel_section);
353 
354     /// Loads the section name string table into m_shstr_data.  Returns the
355     /// number of bytes constituting the table.
356     size_t
357     GetSectionHeaderStringTable();
358 
359     /// Utility method for looking up a section given its name.  Returns the
360     /// index of the corresponding section or zero if no section with the given
361     /// name can be found (note that section indices are always 1 based, and so
362     /// section index 0 is never valid).
363     lldb::user_id_t
364     GetSectionIndexByName(const char *name);
365 
366     // Returns the ID of the first section that has the given type.
367     lldb::user_id_t
368     GetSectionIndexByType(unsigned type);
369 
370     /// Returns the section header with the given id or NULL.
371     const ELFSectionHeaderInfo *
372     GetSectionHeaderByIndex(lldb::user_id_t id);
373 
374     /// @name  ELF header dump routines
375     //@{
376     static void
377     DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
378 
379     static void
380     DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
381                                   unsigned char ei_data);
382 
383     static void
384     DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
385     //@}
386 
387     /// @name ELF program header dump routines
388     //@{
389     void
390     DumpELFProgramHeaders(lldb_private::Stream *s);
391 
392     static void
393     DumpELFProgramHeader(lldb_private::Stream *s,
394                          const elf::ELFProgramHeader &ph);
395 
396     static void
397     DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
398 
399     static void
400     DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
401                                  elf::elf_word p_flags);
402     //@}
403 
404     /// @name ELF section header dump routines
405     //@{
406     void
407     DumpELFSectionHeaders(lldb_private::Stream *s);
408 
409     static void
410     DumpELFSectionHeader(lldb_private::Stream *s,
411                          const ELFSectionHeaderInfo& sh);
412 
413     static void
414     DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
415                                  elf::elf_word sh_type);
416 
417     static void
418     DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
419                                   elf::elf_xword sh_flags);
420     //@}
421 
422     /// ELF dependent module dump routine.
423     void
424     DumpDependentModules(lldb_private::Stream *s);
425 
426     const elf::ELFDynamic *
427     FindDynamicSymbol(unsigned tag);
428 
429     unsigned
430     PLTRelocationType();
431 
432     static lldb_private::Error
433     RefineModuleDetailsFromNote (lldb_private::DataExtractor &data, lldb_private::ArchSpec &arch_spec, lldb_private::UUID &uuid);
434 };
435 
436 #endif // liblldb_ObjectFileELF_h_
437