Project

General

Profile

Statistics
| Revision:

root / lab4 / .minix-src / include / libelf.h @ 14

History | View | Annotate | Download (7.75 KB)

1
/*        $NetBSD: libelf.h,v 1.4 2015/09/29 22:14:14 christos Exp $        */
2

    
3
/*-
4
 * Copyright (c) 2006,2008-2010 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 2366 2011-12-29 06:12:14Z jkoshy 
29
 */
30

    
31
#ifndef        _LIBELF_H_
32
#define        _LIBELF_H_
33

    
34
#if HAVE_NBTOOL_CONFIG_H
35
# include "nbtool_config.h"
36
#endif
37

    
38

    
39
#if HAVE_NBTOOL_CONFIG_H
40
# include <nbinclude/sys/exec_elf.h>
41
#elif defined(__NetBSD__) || defined(__minix)
42
# include <sys/types.h>
43
# include <sys/exec_elf.h>
44
#elif defined(__FreeBSD__)
45
# include <sys/types.h>
46
# include <sys/elf32.h>
47
# include <sys/elf64.h>
48
#else
49
# error "Unsupported platform"
50
#endif
51

    
52
/* Library private data structures */
53
typedef struct _Elf Elf;
54
typedef struct _Elf_Scn Elf_Scn;
55

    
56
/* File types */
57
typedef enum {
58
        ELF_K_NONE = 0,
59
        ELF_K_AR,        /* `ar' archives */
60
        ELF_K_COFF,        /* COFF files (unsupported) */
61
        ELF_K_ELF,        /* ELF files */
62
        ELF_K_NUM
63
} Elf_Kind;
64

    
65
#define        ELF_K_FIRST        ELF_K_NONE
66
#define        ELF_K_LAST        ELF_K_NUM
67

    
68
/* Data types */
69
typedef enum {
70
        ELF_T_ADDR,
71
        ELF_T_BYTE,
72
        ELF_T_CAP,
73
        ELF_T_DYN,
74
        ELF_T_EHDR,
75
        ELF_T_HALF,
76
        ELF_T_LWORD,
77
        ELF_T_MOVE,
78
        ELF_T_MOVEP,
79
        ELF_T_NOTE,
80
        ELF_T_OFF,
81
        ELF_T_PHDR,
82
        ELF_T_REL,
83
        ELF_T_RELA,
84
        ELF_T_SHDR,
85
        ELF_T_SWORD,
86
        ELF_T_SXWORD,
87
        ELF_T_SYMINFO,
88
        ELF_T_SYM,
89
        ELF_T_VDEF,
90
        ELF_T_VNEED,
91
        ELF_T_WORD,
92
        ELF_T_XWORD,
93
        ELF_T_GNUHASH,        /* GNU style hash tables. */
94
        ELF_T_NUM
95
} Elf_Type;
96

    
97
#define        ELF_T_FIRST        ELF_T_ADDR
98
#define        ELF_T_LAST        ELF_T_GNUHASH
99

    
100
/* Commands */
101
typedef enum {
102
        ELF_C_NULL = 0,
103
        ELF_C_CLR,
104
        ELF_C_FDDONE,
105
        ELF_C_FDREAD,
106
        ELF_C_RDWR,
107
        ELF_C_READ,
108
        ELF_C_SET,
109
        ELF_C_WRITE,
110
        ELF_C_NUM
111
} Elf_Cmd;
112

    
113
#define        ELF_C_FIRST        ELF_C_NULL
114
#define        ELF_C_LAST        ELF_C_NUM
115

    
116
/*
117
 * An `Elf_Data' structure describes data in an
118
 * ELF section.
119
 */
120
typedef struct _Elf_Data {
121
        /*
122
         * `Public' members that are part of the ELF(3) API.
123
         */
124
        uint64_t        d_align;
125
        void                *d_buf;
126
        uint64_t        d_off;
127
        uint64_t        d_size;
128
        Elf_Type        d_type;
129
        unsigned int        d_version;
130
} Elf_Data;
131

    
132
/*
133
 * An `Elf_Arhdr' structure describes an archive
134
 * header.
135
 */
136
typedef struct {
137
        time_t                ar_date;
138
        char                *ar_name;        /* archive member name */
139
        gid_t                ar_gid;
140
        mode_t                ar_mode;
141
        char                *ar_rawname;        /* 'raw' member name */
142
        size_t                ar_size;
143
        uid_t                ar_uid;
144

    
145
        /*
146
         * Members that are not part of the public API.
147
         */
148
        int                ar_flags;
149
} Elf_Arhdr;
150

    
151
/*
152
 * An `Elf_Arsym' describes an entry in the archive
153
 * symbol table.
154
 */
155
typedef struct {
156
        off_t                as_off;                /* byte offset to member's header */
157
        unsigned long        as_hash;        /* elf_hash() value for name */
158
        char                *as_name;         /* null terminated symbol name */
159
} Elf_Arsym;
160

    
161
/*
162
 * Error numbers.
163
 */
164

    
165
enum Elf_Error {
166
        ELF_E_NONE,        /* No error */
167
        ELF_E_ARCHIVE,        /* Malformed ar(1) archive */
168
        ELF_E_ARGUMENT,        /* Invalid argument */
169
        ELF_E_CLASS,        /* Mismatched ELF class */
170
        ELF_E_DATA,        /* Invalid data descriptor */
171
        ELF_E_HEADER,        /* Missing or malformed ELF header */
172
        ELF_E_IO,        /* I/O error */
173
        ELF_E_LAYOUT,        /* Layout constraint violation */
174
        ELF_E_MODE,        /* Wrong mode for ELF descriptor */
175
        ELF_E_RANGE,        /* Value out of range */
176
        ELF_E_RESOURCE,        /* Resource exhaustion */
177
        ELF_E_SECTION,        /* Invalid section descriptor */
178
        ELF_E_SEQUENCE,        /* API calls out of sequence */
179
        ELF_E_UNIMPL,        /* Feature is unimplemented */
180
        ELF_E_VERSION,        /* Unknown API version */
181
        ELF_E_NUM        /* Max error number */
182
};
183

    
184
/*
185
 * Flags defined by the API.
186
 */
187

    
188
#define        ELF_F_LAYOUT        0x001U        /* application will layout the file */
189
#define        ELF_F_DIRTY        0x002U        /* a section or ELF file is dirty */
190

    
191
/* ELF(3) API extensions. */
192
#define        ELF_F_ARCHIVE           0x100U /* archive creation */
193
#define        ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */
194

    
195
__BEGIN_DECLS
196
Elf                *elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
197
int                elf_cntl(Elf *_elf, Elf_Cmd _cmd);
198
int                elf_end(Elf *_elf);
199
const char        *elf_errmsg(int _error);
200
int                elf_errno(void);
201
void                elf_fill(int _fill);
202
unsigned int        elf_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd,
203
                        unsigned int _flags);
204
unsigned int        elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd,
205
                        unsigned int _flags);
206
unsigned int        elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
207
unsigned int        elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
208
unsigned int        elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
209
unsigned int        elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
210
unsigned int        elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
211
Elf_Arhdr        *elf_getarhdr(Elf *_elf);
212
Elf_Arsym        *elf_getarsym(Elf *_elf, size_t *_ptr);
213
off_t                elf_getbase(Elf *_elf);
214
Elf_Data        *elf_getdata(Elf_Scn *, Elf_Data *);
215
char                *elf_getident(Elf *_elf, size_t *_ptr);
216
int                elf_getphdrnum(Elf *_elf, size_t *_dst);
217
int                elf_getphnum(Elf *_elf, size_t *_dst);        /* Deprecated */
218
Elf_Scn                *elf_getscn(Elf *_elf, size_t _index);
219
int                elf_getshdrnum(Elf *_elf, size_t *_dst);
220
int                elf_getshnum(Elf *_elf, size_t *_dst);        /* Deprecated */
221
int                elf_getshdrstrndx(Elf *_elf, size_t *_dst);
222
int                elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */
223
unsigned long        elf_hash(const void *_name);
224
Elf_Kind        elf_kind(Elf *_elf);
225
Elf                *elf_memory(char *_image, size_t _size);
226
size_t                elf_ndxscn(Elf_Scn *_scn);
227
Elf_Data        *elf_newdata(Elf_Scn *_scn);
228
Elf_Scn                *elf_newscn(Elf *_elf);
229
Elf_Scn                *elf_nextscn(Elf *_elf, Elf_Scn *_scn);
230
Elf_Cmd                elf_next(Elf *_elf);
231
Elf                *elf_open(int _fd);
232
Elf                *elf_openmemory(char *_image, size_t _size);
233
off_t                elf_rand(Elf *_elf, off_t _off);
234
Elf_Data        *elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
235
char                *elf_rawfile(Elf *_elf, size_t *_size);
236
int                elf_setshstrndx(Elf *_elf, size_t _shnum);
237
char                *elf_strptr(Elf *_elf, size_t _section, size_t _offset);
238
off_t                elf_update(Elf *_elf, Elf_Cmd _cmd);
239
unsigned int        elf_version(unsigned int _version);
240

    
241
long                elf32_checksum(Elf *_elf);
242
size_t                elf32_fsize(Elf_Type _type, size_t _count,
243
                        unsigned int _version);
244
Elf32_Ehdr        *elf32_getehdr(Elf *_elf);
245
Elf32_Phdr        *elf32_getphdr(Elf *_elf);
246
Elf32_Shdr        *elf32_getshdr(Elf_Scn *_scn);
247
Elf32_Ehdr        *elf32_newehdr(Elf *_elf);
248
Elf32_Phdr        *elf32_newphdr(Elf *_elf, size_t _count);
249
Elf_Data        *elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
250
                        unsigned int _enc);
251
Elf_Data        *elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
252
                        unsigned int _enc);
253

    
254
long                elf64_checksum(Elf *_elf);
255
size_t                elf64_fsize(Elf_Type _type, size_t _count,
256
                        unsigned int _version);
257
Elf64_Ehdr        *elf64_getehdr(Elf *_elf);
258
Elf64_Phdr        *elf64_getphdr(Elf *_elf);
259
Elf64_Shdr        *elf64_getshdr(Elf_Scn *_scn);
260
Elf64_Ehdr        *elf64_newehdr(Elf *_elf);
261
Elf64_Phdr        *elf64_newphdr(Elf *_elf, size_t _count);
262
Elf_Data        *elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
263
                        unsigned int _enc);
264
Elf_Data        *elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
265
                        unsigned int _enc);
266
__END_DECLS
267

    
268
#endif        /* _LIBELF_H_ */