2 * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * $FreeBSD: src/libexec/rtld-elf/alpha/reloc.c,v 1.10.2.5 2002/09/02 02:10:20 obrien Exp $
29 * Dynamic linker for ELF.
31 * John Polstra <jdp@polstra.com>.
34 #include <sys/param.h>
50 extern Elf_Dyn _GOT_END_;
53 * Macros for loading/storing unaligned 64-bit values. These are
54 * needed because relocations can point to unaligned data. This
55 * occurs in the DWARF2 exception frame tables generated by the
56 * compiler, for instance.
58 * We don't use these when relocating jump slots and GOT entries,
59 * since they are guaranteed to be aligned.
61 #define load64(p) ({ \
63 __asm__("ldq_u %0,%1" : "=r"(__res) : "m"(*(p))); \
66 #define store64(p, v) \
67 __asm__("stq_u %1,%0" : "=m"(*(p)) : "r"(v))
69 /* Relocate a non-PLT object with addend. */
71 reloc_non_plt_obj(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
74 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
76 switch (ELF_R_TYPE(rela->r_info)) {
81 case R_ALPHA_REFQUAD: {
83 const Obj_Entry *defobj;
85 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
86 &defobj, false, cache);
90 (Elf_Addr) (defobj->relocbase + def->st_value) +
91 load64(where) + rela->r_addend);
95 case R_ALPHA_GLOB_DAT: {
97 const Obj_Entry *defobj;
100 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
101 &defobj, false, cache);
104 val = (Elf_Addr) (defobj->relocbase + def->st_value +
106 if (load64(where) != val)
111 case R_ALPHA_RELATIVE: {
112 if (obj != obj_rtld ||
113 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
114 (caddr_t)where >= (caddr_t)&_GOT_END_)
116 load64(where) + (Elf_Addr) obj->relocbase);
122 * These are deferred until all other relocations
123 * have been done. All we do here is make sure
124 * that the COPY relocation is not in a shared
125 * library. They are allowed only in executable
128 if (!obj->mainprog) {
129 _rtld_error("%s: Unexpected R_COPY "
130 " relocation in shared library",
138 _rtld_error("%s: Unsupported relocation type %d"
139 " in non-PLT relocations\n", obj->path,
140 ELF_R_TYPE(rela->r_info));
146 /* Process the non-PLT relocations. */
148 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
150 const Elf_Rel *rellim;
152 const Elf_Rela *relalim;
153 const Elf_Rela *rela;
155 int bytes = obj->nchains * sizeof(SymCache);
159 * The dynamic loader may be called from a thread, we have
160 * limited amounts of stack available so we cannot use alloca().
162 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
163 if (cache == MAP_FAILED)
166 memset(cache, 0, bytes);
168 /* Perform relocations without addend if there are any: */
169 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
170 for (rel = obj->rel; obj->rel != NULL && rel < rellim; rel++) {
173 locrela.r_info = rel->r_info;
174 locrela.r_offset = rel->r_offset;
175 locrela.r_addend = 0;
176 if (reloc_non_plt_obj(obj_rtld, obj, &locrela, cache))
180 /* Perform relocations with addend if there are any: */
181 relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
182 for (rela = obj->rela; obj->rela != NULL && rela < relalim; rela++) {
183 if (reloc_non_plt_obj(obj_rtld, obj, rela, cache))
189 munmap(cache, bytes);
193 /* Process the PLT relocations. */
195 reloc_plt(Obj_Entry *obj)
197 /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
198 if (obj->pltrelsize != 0) {
199 const Elf_Rel *rellim;
202 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
203 for (rel = obj->pltrel; rel < rellim; rel++) {
206 assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
208 /* Relocate the GOT slot pointing into the PLT. */
209 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
210 *where += (Elf_Addr)obj->relocbase;
213 const Elf_Rela *relalim;
214 const Elf_Rela *rela;
216 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
217 for (rela = obj->pltrela; rela < relalim; rela++) {
220 assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
222 /* Relocate the GOT slot pointing into the PLT. */
223 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
224 *where += (Elf_Addr)obj->relocbase;
230 /* Relocate the jump slots in an object. */
232 reloc_jmpslots(Obj_Entry *obj)
234 if (obj->jmpslots_done)
236 /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
237 if (obj->pltrelsize != 0) {
238 const Elf_Rel *rellim;
241 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
242 for (rel = obj->pltrel; rel < rellim; rel++) {
245 const Obj_Entry *defobj;
247 assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
248 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
249 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true,
254 (Elf_Addr)(defobj->relocbase + def->st_value));
257 const Elf_Rela *relalim;
258 const Elf_Rela *rela;
260 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
261 for (rela = obj->pltrela; rela < relalim; rela++) {
264 const Obj_Entry *defobj;
266 assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
267 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
268 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true,
273 (Elf_Addr)(defobj->relocbase + def->st_value));
276 obj->jmpslots_done = true;
280 /* Fixup the jump slot at "where" to transfer control to "target". */
282 reloc_jmpslot(Elf_Addr *where, Elf_Addr target)
286 dbg(" reloc_jmpslot: where=%p, target=%p", (void *)where, (void *)target);
288 if (stubaddr != target) {
296 /* Point this GOT entry directly at the target. */
300 * There may be multiple GOT tables, each with an entry
301 * pointing to the stub in the PLT. But we can only find and
302 * fix up the first GOT entry. So we must rewrite the stub as
303 * well, to perform a call to the target if it is executed.
305 * When the stub gets control, register pv ($27) contains its
306 * address. We adjust its value so that it points to the
307 * target, and then jump indirect through it.
309 * Each PLT entry has room for 3 instructions. If the
310 * adjustment amount fits in a signed 32-bit integer, we can
311 * simply add it to register pv. Otherwise we must load the
312 * GOT entry itself into the pv register.
314 delta = target - stubaddr;
315 dbg(" stubaddr=%p, where-stubaddr=%ld, delta=%ld", (void *)stubaddr,
316 (long)where - (long)stubaddr, (long)delta);
318 if ((int32_t)delta == delta) {
320 * We can adjust pv with a LDA, LDAH sequence.
322 * First build an LDA instruction to adjust the low 16 bits.
324 inst[instct++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
326 dbg(" LDA $27,%d($27)", (int16_t)delta);
328 * Adjust the delta to account for the effects of the LDA,
329 * including sign-extension.
331 delta -= (int16_t)delta;
333 /* Build an LDAH instruction to adjust the high 16 bits. */
334 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
335 (delta >> 16 & 0xffff);
336 dbg(" LDAH $27,%d($27)", (int16_t)(delta >> 16));
341 /* We must load the GOT entry from memory. */
342 delta = (Elf_Addr)where - stubaddr;
344 * If the GOT entry is too far away from the PLT entry,
345 * then punt. This PLT entry will have to be looked up
346 * manually for all GOT entries except the first one.
347 * The program will still run, albeit very slowly. It's
348 * extremely unlikely that this case could ever arise in
349 * practice, but we might as well handle it correctly if
352 if ((int32_t)delta != delta) {
353 dbg(" PLT stub too far from GOT to relocate");
356 dhigh = delta - (int16_t)delta;
358 /* Build an LDAH instruction to adjust the high 16 bits. */
359 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
360 (dhigh >> 16 & 0xffff);
361 dbg(" LDAH $27,%d($27)", (int16_t)(dhigh >> 16));
363 /* Build an LDQ to load the GOT entry. */
364 inst[instct++] = 0x29 << 26 | 27 << 21 | 27 << 16 |
366 dbg(" LDQ $27,%d($27)", (int16_t)delta);
370 * Build a JMP or BR instruction to jump to the target. If
371 * the instruction displacement fits in a sign-extended 21-bit
372 * field, we can use the more efficient BR instruction.
373 * Otherwise we have to jump indirect through the pv register.
375 pc = stubaddr + 4 * (instct + 1);
376 idisp = (int64_t)(target - pc) >> 2;
377 if (-0x100000 <= idisp && idisp < 0x100000) {
378 inst[instct++] = 0x30 << 26 | 31 << 21 | (idisp & 0x1fffff);
379 dbg(" BR $31,%p", (void *)target);
381 inst[instct++] = 0x1a << 26 | 31 << 21 | 27 << 16 |
383 dbg(" JMP $31,($27),%d", (int)(idisp & 0x3fff));
387 * Fill in the tail of the PLT entry first for reentrancy.
388 * Until we have overwritten the first instruction (an
389 * unconditional branch), the remaining instructions have no
392 stubptr = (u_int32_t *)stubaddr;
395 stubptr[instct] = inst[instct];
398 * Commit the tail of the instruction sequence to memory
399 * before overwriting the first instruction.
401 __asm__ __volatile__("wmb" : : : "memory");
402 stubptr[0] = inst[0];
406 /* Process an R_ALPHA_COPY relocation. */
408 do_copy_relocation(Obj_Entry *dstobj, const Elf_Rela *rela)
411 const Elf_Sym *dstsym;
416 const Elf_Sym *srcsym;
419 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
420 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
421 name = dstobj->strtab + dstsym->st_name;
422 hash = elf_hash(name);
423 size = dstsym->st_size;
425 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
426 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
429 if (srcobj == NULL) {
430 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
431 " relocation in %s", name, dstobj->path);
435 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
436 memcpy(dstaddr, srcaddr, size);
441 * Process the special R_ALPHA_COPY relocations in the main program. These
442 * copy data from a shared object into a region in the main program's BSS
445 * Returns 0 on success, -1 on failure.
448 do_copy_relocations(Obj_Entry *dstobj)
450 const Elf_Rel *rellim;
452 const Elf_Rela *relalim;
453 const Elf_Rela *rela;
455 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
457 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
458 for (rel = dstobj->rel; dstobj->rel != NULL && rel < rellim; rel++) {
459 if (ELF_R_TYPE(rel->r_info) == R_ALPHA_COPY) {
462 locrela.r_info = rel->r_info;
463 locrela.r_offset = rel->r_offset;
464 locrela.r_addend = 0;
465 if (do_copy_relocation(dstobj, &locrela))
470 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
472 for (rela = dstobj->rela; dstobj->rela != NULL && rela < relalim;
474 if (ELF_R_TYPE(rela->r_info) == R_ALPHA_COPY) {
475 if (do_copy_relocation(dstobj, rela))
483 /* Initialize the special PLT entries. */
485 init_pltgot(Obj_Entry *obj)
489 if (obj->pltgot != NULL &&
490 (obj->pltrelsize != 0 || obj->pltrelasize != 0)) {
492 * This function will be called to perform the relocation.
493 * Look for the ldah instruction from the old PLT format since
494 * that will tell us what format we are trying to relocate.
496 pltgot = (u_int32_t *) obj->pltgot;
497 if ((pltgot[8] & 0xffff0000) == 0x279f0000)
498 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
500 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
501 /* Identify this shared object */
502 obj->pltgot[3] = (Elf_Addr) obj;