1 //===-- 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 28 namespace lldb_private { 29 class DataExtractor; 30 } // End namespace lldb_private. 31 32 namespace elf { 33 34 //------------------------------------------------------------------------------ 35 /// @name ELF type definitions. 36 /// 37 /// Types used to represent the various components of ELF structures. All types 38 /// are signed or unsigned integral types wide enough to hold values from both 39 /// 32 and 64 bit ELF variants. 40 //@{ 41 typedef uint64_t elf_addr; 42 typedef uint64_t elf_off; 43 typedef uint16_t elf_half; 44 typedef uint32_t elf_word; 45 typedef int32_t elf_sword; 46 typedef uint64_t elf_size; 47 typedef uint64_t elf_xword; 48 typedef int64_t elf_sxword; 49 //@} 50 51 //------------------------------------------------------------------------------ 52 /// @class ELFHeader 53 /// @brief Generic representation of an ELF file header. 54 /// 55 /// This object is used to identify the general attributes on an ELF file and to 56 /// locate additional sections within the file. 57 struct ELFHeader { 58 unsigned char e_ident[llvm::ELF::EI_NIDENT]; ///< ELF file identification. 59 elf_addr e_entry; ///< Virtual address program entry point. 60 elf_off e_phoff; ///< File offset of program header table. 61 elf_off e_shoff; ///< File offset of section header table. 62 elf_word e_flags; ///< Processor specific flags. 63 elf_word e_version; ///< Version of object file (always 1). 64 elf_half e_type; ///< Object file type. 65 elf_half e_machine; ///< Target architecture. 66 elf_half e_ehsize; ///< Byte size of the ELF header. 67 elf_half e_phentsize; ///< Size of a program header table entry. 68 elf_half e_phnum; ///< Number of program header entries. 69 elf_half e_shentsize; ///< Size of a section header table entry. 70 elf_half e_shnum; ///< Number of section header entries. 71 elf_half e_shstrndx; ///< String table section index. 72 73 ELFHeader(); 74 75 //-------------------------------------------------------------------------- 76 /// Returns true if this is a 32 bit ELF file header. 77 /// 78 /// @return 79 /// True if this is a 32 bit ELF file header. 80 bool Is32Bit() const { 81 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32; 82 } 83 84 //-------------------------------------------------------------------------- 85 /// Returns true if this is a 64 bit ELF file header. 86 /// 87 /// @return 88 /// True if this is a 64 bit ELF file header. 89 bool Is64Bit() const { 90 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS64; 91 } 92 93 //-------------------------------------------------------------------------- 94 /// The byte order of this ELF file header. 95 /// 96 /// @return 97 /// The byte order of this ELF file as described by the header. 98 lldb::ByteOrder GetByteOrder() const; 99 100 //-------------------------------------------------------------------------- 101 /// The jump slot relocation type of this ELF. 102 unsigned GetRelocationJumpSlotType() const; 103 104 //-------------------------------------------------------------------------- 105 /// Parse an ELFHeader 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 ELFHeader was successfully read and false 119 /// otherwise. 120 bool Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset); 121 122 //-------------------------------------------------------------------------- 123 /// Examines at most EI_NIDENT bytes starting from the given pointer and 124 /// determines if the magic ELF identification exists. 125 /// 126 /// @return 127 /// True if the given sequence of bytes identifies an ELF file. 128 static bool MagicBytesMatch(const uint8_t *magic); 129 130 //-------------------------------------------------------------------------- 131 /// Examines at most EI_NIDENT bytes starting from the given address and 132 /// determines the address size of the underlying ELF file. This function 133 /// should only be called on an pointer for which MagicBytesMatch returns 134 /// true. 135 /// 136 /// @return 137 /// The number of bytes forming an address in the ELF file (either 4 or 138 /// 8), else zero if the address size could not be determined. 139 static unsigned AddressSizeInBytes(const uint8_t *magic); 140 }; 141 142 //------------------------------------------------------------------------------ 143 /// @class ELFSectionHeader 144 /// @brief Generic representation of an ELF section header. 145 struct ELFSectionHeader { 146 elf_word sh_name; ///< Section name string index. 147 elf_word sh_type; ///< Section type. 148 elf_xword sh_flags; ///< Section attributes. 149 elf_addr sh_addr; ///< Virtual address of the section in memory. 150 elf_off sh_offset; ///< Start of section from beginning of file. 151 elf_xword sh_size; ///< Number of bytes occupied in the file. 152 elf_word sh_link; ///< Index of associated section. 153 elf_word sh_info; ///< Extra section info (overloaded). 154 elf_xword sh_addralign; ///< Power of two alignment constraint. 155 elf_xword sh_entsize; ///< Byte size of each section entry. 156 157 ELFSectionHeader(); 158 159 //-------------------------------------------------------------------------- 160 /// Parse an ELFSectionHeader entry from the given DataExtracter starting at 161 /// position \p offset. 162 /// 163 /// @param[in] data 164 /// The DataExtractor to read from. The address size of the extractor 165 /// determines if a 32 or 64 bit object should be read. 166 /// 167 /// @param[in,out] offset 168 /// Pointer to an offset in the data. On return the offset will be 169 /// advanced by the number of bytes read. 170 /// 171 /// @return 172 /// True if the ELFSectionHeader was successfully read and false 173 /// otherwise. 174 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 175 }; 176 177 //------------------------------------------------------------------------------ 178 /// @class ELFProgramHeader 179 /// @brief Generic representation of an ELF program header. 180 struct ELFProgramHeader { 181 elf_word p_type; ///< Type of program segment. 182 elf_word p_flags; ///< Segment attributes. 183 elf_off p_offset; ///< Start of segment from beginning of file. 184 elf_addr p_vaddr; ///< Virtual address of segment in memory. 185 elf_addr p_paddr; ///< Physical address (for non-VM systems). 186 elf_xword p_filesz; ///< Byte size of the segment in file. 187 elf_xword p_memsz; ///< Byte size of the segment in memory. 188 elf_xword p_align; ///< Segment alignment constraint. 189 190 ELFProgramHeader(); 191 192 /// Parse an ELFProgramHeader entry from the given DataExtractor starting at 193 /// position \p offset. The address size of the DataExtractor determines if 194 /// a 32 or 64 bit object is to be parsed. 195 /// 196 /// @param[in] data 197 /// The DataExtractor to read from. The address size of the extractor 198 /// determines if a 32 or 64 bit object should be read. 199 /// 200 /// @param[in,out] offset 201 /// Pointer to an offset in the data. On return the offset will be 202 /// advanced by the number of bytes read. 203 /// 204 /// @return 205 /// True if the ELFProgramHeader was successfully read and false 206 /// otherwise. 207 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 208 }; 209 210 //------------------------------------------------------------------------------ 211 /// @class ELFSymbol 212 /// @brief Represents a symbol within an ELF symbol table. 213 struct ELFSymbol { 214 elf_addr st_value; ///< Absolute or relocatable address. 215 elf_xword st_size; ///< Size of the symbol or zero. 216 elf_word st_name; ///< Symbol name string index. 217 unsigned char st_info; ///< Symbol type and binding attributes. 218 unsigned char st_other; ///< Reserved for future use. 219 elf_half st_shndx; ///< Section to which this symbol applies. 220 221 ELFSymbol(); 222 223 /// Returns the binding attribute of the st_info member. 224 unsigned char getBinding() const { return st_info >> 4; } 225 226 /// Returns the type attribute of the st_info member. 227 unsigned char getType() const { return st_info & 0x0F; } 228 229 /// Sets the binding and type of the st_info member. 230 void setBindingAndType(unsigned char binding, unsigned char type) { 231 st_info = (binding << 4) + (type & 0x0F); 232 } 233 234 static const char *bindingToCString(unsigned char binding); 235 236 static const char *typeToCString(unsigned char type); 237 238 static const char * 239 sectionIndexToCString(elf_half shndx, 240 const lldb_private::SectionList *section_list); 241 242 /// Parse an ELFSymbol entry from the given DataExtractor starting at 243 /// position \p offset. The address size of the DataExtractor 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 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 257 258 void Dump(lldb_private::Stream *s, uint32_t idx, 259 const lldb_private::DataExtractor *strtab_data, 260 const lldb_private::SectionList *section_list); 261 }; 262 263 //------------------------------------------------------------------------------ 264 /// @class ELFDynamic 265 /// @brief Represents an entry in an ELF dynamic table. 266 struct ELFDynamic { 267 elf_sxword d_tag; ///< Type of dynamic table entry. 268 union { 269 elf_xword d_val; ///< Integer value of the table entry. 270 elf_addr d_ptr; ///< Pointer value of the table entry. 271 }; 272 273 ELFDynamic(); 274 275 /// Parse an ELFDynamic entry from the given DataExtractor starting at 276 /// position \p offset. The address size of the DataExtractor determines if 277 /// a 32 or 64 bit object is to be parsed. 278 /// 279 /// @param[in] data 280 /// The DataExtractor to read from. The address size of the extractor 281 /// determines if a 32 or 64 bit object should be read. 282 /// 283 /// @param[in,out] offset 284 /// Pointer to an offset in the data. On return the offset will be 285 /// advanced by the number of bytes read. 286 /// 287 /// @return 288 /// True if the ELFDynamic entry was successfully read and false 289 /// otherwise. 290 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 291 }; 292 293 //------------------------------------------------------------------------------ 294 /// @class ELFRel 295 /// @brief Represents a relocation entry with an implicit addend. 296 struct ELFRel { 297 elf_addr r_offset; ///< Address of reference. 298 elf_xword r_info; ///< symbol index and type of relocation. 299 300 ELFRel(); 301 302 /// Parse an ELFRel entry from the given DataExtractor starting at position 303 /// \p offset. The address size of the DataExtractor determines if a 32 or 304 /// 64 bit object is to be parsed. 305 /// 306 /// @param[in] data 307 /// The DataExtractor to read from. The address size of the extractor 308 /// determines if a 32 or 64 bit object should be read. 309 /// 310 /// @param[in,out] offset 311 /// Pointer to an offset in the data. On return the offset will be 312 /// advanced by the number of bytes read. 313 /// 314 /// @return 315 /// True if the ELFRel entry was successfully read and false otherwise. 316 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 317 318 /// Returns the type when the given entry represents a 32-bit relocation. 319 static unsigned RelocType32(const ELFRel &rel) { return rel.r_info & 0x0ff; } 320 321 /// Returns the type when the given entry represents a 64-bit relocation. 322 static unsigned RelocType64(const ELFRel &rel) { 323 return rel.r_info & 0xffffffff; 324 } 325 326 /// Returns the symbol index when the given entry represents a 32-bit 327 /// relocation. 328 static unsigned RelocSymbol32(const ELFRel &rel) { return rel.r_info >> 8; } 329 330 /// Returns the symbol index when the given entry represents a 64-bit 331 /// relocation. 332 static unsigned RelocSymbol64(const ELFRel &rel) { return rel.r_info >> 32; } 333 }; 334 335 //------------------------------------------------------------------------------ 336 /// @class ELFRela 337 /// @brief Represents a relocation entry with an explicit addend. 338 struct ELFRela { 339 elf_addr r_offset; ///< Address of reference. 340 elf_xword r_info; ///< Symbol index and type of relocation. 341 elf_sxword r_addend; ///< Constant part of expression. 342 343 ELFRela(); 344 345 /// Parse an ELFRela entry from the given DataExtractor starting at position 346 /// \p offset. The address size of the DataExtractor determines if a 32 or 347 /// 64 bit object is to be parsed. 348 /// 349 /// @param[in] data 350 /// The DataExtractor to read from. The address size of the extractor 351 /// determines if a 32 or 64 bit object should be read. 352 /// 353 /// @param[in,out] offset 354 /// Pointer to an offset in the data. On return the offset will be 355 /// advanced by the number of bytes read. 356 /// 357 /// @return 358 /// True if the ELFRela entry was successfully read and false otherwise. 359 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 360 361 /// Returns the type when the given entry represents a 32-bit relocation. 362 static unsigned RelocType32(const ELFRela &rela) { 363 return rela.r_info & 0x0ff; 364 } 365 366 /// Returns the type when the given entry represents a 64-bit relocation. 367 static unsigned RelocType64(const ELFRela &rela) { 368 return rela.r_info & 0xffffffff; 369 } 370 371 /// Returns the symbol index when the given entry represents a 32-bit 372 /// relocation. 373 static unsigned RelocSymbol32(const ELFRela &rela) { 374 return rela.r_info >> 8; 375 } 376 377 /// Returns the symbol index when the given entry represents a 64-bit 378 /// relocation. 379 static unsigned RelocSymbol64(const ELFRela &rela) { 380 return rela.r_info >> 32; 381 } 382 }; 383 384 } // End namespace elf. 385 386 #endif // #ifndef liblldb_ELFHeader_h_ 387