2002-09-12 22:28:53 +04:00
|
|
|
/* $NetBSD: mips_reloc.c,v 1.19 2002/09/12 18:28:53 mycroft Exp $ */
|
1998-03-25 07:12:32 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1997 Michael L. Hitch <mhitch@montana.edu>
|
2002-09-12 22:28:53 +04:00
|
|
|
* Portions copyright 2002 Charles M. Hannum.
|
1998-03-25 07:12:32 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
1999-11-07 11:01:51 +03:00
|
|
|
#include <sys/stat.h>
|
1998-03-25 07:12:32 +03:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "rtld.h"
|
|
|
|
|
2002-09-12 21:08:32 +04:00
|
|
|
void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
|
2002-09-06 07:05:35 +04:00
|
|
|
caddr_t _rtld_bind_mips(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
|
1998-03-25 07:12:32 +03:00
|
|
|
|
2002-09-05 19:38:22 +04:00
|
|
|
caddr_t
|
|
|
|
_rtld_bind_mips(a0, a1, a2, a3)
|
|
|
|
Elf_Word a0;
|
|
|
|
Elf_Addr a1, a2, a3;
|
|
|
|
{
|
|
|
|
Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
|
2002-09-06 16:00:40 +04:00
|
|
|
const Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
|
2002-09-05 19:38:22 +04:00
|
|
|
const Elf_Sym *def;
|
|
|
|
const Obj_Entry *defobj;
|
|
|
|
|
2002-09-06 01:21:06 +04:00
|
|
|
def = _rtld_find_symdef(a0, obj, &defobj, true);
|
2002-09-05 19:38:22 +04:00
|
|
|
if (def) {
|
|
|
|
u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
|
|
|
|
(def->st_value + defobj->relocbase);
|
|
|
|
return((caddr_t)(def->st_value + defobj->relocbase));
|
|
|
|
}
|
|
|
|
|
|
|
|
return(NULL); /* XXX */
|
|
|
|
}
|
|
|
|
|
1998-03-25 07:12:32 +03:00
|
|
|
void
|
2002-09-05 19:38:22 +04:00
|
|
|
_rtld_setup_pltgot(obj)
|
2002-09-06 07:05:35 +04:00
|
|
|
const Obj_Entry *obj;
|
1998-03-25 07:12:32 +03:00
|
|
|
{
|
2002-09-05 19:38:22 +04:00
|
|
|
obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
|
|
|
|
/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
|
|
|
|
obj->pltgot[1] |= (Elf_Addr) obj;
|
1998-03-25 07:12:32 +03:00
|
|
|
}
|
2002-09-05 21:58:02 +04:00
|
|
|
|
2002-09-12 21:08:32 +04:00
|
|
|
void
|
|
|
|
_rtld_relocate_nonplt_self(dynp, relocbase)
|
|
|
|
Elf_Dyn *dynp;
|
|
|
|
Elf_Addr relocbase;
|
|
|
|
{
|
|
|
|
const Elf_Rel *rel = 0, *rellim;
|
|
|
|
Elf_Addr relsz = 0;
|
|
|
|
Elf_Addr *where;
|
2002-09-12 22:21:18 +04:00
|
|
|
const Elf_Sym *symtab, *sym;
|
|
|
|
Elf_Addr *got;
|
|
|
|
Elf_Word local_gotno, symtabno, gotsym;
|
|
|
|
int i;
|
2002-09-12 21:08:32 +04:00
|
|
|
|
|
|
|
for (; dynp->d_tag != DT_NULL; dynp++) {
|
|
|
|
switch (dynp->d_tag) {
|
|
|
|
case DT_REL:
|
|
|
|
rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_RELSZ:
|
|
|
|
relsz = dynp->d_un.d_val;
|
|
|
|
break;
|
|
|
|
case DT_SYMTAB:
|
|
|
|
symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
2002-09-12 22:21:18 +04:00
|
|
|
case DT_PLTGOT:
|
|
|
|
got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_MIPS_LOCAL_GOTNO:
|
|
|
|
local_gotno = dynp->d_un.d_val;
|
|
|
|
break;
|
|
|
|
case DT_MIPS_SYMTABNO:
|
|
|
|
symtabno = dynp->d_un.d_val;
|
|
|
|
break;
|
|
|
|
case DT_MIPS_GOTSYM:
|
|
|
|
gotsym = dynp->d_un.d_val;
|
|
|
|
break;
|
2002-09-12 21:08:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
|
|
|
|
for (; rel < rellim; rel++) {
|
|
|
|
where = (Elf_Addr *)(relocbase + rel->r_offset);
|
|
|
|
|
|
|
|
switch (ELF_R_TYPE(rel->r_info)) {
|
|
|
|
case R_TYPE(NONE):
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R_TYPE(REL32):
|
2002-09-12 22:21:18 +04:00
|
|
|
sym = symtab + ELF_R_SYM(rel->r_info);
|
|
|
|
if (ELF_ST_BIND(sym->st_info) == STB_LOCAL &&
|
|
|
|
ELF_ST_TYPE(sym->st_info) == STT_SECTION)
|
|
|
|
*where += (Elf_Addr)(sym->st_value + relocbase);
|
|
|
|
else
|
2002-09-12 21:08:32 +04:00
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2002-09-12 22:21:18 +04:00
|
|
|
|
|
|
|
i = (got[1] & 0x80000000) ? 2 : 1;
|
|
|
|
/* Relocate the local GOT entries */
|
|
|
|
while (i < local_gotno)
|
|
|
|
got[i++] += relocbase;
|
|
|
|
got += local_gotno;
|
|
|
|
sym = symtab + gotsym;
|
|
|
|
/* Now do the global GOT entries */
|
|
|
|
for (i = gotsym; i < symtabno; i++) {
|
|
|
|
if (sym->st_shndx == SHN_UNDEF ||
|
2002-09-12 22:28:53 +04:00
|
|
|
sym->st_shndx == SHN_COMMON)
|
2002-09-12 22:21:18 +04:00
|
|
|
*got = sym->st_value + relocbase;
|
2002-09-12 22:28:53 +04:00
|
|
|
else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
|
|
|
|
*got != sym->st_value)
|
2002-09-12 22:21:18 +04:00
|
|
|
*got += relocbase;
|
2002-09-12 22:28:53 +04:00
|
|
|
else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
|
2002-09-12 22:21:18 +04:00
|
|
|
ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
|
|
|
|
if (sym->st_shndx == SHN_ABS)
|
|
|
|
*got = sym->st_value + relocbase;
|
|
|
|
/* else SGI stuff ignored */
|
|
|
|
} else
|
|
|
|
*got = sym->st_value + relocbase;
|
|
|
|
++sym;
|
|
|
|
++got;
|
|
|
|
}
|
2002-09-12 21:08:32 +04:00
|
|
|
}
|
|
|
|
|
2002-09-05 21:58:02 +04:00
|
|
|
int
|
2002-09-06 19:17:53 +04:00
|
|
|
_rtld_relocate_nonplt_objects(obj, self, dodebug)
|
2002-09-06 07:12:04 +04:00
|
|
|
const Obj_Entry *obj;
|
2002-09-06 19:17:53 +04:00
|
|
|
bool self;
|
2002-09-05 21:58:02 +04:00
|
|
|
bool dodebug;
|
|
|
|
{
|
2002-09-06 00:08:14 +04:00
|
|
|
const Elf_Rel *rel;
|
2002-09-12 22:21:18 +04:00
|
|
|
Elf_Addr *got = obj->pltgot;
|
2002-09-12 22:28:53 +04:00
|
|
|
const Elf_Sym *sym, *def;
|
2002-09-12 22:21:18 +04:00
|
|
|
const Obj_Entry *defobj;
|
|
|
|
int i;
|
2002-09-06 00:08:14 +04:00
|
|
|
|
2002-09-12 21:08:32 +04:00
|
|
|
if (self)
|
|
|
|
return 0;
|
|
|
|
|
2002-09-06 00:08:14 +04:00
|
|
|
for (rel = obj->rel; rel < obj->rellim; rel++) {
|
|
|
|
Elf_Addr *where;
|
2002-09-06 01:21:06 +04:00
|
|
|
unsigned long symnum;
|
2002-09-06 00:08:14 +04:00
|
|
|
|
|
|
|
where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
|
2002-09-06 01:21:06 +04:00
|
|
|
symnum = ELF_R_SYM(rel->r_info);
|
2002-09-06 00:08:14 +04:00
|
|
|
|
|
|
|
switch (ELF_R_TYPE(rel->r_info)) {
|
|
|
|
case R_TYPE(NONE):
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R_TYPE(REL32):
|
|
|
|
/* 32-bit PC-relative reference */
|
2002-09-06 01:21:06 +04:00
|
|
|
def = obj->symtab + symnum;
|
2002-09-06 00:08:14 +04:00
|
|
|
|
|
|
|
if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
|
|
|
|
(ELF_ST_TYPE(def->st_info) == STT_SECTION ||
|
|
|
|
ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
|
|
|
|
/*
|
|
|
|
* XXX: ABI DIFFERENCE!
|
|
|
|
*
|
|
|
|
* Old NetBSD binutils would generate shared
|
|
|
|
* libs with section-relative relocations being
|
|
|
|
* already adjusted for the start address of
|
|
|
|
* the section.
|
|
|
|
*
|
|
|
|
* New binutils, OTOH, generate shared libs
|
|
|
|
* with the same relocations being based at
|
|
|
|
* zero, so we need to add in the start address
|
|
|
|
* of the section.
|
|
|
|
*
|
|
|
|
* We assume that all section-relative relocs
|
|
|
|
* with contents less than the start of the
|
|
|
|
* section need to be adjusted; this should
|
|
|
|
* work with both old and new shlibs.
|
|
|
|
*
|
|
|
|
* --rkb, Oct 6, 2001
|
|
|
|
*/
|
|
|
|
if (def->st_info == STT_SECTION &&
|
2002-09-12 21:08:32 +04:00
|
|
|
*where < def->st_value)
|
|
|
|
*where += (Elf_Addr)def->st_value;
|
2002-09-06 00:08:14 +04:00
|
|
|
|
|
|
|
*where += (Elf_Addr)obj->relocbase;
|
|
|
|
|
|
|
|
rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
|
|
|
|
obj->strtab + def->st_name, obj->path,
|
|
|
|
(void *)*where, obj->path));
|
|
|
|
} else {
|
|
|
|
/* XXX maybe do something re: bootstrapping? */
|
2002-09-06 01:21:06 +04:00
|
|
|
def = _rtld_find_symdef(symnum, obj, &defobj,
|
|
|
|
false);
|
2002-09-06 00:08:14 +04:00
|
|
|
if (def == NULL)
|
|
|
|
return -1;
|
|
|
|
*where += (Elf_Addr)(defobj->relocbase +
|
|
|
|
def->st_value);
|
|
|
|
rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
|
2002-09-06 01:31:30 +04:00
|
|
|
obj->strtab + obj->symtab[symnum].st_name,
|
|
|
|
obj->path, (void *)*where, defobj->path));
|
2002-09-06 00:08:14 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
|
|
|
|
"contents = %p, symbol = %s",
|
2002-09-06 01:21:06 +04:00
|
|
|
symnum, (u_long)ELF_R_TYPE(rel->r_info),
|
2002-09-06 00:08:14 +04:00
|
|
|
(void *)rel->r_offset, (void *)*where,
|
2002-09-06 01:21:06 +04:00
|
|
|
obj->strtab + obj->symtab[symnum].st_name));
|
2002-09-06 00:08:14 +04:00
|
|
|
_rtld_error("%s: Unsupported relocation type %ld "
|
|
|
|
"in non-PLT relocations\n",
|
|
|
|
obj->path, (u_long) ELF_R_TYPE(rel->r_info));
|
|
|
|
return -1;
|
2002-09-05 21:58:02 +04:00
|
|
|
}
|
|
|
|
}
|
2002-09-12 22:21:18 +04:00
|
|
|
|
|
|
|
i = (got[1] & 0x80000000) ? 2 : 1;
|
|
|
|
/* Relocate the local GOT entries */
|
|
|
|
while (i < obj->local_gotno)
|
|
|
|
got[i++] += (Elf_Word)obj->relocbase;
|
|
|
|
got += obj->local_gotno;
|
2002-09-12 22:28:53 +04:00
|
|
|
sym = obj->symtab + obj->gotsym;
|
2002-09-12 22:21:18 +04:00
|
|
|
/* Now do the global GOT entries */
|
|
|
|
for (i = obj->gotsym; i < obj->symtabno; i++) {
|
|
|
|
rdbg(1, (" doing got %d sym %p (%s, %x)", i - obj->gotsym,
|
|
|
|
sym, sym->st_name + obj->strtab, *got));
|
|
|
|
|
|
|
|
def = _rtld_find_symdef(i, obj, &defobj, true);
|
|
|
|
if (def == NULL)
|
2002-09-12 22:28:53 +04:00
|
|
|
return -1;
|
|
|
|
if (sym->st_shndx == SHN_UNDEF ||
|
|
|
|
sym->st_shndx == SHN_COMMON)
|
|
|
|
*got = def->st_value + (Elf_Word)defobj->relocbase;
|
|
|
|
else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
|
|
|
|
*got != sym->st_value)
|
|
|
|
*got += (Elf_Word)obj->relocbase;
|
|
|
|
else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
|
|
|
|
ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
|
|
|
|
if (sym->st_shndx == SHN_ABS)
|
|
|
|
*got = sym->st_value +
|
|
|
|
(Elf_Word)obj->relocbase;
|
|
|
|
/* else SGI stuff ignored */
|
|
|
|
} else
|
|
|
|
*got = def->st_value + (Elf_Word)defobj->relocbase;
|
2002-09-12 22:21:18 +04:00
|
|
|
++sym;
|
|
|
|
++got;
|
|
|
|
}
|
|
|
|
|
2002-09-05 21:58:02 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-09-06 07:12:04 +04:00
|
|
|
_rtld_relocate_plt_lazy(obj, dodebug)
|
|
|
|
const Obj_Entry *obj;
|
2002-09-05 21:58:02 +04:00
|
|
|
bool dodebug;
|
|
|
|
{
|
2002-09-06 07:12:04 +04:00
|
|
|
const Elf_Rel *rel;
|
2002-09-05 21:58:02 +04:00
|
|
|
|
2002-09-06 17:20:29 +04:00
|
|
|
if (!obj->isdynamic)
|
2002-09-06 07:12:04 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
|
|
|
|
Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
|
2002-09-05 21:58:02 +04:00
|
|
|
|
|
|
|
/* Just relocate the GOT slots pointing into the PLT */
|
2002-09-06 07:12:04 +04:00
|
|
|
*where += (Elf_Addr)obj->relocbase;
|
2002-09-05 21:58:02 +04:00
|
|
|
rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
|
|
|
|
(void *)*where));
|
|
|
|
}
|
2002-09-06 07:05:35 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-09-06 07:12:04 +04:00
|
|
|
_rtld_relocate_plt_object(obj, rela, addrp, dodebug)
|
|
|
|
const Obj_Entry *obj;
|
|
|
|
const Elf_Rela *rela;
|
|
|
|
caddr_t *addrp;
|
2002-09-06 07:05:35 +04:00
|
|
|
bool dodebug;
|
|
|
|
{
|
2002-09-06 07:12:04 +04:00
|
|
|
Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
|
|
|
|
Elf_Addr new_value;
|
2002-09-06 07:05:35 +04:00
|
|
|
|
2002-09-06 07:12:04 +04:00
|
|
|
/* Fully resolve procedure addresses now */
|
2002-09-06 07:05:35 +04:00
|
|
|
|
2002-09-06 17:20:29 +04:00
|
|
|
if (obj->isdynamic) {
|
2002-09-06 07:05:35 +04:00
|
|
|
/* Just relocate the GOT slots pointing into the PLT */
|
2002-09-06 07:12:04 +04:00
|
|
|
new_value = *where + (Elf_Addr)(obj->relocbase);
|
2002-09-06 07:05:35 +04:00
|
|
|
rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
|
|
|
|
(void *)*where));
|
2002-09-06 07:12:04 +04:00
|
|
|
} else {
|
|
|
|
return 0;
|
2002-09-05 21:58:02 +04:00
|
|
|
}
|
2002-09-06 07:12:04 +04:00
|
|
|
if (*where != new_value)
|
|
|
|
*where = new_value;
|
2002-09-06 07:05:35 +04:00
|
|
|
|
2002-09-06 07:12:04 +04:00
|
|
|
*addrp = (caddr_t)new_value;
|
2002-09-05 21:58:02 +04:00
|
|
|
return 0;
|
|
|
|
}
|