xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: libelf.h,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006,2008-2010 Joseph Koshy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Id: libelf.h 2366 2011-12-29 06:12:14Z jkoshy
29  */
30 
31 #ifndef	_LIBELF_H_
32 #define	_LIBELF_H_
33 
34 #if HAVE_NBTOOL_CONFIG_H
35 # include "nbtool_config.h"
36 #endif
37 
38 #include <sys/types.h>
39 #ifdef __NetBSD__
40 #include <sys/exec_elf.h>
41 #else
42 #include <sys/elf32.h>
43 #include <sys/elf64.h>
44 #endif
45 
46 /* Library private data structures */
47 typedef struct _Elf Elf;
48 typedef struct _Elf_Scn Elf_Scn;
49 
50 /* File types */
51 typedef enum {
52 	ELF_K_NONE = 0,
53 	ELF_K_AR,	/* `ar' archives */
54 	ELF_K_COFF,	/* COFF files (unsupported) */
55 	ELF_K_ELF,	/* ELF files */
56 	ELF_K_NUM
57 } Elf_Kind;
58 
59 #define	ELF_K_FIRST	ELF_K_NONE
60 #define	ELF_K_LAST	ELF_K_NUM
61 
62 /* Data types */
63 typedef enum {
64 	ELF_T_ADDR,
65 	ELF_T_BYTE,
66 	ELF_T_CAP,
67 	ELF_T_DYN,
68 	ELF_T_EHDR,
69 	ELF_T_HALF,
70 	ELF_T_LWORD,
71 	ELF_T_MOVE,
72 	ELF_T_MOVEP,
73 	ELF_T_NOTE,
74 	ELF_T_OFF,
75 	ELF_T_PHDR,
76 	ELF_T_REL,
77 	ELF_T_RELA,
78 	ELF_T_SHDR,
79 	ELF_T_SWORD,
80 	ELF_T_SXWORD,
81 	ELF_T_SYMINFO,
82 	ELF_T_SYM,
83 	ELF_T_VDEF,
84 	ELF_T_VNEED,
85 	ELF_T_WORD,
86 	ELF_T_XWORD,
87 	ELF_T_GNUHASH,	/* GNU style hash tables. */
88 	ELF_T_NUM
89 } Elf_Type;
90 
91 #define	ELF_T_FIRST	ELF_T_ADDR
92 #define	ELF_T_LAST	ELF_T_GNUHASH
93 
94 /* Commands */
95 typedef enum {
96 	ELF_C_NULL = 0,
97 	ELF_C_CLR,
98 	ELF_C_FDDONE,
99 	ELF_C_FDREAD,
100 	ELF_C_RDWR,
101 	ELF_C_READ,
102 	ELF_C_SET,
103 	ELF_C_WRITE,
104 	ELF_C_NUM
105 } Elf_Cmd;
106 
107 #define	ELF_C_FIRST	ELF_C_NULL
108 #define	ELF_C_LAST	ELF_C_NUM
109 
110 /*
111  * An `Elf_Data' structure describes data in an
112  * ELF section.
113  */
114 typedef struct _Elf_Data {
115 	/*
116 	 * `Public' members that are part of the ELF(3) API.
117 	 */
118 	uint64_t	d_align;
119 	void		*d_buf;
120 	uint64_t	d_off;
121 	uint64_t	d_size;
122 	Elf_Type	d_type;
123 	unsigned int	d_version;
124 } Elf_Data;
125 
126 /*
127  * An `Elf_Arhdr' structure describes an archive
128  * header.
129  */
130 typedef struct {
131 	time_t		ar_date;
132 	char		*ar_name;	/* archive member name */
133 	gid_t		ar_gid;
134 	mode_t		ar_mode;
135 	char		*ar_rawname;	/* 'raw' member name */
136 	size_t		ar_size;
137 	uid_t		ar_uid;
138 
139 	/*
140 	 * Members that are not part of the public API.
141 	 */
142 	int		ar_flags;
143 } Elf_Arhdr;
144 
145 /*
146  * An `Elf_Arsym' describes an entry in the archive
147  * symbol table.
148  */
149 typedef struct {
150 	off_t		as_off;		/* byte offset to member's header */
151 	unsigned long	as_hash;	/* elf_hash() value for name */
152 	char		*as_name; 	/* null terminated symbol name */
153 } Elf_Arsym;
154 
155 /*
156  * Error numbers.
157  */
158 
159 enum Elf_Error {
160 	ELF_E_NONE,	/* No error */
161 	ELF_E_ARCHIVE,	/* Malformed ar(1) archive */
162 	ELF_E_ARGUMENT,	/* Invalid argument */
163 	ELF_E_CLASS,	/* Mismatched ELF class */
164 	ELF_E_DATA,	/* Invalid data descriptor */
165 	ELF_E_HEADER,	/* Missing or malformed ELF header */
166 	ELF_E_IO,	/* I/O error */
167 	ELF_E_LAYOUT,	/* Layout constraint violation */
168 	ELF_E_MODE,	/* Wrong mode for ELF descriptor */
169 	ELF_E_RANGE,	/* Value out of range */
170 	ELF_E_RESOURCE,	/* Resource exhaustion */
171 	ELF_E_SECTION,	/* Invalid section descriptor */
172 	ELF_E_SEQUENCE,	/* API calls out of sequence */
173 	ELF_E_UNIMPL,	/* Feature is unimplemented */
174 	ELF_E_VERSION,	/* Unknown API version */
175 	ELF_E_NUM	/* Max error number */
176 };
177 
178 /*
179  * Flags defined by the API.
180  */
181 
182 #define	ELF_F_LAYOUT	0x001U	/* application will layout the file */
183 #define	ELF_F_DIRTY	0x002U	/* a section or ELF file is dirty */
184 
185 /* ELF(3) API extensions. */
186 #define	ELF_F_ARCHIVE	   0x100U /* archive creation */
187 #define	ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */
188 
189 __BEGIN_DECLS
190 Elf		*elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
191 int		elf_cntl(Elf *_elf, Elf_Cmd _cmd);
192 int		elf_end(Elf *_elf);
193 const char	*elf_errmsg(int _error);
194 int		elf_errno(void);
195 void		elf_fill(int _fill);
196 unsigned int	elf_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd,
197 			unsigned int _flags);
198 unsigned int	elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd,
199 			unsigned int _flags);
200 unsigned int	elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
201 unsigned int	elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
202 unsigned int	elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
203 unsigned int	elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
204 unsigned int	elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
205 Elf_Arhdr	*elf_getarhdr(Elf *_elf);
206 Elf_Arsym	*elf_getarsym(Elf *_elf, size_t *_ptr);
207 off_t		elf_getbase(Elf *_elf);
208 Elf_Data	*elf_getdata(Elf_Scn *, Elf_Data *);
209 char		*elf_getident(Elf *_elf, size_t *_ptr);
210 int		elf_getphdrnum(Elf *_elf, size_t *_dst);
211 int		elf_getphnum(Elf *_elf, size_t *_dst);	/* Deprecated */
212 Elf_Scn		*elf_getscn(Elf *_elf, size_t _index);
213 int		elf_getshdrnum(Elf *_elf, size_t *_dst);
214 int		elf_getshnum(Elf *_elf, size_t *_dst);	/* Deprecated */
215 int		elf_getshdrstrndx(Elf *_elf, size_t *_dst);
216 int		elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */
217 unsigned long	elf_hash(const void *_name);
218 Elf_Kind	elf_kind(Elf *_elf);
219 Elf		*elf_memory(char *_image, size_t _size);
220 size_t		elf_ndxscn(Elf_Scn *_scn);
221 Elf_Data	*elf_newdata(Elf_Scn *_scn);
222 Elf_Scn		*elf_newscn(Elf *_elf);
223 Elf_Scn		*elf_nextscn(Elf *_elf, Elf_Scn *_scn);
224 Elf_Cmd		elf_next(Elf *_elf);
225 Elf		*elf_open(int _fd);
226 Elf		*elf_openmemory(char *_image, size_t _size);
227 off_t		elf_rand(Elf *_elf, off_t _off);
228 Elf_Data	*elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
229 char		*elf_rawfile(Elf *_elf, size_t *_size);
230 int		elf_setshstrndx(Elf *_elf, size_t _shnum);
231 char		*elf_strptr(Elf *_elf, size_t _section, size_t _offset);
232 off_t		elf_update(Elf *_elf, Elf_Cmd _cmd);
233 unsigned int	elf_version(unsigned int _version);
234 
235 long		elf32_checksum(Elf *_elf);
236 size_t		elf32_fsize(Elf_Type _type, size_t _count,
237 			unsigned int _version);
238 Elf32_Ehdr	*elf32_getehdr(Elf *_elf);
239 Elf32_Phdr	*elf32_getphdr(Elf *_elf);
240 Elf32_Shdr	*elf32_getshdr(Elf_Scn *_scn);
241 Elf32_Ehdr	*elf32_newehdr(Elf *_elf);
242 Elf32_Phdr	*elf32_newphdr(Elf *_elf, size_t _count);
243 Elf_Data	*elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
244 			unsigned int _enc);
245 Elf_Data	*elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
246 			unsigned int _enc);
247 
248 long		elf64_checksum(Elf *_elf);
249 size_t		elf64_fsize(Elf_Type _type, size_t _count,
250 			unsigned int _version);
251 Elf64_Ehdr	*elf64_getehdr(Elf *_elf);
252 Elf64_Phdr	*elf64_getphdr(Elf *_elf);
253 Elf64_Shdr	*elf64_getshdr(Elf_Scn *_scn);
254 Elf64_Ehdr	*elf64_newehdr(Elf *_elf);
255 Elf64_Phdr	*elf64_newphdr(Elf *_elf, size_t _count);
256 Elf_Data	*elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
257 			unsigned int _enc);
258 Elf_Data	*elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
259 			unsigned int _enc);
260 __END_DECLS
261 
262 #endif	/* _LIBELF_H_ */
263