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 | // |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 13 | // The details of the ELF32 bits in this file are largely based on the Tool |
| 14 | // Interface Standard (TIS) Executable and Linking Format (ELF) Specification |
| 15 | // Version 1.2, May 1995. The ELF64 is based on HP/Intel definition of the |
| 16 | // ELF-64 object file format document, Version 1.5 Draft 2 May 27, 1998 |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #ifndef CODEGEN_ELF_H |
| 21 | #define CODEGEN_ELF_H |
| 22 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/BinaryObject.h" |
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" |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 28 | class GlobalValue; |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 29 | |
| 30 | // Identification Indexes |
| 31 | enum { |
| 32 | EI_MAG0 = 0, |
| 33 | EI_MAG1 = 1, |
| 34 | EI_MAG2 = 2, |
| 35 | EI_MAG3 = 3 |
| 36 | }; |
| 37 | |
| 38 | // File types |
| 39 | enum { |
| 40 | ET_NONE = 0, // No file type |
| 41 | ET_REL = 1, // Relocatable file |
| 42 | ET_EXEC = 2, // Executable file |
| 43 | ET_DYN = 3, // Shared object file |
| 44 | ET_CORE = 4, // Core file |
| 45 | ET_LOPROC = 0xff00, // Beginning of processor-specific codes |
| 46 | ET_HIPROC = 0xffff // Processor-specific |
| 47 | }; |
| 48 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 49 | // Versioning |
| 50 | enum { |
| 51 | EV_NONE = 0, |
| 52 | EV_CURRENT = 1 |
| 53 | }; |
| 54 | |
| 55 | /// ELFSection - This struct contains information about each section that is |
| 56 | /// emitted to the file. This is eventually turned into the section header |
| 57 | /// table at the end of the file. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 58 | class ELFSection : public BinaryObject { |
| 59 | public: |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 60 | // ELF specific fields |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 61 | unsigned NameIdx; // sh_name - .shstrtab idx of name, once emitted. |
| 62 | unsigned Type; // sh_type - Section contents & semantics |
| 63 | unsigned Flags; // sh_flags - Section flags. |
| 64 | uint64_t Addr; // sh_addr - The mem addr this section is in. |
| 65 | unsigned Offset; // sh_offset - Offset from the file start |
| 66 | unsigned Size; // sh_size - The section size. |
| 67 | unsigned Link; // sh_link - Section header table index link. |
| 68 | unsigned Info; // sh_info - Auxillary information. |
| 69 | unsigned Align; // sh_addralign - Alignment of section. |
| 70 | unsigned EntSize; // sh_entsize - Size of entries in the section e |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 71 | |
| 72 | // Section Header Flags |
| 73 | enum { |
| 74 | SHF_WRITE = 1 << 0, // Writable |
| 75 | SHF_ALLOC = 1 << 1, // Mapped into the process addr space |
| 76 | SHF_EXECINSTR = 1 << 2, // Executable |
| 77 | SHF_MERGE = 1 << 4, // Might be merged if equal |
| 78 | SHF_STRINGS = 1 << 5, // Contains null-terminated strings |
| 79 | SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index |
| 80 | SHF_LINK_ORDER = 1 << 7, // Preserve order after combining |
| 81 | SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required |
| 82 | SHF_GROUP = 1 << 9, // Section is a member of a group |
| 83 | SHF_TLS = 1 << 10 // Section holds thread-local data |
| 84 | }; |
| 85 | |
| 86 | // Section Types |
| 87 | enum { |
| 88 | SHT_NULL = 0, // No associated section (inactive entry). |
| 89 | SHT_PROGBITS = 1, // Program-defined contents. |
| 90 | SHT_SYMTAB = 2, // Symbol table. |
| 91 | SHT_STRTAB = 3, // String table. |
| 92 | SHT_RELA = 4, // Relocation entries; explicit addends. |
| 93 | SHT_HASH = 5, // Symbol hash table. |
| 94 | SHT_DYNAMIC = 6, // Information for dynamic linking. |
| 95 | SHT_NOTE = 7, // Information about the file. |
| 96 | SHT_NOBITS = 8, // Data occupies no space in the file. |
| 97 | SHT_REL = 9, // Relocation entries; no explicit addends. |
| 98 | SHT_SHLIB = 10, // Reserved. |
| 99 | SHT_DYNSYM = 11, // Symbol table. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 100 | SHT_LOPROC = 0x70000000, // Lowest processor arch-specific type. |
| 101 | SHT_HIPROC = 0x7fffffff, // Highest processor arch-specific type. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 102 | SHT_LOUSER = 0x80000000, // Lowest type reserved for applications. |
| 103 | SHT_HIUSER = 0xffffffff // Highest type reserved for applications. |
| 104 | }; |
| 105 | |
| 106 | // Special section indices. |
| 107 | enum { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 108 | SHN_UNDEF = 0, // Undefined, missing, irrelevant |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 109 | SHN_LORESERVE = 0xff00, // Lowest reserved index |
| 110 | SHN_LOPROC = 0xff00, // Lowest processor-specific index |
| 111 | SHN_HIPROC = 0xff1f, // Highest processor-specific index |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 112 | SHN_ABS = 0xfff1, // Symbol has absolute value; no relocation |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 113 | SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables |
| 114 | SHN_HIRESERVE = 0xffff // Highest reserved index |
| 115 | }; |
| 116 | |
| 117 | /// SectionIdx - The number of the section in the Section Table. |
| 118 | unsigned short SectionIdx; |
| 119 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 120 | ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit) |
| 121 | : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0), |
| 122 | Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0) {} |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /// ELFSym - This struct contains information about each symbol that is |
| 126 | /// added to logical symbol table for the module. This is eventually |
| 127 | /// turned into a real symbol table in the file. |
| 128 | struct ELFSym { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 129 | // The global value this corresponds to. Global symbols can be on of the |
| 130 | // 3 types : if this symbol has a zero initializer, it is common or should |
| 131 | // be placed in bss section otherwise it's a constant. |
| 132 | const GlobalValue *GV; |
| 133 | bool IsCommon; |
| 134 | bool IsBss; |
| 135 | bool IsConstant; |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 136 | |
| 137 | // ELF specific fields |
| 138 | unsigned NameIdx; // Index in .strtab of name, once emitted. |
| 139 | uint64_t Value; |
| 140 | unsigned Size; |
| 141 | uint8_t Info; |
| 142 | uint8_t Other; |
| 143 | unsigned short SectionIdx; |
| 144 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 145 | // Symbol index into the Symbol table |
| 146 | unsigned SymTabIdx; |
| 147 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 148 | enum { |
| 149 | STB_LOCAL = 0, |
| 150 | STB_GLOBAL = 1, |
| 151 | STB_WEAK = 2 |
| 152 | }; |
| 153 | |
| 154 | enum { |
| 155 | STT_NOTYPE = 0, |
| 156 | STT_OBJECT = 1, |
| 157 | STT_FUNC = 2, |
| 158 | STT_SECTION = 3, |
| 159 | STT_FILE = 4 |
| 160 | }; |
| 161 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 162 | enum { |
| 163 | STV_DEFAULT = 0, // Visibility is specified by binding type |
| 164 | STV_INTERNAL = 1, // Defined by processor supplements |
| 165 | STV_HIDDEN = 2, // Not visible to other components |
| 166 | STV_PROTECTED = 3 // Visible in other components but not preemptable |
| 167 | }; |
| 168 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 169 | ELFSym(const GlobalValue *gv) : GV(gv), IsCommon(false), IsBss(false), |
| 170 | IsConstant(false), NameIdx(0), Value(0), |
| 171 | Size(0), Info(0), Other(STV_DEFAULT), |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 172 | SectionIdx(ELFSection::SHN_UNDEF), |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 173 | SymTabIdx(0) {} |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 174 | |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 175 | unsigned getBind() { return (Info >> 4) & 0xf; } |
| 176 | unsigned getType() { return Info & 0xf; } |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 177 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 178 | void setBind(unsigned X) { |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 179 | assert(X == (X & 0xF) && "Bind value out of range!"); |
| 180 | Info = (Info & 0x0F) | (X << 4); |
| 181 | } |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 182 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 183 | void setType(unsigned X) { |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 184 | assert(X == (X & 0xF) && "Type value out of range!"); |
| 185 | Info = (Info & 0xF0) | X; |
| 186 | } |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 187 | |
| 188 | void setVisibility(unsigned V) { |
| 189 | assert(V == (V & 0x3) && "Type value out of range!"); |
| 190 | Other = V; |
| 191 | } |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 194 | /// ELFRelocation - This class contains all the information necessary to |
| 195 | /// to generate any 32-bit or 64-bit ELF relocation entry. |
| 196 | class ELFRelocation { |
| 197 | uint64_t r_offset; // offset in the section of the object this applies to |
| 198 | uint32_t r_symidx; // symbol table index of the symbol to use |
| 199 | uint32_t r_type; // machine specific relocation type |
| 200 | int64_t r_add; // explicit relocation addend |
| 201 | bool r_rela; // if true then the addend is part of the entry |
| 202 | // otherwise the addend is at the location specified |
| 203 | // by r_offset |
| 204 | public: |
| 205 | uint64_t getInfo(bool is64Bit) const { |
| 206 | if (is64Bit) |
| 207 | return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL); |
| 208 | else |
| 209 | return (r_symidx << 8) + (r_type & 0xFFL); |
| 210 | } |
| 211 | |
| 212 | uint64_t getOffset() const { return r_offset; } |
| 213 | int64_t getAddend() const { return r_add; } |
| 214 | |
| 215 | ELFRelocation(uint64_t off, uint32_t sym, uint32_t type, |
| 216 | bool rela = true, int64_t addend = 0) : |
| 217 | r_offset(off), r_symidx(sym), r_type(type), |
| 218 | r_add(addend), r_rela(rela) {} |
| 219 | }; |
| 220 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 221 | } // end namespace llvm |
| 222 | |
| 223 | #endif |