1#
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16"""This file contains ELF constants."""
17
18# e_ident[] indices
19EI_MAG0 = 0         # File identification
20EI_MAG1 = 1         # File identification
21EI_MAG2 = 2         # File identification
22EI_MAG3 = 3         # File identification
23EI_CLASS = 4        # File class
24EI_DATA = 5         # Data encoding
25EI_VERSION = 6      # File version
26EI_OSABI = 7        # Operating system/ABI identification
27EI_ABIVERSION = 8   # ABI version
28EI_PAD = 9          # Start of padding bytes
29EI_NIDENT = 16      # Size of e_ident[]
30
31# EI_MAG0 to EI_MAG3
32ELF_MAGIC_NUMBER = b'\x7fELF'
33
34# EI_CLASS
35ELFCLASSNONE = 0    # Invalid class
36ELFCLASS32 = 1      # 32-bit objects
37ELFCLASS64 = 2      # 64-bit objects
38
39# EI_DATA
40ELFDATANONE = 0     # Invalid data encoding
41ELFDATA2LSB = 1     # Little endian
42ELFDATA2MSB = 2     # Big endian
43
44# e_type
45ET_NONE = 0         # No file type
46ET_REL = 1          # Relocatable file (object file)
47ET_EXEC = 2         # Executable file
48ET_DYN = 3          # Shared object file
49ET_CORE = 4         # Core file
50
51# e_machine
52EM_386 = 3
53EM_X86_64 = 62
54EM_MIPS = 8
55EM_ARM = 40
56EM_AARCH64 = 183
57
58# Relocation types
59R_ARM_ABS32 = 0x02
60R_ARM_RELATIVE = 0x17
61R_AARCH64_ABS64 = 0x101
62R_AARCH64_RELATIVE = 0x403
63R_386_32 = 1
64R_386_RELATIVE = 8
65R_X86_64_64 = 1
66R_X86_64_RELATIVE = 8
67
68# Section types
69SHT_NULL = 0
70SHT_PROGBITS = 1
71SHT_SYMTAB = 2
72SHT_STRTAB = 3
73SHT_RELA = 4
74SHT_HASH = 5
75SHT_DYNAMIC = 6
76SHT_NOTE = 7
77SHT_NOBITS = 8
78SHT_REL = 9
79SHT_SHLIB = 10
80SHT_DYNSYM = 11
81SHT_INIT_ARRAY = 14
82SHT_FINI_ARRAY = 15
83SHT_PREINIT_ARRAY = 16
84SHT_GROUP = 17
85SHT_SYMTAB_SHNDX = 18
86SHT_RELR = 19
87SHT_LOOS = 0x60000000
88SHT_ANDROID_REL = SHT_LOOS + 1
89SHT_ANDROID_RELA = SHT_LOOS + 2
90SHT_ANDROID_RELR = SHT_LOOS + 0xfffff00
91
92# Android packed relocation flags
93RELOCATION_GROUPED_BY_INFO_FLAG = 1
94RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG = 2
95RELOCATION_GROUPED_BY_ADDEND_FLAG = 4
96RELOCATION_GROUP_HAS_ADDEND_FLAG = 8
97
98# Section names
99SYMTAB = '.symtab'
100STRTAB = '.strtab'
101DYNSYM = '.dynsym'
102DYNSTR = '.dynstr'
103
104# Special section indices
105SHN_UNDEF = 0
106SHN_LORESERVE = 0xff00
107SHN_LOPROC = 0xff00
108SHN_HIPROC = 0xff1f
109SHN_LOOS = 0xff20
110SHN_HIOS = 0xff3f
111SHN_ABS = 0xfff1
112SHN_COMMON = 0xfff2
113SHN_XINDEX = 0xffff
114SHN_HIRESERVE = 0xffff
115
116# Symbol bindings
117STB_LOCAL = 0
118STB_GLOBAL = 1
119STB_WEAK = 2
120STB_LOOS = 10
121STB_HIOS = 12
122STB_LOPROC = 13
123STB_HIPROC = 15
124
125# Symbol types
126STT_NOTYPE = 0
127STT_OBJECT = 1
128STT_FUNC = 2
129STT_SECTION = 3
130STT_FILE = 4
131STT_COMMON = 5
132STT_TLS = 6
133STT_LOOS = 10
134STT_HIOS = 12
135STT_LOPROC = 13
136STT_HIPROC = 15
137
138# Segment types
139PT_NULL = 0
140PT_LOAD = 1
141PT_DYNAMIC = 2
142PT_INTERP = 3
143PT_NOTE = 4
144PT_SHLIB = 5
145PT_PHDR = 6
146PT_TLS = 7
147PT_LOOS = 0x60000000
148PT_HIOS = 0x6fffffff
149PT_LOPROC = 0x70000000
150PT_HIPROC = 0x7fffffff
151PT_GNU_EH_FRAME = 0x6474e550
152PT_SUNW_EH_FRAME = 0x6474e550
153PT_SUNW_UNWIND = 0x6464e550
154PT_GNU_STACK = 0x6474e551
155PT_GNU_RELRO = 0x6474e552
156PT_ARM_ARCHEXT = 0x70000000
157PT_ARM_EXIDX = 0x70000001
158PT_ARM_UNWIND = 0x70000001
159PT_MIPS_REGINFO = 0x70000000
160PT_MIPS_RTPROC = 0x70000001
161PT_MIPS_OPTIONS = 0x70000002
162PT_MIPS_ABIFLAGS = 0x70000003
163
164# Dynamic array tags
165# Name     Value        d_un          Executable  Shared Object
166DT_NULL = 0             # ignored     mandatory   mandatory
167DT_NEEDED = 1           # d_val       optional    optional
168DT_PLTRELSZ = 2         # d_val       optional    optional
169DT_PLTGOT = 3           # d_ptr       optional    optional
170DT_HASH = 4             # d_ptr       mandatory   mandatory
171DT_STRTAB = 5           # d_ptr       mandatory   mandatory
172DT_SYMTAB = 6           # d_ptr       mandatory   mandatory
173DT_RELA = 7             # d_ptr       mandatory   optional
174DT_RELASZ = 8           # d_val       mandatory   optional
175DT_RELAENT = 9          # d_val       mandatory   optional
176DT_STRSZ = 10           # d_val       mandatory   mandatory
177DT_SYMENT = 11          # d_val       mandatory   mandatory
178DT_INIT = 12            # d_ptr       optional    optional
179DT_FINI = 13            # d_ptr       optional    optional
180DT_SONAME = 14          # d_val       ignored     optional
181DT_RPATH = 15           # d_val       optional    ignored
182DT_SYMBOLIC = 16        # ignored     ignored     optional
183DT_REL = 17             # d_ptr       mandatory   optional
184DT_RELSZ = 18           # d_val       mandatory   optional
185DT_RELENT = 19          # d_val       mandatory   optional
186DT_PLTREL = 20          # d_val       optional    optional
187DT_DEBUG = 21           # d_ptr       optional    ignored
188DT_TEXTREL = 22         # ignored     optional    optional
189DT_JMPREL = 23          # d_ptr       optional    optional
190DT_BIND_NOW = 24        # ignored     optional    optional
191DT_INIT_ARRAY = 25      # d_ptr       optional    optional
192DT_FINI_ARRAY = 26      # d_ptr       optional    optional
193DT_INIT_ARRAYSZ = 27    # d_val       optional    optional
194DT_FINI_ARRAYSZ = 28    # d_val       optional    optional
195DT_RUNPATH = 29         # d_val       optional    optional
196DT_FLAGS = 30           # d_val       optional    optional
197DT_ENCODING = 32        # unspecified unspecified unspecified
198DT_LOOS = 0x6000000D    # unspecified unspecified unspecified
199DT_ANDROID_REL = DT_LOOS + 2      # d_ptr
200DT_ANDROID_RELSZ = DT_LOOS + 3    # d_val
201DT_ANRDOID_RELA = DT_LOOS + 4     # d_ptr
202DT_ANRDOID_RELASZ = DT_LOOS + 5   # d_val
203DT_RELR = 0x6fffe000              # d_ptr
204DT_RELRSZ = 0x6fffe001            # d_val
205DT_RELRENT = 0x6fffe003           # d_val
206