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