Fix think-o: emit all 8 bytes of the EOF marker. Also reflow a line in a
[llvm/stm8.git] / lib / CodeGen / ELF.h
blob5b634682cc879b95a9683703dd61add2045f9dac
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.
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 //===----------------------------------------------------------------------===//
20 #ifndef CODEGEN_ELF_H
21 #define CODEGEN_ELF_H
23 #include "llvm/CodeGen/BinaryObject.h"
24 #include "llvm/CodeGen/MachineRelocation.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/DataTypes.h"
28 namespace llvm {
29 class GlobalValue;
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 {
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
38 // assumed by default.
39 union {
40 const GlobalValue *GV; // If this is a pointer to a GV
41 const char *Ext; // If this is a pointer to a named symbol
42 } Source;
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
51 unsigned SourceType;
53 bool isGlobalValue() const { return SourceType == isGV; }
54 bool isExternalSym() const { return SourceType == isExtSym; }
56 // getGlobalValue - If this is a global value which originated the
57 // elf symbol, return a reference to it.
58 const GlobalValue *getGlobalValue() const {
59 assert(SourceType == isGV && "This is not a global value");
60 return Source.GV;
63 // getExternalSym - If this is an external symbol which originated the
64 // elf symbol, return a reference to it.
65 const char *getExternalSymbol() const {
66 assert(SourceType == isExtSym && "This is not an external symbol");
67 return Source.Ext;
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;
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;
87 Sym->setBind(ELF::STB_GLOBAL);
88 Sym->setType(ELF::STT_NOTYPE);
89 Sym->setVisibility(ELF::STV_DEFAULT);
90 Sym->SourceType = isExtSym;
91 return Sym;
94 // getSectionSym - Returns a elf symbol to represent an elf section
95 static ELFSym *getSectionSym() {
96 ELFSym *Sym = new ELFSym();
97 Sym->setBind(ELF::STB_LOCAL);
98 Sym->setType(ELF::STT_SECTION);
99 Sym->setVisibility(ELF::STV_DEFAULT);
100 Sym->SourceType = isOther;
101 return Sym;
104 // getFileSym - Returns a elf symbol to represent the module identifier
105 static ELFSym *getFileSym() {
106 ELFSym *Sym = new ELFSym();
107 Sym->setBind(ELF::STB_LOCAL);
108 Sym->setType(ELF::STT_FILE);
109 Sym->setVisibility(ELF::STV_DEFAULT);
110 Sym->SectionIdx = 0xfff1; // ELFSection::SHN_ABS;
111 Sym->SourceType = isOther;
112 return Sym;
115 // getUndefGV - Returns a STT_NOTYPE symbol
116 static ELFSym *getUndefGV(const GlobalValue *GV, unsigned Bind) {
117 ELFSym *Sym = new ELFSym();
118 Sym->Source.GV = GV;
119 Sym->setBind(Bind);
120 Sym->setType(ELF::STT_NOTYPE);
121 Sym->setVisibility(ELF::STV_DEFAULT);
122 Sym->SectionIdx = 0; //ELFSection::SHN_UNDEF;
123 Sym->SourceType = isGV;
124 return Sym;
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;
135 // Symbol index into the Symbol table
136 unsigned SymTabIdx;
138 ELFSym() : SourceType(isOther), NameIdx(0), Value(0),
139 Size(0), Info(0), Other(ELF::STV_DEFAULT), SectionIdx(0),
140 SymTabIdx(0) {}
142 unsigned getBind() const { return (Info >> 4) & 0xf; }
143 unsigned getType() const { return Info & 0xf; }
144 bool isLocalBind() const { return getBind() == ELF::STB_LOCAL; }
145 bool isFileType() const { return getType() == ELF::STT_FILE; }
147 void setBind(unsigned X) {
148 assert(X == (X & 0xF) && "Bind value out of range!");
149 Info = (Info & 0x0F) | (X << 4);
152 void setType(unsigned X) {
153 assert(X == (X & 0xF) && "Type value out of range!");
154 Info = (Info & 0xF0) | X;
157 void setVisibility(unsigned V) {
158 assert(V == (V & 0x3) && "Visibility value out of range!");
159 Other = V;
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.
166 class ELFSection : public BinaryObject {
167 public:
168 // ELF specific fields
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 - Auxiliary information.
177 unsigned Align; // sh_addralign - Alignment of section.
178 unsigned EntSize; // sh_entsize - Size of entries in the section e
180 /// SectionIdx - The number of the section in the Section Table.
181 unsigned short SectionIdx;
183 /// Sym - The symbol to represent this section if it has one.
184 ELFSym *Sym;
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;
193 ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
194 : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
195 Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
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);
216 uint64_t getOffset() const { return r_offset; }
217 int64_t getAddend() const { return r_add; }
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) {}
225 } // end namespace llvm
227 #endif