1/*	$NetBSD: _libelf.h,v 1.4 2024/03/03 17:37:33 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2006,2008-2011 Joseph Koshy
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * Id: _libelf.h 3902 2020-11-24 21:17:41Z jkoshy
29 */
30
31#ifndef	__LIBELF_H_
32#define	__LIBELF_H_
33
34#include <sys/queue.h>
35
36#include "_libelf_config.h"
37
38#include "_elftc.h"
39
40/*
41 * Library-private data structures.
42 */
43
44#define LIBELF_MSG_SIZE	256
45
46struct _libelf_globals {
47	int		libelf_arch;
48	unsigned int	libelf_byteorder;
49	int		libelf_class;
50	int		libelf_error;
51	int		libelf_fillchar;
52	unsigned int	libelf_version;
53	unsigned char	libelf_msg[LIBELF_MSG_SIZE];
54};
55
56extern struct _libelf_globals _libelf;
57
58#define	LIBELF_PRIVATE(N)	(_libelf.libelf_##N)
59
60#define	LIBELF_ELF_ERROR_MASK			0xFF
61#define	LIBELF_OS_ERROR_SHIFT			8
62
63#define	LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) |	\
64	((O) << LIBELF_OS_ERROR_SHIFT))
65
66#define	LIBELF_SET_ERROR(E, O) do {					\
67		LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O));	\
68	} while (/* CONSTCOND */ 0)
69
70#define	LIBELF_ADJUST_AR_SIZE(S)	(((S) + 1U) & ~1U)
71
72/*
73 * Flags for library internal use.  These use the upper 16 bits of the
74 * `e_flags' field.
75 */
76#define	LIBELF_F_API_MASK	0x00FFFFU  /* Flags defined by the API. */
77#define	LIBELF_F_AR_HEADER	0x010000U  /* translated header available */
78#define	LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
79#define	LIBELF_F_DATA_MALLOCED	0x040000U /* whether data was malloc'ed */
80#define	LIBELF_F_RAWFILE_MALLOC	0x080000U /* whether e_rawfile was malloc'ed */
81#define	LIBELF_F_RAWFILE_MMAP	0x100000U /* whether e_rawfile was mmap'ed */
82#define	LIBELF_F_SHDRS_LOADED	0x200000U /* whether all shdrs were read in */
83#define	LIBELF_F_SPECIAL_FILE	0x400000U /* non-regular file */
84
85struct _Elf {
86	int		e_activations;	/* activation count */
87	unsigned int	e_byteorder;	/* ELFDATA* */
88	int		e_class;	/* ELFCLASS*  */
89	Elf_Cmd		e_cmd;		/* ELF_C_* used at creation time */
90	int		e_fd;		/* associated file descriptor */
91	unsigned int	e_flags;	/* ELF_F_* & LIBELF_F_* flags */
92	Elf_Kind	e_kind;		/* ELF_K_* */
93	Elf		*e_parent; 	/* non-NULL for archive members */
94	unsigned char	*e_rawfile;	/* uninterpreted bytes */
95	off_t		e_rawsize;	/* size of uninterpreted bytes */
96	unsigned int	e_version;	/* file version */
97
98	/*
99	 * Header information for archive members.  See the
100	 * LIBELF_F_AR_HEADER flag.
101	 */
102	union {
103		Elf_Arhdr	*e_arhdr;	/* translated header */
104		unsigned char	*e_rawhdr;	/* untranslated header */
105	} e_hdr;
106
107	union {
108		struct {		/* ar(1) archives */
109			off_t	e_next;	/* set by elf_rand()/elf_next() */
110			int	e_nchildren;
111			unsigned char *e_rawstrtab; /* file name strings */
112			size_t	e_rawstrtabsz;
113			unsigned char *e_rawsymtab;	/* symbol table */
114			size_t	e_rawsymtabsz;
115			Elf_Arsym *e_symtab;
116			size_t	e_symtabsz;
117		} e_ar;
118		struct {		/* regular ELF files */
119			union {
120				Elf32_Ehdr *e_ehdr32;
121				Elf64_Ehdr *e_ehdr64;
122			} e_ehdr;
123			union {
124				Elf32_Phdr *e_phdr32;
125				Elf64_Phdr *e_phdr64;
126			} e_phdr;
127			STAILQ_HEAD(, _Elf_Scn)	e_scn;	/* section list */
128			size_t	e_nphdr;	/* number of Phdr entries */
129			size_t	e_nscn;		/* number of sections */
130			size_t	e_strndx;	/* string table section index */
131		} e_elf;
132	} e_u;
133};
134
135/*
136 * The internal descriptor wrapping the "Elf_Data" type.
137 */
138struct _Libelf_Data {
139	Elf_Data	d_data;		/* The exported descriptor. */
140	Elf_Scn		*d_scn;		/* The containing section */
141	unsigned int	d_flags;
142	STAILQ_ENTRY(_Libelf_Data) d_next;
143};
144
145struct _Elf_Scn {
146	union {
147		Elf32_Shdr	s_shdr32;
148		Elf64_Shdr	s_shdr64;
149	} s_shdr;
150	STAILQ_HEAD(, _Libelf_Data) s_data;	/* translated data */
151	STAILQ_HEAD(, _Libelf_Data) s_rawdata;	/* raw data */
152	STAILQ_ENTRY(_Elf_Scn) s_next;
153	struct _Elf	*s_elf;		/* parent ELF descriptor */
154	unsigned int	s_flags;	/* flags for the section as a whole */
155	size_t		s_ndx;		/* index# for this section */
156	uint64_t	s_offset;	/* managed by elf_update() */
157	uint64_t	s_rawoff;	/* original offset in the file */
158	uint64_t	s_size;		/* managed by elf_update() */
159};
160
161
162enum {
163	ELF_TOFILE,
164	ELF_TOMEMORY
165};
166
167
168/*
169 * The LIBELF_COPY macros are used to copy fields from a GElf_*
170 * structure to their 32-bit counterparts, while checking for out of
171 * range values.
172 *
173 * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.
174 * - LIBELF_COPY_S32 :: copy a signed 32 bit field.
175 */
176
177#define	LIBELF_COPY_U32(DST, SRC, NAME)	do {			\
178		if ((SRC)->NAME > UINT32_MAX) {			\
179			LIBELF_SET_ERROR(RANGE, 0);		\
180			return (0);				\
181		}						\
182		(DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU;	\
183	} while (/* CONSTCOND */ 0)
184
185#define	LIBELF_COPY_S32(DST, SRC, NAME)	do {			\
186		if ((SRC)->NAME > INT32_MAX ||			\
187		    (SRC)->NAME < INT32_MIN) {			\
188			LIBELF_SET_ERROR(RANGE, 0);		\
189			return (0);				\
190		}						\
191		(DST)->NAME = (int32_t) (SRC)->NAME;		\
192	} while (/* CONSTCOND */ 0)
193
194
195/*
196 * Function Prototypes.
197 */
198
199typedef int _libelf_translator_function(unsigned char *_dst, size_t dsz,
200    unsigned char *_src, size_t _cnt, int _byteswap);
201
202#ifdef __cplusplus
203extern "C" {
204#endif
205struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);
206Elf	*_libelf_allocate_elf(void);
207Elf_Scn	*_libelf_allocate_scn(Elf *_e, size_t _ndx);
208Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
209Elf	*_libelf_ar_open(Elf *_e, int _reporterror);
210Elf	*_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
211Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
212Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
213long	 _libelf_checksum(Elf *_e, int _elfclass);
214void	*_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
215int	_libelf_elfmachine(Elf *_e);
216unsigned int _libelf_falign(Elf_Type _t, int _elfclass);
217size_t	_libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
218    size_t count);
219_libelf_translator_function *_libelf_get_translator(Elf_Type _t,
220    int _direction, int _elfclass, int _elfmachine);
221void	*_libelf_getphdr(Elf *_e, int _elfclass);
222void	*_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
223void	_libelf_init_elf(Elf *_e, Elf_Kind _kind);
224int	_libelf_load_section_headers(Elf *e, void *ehdr);
225unsigned int _libelf_malign(Elf_Type _t, int _elfclass);
226Elf	*_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
227size_t	_libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
228void	*_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
229Elf	*_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
230struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);
231void	_libelf_release_elf(Elf *_e);
232Elf_Scn	*_libelf_release_scn(Elf_Scn *_s);
233int	_libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
234int	_libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
235int	_libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
236    size_t _shstrndx);
237Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
238    unsigned int _encoding, int _elfclass, int _elfmachine, int _direction);
239int	_libelf_xlate_shtype(uint32_t _sht);
240#ifdef __cplusplus
241}
242#endif
243
244#endif	/* __LIBELF_H_ */
245