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