1 //===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
11 
12 #include <stdint.h>
13 
14 #include <vector>
15 
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/ArchSpec.h"
18 #include "lldb/Utility/FileSpec.h"
19 #include "lldb/Utility/UUID.h"
20 #include "lldb/lldb-private.h"
21 
22 #include "ELFHeader.h"
23 
24 struct ELFNote {
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   /// Parse an ELFNote entry from the given DataExtractor starting at position
34   /// \p offset.
35   ///
36   /// \param[in] data
37   ///    The DataExtractor to read from.
38   ///
39   /// \param[in,out] offset
40   ///    Pointer to an offset in the data.  On return the offset will be
41   ///    advanced by the number of bytes read.
42   ///
43   /// \return
44   ///    True if the ELFRel entry was successfully read and false otherwise.
45   bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
46 
47   size_t GetByteSize() const {
48     return 12 + llvm::alignTo(n_namesz, 4) + llvm::alignTo(n_descsz, 4);
49   }
50 };
51 
52 /// \class ObjectFileELF
53 /// Generic ELF object file reader.
54 ///
55 /// This class provides a generic ELF (32/64 bit) reader plugin implementing
56 /// the ObjectFile protocol.
57 class ObjectFileELF : public lldb_private::ObjectFile {
58 public:
59   // Static Functions
60   static void Initialize();
61 
62   static void Terminate();
63 
64   static lldb_private::ConstString GetPluginNameStatic();
65 
66   static const char *GetPluginDescriptionStatic();
67 
68   static lldb_private::ObjectFile *
69   CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
70                  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
71                  lldb::offset_t file_offset, lldb::offset_t length);
72 
73   static lldb_private::ObjectFile *CreateMemoryInstance(
74       const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
75       const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
76 
77   static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
78                                         lldb::DataBufferSP &data_sp,
79                                         lldb::offset_t data_offset,
80                                         lldb::offset_t file_offset,
81                                         lldb::offset_t length,
82                                         lldb_private::ModuleSpecList &specs);
83 
84   static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
85                               lldb::addr_t length);
86 
87   // PluginInterface protocol
88   lldb_private::ConstString GetPluginName() override;
89 
90   uint32_t GetPluginVersion() override;
91 
92   // LLVM RTTI support
93   static char ID;
94   bool isA(const void *ClassID) const override {
95     return ClassID == &ID || ObjectFile::isA(ClassID);
96   }
97   static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
98 
99   // ObjectFile Protocol.
100   bool ParseHeader() override;
101 
102   bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
103                       bool value_is_offset) override;
104 
105   lldb::ByteOrder GetByteOrder() const override;
106 
107   bool IsExecutable() const override;
108 
109   uint32_t GetAddressByteSize() const override;
110 
111   lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
112 
113   lldb_private::Symtab *GetSymtab() override;
114 
115   bool IsStripped() override;
116 
117   void CreateSections(lldb_private::SectionList &unified_section_list) override;
118 
119   void Dump(lldb_private::Stream *s) override;
120 
121   lldb_private::ArchSpec GetArchitecture() override;
122 
123   lldb_private::UUID GetUUID() override;
124 
125   /// Return the contents of the .gnu_debuglink section, if the object file
126   /// contains it.
127   llvm::Optional<lldb_private::FileSpec> GetDebugLink();
128 
129   uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
130 
131   lldb_private::Address
132   GetImageInfoAddress(lldb_private::Target *target) override;
133 
134   lldb_private::Address GetEntryPointAddress() override;
135 
136   lldb_private::Address GetBaseAddress() override;
137 
138   ObjectFile::Type CalculateType() override;
139 
140   ObjectFile::Strata CalculateStrata() override;
141 
142   size_t ReadSectionData(lldb_private::Section *section,
143                          lldb::offset_t section_offset, void *dst,
144                          size_t dst_len) override;
145 
146   size_t ReadSectionData(lldb_private::Section *section,
147                          lldb_private::DataExtractor &section_data) override;
148 
149   llvm::ArrayRef<elf::ELFProgramHeader> ProgramHeaders();
150   lldb_private::DataExtractor GetSegmentData(const elf::ELFProgramHeader &H);
151 
152   llvm::StringRef
153   StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
154 
155   void RelocateSection(lldb_private::Section *section) override;
156 
157 protected:
158 
159   std::vector<LoadableData>
160   GetLoadableData(lldb_private::Target &target) override;
161 
162 private:
163   ObjectFileELF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
164                 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
165                 lldb::offset_t offset, lldb::offset_t length);
166 
167   ObjectFileELF(const lldb::ModuleSP &module_sp,
168                 lldb::DataBufferSP &header_data_sp,
169                 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
170 
171   typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
172 
173   struct ELFSectionHeaderInfo : public elf::ELFSectionHeader {
174     lldb_private::ConstString section_name;
175   };
176 
177   typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
178   typedef SectionHeaderColl::iterator SectionHeaderCollIter;
179   typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
180 
181   typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
182   typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
183   typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
184 
185   typedef std::map<lldb::addr_t, lldb_private::AddressClass>
186       FileAddressToAddressClassMap;
187 
188   /// Version of this reader common to all plugins based on this class.
189   static const uint32_t m_plugin_version = 1;
190   static const uint32_t g_core_uuid_magic;
191 
192   /// ELF file header.
193   elf::ELFHeader m_header;
194 
195   /// ELF build ID.
196   lldb_private::UUID m_uuid;
197 
198   /// ELF .gnu_debuglink file and crc data if available.
199   std::string m_gnu_debuglink_file;
200   uint32_t m_gnu_debuglink_crc = 0;
201 
202   /// Collection of program headers.
203   ProgramHeaderColl m_program_headers;
204 
205   /// Collection of section headers.
206   SectionHeaderColl m_section_headers;
207 
208   /// Collection of symbols from the dynamic table.
209   DynamicSymbolColl m_dynamic_symbols;
210 
211   /// Object file parsed from .gnu_debugdata section (\sa
212   /// GetGnuDebugDataObjectFile())
213   std::shared_ptr<ObjectFileELF> m_gnu_debug_data_object_file;
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_up;
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 the index of the given section header.
229   size_t SectionIndex(const SectionHeaderCollIter &I);
230 
231   /// Returns the 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 true iff the headers have been successfully parsed.
247   bool 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   lldb::SectionType GetSectionType(const ELFSectionHeaderInfo &H) const;
255 
256   static void ParseARMAttributes(lldb_private::DataExtractor &data,
257                                  uint64_t length,
258                                  lldb_private::ArchSpec &arch_spec);
259 
260   /// Parses the elf section headers and returns the uuid, debug link name,
261   /// crc, archspec.
262   static size_t GetSectionHeaderInfo(SectionHeaderColl &section_headers,
263                                      lldb_private::DataExtractor &object_data,
264                                      const elf::ELFHeader &header,
265                                      lldb_private::UUID &uuid,
266                                      std::string &gnu_debuglink_file,
267                                      uint32_t &gnu_debuglink_crc,
268                                      lldb_private::ArchSpec &arch_spec);
269 
270   /// Scans the dynamic section and locates all dependent modules (shared
271   /// libraries) populating m_filespec_up.  This method will compute the
272   /// dependent module list only once.  Returns the number of dependent
273   /// modules parsed.
274   size_t ParseDependentModules();
275 
276   /// Parses the dynamic symbol table and populates m_dynamic_symbols.  The
277   /// vector retains the order as found in the object file.  Returns the
278   /// number of dynamic symbols parsed.
279   size_t ParseDynamicSymbols();
280 
281   /// Populates m_symtab_up will all non-dynamic linker symbols.  This method
282   /// will parse the symbols only once.  Returns the number of symbols parsed.
283   unsigned ParseSymbolTable(lldb_private::Symtab *symbol_table,
284                             lldb::user_id_t start_id,
285                             lldb_private::Section *symtab);
286 
287   /// Helper routine for ParseSymbolTable().
288   unsigned ParseSymbols(lldb_private::Symtab *symbol_table,
289                         lldb::user_id_t start_id,
290                         lldb_private::SectionList *section_list,
291                         const size_t num_symbols,
292                         const lldb_private::DataExtractor &symtab_data,
293                         const lldb_private::DataExtractor &strtab_data);
294 
295   /// Scans the relocation entries and adds a set of artificial symbols to the
296   /// given symbol table for each PLT slot.  Returns the number of symbols
297   /// added.
298   unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
299                                   lldb::user_id_t start_id,
300                                   const ELFSectionHeaderInfo *rela_hdr,
301                                   lldb::user_id_t section_id);
302 
303   void ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
304                           lldb_private::DWARFCallFrameInfo *eh_frame);
305 
306   /// Relocates debug sections
307   unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr,
308                                  lldb::user_id_t rel_id,
309                                  lldb_private::Symtab *thetab);
310 
311   unsigned ApplyRelocations(lldb_private::Symtab *symtab,
312                             const elf::ELFHeader *hdr,
313                             const elf::ELFSectionHeader *rel_hdr,
314                             const elf::ELFSectionHeader *symtab_hdr,
315                             const elf::ELFSectionHeader *debug_hdr,
316                             lldb_private::DataExtractor &rel_data,
317                             lldb_private::DataExtractor &symtab_data,
318                             lldb_private::DataExtractor &debug_data,
319                             lldb_private::Section *rel_section);
320 
321   /// Loads the section name string table into m_shstr_data.  Returns the
322   /// number of bytes constituting the table.
323   size_t GetSectionHeaderStringTable();
324 
325   /// Utility method for looking up a section given its name.  Returns the
326   /// index of the corresponding section or zero if no section with the given
327   /// name can be found (note that section indices are always 1 based, and so
328   /// section index 0 is never valid).
329   lldb::user_id_t GetSectionIndexByName(const char *name);
330 
331   /// Returns the section header with the given id or NULL.
332   const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id);
333 
334   /// \name  ELF header dump routines
335   //@{
336   static void DumpELFHeader(lldb_private::Stream *s,
337                             const elf::ELFHeader &header);
338 
339   static void DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
340                                             unsigned char ei_data);
341 
342   static void DumpELFHeader_e_type(lldb_private::Stream *s,
343                                    elf::elf_half e_type);
344   //@}
345 
346   /// \name ELF program header dump routines
347   //@{
348   void DumpELFProgramHeaders(lldb_private::Stream *s);
349 
350   static void DumpELFProgramHeader(lldb_private::Stream *s,
351                                    const elf::ELFProgramHeader &ph);
352 
353   static void DumpELFProgramHeader_p_type(lldb_private::Stream *s,
354                                           elf::elf_word p_type);
355 
356   static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
357                                            elf::elf_word p_flags);
358   //@}
359 
360   /// \name ELF section header dump routines
361   //@{
362   void DumpELFSectionHeaders(lldb_private::Stream *s);
363 
364   static void DumpELFSectionHeader(lldb_private::Stream *s,
365                                    const ELFSectionHeaderInfo &sh);
366 
367   static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
368                                            elf::elf_word sh_type);
369 
370   static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
371                                             elf::elf_xword sh_flags);
372   //@}
373 
374   /// ELF dependent module dump routine.
375   void DumpDependentModules(lldb_private::Stream *s);
376 
377   const elf::ELFDynamic *FindDynamicSymbol(unsigned tag);
378 
379   unsigned PLTRelocationType();
380 
381   static lldb_private::Status
382   RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
383                               lldb_private::ArchSpec &arch_spec,
384                               lldb_private::UUID &uuid);
385 
386   bool AnySegmentHasPhysicalAddress();
387 
388   /// Takes the .gnu_debugdata and returns the decompressed object file that is
389   /// stored within that section.
390   ///
391   /// \returns either the decompressed object file stored within the
392   /// .gnu_debugdata section or \c nullptr if an error occured or if there's no
393   /// section with that name.
394   std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile();
395 };
396 
397 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
398