Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4973 right-hear 1
/*
2
private.h - private definitions for libelf.
3
Copyright (C) 1995 - 1998 Michael Riepe 
4
 
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
9
 
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
Library General Public License for more details.
14
 
15
You should have received a copy of the GNU Library General Public
16
License along with this library; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
*/
19
 
20
/* @(#) $Id: private.h,v 1.10 1998/11/27 21:25:19 michael Exp $ */
21
 
22
#ifndef _PRIVATE_H
23
#define _PRIVATE_H
24
 
25
#define __LIBELF_INTERNAL__ 1
26
 
27
#if HAVE_CONFIG_H
28
# include 
29
#endif /* HAVE_CONFIG_H */
30
 
31
#include 
32
 
33
#if STDC_HEADERS
34
# include 
35
# include 
36
#else /* STDC_HEADERS */
37
extern char *malloc(), *realloc();
38
extern void free(), bcopy();
39
extern int strcmp(), strncmp(), memcmp();
40
extern void *memcpy(), *memmove(), *memset();
41
#endif /* STDC_HEADERS */
42
 
43
#if HAVE_UNISTD_H
44
# include 
45
#else /* HAVE_UNISTD_H */
46
extern int read(), write();
47
extern off_t lseek();
48
#endif /* HAVE_UNISTD_H */
49
 
50
#if !HAVE_MEMCMP
51
# define memcmp	strncmp
52
#endif /* !HAVE_MEMCMP */
53
#if !HAVE_MEMCPY
54
# define memcpy(d,s,n)	bcopy(s,d,n)
55
#endif /* !HAVE_MEMCPY */
56
#if !HAVE_MEMMOVE
57
# define memmove(d,s,n)	bcopy(s,d,n)
58
#endif /* !HAVE_MEMMOVE */
59
 
60
#if !HAVE_MEMSET
61
# define memset _elf_memset
62
extern void *_elf_memset();
63
#endif /* !HAVE_MEMSET */
64
 
65
#if HAVE_STRUCT_NLIST_DECLARATION
66
# define nlist __override_nlist_declaration
67
#endif /* HAVE_STRUCT_NLIST_DECLARATION */
68
 
69
#if NEED_LINK_H
70
# include 
71
#endif /* NEED_LINK_H */
72
 
73
#include 
74
 
75
#if HAVE_STRUCT_NLIST_DECLARATION
76
# undef nlist
77
#endif /* HAVE_STRUCT_NLIST_DECLARATION */
78
 
79
typedef struct Scn_Data Scn_Data;
80
 
81
/*
82
 * ELF descriptor
83
 */
84
struct Elf {
85
    /* common */
86
    size_t	e_size;			/* file/member size */
87
    size_t	e_dsize;		/* size of memory image */
88
    Elf_Kind	e_kind;			/* kind of file */
89
    char*	e_data;			/* file/member data */
90
    char*	e_rawdata;		/* file/member raw data */
91
    size_t	e_idlen;		/* identifier size */
92
    int		e_fd;			/* file descriptor */
93
    unsigned	e_count;		/* activation count */
94
    /* archive members (still common) */
95
    Elf*	e_parent;		/* NULL if not an archive member */
96
    size_t	e_next;			/* 0 if not an archive member */
97
    size_t	e_base;			/* 0 if not an archive member */
98
    Elf*	e_link;			/* next archive member or NULL */
99
    Elf_Arhdr*	e_arhdr;		/* archive member header or NULL */
100
    /* archives */
101
    size_t	e_off;			/* current member offset (for elf_begin) */
102
    Elf*	e_members;		/* linked list of active archive members */
103
    char*	e_symtab;		/* archive symbol table */
104
    size_t	e_symlen;		/* length of archive symbol table */
105
    char*	e_strtab;		/* archive string table */
106
    size_t	e_strlen;		/* length of archive string table */
107
    /* ELF files */
108
    unsigned	e_class;		/* ELF class */
109
    unsigned	e_encoding;		/* ELF data encoding */
110
    unsigned	e_version;		/* ELF version */
111
    char*	e_ehdr;			/* ELF header */
112
    char*	e_phdr;			/* ELF program header table */
113
    size_t	e_phnum;		/* size of program header table */
114
    Elf_Scn*	e_scn_1;		/* first section */
115
    Elf_Scn*	e_scn_n;		/* last section */
116
    unsigned	e_elf_flags;		/* elf flags (ELF_F_*) */
117
    unsigned	e_ehdr_flags;		/* ehdr flags (ELF_F_*) */
118
    unsigned	e_phdr_flags;		/* phdr flags (ELF_F_*) */
119
    /* misc flags */
120
    unsigned	e_readable : 1;		/* file is readable */
121
    unsigned	e_writable : 1;		/* file is writable */
122
    unsigned	e_disabled : 1;		/* e_fd has been disabled */
123
    unsigned	e_cooked : 1;		/* e_data was modified */
124
    unsigned	e_free_syms : 1;	/* e_symtab is malloc'ed */
125
    unsigned	e_free_ehdr : 1;	/* e_ehdr is malloc'ed */
126
    unsigned	e_free_phdr : 1;	/* e_phdr is malloc'ed */
127
    unsigned	e_unmap_data : 1;	/* e_data is mmap'ed */
128
    unsigned	e_memory : 1;		/* created by elf_memory() */
129
    /* magic number for debugging */
130
    long	e_magic;
131
};
132
 
133
#define ELF_MAGIC	0x012b649e
134
 
135
#define INIT_ELF	{\
136
    /* e_size */	0,\
137
    /* e_dsize */	0,\
138
    /* e_kind */	ELF_K_NONE,\
139
    /* e_data */	NULL,\
140
    /* e_rawdata */	NULL,\
141
    /* e_idlen */	0,\
142
    /* e_fd */		-1,\
143
    /* e_count */	1,\
144
    /* e_parent */	NULL,\
145
    /* e_next */	0,\
146
    /* e_base */	0,\
147
    /* e_link */	NULL,\
148
    /* e_arhdr */	NULL,\
149
    /* e_off */		0,\
150
    /* e_members */	NULL,\
151
    /* e_symtab */	NULL,\
152
    /* e_symlen */	0,\
153
    /* e_strtab */	NULL,\
154
    /* e_strlen */	0,\
155
    /* e_class */	ELFCLASSNONE,\
156
    /* e_encoding */	ELFDATANONE,\
157
    /* e_version */	EV_NONE,\
158
    /* e_ehdr */	NULL,\
159
    /* e_phdr */	NULL,\
160
    /* e_phnum */	0,\
161
    /* e_scn_1 */	NULL,\
162
    /* e_scn_n */	NULL,\
163
    /* e_elf_flags */	0,\
164
    /* e_ehdr_flags */	0,\
165
    /* e_phdr_flags */	0,\
166
    /* e_readable */	0,\
167
    /* e_writable */	0,\
168
    /* e_disabled */	0,\
169
    /* e_cooked */	0,\
170
    /* e_free_syms */	0,\
171
    /* e_free_ehdr */	0,\
172
    /* e_free_phdr */	0,\
173
    /* e_unmap_data */	0,\
174
    /* e_memory */	0,\
175
    /* e_magic */	ELF_MAGIC\
176
}
177
 
178
/*
179
 * Section descriptor
180
 */
181
struct Elf_Scn {
182
    Elf_Scn*	s_link;			/* pointer to next Elf_Scn */
183
    Elf*	s_elf;			/* pointer to elf descriptor */
184
    size_t	s_index;		/* number of this section */
185
    unsigned	s_scn_flags;		/* section flags (ELF_F_*) */
186
    unsigned	s_shdr_flags;		/* shdr flags (ELF_F_*) */
187
    Scn_Data*	s_data_1;		/* first data buffer */
188
    Scn_Data*	s_data_n;		/* last data buffer */
189
    Scn_Data*	s_rawdata;		/* raw data buffer */
190
    /* data copied from shdr */
191
    unsigned	s_type;			/* section type */
192
    size_t	s_offset;		/* section offset */
193
    size_t	s_size;			/* section size */
194
    /* misc flags */
195
    unsigned	s_freeme : 1;		/* this Elf_Scn was malloc'ed */
196
    /* section header */
197
    union {
198
#if __LIBELF64
199
	Elf64_Shdr	u_shdr64;
200
#endif /* __LIBELF64 */
201
	Elf32_Shdr	u_shdr32;
202
    }		s_uhdr;
203
    /* magic number for debugging */
204
    long	s_magic;
205
};
206
#define s_shdr32	s_uhdr.u_shdr32
207
#define s_shdr64	s_uhdr.u_shdr64
208
 
209
#define SCN_MAGIC	0x012c747d
210
 
211
#define INIT_SCN	{\
212
    /* s_link */	NULL,\
213
    /* s_elf */		NULL,\
214
    /* s_index */	0,\
215
    /* s_scn_flags */	0,\
216
    /* s_shdr_flags */	0,\
217
    /* s_data_1 */	NULL,\
218
    /* s_data_n */	NULL,\
219
    /* s_rawdata */	NULL,\
220
    /* s_type */	SHT_NULL,\
221
    /* s_offset */	0,\
222
    /* s_size */	0,\
223
    /* s_freeme */	0,\
224
    /* s_uhdr */	{{0,}},\
225
    /* s_magic */	SCN_MAGIC\
226
}
227
 
228
/*
229
 * Data descriptor
230
 */
231
struct Scn_Data {
232
    Elf_Data	sd_data;		/* must be first! */
233
    Scn_Data*	sd_link;		/* pointer to next Scn_Data */
234
    char*	sd_memdata;		/* memory image of section */
235
    unsigned	sd_data_flags;		/* data flags (ELF_F_*) */
236
    /* misc flags */
237
    unsigned	sd_freeme : 1;		/* this Scn_Data was malloc'ed */
238
    unsigned	sd_free_data : 1;	/* sd_memdata is malloc'ed */
239
    /* magic number for debugging */
240
    long	sd_magic;
241
};
242
 
243
#define DATA_MAGIC	0x01072639
244
 
245
#define INIT_DATA	{\
246
    {\
247
    /* d_buf */		NULL,\
248
    /* d_type */	ELF_T_BYTE,\
249
    /* d_size */	0,\
250
    /* d_off */		0,\
251
    /* d_align */	0,\
252
    /* d_version */	EV_NONE\
253
    },\
254
    /* sd_link */	NULL,\
255
    /* sd_memdata */	NULL,\
256
    /* sd_data_flags */	0,\
257
    /* sd_freeme */	0,\
258
    /* sd_free_data */	0,\
259
    /* sd_magic */	DATA_MAGIC\
260
}
261
 
262
/*
263
 * Private status variables
264
 */
265
extern unsigned _elf_version;
266
extern int _elf_errno;
267
extern int _elf_fill;
268
 
269
/*
270
 * Private functions
271
 */
272
extern void *_elf_read __P((Elf*, void*, size_t, size_t));
273
extern void *_elf_mmap __P((Elf*));
274
extern int _elf_cook __P((Elf*));
275
extern char *_elf_getehdr __P((Elf*, unsigned));
276
extern Elf_Data *_elf_xlatetom(const Elf*, Elf_Data*, const Elf_Data*);
277
 
278
/*
279
 * Private data
280
 */
281
extern const Elf_Scn _elf_scn_init;
282
extern const Scn_Data _elf_data_init;
283
extern const Elf_Type _elf_scn_types[SHT_NUM];
284
extern const size_t _elf_fmsize[2][EV_CURRENT - EV_NONE][ELF_T_NUM][2];
285
 
286
/*
287
 * Access macros for _elf_fmsize[]
288
 */
289
#define _fmsize(c,v,t,w)	\
290
	(_elf_fmsize[(c)-ELFCLASS32][(v)-EV_NONE-1][(t)-ELF_T_BYTE][(w)])
291
#define _fsize(c,v,t)		_fmsize((c),(v),(t),1)
292
#define _msize(c,v,t)		_fmsize((c),(v),(t),0)
293
 
294
/*
295
 * Various checks
296
 */
297
#define valid_class(c)		((c) >= ELFCLASS32 && (c) <= ELFCLASS64)
298
#define valid_encoding(e)	((e) >= ELFDATA2LSB && (e) <= ELFDATA2MSB)
299
#define valid_version(v)	((v) > EV_NONE && (v) <= EV_CURRENT)
300
#define valid_type(t)		((t) >= ELF_T_BYTE && (t) < ELF_T_NUM)
301
#define valid_scntype(s)	((s) >= SHT_NULL && (s) < SHT_NUM)
302
 
303
/*
304
 * Error codes
305
 */
306
enum {
307
#define __err__(a,b)	a,
308
#include 		/* include constants from errors.h */
309
#undef __err__
310
ERROR_NUM
311
};
312
 
313
#define seterr(err)	(_elf_errno = (err))
314
 
315
/*
316
 * Sizes of data types (external representation)
317
 * These definitions should be in , but...
318
 */
319
#ifndef ELF32_FSZ_ADDR
320
# define ELF32_FSZ_ADDR		4
321
# define ELF32_FSZ_HALF		2
322
# define ELF32_FSZ_OFF		4
323
# define ELF32_FSZ_SWORD	4
324
# define ELF32_FSZ_WORD		4
325
#endif /* ELF32_FSZ_ADDR */
326
#ifndef ELF64_FSZ_ADDR
327
# define ELF64_FSZ_ADDR		8
328
# define ELF64_FSZ_HALF		2
329
# define ELF64_FSZ_OFF		8
330
# define ELF64_FSZ_SWORD	4
331
# define ELF64_FSZ_SXWORD	8
332
# define ELF64_FSZ_WORD		4
333
# define ELF64_FSZ_XWORD	8
334
#endif /* ELF64_FSZ_ADDR */
335
 
336
/*
337
 * Alignment
338
 */
339
#define _ELF32_ALIGN_PHDR	4
340
#define _ELF32_ALIGN_SHDR	4
341
#define _ELF64_ALIGN_PHDR	8
342
#define _ELF64_ALIGN_SHDR	8
343
 
344
/*
345
 * Debugging
346
 */
347
#if ENABLE_DEBUG
348
# include 
349
# if __STDC__
350
#  define elf_assert(x)	((void)((x)||__elf_assert(__FILE__,__LINE__,#x)))
351
# else /* __STDC__ */
352
#  define elf_assert(x)	((void)((x)||__elf_assert(__FILE__,__LINE__,"x")))
353
# endif /* __STDC__ */
354
# define __elf_assert(f,l,x)	(fprintf(stderr,\
355
	"%s:%u: libelf assertion failure: %s\n",(f),(l),(x)),abort(),0)
356
#else /* ENABLE_DEBUG */
357
# define elf_assert(x)	((void)0)
358
#endif /* ENABLE_DEBUG */
359
 
360
#endif /* _PRIVATE_H */