1 /* $NetBSD: _libelf.h,v 1.3 2009/12/19 07:47:22 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.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $
34 #include <sys/queue.h>
37 #define NULL ((void *) 0)
41 * Library-private data structures.
44 #define LIBELF_MSG_SIZE 256
46 struct _libelf_globals
{
49 unsigned int libelf_version
;
50 char libelf_msg
[LIBELF_MSG_SIZE
];
53 extern struct _libelf_globals _libelf
;
55 #define LIBELF_PRIVATE(N) (_libelf.libelf_##N)
57 #define LIBELF_ELF_ERROR_MASK 0xFF
58 #define LIBELF_OS_ERROR_SHIFT 8
60 #define LIBELF_SET_ERROR(E, O) do { \
61 LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)| \
62 ((O) << LIBELF_OS_ERROR_SHIFT)); \
63 } while (/*CONSTCOND*/0)
65 #define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U)
68 * Flags for library internal use. These use the upper 16 bits of a
71 #define LIBELF_F_MALLOCED 0x010000 /* whether data was malloc'ed */
72 #define LIBELF_F_MMAP 0x020000 /* whether e_rawfile was mmap'ed */
73 #define LIBELF_F_SHDRS_LOADED 0x040000 /* whether all shdrs were read in */
76 int e_activations
; /* activation count */
77 Elf_Arhdr
*e_arhdr
; /* header for archive members */
78 unsigned int e_byteorder
; /* ELFDATA* */
79 int e_class
; /* ELFCLASS* */
80 Elf_Cmd e_cmd
; /* ELF_C_* used at creation time */
81 int e_fd
; /* associated file descriptor */
82 unsigned int e_flags
; /* ELF_F_*, LIBELF_F_* flags */
83 Elf_Kind e_kind
; /* ELF_K_* */
84 Elf
*e_parent
; /* non-NULL for archive members */
85 char *e_rawfile
; /* uninterpreted bytes */
86 size_t e_rawsize
; /* size of uninterpreted bytes */
87 unsigned int e_version
; /* file version */
90 struct { /* ar(1) archives */
91 off_t e_next
; /* set by elf_rand()/elf_next() */
93 char *e_rawstrtab
; /* file name strings */
95 char *e_rawsymtab
; /* symbol table */
100 struct { /* regular ELF files */
102 Elf32_Ehdr
*e_ehdr32
;
103 Elf64_Ehdr
*e_ehdr64
;
106 Elf32_Phdr
*e_phdr32
;
107 Elf64_Phdr
*e_phdr64
;
109 STAILQ_HEAD(, _Elf_Scn
) e_scn
; /* section list */
110 size_t e_nphdr
; /* number of Phdr entries */
111 size_t e_nscn
; /* number of sections */
112 size_t e_strndx
; /* string table section index */
122 STAILQ_HEAD(, _Elf_Data
) s_data
; /* list of Elf_Data descriptors */
123 STAILQ_HEAD(, _Elf_Data
) s_rawdata
; /* raw data for this section */
124 STAILQ_ENTRY(_Elf_Scn
) s_next
;
125 struct _Elf
*s_elf
; /* parent ELF descriptor */
126 unsigned int s_flags
; /* flags for the section as a whole */
127 size_t s_ndx
; /* index# for this section */
128 uint64_t s_offset
; /* managed by elf_update() */
129 uint64_t s_rawoff
; /* original offset in the file */
130 uint64_t s_size
; /* managed by elf_update() */
139 #define LIBELF_COPY_U32(DST,SRC,NAME) do { \
140 if ((uint64_t)(SRC)->NAME > UINT_MAX) { \
141 LIBELF_SET_ERROR(RANGE, 0); \
144 (DST)->NAME = (SRC)->NAME; \
145 } while (/*CONSTCOND*/0)
147 #define LIBELF_COPY_S32(DST,SRC,NAME) do { \
148 if ((int64_t)(SRC)->NAME > INT_MAX || \
149 (int64_t)(SRC)->NAME < INT_MIN) { \
150 LIBELF_SET_ERROR(RANGE, 0); \
153 (DST)->NAME = (SRC)->NAME; \
154 } while (/*CONSTCOND*/0)
161 unsigned int _libelf_host_byteorder(void);
163 Elf_Data
*_libelf_allocate_data(Elf_Scn
*_s
);
164 Elf
*_libelf_allocate_elf(void);
165 Elf_Scn
*_libelf_allocate_scn(Elf
*_e
, size_t _ndx
);
166 Elf_Arhdr
*_libelf_ar_gethdr(Elf
*_e
);
167 Elf
*_libelf_ar_open(Elf
*_e
);
168 Elf
*_libelf_ar_open_member(int _fd
, Elf_Cmd _c
, Elf
*_ar
);
169 Elf_Arsym
*_libelf_ar_process_symtab(Elf
*_ar
, size_t *_dst
);
170 unsigned long _libelf_checksum(Elf
*_e
, int _elfclass
);
171 void *_libelf_ehdr(Elf
*_e
, int _elfclass
, int _allocate
);
172 int _libelf_falign(Elf_Type _t
, int _elfclass
);
173 size_t _libelf_fsize(Elf_Type _t
, int _elfclass
, unsigned int _version
,
175 void (*_libelf_get_translator(Elf_Type _t
, int _direction
, int _elfclass
))
176 (char *_dst
, char *_src
, size_t _cnt
, int _byteswap
);
177 void *_libelf_getphdr(Elf
*_e
, int _elfclass
);
178 void *_libelf_getshdr(Elf_Scn
*_scn
, int _elfclass
);
179 void _libelf_init_elf(Elf
*_e
, Elf_Kind _kind
);
180 int _libelf_malign(Elf_Type _t
, int _elfclass
);
181 size_t _libelf_msize(Elf_Type _t
, int _elfclass
, unsigned int _version
);
182 void *_libelf_newphdr(Elf
*_e
, int _elfclass
, size_t _count
);
183 Elf_Data
*_libelf_release_data(Elf_Data
*_d
);
184 Elf
*_libelf_release_elf(Elf
*_e
);
185 Elf_Scn
*_libelf_release_scn(Elf_Scn
*_s
);
186 int _libelf_setphnum(Elf
*_e
, void *_eh
, int _elfclass
, size_t _phnum
);
187 int _libelf_setshnum(Elf
*_e
, void *_eh
, int _elfclass
, size_t _shnum
);
188 int _libelf_setshstrndx(Elf
*_e
, void *_eh
, int _elfclass
,
190 Elf_Data
*_libelf_xlate(Elf_Data
*_d
, const Elf_Data
*_s
,
191 unsigned int _encoding
, int _elfclass
, int _direction
);
192 int _libelf_xlate_shtype(uint32_t _sht
);
194 #endif /* __LIBELF_H_ */