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 _EF_H_ 36 #define _EF_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 52 /* XXX, should have a different name. */ 53 typedef struct ef_file *elf_file_t; 54 55 /* FreeBSD's headers define additional typedef's for ELF structures. */ 56 typedef Elf64_Size GElf_Size; 57 typedef Elf64_Hashelt GElf_Hashelt; 58 59 struct elf_file; 60 61 struct elf_file_ops { 62 void (*close)(elf_file_t ef); 63 int (*seg_read_rel)(elf_file_t ef, GElf_Addr address, size_t len, 64 void *dest); 65 int (*seg_read_string)(elf_file_t ef, GElf_Addr address, size_t len, 66 char *dest); 67 GElf_Addr (*symaddr)(elf_file_t ef, GElf_Size symidx); 68 int (*lookup_set)(elf_file_t ef, const char *name, GElf_Addr *startp, 69 GElf_Addr *stopp, long *countp); 70 }; 71 72 typedef int (elf_reloc_t)(struct elf_file *ef, const void *reldata, 73 Elf_Type reltype, GElf_Addr relbase, GElf_Addr dataoff, size_t len, 74 void *dest); 75 76 struct elf_reloc_data { 77 unsigned char class; 78 unsigned char data; 79 GElf_Half machine; 80 elf_reloc_t *reloc; 81 }; 82 83 #define ELF_RELOC(_class, _data, _machine, _reloc) \ 84 static struct elf_reloc_data __CONCAT(elf_reloc_data_, __LINE__) = { \ 85 .class = (_class), \ 86 .data = (_data), \ 87 .machine = (_machine), \ 88 .reloc = (_reloc) \ 89 }; \ 90 DATA_SET(elf_reloc, __CONCAT(elf_reloc_data_, __LINE__)) 91 92 struct elf_file { 93 elf_file_t ef_ef; 94 struct elf_file_ops *ef_ops; 95 const char *ef_filename; 96 Elf *ef_elf; 97 elf_reloc_t *ef_reloc; 98 GElf_Ehdr ef_hdr; 99 size_t ef_pointer_size; 100 int ef_fd; 101 }; 102 103 #define elf_machine(ef) ((ef)->ef_hdr.e_machine) 104 #define elf_class(ef) ((ef)->ef_hdr.e_ident[EI_CLASS]) 105 #define elf_encoding(ef) ((ef)->ef_hdr.e_ident[EI_DATA]) 106 107 /* 108 * "Generic" versions of module metadata structures. 109 */ 110 struct Gmod_depend { 111 int md_ver_minimum; 112 int md_ver_preferred; 113 int md_ver_maximum; 114 }; 115 116 struct Gmod_version { 117 int mv_version; 118 }; 119 120 struct Gmod_metadata { 121 int md_version; /* structure version MDTV_* */ 122 int md_type; /* type of entry MDT_* */ 123 GElf_Addr md_data; /* specific data */ 124 GElf_Addr md_cval; /* common string label */ 125 }; 126 127 struct Gmod_pnp_match_info 128 { 129 GElf_Addr descr; /* Description of the table */ 130 GElf_Addr bus; /* Name of the bus for this table */ 131 GElf_Addr table; /* Pointer to pnp table */ 132 int entry_len; /* Length of each entry in the table (may be */ 133 /* longer than descr describes). */ 134 int num_entry; /* Number of entries in the table */ 135 }; 136 137 __BEGIN_DECLS 138 139 /* 140 * Attempt to parse an open ELF file as either an executable or DSO 141 * (ef_open) or an object file (ef_obj_open). On success, these 142 * routines initialize the 'ef_ef' and 'ef_ops' members of 'ef'. 143 */ 144 int ef_open(struct elf_file *ef, int verbose); 145 int ef_obj_open(struct elf_file *ef, int verbose); 146 147 /* 148 * Direct operations on an ELF file regardless of type. Many of these 149 * use libelf. 150 */ 151 152 /* 153 * Open an ELF file with libelf. Populates fields other than ef_ef 154 * and ef_ops in '*efile'. 155 */ 156 int elf_open_file(struct elf_file *efile, const char *filename, 157 int verbose); 158 159 /* Close an ELF file. */ 160 void elf_close_file(struct elf_file *efile); 161 162 /* Is an ELF file the same architecture as hdr? */ 163 bool elf_compatible(struct elf_file *efile, const GElf_Ehdr *hdr); 164 165 /* The size of a single object of 'type'. */ 166 size_t elf_object_size(struct elf_file *efile, Elf_Type type); 167 168 /* The size of a pointer in architecture of 'efile'. */ 169 size_t elf_pointer_size(struct elf_file *efile); 170 171 /* 172 * Read and convert an array of a data type from an ELF file. This is 173 * a wrapper around gelf_xlatetom() which reads an array of raw ELF 174 * objects from the file and converts them into host structures using 175 * native endianness. The data is returned in a dynamically-allocated 176 * buffer. 177 */ 178 int elf_read_data(struct elf_file *efile, Elf_Type type, off_t offset, 179 size_t len, void **out); 180 181 /* Reads "raw" data from an ELF file without any translation. */ 182 int elf_read_raw_data(struct elf_file *efile, off_t offset, void *dst, 183 size_t len); 184 185 /* 186 * A wrapper around elf_read_raw_data which returns the data in a 187 * dynamically-allocated buffer. 188 */ 189 int elf_read_raw_data_alloc(struct elf_file *efile, off_t offset, 190 size_t len, void **out); 191 192 /* 193 * Read relocated data from an ELF file and return it in a 194 * dynamically-allocated buffer. Note that no translation 195 * (byte-swapping for endianness, 32-vs-64) is performed on the 196 * returned data, but any ELF relocations which affect the contents 197 * are applied to the returned data. The address parameter gives the 198 * address of the data buffer if the ELF file were loaded into memory 199 * rather than a direct file offset. 200 */ 201 int elf_read_relocated_data(struct elf_file *efile, GElf_Addr address, 202 size_t len, void **buf); 203 204 /* 205 * Read the program headers from an ELF file and return them in a 206 * dynamically-allocated array of GElf_Phdr objects. 207 */ 208 int elf_read_phdrs(struct elf_file *efile, size_t *nphdrp, 209 GElf_Phdr **phdrp); 210 211 /* 212 * Read the section headers from an ELF file and return them in a 213 * dynamically-allocated array of GElf_Shdr objects. 214 */ 215 int elf_read_shdrs(struct elf_file *efile, size_t *nshdrp, 216 GElf_Shdr **shdrp); 217 218 /* 219 * Read the dynamic table from a section of an ELF file into a 220 * dynamically-allocated array of GElf_Dyn objects. 221 */ 222 int elf_read_dynamic(struct elf_file *efile, int section_index, size_t *ndynp, 223 GElf_Dyn **dynp); 224 225 /* 226 * Read a symbol table from a section of an ELF file into a 227 * dynamically-allocated array of GElf_Sym objects. 228 */ 229 int elf_read_symbols(struct elf_file *efile, int section_index, 230 size_t *nsymp, GElf_Sym **symp); 231 232 /* 233 * Read a string table described by a section header of an ELF file 234 * into a dynamically-allocated buffer. 235 */ 236 int elf_read_string_table(struct elf_file *efile, const GElf_Shdr *shdr, 237 long *strcnt, char **strtab); 238 239 /* 240 * Read a table of relocation objects from a section of an ELF file 241 * into a dynamically-allocated array of GElf_Rel objects. 242 */ 243 int elf_read_rel(struct elf_file *efile, int section_index, long *nrelp, 244 GElf_Rel **relp); 245 246 /* 247 * Read a table of relocation-with-addend objects from a section of an 248 * ELF file into a dynamically-allocated array of GElf_Rela objects. 249 */ 250 int elf_read_rela(struct elf_file *efile, int section_index, long *nrelap, 251 GElf_Rela **relap); 252 253 /* 254 * Read a string from an ELF file and return it in the provided 255 * buffer. If the string is longer than the buffer, this fails with 256 * EFAULT. The address parameter gives the address of the data buffer 257 * if the ELF file were loaded into memory rather than a direct file 258 * offset. 259 */ 260 int elf_read_string(struct elf_file *efile, GElf_Addr address, void *dst, 261 size_t len); 262 263 /* Return the address extracted from a target pointer stored at 'p'. */ 264 GElf_Addr elf_address_from_pointer(struct elf_file *efile, const void *p); 265 266 /* 267 * Read a linker set and return an array of addresses extracted from the 268 * relocated pointers in the linker set. 269 */ 270 int elf_read_linker_set(struct elf_file *efile, const char *name, 271 GElf_Addr **buf, long *countp); 272 273 /* 274 * Read and convert a target 'struct mod_depend' into a host 275 * 'struct Gmod_depend'. 276 */ 277 int elf_read_mod_depend(struct elf_file *efile, GElf_Addr addr, 278 struct Gmod_depend *mdp); 279 280 /* 281 * Read and convert a target 'struct mod_version' into a host 282 * 'struct Gmod_version'. 283 */ 284 int elf_read_mod_version(struct elf_file *efile, GElf_Addr addr, 285 struct Gmod_version *mdv); 286 287 /* 288 * Read and convert a target 'struct mod_metadata' into a host 289 * 'struct Gmod_metadata'. 290 */ 291 int elf_read_mod_metadata(struct elf_file *efile, GElf_Addr addr, 292 struct Gmod_metadata *md); 293 294 /* 295 * Read and convert a target 'struct mod_pnp_match_info' into a host 296 * 'struct Gmod_pnp_match_info'. 297 */ 298 int elf_read_mod_pnp_match_info(struct elf_file *efile, GElf_Addr addr, 299 struct Gmod_pnp_match_info *pnp); 300 301 /* 302 * Apply relocations to the values obtained from the file. `relbase' is the 303 * target relocation address of the section, and `dataoff/len' is the region 304 * that is to be relocated, and has been copied to *dest 305 */ 306 int elf_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 307 GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest); 308 309 __END_DECLS 310 311 #endif /* _EF_H_*/ 312