1f325ba9dSStephen Wilson //===-- ELFHeader.h ------------------------------------------- -*- C++ -*-===//
2f325ba9dSStephen Wilson //
3f325ba9dSStephen Wilson //                     The LLVM Compiler Infrastructure
4f325ba9dSStephen Wilson //
5f325ba9dSStephen Wilson // This file is distributed under the University of Illinois Open Source
6f325ba9dSStephen Wilson // License. See LICENSE.TXT for details.
7f325ba9dSStephen Wilson //
8f325ba9dSStephen Wilson //===----------------------------------------------------------------------===//
9f325ba9dSStephen Wilson //
10f325ba9dSStephen Wilson /// @file
11f325ba9dSStephen Wilson /// @brief Generic structures and typedefs for ELF files.
12f325ba9dSStephen Wilson ///
13f325ba9dSStephen Wilson /// This file provides definitions for the various entities comprising an ELF
14f325ba9dSStephen Wilson /// file.  The structures are generic in the sense that they do not correspond
15f325ba9dSStephen Wilson /// to the exact binary layout of an ELF, but can be used to hold the
16f325ba9dSStephen Wilson /// information present in both 32 and 64 bit variants of the format.  Each
17f325ba9dSStephen Wilson /// entity provides a \c Parse method which is capable of transparently reading
18f325ba9dSStephen Wilson /// both 32 and 64 bit instances of the object.
19f325ba9dSStephen Wilson //===----------------------------------------------------------------------===//
20f325ba9dSStephen Wilson 
21f325ba9dSStephen Wilson #ifndef liblldb_ELFHeader_h_
22f325ba9dSStephen Wilson #define liblldb_ELFHeader_h_
23f325ba9dSStephen Wilson 
24f325ba9dSStephen Wilson #include "llvm/Support/ELF.h"
25f325ba9dSStephen Wilson 
26f325ba9dSStephen Wilson #include "lldb/lldb-enumerations.h"
27f325ba9dSStephen Wilson 
28f325ba9dSStephen Wilson namespace lldb_private
29f325ba9dSStephen Wilson {
30f325ba9dSStephen Wilson class DataExtractor;
31f325ba9dSStephen Wilson } // End namespace lldb_private.
32f325ba9dSStephen Wilson 
33f325ba9dSStephen Wilson namespace elf
34f325ba9dSStephen Wilson {
35f325ba9dSStephen Wilson 
36f325ba9dSStephen Wilson //------------------------------------------------------------------------------
37f325ba9dSStephen Wilson /// @name ELF type definitions.
38f325ba9dSStephen Wilson ///
39f325ba9dSStephen Wilson /// Types used to represent the various components of ELF structures.  All types
40f325ba9dSStephen Wilson /// are signed or unsigned integral types wide enough to hold values from both
41f325ba9dSStephen Wilson /// 32 and 64 bit ELF variants.
42f325ba9dSStephen Wilson //@{
43f325ba9dSStephen Wilson typedef uint64_t elf_addr;
44f325ba9dSStephen Wilson typedef uint64_t elf_off;
45f325ba9dSStephen Wilson typedef uint16_t elf_half;
46f325ba9dSStephen Wilson typedef uint32_t elf_word;
47f325ba9dSStephen Wilson typedef int32_t  elf_sword;
48f325ba9dSStephen Wilson typedef uint64_t elf_size;
49f325ba9dSStephen Wilson typedef uint64_t elf_xword;
50f325ba9dSStephen Wilson typedef int64_t  elf_sxword;
51f325ba9dSStephen Wilson //@}
52f325ba9dSStephen Wilson 
53f325ba9dSStephen Wilson //------------------------------------------------------------------------------
54f325ba9dSStephen Wilson /// @class ELFHeader
55f325ba9dSStephen Wilson /// @brief Generic representation of an ELF file header.
56f325ba9dSStephen Wilson ///
57f325ba9dSStephen Wilson /// This object is used to identify the general attributes on an ELF file and to
58f325ba9dSStephen Wilson /// locate additional sections within the file.
59f325ba9dSStephen Wilson struct ELFHeader
60f325ba9dSStephen Wilson {
61f325ba9dSStephen Wilson     unsigned char e_ident[llvm::ELF::EI_NIDENT]; ///< ELF file identification.
62f325ba9dSStephen Wilson     elf_addr      e_entry;            ///< Virtual address program entry point.
63f325ba9dSStephen Wilson     elf_off       e_phoff;            ///< File offset of program header table.
64f325ba9dSStephen Wilson     elf_off       e_shoff;            ///< File offset of section header table.
65f325ba9dSStephen Wilson     elf_word      e_flags;            ///< Processor specific flags.
66f325ba9dSStephen Wilson     elf_word      e_version;          ///< Version of object file (always 1).
67f325ba9dSStephen Wilson     elf_half      e_type;             ///< Object file type.
68f325ba9dSStephen Wilson     elf_half      e_machine;          ///< Target architecture.
69f325ba9dSStephen Wilson     elf_half      e_ehsize;           ///< Byte size of the ELF header.
70f325ba9dSStephen Wilson     elf_half      e_phentsize;        ///< Size of a program header table entry.
71710dd5aeSGreg Clayton     elf_half      e_phnum;            ///< Number of program header entries.
72f325ba9dSStephen Wilson     elf_half      e_shentsize;        ///< Size of a section header table entry.
73710dd5aeSGreg Clayton     elf_half      e_shnum;            ///< Number of section header entries.
74f325ba9dSStephen Wilson     elf_half      e_shstrndx;         ///< String table section index.
75f325ba9dSStephen Wilson 
76f325ba9dSStephen Wilson     ELFHeader();
77f325ba9dSStephen Wilson 
78f325ba9dSStephen Wilson     //--------------------------------------------------------------------------
79f325ba9dSStephen Wilson     /// Returns true if this is a 32 bit ELF file header.
80f325ba9dSStephen Wilson     ///
81f325ba9dSStephen Wilson     /// @return
82f325ba9dSStephen Wilson     ///    True if this is a 32 bit ELF file header.
83f325ba9dSStephen Wilson     bool Is32Bit() const {
84f325ba9dSStephen Wilson         return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32;
85f325ba9dSStephen Wilson     }
86f325ba9dSStephen Wilson 
87f325ba9dSStephen Wilson     //--------------------------------------------------------------------------
88f325ba9dSStephen Wilson     /// Returns true if this is a 64 bit ELF file header.
89f325ba9dSStephen Wilson     ///
90f325ba9dSStephen Wilson     /// @return
91f325ba9dSStephen Wilson     ///   True if this is a 64 bit ELF file header.
92f325ba9dSStephen Wilson     bool Is64Bit() const {
93f325ba9dSStephen Wilson         return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS64;
94f325ba9dSStephen Wilson     }
95f325ba9dSStephen Wilson 
96f325ba9dSStephen Wilson     //--------------------------------------------------------------------------
97f325ba9dSStephen Wilson     /// The byte order of this ELF file header.
98f325ba9dSStephen Wilson     ///
99f325ba9dSStephen Wilson     /// @return
100f325ba9dSStephen Wilson     ///    The byte order of this ELF file as described by the header.
101f325ba9dSStephen Wilson     lldb::ByteOrder
102f325ba9dSStephen Wilson     GetByteOrder() const;
103f325ba9dSStephen Wilson 
104f325ba9dSStephen Wilson     //--------------------------------------------------------------------------
105*43fe645bSStephen Wilson     /// The jump slot relocation type of this ELF.
106*43fe645bSStephen Wilson     unsigned
107*43fe645bSStephen Wilson     GetRelocationJumpSlotType() const;
108*43fe645bSStephen Wilson 
109*43fe645bSStephen Wilson     //--------------------------------------------------------------------------
110f325ba9dSStephen Wilson     /// Parse an ELFSectionHeader entry starting at position \p offset and
111f325ba9dSStephen Wilson     /// update the data extractor with the address size and byte order
112f325ba9dSStephen Wilson     /// attributes as defined by the header.
113f325ba9dSStephen Wilson     ///
114f325ba9dSStephen Wilson     /// @param[in,out] data
115f325ba9dSStephen Wilson     ///    The DataExtractor to read from.  Updated with the address size and
116f325ba9dSStephen Wilson     ///    byte order attributes appropriate to this header.
117f325ba9dSStephen Wilson     ///
118f325ba9dSStephen Wilson     /// @param[in,out] offset
119f325ba9dSStephen Wilson     ///    Pointer to an offset in the data.  On return the offset will be
120f325ba9dSStephen Wilson     ///    advanced by the number of bytes read.
121f325ba9dSStephen Wilson     ///
122f325ba9dSStephen Wilson     /// @return
123f325ba9dSStephen Wilson     ///    True if the ELFSectionHeader was successfully read and false
124f325ba9dSStephen Wilson     ///    otherwise.
125f325ba9dSStephen Wilson     bool
126f325ba9dSStephen Wilson     Parse(lldb_private::DataExtractor &data, uint32_t *offset);
127f325ba9dSStephen Wilson 
128f325ba9dSStephen Wilson     //--------------------------------------------------------------------------
129f325ba9dSStephen Wilson     /// Examines at most EI_NIDENT bytes starting from the given pointer and
130f325ba9dSStephen Wilson     /// determines if the magic ELF identification exists.
131f325ba9dSStephen Wilson     ///
132f325ba9dSStephen Wilson     /// @return
133f325ba9dSStephen Wilson     ///    True if the given sequence of bytes identifies an ELF file.
134f325ba9dSStephen Wilson     static bool
135f325ba9dSStephen Wilson     MagicBytesMatch(const uint8_t *magic);
136f325ba9dSStephen Wilson 
137f325ba9dSStephen Wilson     //--------------------------------------------------------------------------
138f325ba9dSStephen Wilson     /// Examines at most EI_NIDENT bytes starting from the given address and
139f325ba9dSStephen Wilson     /// determines the address size of the underlying ELF file.  This function
140f325ba9dSStephen Wilson     /// should only be called on an pointer for which MagicBytesMatch returns
141f325ba9dSStephen Wilson     /// true.
142f325ba9dSStephen Wilson     ///
143f325ba9dSStephen Wilson     /// @return
144f325ba9dSStephen Wilson     ///    The number of bytes forming an address in the ELF file (either 4 or
145f325ba9dSStephen Wilson     ///    8), else zero if the address size could not be determined.
146f325ba9dSStephen Wilson     static unsigned
147f325ba9dSStephen Wilson     AddressSizeInBytes(const uint8_t *magic);
148f325ba9dSStephen Wilson };
149f325ba9dSStephen Wilson 
150f325ba9dSStephen Wilson //------------------------------------------------------------------------------
151f325ba9dSStephen Wilson /// @class ELFSectionHeader
152f325ba9dSStephen Wilson /// @brief Generic representation of an ELF section header.
153f325ba9dSStephen Wilson struct ELFSectionHeader
154f325ba9dSStephen Wilson {
155f325ba9dSStephen Wilson     elf_word  sh_name;          ///< Section name string index.
156f325ba9dSStephen Wilson     elf_word  sh_type;          ///< Section type.
157f325ba9dSStephen Wilson     elf_xword sh_flags;         ///< Section attributes.
158f325ba9dSStephen Wilson     elf_addr  sh_addr;          ///< Virtual address of the section in memory.
159f325ba9dSStephen Wilson     elf_off   sh_offset;        ///< Start of section from beginning of file.
160f325ba9dSStephen Wilson     elf_xword sh_size;          ///< Number of bytes occupied in the file.
161f325ba9dSStephen Wilson     elf_word  sh_link;          ///< Index of associated section.
162f325ba9dSStephen Wilson     elf_word  sh_info;          ///< Extra section info (overloaded).
163f325ba9dSStephen Wilson     elf_xword sh_addralign;     ///< Power of two alignment constraint.
164f325ba9dSStephen Wilson     elf_xword sh_entsize;       ///< Byte size of each section entry.
165f325ba9dSStephen Wilson 
166f325ba9dSStephen Wilson     ELFSectionHeader();
167f325ba9dSStephen Wilson 
168f325ba9dSStephen Wilson     //--------------------------------------------------------------------------
169f325ba9dSStephen Wilson     /// Parse an ELFSectionHeader entry from the given DataExtracter starting at
170f325ba9dSStephen Wilson     /// position \p offset.
171f325ba9dSStephen Wilson     ///
172f325ba9dSStephen Wilson     /// @param[in] data
173f325ba9dSStephen Wilson     ///    The DataExtractor to read from.  The address size of the extractor
174f325ba9dSStephen Wilson     ///    determines if a 32 or 64 bit object should be read.
175f325ba9dSStephen Wilson     ///
176f325ba9dSStephen Wilson     /// @param[in,out] offset
177f325ba9dSStephen Wilson     ///    Pointer to an offset in the data.  On return the offset will be
178f325ba9dSStephen Wilson     ///    advanced by the number of bytes read.
179f325ba9dSStephen Wilson     ///
180f325ba9dSStephen Wilson     /// @return
181f325ba9dSStephen Wilson     ///    True if the ELFSectionHeader was successfully read and false
182f325ba9dSStephen Wilson     ///    otherwise.
183f325ba9dSStephen Wilson     bool
184f325ba9dSStephen Wilson     Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
185f325ba9dSStephen Wilson };
186f325ba9dSStephen Wilson 
187f325ba9dSStephen Wilson //------------------------------------------------------------------------------
188f325ba9dSStephen Wilson /// @class ELFProgramHeader
189f325ba9dSStephen Wilson /// @brief Generic representation of an ELF program header.
190f325ba9dSStephen Wilson struct ELFProgramHeader
191f325ba9dSStephen Wilson {
192710dd5aeSGreg Clayton     elf_word  p_type;           ///< Type of program segment.
193710dd5aeSGreg Clayton     elf_word  p_flags;          ///< Segment attributes.
194710dd5aeSGreg Clayton     elf_off   p_offset;         ///< Start of segment from beginning of file.
195f325ba9dSStephen Wilson     elf_addr  p_vaddr;          ///< Virtual address of segment in memory.
196f325ba9dSStephen Wilson     elf_addr  p_paddr;          ///< Physical address (for non-VM systems).
197f325ba9dSStephen Wilson     elf_xword p_filesz;         ///< Byte size of the segment in file.
198f325ba9dSStephen Wilson     elf_xword p_memsz;          ///< Byte size of the segment in memory.
199710dd5aeSGreg Clayton     elf_xword p_align;          ///< Segment alignment constraint.
200f325ba9dSStephen Wilson 
201f325ba9dSStephen Wilson     ELFProgramHeader();
202f325ba9dSStephen Wilson 
203710dd5aeSGreg Clayton     /// Parse an ELFProgramHeader entry from the given DataExtractor starting at
204710dd5aeSGreg Clayton     /// position \p offset.  The address size of the DataExtractor determines if
205f325ba9dSStephen Wilson     /// a 32 or 64 bit object is to be parsed.
206f325ba9dSStephen Wilson     ///
207f325ba9dSStephen Wilson     /// @param[in] data
208f325ba9dSStephen Wilson     ///    The DataExtractor to read from.  The address size of the extractor
209f325ba9dSStephen Wilson     ///    determines if a 32 or 64 bit object should be read.
210f325ba9dSStephen Wilson     ///
211f325ba9dSStephen Wilson     /// @param[in,out] offset
212f325ba9dSStephen Wilson     ///    Pointer to an offset in the data.  On return the offset will be
213f325ba9dSStephen Wilson     ///    advanced by the number of bytes read.
214f325ba9dSStephen Wilson     ///
215f325ba9dSStephen Wilson     /// @return
216f325ba9dSStephen Wilson     ///    True if the ELFProgramHeader was successfully read and false
217f325ba9dSStephen Wilson     ///    otherwise.
218f325ba9dSStephen Wilson     bool
219f325ba9dSStephen Wilson     Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
220f325ba9dSStephen Wilson };
221f325ba9dSStephen Wilson 
222f325ba9dSStephen Wilson //------------------------------------------------------------------------------
223f325ba9dSStephen Wilson /// @class ELFSymbol
224f325ba9dSStephen Wilson /// @brief Represents a symbol within an ELF symbol table.
225f325ba9dSStephen Wilson struct ELFSymbol
226f325ba9dSStephen Wilson {
227f325ba9dSStephen Wilson     elf_addr      st_value;     ///< Absolute or relocatable address.
228f325ba9dSStephen Wilson     elf_xword     st_size;      ///< Size of the symbol or zero.
229f325ba9dSStephen Wilson     elf_word      st_name;      ///< Symbol name string index.
230f325ba9dSStephen Wilson     unsigned char st_info;      ///< Symbol type and binding attributes.
231f325ba9dSStephen Wilson     unsigned char st_other;     ///< Reserved for future use.
232f325ba9dSStephen Wilson     elf_half      st_shndx;     ///< Section to which this symbol applies.
233f325ba9dSStephen Wilson 
234f325ba9dSStephen Wilson     ELFSymbol();
235f325ba9dSStephen Wilson 
236f325ba9dSStephen Wilson     /// Returns the binding attribute of the st_info member.
237f325ba9dSStephen Wilson     unsigned char getBinding() const { return st_info >> 4; }
238f325ba9dSStephen Wilson 
239f325ba9dSStephen Wilson     /// Returns the type attribute of the st_info member.
240f325ba9dSStephen Wilson     unsigned char getType() const { return st_info & 0x0F; }
241f325ba9dSStephen Wilson 
242710dd5aeSGreg Clayton     /// Sets the binding and type of the st_info member.
243f325ba9dSStephen Wilson     void setBindingAndType(unsigned char binding, unsigned char type) {
244f325ba9dSStephen Wilson         st_info = (binding << 4) + (type & 0x0F);
245f325ba9dSStephen Wilson     }
246f325ba9dSStephen Wilson 
247710dd5aeSGreg Clayton     /// Parse an ELFSymbol entry from the given DataExtractor starting at
248710dd5aeSGreg Clayton     /// position \p offset.  The address size of the DataExtractor determines if
249f325ba9dSStephen Wilson     /// a 32 or 64 bit object is to be parsed.
250f325ba9dSStephen Wilson     ///
251f325ba9dSStephen Wilson     /// @param[in] data
252f325ba9dSStephen Wilson     ///    The DataExtractor to read from.  The address size of the extractor
253f325ba9dSStephen Wilson     ///    determines if a 32 or 64 bit object should be read.
254f325ba9dSStephen Wilson     ///
255f325ba9dSStephen Wilson     /// @param[in,out] offset
256f325ba9dSStephen Wilson     ///    Pointer to an offset in the data.  On return the offset will be
257f325ba9dSStephen Wilson     ///    advanced by the number of bytes read.
258f325ba9dSStephen Wilson     ///
259f325ba9dSStephen Wilson     /// @return
260f325ba9dSStephen Wilson     ///    True if the ELFSymbol was successfully read and false otherwise.
261f325ba9dSStephen Wilson     bool
262f325ba9dSStephen Wilson     Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
263f325ba9dSStephen Wilson };
264f325ba9dSStephen Wilson 
265f325ba9dSStephen Wilson //------------------------------------------------------------------------------
266f325ba9dSStephen Wilson /// @class ELFDynamic
267f325ba9dSStephen Wilson /// @brief Represents an entry in an ELF dynamic table.
268f325ba9dSStephen Wilson struct ELFDynamic
269f325ba9dSStephen Wilson {
270f325ba9dSStephen Wilson     elf_sxword d_tag;           ///< Type of dynamic table entry.
271f325ba9dSStephen Wilson     union
272f325ba9dSStephen Wilson     {
273f325ba9dSStephen Wilson         elf_xword d_val;        ///< Integer value of the table entry.
274f325ba9dSStephen Wilson         elf_addr  d_ptr;        ///< Pointer value of the table entry.
275f325ba9dSStephen Wilson     };
276f325ba9dSStephen Wilson 
277f325ba9dSStephen Wilson     ELFDynamic();
278f325ba9dSStephen Wilson 
279710dd5aeSGreg Clayton     /// Parse an ELFDynamic entry from the given DataExtractor starting at
280710dd5aeSGreg Clayton     /// position \p offset.  The address size of the DataExtractor determines if
281f325ba9dSStephen Wilson     /// a 32 or 64 bit object is to be parsed.
282f325ba9dSStephen Wilson     ///
283f325ba9dSStephen Wilson     /// @param[in] data
284f325ba9dSStephen Wilson     ///    The DataExtractor to read from.  The address size of the extractor
285f325ba9dSStephen Wilson     ///    determines if a 32 or 64 bit object should be read.
286f325ba9dSStephen Wilson     ///
287f325ba9dSStephen Wilson     /// @param[in,out] offset
288f325ba9dSStephen Wilson     ///    Pointer to an offset in the data.  On return the offset will be
289f325ba9dSStephen Wilson     ///    advanced by the number of bytes read.
290f325ba9dSStephen Wilson     ///
291f325ba9dSStephen Wilson     /// @return
292f325ba9dSStephen Wilson     ///    True if the ELFDynamic entry was successfully read and false
293f325ba9dSStephen Wilson     ///    otherwise.
294f325ba9dSStephen Wilson     bool
295f325ba9dSStephen Wilson     Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
296f325ba9dSStephen Wilson };
297f325ba9dSStephen Wilson 
298*43fe645bSStephen Wilson //------------------------------------------------------------------------------
299*43fe645bSStephen Wilson /// @class ELFRel
300*43fe645bSStephen Wilson /// @brief Represents a relocation entry with an implicit addend.
301*43fe645bSStephen Wilson struct ELFRel
302*43fe645bSStephen Wilson {
303*43fe645bSStephen Wilson     elf_addr  r_offset;         ///< Address of reference.
304*43fe645bSStephen Wilson     elf_xword r_info;           ///< symbol index and type of relocation.
305*43fe645bSStephen Wilson 
306*43fe645bSStephen Wilson     ELFRel();
307*43fe645bSStephen Wilson 
308*43fe645bSStephen Wilson     /// Parse an ELFRel entry from the given DataExtractor starting at position
309*43fe645bSStephen Wilson     /// \p offset.  The address size of the DataExtractor determines if a 32 or
310*43fe645bSStephen Wilson     /// 64 bit object is to be parsed.
311*43fe645bSStephen Wilson     ///
312*43fe645bSStephen Wilson     /// @param[in] data
313*43fe645bSStephen Wilson     ///    The DataExtractor to read from.  The address size of the extractor
314*43fe645bSStephen Wilson     ///    determines if a 32 or 64 bit object should be read.
315*43fe645bSStephen Wilson     ///
316*43fe645bSStephen Wilson     /// @param[in,out] offset
317*43fe645bSStephen Wilson     ///    Pointer to an offset in the data.  On return the offset will be
318*43fe645bSStephen Wilson     ///    advanced by the number of bytes read.
319*43fe645bSStephen Wilson     ///
320*43fe645bSStephen Wilson     /// @return
321*43fe645bSStephen Wilson     ///    True if the ELFRel entry was successfully read and false otherwise.
322*43fe645bSStephen Wilson     bool
323*43fe645bSStephen Wilson     Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
324*43fe645bSStephen Wilson 
325*43fe645bSStephen Wilson     /// Returns the type when the given entry represents a 32-bit relocation.
326*43fe645bSStephen Wilson     static unsigned
327*43fe645bSStephen Wilson     RelocType32(const ELFRel &rel)
328*43fe645bSStephen Wilson     {
329*43fe645bSStephen Wilson         return rel.r_info & 0x0ff;
330*43fe645bSStephen Wilson     }
331*43fe645bSStephen Wilson 
332*43fe645bSStephen Wilson     /// Returns the type when the given entry represents a 64-bit relocation.
333*43fe645bSStephen Wilson     static unsigned
334*43fe645bSStephen Wilson     RelocType64(const ELFRel &rel)
335*43fe645bSStephen Wilson     {
336*43fe645bSStephen Wilson         return rel.r_info & 0xffffffff;
337*43fe645bSStephen Wilson     }
338*43fe645bSStephen Wilson 
339*43fe645bSStephen Wilson     /// Returns the symbol index when the given entry represents a 32-bit
340*43fe645bSStephen Wilson     /// reloction.
341*43fe645bSStephen Wilson     static unsigned
342*43fe645bSStephen Wilson     RelocSymbol32(const ELFRel &rel)
343*43fe645bSStephen Wilson     {
344*43fe645bSStephen Wilson         return rel.r_info >> 8;
345*43fe645bSStephen Wilson     }
346*43fe645bSStephen Wilson 
347*43fe645bSStephen Wilson     /// Returns the symbol index when the given entry represents a 64-bit
348*43fe645bSStephen Wilson     /// reloction.
349*43fe645bSStephen Wilson     static unsigned
350*43fe645bSStephen Wilson     RelocSymbol64(const ELFRel &rel)
351*43fe645bSStephen Wilson     {
352*43fe645bSStephen Wilson         return rel.r_info >> 32;
353*43fe645bSStephen Wilson     }
354*43fe645bSStephen Wilson };
355*43fe645bSStephen Wilson 
356*43fe645bSStephen Wilson //------------------------------------------------------------------------------
357*43fe645bSStephen Wilson /// @class ELFRela
358*43fe645bSStephen Wilson /// @brief Represents a relocation entry with an explicit addend.
359*43fe645bSStephen Wilson struct ELFRela
360*43fe645bSStephen Wilson {
361*43fe645bSStephen Wilson     elf_addr   r_offset;        ///< Address of reference.
362*43fe645bSStephen Wilson     elf_xword  r_info;          ///< Symbol index and type of relocation.
363*43fe645bSStephen Wilson     elf_sxword r_addend;        ///< Constant part of expression.
364*43fe645bSStephen Wilson 
365*43fe645bSStephen Wilson     ELFRela();
366*43fe645bSStephen Wilson 
367*43fe645bSStephen Wilson     /// Parse an ELFRela entry from the given DataExtractor starting at position
368*43fe645bSStephen Wilson     /// \p offset.  The address size of the DataExtractor determines if a 32 or
369*43fe645bSStephen Wilson     /// 64 bit object is to be parsed.
370*43fe645bSStephen Wilson     ///
371*43fe645bSStephen Wilson     /// @param[in] data
372*43fe645bSStephen Wilson     ///    The DataExtractor to read from.  The address size of the extractor
373*43fe645bSStephen Wilson     ///    determines if a 32 or 64 bit object should be read.
374*43fe645bSStephen Wilson     ///
375*43fe645bSStephen Wilson     /// @param[in,out] offset
376*43fe645bSStephen Wilson     ///    Pointer to an offset in the data.  On return the offset will be
377*43fe645bSStephen Wilson     ///    advanced by the number of bytes read.
378*43fe645bSStephen Wilson     ///
379*43fe645bSStephen Wilson     /// @return
380*43fe645bSStephen Wilson     ///    True if the ELFRela entry was successfully read and false otherwise.
381*43fe645bSStephen Wilson     bool
382*43fe645bSStephen Wilson     Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
383*43fe645bSStephen Wilson 
384*43fe645bSStephen Wilson     /// Returns the type when the given entry represents a 32-bit relocation.
385*43fe645bSStephen Wilson     static unsigned
386*43fe645bSStephen Wilson     RelocType32(const ELFRela &rela)
387*43fe645bSStephen Wilson     {
388*43fe645bSStephen Wilson         return rela.r_info & 0x0ff;
389*43fe645bSStephen Wilson     }
390*43fe645bSStephen Wilson 
391*43fe645bSStephen Wilson     /// Returns the type when the given entry represents a 64-bit relocation.
392*43fe645bSStephen Wilson     static unsigned
393*43fe645bSStephen Wilson     RelocType64(const ELFRela &rela)
394*43fe645bSStephen Wilson     {
395*43fe645bSStephen Wilson         return rela.r_info & 0xffffffff;
396*43fe645bSStephen Wilson     }
397*43fe645bSStephen Wilson 
398*43fe645bSStephen Wilson     /// Returns the symbol index when the given entry represents a 32-bit
399*43fe645bSStephen Wilson     /// reloction.
400*43fe645bSStephen Wilson     static unsigned
401*43fe645bSStephen Wilson     RelocSymbol32(const ELFRela &rela)
402*43fe645bSStephen Wilson     {
403*43fe645bSStephen Wilson         return rela.r_info >> 8;
404*43fe645bSStephen Wilson     }
405*43fe645bSStephen Wilson 
406*43fe645bSStephen Wilson     /// Returns the symbol index when the given entry represents a 64-bit
407*43fe645bSStephen Wilson     /// reloction.
408*43fe645bSStephen Wilson     static unsigned
409*43fe645bSStephen Wilson     RelocSymbol64(const ELFRela &rela)
410*43fe645bSStephen Wilson     {
411*43fe645bSStephen Wilson         return rela.r_info >> 32;
412*43fe645bSStephen Wilson     }
413*43fe645bSStephen Wilson };
414*43fe645bSStephen Wilson 
415f325ba9dSStephen Wilson } // End namespace elf.
416f325ba9dSStephen Wilson 
417f325ba9dSStephen Wilson #endif // #ifndef liblldb_ELFHeader_h_
418