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/ELF.h"
26 #include "llvm/Support/DataTypes.h"
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.
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.
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
44 // Describes from which source type this ELF symbol comes from,
45 // they can be GlobalValue, ExternalSymbol or neither.
47 isGV
, // The Source.GV field is valid.
48 isExtSym
, // The Source.ExtSym field is valid.
49 isOther
// Not a GlobalValue or External Symbol
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");
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");
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();
77 Sym
->setVisibility(Visibility
);
78 Sym
->SourceType
= isGV
;
82 // getExtSym - Create and return an elf symbol to represent an
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
;
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
;
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
;
115 // getUndefGV - Returns a STT_NOTYPE symbol
116 static ELFSym
*getUndefGV(const GlobalValue
*GV
, unsigned Bind
) {
117 ELFSym
*Sym
= new ELFSym();
120 Sym
->setType(ELF::STT_NOTYPE
);
121 Sym
->setVisibility(ELF::STV_DEFAULT
);
122 Sym
->SectionIdx
= 0; //ELFSection::SHN_UNDEF;
123 Sym
->SourceType
= isGV
;
127 // ELF specific fields
128 unsigned NameIdx
; // Index in .strtab of name, once emitted.
133 unsigned short SectionIdx
;
135 // Symbol index into the Symbol table
138 ELFSym() : SourceType(isOther
), NameIdx(0), Value(0),
139 Size(0), Info(0), Other(ELF::STV_DEFAULT
), SectionIdx(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!");
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
{
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.
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
209 uint64_t getInfo(bool is64Bit
) const {
211 return ((uint64_t)r_symidx
<< 32) + ((uint64_t)r_type
& 0xFFFFFFFFL
);
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