mips_reloc.c revision 1.6 1 1.6 mycroft /* $NetBSD: mips_reloc.c,v 1.6 2002/09/05 15:38:28 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.6 mycroft * _rtld_bind_mips(symbol_index, return_address, old_gp, stub_return_addr)
40 1.1 mhitch */
41 1.1 mhitch
42 1.6 mycroft caddr_t
43 1.6 mycroft _rtld_bind_mips(a0, a1, a2, a3)
44 1.6 mycroft Elf_Word a0;
45 1.6 mycroft Elf_Addr a1, a2, a3;
46 1.6 mycroft {
47 1.6 mycroft Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
48 1.6 mycroft Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
49 1.6 mycroft const Elf_Sym *def;
50 1.6 mycroft const Obj_Entry *defobj;
51 1.6 mycroft
52 1.6 mycroft def = _rtld_find_symdef(_rtld_objlist, a0 << 8, NULL, obj, &defobj,
53 1.6 mycroft true);
54 1.6 mycroft if (def) {
55 1.6 mycroft u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
56 1.6 mycroft (def->st_value + defobj->relocbase);
57 1.6 mycroft return((caddr_t)(def->st_value + defobj->relocbase));
58 1.6 mycroft }
59 1.6 mycroft
60 1.6 mycroft return(NULL); /* XXX */
61 1.6 mycroft }
62 1.6 mycroft
63 1.1 mhitch void
64 1.6 mycroft _rtld_setup_pltgot(obj)
65 1.1 mhitch Obj_Entry *obj;
66 1.1 mhitch {
67 1.1 mhitch Elf_Addr *got = obj->pltgot;
68 1.1 mhitch const Elf_Sym *sym = obj->symtab;
69 1.1 mhitch const Elf_Sym *def;
70 1.1 mhitch const Obj_Entry *defobj;
71 1.4 rafal Elf_Word info;
72 1.1 mhitch int i;
73 1.1 mhitch
74 1.1 mhitch i = (got[1] & 0x80000000) ? 2 : 1;
75 1.1 mhitch /* Relocate the local GOT entries */
76 1.1 mhitch while (i < obj->local_gotno)
77 1.1 mhitch got[i++] += (Elf_Word)obj->relocbase;
78 1.1 mhitch got += obj->local_gotno;
79 1.1 mhitch i = obj->symtabno - obj->gotsym;
80 1.1 mhitch sym += obj->gotsym;
81 1.1 mhitch /* Now do the global GOT entries */
82 1.1 mhitch while (i--) {
83 1.4 rafal rdbg(1, (" doing got %d sym %p (%s, %x)",
84 1.4 rafal obj->symtabno - obj->gotsym - i - 1,
85 1.4 rafal sym, sym->st_name + obj->strtab, *got));
86 1.4 rafal
87 1.4 rafal info = ELF32_R_INFO(obj->symtabno - i - 1, sym->st_info);
88 1.4 rafal def = _rtld_find_symdef(_rtld_objlist, info, NULL, obj,
89 1.4 rafal &defobj, true);
90 1.4 rafal
91 1.4 rafal if (def == NULL)
92 1.4 rafal _rtld_error(
93 1.4 rafal "%s: Undefined PLT symbol \"%s\" (reloc type = %ld, symnum = %ld)",
94 1.4 rafal obj->path, sym->st_name + obj->strtab,
95 1.4 rafal (u_long) ELF_R_TYPE(info),
96 1.4 rafal (u_long) obj->symtabno - i - 1);
97 1.5 rafal else {
98 1.4 rafal
99 1.5 rafal if (sym->st_shndx == SHN_UNDEF) {
100 1.1 mhitch #if 0 /* These don't seem to work? */
101 1.1 mhitch
102 1.5 rafal if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
103 1.5 rafal STT_FUNC) {
104 1.5 rafal if (sym->st_value)
105 1.5 rafal *got = sym->st_value +
106 1.5 rafal (Elf_Word)obj->relocbase;
107 1.5 rafal else
108 1.5 rafal *got = def->st_value +
109 1.5 rafal (Elf_Word)defobj->relocbase;
110 1.5 rafal } else
111 1.5 rafal #endif
112 1.5 rafal *got = def->st_value +
113 1.5 rafal (Elf_Word)defobj->relocbase;
114 1.5 rafal } else if (sym->st_shndx == SHN_COMMON) {
115 1.5 rafal *got = def->st_value +
116 1.5 rafal (Elf_Word)defobj->relocbase;
117 1.5 rafal } else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
118 1.5 rafal STT_FUNC &&
119 1.5 rafal *got != sym->st_value) {
120 1.5 rafal *got += (Elf_Word)obj->relocbase;
121 1.5 rafal } else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
122 1.5 rafal STT_SECTION && ELFDEFNNAME(ST_BIND)(sym->st_info) ==
123 1.5 rafal STB_GLOBAL) {
124 1.5 rafal if (sym->st_shndx == SHN_ABS)
125 1.4 rafal *got = sym->st_value +
126 1.4 rafal (Elf_Word)obj->relocbase;
127 1.5 rafal /* else SGI stuff ignored */
128 1.1 mhitch } else
129 1.1 mhitch *got = def->st_value +
130 1.1 mhitch (Elf_Word)defobj->relocbase;
131 1.5 rafal }
132 1.4 rafal
133 1.1 mhitch ++sym;
134 1.1 mhitch ++got;
135 1.1 mhitch }
136 1.1 mhitch
137 1.6 mycroft obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
138 1.6 mycroft /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
139 1.6 mycroft obj->pltgot[1] |= (Elf_Addr) obj;
140 1.1 mhitch }
141