1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*	Copyright (c) 1988 AT&T	*/
22/*	  All Rights Reserved	*/
23
24/*
25 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26 * Use is subject to license terms.
27 * Copyright 2014 PALO, Richard. All rights reserved.
28 */
29
30#ifndef _LIBELF_H
31#define	_LIBELF_H
32
33#include <sys/types.h>
34#include <sys/elf.h>
35
36
37#ifdef	__cplusplus
38extern "C" {
39#endif
40
41
42#if defined(_ILP32) && (_FILE_OFFSET_BITS != 32)
43#error "large files are not supported by libelf"
44#endif
45
46typedef void	Elf_Void;
47
48/*
49 * Commands
50 */
51typedef enum {
52	ELF_C_NULL = 0,	/* must be first, 0 */
53	ELF_C_READ,
54	ELF_C_WRITE,
55	ELF_C_CLR,
56	ELF_C_SET,
57	ELF_C_FDDONE,
58	ELF_C_FDREAD,
59	ELF_C_RDWR,
60	ELF_C_WRIMAGE,
61	ELF_C_IMAGE,
62	ELF_C_NUM	/* must be last */
63} Elf_Cmd;
64
65
66/*
67 * Flags
68 */
69#define	ELF_F_DIRTY	0x1
70#define	ELF_F_LAYOUT	0x4
71
72
73/*
74 * File types
75 */
76typedef enum {
77	ELF_K_NONE = 0,	/* must be first, 0 */
78	ELF_K_AR,
79	ELF_K_COFF,
80	ELF_K_ELF,
81	ELF_K_NUM	/* must be last */
82} Elf_Kind;
83
84
85/*
86 * Translation types
87 */
88typedef enum {
89	ELF_T_BYTE = 0,	/* must be first, 0 */
90	ELF_T_ADDR,
91	ELF_T_DYN,
92	ELF_T_EHDR,
93	ELF_T_HALF,
94	ELF_T_OFF,
95	ELF_T_PHDR,
96	ELF_T_RELA,
97	ELF_T_REL,
98	ELF_T_SHDR,
99	ELF_T_SWORD,
100	ELF_T_SYM,
101	ELF_T_WORD,
102	ELF_T_VDEF,
103	ELF_T_VNEED,
104	ELF_T_SXWORD,
105	ELF_T_XWORD,
106	ELF_T_SYMINFO,
107	ELF_T_NOTE,
108	ELF_T_MOVE,
109	ELF_T_MOVEP,
110	ELF_T_CAP,
111	ELF_T_NUM	/* must be last */
112} Elf_Type;
113
114
115typedef struct Elf	Elf;
116typedef struct Elf_Scn	Elf_Scn;
117
118
119/*
120 * Archive member header
121 */
122typedef struct {
123	char		*ar_name;
124	time_t		ar_date;
125	uid_t		ar_uid;
126	gid_t		ar_gid;
127	mode_t		ar_mode;
128	off_t		ar_size;
129	char		*ar_rawname;
130} Elf_Arhdr;
131
132
133/*
134 * Archive symbol table
135 */
136typedef struct {
137	char		*as_name;
138	size_t		as_off;
139	unsigned long	as_hash;
140} Elf_Arsym;
141
142
143/*
144 * Data descriptor
145 */
146typedef struct {
147	Elf_Void	*d_buf;
148	Elf_Type	d_type;
149	size_t		d_size;
150	off_t		d_off;		/* offset into section */
151	size_t		d_align;	/* alignment in section */
152	unsigned	d_version;	/* elf version */
153} Elf_Data;
154
155
156/*
157 * Function declarations
158 */
159Elf		*elf_begin(int, Elf_Cmd, Elf *);
160int		elf_cntl(Elf *, Elf_Cmd);
161int		elf_end(Elf *);
162const char	*elf_errmsg(int);
163int		elf_errno(void);
164void		elf_fill(int);
165unsigned	elf_flagdata(Elf_Data *, Elf_Cmd, unsigned);
166unsigned	elf_flagehdr(Elf *, Elf_Cmd,  unsigned);
167unsigned	elf_flagelf(Elf *, Elf_Cmd, unsigned);
168unsigned	elf_flagphdr(Elf *, Elf_Cmd, unsigned);
169unsigned	elf_flagscn(Elf_Scn *, Elf_Cmd, unsigned);
170unsigned	elf_flagshdr(Elf_Scn *, Elf_Cmd, unsigned);
171size_t		elf32_fsize(Elf_Type, size_t, unsigned);
172Elf_Arhdr	*elf_getarhdr(Elf *);
173Elf_Arsym	*elf_getarsym(Elf *, size_t *);
174off_t		elf_getbase(Elf *);
175Elf_Data	*elf_getdata(Elf_Scn *, Elf_Data *);
176Elf32_Ehdr	*elf32_getehdr(Elf *);
177char		*elf_getident(Elf *, size_t *);
178Elf32_Phdr	*elf32_getphdr(Elf *);
179Elf_Scn		*elf_getscn(Elf *elf, size_t);
180Elf32_Shdr	*elf32_getshdr(Elf_Scn *);
181int		elf_getphnum(Elf *, size_t *);
182int		elf_getphdrnum(Elf *, size_t *);
183int		elf_getshnum(Elf *, size_t *);
184int		elf_getshdrnum(Elf *, size_t *);
185int		elf_getshstrndx(Elf *, size_t *);
186int		elf_getshdrstrndx(Elf *, size_t *);
187unsigned long	elf_hash(const char *);
188uint_t		elf_sys_encoding(void);
189long		elf32_checksum(Elf *);
190Elf_Kind	elf_kind(Elf *);
191Elf		*elf_memory(char *, size_t);
192size_t		elf_ndxscn(Elf_Scn *);
193Elf_Data	*elf_newdata(Elf_Scn *);
194Elf32_Ehdr	*elf32_newehdr(Elf *);
195Elf32_Phdr	*elf32_newphdr(Elf *, size_t);
196Elf_Scn		*elf_newscn(Elf *);
197Elf_Scn		*elf_nextscn(Elf *, Elf_Scn *);
198Elf_Cmd		elf_next(Elf *);
199size_t		elf_rand(Elf *, size_t);
200Elf_Data	*elf_rawdata(Elf_Scn *, Elf_Data *);
201char		*elf_rawfile(Elf *, size_t *);
202char		*elf_strptr(Elf *, size_t, size_t);
203off_t		elf_update(Elf *, Elf_Cmd);
204unsigned	elf_version(unsigned);
205Elf_Data	*elf32_xlatetof(Elf_Data *, const Elf_Data *, unsigned);
206Elf_Data	*elf32_xlatetom(Elf_Data *, const Elf_Data *, unsigned);
207
208#if defined(_LP64) || defined(_LONGLONG_TYPE)
209size_t		elf64_fsize(Elf_Type, size_t, unsigned);
210Elf64_Ehdr	*elf64_getehdr(Elf *);
211Elf64_Phdr	*elf64_getphdr(Elf *);
212Elf64_Shdr	*elf64_getshdr(Elf_Scn *);
213long		elf64_checksum(Elf *);
214Elf64_Ehdr	*elf64_newehdr(Elf *);
215Elf64_Phdr	*elf64_newphdr(Elf *, size_t);
216Elf_Data	*elf64_xlatetof(Elf_Data *, const Elf_Data *, unsigned);
217Elf_Data	*elf64_xlatetom(Elf_Data *, const Elf_Data *, unsigned);
218#endif /* (defined(_LP64) || defined(_LONGLONG_TYPE) */
219
220#ifdef	__cplusplus
221}
222#endif
223
224#endif	/* _LIBELF_H */
225