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
14 // the Tool Interface Standard (TIS) Executable and Linking Format
15 // (ELF) Specification Version 1.2, May 1995. The ELF64 stuff is not
16 // standardized, as far as I can tell. It was largely based on information
17 // I found in OpenBSD header files.
19 //===----------------------------------------------------------------------===//
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/CodeGen/MachineRelocation.h"
26 #include "llvm/Support/DataTypes.h"
31 // Identification Indexes
41 ET_NONE = 0, // No file type
42 ET_REL = 1, // Relocatable file
43 ET_EXEC = 2, // Executable file
44 ET_DYN = 3, // Shared object file
45 ET_CORE = 4, // Core file
46 ET_LOPROC = 0xff00, // Beginning of processor-specific codes
47 ET_HIPROC = 0xffff // Processor-specific
50 // Object file classes.
52 ELFCLASS32 = 1, // 32-bit object file
53 ELFCLASS64 = 2 // 64-bit object file
56 // Object file byte orderings.
58 ELFDATA2LSB = 1, // Little-endian object file
59 ELFDATA2MSB = 2 // Big-endian object file
69 // e_machine - This field is the target specific value to emit as the
70 // e_machine member of the ELF header.
71 unsigned short e_machine;
73 // e_flags - The machine flags for the target. This defaults to zero.
76 // e_size - Holds the ELF header's size in bytes
79 // Endianess and ELF Class (64 or 32 bits)
83 unsigned getByteOrder() const { return ByteOrder; }
84 unsigned getElfClass() const { return ElfClass; }
85 unsigned getSize() const { return e_ehsize; }
86 unsigned getMachine() const { return e_machine; }
87 unsigned getFlags() const { return e_flags; }
89 ELFHeader(unsigned short machine, unsigned flags,
90 bool is64Bit, bool isLittleEndian)
91 : e_machine(machine), e_flags(flags) {
92 ElfClass = is64Bit ? ELFCLASS64 : ELFCLASS32;
93 ByteOrder = isLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
94 e_ehsize = is64Bit ? 64 : 52;
98 /// ELFSection - This struct contains information about each section that is
99 /// emitted to the file. This is eventually turned into the section header
100 /// table at the end of the file.
103 // Name of the section
106 // ELF specific fields
107 unsigned NameIdx; // sh_name - .shstrtab idx of name, once emitted.
108 unsigned Type; // sh_type - Section contents & semantics
109 unsigned Flags; // sh_flags - Section flags.
110 uint64_t Addr; // sh_addr - The mem addr this section is in.
111 unsigned Offset; // sh_offset - Offset from the file start
112 unsigned Size; // sh_size - The section size.
113 unsigned Link; // sh_link - Section header table index link.
114 unsigned Info; // sh_info - Auxillary information.
115 unsigned Align; // sh_addralign - Alignment of section.
116 unsigned EntSize; // sh_entsize - Size of entries in the section e
118 // Section Header Flags
120 SHF_WRITE = 1 << 0, // Writable
121 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
122 SHF_EXECINSTR = 1 << 2, // Executable
123 SHF_MERGE = 1 << 4, // Might be merged if equal
124 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
125 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
126 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
127 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
128 SHF_GROUP = 1 << 9, // Section is a member of a group
129 SHF_TLS = 1 << 10 // Section holds thread-local data
134 SHT_NULL = 0, // No associated section (inactive entry).
135 SHT_PROGBITS = 1, // Program-defined contents.
136 SHT_SYMTAB = 2, // Symbol table.
137 SHT_STRTAB = 3, // String table.
138 SHT_RELA = 4, // Relocation entries; explicit addends.
139 SHT_HASH = 5, // Symbol hash table.
140 SHT_DYNAMIC = 6, // Information for dynamic linking.
141 SHT_NOTE = 7, // Information about the file.
142 SHT_NOBITS = 8, // Data occupies no space in the file.
143 SHT_REL = 9, // Relocation entries; no explicit addends.
144 SHT_SHLIB = 10, // Reserved.
145 SHT_DYNSYM = 11, // Symbol table.
146 SHT_LOPROC = 0x70000000, // Lowest processor architecture-specific type.
147 SHT_HIPROC = 0x7fffffff, // Highest processor architecture-specific type.
148 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
149 SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
152 // Special section indices.
154 SHN_UNDEF = 0, // Undefined, missing, irrelevant
155 SHN_LORESERVE = 0xff00, // Lowest reserved index
156 SHN_LOPROC = 0xff00, // Lowest processor-specific index
157 SHN_HIPROC = 0xff1f, // Highest processor-specific index
158 SHN_ABS = 0xfff1, // Symbol has absolute value; no relocation
159 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
160 SHN_HIRESERVE = 0xffff // Highest reserved index
163 /// SectionIdx - The number of the section in the Section Table.
164 unsigned short SectionIdx;
166 /// SectionData - The actual data for this section which we are building
167 /// up for emission to the file.
168 std::vector<unsigned char> SectionData;
170 /// Relocations - The relocations that we have encountered so far in this
171 /// section that we will need to convert to Elf relocation entries when
172 /// the file is written.
173 std::vector<MachineRelocation> Relocations;
175 /// Section Header Size
176 static unsigned getSectionHdrSize(bool is64Bit)
177 { return is64Bit ? 64 : 40; }
179 ELFSection(const std::string &name)
180 : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
181 Link(0), Info(0), Align(0), EntSize(0) {}
184 /// ELFSym - This struct contains information about each symbol that is
185 /// added to logical symbol table for the module. This is eventually
186 /// turned into a real symbol table in the file.
188 const GlobalValue *GV; // The global value this corresponds to.
190 // ELF specific fields
191 unsigned NameIdx; // Index in .strtab of name, once emitted.
196 unsigned short SectionIdx;
213 STV_DEFAULT = 0, // Visibility is specified by binding type
214 STV_INTERNAL = 1, // Defined by processor supplements
215 STV_HIDDEN = 2, // Not visible to other components
216 STV_PROTECTED = 3 // Visible in other components but not preemptable
219 ELFSym(const GlobalValue *gv) : GV(gv), NameIdx(0), Value(0),
220 Size(0), Info(0), Other(0),
221 SectionIdx(ELFSection::SHN_UNDEF) {
225 switch (GV->getVisibility()) {
227 assert(0 && "unknown visibility type");
228 case GlobalValue::DefaultVisibility:
231 case GlobalValue::HiddenVisibility:
234 case GlobalValue::ProtectedVisibility:
235 Other = STV_PROTECTED;
240 void SetBind(unsigned X) {
241 assert(X == (X & 0xF) && "Bind value out of range!");
242 Info = (Info & 0x0F) | (X << 4);
244 void SetType(unsigned X) {
245 assert(X == (X & 0xF) && "Type value out of range!");
246 Info = (Info & 0xF0) | X;
249 static unsigned getEntrySize(bool is64Bit)
250 { return is64Bit ? 24 : 16; }
253 } // end namespace llvm