xref: /openbsd/lib/libelf/libelf.h (revision a1b5ec25)
1 /*-
2  * Copyright (c) 2006,2008-2010 Joseph Koshy
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $Id: libelf.h,v 1.1 2019/02/01 05:27:38 jsg Exp $
27  */
28 
29 #ifndef	_LIBELF_H_
30 #define	_LIBELF_H_
31 
32 #include <sys/types.h>
33 
34 #include <sys/exec_elf.h>
35 
36 /* Non-standard public data structures */
37 
38 /* Capability */
39 typedef struct {
40 	Elf32_Word	c_tag;
41 	union {
42 		Elf32_Word	c_val;
43 		Elf32_Addr	c_ptr;
44 	} c_un;
45 } Elf32_Cap;
46 
47 typedef struct {
48 	Elf64_Xword	c_tag;
49 	union {
50 		Elf64_Xword	c_val;
51 		Elf64_Addr	c_ptr;
52 	} c_un;
53 } Elf64_Cap;
54 
55 /* Move */
56 typedef struct {
57 	Elf32_Lword	m_value;
58 	Elf32_Word	m_info;
59 	Elf32_Word	m_poffset;
60 	Elf32_Half	m_repeat;
61 	Elf32_Half	m_stride;
62 } Elf32_Move;
63 
64 typedef struct {
65 	Elf64_Lword	m_value;
66 	Elf64_Xword	m_info;
67 	Elf64_Xword	m_poffset;
68 	Elf64_Half	m_repeat;
69 	Elf64_Half	m_stride;
70 } Elf64_Move;
71 
72 /* Syminfo Table */
73 typedef struct {
74 	Elf32_Half	si_boundto;
75 	Elf32_Half	si_flags;
76 } Elf32_Syminfo;
77 
78 typedef struct {
79 	Elf64_Half	si_boundto;
80 	Elf64_Half	si_flags;
81 } Elf64_Syminfo;
82 
83 /* Version Definition */
84 typedef struct {
85 	Elf32_Half	vd_version;
86 	Elf32_Half	vd_flags;
87 	Elf32_Half	vd_ndx;
88 	Elf32_Half	vd_cnt;
89 	Elf32_Word	vd_hash;
90 	Elf32_Word	vd_aux;
91 	Elf32_Word	vd_next;
92 } Elf32_Verdef;
93 
94 typedef struct {
95 	Elf32_Word	vda_name;
96 	Elf32_Word	vda_next;
97 } Elf32_Verdaux;
98 
99 typedef struct {
100 	Elf64_Half	vd_version;
101 	Elf64_Half	vd_flags;
102 	Elf64_Half	vd_ndx;
103 	Elf64_Half	vd_cnt;
104 	Elf64_Word	vd_hash;
105 	Elf64_Word	vd_aux;
106 	Elf64_Word	vd_next;
107 } Elf64_Verdef;
108 
109 typedef struct {
110 	Elf64_Word	vda_name;
111 	Elf64_Word	vda_next;
112 } Elf64_Verdaux;
113 
114 /* Version Dependency */
115 
116 typedef struct {
117 	Elf32_Half	vn_version;
118 	Elf32_Half	vn_cnt;
119 	Elf32_Word	vn_file;
120 	Elf32_Word	vn_aux;
121 	Elf32_Word	vn_next;
122 } Elf32_Verneed;
123 
124 typedef struct {
125 	Elf32_Word	vna_hash;
126 	Elf32_Half	vna_flags;
127 	Elf32_Half	vna_other;
128 	Elf32_Word	vna_name;
129 	Elf32_Word	vna_next;
130 } Elf32_Vernaux;
131 
132 typedef struct {
133         Elf64_Half	vn_version;
134         Elf64_Half	vn_cnt;
135         Elf64_Word	vn_file;
136         Elf64_Word	vn_aux;
137         Elf64_Word	vn_next;
138 } Elf64_Verneed;
139 
140 typedef struct {
141 	Elf64_Word	vna_hash;
142 	Elf64_Half	vna_flags;
143 	Elf64_Half	vna_other;
144 	Elf64_Word	vna_name;
145 	Elf64_Word	vna_next;
146 } Elf64_Vernaux;
147 
148 /* GNU-style hash sections */
149 typedef struct {
150 	uint32_t	gh_nbuckets;	/* Number of hash buckets. */
151 	uint32_t	gh_symndx;	/* First visible symbol in .dynsym. */
152 	uint32_t	gh_maskwords;	/* #maskwords used in bloom filter. */
153 	uint32_t	gh_shift2;	/* Bloom filter shift count. */
154 } Elf_GNU_Hash_Header;
155 
156 /* Library private data structures */
157 typedef struct _Elf Elf;
158 typedef struct _Elf_Scn Elf_Scn;
159 
160 /* File types */
161 typedef enum {
162 	ELF_K_NONE = 0,
163 	ELF_K_AR,	/* `ar' archives */
164 	ELF_K_COFF,	/* COFF files (unsupported) */
165 	ELF_K_ELF,	/* ELF files */
166 	ELF_K_NUM
167 } Elf_Kind;
168 
169 #define	ELF_K_FIRST	ELF_K_NONE
170 #define	ELF_K_LAST	ELF_K_NUM
171 
172 /* Data types */
173 typedef enum {
174 	ELF_T_ADDR,
175 	ELF_T_BYTE,
176 	ELF_T_CAP,
177 	ELF_T_DYN,
178 	ELF_T_EHDR,
179 	ELF_T_HALF,
180 	ELF_T_LWORD,
181 	ELF_T_MOVE,
182 	ELF_T_MOVEP,
183 	ELF_T_NOTE,
184 	ELF_T_OFF,
185 	ELF_T_PHDR,
186 	ELF_T_REL,
187 	ELF_T_RELA,
188 	ELF_T_SHDR,
189 	ELF_T_SWORD,
190 	ELF_T_SXWORD,
191 	ELF_T_SYMINFO,
192 	ELF_T_SYM,
193 	ELF_T_VDEF,
194 	ELF_T_VNEED,
195 	ELF_T_WORD,
196 	ELF_T_XWORD,
197 	ELF_T_GNUHASH,	/* GNU style hash tables. */
198 	ELF_T_NUM
199 } Elf_Type;
200 
201 #define	ELF_T_FIRST	ELF_T_ADDR
202 #define	ELF_T_LAST	ELF_T_GNUHASH
203 
204 /* Commands */
205 typedef enum {
206 	ELF_C_NULL = 0,
207 	ELF_C_CLR,
208 	ELF_C_FDDONE,
209 	ELF_C_FDREAD,
210 	ELF_C_RDWR,
211 	ELF_C_READ,
212 	ELF_C_SET,
213 	ELF_C_WRITE,
214 	ELF_C_NUM
215 } Elf_Cmd;
216 
217 #define	ELF_C_FIRST	ELF_C_NULL
218 #define	ELF_C_LAST	ELF_C_NUM
219 
220 /*
221  * An `Elf_Data' structure describes data in an
222  * ELF section.
223  */
224 typedef struct _Elf_Data {
225 	/*
226 	 * `Public' members that are part of the ELF(3) API.
227 	 */
228 	uint64_t	d_align;
229 	void		*d_buf;
230 	uint64_t	d_off;
231 	uint64_t	d_size;
232 	Elf_Type	d_type;
233 	unsigned int	d_version;
234 } Elf_Data;
235 
236 /*
237  * An `Elf_Arhdr' structure describes an archive
238  * header.
239  */
240 typedef struct {
241 	time_t		ar_date;
242 	char		*ar_name;	/* archive member name */
243 	gid_t		ar_gid;
244 	mode_t		ar_mode;
245 	char		*ar_rawname;	/* 'raw' member name */
246 	size_t		ar_size;
247 	uid_t		ar_uid;
248 
249 	/*
250 	 * Members that are not part of the public API.
251 	 */
252 	unsigned int	ar_flags;
253 } Elf_Arhdr;
254 
255 /*
256  * An `Elf_Arsym' describes an entry in the archive
257  * symbol table.
258  */
259 typedef struct {
260 	off_t		as_off;		/* byte offset to member's header */
261 	unsigned long	as_hash;	/* elf_hash() value for name */
262 	char		*as_name; 	/* null terminated symbol name */
263 } Elf_Arsym;
264 
265 /*
266  * Error numbers.
267  */
268 
269 enum Elf_Error {
270 	ELF_E_NONE,	/* No error */
271 	ELF_E_ARCHIVE,	/* Malformed ar(1) archive */
272 	ELF_E_ARGUMENT,	/* Invalid argument */
273 	ELF_E_CLASS,	/* Mismatched ELF class */
274 	ELF_E_DATA,	/* Invalid data descriptor */
275 	ELF_E_HEADER,	/* Missing or malformed ELF header */
276 	ELF_E_IO,	/* I/O error */
277 	ELF_E_LAYOUT,	/* Layout constraint violation */
278 	ELF_E_MODE,	/* Wrong mode for ELF descriptor */
279 	ELF_E_RANGE,	/* Value out of range */
280 	ELF_E_RESOURCE,	/* Resource exhaustion */
281 	ELF_E_SECTION,	/* Invalid section descriptor */
282 	ELF_E_SEQUENCE,	/* API calls out of sequence */
283 	ELF_E_UNIMPL,	/* Feature is unimplemented */
284 	ELF_E_VERSION,	/* Unknown API version */
285 	ELF_E_NUM	/* Max error number */
286 };
287 
288 /*
289  * Flags defined by the API.
290  */
291 
292 #define	ELF_F_LAYOUT	0x001U	/* application will layout the file */
293 #define	ELF_F_DIRTY	0x002U	/* a section or ELF file is dirty */
294 
295 /* ELF(3) API extensions. */
296 #define	ELF_F_ARCHIVE	   0x100U /* archive creation */
297 #define	ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */
298 
299 #ifdef __cplusplus
300 extern "C" {
301 #endif
302 Elf		*elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
303 int		elf_cntl(Elf *_elf, Elf_Cmd _cmd);
304 int		elf_end(Elf *_elf);
305 const char	*elf_errmsg(int _error);
306 int		elf_errno(void);
307 void		elf_fill(int _fill);
308 unsigned int	elf_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd,
309 			unsigned int _flags);
310 unsigned int	elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd,
311 			unsigned int _flags);
312 unsigned int	elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
313 unsigned int	elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
314 unsigned int	elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
315 unsigned int	elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
316 unsigned int	elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
317 Elf_Arhdr	*elf_getarhdr(Elf *_elf);
318 Elf_Arsym	*elf_getarsym(Elf *_elf, size_t *_ptr);
319 off_t		elf_getbase(Elf *_elf);
320 Elf_Data	*elf_getdata(Elf_Scn *, Elf_Data *);
321 char		*elf_getident(Elf *_elf, size_t *_ptr);
322 int		elf_getphdrnum(Elf *_elf, size_t *_dst);
323 int		elf_getphnum(Elf *_elf, size_t *_dst);	/* Deprecated */
324 Elf_Scn		*elf_getscn(Elf *_elf, size_t _index);
325 int		elf_getshdrnum(Elf *_elf, size_t *_dst);
326 int		elf_getshnum(Elf *_elf, size_t *_dst);	/* Deprecated */
327 int		elf_getshdrstrndx(Elf *_elf, size_t *_dst);
328 int		elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */
329 unsigned long	elf_hash(const char *_name);
330 Elf_Kind	elf_kind(Elf *_elf);
331 Elf		*elf_memory(char *_image, size_t _size);
332 size_t		elf_ndxscn(Elf_Scn *_scn);
333 Elf_Data	*elf_newdata(Elf_Scn *_scn);
334 Elf_Scn		*elf_newscn(Elf *_elf);
335 Elf_Scn		*elf_nextscn(Elf *_elf, Elf_Scn *_scn);
336 Elf_Cmd		elf_next(Elf *_elf);
337 Elf		*elf_open(int _fd);
338 Elf		*elf_openmemory(char *_image, size_t _size);
339 off_t		elf_rand(Elf *_elf, off_t _off);
340 Elf_Data	*elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
341 char		*elf_rawfile(Elf *_elf, size_t *_size);
342 int		elf_setshstrndx(Elf *_elf, size_t _shnum);
343 char		*elf_strptr(Elf *_elf, size_t _section, size_t _offset);
344 off_t		elf_update(Elf *_elf, Elf_Cmd _cmd);
345 unsigned int	elf_version(unsigned int _version);
346 
347 long		elf32_checksum(Elf *_elf);
348 size_t		elf32_fsize(Elf_Type _type, size_t _count,
349 			unsigned int _version);
350 Elf32_Ehdr	*elf32_getehdr(Elf *_elf);
351 Elf32_Phdr	*elf32_getphdr(Elf *_elf);
352 Elf32_Shdr	*elf32_getshdr(Elf_Scn *_scn);
353 Elf32_Ehdr	*elf32_newehdr(Elf *_elf);
354 Elf32_Phdr	*elf32_newphdr(Elf *_elf, size_t _count);
355 Elf_Data	*elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
356 			unsigned int _enc);
357 Elf_Data	*elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
358 			unsigned int _enc);
359 
360 long		elf64_checksum(Elf *_elf);
361 size_t		elf64_fsize(Elf_Type _type, size_t _count,
362 			unsigned int _version);
363 Elf64_Ehdr	*elf64_getehdr(Elf *_elf);
364 Elf64_Phdr	*elf64_getphdr(Elf *_elf);
365 Elf64_Shdr	*elf64_getshdr(Elf_Scn *_scn);
366 Elf64_Ehdr	*elf64_newehdr(Elf *_elf);
367 Elf64_Phdr	*elf64_newphdr(Elf *_elf, size_t _count);
368 Elf_Data	*elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
369 			unsigned int _enc);
370 Elf_Data	*elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
371 			unsigned int _enc);
372 #ifdef __cplusplus
373 }
374 #endif
375 
376 #endif	/* _LIBELF_H_ */
377