2 * Copyright (c) 1997 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/kern/link_aout.c,v 1.26 1999/12/24 15:33:36 bde Exp $
27 * $DragonFly: src/sys/kern/link_aout.c,v 1.5 2003/07/18 05:12:39 dillon Exp $
32 #define FREEBSD_AOUT 1
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/namei.h>
40 #include <sys/fcntl.h>
41 #include <sys/vnode.h>
42 #include <sys/linker.h>
44 #include <vm/vm_zone.h>
49 #include <machine/vmparam.h>
55 static int link_aout_load_module(const char*, linker_file_t*);
57 static int link_aout_load_file(const char*, linker_file_t*);
59 static int link_aout_lookup_symbol(linker_file_t, const char*,
61 static int link_aout_symbol_values(linker_file_t file, c_linker_sym_t sym,
62 linker_symval_t* symval);
63 static int link_aout_search_symbol(linker_file_t lf, caddr_t value,
64 c_linker_sym_t* sym, long* diffp);
65 static void link_aout_unload_file(linker_file_t);
66 static void link_aout_unload_module(linker_file_t);
68 static struct linker_class_ops link_aout_class_ops = {
69 link_aout_load_module,
72 static struct linker_file_ops link_aout_file_ops = {
73 link_aout_lookup_symbol,
74 link_aout_symbol_values,
75 link_aout_search_symbol,
76 link_aout_unload_file,
78 static struct linker_file_ops link_aout_module_ops = {
79 link_aout_lookup_symbol,
80 link_aout_symbol_values,
81 link_aout_search_symbol,
82 link_aout_unload_module,
85 typedef struct aout_file {
86 char* address; /* Load address */
87 struct _dynamic* dynamic; /* Symbol table etc. */
90 static int load_dependancies(linker_file_t lf);
91 static int relocate_file(linker_file_t lf);
94 * The kernel symbol table starts here.
96 extern struct _dynamic _DYNAMIC;
99 link_aout_init(void* arg)
102 struct _dynamic* dp = &_DYNAMIC;
105 linker_add_class("a.out", NULL, &link_aout_class_ops);
111 af = malloc(sizeof(struct aout_file), M_LINKER, M_NOWAIT);
113 panic("link_aout_init: Can't create linker structures for kernel");
114 bzero(af, sizeof(*af));
119 linker_make_file(kernelname, af, &link_aout_file_ops);
120 if (linker_kernel_file == NULL)
121 panic("link_aout_init: Can't create linker structures for kernel");
122 linker_kernel_file->address = (caddr_t) KERNBASE;
123 linker_kernel_file->size = -(long)linker_kernel_file->address;
124 linker_current_file = linker_kernel_file;
125 linker_kernel_file->flags |= LINKER_FILE_LINKED;
130 SYSINIT(link_aout, SI_SUB_KLD, SI_ORDER_THIRD, link_aout_init, 0);
133 link_aout_load_module(const char* filename, linker_file_t* result)
135 caddr_t modptr, baseptr;
142 /* Look to see if we have the module preloaded. */
143 if ((modptr = preload_search_by_name(filename)) == NULL)
144 return(link_aout_load_file(filename, result));
146 /* It's preloaded, check we can handle it and collect information. */
147 if (((type = (char *)preload_search_info(modptr, MODINFO_TYPE)) == NULL) ||
148 strcmp(type, "a.out module") ||
149 ((baseptr = preload_search_info(modptr, MODINFO_ADDR)) == NULL) ||
150 ((ehdr = (struct exec *)preload_search_info(modptr, MODINFO_METADATA | MODINFOMD_AOUTEXEC)) == NULL))
151 return(0); /* we can't handle this */
153 /* Looks like we can handle this one */
154 af = malloc(sizeof(struct aout_file), M_LINKER, M_WAITOK);
155 bzero(af, sizeof(*af));
156 af->address = baseptr;
158 /* Assume _DYNAMIC is the first data item. */
159 af->dynamic = (struct _dynamic*)(af->address + ehdr->a_text);
160 if (af->dynamic->d_version != LD_VERSION_BSD) {
162 return(0); /* we can't handle this */
164 af->dynamic->d_un.d_sdt = (struct section_dispatch_table *)
165 ((char *)af->dynamic->d_un.d_sdt + (vm_offset_t)af->address);
167 /* Register with kld */
168 lf = linker_make_file(filename, af, &link_aout_module_ops);
173 lf->address = af->address;
174 lf->size = ehdr->a_text + ehdr->a_data + ehdr->a_bss;
176 /* Try to load dependancies */
177 if (((error = load_dependancies(lf)) != 0) ||
178 ((error = relocate_file(lf)) != 0)) {
179 linker_file_unload(lf);
182 lf->flags |= LINKER_FILE_LINKED;
188 link_aout_load_file(const char* filename, linker_file_t* result)
191 struct thread *td = curthread;
192 struct proc *p = td->td_proc;
202 pathname = linker_search_path(filename);
203 if (pathname == NULL)
205 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, pathname, td);
206 error = vn_open(&nd, FREAD, 0);
207 free(pathname, M_LINKER);
210 NDFREE(&nd, NDF_ONLY_PNBUF);
213 * Read the a.out header from the file.
215 error = vn_rdwr(UIO_READ, nd.ni_vp, (void*) &header, sizeof header, 0,
216 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
220 if (N_BADMAG(header) || !(N_GETFLAG(header) & EX_DYNAMIC))
224 * We have an a.out file, so make some space to read it in.
226 af = malloc(sizeof(struct aout_file), M_LINKER, M_WAITOK);
227 bzero(af, sizeof(*af));
228 af->address = malloc(header.a_text + header.a_data + header.a_bss,
232 * Read the text and data sections and zero the bss.
234 error = vn_rdwr(UIO_READ, nd.ni_vp, (void*) af->address,
235 header.a_text + header.a_data, 0,
236 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
239 bzero(af->address + header.a_text + header.a_data, header.a_bss);
242 * Assume _DYNAMIC is the first data item.
244 af->dynamic = (struct _dynamic*) (af->address + header.a_text);
245 if (af->dynamic->d_version != LD_VERSION_BSD) {
246 free(af->address, M_LINKER);
250 af->dynamic->d_un.d_sdt = (struct section_dispatch_table *)
251 ((char *)af->dynamic->d_un.d_sdt + (vm_offset_t)af->address);
253 lf = linker_make_file(filename, af, &link_aout_file_ops);
255 free(af->address, M_LINKER);
260 lf->address = af->address;
261 lf->size = header.a_text + header.a_data + header.a_bss;
263 if ((error = load_dependancies(lf)) != 0
264 || (error = relocate_file(lf)) != 0) {
265 linker_file_unload(lf);
269 lf->flags |= LINKER_FILE_LINKED;
273 VOP_UNLOCK(nd.ni_vp, 0, td);
274 vn_close(nd.ni_vp, FREAD, td);
280 link_aout_unload_file(linker_file_t file)
282 aout_file_t af = file->priv;
286 free(af->address, M_LINKER);
292 link_aout_unload_module(linker_file_t file)
294 aout_file_t af = file->priv;
299 preload_delete_name(file->filename);
302 #define AOUT_RELOC(af, type, off) (type*) ((af)->address + (off))
305 load_dependancies(linker_file_t lf)
307 aout_file_t af = lf->priv;
316 * All files are dependant on /kernel.
318 if (linker_kernel_file) {
319 linker_kernel_file->refs++;
320 linker_file_add_dependancy(lf, linker_kernel_file);
323 off = LD_NEED(af->dynamic);
326 * Load the dependancies.
329 sodp = AOUT_RELOC(af, struct sod, off);
330 name = AOUT_RELOC(af, char, sodp->sod_name);
332 error = linker_load_file(name, &lfdep);
335 error = linker_file_add_dependancy(lf, lfdep);
338 off = sodp->sod_next;
343 free(filename, M_TEMP);
348 * XXX i386 dependant.
351 read_relocation(struct relocation_info* r, char* addr)
353 int length = r->r_length;
355 return *(u_char*) addr;
356 else if (length == 1)
357 return *(u_short*) addr;
358 else if (length == 2)
359 return *(u_int*) addr;
361 printf("link_aout: unsupported relocation size %d\n", r->r_length);
366 write_relocation(struct relocation_info* r, char* addr, long value)
368 int length = r->r_length;
370 *(u_char*) addr = value;
371 else if (length == 1)
372 *(u_short*) addr = value;
373 else if (length == 2)
374 *(u_int*) addr = value;
376 printf("link_aout: unsupported relocation size %d\n", r->r_length);
380 relocate_file(linker_file_t lf)
382 aout_file_t af = lf->priv;
383 struct relocation_info* rel;
384 struct relocation_info* erel;
385 struct relocation_info* r;
386 struct nzlist* symbolbase;
392 rel = AOUT_RELOC(af, struct relocation_info, LD_REL(af->dynamic));
393 erel = AOUT_RELOC(af, struct relocation_info,
394 LD_REL(af->dynamic) + LD_RELSZ(af->dynamic));
395 symbolbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
396 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
398 for (r = rel; r < erel; r++) {
401 if (r->r_address == 0)
404 addr = AOUT_RELOC(af, char, r->r_address);
406 np = &symbolbase[r->r_symbolnum];
407 sym = &stringbase[np->nz_strx];
410 printf("link_aout: bad symbol name %s\n", sym);
411 printf("link_aout: symbol %s not found\n", sym);
414 if (linker_file_lookup_symbol(lf, sym + 1,
415 (np->nz_type != (N_SETV+N_EXT)), (caddr_t *)&relocation)) {
416 printf("link_aout: symbol %s not found\n", sym);
421 relocation += read_relocation(r, addr);
424 printf("link_aout: can't cope with jump table relocations\n");
429 relocation -= (intptr_t) af->address;
432 printf("link_aout: can't cope with copy relocations\n");
436 write_relocation(r, addr, relocation);
438 write_relocation(r, addr,
439 (intptr_t)(read_relocation(r, addr) + af->address));
448 symbol_hash_value(aout_file_t af, const char* name)
453 hashval = '_'; /* fake a starting '_' for C symbols */
454 for (p = name; *p; p++)
455 hashval = (hashval << 1) + *p;
457 return (hashval & 0x7fffffff) % LD_BUCKETS(af->dynamic);
461 link_aout_lookup_symbol(linker_file_t file, const char* name,
464 aout_file_t af = file->priv;
466 struct rrs_hash* hashbase;
467 struct nzlist* symbolbase;
473 if (LD_BUCKETS(af->dynamic) == 0)
476 hashbase = AOUT_RELOC(af, struct rrs_hash, LD_HASH(af->dynamic));
477 symbolbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
478 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
481 hashval = symbol_hash_value(af, name);
482 hp = &hashbase[hashval];
483 if (hp->rh_symbolnum == -1)
487 np = (struct nzlist *) &symbolbase[hp->rh_symbolnum];
488 cp = stringbase + np->nz_strx;
490 * Note: we fake the leading '_' for C symbols.
492 if (cp[0] == '_' && !strcmp(cp + 1, name))
495 if (hp->rh_next == 0)
498 hp = &hashbase[hp->rh_next];
508 * Check for an aliased symbol, whatever that is.
510 if (np->nz_type == N_INDR+N_EXT) {
511 name = stringbase + (++np)->nz_strx + 1; /* +1 for '_' */
516 * Check this is an actual definition of the symbol.
518 if (np->nz_value == 0)
521 if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
522 if (np->nz_other == AUX_FUNC)
527 *sym = (linker_sym_t) np;
534 link_aout_symbol_values(linker_file_t file, c_linker_sym_t sym,
535 linker_symval_t* symval)
537 aout_file_t af = file->priv;
538 const struct nzlist* np = (const struct nzlist*) sym;
540 long numsym = LD_STABSZ(af->dynamic) / sizeof(struct nzlist);
541 struct nzlist *symbase;
543 /* Is it one of ours? It could be another module... */
544 symbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
547 if ((np - symbase) > numsym)
550 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
552 symval->name = stringbase + np->nz_strx + 1; /* +1 for '_' */
553 if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
555 symval->size = np->nz_value;
557 symval->value = AOUT_RELOC(af, char, np->nz_value);
558 symval->size = np->nz_size;
564 link_aout_search_symbol(linker_file_t lf, caddr_t value,
565 c_linker_sym_t* sym, long* diffp)
567 aout_file_t af = lf->priv;
568 u_long off = (uintptr_t) (void *) value;
573 struct nzlist* best = 0;
575 for (sp = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic)),
576 ep = (struct nzlist *) ((caddr_t) sp + LD_STABSZ(af->dynamic));
578 if (sp->nz_name == 0)
580 sp_nz_value = sp->nz_value + (uintptr_t) (void *) af->address;
581 if (off >= sp_nz_value) {
582 if (off - sp_nz_value < diff) {
583 diff = off - sp_nz_value;
587 } else if (off - sp_nz_value == diff) {
596 *sym = (linker_sym_t) best;
601 #endif /* !__alpha__ */