blob: 2a2b13243fe38b42d094f91a9e68ebaf55f1b87d [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ELFHeader.h ------------------------------------------- -*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Stephen Wilsonf325ba92010-07-13 23:07:23 +00006//
7//===----------------------------------------------------------------------===//
8//
Adrian Prantlf05b42e2019-03-11 17:09:29 +00009/// \file
Adrian Prantld8f460e2018-05-02 16:55:16 +000010/// Generic structures and typedefs for ELF files.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000011///
12/// This file provides definitions for the various entities comprising an ELF
13/// file. The structures are generic in the sense that they do not correspond
14/// to the exact binary layout of an ELF, but can be used to hold the
15/// information present in both 32 and 64 bit variants of the format. Each
Adrian Prantld8f460e2018-05-02 16:55:16 +000016/// entity provides a \c Parse method which is capable of transparently
17/// reading both 32 and 64 bit instances of the object.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000018//===----------------------------------------------------------------------===//
19
20#ifndef liblldb_ELFHeader_h_
21#define liblldb_ELFHeader_h_
22
Zachary Turner264b5d92017-06-07 03:48:56 +000023#include "llvm/BinaryFormat/ELF.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000024
25#include "lldb/lldb-enumerations.h"
Pavel Labath23ccc292017-01-31 23:09:46 +000026#include "lldb/lldb-types.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028namespace lldb_private {
Stephen Wilsonf325ba92010-07-13 23:07:23 +000029class DataExtractor;
30} // End namespace lldb_private.
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032namespace elf {
Stephen Wilsonf325ba92010-07-13 23:07:23 +000033
34//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +000035/// \name ELF type definitions.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000036///
Adrian Prantld8f460e2018-05-02 16:55:16 +000037/// Types used to represent the various components of ELF structures. All
38/// types are signed or unsigned integral types wide enough to hold values
39/// from both
Stephen Wilsonf325ba92010-07-13 23:07:23 +000040/// 32 and 64 bit ELF variants.
41//@{
42typedef uint64_t elf_addr;
43typedef uint64_t elf_off;
44typedef uint16_t elf_half;
45typedef uint32_t elf_word;
Kate Stoneb9c1b512016-09-06 20:57:50 +000046typedef int32_t elf_sword;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000047typedef uint64_t elf_size;
48typedef uint64_t elf_xword;
Kate Stoneb9c1b512016-09-06 20:57:50 +000049typedef int64_t elf_sxword;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000050//@}
51
52//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +000053/// \class ELFHeader
Adrian Prantld8f460e2018-05-02 16:55:16 +000054/// Generic representation of an ELF file header.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000055///
Adrian Prantld8f460e2018-05-02 16:55:16 +000056/// This object is used to identify the general attributes on an ELF file and
57/// to locate additional sections within the file.
Kate Stoneb9c1b512016-09-06 20:57:50 +000058struct ELFHeader {
59 unsigned char e_ident[llvm::ELF::EI_NIDENT]; ///< ELF file identification.
60 elf_addr e_entry; ///< Virtual address program entry point.
61 elf_off e_phoff; ///< File offset of program header table.
62 elf_off e_shoff; ///< File offset of section header table.
63 elf_word e_flags; ///< Processor specific flags.
64 elf_word e_version; ///< Version of object file (always 1).
65 elf_half e_type; ///< Object file type.
66 elf_half e_machine; ///< Target architecture.
67 elf_half e_ehsize; ///< Byte size of the ELF header.
68 elf_half e_phentsize; ///< Size of a program header table entry.
Pavel Labath23ccc292017-01-31 23:09:46 +000069 elf_half e_phnum_hdr; ///< Number of program header entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 elf_half e_shentsize; ///< Size of a section header table entry.
Pavel Labath23ccc292017-01-31 23:09:46 +000071 elf_half e_shnum_hdr; ///< Number of section header entries.
72 elf_half e_shstrndx_hdr; ///< String table section index.
73
74 // In some cases these numbers do not fit in 16 bits and they are
75 // stored outside of the header in section #0. Here are the actual
76 // values.
77 elf_word e_phnum; ///< Number of program header entries.
78 elf_word e_shnum; ///< Number of section header entries.
79 elf_word e_shstrndx; ///< String table section index.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 ELFHeader();
Stephen Wilsonf325ba92010-07-13 23:07:23 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 //--------------------------------------------------------------------------
84 /// Returns true if this is a 32 bit ELF file header.
85 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000086 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 /// True if this is a 32 bit ELF file header.
88 bool Is32Bit() const {
89 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32;
90 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 //--------------------------------------------------------------------------
93 /// Returns true if this is a 64 bit ELF file header.
94 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000095 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 /// True if this is a 64 bit ELF file header.
97 bool Is64Bit() const {
98 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS64;
99 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 //--------------------------------------------------------------------------
102 /// The byte order of this ELF file header.
103 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000104 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 /// The byte order of this ELF file as described by the header.
106 lldb::ByteOrder GetByteOrder() const;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 //--------------------------------------------------------------------------
109 /// The jump slot relocation type of this ELF.
110 unsigned GetRelocationJumpSlotType() const;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 //--------------------------------------------------------------------------
Pavel Labath23ccc292017-01-31 23:09:46 +0000113 /// Check if there should be header extension in section header #0
114 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000115 /// \return
Pavel Labath23ccc292017-01-31 23:09:46 +0000116 /// True if parsing the ELFHeader requires reading header extension
117 /// and false otherwise.
118 bool HasHeaderExtension() const;
119
120 //--------------------------------------------------------------------------
Adrian Prantld8f460e2018-05-02 16:55:16 +0000121 /// Parse an ELFHeader entry starting at position \p offset and update the
122 /// data extractor with the address size and byte order attributes as
123 /// defined by the header.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000125 /// \param[in,out] data
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 /// The DataExtractor to read from. Updated with the address size and
127 /// byte order attributes appropriate to this header.
128 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000129 /// \param[in,out] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 /// Pointer to an offset in the data. On return the offset will be
131 /// advanced by the number of bytes read.
132 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000133 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 /// True if the ELFHeader was successfully read and false
135 /// otherwise.
136 bool Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 //--------------------------------------------------------------------------
139 /// Examines at most EI_NIDENT bytes starting from the given pointer and
140 /// determines if the magic ELF identification exists.
141 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000142 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 /// True if the given sequence of bytes identifies an ELF file.
144 static bool MagicBytesMatch(const uint8_t *magic);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 //--------------------------------------------------------------------------
147 /// Examines at most EI_NIDENT bytes starting from the given address and
148 /// determines the address size of the underlying ELF file. This function
149 /// should only be called on an pointer for which MagicBytesMatch returns
150 /// true.
151 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000152 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 /// The number of bytes forming an address in the ELF file (either 4 or
154 /// 8), else zero if the address size could not be determined.
155 static unsigned AddressSizeInBytes(const uint8_t *magic);
Pavel Labath23ccc292017-01-31 23:09:46 +0000156
157private:
158
159 //--------------------------------------------------------------------------
Adrian Prantld8f460e2018-05-02 16:55:16 +0000160 /// Parse an ELFHeader header extension entry. This method is called by
161 /// Parse().
Pavel Labath23ccc292017-01-31 23:09:46 +0000162 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000163 /// \param[in] data
Pavel Labath23ccc292017-01-31 23:09:46 +0000164 /// The DataExtractor to read from.
165 void ParseHeaderExtension(lldb_private::DataExtractor &data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000166};
167
168//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000169/// \class ELFSectionHeader
Adrian Prantld8f460e2018-05-02 16:55:16 +0000170/// Generic representation of an ELF section header.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171struct ELFSectionHeader {
172 elf_word sh_name; ///< Section name string index.
173 elf_word sh_type; ///< Section type.
174 elf_xword sh_flags; ///< Section attributes.
175 elf_addr sh_addr; ///< Virtual address of the section in memory.
176 elf_off sh_offset; ///< Start of section from beginning of file.
177 elf_xword sh_size; ///< Number of bytes occupied in the file.
178 elf_word sh_link; ///< Index of associated section.
179 elf_word sh_info; ///< Extra section info (overloaded).
180 elf_xword sh_addralign; ///< Power of two alignment constraint.
181 elf_xword sh_entsize; ///< Byte size of each section entry.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 ELFSectionHeader();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 //--------------------------------------------------------------------------
186 /// Parse an ELFSectionHeader entry from the given DataExtracter starting at
187 /// position \p offset.
188 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000189 /// \param[in] data
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 /// The DataExtractor to read from. The address size of the extractor
191 /// determines if a 32 or 64 bit object should be read.
192 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000193 /// \param[in,out] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 /// Pointer to an offset in the data. On return the offset will be
195 /// advanced by the number of bytes read.
196 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000197 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 /// True if the ELFSectionHeader was successfully read and false
199 /// otherwise.
200 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000201};
202
203//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000204/// \class ELFProgramHeader
Adrian Prantld8f460e2018-05-02 16:55:16 +0000205/// Generic representation of an ELF program header.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206struct ELFProgramHeader {
207 elf_word p_type; ///< Type of program segment.
208 elf_word p_flags; ///< Segment attributes.
209 elf_off p_offset; ///< Start of segment from beginning of file.
210 elf_addr p_vaddr; ///< Virtual address of segment in memory.
211 elf_addr p_paddr; ///< Physical address (for non-VM systems).
212 elf_xword p_filesz; ///< Byte size of the segment in file.
213 elf_xword p_memsz; ///< Byte size of the segment in memory.
214 elf_xword p_align; ///< Segment alignment constraint.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 ELFProgramHeader();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 /// Parse an ELFProgramHeader entry from the given DataExtractor starting at
219 /// position \p offset. The address size of the DataExtractor determines if
220 /// a 32 or 64 bit object is to be parsed.
221 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000222 /// \param[in] data
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 /// The DataExtractor to read from. The address size of the extractor
224 /// determines if a 32 or 64 bit object should be read.
225 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000226 /// \param[in,out] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 /// Pointer to an offset in the data. On return the offset will be
228 /// advanced by the number of bytes read.
229 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000230 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 /// True if the ELFProgramHeader was successfully read and false
232 /// otherwise.
233 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000234};
235
236//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000237/// \class ELFSymbol
Adrian Prantld8f460e2018-05-02 16:55:16 +0000238/// Represents a symbol within an ELF symbol table.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239struct ELFSymbol {
240 elf_addr st_value; ///< Absolute or relocatable address.
241 elf_xword st_size; ///< Size of the symbol or zero.
242 elf_word st_name; ///< Symbol name string index.
243 unsigned char st_info; ///< Symbol type and binding attributes.
244 unsigned char st_other; ///< Reserved for future use.
245 elf_half st_shndx; ///< Section to which this symbol applies.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 ELFSymbol();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 /// Returns the binding attribute of the st_info member.
250 unsigned char getBinding() const { return st_info >> 4; }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 /// Returns the type attribute of the st_info member.
253 unsigned char getType() const { return st_info & 0x0F; }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 /// Sets the binding and type of the st_info member.
256 void setBindingAndType(unsigned char binding, unsigned char type) {
257 st_info = (binding << 4) + (type & 0x0F);
258 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 static const char *bindingToCString(unsigned char binding);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 static const char *typeToCString(unsigned char type);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 static const char *
265 sectionIndexToCString(elf_half shndx,
266 const lldb_private::SectionList *section_list);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 /// Parse an ELFSymbol entry from the given DataExtractor starting at
269 /// position \p offset. The address size of the DataExtractor determines if
270 /// a 32 or 64 bit object is to be parsed.
271 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000272 /// \param[in] data
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 /// The DataExtractor to read from. The address size of the extractor
274 /// determines if a 32 or 64 bit object should be read.
275 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000276 /// \param[in,out] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 /// Pointer to an offset in the data. On return the offset will be
278 /// advanced by the number of bytes read.
279 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000280 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 /// True if the ELFSymbol was successfully read and false otherwise.
282 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
283
284 void Dump(lldb_private::Stream *s, uint32_t idx,
285 const lldb_private::DataExtractor *strtab_data,
286 const lldb_private::SectionList *section_list);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000287};
288
289//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000290/// \class ELFDynamic
Adrian Prantld8f460e2018-05-02 16:55:16 +0000291/// Represents an entry in an ELF dynamic table.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292struct ELFDynamic {
293 elf_sxword d_tag; ///< Type of dynamic table entry.
294 union {
295 elf_xword d_val; ///< Integer value of the table entry.
296 elf_addr d_ptr; ///< Pointer value of the table entry.
297 };
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 ELFDynamic();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 /// Parse an ELFDynamic entry from the given DataExtractor starting at
302 /// position \p offset. The address size of the DataExtractor determines if
303 /// a 32 or 64 bit object is to be parsed.
304 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000305 /// \param[in] data
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 /// The DataExtractor to read from. The address size of the extractor
307 /// determines if a 32 or 64 bit object should be read.
308 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000309 /// \param[in,out] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 /// Pointer to an offset in the data. On return the offset will be
311 /// advanced by the number of bytes read.
312 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000313 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 /// True if the ELFDynamic entry was successfully read and false
315 /// otherwise.
316 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000317};
318
Stephen Wilson43fe6452011-03-30 15:59:12 +0000319//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000320/// \class ELFRel
Adrian Prantld8f460e2018-05-02 16:55:16 +0000321/// Represents a relocation entry with an implicit addend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322struct ELFRel {
323 elf_addr r_offset; ///< Address of reference.
324 elf_xword r_info; ///< symbol index and type of relocation.
Stephen Wilson43fe6452011-03-30 15:59:12 +0000325
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 ELFRel();
Stephen Wilson43fe6452011-03-30 15:59:12 +0000327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 /// Parse an ELFRel entry from the given DataExtractor starting at position
329 /// \p offset. The address size of the DataExtractor determines if a 32 or
330 /// 64 bit object is to be parsed.
331 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000332 /// \param[in] data
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 /// The DataExtractor to read from. The address size of the extractor
334 /// determines if a 32 or 64 bit object should be read.
335 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000336 /// \param[in,out] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 /// Pointer to an offset in the data. On return the offset will be
338 /// advanced by the number of bytes read.
339 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000340 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 /// True if the ELFRel entry was successfully read and false otherwise.
342 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson43fe6452011-03-30 15:59:12 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 /// Returns the type when the given entry represents a 32-bit relocation.
345 static unsigned RelocType32(const ELFRel &rel) { return rel.r_info & 0x0ff; }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 /// Returns the type when the given entry represents a 64-bit relocation.
348 static unsigned RelocType64(const ELFRel &rel) {
349 return rel.r_info & 0xffffffff;
350 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 /// Returns the symbol index when the given entry represents a 32-bit
353 /// relocation.
354 static unsigned RelocSymbol32(const ELFRel &rel) { return rel.r_info >> 8; }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 /// Returns the symbol index when the given entry represents a 64-bit
357 /// relocation.
358 static unsigned RelocSymbol64(const ELFRel &rel) { return rel.r_info >> 32; }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000359};
360
361//------------------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000362/// \class ELFRela
Adrian Prantld8f460e2018-05-02 16:55:16 +0000363/// Represents a relocation entry with an explicit addend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364struct ELFRela {
365 elf_addr r_offset; ///< Address of reference.
366 elf_xword r_info; ///< Symbol index and type of relocation.
367 elf_sxword r_addend; ///< Constant part of expression.
Stephen Wilson43fe6452011-03-30 15:59:12 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 ELFRela();
Stephen Wilson43fe6452011-03-30 15:59:12 +0000370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 /// Parse an ELFRela entry from the given DataExtractor starting at position
372 /// \p offset. The address size of the DataExtractor determines if a 32 or
373 /// 64 bit object is to be parsed.
374 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000375 /// \param[in] data
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 /// The DataExtractor to read from. The address size of the extractor
377 /// determines if a 32 or 64 bit object should be read.
378 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000379 /// \param[in,out] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 /// Pointer to an offset in the data. On return the offset will be
381 /// advanced by the number of bytes read.
382 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000383 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 /// True if the ELFRela entry was successfully read and false otherwise.
385 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson43fe6452011-03-30 15:59:12 +0000386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 /// Returns the type when the given entry represents a 32-bit relocation.
388 static unsigned RelocType32(const ELFRela &rela) {
389 return rela.r_info & 0x0ff;
390 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000391
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 /// Returns the type when the given entry represents a 64-bit relocation.
393 static unsigned RelocType64(const ELFRela &rela) {
394 return rela.r_info & 0xffffffff;
395 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 /// Returns the symbol index when the given entry represents a 32-bit
398 /// relocation.
399 static unsigned RelocSymbol32(const ELFRela &rela) {
400 return rela.r_info >> 8;
401 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 /// Returns the symbol index when the given entry represents a 64-bit
404 /// relocation.
405 static unsigned RelocSymbol64(const ELFRela &rela) {
406 return rela.r_info >> 32;
407 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000408};
409
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000410} // End namespace elf.
411
412#endif // #ifndef liblldb_ELFHeader_h_