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