blob: f6b1653e50dc7dd5102f63f483878ec94624dabc [file] [log] [blame]
Stephen Wilsonddd29622010-07-13 23:07:23 +00001//===-- ELFHeader.h ------------------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// @file
11/// @brief Generic structures and typedefs for ELF files.
12///
13/// This file provides definitions for the various entities comprising an ELF
14/// file. The structures are generic in the sense that they do not correspond
15/// to the exact binary layout of an ELF, but can be used to hold the
16/// information present in both 32 and 64 bit variants of the format. Each
17/// entity provides a \c Parse method which is capable of transparently reading
18/// both 32 and 64 bit instances of the object.
19//===----------------------------------------------------------------------===//
20
21#ifndef liblldb_ELFHeader_h_
22#define liblldb_ELFHeader_h_
23
24#include "llvm/Support/ELF.h"
25
26#include "lldb/lldb-enumerations.h"
27
28namespace lldb_private
29{
30class DataExtractor;
31} // End namespace lldb_private.
32
33namespace elf
34{
35
36//------------------------------------------------------------------------------
37/// @name ELF type definitions.
38///
39/// Types used to represent the various components of ELF structures. All types
40/// are signed or unsigned integral types wide enough to hold values from both
41/// 32 and 64 bit ELF variants.
42//@{
43typedef uint64_t elf_addr;
44typedef uint64_t elf_off;
45typedef uint16_t elf_half;
46typedef uint32_t elf_word;
47typedef int32_t elf_sword;
48typedef uint64_t elf_size;
49typedef uint64_t elf_xword;
50typedef int64_t elf_sxword;
51//@}
52
53//------------------------------------------------------------------------------
54/// @class ELFHeader
55/// @brief Generic representation of an ELF file header.
56///
57/// This object is used to identify the general attributes on an ELF file and to
58/// locate additional sections within the file.
59struct ELFHeader
60{
61 unsigned char e_ident[llvm::ELF::EI_NIDENT]; ///< ELF file identification.
62 elf_addr e_entry; ///< Virtual address program entry point.
63 elf_off e_phoff; ///< File offset of program header table.
64 elf_off e_shoff; ///< File offset of section header table.
65 elf_word e_flags; ///< Processor specific flags.
66 elf_word e_version; ///< Version of object file (always 1).
67 elf_half e_type; ///< Object file type.
68 elf_half e_machine; ///< Target architecture.
69 elf_half e_ehsize; ///< Byte size of the ELF header.
70 elf_half e_phentsize; ///< Size of a program header table entry.
71 elf_half e_phnum; ///< Number of program header entrys.
72 elf_half e_shentsize; ///< Size of a section header table entry.
73 elf_half e_shnum; ///< Number of section header entrys.
74 elf_half e_shstrndx; ///< String table section index.
75
76 ELFHeader();
77
78 //--------------------------------------------------------------------------
79 /// Returns true if this is a 32 bit ELF file header.
80 ///
81 /// @return
82 /// True if this is a 32 bit ELF file header.
83 bool Is32Bit() const {
84 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32;
85 }
86
87 //--------------------------------------------------------------------------
88 /// Returns true if this is a 64 bit ELF file header.
89 ///
90 /// @return
91 /// True if this is a 64 bit ELF file header.
92 bool Is64Bit() const {
93 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS64;
94 }
95
96 //--------------------------------------------------------------------------
97 /// The byte order of this ELF file header.
98 ///
99 /// @return
100 /// The byte order of this ELF file as described by the header.
101 lldb::ByteOrder
102 GetByteOrder() const;
103
104 //--------------------------------------------------------------------------
105 /// Parse an ELFSectionHeader entry starting at position \p offset and
106 /// update the data extractor with the address size and byte order
107 /// attributes as defined by the header.
108 ///
109 /// @param[in,out] data
110 /// The DataExtractor to read from. Updated with the address size and
111 /// byte order attributes appropriate to this header.
112 ///
113 /// @param[in,out] offset
114 /// Pointer to an offset in the data. On return the offset will be
115 /// advanced by the number of bytes read.
116 ///
117 /// @return
118 /// True if the ELFSectionHeader was successfully read and false
119 /// otherwise.
120 bool
121 Parse(lldb_private::DataExtractor &data, uint32_t *offset);
122
123 //--------------------------------------------------------------------------
124 /// Examines at most EI_NIDENT bytes starting from the given pointer and
125 /// determines if the magic ELF identification exists.
126 ///
127 /// @return
128 /// True if the given sequence of bytes identifies an ELF file.
129 static bool
130 MagicBytesMatch(const uint8_t *magic);
131
132 //--------------------------------------------------------------------------
133 /// Examines at most EI_NIDENT bytes starting from the given address and
134 /// determines the address size of the underlying ELF file. This function
135 /// should only be called on an pointer for which MagicBytesMatch returns
136 /// true.
137 ///
138 /// @return
139 /// The number of bytes forming an address in the ELF file (either 4 or
140 /// 8), else zero if the address size could not be determined.
141 static unsigned
142 AddressSizeInBytes(const uint8_t *magic);
143};
144
145//------------------------------------------------------------------------------
146/// @class ELFSectionHeader
147/// @brief Generic representation of an ELF section header.
148struct ELFSectionHeader
149{
150 elf_word sh_name; ///< Section name string index.
151 elf_word sh_type; ///< Section type.
152 elf_xword sh_flags; ///< Section attributes.
153 elf_addr sh_addr; ///< Virtual address of the section in memory.
154 elf_off sh_offset; ///< Start of section from beginning of file.
155 elf_xword sh_size; ///< Number of bytes occupied in the file.
156 elf_word sh_link; ///< Index of associated section.
157 elf_word sh_info; ///< Extra section info (overloaded).
158 elf_xword sh_addralign; ///< Power of two alignment constraint.
159 elf_xword sh_entsize; ///< Byte size of each section entry.
160
161 ELFSectionHeader();
162
163 //--------------------------------------------------------------------------
164 /// Parse an ELFSectionHeader entry from the given DataExtracter starting at
165 /// position \p offset.
166 ///
167 /// @param[in] data
168 /// The DataExtractor to read from. The address size of the extractor
169 /// determines if a 32 or 64 bit object should be read.
170 ///
171 /// @param[in,out] offset
172 /// Pointer to an offset in the data. On return the offset will be
173 /// advanced by the number of bytes read.
174 ///
175 /// @return
176 /// True if the ELFSectionHeader was successfully read and false
177 /// otherwise.
178 bool
179 Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
180};
181
182//------------------------------------------------------------------------------
183/// @class ELFProgramHeader
184/// @brief Generic representation of an ELF program header.
185struct ELFProgramHeader
186{
187 elf_word p_type; ///< Type of program segement.
188 elf_word p_flags; ///< Segement attibutes.
189 elf_off p_offset; ///< Start of segment from begining of file.
190 elf_addr p_vaddr; ///< Virtual address of segment in memory.
191 elf_addr p_paddr; ///< Physical address (for non-VM systems).
192 elf_xword p_filesz; ///< Byte size of the segment in file.
193 elf_xword p_memsz; ///< Byte size of the segment in memory.
194 elf_xword p_align; ///< Segement alignement constraint.
195
196 ELFProgramHeader();
197
198 /// Parse an ELFProgramHeader entry from the given DataExtracter starting at
199 /// position \p offset. The address size of the DataExtracter determines if
200 /// a 32 or 64 bit object is to be parsed.
201 ///
202 /// @param[in] data
203 /// The DataExtractor to read from. The address size of the extractor
204 /// determines if a 32 or 64 bit object should be read.
205 ///
206 /// @param[in,out] offset
207 /// Pointer to an offset in the data. On return the offset will be
208 /// advanced by the number of bytes read.
209 ///
210 /// @return
211 /// True if the ELFProgramHeader was successfully read and false
212 /// otherwise.
213 bool
214 Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
215};
216
217//------------------------------------------------------------------------------
218/// @class ELFSymbol
219/// @brief Represents a symbol within an ELF symbol table.
220struct ELFSymbol
221{
222 elf_addr st_value; ///< Absolute or relocatable address.
223 elf_xword st_size; ///< Size of the symbol or zero.
224 elf_word st_name; ///< Symbol name string index.
225 unsigned char st_info; ///< Symbol type and binding attributes.
226 unsigned char st_other; ///< Reserved for future use.
227 elf_half st_shndx; ///< Section to which this symbol applies.
228
229 ELFSymbol();
230
231 /// Returns the binding attribute of the st_info member.
232 unsigned char getBinding() const { return st_info >> 4; }
233
234 /// Returns the type attribute of the st_info member.
235 unsigned char getType() const { return st_info & 0x0F; }
236
237 /// Sets the bining and type of the st_info member.
238 void setBindingAndType(unsigned char binding, unsigned char type) {
239 st_info = (binding << 4) + (type & 0x0F);
240 }
241
242 /// Parse an ELFSymbol entry from the given DataExtracter starting at
243 /// position \p offset. The address size of the DataExtracter determines if
244 /// a 32 or 64 bit object is to be parsed.
245 ///
246 /// @param[in] data
247 /// The DataExtractor to read from. The address size of the extractor
248 /// determines if a 32 or 64 bit object should be read.
249 ///
250 /// @param[in,out] offset
251 /// Pointer to an offset in the data. On return the offset will be
252 /// advanced by the number of bytes read.
253 ///
254 /// @return
255 /// True if the ELFSymbol was successfully read and false otherwise.
256 bool
257 Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
258};
259
260//------------------------------------------------------------------------------
261/// @class ELFDynamic
262/// @brief Represents an entry in an ELF dynamic table.
263struct ELFDynamic
264{
265 elf_sxword d_tag; ///< Type of dynamic table entry.
266 union
267 {
268 elf_xword d_val; ///< Integer value of the table entry.
269 elf_addr d_ptr; ///< Pointer value of the table entry.
270 };
271
272 ELFDynamic();
273
274 /// Parse an ELFDynamic entry from the given DataExtracter starting at
275 /// position \p offset. The address size of the DataExtracter determines if
276 /// a 32 or 64 bit object is to be parsed.
277 ///
278 /// @param[in] data
279 /// The DataExtractor to read from. The address size of the extractor
280 /// determines if a 32 or 64 bit object should be read.
281 ///
282 /// @param[in,out] offset
283 /// Pointer to an offset in the data. On return the offset will be
284 /// advanced by the number of bytes read.
285 ///
286 /// @return
287 /// True if the ELFDynamic entry was successfully read and false
288 /// otherwise.
289 bool
290 Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
291};
292
293} // End namespace elf.
294
295#endif // #ifndef liblldb_ELFHeader_h_