libelf.h revision 164190
1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy *
26164190Sjkoshy * $FreeBSD: head/lib/libelf/libelf.h 164190 2006-11-11 17:16:35Z jkoshy $
27164190Sjkoshy */
28164190Sjkoshy
29164190Sjkoshy#ifndef	_LIBELF_H_
30164190Sjkoshy#define	_LIBELF_H_
31164190Sjkoshy
32164190Sjkoshy#include <sys/types.h>
33164190Sjkoshy#include <sys/elf32.h>
34164190Sjkoshy#include <sys/elf64.h>
35164190Sjkoshy#include <sys/queue.h>
36164190Sjkoshy
37164190Sjkoshy/* Library private data structures */
38164190Sjkoshytypedef struct _Elf Elf;
39164190Sjkoshytypedef struct _Elf_Scn Elf_Scn;
40164190Sjkoshy
41164190Sjkoshy/* File types */
42164190Sjkoshytypedef enum {
43164190Sjkoshy	ELF_K_NONE = 0,
44164190Sjkoshy	ELF_K_AR,	/* `ar' archives */
45164190Sjkoshy	ELF_K_COFF,	/* COFF files (unsupported) */
46164190Sjkoshy	ELF_K_ELF,	/* ELF files */
47164190Sjkoshy	ELF_K_NUM
48164190Sjkoshy} Elf_Kind;
49164190Sjkoshy
50164190Sjkoshy#define	ELF_K_FIRST	ELF_K_NONE
51164190Sjkoshy#define	ELF_K_LAST	ELF_K_NUM
52164190Sjkoshy
53164190Sjkoshy/* Data types */
54164190Sjkoshytypedef enum {
55164190Sjkoshy	ELF_T_ADDR,
56164190Sjkoshy	ELF_T_BYTE,
57164190Sjkoshy	ELF_T_CAP,
58164190Sjkoshy	ELF_T_DYN,
59164190Sjkoshy	ELF_T_EHDR,
60164190Sjkoshy	ELF_T_HALF,
61164190Sjkoshy	ELF_T_LWORD,
62164190Sjkoshy	ELF_T_MOVE,
63164190Sjkoshy	ELF_T_MOVEP,
64164190Sjkoshy	ELF_T_NOTE,
65164190Sjkoshy	ELF_T_OFF,
66164190Sjkoshy	ELF_T_PHDR,
67164190Sjkoshy	ELF_T_REL,
68164190Sjkoshy	ELF_T_RELA,
69164190Sjkoshy	ELF_T_SHDR,
70164190Sjkoshy	ELF_T_SWORD,
71164190Sjkoshy	ELF_T_SXWORD,
72164190Sjkoshy	ELF_T_SYMINFO,
73164190Sjkoshy	ELF_T_SYM,
74164190Sjkoshy	ELF_T_VDEF,
75164190Sjkoshy	ELF_T_VNEED,
76164190Sjkoshy	ELF_T_WORD,
77164190Sjkoshy	ELF_T_XWORD,
78164190Sjkoshy	ELF_T_NUM
79164190Sjkoshy} Elf_Type;
80164190Sjkoshy
81164190Sjkoshy#define	ELF_T_FIRST	ELF_T_ADDR
82164190Sjkoshy#define	ELF_T_LAST	ELF_T_XWORD
83164190Sjkoshy
84164190Sjkoshy/* Commands */
85164190Sjkoshytypedef enum {
86164190Sjkoshy	ELF_C_NULL = 0,
87164190Sjkoshy	ELF_C_CLR,
88164190Sjkoshy	ELF_C_FDDONE,
89164190Sjkoshy	ELF_C_FDREAD,
90164190Sjkoshy	ELF_C_RDWR,
91164190Sjkoshy	ELF_C_READ,
92164190Sjkoshy	ELF_C_SET,
93164190Sjkoshy	ELF_C_WRITE,
94164190Sjkoshy	ELF_C_NUM
95164190Sjkoshy} Elf_Cmd;
96164190Sjkoshy
97164190Sjkoshy#define	ELF_C_FIRST	ELF_C_NULL
98164190Sjkoshy#define	ELF_C_LAST	ELF_C_NUM
99164190Sjkoshy
100164190Sjkoshy/*
101164190Sjkoshy * An `Elf_Data' structure describes data in an
102164190Sjkoshy * ELF section.
103164190Sjkoshy */
104164190Sjkoshytypedef struct _Elf_Data {
105164190Sjkoshy	/*
106164190Sjkoshy	 * `Public' members that are part of the ELF(3) API.
107164190Sjkoshy	 */
108164190Sjkoshy	uint64_t	d_align;
109164190Sjkoshy	void		*d_buf;
110164190Sjkoshy	uint64_t	d_off;
111164190Sjkoshy	uint64_t	d_size;
112164190Sjkoshy	Elf_Type	d_type;
113164190Sjkoshy	unsigned int	d_version;
114164190Sjkoshy
115164190Sjkoshy	/*
116164190Sjkoshy	 * Members that are not part of the public API.
117164190Sjkoshy	 */
118164190Sjkoshy	Elf_Scn		*d_scn;		/* containing section */
119164190Sjkoshy	unsigned int	d_flags;
120164190Sjkoshy	STAILQ_ENTRY(_Elf_Data)	d_next;
121164190Sjkoshy} Elf_Data;
122164190Sjkoshy
123164190Sjkoshy/*
124164190Sjkoshy * An `Elf_Arhdr' structure describes an archive
125164190Sjkoshy * header.
126164190Sjkoshy */
127164190Sjkoshytypedef struct {
128164190Sjkoshy	time_t		ar_date;
129164190Sjkoshy	char		*ar_name;	/* archive member name */
130164190Sjkoshy	gid_t		ar_gid;
131164190Sjkoshy	mode_t		ar_mode;
132164190Sjkoshy	char		*ar_rawname;	/* 'raw' member name */
133164190Sjkoshy	size_t		ar_size;
134164190Sjkoshy	uid_t		ar_uid;
135164190Sjkoshy} Elf_Arhdr;
136164190Sjkoshy
137164190Sjkoshy/*
138164190Sjkoshy * An `Elf_Arsym' describes an entry in the archive
139164190Sjkoshy * symbol table.
140164190Sjkoshy */
141164190Sjkoshytypedef struct {
142164190Sjkoshy	off_t		as_off;		/* byte offset to member's header */
143164190Sjkoshy	unsigned long	as_hash;	/* elf_hash() value for name */
144164190Sjkoshy	char		*as_name; 	/* null terminated symbol name */
145164190Sjkoshy} Elf_Arsym;
146164190Sjkoshy
147164190Sjkoshy/*
148164190Sjkoshy * Error numbers.
149164190Sjkoshy */
150164190Sjkoshy
151164190Sjkoshyenum Elf_Error {
152164190Sjkoshy	ELF_E_NONE,	/* No error */
153164190Sjkoshy	ELF_E_ARCHIVE,	/* Malformed ar(1) archive */
154164190Sjkoshy	ELF_E_ARGUMENT,	/* Invalid argument */
155164190Sjkoshy	ELF_E_CLASS,	/* Mismatched ELF class */
156164190Sjkoshy	ELF_E_DATA,	/* Invalid data descriptor */
157164190Sjkoshy	ELF_E_HEADER,	/* Missing or malformed ELF header */
158164190Sjkoshy	ELF_E_IO,	/* I/O error */
159164190Sjkoshy	ELF_E_LAYOUT,	/* Layout constraint violation */
160164190Sjkoshy	ELF_E_MODE,	/* Wrong mode for ELF descriptor */
161164190Sjkoshy	ELF_E_RANGE,	/* Value out of range */
162164190Sjkoshy	ELF_E_RESOURCE,	/* Resource exhaustion */
163164190Sjkoshy	ELF_E_SECTION,	/* Invalid section descriptor */
164164190Sjkoshy	ELF_E_SEQUENCE,	/* API calls out of sequence */
165164190Sjkoshy	ELF_E_UNIMPL,	/* Feature is unimplemented */
166164190Sjkoshy	ELF_E_VERSION,	/* Unknown API version */
167164190Sjkoshy	ELF_E_NUM	/* Max error number */
168164190Sjkoshy};
169164190Sjkoshy
170164190Sjkoshy/*
171164190Sjkoshy * Flags defined by the API.
172164190Sjkoshy */
173164190Sjkoshy
174164190Sjkoshy#define	ELF_F_LAYOUT	0x001U	/* application will layout the file */
175164190Sjkoshy#define	ELF_F_DIRTY	0x002U	/* a section or ELF file is dirty */
176164190Sjkoshy
177164190Sjkoshy__BEGIN_DECLS
178164190SjkoshyElf		*elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
179164190Sjkoshyint		elf_cntl(Elf *_elf, Elf_Cmd _cmd);
180164190Sjkoshyint		elf_end(Elf *_elf);
181164190Sjkoshyconst char	*elf_errmsg(int _error);
182164190Sjkoshyint		elf_errno(void);
183164190Sjkoshyvoid		elf_fill(int _fill);
184164190Sjkoshyunsigned int	elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd, unsigned int _flags);
185164190Sjkoshyunsigned int	elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
186164190Sjkoshyunsigned int	elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
187164190Sjkoshyunsigned int	elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
188164190Sjkoshyunsigned int	elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
189164190Sjkoshyunsigned int	elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
190164190SjkoshyElf_Arhdr	*elf_getarhdr(Elf *_elf);
191164190SjkoshyElf_Arsym	*elf_getarsym(Elf *_elf, size_t *_ptr);
192164190Sjkoshyoff_t		elf_getbase(Elf *_elf);
193164190SjkoshyElf_Data	*elf_getdata(Elf_Scn *, Elf_Data *);
194164190Sjkoshychar		*elf_getident(Elf *_elf, size_t *_ptr);
195164190Sjkoshyint		elf_getphnum(Elf *_elf, size_t *_dst);
196164190SjkoshyElf_Scn		*elf_getscn(Elf *_elf, size_t _index);
197164190Sjkoshyint		elf_getshnum(Elf *_elf, size_t *_dst);
198164190Sjkoshyint		elf_getshstrndx(Elf *_elf, size_t *_dst);
199164190Sjkoshyunsigned long	elf_hash(const char *_name);
200164190SjkoshyElf_Kind	elf_kind(Elf *_elf);
201164190SjkoshyElf		*elf_memory(char *_image, size_t _size);
202164190Sjkoshysize_t		elf_ndxscn(Elf_Scn *_scn);
203164190SjkoshyElf_Data	*elf_newdata(Elf_Scn *_scn);
204164190SjkoshyElf_Scn		*elf_newscn(Elf *_elf);
205164190SjkoshyElf_Scn		*elf_nextscn(Elf *_elf, Elf_Scn *_scn);
206164190SjkoshyElf_Cmd		elf_next(Elf *_elf);
207164190Sjkoshyoff_t		elf_rand(Elf *_elf, off_t _off);
208164190SjkoshyElf_Data	*elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
209164190Sjkoshychar		*elf_rawfile(Elf *_elf, size_t *_size);
210164190Sjkoshyint		elf_setshstrndx(Elf *_elf, size_t _shnum);
211164190Sjkoshychar		*elf_strptr(Elf *_elf, size_t _section, size_t _offset);
212164190Sjkoshyoff_t		elf_update(Elf *_elf, Elf_Cmd _cmd);
213164190Sjkoshyunsigned int	elf_version(unsigned int _version);
214164190Sjkoshy
215164190Sjkoshylong		elf32_checksum(Elf *_elf);
216164190Sjkoshysize_t		elf32_fsize(Elf_Type _type, size_t _count,
217164190Sjkoshy			unsigned int _version);
218164190SjkoshyElf32_Ehdr	*elf32_getehdr(Elf *_elf);
219164190SjkoshyElf32_Phdr	*elf32_getphdr(Elf *_elf);
220164190SjkoshyElf32_Shdr	*elf32_getshdr(Elf_Scn *_scn);
221164190SjkoshyElf32_Ehdr	*elf32_newehdr(Elf *_elf);
222164190SjkoshyElf32_Phdr	*elf32_newphdr(Elf *_elf, size_t _count);
223164190SjkoshyElf_Data	*elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
224164190Sjkoshy			unsigned int _enc);
225164190SjkoshyElf_Data	*elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
226164190Sjkoshy			unsigned int _enc);
227164190Sjkoshy
228164190Sjkoshylong		elf64_checksum(Elf *_elf);
229164190Sjkoshysize_t		elf64_fsize(Elf_Type _type, size_t _count,
230164190Sjkoshy			unsigned int _version);
231164190SjkoshyElf64_Ehdr	*elf64_getehdr(Elf *_elf);
232164190SjkoshyElf64_Phdr	*elf64_getphdr(Elf *_elf);
233164190SjkoshyElf64_Shdr	*elf64_getshdr(Elf_Scn *_scn);
234164190SjkoshyElf64_Ehdr	*elf64_newehdr(Elf *_elf);
235164190SjkoshyElf64_Phdr	*elf64_newphdr(Elf *_elf, size_t _count);
236164190SjkoshyElf_Data	*elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
237164190Sjkoshy			unsigned int _enc);
238164190SjkoshyElf_Data	*elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
239164190Sjkoshy			unsigned int _enc);
240164190Sjkoshy
241164190Sjkoshy#if	defined(LIBELF_TEST_HOOKS)
242164190Sjkoshyint		_libelf_get_elf_class(Elf *_elf);
243164190Sjkoshyint		_libelf_get_max_error(void);
244164190Sjkoshyconst char	*_libelf_get_no_error_message(void);
245164190Sjkoshyconst char	*_libelf_get_unknown_error_message(void);
246164190Sjkoshyvoid		_libelf_set_elf_class(Elf *_elf, int _class);
247164190Sjkoshyvoid		_libelf_set_error(int _error);
248164190Sjkoshy#endif	/* LIBELF_TEST_HOOKS */
249164190Sjkoshy__END_DECLS
250164190Sjkoshy
251164190Sjkoshy#endif	/* _LIBELF_H_ */
252