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" |
Eli Friedman | 10bb421 | 2010-07-16 07:53:29 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/ELF.h" |
Chandler Carruth | 8b67f77 | 2009-10-26 01:35:46 +0000 | [diff] [blame] | 26 | #include "llvm/System/DataTypes.h" |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 27 | |
| 28 | namespace llvm { |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 29 | class GlobalValue; |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 30 | |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 31 | /// ELFSym - This struct contains information about each symbol that is |
| 32 | /// added to logical symbol table for the module. This is eventually |
| 33 | /// turned into a real symbol table in the file. |
| 34 | struct ELFSym { |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 35 | |
| 36 | // ELF symbols are related to llvm ones by being one of the two llvm |
| 37 | // types, for the other ones (section, file, func) a null pointer is |
Bruno Cardoso Lopes | df0b650 | 2009-07-27 19:32:57 +0000 | [diff] [blame] | 38 | // assumed by default. |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 39 | union { |
| 40 | const GlobalValue *GV; // If this is a pointer to a GV |
Bruno Cardoso Lopes | df0b650 | 2009-07-27 19:32:57 +0000 | [diff] [blame] | 41 | const char *Ext; // If this is a pointer to a named symbol |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 42 | } Source; |
| 43 | |
| 44 | // Describes from which source type this ELF symbol comes from, |
| 45 | // they can be GlobalValue, ExternalSymbol or neither. |
| 46 | enum { |
| 47 | isGV, // The Source.GV field is valid. |
| 48 | isExtSym, // The Source.ExtSym field is valid. |
| 49 | isOther // Not a GlobalValue or External Symbol |
| 50 | }; |
| 51 | unsigned SourceType; |
| 52 | |
Bruno Cardoso Lopes | d163e8b | 2009-08-13 21:25:27 +0000 | [diff] [blame] | 53 | bool isGlobalValue() const { return SourceType == isGV; } |
| 54 | bool isExternalSym() const { return SourceType == isExtSym; } |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 55 | |
| 56 | // getGlobalValue - If this is a global value which originated the |
| 57 | // elf symbol, return a reference to it. |
Bruno Cardoso Lopes | d163e8b | 2009-08-13 21:25:27 +0000 | [diff] [blame] | 58 | const GlobalValue *getGlobalValue() const { |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 59 | assert(SourceType == isGV && "This is not a global value"); |
| 60 | return Source.GV; |
Douglas Gregor | cabdd74 | 2009-12-19 07:05:23 +0000 | [diff] [blame] | 61 | } |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 62 | |
| 63 | // getExternalSym - If this is an external symbol which originated the |
| 64 | // elf symbol, return a reference to it. |
Bruno Cardoso Lopes | d163e8b | 2009-08-13 21:25:27 +0000 | [diff] [blame] | 65 | const char *getExternalSymbol() const { |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 66 | assert(SourceType == isExtSym && "This is not an external symbol"); |
| 67 | return Source.Ext; |
Douglas Gregor | cabdd74 | 2009-12-19 07:05:23 +0000 | [diff] [blame] | 68 | } |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 69 | |
| 70 | // getGV - From a global value return a elf symbol to represent it |
| 71 | static ELFSym *getGV(const GlobalValue *GV, unsigned Bind, |
| 72 | unsigned Type, unsigned Visibility) { |
| 73 | ELFSym *Sym = new ELFSym(); |
| 74 | Sym->Source.GV = GV; |
| 75 | Sym->setBind(Bind); |
| 76 | Sym->setType(Type); |
| 77 | Sym->setVisibility(Visibility); |
| 78 | Sym->SourceType = isGV; |
| 79 | return Sym; |
| 80 | } |
| 81 | |
| 82 | // getExtSym - Create and return an elf symbol to represent an |
| 83 | // external symbol |
| 84 | static ELFSym *getExtSym(const char *Ext) { |
| 85 | ELFSym *Sym = new ELFSym(); |
| 86 | Sym->Source.Ext = Ext; |
Eli Friedman | 10bb421 | 2010-07-16 07:53:29 +0000 | [diff] [blame^] | 87 | Sym->setBind(ELF::STB_GLOBAL); |
| 88 | Sym->setType(ELF::STT_NOTYPE); |
| 89 | Sym->setVisibility(ELF::STV_DEFAULT); |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 90 | Sym->SourceType = isExtSym; |
| 91 | return Sym; |
| 92 | } |
| 93 | |
| 94 | // getSectionSym - Returns a elf symbol to represent an elf section |
| 95 | static ELFSym *getSectionSym() { |
| 96 | ELFSym *Sym = new ELFSym(); |
Eli Friedman | 10bb421 | 2010-07-16 07:53:29 +0000 | [diff] [blame^] | 97 | Sym->setBind(ELF::STB_LOCAL); |
| 98 | Sym->setType(ELF::STT_SECTION); |
| 99 | Sym->setVisibility(ELF::STV_DEFAULT); |
Bruno Cardoso Lopes | df0b650 | 2009-07-27 19:32:57 +0000 | [diff] [blame] | 100 | Sym->SourceType = isOther; |
| 101 | return Sym; |
| 102 | } |
| 103 | |
Bruno Cardoso Lopes | 4228656 | 2009-07-27 19:38:38 +0000 | [diff] [blame] | 104 | // getFileSym - Returns a elf symbol to represent the module identifier |
Bruno Cardoso Lopes | df0b650 | 2009-07-27 19:32:57 +0000 | [diff] [blame] | 105 | static ELFSym *getFileSym() { |
| 106 | ELFSym *Sym = new ELFSym(); |
Eli Friedman | 10bb421 | 2010-07-16 07:53:29 +0000 | [diff] [blame^] | 107 | Sym->setBind(ELF::STB_LOCAL); |
| 108 | Sym->setType(ELF::STT_FILE); |
| 109 | Sym->setVisibility(ELF::STV_DEFAULT); |
Bruno Cardoso Lopes | df0b650 | 2009-07-27 19:32:57 +0000 | [diff] [blame] | 110 | Sym->SectionIdx = 0xfff1; // ELFSection::SHN_ABS; |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 111 | Sym->SourceType = isOther; |
| 112 | return Sym; |
| 113 | } |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 114 | |
Bruno Cardoso Lopes | 3e0094d | 2009-08-08 17:29:04 +0000 | [diff] [blame] | 115 | // getUndefGV - Returns a STT_NOTYPE symbol |
| 116 | static ELFSym *getUndefGV(const GlobalValue *GV, unsigned Bind) { |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 117 | ELFSym *Sym = new ELFSym(); |
| 118 | Sym->Source.GV = GV; |
Bruno Cardoso Lopes | 3e0094d | 2009-08-08 17:29:04 +0000 | [diff] [blame] | 119 | Sym->setBind(Bind); |
Eli Friedman | 10bb421 | 2010-07-16 07:53:29 +0000 | [diff] [blame^] | 120 | Sym->setType(ELF::STT_NOTYPE); |
| 121 | Sym->setVisibility(ELF::STV_DEFAULT); |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 122 | Sym->SectionIdx = 0; //ELFSection::SHN_UNDEF; |
| 123 | Sym->SourceType = isGV; |
| 124 | return Sym; |
| 125 | } |
| 126 | |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 127 | // ELF specific fields |
| 128 | unsigned NameIdx; // Index in .strtab of name, once emitted. |
| 129 | uint64_t Value; |
| 130 | unsigned Size; |
| 131 | uint8_t Info; |
| 132 | uint8_t Other; |
| 133 | unsigned short SectionIdx; |
| 134 | |
| 135 | // Symbol index into the Symbol table |
| 136 | unsigned SymTabIdx; |
| 137 | |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 138 | ELFSym() : SourceType(isOther), NameIdx(0), Value(0), |
Eli Friedman | 10bb421 | 2010-07-16 07:53:29 +0000 | [diff] [blame^] | 139 | Size(0), Info(0), Other(ELF::STV_DEFAULT), SectionIdx(0), |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 140 | SymTabIdx(0) {} |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 141 | |
| 142 | unsigned getBind() const { return (Info >> 4) & 0xf; } |
| 143 | unsigned getType() const { return Info & 0xf; } |
Eli Friedman | 10bb421 | 2010-07-16 07:53:29 +0000 | [diff] [blame^] | 144 | bool isLocalBind() const { return getBind() == ELF::STB_LOCAL; } |
| 145 | bool isFileType() const { return getType() == ELF::STT_FILE; } |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 146 | |
| 147 | void setBind(unsigned X) { |
| 148 | assert(X == (X & 0xF) && "Bind value out of range!"); |
| 149 | Info = (Info & 0x0F) | (X << 4); |
| 150 | } |
| 151 | |
| 152 | void setType(unsigned X) { |
| 153 | assert(X == (X & 0xF) && "Type value out of range!"); |
| 154 | Info = (Info & 0xF0) | X; |
| 155 | } |
| 156 | |
| 157 | void setVisibility(unsigned V) { |
| 158 | assert(V == (V & 0x3) && "Visibility value out of range!"); |
| 159 | Other = V; |
| 160 | } |
| 161 | }; |
| 162 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 163 | /// ELFSection - This struct contains information about each section that is |
| 164 | /// emitted to the file. This is eventually turned into the section header |
| 165 | /// table at the end of the file. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 166 | class ELFSection : public BinaryObject { |
| 167 | public: |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 168 | // ELF specific fields |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 169 | unsigned NameIdx; // sh_name - .shstrtab idx of name, once emitted. |
| 170 | unsigned Type; // sh_type - Section contents & semantics |
| 171 | unsigned Flags; // sh_flags - Section flags. |
| 172 | uint64_t Addr; // sh_addr - The mem addr this section is in. |
| 173 | unsigned Offset; // sh_offset - Offset from the file start |
| 174 | unsigned Size; // sh_size - The section size. |
| 175 | unsigned Link; // sh_link - Section header table index link. |
| 176 | unsigned Info; // sh_info - Auxillary information. |
| 177 | unsigned Align; // sh_addralign - Alignment of section. |
| 178 | unsigned EntSize; // sh_entsize - Size of entries in the section e |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 179 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 180 | /// SectionIdx - The number of the section in the Section Table. |
| 181 | unsigned short SectionIdx; |
| 182 | |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 183 | /// Sym - The symbol to represent this section if it has one. |
| 184 | ELFSym *Sym; |
| 185 | |
Bruno Cardoso Lopes | 171375f | 2009-07-18 19:30:09 +0000 | [diff] [blame] | 186 | /// getSymIndex - Returns the symbol table index of the symbol |
| 187 | /// representing this section. |
| 188 | unsigned getSymbolTableIndex() const { |
| 189 | assert(Sym && "section not present in the symbol table"); |
| 190 | return Sym->SymTabIdx; |
| 191 | } |
| 192 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 193 | ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit) |
| 194 | : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0), |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 195 | Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {} |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 198 | /// ELFRelocation - This class contains all the information necessary to |
| 199 | /// to generate any 32-bit or 64-bit ELF relocation entry. |
| 200 | class ELFRelocation { |
| 201 | uint64_t r_offset; // offset in the section of the object this applies to |
| 202 | uint32_t r_symidx; // symbol table index of the symbol to use |
| 203 | uint32_t r_type; // machine specific relocation type |
| 204 | int64_t r_add; // explicit relocation addend |
| 205 | bool r_rela; // if true then the addend is part of the entry |
| 206 | // otherwise the addend is at the location specified |
| 207 | // by r_offset |
| 208 | public: |
| 209 | uint64_t getInfo(bool is64Bit) const { |
| 210 | if (is64Bit) |
| 211 | return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL); |
| 212 | else |
| 213 | return (r_symidx << 8) + (r_type & 0xFFL); |
| 214 | } |
| 215 | |
| 216 | uint64_t getOffset() const { return r_offset; } |
| 217 | int64_t getAddend() const { return r_add; } |
| 218 | |
| 219 | ELFRelocation(uint64_t off, uint32_t sym, uint32_t type, |
| 220 | bool rela = true, int64_t addend = 0) : |
| 221 | r_offset(off), r_symidx(sym), r_type(type), |
| 222 | r_add(addend), r_rela(rela) {} |
| 223 | }; |
| 224 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 225 | } // end namespace llvm |
| 226 | |
| 227 | #endif |