blob: cb5a8c0eae1d3a42c119fbc62afd28de64e78659 [file] [log] [blame]
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +00001//===-- 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 Lopesae9163f2009-06-14 07:53:21 +000013// 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 Lopesf5b0c5a2009-06-06 03:56:29 +000017//
18//===----------------------------------------------------------------------===//
19
20#ifndef CODEGEN_ELF_H
21#define CODEGEN_ELF_H
22
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000023#include "llvm/CodeGen/BinaryObject.h"
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000024#include "llvm/CodeGen/MachineRelocation.h"
Chandler Carruth8b67f772009-10-26 01:35:46 +000025#include "llvm/System/DataTypes.h"
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000026
27namespace llvm {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000028 class GlobalValue;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000029
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 Lopesf5b0c5a2009-06-06 03:56:29 +000049 // Versioning
50 enum {
51 EV_NONE = 0,
52 EV_CURRENT = 1
53 };
54
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000055 /// 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.
58 struct ELFSym {
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000059
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
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +000062 // assumed by default.
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000063 union {
64 const GlobalValue *GV; // If this is a pointer to a GV
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +000065 const char *Ext; // If this is a pointer to a named symbol
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000066 } Source;
67
68 // Describes from which source type this ELF symbol comes from,
69 // they can be GlobalValue, ExternalSymbol or neither.
70 enum {
71 isGV, // The Source.GV field is valid.
72 isExtSym, // The Source.ExtSym field is valid.
73 isOther // Not a GlobalValue or External Symbol
74 };
75 unsigned SourceType;
76
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +000077 bool isGlobalValue() const { return SourceType == isGV; }
78 bool isExternalSym() const { return SourceType == isExtSym; }
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000079
80 // getGlobalValue - If this is a global value which originated the
81 // elf symbol, return a reference to it.
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +000082 const GlobalValue *getGlobalValue() const {
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000083 assert(SourceType == isGV && "This is not a global value");
84 return Source.GV;
Douglas Gregorcabdd742009-12-19 07:05:23 +000085 }
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000086
87 // getExternalSym - If this is an external symbol which originated the
88 // elf symbol, return a reference to it.
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +000089 const char *getExternalSymbol() const {
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000090 assert(SourceType == isExtSym && "This is not an external symbol");
91 return Source.Ext;
Douglas Gregorcabdd742009-12-19 07:05:23 +000092 }
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000093
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();
98 Sym->Source.GV = GV;
99 Sym->setBind(Bind);
100 Sym->setType(Type);
101 Sym->setVisibility(Visibility);
102 Sym->SourceType = isGV;
103 return Sym;
104 }
105
106 // getExtSym - Create and return an elf symbol to represent an
107 // external symbol
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;
115 return Sym;
116 }
117
118 // getSectionSym - Returns a elf symbol to represent an elf section
119 static ELFSym *getSectionSym() {
120 ELFSym *Sym = new ELFSym();
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000121 Sym->setBind(STB_LOCAL);
122 Sym->setType(STT_SECTION);
123 Sym->setVisibility(STV_DEFAULT);
124 Sym->SourceType = isOther;
125 return Sym;
126 }
127
Bruno Cardoso Lopes42286562009-07-27 19:38:38 +0000128 // getFileSym - Returns a elf symbol to represent the module identifier
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000129 static ELFSym *getFileSym() {
130 ELFSym *Sym = new ELFSym();
131 Sym->setBind(STB_LOCAL);
132 Sym->setType(STT_FILE);
133 Sym->setVisibility(STV_DEFAULT);
134 Sym->SectionIdx = 0xfff1; // ELFSection::SHN_ABS;
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000135 Sym->SourceType = isOther;
136 return Sym;
137 }
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000138
Bruno Cardoso Lopes3e0094d2009-08-08 17:29:04 +0000139 // getUndefGV - Returns a STT_NOTYPE symbol
140 static ELFSym *getUndefGV(const GlobalValue *GV, unsigned Bind) {
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000141 ELFSym *Sym = new ELFSym();
142 Sym->Source.GV = GV;
Bruno Cardoso Lopes3e0094d2009-08-08 17:29:04 +0000143 Sym->setBind(Bind);
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000144 Sym->setType(STT_NOTYPE);
145 Sym->setVisibility(STV_DEFAULT);
146 Sym->SectionIdx = 0; //ELFSection::SHN_UNDEF;
147 Sym->SourceType = isGV;
148 return Sym;
149 }
150
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000151 // ELF specific fields
152 unsigned NameIdx; // Index in .strtab of name, once emitted.
153 uint64_t Value;
154 unsigned Size;
155 uint8_t Info;
156 uint8_t Other;
157 unsigned short SectionIdx;
158
159 // Symbol index into the Symbol table
160 unsigned SymTabIdx;
161
162 enum {
Bruno Cardoso Lopes115934e2009-07-16 06:26:41 +0000163 STB_LOCAL = 0, // Local sym, not visible outside obj file containing def
164 STB_GLOBAL = 1, // Global sym, visible to all object files being combined
165 STB_WEAK = 2 // Weak symbol, like global but lower-precedence
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000166 };
167
168 enum {
Bruno Cardoso Lopes115934e2009-07-16 06:26:41 +0000169 STT_NOTYPE = 0, // Symbol's type is not specified
170 STT_OBJECT = 1, // Symbol is a data object (variable, array, etc.)
171 STT_FUNC = 2, // Symbol is executable code (function, etc.)
172 STT_SECTION = 3, // Symbol refers to a section
173 STT_FILE = 4 // Local, absolute symbol that refers to a file
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000174 };
175
176 enum {
177 STV_DEFAULT = 0, // Visibility is specified by binding type
178 STV_INTERNAL = 1, // Defined by processor supplements
179 STV_HIDDEN = 2, // Not visible to other components
180 STV_PROTECTED = 3 // Visible in other components but not preemptable
181 };
182
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000183 ELFSym() : SourceType(isOther), NameIdx(0), Value(0),
184 Size(0), Info(0), Other(STV_DEFAULT), SectionIdx(0),
185 SymTabIdx(0) {}
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000186
187 unsigned getBind() const { return (Info >> 4) & 0xf; }
188 unsigned getType() const { return Info & 0xf; }
189 bool isLocalBind() const { return getBind() == STB_LOCAL; }
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000190 bool isFileType() const { return getType() == STT_FILE; }
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000191
192 void setBind(unsigned X) {
193 assert(X == (X & 0xF) && "Bind value out of range!");
194 Info = (Info & 0x0F) | (X << 4);
195 }
196
197 void setType(unsigned X) {
198 assert(X == (X & 0xF) && "Type value out of range!");
199 Info = (Info & 0xF0) | X;
200 }
201
202 void setVisibility(unsigned V) {
203 assert(V == (V & 0x3) && "Visibility value out of range!");
204 Other = V;
205 }
206 };
207
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000208 /// ELFSection - This struct contains information about each section that is
209 /// emitted to the file. This is eventually turned into the section header
210 /// table at the end of the file.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000211 class ELFSection : public BinaryObject {
212 public:
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000213 // ELF specific fields
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000214 unsigned NameIdx; // sh_name - .shstrtab idx of name, once emitted.
215 unsigned Type; // sh_type - Section contents & semantics
216 unsigned Flags; // sh_flags - Section flags.
217 uint64_t Addr; // sh_addr - The mem addr this section is in.
218 unsigned Offset; // sh_offset - Offset from the file start
219 unsigned Size; // sh_size - The section size.
220 unsigned Link; // sh_link - Section header table index link.
221 unsigned Info; // sh_info - Auxillary information.
222 unsigned Align; // sh_addralign - Alignment of section.
223 unsigned EntSize; // sh_entsize - Size of entries in the section e
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000224
225 // Section Header Flags
226 enum {
227 SHF_WRITE = 1 << 0, // Writable
228 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
229 SHF_EXECINSTR = 1 << 2, // Executable
230 SHF_MERGE = 1 << 4, // Might be merged if equal
231 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
232 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
233 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
234 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
235 SHF_GROUP = 1 << 9, // Section is a member of a group
236 SHF_TLS = 1 << 10 // Section holds thread-local data
237 };
238
239 // Section Types
240 enum {
241 SHT_NULL = 0, // No associated section (inactive entry).
242 SHT_PROGBITS = 1, // Program-defined contents.
243 SHT_SYMTAB = 2, // Symbol table.
244 SHT_STRTAB = 3, // String table.
245 SHT_RELA = 4, // Relocation entries; explicit addends.
246 SHT_HASH = 5, // Symbol hash table.
247 SHT_DYNAMIC = 6, // Information for dynamic linking.
248 SHT_NOTE = 7, // Information about the file.
249 SHT_NOBITS = 8, // Data occupies no space in the file.
250 SHT_REL = 9, // Relocation entries; no explicit addends.
251 SHT_SHLIB = 10, // Reserved.
252 SHT_DYNSYM = 11, // Symbol table.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000253 SHT_LOPROC = 0x70000000, // Lowest processor arch-specific type.
254 SHT_HIPROC = 0x7fffffff, // Highest processor arch-specific type.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000255 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
256 SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
257 };
258
259 // Special section indices.
260 enum {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000261 SHN_UNDEF = 0, // Undefined, missing, irrelevant
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000262 SHN_LORESERVE = 0xff00, // Lowest reserved index
263 SHN_LOPROC = 0xff00, // Lowest processor-specific index
264 SHN_HIPROC = 0xff1f, // Highest processor-specific index
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000265 SHN_ABS = 0xfff1, // Symbol has absolute value; no relocation
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000266 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
267 SHN_HIRESERVE = 0xffff // Highest reserved index
268 };
269
270 /// SectionIdx - The number of the section in the Section Table.
271 unsigned short SectionIdx;
272
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000273 /// Sym - The symbol to represent this section if it has one.
274 ELFSym *Sym;
275
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000276 /// getSymIndex - Returns the symbol table index of the symbol
277 /// representing this section.
278 unsigned getSymbolTableIndex() const {
279 assert(Sym && "section not present in the symbol table");
280 return Sym->SymTabIdx;
281 }
282
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000283 ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
284 : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000285 Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000286 };
287
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000288 /// ELFRelocation - This class contains all the information necessary to
289 /// to generate any 32-bit or 64-bit ELF relocation entry.
290 class ELFRelocation {
291 uint64_t r_offset; // offset in the section of the object this applies to
292 uint32_t r_symidx; // symbol table index of the symbol to use
293 uint32_t r_type; // machine specific relocation type
294 int64_t r_add; // explicit relocation addend
295 bool r_rela; // if true then the addend is part of the entry
296 // otherwise the addend is at the location specified
297 // by r_offset
298 public:
299 uint64_t getInfo(bool is64Bit) const {
300 if (is64Bit)
301 return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
302 else
303 return (r_symidx << 8) + (r_type & 0xFFL);
304 }
305
306 uint64_t getOffset() const { return r_offset; }
307 int64_t getAddend() const { return r_add; }
308
309 ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
310 bool rela = true, int64_t addend = 0) :
311 r_offset(off), r_symidx(sym), r_type(type),
312 r_add(addend), r_rela(rela) {}
313 };
314
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000315} // end namespace llvm
316
317#endif