blob: fb884c9e8b712391e30947c6eec633142c5a9ad3 [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"
Eli Friedman10bb4212010-07-16 07:53:29 +000025#include "llvm/Support/ELF.h"
Chandler Carruth8b67f772009-10-26 01:35:46 +000026#include "llvm/System/DataTypes.h"
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000027
28namespace llvm {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000029 class GlobalValue;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000030
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000031 /// 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 Lopes746e3bb2009-07-27 18:54:47 +000035
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 Lopesdf0b6502009-07-27 19:32:57 +000038 // assumed by default.
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000039 union {
40 const GlobalValue *GV; // If this is a pointer to a GV
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +000041 const char *Ext; // If this is a pointer to a named symbol
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000042 } 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 Lopesd163e8b2009-08-13 21:25:27 +000053 bool isGlobalValue() const { return SourceType == isGV; }
54 bool isExternalSym() const { return SourceType == isExtSym; }
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000055
56 // getGlobalValue - If this is a global value which originated the
57 // elf symbol, return a reference to it.
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +000058 const GlobalValue *getGlobalValue() const {
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000059 assert(SourceType == isGV && "This is not a global value");
60 return Source.GV;
Douglas Gregorcabdd742009-12-19 07:05:23 +000061 }
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000062
63 // getExternalSym - If this is an external symbol which originated the
64 // elf symbol, return a reference to it.
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +000065 const char *getExternalSymbol() const {
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000066 assert(SourceType == isExtSym && "This is not an external symbol");
67 return Source.Ext;
Douglas Gregorcabdd742009-12-19 07:05:23 +000068 }
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000069
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 Friedman10bb4212010-07-16 07:53:29 +000087 Sym->setBind(ELF::STB_GLOBAL);
88 Sym->setType(ELF::STT_NOTYPE);
89 Sym->setVisibility(ELF::STV_DEFAULT);
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000090 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 Friedman10bb4212010-07-16 07:53:29 +000097 Sym->setBind(ELF::STB_LOCAL);
98 Sym->setType(ELF::STT_SECTION);
99 Sym->setVisibility(ELF::STV_DEFAULT);
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000100 Sym->SourceType = isOther;
101 return Sym;
102 }
103
Bruno Cardoso Lopes42286562009-07-27 19:38:38 +0000104 // getFileSym - Returns a elf symbol to represent the module identifier
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000105 static ELFSym *getFileSym() {
106 ELFSym *Sym = new ELFSym();
Eli Friedman10bb4212010-07-16 07:53:29 +0000107 Sym->setBind(ELF::STB_LOCAL);
108 Sym->setType(ELF::STT_FILE);
109 Sym->setVisibility(ELF::STV_DEFAULT);
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000110 Sym->SectionIdx = 0xfff1; // ELFSection::SHN_ABS;
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000111 Sym->SourceType = isOther;
112 return Sym;
113 }
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000114
Bruno Cardoso Lopes3e0094d2009-08-08 17:29:04 +0000115 // getUndefGV - Returns a STT_NOTYPE symbol
116 static ELFSym *getUndefGV(const GlobalValue *GV, unsigned Bind) {
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000117 ELFSym *Sym = new ELFSym();
118 Sym->Source.GV = GV;
Bruno Cardoso Lopes3e0094d2009-08-08 17:29:04 +0000119 Sym->setBind(Bind);
Eli Friedman10bb4212010-07-16 07:53:29 +0000120 Sym->setType(ELF::STT_NOTYPE);
121 Sym->setVisibility(ELF::STV_DEFAULT);
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000122 Sym->SectionIdx = 0; //ELFSection::SHN_UNDEF;
123 Sym->SourceType = isGV;
124 return Sym;
125 }
126
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000127 // 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 Lopes746e3bb2009-07-27 18:54:47 +0000138 ELFSym() : SourceType(isOther), NameIdx(0), Value(0),
Eli Friedman10bb4212010-07-16 07:53:29 +0000139 Size(0), Info(0), Other(ELF::STV_DEFAULT), SectionIdx(0),
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000140 SymTabIdx(0) {}
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000141
142 unsigned getBind() const { return (Info >> 4) & 0xf; }
143 unsigned getType() const { return Info & 0xf; }
Eli Friedman10bb4212010-07-16 07:53:29 +0000144 bool isLocalBind() const { return getBind() == ELF::STB_LOCAL; }
145 bool isFileType() const { return getType() == ELF::STT_FILE; }
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000146
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 Lopesf5b0c5a2009-06-06 03:56:29 +0000163 /// 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 Lopesae9163f2009-06-14 07:53:21 +0000166 class ELFSection : public BinaryObject {
167 public:
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000168 // ELF specific fields
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000169 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 Lopesf5b0c5a2009-06-06 03:56:29 +0000179
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000180 /// SectionIdx - The number of the section in the Section Table.
181 unsigned short SectionIdx;
182
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000183 /// Sym - The symbol to represent this section if it has one.
184 ELFSym *Sym;
185
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000186 /// 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 Lopesae9163f2009-06-14 07:53:21 +0000193 ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
194 : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000195 Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000196 };
197
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000198 /// 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 Lopesf5b0c5a2009-06-06 03:56:29 +0000225} // end namespace llvm
226
227#endif