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 $
26 * $DragonFly: src/libexec/rtld-elf/alpha/Attic/reloc.c,v 1.2 2003/06/17 04:27:08 dillon Exp $
30 * Dynamic linker for ELF.
32 * John Polstra <jdp@polstra.com>.
35 #include <sys/param.h>
51 extern Elf_Dyn _GOT_END_;
54 * Macros for loading/storing unaligned 64-bit values. These are
55 * needed because relocations can point to unaligned data. This
56 * occurs in the DWARF2 exception frame tables generated by the
57 * compiler, for instance.
59 * We don't use these when relocating jump slots and GOT entries,
60 * since they are guaranteed to be aligned.
62 #define load64(p) ({ \
64 __asm__("ldq_u %0,%1" : "=r"(__res) : "m"(*(p))); \
67 #define store64(p, v) \
68 __asm__("stq_u %1,%0" : "=m"(*(p)) : "r"(v))
70 /* Relocate a non-PLT object with addend. */
72 reloc_non_plt_obj(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
75 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
77 switch (ELF_R_TYPE(rela->r_info)) {
82 case R_ALPHA_REFQUAD: {
84 const Obj_Entry *defobj;
86 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
87 &defobj, false, cache);
91 (Elf_Addr) (defobj->relocbase + def->st_value) +
92 load64(where) + rela->r_addend);
96 case R_ALPHA_GLOB_DAT: {
98 const Obj_Entry *defobj;
101 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
102 &defobj, false, cache);
105 val = (Elf_Addr) (defobj->relocbase + def->st_value +
107 if (load64(where) != val)
112 case R_ALPHA_RELATIVE: {
113 if (obj != obj_rtld ||
114 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
115 (caddr_t)where >= (caddr_t)&_GOT_END_)
117 load64(where) + (Elf_Addr) obj->relocbase);
123 * These are deferred until all other relocations
124 * have been done. All we do here is make sure
125 * that the COPY relocation is not in a shared
126 * library. They are allowed only in executable
129 if (!obj->mainprog) {
130 _rtld_error("%s: Unexpected R_COPY "
131 " relocation in shared library",
139 _rtld_error("%s: Unsupported relocation type %d"
140 " in non-PLT relocations\n", obj->path,
141 ELF_R_TYPE(rela->r_info));
147 /* Process the non-PLT relocations. */
149 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
151 const Elf_Rel *rellim;
153 const Elf_Rela *relalim;
154 const Elf_Rela *rela;
156 int bytes = obj->nchains * sizeof(SymCache);
160 * The dynamic loader may be called from a thread, we have
161 * limited amounts of stack available so we cannot use alloca().
163 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
164 if (cache == MAP_FAILED)
167 memset(cache, 0, bytes);
169 /* Perform relocations without addend if there are any: */
170 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
171 for (rel = obj->rel; obj->rel != NULL && rel < rellim; rel++) {
174 locrela.r_info = rel->r_info;
175 locrela.r_offset = rel->r_offset;
176 locrela.r_addend = 0;
177 if (reloc_non_plt_obj(obj_rtld, obj, &locrela, cache))
181 /* Perform relocations with addend if there are any: */
182 relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
183 for (rela = obj->rela; obj->rela != NULL && rela < relalim; rela++) {
184 if (reloc_non_plt_obj(obj_rtld, obj, rela, cache))
190 munmap(cache, bytes);
194 /* Process the PLT relocations. */
196 reloc_plt(Obj_Entry *obj)
198 /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
199 if (obj->pltrelsize != 0) {
200 const Elf_Rel *rellim;
203 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
204 for (rel = obj->pltrel; rel < rellim; rel++) {
207 assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
209 /* Relocate the GOT slot pointing into the PLT. */
210 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
211 *where += (Elf_Addr)obj->relocbase;
214 const Elf_Rela *relalim;
215 const Elf_Rela *rela;
217 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
218 for (rela = obj->pltrela; rela < relalim; rela++) {
221 assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
223 /* Relocate the GOT slot pointing into the PLT. */
224 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
225 *where += (Elf_Addr)obj->relocbase;
231 /* Relocate the jump slots in an object. */
233 reloc_jmpslots(Obj_Entry *obj)
235 if (obj->jmpslots_done)
237 /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
238 if (obj->pltrelsize != 0) {
239 const Elf_Rel *rellim;
242 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
243 for (rel = obj->pltrel; rel < rellim; rel++) {
246 const Obj_Entry *defobj;
248 assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
249 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
250 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true,
255 (Elf_Addr)(defobj->relocbase + def->st_value));
258 const Elf_Rela *relalim;
259 const Elf_Rela *rela;
261 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
262 for (rela = obj->pltrela; rela < relalim; rela++) {
265 const Obj_Entry *defobj;
267 assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
268 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
269 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true,
274 (Elf_Addr)(defobj->relocbase + def->st_value));
277 obj->jmpslots_done = true;
281 /* Fixup the jump slot at "where" to transfer control to "target". */
283 reloc_jmpslot(Elf_Addr *where, Elf_Addr target)
287 dbg(" reloc_jmpslot: where=%p, target=%p", (void *)where, (void *)target);
289 if (stubaddr != target) {
297 /* Point this GOT entry directly at the target. */
301 * There may be multiple GOT tables, each with an entry
302 * pointing to the stub in the PLT. But we can only find and
303 * fix up the first GOT entry. So we must rewrite the stub as
304 * well, to perform a call to the target if it is executed.
306 * When the stub gets control, register pv ($27) contains its
307 * address. We adjust its value so that it points to the
308 * target, and then jump indirect through it.
310 * Each PLT entry has room for 3 instructions. If the
311 * adjustment amount fits in a signed 32-bit integer, we can
312 * simply add it to register pv. Otherwise we must load the
313 * GOT entry itself into the pv register.
315 delta = target - stubaddr;
316 dbg(" stubaddr=%p, where-stubaddr=%ld, delta=%ld", (void *)stubaddr,
317 (long)where - (long)stubaddr, (long)delta);
319 if ((int32_t)delta == delta) {
321 * We can adjust pv with a LDA, LDAH sequence.
323 * First build an LDA instruction to adjust the low 16 bits.
325 inst[instct++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
327 dbg(" LDA $27,%d($27)", (int16_t)delta);
329 * Adjust the delta to account for the effects of the LDA,
330 * including sign-extension.
332 delta -= (int16_t)delta;
334 /* Build an LDAH instruction to adjust the high 16 bits. */
335 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
336 (delta >> 16 & 0xffff);
337 dbg(" LDAH $27,%d($27)", (int16_t)(delta >> 16));
342 /* We must load the GOT entry from memory. */
343 delta = (Elf_Addr)where - stubaddr;
345 * If the GOT entry is too far away from the PLT entry,
346 * then punt. This PLT entry will have to be looked up
347 * manually for all GOT entries except the first one.
348 * The program will still run, albeit very slowly. It's
349 * extremely unlikely that this case could ever arise in
350 * practice, but we might as well handle it correctly if
353 if ((int32_t)delta != delta) {
354 dbg(" PLT stub too far from GOT to relocate");
357 dhigh = delta - (int16_t)delta;
359 /* Build an LDAH instruction to adjust the high 16 bits. */
360 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
361 (dhigh >> 16 & 0xffff);
362 dbg(" LDAH $27,%d($27)", (int16_t)(dhigh >> 16));
364 /* Build an LDQ to load the GOT entry. */
365 inst[instct++] = 0x29 << 26 | 27 << 21 | 27 << 16 |
367 dbg(" LDQ $27,%d($27)", (int16_t)delta);
371 * Build a JMP or BR instruction to jump to the target. If
372 * the instruction displacement fits in a sign-extended 21-bit
373 * field, we can use the more efficient BR instruction.
374 * Otherwise we have to jump indirect through the pv register.
376 pc = stubaddr + 4 * (instct + 1);
377 idisp = (int64_t)(target - pc) >> 2;
378 if (-0x100000 <= idisp && idisp < 0x100000) {
379 inst[instct++] = 0x30 << 26 | 31 << 21 | (idisp & 0x1fffff);
380 dbg(" BR $31,%p", (void *)target);
382 inst[instct++] = 0x1a << 26 | 31 << 21 | 27 << 16 |
384 dbg(" JMP $31,($27),%d", (int)(idisp & 0x3fff));
388 * Fill in the tail of the PLT entry first for reentrancy.
389 * Until we have overwritten the first instruction (an
390 * unconditional branch), the remaining instructions have no
393 stubptr = (u_int32_t *)stubaddr;
396 stubptr[instct] = inst[instct];
399 * Commit the tail of the instruction sequence to memory
400 * before overwriting the first instruction.
402 __asm__ __volatile__("wmb" : : : "memory");
403 stubptr[0] = inst[0];
407 /* Process an R_ALPHA_COPY relocation. */
409 do_copy_relocation(Obj_Entry *dstobj, const Elf_Rela *rela)
412 const Elf_Sym *dstsym;
417 const Elf_Sym *srcsym;
420 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
421 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
422 name = dstobj->strtab + dstsym->st_name;
423 hash = elf_hash(name);
424 size = dstsym->st_size;
426 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
427 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
430 if (srcobj == NULL) {
431 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
432 " relocation in %s", name, dstobj->path);
436 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
437 memcpy(dstaddr, srcaddr, size);
442 * Process the special R_ALPHA_COPY relocations in the main program. These
443 * copy data from a shared object into a region in the main program's BSS
446 * Returns 0 on success, -1 on failure.
449 do_copy_relocations(Obj_Entry *dstobj)
451 const Elf_Rel *rellim;
453 const Elf_Rela *relalim;
454 const Elf_Rela *rela;
456 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
458 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
459 for (rel = dstobj->rel; dstobj->rel != NULL && rel < rellim; rel++) {
460 if (ELF_R_TYPE(rel->r_info) == R_ALPHA_COPY) {
463 locrela.r_info = rel->r_info;
464 locrela.r_offset = rel->r_offset;
465 locrela.r_addend = 0;
466 if (do_copy_relocation(dstobj, &locrela))
471 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
473 for (rela = dstobj->rela; dstobj->rela != NULL && rela < relalim;
475 if (ELF_R_TYPE(rela->r_info) == R_ALPHA_COPY) {
476 if (do_copy_relocation(dstobj, rela))
484 /* Initialize the special PLT entries. */
486 init_pltgot(Obj_Entry *obj)
490 if (obj->pltgot != NULL &&
491 (obj->pltrelsize != 0 || obj->pltrelasize != 0)) {
493 * This function will be called to perform the relocation.
494 * Look for the ldah instruction from the old PLT format since
495 * that will tell us what format we are trying to relocate.
497 pltgot = (u_int32_t *) obj->pltgot;
498 if ((pltgot[8] & 0xffff0000) == 0x279f0000)
499 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
501 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
502 /* Identify this shared object */
503 obj->pltgot[3] = (Elf_Addr) obj;