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