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