1 /* $NetBSD: libelf.h,v 1.5 2009/12/21 18:21:17 thorpej Exp $ */
4 * Copyright (c) 2006 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $FreeBSD: src/lib/libelf/libelf.h,v 1.1.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $
34 #include <sys/types.h>
35 #include <sys/queue.h>
38 #if HAVE_NBTOOL_CONFIG_H
39 # include "nbtool_config.h"
40 # include <nbinclude/sys/exec_elf.h>
41 # define __LIBELF_HAVE_ELF_CAP 1
42 # define __LIBELF_HAVE_ELF_MOVE 1
43 # define __LIBELF_HAVE_ELF_NOTE 1
44 # define __LIBELF_HAVE_ELF_SYMINFO 1
45 # define __LIBELF_HAVE_ELF_VERS 1
46 #elif defined(__NetBSD__)
47 # include <sys/exec_elf.h>
48 # define __LIBELF_HAVE_ELF_CAP 1
49 # define __LIBELF_HAVE_ELF_MOVE 1
50 # define __LIBELF_HAVE_ELF_NOTE 1
51 # define __LIBELF_HAVE_ELF_SYMINFO 1
52 # define __LIBELF_HAVE_ELF_VERS 1
53 #elif defined(__FreeBSD__)
54 # include <sys/elf32.h>
55 # include <sys/elf64.h>
56 # include <osreldate.h>
57 # if __FreeBSD_version >= 700025
58 # define __LIBELF_HAVE_ELF_CAP 1
59 # define __LIBELF_HAVE_ELF_MOVE 1
60 # define __LIBELF_HAVE_ELF_NOTE 1
61 # define __LIBELF_HAVE_ELF_SYMINFO 1
62 # endif /* __FreeBSD_version >= 700025 */
63 # if __FreeBSD_version >= 700009
64 # define __LIBELF_HAVE_ELF_VERS 1
65 # endif /* __FreeBSD_version >= 700009 */
68 /* Library private data structures */
69 typedef struct _Elf Elf
;
70 typedef struct _Elf_Scn Elf_Scn
;
75 ELF_K_AR
, /* `ar' archives */
76 ELF_K_COFF
, /* COFF files (unsupported) */
77 ELF_K_ELF
, /* ELF files */
81 #define ELF_K_FIRST ELF_K_NONE
82 #define ELF_K_LAST ELF_K_NUM
112 #define ELF_T_FIRST ELF_T_ADDR
113 #define ELF_T_LAST ELF_T_XWORD
128 #define ELF_C_FIRST ELF_C_NULL
129 #define ELF_C_LAST ELF_C_NUM
132 * An `Elf_Data' structure describes data in an
135 typedef struct _Elf_Data
{
137 * `Public' members that are part of the ELF(3) API.
144 unsigned int d_version
;
147 * Members that are not part of the public API.
149 Elf_Scn
*d_scn
; /* containing section */
150 unsigned int d_flags
;
151 STAILQ_ENTRY(_Elf_Data
) d_next
;
155 * An `Elf_Arhdr' structure describes an archive
160 char *ar_name
; /* archive member name */
163 char *ar_rawname
; /* 'raw' member name */
169 * An `Elf_Arsym' describes an entry in the archive
173 off_t as_off
; /* byte offset to member's header */
174 unsigned long as_hash
; /* elf_hash() value for name */
175 char *as_name
; /* null terminated symbol name */
183 ELF_E_NONE
, /* No error */
184 ELF_E_ARCHIVE
, /* Malformed ar(1) archive */
185 ELF_E_ARGUMENT
, /* Invalid argument */
186 ELF_E_CLASS
, /* Mismatched ELF class */
187 ELF_E_DATA
, /* Invalid data descriptor */
188 ELF_E_HEADER
, /* Missing or malformed ELF header */
189 ELF_E_IO
, /* I/O error */
190 ELF_E_LAYOUT
, /* Layout constraint violation */
191 ELF_E_MODE
, /* Wrong mode for ELF descriptor */
192 ELF_E_RANGE
, /* Value out of range */
193 ELF_E_RESOURCE
, /* Resource exhaustion */
194 ELF_E_SECTION
, /* Invalid section descriptor */
195 ELF_E_SEQUENCE
, /* API calls out of sequence */
196 ELF_E_UNIMPL
, /* Feature is unimplemented */
197 ELF_E_VERSION
, /* Unknown API version */
198 ELF_E_NUM
/* Max error number */
202 * Flags defined by the API.
205 #define ELF_F_LAYOUT 0x001U /* application will layout the file */
206 #define ELF_F_DIRTY 0x002U /* a section or ELF file is dirty */
209 Elf
*elf_begin(int _fd
, Elf_Cmd _cmd
, Elf
*_elf
);
210 int elf_cntl(Elf
*_elf
, Elf_Cmd _cmd
);
211 int elf_end(Elf
*_elf
);
212 const char *elf_errmsg(int _error
);
214 void elf_fill(int _fill
);
215 unsigned int elf_flagdata(Elf_Data
*_data
, Elf_Cmd _cmd
, unsigned int _flags
);
216 unsigned int elf_flagehdr(Elf
*_elf
, Elf_Cmd _cmd
, unsigned int _flags
);
217 unsigned int elf_flagelf(Elf
*_elf
, Elf_Cmd _cmd
, unsigned int _flags
);
218 unsigned int elf_flagphdr(Elf
*_elf
, Elf_Cmd _cmd
, unsigned int _flags
);
219 unsigned int elf_flagscn(Elf_Scn
*_scn
, Elf_Cmd _cmd
, unsigned int _flags
);
220 unsigned int elf_flagshdr(Elf_Scn
*_scn
, Elf_Cmd _cmd
, unsigned int _flags
);
221 Elf_Arhdr
*elf_getarhdr(Elf
*_elf
);
222 Elf_Arsym
*elf_getarsym(Elf
*_elf
, size_t *_ptr
);
223 off_t
elf_getbase(Elf
*_elf
);
224 Elf_Data
*elf_getdata(Elf_Scn
*, Elf_Data
*);
225 char *elf_getident(Elf
*_elf
, size_t *_ptr
);
226 int elf_getphnum(Elf
*_elf
, size_t *_dst
);
227 Elf_Scn
*elf_getscn(Elf
*_elf
, size_t _index
);
228 int elf_getshnum(Elf
*_elf
, size_t *_dst
);
229 int elf_getshstrndx(Elf
*_elf
, size_t *_dst
);
230 unsigned long elf_hash(const void *_name
);
231 Elf_Kind
elf_kind(Elf
*_elf
);
232 Elf
*elf_memory(char *_image
, size_t _size
);
233 size_t elf_ndxscn(Elf_Scn
*_scn
);
234 Elf_Data
*elf_newdata(Elf_Scn
*_scn
);
235 Elf_Scn
*elf_newscn(Elf
*_elf
);
236 Elf_Scn
*elf_nextscn(Elf
*_elf
, Elf_Scn
*_scn
);
237 Elf_Cmd
elf_next(Elf
*_elf
);
238 off_t
elf_rand(Elf
*_elf
, off_t _off
);
239 Elf_Data
*elf_rawdata(Elf_Scn
*_scn
, Elf_Data
*_data
);
240 char *elf_rawfile(Elf
*_elf
, size_t *_size
);
241 int elf_setshstrndx(Elf
*_elf
, size_t _shnum
);
242 char *elf_strptr(Elf
*_elf
, size_t _section
, size_t _offset
);
243 off_t
elf_update(Elf
*_elf
, Elf_Cmd _cmd
);
244 unsigned int elf_version(unsigned int _version
);
246 long elf32_checksum(Elf
*_elf
);
247 size_t elf32_fsize(Elf_Type _type
, size_t _count
,
248 unsigned int _version
);
249 Elf32_Ehdr
*elf32_getehdr(Elf
*_elf
);
250 Elf32_Phdr
*elf32_getphdr(Elf
*_elf
);
251 Elf32_Shdr
*elf32_getshdr(Elf_Scn
*_scn
);
252 Elf32_Ehdr
*elf32_newehdr(Elf
*_elf
);
253 Elf32_Phdr
*elf32_newphdr(Elf
*_elf
, size_t _count
);
254 Elf_Data
*elf32_xlatetof(Elf_Data
*_dst
, const Elf_Data
*_src
,
256 Elf_Data
*elf32_xlatetom(Elf_Data
*_dst
, const Elf_Data
*_src
,
259 long elf64_checksum(Elf
*_elf
);
260 size_t elf64_fsize(Elf_Type _type
, size_t _count
,
261 unsigned int _version
);
262 Elf64_Ehdr
*elf64_getehdr(Elf
*_elf
);
263 Elf64_Phdr
*elf64_getphdr(Elf
*_elf
);
264 Elf64_Shdr
*elf64_getshdr(Elf_Scn
*_scn
);
265 Elf64_Ehdr
*elf64_newehdr(Elf
*_elf
);
266 Elf64_Phdr
*elf64_newphdr(Elf
*_elf
, size_t _count
);
267 Elf_Data
*elf64_xlatetof(Elf_Data
*_dst
, const Elf_Data
*_src
,
269 Elf_Data
*elf64_xlatetom(Elf_Data
*_dst
, const Elf_Data
*_src
,
272 #if defined(LIBELF_TEST_HOOKS)
273 int _libelf_get_elf_class(Elf
*_elf
);
274 int _libelf_get_max_error(void);
275 const char *_libelf_get_no_error_message(void);
276 const char *_libelf_get_unknown_error_message(void);
277 void _libelf_set_elf_class(Elf
*_elf
, int _class
);
278 void _libelf_set_error(int _error
);
279 #endif /* LIBELF_TEST_HOOKS */
282 #endif /* _LIBELF_H_ */