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