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

source code of lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h