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/GlobalVariable.h"
24 #include "llvm/CodeGen/BinaryObject.h"
25 #include "llvm/CodeGen/MachineRelocation.h"
26 #include "llvm/Support/DataTypes.h"
32 // Identification Indexes
42 ET_NONE = 0, // No file type
43 ET_REL = 1, // Relocatable file
44 ET_EXEC = 2, // Executable file
45 ET_DYN = 3, // Shared object file
46 ET_CORE = 4, // Core file
47 ET_LOPROC = 0xff00, // Beginning of processor-specific codes
48 ET_HIPROC = 0xffff // Processor-specific
57 /// ELFSection - This struct contains information about each section that is
58 /// emitted to the file. This is eventually turned into the section header
59 /// table at the end of the file.
60 class ELFSection : public BinaryObject {
62 // ELF specific fields
63 unsigned NameIdx; // sh_name - .shstrtab idx of name, once emitted.
64 unsigned Type; // sh_type - Section contents & semantics
65 unsigned Flags; // sh_flags - Section flags.
66 uint64_t Addr; // sh_addr - The mem addr this section is in.
67 unsigned Offset; // sh_offset - Offset from the file start
68 unsigned Size; // sh_size - The section size.
69 unsigned Link; // sh_link - Section header table index link.
70 unsigned Info; // sh_info - Auxillary information.
71 unsigned Align; // sh_addralign - Alignment of section.
72 unsigned EntSize; // sh_entsize - Size of entries in the section e
74 // Section Header Flags
76 SHF_WRITE = 1 << 0, // Writable
77 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
78 SHF_EXECINSTR = 1 << 2, // Executable
79 SHF_MERGE = 1 << 4, // Might be merged if equal
80 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
81 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
82 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
83 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
84 SHF_GROUP = 1 << 9, // Section is a member of a group
85 SHF_TLS = 1 << 10 // Section holds thread-local data
90 SHT_NULL = 0, // No associated section (inactive entry).
91 SHT_PROGBITS = 1, // Program-defined contents.
92 SHT_SYMTAB = 2, // Symbol table.
93 SHT_STRTAB = 3, // String table.
94 SHT_RELA = 4, // Relocation entries; explicit addends.
95 SHT_HASH = 5, // Symbol hash table.
96 SHT_DYNAMIC = 6, // Information for dynamic linking.
97 SHT_NOTE = 7, // Information about the file.
98 SHT_NOBITS = 8, // Data occupies no space in the file.
99 SHT_REL = 9, // Relocation entries; no explicit addends.
100 SHT_SHLIB = 10, // Reserved.
101 SHT_DYNSYM = 11, // Symbol table.
102 SHT_LOPROC = 0x70000000, // Lowest processor arch-specific type.
103 SHT_HIPROC = 0x7fffffff, // Highest processor arch-specific type.
104 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
105 SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
108 // Special section indices.
110 SHN_UNDEF = 0, // Undefined, missing, irrelevant
111 SHN_LORESERVE = 0xff00, // Lowest reserved index
112 SHN_LOPROC = 0xff00, // Lowest processor-specific index
113 SHN_HIPROC = 0xff1f, // Highest processor-specific index
114 SHN_ABS = 0xfff1, // Symbol has absolute value; no relocation
115 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
116 SHN_HIRESERVE = 0xffff // Highest reserved index
119 /// SectionIdx - The number of the section in the Section Table.
120 unsigned short SectionIdx;
122 ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
123 : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
124 Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0) {}
127 /// ELFSym - This struct contains information about each symbol that is
128 /// added to logical symbol table for the module. This is eventually
129 /// turned into a real symbol table in the file.
131 // The global value this corresponds to. Global symbols can be on of the
132 // 3 types : if this symbol has a zero initializer, it is common or should
133 // be placed in bss section otherwise it's a constant.
134 const GlobalValue *GV;
139 // ELF specific fields
140 unsigned NameIdx; // Index in .strtab of name, once emitted.
145 unsigned short SectionIdx;
147 // Symbol index into the Symbol table
165 STV_DEFAULT = 0, // Visibility is specified by binding type
166 STV_INTERNAL = 1, // Defined by processor supplements
167 STV_HIDDEN = 2, // Not visible to other components
168 STV_PROTECTED = 3 // Visible in other components but not preemptable
171 ELFSym(const GlobalValue *gv) : GV(gv), IsCommon(false), IsBss(false),
172 IsConstant(false), NameIdx(0), Value(0),
173 Size(0), Info(0), Other(STV_DEFAULT),
174 SectionIdx(ELFSection::SHN_UNDEF),
179 switch (GV->getVisibility()) {
181 assert(0 && "unknown visibility type");
182 case GlobalValue::DefaultVisibility:
185 case GlobalValue::HiddenVisibility:
188 case GlobalValue::ProtectedVisibility:
189 Other = STV_PROTECTED;
195 return (Info >> 4) & 0xf;
202 void setBind(unsigned X) {
203 assert(X == (X & 0xF) && "Bind value out of range!");
204 Info = (Info & 0x0F) | (X << 4);
206 void setType(unsigned X) {
207 assert(X == (X & 0xF) && "Type value out of range!");
208 Info = (Info & 0xF0) | X;
212 /// ELFRelocation - This class contains all the information necessary to
213 /// to generate any 32-bit or 64-bit ELF relocation entry.
214 class ELFRelocation {
215 uint64_t r_offset; // offset in the section of the object this applies to
216 uint32_t r_symidx; // symbol table index of the symbol to use
217 uint32_t r_type; // machine specific relocation type
218 int64_t r_add; // explicit relocation addend
219 bool r_rela; // if true then the addend is part of the entry
220 // otherwise the addend is at the location specified
223 uint64_t getInfo(bool is64Bit) const {
225 return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
227 return (r_symidx << 8) + (r_type & 0xFFL);
230 uint64_t getOffset() const { return r_offset; }
231 int64_t getAddend() const { return r_add; }
233 ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
234 bool rela = true, int64_t addend = 0) :
235 r_offset(off), r_symidx(sym), r_type(type),
236 r_add(addend), r_rela(rela) {}
239 } // end namespace llvm