1 # -*- coding: utf-8 -*-
3 # kate: space-indent on; indent-width 2; mixedindent off; indent-mode python;
5 # Copyright (C) 2009 Amand 'alrj' Tihon <amand.tihon@alrj.org>
7 # This file is part of bold, the Byte Optimized Linker.
8 # Heavily inspired by elf.h from the GNU C Library.
10 # You can redistribute this file and/or modify it under the terms of the
11 # GNU Lesser General Public License as published by the Free Software
12 # Foundation, version 2.1.
14 """This file defines standard ELF constants."""
16 class SymbolicConstant(long):
17 """Allows you to map a symbolic name with a given integer."""
20 def __new__(cls, value, symbolic=None):
22 cls._symbolics[value] = symbolic
23 return long.__new__(cls, value)
26 if long(self) in self._symbolics:
27 return self._symbolics[long(self)]
29 return self._default % long(self)
31 return str(long(self))
34 class ElfClass(SymbolicConstant):
36 ELFCLASSNONE = ElfClass(0, "Invalid ELF class")
37 ELFCLASS32 = ElfClass(1, "ELF32")
38 ELFCLASS64 = ElfClass(2, "ELF64")
41 class ElfData(SymbolicConstant):
43 ELFDATANONE = ElfData(0, "Invalid data encoding")
44 ELFDATA2LSB = ElfData(1, "Little endian")
45 ELFDATA2MSB = ElfData(2, "Big endian")
48 class ElfVersion(SymbolicConstant):
50 EV_NONE = ElfVersion(0, "Invalid ELF version")
51 EV_CURRENT = ElfVersion(1, "Current version (1)")
54 class ElfOsAbi(SymbolicConstant):
57 ELFOSABI_NONE = ElfOsAbi(0, "UNIX - System V")
58 ELFOSABI_SYSV = ElfOsAbi(0, "UNIX - System V")
61 class ElfType(SymbolicConstant):
63 ET_NONE = ElfType(0, "No file type")
64 ET_REL = ElfType(1, "Relocatable file")
65 ET_EXEC = ElfType(2, "Executable file")
66 ET_DYN = ElfType(3, "Shared object file")
67 ET_CORE = ElfType(4, "Core file")
70 class ElfMachine(SymbolicConstant):
73 EM_NONE = ElfMachine(0, "No machine")
74 EM_386 = ElfMachine(3, "Intel 80386")
75 EM_X86_64 = ElfMachine(62, "AMD x86-64 architecture")
77 class ElfSectionIndex(SymbolicConstant):
79 SHN_UNDEF = ElfSectionIndex(0, "UND")
80 SHN_ABS = ElfSectionIndex(0xfff1, "ABS")
81 SHN_COMMON = ElfSectionIndex(0xfff2, "COM")
83 class ElfShType(SymbolicConstant):
85 SHT_NULL = ElfShType(0, "NULL")
86 SHT_PROGBITS = ElfShType(1, "PROGBITS")
87 SHT_SYMTAB = ElfShType(2, "SYMTAB")
88 SHT_STRTAB = ElfShType(3, "STRTAB")
89 SHT_RELA = ElfShType(4, "RELA")
90 SHT_HASH = ElfShType(5, "HASH")
91 SHT_DYNAMIC = ElfShType(6, "DYNAMIC")
92 SHT_NOTE = ElfShType(7, "NOTE")
93 SHT_NOBITS = ElfShType(8, "NOBITS")
94 SHT_REL = ElfShType(9, "REL")
95 SHT_SHLIB = ElfShType(10, "SHLIB")
96 SHT_DYNSYM = ElfShType(11, "DYNSYM")
100 SHF_EXECINSTR = 1 << 2
103 SHF_INFO_LINK = 1 << 6
104 SHF_LINK_ORDER = 1 << 7
105 SHF_OS_NONCONFORMING = 1 << 8
108 SHF_MASKOS = 0x0f00000
109 SHF_MASKPROC = 0xf000000
114 class ElfSymbolBinding(SymbolicConstant):
116 STB_LOCAL = ElfSymbolBinding(0, "LOCAL")
117 STB_GLOBAL = ElfSymbolBinding(1, "GLOBAL")
118 STB_WEAK = ElfSymbolBinding(2, "WEAK")
121 class ElfSymbolType(SymbolicConstant):
123 STT_NOTYPE = ElfSymbolType(0, "NOTYPE")
124 STT_OBJECT = ElfSymbolType(1, "OBJECT")
125 STT_FUNC = ElfSymbolType(2, "FUNC")
126 STT_SECTION = ElfSymbolType(3, "SECTION")
127 STT_FILE = ElfSymbolType(4, "FILE")
128 STT_COMMON = ElfSymbolType(5, "COMMON")
129 STT_TLS = ElfSymbolType(6, "TLS")
132 class ElfSymbolVisibility(SymbolicConstant):
134 STV_DEFAULT = ElfSymbolVisibility(0, "DEFAULT")
135 STV_INTERNAL = ElfSymbolVisibility(1, "INTERN")
136 STV_HIDDEN = ElfSymbolVisibility(2, "HIDDEN")
137 STV_PROTECTED = ElfSymbolVisibility(3, "PROTECTED")
140 class ElfPhType(SymbolicConstant):
142 PT_NULL = ElfPhType(0, "NULL")
143 PT_LOAD = ElfPhType(1, "LOAD")
144 PT_DYNAMIC = ElfPhType(2, "DYNAMIC")
145 PT_INTERP = ElfPhType(3, "INTERP")
146 PT_NOTE = ElfPhType(4, "NOTE")
147 PT_SHLIB = ElfPhType(5, "SHLIB")
148 PT_PHDR = ElfPhType(6, "PHDR")
149 PT_TLS = ElfPhType(7, "TLS")
155 class ElfDynamicType(SymbolicConstant):
157 _default = "Unknown (0x%x)"
158 DT_NULL = ElfDynamicType(0, "NULL")
159 DT_NEEDED = ElfDynamicType(1, "NEEDED")
160 DT_PLTRELSZ = ElfDynamicType(2, "PLTRELSZ")
161 DT_PLTGOT = ElfDynamicType(3, "PLTGOT")
162 DT_HASH = ElfDynamicType(4, "HASH")
163 DT_STRTAB = ElfDynamicType(5, "STRTAB")
164 DT_SYMTAB = ElfDynamicType(6, "SYMTAB")
165 DT_RELA = ElfDynamicType(7, "RELA")
166 DT_RELASZ = ElfDynamicType(8, "RELASZ")
167 DT_RELAENT = ElfDynamicType(9, "RELAENT")
168 DT_STRSZ = ElfDynamicType(10, "STRSZ")
169 DT_SYMENT = ElfDynamicType(11, "SYMENT")
170 DT_INIT = ElfDynamicType(12, "INIT")
171 DT_FINI = ElfDynamicType(13, "FINI")
172 DT_SONAME = ElfDynamicType(14, "SONAME")
173 DT_RPATH = ElfDynamicType(15, "RPATH")
174 DT_SYMBOLIC = ElfDynamicType(16, "SYMBOLIC")
175 DT_REL = ElfDynamicType(17, "REL")
176 DT_RELSZ = ElfDynamicType(18, "RELSZ")
177 DT_RELENT = ElfDynamicType(19, "RELENT")
178 DT_PLTREL = ElfDynamicType(20, "PLTREL")
179 DT_DEBUG = ElfDynamicType(21, "DEBUG")
180 DT_TEXTREL = ElfDynamicType(22, "TEXTREL")
181 DT_JMPREL = ElfDynamicType(23, "JMPREL")
182 DT_BIND_NOW = ElfDynamicType(24, "BIND_NOW")
183 DT_INIT_ARRAY = ElfDynamicType(25, "INIT_ARRAY")
184 DT_FINI_ARRAY = ElfDynamicType(26, "FINI_ARRAY")
185 DT_INIT_ARRAYSZ = ElfDynamicType(27, "INIT_ARRAYSZ")
186 DT_FINI_ARRAYSZ = ElfDynamicType(28, "FINI_ARRAYSZ")
187 DT_RUNPATH = ElfDynamicType(29, "RUNPATH")
188 DT_FLAGS = ElfDynamicType(30, "FLAGS")
189 DT_ENCODING = ElfDynamicType(31, "ENCODING")
190 DT_PREINIT_ARRAY = ElfDynamicType(32, "PREINIT_ARRAY")
191 DT_PREINIT_ARRAYSZ = ElfDynamicType(33, "PREINIT_ARRAYSZ")
193 # AMD x86-64 relocations
194 class Amd64Relocation(SymbolicConstant):
197 R_X86_64_NONE = Amd64Relocation(0, "NONE")
198 R_X86_64_64 = Amd64Relocation(1, "64")
199 R_X86_64_PC32 = Amd64Relocation(2, "PC32")
200 R_X86_64_GOT32 = Amd64Relocation(3, "GOT32")
201 R_X86_64_PLT32 = Amd64Relocation(4, "PLT32")
202 R_X86_64_COPY = Amd64Relocation(5, "COPY")
203 R_X86_64_GLOB_DAT = Amd64Relocation(6, "GLOB_DAT")
204 R_X86_64_JUMP_SLOT = Amd64Relocation(7, "JUMP_SLOT")
205 R_X86_64_RELATIVE = Amd64Relocation(8, "RELATIVE")
206 R_X86_64_GOTPCREL = Amd64Relocation(9, "GOTPCREL")
207 R_X86_64_32 = Amd64Relocation(10, "32")
208 R_X86_64_32S = Amd64Relocation(11, "32S")
209 R_X86_64_16 = Amd64Relocation(12, "16")
210 R_X86_64_PC16 = Amd64Relocation(13, "PC16")
211 R_X86_64_8 = Amd64Relocation(14, "8")
212 R_X86_64_PC8 = Amd64Relocation(15, "PC8")
213 R_X86_64_DTPMOD64 = Amd64Relocation(16, "DTPMOD64")
214 R_X86_64_DTPOFF64 = Amd64Relocation(17, "DTPOFF64")
215 R_X86_64_TPOFF64 = Amd64Relocation(18, "TPOFF64")
216 R_X86_64_TLSGD = Amd64Relocation(19, "TLSGD")
217 R_X86_64_TLSLD = Amd64Relocation(20, "TLSLD")
218 R_X86_64_DTPOFF32 = Amd64Relocation(21, "DTPOFF32")
219 R_X86_64_GOTTPOFF = Amd64Relocation(22, "GOTTPOFF")
220 R_X86_64_TPOFF32 = Amd64Relocation(23, "TPOFF32")