1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*-
3  * Copyright (c) 1996-1998 John D. Polstra.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #ifndef _SYS_ELF64_H_
31 #define _SYS_ELF64_H_ 1
32 
33 #include <elf_common.h>
34 #include <stdint.h>
35 
36 /*
37  * ELF definitions common to all 64-bit architectures.
38  */
39 
40 typedef uint64_t	Elf64_Addr;
41 typedef uint16_t	Elf64_Half;
42 typedef uint64_t	Elf64_Off;
43 typedef int32_t		Elf64_Sword;
44 typedef int64_t		Elf64_Sxword;
45 typedef uint32_t	Elf64_Word;
46 typedef uint64_t	Elf64_Lword;
47 typedef uint64_t	Elf64_Xword;
48 
49 /*
50  * Types of dynamic symbol hash table bucket and chain elements.
51  *
52  * This is inconsistent among 64 bit architectures, so a machine dependent
53  * typedef is required.
54  */
55 
56 typedef Elf64_Word	Elf64_Hashelt;
57 
58 /* Non-standard class-dependent datatype used for abstraction. */
59 typedef Elf64_Xword	Elf64_Size;
60 typedef Elf64_Sxword	Elf64_Ssize;
61 
62 /*
63  * ELF header.
64  */
65 
66 typedef struct {
67 	unsigned char	e_ident[EI_NIDENT];	/* File identification. */
68 	Elf64_Half	e_type;		/* File type. */
69 	Elf64_Half	e_machine;	/* Machine architecture. */
70 	Elf64_Word	e_version;	/* ELF format version. */
71 	Elf64_Addr	e_entry;	/* Entry point. */
72 	Elf64_Off	e_phoff;	/* Program header file offset. */
73 	Elf64_Off	e_shoff;	/* Section header file offset. */
74 	Elf64_Word	e_flags;	/* Architecture-specific flags. */
75 	Elf64_Half	e_ehsize;	/* Size of ELF header in bytes. */
76 	Elf64_Half	e_phentsize;	/* Size of program header entry. */
77 	Elf64_Half	e_phnum;	/* Number of program header entries. */
78 	Elf64_Half	e_shentsize;	/* Size of section header entry. */
79 	Elf64_Half	e_shnum;	/* Number of section header entries. */
80 	Elf64_Half	e_shstrndx;	/* Section name strings section. */
81 } Elf64_Ehdr;
82 
83 /*
84  * Section header.
85  */
86 
87 typedef struct {
88 	Elf64_Word	sh_name;	/* Section name (index into the
89 					   section header string table). */
90 	Elf64_Word	sh_type;	/* Section type. */
91 	Elf64_Xword	sh_flags;	/* Section flags. */
92 	Elf64_Addr	sh_addr;	/* Address in memory image. */
93 	Elf64_Off	sh_offset;	/* Offset in file. */
94 	Elf64_Xword	sh_size;	/* Size in bytes. */
95 	Elf64_Word	sh_link;	/* Index of a related section. */
96 	Elf64_Word	sh_info;	/* Depends on section type. */
97 	Elf64_Xword	sh_addralign;	/* Alignment in bytes. */
98 	Elf64_Xword	sh_entsize;	/* Size of each entry in section. */
99 } Elf64_Shdr;
100 
101 /*
102  * Program header.
103  */
104 
105 typedef struct {
106 	Elf64_Word	p_type;		/* Entry type. */
107 	Elf64_Word	p_flags;	/* Access permission flags. */
108 	Elf64_Off	p_offset;	/* File offset of contents. */
109 	Elf64_Addr	p_vaddr;	/* Virtual address in memory image. */
110 	Elf64_Addr	p_paddr;	/* Physical address (not used). */
111 	Elf64_Xword	p_filesz;	/* Size of contents in file. */
112 	Elf64_Xword	p_memsz;	/* Size of contents in memory. */
113 	Elf64_Xword	p_align;	/* Alignment in memory and file. */
114 } Elf64_Phdr;
115 
116 /*
117  * Dynamic structure.  The ".dynamic" section contains an array of them.
118  */
119 
120 typedef struct {
121 	Elf64_Sxword	d_tag;		/* Entry type. */
122 	union {
123 		Elf64_Xword	d_val;	/* Integer value. */
124 		Elf64_Addr	d_ptr;	/* Address value. */
125 	} d_un;
126 } Elf64_Dyn;
127 
128 /*
129  * Relocation entries.
130  */
131 
132 /* Relocations that don't need an addend field. */
133 typedef struct {
134 	Elf64_Addr	r_offset;	/* Location to be relocated. */
135 	Elf64_Xword	r_info;		/* Relocation type and symbol index. */
136 } Elf64_Rel;
137 
138 /* Relocations that need an addend field. */
139 typedef struct {
140 	Elf64_Addr	r_offset;	/* Location to be relocated. */
141 	Elf64_Xword	r_info;		/* Relocation type and symbol index. */
142 	Elf64_Sxword	r_addend;	/* Addend. */
143 } Elf64_Rela;
144 
145 /* Macros for accessing the fields of r_info. */
146 #define	ELF64_R_SYM(info)	((info) >> 32)
147 #define	ELF64_R_TYPE(info)	((info) & 0xffffffffL)
148 
149 /* Macro for constructing r_info from field values. */
150 #define	ELF64_R_INFO(sym, type)	(((sym) << 32) + ((type) & 0xffffffffL))
151 
152 #define	ELF64_R_TYPE_DATA(info)	(((Elf64_Xword)(info)<<32)>>40)
153 #define	ELF64_R_TYPE_ID(info)	(((Elf64_Xword)(info)<<56)>>56)
154 #define	ELF64_R_TYPE_INFO(data, type)	\
155 				(((Elf64_Xword)(data)<<8)+(Elf64_Xword)(type))
156 
157 /*
158  *	Note entry header
159  */
160 typedef Elf_Note Elf64_Nhdr;
161 
162 /*
163  *	Move entry
164  */
165 typedef struct {
166 	Elf64_Lword	m_value;	/* symbol value */
167 	Elf64_Xword 	m_info;		/* size + index */
168 	Elf64_Xword	m_poffset;	/* symbol offset */
169 	Elf64_Half	m_repeat;	/* repeat count */
170 	Elf64_Half	m_stride;	/* stride info */
171 } Elf64_Move;
172 
173 #define	ELF64_M_SYM(info)	((info)>>8)
174 #define	ELF64_M_SIZE(info)	((unsigned char)(info))
175 #define	ELF64_M_INFO(sym, size)	(((sym)<<8)+(unsigned char)(size))
176 
177 /*
178  *	Hardware/Software capabilities entry
179  */
180 typedef struct {
181 	Elf64_Xword	c_tag;		/* how to interpret value */
182 	union {
183 		Elf64_Xword	c_val;
184 		Elf64_Addr	c_ptr;
185 	} c_un;
186 } Elf64_Cap;
187 
188 /*
189  * Symbol table entries.
190  */
191 
192 typedef struct {
193 	Elf64_Word	st_name;	/* String table index of name. */
194 	unsigned char	st_info;	/* Type and binding information. */
195 	unsigned char	st_other;	/* Reserved (not used). */
196 	Elf64_Half	st_shndx;	/* Section index of symbol. */
197 	Elf64_Addr	st_value;	/* Symbol value. */
198 	Elf64_Xword	st_size;	/* Size of associated object. */
199 } Elf64_Sym;
200 
201 /* Macros for accessing the fields of st_info. */
202 #define	ELF64_ST_BIND(info)		((info) >> 4)
203 #define	ELF64_ST_TYPE(info)		((info) & 0xf)
204 
205 /* Macro for constructing st_info from field values. */
206 #define	ELF64_ST_INFO(bind, type)	(((bind) << 4) + ((type) & 0xf))
207 
208 /* Macro for accessing the fields of st_other. */
209 #define	ELF64_ST_VISIBILITY(oth)	((oth) & 0x3)
210 
211 /* Structures used by Sun & GNU-style symbol versioning. */
212 typedef struct {
213 	Elf64_Half	vd_version;
214 	Elf64_Half	vd_flags;
215 	Elf64_Half	vd_ndx;
216 	Elf64_Half	vd_cnt;
217 	Elf64_Word	vd_hash;
218 	Elf64_Word	vd_aux;
219 	Elf64_Word	vd_next;
220 } Elf64_Verdef;
221 
222 typedef struct {
223 	Elf64_Word	vda_name;
224 	Elf64_Word	vda_next;
225 } Elf64_Verdaux;
226 
227 typedef struct {
228 	Elf64_Half	vn_version;
229 	Elf64_Half	vn_cnt;
230 	Elf64_Word	vn_file;
231 	Elf64_Word	vn_aux;
232 	Elf64_Word	vn_next;
233 } Elf64_Verneed;
234 
235 typedef struct {
236 	Elf64_Word	vna_hash;
237 	Elf64_Half	vna_flags;
238 	Elf64_Half	vna_other;
239 	Elf64_Word	vna_name;
240 	Elf64_Word	vna_next;
241 } Elf64_Vernaux;
242 
243 typedef Elf64_Half Elf64_Versym;
244 
245 typedef struct {
246 	Elf64_Half	si_boundto;	/* direct bindings - symbol bound to */
247 	Elf64_Half	si_flags;	/* per symbol flags */
248 } Elf64_Syminfo;
249 
250 #endif /* !_SYS_ELF64_H_ */
251