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 lldb::ByteOrder
122     GetByteOrder() const;
123 
124     virtual bool
125     IsExecutable () const;
126 
127     virtual uint32_t
128     GetAddressByteSize() const;
129 
130     virtual lldb_private::Symtab *
131     GetSymtab();
132 
133     virtual lldb_private::Symbol *
134     ResolveSymbolForAddress(const lldb_private::Address& so_addr, bool verify_unique);
135 
136     virtual bool
137     IsStripped ();
138 
139     virtual void
140     CreateSections (lldb_private::SectionList &unified_section_list);
141 
142     virtual void
143     Dump(lldb_private::Stream *s);
144 
145     virtual bool
146     GetArchitecture (lldb_private::ArchSpec &arch);
147 
148     virtual bool
149     GetUUID(lldb_private::UUID* uuid);
150 
151     virtual lldb_private::FileSpecList
152     GetDebugSymbolFilePaths();
153 
154     virtual uint32_t
155     GetDependentModules(lldb_private::FileSpecList& files);
156 
157     virtual lldb_private::Address
158     GetImageInfoAddress(lldb_private::Target *target);
159 
160     virtual lldb_private::Address
161     GetEntryPointAddress ();
162 
163     virtual ObjectFile::Type
164     CalculateType();
165 
166     virtual ObjectFile::Strata
167     CalculateStrata();
168 
169     // Returns number of program headers found in the ELF file.
170     size_t
171     GetProgramHeaderCount();
172 
173     // Returns the program header with the given index.
174     const elf::ELFProgramHeader *
175     GetProgramHeaderByIndex(lldb::user_id_t id);
176 
177     // Returns segment data for the given index.
178     lldb_private::DataExtractor
179     GetSegmentDataByIndex(lldb::user_id_t id);
180 
181 private:
182     ObjectFileELF(const lldb::ModuleSP &module_sp,
183                   lldb::DataBufferSP& data_sp,
184                   lldb::offset_t data_offset,
185                   const lldb_private::FileSpec* file,
186                   lldb::offset_t offset,
187                   lldb::offset_t length);
188 
189     typedef std::vector<elf::ELFProgramHeader>  ProgramHeaderColl;
190     typedef ProgramHeaderColl::iterator         ProgramHeaderCollIter;
191     typedef ProgramHeaderColl::const_iterator   ProgramHeaderCollConstIter;
192 
193     struct ELFSectionHeaderInfo : public elf::ELFSectionHeader
194     {
195         lldb_private::ConstString section_name;
196     };
197     typedef std::vector<ELFSectionHeaderInfo>   SectionHeaderColl;
198     typedef SectionHeaderColl::iterator         SectionHeaderCollIter;
199     typedef SectionHeaderColl::const_iterator   SectionHeaderCollConstIter;
200 
201     typedef std::vector<elf::ELFDynamic>        DynamicSymbolColl;
202     typedef DynamicSymbolColl::iterator         DynamicSymbolCollIter;
203     typedef DynamicSymbolColl::const_iterator   DynamicSymbolCollConstIter;
204 
205     /// Version of this reader common to all plugins based on this class.
206     static const uint32_t m_plugin_version = 1;
207 
208     /// ELF file header.
209     elf::ELFHeader m_header;
210 
211     /// ELF build ID.
212     lldb_private::UUID m_uuid;
213 
214     /// ELF .gnu_debuglink file and crc data if available.
215     std::string m_gnu_debuglink_file;
216     uint32_t m_gnu_debuglink_crc;
217 
218     /// Collection of program headers.
219     ProgramHeaderColl m_program_headers;
220 
221     /// Collection of section headers.
222     SectionHeaderColl m_section_headers;
223 
224     /// Collection of symbols from the dynamic table.
225     DynamicSymbolColl m_dynamic_symbols;
226 
227     /// List of file specifications corresponding to the modules (shared
228     /// libraries) on which this object file depends.
229     mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
230 
231     /// Cached value of the entry point for this module.
232     lldb_private::Address  m_entry_point_address;
233 
234     /// Returns a 1 based index of the given section header.
235     size_t
236     SectionIndex(const SectionHeaderCollIter &I);
237 
238     /// Returns a 1 based index of the given section header.
239     size_t
240     SectionIndex(const SectionHeaderCollConstIter &I) const;
241 
242     /// Parses all section headers present in this object file and populates
243     /// m_program_headers.  This method will compute the header list only once.
244     /// Returns the number of headers parsed.
245     size_t
246     ParseProgramHeaders();
247 
248     /// Parses all section headers present in this object file and populates
249     /// m_section_headers.  This method will compute the header list only once.
250     /// Returns the number of headers parsed.
251     size_t
252     ParseSectionHeaders();
253 
254     /// Parses the elf section headers and returns the uuid, debug link name, crc.
255     static size_t
256     GetSectionHeaderInfo(SectionHeaderColl &section_headers,
257                          lldb_private::DataExtractor &data,
258                          const elf::ELFHeader &header,
259                          lldb_private::UUID &uuid,
260                          std::string &gnu_debuglink_file,
261                          uint32_t &gnu_debuglink_crc);
262 
263     /// Scans the dynamic section and locates all dependent modules (shared
264     /// libraries) populating m_filespec_ap.  This method will compute the
265     /// dependent module list only once.  Returns the number of dependent
266     /// modules parsed.
267     size_t
268     ParseDependentModules();
269 
270     /// Parses the dynamic symbol table and populates m_dynamic_symbols.  The
271     /// vector retains the order as found in the object file.  Returns the
272     /// number of dynamic symbols parsed.
273     size_t
274     ParseDynamicSymbols();
275 
276     /// Populates m_symtab_ap will all non-dynamic linker symbols.  This method
277     /// will parse the symbols only once.  Returns the number of symbols parsed.
278     unsigned
279     ParseSymbolTable(lldb_private::Symtab *symbol_table,
280                      lldb::user_id_t start_id,
281                      lldb_private::Section *symtab);
282 
283     /// Helper routine for ParseSymbolTable().
284     unsigned
285     ParseSymbols(lldb_private::Symtab *symbol_table,
286                  lldb::user_id_t start_id,
287                  lldb_private::SectionList *section_list,
288                  const size_t num_symbols,
289                  const lldb_private::DataExtractor &symtab_data,
290                  const lldb_private::DataExtractor &strtab_data);
291 
292     /// Scans the relocation entries and adds a set of artificial symbols to the
293     /// given symbol table for each PLT slot.  Returns the number of symbols
294     /// added.
295     unsigned
296     ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
297                            lldb::user_id_t start_id,
298                            const ELFSectionHeaderInfo *rela_hdr,
299                            lldb::user_id_t section_id);
300 
301     /// Returns the section header with the given id or NULL.
302     const ELFSectionHeaderInfo *
303     GetSectionHeaderByIndex(lldb::user_id_t id);
304 
305     /// @name  ELF header dump routines
306     //@{
307     static void
308     DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header);
309 
310     static void
311     DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
312                                   unsigned char ei_data);
313 
314     static void
315     DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type);
316     //@}
317 
318     /// @name ELF program header dump routines
319     //@{
320     void
321     DumpELFProgramHeaders(lldb_private::Stream *s);
322 
323     static void
324     DumpELFProgramHeader(lldb_private::Stream *s,
325                          const elf::ELFProgramHeader &ph);
326 
327     static void
328     DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type);
329 
330     static void
331     DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
332                                  elf::elf_word p_flags);
333     //@}
334 
335     /// @name ELF section header dump routines
336     //@{
337     void
338     DumpELFSectionHeaders(lldb_private::Stream *s);
339 
340     static void
341     DumpELFSectionHeader(lldb_private::Stream *s,
342                          const ELFSectionHeaderInfo& sh);
343 
344     static void
345     DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
346                                  elf::elf_word sh_type);
347 
348     static void
349     DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
350                                   elf::elf_xword sh_flags);
351     //@}
352 
353     /// ELF dependent module dump routine.
354     void
355     DumpDependentModules(lldb_private::Stream *s);
356 
357     const elf::ELFDynamic *
358     FindDynamicSymbol(unsigned tag);
359 
360     unsigned
361     PLTRelocationType();
362 };
363 
364 #endif // #ifndef liblldb_ObjectFileELF_h_
365