1/** \file
2 * \brief ELF file format definitions
3 */
4
5/*
6 * Copyright (c) 2007-2010, ETH Zurich.
7 * Copyright (c) 2015, Hewlett Packard Enterprise Development LP.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstr. 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15/*-
16 * Copyright (c) 1998 John D. Polstra.
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 *    notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 *    notice, this list of conditions and the following disclaimer in the
26 *    documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * $FreeBSD: src/sys/sys/elf_common.h,v 1.23 2007/12/02 00:05:18 jb Exp $
41 */
42
43#ifndef ELF_H
44#define ELF_H
45
46#include <sys/cdefs.h>
47
48__BEGIN_DECLS
49
50/* Indexes into the e_ident array.  Keep synced with
51   http://www.sco.com/developers/gabi/latest/ch4.eheader.html */
52#define EI_MAG0         0       /* Magic number, byte 0. */
53#define EI_MAG1         1       /* Magic number, byte 1. */
54#define EI_MAG2         2       /* Magic number, byte 2. */
55#define EI_MAG3         3       /* Magic number, byte 3. */
56#define EI_CLASS        4       /* Class of machine. */
57#define EI_DATA         5       /* Data format. */
58#define EI_VERSION      6       /* ELF format version. */
59#define EI_OSABI        7       /* Operating system / ABI identification */
60#define EI_ABIVERSION   8       /* ABI version */
61#define OLD_EI_BRAND    8       /* Start of architecture identification. */
62#define EI_PAD          9       /* Start of padding (per SVR4 ABI). */
63#define EI_NIDENT       16      /* Size of e_ident array. */
64
65/* Values for the magic number bytes. */
66#define ELFMAG0         0x7f
67#define ELFMAG1         'E'
68#define ELFMAG2         'L'
69#define ELFMAG3         'F'
70#define ELFMAG          "\177ELF"       /* magic string */
71#define SELFMAG         4               /* magic string size */
72
73/* Values for e_ident[EI_VERSION] and e_version. */
74#define EV_NONE         0
75#define EV_CURRENT      1
76
77/* Values for e_ident[EI_CLASS]. */
78#define ELFCLASSNONE    0       /* Unknown class. */
79#define ELFCLASS32      1       /* 32-bit architecture. */
80#define ELFCLASS64      2       /* 64-bit architecture. */
81
82/* Values for e_ident[EI_DATA]. */
83#define ELFDATANONE     0       /* Unknown data format. */
84#define ELFDATA2LSB     1       /* 2's complement little-endian. */
85#define ELFDATA2MSB     2       /* 2's complement big-endian. */
86
87/* Values for e_ident[EI_OSABI]. */
88#define ELFOSABI_NONE           0       /* UNIX System V ABI */
89#define ELFOSABI_HPUX           1       /* HP-UX operating system */
90#define ELFOSABI_NETBSD         2       /* NetBSD */
91#define ELFOSABI_LINUX          3       /* GNU/Linux */
92#define ELFOSABI_HURD           4       /* GNU/Hurd */
93#define ELFOSABI_86OPEN         5       /* 86Open common IA32 ABI */
94#define ELFOSABI_SOLARIS        6       /* Solaris */
95#define ELFOSABI_AIX            7       /* AIX */
96#define ELFOSABI_IRIX           8       /* IRIX */
97#define ELFOSABI_FREEBSD        9       /* FreeBSD */
98#define ELFOSABI_TRU64          10      /* TRU64 UNIX */
99#define ELFOSABI_MODESTO        11      /* Novell Modesto */
100#define ELFOSABI_OPENBSD        12      /* OpenBSD */
101#define ELFOSABI_OPENVMS        13      /* Open VMS */
102#define ELFOSABI_NSK            14      /* HP Non-Stop Kernel */
103#define ELFOSABI_ARM            97      /* ARM */
104#define ELFOSABI_STANDALONE     255     /* Standalone (embedded) application */
105
106#define ELFOSABI_SYSV           ELFOSABI_NONE   /* symbol used in old spec */
107#define ELFOSABI_MONTEREY       ELFOSABI_AIX    /* Monterey */
108
109/* e_ident */
110#define IS_ELF(ehdr)    ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
111                         (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
112                         (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
113                         (ehdr).e_ident[EI_MAG3] == ELFMAG3)
114
115/* Values for e_type. */
116#define ET_NONE         0       /* Unknown type. */
117#define ET_REL          1       /* Relocatable. */
118#define ET_EXEC         2       /* Executable. */
119#define ET_DYN          3       /* Shared object. */
120#define ET_CORE         4       /* Core file. */
121#define ET_LOOS         0xfe00  /* First operating system specific. */
122#define ET_HIOS         0xfeff  /* Last operating system-specific. */
123#define ET_LOPROC       0xff00  /* First processor-specific. */
124#define ET_HIPROC       0xffff  /* Last processor-specific. */
125
126/* Values for e_machine. */
127#define EM_NONE         0       /* Unknown machine. */
128#define EM_M32          1       /* AT&T WE32100. */
129#define EM_SPARC        2       /* Sun SPARC. */
130#define EM_386          3       /* Intel i386. */
131#define EM_68K          4       /* Motorola 68000. */
132#define EM_88K          5       /* Motorola 88000. */
133#define EM_860          7       /* Intel i860. */
134#define EM_MIPS         8       /* MIPS R3000 Big-Endian only. */
135#define EM_S370         9       /* IBM System/370. */
136#define EM_MIPS_RS3_LE  10      /* MIPS R3000 Little-Endian. */
137#define EM_PARISC       15      /* HP PA-RISC. */
138#define EM_VPP500       17      /* Fujitsu VPP500. */
139#define EM_SPARC32PLUS  18      /* SPARC v8plus. */
140#define EM_960          19      /* Intel 80960. */
141#define EM_PPC          20      /* PowerPC 32-bit. */
142#define EM_PPC64        21      /* PowerPC 64-bit. */
143#define EM_S390         22      /* IBM System/390. */
144#define EM_V800         36      /* NEC V800. */
145#define EM_FR20         37      /* Fujitsu FR20. */
146#define EM_RH32         38      /* TRW RH-32. */
147#define EM_RCE          39      /* Motorola RCE. */
148#define EM_ARM          40      /* ARM. */
149#define EM_SH           42      /* Hitachi SH. */
150#define EM_SPARCV9      43      /* SPARC v9 64-bit. */
151#define EM_TRICORE      44      /* Siemens TriCore embedded processor. */
152#define EM_ARC          45      /* Argonaut RISC Core. */
153#define EM_H8_300       46      /* Hitachi H8/300. */
154#define EM_H8_300H      47      /* Hitachi H8/300H. */
155#define EM_H8S          48      /* Hitachi H8S. */
156#define EM_H8_500       49      /* Hitachi H8/500. */
157#define EM_IA_64        50      /* Intel IA-64 Processor. */
158#define EM_MIPS_X       51      /* Stanford MIPS-X. */
159#define EM_COLDFIRE     52      /* Motorola ColdFire. */
160#define EM_68HC12       53      /* Motorola M68HC12. */
161#define EM_MMA          54      /* Fujitsu MMA. */
162#define EM_PCP          55      /* Siemens PCP. */
163#define EM_NCPU         56      /* Sony nCPU. */
164#define EM_NDR1         57      /* Denso NDR1 microprocessor. */
165#define EM_STARCORE     58      /* Motorola Star*Core processor. */
166#define EM_ME16         59      /* Toyota ME16 processor. */
167#define EM_ST100        60      /* STMicroelectronics ST100 processor. */
168#define EM_TINYJ        61      /* Advanced Logic Corp. TinyJ processor. */
169#define EM_X86_64       62      /* Advanced Micro Devices x86-64 */
170#define EM_AMD64        EM_X86_64       /* Advanced Micro Devices x86-64 (compat) */
171#define EM_K1OM         181     /* Intel K1OM (Xeon Phi) */
172#define EM_AARCH64      183     /* ARM 64 bit */
173
174/* Non-standard or deprecated. */
175#define EM_486          6       /* Intel i486. */
176#define EM_MIPS_RS4_BE  10      /* MIPS R4000 Big-Endian */
177#define EM_ALPHA_STD    41      /* Digital Alpha (standard value). */
178#define EM_ALPHA        0x9026  /* Alpha (written in the absence of an ABI) */
179
180/* Special section indexes. */
181#define SHN_UNDEF            0          /* Undefined, missing, irrelevant. */
182#define SHN_LORESERVE   0xff00          /* First of reserved range. */
183#define SHN_LOPROC      0xff00          /* First processor-specific. */
184#define SHN_HIPROC      0xff1f          /* Last processor-specific. */
185#define SHN_LOOS        0xff20          /* First operating system-specific. */
186#define SHN_HIOS        0xff3f          /* Last operating system-specific. */
187#define SHN_ABS         0xfff1          /* Absolute values. */
188#define SHN_COMMON      0xfff2          /* Common data. */
189#define SHN_XINDEX      0xffff          /* Escape -- index stored elsewhere. */
190#define SHN_HIRESERVE   0xffff          /* Last of reserved range. */
191
192/* sh_type */
193#define SHT_NULL                0       /* inactive */
194#define SHT_PROGBITS            1       /* program defined information */
195#define SHT_SYMTAB              2       /* symbol table section */
196#define SHT_STRTAB              3       /* string table section */
197#define SHT_RELA                4       /* relocation section with addends */
198#define SHT_HASH                5       /* symbol hash table section */
199#define SHT_DYNAMIC             6       /* dynamic section */
200#define SHT_NOTE                7       /* note section */
201#define SHT_NOBITS              8       /* no space section */
202#define SHT_REL                 9       /* relocation section - no addends */
203#define SHT_SHLIB               10      /* reserved - purpose unknown */
204#define SHT_DYNSYM              11      /* dynamic symbol table section */
205#define SHT_INIT_ARRAY          14      /* Initialization function pointers. */
206#define SHT_FINI_ARRAY          15      /* Termination function pointers. */
207#define SHT_PREINIT_ARRAY       16      /* Pre-initialization function ptrs. */
208#define SHT_GROUP               17      /* Section group. */
209#define SHT_SYMTAB_SHNDX        18      /* Section indexes (see SHN_XINDEX). */
210#define SHT_LOOS                0x60000000      /* First of OS specific semantics */
211#define SHT_HIOS                0x6fffffff      /* Last of OS specific semantics */
212#define SHT_LOPROC              0x70000000      /* reserved range for processor */
213#define SHT_AMD64_UNWIND        0x70000001      /* unwind information */
214#define SHT_HIPROC              0x7fffffff      /* specific section header types */
215#define SHT_LOUSER              0x80000000      /* reserved range for application */
216#define SHT_HIUSER              0xffffffff      /* specific indexes */
217
218/* Flags for sh_flags. */
219#define SHF_WRITE               0x1     /* Section contains writable data. */
220#define SHF_ALLOC               0x2     /* Section occupies memory. */
221#define SHF_EXECINSTR           0x4     /* Section contains instructions. */
222#define SHF_MERGE               0x10    /* Section may be merged. */
223#define SHF_STRINGS             0x20    /* Section contains strings. */
224#define SHF_INFO_LINK           0x40    /* sh_info holds section index. */
225#define SHF_LINK_ORDER          0x80    /* Special ordering requirements. */
226#define SHF_OS_NONCONFORMING    0x100   /* OS-specific processing required. */
227#define SHF_GROUP               0x200   /* Member of section group. */
228#define SHF_TLS                 0x400   /* Section contains TLS data. */
229#define SHF_MASKOS      0x0ff00000      /* OS-specific semantics. */
230#define SHF_MASKPROC    0xf0000000      /* Processor-specific semantics. */
231
232/* Values for p_type. */
233#define PT_NULL         0       /* Unused entry. */
234#define PT_LOAD         1       /* Loadable segment. */
235#define PT_DYNAMIC      2       /* Dynamic linking information segment. */
236#define PT_INTERP       3       /* Pathname of interpreter. */
237#define PT_NOTE         4       /* Auxiliary information. */
238#define PT_SHLIB        5       /* Reserved (not used). */
239#define PT_PHDR         6       /* Location of program header itself. */
240#define PT_TLS          7       /* Thread local storage segment */
241#define PT_LOOS         0x60000000      /* First OS-specific. */
242#define PT_BF_RELOC     PT_LOOS         /* Barrelfish relocatable segment. */
243#define PT_SUNW_UNWIND  0x6464e550      /* amd64 UNWIND program header */
244#define PT_GNU_EH_FRAME 0x6474e550
245#define PT_LOSUNW       0x6ffffffa
246#define PT_SUNWBSS      0x6ffffffa      /* Sun Specific segment */
247#define PT_SUNWSTACK    0x6ffffffb      /* describes the stack segment */
248#define PT_SUNWDTRACE   0x6ffffffc      /* private */
249#define PT_SUNWCAP      0x6ffffffd      /* hard/soft capabilities segment */
250#define PT_HISUNW       0x6fffffff
251#define PT_HIOS         0x6fffffff      /* Last OS-specific. */
252#define PT_LOPROC       0x70000000      /* First processor-specific type. */
253#define PT_HIPROC       0x7fffffff      /* Last processor-specific type. */
254
255/* Values for p_flags. */
256#define PF_X            0x1             /* Executable. */
257#define PF_W            0x2             /* Writable. */
258#define PF_R            0x4             /* Readable. */
259#define PF_MASKOS       0x0ff00000      /* Operating system-specific. */
260#define PF_MASKPROC     0xf0000000      /* Processor-specific. */
261
262/* Extended program header index. */
263#define PN_XNUM         0xffff
264
265/* Values for d_tag. */
266#define DT_NULL         0       /* Terminating entry. */
267#define DT_NEEDED       1       /* String table offset of a needed shared
268                                   library. */
269#define DT_PLTRELSZ     2       /* Total size in bytes of PLT relocations. */
270#define DT_PLTGOT       3       /* Processor-dependent address. */
271#define DT_HASH         4       /* Address of symbol hash table. */
272#define DT_STRTAB       5       /* Address of string table. */
273#define DT_SYMTAB       6       /* Address of symbol table. */
274#define DT_RELA         7       /* Address of ElfNN_Rela relocations. */
275#define DT_RELASZ       8       /* Total size of ElfNN_Rela relocations. */
276#define DT_RELAENT      9       /* Size of each ElfNN_Rela relocation entry. */
277#define DT_STRSZ        10      /* Size of string table. */
278#define DT_SYMENT       11      /* Size of each symbol table entry. */
279#define DT_INIT         12      /* Address of initialization function. */
280#define DT_FINI         13      /* Address of finalization function. */
281#define DT_SONAME       14      /* String table offset of shared object
282                                   name. */
283#define DT_RPATH        15      /* String table offset of library path. [sup] */
284#define DT_SYMBOLIC     16      /* Indicates "symbolic" linking. [sup] */
285#define DT_REL          17      /* Address of ElfNN_Rel relocations. */
286#define DT_RELSZ        18      /* Total size of ElfNN_Rel relocations. */
287#define DT_RELENT       19      /* Size of each ElfNN_Rel relocation. */
288#define DT_PLTREL       20      /* Type of relocation used for PLT. */
289#define DT_DEBUG        21      /* Reserved (not used). */
290#define DT_TEXTREL      22      /* Indicates there may be relocations in
291                                   non-writable segments. [sup] */
292#define DT_JMPREL       23      /* Address of PLT relocations. */
293#define DT_BIND_NOW     24      /* [sup] */
294#define DT_INIT_ARRAY   25      /* Address of the array of pointers to
295                                   initialization functions */
296#define DT_FINI_ARRAY   26      /* Address of the array of pointers to
297                                   termination functions */
298#define DT_INIT_ARRAYSZ 27      /* Size in bytes of the array of
299                                   initialization functions. */
300#define DT_FINI_ARRAYSZ 28      /* Size in bytes of the array of
301                                   terminationfunctions. */
302#define DT_RUNPATH      29      /* String table offset of a null-terminated
303                                   library search path string. */
304#define DT_FLAGS        30      /* Object specific flag values. */
305#define DT_ENCODING     32      /* Values greater than or equal to DT_ENCODING
306                                   and less than DT_LOOS follow the rules for
307                                   the interpretation of the d_un union
308                                   as follows: even == 'd_ptr', even == 'd_val'
309                                   or none */
310#define DT_PREINIT_ARRAY 32     /* Address of the array of pointers to
311                                   pre-initialization functions. */
312#define DT_PREINIT_ARRAYSZ 33   /* Size in bytes of the array of
313                                   pre-initialization functions. */
314#define DT_MAXPOSTAGS   34      /* number of positive tags */
315#define DT_LOOS         0x6000000d      /* First OS-specific */
316#define DT_SUNW_AUXILIARY       0x6000000d      /* symbol auxiliary name */
317#define DT_SUNW_RTLDINF         0x6000000e      /* ld.so.1 info (private) */
318#define DT_SUNW_FILTER          0x6000000f      /* symbol filter name */
319#define DT_SUNW_CAP             0x60000010      /* hardware/software */
320#define DT_HIOS         0x6ffff000      /* Last OS-specific */
321
322/*
323 * DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the
324 * Dyn.d_un.d_val field of the Elf*_Dyn structure.
325 */
326#define DT_VALRNGLO     0x6ffffd00
327#define DT_CHECKSUM     0x6ffffdf8      /* elf checksum */
328#define DT_PLTPADSZ     0x6ffffdf9      /* pltpadding size */
329#define DT_MOVEENT      0x6ffffdfa      /* move table entry size */
330#define DT_MOVESZ       0x6ffffdfb      /* move table size */
331#define DT_FEATURE_1    0x6ffffdfc      /* feature holder */
332#define DT_POSFLAG_1    0x6ffffdfd      /* flags for DT_* entries, effecting */
333                                        /*      the following DT_* entry. */
334                                        /*      See DF_P1_* definitions */
335#define DT_SYMINSZ      0x6ffffdfe      /* syminfo table size (in bytes) */
336#define DT_SYMINENT     0x6ffffdff      /* syminfo entry size (in bytes) */
337#define DT_VALRNGHI     0x6ffffdff
338
339/*
340 * DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the
341 * Dyn.d_un.d_ptr field of the Elf*_Dyn structure.
342 *
343 * If any adjustment is made to the ELF object after it has been
344 * built, these entries will need to be adjusted.
345 */
346#define DT_ADDRRNGLO    0x6ffffe00
347#define DT_CONFIG       0x6ffffefa      /* configuration information */
348#define DT_DEPAUDIT     0x6ffffefb      /* dependency auditing */
349#define DT_AUDIT        0x6ffffefc      /* object auditing */
350#define DT_PLTPAD       0x6ffffefd      /* pltpadding (sparcv9) */
351#define DT_MOVETAB      0x6ffffefe      /* move table */
352#define DT_SYMINFO      0x6ffffeff      /* syminfo table */
353#define DT_ADDRRNGHI    0x6ffffeff
354
355#define DT_VERSYM       0x6ffffff0      /* Address of versym section. */
356#define DT_RELACOUNT    0x6ffffff9      /* number of RELATIVE relocations */
357#define DT_RELCOUNT     0x6ffffffa      /* number of RELATIVE relocations */
358#define DT_FLAGS_1      0x6ffffffb      /* state flags - see DF_1_* defs */
359#define DT_VERDEF       0x6ffffffc      /* Address of verdef section. */
360#define DT_VERDEFNUM    0x6ffffffd      /* Number of elems in verdef section */
361#define DT_VERNEED      0x6ffffffe      /* Address of verneed section. */
362#define DT_VERNEEDNUM   0x6fffffff      /* Number of elems in verneed section */
363
364#define DT_LOPROC       0x70000000      /* First processor-specific type. */
365#define DT_DEPRECATED_SPARC_REGISTER    0x7000001
366#define DT_AUXILIARY    0x7ffffffd      /* shared library auxiliary name */
367#define DT_USED         0x7ffffffe      /* ignored - same as needed */
368#define DT_FILTER       0x7fffffff      /* shared library filter name */
369#define DT_HIPROC       0x7fffffff      /* Last processor-specific type. */
370
371/* Values for DT_FLAGS */
372#define DF_ORIGIN       0x0001  /* Indicates that the object being loaded may
373                                   make reference to the $ORIGIN substitution
374                                   string */
375#define DF_SYMBOLIC     0x0002  /* Indicates "symbolic" linking. */
376#define DF_TEXTREL      0x0004  /* Indicates there may be relocations in
377                                   non-writable segments. */
378#define DF_BIND_NOW     0x0008  /* Indicates that the dynamic linker should
379                                   process all relocations for the object
380                                   containing this entry before transferring
381                                   control to the program. */
382#define DF_STATIC_TLS   0x0010  /* Indicates that the shared object or
383                                   executable contains code using a static
384                                   thread-local storage scheme. */
385
386/* Values for n_type.  Used in core files. */
387#define NT_PRSTATUS     1       /* Process status. */
388#define NT_FPREGSET     2       /* Floating point registers. */
389#define NT_PRPSINFO     3       /* Process state info. */
390
391/* Symbol Binding - ELFNN_ST_BIND - st_info */
392#define STB_LOCAL       0       /* Local symbol */
393#define STB_GLOBAL      1       /* Global symbol */
394#define STB_WEAK        2       /* like global - lower precedence */
395#define STB_LOOS        10      /* Reserved range for operating system */
396#define STB_HIOS        12      /*   specific semantics. */
397#define STB_LOPROC      13      /* reserved range for processor */
398#define STB_HIPROC      15      /*   specific semantics. */
399
400/* Symbol type - ELFNN_ST_TYPE - st_info */
401#define STT_NOTYPE      0       /* Unspecified type. */
402#define STT_OBJECT      1       /* Data object. */
403#define STT_FUNC        2       /* Function. */
404#define STT_SECTION     3       /* Section. */
405#define STT_FILE        4       /* Source file. */
406#define STT_COMMON      5       /* Uninitialized common block. */
407#define STT_TLS         6       /* TLS object. */
408#define STT_NUM         7
409#define STT_LOOS        10      /* Reserved range for operating system */
410#define STT_HIOS        12      /*   specific semantics. */
411#define STT_LOPROC      13      /* reserved range for processor */
412#define STT_HIPROC      15      /*   specific semantics. */
413
414/* Symbol visibility - ELFNN_ST_VISIBILITY - st_other */
415#define STV_DEFAULT     0x0     /* Default visibility (see binding). */
416#define STV_INTERNAL    0x1     /* Special meaning in relocatable objects. */
417#define STV_HIDDEN      0x2     /* Not visible. */
418#define STV_PROTECTED   0x3     /* Visible but not preemptible. */
419
420/* Special symbol table indexes. */
421#define STN_UNDEF       0       /* Undefined symbol index. */
422
423/* Symbol versioning flags. */
424#define VER_DEF_CURRENT 1
425#define VER_DEF_IDX(x)  VER_NDX(x)
426
427#define VER_FLG_BASE    0x01
428#define VER_FLG_WEAK    0x02
429
430#define VER_NEED_CURRENT        1
431#define VER_NEED_WEAK   (1u << 15)
432#define VER_NEED_HIDDEN VER_NDX_HIDDEN
433#define VER_NEED_IDX(x) VER_NDX(x)
434
435#define VER_NDX_LOCAL   0
436#define VER_NDX_GLOBAL  1
437#define VER_NDX_GIVEN   2
438
439#define VER_NDX_HIDDEN  (1u << 15)
440#define VER_NDX(x)      ((x) & ~(1u << 15))
441
442#define CA_SUNW_NULL    0
443#define CA_SUNW_HW_1    1               /* first hardware capabilities entry */
444#define CA_SUNW_SF_1    2               /* first software capabilities entry */
445
446/*
447 * Syminfo flag values
448 */
449#define SYMINFO_FLG_DIRECT      0x0001  /* symbol ref has direct association */
450                                        /*      to object containing defn. */
451#define SYMINFO_FLG_PASSTHRU    0x0002  /* ignored - see SYMINFO_FLG_FILTER */
452#define SYMINFO_FLG_COPY        0x0004  /* symbol is a copy-reloc */
453#define SYMINFO_FLG_LAZYLOAD    0x0008  /* object containing defn should be */
454                                        /*      lazily-loaded */
455#define SYMINFO_FLG_DIRECTBIND  0x0010  /* ref should be bound directly to */
456                                        /*      object containing defn. */
457#define SYMINFO_FLG_NOEXTDIRECT 0x0020  /* don't let an external reference */
458                                        /*      directly bind to this symbol */
459#define SYMINFO_FLG_FILTER      0x0002  /* symbol ref is associated to a */
460#define SYMINFO_FLG_AUXILIARY   0x0040  /*      standard or auxiliary filter */
461
462/*
463 * Syminfo.si_boundto values.
464 */
465#define SYMINFO_BT_SELF         0xffff  /* symbol bound to self */
466#define SYMINFO_BT_PARENT       0xfffe  /* symbol bound to parent */
467#define SYMINFO_BT_NONE         0xfffd  /* no special symbol binding */
468#define SYMINFO_BT_EXTERN       0xfffc  /* symbol defined as external */
469#define SYMINFO_BT_LOWRESERVE   0xff00  /* beginning of reserved entries */
470
471/*
472 * Syminfo version values.
473 */
474#define SYMINFO_NONE            0       /* Syminfo version */
475#define SYMINFO_CURRENT         1
476#define SYMINFO_NUM             2
477
478#define ELF64_R_SYM(i)          ((i) >> 32)
479#define ELF64_R_TYPE(i)         ((i) & 0xffffffffL)
480
481#define ELF32_R_SYM(i)          ((i)>>8)
482#define ELF32_R_TYPE(i)         ((uint8_t)(i))
483#define ELF32_R_INFO(s,t)       (((s)<<8)+(uint8_t)(t))
484
485/* AMD x86-64 relocations.  */
486#define R_X86_64_NONE           0       /* No relocation. */
487#define R_X86_64_64             1       /* Add 64 bit symbol value. */
488#define R_X86_64_PC32           2       /* PC-relative 32 bit signed sym value. */
489#define R_X86_64_GOT32          3       /* PC-relative 32 bit GOT offset. */
490#define R_X86_64_PLT32          4       /* PC-relative 32 bit PLT offset. */
491#define R_X86_64_COPY           5       /* Copy data from shared object. */
492#define R_X86_64_GLOB_DAT       6       /* Set GOT entry to data address. */
493#define R_X86_64_JMP_SLOT       7       /* Set GOT entry to code address. */
494#define R_X86_64_RELATIVE       8       /* Add load address of shared object. */
495#define R_X86_64_GOTPCREL       9       /* Add 32 bit signed pcrel offset to GOT. */
496#define R_X86_64_32             10      /* Add 32 bit zero extended symbol value */
497#define R_X86_64_32S            11      /* Add 32 bit sign extended symbol value */
498#define R_X86_64_16             12      /* Add 16 bit zero extended symbol value */
499#define R_X86_64_PC16           13      /* Add 16 bit signed extended pc relative symbol value */
500#define R_X86_64_8              14      /* Add 8 bit zero extended symbol value */
501#define R_X86_64_PC8            15      /* Add 8 bit signed extended pc relative symbol value */
502#define R_X86_64_DTPMOD64       16      /* ID of module containing symbol */
503#define R_X86_64_DTPOFF64       17      /* Offset in TLS block */
504#define R_X86_64_TPOFF64        18      /* Offset in static TLS block */
505#define R_X86_64_TLSGD          19      /* PC relative offset to GD GOT entry */
506#define R_X86_64_TLSLD          20      /* PC relative offset to LD GOT entry */
507#define R_X86_64_DTPOFF32       21      /* Offset in TLS block */
508#define R_X86_64_GOTTPOFF       22      /* PC relative offset to IE GOT entry */
509#define R_X86_64_TPOFF32        23      /* Offset in static TLS block */
510
511/* i386 relocations.  */
512#define R_386_NONE              0       /* No relocation. */
513#define R_386_32                1       /* Add symbol value. */
514#define R_386_PC32              2       /* Add PC-relative symbol value. */
515#define R_386_GOT32             3       /* Add PC-relative GOT offset. */
516#define R_386_PLT32             4       /* Add PC-relative PLT offset. */
517#define R_386_COPY              5       /* Copy data from shared object. */
518#define R_386_GLOB_DAT          6       /* Set GOT entry to data address. */
519#define R_386_JMP_SLOT          7       /* Set GOT entry to code address. */
520#define R_386_RELATIVE          8       /* Add load address of shared object. */
521#define R_386_GOTOFF            9       /* Add GOT-relative symbol address. */
522#define R_386_GOTPC             10      /* Add PC-relative GOT table address. */
523#define R_386_TLS_TPOFF         14      /* Negative offset in static TLS block */
524#define R_386_TLS_IE            15      /* Absolute address of GOT for -ve static TLS */
525#define R_386_TLS_GOTIE         16      /* GOT entry for negative static TLS block */
526#define R_386_TLS_LE            17      /* Negative offset relative to static TLS */
527#define R_386_TLS_GD            18      /* 32 bit offset to GOT (index,off) pair */
528#define R_386_TLS_LDM           19      /* 32 bit offset to GOT (index,zero) pair */
529#define R_386_TLS_GD_32         24      /* 32 bit offset to GOT (index,off) pair */
530#define R_386_TLS_GD_PUSH       25      /* pushl instruction for Sun ABI GD sequence */
531#define R_386_TLS_GD_CALL       26      /* call instruction for Sun ABI GD sequence */
532#define R_386_TLS_GD_POP        27      /* popl instruction for Sun ABI GD sequence */
533#define R_386_TLS_LDM_32        28      /* 32 bit offset to GOT (index,zero) pair */
534#define R_386_TLS_LDM_PUSH      29      /* pushl instruction for Sun ABI LD sequence */
535#define R_386_TLS_LDM_CALL      30      /* call instruction for Sun ABI LD sequence */
536#define R_386_TLS_LDM_POP       31      /* popl instruction for Sun ABI LD sequence */
537#define R_386_TLS_LDO_32        32      /* 32 bit offset from start of TLS block */
538#define R_386_TLS_IE_32         33      /* 32 bit offset to GOT static TLS offset entry */
539#define R_386_TLS_LE_32         34      /* 32 bit offset within static TLS block */
540#define R_386_TLS_DTPMOD32      35      /* GOT entry containing TLS index */
541#define R_386_TLS_DTPOFF32      36      /* GOT entry containing TLS offset */
542#define R_386_TLS_TPOFF32       37      /* GOT entry of -ve static TLS offset */
543
544/* ARM relocations. A LOT MISSING! */
545#define R_ARM_NONE				0
546#define R_ARM_ABS32				2
547#define R_ARM_RELATIVE			23
548
549/* AARCH64 relocations. A LOT MISSING! */
550#define R_AARCH64_NONE          0
551#define R_AARCH64_ABS64         257
552#define R_AARCH64_RELATIVE      1027
553
554/**
555 * \brief ELF64 file header.
556 */
557struct Elf64_Ehdr {
558    uint8_t        e_ident[EI_NIDENT];
559    uint16_t       e_type;
560    uint16_t       e_machine;
561    uint32_t       e_version;
562    uint64_t       e_entry;
563    uint64_t       e_phoff;
564    uint64_t       e_shoff;
565    uint32_t       e_flags;
566    uint16_t       e_ehsize;
567    uint16_t       e_phentsize;
568    uint16_t       e_phnum;
569    uint16_t       e_shentsize;
570    uint16_t       e_shnum;
571    uint16_t       e_shstrndx;
572} __attribute__ ((packed));
573
574/**
575 * \brief ELF64 program header.
576 */
577struct Elf64_Phdr {
578    uint32_t        p_type;
579    uint32_t        p_flags;
580    uint64_t        p_offset;
581    uint64_t        p_vaddr;
582    uint64_t        p_paddr;
583    uint64_t        p_filesz;
584    uint64_t        p_memsz;
585    uint64_t        p_align;
586} __attribute__ ((packed));
587
588/**
589 * \brief ELF64 section header.
590 */
591struct Elf64_Shdr {
592    uint32_t    sh_name;
593    uint32_t    sh_type;
594    uint64_t    sh_flags;
595    uint64_t    sh_addr;
596    uint64_t    sh_offset;
597    uint64_t    sh_size;
598    uint32_t    sh_link;
599    uint32_t    sh_info;
600    uint64_t    sh_addralign;
601    uint64_t    sh_entsize;
602} __attribute__ ((packed));
603
604/**
605 * \brief ELF64 relocation entry.
606 */
607struct Elf64_Rela {
608    uint64_t    r_offset;
609    uint64_t    r_info;
610    int64_t     r_addend;
611} __attribute__ ((packed));
612
613struct Elf64_Rel {
614    uint64_t    r_offset;
615    uint64_t    r_info;
616} __attribute__ ((packed));
617
618
619/**
620 * \brief ELF64 symbol table entry.
621 */
622struct Elf64_Sym {
623    uint32_t        st_name;
624    uint8_t         st_info;
625    uint8_t         st_other;
626    uint16_t        st_shndx;
627    uint64_t        st_value;
628    uint64_t        st_size;
629} __attribute__ ((packed));
630
631/**
632 * \brief ELF64 Dynamic section entry
633 */
634struct Elf64_Dyn {
635    int64_t         d_tag;
636    union {
637        uint64_t    d_val;
638        uint64_t    d_ptr;
639    } d_un;
640} __attribute__ ((packed));
641
642/**
643 * \brief ELF32 file header.
644 */
645struct Elf32_Ehdr {
646    uint8_t  e_ident[EI_NIDENT];        // 00
647    uint16_t e_type;                    // 16
648    uint16_t e_machine;                 // 18
649    uint32_t e_version;                 // 20
650    uint32_t e_entry;                   // 24
651    uint32_t e_phoff;                   // 28
652    uint32_t e_shoff;                   // 32
653    uint32_t e_flags;                   // 36
654    uint16_t e_ehsize;                  // 40
655    uint16_t e_phentsize;               // 42
656    uint16_t e_phnum;                   // 44
657    uint16_t e_shentsize;               // 46
658    uint16_t e_shnum;                   // 48
659    uint16_t e_shstrndx;                // 50
660};
661
662/**
663 * \brief ELF32 program header.
664 */
665struct  Elf32_Phdr {
666    uint32_t p_type;                    // 0
667    uint32_t p_offset;                  // 4
668    uint32_t p_vaddr;                   // 8
669    uint32_t p_paddr;                   // 12
670    uint32_t p_filesz;                  // 16
671    uint32_t p_memsz;                   // 20
672    uint32_t p_flags;                   // 24
673    uint32_t p_align;                   // 28
674};
675
676/**
677 * \brief ELF32 section header.
678 */
679struct Elf32_Shdr {
680    uint32_t sh_name;                   // 0
681    uint32_t sh_type;                   // 4
682    uint32_t sh_flags;                  // 8
683    uint32_t sh_addr;                   // 12
684    uint32_t sh_offset;                 // 16
685    uint32_t sh_size;                   // 20
686    uint32_t sh_link;                   // 24
687    uint32_t sh_info;                   // 28
688    uint32_t sh_addralign;              // 32
689    uint32_t sh_entsize;                // 36
690};
691
692/**
693 * \brief ELF32 relocation entry.
694 */
695struct Elf32_Rel {
696    uint32_t r_offset;
697    uint32_t r_info;
698};
699
700/**
701 * \brief ELF32 relocation entry with addend.
702 */
703struct Elf32_Rela {
704    uint32_t r_offset;
705    uint32_t r_info;
706    int32_t  r_addend;
707};
708
709/**
710 * \brief ELF32 symbol table entry.
711 */
712struct Elf32_Sym {
713    uint32_t st_name;
714    uint32_t st_value;
715    uint32_t st_size;
716    uint8_t  st_info;
717    uint8_t  st_other;
718    uint16_t st_shndx;
719};
720
721
722struct Elf32_Dyn {
723    int32_t          d_tag;
724    union {
725        uint32_t     d_val;
726        uint32_t     d_ptr;
727    } d_un;
728} __attribute__ ((packed));
729
730/**
731 * \brief ELF32 note entry
732 */
733struct Elf32_Nhdr {
734    uint32_t n_namesz;
735    uint32_t n_descsz;
736    uint32_t n_type;
737};
738
739/* A GNU build id (SHA-1 hash). */
740#define NT_GNU_BUILD_ID 3
741
742struct Elf64_Shdr *
743elf64_find_section_header_type(struct Elf64_Shdr *shdr,
744                               uint32_t entries, uint32_t type);
745struct Elf32_Shdr *
746elf32_find_section_header_type(struct Elf32_Shdr *shdr,
747                               uint32_t entries, uint32_t type);
748
749struct Elf64_Shdr *
750elf64_find_section_header_name(genvaddr_t elf_base, size_t elf_bytes,
751                               const char* section_name);
752struct Elf32_Shdr *
753elf32_find_section_header_name(genvaddr_t elf_base, size_t elf_bytes,
754                               const char* section_name);
755
756struct Elf64_Shdr *
757elf64_find_symtab(genvaddr_t elf_base, size_t elf_bytes);
758struct Elf32_Shdr *
759elf32_find_symtab(genvaddr_t elf_base, size_t elf_bytes);
760
761struct Elf64_Sym *
762elf64_find_symbol_by_name(genvaddr_t elf_base, size_t elf_bytes,
763                          const char *name, uint8_t contains, uint8_t type,
764                          uintptr_t *index);
765struct Elf32_Sym *
766elf32_find_symbol_by_name(genvaddr_t elf_base, size_t elf_bytes,
767                          const char *name,
768                          uint8_t contains, uint8_t type,
769                          uintptr_t *index);
770uint32_t
771elf64_count_symbol_by_name(genvaddr_t elf_base, size_t elf_bytes,
772                          const char *name, uint8_t contains, uint8_t type,
773                          size_t *ret_bytes);
774uint32_t
775elf32_count_symbol_by_name(genvaddr_t elf_base, size_t elf_bytes,
776                          const char *name, uint8_t contains, uint8_t type,
777                          size_t *ret_bytes);
778struct Elf64_Sym *
779elf64_find_symbol_by_addr(genvaddr_t elf_base, size_t elf_bytes,
780                          lvaddr_t addr, uintptr_t *index);
781struct Elf32_Sym *
782elf32_find_symbol_by_addr(genvaddr_t elf_base, size_t elf_bytes,
783                          lvaddr_t addr, uintptr_t *index);
784const char *
785elf64_get_symbolname(struct Elf64_Ehdr *head, struct Elf64_Sym *sym);
786const char *
787elf32_get_symbolname(struct Elf32_Ehdr *head, struct Elf32_Sym *sym);
788
789void elf64_relocate(genvaddr_t dst, genvaddr_t src,
790                    struct Elf64_Rela * rela, size_t size,
791                    struct Elf64_Sym * symtab, size_t symsize,
792                    genvaddr_t start, void *vbase);
793void elf32_relocate(genvaddr_t dst, genvaddr_t src,
794                    struct Elf32_Rel * rela, size_t size,
795                    struct Elf32_Sym * symtab, size_t symsize,
796                    genvaddr_t start, void *vbase);
797
798typedef errval_t (*elf_allocator_fn)(void *state, genvaddr_t base,
799                                     size_t size, uint32_t flags, void **ret);
800
801errval_t elf64_load(uint16_t em_machine, elf_allocator_fn allocate_func,
802                    void *state, lvaddr_t base, size_t size,
803                    genvaddr_t *retentry,
804                    genvaddr_t *ret_tlsbase, size_t *ret_tlsinitlen,
805                    size_t *ret_tlstotallen);
806
807errval_t elf32_load(uint16_t em_machine, elf_allocator_fn allocate_func,
808                    void *state, lvaddr_t base,
809                    size_t size, genvaddr_t *retentry,
810                    genvaddr_t *ret_tlsbase, size_t *ret_tlsinitlen,
811                    size_t *ret_tlstotallen);
812errval_t elf_load(uint16_t em_machine, elf_allocator_fn allocate_func,
813                  void *state, lvaddr_t base,
814                  size_t size, genvaddr_t *retentry);
815
816errval_t elf_load_tls(uint16_t em_machine, elf_allocator_fn allocate_func,
817                      void *state, lvaddr_t base,
818                      size_t size, genvaddr_t *retentry,
819                      genvaddr_t *ret_tlsbase, size_t *ret_tlsinitlen,
820                      size_t *ret_tlstotallen);
821
822size_t  elf_virtual_size(lvaddr_t base);
823
824errval_t elf_get_eh_info(lvaddr_t elfbase, size_t elfsize,
825                         lvaddr_t *eh_frame, size_t *eh_frame_size,
826                         lvaddr_t *eh_frame_hdr, size_t *eh_frame_hdr_size);
827
828
829
830genvaddr_t elf_virtual_base32(struct Elf32_Ehdr *ehead);
831genvaddr_t elf_virtual_base64(struct Elf64_Ehdr *ehead);
832genvaddr_t elf_virtual_base(lvaddr_t base);
833
834__END_DECLS
835
836#endif // ELF_H
837