xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/_libelf.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: _libelf.h,v 1.3 2016/02/20 02:43:42 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 3174 2015-03-27 17:13:41Z emaste
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 #ifndef roundup2
41 #define roundup2(x, m)  (((x) + (m) - 1) & ~((m) - 1))
42 #endif
43 
44 /*
45  * Library-private data structures.
46  */
47 
48 #define LIBELF_MSG_SIZE	256
49 
50 struct _libelf_globals {
51 	int		libelf_error;
52 	int		libelf_fillchar;
53 	unsigned int	libelf_version;
54 	unsigned char	libelf_msg[LIBELF_MSG_SIZE];
55 };
56 
57 extern struct _libelf_globals _libelf;
58 
59 #define	LIBELF_PRIVATE(N)	(_libelf.libelf_##N)
60 
61 #define	LIBELF_ELF_ERROR_MASK			0xFF
62 #define	LIBELF_OS_ERROR_SHIFT			8
63 
64 #define	LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) |	\
65 	((O) << LIBELF_OS_ERROR_SHIFT))
66 
67 #define	LIBELF_SET_ERROR(E, O) do {					\
68 		LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O));	\
69 	} while (/*CONSTCOND*/0)
70 
71 #define	LIBELF_ADJUST_AR_SIZE(S)	(((S) + 1U) & ~1U)
72 
73 /*
74  * Flags for library internal use.  These use the upper 16 bits of the
75  * `e_flags' field.
76  */
77 #define	LIBELF_F_API_MASK	0x00FFFFU  /* Flags defined by the API. */
78 #define	LIBELF_F_AR_HEADER	0x010000U  /* translated header available */
79 #define	LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
80 #define	LIBELF_F_DATA_MALLOCED	0x040000U /* whether data was malloc'ed */
81 #define	LIBELF_F_RAWFILE_MALLOC	0x080000U /* whether e_rawfile was malloc'ed */
82 #define	LIBELF_F_RAWFILE_MMAP	0x100000U /* whether e_rawfile was mmap'ed */
83 #define	LIBELF_F_SHDRS_LOADED	0x200000U /* whether all shdrs were read in */
84 #define	LIBELF_F_SPECIAL_FILE	0x400000U /* non-regular file */
85 
86 struct _Elf {
87 	int		e_activations;	/* activation count */
88 	unsigned int	e_byteorder;	/* ELFDATA* */
89 	int		e_class;	/* ELFCLASS*  */
90 	Elf_Cmd		e_cmd;		/* ELF_C_* used at creation time */
91 	int		e_fd;		/* associated file descriptor */
92 	unsigned int	e_flags;	/* ELF_F_* & LIBELF_F_* flags */
93 	Elf_Kind	e_kind;		/* ELF_K_* */
94 	Elf		*e_parent; 	/* non-NULL for archive members */
95 	unsigned char	*e_rawfile;	/* uninterpreted bytes */
96 	size_t		e_rawsize;	/* size of uninterpreted bytes */
97 	unsigned int	e_version;	/* file version */
98 
99 	/*
100 	 * Header information for archive members.  See the
101 	 * LIBELF_F_AR_HEADER flag.
102 	 */
103 	union {
104 		Elf_Arhdr	*e_arhdr;	/* translated header */
105 		unsigned char	*e_rawhdr;	/* untranslated header */
106 	} e_hdr;
107 
108 	union {
109 		struct {		/* ar(1) archives */
110 			off_t	e_next;	/* set by elf_rand()/elf_next() */
111 			int	e_nchildren;
112 			unsigned char *e_rawstrtab; /* file name strings */
113 			size_t	e_rawstrtabsz;
114 			unsigned char *e_rawsymtab;	/* symbol table */
115 			size_t	e_rawsymtabsz;
116 			Elf_Arsym *e_symtab;
117 			size_t	e_symtabsz;
118 		} e_ar;
119 		struct {		/* regular ELF files */
120 			union {
121 				Elf32_Ehdr *e_ehdr32;
122 				Elf64_Ehdr *e_ehdr64;
123 			} e_ehdr;
124 			union {
125 				Elf32_Phdr *e_phdr32;
126 				Elf64_Phdr *e_phdr64;
127 			} e_phdr;
128 			STAILQ_HEAD(, _Elf_Scn)	e_scn;	/* section list */
129 			size_t	e_nphdr;	/* number of Phdr entries */
130 			size_t	e_nscn;		/* number of sections */
131 			size_t	e_strndx;	/* string table section index */
132 		} e_elf;
133 	} e_u;
134 };
135 
136 /*
137  * The internal descriptor wrapping the "Elf_Data" type.
138  */
139 struct _Libelf_Data {
140 	Elf_Data	d_data;		/* The exported descriptor. */
141 	Elf_Scn		*d_scn;		/* The containing section */
142 	unsigned int	d_flags;
143 	STAILQ_ENTRY(_Libelf_Data) d_next;
144 };
145 
146 struct _Elf_Scn {
147 	union {
148 		Elf32_Shdr	s_shdr32;
149 		Elf64_Shdr	s_shdr64;
150 	} s_shdr;
151 	STAILQ_HEAD(, _Libelf_Data) s_data;	/* translated data */
152 	STAILQ_HEAD(, _Libelf_Data) s_rawdata;	/* raw data */
153 	STAILQ_ENTRY(_Elf_Scn) s_next;
154 	struct _Elf	*s_elf;		/* parent ELF descriptor */
155 	unsigned int	s_flags;	/* flags for the section as a whole */
156 	size_t		s_ndx;		/* index# for this section */
157 	uint64_t	s_offset;	/* managed by elf_update() */
158 	uint64_t	s_rawoff;	/* original offset in the file */
159 	uint64_t	s_size;		/* managed by elf_update() */
160 };
161 
162 
163 enum {
164 	ELF_TOFILE,
165 	ELF_TOMEMORY
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 
199 #ifdef __cplusplus
200 extern "C" {
201 #endif
202 unsigned int _libelf_host_byteorder(void);
203 struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);
204 Elf	*_libelf_allocate_elf(void);
205 Elf_Scn	*_libelf_allocate_scn(Elf *_e, size_t _ndx);
206 Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
207 Elf	*_libelf_ar_open(Elf *_e, int _reporterror);
208 Elf	*_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
209 Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
210 Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
211 long	 _libelf_checksum(Elf *_e, int _elfclass);
212 void	*_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
213 unsigned int _libelf_falign(Elf_Type _t, int _elfclass);
214 size_t	_libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
215     size_t count);
216 int	(*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
217 	    (unsigned char *_dst, size_t dsz, unsigned char *_src,
218 	     size_t _cnt, int _byteswap);
219 void	*_libelf_getphdr(Elf *_e, int _elfclass);
220 void	*_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
221 void	_libelf_init_elf(Elf *_e, Elf_Kind _kind);
222 int	_libelf_load_section_headers(Elf *e, void *ehdr);
223 unsigned int _libelf_malign(Elf_Type _t, int _elfclass);
224 Elf	*_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
225 size_t	_libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
226 void	*_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
227 Elf	*_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
228 struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);
229 Elf	*_libelf_release_elf(Elf *_e);
230 Elf_Scn	*_libelf_release_scn(Elf_Scn *_s);
231 int	_libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
232 int	_libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
233 int	_libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
234     size_t _shstrndx);
235 Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
236     unsigned int _encoding, int _elfclass, int _direction);
237 int	_libelf_xlate_shtype(uint32_t _sht);
238 #ifdef __cplusplus
239 }
240 #endif
241 
242 #endif	/* __LIBELF_H_ */
243