1260684Skaiw/*-
2260684Skaiw * Copyright (c) 2006,2008-2011 Joseph Koshy
3260684Skaiw * All rights reserved.
4260684Skaiw *
5260684Skaiw * Redistribution and use in source and binary forms, with or without
6260684Skaiw * modification, are permitted provided that the following conditions
7260684Skaiw * are met:
8260684Skaiw * 1. Redistributions of source code must retain the above copyright
9260684Skaiw *    notice, this list of conditions and the following disclaimer.
10260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright
11260684Skaiw *    notice, this list of conditions and the following disclaimer in the
12260684Skaiw *    documentation and/or other materials provided with the distribution.
13260684Skaiw *
14260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17260684Skaiw * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21260684Skaiw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24260684Skaiw * SUCH DAMAGE.
25260684Skaiw *
26367466Sdim * $Id: _libelf.h 3738 2019-05-05 21:49:06Z jkoshy $
27260684Skaiw */
28260684Skaiw
29260684Skaiw#ifndef	__LIBELF_H_
30260684Skaiw#define	__LIBELF_H_
31260684Skaiw
32260684Skaiw#include <sys/queue.h>
33367466Sdim#include <sys/tree.h>
34260684Skaiw
35260684Skaiw#include "_libelf_config.h"
36260684Skaiw
37260684Skaiw#include "_elftc.h"
38260684Skaiw
39260684Skaiw/*
40260684Skaiw * Library-private data structures.
41260684Skaiw */
42260684Skaiw
43260684Skaiw#define LIBELF_MSG_SIZE	256
44260684Skaiw
45260684Skaiwstruct _libelf_globals {
46260684Skaiw	int		libelf_arch;
47260684Skaiw	unsigned int	libelf_byteorder;
48260684Skaiw	int		libelf_class;
49260684Skaiw	int		libelf_error;
50260684Skaiw	int		libelf_fillchar;
51260684Skaiw	unsigned int	libelf_version;
52276371Semaste	unsigned char	libelf_msg[LIBELF_MSG_SIZE];
53260684Skaiw};
54260684Skaiw
55260684Skaiwextern struct _libelf_globals _libelf;
56260684Skaiw
57260684Skaiw#define	LIBELF_PRIVATE(N)	(_libelf.libelf_##N)
58260684Skaiw
59260684Skaiw#define	LIBELF_ELF_ERROR_MASK			0xFF
60260684Skaiw#define	LIBELF_OS_ERROR_SHIFT			8
61260684Skaiw
62260684Skaiw#define	LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) |	\
63260684Skaiw	((O) << LIBELF_OS_ERROR_SHIFT))
64260684Skaiw
65260684Skaiw#define	LIBELF_SET_ERROR(E, O) do {					\
66260684Skaiw		LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O));	\
67260684Skaiw	} while (0)
68260684Skaiw
69260684Skaiw#define	LIBELF_ADJUST_AR_SIZE(S)	(((S) + 1U) & ~1U)
70260684Skaiw
71260684Skaiw/*
72260684Skaiw * Flags for library internal use.  These use the upper 16 bits of the
73260684Skaiw * `e_flags' field.
74260684Skaiw */
75276371Semaste#define	LIBELF_F_API_MASK	0x00FFFFU  /* Flags defined by the API. */
76276371Semaste#define	LIBELF_F_AR_HEADER	0x010000U  /* translated header available */
77276371Semaste#define	LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
78276371Semaste#define	LIBELF_F_DATA_MALLOCED	0x040000U /* whether data was malloc'ed */
79276371Semaste#define	LIBELF_F_RAWFILE_MALLOC	0x080000U /* whether e_rawfile was malloc'ed */
80276371Semaste#define	LIBELF_F_RAWFILE_MMAP	0x100000U /* whether e_rawfile was mmap'ed */
81276371Semaste#define	LIBELF_F_SHDRS_LOADED	0x200000U /* whether all shdrs were read in */
82276371Semaste#define	LIBELF_F_SPECIAL_FILE	0x400000U /* non-regular file */
83260684Skaiw
84367466SdimRB_HEAD(scntree, _Elf_Scn);
85367466SdimRB_PROTOTYPE(scntree, _Elf_Scn, e_scn, elfscn_cmp);
86367466Sdim
87260684Skaiwstruct _Elf {
88260684Skaiw	int		e_activations;	/* activation count */
89260684Skaiw	unsigned int	e_byteorder;	/* ELFDATA* */
90260684Skaiw	int		e_class;	/* ELFCLASS*  */
91260684Skaiw	Elf_Cmd		e_cmd;		/* ELF_C_* used at creation time */
92260684Skaiw	int		e_fd;		/* associated file descriptor */
93260684Skaiw	unsigned int	e_flags;	/* ELF_F_* & LIBELF_F_* flags */
94260684Skaiw	Elf_Kind	e_kind;		/* ELF_K_* */
95260684Skaiw	Elf		*e_parent; 	/* non-NULL for archive members */
96276371Semaste	unsigned char	*e_rawfile;	/* uninterpreted bytes */
97367466Sdim	off_t		e_rawsize;	/* size of uninterpreted bytes */
98260684Skaiw	unsigned int	e_version;	/* file version */
99260684Skaiw
100260684Skaiw	/*
101260684Skaiw	 * Header information for archive members.  See the
102260684Skaiw	 * LIBELF_F_AR_HEADER flag.
103260684Skaiw	 */
104260684Skaiw	union {
105260684Skaiw		Elf_Arhdr	*e_arhdr;	/* translated header */
106276371Semaste		unsigned char	*e_rawhdr;	/* untranslated header */
107260684Skaiw	} e_hdr;
108260684Skaiw
109260684Skaiw	union {
110260684Skaiw		struct {		/* ar(1) archives */
111260684Skaiw			off_t	e_next;	/* set by elf_rand()/elf_next() */
112260684Skaiw			int	e_nchildren;
113276371Semaste			unsigned char *e_rawstrtab; /* file name strings */
114260684Skaiw			size_t	e_rawstrtabsz;
115276371Semaste			unsigned char *e_rawsymtab;	/* symbol table */
116260684Skaiw			size_t	e_rawsymtabsz;
117260684Skaiw			Elf_Arsym *e_symtab;
118260684Skaiw			size_t	e_symtabsz;
119260684Skaiw		} e_ar;
120260684Skaiw		struct {		/* regular ELF files */
121260684Skaiw			union {
122260684Skaiw				Elf32_Ehdr *e_ehdr32;
123260684Skaiw				Elf64_Ehdr *e_ehdr64;
124260684Skaiw			} e_ehdr;
125260684Skaiw			union {
126260684Skaiw				Elf32_Phdr *e_phdr32;
127260684Skaiw				Elf64_Phdr *e_phdr64;
128260684Skaiw			} e_phdr;
129367466Sdim			struct scntree	e_scn;	/* sections */
130260684Skaiw			size_t	e_nphdr;	/* number of Phdr entries */
131260684Skaiw			size_t	e_nscn;		/* number of sections */
132260684Skaiw			size_t	e_strndx;	/* string table section index */
133260684Skaiw		} e_elf;
134260684Skaiw	} e_u;
135260684Skaiw};
136260684Skaiw
137260684Skaiw/*
138260684Skaiw * The internal descriptor wrapping the "Elf_Data" type.
139260684Skaiw */
140260684Skaiwstruct _Libelf_Data {
141260684Skaiw	Elf_Data	d_data;		/* The exported descriptor. */
142260684Skaiw	Elf_Scn		*d_scn;		/* The containing section */
143260684Skaiw	unsigned int	d_flags;
144260684Skaiw	STAILQ_ENTRY(_Libelf_Data) d_next;
145260684Skaiw};
146260684Skaiw
147260684Skaiwstruct _Elf_Scn {
148260684Skaiw	union {
149260684Skaiw		Elf32_Shdr	s_shdr32;
150260684Skaiw		Elf64_Shdr	s_shdr64;
151260684Skaiw	} s_shdr;
152260684Skaiw	STAILQ_HEAD(, _Libelf_Data) s_data;	/* translated data */
153260684Skaiw	STAILQ_HEAD(, _Libelf_Data) s_rawdata;	/* raw data */
154367466Sdim	RB_ENTRY(_Elf_Scn) s_tree;
155260684Skaiw	struct _Elf	*s_elf;		/* parent ELF descriptor */
156260684Skaiw	unsigned int	s_flags;	/* flags for the section as a whole */
157260684Skaiw	size_t		s_ndx;		/* index# for this section */
158260684Skaiw	uint64_t	s_offset;	/* managed by elf_update() */
159260684Skaiw	uint64_t	s_rawoff;	/* original offset in the file */
160260684Skaiw	uint64_t	s_size;		/* managed by elf_update() */
161260684Skaiw};
162260684Skaiw
163260684Skaiw
164260684Skaiwenum {
165260684Skaiw	ELF_TOFILE,
166260684Skaiw	ELF_TOMEMORY
167260684Skaiw};
168260684Skaiw
169276371Semaste
170276371Semaste/*
171276371Semaste * The LIBELF_COPY macros are used to copy fields from a GElf_*
172276371Semaste * structure to their 32-bit counterparts, while checking for out of
173276371Semaste * range values.
174276371Semaste *
175276371Semaste * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.
176276371Semaste * - LIBELF_COPY_S32 :: copy a signed 32 bit field.
177276371Semaste */
178276371Semaste
179276371Semaste#define	LIBELF_COPY_U32(DST, SRC, NAME)	do {			\
180276371Semaste		if ((SRC)->NAME > UINT32_MAX) {			\
181276371Semaste			LIBELF_SET_ERROR(RANGE, 0);		\
182276371Semaste			return (0);				\
183276371Semaste		}						\
184276371Semaste		(DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU;	\
185260684Skaiw	} while (0)
186260684Skaiw
187276371Semaste#define	LIBELF_COPY_S32(DST, SRC, NAME)	do {			\
188276371Semaste		if ((SRC)->NAME > INT32_MAX ||			\
189276371Semaste		    (SRC)->NAME < INT32_MIN) {			\
190276371Semaste			LIBELF_SET_ERROR(RANGE, 0);		\
191276371Semaste			return (0);				\
192276371Semaste		}						\
193276371Semaste		(DST)->NAME = (int32_t) (SRC)->NAME;		\
194260684Skaiw	} while (0)
195260684Skaiw
196260684Skaiw
197260684Skaiw/*
198260684Skaiw * Function Prototypes.
199260684Skaiw */
200260684Skaiw
201367466Sdimtypedef int _libelf_translator_function(unsigned char *_dst, size_t dsz,
202367466Sdim    unsigned char *_src, size_t _cnt, int _byteswap);
203367466Sdim
204280932Semaste#ifdef __cplusplus
205280932Semasteextern "C" {
206280932Semaste#endif
207260684Skaiwstruct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);
208260684SkaiwElf	*_libelf_allocate_elf(void);
209260684SkaiwElf_Scn	*_libelf_allocate_scn(Elf *_e, size_t _ndx);
210260684SkaiwElf_Arhdr *_libelf_ar_gethdr(Elf *_e);
211260684SkaiwElf	*_libelf_ar_open(Elf *_e, int _reporterror);
212260684SkaiwElf	*_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
213260684SkaiwElf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
214260684SkaiwElf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
215276371Semastelong	 _libelf_checksum(Elf *_e, int _elfclass);
216260684Skaiwvoid	*_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
217367466Sdimint	_libelf_elfmachine(Elf *_e);
218276371Semasteunsigned int _libelf_falign(Elf_Type _t, int _elfclass);
219260684Skaiwsize_t	_libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
220260684Skaiw    size_t count);
221367466Sdim_libelf_translator_function *_libelf_get_translator(Elf_Type _t,
222367466Sdim    int _direction, int _elfclass, int _elfmachine);
223367466Sdimvoid	*_libelf_getchdr(Elf_Scn *_e, int _elfclass);
224260684Skaiwvoid	*_libelf_getphdr(Elf *_e, int _elfclass);
225260684Skaiwvoid	*_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
226260684Skaiwvoid	_libelf_init_elf(Elf *_e, Elf_Kind _kind);
227340309Semasteint	_libelf_is_mips64el(Elf *e);
228260684Skaiwint	_libelf_load_section_headers(Elf *e, void *ehdr);
229276371Semasteunsigned int _libelf_malign(Elf_Type _t, int _elfclass);
230276371SemasteElf	*_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
231260684Skaiwsize_t	_libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
232260684Skaiwvoid	*_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
233260684SkaiwElf	*_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
234340309SemasteElf64_Xword _libelf_mips64el_r_info_tof(Elf64_Xword r_info);
235340309SemasteElf64_Xword _libelf_mips64el_r_info_tom(Elf64_Xword r_info);
236260684Skaiwstruct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);
237367466Sdimvoid	_libelf_release_elf(Elf *_e);
238260684SkaiwElf_Scn	*_libelf_release_scn(Elf_Scn *_s);
239260684Skaiwint	_libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
240260684Skaiwint	_libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
241260684Skaiwint	_libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
242260684Skaiw    size_t _shstrndx);
243260684SkaiwElf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
244367466Sdim    unsigned int _encoding, int _elfclass, int _elfmachine, int _direction);
245260684Skaiwint	_libelf_xlate_shtype(uint32_t _sht);
246280932Semaste#ifdef __cplusplus
247280932Semaste}
248280932Semaste#endif
249260684Skaiw
250260684Skaiw#endif	/* __LIBELF_H_ */
251