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.
29 * Dynamic linker for ELF.
31 * John Polstra <jdp@polstra.com>.
34 #include <sys/param.h>
50 extern Elf_Dyn _DYNAMIC;
52 /* Relocate a non-PLT object with addend. */
54 reloc_non_plt_obj(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela)
56 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
58 switch (ELF_R_TYPE(rela->r_info)) {
63 case R_ALPHA_REFQUAD: {
65 const Obj_Entry *defobj;
68 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
73 tmp_value = (Elf_Addr) (defobj->relocbase +
74 def->st_value) + *where + rela->r_addend;
75 if (*where != tmp_value)
80 case R_ALPHA_GLOB_DAT: {
82 const Obj_Entry *defobj;
84 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
89 if (*where != (Elf_Addr) (defobj->relocbase +
90 def->st_value + rela->r_addend))
91 *where = (Elf_Addr) (defobj->relocbase +
92 def->st_value + rela->r_addend);
96 case R_ALPHA_RELATIVE: {
97 if (obj != obj_rtld ||
98 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
99 (caddr_t)where >= (caddr_t)&_DYNAMIC)
100 *where += (Elf_Addr) obj->relocbase;
106 * These are deferred until all other relocations
107 * have been done. All we do here is make sure
108 * that the COPY relocation is not in a shared
109 * library. They are allowed only in executable
112 if (!obj->mainprog) {
113 _rtld_error("%s: Unexpected R_COPY "
114 " relocation in shared library",
122 _rtld_error("%s: Unsupported relocation type %d"
123 " in non-PLT relocations\n", obj->path,
124 ELF_R_TYPE(rela->r_info));
130 /* Process the non-PLT relocations. */
132 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
134 const Elf_Rel *rellim;
136 const Elf_Rela *relalim;
137 const Elf_Rela *rela;
139 /* Perform relocations without addend if there are any: */
140 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
141 for (rel = obj->rel; obj->rel != NULL && rel < rellim; rel++) {
144 locrela.r_info = rel->r_info;
145 locrela.r_offset = rel->r_offset;
146 locrela.r_addend = 0;
147 if (reloc_non_plt_obj(obj_rtld, obj, &locrela))
151 /* Perform relocations with addend if there are any: */
152 relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
153 for (rela = obj->rela; obj->rela != NULL && rela < relalim; rela++) {
154 if (reloc_non_plt_obj(obj_rtld, obj, rela))
160 /* Process the PLT relocations. */
162 reloc_plt(Obj_Entry *obj)
164 /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
165 if (obj->pltrelsize != 0) {
166 const Elf_Rel *rellim;
169 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
170 for (rel = obj->pltrel; rel < rellim; rel++) {
173 assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
175 /* Relocate the GOT slot pointing into the PLT. */
176 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
177 *where += (Elf_Addr)obj->relocbase;
180 const Elf_Rela *relalim;
181 const Elf_Rela *rela;
183 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
184 for (rela = obj->pltrela; rela < relalim; rela++) {
187 assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
189 /* Relocate the GOT slot pointing into the PLT. */
190 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
191 *where += (Elf_Addr)obj->relocbase;
197 /* Relocate the jump slots in an object. */
199 reloc_jmpslots(Obj_Entry *obj)
201 if (obj->jmpslots_done)
203 /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
204 if (obj->pltrelsize != 0) {
205 const Elf_Rel *rellim;
208 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
209 for (rel = obj->pltrel; rel < rellim; rel++) {
212 const Obj_Entry *defobj;
214 assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
215 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
216 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
220 (Elf_Addr)(defobj->relocbase + def->st_value));
223 const Elf_Rela *relalim;
224 const Elf_Rela *rela;
226 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
227 for (rela = obj->pltrela; rela < relalim; rela++) {
230 const Obj_Entry *defobj;
232 assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
233 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
234 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
238 (Elf_Addr)(defobj->relocbase + def->st_value));
241 obj->jmpslots_done = true;
245 /* Fixup the jump slot at "where" to transfer control to "target". */
247 reloc_jmpslot(Elf_Addr *where, Elf_Addr target)
251 dbg(" reloc_jmpslot: where=%p, target=%p", (void *)where, (void *)target);
253 if (stubaddr != target) {
261 /* Point this GOT entry directly at the target. */
265 * There may be multiple GOT tables, each with an entry
266 * pointing to the stub in the PLT. But we can only find and
267 * fix up the first GOT entry. So we must rewrite the stub as
268 * well, to perform a call to the target if it is executed.
270 * When the stub gets control, register pv ($27) contains its
271 * address. We adjust its value so that it points to the
272 * target, and then jump indirect through it.
274 * Each PLT entry has room for 3 instructions. If the
275 * adjustment amount fits in a signed 32-bit integer, we can
276 * simply add it to register pv. Otherwise we must load the
277 * GOT entry itself into the pv register.
279 delta = target - stubaddr;
280 dbg(" stubaddr=%p, where-stubaddr=%ld, delta=%ld", (void *)stubaddr,
281 (long)where - (long)stubaddr, (long)delta);
283 if ((int32_t)delta == delta) {
285 * We can adjust pv with a LDA, LDAH sequence.
287 * First build an LDA instruction to adjust the low 16 bits.
289 inst[instct++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
291 dbg(" LDA $27,%d($27)", (int16_t)delta);
293 * Adjust the delta to account for the effects of the LDA,
294 * including sign-extension.
296 delta -= (int16_t)delta;
298 /* Build an LDAH instruction to adjust the high 16 bits. */
299 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
300 (delta >> 16 & 0xffff);
301 dbg(" LDAH $27,%d($27)", (int16_t)(delta >> 16));
306 /* We must load the GOT entry from memory. */
307 delta = (Elf_Addr)where - stubaddr;
309 * If the GOT entry is too far away from the PLT entry,
310 * then punt. This PLT entry will have to be looked up
311 * manually for all GOT entries except the first one.
312 * The program will still run, albeit very slowly. It's
313 * extremely unlikely that this case could ever arise in
314 * practice, but we might as well handle it correctly if
317 if ((int32_t)delta != delta) {
318 dbg(" PLT stub too far from GOT to relocate");
321 dhigh = delta - (int16_t)delta;
323 /* Build an LDAH instruction to adjust the high 16 bits. */
324 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
325 (dhigh >> 16 & 0xffff);
326 dbg(" LDAH $27,%d($27)", (int16_t)(dhigh >> 16));
328 /* Build an LDQ to load the GOT entry. */
329 inst[instct++] = 0x29 << 26 | 27 << 21 | 27 << 16 |
331 dbg(" LDQ $27,%d($27)", (int16_t)delta);
335 * Build a JMP or BR instruction to jump to the target. If
336 * the instruction displacement fits in a sign-extended 21-bit
337 * field, we can use the more efficient BR instruction.
338 * Otherwise we have to jump indirect through the pv register.
340 pc = stubaddr + 4 * (instct + 1);
341 idisp = (int64_t)(target - pc) >> 2;
342 if (-0x100000 <= idisp && idisp < 0x100000) {
343 inst[instct++] = 0x30 << 26 | 31 << 21 | (idisp & 0x1fffff);
344 dbg(" BR $31,%p", (void *)target);
346 inst[instct++] = 0x1a << 26 | 31 << 21 | 27 << 16 |
348 dbg(" JMP $31,($27),%d", (int)(idisp & 0x3fff));
352 * Fill in the tail of the PLT entry first for reentrancy.
353 * Until we have overwritten the first instruction (an
354 * unconditional branch), the remaining instructions have no
357 stubptr = (u_int32_t *)stubaddr;
360 stubptr[instct] = inst[instct];
363 * Commit the tail of the instruction sequence to memory
364 * before overwriting the first instruction.
366 __asm__ __volatile__("wmb" : : : "memory");
367 stubptr[0] = inst[0];
371 /* Process an R_ALPHA_COPY relocation. */
373 do_copy_relocation(Obj_Entry *dstobj, const Elf_Rela *rela)
376 const Elf_Sym *dstsym;
381 const Elf_Sym *srcsym;
384 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
385 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
386 name = dstobj->strtab + dstsym->st_name;
387 hash = elf_hash(name);
388 size = dstsym->st_size;
390 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
391 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
394 if (srcobj == NULL) {
395 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
396 " relocation in %s", name, dstobj->path);
400 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
401 memcpy(dstaddr, srcaddr, size);
406 * Process the special R_ALPHA_COPY relocations in the main program. These
407 * copy data from a shared object into a region in the main program's BSS
410 * Returns 0 on success, -1 on failure.
413 do_copy_relocations(Obj_Entry *dstobj)
415 const Elf_Rel *rellim;
417 const Elf_Rela *relalim;
418 const Elf_Rela *rela;
420 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
422 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
423 for (rel = dstobj->rel; dstobj->rel != NULL && rel < rellim; rel++) {
424 if (ELF_R_TYPE(rel->r_info) == R_ALPHA_COPY) {
427 locrela.r_info = rel->r_info;
428 locrela.r_offset = rel->r_offset;
429 locrela.r_addend = 0;
430 if (do_copy_relocation(dstobj, &locrela))
435 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
437 for (rela = dstobj->rela; dstobj->rela != NULL && rela < relalim;
439 if (ELF_R_TYPE(rela->r_info) == R_ALPHA_COPY) {
440 if (do_copy_relocation(dstobj, rela))
448 /* Initialize the special PLT entries. */
450 init_pltgot(Obj_Entry *obj)
452 if (obj->pltgot != NULL &&
453 (obj->pltrelsize != 0 || obj->pltrelasize != 0)) {
454 /* This function will be called to perform the relocation. */
455 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
456 /* Identify this shared object */
457 obj->pltgot[3] = (Elf_Addr) obj;