xref: /freebsd/lib/libkldelf/kldelf.h (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2000, Boris Popov
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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *    This product includes software developed by Boris Popov.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef _KLDELF_H_
36 #define _KLDELF_H_
37 
38 #include <sys/linker_set.h>
39 #include <stdbool.h>
40 
41 #define EF_CLOSE(ef) \
42     (ef)->ef_ops->close((ef)->ef_ef)
43 #define EF_SEG_READ_REL(ef, address, len, dest) \
44     (ef)->ef_ops->seg_read_rel((ef)->ef_ef, address, len, dest)
45 #define EF_SEG_READ_STRING(ef, address, len, dest) \
46     (ef)->ef_ops->seg_read_string((ef)->ef_ef, address, len, dest)
47 #define EF_SYMADDR(ef, symidx) \
48     (ef)->ef_ops->symaddr((ef)->ef_ef, symidx)
49 #define EF_LOOKUP_SET(ef, name, startp, stopp, countp) \
50     (ef)->ef_ops->lookup_set((ef)->ef_ef, name, startp, stopp, countp)
51 #define EF_LOOKUP_SYMBOL(ef, name, sym, see_local) \
52     (ef)->ef_ops->lookup_symbol((ef)->ef_ef, name, sym, see_local)
53 
54 /* XXX, should have a different name. */
55 typedef struct ef_file *elf_file_t;
56 
57 /* FreeBSD's headers define additional typedef's for ELF structures. */
58 typedef Elf64_Size GElf_Size;
59 typedef Elf64_Hashelt GElf_Hashelt;
60 
61 struct elf_file;
62 
63 struct elf_file_ops {
64 	void (*close)(elf_file_t ef);
65 	int (*seg_read_rel)(elf_file_t ef, GElf_Addr address, size_t len,
66 	    void *dest);
67 	int (*seg_read_string)(elf_file_t ef, GElf_Addr address, size_t len,
68 	    char *dest);
69 	GElf_Addr (*symaddr)(elf_file_t ef, GElf_Size symidx);
70 	int (*lookup_set)(elf_file_t ef, const char *name, GElf_Addr *startp,
71 	    GElf_Addr *stopp, long *countp);
72 	int (*lookup_symbol)(elf_file_t ef, const char *name, GElf_Sym **sym,
73 	    bool see_local);
74 };
75 
76 typedef int (elf_reloc_t)(struct elf_file *ef, const void *reldata,
77     Elf_Type reltype, GElf_Addr relbase, GElf_Addr dataoff, size_t len,
78     void *dest);
79 
80 struct elf_reloc_data {
81 	unsigned char class;
82 	unsigned char data;
83 	GElf_Half machine;
84 	elf_reloc_t *reloc;
85 };
86 
87 #define	ELF_RELOC(_class, _data, _machine, _reloc)			\
88 	static struct elf_reloc_data __CONCAT(elf_reloc_data_, __LINE__) = { \
89 	    .class = (_class),						\
90 	    .data = (_data),						\
91 	    .machine = (_machine),					\
92 	    .reloc = (_reloc)						\
93 	};								\
94 	DATA_SET(elf_reloc, __CONCAT(elf_reloc_data_, __LINE__))
95 
96 struct elf_file {
97 	elf_file_t ef_ef;
98 	struct elf_file_ops *ef_ops;
99 	const char *ef_filename;
100 	Elf *ef_elf;
101 	elf_reloc_t *ef_reloc;
102 	GElf_Ehdr ef_hdr;
103 	size_t ef_pointer_size;
104 	int ef_fd;
105 };
106 
107 #define	elf_machine(ef)		((ef)->ef_hdr.e_machine)
108 #define	elf_class(ef)		((ef)->ef_hdr.e_ident[EI_CLASS])
109 #define	elf_encoding(ef)	((ef)->ef_hdr.e_ident[EI_DATA])
110 
111 /*
112  * "Generic" versions of module metadata structures.
113  */
114 struct Gmod_depend {
115 	int	md_ver_minimum;
116 	int	md_ver_preferred;
117 	int	md_ver_maximum;
118 };
119 
120 struct Gmod_version {
121 	int	mv_version;
122 };
123 
124 struct Gmod_metadata {
125 	int		md_version;	/* structure version MDTV_* */
126 	int		md_type;	/* type of entry MDT_* */
127 	GElf_Addr	md_data;	/* specific data */
128 	GElf_Addr	md_cval;	/* common string label */
129 };
130 
131 struct Gmod_pnp_match_info
132 {
133 	GElf_Addr	descr;	/* Description of the table */
134 	GElf_Addr	bus;	/* Name of the bus for this table */
135 	GElf_Addr	table;	/* Pointer to pnp table */
136 	int entry_len;		/* Length of each entry in the table (may be */
137 				/*   longer than descr describes). */
138 	int num_entry;		/* Number of entries in the table */
139 };
140 
141 __BEGIN_DECLS
142 
143 /*
144  * Attempt to parse an open ELF file as either an executable or DSO
145  * (ef_open) or an object file (ef_obj_open).  On success, these
146  * routines initialize the 'ef_ef' and 'ef_ops' members of 'ef'.
147  */
148 int ef_open(struct elf_file *ef, int verbose);
149 int ef_obj_open(struct elf_file *ef, int verbose);
150 
151 /*
152  * Direct operations on an ELF file regardless of type.  Many of these
153  * use libelf.
154  */
155 
156 /*
157  * Open an ELF file with libelf.  Populates fields other than ef_ef
158  * and ef_ops in '*efile'.
159  */
160 int	elf_open_file(struct elf_file *efile, const char *filename,
161     int verbose);
162 
163 /* Close an ELF file. */
164 void	elf_close_file(struct elf_file *efile);
165 
166 /* Is an ELF file the same architecture as hdr? */
167 bool	elf_compatible(struct elf_file *efile, const GElf_Ehdr *hdr);
168 
169 /* The size of a single object of 'type'. */
170 size_t	elf_object_size(struct elf_file *efile, Elf_Type type);
171 
172 /* The size of a pointer in architecture of 'efile'. */
173 size_t	elf_pointer_size(struct elf_file *efile);
174 
175 /*
176  * Read and convert an array of a data type from an ELF file.  This is
177  * a wrapper around gelf_xlatetom() which reads an array of raw ELF
178  * objects from the file and converts them into host structures using
179  * native endianness.  The data is returned in a dynamically-allocated
180  * buffer.
181  */
182 int	elf_read_data(struct elf_file *efile, Elf_Type type, off_t offset,
183     size_t len, void **out);
184 
185 /* Reads "raw" data from an ELF file without any translation. */
186 int	elf_read_raw_data(struct elf_file *efile, off_t offset, void *dst,
187     size_t len);
188 
189 /*
190  * A wrapper around elf_read_raw_data which returns the data in a
191  * dynamically-allocated buffer.
192  */
193 int	elf_read_raw_data_alloc(struct elf_file *efile, off_t offset,
194     size_t len, void **out);
195 
196 /* Reads a single string at the given offset from an ELF file. */
197 int	elf_read_raw_string(struct elf_file *efile, off_t offset, char *dst,
198     size_t len);
199 
200 /*
201  * Read relocated data from an ELF file and return it in a
202  * dynamically-allocated buffer.  Note that no translation
203  * (byte-swapping for endianness, 32-vs-64) is performed on the
204  * returned data, but any ELF relocations which affect the contents
205  * are applied to the returned data.  The address parameter gives the
206  * address of the data buffer if the ELF file were loaded into memory
207  * rather than a direct file offset.
208  */
209 int	elf_read_relocated_data(struct elf_file *efile, GElf_Addr address,
210     size_t len, void **buf);
211 
212 /*
213  * Read the program headers from an ELF file and return them in a
214  * dynamically-allocated array of GElf_Phdr objects.
215  */
216 int	elf_read_phdrs(struct elf_file *efile, size_t *nphdrp,
217     GElf_Phdr **phdrp);
218 
219 /*
220  * Read the section headers from an ELF file and return them in a
221  * dynamically-allocated array of GElf_Shdr objects.
222  */
223 int	elf_read_shdrs(struct elf_file *efile, size_t *nshdrp,
224     GElf_Shdr **shdrp);
225 
226 /*
227  * Read the dynamic table from a section of an ELF file into a
228  * dynamically-allocated array of GElf_Dyn objects.
229  */
230 int	elf_read_dynamic(struct elf_file *efile, int section_index, size_t *ndynp,
231     GElf_Dyn **dynp);
232 
233 /*
234  * Read a symbol table from a section of an ELF file into a
235  * dynamically-allocated array of GElf_Sym objects.
236  */
237 int	elf_read_symbols(struct elf_file *efile, int section_index,
238     size_t *nsymp, GElf_Sym **symp);
239 
240 /*
241  * Read a string table described by a section header of an ELF file
242  * into a dynamically-allocated buffer.
243  */
244 int	elf_read_string_table(struct elf_file *efile, const GElf_Shdr *shdr,
245     long *strcnt, char **strtab);
246 
247 /*
248  * Read a table of relocation objects from a section of an ELF file
249  * into a dynamically-allocated array of GElf_Rel objects.
250  */
251 int	elf_read_rel(struct elf_file *efile, int section_index, long *nrelp,
252     GElf_Rel **relp);
253 
254 /*
255  * Read a table of relocation-with-addend objects from a section of an
256  * ELF file into a dynamically-allocated array of GElf_Rela objects.
257  */
258 int	elf_read_rela(struct elf_file *efile, int section_index, long *nrelap,
259     GElf_Rela **relap);
260 
261 /*
262  * Read a string from an ELF file and return it in the provided
263  * buffer.  If the string is longer than the buffer, this fails with
264  * EFAULT.  The address parameter gives the address of the data buffer
265  * if the ELF file were loaded into memory rather than a direct file
266  * offset.
267  */
268 int	elf_read_string(struct elf_file *efile, GElf_Addr address, void *dst,
269     size_t len);
270 
271 /* Return the address extracted from a target pointer stored at 'p'. */
272 GElf_Addr elf_address_from_pointer(struct elf_file *efile, const void *p);
273 
274 /*
275  * Read a linker set and return an array of addresses extracted from the
276  * relocated pointers in the linker set.
277  */
278 int	elf_read_linker_set(struct elf_file *efile, const char *name,
279     GElf_Addr **buf, long *countp);
280 
281 /*
282  * Read and convert a target 'struct mod_depend' into a host
283  * 'struct Gmod_depend'.
284  */
285 int	elf_read_mod_depend(struct elf_file *efile, GElf_Addr addr,
286     struct Gmod_depend *mdp);
287 
288 /*
289  * Read and convert a target 'struct mod_version' into a host
290  * 'struct Gmod_version'.
291  */
292 int	elf_read_mod_version(struct elf_file *efile, GElf_Addr addr,
293     struct Gmod_version *mdv);
294 
295 /*
296  * Read and convert a target 'struct mod_metadata' into a host
297  * 'struct Gmod_metadata'.
298  */
299 int	elf_read_mod_metadata(struct elf_file *efile, GElf_Addr addr,
300     struct Gmod_metadata *md);
301 
302 /*
303  * Read and convert a target 'struct mod_pnp_match_info' into a host
304  * 'struct Gmod_pnp_match_info'.
305  */
306 int	elf_read_mod_pnp_match_info(struct elf_file *efile, GElf_Addr addr,
307     struct Gmod_pnp_match_info *pnp);
308 
309 /*
310  * Apply relocations to the values obtained from the file. `relbase' is the
311  * target relocation address of the section, and `dataoff/len' is the region
312  * that is to be relocated, and has been copied to *dest
313  */
314 int	elf_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
315     GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest);
316 
317 /*
318  * Find the symbol with the specified symbol name 'name' within the given
319  * 'efile'. 0 is returned when such a symbol is found, otherwise ENOENT is
320  * returned.
321  */
322 int	elf_lookup_symbol(struct elf_file *efile, const char *name,
323     GElf_Sym **sym, bool see_local);
324 
325 __END_DECLS
326 
327 #endif /* _KLDELF_H_*/
328