_libelf.h (4484:7c56a6c9c265) _libelf.h (8349:931ef19535e0)
1/*-
2 * Copyright (c) 2006 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/lib/libelf/_libelf.h,v 1.2 2006/12/25 02:22:22 jkoshy Exp $
27 */
28
29#ifndef __LIBELF_H_
30#define __LIBELF_H_
31
32#include "elf_queue.h"
1/*-
2 * Copyright (c) 2006 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/lib/libelf/_libelf.h,v 1.2 2006/12/25 02:22:22 jkoshy Exp $
27 */
28
29#ifndef __LIBELF_H_
30#define __LIBELF_H_
31
32#include "elf_queue.h"
33#include "libelf.h"
33
34#ifndef NULL
35#define NULL ((void *) 0)
36#endif
37
38/*
39 * Library-private data structures.
40 */
41
42#define LIBELF_MSG_SIZE 256
43
44struct _libelf_globals {
45 int libelf_arch;
46 unsigned int libelf_byteorder;
47 int libelf_class;
48 int libelf_error;
49 int libelf_fillchar;
50 unsigned int libelf_version;
51 char libelf_msg[LIBELF_MSG_SIZE];
52};
53
54struct _libelf_globals *_libelf_private();
55
56#define LIBELF_PRIVATE(N) (_libelf_private()->libelf_##N)
57
58#define LIBELF_ELF_ERROR_MASK 0xFF
59#define LIBELF_OS_ERROR_SHIFT 8
60
61#define LIBELF_SET_ERROR(E, O) do { \
62 LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)| \
63 ((O) << LIBELF_OS_ERROR_SHIFT)); \
64 } while (0)
65
66#define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U)
67
68/*
69 * Flags for library internal use. These use the upper 16 bits of a
70 * flags field.
71 */
72#define LIBELF_F_MALLOCED 0x010000 /* whether data was malloc'ed */
73#define LIBELF_F_MMAP 0x020000 /* whether e_rawfile was mmap'ed */
74#define LIBELF_F_SHDRS_LOADED 0x040000 /* whether all shdrs were read in */
75
76struct _Elf {
77 int e_activations; /* activation count */
78 Elf_Arhdr *e_arhdr; /* header for archive members */
79 unsigned int e_byteorder; /* ELFDATA* */
80 int e_class; /* ELFCLASS* */
81 Elf_Cmd e_cmd; /* ELF_C_* used at creation time */
82 int e_fd; /* associated file descriptor */
83 unsigned int e_flags; /* ELF_F_*, LIBELF_F_* flags */
84 Elf_Kind e_kind; /* ELF_K_* */
85 Elf *e_parent; /* non-NULL for archive members */
86 char *e_rawfile; /* uninterpreted bytes */
87 size_t e_rawsize; /* size of uninterpreted bytes */
88 unsigned int e_version; /* file version */
89
90 union {
91 struct { /* ar(1) archives */
92 off_t e_next; /* set by elf_rand()/elf_next() */
93 int e_nchildren;
94 char *e_rawstrtab; /* file name strings */
95 size_t e_rawstrtabsz;
96 char *e_rawsymtab; /* symbol table */
97 size_t e_rawsymtabsz;
98 Elf_Arsym *e_symtab;
99 size_t e_symtabsz;
100 } e_ar;
101 struct { /* regular ELF files */
102 union {
103 Elf32_Ehdr *e_ehdr32;
104 Elf64_Ehdr *e_ehdr64;
105 } e_ehdr;
106 union {
107 Elf32_Phdr *e_phdr32;
108 Elf64_Phdr *e_phdr64;
109 } e_phdr;
110 STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */
111 size_t e_nphdr; /* number of Phdr entries */
112 size_t e_nscn; /* number of sections */
113 size_t e_strndx; /* string table section index */
114 } e_elf;
115 } e_u;
116};
117
118struct _Elf_Scn {
119 union {
120 Elf32_Shdr s_shdr32;
121 Elf64_Shdr s_shdr64;
122 } s_shdr;
123 STAILQ_HEAD(, _Elf_Data) s_data; /* list of Elf_Data descriptors */
124 STAILQ_HEAD(, _Elf_Data) s_rawdata; /* raw data for this section */
125 STAILQ_ENTRY(_Elf_Scn) s_next;
126 struct _Elf *s_elf; /* parent ELF descriptor */
127 unsigned int s_flags; /* flags for the section as a whole */
128 size_t s_ndx; /* index# for this section */
129 uint64_t s_offset; /* managed by elf_update() */
130 uint64_t s_rawoff; /* original offset in the file */
131 uint64_t s_size; /* managed by elf_update() */
132};
133
134
135enum {
136 ELF_TOFILE,
137 ELF_TOMEMORY
138};
139
140#define LIBELF_COPY_U32(DST,SRC,NAME) do { \
141 if ((SRC)->NAME > UINT_MAX) { \
142 LIBELF_SET_ERROR(RANGE, 0); \
143 return (0); \
144 } \
145 (DST)->NAME = (SRC)->NAME; \
146 } while (0)
147
148#define LIBELF_COPY_S32(DST,SRC,NAME) do { \
149 if ((SRC)->NAME > INT_MAX || \
150 (SRC)->NAME < INT_MIN) { \
151 LIBELF_SET_ERROR(RANGE, 0); \
152 return (0); \
153 } \
154 (DST)->NAME = (SRC)->NAME; \
155 } while (0)
156
157
158/*
159 * Prototypes
160 */
161
162Elf_Data *_libelf_allocate_data(Elf_Scn *_s);
163Elf *_libelf_allocate_elf(void);
164Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx);
165Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
166Elf *_libelf_ar_open(Elf *_e);
167Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
168Elf_Arsym *_libelf_ar_process_symtab(Elf *_ar, size_t *_dst);
169unsigned long _libelf_checksum(Elf *_e, int _elfclass);
170void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
171int _libelf_falign(Elf_Type _t, int _elfclass);
172size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
173 size_t count);
174void (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
175 (char *_dst, char *_src, size_t _cnt, int _byteswap);
176void *_libelf_getphdr(Elf *_e, int _elfclass);
177void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
178void _libelf_init_elf(Elf *_e, Elf_Kind _kind);
179int _libelf_malign(Elf_Type _t, int _elfclass);
180size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
181void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
182Elf_Data *_libelf_release_data(Elf_Data *_d);
183Elf *_libelf_release_elf(Elf *_e);
184Elf_Scn *_libelf_release_scn(Elf_Scn *_s);
185int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
186int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
187int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
188 size_t _shstrndx);
189Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
190 unsigned int _encoding, int _elfclass, int _direction);
191int _libelf_xlate_shtype(uint32_t _sht);
192
193#endif /* __LIBELF_H_ */
34
35#ifndef NULL
36#define NULL ((void *) 0)
37#endif
38
39/*
40 * Library-private data structures.
41 */
42
43#define LIBELF_MSG_SIZE 256
44
45struct _libelf_globals {
46 int libelf_arch;
47 unsigned int libelf_byteorder;
48 int libelf_class;
49 int libelf_error;
50 int libelf_fillchar;
51 unsigned int libelf_version;
52 char libelf_msg[LIBELF_MSG_SIZE];
53};
54
55struct _libelf_globals *_libelf_private();
56
57#define LIBELF_PRIVATE(N) (_libelf_private()->libelf_##N)
58
59#define LIBELF_ELF_ERROR_MASK 0xFF
60#define LIBELF_OS_ERROR_SHIFT 8
61
62#define LIBELF_SET_ERROR(E, O) do { \
63 LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)| \
64 ((O) << LIBELF_OS_ERROR_SHIFT)); \
65 } while (0)
66
67#define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U)
68
69/*
70 * Flags for library internal use. These use the upper 16 bits of a
71 * flags field.
72 */
73#define LIBELF_F_MALLOCED 0x010000 /* whether data was malloc'ed */
74#define LIBELF_F_MMAP 0x020000 /* whether e_rawfile was mmap'ed */
75#define LIBELF_F_SHDRS_LOADED 0x040000 /* whether all shdrs were read in */
76
77struct _Elf {
78 int e_activations; /* activation count */
79 Elf_Arhdr *e_arhdr; /* header for archive members */
80 unsigned int e_byteorder; /* ELFDATA* */
81 int e_class; /* ELFCLASS* */
82 Elf_Cmd e_cmd; /* ELF_C_* used at creation time */
83 int e_fd; /* associated file descriptor */
84 unsigned int e_flags; /* ELF_F_*, LIBELF_F_* flags */
85 Elf_Kind e_kind; /* ELF_K_* */
86 Elf *e_parent; /* non-NULL for archive members */
87 char *e_rawfile; /* uninterpreted bytes */
88 size_t e_rawsize; /* size of uninterpreted bytes */
89 unsigned int e_version; /* file version */
90
91 union {
92 struct { /* ar(1) archives */
93 off_t e_next; /* set by elf_rand()/elf_next() */
94 int e_nchildren;
95 char *e_rawstrtab; /* file name strings */
96 size_t e_rawstrtabsz;
97 char *e_rawsymtab; /* symbol table */
98 size_t e_rawsymtabsz;
99 Elf_Arsym *e_symtab;
100 size_t e_symtabsz;
101 } e_ar;
102 struct { /* regular ELF files */
103 union {
104 Elf32_Ehdr *e_ehdr32;
105 Elf64_Ehdr *e_ehdr64;
106 } e_ehdr;
107 union {
108 Elf32_Phdr *e_phdr32;
109 Elf64_Phdr *e_phdr64;
110 } e_phdr;
111 STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */
112 size_t e_nphdr; /* number of Phdr entries */
113 size_t e_nscn; /* number of sections */
114 size_t e_strndx; /* string table section index */
115 } e_elf;
116 } e_u;
117};
118
119struct _Elf_Scn {
120 union {
121 Elf32_Shdr s_shdr32;
122 Elf64_Shdr s_shdr64;
123 } s_shdr;
124 STAILQ_HEAD(, _Elf_Data) s_data; /* list of Elf_Data descriptors */
125 STAILQ_HEAD(, _Elf_Data) s_rawdata; /* raw data for this section */
126 STAILQ_ENTRY(_Elf_Scn) s_next;
127 struct _Elf *s_elf; /* parent ELF descriptor */
128 unsigned int s_flags; /* flags for the section as a whole */
129 size_t s_ndx; /* index# for this section */
130 uint64_t s_offset; /* managed by elf_update() */
131 uint64_t s_rawoff; /* original offset in the file */
132 uint64_t s_size; /* managed by elf_update() */
133};
134
135
136enum {
137 ELF_TOFILE,
138 ELF_TOMEMORY
139};
140
141#define LIBELF_COPY_U32(DST,SRC,NAME) do { \
142 if ((SRC)->NAME > UINT_MAX) { \
143 LIBELF_SET_ERROR(RANGE, 0); \
144 return (0); \
145 } \
146 (DST)->NAME = (SRC)->NAME; \
147 } while (0)
148
149#define LIBELF_COPY_S32(DST,SRC,NAME) do { \
150 if ((SRC)->NAME > INT_MAX || \
151 (SRC)->NAME < INT_MIN) { \
152 LIBELF_SET_ERROR(RANGE, 0); \
153 return (0); \
154 } \
155 (DST)->NAME = (SRC)->NAME; \
156 } while (0)
157
158
159/*
160 * Prototypes
161 */
162
163Elf_Data *_libelf_allocate_data(Elf_Scn *_s);
164Elf *_libelf_allocate_elf(void);
165Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx);
166Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
167Elf *_libelf_ar_open(Elf *_e);
168Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
169Elf_Arsym *_libelf_ar_process_symtab(Elf *_ar, size_t *_dst);
170unsigned long _libelf_checksum(Elf *_e, int _elfclass);
171void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
172int _libelf_falign(Elf_Type _t, int _elfclass);
173size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
174 size_t count);
175void (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
176 (char *_dst, char *_src, size_t _cnt, int _byteswap);
177void *_libelf_getphdr(Elf *_e, int _elfclass);
178void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
179void _libelf_init_elf(Elf *_e, Elf_Kind _kind);
180int _libelf_malign(Elf_Type _t, int _elfclass);
181size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
182void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
183Elf_Data *_libelf_release_data(Elf_Data *_d);
184Elf *_libelf_release_elf(Elf *_e);
185Elf_Scn *_libelf_release_scn(Elf_Scn *_s);
186int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
187int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
188int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
189 size_t _shstrndx);
190Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
191 unsigned int _encoding, int _elfclass, int _direction);
192int _libelf_xlate_shtype(uint32_t _sht);
193
194#endif /* __LIBELF_H_ */