Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 1 | //===-- lib/CodeGen/ELF.h - ELF constants and data structures ---*- 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 | // This header contains common, non-processor-specific data structures and |
| 11 | // constants for the ELF file format. |
| 12 | // |
| 13 | // The details of the ELF32 bits in this file are largely based on |
| 14 | // the Tool Interface Standard (TIS) Executable and Linking Format |
| 15 | // (ELF) Specification Version 1.2, May 1995. The ELF64 stuff is not |
| 16 | // standardized, as far as I can tell. It was largely based on information |
| 17 | // I found in OpenBSD header files. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #ifndef CODEGEN_ELF_H |
| 22 | #define CODEGEN_ELF_H |
| 23 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRelocation.h" |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 25 | #include "llvm/Support/DataTypes.h" |
| 26 | #include <cstring> |
| 27 | |
| 28 | namespace llvm { |
| 29 | class GlobalVariable; |
| 30 | |
| 31 | // Identification Indexes |
| 32 | enum { |
| 33 | EI_MAG0 = 0, |
| 34 | EI_MAG1 = 1, |
| 35 | EI_MAG2 = 2, |
| 36 | EI_MAG3 = 3 |
| 37 | }; |
| 38 | |
| 39 | // File types |
| 40 | enum { |
| 41 | ET_NONE = 0, // No file type |
| 42 | ET_REL = 1, // Relocatable file |
| 43 | ET_EXEC = 2, // Executable file |
| 44 | ET_DYN = 3, // Shared object file |
| 45 | ET_CORE = 4, // Core file |
| 46 | ET_LOPROC = 0xff00, // Beginning of processor-specific codes |
| 47 | ET_HIPROC = 0xffff // Processor-specific |
| 48 | }; |
| 49 | |
| 50 | // Object file classes. |
| 51 | enum { |
| 52 | ELFCLASS32 = 1, // 32-bit object file |
| 53 | ELFCLASS64 = 2 // 64-bit object file |
| 54 | }; |
| 55 | |
| 56 | // Object file byte orderings. |
| 57 | enum { |
| 58 | ELFDATA2LSB = 1, // Little-endian object file |
| 59 | ELFDATA2MSB = 2 // Big-endian object file |
| 60 | }; |
| 61 | |
| 62 | // Versioning |
| 63 | enum { |
| 64 | EV_NONE = 0, |
| 65 | EV_CURRENT = 1 |
| 66 | }; |
| 67 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 68 | struct ELFHeader { |
| 69 | // e_machine - This field is the target specific value to emit as the |
| 70 | // e_machine member of the ELF header. |
| 71 | unsigned short e_machine; |
| 72 | |
| 73 | // e_flags - The machine flags for the target. This defaults to zero. |
| 74 | unsigned e_flags; |
| 75 | |
| 76 | // e_size - Holds the ELF header's size in bytes |
| 77 | unsigned e_ehsize; |
| 78 | |
| 79 | // Endianess and ELF Class (64 or 32 bits) |
| 80 | unsigned ByteOrder; |
| 81 | unsigned ElfClass; |
| 82 | |
| 83 | unsigned getByteOrder() const { return ByteOrder; } |
| 84 | unsigned getElfClass() const { return ElfClass; } |
| 85 | unsigned getSize() const { return e_ehsize; } |
| 86 | unsigned getMachine() const { return e_machine; } |
| 87 | unsigned getFlags() const { return e_flags; } |
| 88 | |
| 89 | ELFHeader(unsigned short machine, unsigned flags, |
| 90 | bool is64Bit, bool isLittleEndian) |
| 91 | : e_machine(machine), e_flags(flags) { |
Bruno Cardoso Lopes | 24f14f1 | 2009-06-07 21:33:20 +0000 | [diff] [blame^] | 92 | ElfClass = is64Bit ? ELFCLASS64 : ELFCLASS32; |
| 93 | ByteOrder = isLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 94 | e_ehsize = is64Bit ? 64 : 52; |
| 95 | } |
| 96 | }; |
| 97 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 98 | /// ELFSection - This struct contains information about each section that is |
| 99 | /// emitted to the file. This is eventually turned into the section header |
| 100 | /// table at the end of the file. |
| 101 | struct ELFSection { |
| 102 | |
| 103 | // ELF specific fields |
| 104 | std::string Name; // Name of the section. |
| 105 | unsigned NameIdx; // Index in .shstrtab of name, once emitted. |
| 106 | unsigned Type; |
| 107 | unsigned Flags; |
| 108 | uint64_t Addr; |
| 109 | unsigned Offset; |
| 110 | unsigned Size; |
| 111 | unsigned Link; |
| 112 | unsigned Info; |
| 113 | unsigned Align; |
| 114 | unsigned EntSize; |
| 115 | |
| 116 | // Section Header Flags |
| 117 | enum { |
| 118 | SHF_WRITE = 1 << 0, // Writable |
| 119 | SHF_ALLOC = 1 << 1, // Mapped into the process addr space |
| 120 | SHF_EXECINSTR = 1 << 2, // Executable |
| 121 | SHF_MERGE = 1 << 4, // Might be merged if equal |
| 122 | SHF_STRINGS = 1 << 5, // Contains null-terminated strings |
| 123 | SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index |
| 124 | SHF_LINK_ORDER = 1 << 7, // Preserve order after combining |
| 125 | SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required |
| 126 | SHF_GROUP = 1 << 9, // Section is a member of a group |
| 127 | SHF_TLS = 1 << 10 // Section holds thread-local data |
| 128 | }; |
| 129 | |
| 130 | // Section Types |
| 131 | enum { |
| 132 | SHT_NULL = 0, // No associated section (inactive entry). |
| 133 | SHT_PROGBITS = 1, // Program-defined contents. |
| 134 | SHT_SYMTAB = 2, // Symbol table. |
| 135 | SHT_STRTAB = 3, // String table. |
| 136 | SHT_RELA = 4, // Relocation entries; explicit addends. |
| 137 | SHT_HASH = 5, // Symbol hash table. |
| 138 | SHT_DYNAMIC = 6, // Information for dynamic linking. |
| 139 | SHT_NOTE = 7, // Information about the file. |
| 140 | SHT_NOBITS = 8, // Data occupies no space in the file. |
| 141 | SHT_REL = 9, // Relocation entries; no explicit addends. |
| 142 | SHT_SHLIB = 10, // Reserved. |
| 143 | SHT_DYNSYM = 11, // Symbol table. |
| 144 | SHT_LOPROC = 0x70000000, // Lowest processor architecture-specific type. |
| 145 | SHT_HIPROC = 0x7fffffff, // Highest processor architecture-specific type. |
| 146 | SHT_LOUSER = 0x80000000, // Lowest type reserved for applications. |
| 147 | SHT_HIUSER = 0xffffffff // Highest type reserved for applications. |
| 148 | }; |
| 149 | |
| 150 | // Special section indices. |
| 151 | enum { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 152 | SHN_UNDEF = 0, // Undefined, missing, irrelevant |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 153 | SHN_LORESERVE = 0xff00, // Lowest reserved index |
| 154 | SHN_LOPROC = 0xff00, // Lowest processor-specific index |
| 155 | SHN_HIPROC = 0xff1f, // Highest processor-specific index |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 156 | SHN_ABS = 0xfff1, // Symbol has absolute value; no relocation |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 157 | SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables |
| 158 | SHN_HIRESERVE = 0xffff // Highest reserved index |
| 159 | }; |
| 160 | |
| 161 | /// SectionIdx - The number of the section in the Section Table. |
| 162 | unsigned short SectionIdx; |
| 163 | |
| 164 | /// SectionData - The actual data for this section which we are building |
| 165 | /// up for emission to the file. |
| 166 | std::vector<unsigned char> SectionData; |
| 167 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 168 | /// Relocations - The relocations that we have encountered so far in this |
| 169 | /// section that we will need to convert to MachORelocation entries when |
| 170 | /// the file is written. |
| 171 | std::vector<MachineRelocation> Relocations; |
| 172 | |
| 173 | /// Section Header Size |
| 174 | static unsigned getSectionHdrSize(bool is64Bit) |
| 175 | { return is64Bit ? 64 : 40; } |
| 176 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 177 | ELFSection(const std::string &name) |
| 178 | : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0), |
| 179 | Link(0), Info(0), Align(0), EntSize(0) {} |
| 180 | }; |
| 181 | |
| 182 | /// ELFSym - This struct contains information about each symbol that is |
| 183 | /// added to logical symbol table for the module. This is eventually |
| 184 | /// turned into a real symbol table in the file. |
| 185 | struct ELFSym { |
| 186 | const GlobalValue *GV; // The global value this corresponds to. |
| 187 | |
| 188 | // ELF specific fields |
| 189 | unsigned NameIdx; // Index in .strtab of name, once emitted. |
| 190 | uint64_t Value; |
| 191 | unsigned Size; |
| 192 | uint8_t Info; |
| 193 | uint8_t Other; |
| 194 | unsigned short SectionIdx; |
| 195 | |
| 196 | enum { |
| 197 | STB_LOCAL = 0, |
| 198 | STB_GLOBAL = 1, |
| 199 | STB_WEAK = 2 |
| 200 | }; |
| 201 | |
| 202 | enum { |
| 203 | STT_NOTYPE = 0, |
| 204 | STT_OBJECT = 1, |
| 205 | STT_FUNC = 2, |
| 206 | STT_SECTION = 3, |
| 207 | STT_FILE = 4 |
| 208 | }; |
| 209 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 210 | ELFSym(const GlobalValue *gv) : GV(gv), NameIdx(0), Value(0), |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 211 | Size(0), Info(0), Other(0), |
| 212 | SectionIdx(ELFSection::SHN_UNDEF) {} |
| 213 | |
| 214 | void SetBind(unsigned X) { |
| 215 | assert(X == (X & 0xF) && "Bind value out of range!"); |
| 216 | Info = (Info & 0x0F) | (X << 4); |
| 217 | } |
| 218 | void SetType(unsigned X) { |
| 219 | assert(X == (X & 0xF) && "Type value out of range!"); |
| 220 | Info = (Info & 0xF0) | X; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | } // end namespace llvm |
| 225 | |
| 226 | #endif |