73/* XXX wrong name; we're looking at version provision tags here, not modules */ 74typedef TAILQ_HEAD(, modlist) modlisthead_t; 75struct modlist { 76 TAILQ_ENTRY(modlist) link; /* chain together all modules */ 77 linker_file_t container; 78 const char *name; 79 int version; 80}; 81typedef struct modlist *modlist_t; 82static modlisthead_t found_modules; 83 84static char * 85linker_strdup(const char *str) 86{ 87 char *result; 88 89 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 90 strcpy(result, str); 91 return(result); 92} 93 94static void 95linker_init(void* arg) 96{ 97 lockinit(&lock, PVM, "klink", 0, 0); 98 TAILQ_INIT(&classes); 99 TAILQ_INIT(&linker_files); 100} 101 102SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0); 103 104int 105linker_add_class(linker_class_t lc) 106{ 107 kobj_class_compile((kobj_class_t) lc); 108 TAILQ_INSERT_TAIL(&classes, lc, link); 109 return 0; 110} 111 112static void 113linker_file_sysinit(linker_file_t lf) 114{ 115 struct sysinit** start, ** stop; 116 struct sysinit** sipp; 117 struct sysinit** xipp; 118 struct sysinit* save; 119 120 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 121 lf->filename)); 122 123 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 124 return; 125 /* 126 * Perform a bubble sort of the system initialization objects by 127 * their subsystem (primary key) and order (secondary key). 128 * 129 * Since some things care about execution order, this is the 130 * operation which ensures continued function. 131 */ 132 for (sipp = start; sipp < stop; sipp++) { 133 for (xipp = sipp + 1; xipp < stop; xipp++) { 134 if ((*sipp)->subsystem < (*xipp)->subsystem || 135 ((*sipp)->subsystem == (*xipp)->subsystem && 136 (*sipp)->order <= (*xipp)->order)) 137 continue; /* skip*/ 138 save = *sipp; 139 *sipp = *xipp; 140 *xipp = save; 141 } 142 } 143 144 145 /* 146 * Traverse the (now) ordered list of system initialization tasks. 147 * Perform each task, and continue on to the next task. 148 */ 149 for (sipp = start; sipp < stop; sipp++) { 150 if ((*sipp)->subsystem == SI_SUB_DUMMY) 151 continue; /* skip dummy task(s)*/ 152 153 /* Call function */ 154 (*((*sipp)->func))((*sipp)->udata); 155 } 156} 157 158static void 159linker_file_sysuninit(linker_file_t lf) 160{ 161 struct sysinit** start, ** stop; 162 struct sysinit** sipp; 163 struct sysinit** xipp; 164 struct sysinit* save; 165 166 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 167 lf->filename)); 168 169 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0) 170 return; 171 172 /* 173 * Perform a reverse bubble sort of the system initialization objects 174 * by their subsystem (primary key) and order (secondary key). 175 * 176 * Since some things care about execution order, this is the 177 * operation which ensures continued function. 178 */ 179 for (sipp = start; sipp < stop; sipp++) { 180 for (xipp = sipp + 1; xipp < stop; xipp++) { 181 if ((*sipp)->subsystem > (*xipp)->subsystem || 182 ((*sipp)->subsystem == (*xipp)->subsystem && 183 (*sipp)->order >= (*xipp)->order)) 184 continue; /* skip*/ 185 save = *sipp; 186 *sipp = *xipp; 187 *xipp = save; 188 } 189 } 190 191 /* 192 * Traverse the (now) ordered list of system initialization tasks. 193 * Perform each task, and continue on to the next task. 194 */ 195 for (sipp = start; sipp < stop; sipp++) { 196 if ((*sipp)->subsystem == SI_SUB_DUMMY) 197 continue; /* skip dummy task(s)*/ 198 199 /* Call function */ 200 (*((*sipp)->func))((*sipp)->udata); 201 } 202} 203 204static void 205linker_file_register_sysctls(linker_file_t lf) 206{ 207 struct sysctl_oid **start, **stop, **oidp; 208 209 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 210 lf->filename)); 211 212 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 213 return; 214 215 for (oidp = start; oidp < stop; oidp++) 216 sysctl_register_oid(*oidp); 217} 218 219static void 220linker_file_unregister_sysctls(linker_file_t lf) 221{ 222 struct sysctl_oid **start, **stop, **oidp; 223 224 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n", 225 lf->filename)); 226 227 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 228 return; 229 230 for (oidp = start; oidp < stop; oidp++) 231 sysctl_unregister_oid(*oidp); 232} 233 234static int 235linker_file_register_modules(linker_file_t lf) 236{ 237 int error; 238 struct mod_metadata **start, **stop; 239 struct mod_metadata **mdp; 240 const moduledata_t *moddata; 241 242 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n", 243 lf->filename)); 244 245 if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, 0) != 0) { 246 /* 247 * This fallback should be unnecessary, but if we get booted from 248 * boot2 instead of loader and we are missing our metadata then 249 * we have to try the best we can. 250 */ 251 if (lf == linker_kernel_file) { 252 start = SET_BEGIN(modmetadata_set); 253 stop = SET_LIMIT(modmetadata_set); 254 } else { 255 return 0; 256 } 257 } 258 for (mdp = start; mdp < stop; mdp++) { 259 if ((*mdp)->md_type != MDT_MODULE) 260 continue; 261 moddata = (*mdp)->md_data; 262 KLD_DPF(FILE, ("Registering module %s in %s\n", 263 moddata->name, lf->filename)); 264 if (module_lookupbyname(moddata->name) != NULL) { 265 printf("Warning: module %s already exists\n", moddata->name); 266 continue; /* or return a error ? */ 267 } 268 error = module_register(moddata, lf); 269 if (error) 270 printf("Module %s failed to register: %d\n", moddata->name, error); 271 } 272 return 0; 273} 274 275static void 276linker_init_kernel_modules(void) 277{ 278 linker_file_register_modules(linker_kernel_file); 279} 280 281SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0); 282 283int 284linker_load_file(const char* filename, linker_file_t* result) 285{ 286 linker_class_t lc; 287 linker_file_t lf; 288 int foundfile, error = 0; 289 290 /* Refuse to load modules if securelevel raised */ 291 if (securelevel > 0) 292 return EPERM; 293 294 lf = linker_find_file_by_name(filename); 295 if (lf) { 296 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 297 *result = lf; 298 lf->refs++; 299 goto out; 300 } 301 302 lf = NULL; 303 foundfile = 0; 304 TAILQ_FOREACH(lc, &classes, link) { 305 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n", 306 filename)); 307 error = LINKER_LOAD_FILE(lc, filename, &lf); 308 /* 309 * If we got something other than ENOENT, then it exists but we cannot 310 * load it for some other reason. 311 */ 312 if (error != ENOENT) 313 foundfile = 1; 314 if (lf) { 315 linker_file_register_modules(lf); 316 linker_file_register_sysctls(lf); 317 linker_file_sysinit(lf); 318 lf->flags |= LINKER_FILE_LINKED; 319 320 *result = lf; 321 error = 0; 322 goto out; 323 } 324 } 325 /* 326 * Less than ideal, but tells the user whether it failed to load or 327 * the module was not found. 328 */ 329 if (foundfile) 330 error = ENOEXEC; /* Format not recognised (or unloadable) */ 331 else 332 error = ENOENT; /* Nothing found */ 333 334out: 335 return error; 336} 337 338/* XXX: function parameters are incomplete */ 339int 340linker_reference_module(const char *modname, linker_file_t *result) 341{ 342 343 return linker_load_module(NULL, modname, NULL, NULL, result); 344} 345 346linker_file_t 347linker_find_file_by_name(const char* filename) 348{ 349 linker_file_t lf = 0; 350 char *koname; 351 352 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 353 if (koname == NULL) 354 goto out; 355 sprintf(koname, "%s.ko", filename); 356 357 lockmgr(&lock, LK_SHARED, 0, curthread); 358 TAILQ_FOREACH(lf, &linker_files, link) { 359 if (!strcmp(lf->filename, koname)) 360 break; 361 if (!strcmp(lf->filename, filename)) 362 break; 363 } 364 lockmgr(&lock, LK_RELEASE, 0, curthread); 365 366out: 367 if (koname) 368 free(koname, M_LINKER); 369 return lf; 370} 371 372linker_file_t 373linker_find_file_by_id(int fileid) 374{ 375 linker_file_t lf = 0; 376 377 lockmgr(&lock, LK_SHARED, 0, curthread); 378 TAILQ_FOREACH(lf, &linker_files, link) 379 if (lf->id == fileid) 380 break; 381 lockmgr(&lock, LK_RELEASE, 0, curthread); 382 383 return lf; 384} 385 386linker_file_t 387linker_make_file(const char* pathname, linker_class_t lc) 388{ 389 linker_file_t lf = 0; 390 const char *filename; 391 392 filename = linker_basename(pathname); 393 394 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 395 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread); 396 lf = (linker_file_t) kobj_create((kobj_class_t) lc, M_LINKER, M_WAITOK); 397 if (!lf) 398 goto out; 399 400 lf->refs = 1; 401 lf->userrefs = 0; 402 lf->flags = 0; 403 lf->filename = linker_strdup(filename);
| 87/* XXX wrong name; we're looking at version provision tags here, not modules */ 88typedef TAILQ_HEAD(, modlist) modlisthead_t; 89struct modlist { 90 TAILQ_ENTRY(modlist) link; /* chain together all modules */ 91 linker_file_t container; 92 const char *name; 93 int version; 94}; 95typedef struct modlist *modlist_t; 96static modlisthead_t found_modules; 97 98static char * 99linker_strdup(const char *str) 100{ 101 char *result; 102 103 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 104 strcpy(result, str); 105 return(result); 106} 107 108static void 109linker_init(void* arg) 110{ 111 lockinit(&lock, PVM, "klink", 0, 0); 112 TAILQ_INIT(&classes); 113 TAILQ_INIT(&linker_files); 114} 115 116SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0); 117 118int 119linker_add_class(linker_class_t lc) 120{ 121 kobj_class_compile((kobj_class_t) lc); 122 TAILQ_INSERT_TAIL(&classes, lc, link); 123 return 0; 124} 125 126static void 127linker_file_sysinit(linker_file_t lf) 128{ 129 struct sysinit** start, ** stop; 130 struct sysinit** sipp; 131 struct sysinit** xipp; 132 struct sysinit* save; 133 134 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 135 lf->filename)); 136 137 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 138 return; 139 /* 140 * Perform a bubble sort of the system initialization objects by 141 * their subsystem (primary key) and order (secondary key). 142 * 143 * Since some things care about execution order, this is the 144 * operation which ensures continued function. 145 */ 146 for (sipp = start; sipp < stop; sipp++) { 147 for (xipp = sipp + 1; xipp < stop; xipp++) { 148 if ((*sipp)->subsystem < (*xipp)->subsystem || 149 ((*sipp)->subsystem == (*xipp)->subsystem && 150 (*sipp)->order <= (*xipp)->order)) 151 continue; /* skip*/ 152 save = *sipp; 153 *sipp = *xipp; 154 *xipp = save; 155 } 156 } 157 158 159 /* 160 * Traverse the (now) ordered list of system initialization tasks. 161 * Perform each task, and continue on to the next task. 162 */ 163 for (sipp = start; sipp < stop; sipp++) { 164 if ((*sipp)->subsystem == SI_SUB_DUMMY) 165 continue; /* skip dummy task(s)*/ 166 167 /* Call function */ 168 (*((*sipp)->func))((*sipp)->udata); 169 } 170} 171 172static void 173linker_file_sysuninit(linker_file_t lf) 174{ 175 struct sysinit** start, ** stop; 176 struct sysinit** sipp; 177 struct sysinit** xipp; 178 struct sysinit* save; 179 180 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 181 lf->filename)); 182 183 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0) 184 return; 185 186 /* 187 * Perform a reverse bubble sort of the system initialization objects 188 * by their subsystem (primary key) and order (secondary key). 189 * 190 * Since some things care about execution order, this is the 191 * operation which ensures continued function. 192 */ 193 for (sipp = start; sipp < stop; sipp++) { 194 for (xipp = sipp + 1; xipp < stop; xipp++) { 195 if ((*sipp)->subsystem > (*xipp)->subsystem || 196 ((*sipp)->subsystem == (*xipp)->subsystem && 197 (*sipp)->order >= (*xipp)->order)) 198 continue; /* skip*/ 199 save = *sipp; 200 *sipp = *xipp; 201 *xipp = save; 202 } 203 } 204 205 /* 206 * Traverse the (now) ordered list of system initialization tasks. 207 * Perform each task, and continue on to the next task. 208 */ 209 for (sipp = start; sipp < stop; sipp++) { 210 if ((*sipp)->subsystem == SI_SUB_DUMMY) 211 continue; /* skip dummy task(s)*/ 212 213 /* Call function */ 214 (*((*sipp)->func))((*sipp)->udata); 215 } 216} 217 218static void 219linker_file_register_sysctls(linker_file_t lf) 220{ 221 struct sysctl_oid **start, **stop, **oidp; 222 223 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 224 lf->filename)); 225 226 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 227 return; 228 229 for (oidp = start; oidp < stop; oidp++) 230 sysctl_register_oid(*oidp); 231} 232 233static void 234linker_file_unregister_sysctls(linker_file_t lf) 235{ 236 struct sysctl_oid **start, **stop, **oidp; 237 238 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n", 239 lf->filename)); 240 241 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 242 return; 243 244 for (oidp = start; oidp < stop; oidp++) 245 sysctl_unregister_oid(*oidp); 246} 247 248static int 249linker_file_register_modules(linker_file_t lf) 250{ 251 int error; 252 struct mod_metadata **start, **stop; 253 struct mod_metadata **mdp; 254 const moduledata_t *moddata; 255 256 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n", 257 lf->filename)); 258 259 if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, 0) != 0) { 260 /* 261 * This fallback should be unnecessary, but if we get booted from 262 * boot2 instead of loader and we are missing our metadata then 263 * we have to try the best we can. 264 */ 265 if (lf == linker_kernel_file) { 266 start = SET_BEGIN(modmetadata_set); 267 stop = SET_LIMIT(modmetadata_set); 268 } else { 269 return 0; 270 } 271 } 272 for (mdp = start; mdp < stop; mdp++) { 273 if ((*mdp)->md_type != MDT_MODULE) 274 continue; 275 moddata = (*mdp)->md_data; 276 KLD_DPF(FILE, ("Registering module %s in %s\n", 277 moddata->name, lf->filename)); 278 if (module_lookupbyname(moddata->name) != NULL) { 279 printf("Warning: module %s already exists\n", moddata->name); 280 continue; /* or return a error ? */ 281 } 282 error = module_register(moddata, lf); 283 if (error) 284 printf("Module %s failed to register: %d\n", moddata->name, error); 285 } 286 return 0; 287} 288 289static void 290linker_init_kernel_modules(void) 291{ 292 linker_file_register_modules(linker_kernel_file); 293} 294 295SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0); 296 297int 298linker_load_file(const char* filename, linker_file_t* result) 299{ 300 linker_class_t lc; 301 linker_file_t lf; 302 int foundfile, error = 0; 303 304 /* Refuse to load modules if securelevel raised */ 305 if (securelevel > 0) 306 return EPERM; 307 308 lf = linker_find_file_by_name(filename); 309 if (lf) { 310 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 311 *result = lf; 312 lf->refs++; 313 goto out; 314 } 315 316 lf = NULL; 317 foundfile = 0; 318 TAILQ_FOREACH(lc, &classes, link) { 319 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n", 320 filename)); 321 error = LINKER_LOAD_FILE(lc, filename, &lf); 322 /* 323 * If we got something other than ENOENT, then it exists but we cannot 324 * load it for some other reason. 325 */ 326 if (error != ENOENT) 327 foundfile = 1; 328 if (lf) { 329 linker_file_register_modules(lf); 330 linker_file_register_sysctls(lf); 331 linker_file_sysinit(lf); 332 lf->flags |= LINKER_FILE_LINKED; 333 334 *result = lf; 335 error = 0; 336 goto out; 337 } 338 } 339 /* 340 * Less than ideal, but tells the user whether it failed to load or 341 * the module was not found. 342 */ 343 if (foundfile) 344 error = ENOEXEC; /* Format not recognised (or unloadable) */ 345 else 346 error = ENOENT; /* Nothing found */ 347 348out: 349 return error; 350} 351 352/* XXX: function parameters are incomplete */ 353int 354linker_reference_module(const char *modname, linker_file_t *result) 355{ 356 357 return linker_load_module(NULL, modname, NULL, NULL, result); 358} 359 360linker_file_t 361linker_find_file_by_name(const char* filename) 362{ 363 linker_file_t lf = 0; 364 char *koname; 365 366 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 367 if (koname == NULL) 368 goto out; 369 sprintf(koname, "%s.ko", filename); 370 371 lockmgr(&lock, LK_SHARED, 0, curthread); 372 TAILQ_FOREACH(lf, &linker_files, link) { 373 if (!strcmp(lf->filename, koname)) 374 break; 375 if (!strcmp(lf->filename, filename)) 376 break; 377 } 378 lockmgr(&lock, LK_RELEASE, 0, curthread); 379 380out: 381 if (koname) 382 free(koname, M_LINKER); 383 return lf; 384} 385 386linker_file_t 387linker_find_file_by_id(int fileid) 388{ 389 linker_file_t lf = 0; 390 391 lockmgr(&lock, LK_SHARED, 0, curthread); 392 TAILQ_FOREACH(lf, &linker_files, link) 393 if (lf->id == fileid) 394 break; 395 lockmgr(&lock, LK_RELEASE, 0, curthread); 396 397 return lf; 398} 399 400linker_file_t 401linker_make_file(const char* pathname, linker_class_t lc) 402{ 403 linker_file_t lf = 0; 404 const char *filename; 405 406 filename = linker_basename(pathname); 407 408 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 409 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread); 410 lf = (linker_file_t) kobj_create((kobj_class_t) lc, M_LINKER, M_WAITOK); 411 if (!lf) 412 goto out; 413 414 lf->refs = 1; 415 lf->userrefs = 0; 416 lf->flags = 0; 417 lf->filename = linker_strdup(filename);
|
405 lf->ndeps = 0; 406 lf->deps = NULL; 407 STAILQ_INIT(&lf->common); 408 TAILQ_INIT(&lf->modules); 409 410 TAILQ_INSERT_TAIL(&linker_files, lf, link); 411 412out: 413 lockmgr(&lock, LK_RELEASE, 0, curthread); 414 return lf; 415} 416 417int 418linker_file_unload(linker_file_t file) 419{ 420 module_t mod, next; 421 modlist_t ml, nextml; 422 struct common_symbol* cp; 423 int error = 0; 424 int i; 425 426 /* Refuse to unload modules if securelevel raised */ 427 if (securelevel > 0) 428 return EPERM; 429 430 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 431 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread); 432 if (file->refs == 1) { 433 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 434 /* 435 * Inform any modules associated with this file. 436 */ 437 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 438 next = module_getfnext(mod); 439 440 /* 441 * Give the module a chance to veto the unload. 442 */ 443 if ((error = module_unload(mod)) != 0) { 444 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 445 mod)); 446 lockmgr(&lock, LK_RELEASE, 0, curthread); 447 goto out; 448 } 449 450 module_release(mod); 451 } 452 } 453 454 file->refs--; 455 if (file->refs > 0) { 456 lockmgr(&lock, LK_RELEASE, 0, curthread); 457 goto out; 458 } 459 460 for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) { 461 nextml = TAILQ_NEXT(ml, link); 462 if (ml->container == file) { 463 TAILQ_REMOVE(&found_modules, ml, link); 464 } 465 } 466 467 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 468 if (file->flags & LINKER_FILE_LINKED) { 469 linker_file_sysuninit(file); 470 linker_file_unregister_sysctls(file); 471 } 472 473 TAILQ_REMOVE(&linker_files, file, link); 474 lockmgr(&lock, LK_RELEASE, 0, curthread); 475 476 if (file->deps) { 477 for (i = 0; i < file->ndeps; i++) 478 linker_file_unload(file->deps[i]); 479 free(file->deps, M_LINKER); 480 file->deps = NULL; 481 } 482 483 for (cp = STAILQ_FIRST(&file->common); cp; 484 cp = STAILQ_FIRST(&file->common)) { 485 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 486 free(cp, M_LINKER); 487 } 488 489 LINKER_UNLOAD(file); 490 if (file->filename) { 491 free(file->filename, M_LINKER); 492 file->filename = NULL; 493 } 494 kobj_delete((kobj_t) file, M_LINKER); 495 496out: 497 return error; 498} 499 500int 501linker_file_add_dependency(linker_file_t file, linker_file_t dep) 502{ 503 linker_file_t* newdeps; 504 505 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 506 M_LINKER, M_WAITOK | M_ZERO); 507 if (newdeps == NULL) 508 return ENOMEM; 509 510 if (file->deps) { 511 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 512 free(file->deps, M_LINKER); 513 } 514 file->deps = newdeps; 515 file->deps[file->ndeps] = dep; 516 file->ndeps++; 517 518 return 0; 519} 520 521/* 522 * Locate a linker set and its contents. 523 * This is a helper function to avoid linker_if.h exposure elsewhere. 524 * Note: firstp and lastp are really void *** 525 */ 526int 527linker_file_lookup_set(linker_file_t file, const char *name, 528 void *firstp, void *lastp, int *countp) 529{ 530 531 return LINKER_LOOKUP_SET(file, name, firstp, lastp, countp); 532} 533 534caddr_t 535linker_file_lookup_symbol(linker_file_t file, const char* name, int deps) 536{ 537 c_linker_sym_t sym; 538 linker_symval_t symval; 539 caddr_t address; 540 size_t common_size = 0; 541 int i; 542 543 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 544 file, name, deps)); 545 546 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 547 LINKER_SYMBOL_VALUES(file, sym, &symval); 548 if (symval.value == 0) 549 /* 550 * For commons, first look them up in the dependencies and 551 * only allocate space if not found there. 552 */ 553 common_size = symval.size; 554 else { 555 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 556 return symval.value; 557 } 558 } 559 560 if (deps) { 561 for (i = 0; i < file->ndeps; i++) { 562 address = linker_file_lookup_symbol(file->deps[i], name, 0); 563 if (address) { 564 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); 565 return address; 566 } 567 } 568 } 569 570 if (common_size > 0) { 571 /* 572 * This is a common symbol which was not found in the 573 * dependencies. We maintain a simple common symbol table in 574 * the file object. 575 */ 576 struct common_symbol* cp; 577 578 STAILQ_FOREACH(cp, &file->common, link) 579 if (!strcmp(cp->name, name)) { 580 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 581 return cp->address; 582 } 583 584 /* 585 * Round the symbol size up to align. 586 */ 587 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 588 cp = malloc(sizeof(struct common_symbol) 589 + common_size 590 + strlen(name) + 1, 591 M_LINKER, M_WAITOK | M_ZERO); 592 if (!cp) { 593 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 594 return 0; 595 } 596 597 cp->address = (caddr_t) (cp + 1); 598 cp->name = cp->address + common_size; 599 strcpy(cp->name, name); 600 bzero(cp->address, common_size); 601 STAILQ_INSERT_TAIL(&file->common, cp, link); 602 603 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 604 return cp->address; 605 } 606 607 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 608 return 0; 609} 610 611#ifdef DDB 612/* 613 * DDB Helpers. DDB has to look across multiple files with their own 614 * symbol tables and string tables. 615 * 616 * Note that we do not obey list locking protocols here. We really don't 617 * need DDB to hang because somebody's got the lock held. We'll take the 618 * chance that the files list is inconsistant instead. 619 */ 620 621int 622linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 623{ 624 linker_file_t lf; 625 626 TAILQ_FOREACH(lf, &linker_files, link) { 627 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 628 return 0; 629 } 630 return ENOENT; 631} 632 633int 634linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 635{ 636 linker_file_t lf; 637 u_long off = (uintptr_t)value; 638 u_long diff, bestdiff; 639 c_linker_sym_t best; 640 c_linker_sym_t es; 641 642 best = 0; 643 bestdiff = off; 644 TAILQ_FOREACH(lf, &linker_files, link) { 645 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 646 continue; 647 if (es != 0 && diff < bestdiff) { 648 best = es; 649 bestdiff = diff; 650 } 651 if (bestdiff == 0) 652 break; 653 } 654 if (best) { 655 *sym = best; 656 *diffp = bestdiff; 657 return 0; 658 } else { 659 *sym = 0; 660 *diffp = off; 661 return ENOENT; 662 } 663} 664 665int 666linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 667{ 668 linker_file_t lf; 669 670 TAILQ_FOREACH(lf, &linker_files, link) { 671 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 672 return 0; 673 } 674 return ENOENT; 675} 676 677#endif 678 679/* 680 * Syscalls. 681 */ 682/* 683 * MPSAFE 684 */ 685int 686kldload(struct thread* td, struct kldload_args* uap) 687{ 688 char *kldname, *modname; 689 char *pathname = NULL; 690 linker_file_t lf; 691 int error = 0; 692 693 td->td_retval[0] = -1; 694 695 if (securelevel > 0) /* redundant, but that's OK */ 696 return EPERM; 697 698 mtx_lock(&Giant); 699 700 if ((error = suser_xxx(td->td_proc->p_ucred, NULL, 0)) != 0) 701 goto out; 702 703 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 704 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0) 705 goto out; 706 707 /* 708 * If path do not contain qualified name or any dot in it (kldname.ko, or 709 * kldname.ver.ko) treat it as interface name. 710 */ 711 if (index(pathname, '/') || index(pathname, '.')) { 712 kldname = pathname; 713 modname = NULL; 714 } else { 715 kldname = NULL; 716 modname = pathname; 717 } 718 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 719 if (error) 720 goto out; 721 722 lf->userrefs++; 723 td->td_retval[0] = lf->id; 724 725out: 726 if (pathname) 727 free(pathname, M_TEMP); 728 mtx_unlock(&Giant); 729 return (error); 730} 731 732/* 733 * MPSAFE 734 */ 735int 736kldunload(struct thread* td, struct kldunload_args* uap) 737{ 738 linker_file_t lf; 739 int error = 0; 740 741 if (securelevel > 0) /* redundant, but that's OK */ 742 return EPERM; 743 744 mtx_lock(&Giant); 745 746 if ((error = suser_xxx(td->td_proc->p_ucred, NULL, 0)) != 0) 747 goto out; 748 749 lf = linker_find_file_by_id(SCARG(uap, fileid)); 750 if (lf) { 751 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 752 if (lf->userrefs == 0) { 753 printf("kldunload: attempt to unload file that was loaded by the kernel\n"); 754 error = EBUSY; 755 goto out; 756 } 757 lf->userrefs--; 758 error = linker_file_unload(lf); 759 if (error) 760 lf->userrefs++; 761 } else { 762 error = ENOENT; 763 } 764out: 765 mtx_unlock(&Giant); 766 return (error); 767} 768 769/* 770 * MPSAFE 771 */ 772int 773kldfind(struct thread* td, struct kldfind_args* uap) 774{ 775 char* pathname; 776 const char *filename; 777 linker_file_t lf; 778 int error = 0; 779 780 mtx_lock(&Giant); 781 td->td_retval[0] = -1; 782 783 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 784 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0) 785 goto out; 786 787 filename = linker_basename(pathname); 788 789 lf = linker_find_file_by_name(filename); 790 if (lf) 791 td->td_retval[0] = lf->id; 792 else 793 error = ENOENT; 794 795out: 796 if (pathname) 797 free(pathname, M_TEMP); 798 mtx_unlock(&Giant); 799 return (error); 800} 801 802/* 803 * MPSAFE 804 */ 805int 806kldnext(struct thread* td, struct kldnext_args* uap) 807{ 808 linker_file_t lf; 809 int error = 0; 810 811 mtx_lock(&Giant); 812 813 if (SCARG(uap, fileid) == 0) { 814 if (TAILQ_FIRST(&linker_files)) 815 td->td_retval[0] = TAILQ_FIRST(&linker_files)->id; 816 else 817 td->td_retval[0] = 0; 818 goto out; 819 } 820 821 lf = linker_find_file_by_id(SCARG(uap, fileid)); 822 if (lf) { 823 if (TAILQ_NEXT(lf, link)) 824 td->td_retval[0] = TAILQ_NEXT(lf, link)->id; 825 else 826 td->td_retval[0] = 0; 827 } else { 828 error = ENOENT; 829 } 830out: 831 mtx_unlock(&Giant); 832 return (error); 833} 834 835/* 836 * MPSAFE 837 */ 838int 839kldstat(struct thread* td, struct kldstat_args* uap) 840{ 841 linker_file_t lf; 842 int error = 0; 843 int version; 844 struct kld_file_stat* stat; 845 int namelen; 846 847 mtx_lock(&Giant); 848 849 lf = linker_find_file_by_id(SCARG(uap, fileid)); 850 if (!lf) { 851 error = ENOENT; 852 goto out; 853 } 854 855 stat = SCARG(uap, stat); 856 857 /* 858 * Check the version of the user's structure. 859 */ 860 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 861 goto out; 862 if (version != sizeof(struct kld_file_stat)) { 863 error = EINVAL; 864 goto out; 865 } 866 867 namelen = strlen(lf->filename) + 1; 868 if (namelen > MAXPATHLEN) 869 namelen = MAXPATHLEN; 870 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 871 goto out; 872 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 873 goto out; 874 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 875 goto out; 876 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 877 goto out; 878 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 879 goto out; 880 881 td->td_retval[0] = 0; 882 883out: 884 mtx_unlock(&Giant); 885 return (error); 886} 887 888/* 889 * MPSAFE 890 */ 891int 892kldfirstmod(struct thread* td, struct kldfirstmod_args* uap) 893{ 894 linker_file_t lf; 895 int error = 0; 896 897 mtx_lock(&Giant); 898 lf = linker_find_file_by_id(SCARG(uap, fileid)); 899 if (lf) { 900 if (TAILQ_FIRST(&lf->modules)) 901 td->td_retval[0] = module_getid(TAILQ_FIRST(&lf->modules)); 902 else 903 td->td_retval[0] = 0; 904 } else { 905 error = ENOENT; 906 } 907 mtx_unlock(&Giant); 908 return (error); 909} 910 911/* 912 * MPSAFE 913 */ 914int 915kldsym(struct thread *td, struct kldsym_args *uap) 916{ 917 char *symstr = NULL; 918 c_linker_sym_t sym; 919 linker_symval_t symval; 920 linker_file_t lf; 921 struct kld_sym_lookup lookup; 922 int error = 0; 923 924 mtx_lock(&Giant); 925 926 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0) 927 goto out; 928 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) { 929 error = EINVAL; 930 goto out; 931 } 932 933 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 934 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 935 goto out; 936 937 if (SCARG(uap, fileid) != 0) { 938 lf = linker_find_file_by_id(SCARG(uap, fileid)); 939 if (lf == NULL) { 940 error = ENOENT; 941 goto out; 942 } 943 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 944 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 945 lookup.symvalue = (uintptr_t)symval.value; 946 lookup.symsize = symval.size; 947 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 948 } else 949 error = ENOENT; 950 } else { 951 TAILQ_FOREACH(lf, &linker_files, link) { 952 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 953 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 954 lookup.symvalue = (uintptr_t)symval.value; 955 lookup.symsize = symval.size; 956 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 957 break; 958 } 959 } 960 if (!lf) 961 error = ENOENT; 962 } 963out: 964 if (symstr) 965 free(symstr, M_TEMP); 966 mtx_unlock(&Giant); 967 return (error); 968} 969 970/* 971 * Preloaded module support 972 */ 973 974static modlist_t 975modlist_lookup(const char *name, int ver) 976{ 977 modlist_t mod; 978 979 TAILQ_FOREACH(mod, &found_modules, link) { 980 if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver)) 981 return mod; 982 } 983 return NULL; 984} 985 986static modlist_t 987modlist_lookup2(const char *name, struct mod_depend *verinfo) 988{ 989 modlist_t mod, bestmod; 990 int ver; 991 992 if (verinfo == NULL) 993 return modlist_lookup(name, 0); 994 bestmod = NULL; 995 for (mod = TAILQ_FIRST(&found_modules); mod; mod = TAILQ_NEXT(mod, link)) { 996 if (strcmp(mod->name, name) != 0) 997 continue; 998 ver = mod->version; 999 if (ver == verinfo->md_ver_preferred) 1000 return mod; 1001 if (ver >= verinfo->md_ver_minimum && 1002 ver <= verinfo->md_ver_maximum && 1003 ver > bestmod->version) 1004 bestmod = mod; 1005 } 1006 return bestmod; 1007} 1008 1009static modlist_t 1010modlist_newmodule(const char *modname, int version, linker_file_t container) 1011{ 1012 modlist_t mod; 1013 1014 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT); 1015 if (mod == NULL) 1016 panic("no memory for module list"); 1017 bzero(mod, sizeof(*mod)); 1018 mod->container = container; 1019 mod->name = modname; 1020 mod->version = version; 1021 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1022 return mod; 1023} 1024 1025/* 1026 * This routine is cheap and nasty but will work for data pointers. 1027 */ 1028static void * 1029linker_reloc_ptr(linker_file_t lf, const void *offset) 1030{ 1031 return lf->address + (uintptr_t)offset; 1032} 1033 1034/* 1035 * Dereference MDT_VERSION metadata into module name and version 1036 */ 1037static void 1038linker_mdt_version(linker_file_t lf, struct mod_metadata *mp, 1039 const char **modname, int *version) 1040{ 1041 struct mod_version *mvp; 1042 1043 if (modname) 1044 *modname = linker_reloc_ptr(lf, mp->md_cval); 1045 if (version) { 1046 mvp = linker_reloc_ptr(lf, mp->md_data); 1047 *version = mvp->mv_version; 1048 } 1049} 1050 1051/* 1052 * Dereference MDT_DEPEND metadata into module name and mod_depend structure 1053 */ 1054static void 1055linker_mdt_depend(linker_file_t lf, struct mod_metadata *mp, 1056 const char **modname, struct mod_depend **verinfo) 1057{ 1058 1059 if (modname) 1060 *modname = linker_reloc_ptr(lf, mp->md_cval); 1061 if (verinfo) 1062 *verinfo = linker_reloc_ptr(lf, mp->md_data); 1063} 1064 1065static void 1066linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1067 struct mod_metadata **stop, int preload) 1068{ 1069 struct mod_metadata *mp, **mdp; 1070 const char *modname; 1071 int ver; 1072 1073 for (mdp = start; mdp < stop; mdp++) { 1074 if (preload) 1075 mp = *mdp; 1076 else 1077 mp = linker_reloc_ptr(lf, *mdp); 1078 if (mp->md_type != MDT_VERSION) 1079 continue; 1080 if (preload) { 1081 modname = mp->md_cval; 1082 ver = ((struct mod_version*)mp->md_data)->mv_version; 1083 } else 1084 linker_mdt_version(lf, mp, &modname, &ver); 1085 if (modlist_lookup(modname, ver) != NULL) { 1086 printf("module %s already present!\n", modname); 1087 /* XXX what can we do? this is a build error. :-( */ 1088 continue; 1089 } 1090 modlist_newmodule(modname, ver, lf); 1091 } 1092} 1093 1094static void 1095linker_preload(void* arg) 1096{ 1097 caddr_t modptr; 1098 const char *modname, *nmodname; 1099 char *modtype; 1100 linker_file_t lf; 1101 linker_class_t lc; 1102 int error; 1103 linker_file_list_t loaded_files; 1104 linker_file_list_t depended_files; 1105 struct mod_metadata *mp, *nmp; 1106 struct mod_metadata **start, **stop, **mdp, **nmdp; 1107 struct mod_depend *verinfo; 1108 int nver; 1109 int resolves; 1110 modlist_t mod; 1111 struct sysinit **si_start, **si_stop; 1112 1113 TAILQ_INIT(&loaded_files); 1114 TAILQ_INIT(&depended_files); 1115 TAILQ_INIT(&found_modules); 1116 error = 0; 1117 1118 modptr = NULL; 1119 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1120 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1121 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1122 if (modname == NULL) { 1123 printf("Preloaded module at %p does not have a name!\n", modptr); 1124 continue; 1125 } 1126 if (modtype == NULL) { 1127 printf("Preloaded module at %p does not have a type!\n", modptr); 1128 continue; 1129 } 1130 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 1131 lf = NULL; 1132 TAILQ_FOREACH(lc, &classes, link) { 1133 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1134 if (error) { 1135 lf = NULL; 1136 break; 1137 } 1138 } 1139 if (lf) 1140 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1141 } 1142 1143 /* 1144 * First get a list of stuff in the kernel. 1145 */ 1146 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, &stop, 1147 NULL) == 0) 1148 linker_addmodules(linker_kernel_file, start, stop, 1); 1149 1150 /* 1151 * this is a once-off kinky bubble sort 1152 * resolve relocation dependency requirements 1153 */ 1154restart: 1155 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1156 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL); 1157 /* 1158 * First, look to see if we would successfully link with this stuff. 1159 */ 1160 resolves = 1; /* unless we know otherwise */ 1161 if (!error) { 1162 for (mdp = start; mdp < stop; mdp++) { 1163 mp = linker_reloc_ptr(lf, *mdp); 1164 if (mp->md_type != MDT_DEPEND) 1165 continue; 1166 linker_mdt_depend(lf, mp, &modname, &verinfo); 1167 for (nmdp = start; nmdp < stop; nmdp++) { 1168 nmp = linker_reloc_ptr(lf, *nmdp); 1169 if (nmp->md_type != MDT_VERSION) 1170 continue; 1171 linker_mdt_version(lf, nmp, &nmodname, NULL); 1172 nmodname = linker_reloc_ptr(lf, nmp->md_cval); 1173 if (strcmp(modname, nmodname) == 0) 1174 break; 1175 } 1176 if (nmdp < stop) /* it's a self reference */ 1177 continue; 1178 if (modlist_lookup2(modname, verinfo) == NULL) { 1179 /* ok, the module isn't here yet, we are not finished */ 1180 resolves = 0; 1181 } 1182 } 1183 } 1184 /* 1185 * OK, if we found our modules, we can link. So, "provide" the 1186 * modules inside and add it to the end of the link order list. 1187 */ 1188 if (resolves) { 1189 if (!error) { 1190 for (mdp = start; mdp < stop; mdp++) { 1191 mp = linker_reloc_ptr(lf, *mdp); 1192 if (mp->md_type != MDT_VERSION) 1193 continue; 1194 linker_mdt_version(lf, mp, &modname, &nver); 1195 if (modlist_lookup(modname, nver) != NULL) { 1196 printf("module %s already present!\n", modname); 1197 linker_file_unload(lf); 1198 TAILQ_REMOVE(&loaded_files, lf, loaded); 1199 goto restart; /* we changed the tailq next ptr */ 1200 } 1201 modlist_newmodule(modname, nver, lf); 1202 } 1203 } 1204 TAILQ_REMOVE(&loaded_files, lf, loaded); 1205 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1206 /* 1207 * Since we provided modules, we need to restart the sort so 1208 * that the previous files that depend on us have a chance. 1209 * Also, we've busted the tailq next pointer with the REMOVE. 1210 */ 1211 goto restart; 1212 } 1213 } 1214 1215 /* 1216 * At this point, we check to see what could not be resolved.. 1217 */ 1218 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1219 printf("KLD file %s is missing dependencies\n", lf->filename); 1220 linker_file_unload(lf); 1221 TAILQ_REMOVE(&loaded_files, lf, loaded); 1222 } 1223 1224 /* 1225 * We made it. Finish off the linking in the order we determined. 1226 */ 1227 TAILQ_FOREACH(lf, &depended_files, loaded) { 1228 if (linker_kernel_file) { 1229 linker_kernel_file->refs++; 1230 error = linker_file_add_dependency(lf, linker_kernel_file); 1231 if (error) 1232 panic("cannot add dependency"); 1233 } 1234 lf->userrefs++; /* so we can (try to) kldunload it */ 1235 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL); 1236 if (!error) { 1237 for (mdp = start; mdp < stop; mdp++) { 1238 mp = linker_reloc_ptr(lf, *mdp); 1239 if (mp->md_type != MDT_DEPEND) 1240 continue; 1241 linker_mdt_depend(lf, mp, &modname, &verinfo); 1242 mod = modlist_lookup2(modname, verinfo); 1243 mod->container->refs++; 1244 error = linker_file_add_dependency(lf, mod->container); 1245 if (error) 1246 panic("cannot add dependency"); 1247 } 1248 } 1249 1250 /* 1251 * Now do relocation etc using the symbol search paths established by 1252 * the dependencies 1253 */ 1254 error = LINKER_LINK_PRELOAD_FINISH(lf); 1255 if (error) { 1256 printf("KLD file %s - could not finalize loading\n", lf->filename); 1257 linker_file_unload(lf); 1258 continue; 1259 } 1260 1261 linker_file_register_modules(lf); 1262 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) 1263 sysinit_add(si_start, si_stop); 1264 linker_file_register_sysctls(lf); 1265 lf->flags |= LINKER_FILE_LINKED; 1266 } 1267 /* woohoo! we made it! */ 1268} 1269 1270SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1271 1272/* 1273 * Search for a not-loaded module by name. 1274 * 1275 * Modules may be found in the following locations: 1276 * 1277 * - preloaded (result is just the module name) 1278 * - on disk (result is full path to module) 1279 * 1280 * If the module name is qualified in any way (contains path, etc.) 1281 * the we simply return a copy of it. 1282 * 1283 * The search path can be manipulated via sysctl. Note that we use the ';' 1284 * character as a separator to be consistent with the bootloader. 1285 */ 1286 1287static char linker_hintfile[] = "linker.hints"; 1288static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules;/modules"; 1289 1290SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1291 sizeof(linker_path), "module load search path"); 1292 1293TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1294 1295static char *linker_ext_list[] = { 1296 "", 1297 ".ko", 1298 NULL 1299}; 1300 1301/* 1302 * Check if file actually exists either with or without extension listed 1303 * in the linker_ext_list. 1304 * (probably should be generic for the rest of the kernel) 1305 */ 1306static char * 1307linker_lookup_file(const char *path, int pathlen, 1308 const char *name, int namelen, struct vattr *vap) 1309{ 1310 struct nameidata nd; 1311 struct thread *td = curthread; /* XXX */ 1312 char *result, **cpp, *sep; 1313 int error, len, extlen, reclen, flags; 1314 enum vtype type; 1315 1316 extlen = 0; 1317 for (cpp = linker_ext_list; *cpp; cpp++) { 1318 len = strlen(*cpp); 1319 if (len > extlen) 1320 extlen = len; 1321 } 1322 extlen++; /* trailing '\0' */ 1323 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1324 1325 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1326 result = malloc(reclen, M_LINKER, M_WAITOK); 1327 for (cpp = linker_ext_list; *cpp; cpp++) { 1328 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1329 namelen, name, *cpp); 1330 /* 1331 * Attempt to open the file, and return the path if we succeed 1332 * and it's a regular file. 1333 */ 1334 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td); 1335 flags = FREAD; 1336 error = vn_open(&nd, &flags, 0); 1337 if (error == 0) { 1338 NDFREE(&nd, NDF_ONLY_PNBUF); 1339 type = nd.ni_vp->v_type; 1340 if (vap) 1341 VOP_GETATTR(nd.ni_vp, vap, td->td_proc->p_ucred, td); 1342 VOP_UNLOCK(nd.ni_vp, 0, td); 1343 vn_close(nd.ni_vp, FREAD, td->td_proc->p_ucred, td); 1344 if (type == VREG) 1345 return(result); 1346 } 1347 } 1348 free(result, M_LINKER); 1349 return(NULL); 1350} 1351 1352#define INT_ALIGN(base, ptr) ptr = \ 1353 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1354 1355/* 1356 * Lookup KLD which contains requested module in the "linker.hints" file. 1357 * If version specification is available, then try to find the best KLD. 1358 * Otherwise just find the latest one. 1359 */ 1360static char * 1361linker_hints_lookup(const char *path, int pathlen, 1362 const char *modname, int modnamelen, 1363 struct mod_depend *verinfo) 1364{ 1365 struct thread *td = curthread; /* XXX */ 1366 struct ucred *cred = td ? td->td_proc->p_ucred : NULL; 1367 struct nameidata nd; 1368 struct vattr vattr, mattr; 1369 u_char *hints = NULL; 1370 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1371 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1372 1373 result = NULL; 1374 bestver = found = 0; 1375 1376 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1377 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1378 strlen(sep) + 1; 1379 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1380 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, linker_hintfile); 1381 1382 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td); 1383 flags = FREAD; 1384 error = vn_open(&nd, &flags, 0); 1385 if (error) 1386 goto bad; 1387 NDFREE(&nd, NDF_ONLY_PNBUF); 1388 VOP_UNLOCK(nd.ni_vp, 0, td); 1389 if (nd.ni_vp->v_type != VREG) 1390 goto bad; 1391 best = cp = NULL; 1392 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td); 1393 if (error) 1394 goto bad; 1395 /* 1396 * XXX: we need to limit this number to some reasonable value 1397 */ 1398 if (vattr.va_size > 100 * 1024) { 1399 printf("hints file too large %ld\n", (long)vattr.va_size); 1400 goto bad; 1401 } 1402 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1403 if (hints == NULL) 1404 goto bad; 1405 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1406 UIO_SYSSPACE, IO_NODELOCKED, cred, &reclen, td); 1407 if (error) 1408 goto bad; 1409 vn_close(nd.ni_vp, FREAD, cred, td); 1410 nd.ni_vp = NULL; 1411 if (reclen != 0) { 1412 printf("can't read %d\n", reclen); 1413 goto bad; 1414 } 1415 intp = (int*)hints; 1416 ival = *intp++; 1417 if (ival != LINKER_HINTS_VERSION) { 1418 printf("hints file version mismatch %d\n", ival); 1419 goto bad; 1420 } 1421 bufend = hints + vattr.va_size; 1422 recptr = (u_char*)intp; 1423 clen = blen = 0; 1424 while (recptr < bufend && !found) { 1425 intp = (int*)recptr; 1426 reclen = *intp++; 1427 ival = *intp++; 1428 cp = (char*)intp; 1429 switch (ival) { 1430 case MDT_VERSION: 1431 clen = *cp++; 1432 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1433 break; 1434 cp += clen; 1435 INT_ALIGN(hints, cp); 1436 ival = *(int*)cp; 1437 cp += sizeof(int); 1438 clen = *cp++; 1439 if (verinfo == NULL || ival == verinfo->md_ver_preferred) { 1440 found = 1; 1441 break; 1442 } 1443 if (ival >= verinfo->md_ver_minimum && 1444 ival <= verinfo->md_ver_maximum && 1445 ival > bestver) { 1446 bestver = ival; 1447 best = cp; 1448 blen = clen; 1449 } 1450 break; 1451 default: 1452 break; 1453 } 1454 recptr += reclen + sizeof(int); 1455 } 1456 /* 1457 * Finally check if KLD is in the place 1458 */ 1459 if (found) 1460 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1461 else if (best) 1462 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1463 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) { 1464 /* 1465 * KLD is newer than hints file. What we should do now ? 1466 */ 1467 printf("warning: KLD '%s' is newer than the linker.hints file\n", 1468 result); 1469 } 1470bad: 1471 if (hints) 1472 free(hints, M_TEMP); 1473 if (nd.ni_vp != NULL) 1474 vn_close(nd.ni_vp, FREAD, cred, td); 1475 /* 1476 * If nothing found or hints is absent - fallback to the old way 1477 * by using "kldname[.ko]" as module name. 1478 */ 1479 if (!found && !bestver && result == NULL) 1480 result = linker_lookup_file(path, pathlen, modname, modnamelen, NULL); 1481 return result; 1482} 1483 1484/* 1485 * Lookup KLD which contains requested module in the all directories. 1486 */ 1487static char * 1488linker_search_module(const char *modname, int modnamelen, 1489 struct mod_depend *verinfo) 1490{ 1491 char *cp, *ep, *result; 1492 1493 /* 1494 * traverse the linker path 1495 */ 1496 for (cp = linker_path; *cp; cp = ep + 1) { 1497 1498 /* find the end of this component */ 1499 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 1500 ; 1501 result = linker_hints_lookup(cp, ep - cp, modname, modnamelen, verinfo); 1502 if (result != NULL) 1503 return(result); 1504 if (*ep == 0) 1505 break; 1506 } 1507 return (NULL); 1508} 1509 1510/* 1511 * Search for module in all directories listed in the linker_path. 1512 */ 1513static char * 1514linker_search_kld(const char *name) 1515{ 1516 char *cp, *ep, *result, **cpp; 1517 int extlen, len; 1518 1519 /* qualified at all? */ 1520 if (index(name, '/')) 1521 return(linker_strdup(name)); 1522 1523 extlen = 0; 1524 for (cpp = linker_ext_list; *cpp; cpp++) { 1525 len = strlen(*cpp); 1526 if (len > extlen) 1527 extlen = len; 1528 } 1529 extlen++; /* trailing '\0' */ 1530 1531 /* traverse the linker path */ 1532 len = strlen(name); 1533 for (ep = linker_path; *ep; ep++) { 1534 cp = ep; 1535 /* find the end of this component */ 1536 for (; *ep != 0 && *ep != ';'; ep++) 1537 ; 1538 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1539 if (result != NULL) 1540 return(result); 1541 } 1542 return(NULL); 1543} 1544 1545static const char * 1546linker_basename(const char* path) 1547{ 1548 const char *filename; 1549 1550 filename = rindex(path, '/'); 1551 if (filename == NULL) 1552 return path; 1553 if (filename[1]) 1554 filename++; 1555 return filename; 1556} 1557 1558/* 1559 * Find a file which contains given module and load it, 1560 * if "parent" is not NULL, register a reference to it. 1561 */ 1562static int 1563linker_load_module(const char *kldname, const char *modname, 1564 struct linker_file *parent, struct mod_depend *verinfo, 1565 struct linker_file **lfpp) 1566{ 1567 linker_file_t lfdep; 1568 const char *filename; 1569 char *pathname; 1570 int error; 1571 1572 if (modname == NULL) { 1573 /* 1574 * We have to load KLD 1575 */ 1576 KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL")); 1577 pathname = linker_search_kld(kldname); 1578 } else { 1579 if (modlist_lookup2(modname, verinfo) != NULL) 1580 return (EEXIST); 1581 if (kldname == NULL) { 1582 /* 1583 * Need to find a KLD with required module 1584 */ 1585 pathname = linker_search_module(modname, strlen(modname), verinfo); 1586 } else 1587 pathname = linker_strdup(kldname); 1588 } 1589 if (pathname == NULL) 1590 return (ENOENT); 1591 1592 /* 1593 * Can't load more than one file with the same basename 1594 * XXX: Actually it should be possible to have multiple KLDs 1595 * with the same basename but different path because they can provide 1596 * different versions of the same modules. 1597 */ 1598 filename = linker_basename(pathname); 1599 if (linker_find_file_by_name(filename)) { 1600 error = EEXIST; 1601 goto out; 1602 } 1603 1604 do { 1605 error = linker_load_file(pathname, &lfdep); 1606 if (error) 1607 break; 1608 if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) { 1609 linker_file_unload(lfdep); 1610 error = ENOENT; 1611 break; 1612 } 1613 if (parent) { 1614 error = linker_file_add_dependency(parent, lfdep); 1615 if (error) 1616 break; 1617 } 1618 if (lfpp) 1619 *lfpp = lfdep; 1620 } while(0); 1621out: 1622 if (pathname) 1623 free(pathname, M_LINKER); 1624 return error; 1625} 1626 1627/* 1628 * This routine is responsible for finding dependencies of userland 1629 * initiated kldload(2)'s of files. 1630 */ 1631int 1632linker_load_dependencies(linker_file_t lf) 1633{ 1634 linker_file_t lfdep; 1635 struct mod_metadata **start, **stop, **mdp, **nmdp; 1636 struct mod_metadata *mp, *nmp; 1637 struct mod_depend *verinfo; 1638 modlist_t mod; 1639 const char *modname, *nmodname; 1640 int ver, error = 0, count; 1641 1642 /* 1643 * All files are dependant on /kernel. 1644 */ 1645 if (linker_kernel_file) { 1646 linker_kernel_file->refs++; 1647 error = linker_file_add_dependency(lf, linker_kernel_file); 1648 if (error) 1649 return error; 1650 } 1651 1652 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0) 1653 return 0; 1654 for (mdp = start; mdp < stop; mdp++) { 1655 mp = linker_reloc_ptr(lf, *mdp); 1656 if (mp->md_type != MDT_VERSION) 1657 continue; 1658 linker_mdt_version(lf, mp, &modname, &ver); 1659 mod = modlist_lookup(modname, ver); 1660 if (mod != NULL) { 1661 printf("interface %s.%d already present in the KLD '%s'!\n", 1662 modname, ver, mod->container->filename); 1663 return EEXIST; 1664 } 1665 } 1666 1667 for (mdp = start; mdp < stop; mdp++) { 1668 mp = linker_reloc_ptr(lf, *mdp); 1669 if (mp->md_type != MDT_DEPEND) 1670 continue; 1671 linker_mdt_depend(lf, mp, &modname, &verinfo); 1672 nmodname = NULL; 1673 for (nmdp = start; nmdp < stop; nmdp++) { 1674 nmp = linker_reloc_ptr(lf, *nmdp); 1675 if (nmp->md_type != MDT_VERSION) 1676 continue; 1677 nmodname = linker_reloc_ptr(lf, nmp->md_cval); 1678 if (strcmp(modname, nmodname) == 0) 1679 break; 1680 } 1681 if (nmdp < stop) /* early exit, it's a self reference */ 1682 continue; 1683 mod = modlist_lookup2(modname, verinfo); 1684 if (mod) { /* woohoo, it's loaded already */ 1685 lfdep = mod->container; 1686 lfdep->refs++; 1687 error = linker_file_add_dependency(lf, lfdep); 1688 if (error) 1689 break; 1690 continue; 1691 } 1692 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 1693 if (error) { 1694 printf("KLD %s: depends on %s - not available\n", 1695 lf->filename, modname); 1696 break; 1697 } 1698 } 1699 1700 if (error) 1701 return error; 1702 linker_addmodules(lf, start, stop, 0); 1703 return error; 1704} 1705 1706static int 1707sysctl_kern_function_list_iterate(const char *name, void *opaque) 1708{ 1709 struct sysctl_req *req; 1710 1711 req = opaque; 1712 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 1713} 1714 1715/* 1716 * Export a nul-separated, double-nul-terminated list of all function names 1717 * in the kernel. 1718 */ 1719static int 1720sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 1721{ 1722 linker_file_t lf; 1723 int error; 1724 1725 TAILQ_FOREACH(lf, &linker_files, link) { 1726 error = LINKER_EACH_FUNCTION_NAME(lf, 1727 sysctl_kern_function_list_iterate, req); 1728 if (error) 1729 return (error); 1730 } 1731 return (SYSCTL_OUT(req, "", 1)); 1732} 1733 1734SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 1735 NULL, 0, sysctl_kern_function_list, "", "kernel function list");
| 419 lf->ndeps = 0; 420 lf->deps = NULL; 421 STAILQ_INIT(&lf->common); 422 TAILQ_INIT(&lf->modules); 423 424 TAILQ_INSERT_TAIL(&linker_files, lf, link); 425 426out: 427 lockmgr(&lock, LK_RELEASE, 0, curthread); 428 return lf; 429} 430 431int 432linker_file_unload(linker_file_t file) 433{ 434 module_t mod, next; 435 modlist_t ml, nextml; 436 struct common_symbol* cp; 437 int error = 0; 438 int i; 439 440 /* Refuse to unload modules if securelevel raised */ 441 if (securelevel > 0) 442 return EPERM; 443 444 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 445 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread); 446 if (file->refs == 1) { 447 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 448 /* 449 * Inform any modules associated with this file. 450 */ 451 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 452 next = module_getfnext(mod); 453 454 /* 455 * Give the module a chance to veto the unload. 456 */ 457 if ((error = module_unload(mod)) != 0) { 458 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 459 mod)); 460 lockmgr(&lock, LK_RELEASE, 0, curthread); 461 goto out; 462 } 463 464 module_release(mod); 465 } 466 } 467 468 file->refs--; 469 if (file->refs > 0) { 470 lockmgr(&lock, LK_RELEASE, 0, curthread); 471 goto out; 472 } 473 474 for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) { 475 nextml = TAILQ_NEXT(ml, link); 476 if (ml->container == file) { 477 TAILQ_REMOVE(&found_modules, ml, link); 478 } 479 } 480 481 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 482 if (file->flags & LINKER_FILE_LINKED) { 483 linker_file_sysuninit(file); 484 linker_file_unregister_sysctls(file); 485 } 486 487 TAILQ_REMOVE(&linker_files, file, link); 488 lockmgr(&lock, LK_RELEASE, 0, curthread); 489 490 if (file->deps) { 491 for (i = 0; i < file->ndeps; i++) 492 linker_file_unload(file->deps[i]); 493 free(file->deps, M_LINKER); 494 file->deps = NULL; 495 } 496 497 for (cp = STAILQ_FIRST(&file->common); cp; 498 cp = STAILQ_FIRST(&file->common)) { 499 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 500 free(cp, M_LINKER); 501 } 502 503 LINKER_UNLOAD(file); 504 if (file->filename) { 505 free(file->filename, M_LINKER); 506 file->filename = NULL; 507 } 508 kobj_delete((kobj_t) file, M_LINKER); 509 510out: 511 return error; 512} 513 514int 515linker_file_add_dependency(linker_file_t file, linker_file_t dep) 516{ 517 linker_file_t* newdeps; 518 519 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 520 M_LINKER, M_WAITOK | M_ZERO); 521 if (newdeps == NULL) 522 return ENOMEM; 523 524 if (file->deps) { 525 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 526 free(file->deps, M_LINKER); 527 } 528 file->deps = newdeps; 529 file->deps[file->ndeps] = dep; 530 file->ndeps++; 531 532 return 0; 533} 534 535/* 536 * Locate a linker set and its contents. 537 * This is a helper function to avoid linker_if.h exposure elsewhere. 538 * Note: firstp and lastp are really void *** 539 */ 540int 541linker_file_lookup_set(linker_file_t file, const char *name, 542 void *firstp, void *lastp, int *countp) 543{ 544 545 return LINKER_LOOKUP_SET(file, name, firstp, lastp, countp); 546} 547 548caddr_t 549linker_file_lookup_symbol(linker_file_t file, const char* name, int deps) 550{ 551 c_linker_sym_t sym; 552 linker_symval_t symval; 553 caddr_t address; 554 size_t common_size = 0; 555 int i; 556 557 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 558 file, name, deps)); 559 560 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 561 LINKER_SYMBOL_VALUES(file, sym, &symval); 562 if (symval.value == 0) 563 /* 564 * For commons, first look them up in the dependencies and 565 * only allocate space if not found there. 566 */ 567 common_size = symval.size; 568 else { 569 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 570 return symval.value; 571 } 572 } 573 574 if (deps) { 575 for (i = 0; i < file->ndeps; i++) { 576 address = linker_file_lookup_symbol(file->deps[i], name, 0); 577 if (address) { 578 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); 579 return address; 580 } 581 } 582 } 583 584 if (common_size > 0) { 585 /* 586 * This is a common symbol which was not found in the 587 * dependencies. We maintain a simple common symbol table in 588 * the file object. 589 */ 590 struct common_symbol* cp; 591 592 STAILQ_FOREACH(cp, &file->common, link) 593 if (!strcmp(cp->name, name)) { 594 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 595 return cp->address; 596 } 597 598 /* 599 * Round the symbol size up to align. 600 */ 601 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 602 cp = malloc(sizeof(struct common_symbol) 603 + common_size 604 + strlen(name) + 1, 605 M_LINKER, M_WAITOK | M_ZERO); 606 if (!cp) { 607 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 608 return 0; 609 } 610 611 cp->address = (caddr_t) (cp + 1); 612 cp->name = cp->address + common_size; 613 strcpy(cp->name, name); 614 bzero(cp->address, common_size); 615 STAILQ_INSERT_TAIL(&file->common, cp, link); 616 617 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 618 return cp->address; 619 } 620 621 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 622 return 0; 623} 624 625#ifdef DDB 626/* 627 * DDB Helpers. DDB has to look across multiple files with their own 628 * symbol tables and string tables. 629 * 630 * Note that we do not obey list locking protocols here. We really don't 631 * need DDB to hang because somebody's got the lock held. We'll take the 632 * chance that the files list is inconsistant instead. 633 */ 634 635int 636linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 637{ 638 linker_file_t lf; 639 640 TAILQ_FOREACH(lf, &linker_files, link) { 641 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 642 return 0; 643 } 644 return ENOENT; 645} 646 647int 648linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 649{ 650 linker_file_t lf; 651 u_long off = (uintptr_t)value; 652 u_long diff, bestdiff; 653 c_linker_sym_t best; 654 c_linker_sym_t es; 655 656 best = 0; 657 bestdiff = off; 658 TAILQ_FOREACH(lf, &linker_files, link) { 659 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 660 continue; 661 if (es != 0 && diff < bestdiff) { 662 best = es; 663 bestdiff = diff; 664 } 665 if (bestdiff == 0) 666 break; 667 } 668 if (best) { 669 *sym = best; 670 *diffp = bestdiff; 671 return 0; 672 } else { 673 *sym = 0; 674 *diffp = off; 675 return ENOENT; 676 } 677} 678 679int 680linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 681{ 682 linker_file_t lf; 683 684 TAILQ_FOREACH(lf, &linker_files, link) { 685 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 686 return 0; 687 } 688 return ENOENT; 689} 690 691#endif 692 693/* 694 * Syscalls. 695 */ 696/* 697 * MPSAFE 698 */ 699int 700kldload(struct thread* td, struct kldload_args* uap) 701{ 702 char *kldname, *modname; 703 char *pathname = NULL; 704 linker_file_t lf; 705 int error = 0; 706 707 td->td_retval[0] = -1; 708 709 if (securelevel > 0) /* redundant, but that's OK */ 710 return EPERM; 711 712 mtx_lock(&Giant); 713 714 if ((error = suser_xxx(td->td_proc->p_ucred, NULL, 0)) != 0) 715 goto out; 716 717 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 718 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0) 719 goto out; 720 721 /* 722 * If path do not contain qualified name or any dot in it (kldname.ko, or 723 * kldname.ver.ko) treat it as interface name. 724 */ 725 if (index(pathname, '/') || index(pathname, '.')) { 726 kldname = pathname; 727 modname = NULL; 728 } else { 729 kldname = NULL; 730 modname = pathname; 731 } 732 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 733 if (error) 734 goto out; 735 736 lf->userrefs++; 737 td->td_retval[0] = lf->id; 738 739out: 740 if (pathname) 741 free(pathname, M_TEMP); 742 mtx_unlock(&Giant); 743 return (error); 744} 745 746/* 747 * MPSAFE 748 */ 749int 750kldunload(struct thread* td, struct kldunload_args* uap) 751{ 752 linker_file_t lf; 753 int error = 0; 754 755 if (securelevel > 0) /* redundant, but that's OK */ 756 return EPERM; 757 758 mtx_lock(&Giant); 759 760 if ((error = suser_xxx(td->td_proc->p_ucred, NULL, 0)) != 0) 761 goto out; 762 763 lf = linker_find_file_by_id(SCARG(uap, fileid)); 764 if (lf) { 765 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 766 if (lf->userrefs == 0) { 767 printf("kldunload: attempt to unload file that was loaded by the kernel\n"); 768 error = EBUSY; 769 goto out; 770 } 771 lf->userrefs--; 772 error = linker_file_unload(lf); 773 if (error) 774 lf->userrefs++; 775 } else { 776 error = ENOENT; 777 } 778out: 779 mtx_unlock(&Giant); 780 return (error); 781} 782 783/* 784 * MPSAFE 785 */ 786int 787kldfind(struct thread* td, struct kldfind_args* uap) 788{ 789 char* pathname; 790 const char *filename; 791 linker_file_t lf; 792 int error = 0; 793 794 mtx_lock(&Giant); 795 td->td_retval[0] = -1; 796 797 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 798 if ((error = copyinstr(SCARG(uap, file), pathname, MAXPATHLEN, NULL)) != 0) 799 goto out; 800 801 filename = linker_basename(pathname); 802 803 lf = linker_find_file_by_name(filename); 804 if (lf) 805 td->td_retval[0] = lf->id; 806 else 807 error = ENOENT; 808 809out: 810 if (pathname) 811 free(pathname, M_TEMP); 812 mtx_unlock(&Giant); 813 return (error); 814} 815 816/* 817 * MPSAFE 818 */ 819int 820kldnext(struct thread* td, struct kldnext_args* uap) 821{ 822 linker_file_t lf; 823 int error = 0; 824 825 mtx_lock(&Giant); 826 827 if (SCARG(uap, fileid) == 0) { 828 if (TAILQ_FIRST(&linker_files)) 829 td->td_retval[0] = TAILQ_FIRST(&linker_files)->id; 830 else 831 td->td_retval[0] = 0; 832 goto out; 833 } 834 835 lf = linker_find_file_by_id(SCARG(uap, fileid)); 836 if (lf) { 837 if (TAILQ_NEXT(lf, link)) 838 td->td_retval[0] = TAILQ_NEXT(lf, link)->id; 839 else 840 td->td_retval[0] = 0; 841 } else { 842 error = ENOENT; 843 } 844out: 845 mtx_unlock(&Giant); 846 return (error); 847} 848 849/* 850 * MPSAFE 851 */ 852int 853kldstat(struct thread* td, struct kldstat_args* uap) 854{ 855 linker_file_t lf; 856 int error = 0; 857 int version; 858 struct kld_file_stat* stat; 859 int namelen; 860 861 mtx_lock(&Giant); 862 863 lf = linker_find_file_by_id(SCARG(uap, fileid)); 864 if (!lf) { 865 error = ENOENT; 866 goto out; 867 } 868 869 stat = SCARG(uap, stat); 870 871 /* 872 * Check the version of the user's structure. 873 */ 874 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 875 goto out; 876 if (version != sizeof(struct kld_file_stat)) { 877 error = EINVAL; 878 goto out; 879 } 880 881 namelen = strlen(lf->filename) + 1; 882 if (namelen > MAXPATHLEN) 883 namelen = MAXPATHLEN; 884 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 885 goto out; 886 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 887 goto out; 888 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 889 goto out; 890 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 891 goto out; 892 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 893 goto out; 894 895 td->td_retval[0] = 0; 896 897out: 898 mtx_unlock(&Giant); 899 return (error); 900} 901 902/* 903 * MPSAFE 904 */ 905int 906kldfirstmod(struct thread* td, struct kldfirstmod_args* uap) 907{ 908 linker_file_t lf; 909 int error = 0; 910 911 mtx_lock(&Giant); 912 lf = linker_find_file_by_id(SCARG(uap, fileid)); 913 if (lf) { 914 if (TAILQ_FIRST(&lf->modules)) 915 td->td_retval[0] = module_getid(TAILQ_FIRST(&lf->modules)); 916 else 917 td->td_retval[0] = 0; 918 } else { 919 error = ENOENT; 920 } 921 mtx_unlock(&Giant); 922 return (error); 923} 924 925/* 926 * MPSAFE 927 */ 928int 929kldsym(struct thread *td, struct kldsym_args *uap) 930{ 931 char *symstr = NULL; 932 c_linker_sym_t sym; 933 linker_symval_t symval; 934 linker_file_t lf; 935 struct kld_sym_lookup lookup; 936 int error = 0; 937 938 mtx_lock(&Giant); 939 940 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0) 941 goto out; 942 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) { 943 error = EINVAL; 944 goto out; 945 } 946 947 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 948 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 949 goto out; 950 951 if (SCARG(uap, fileid) != 0) { 952 lf = linker_find_file_by_id(SCARG(uap, fileid)); 953 if (lf == NULL) { 954 error = ENOENT; 955 goto out; 956 } 957 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 958 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 959 lookup.symvalue = (uintptr_t)symval.value; 960 lookup.symsize = symval.size; 961 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 962 } else 963 error = ENOENT; 964 } else { 965 TAILQ_FOREACH(lf, &linker_files, link) { 966 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 967 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 968 lookup.symvalue = (uintptr_t)symval.value; 969 lookup.symsize = symval.size; 970 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 971 break; 972 } 973 } 974 if (!lf) 975 error = ENOENT; 976 } 977out: 978 if (symstr) 979 free(symstr, M_TEMP); 980 mtx_unlock(&Giant); 981 return (error); 982} 983 984/* 985 * Preloaded module support 986 */ 987 988static modlist_t 989modlist_lookup(const char *name, int ver) 990{ 991 modlist_t mod; 992 993 TAILQ_FOREACH(mod, &found_modules, link) { 994 if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver)) 995 return mod; 996 } 997 return NULL; 998} 999 1000static modlist_t 1001modlist_lookup2(const char *name, struct mod_depend *verinfo) 1002{ 1003 modlist_t mod, bestmod; 1004 int ver; 1005 1006 if (verinfo == NULL) 1007 return modlist_lookup(name, 0); 1008 bestmod = NULL; 1009 for (mod = TAILQ_FIRST(&found_modules); mod; mod = TAILQ_NEXT(mod, link)) { 1010 if (strcmp(mod->name, name) != 0) 1011 continue; 1012 ver = mod->version; 1013 if (ver == verinfo->md_ver_preferred) 1014 return mod; 1015 if (ver >= verinfo->md_ver_minimum && 1016 ver <= verinfo->md_ver_maximum && 1017 ver > bestmod->version) 1018 bestmod = mod; 1019 } 1020 return bestmod; 1021} 1022 1023static modlist_t 1024modlist_newmodule(const char *modname, int version, linker_file_t container) 1025{ 1026 modlist_t mod; 1027 1028 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT); 1029 if (mod == NULL) 1030 panic("no memory for module list"); 1031 bzero(mod, sizeof(*mod)); 1032 mod->container = container; 1033 mod->name = modname; 1034 mod->version = version; 1035 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1036 return mod; 1037} 1038 1039/* 1040 * This routine is cheap and nasty but will work for data pointers. 1041 */ 1042static void * 1043linker_reloc_ptr(linker_file_t lf, const void *offset) 1044{ 1045 return lf->address + (uintptr_t)offset; 1046} 1047 1048/* 1049 * Dereference MDT_VERSION metadata into module name and version 1050 */ 1051static void 1052linker_mdt_version(linker_file_t lf, struct mod_metadata *mp, 1053 const char **modname, int *version) 1054{ 1055 struct mod_version *mvp; 1056 1057 if (modname) 1058 *modname = linker_reloc_ptr(lf, mp->md_cval); 1059 if (version) { 1060 mvp = linker_reloc_ptr(lf, mp->md_data); 1061 *version = mvp->mv_version; 1062 } 1063} 1064 1065/* 1066 * Dereference MDT_DEPEND metadata into module name and mod_depend structure 1067 */ 1068static void 1069linker_mdt_depend(linker_file_t lf, struct mod_metadata *mp, 1070 const char **modname, struct mod_depend **verinfo) 1071{ 1072 1073 if (modname) 1074 *modname = linker_reloc_ptr(lf, mp->md_cval); 1075 if (verinfo) 1076 *verinfo = linker_reloc_ptr(lf, mp->md_data); 1077} 1078 1079static void 1080linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1081 struct mod_metadata **stop, int preload) 1082{ 1083 struct mod_metadata *mp, **mdp; 1084 const char *modname; 1085 int ver; 1086 1087 for (mdp = start; mdp < stop; mdp++) { 1088 if (preload) 1089 mp = *mdp; 1090 else 1091 mp = linker_reloc_ptr(lf, *mdp); 1092 if (mp->md_type != MDT_VERSION) 1093 continue; 1094 if (preload) { 1095 modname = mp->md_cval; 1096 ver = ((struct mod_version*)mp->md_data)->mv_version; 1097 } else 1098 linker_mdt_version(lf, mp, &modname, &ver); 1099 if (modlist_lookup(modname, ver) != NULL) { 1100 printf("module %s already present!\n", modname); 1101 /* XXX what can we do? this is a build error. :-( */ 1102 continue; 1103 } 1104 modlist_newmodule(modname, ver, lf); 1105 } 1106} 1107 1108static void 1109linker_preload(void* arg) 1110{ 1111 caddr_t modptr; 1112 const char *modname, *nmodname; 1113 char *modtype; 1114 linker_file_t lf; 1115 linker_class_t lc; 1116 int error; 1117 linker_file_list_t loaded_files; 1118 linker_file_list_t depended_files; 1119 struct mod_metadata *mp, *nmp; 1120 struct mod_metadata **start, **stop, **mdp, **nmdp; 1121 struct mod_depend *verinfo; 1122 int nver; 1123 int resolves; 1124 modlist_t mod; 1125 struct sysinit **si_start, **si_stop; 1126 1127 TAILQ_INIT(&loaded_files); 1128 TAILQ_INIT(&depended_files); 1129 TAILQ_INIT(&found_modules); 1130 error = 0; 1131 1132 modptr = NULL; 1133 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1134 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1135 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1136 if (modname == NULL) { 1137 printf("Preloaded module at %p does not have a name!\n", modptr); 1138 continue; 1139 } 1140 if (modtype == NULL) { 1141 printf("Preloaded module at %p does not have a type!\n", modptr); 1142 continue; 1143 } 1144 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 1145 lf = NULL; 1146 TAILQ_FOREACH(lc, &classes, link) { 1147 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1148 if (error) { 1149 lf = NULL; 1150 break; 1151 } 1152 } 1153 if (lf) 1154 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1155 } 1156 1157 /* 1158 * First get a list of stuff in the kernel. 1159 */ 1160 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, &stop, 1161 NULL) == 0) 1162 linker_addmodules(linker_kernel_file, start, stop, 1); 1163 1164 /* 1165 * this is a once-off kinky bubble sort 1166 * resolve relocation dependency requirements 1167 */ 1168restart: 1169 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1170 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL); 1171 /* 1172 * First, look to see if we would successfully link with this stuff. 1173 */ 1174 resolves = 1; /* unless we know otherwise */ 1175 if (!error) { 1176 for (mdp = start; mdp < stop; mdp++) { 1177 mp = linker_reloc_ptr(lf, *mdp); 1178 if (mp->md_type != MDT_DEPEND) 1179 continue; 1180 linker_mdt_depend(lf, mp, &modname, &verinfo); 1181 for (nmdp = start; nmdp < stop; nmdp++) { 1182 nmp = linker_reloc_ptr(lf, *nmdp); 1183 if (nmp->md_type != MDT_VERSION) 1184 continue; 1185 linker_mdt_version(lf, nmp, &nmodname, NULL); 1186 nmodname = linker_reloc_ptr(lf, nmp->md_cval); 1187 if (strcmp(modname, nmodname) == 0) 1188 break; 1189 } 1190 if (nmdp < stop) /* it's a self reference */ 1191 continue; 1192 if (modlist_lookup2(modname, verinfo) == NULL) { 1193 /* ok, the module isn't here yet, we are not finished */ 1194 resolves = 0; 1195 } 1196 } 1197 } 1198 /* 1199 * OK, if we found our modules, we can link. So, "provide" the 1200 * modules inside and add it to the end of the link order list. 1201 */ 1202 if (resolves) { 1203 if (!error) { 1204 for (mdp = start; mdp < stop; mdp++) { 1205 mp = linker_reloc_ptr(lf, *mdp); 1206 if (mp->md_type != MDT_VERSION) 1207 continue; 1208 linker_mdt_version(lf, mp, &modname, &nver); 1209 if (modlist_lookup(modname, nver) != NULL) { 1210 printf("module %s already present!\n", modname); 1211 linker_file_unload(lf); 1212 TAILQ_REMOVE(&loaded_files, lf, loaded); 1213 goto restart; /* we changed the tailq next ptr */ 1214 } 1215 modlist_newmodule(modname, nver, lf); 1216 } 1217 } 1218 TAILQ_REMOVE(&loaded_files, lf, loaded); 1219 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1220 /* 1221 * Since we provided modules, we need to restart the sort so 1222 * that the previous files that depend on us have a chance. 1223 * Also, we've busted the tailq next pointer with the REMOVE. 1224 */ 1225 goto restart; 1226 } 1227 } 1228 1229 /* 1230 * At this point, we check to see what could not be resolved.. 1231 */ 1232 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1233 printf("KLD file %s is missing dependencies\n", lf->filename); 1234 linker_file_unload(lf); 1235 TAILQ_REMOVE(&loaded_files, lf, loaded); 1236 } 1237 1238 /* 1239 * We made it. Finish off the linking in the order we determined. 1240 */ 1241 TAILQ_FOREACH(lf, &depended_files, loaded) { 1242 if (linker_kernel_file) { 1243 linker_kernel_file->refs++; 1244 error = linker_file_add_dependency(lf, linker_kernel_file); 1245 if (error) 1246 panic("cannot add dependency"); 1247 } 1248 lf->userrefs++; /* so we can (try to) kldunload it */ 1249 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL); 1250 if (!error) { 1251 for (mdp = start; mdp < stop; mdp++) { 1252 mp = linker_reloc_ptr(lf, *mdp); 1253 if (mp->md_type != MDT_DEPEND) 1254 continue; 1255 linker_mdt_depend(lf, mp, &modname, &verinfo); 1256 mod = modlist_lookup2(modname, verinfo); 1257 mod->container->refs++; 1258 error = linker_file_add_dependency(lf, mod->container); 1259 if (error) 1260 panic("cannot add dependency"); 1261 } 1262 } 1263 1264 /* 1265 * Now do relocation etc using the symbol search paths established by 1266 * the dependencies 1267 */ 1268 error = LINKER_LINK_PRELOAD_FINISH(lf); 1269 if (error) { 1270 printf("KLD file %s - could not finalize loading\n", lf->filename); 1271 linker_file_unload(lf); 1272 continue; 1273 } 1274 1275 linker_file_register_modules(lf); 1276 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) 1277 sysinit_add(si_start, si_stop); 1278 linker_file_register_sysctls(lf); 1279 lf->flags |= LINKER_FILE_LINKED; 1280 } 1281 /* woohoo! we made it! */ 1282} 1283 1284SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1285 1286/* 1287 * Search for a not-loaded module by name. 1288 * 1289 * Modules may be found in the following locations: 1290 * 1291 * - preloaded (result is just the module name) 1292 * - on disk (result is full path to module) 1293 * 1294 * If the module name is qualified in any way (contains path, etc.) 1295 * the we simply return a copy of it. 1296 * 1297 * The search path can be manipulated via sysctl. Note that we use the ';' 1298 * character as a separator to be consistent with the bootloader. 1299 */ 1300 1301static char linker_hintfile[] = "linker.hints"; 1302static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules;/modules"; 1303 1304SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1305 sizeof(linker_path), "module load search path"); 1306 1307TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1308 1309static char *linker_ext_list[] = { 1310 "", 1311 ".ko", 1312 NULL 1313}; 1314 1315/* 1316 * Check if file actually exists either with or without extension listed 1317 * in the linker_ext_list. 1318 * (probably should be generic for the rest of the kernel) 1319 */ 1320static char * 1321linker_lookup_file(const char *path, int pathlen, 1322 const char *name, int namelen, struct vattr *vap) 1323{ 1324 struct nameidata nd; 1325 struct thread *td = curthread; /* XXX */ 1326 char *result, **cpp, *sep; 1327 int error, len, extlen, reclen, flags; 1328 enum vtype type; 1329 1330 extlen = 0; 1331 for (cpp = linker_ext_list; *cpp; cpp++) { 1332 len = strlen(*cpp); 1333 if (len > extlen) 1334 extlen = len; 1335 } 1336 extlen++; /* trailing '\0' */ 1337 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1338 1339 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1340 result = malloc(reclen, M_LINKER, M_WAITOK); 1341 for (cpp = linker_ext_list; *cpp; cpp++) { 1342 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1343 namelen, name, *cpp); 1344 /* 1345 * Attempt to open the file, and return the path if we succeed 1346 * and it's a regular file. 1347 */ 1348 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td); 1349 flags = FREAD; 1350 error = vn_open(&nd, &flags, 0); 1351 if (error == 0) { 1352 NDFREE(&nd, NDF_ONLY_PNBUF); 1353 type = nd.ni_vp->v_type; 1354 if (vap) 1355 VOP_GETATTR(nd.ni_vp, vap, td->td_proc->p_ucred, td); 1356 VOP_UNLOCK(nd.ni_vp, 0, td); 1357 vn_close(nd.ni_vp, FREAD, td->td_proc->p_ucred, td); 1358 if (type == VREG) 1359 return(result); 1360 } 1361 } 1362 free(result, M_LINKER); 1363 return(NULL); 1364} 1365 1366#define INT_ALIGN(base, ptr) ptr = \ 1367 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1368 1369/* 1370 * Lookup KLD which contains requested module in the "linker.hints" file. 1371 * If version specification is available, then try to find the best KLD. 1372 * Otherwise just find the latest one. 1373 */ 1374static char * 1375linker_hints_lookup(const char *path, int pathlen, 1376 const char *modname, int modnamelen, 1377 struct mod_depend *verinfo) 1378{ 1379 struct thread *td = curthread; /* XXX */ 1380 struct ucred *cred = td ? td->td_proc->p_ucred : NULL; 1381 struct nameidata nd; 1382 struct vattr vattr, mattr; 1383 u_char *hints = NULL; 1384 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1385 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1386 1387 result = NULL; 1388 bestver = found = 0; 1389 1390 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1391 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1392 strlen(sep) + 1; 1393 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1394 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, linker_hintfile); 1395 1396 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td); 1397 flags = FREAD; 1398 error = vn_open(&nd, &flags, 0); 1399 if (error) 1400 goto bad; 1401 NDFREE(&nd, NDF_ONLY_PNBUF); 1402 VOP_UNLOCK(nd.ni_vp, 0, td); 1403 if (nd.ni_vp->v_type != VREG) 1404 goto bad; 1405 best = cp = NULL; 1406 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td); 1407 if (error) 1408 goto bad; 1409 /* 1410 * XXX: we need to limit this number to some reasonable value 1411 */ 1412 if (vattr.va_size > 100 * 1024) { 1413 printf("hints file too large %ld\n", (long)vattr.va_size); 1414 goto bad; 1415 } 1416 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1417 if (hints == NULL) 1418 goto bad; 1419 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1420 UIO_SYSSPACE, IO_NODELOCKED, cred, &reclen, td); 1421 if (error) 1422 goto bad; 1423 vn_close(nd.ni_vp, FREAD, cred, td); 1424 nd.ni_vp = NULL; 1425 if (reclen != 0) { 1426 printf("can't read %d\n", reclen); 1427 goto bad; 1428 } 1429 intp = (int*)hints; 1430 ival = *intp++; 1431 if (ival != LINKER_HINTS_VERSION) { 1432 printf("hints file version mismatch %d\n", ival); 1433 goto bad; 1434 } 1435 bufend = hints + vattr.va_size; 1436 recptr = (u_char*)intp; 1437 clen = blen = 0; 1438 while (recptr < bufend && !found) { 1439 intp = (int*)recptr; 1440 reclen = *intp++; 1441 ival = *intp++; 1442 cp = (char*)intp; 1443 switch (ival) { 1444 case MDT_VERSION: 1445 clen = *cp++; 1446 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1447 break; 1448 cp += clen; 1449 INT_ALIGN(hints, cp); 1450 ival = *(int*)cp; 1451 cp += sizeof(int); 1452 clen = *cp++; 1453 if (verinfo == NULL || ival == verinfo->md_ver_preferred) { 1454 found = 1; 1455 break; 1456 } 1457 if (ival >= verinfo->md_ver_minimum && 1458 ival <= verinfo->md_ver_maximum && 1459 ival > bestver) { 1460 bestver = ival; 1461 best = cp; 1462 blen = clen; 1463 } 1464 break; 1465 default: 1466 break; 1467 } 1468 recptr += reclen + sizeof(int); 1469 } 1470 /* 1471 * Finally check if KLD is in the place 1472 */ 1473 if (found) 1474 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1475 else if (best) 1476 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1477 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) { 1478 /* 1479 * KLD is newer than hints file. What we should do now ? 1480 */ 1481 printf("warning: KLD '%s' is newer than the linker.hints file\n", 1482 result); 1483 } 1484bad: 1485 if (hints) 1486 free(hints, M_TEMP); 1487 if (nd.ni_vp != NULL) 1488 vn_close(nd.ni_vp, FREAD, cred, td); 1489 /* 1490 * If nothing found or hints is absent - fallback to the old way 1491 * by using "kldname[.ko]" as module name. 1492 */ 1493 if (!found && !bestver && result == NULL) 1494 result = linker_lookup_file(path, pathlen, modname, modnamelen, NULL); 1495 return result; 1496} 1497 1498/* 1499 * Lookup KLD which contains requested module in the all directories. 1500 */ 1501static char * 1502linker_search_module(const char *modname, int modnamelen, 1503 struct mod_depend *verinfo) 1504{ 1505 char *cp, *ep, *result; 1506 1507 /* 1508 * traverse the linker path 1509 */ 1510 for (cp = linker_path; *cp; cp = ep + 1) { 1511 1512 /* find the end of this component */ 1513 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 1514 ; 1515 result = linker_hints_lookup(cp, ep - cp, modname, modnamelen, verinfo); 1516 if (result != NULL) 1517 return(result); 1518 if (*ep == 0) 1519 break; 1520 } 1521 return (NULL); 1522} 1523 1524/* 1525 * Search for module in all directories listed in the linker_path. 1526 */ 1527static char * 1528linker_search_kld(const char *name) 1529{ 1530 char *cp, *ep, *result, **cpp; 1531 int extlen, len; 1532 1533 /* qualified at all? */ 1534 if (index(name, '/')) 1535 return(linker_strdup(name)); 1536 1537 extlen = 0; 1538 for (cpp = linker_ext_list; *cpp; cpp++) { 1539 len = strlen(*cpp); 1540 if (len > extlen) 1541 extlen = len; 1542 } 1543 extlen++; /* trailing '\0' */ 1544 1545 /* traverse the linker path */ 1546 len = strlen(name); 1547 for (ep = linker_path; *ep; ep++) { 1548 cp = ep; 1549 /* find the end of this component */ 1550 for (; *ep != 0 && *ep != ';'; ep++) 1551 ; 1552 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1553 if (result != NULL) 1554 return(result); 1555 } 1556 return(NULL); 1557} 1558 1559static const char * 1560linker_basename(const char* path) 1561{ 1562 const char *filename; 1563 1564 filename = rindex(path, '/'); 1565 if (filename == NULL) 1566 return path; 1567 if (filename[1]) 1568 filename++; 1569 return filename; 1570} 1571 1572/* 1573 * Find a file which contains given module and load it, 1574 * if "parent" is not NULL, register a reference to it. 1575 */ 1576static int 1577linker_load_module(const char *kldname, const char *modname, 1578 struct linker_file *parent, struct mod_depend *verinfo, 1579 struct linker_file **lfpp) 1580{ 1581 linker_file_t lfdep; 1582 const char *filename; 1583 char *pathname; 1584 int error; 1585 1586 if (modname == NULL) { 1587 /* 1588 * We have to load KLD 1589 */ 1590 KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL")); 1591 pathname = linker_search_kld(kldname); 1592 } else { 1593 if (modlist_lookup2(modname, verinfo) != NULL) 1594 return (EEXIST); 1595 if (kldname == NULL) { 1596 /* 1597 * Need to find a KLD with required module 1598 */ 1599 pathname = linker_search_module(modname, strlen(modname), verinfo); 1600 } else 1601 pathname = linker_strdup(kldname); 1602 } 1603 if (pathname == NULL) 1604 return (ENOENT); 1605 1606 /* 1607 * Can't load more than one file with the same basename 1608 * XXX: Actually it should be possible to have multiple KLDs 1609 * with the same basename but different path because they can provide 1610 * different versions of the same modules. 1611 */ 1612 filename = linker_basename(pathname); 1613 if (linker_find_file_by_name(filename)) { 1614 error = EEXIST; 1615 goto out; 1616 } 1617 1618 do { 1619 error = linker_load_file(pathname, &lfdep); 1620 if (error) 1621 break; 1622 if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) { 1623 linker_file_unload(lfdep); 1624 error = ENOENT; 1625 break; 1626 } 1627 if (parent) { 1628 error = linker_file_add_dependency(parent, lfdep); 1629 if (error) 1630 break; 1631 } 1632 if (lfpp) 1633 *lfpp = lfdep; 1634 } while(0); 1635out: 1636 if (pathname) 1637 free(pathname, M_LINKER); 1638 return error; 1639} 1640 1641/* 1642 * This routine is responsible for finding dependencies of userland 1643 * initiated kldload(2)'s of files. 1644 */ 1645int 1646linker_load_dependencies(linker_file_t lf) 1647{ 1648 linker_file_t lfdep; 1649 struct mod_metadata **start, **stop, **mdp, **nmdp; 1650 struct mod_metadata *mp, *nmp; 1651 struct mod_depend *verinfo; 1652 modlist_t mod; 1653 const char *modname, *nmodname; 1654 int ver, error = 0, count; 1655 1656 /* 1657 * All files are dependant on /kernel. 1658 */ 1659 if (linker_kernel_file) { 1660 linker_kernel_file->refs++; 1661 error = linker_file_add_dependency(lf, linker_kernel_file); 1662 if (error) 1663 return error; 1664 } 1665 1666 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0) 1667 return 0; 1668 for (mdp = start; mdp < stop; mdp++) { 1669 mp = linker_reloc_ptr(lf, *mdp); 1670 if (mp->md_type != MDT_VERSION) 1671 continue; 1672 linker_mdt_version(lf, mp, &modname, &ver); 1673 mod = modlist_lookup(modname, ver); 1674 if (mod != NULL) { 1675 printf("interface %s.%d already present in the KLD '%s'!\n", 1676 modname, ver, mod->container->filename); 1677 return EEXIST; 1678 } 1679 } 1680 1681 for (mdp = start; mdp < stop; mdp++) { 1682 mp = linker_reloc_ptr(lf, *mdp); 1683 if (mp->md_type != MDT_DEPEND) 1684 continue; 1685 linker_mdt_depend(lf, mp, &modname, &verinfo); 1686 nmodname = NULL; 1687 for (nmdp = start; nmdp < stop; nmdp++) { 1688 nmp = linker_reloc_ptr(lf, *nmdp); 1689 if (nmp->md_type != MDT_VERSION) 1690 continue; 1691 nmodname = linker_reloc_ptr(lf, nmp->md_cval); 1692 if (strcmp(modname, nmodname) == 0) 1693 break; 1694 } 1695 if (nmdp < stop) /* early exit, it's a self reference */ 1696 continue; 1697 mod = modlist_lookup2(modname, verinfo); 1698 if (mod) { /* woohoo, it's loaded already */ 1699 lfdep = mod->container; 1700 lfdep->refs++; 1701 error = linker_file_add_dependency(lf, lfdep); 1702 if (error) 1703 break; 1704 continue; 1705 } 1706 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 1707 if (error) { 1708 printf("KLD %s: depends on %s - not available\n", 1709 lf->filename, modname); 1710 break; 1711 } 1712 } 1713 1714 if (error) 1715 return error; 1716 linker_addmodules(lf, start, stop, 0); 1717 return error; 1718} 1719 1720static int 1721sysctl_kern_function_list_iterate(const char *name, void *opaque) 1722{ 1723 struct sysctl_req *req; 1724 1725 req = opaque; 1726 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 1727} 1728 1729/* 1730 * Export a nul-separated, double-nul-terminated list of all function names 1731 * in the kernel. 1732 */ 1733static int 1734sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 1735{ 1736 linker_file_t lf; 1737 int error; 1738 1739 TAILQ_FOREACH(lf, &linker_files, link) { 1740 error = LINKER_EACH_FUNCTION_NAME(lf, 1741 sysctl_kern_function_list_iterate, req); 1742 if (error) 1743 return (error); 1744 } 1745 return (SYSCTL_OUT(req, "", 1)); 1746} 1747 1748SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 1749 NULL, 0, sysctl_kern_function_list, "", "kernel function list");
|