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 $
31 #define FREEBSD_AOUT 1
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/namei.h>
39 #include <sys/fcntl.h>
40 #include <sys/vnode.h>
41 #include <sys/linker.h>
43 #include <vm/vm_zone.h>
48 #include <machine/vmparam.h>
54 static int link_aout_load_module(const char*, linker_file_t*);
56 static int link_aout_load_file(const char*, linker_file_t*);
58 static int link_aout_lookup_symbol(linker_file_t, const char*,
60 static int link_aout_symbol_values(linker_file_t file, c_linker_sym_t sym,
61 linker_symval_t* symval);
62 static int link_aout_search_symbol(linker_file_t lf, caddr_t value,
63 c_linker_sym_t* sym, long* diffp);
64 static void link_aout_unload_file(linker_file_t);
65 static void link_aout_unload_module(linker_file_t);
67 static struct linker_class_ops link_aout_class_ops = {
68 link_aout_load_module,
71 static struct linker_file_ops link_aout_file_ops = {
72 link_aout_lookup_symbol,
73 link_aout_symbol_values,
74 link_aout_search_symbol,
75 link_aout_unload_file,
77 static struct linker_file_ops link_aout_module_ops = {
78 link_aout_lookup_symbol,
79 link_aout_symbol_values,
80 link_aout_search_symbol,
81 link_aout_unload_module,
84 typedef struct aout_file {
85 char* address; /* Load address */
86 struct _dynamic* dynamic; /* Symbol table etc. */
89 static int load_dependancies(linker_file_t lf);
90 static int relocate_file(linker_file_t lf);
93 * The kernel symbol table starts here.
95 extern struct _dynamic _DYNAMIC;
98 link_aout_init(void* arg)
101 struct _dynamic* dp = &_DYNAMIC;
104 linker_add_class("a.out", NULL, &link_aout_class_ops);
110 af = malloc(sizeof(struct aout_file), M_LINKER, M_NOWAIT);
112 panic("link_aout_init: Can't create linker structures for kernel");
113 bzero(af, sizeof(*af));
118 linker_make_file(kernelname, af, &link_aout_file_ops);
119 if (linker_kernel_file == NULL)
120 panic("link_aout_init: Can't create linker structures for kernel");
121 linker_kernel_file->address = (caddr_t) KERNBASE;
122 linker_kernel_file->size = -(long)linker_kernel_file->address;
123 linker_current_file = linker_kernel_file;
124 linker_kernel_file->flags |= LINKER_FILE_LINKED;
129 SYSINIT(link_aout, SI_SUB_KLD, SI_ORDER_THIRD, link_aout_init, 0);
132 link_aout_load_module(const char* filename, linker_file_t* result)
134 caddr_t modptr, baseptr;
141 /* Look to see if we have the module preloaded. */
142 if ((modptr = preload_search_by_name(filename)) == NULL)
143 return(link_aout_load_file(filename, result));
145 /* It's preloaded, check we can handle it and collect information. */
146 if (((type = (char *)preload_search_info(modptr, MODINFO_TYPE)) == NULL) ||
147 strcmp(type, "a.out module") ||
148 ((baseptr = preload_search_info(modptr, MODINFO_ADDR)) == NULL) ||
149 ((ehdr = (struct exec *)preload_search_info(modptr, MODINFO_METADATA | MODINFOMD_AOUTEXEC)) == NULL))
150 return(0); /* we can't handle this */
152 /* Looks like we can handle this one */
153 af = malloc(sizeof(struct aout_file), M_LINKER, M_WAITOK);
154 bzero(af, sizeof(*af));
155 af->address = baseptr;
157 /* Assume _DYNAMIC is the first data item. */
158 af->dynamic = (struct _dynamic*)(af->address + ehdr->a_text);
159 if (af->dynamic->d_version != LD_VERSION_BSD) {
161 return(0); /* we can't handle this */
163 af->dynamic->d_un.d_sdt = (struct section_dispatch_table *)
164 ((char *)af->dynamic->d_un.d_sdt + (vm_offset_t)af->address);
166 /* Register with kld */
167 lf = linker_make_file(filename, af, &link_aout_module_ops);
172 lf->address = af->address;
173 lf->size = ehdr->a_text + ehdr->a_data + ehdr->a_bss;
175 /* Try to load dependancies */
176 if (((error = load_dependancies(lf)) != 0) ||
177 ((error = relocate_file(lf)) != 0)) {
178 linker_file_unload(lf);
181 lf->flags |= LINKER_FILE_LINKED;
187 link_aout_load_file(const char* filename, linker_file_t* result)
190 struct proc* p = curproc; /* XXX */
198 pathname = linker_search_path(filename);
199 if (pathname == NULL)
201 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, pathname, p);
202 error = vn_open(&nd, FREAD, 0);
203 free(pathname, M_LINKER);
206 NDFREE(&nd, NDF_ONLY_PNBUF);
209 * Read the a.out header from the file.
211 error = vn_rdwr(UIO_READ, nd.ni_vp, (void*) &header, sizeof header, 0,
212 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
216 if (N_BADMAG(header) || !(N_GETFLAG(header) & EX_DYNAMIC))
220 * We have an a.out file, so make some space to read it in.
222 af = malloc(sizeof(struct aout_file), M_LINKER, M_WAITOK);
223 bzero(af, sizeof(*af));
224 af->address = malloc(header.a_text + header.a_data + header.a_bss,
228 * Read the text and data sections and zero the bss.
230 error = vn_rdwr(UIO_READ, nd.ni_vp, (void*) af->address,
231 header.a_text + header.a_data, 0,
232 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
235 bzero(af->address + header.a_text + header.a_data, header.a_bss);
238 * Assume _DYNAMIC is the first data item.
240 af->dynamic = (struct _dynamic*) (af->address + header.a_text);
241 if (af->dynamic->d_version != LD_VERSION_BSD) {
242 free(af->address, M_LINKER);
246 af->dynamic->d_un.d_sdt = (struct section_dispatch_table *)
247 ((char *)af->dynamic->d_un.d_sdt + (vm_offset_t)af->address);
249 lf = linker_make_file(filename, af, &link_aout_file_ops);
251 free(af->address, M_LINKER);
256 lf->address = af->address;
257 lf->size = header.a_text + header.a_data + header.a_bss;
259 if ((error = load_dependancies(lf)) != 0
260 || (error = relocate_file(lf)) != 0) {
261 linker_file_unload(lf);
265 lf->flags |= LINKER_FILE_LINKED;
269 VOP_UNLOCK(nd.ni_vp, 0, p);
270 vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
276 link_aout_unload_file(linker_file_t file)
278 aout_file_t af = file->priv;
282 free(af->address, M_LINKER);
288 link_aout_unload_module(linker_file_t file)
290 aout_file_t af = file->priv;
295 preload_delete_name(file->filename);
298 #define AOUT_RELOC(af, type, off) (type*) ((af)->address + (off))
301 load_dependancies(linker_file_t lf)
303 aout_file_t af = lf->priv;
312 * All files are dependant on /kernel.
314 if (linker_kernel_file) {
315 linker_kernel_file->refs++;
316 linker_file_add_dependancy(lf, linker_kernel_file);
319 off = LD_NEED(af->dynamic);
322 * Load the dependancies.
325 sodp = AOUT_RELOC(af, struct sod, off);
326 name = AOUT_RELOC(af, char, sodp->sod_name);
328 error = linker_load_file(name, &lfdep);
331 error = linker_file_add_dependancy(lf, lfdep);
334 off = sodp->sod_next;
339 free(filename, M_TEMP);
344 * XXX i386 dependant.
347 read_relocation(struct relocation_info* r, char* addr)
349 int length = r->r_length;
351 return *(u_char*) addr;
352 else if (length == 1)
353 return *(u_short*) addr;
354 else if (length == 2)
355 return *(u_int*) addr;
357 printf("link_aout: unsupported relocation size %d\n", r->r_length);
362 write_relocation(struct relocation_info* r, char* addr, long value)
364 int length = r->r_length;
366 *(u_char*) addr = value;
367 else if (length == 1)
368 *(u_short*) addr = value;
369 else if (length == 2)
370 *(u_int*) addr = value;
372 printf("link_aout: unsupported relocation size %d\n", r->r_length);
376 relocate_file(linker_file_t lf)
378 aout_file_t af = lf->priv;
379 struct relocation_info* rel;
380 struct relocation_info* erel;
381 struct relocation_info* r;
382 struct nzlist* symbolbase;
388 rel = AOUT_RELOC(af, struct relocation_info, LD_REL(af->dynamic));
389 erel = AOUT_RELOC(af, struct relocation_info,
390 LD_REL(af->dynamic) + LD_RELSZ(af->dynamic));
391 symbolbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
392 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
394 for (r = rel; r < erel; r++) {
397 if (r->r_address == 0)
400 addr = AOUT_RELOC(af, char, r->r_address);
402 np = &symbolbase[r->r_symbolnum];
403 sym = &stringbase[np->nz_strx];
406 printf("link_aout: bad symbol name %s\n", sym);
409 relocation = (intptr_t)
410 linker_file_lookup_symbol(lf, sym + 1,
411 np->nz_type != (N_SETV+N_EXT));
413 printf("link_aout: symbol %s not found\n", sym);
417 relocation += read_relocation(r, addr);
420 printf("link_aout: can't cope with jump table relocations\n");
425 relocation -= (intptr_t) af->address;
428 printf("link_aout: can't cope with copy relocations\n");
432 write_relocation(r, addr, relocation);
434 write_relocation(r, addr,
435 (intptr_t)(read_relocation(r, addr) + af->address));
444 symbol_hash_value(aout_file_t af, const char* name)
449 hashval = '_'; /* fake a starting '_' for C symbols */
450 for (p = name; *p; p++)
451 hashval = (hashval << 1) + *p;
453 return (hashval & 0x7fffffff) % LD_BUCKETS(af->dynamic);
457 link_aout_lookup_symbol(linker_file_t file, const char* name,
460 aout_file_t af = file->priv;
462 struct rrs_hash* hashbase;
463 struct nzlist* symbolbase;
469 if (LD_BUCKETS(af->dynamic) == 0)
472 hashbase = AOUT_RELOC(af, struct rrs_hash, LD_HASH(af->dynamic));
473 symbolbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
474 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
477 hashval = symbol_hash_value(af, name);
478 hp = &hashbase[hashval];
479 if (hp->rh_symbolnum == -1)
483 np = (struct nzlist *) &symbolbase[hp->rh_symbolnum];
484 cp = stringbase + np->nz_strx;
486 * Note: we fake the leading '_' for C symbols.
488 if (cp[0] == '_' && !strcmp(cp + 1, name))
491 if (hp->rh_next == 0)
494 hp = &hashbase[hp->rh_next];
504 * Check for an aliased symbol, whatever that is.
506 if (np->nz_type == N_INDR+N_EXT) {
507 name = stringbase + (++np)->nz_strx + 1; /* +1 for '_' */
512 * Check this is an actual definition of the symbol.
514 if (np->nz_value == 0)
517 if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
518 if (np->nz_other == AUX_FUNC)
523 *sym = (linker_sym_t) np;
530 link_aout_symbol_values(linker_file_t file, c_linker_sym_t sym,
531 linker_symval_t* symval)
533 aout_file_t af = file->priv;
534 const struct nzlist* np = (const struct nzlist*) sym;
536 long numsym = LD_STABSZ(af->dynamic) / sizeof(struct nzlist);
537 struct nzlist *symbase;
539 /* Is it one of ours? It could be another module... */
540 symbase = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic));
543 if ((np - symbase) > numsym)
546 stringbase = AOUT_RELOC(af, char, LD_STRINGS(af->dynamic));
548 symval->name = stringbase + np->nz_strx + 1; /* +1 for '_' */
549 if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
551 symval->size = np->nz_value;
553 symval->value = AOUT_RELOC(af, char, np->nz_value);
554 symval->size = np->nz_size;
560 link_aout_search_symbol(linker_file_t lf, caddr_t value,
561 c_linker_sym_t* sym, long* diffp)
563 aout_file_t af = lf->priv;
564 u_long off = (uintptr_t) (void *) value;
569 struct nzlist* best = 0;
571 for (sp = AOUT_RELOC(af, struct nzlist, LD_SYMBOL(af->dynamic)),
572 ep = (struct nzlist *) ((caddr_t) sp + LD_STABSZ(af->dynamic));
574 if (sp->nz_name == 0)
576 sp_nz_value = sp->nz_value + (uintptr_t) (void *) af->address;
577 if (off >= sp_nz_value) {
578 if (off - sp_nz_value < diff) {
579 diff = off - sp_nz_value;
583 } else if (off - sp_nz_value == diff) {
592 *sym = (linker_sym_t) best;
597 #endif /* !__alpha__ */