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 <cstdint>
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 = 0;
26   elf::elf_word n_descsz = 0;
27   elf::elf_word n_type = 0;
28 
29   std::string n_name;
30 
31   ELFNote() = default;
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 llvm::StringRef GetPluginNameStatic() { return "elf"; }
65 
66   static llvm::StringRef GetPluginDescriptionStatic() {
67     return "ELF object file reader.";
68   }
69 
70   static lldb_private::ObjectFile *
71   CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
72                  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
73                  lldb::offset_t file_offset, lldb::offset_t length);
74 
75   static lldb_private::ObjectFile *CreateMemoryInstance(
76       const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
77       const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
78 
79   static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
80                                         lldb::DataBufferSP &data_sp,
81                                         lldb::offset_t data_offset,
82                                         lldb::offset_t file_offset,
83                                         lldb::offset_t length,
84                                         lldb_private::ModuleSpecList &specs);
85 
86   static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
87                               lldb::addr_t length);
88 
89   // PluginInterface protocol
90   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
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   void ParseSymtab(lldb_private::Symtab &symtab) 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 the symbol table with all non-dynamic linker symbols.  This
282   /// method will parse the symbols only once.  Returns the number of symbols
283   /// parsed.
284   unsigned ParseSymbolTable(lldb_private::Symtab *symbol_table,
285                             lldb::user_id_t start_id,
286                             lldb_private::Section *symtab);
287 
288   /// Helper routine for ParseSymbolTable().
289   unsigned ParseSymbols(lldb_private::Symtab *symbol_table,
290                         lldb::user_id_t start_id,
291                         lldb_private::SectionList *section_list,
292                         const size_t num_symbols,
293                         const lldb_private::DataExtractor &symtab_data,
294                         const lldb_private::DataExtractor &strtab_data);
295 
296   /// Scans the relocation entries and adds a set of artificial symbols to the
297   /// given symbol table for each PLT slot.  Returns the number of symbols
298   /// added.
299   unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
300                                   lldb::user_id_t start_id,
301                                   const ELFSectionHeaderInfo *rela_hdr,
302                                   lldb::user_id_t section_id);
303 
304   void ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
305                           lldb_private::DWARFCallFrameInfo *eh_frame);
306 
307   /// Relocates debug sections
308   unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr,
309                                  lldb::user_id_t rel_id,
310                                  lldb_private::Symtab *thetab);
311 
312   unsigned ApplyRelocations(lldb_private::Symtab *symtab,
313                             const elf::ELFHeader *hdr,
314                             const elf::ELFSectionHeader *rel_hdr,
315                             const elf::ELFSectionHeader *symtab_hdr,
316                             const elf::ELFSectionHeader *debug_hdr,
317                             lldb_private::DataExtractor &rel_data,
318                             lldb_private::DataExtractor &symtab_data,
319                             lldb_private::DataExtractor &debug_data,
320                             lldb_private::Section *rel_section);
321 
322   /// Loads the section name string table into m_shstr_data.  Returns the
323   /// number of bytes constituting the table.
324   size_t GetSectionHeaderStringTable();
325 
326   /// Utility method for looking up a section given its name.  Returns the
327   /// index of the corresponding section or zero if no section with the given
328   /// name can be found (note that section indices are always 1 based, and so
329   /// section index 0 is never valid).
330   lldb::user_id_t GetSectionIndexByName(const char *name);
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   bool AnySegmentHasPhysicalAddress();
388 
389   /// Takes the .gnu_debugdata and returns the decompressed object file that is
390   /// stored within that section.
391   ///
392   /// \returns either the decompressed object file stored within the
393   /// .gnu_debugdata section or \c nullptr if an error occured or if there's no
394   /// section with that name.
395   std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile();
396 };
397 
398 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
399