mips_reloc.c revision 1.3 1 1.3 mycroft /* $NetBSD: mips_reloc.c,v 1.3 1999/11/07 08:01:51 mycroft Exp $ */
2 1.1 mhitch
3 1.1 mhitch /*
4 1.1 mhitch * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
5 1.1 mhitch * All rights reserved.
6 1.1 mhitch *
7 1.1 mhitch * Redistribution and use in source and binary forms, with or without
8 1.1 mhitch * modification, are permitted provided that the following conditions
9 1.1 mhitch * are met:
10 1.1 mhitch * 1. Redistributions of source code must retain the above copyright
11 1.1 mhitch * notice, this list of conditions and the following disclaimer.
12 1.1 mhitch * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mhitch * notice, this list of conditions and the following disclaimer in the
14 1.1 mhitch * documentation and/or other materials provided with the distribution.
15 1.1 mhitch * 3. The name of the author may not be used to endorse or promote products
16 1.1 mhitch * derived from this software without specific prior written permission.
17 1.1 mhitch *
18 1.1 mhitch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mhitch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mhitch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mhitch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mhitch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 mhitch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 mhitch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 mhitch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 mhitch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 mhitch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 mhitch */
29 1.1 mhitch
30 1.1 mhitch
31 1.1 mhitch #include <stdarg.h>
32 1.1 mhitch #include <sys/types.h>
33 1.3 mycroft #include <sys/stat.h>
34 1.1 mhitch
35 1.1 mhitch #include "debug.h"
36 1.1 mhitch #include "rtld.h"
37 1.1 mhitch
38 1.1 mhitch /*
39 1.1 mhitch * Relocate a MIPS GOT
40 1.1 mhitch */
41 1.1 mhitch
42 1.1 mhitch void
43 1.1 mhitch _rtld_relocate_mips_got(obj)
44 1.1 mhitch Obj_Entry *obj;
45 1.1 mhitch {
46 1.1 mhitch Elf_Addr *got = obj->pltgot;
47 1.1 mhitch const Elf_Sym *sym = obj->symtab;
48 1.1 mhitch const Elf_Sym *def;
49 1.1 mhitch const Obj_Entry *defobj;
50 1.1 mhitch int i;
51 1.1 mhitch
52 1.1 mhitch i = (got[1] & 0x80000000) ? 2 : 1;
53 1.1 mhitch /* Relocate the local GOT entries */
54 1.1 mhitch while (i < obj->local_gotno)
55 1.1 mhitch got[i++] += (Elf_Word)obj->relocbase;
56 1.1 mhitch got += obj->local_gotno;
57 1.1 mhitch i = obj->symtabno - obj->gotsym;
58 1.1 mhitch sym += obj->gotsym;
59 1.1 mhitch /* Now do the global GOT entries */
60 1.1 mhitch while (i--) {
61 1.1 mhitch def = _rtld_find_symdef(_rtld_objlist, 0,
62 1.1 mhitch sym->st_name + obj->strtab, obj, &defobj, true);
63 1.1 mhitch if (def != NULL) {
64 1.2 kleink if (sym->st_shndx == SHN_UNDEF) {
65 1.1 mhitch #if 0 /* These don't seem to work? */
66 1.1 mhitch
67 1.2 kleink if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
68 1.2 kleink STT_FUNC) {
69 1.1 mhitch if (sym->st_value)
70 1.1 mhitch *got = sym->st_value +
71 1.1 mhitch (Elf_Word)obj->relocbase;
72 1.1 mhitch else
73 1.1 mhitch *got = def->st_value +
74 1.1 mhitch (Elf_Word)defobj->relocbase;
75 1.1 mhitch } else
76 1.1 mhitch #endif
77 1.1 mhitch *got = def->st_value +
78 1.1 mhitch (Elf_Word)defobj->relocbase;
79 1.2 kleink } else if (sym->st_shndx == SHN_COMMON) {
80 1.1 mhitch *got = def->st_value +
81 1.1 mhitch (Elf_Word)defobj->relocbase;
82 1.2 kleink } else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
83 1.2 kleink STT_FUNC &&
84 1.2 kleink *got != sym->st_value) {
85 1.1 mhitch *got += (Elf_Word)obj->relocbase;
86 1.2 kleink } else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
87 1.2 kleink STT_SECTION && ELFDEFNNAME(ST_BIND)(sym->st_info) ==
88 1.2 kleink STB_GLOBAL) {
89 1.2 kleink if (sym->st_shndx == SHN_ABS)
90 1.1 mhitch *got = sym->st_value +
91 1.1 mhitch (Elf_Word)obj->relocbase;
92 1.1 mhitch /* else SGI stuff ignored */
93 1.1 mhitch } else
94 1.1 mhitch *got = def->st_value +
95 1.1 mhitch (Elf_Word)defobj->relocbase;
96 1.1 mhitch }
97 1.1 mhitch ++sym;
98 1.1 mhitch ++got;
99 1.1 mhitch }
100 1.1 mhitch }
101 1.1 mhitch
102 1.1 mhitch /*
103 1.1 mhitch * _rtld_bind_mips(symbol_index, return_address, old_gp, stub_return_addr)
104 1.1 mhitch */
105 1.1 mhitch
106 1.1 mhitch caddr_t
107 1.1 mhitch _rtld_bind_mips(a0, a1, a2, a3)
108 1.1 mhitch Elf_Word a0;
109 1.1 mhitch Elf_Addr a1, a2, a3;
110 1.1 mhitch {
111 1.1 mhitch Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
112 1.1 mhitch Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
113 1.1 mhitch const Elf_Sym *def;
114 1.1 mhitch const Obj_Entry *defobj;
115 1.1 mhitch
116 1.1 mhitch def = _rtld_find_symdef(_rtld_objlist, a0 << 8, NULL, obj, &defobj,
117 1.1 mhitch true);
118 1.1 mhitch if (def) {
119 1.1 mhitch u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
120 1.1 mhitch (def->st_value + defobj->relocbase);
121 1.1 mhitch return((caddr_t)(def->st_value + defobj->relocbase));
122 1.1 mhitch }
123 1.1 mhitch
124 1.1 mhitch return(NULL); /* XXX */
125 1.1 mhitch }
126