1 //===-- lib/CodeGen/ELF.h - ELF constants and data structures ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header contains common, non-processor-specific data structures and
11 // constants for the ELF file format.
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
18 //===----------------------------------------------------------------------===//
23 #include "llvm/CodeGen/BinaryObject.h"
24 #include "llvm/CodeGen/MachineRelocation.h"
25 #include "llvm/Support/DataTypes.h"
30 // Identification Indexes
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
55 /// ELFSym - This struct contains information about each symbol that is
56 /// added to logical symbol table for the module. This is eventually
57 /// turned into a real symbol table in the file.
60 // ELF symbols are related to llvm ones by being one of the two llvm
61 // types, for the other ones (section, file, func) a null pointer is
64 const GlobalValue *GV; // If this is a pointer to a GV
65 const char *Ext; // If this is a pointer to a named symbol
68 // Describes from which source type this ELF symbol comes from,
69 // they can be GlobalValue, ExternalSymbol or neither.
71 isGV, // The Source.GV field is valid.
72 isExtSym, // The Source.ExtSym field is valid.
73 isOther // Not a GlobalValue or External Symbol
77 bool isGlobalValue() { return SourceType == isGV; }
78 bool isExternalSym() { return SourceType == isExtSym; }
80 // getGlobalValue - If this is a global value which originated the
81 // elf symbol, return a reference to it.
82 const GlobalValue *getGlobalValue() {
83 assert(SourceType == isGV && "This is not a global value");
87 // getExternalSym - If this is an external symbol which originated the
88 // elf symbol, return a reference to it.
89 const char *getExternalSymbol() {
90 assert(SourceType == isExtSym && "This is not an external symbol");
94 // getGV - From a global value return a elf symbol to represent it
95 static ELFSym *getGV(const GlobalValue *GV, unsigned Bind,
96 unsigned Type, unsigned Visibility) {
97 ELFSym *Sym = new ELFSym();
101 Sym->setVisibility(Visibility);
102 Sym->SourceType = isGV;
106 // getExtSym - Create and return an elf symbol to represent an
108 static ELFSym *getExtSym(const char *Ext) {
109 ELFSym *Sym = new ELFSym();
110 Sym->Source.Ext = Ext;
111 Sym->setBind(STB_GLOBAL);
112 Sym->setType(STT_NOTYPE);
113 Sym->setVisibility(STV_DEFAULT);
114 Sym->SourceType = isExtSym;
118 // getSectionSym - Returns a elf symbol to represent an elf section
119 static ELFSym *getSectionSym() {
120 ELFSym *Sym = new ELFSym();
121 Sym->setBind(ELFSym::STB_LOCAL);
122 Sym->setType(ELFSym::STT_SECTION);
123 Sym->setVisibility(ELFSym::STV_DEFAULT);
124 Sym->SourceType = isOther;
128 // ELF specific fields
129 unsigned NameIdx; // Index in .strtab of name, once emitted.
134 unsigned short SectionIdx;
136 // Symbol index into the Symbol table
140 STB_LOCAL = 0, // Local sym, not visible outside obj file containing def
141 STB_GLOBAL = 1, // Global sym, visible to all object files being combined
142 STB_WEAK = 2 // Weak symbol, like global but lower-precedence
146 STT_NOTYPE = 0, // Symbol's type is not specified
147 STT_OBJECT = 1, // Symbol is a data object (variable, array, etc.)
148 STT_FUNC = 2, // Symbol is executable code (function, etc.)
149 STT_SECTION = 3, // Symbol refers to a section
150 STT_FILE = 4 // Local, absolute symbol that refers to a file
154 STV_DEFAULT = 0, // Visibility is specified by binding type
155 STV_INTERNAL = 1, // Defined by processor supplements
156 STV_HIDDEN = 2, // Not visible to other components
157 STV_PROTECTED = 3 // Visible in other components but not preemptable
160 ELFSym() : SourceType(isOther), NameIdx(0), Value(0),
161 Size(0), Info(0), Other(STV_DEFAULT), SectionIdx(0),
164 unsigned getBind() const { return (Info >> 4) & 0xf; }
165 unsigned getType() const { return Info & 0xf; }
166 bool isLocalBind() const { return getBind() == STB_LOCAL; }
168 void setBind(unsigned X) {
169 assert(X == (X & 0xF) && "Bind value out of range!");
170 Info = (Info & 0x0F) | (X << 4);
173 void setType(unsigned X) {
174 assert(X == (X & 0xF) && "Type value out of range!");
175 Info = (Info & 0xF0) | X;
178 void setVisibility(unsigned V) {
179 assert(V == (V & 0x3) && "Visibility value out of range!");
184 /// ELFSection - This struct contains information about each section that is
185 /// emitted to the file. This is eventually turned into the section header
186 /// table at the end of the file.
187 class ELFSection : public BinaryObject {
189 // ELF specific fields
190 unsigned NameIdx; // sh_name - .shstrtab idx of name, once emitted.
191 unsigned Type; // sh_type - Section contents & semantics
192 unsigned Flags; // sh_flags - Section flags.
193 uint64_t Addr; // sh_addr - The mem addr this section is in.
194 unsigned Offset; // sh_offset - Offset from the file start
195 unsigned Size; // sh_size - The section size.
196 unsigned Link; // sh_link - Section header table index link.
197 unsigned Info; // sh_info - Auxillary information.
198 unsigned Align; // sh_addralign - Alignment of section.
199 unsigned EntSize; // sh_entsize - Size of entries in the section e
201 // Section Header Flags
203 SHF_WRITE = 1 << 0, // Writable
204 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
205 SHF_EXECINSTR = 1 << 2, // Executable
206 SHF_MERGE = 1 << 4, // Might be merged if equal
207 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
208 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
209 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
210 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
211 SHF_GROUP = 1 << 9, // Section is a member of a group
212 SHF_TLS = 1 << 10 // Section holds thread-local data
217 SHT_NULL = 0, // No associated section (inactive entry).
218 SHT_PROGBITS = 1, // Program-defined contents.
219 SHT_SYMTAB = 2, // Symbol table.
220 SHT_STRTAB = 3, // String table.
221 SHT_RELA = 4, // Relocation entries; explicit addends.
222 SHT_HASH = 5, // Symbol hash table.
223 SHT_DYNAMIC = 6, // Information for dynamic linking.
224 SHT_NOTE = 7, // Information about the file.
225 SHT_NOBITS = 8, // Data occupies no space in the file.
226 SHT_REL = 9, // Relocation entries; no explicit addends.
227 SHT_SHLIB = 10, // Reserved.
228 SHT_DYNSYM = 11, // Symbol table.
229 SHT_LOPROC = 0x70000000, // Lowest processor arch-specific type.
230 SHT_HIPROC = 0x7fffffff, // Highest processor arch-specific type.
231 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
232 SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
235 // Special section indices.
237 SHN_UNDEF = 0, // Undefined, missing, irrelevant
238 SHN_LORESERVE = 0xff00, // Lowest reserved index
239 SHN_LOPROC = 0xff00, // Lowest processor-specific index
240 SHN_HIPROC = 0xff1f, // Highest processor-specific index
241 SHN_ABS = 0xfff1, // Symbol has absolute value; no relocation
242 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
243 SHN_HIRESERVE = 0xffff // Highest reserved index
246 /// SectionIdx - The number of the section in the Section Table.
247 unsigned short SectionIdx;
249 /// Sym - The symbol to represent this section if it has one.
252 /// getSymIndex - Returns the symbol table index of the symbol
253 /// representing this section.
254 unsigned getSymbolTableIndex() const {
255 assert(Sym && "section not present in the symbol table");
256 return Sym->SymTabIdx;
259 ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
260 : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
261 Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
264 /// ELFRelocation - This class contains all the information necessary to
265 /// to generate any 32-bit or 64-bit ELF relocation entry.
266 class ELFRelocation {
267 uint64_t r_offset; // offset in the section of the object this applies to
268 uint32_t r_symidx; // symbol table index of the symbol to use
269 uint32_t r_type; // machine specific relocation type
270 int64_t r_add; // explicit relocation addend
271 bool r_rela; // if true then the addend is part of the entry
272 // otherwise the addend is at the location specified
275 uint64_t getInfo(bool is64Bit) const {
277 return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
279 return (r_symidx << 8) + (r_type & 0xFFL);
282 uint64_t getOffset() const { return r_offset; }
283 int64_t getAddend() const { return r_add; }
285 ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
286 bool rela = true, int64_t addend = 0) :
287 r_offset(off), r_symidx(sym), r_type(type),
288 r_add(addend), r_rela(rela) {}
291 } // end namespace llvm