blob: bf43622a6faab3ea6a2e53c30417961998c4435c [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//
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.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef CODEGEN_ELF_H
22#define CODEGEN_ELF_H
23
24#include "llvm/Support/DataTypes.h"
25#include <cstring>
26
27namespace llvm {
28 class GlobalVariable;
29
30 // Identification Indexes
31 enum {
32 EI_MAG0 = 0,
33 EI_MAG1 = 1,
34 EI_MAG2 = 2,
35 EI_MAG3 = 3
36 };
37
38 // File types
39 enum {
40 ET_NONE = 0, // No file type
41 ET_REL = 1, // Relocatable file
42 ET_EXEC = 2, // Executable file
43 ET_DYN = 3, // Shared object file
44 ET_CORE = 4, // Core file
45 ET_LOPROC = 0xff00, // Beginning of processor-specific codes
46 ET_HIPROC = 0xffff // Processor-specific
47 };
48
49 // Object file classes.
50 enum {
51 ELFCLASS32 = 1, // 32-bit object file
52 ELFCLASS64 = 2 // 64-bit object file
53 };
54
55 // Object file byte orderings.
56 enum {
57 ELFDATA2LSB = 1, // Little-endian object file
58 ELFDATA2MSB = 2 // Big-endian object file
59 };
60
61 // Versioning
62 enum {
63 EV_NONE = 0,
64 EV_CURRENT = 1
65 };
66
67 /// ELFSection - This struct contains information about each section that is
68 /// emitted to the file. This is eventually turned into the section header
69 /// table at the end of the file.
70 struct ELFSection {
71
72 // ELF specific fields
73 std::string Name; // Name of the section.
74 unsigned NameIdx; // Index in .shstrtab of name, once emitted.
75 unsigned Type;
76 unsigned Flags;
77 uint64_t Addr;
78 unsigned Offset;
79 unsigned Size;
80 unsigned Link;
81 unsigned Info;
82 unsigned Align;
83 unsigned EntSize;
84
85 // Section Header Flags
86 enum {
87 SHF_WRITE = 1 << 0, // Writable
88 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
89 SHF_EXECINSTR = 1 << 2, // Executable
90 SHF_MERGE = 1 << 4, // Might be merged if equal
91 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
92 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
93 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
94 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
95 SHF_GROUP = 1 << 9, // Section is a member of a group
96 SHF_TLS = 1 << 10 // Section holds thread-local data
97 };
98
99 // Section Types
100 enum {
101 SHT_NULL = 0, // No associated section (inactive entry).
102 SHT_PROGBITS = 1, // Program-defined contents.
103 SHT_SYMTAB = 2, // Symbol table.
104 SHT_STRTAB = 3, // String table.
105 SHT_RELA = 4, // Relocation entries; explicit addends.
106 SHT_HASH = 5, // Symbol hash table.
107 SHT_DYNAMIC = 6, // Information for dynamic linking.
108 SHT_NOTE = 7, // Information about the file.
109 SHT_NOBITS = 8, // Data occupies no space in the file.
110 SHT_REL = 9, // Relocation entries; no explicit addends.
111 SHT_SHLIB = 10, // Reserved.
112 SHT_DYNSYM = 11, // Symbol table.
113 SHT_LOPROC = 0x70000000, // Lowest processor architecture-specific type.
114 SHT_HIPROC = 0x7fffffff, // Highest processor architecture-specific type.
115 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
116 SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
117 };
118
119 // Special section indices.
120 enum {
121 SHN_UNDEF = 0, // Undefined, missing, irrelevant, or meaningless
122 SHN_LORESERVE = 0xff00, // Lowest reserved index
123 SHN_LOPROC = 0xff00, // Lowest processor-specific index
124 SHN_HIPROC = 0xff1f, // Highest processor-specific index
125 SHN_ABS = 0xfff1, // Symbol has absolute value; does not need relocation
126 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
127 SHN_HIRESERVE = 0xffff // Highest reserved index
128 };
129
130 /// SectionIdx - The number of the section in the Section Table.
131 unsigned short SectionIdx;
132
133 /// SectionData - The actual data for this section which we are building
134 /// up for emission to the file.
135 std::vector<unsigned char> SectionData;
136
137 ELFSection(const std::string &name)
138 : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
139 Link(0), Info(0), Align(0), EntSize(0) {}
140 };
141
142 /// ELFSym - This struct contains information about each symbol that is
143 /// added to logical symbol table for the module. This is eventually
144 /// turned into a real symbol table in the file.
145 struct ELFSym {
146 const GlobalValue *GV; // The global value this corresponds to.
147
148 // ELF specific fields
149 unsigned NameIdx; // Index in .strtab of name, once emitted.
150 uint64_t Value;
151 unsigned Size;
152 uint8_t Info;
153 uint8_t Other;
154 unsigned short SectionIdx;
155
156 enum {
157 STB_LOCAL = 0,
158 STB_GLOBAL = 1,
159 STB_WEAK = 2
160 };
161
162 enum {
163 STT_NOTYPE = 0,
164 STT_OBJECT = 1,
165 STT_FUNC = 2,
166 STT_SECTION = 3,
167 STT_FILE = 4
168 };
169
170 ELFSym(const GlobalValue *gv) : GV(gv), Value(0),
171 Size(0), Info(0), Other(0),
172 SectionIdx(ELFSection::SHN_UNDEF) {}
173
174 void SetBind(unsigned X) {
175 assert(X == (X & 0xF) && "Bind value out of range!");
176 Info = (Info & 0x0F) | (X << 4);
177 }
178 void SetType(unsigned X) {
179 assert(X == (X & 0xF) && "Type value out of range!");
180 Info = (Info & 0xF0) | X;
181 }
182 };
183
184} // end namespace llvm
185
186#endif