1 /*	$NetBSD: _libelf.h,v 1.2 2014/03/09 16:58:04 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 2365 2011-12-29 04:36:44Z 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 #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 	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	0x00FFFF  /* Flags defined by the API. */
78 #define	LIBELF_F_AR_HEADER	0x010000  /* translated header available */
79 #define	LIBELF_F_AR_VARIANT_SVR4 0x020000 /* BSD style ar(1) archive */
80 #define	LIBELF_F_DATA_MALLOCED	0x040000 /* whether data was malloc'ed */
81 #define	LIBELF_F_RAWFILE_MALLOC	0x080000 /* whether e_rawfile was malloc'ed */
82 #define	LIBELF_F_RAWFILE_MMAP	0x100000 /* whether e_rawfile was mmap'ed */
83 #define	LIBELF_F_SHDRS_LOADED	0x200000 /* whether all shdrs were read in */
84 #define	LIBELF_F_SPECIAL_FILE	0x400000 /* 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 	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 		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 			char	*e_rawstrtab;	/* file name strings */
113 			size_t	e_rawstrtabsz;
114 			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 #define	LIBELF_COPY_U32(DST,SRC,NAME)	do {		\
169 		if ((uint64_t)(SRC)->NAME > UINT_MAX) {	\
170 			LIBELF_SET_ERROR(RANGE, 0);	\
171 			return (0);			\
172 		}					\
173 		(DST)->NAME = (SRC)->NAME;		\
174 	} while (/*CONSTCOND*/0)
175 
176 #define	LIBELF_COPY_S32(DST,SRC,NAME)	do {		\
177 		if ((uint64_t)(SRC)->NAME > INT_MAX ||	\
178 		    (uint64_t)(SRC)->NAME < INT_MIN) {	\
179 			LIBELF_SET_ERROR(RANGE, 0);	\
180 			return (0);			\
181 		}					\
182 		(DST)->NAME = (SRC)->NAME;		\
183 	} while (/*CONSTCOND*/0)
184 
185 
186 /*
187  * Function Prototypes.
188  */
189 
190 __BEGIN_DECLS
191 unsigned int _libelf_host_byteorder(void);
192 struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);
193 Elf	*_libelf_allocate_elf(void);
194 Elf_Scn	*_libelf_allocate_scn(Elf *_e, size_t _ndx);
195 Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
196 Elf	*_libelf_ar_open(Elf *_e, int _reporterror);
197 Elf	*_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
198 int	_libelf_ar_get_member(char *_s, size_t _sz, int _base, size_t *_ret);
199 Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
200 Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
201 unsigned long _libelf_checksum(Elf *_e, int _elfclass);
202 void	*_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
203 int	_libelf_falign(Elf_Type _t, int _elfclass);
204 size_t	_libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
205     size_t count);
206 int	(*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
207 	    (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap);
208 void	*_libelf_getphdr(Elf *_e, int _elfclass);
209 void	*_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
210 void	_libelf_init_elf(Elf *_e, Elf_Kind _kind);
211 int	_libelf_load_section_headers(Elf *e, void *ehdr);
212 int	_libelf_malign(Elf_Type _t, int _elfclass);
213 Elf	*_libelf_memory(char *_image, size_t _sz, int _reporterror);
214 size_t	_libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
215 void	*_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
216 Elf	*_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
217 struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);
218 Elf	*_libelf_release_elf(Elf *_e);
219 Elf_Scn	*_libelf_release_scn(Elf_Scn *_s);
220 int	_libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
221 int	_libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
222 int	_libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
223     size_t _shstrndx);
224 Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
225     unsigned int _encoding, int _elfclass, int _direction);
226 int	_libelf_xlate_shtype(uint32_t _sht);
227 __END_DECLS
228 
229 #endif	/* __LIBELF_H_ */
230