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