blob: 8d92373b713d38c3a8b631618108c47790b87dde [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 Lopesc997d452009-06-11 19:16:03 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000024#include "llvm/CodeGen/BinaryObject.h"
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000025#include "llvm/CodeGen/MachineRelocation.h"
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000026#include "llvm/Support/DataTypes.h"
27#include <cstring>
28
29namespace llvm {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000030 class BinaryObject;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000031
32 // Identification Indexes
33 enum {
34 EI_MAG0 = 0,
35 EI_MAG1 = 1,
36 EI_MAG2 = 2,
37 EI_MAG3 = 3
38 };
39
40 // File types
41 enum {
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
49 };
50
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000051 // Versioning
52 enum {
53 EV_NONE = 0,
54 EV_CURRENT = 1
55 };
56
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.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000060 class ELFSection : public BinaryObject {
61 public:
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000062 // ELF specific fields
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000063 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
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000073
74 // Section Header Flags
75 enum {
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
86 };
87
88 // Section Types
89 enum {
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.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000102 SHT_LOPROC = 0x70000000, // Lowest processor arch-specific type.
103 SHT_HIPROC = 0x7fffffff, // Highest processor arch-specific type.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000104 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
105 SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
106 };
107
108 // Special section indices.
109 enum {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000110 SHN_UNDEF = 0, // Undefined, missing, irrelevant
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000111 SHN_LORESERVE = 0xff00, // Lowest reserved index
112 SHN_LOPROC = 0xff00, // Lowest processor-specific index
113 SHN_HIPROC = 0xff1f, // Highest processor-specific index
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000114 SHN_ABS = 0xfff1, // Symbol has absolute value; no relocation
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000115 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
116 SHN_HIRESERVE = 0xffff // Highest reserved index
117 };
118
119 /// SectionIdx - The number of the section in the Section Table.
120 unsigned short SectionIdx;
121
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000122 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) {}
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000125 };
126
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.
130 struct ELFSym {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000131 // 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;
135 bool IsCommon;
136 bool IsBss;
137 bool IsConstant;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000138
139 // ELF specific fields
140 unsigned NameIdx; // Index in .strtab of name, once emitted.
141 uint64_t Value;
142 unsigned Size;
143 uint8_t Info;
144 uint8_t Other;
145 unsigned short SectionIdx;
146
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000147 // Symbol index into the Symbol table
148 unsigned SymTabIdx;
149
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000150 enum {
151 STB_LOCAL = 0,
152 STB_GLOBAL = 1,
153 STB_WEAK = 2
154 };
155
156 enum {
157 STT_NOTYPE = 0,
158 STT_OBJECT = 1,
159 STT_FUNC = 2,
160 STT_SECTION = 3,
161 STT_FILE = 4
162 };
163
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000164 enum {
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
169 };
170
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000171 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),
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000174 SectionIdx(ELFSection::SHN_UNDEF),
175 SymTabIdx(0) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000176 if (!GV)
177 return;
178
179 switch (GV->getVisibility()) {
180 default:
181 assert(0 && "unknown visibility type");
182 case GlobalValue::DefaultVisibility:
183 Other = STV_DEFAULT;
184 break;
185 case GlobalValue::HiddenVisibility:
186 Other = STV_HIDDEN;
187 break;
188 case GlobalValue::ProtectedVisibility:
189 Other = STV_PROTECTED;
190 break;
191 }
192 }
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000193
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000194 unsigned getBind() {
195 return (Info >> 4) & 0xf;
196 }
197
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000198 unsigned getType() {
199 return Info & 0xf;
200 }
201
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000202 void setBind(unsigned X) {
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000203 assert(X == (X & 0xF) && "Bind value out of range!");
204 Info = (Info & 0x0F) | (X << 4);
205 }
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000206 void setType(unsigned X) {
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000207 assert(X == (X & 0xF) && "Type value out of range!");
208 Info = (Info & 0xF0) | X;
209 }
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000210 };
211
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000212 /// 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
221 // by r_offset
222 public:
223 uint64_t getInfo(bool is64Bit) const {
224 if (is64Bit)
225 return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
226 else
227 return (r_symidx << 8) + (r_type & 0xFFL);
228 }
229
230 uint64_t getOffset() const { return r_offset; }
231 int64_t getAddend() const { return r_add; }
232
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) {}
237 };
238
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000239} // end namespace llvm
240
241#endif