libelf.h revision 164190
1147020Ssam/*-
2147020Ssam * Copyright (c) 2006 Joseph Koshy
3147020Ssam * 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 * $FreeBSD: head/lib/libelf/libelf.h 164190 2006-11-11 17:16:35Z jkoshy $
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#include <sys/queue.h>
36
37/* Library private data structures */
38typedef struct _Elf Elf;
39typedef struct _Elf_Scn Elf_Scn;
40
41/* File types */
42typedef enum {
43	ELF_K_NONE = 0,
44	ELF_K_AR,	/* `ar' archives */
45	ELF_K_COFF,	/* COFF files (unsupported) */
46	ELF_K_ELF,	/* ELF files */
47	ELF_K_NUM
48} Elf_Kind;
49
50#define	ELF_K_FIRST	ELF_K_NONE
51#define	ELF_K_LAST	ELF_K_NUM
52
53/* Data types */
54typedef enum {
55	ELF_T_ADDR,
56	ELF_T_BYTE,
57	ELF_T_CAP,
58	ELF_T_DYN,
59	ELF_T_EHDR,
60	ELF_T_HALF,
61	ELF_T_LWORD,
62	ELF_T_MOVE,
63	ELF_T_MOVEP,
64	ELF_T_NOTE,
65	ELF_T_OFF,
66	ELF_T_PHDR,
67	ELF_T_REL,
68	ELF_T_RELA,
69	ELF_T_SHDR,
70	ELF_T_SWORD,
71	ELF_T_SXWORD,
72	ELF_T_SYMINFO,
73	ELF_T_SYM,
74	ELF_T_VDEF,
75	ELF_T_VNEED,
76	ELF_T_WORD,
77	ELF_T_XWORD,
78	ELF_T_NUM
79} Elf_Type;
80
81#define	ELF_T_FIRST	ELF_T_ADDR
82#define	ELF_T_LAST	ELF_T_XWORD
83
84/* Commands */
85typedef 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 */
104typedef 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
115	/*
116	 * Members that are not part of the public API.
117	 */
118	Elf_Scn		*d_scn;		/* containing section */
119	unsigned int	d_flags;
120	STAILQ_ENTRY(_Elf_Data)	d_next;
121} Elf_Data;
122
123/*
124 * An `Elf_Arhdr' structure describes an archive
125 * header.
126 */
127typedef struct {
128	time_t		ar_date;
129	char		*ar_name;	/* archive member name */
130	gid_t		ar_gid;
131	mode_t		ar_mode;
132	char		*ar_rawname;	/* 'raw' member name */
133	size_t		ar_size;
134	uid_t		ar_uid;
135} Elf_Arhdr;
136
137/*
138 * An `Elf_Arsym' describes an entry in the archive
139 * symbol table.
140 */
141typedef struct {
142	off_t		as_off;		/* byte offset to member's header */
143	unsigned long	as_hash;	/* elf_hash() value for name */
144	char		*as_name; 	/* null terminated symbol name */
145} Elf_Arsym;
146
147/*
148 * Error numbers.
149 */
150
151enum Elf_Error {
152	ELF_E_NONE,	/* No error */
153	ELF_E_ARCHIVE,	/* Malformed ar(1) archive */
154	ELF_E_ARGUMENT,	/* Invalid argument */
155	ELF_E_CLASS,	/* Mismatched ELF class */
156	ELF_E_DATA,	/* Invalid data descriptor */
157	ELF_E_HEADER,	/* Missing or malformed ELF header */
158	ELF_E_IO,	/* I/O error */
159	ELF_E_LAYOUT,	/* Layout constraint violation */
160	ELF_E_MODE,	/* Wrong mode for ELF descriptor */
161	ELF_E_RANGE,	/* Value out of range */
162	ELF_E_RESOURCE,	/* Resource exhaustion */
163	ELF_E_SECTION,	/* Invalid section descriptor */
164	ELF_E_SEQUENCE,	/* API calls out of sequence */
165	ELF_E_UNIMPL,	/* Feature is unimplemented */
166	ELF_E_VERSION,	/* Unknown API version */
167	ELF_E_NUM	/* Max error number */
168};
169
170/*
171 * Flags defined by the API.
172 */
173
174#define	ELF_F_LAYOUT	0x001U	/* application will layout the file */
175#define	ELF_F_DIRTY	0x002U	/* a section or ELF file is dirty */
176
177__BEGIN_DECLS
178Elf		*elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
179int		elf_cntl(Elf *_elf, Elf_Cmd _cmd);
180int		elf_end(Elf *_elf);
181const char	*elf_errmsg(int _error);
182int		elf_errno(void);
183void		elf_fill(int _fill);
184unsigned int	elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd, unsigned int _flags);
185unsigned int	elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
186unsigned int	elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
187unsigned int	elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
188unsigned int	elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
189unsigned int	elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
190Elf_Arhdr	*elf_getarhdr(Elf *_elf);
191Elf_Arsym	*elf_getarsym(Elf *_elf, size_t *_ptr);
192off_t		elf_getbase(Elf *_elf);
193Elf_Data	*elf_getdata(Elf_Scn *, Elf_Data *);
194char		*elf_getident(Elf *_elf, size_t *_ptr);
195int		elf_getphnum(Elf *_elf, size_t *_dst);
196Elf_Scn		*elf_getscn(Elf *_elf, size_t _index);
197int		elf_getshnum(Elf *_elf, size_t *_dst);
198int		elf_getshstrndx(Elf *_elf, size_t *_dst);
199unsigned long	elf_hash(const char *_name);
200Elf_Kind	elf_kind(Elf *_elf);
201Elf		*elf_memory(char *_image, size_t _size);
202size_t		elf_ndxscn(Elf_Scn *_scn);
203Elf_Data	*elf_newdata(Elf_Scn *_scn);
204Elf_Scn		*elf_newscn(Elf *_elf);
205Elf_Scn		*elf_nextscn(Elf *_elf, Elf_Scn *_scn);
206Elf_Cmd		elf_next(Elf *_elf);
207off_t		elf_rand(Elf *_elf, off_t _off);
208Elf_Data	*elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
209char		*elf_rawfile(Elf *_elf, size_t *_size);
210int		elf_setshstrndx(Elf *_elf, size_t _shnum);
211char		*elf_strptr(Elf *_elf, size_t _section, size_t _offset);
212off_t		elf_update(Elf *_elf, Elf_Cmd _cmd);
213unsigned int	elf_version(unsigned int _version);
214
215long		elf32_checksum(Elf *_elf);
216size_t		elf32_fsize(Elf_Type _type, size_t _count,
217			unsigned int _version);
218Elf32_Ehdr	*elf32_getehdr(Elf *_elf);
219Elf32_Phdr	*elf32_getphdr(Elf *_elf);
220Elf32_Shdr	*elf32_getshdr(Elf_Scn *_scn);
221Elf32_Ehdr	*elf32_newehdr(Elf *_elf);
222Elf32_Phdr	*elf32_newphdr(Elf *_elf, size_t _count);
223Elf_Data	*elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
224			unsigned int _enc);
225Elf_Data	*elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
226			unsigned int _enc);
227
228long		elf64_checksum(Elf *_elf);
229size_t		elf64_fsize(Elf_Type _type, size_t _count,
230			unsigned int _version);
231Elf64_Ehdr	*elf64_getehdr(Elf *_elf);
232Elf64_Phdr	*elf64_getphdr(Elf *_elf);
233Elf64_Shdr	*elf64_getshdr(Elf_Scn *_scn);
234Elf64_Ehdr	*elf64_newehdr(Elf *_elf);
235Elf64_Phdr	*elf64_newphdr(Elf *_elf, size_t _count);
236Elf_Data	*elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
237			unsigned int _enc);
238Elf_Data	*elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
239			unsigned int _enc);
240
241#if	defined(LIBELF_TEST_HOOKS)
242int		_libelf_get_elf_class(Elf *_elf);
243int		_libelf_get_max_error(void);
244const char	*_libelf_get_no_error_message(void);
245const char	*_libelf_get_unknown_error_message(void);
246void		_libelf_set_elf_class(Elf *_elf, int _class);
247void		_libelf_set_error(int _error);
248#endif	/* LIBELF_TEST_HOOKS */
249__END_DECLS
250
251#endif	/* _LIBELF_H_ */
252