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