1cea6c86cSDoug Rabson /*- 2326e27d8SDoug Rabson * Copyright (c) 1997-2000 Doug Rabson 3cea6c86cSDoug Rabson * All rights reserved. 4cea6c86cSDoug Rabson * 5cea6c86cSDoug Rabson * Redistribution and use in source and binary forms, with or without 6cea6c86cSDoug Rabson * modification, are permitted provided that the following conditions 7cea6c86cSDoug Rabson * are met: 8cea6c86cSDoug Rabson * 1. Redistributions of source code must retain the above copyright 9cea6c86cSDoug Rabson * notice, this list of conditions and the following disclaimer. 10cea6c86cSDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 11cea6c86cSDoug Rabson * notice, this list of conditions and the following disclaimer in the 12cea6c86cSDoug Rabson * documentation and/or other materials provided with the distribution. 13cea6c86cSDoug Rabson * 14cea6c86cSDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15cea6c86cSDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16cea6c86cSDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17cea6c86cSDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18cea6c86cSDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19cea6c86cSDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20cea6c86cSDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21cea6c86cSDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22cea6c86cSDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23cea6c86cSDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24cea6c86cSDoug Rabson * SUCH DAMAGE. 25cea6c86cSDoug Rabson * 26c3aac50fSPeter Wemm * $FreeBSD$ 27cea6c86cSDoug Rabson */ 28cea6c86cSDoug Rabson 29cea6c86cSDoug Rabson #ifndef _SYS_LINKER_H_ 30cea6c86cSDoug Rabson #define _SYS_LINKER_H_ 31cea6c86cSDoug Rabson 32664a31e4SPeter Wemm #ifdef _KERNEL 33cea6c86cSDoug Rabson 3481450045SPeter Wemm #include <machine/elf.h> 35326e27d8SDoug Rabson #include <sys/kobj.h> 3681450045SPeter Wemm 37452ed6dcSBruce Evans #ifdef MALLOC_DECLARE 38452ed6dcSBruce Evans MALLOC_DECLARE(M_LINKER); 39452ed6dcSBruce Evans #endif 40cea6c86cSDoug Rabson 4196987c74SBrian Somers struct mod_depend; 4296987c74SBrian Somers 43cea6c86cSDoug Rabson /* 44cea6c86cSDoug Rabson * Object representing a file which has been loaded by the linker. 45cea6c86cSDoug Rabson */ 46cea6c86cSDoug Rabson typedef struct linker_file* linker_file_t; 47e3975643SJake Burkholder typedef TAILQ_HEAD(, linker_file) linker_file_list_t; 48cea6c86cSDoug Rabson 49a2c99e3eSDoug Rabson typedef caddr_t linker_sym_t; /* opaque symbol */ 50d254af07SMatthew Dillon typedef c_caddr_t c_linker_sym_t; /* const opaque symbol */ 51bb9fe9ddSBrian Feldman typedef int (*linker_function_name_callback_t)(const char *, void *); 52a2c99e3eSDoug Rabson 53a2c99e3eSDoug Rabson /* 54a2c99e3eSDoug Rabson * expanded out linker_sym_t 55a2c99e3eSDoug Rabson */ 56a2c99e3eSDoug Rabson typedef struct linker_symval { 57a2c99e3eSDoug Rabson const char* name; 58a2c99e3eSDoug Rabson caddr_t value; 59a2c99e3eSDoug Rabson size_t size; 60a2c99e3eSDoug Rabson } linker_symval_t; 61a2c99e3eSDoug Rabson 62cea6c86cSDoug Rabson struct common_symbol { 63e3975643SJake Burkholder STAILQ_ENTRY(common_symbol) link; 64cea6c86cSDoug Rabson char* name; 65cea6c86cSDoug Rabson caddr_t address; 66cea6c86cSDoug Rabson }; 67cea6c86cSDoug Rabson 68cea6c86cSDoug Rabson struct linker_file { 69326e27d8SDoug Rabson KOBJ_FIELDS; 70cea6c86cSDoug Rabson int refs; /* reference count */ 7181450045SPeter Wemm int userrefs; /* kldload(2) count */ 72149a155cSDoug Rabson int flags; 73149a155cSDoug Rabson #define LINKER_FILE_LINKED 0x1 /* file has been fully linked */ 74e3975643SJake Burkholder TAILQ_ENTRY(linker_file) link; /* list of all loaded files */ 75cea6c86cSDoug Rabson char* filename; /* file which was loaded */ 76cea6c86cSDoug Rabson int id; /* unique id */ 77cea6c86cSDoug Rabson caddr_t address; /* load address */ 78cea6c86cSDoug Rabson size_t size; /* size of file */ 797b9716baSIan Dowse int ndeps; /* number of dependencies */ 807b9716baSIan Dowse linker_file_t* deps; /* list of dependencies */ 81e3975643SJake Burkholder STAILQ_HEAD(, common_symbol) common; /* list of common symbols */ 82e3975643SJake Burkholder TAILQ_HEAD(, module) modules; /* modules in this file */ 83e3975643SJake Burkholder TAILQ_ENTRY(linker_file) loaded; /* preload dependency support */ 84cea6c86cSDoug Rabson }; 85cea6c86cSDoug Rabson 86cea6c86cSDoug Rabson /* 87cea6c86cSDoug Rabson * Object implementing a class of file (a.out, elf, etc.) 88cea6c86cSDoug Rabson */ 89cea6c86cSDoug Rabson typedef struct linker_class *linker_class_t; 90e3975643SJake Burkholder typedef TAILQ_HEAD(, linker_class) linker_class_list_t; 91cea6c86cSDoug Rabson 92cea6c86cSDoug Rabson struct linker_class { 93326e27d8SDoug Rabson KOBJ_CLASS_FIELDS; 94e3975643SJake Burkholder TAILQ_ENTRY(linker_class) link; /* list of all file classes */ 95cea6c86cSDoug Rabson }; 96cea6c86cSDoug Rabson 97cea6c86cSDoug Rabson /* 9884e40f56SPeter Wemm * The "file" for the kernel. 9984e40f56SPeter Wemm */ 10084e40f56SPeter Wemm extern linker_file_t linker_kernel_file; 10184e40f56SPeter Wemm 10284e40f56SPeter Wemm /* 103cea6c86cSDoug Rabson * Add a new file class to the linker. 104cea6c86cSDoug Rabson */ 105edf6db62SJohn Baldwin int linker_add_class(linker_class_t _cls); 106cea6c86cSDoug Rabson 107cea6c86cSDoug Rabson /* 108f2b17113SMaxime Henrion * Load a kernel module. 109cea6c86cSDoug Rabson */ 110f2b17113SMaxime Henrion int linker_load_module(const char *_kldname, const char *_modname, 111f2b17113SMaxime Henrion struct linker_file *_parent, struct mod_depend *_verinfo, 112f2b17113SMaxime Henrion struct linker_file **_lfpp); 113cea6c86cSDoug Rabson 114cea6c86cSDoug Rabson /* 11509dbb404SBrian Somers * Obtain a reference to a module, loading it if required. 11609dbb404SBrian Somers */ 11796987c74SBrian Somers int linker_reference_module(const char* _modname, struct mod_depend *_verinfo, 11896987c74SBrian Somers linker_file_t* _result); 11909dbb404SBrian Somers 12009dbb404SBrian Somers /* 121cea6c86cSDoug Rabson * Find a currently loaded file given its filename. 122cea6c86cSDoug Rabson */ 123edf6db62SJohn Baldwin linker_file_t linker_find_file_by_name(const char* _filename); 124cea6c86cSDoug Rabson 125cea6c86cSDoug Rabson /* 126cea6c86cSDoug Rabson * Find a currently loaded file given its file id. 127cea6c86cSDoug Rabson */ 128edf6db62SJohn Baldwin linker_file_t linker_find_file_by_id(int _fileid); 129cea6c86cSDoug Rabson 130cea6c86cSDoug Rabson /* 131cea6c86cSDoug Rabson * Called from a class handler when a file is laoded. 132cea6c86cSDoug Rabson */ 133edf6db62SJohn Baldwin linker_file_t linker_make_file(const char* _filename, linker_class_t _cls); 134cea6c86cSDoug Rabson 135cea6c86cSDoug Rabson /* 136cea6c86cSDoug Rabson * Unload a file, freeing up memory. 137cea6c86cSDoug Rabson */ 138edf6db62SJohn Baldwin int linker_file_unload(linker_file_t _file); 139cea6c86cSDoug Rabson 140cea6c86cSDoug Rabson /* 1417b9716baSIan Dowse * Add a dependency to a file. 142cea6c86cSDoug Rabson */ 1437b9716baSIan Dowse int linker_file_add_dependency(linker_file_t _file, linker_file_t _dep); 144cea6c86cSDoug Rabson 145cea6c86cSDoug Rabson /* 1467b9716baSIan Dowse * Lookup a symbol in a file. If deps is TRUE, look in dependencies 147cea6c86cSDoug Rabson * if not found in file. 148cea6c86cSDoug Rabson */ 149edf6db62SJohn Baldwin caddr_t linker_file_lookup_symbol(linker_file_t _file, const char* _name, 150edf6db62SJohn Baldwin int _deps); 151cea6c86cSDoug Rabson 1527ec76718SMike Smith /* 153f41325dbSPeter Wemm * Lookup a linker set in a file. Return pointers to the first entry, 154f41325dbSPeter Wemm * last + 1, and count of entries. Use: for (p = start; p < stop; p++) {} 155f41325dbSPeter Wemm * void *start is really: "struct yoursetmember ***start;" 156f41325dbSPeter Wemm */ 157f41325dbSPeter Wemm int linker_file_lookup_set(linker_file_t _file, const char *_name, 158f41325dbSPeter Wemm void *_start, void *_stop, int *_count); 159f41325dbSPeter Wemm 160f41325dbSPeter Wemm /* 16154823af2SPeter Wemm * This routine is responsible for finding dependencies of userland 16254823af2SPeter Wemm * initiated kldload(2)'s of files. 16381450045SPeter Wemm */ 1647b9716baSIan Dowse int linker_load_dependencies(linker_file_t _lf); 16581450045SPeter Wemm 16681450045SPeter Wemm /* 16781450045SPeter Wemm * DDB Helpers, tuned specifically for ddb/db_kld.c 16881450045SPeter Wemm */ 169edf6db62SJohn Baldwin int linker_ddb_lookup(const char *_symstr, c_linker_sym_t *_sym); 170edf6db62SJohn Baldwin int linker_ddb_search_symbol(caddr_t _value, c_linker_sym_t *_sym, 171edf6db62SJohn Baldwin long *_diffp); 172edf6db62SJohn Baldwin int linker_ddb_symbol_values(c_linker_sym_t _sym, linker_symval_t *_symval); 17381450045SPeter Wemm 17481450045SPeter Wemm 175664a31e4SPeter Wemm #endif /* _KERNEL */ 17681450045SPeter Wemm 17781450045SPeter Wemm /* 1787ec76718SMike Smith * Module information subtypes 1797ec76718SMike Smith */ 18081450045SPeter Wemm #define MODINFO_END 0x0000 /* End of list */ 18181450045SPeter Wemm #define MODINFO_NAME 0x0001 /* Name of module (string) */ 18281450045SPeter Wemm #define MODINFO_TYPE 0x0002 /* Type of module (string) */ 18381450045SPeter Wemm #define MODINFO_ADDR 0x0003 /* Loaded address */ 18481450045SPeter Wemm #define MODINFO_SIZE 0x0004 /* Size of module */ 18581450045SPeter Wemm #define MODINFO_EMPTY 0x0005 /* Has been deleted */ 186445a0357SDaniel C. Sobral #define MODINFO_ARGS 0x0006 /* Parameters string */ 18781450045SPeter Wemm #define MODINFO_METADATA 0x8000 /* Module-specfic */ 1887ec76718SMike Smith 1897ec76718SMike Smith #define MODINFOMD_AOUTEXEC 0x0001 /* a.out exec header */ 1907ec76718SMike Smith #define MODINFOMD_ELFHDR 0x0002 /* ELF header */ 19181450045SPeter Wemm #define MODINFOMD_SSYM 0x0003 /* start of symbols */ 19281450045SPeter Wemm #define MODINFOMD_ESYM 0x0004 /* end of symbols */ 19381450045SPeter Wemm #define MODINFOMD_DYNAMIC 0x0005 /* _DYNAMIC pointer */ 1947ec76718SMike Smith #define MODINFOMD_NOCOPY 0x8000 /* don't copy this metadata to the kernel */ 1957ec76718SMike Smith 19681450045SPeter Wemm #define MODINFOMD_DEPLIST (0x4001 | MODINFOMD_NOCOPY) /* depends on */ 19781450045SPeter Wemm 198505222d3SPeter Wemm #define LINKER_HINTS_VERSION 1 /* linker.hints file version */ 199505222d3SPeter Wemm 200664a31e4SPeter Wemm #ifdef _KERNEL 2017ec76718SMike Smith 2021d13be33SMike Smith /* 2031d13be33SMike Smith * Module lookup 2041d13be33SMike Smith */ 20581450045SPeter Wemm extern caddr_t preload_metadata; 206edf6db62SJohn Baldwin extern caddr_t preload_search_by_name(const char *_name); 207edf6db62SJohn Baldwin extern caddr_t preload_search_by_type(const char *_type); 208edf6db62SJohn Baldwin extern caddr_t preload_search_next_name(caddr_t _base); 209edf6db62SJohn Baldwin extern caddr_t preload_search_info(caddr_t _mod, int _inf); 210edf6db62SJohn Baldwin extern void preload_delete_name(const char *_name); 211edf6db62SJohn Baldwin extern void preload_bootstrap_relocate(vm_offset_t _offset); 2121d13be33SMike Smith 213cea6c86cSDoug Rabson #ifdef KLD_DEBUG 214cea6c86cSDoug Rabson 215cea6c86cSDoug Rabson extern int kld_debug; 216cea6c86cSDoug Rabson #define KLD_DEBUG_FILE 1 /* file load/unload */ 217cea6c86cSDoug Rabson #define KLD_DEBUG_SYM 2 /* symbol lookup */ 218cea6c86cSDoug Rabson 219cea6c86cSDoug Rabson #define KLD_DPF(cat, args) \ 220cea6c86cSDoug Rabson do { \ 22181450045SPeter Wemm if (kld_debug & KLD_DEBUG_##cat) printf args; \ 222cea6c86cSDoug Rabson } while (0) 223cea6c86cSDoug Rabson 224cea6c86cSDoug Rabson #else 225cea6c86cSDoug Rabson 226cea6c86cSDoug Rabson #define KLD_DPF(cat, args) 227cea6c86cSDoug Rabson 228cea6c86cSDoug Rabson #endif 229cea6c86cSDoug Rabson 23081450045SPeter Wemm /* Support functions */ 231d297ad16SMarcel Moolenaar int elf_reloc(linker_file_t _lf, const void *_rel, int _type); 232d297ad16SMarcel Moolenaar Elf_Addr elf_lookup(linker_file_t, Elf_Word, int); 233d297ad16SMarcel Moolenaar 234aa855a59SPeter Wemm /* values for type */ 235aa855a59SPeter Wemm #define ELF_RELOC_REL 1 236aa855a59SPeter Wemm #define ELF_RELOC_RELA 2 23781450045SPeter Wemm 238664a31e4SPeter Wemm #endif /* _KERNEL */ 239cea6c86cSDoug Rabson 240cea6c86cSDoug Rabson struct kld_file_stat { 241cea6c86cSDoug Rabson int version; /* set to sizeof(linker_file_stat) */ 242cea6c86cSDoug Rabson char name[MAXPATHLEN]; 243cea6c86cSDoug Rabson int refs; 244cea6c86cSDoug Rabson int id; 245cea6c86cSDoug Rabson caddr_t address; /* load address */ 246cea6c86cSDoug Rabson size_t size; /* size in bytes */ 247cea6c86cSDoug Rabson }; 248cea6c86cSDoug Rabson 249ba031106SPeter Wemm struct kld_sym_lookup { 250ba031106SPeter Wemm int version; /* set to sizeof(struct kld_sym_lookup) */ 251ba031106SPeter Wemm char *symname; /* Symbol name we are looking up */ 252ba031106SPeter Wemm u_long symvalue; 253ba031106SPeter Wemm size_t symsize; 254ba031106SPeter Wemm }; 255ba031106SPeter Wemm #define KLDSYM_LOOKUP 1 256ba031106SPeter Wemm 257664a31e4SPeter Wemm #ifndef _KERNEL 258cea6c86cSDoug Rabson 259cea6c86cSDoug Rabson #include <sys/cdefs.h> 260cea6c86cSDoug Rabson 261cea6c86cSDoug Rabson __BEGIN_DECLS 262edf6db62SJohn Baldwin int kldload(const char* _file); 263edf6db62SJohn Baldwin int kldunload(int _fileid); 264edf6db62SJohn Baldwin int kldfind(const char* _file); 265edf6db62SJohn Baldwin int kldnext(int _fileid); 266edf6db62SJohn Baldwin int kldstat(int _fileid, struct kld_file_stat* _stat); 267edf6db62SJohn Baldwin int kldfirstmod(int _fileid); 268ba031106SPeter Wemm int kldsym(int _fileid, int _cmd, void *_data); 269cea6c86cSDoug Rabson __END_DECLS 270cea6c86cSDoug Rabson 271cea6c86cSDoug Rabson #endif 272cea6c86cSDoug Rabson 273452ed6dcSBruce Evans #endif /* !_SYS_LINKER_H_ */ 274