Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.47
      1  1.47       he /*	$NetBSD: mips_reloc.c,v 1.47 2005/06/07 09:20:19 he Exp $	*/
      2   1.1   mhitch 
      3   1.1   mhitch /*
      4   1.1   mhitch  * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
      5  1.37  mycroft  * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
      6   1.1   mhitch  * All rights reserved.
      7   1.1   mhitch  *
      8   1.1   mhitch  * Redistribution and use in source and binary forms, with or without
      9   1.1   mhitch  * modification, are permitted provided that the following conditions
     10   1.1   mhitch  * are met:
     11   1.1   mhitch  * 1. Redistributions of source code must retain the above copyright
     12   1.1   mhitch  *    notice, this list of conditions and the following disclaimer.
     13   1.1   mhitch  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   mhitch  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   mhitch  *    documentation and/or other materials provided with the distribution.
     16   1.1   mhitch  * 3. The name of the author may not be used to endorse or promote products
     17   1.1   mhitch  *    derived from this software without specific prior written permission.
     18   1.1   mhitch  *
     19   1.1   mhitch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20   1.1   mhitch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21   1.1   mhitch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22   1.1   mhitch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23   1.1   mhitch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24   1.1   mhitch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25   1.1   mhitch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26   1.1   mhitch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27   1.1   mhitch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28   1.1   mhitch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29   1.1   mhitch  */
     30   1.1   mhitch 
     31   1.1   mhitch #include <sys/types.h>
     32   1.3  mycroft #include <sys/stat.h>
     33  1.40      mrg 
     34  1.40      mrg #include <stdlib.h>
     35  1.33  thorpej #include <string.h>
     36   1.1   mhitch 
     37   1.1   mhitch #include "debug.h"
     38   1.1   mhitch #include "rtld.h"
     39   1.1   mhitch 
     40  1.36  mycroft #define SUPPORT_OLD_BROKEN_LD
     41  1.36  mycroft 
     42  1.22  mycroft void _rtld_bind_start(void);
     43  1.17  mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     44  1.36  mycroft caddr_t _rtld_bind(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
     45   1.6  mycroft 
     46  1.45   simonb /*
     47  1.45   simonb  * It is possible for the compiler to emit relocations for unaligned data.
     48  1.45   simonb  * We handle this situation with these inlines.
     49  1.45   simonb  */
     50  1.45   simonb #define	RELOC_ALIGNED_P(x) \
     51  1.45   simonb 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
     52  1.45   simonb 
     53  1.45   simonb static __inline Elf_Addr
     54  1.45   simonb load_ptr(void *where)
     55  1.45   simonb {
     56  1.45   simonb 	Elf_Addr res;
     57  1.45   simonb 
     58  1.45   simonb 	memcpy(&res, where, sizeof(res));
     59  1.45   simonb 
     60  1.45   simonb 	return res;
     61  1.45   simonb }
     62  1.45   simonb 
     63  1.45   simonb static __inline void
     64  1.45   simonb store_ptr(void *where, Elf_Addr val)
     65  1.45   simonb {
     66  1.45   simonb 
     67  1.45   simonb 	memcpy(where, &val, sizeof(val));
     68  1.45   simonb }
     69  1.45   simonb 
     70  1.45   simonb 
     71   1.1   mhitch void
     72  1.39    skrll _rtld_setup_pltgot(const Obj_Entry *obj)
     73   1.1   mhitch {
     74   1.6  mycroft 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
     75   1.6  mycroft 	/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
     76   1.6  mycroft 	obj->pltgot[1] |= (Elf_Addr) obj;
     77   1.8  mycroft }
     78   1.8  mycroft 
     79  1.17  mycroft void
     80  1.39    skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     81  1.17  mycroft {
     82  1.17  mycroft 	const Elf_Rel *rel = 0, *rellim;
     83  1.17  mycroft 	Elf_Addr relsz = 0;
     84  1.17  mycroft 	Elf_Addr *where;
     85  1.47       he 	const Elf_Sym *symtab = NULL, *sym;
     86  1.47       he 	Elf_Addr *got = NULL;
     87  1.47       he 	Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
     88  1.18  mycroft 	int i;
     89  1.17  mycroft 
     90  1.17  mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
     91  1.17  mycroft 		switch (dynp->d_tag) {
     92  1.17  mycroft 		case DT_REL:
     93  1.17  mycroft 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     94  1.17  mycroft 			break;
     95  1.17  mycroft 		case DT_RELSZ:
     96  1.17  mycroft 			relsz = dynp->d_un.d_val;
     97  1.17  mycroft 			break;
     98  1.17  mycroft 		case DT_SYMTAB:
     99  1.17  mycroft 			symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
    100  1.17  mycroft 			break;
    101  1.18  mycroft 		case DT_PLTGOT:
    102  1.18  mycroft 			got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
    103  1.18  mycroft 			break;
    104  1.18  mycroft 		case DT_MIPS_LOCAL_GOTNO:
    105  1.18  mycroft 			local_gotno = dynp->d_un.d_val;
    106  1.18  mycroft 			break;
    107  1.18  mycroft 		case DT_MIPS_SYMTABNO:
    108  1.18  mycroft 			symtabno = dynp->d_un.d_val;
    109  1.18  mycroft 			break;
    110  1.18  mycroft 		case DT_MIPS_GOTSYM:
    111  1.18  mycroft 			gotsym = dynp->d_un.d_val;
    112  1.18  mycroft 			break;
    113  1.17  mycroft 		}
    114  1.17  mycroft 	}
    115  1.34  mycroft 
    116  1.34  mycroft 	i = (got[1] & 0x80000000) ? 2 : 1;
    117  1.34  mycroft 	/* Relocate the local GOT entries */
    118  1.34  mycroft 	got += i;
    119  1.34  mycroft 	for (; i < local_gotno; i++)
    120  1.34  mycroft 		*got++ += relocbase;
    121  1.34  mycroft 	sym = symtab + gotsym;
    122  1.34  mycroft 	/* Now do the global GOT entries */
    123  1.34  mycroft 	for (i = gotsym; i < symtabno; i++) {
    124  1.34  mycroft 		*got = sym->st_value + relocbase;
    125  1.34  mycroft 		++sym;
    126  1.34  mycroft 		++got;
    127  1.34  mycroft 	}
    128  1.34  mycroft 
    129  1.17  mycroft 	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
    130  1.17  mycroft 	for (; rel < rellim; rel++) {
    131  1.17  mycroft 		where = (Elf_Addr *)(relocbase + rel->r_offset);
    132  1.17  mycroft 
    133  1.17  mycroft 		switch (ELF_R_TYPE(rel->r_info)) {
    134  1.17  mycroft 		case R_TYPE(NONE):
    135  1.17  mycroft 			break;
    136  1.17  mycroft 
    137  1.17  mycroft 		case R_TYPE(REL32):
    138  1.34  mycroft 			assert(ELF_R_SYM(rel->r_info) < gotsym);
    139  1.18  mycroft 			sym = symtab + ELF_R_SYM(rel->r_info);
    140  1.46    skrll 			assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
    141  1.45   simonb 			if (__predict_true(RELOC_ALIGNED_P(where)))
    142  1.46    skrll 				*where += relocbase;
    143  1.45   simonb 			else
    144  1.46    skrll 				store_ptr(where, load_ptr(where) + relocbase);
    145  1.17  mycroft 			break;
    146  1.17  mycroft 
    147  1.17  mycroft 		default:
    148  1.17  mycroft 			abort();
    149  1.17  mycroft 		}
    150  1.17  mycroft 	}
    151  1.17  mycroft }
    152  1.17  mycroft 
    153   1.8  mycroft int
    154  1.39    skrll _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    155   1.8  mycroft {
    156   1.9  mycroft 	const Elf_Rel *rel;
    157  1.18  mycroft 	Elf_Addr *got = obj->pltgot;
    158  1.19  mycroft 	const Elf_Sym *sym, *def;
    159  1.18  mycroft 	const Obj_Entry *defobj;
    160  1.18  mycroft 	int i;
    161  1.36  mycroft #ifdef SUPPORT_OLD_BROKEN_LD
    162  1.36  mycroft 	int broken;
    163  1.36  mycroft #endif
    164  1.36  mycroft 
    165  1.36  mycroft #ifdef SUPPORT_OLD_BROKEN_LD
    166  1.36  mycroft 	broken = 0;
    167  1.36  mycroft 	sym = obj->symtab;
    168  1.36  mycroft 	for (i = 1; i < 12; i++)
    169  1.36  mycroft 		if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
    170  1.36  mycroft 			broken = 1;
    171  1.36  mycroft 	dbg(("%s: broken=%d", obj->path, broken));
    172  1.36  mycroft #endif
    173  1.17  mycroft 
    174  1.34  mycroft 	i = (got[1] & 0x80000000) ? 2 : 1;
    175  1.34  mycroft 	/* Relocate the local GOT entries */
    176  1.34  mycroft 	got += i;
    177  1.34  mycroft 	for (; i < obj->local_gotno; i++)
    178  1.34  mycroft 		*got++ += (Elf_Addr)obj->relocbase;
    179  1.34  mycroft 	sym = obj->symtab + obj->gotsym;
    180  1.34  mycroft 	/* Now do the global GOT entries */
    181  1.34  mycroft 	for (i = obj->gotsym; i < obj->symtabno; i++) {
    182  1.34  mycroft 		rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
    183  1.34  mycroft 		    sym->st_name + obj->strtab, *got));
    184  1.34  mycroft 
    185  1.36  mycroft #ifdef SUPPORT_OLD_BROKEN_LD
    186  1.34  mycroft 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    187  1.36  mycroft 		    broken && sym->st_shndx == SHN_UNDEF) {
    188  1.34  mycroft 			/*
    189  1.34  mycroft 			 * XXX DANGER WILL ROBINSON!
    190  1.34  mycroft 			 * You might think this is stupid, as it intentionally
    191  1.34  mycroft 			 * defeats lazy binding -- and you'd be right.
    192  1.34  mycroft 			 * Unfortunately, for lazy binding to work right, we
    193  1.34  mycroft 			 * need to a way to force the GOT slots used for
    194  1.34  mycroft 			 * function pointers to be resolved immediately.  This
    195  1.34  mycroft 			 * is supposed to be done automatically by the linker,
    196  1.34  mycroft 			 * by not outputting a PLT slot and setting st_value
    197  1.36  mycroft 			 * to 0 if there are non-PLT references, but older
    198  1.36  mycroft 			 * versions of GNU ld do not do this.
    199  1.34  mycroft 			 */
    200  1.38    skrll 			def = _rtld_find_symdef(i, obj, &defobj, false);
    201  1.34  mycroft 			if (def == NULL)
    202  1.34  mycroft 				return -1;
    203  1.34  mycroft 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    204  1.36  mycroft 		} else
    205  1.36  mycroft #endif
    206  1.36  mycroft 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    207  1.41  mycroft 		    sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
    208  1.34  mycroft 			/*
    209  1.34  mycroft 			 * If there are non-PLT references to the function,
    210  1.34  mycroft 			 * st_value should be 0, forcing us to resolve the
    211  1.34  mycroft 			 * address immediately.
    212  1.41  mycroft 			 *
    213  1.41  mycroft 			 * XXX DANGER WILL ROBINSON!
    214  1.41  mycroft 			 * The linker is not outputting PLT slots for calls to
    215  1.41  mycroft 			 * functions that are defined in the same shared
    216  1.42  mycroft 			 * library.  This is a bug, because it can screw up
    217  1.42  mycroft 			 * link ordering rules if the symbol is defined in
    218  1.42  mycroft 			 * more than one module.  For now, if there is a
    219  1.42  mycroft 			 * definition, we fail the test above and force a full
    220  1.44  mycroft 			 * symbol lookup.  This means that all intra-module
    221  1.44  mycroft 			 * calls are bound immediately.  - mycroft, 2003/09/24
    222  1.34  mycroft 			 */
    223  1.34  mycroft 			*got = sym->st_value + (Elf_Addr)obj->relocbase;
    224  1.34  mycroft 		} else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
    225  1.34  mycroft 			/* Symbols with index SHN_ABS are not relocated. */
    226  1.34  mycroft 			if (sym->st_shndx != SHN_ABS)
    227  1.34  mycroft 				*got = sym->st_value +
    228  1.34  mycroft 				    (Elf_Addr)obj->relocbase;
    229  1.34  mycroft 		} else {
    230  1.38    skrll 			def = _rtld_find_symdef(i, obj, &defobj, false);
    231  1.34  mycroft 			if (def == NULL)
    232  1.34  mycroft 				return -1;
    233  1.34  mycroft 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    234  1.34  mycroft 		}
    235  1.34  mycroft 
    236  1.34  mycroft 		rdbg(("  --> now %x", *got));
    237  1.34  mycroft 		++sym;
    238  1.34  mycroft 		++got;
    239  1.34  mycroft 	}
    240  1.34  mycroft 
    241  1.34  mycroft 	got = obj->pltgot;
    242   1.9  mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
    243  1.33  thorpej 		Elf_Addr        *where, tmp;
    244  1.10  mycroft 		unsigned long	 symnum;
    245   1.9  mycroft 
    246   1.9  mycroft 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    247  1.10  mycroft 		symnum = ELF_R_SYM(rel->r_info);
    248   1.9  mycroft 
    249   1.9  mycroft 		switch (ELF_R_TYPE(rel->r_info)) {
    250   1.9  mycroft 		case R_TYPE(NONE):
    251   1.9  mycroft 			break;
    252   1.9  mycroft 
    253   1.9  mycroft 		case R_TYPE(REL32):
    254   1.9  mycroft 			/* 32-bit PC-relative reference */
    255  1.10  mycroft 			def = obj->symtab + symnum;
    256   1.9  mycroft 
    257  1.34  mycroft 			if (symnum >= obj->gotsym) {
    258  1.45   simonb 				if (__predict_true(RELOC_ALIGNED_P(where)))
    259  1.45   simonb 					tmp = *where;
    260  1.45   simonb 				else
    261  1.45   simonb 					tmp = load_ptr(where);
    262  1.34  mycroft 				tmp += got[obj->local_gotno + symnum - obj->gotsym];
    263  1.45   simonb 				if (__predict_true(RELOC_ALIGNED_P(where)))
    264  1.45   simonb 					*where = tmp;
    265  1.45   simonb 				else
    266  1.45   simonb 					store_ptr(where, tmp);
    267  1.34  mycroft 
    268  1.34  mycroft 				rdbg(("REL32/G %s in %s --> %p in %s",
    269  1.34  mycroft 				    obj->strtab + def->st_name, obj->path,
    270  1.34  mycroft 				    (void *)tmp, obj->path));
    271  1.34  mycroft 				break;
    272  1.34  mycroft 			} else {
    273   1.9  mycroft 				/*
    274   1.9  mycroft 				 * XXX: ABI DIFFERENCE!
    275   1.9  mycroft 				 *
    276   1.9  mycroft 				 * Old NetBSD binutils would generate shared
    277   1.9  mycroft 				 * libs with section-relative relocations being
    278   1.9  mycroft 				 * already adjusted for the start address of
    279   1.9  mycroft 				 * the section.
    280   1.9  mycroft 				 *
    281   1.9  mycroft 				 * New binutils, OTOH, generate shared libs
    282   1.9  mycroft 				 * with the same relocations being based at
    283   1.9  mycroft 				 * zero, so we need to add in the start address
    284   1.9  mycroft 				 * of the section.
    285   1.9  mycroft 				 *
    286   1.9  mycroft 				 * --rkb, Oct 6, 2001
    287   1.9  mycroft 				 */
    288  1.45   simonb 				if (__predict_true(RELOC_ALIGNED_P(where)))
    289  1.45   simonb 					tmp = *where;
    290  1.45   simonb 				else
    291  1.45   simonb 					tmp = load_ptr(where);
    292  1.34  mycroft 
    293  1.35  mycroft 				if (def->st_info ==
    294  1.36  mycroft 				    ELF_ST_INFO(STB_LOCAL, STT_SECTION)
    295  1.36  mycroft #ifdef SUPPORT_OLD_BROKEN_LD
    296  1.36  mycroft 				    && !broken
    297  1.36  mycroft #endif
    298  1.36  mycroft 				    )
    299  1.34  mycroft 					tmp += (Elf_Addr)def->st_value;
    300   1.9  mycroft 
    301  1.34  mycroft 				tmp += (Elf_Addr)obj->relocbase;
    302  1.45   simonb 				if (__predict_true(RELOC_ALIGNED_P(where)))
    303  1.45   simonb 					*where = tmp;
    304  1.45   simonb 				else
    305  1.45   simonb 					store_ptr(where, tmp);
    306   1.9  mycroft 
    307  1.35  mycroft 				rdbg(("REL32/L %s in %s --> %p in %s",
    308   1.9  mycroft 				    obj->strtab + def->st_name, obj->path,
    309  1.33  thorpej 				    (void *)tmp, obj->path));
    310   1.9  mycroft 			}
    311   1.9  mycroft 			break;
    312   1.9  mycroft 
    313   1.9  mycroft 		default:
    314  1.23  mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    315   1.9  mycroft 			    "contents = %p, symbol = %s",
    316  1.10  mycroft 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    317  1.33  thorpej 			    (void *)rel->r_offset, (void *)load_ptr(where),
    318  1.10  mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    319   1.9  mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    320   1.9  mycroft 			    "in non-PLT relocations\n",
    321   1.9  mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    322   1.9  mycroft 			return -1;
    323   1.8  mycroft 		}
    324  1.18  mycroft 	}
    325  1.18  mycroft 
    326   1.8  mycroft 	return 0;
    327   1.8  mycroft }
    328   1.8  mycroft 
    329   1.8  mycroft int
    330  1.39    skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    331  1.13  mycroft {
    332  1.31  mycroft 	/* PLT fixups were done above in the GOT relocation. */
    333  1.28  mycroft 	return 0;
    334  1.36  mycroft }
    335  1.36  mycroft 
    336  1.36  mycroft caddr_t
    337  1.39    skrll _rtld_bind(Elf_Word a0, Elf_Addr a1, Elf_Addr a2, Elf_Addr a3)
    338  1.36  mycroft {
    339  1.36  mycroft 	Elf_Addr *got = (Elf_Addr *)(a2 - 0x7ff0);
    340  1.36  mycroft 	const Obj_Entry *obj = (Obj_Entry *)(got[1] & 0x7fffffff);
    341  1.36  mycroft 	const Elf_Sym *def;
    342  1.36  mycroft 	const Obj_Entry *defobj;
    343  1.36  mycroft 	Elf_Addr new_value;
    344  1.36  mycroft 
    345  1.36  mycroft 	def = _rtld_find_symdef(a0, obj, &defobj, true);
    346  1.36  mycroft 	if (def == NULL)
    347  1.36  mycroft 		_rtld_die();
    348  1.36  mycroft 
    349  1.36  mycroft 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    350  1.36  mycroft 	rdbg(("bind now/fixup in %s --> new=%p",
    351  1.36  mycroft 	    defobj->strtab + def->st_name, (void *)new_value));
    352  1.36  mycroft 	got[obj->local_gotno + a0 - obj->gotsym] = new_value;
    353  1.36  mycroft 	return ((caddr_t)new_value);
    354   1.1   mhitch }
    355