Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / kern / link_aout.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/kern/link_aout.c,v 1.26 1999/12/24 15:33:36 bde Exp $
6ea70f76 27 * $DragonFly: src/sys/kern/link_aout.c,v 1.21 2006/12/23 00:35:04 swildner Exp $
984263bc
MD
28 */
29
984263bc
MD
30#define FREEBSD_AOUT 1
31
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/systm.h>
1f2de5d4 35#include <sys/types.h>
984263bc
MD
36#include <sys/malloc.h>
37#include <sys/proc.h>
fad57d0e 38#include <sys/nlookup.h>
984263bc
MD
39#include <sys/fcntl.h>
40#include <sys/vnode.h>
41#include <sys/linker.h>
42
43#include <vm/vm_zone.h>
44
45#ifndef __ELF__
46#include <vm/vm.h>
47#include <vm/pmap.h>
48#include <machine/vmparam.h>
49#endif
50
1f2de5d4
MD
51#include <machine/exec.h>
52#include <sys/imgact_aout.h>
53#include <machine/reloc.h>
54#define _AOUT_INCLUDE_
55#include <sys/nlist_aout.h>
56#include <sys/link_aout.h>
984263bc
MD
57
58static int link_aout_load_module(const char*, linker_file_t*);
59
60static int link_aout_load_file(const char*, linker_file_t*);
61
62static int link_aout_lookup_symbol(linker_file_t, const char*,
63 c_linker_sym_t*);
64static int link_aout_symbol_values(linker_file_t file, c_linker_sym_t sym,
65 linker_symval_t* symval);
66static int link_aout_search_symbol(linker_file_t lf, caddr_t value,
67 c_linker_sym_t* sym, long* diffp);
68static void link_aout_unload_file(linker_file_t);
69static void link_aout_unload_module(linker_file_t);
dc62b251
MD
70static int link_aout_lookup_set(linker_file_t lf, const char *name,
71 void ***startp, void ***stopp, int *countp);
984263bc
MD
72
73static struct linker_class_ops link_aout_class_ops = {
74 link_aout_load_module,
75};
76
77static struct linker_file_ops link_aout_file_ops = {
78 link_aout_lookup_symbol,
79 link_aout_symbol_values,
80 link_aout_search_symbol,
81 link_aout_unload_file,
dc62b251 82 link_aout_lookup_set
984263bc
MD
83};
84static struct linker_file_ops link_aout_module_ops = {
85 link_aout_lookup_symbol,
86 link_aout_symbol_values,
87 link_aout_search_symbol,
88 link_aout_unload_module,
dc62b251 89 link_aout_lookup_set
984263bc
MD
90};
91
92typedef struct aout_file {
93 char* address; /* Load address */
94 struct _dynamic* dynamic; /* Symbol table etc. */
95} *aout_file_t;
96
97static int load_dependancies(linker_file_t lf);
98static int relocate_file(linker_file_t lf);
99
100/*
101 * The kernel symbol table starts here.
102 */
103extern struct _dynamic _DYNAMIC;
104
105static void
106link_aout_init(void* arg)
107{
108#ifndef __ELF__
109 struct _dynamic* dp = &_DYNAMIC;
110#endif
111
112 linker_add_class("a.out", NULL, &link_aout_class_ops);
113
114#ifndef __ELF__
115 if (dp) {
116 aout_file_t af;
117
efda3bd0 118 af = kmalloc(sizeof(struct aout_file), M_LINKER, M_NOWAIT);
984263bc
MD
119 if (af == NULL)
120 panic("link_aout_init: Can't create linker structures for kernel");
121 bzero(af, sizeof(*af));
122
123 af->address = 0;
124 af->dynamic = dp;
125 linker_kernel_file =
126 linker_make_file(kernelname, af, &link_aout_file_ops);
127 if (linker_kernel_file == NULL)
128 panic("link_aout_init: Can't create linker structures for kernel");
129 linker_kernel_file->address = (caddr_t) KERNBASE;
130 linker_kernel_file->size = -(long)linker_kernel_file->address;
131 linker_current_file = linker_kernel_file;
132 linker_kernel_file->flags |= LINKER_FILE_LINKED;
133 }
134#endif
135}
136
137SYSINIT(link_aout, SI_SUB_KLD, SI_ORDER_THIRD, link_aout_init, 0);
138
139static int
140link_aout_load_module(const char* filename, linker_file_t* result)
141{
142 caddr_t modptr, baseptr;
143 char *type;
144 struct exec *ehdr;
145 aout_file_t af;
146 linker_file_t lf;
147 int error;
148
149 /* Look to see if we have the module preloaded. */
150 if ((modptr = preload_search_by_name(filename)) == NULL)
151 return(link_aout_load_file(filename, result));
152
153 /* It's preloaded, check we can handle it and collect information. */
154 if (((type = (char *)preload_search_info(modptr, MODINFO_TYPE)) == NULL) ||
155 strcmp(type, "a.out module") ||
156 ((baseptr = preload_search_info(modptr, MODINFO_ADDR)) == NULL) ||
157 ((ehdr = (struct exec *)preload_search_info(modptr, MODINFO_METADATA | MODINFOMD_AOUTEXEC)) == NULL))
158 return(0); /* we can't handle this */
159
160 /* Looks like we can handle this one */
efda3bd0 161 af = kmalloc(sizeof(struct aout_file), M_LINKER, M_WAITOK);
984263bc
MD
162 bzero(af, sizeof(*af));
163 af->address = baseptr;
164
165 /* Assume _DYNAMIC is the first data item. */
166 af->dynamic = (struct _dynamic*)(af->address + ehdr->a_text);
167 if (af->dynamic->d_version != LD_VERSION_BSD) {
efda3bd0 168 kfree(af, M_LINKER);
984263bc
MD
169 return(0); /* we can't handle this */
170 }
171 af->dynamic->d_un.d_sdt = (struct section_dispatch_table *)
172 ((char *)af->dynamic->d_un.d_sdt + (vm_offset_t)af->address);
173
174 /* Register with kld */
175 lf = linker_make_file(filename, af, &link_aout_module_ops);
176 if (lf == NULL) {
efda3bd0 177 kfree(af, M_LINKER);
984263bc
MD
178 return(ENOMEM);
179 }
180 lf->address = af->address;
181 lf->size = ehdr->a_text + ehdr->a_data + ehdr->a_bss;
182
183 /* Try to load dependancies */
184 if (((error = load_dependancies(lf)) != 0) ||
185 ((error = relocate_file(lf)) != 0)) {
186 linker_file_unload(lf);
187 return(error);
188 }
189 lf->flags |= LINKER_FILE_LINKED;
190 *result = lf;
191 return(0);
192}
193
194static int
195link_aout_load_file(const char* filename, linker_file_t* result)
196{
fad57d0e 197 struct nlookupdata nd;
dadab5e9
MD
198 struct thread *td = curthread;
199 struct proc *p = td->td_proc;
fad57d0e 200 struct vnode *vp;
984263bc
MD
201 int error = 0;
202 int resid;
203 struct exec header;
204 aout_file_t af;
205 linker_file_t lf;
206 char *pathname;
207
dadab5e9
MD
208 KKASSERT(p != NULL);
209
a3d82f10 210 if (p->p_ucred == NULL) {
6ea70f76 211 kprintf("link_aout_load_file: cannot load '%s' from filesystem"
a3d82f10
MD
212 " this early\n", filename);
213 return ENOENT;
214 }
215
984263bc
MD
216 pathname = linker_search_path(filename);
217 if (pathname == NULL)
218 return ENOENT;
fad57d0e
MD
219 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
220 if (error == 0)
221 error = vn_open(&nd, NULL, FREAD, 0);
efda3bd0 222 kfree(pathname, M_LINKER);
fad57d0e
MD
223 if (error) {
224 nlookup_done(&nd);
984263bc 225 return error;
fad57d0e
MD
226 }
227 vp = nd.nl_open_vp;
228 nd.nl_open_vp = NULL;
229 nlookup_done(&nd);
984263bc
MD
230
231 /*
232 * Read the a.out header from the file.
233 */
fad57d0e 234 error = vn_rdwr(UIO_READ, vp, (void*) &header, sizeof header, 0,
87de5057 235 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
236 if (error)
237 goto out;
238
239 if (N_BADMAG(header) || !(N_GETFLAG(header) & EX_DYNAMIC))
240 goto out;
241
242 /*
243 * We have an a.out file, so make some space to read it in.
244 */
efda3bd0 245 af = kmalloc(sizeof(struct aout_file), M_LINKER, M_WAITOK);
984263bc 246 bzero(af, sizeof(*af));
77652cad 247 af->address = kmalloc(header.a_text + header.a_data + header.a_bss,
984263bc
MD
248 M_LINKER, M_WAITOK);
249
250 /*
251 * Read the text and data sections and zero the bss.
252 */
fad57d0e 253 error = vn_rdwr(UIO_READ, vp, (void*) af->address,
984263bc 254 header.a_text + header.a_data, 0,
87de5057 255 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
256 if (error)
257 goto out;
258 bzero(af->address + header.a_text + header.a_data, header.a_bss);
259
260 /*
261 * Assume _DYNAMIC is the first data item.
262 */
263 af->dynamic = (struct _dynamic*) (af->address + header.a_text);
264 if (af->dynamic->d_version != LD_VERSION_BSD) {
efda3bd0
MD
265 kfree(af->address, M_LINKER);
266 kfree(af, M_LINKER);
984263bc
MD
267 goto out;
268 }
269 af->dynamic->d_un.d_sdt = (struct section_dispatch_table *)
270 ((char *)af->dynamic->d_un.d_sdt + (vm_offset_t)af->address);
271
272 lf = linker_make_file(filename, af, &link_aout_file_ops);
273 if (lf == NULL) {
efda3bd0
MD
274 kfree(af->address, M_LINKER);
275 kfree(af, M_LINKER);
984263bc
MD
276 error = ENOMEM;
277 goto out;
278 }
279 lf->address = af->address;
280 lf->size = header.a_text + header.a_data + header.a_bss;
281
282 if ((error = load_dependancies(lf)) != 0
283 || (error = relocate_file(lf)) != 0) {
284 linker_file_unload(lf);
285 goto out;
286 }
287
288 lf->flags |= LINKER_FILE_LINKED;
289 *result = lf;
290
291out:
a11aaa81 292 vn_unlock(vp);
87de5057 293 vn_close(vp, FREAD);
984263bc
MD
294
295 return error;
296}
297
298static void
299link_aout_unload_file(linker_file_t file)
300{
301 aout_file_t af = file->priv;
302
303 if (af) {
304 if (af->address)
efda3bd0
MD
305 kfree(af->address, M_LINKER);
306 kfree(af, M_LINKER);
984263bc
MD
307 }
308}
309
310static void
311link_aout_unload_module(linker_file_t file)
312{
313 aout_file_t af = file->priv;
314
315 if (af)
efda3bd0 316 kfree(af, M_LINKER);
984263bc
MD
317 if (file->filename)
318 preload_delete_name(file->filename);
319}
320
321#define AOUT_RELOC(af, type, off) (type*) ((af)->address + (off))
322
323static int
324load_dependancies(linker_file_t lf)
325{
326 aout_file_t af = lf->priv;
327 linker_file_t lfdep;
328 long off;
329 struct sod* sodp;
330 char* name;
331 char* filename = 0;
332 int error = 0;
333
334 /*
335 * All files are dependant on /kernel.
336 */
337 if (linker_kernel_file) {
338 linker_kernel_file->refs++;
339 linker_file_add_dependancy(lf, linker_kernel_file);
340 }
341
342 off = LD_NEED(af->dynamic);
343
344 /*
345 * Load the dependancies.
346 */
347 while (off != 0) {
348 sodp = AOUT_RELOC(af, struct sod, off);
349 name = AOUT_RELOC(af, char, sodp->sod_name);
350
351 error = linker_load_file(name, &lfdep);
352 if (error)
353 goto out;
354 error = linker_file_add_dependancy(lf, lfdep);
355 if (error)
356 goto out;
357 off = sodp->sod_next;
358 }
359
360out:
361 if (filename)
efda3bd0 362 kfree(filename, M_TEMP);
984263bc
MD
363 return error;
364}
365
366/*
367 * XXX i386 dependant.
368 */
369static long
370read_relocation(struct relocation_info* r, char* addr)
371{
372 int length = r->r_length;
373 if (length == 0)
374 return *(u_char*) addr;
375 else if (length == 1)
376 return *(u_short*) addr;
377 else if (length == 2)
378 return *(u_int*) addr;
379 else
6ea70f76 380 kprintf("link_aout: unsupported relocation size %d\n", r->r_length);
984263bc
MD
381 return 0;
382}
383
384static void
385write_relocation(struct relocation_info* r, char* addr, long value)
386{
387 int length = r->r_length;
388 if (length == 0)
389 *(u_char*) addr = value;
390 else if (length == 1)
391 *(u_short*) addr = value;
392 else if (length == 2)
393 *(u_int*) addr = value;
394 else
6ea70f76 395 kprintf("link_aout: unsupported relocation size %d\n", r->r_length);
984263bc
MD
396}
397
398static int
399relocate_file(linker_file_t lf)
400{
401 aout_file_t af = lf->priv;
402 struct relocation_info* rel;
403 struct relocation_info* erel;
404 struct relocation_info* r;
405 struct nzlist* symbolbase;
406 char* stringbase;
407 struct nzlist* np;
408 char* sym;
409 long relocation;
410
411 rel = AOUT_RELOC(af, struct relocation_info, LD_REL(af->dynamic));
412 erel = AOUT_RELOC(af, struct relocation_info,
413 LD_REL(af->dynamic) + LD_RELSZ(af->dynamic));
414 symbolbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
415 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
416
417 for (r = rel; r < erel; r++) {
418 char* addr;
419
420 if (r->r_address == 0)
421 break;
422
423 addr = AOUT_RELOC(af, char, r->r_address);
424 if (r->r_extern) {
425 np = &symbolbase[r->r_symbolnum];
426 sym = &stringbase[np->nz_strx];
427
428 if (sym[0] != '_') {
6ea70f76
SW
429 kprintf("link_aout: bad symbol name %s\n", sym);
430 kprintf("link_aout: symbol %s not found\n", sym);
984263bc 431 return ENOENT;
d1786c1b
MD
432 } else {
433 if (linker_file_lookup_symbol(lf, sym + 1,
434 (np->nz_type != (N_SETV+N_EXT)), (caddr_t *)&relocation)) {
6ea70f76 435 kprintf("link_aout: symbol %s not found\n", sym);
d1786c1b
MD
436 return ENOENT;
437 }
984263bc
MD
438 }
439
440 relocation += read_relocation(r, addr);
441
442 if (r->r_jmptable) {
6ea70f76 443 kprintf("link_aout: can't cope with jump table relocations\n");
984263bc
MD
444 continue;
445 }
446
447 if (r->r_pcrel)
448 relocation -= (intptr_t) af->address;
449
450 if (r->r_copy) {
6ea70f76 451 kprintf("link_aout: can't cope with copy relocations\n");
984263bc
MD
452 continue;
453 }
454
455 write_relocation(r, addr, relocation);
456 } else {
457 write_relocation(r, addr,
458 (intptr_t)(read_relocation(r, addr) + af->address));
459 }
460
461 }
462
463 return 0;
464}
465
466static long
467symbol_hash_value(aout_file_t af, const char* name)
468{
469 long hashval;
470 const char* p;
471
472 hashval = '_'; /* fake a starting '_' for C symbols */
473 for (p = name; *p; p++)
474 hashval = (hashval << 1) + *p;
475
476 return (hashval & 0x7fffffff) % LD_BUCKETS(af->dynamic);
477}
478
479int
480link_aout_lookup_symbol(linker_file_t file, const char* name,
481 c_linker_sym_t* sym)
482{
483 aout_file_t af = file->priv;
484 long hashval;
485 struct rrs_hash* hashbase;
486 struct nzlist* symbolbase;
487 char* stringbase;
488 struct rrs_hash* hp;
489 struct nzlist* np;
490 char* cp;
491
492 if (LD_BUCKETS(af->dynamic) == 0)
493 return 0;
494
495 hashbase = AOUT_RELOC(af, struct rrs_hash, LD_HASH(af->dynamic));
496 symbolbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
497 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
498
499restart:
500 hashval = symbol_hash_value(af, name);
501 hp = &hashbase[hashval];
502 if (hp->rh_symbolnum == -1)
503 return ENOENT;
504
505 while (hp) {
506 np = (struct nzlist *) &symbolbase[hp->rh_symbolnum];
507 cp = stringbase + np->nz_strx;
508 /*
509 * Note: we fake the leading '_' for C symbols.
510 */
511 if (cp[0] == '_' && !strcmp(cp + 1, name))
512 break;
513
514 if (hp->rh_next == 0)
515 hp = NULL;
516 else
517 hp = &hashbase[hp->rh_next];
518 }
519
520 if (hp == NULL)
521 /*
522 * Not found.
523 */
524 return ENOENT;
525
526 /*
527 * Check for an aliased symbol, whatever that is.
528 */
529 if (np->nz_type == N_INDR+N_EXT) {
530 name = stringbase + (++np)->nz_strx + 1; /* +1 for '_' */
531 goto restart;
532 }
533
534 /*
535 * Check this is an actual definition of the symbol.
536 */
537 if (np->nz_value == 0)
538 return ENOENT;
539
540 if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
541 if (np->nz_other == AUX_FUNC)
542 /* weak function */
543 return ENOENT;
544 }
545
546 *sym = (linker_sym_t) np;
547
548 return 0;
549}
550
551
552static int
553link_aout_symbol_values(linker_file_t file, c_linker_sym_t sym,
554 linker_symval_t* symval)
555{
556 aout_file_t af = file->priv;
557 const struct nzlist* np = (const struct nzlist*) sym;
558 char* stringbase;
559 long numsym = LD_STABSZ(af->dynamic) / sizeof(struct nzlist);
560 struct nzlist *symbase;
561
562 /* Is it one of ours? It could be another module... */
563 symbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
564 if (np < symbase)
565 return ENOENT;
566 if ((np - symbase) > numsym)
567 return ENOENT;
568
569 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
570
571 symval->name = stringbase + np->nz_strx + 1; /* +1 for '_' */
572 if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
573 symval->value = 0;
574 symval->size = np->nz_value;
575 } else {
576 symval->value = AOUT_RELOC(af, char, np->nz_value);
577 symval->size = np->nz_size;
578 }
579 return 0;
580}
581
582static int
583link_aout_search_symbol(linker_file_t lf, caddr_t value,
584 c_linker_sym_t* sym, long* diffp)
585{
586 aout_file_t af = lf->priv;
587 u_long off = (uintptr_t) (void *) value;
588 u_long diff = off;
589 u_long sp_nz_value;
590 struct nzlist* sp;
591 struct nzlist* ep;
592 struct nzlist* best = 0;
593
594 for (sp = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic)),
595 ep = (struct nzlist *) ((caddr_t) sp + LD_STABSZ(af->dynamic));
596 sp < ep; sp++) {
597 if (sp->nz_name == 0)
598 continue;
599 sp_nz_value = sp->nz_value + (uintptr_t) (void *) af->address;
600 if (off >= sp_nz_value) {
601 if (off - sp_nz_value < diff) {
602 diff = off - sp_nz_value;
603 best = sp;
604 if (diff == 0)
605 break;
606 } else if (off - sp_nz_value == diff) {
607 best = sp;
608 }
609 }
610 }
611 if (best == 0)
612 *diffp = off;
613 else
614 *diffp = diff;
615 *sym = (linker_sym_t) best;
616
617 return 0;
618}
619
dc62b251
MD
620/*
621 * Look up a linker set on an a.out + gnu LD system.
622 */
623
624struct generic_linker_set {
625 int ls_length;
626 void *ls_items[1];
627};
628
629static int
630link_aout_lookup_set(linker_file_t lf, const char *name,
631 void ***startp, void ***stopp, int *countp)
632{
633 c_linker_sym_t sym;
634 linker_symval_t symval;
635 void **start, **stop;
636 int error, count;
637 struct generic_linker_set *setp;
638
639 error = link_aout_lookup_symbol(lf, name, &sym);
640 if (error)
641 return error;
642 link_aout_symbol_values(lf, sym, &symval);
643 if (symval.value == 0)
644 return ESRCH;
645 setp = (struct generic_linker_set *)symval.value;
646 count = setp->ls_length;
647 start = &setp->ls_items[0];
648 stop = &setp->ls_items[count];
649 if (startp)
650 *startp = start;
651 if (stopp)
652 *stopp = stop;
653 if (countp)
654 *countp = count;
655 return 0;
656}