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 
15 #include <vector>
16 
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/Utility/FileSpec.h"
20 #include "lldb/Utility/UUID.h"
21 #include "lldb/lldb-private.h"
22 
23 #include "ELFHeader.h"
24 
25 struct ELFNote {
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   /// Parse an ELFNote entry from the given DataExtractor starting at position
35   /// \p offset.
36   ///
37   /// @param[in] data
38   ///    The DataExtractor to read from.
39   ///
40   /// @param[in,out] offset
41   ///    Pointer to an offset in the data.  On return the offset will be
42   ///    advanced by the number of bytes read.
43   ///
44   /// @return
45   ///    True if the ELFRel entry was successfully read and false otherwise.
46   bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
47 
48   size_t GetByteSize() const {
49     return 12 + llvm::alignTo(n_namesz, 4) + llvm::alignTo(n_descsz, 4);
50   }
51 };
52 
53 //------------------------------------------------------------------------------
54 /// @class ObjectFileELF
55 /// Generic ELF object file reader.
56 ///
57 /// This class provides a generic ELF (32/64 bit) reader plugin implementing
58 /// the ObjectFile protocol.
59 class ObjectFileELF : public lldb_private::ObjectFile {
60 public:
61   ~ObjectFileELF() override;
62 
63   //------------------------------------------------------------------
64   // Static Functions
65   //------------------------------------------------------------------
66   static void Initialize();
67 
68   static void Terminate();
69 
70   static lldb_private::ConstString GetPluginNameStatic();
71 
72   static const char *GetPluginDescriptionStatic();
73 
74   static lldb_private::ObjectFile *
75   CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
76                  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
77                  lldb::offset_t file_offset, lldb::offset_t length);
78 
79   static lldb_private::ObjectFile *CreateMemoryInstance(
80       const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
81       const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
82 
83   static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
84                                         lldb::DataBufferSP &data_sp,
85                                         lldb::offset_t data_offset,
86                                         lldb::offset_t file_offset,
87                                         lldb::offset_t length,
88                                         lldb_private::ModuleSpecList &specs);
89 
90   static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
91                               lldb::addr_t length);
92 
93   //------------------------------------------------------------------
94   // PluginInterface protocol
95   //------------------------------------------------------------------
96   lldb_private::ConstString GetPluginName() override;
97 
98   uint32_t GetPluginVersion() override;
99 
100   //------------------------------------------------------------------
101   // ObjectFile Protocol.
102   //------------------------------------------------------------------
103   bool ParseHeader() override;
104 
105   bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
106                       bool value_is_offset) override;
107 
108   lldb::ByteOrder GetByteOrder() const override;
109 
110   bool IsExecutable() const override;
111 
112   uint32_t GetAddressByteSize() const override;
113 
114   lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
115 
116   lldb_private::Symtab *GetSymtab() override;
117 
118   bool IsStripped() override;
119 
120   void CreateSections(lldb_private::SectionList &unified_section_list) override;
121 
122   void Dump(lldb_private::Stream *s) override;
123 
124   lldb_private::ArchSpec GetArchitecture() override;
125 
126   bool GetUUID(lldb_private::UUID *uuid) override;
127 
128   lldb_private::FileSpecList GetDebugSymbolFilePaths() override;
129 
130   uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
131 
132   lldb_private::Address
133   GetImageInfoAddress(lldb_private::Target *target) override;
134 
135   lldb_private::Address GetEntryPointAddress() override;
136 
137   ObjectFile::Type CalculateType() override;
138 
139   ObjectFile::Strata CalculateStrata() override;
140 
141   size_t ReadSectionData(lldb_private::Section *section,
142                          lldb::offset_t section_offset, void *dst,
143                          size_t dst_len) override;
144 
145   size_t ReadSectionData(lldb_private::Section *section,
146                          lldb_private::DataExtractor &section_data) override;
147 
148   llvm::ArrayRef<elf::ELFProgramHeader> ProgramHeaders();
149   lldb_private::DataExtractor GetSegmentData(const elf::ELFProgramHeader &H);
150 
151   llvm::StringRef
152   StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
153 
154   void RelocateSection(lldb_private::Section *section) override;
155 
156 protected:
157 
158   std::vector<LoadableData>
159   GetLoadableData(lldb_private::Target &target) override;
160 
161 private:
162   ObjectFileELF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
163                 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
164                 lldb::offset_t offset, lldb::offset_t length);
165 
166   ObjectFileELF(const lldb::ModuleSP &module_sp,
167                 lldb::DataBufferSP &header_data_sp,
168                 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
169 
170   typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
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_private::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 the index of the given section header.
224   size_t SectionIndex(const SectionHeaderCollIter &I);
225 
226   /// Returns the 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 true iff the headers have been successfully parsed.
242   bool 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   lldb::SectionType GetSectionType(const ELFSectionHeaderInfo &H) const;
250 
251   static void ParseARMAttributes(lldb_private::DataExtractor &data,
252                                  uint64_t length,
253                                  lldb_private::ArchSpec &arch_spec);
254 
255   /// Parses the elf section headers and returns the uuid, debug link name,
256   /// crc, archspec.
257   static size_t GetSectionHeaderInfo(SectionHeaderColl &section_headers,
258                                      lldb_private::DataExtractor &object_data,
259                                      const elf::ELFHeader &header,
260                                      lldb_private::UUID &uuid,
261                                      std::string &gnu_debuglink_file,
262                                      uint32_t &gnu_debuglink_crc,
263                                      lldb_private::ArchSpec &arch_spec);
264 
265   /// Scans the dynamic section and locates all dependent modules (shared
266   /// libraries) populating m_filespec_ap.  This method will compute the
267   /// dependent module list only once.  Returns the number of dependent
268   /// modules parsed.
269   size_t ParseDependentModules();
270 
271   /// Parses the dynamic symbol table and populates m_dynamic_symbols.  The
272   /// vector retains the order as found in the object file.  Returns the
273   /// number of dynamic symbols parsed.
274   size_t 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 ParseSymbolTable(lldb_private::Symtab *symbol_table,
279                             lldb::user_id_t start_id,
280                             lldb_private::Section *symtab);
281 
282   /// Helper routine for ParseSymbolTable().
283   unsigned ParseSymbols(lldb_private::Symtab *symbol_table,
284                         lldb::user_id_t start_id,
285                         lldb_private::SectionList *section_list,
286                         const size_t num_symbols,
287                         const lldb_private::DataExtractor &symtab_data,
288                         const lldb_private::DataExtractor &strtab_data);
289 
290   /// Scans the relocation entries and adds a set of artificial symbols to the
291   /// given symbol table for each PLT slot.  Returns the number of symbols
292   /// added.
293   unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
294                                   lldb::user_id_t start_id,
295                                   const ELFSectionHeaderInfo *rela_hdr,
296                                   lldb::user_id_t section_id);
297 
298   void ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
299                           lldb_private::DWARFCallFrameInfo *eh_frame);
300 
301   /// Relocates debug sections
302   unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr,
303                                  lldb::user_id_t rel_id,
304                                  lldb_private::Symtab *thetab);
305 
306   unsigned ApplyRelocations(lldb_private::Symtab *symtab,
307                             const elf::ELFHeader *hdr,
308                             const elf::ELFSectionHeader *rel_hdr,
309                             const elf::ELFSectionHeader *symtab_hdr,
310                             const elf::ELFSectionHeader *debug_hdr,
311                             lldb_private::DataExtractor &rel_data,
312                             lldb_private::DataExtractor &symtab_data,
313                             lldb_private::DataExtractor &debug_data,
314                             lldb_private::Section *rel_section);
315 
316   /// Loads the section name string table into m_shstr_data.  Returns the
317   /// number of bytes constituting the table.
318   size_t GetSectionHeaderStringTable();
319 
320   /// Utility method for looking up a section given its name.  Returns the
321   /// index of the corresponding section or zero if no section with the given
322   /// name can be found (note that section indices are always 1 based, and so
323   /// section index 0 is never valid).
324   lldb::user_id_t GetSectionIndexByName(const char *name);
325 
326   // Returns the ID of the first section that has the given type.
327   lldb::user_id_t GetSectionIndexByType(unsigned type);
328 
329   /// Returns the section header with the given id or NULL.
330   const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id);
331 
332   /// @name  ELF header dump routines
333   //@{
334   static void DumpELFHeader(lldb_private::Stream *s,
335                             const elf::ELFHeader &header);
336 
337   static void DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
338                                             unsigned char ei_data);
339 
340   static void DumpELFHeader_e_type(lldb_private::Stream *s,
341                                    elf::elf_half e_type);
342   //@}
343 
344   /// @name ELF program header dump routines
345   //@{
346   void DumpELFProgramHeaders(lldb_private::Stream *s);
347 
348   static void DumpELFProgramHeader(lldb_private::Stream *s,
349                                    const elf::ELFProgramHeader &ph);
350 
351   static void DumpELFProgramHeader_p_type(lldb_private::Stream *s,
352                                           elf::elf_word p_type);
353 
354   static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
355                                            elf::elf_word p_flags);
356   //@}
357 
358   /// @name ELF section header dump routines
359   //@{
360   void DumpELFSectionHeaders(lldb_private::Stream *s);
361 
362   static void DumpELFSectionHeader(lldb_private::Stream *s,
363                                    const ELFSectionHeaderInfo &sh);
364 
365   static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
366                                            elf::elf_word sh_type);
367 
368   static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
369                                             elf::elf_xword sh_flags);
370   //@}
371 
372   /// ELF dependent module dump routine.
373   void DumpDependentModules(lldb_private::Stream *s);
374 
375   const elf::ELFDynamic *FindDynamicSymbol(unsigned tag);
376 
377   unsigned PLTRelocationType();
378 
379   static lldb_private::Status
380   RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
381                               lldb_private::ArchSpec &arch_spec,
382                               lldb_private::UUID &uuid);
383 
384   bool AnySegmentHasPhysicalAddress();
385 };
386 
387 #endif // liblldb_ObjectFileELF_h_
388