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