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 62a2024a3eSJohn Birrell typedef int (*linker_function_nameval_callback_t)(linker_file_t, int, linker_symval_t *, void *); 63f6c15301SJohn Birrell 64cea6c86cSDoug Rabson struct common_symbol { 65e3975643SJake Burkholder STAILQ_ENTRY(common_symbol) link; 66cea6c86cSDoug Rabson char* name; 67cea6c86cSDoug Rabson caddr_t address; 68cea6c86cSDoug Rabson }; 69cea6c86cSDoug Rabson 70cea6c86cSDoug Rabson struct linker_file { 71326e27d8SDoug Rabson KOBJ_FIELDS; 72cea6c86cSDoug Rabson int refs; /* reference count */ 7381450045SPeter Wemm int userrefs; /* kldload(2) count */ 74149a155cSDoug Rabson int flags; 75149a155cSDoug Rabson #define LINKER_FILE_LINKED 0x1 /* file has been fully linked */ 76e3975643SJake Burkholder TAILQ_ENTRY(linker_file) link; /* list of all loaded files */ 77cea6c86cSDoug Rabson char* filename; /* file which was loaded */ 781676805cSJohn Birrell char* pathname; /* file name with full path */ 79cea6c86cSDoug Rabson int id; /* unique id */ 80cea6c86cSDoug Rabson caddr_t address; /* load address */ 81cea6c86cSDoug Rabson size_t size; /* size of file */ 827b9716baSIan Dowse int ndeps; /* number of dependencies */ 837b9716baSIan Dowse linker_file_t* deps; /* list of dependencies */ 84e3975643SJake Burkholder STAILQ_HEAD(, common_symbol) common; /* list of common symbols */ 85e3975643SJake Burkholder TAILQ_HEAD(, module) modules; /* modules in this file */ 86e3975643SJake Burkholder TAILQ_ENTRY(linker_file) loaded; /* preload dependency support */ 871676805cSJohn Birrell int loadcnt; /* load counter value */ 881676805cSJohn Birrell 891676805cSJohn Birrell /* 901676805cSJohn Birrell * Function Boundary Tracing (FBT) or Statically Defined Tracing (SDT) 911676805cSJohn Birrell * fields. 921676805cSJohn Birrell */ 931676805cSJohn Birrell int nenabled; /* number of enabled probes. */ 941676805cSJohn Birrell int fbt_nentries; /* number of fbt entries created. */ 951676805cSJohn Birrell void *sdt_probes; 961676805cSJohn Birrell int sdt_nentries; 971676805cSJohn Birrell size_t sdt_nprobes; 981676805cSJohn Birrell size_t sdt_size; 99cea6c86cSDoug Rabson }; 100cea6c86cSDoug Rabson 101cea6c86cSDoug Rabson /* 102cea6c86cSDoug Rabson * Object implementing a class of file (a.out, elf, etc.) 103cea6c86cSDoug Rabson */ 104cea6c86cSDoug Rabson typedef struct linker_class *linker_class_t; 105e3975643SJake Burkholder typedef TAILQ_HEAD(, linker_class) linker_class_list_t; 106cea6c86cSDoug Rabson 107cea6c86cSDoug Rabson struct linker_class { 108326e27d8SDoug Rabson KOBJ_CLASS_FIELDS; 109e3975643SJake Burkholder TAILQ_ENTRY(linker_class) link; /* list of all file classes */ 110cea6c86cSDoug Rabson }; 111cea6c86cSDoug Rabson 112cea6c86cSDoug Rabson /* 11393215106SJohn Baldwin * Function type used when iterating over the list of linker files. 11493215106SJohn Baldwin */ 11593215106SJohn Baldwin typedef int linker_predicate_t(linker_file_t, void *); 11693215106SJohn Baldwin 11793215106SJohn Baldwin /* 11884e40f56SPeter Wemm * The "file" for the kernel. 11984e40f56SPeter Wemm */ 12084e40f56SPeter Wemm extern linker_file_t linker_kernel_file; 12184e40f56SPeter Wemm 12284e40f56SPeter Wemm /* 12309dbb404SBrian Somers * Obtain a reference to a module, loading it if required. 12409dbb404SBrian Somers */ 12596987c74SBrian Somers int linker_reference_module(const char* _modname, struct mod_depend *_verinfo, 12696987c74SBrian Somers linker_file_t* _result); 12709dbb404SBrian Somers 12809dbb404SBrian Somers /* 129aeeb017bSJohn Baldwin * Release a reference to a module, unloading it if there are no more 130aeeb017bSJohn Baldwin * references. Note that one should either provide a module name and 131aeeb017bSJohn Baldwin * optional version info or a linker file, but not both. 132aeeb017bSJohn Baldwin */ 133aeeb017bSJohn Baldwin int linker_release_module(const char *_modname, struct mod_depend *_verinfo, 134aeeb017bSJohn Baldwin linker_file_t _file); 135aeeb017bSJohn Baldwin 136aeeb017bSJohn Baldwin /* 13793215106SJohn Baldwin * Iterate over all of the currently loaded linker files calling the 13893215106SJohn Baldwin * predicate function while the function returns 0. Returns the value 13993215106SJohn Baldwin * returned by the last predicate function. 14093215106SJohn Baldwin */ 14193215106SJohn Baldwin int linker_file_foreach(linker_predicate_t *_predicate, void *_context); 14293215106SJohn Baldwin 14393215106SJohn Baldwin /* 1447b9716baSIan Dowse * Lookup a symbol in a file. If deps is TRUE, look in dependencies 145cea6c86cSDoug Rabson * if not found in file. 146cea6c86cSDoug Rabson */ 147edf6db62SJohn Baldwin caddr_t linker_file_lookup_symbol(linker_file_t _file, const char* _name, 148edf6db62SJohn Baldwin int _deps); 149cea6c86cSDoug Rabson 1507ec76718SMike Smith /* 151f41325dbSPeter Wemm * Lookup a linker set in a file. Return pointers to the first entry, 152f41325dbSPeter Wemm * last + 1, and count of entries. Use: for (p = start; p < stop; p++) {} 153f41325dbSPeter Wemm * void *start is really: "struct yoursetmember ***start;" 154f41325dbSPeter Wemm */ 155f41325dbSPeter Wemm int linker_file_lookup_set(linker_file_t _file, const char *_name, 156f41325dbSPeter Wemm void *_start, void *_stop, int *_count); 157f41325dbSPeter Wemm 158f41325dbSPeter Wemm /* 159f6c15301SJohn Birrell * List all functions in a file. 160f6c15301SJohn Birrell */ 161a2024a3eSJohn Birrell int linker_file_function_listall(linker_file_t, 162a2024a3eSJohn Birrell linker_function_nameval_callback_t, void *); 163f6c15301SJohn Birrell 164f6c15301SJohn Birrell /* 165880eb8c1SJohn Baldwin * Functions soley for use by the linker class handlers. 16681450045SPeter Wemm */ 167880eb8c1SJohn Baldwin int linker_add_class(linker_class_t _cls); 168880eb8c1SJohn Baldwin int linker_file_unload(linker_file_t _file, int flags); 1697b9716baSIan Dowse int linker_load_dependencies(linker_file_t _lf); 170880eb8c1SJohn Baldwin linker_file_t linker_make_file(const char* _filename, linker_class_t _cls); 17181450045SPeter Wemm 17281450045SPeter Wemm /* 17381450045SPeter Wemm * DDB Helpers, tuned specifically for ddb/db_kld.c 17481450045SPeter Wemm */ 175edf6db62SJohn Baldwin int linker_ddb_lookup(const char *_symstr, c_linker_sym_t *_sym); 176edf6db62SJohn Baldwin int linker_ddb_search_symbol(caddr_t _value, c_linker_sym_t *_sym, 177edf6db62SJohn Baldwin long *_diffp); 178edf6db62SJohn Baldwin int linker_ddb_symbol_values(c_linker_sym_t _sym, linker_symval_t *_symval); 179cdd475b3SRobert Watson int linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen, 180cdd475b3SRobert Watson long *offset); 181cdd475b3SRobert Watson 182cdd475b3SRobert Watson /* 183cdd475b3SRobert Watson * stack(9) helper for situations where kernel locking is required. 184cdd475b3SRobert Watson */ 185cdd475b3SRobert Watson int linker_search_symbol_name(caddr_t value, char *buf, u_int buflen, 186cdd475b3SRobert Watson long *offset); 18781450045SPeter Wemm 18881450045SPeter Wemm 18949874f6eSJoseph Koshy /* HWPMC helper */ 19049874f6eSJoseph Koshy void *linker_hwpmc_list_objects(void); 19149874f6eSJoseph Koshy 192664a31e4SPeter Wemm #endif /* _KERNEL */ 19381450045SPeter Wemm 19481450045SPeter Wemm /* 1957ec76718SMike Smith * Module information subtypes 1967ec76718SMike Smith */ 19781450045SPeter Wemm #define MODINFO_END 0x0000 /* End of list */ 19881450045SPeter Wemm #define MODINFO_NAME 0x0001 /* Name of module (string) */ 19981450045SPeter Wemm #define MODINFO_TYPE 0x0002 /* Type of module (string) */ 20081450045SPeter Wemm #define MODINFO_ADDR 0x0003 /* Loaded address */ 20181450045SPeter Wemm #define MODINFO_SIZE 0x0004 /* Size of module */ 20281450045SPeter Wemm #define MODINFO_EMPTY 0x0005 /* Has been deleted */ 203445a0357SDaniel C. Sobral #define MODINFO_ARGS 0x0006 /* Parameters string */ 20481450045SPeter Wemm #define MODINFO_METADATA 0x8000 /* Module-specfic */ 2057ec76718SMike Smith 2067ec76718SMike Smith #define MODINFOMD_AOUTEXEC 0x0001 /* a.out exec header */ 2077ec76718SMike Smith #define MODINFOMD_ELFHDR 0x0002 /* ELF header */ 20881450045SPeter Wemm #define MODINFOMD_SSYM 0x0003 /* start of symbols */ 20981450045SPeter Wemm #define MODINFOMD_ESYM 0x0004 /* end of symbols */ 21081450045SPeter Wemm #define MODINFOMD_DYNAMIC 0x0005 /* _DYNAMIC pointer */ 2117f476681SPeter Wemm /* These values are MD on these two platforms */ 2127f476681SPeter Wemm #if !defined(__sparc64__) && !defined(__powerpc__) 2137f476681SPeter Wemm #define MODINFOMD_ENVP 0x0006 /* envp[] */ 2147f476681SPeter Wemm #define MODINFOMD_HOWTO 0x0007 /* boothowto */ 2157f476681SPeter Wemm #define MODINFOMD_KERNEND 0x0008 /* kernend */ 2167f476681SPeter Wemm #endif 2176f2e8645SIan Dowse #define MODINFOMD_SHDR 0x0009 /* section header table */ 2187ec76718SMike Smith #define MODINFOMD_NOCOPY 0x8000 /* don't copy this metadata to the kernel */ 2197ec76718SMike Smith 22081450045SPeter Wemm #define MODINFOMD_DEPLIST (0x4001 | MODINFOMD_NOCOPY) /* depends on */ 22181450045SPeter Wemm 2227f476681SPeter Wemm #ifdef _KERNEL 2237f476681SPeter Wemm #define MD_FETCH(mdp, info, type) ({ \ 2247f476681SPeter Wemm type *__p; \ 2257f476681SPeter Wemm __p = (type *)preload_search_info((mdp), MODINFO_METADATA | (info)); \ 2267f476681SPeter Wemm __p ? *__p : 0; \ 2277f476681SPeter Wemm }) 2287f476681SPeter Wemm #endif 2297f476681SPeter Wemm 230505222d3SPeter Wemm #define LINKER_HINTS_VERSION 1 /* linker.hints file version */ 231505222d3SPeter Wemm 232664a31e4SPeter Wemm #ifdef _KERNEL 2337ec76718SMike Smith 2341d13be33SMike Smith /* 2351d13be33SMike Smith * Module lookup 2361d13be33SMike Smith */ 23781450045SPeter Wemm extern caddr_t preload_metadata; 238edf6db62SJohn Baldwin extern caddr_t preload_search_by_name(const char *_name); 239edf6db62SJohn Baldwin extern caddr_t preload_search_by_type(const char *_type); 240edf6db62SJohn Baldwin extern caddr_t preload_search_next_name(caddr_t _base); 241edf6db62SJohn Baldwin extern caddr_t preload_search_info(caddr_t _mod, int _inf); 242edf6db62SJohn Baldwin extern void preload_delete_name(const char *_name); 243edf6db62SJohn Baldwin extern void preload_bootstrap_relocate(vm_offset_t _offset); 2441d13be33SMike Smith 245cea6c86cSDoug Rabson #ifdef KLD_DEBUG 246cea6c86cSDoug Rabson 247cea6c86cSDoug Rabson extern int kld_debug; 248cea6c86cSDoug Rabson #define KLD_DEBUG_FILE 1 /* file load/unload */ 249cea6c86cSDoug Rabson #define KLD_DEBUG_SYM 2 /* symbol lookup */ 250cea6c86cSDoug Rabson 251cea6c86cSDoug Rabson #define KLD_DPF(cat, args) \ 252cea6c86cSDoug Rabson do { \ 25381450045SPeter Wemm if (kld_debug & KLD_DEBUG_##cat) printf args; \ 254cea6c86cSDoug Rabson } while (0) 255cea6c86cSDoug Rabson 256cea6c86cSDoug Rabson #else 257cea6c86cSDoug Rabson 258cea6c86cSDoug Rabson #define KLD_DPF(cat, args) 259cea6c86cSDoug Rabson 260cea6c86cSDoug Rabson #endif 261cea6c86cSDoug Rabson 262757686b1SMarcel Moolenaar typedef Elf_Addr elf_lookup_fn(linker_file_t, Elf_Size, int); 263e8855d4fSPeter Wemm 26481450045SPeter Wemm /* Support functions */ 265e8855d4fSPeter Wemm int elf_reloc(linker_file_t _lf, Elf_Addr base, const void *_rel, int _type, elf_lookup_fn _lu); 266e8855d4fSPeter Wemm int elf_reloc_local(linker_file_t _lf, Elf_Addr base, const void *_rel, int _type, elf_lookup_fn _lu); 267757686b1SMarcel Moolenaar const Elf_Sym *elf_get_sym(linker_file_t _lf, Elf_Size _symidx); 268757686b1SMarcel Moolenaar const char *elf_get_symname(linker_file_t _lf, Elf_Size _symidx); 269d297ad16SMarcel Moolenaar 270a2024a3eSJohn Birrell typedef struct linker_ctf { 271a2024a3eSJohn Birrell const uint8_t *ctftab; /* Decompressed CTF data. */ 272a2024a3eSJohn Birrell int ctfcnt; /* Number of CTF data bytes. */ 273a2024a3eSJohn Birrell const Elf_Sym *symtab; /* Ptr to the symbol table. */ 274a2024a3eSJohn Birrell int nsym; /* Number of symbols. */ 275a2024a3eSJohn Birrell const char *strtab; /* Ptr to the string table. */ 276a2024a3eSJohn Birrell int strcnt; /* Number of string bytes. */ 277a2024a3eSJohn Birrell uint32_t **ctfoffp; /* Ptr to array of obj/fnc offsets. */ 278a2024a3eSJohn Birrell uint32_t **typoffp; /* Ptr to array of type offsets. */ 279a2024a3eSJohn Birrell long *typlenp; /* Ptr to number of type data entries. */ 280a2024a3eSJohn Birrell } linker_ctf_t; 281a2024a3eSJohn Birrell 282a2024a3eSJohn Birrell int linker_ctf_get(linker_file_t, linker_ctf_t *); 283a2024a3eSJohn Birrell 2841aeb23cdSMarcel Moolenaar int elf_cpu_load_file(linker_file_t); 2851aeb23cdSMarcel Moolenaar int elf_cpu_unload_file(linker_file_t); 2861aeb23cdSMarcel Moolenaar 287aa855a59SPeter Wemm /* values for type */ 288aa855a59SPeter Wemm #define ELF_RELOC_REL 1 289aa855a59SPeter Wemm #define ELF_RELOC_RELA 2 29081450045SPeter Wemm 2911676805cSJohn Birrell /* 2921676805cSJohn Birrell * This is version 1 of the KLD file status structure. It is identified 293c9f54e81SRuslan Ermilov * by its _size_ in the version field. 2941676805cSJohn Birrell */ 2951676805cSJohn Birrell struct kld_file_stat_1 { 296c9f54e81SRuslan Ermilov int version; /* set to sizeof(struct kld_file_stat_1) */ 2971676805cSJohn Birrell char name[MAXPATHLEN]; 2981676805cSJohn Birrell int refs; 2991676805cSJohn Birrell int id; 3001676805cSJohn Birrell caddr_t address; /* load address */ 3011676805cSJohn Birrell size_t size; /* size in bytes */ 3021676805cSJohn Birrell }; 303664a31e4SPeter Wemm #endif /* _KERNEL */ 304cea6c86cSDoug Rabson 305cea6c86cSDoug Rabson struct kld_file_stat { 306c9f54e81SRuslan Ermilov int version; /* set to sizeof(struct kld_file_stat) */ 307cea6c86cSDoug Rabson char name[MAXPATHLEN]; 308cea6c86cSDoug Rabson int refs; 309cea6c86cSDoug Rabson int id; 310cea6c86cSDoug Rabson caddr_t address; /* load address */ 311cea6c86cSDoug Rabson size_t size; /* size in bytes */ 3121676805cSJohn Birrell char pathname[MAXPATHLEN]; 313cea6c86cSDoug Rabson }; 314cea6c86cSDoug Rabson 315ba031106SPeter Wemm struct kld_sym_lookup { 316ba031106SPeter Wemm int version; /* set to sizeof(struct kld_sym_lookup) */ 317ba031106SPeter Wemm char *symname; /* Symbol name we are looking up */ 318ba031106SPeter Wemm u_long symvalue; 319ba031106SPeter Wemm size_t symsize; 320ba031106SPeter Wemm }; 321ba031106SPeter Wemm #define KLDSYM_LOOKUP 1 322ba031106SPeter Wemm 32365a311fcSPoul-Henning Kamp /* 32465a311fcSPoul-Henning Kamp * Flags for kldunloadf() and linker_file_unload() 32565a311fcSPoul-Henning Kamp */ 32665a311fcSPoul-Henning Kamp #define LINKER_UNLOAD_NORMAL 0 32765a311fcSPoul-Henning Kamp #define LINKER_UNLOAD_FORCE 1 32865a311fcSPoul-Henning Kamp 329664a31e4SPeter Wemm #ifndef _KERNEL 330cea6c86cSDoug Rabson 331cea6c86cSDoug Rabson #include <sys/cdefs.h> 332cea6c86cSDoug Rabson 333cea6c86cSDoug Rabson __BEGIN_DECLS 334edf6db62SJohn Baldwin int kldload(const char* _file); 335edf6db62SJohn Baldwin int kldunload(int _fileid); 33665a311fcSPoul-Henning Kamp int kldunloadf(int _fileid, int flags); 337edf6db62SJohn Baldwin int kldfind(const char* _file); 338edf6db62SJohn Baldwin int kldnext(int _fileid); 339edf6db62SJohn Baldwin int kldstat(int _fileid, struct kld_file_stat* _stat); 340edf6db62SJohn Baldwin int kldfirstmod(int _fileid); 341ba031106SPeter Wemm int kldsym(int _fileid, int _cmd, void *_data); 342cea6c86cSDoug Rabson __END_DECLS 343cea6c86cSDoug Rabson 344cea6c86cSDoug Rabson #endif 345cea6c86cSDoug Rabson 346452ed6dcSBruce Evans #endif /* !_SYS_LINKER_H_ */ 347