xref: /freebsd/contrib/elftoolchain/libelf/libelf.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 3174 2015-03-27 17:13:41Z emaste $
27  */
28 
29 #ifndef	_LIBELF_H_
30 #define	_LIBELF_H_
31 
32 #include <sys/types.h>
33 #include <sys/elf32.h>
34 #include <sys/elf64.h>
35 
36 /* Library private data structures */
37 typedef struct _Elf Elf;
38 typedef struct _Elf_Scn Elf_Scn;
39 
40 /* File types */
41 typedef enum {
42 	ELF_K_NONE = 0,
43 	ELF_K_AR,	/* `ar' archives */
44 	ELF_K_COFF,	/* COFF files (unsupported) */
45 	ELF_K_ELF,	/* ELF files */
46 	ELF_K_NUM
47 } Elf_Kind;
48 
49 #define	ELF_K_FIRST	ELF_K_NONE
50 #define	ELF_K_LAST	ELF_K_NUM
51 
52 /* Data types */
53 typedef enum {
54 	ELF_T_ADDR,
55 	ELF_T_BYTE,
56 	ELF_T_CAP,
57 	ELF_T_DYN,
58 	ELF_T_EHDR,
59 	ELF_T_HALF,
60 	ELF_T_LWORD,
61 	ELF_T_MOVE,
62 	ELF_T_MOVEP,
63 	ELF_T_NOTE,
64 	ELF_T_OFF,
65 	ELF_T_PHDR,
66 	ELF_T_REL,
67 	ELF_T_RELA,
68 	ELF_T_SHDR,
69 	ELF_T_SWORD,
70 	ELF_T_SXWORD,
71 	ELF_T_SYMINFO,
72 	ELF_T_SYM,
73 	ELF_T_VDEF,
74 	ELF_T_VNEED,
75 	ELF_T_WORD,
76 	ELF_T_XWORD,
77 	ELF_T_GNUHASH,	/* GNU style hash tables. */
78 	ELF_T_NUM
79 } Elf_Type;
80 
81 #define	ELF_T_FIRST	ELF_T_ADDR
82 #define	ELF_T_LAST	ELF_T_GNUHASH
83 
84 /* Commands */
85 typedef enum {
86 	ELF_C_NULL = 0,
87 	ELF_C_CLR,
88 	ELF_C_FDDONE,
89 	ELF_C_FDREAD,
90 	ELF_C_RDWR,
91 	ELF_C_READ,
92 	ELF_C_SET,
93 	ELF_C_WRITE,
94 	ELF_C_NUM
95 } Elf_Cmd;
96 
97 #define	ELF_C_FIRST	ELF_C_NULL
98 #define	ELF_C_LAST	ELF_C_NUM
99 
100 /*
101  * An `Elf_Data' structure describes data in an
102  * ELF section.
103  */
104 typedef struct _Elf_Data {
105 	/*
106 	 * `Public' members that are part of the ELF(3) API.
107 	 */
108 	uint64_t	d_align;
109 	void		*d_buf;
110 	uint64_t	d_off;
111 	uint64_t	d_size;
112 	Elf_Type	d_type;
113 	unsigned int	d_version;
114 } Elf_Data;
115 
116 /*
117  * An `Elf_Arhdr' structure describes an archive
118  * header.
119  */
120 typedef struct {
121 	time_t		ar_date;
122 	char		*ar_name;	/* archive member name */
123 	gid_t		ar_gid;
124 	mode_t		ar_mode;
125 	char		*ar_rawname;	/* 'raw' member name */
126 	size_t		ar_size;
127 	uid_t		ar_uid;
128 
129 	/*
130 	 * Members that are not part of the public API.
131 	 */
132 	unsigned int	ar_flags;
133 } Elf_Arhdr;
134 
135 /*
136  * An `Elf_Arsym' describes an entry in the archive
137  * symbol table.
138  */
139 typedef struct {
140 	off_t		as_off;		/* byte offset to member's header */
141 	unsigned long	as_hash;	/* elf_hash() value for name */
142 	char		*as_name; 	/* null terminated symbol name */
143 } Elf_Arsym;
144 
145 /*
146  * Error numbers.
147  */
148 
149 enum Elf_Error {
150 	ELF_E_NONE,	/* No error */
151 	ELF_E_ARCHIVE,	/* Malformed ar(1) archive */
152 	ELF_E_ARGUMENT,	/* Invalid argument */
153 	ELF_E_CLASS,	/* Mismatched ELF class */
154 	ELF_E_DATA,	/* Invalid data descriptor */
155 	ELF_E_HEADER,	/* Missing or malformed ELF header */
156 	ELF_E_IO,	/* I/O error */
157 	ELF_E_LAYOUT,	/* Layout constraint violation */
158 	ELF_E_MODE,	/* Wrong mode for ELF descriptor */
159 	ELF_E_RANGE,	/* Value out of range */
160 	ELF_E_RESOURCE,	/* Resource exhaustion */
161 	ELF_E_SECTION,	/* Invalid section descriptor */
162 	ELF_E_SEQUENCE,	/* API calls out of sequence */
163 	ELF_E_UNIMPL,	/* Feature is unimplemented */
164 	ELF_E_VERSION,	/* Unknown API version */
165 	ELF_E_INVALID_SECTION_FLAGS, /* Invalid ELF section header flags */
166 	ELF_E_INVALID_SECTION_TYPE, /* Invalid ELF section header type */
167 	ELF_E_NOT_COMPRESSED, /* Section is not compressed */
168 	ELF_E_NUM	/* Max error number */
169 };
170 
171 /*
172  * Flags defined by the API.
173  */
174 
175 #define	ELF_F_LAYOUT	0x001U	/* application will layout the file */
176 #define	ELF_F_DIRTY	0x002U	/* a section or ELF file is dirty */
177 
178 /* ELF(3) API extensions. */
179 #define	ELF_F_ARCHIVE	   0x100U /* archive creation */
180 #define	ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */
181 
182 #ifdef __cplusplus
183 extern "C" {
184 #endif
185 Elf		*elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
186 int		elf_cntl(Elf *_elf, Elf_Cmd _cmd);
187 int		elf_end(Elf *_elf);
188 const char	*elf_errmsg(int _error);
189 int		elf_errno(void);
190 void		elf_fill(int _fill);
191 unsigned int	elf_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd,
192 			unsigned int _flags);
193 unsigned int	elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd,
194 			unsigned int _flags);
195 unsigned int	elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
196 unsigned int	elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
197 unsigned int	elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
198 unsigned int	elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
199 unsigned int	elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
200 Elf_Arhdr	*elf_getarhdr(Elf *_elf);
201 Elf_Arsym	*elf_getarsym(Elf *_elf, size_t *_ptr);
202 off_t		elf_getbase(Elf *_elf);
203 Elf_Data	*elf_getdata(Elf_Scn *, Elf_Data *);
204 char		*elf_getident(Elf *_elf, size_t *_ptr);
205 int		elf_getphdrnum(Elf *_elf, size_t *_dst);
206 int		elf_getphnum(Elf *_elf, size_t *_dst);	/* Deprecated */
207 Elf_Scn		*elf_getscn(Elf *_elf, size_t _index);
208 int		elf_getshdrnum(Elf *_elf, size_t *_dst);
209 int		elf_getshnum(Elf *_elf, size_t *_dst);	/* Deprecated */
210 int		elf_getshdrstrndx(Elf *_elf, size_t *_dst);
211 int		elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */
212 unsigned long	elf_hash(const char *_name);
213 Elf_Kind	elf_kind(Elf *_elf);
214 Elf		*elf_memory(char *_image, size_t _size);
215 size_t		elf_ndxscn(Elf_Scn *_scn);
216 Elf_Data	*elf_newdata(Elf_Scn *_scn);
217 Elf_Scn		*elf_newscn(Elf *_elf);
218 Elf_Scn		*elf_nextscn(Elf *_elf, Elf_Scn *_scn);
219 Elf_Cmd		elf_next(Elf *_elf);
220 Elf		*elf_open(int _fd);
221 Elf		*elf_openmemory(char *_image, size_t _size);
222 off_t		elf_rand(Elf *_elf, off_t _off);
223 Elf_Data	*elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
224 char		*elf_rawfile(Elf *_elf, size_t *_size);
225 int		elf_setshstrndx(Elf *_elf, size_t _shnum);
226 char		*elf_strptr(Elf *_elf, size_t _section, size_t _offset);
227 off_t		elf_update(Elf *_elf, Elf_Cmd _cmd);
228 unsigned int	elf_version(unsigned int _version);
229 
230 long		elf32_checksum(Elf *_elf);
231 size_t		elf32_fsize(Elf_Type _type, size_t _count,
232 			unsigned int _version);
233 Elf32_Chdr	*elf32_getchdr(Elf_Scn *_scn);
234 Elf32_Ehdr	*elf32_getehdr(Elf *_elf);
235 Elf32_Phdr	*elf32_getphdr(Elf *_elf);
236 Elf32_Shdr	*elf32_getshdr(Elf_Scn *_scn);
237 Elf32_Ehdr	*elf32_newehdr(Elf *_elf);
238 Elf32_Phdr	*elf32_newphdr(Elf *_elf, size_t _count);
239 Elf_Data	*elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
240 			unsigned int _enc);
241 Elf_Data	*elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
242 			unsigned int _enc);
243 
244 long		elf64_checksum(Elf *_elf);
245 size_t		elf64_fsize(Elf_Type _type, size_t _count,
246 			unsigned int _version);
247 Elf64_Chdr	*elf64_getchdr(Elf_Scn *_scn);
248 Elf64_Ehdr	*elf64_getehdr(Elf *_elf);
249 Elf64_Phdr	*elf64_getphdr(Elf *_elf);
250 Elf64_Shdr	*elf64_getshdr(Elf_Scn *_scn);
251 Elf64_Ehdr	*elf64_newehdr(Elf *_elf);
252 Elf64_Phdr	*elf64_newphdr(Elf *_elf, size_t _count);
253 Elf_Data	*elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
254 			unsigned int _enc);
255 Elf_Data	*elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
256 			unsigned int _enc);
257 #ifdef __cplusplus
258 }
259 #endif
260 
261 #endif	/* _LIBELF_H_ */
262