1/*- 2 * Copyright (c) 1997 Doug Rabson 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 *
| 1/*- 2 * Copyright (c) 1997 Doug Rabson 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 * $Id$
| 26 * $Id: linker.h,v 1.1 1997/05/07 16:05:45 dfr Exp $
|
27 */ 28 29#ifndef _SYS_LINKER_H_ 30#define _SYS_LINKER_H_ 31 32#ifdef KERNEL 33 34#define M_LINKER M_TEMP /* XXX */ 35 36/* 37 * Object representing a file which has been loaded by the linker. 38 */ 39typedef struct linker_file* linker_file_t; 40typedef TAILQ_HEAD(, linker_file) linker_file_list_t; 41 42struct linker_file_ops { 43 /* 44 * Lookup a symbol in the file's symbol table. If the symbol is 45 * not found then return ENOENT, otherwise zero. If the symbol 46 * found is a common symbol, return with *address set to zero and 47 * *size set to the size of the common space required. Otherwise 48 * set *address the value of the symbol. 49 */ 50 int (*lookup_symbol)(linker_file_t, const char* name, 51 caddr_t* address, size_t* size); 52 53 /* 54 * Unload a file, releasing dependancies and freeing storage. 55 */ 56 void (*unload)(linker_file_t); 57}; 58 59struct common_symbol { 60 STAILQ_ENTRY(common_symbol) link; 61 char* name; 62 caddr_t address; 63}; 64 65struct linker_file { 66 int refs; /* reference count */ 67 int userrefs; /* modload(2) count */ 68 TAILQ_ENTRY(linker_file) link; /* list of all loaded files */ 69 char* filename; /* file which was loaded */ 70 int id; /* unique id */ 71 caddr_t address; /* load address */ 72 size_t size; /* size of file */ 73 int ndeps; /* number of dependancies */ 74 linker_file_t* deps; /* list of dependancies */ 75 STAILQ_HEAD(, common_symbol) common; /* list of common symbols */ 76 TAILQ_HEAD(, module) modules; /* modules in this file */ 77 void* priv; /* implementation data */ 78 79 struct linker_file_ops* ops; 80}; 81 82/* 83 * Object implementing a class of file (a.out, elf, etc.) 84 */ 85typedef struct linker_class *linker_class_t; 86typedef TAILQ_HEAD(, linker_class) linker_class_list_t; 87 88struct linker_class_ops { 89 /* 90 * Load a file, returning the new linker_file_t in *result. If 91 * the class does not recognise the file type, zero should be 92 * returned, without modifying *result. If the file is 93 * recognised, the file should be loaded, *result set to the new 94 * file and zero returned. If some other error is detected an 95 * appropriate errno should be returned. 96 */ 97 int (*load_file)(const char* filename, linker_file_t* result); 98}; 99 100struct linker_class { 101 TAILQ_ENTRY(linker_class) link; /* list of all file classes */ 102 const char* desc; /* description (e.g. "a.out") */ 103 void* priv; /* implementation data */ 104 105 struct linker_class_ops *ops; 106}; 107 108/*
| 27 */ 28 29#ifndef _SYS_LINKER_H_ 30#define _SYS_LINKER_H_ 31 32#ifdef KERNEL 33 34#define M_LINKER M_TEMP /* XXX */ 35 36/* 37 * Object representing a file which has been loaded by the linker. 38 */ 39typedef struct linker_file* linker_file_t; 40typedef TAILQ_HEAD(, linker_file) linker_file_list_t; 41 42struct linker_file_ops { 43 /* 44 * Lookup a symbol in the file's symbol table. If the symbol is 45 * not found then return ENOENT, otherwise zero. If the symbol 46 * found is a common symbol, return with *address set to zero and 47 * *size set to the size of the common space required. Otherwise 48 * set *address the value of the symbol. 49 */ 50 int (*lookup_symbol)(linker_file_t, const char* name, 51 caddr_t* address, size_t* size); 52 53 /* 54 * Unload a file, releasing dependancies and freeing storage. 55 */ 56 void (*unload)(linker_file_t); 57}; 58 59struct common_symbol { 60 STAILQ_ENTRY(common_symbol) link; 61 char* name; 62 caddr_t address; 63}; 64 65struct linker_file { 66 int refs; /* reference count */ 67 int userrefs; /* modload(2) count */ 68 TAILQ_ENTRY(linker_file) link; /* list of all loaded files */ 69 char* filename; /* file which was loaded */ 70 int id; /* unique id */ 71 caddr_t address; /* load address */ 72 size_t size; /* size of file */ 73 int ndeps; /* number of dependancies */ 74 linker_file_t* deps; /* list of dependancies */ 75 STAILQ_HEAD(, common_symbol) common; /* list of common symbols */ 76 TAILQ_HEAD(, module) modules; /* modules in this file */ 77 void* priv; /* implementation data */ 78 79 struct linker_file_ops* ops; 80}; 81 82/* 83 * Object implementing a class of file (a.out, elf, etc.) 84 */ 85typedef struct linker_class *linker_class_t; 86typedef TAILQ_HEAD(, linker_class) linker_class_list_t; 87 88struct linker_class_ops { 89 /* 90 * Load a file, returning the new linker_file_t in *result. If 91 * the class does not recognise the file type, zero should be 92 * returned, without modifying *result. If the file is 93 * recognised, the file should be loaded, *result set to the new 94 * file and zero returned. If some other error is detected an 95 * appropriate errno should be returned. 96 */ 97 int (*load_file)(const char* filename, linker_file_t* result); 98}; 99 100struct linker_class { 101 TAILQ_ENTRY(linker_class) link; /* list of all file classes */ 102 const char* desc; /* description (e.g. "a.out") */ 103 void* priv; /* implementation data */ 104 105 struct linker_class_ops *ops; 106}; 107 108/*
|
109 * The file representing the currently running kernel. This contains 110 * the global symbol table. 111 */ 112linker_file_t linker_kernel_file; 113 114/*
| |
115 * The file which is currently loading. Used to register modules with 116 * the files which contain them. 117 */
| 109 * The file which is currently loading. Used to register modules with 110 * the files which contain them. 111 */
|
118linker_file_t linker_current_file;
| 112extern linker_file_t linker_current_file;
|
119 120/* 121 * Add a new file class to the linker. 122 */ 123int linker_add_class(const char* desc, void* priv, 124 struct linker_class_ops* ops); 125 126/* 127 * Load a file, trying each file class until one succeeds. 128 */ 129int linker_load_file(const char* filename, linker_file_t* result); 130 131/* 132 * Find a currently loaded file given its filename. 133 */ 134linker_file_t linker_find_file_by_name(const char* filename); 135 136/* 137 * Find a currently loaded file given its file id. 138 */ 139linker_file_t linker_find_file_by_id(int fileid); 140 141/* 142 * Called from a class handler when a file is laoded. 143 */ 144linker_file_t linker_make_file(const char* filename, void* priv, 145 struct linker_file_ops* ops); 146 147/* 148 * Unload a file, freeing up memory. 149 */ 150int linker_file_unload(linker_file_t file); 151 152/* 153 * Add a dependancy to a file. 154 */ 155int linker_file_add_dependancy(linker_file_t file, linker_file_t dep); 156 157/* 158 * Lookup a symbol in a file. If deps is TRUE, look in dependancies 159 * if not found in file. 160 */ 161caddr_t linker_file_lookup_symbol(linker_file_t file, const char* name, 162 int deps); 163 164#ifdef KLD_DEBUG 165 166extern int kld_debug; 167#define KLD_DEBUG_FILE 1 /* file load/unload */ 168#define KLD_DEBUG_SYM 2 /* symbol lookup */ 169 170#define KLD_DPF(cat, args) \ 171 do { \ 172 if (KLD_debug & KLD_DEBUG_##cat) printf args; \ 173 } while (0) 174 175#else 176 177#define KLD_DPF(cat, args) 178 179#endif 180 181#endif /* KERNEL */ 182 183struct kld_file_stat { 184 int version; /* set to sizeof(linker_file_stat) */ 185 char name[MAXPATHLEN]; 186 int refs; 187 int id; 188 caddr_t address; /* load address */ 189 size_t size; /* size in bytes */ 190}; 191 192#ifndef KERNEL 193 194#include <sys/cdefs.h> 195 196__BEGIN_DECLS 197int kldload(const char* file); 198int kldunload(int fileid); 199int kldfind(const char* file); 200int kldnext(int fileid); 201int kldstat(int fileid, struct kld_file_stat* stat); 202int kldfirstmod(int fileid); 203__END_DECLS 204 205#endif 206 207#endif /* !_SYS_KLD_H_ */
| 113 114/* 115 * Add a new file class to the linker. 116 */ 117int linker_add_class(const char* desc, void* priv, 118 struct linker_class_ops* ops); 119 120/* 121 * Load a file, trying each file class until one succeeds. 122 */ 123int linker_load_file(const char* filename, linker_file_t* result); 124 125/* 126 * Find a currently loaded file given its filename. 127 */ 128linker_file_t linker_find_file_by_name(const char* filename); 129 130/* 131 * Find a currently loaded file given its file id. 132 */ 133linker_file_t linker_find_file_by_id(int fileid); 134 135/* 136 * Called from a class handler when a file is laoded. 137 */ 138linker_file_t linker_make_file(const char* filename, void* priv, 139 struct linker_file_ops* ops); 140 141/* 142 * Unload a file, freeing up memory. 143 */ 144int linker_file_unload(linker_file_t file); 145 146/* 147 * Add a dependancy to a file. 148 */ 149int linker_file_add_dependancy(linker_file_t file, linker_file_t dep); 150 151/* 152 * Lookup a symbol in a file. If deps is TRUE, look in dependancies 153 * if not found in file. 154 */ 155caddr_t linker_file_lookup_symbol(linker_file_t file, const char* name, 156 int deps); 157 158#ifdef KLD_DEBUG 159 160extern int kld_debug; 161#define KLD_DEBUG_FILE 1 /* file load/unload */ 162#define KLD_DEBUG_SYM 2 /* symbol lookup */ 163 164#define KLD_DPF(cat, args) \ 165 do { \ 166 if (KLD_debug & KLD_DEBUG_##cat) printf args; \ 167 } while (0) 168 169#else 170 171#define KLD_DPF(cat, args) 172 173#endif 174 175#endif /* KERNEL */ 176 177struct kld_file_stat { 178 int version; /* set to sizeof(linker_file_stat) */ 179 char name[MAXPATHLEN]; 180 int refs; 181 int id; 182 caddr_t address; /* load address */ 183 size_t size; /* size in bytes */ 184}; 185 186#ifndef KERNEL 187 188#include <sys/cdefs.h> 189 190__BEGIN_DECLS 191int kldload(const char* file); 192int kldunload(int fileid); 193int kldfind(const char* file); 194int kldnext(int fileid); 195int kldstat(int fileid, struct kld_file_stat* stat); 196int kldfirstmod(int fileid); 197__END_DECLS 198 199#endif 200 201#endif /* !_SYS_KLD_H_ */
|