mdreloc.c revision 1.2 1 1.1 mycroft #include <sys/types.h>
2 1.1 mycroft #include <sys/stat.h>
3 1.1 mycroft
4 1.1 mycroft #include "debug.h"
5 1.1 mycroft #include "rtld.h"
6 1.1 mycroft
7 1.1 mycroft void
8 1.1 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
9 1.1 mycroft {
10 1.1 mycroft obj->pltgot[1] = (Elf_Addr) obj;
11 1.1 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
12 1.1 mycroft }
13 1.2 mycroft
14 1.2 mycroft int
15 1.2 mycroft _rtld_relocate_nonplt_object(obj, rela, dodebug)
16 1.2 mycroft Obj_Entry *obj;
17 1.2 mycroft const Elf_Rela *rela;
18 1.2 mycroft bool dodebug;
19 1.2 mycroft {
20 1.2 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
21 1.2 mycroft const Elf_Sym *def;
22 1.2 mycroft const Obj_Entry *defobj;
23 1.2 mycroft Elf_Addr tmp;
24 1.2 mycroft
25 1.2 mycroft switch (ELF_R_TYPE(rela->r_info)) {
26 1.2 mycroft
27 1.2 mycroft case R_TYPE(NONE):
28 1.2 mycroft break;
29 1.2 mycroft
30 1.2 mycroft #if 1 /* XXX should not occur */
31 1.2 mycroft case R_TYPE(PC24): { /* word32 S - P + A */
32 1.2 mycroft Elf32_Sword addend;
33 1.2 mycroft
34 1.2 mycroft /*
35 1.2 mycroft * Extract addend and sign-extend if needed.
36 1.2 mycroft */
37 1.2 mycroft addend = *where;
38 1.2 mycroft if (addend & 0x00800000)
39 1.2 mycroft addend |= 0xff000000;
40 1.2 mycroft
41 1.2 mycroft def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
42 1.2 mycroft if (def == NULL)
43 1.2 mycroft return -1;
44 1.2 mycroft tmp = (Elf_Addr)obj->relocbase + def->st_value
45 1.2 mycroft - (Elf_Addr)where + (addend << 2);
46 1.2 mycroft if ((tmp & 0xfe000000) != 0xfe000000 &&
47 1.2 mycroft (tmp & 0xfe000000) != 0) {
48 1.2 mycroft _rtld_error(
49 1.2 mycroft "%s: R_ARM_PC24 relocation @ %p to %s failed "
50 1.2 mycroft "(displacement %ld (%#lx) out of range)",
51 1.2 mycroft obj->path, where, defobj->strtab + def->st_name,
52 1.2 mycroft (long) tmp, (long) tmp);
53 1.2 mycroft return -1;
54 1.2 mycroft }
55 1.2 mycroft tmp >>= 2;
56 1.2 mycroft *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
57 1.2 mycroft rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
58 1.2 mycroft defobj->strtab + def->st_name, obj->path,
59 1.2 mycroft (void *)*where, where, defobj->path));
60 1.2 mycroft break;
61 1.2 mycroft }
62 1.2 mycroft #endif
63 1.2 mycroft
64 1.2 mycroft case R_TYPE(ABS32): /* word32 B + S + A */
65 1.2 mycroft def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
66 1.2 mycroft if (def == NULL)
67 1.2 mycroft return -1;
68 1.2 mycroft *where += (Elf_Addr)defobj->relocbase + def->st_value;
69 1.2 mycroft rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
70 1.2 mycroft defobj->strtab + def->st_name, obj->path,
71 1.2 mycroft (void *)*where, where, defobj->path));
72 1.2 mycroft break;
73 1.2 mycroft
74 1.2 mycroft case R_TYPE(GLOB_DAT): /* word32 B + S */
75 1.2 mycroft def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
76 1.2 mycroft if (def == NULL)
77 1.2 mycroft return -1;
78 1.2 mycroft *where = (Elf_Addr)(defobj->relocbase + def->st_value);
79 1.2 mycroft rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
80 1.2 mycroft defobj->strtab + def->st_name, obj->path,
81 1.2 mycroft (void *)*where, where, defobj->path));
82 1.2 mycroft break;
83 1.2 mycroft
84 1.2 mycroft case R_TYPE(RELATIVE): /* word32 B + A */
85 1.2 mycroft *where += (Elf_Addr)obj->relocbase;
86 1.2 mycroft rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
87 1.2 mycroft (void *)*where, where));
88 1.2 mycroft break;
89 1.2 mycroft
90 1.2 mycroft case R_TYPE(COPY):
91 1.2 mycroft /*
92 1.2 mycroft * These are deferred until all other relocations have
93 1.2 mycroft * been done. All we do here is make sure that the COPY
94 1.2 mycroft * relocation is not in a shared library. They are allowed
95 1.2 mycroft * only in executable files.
96 1.2 mycroft */
97 1.2 mycroft if (!obj->mainprog) {
98 1.2 mycroft _rtld_error(
99 1.2 mycroft "%s: Unexpected R_COPY relocation in shared library",
100 1.2 mycroft obj->path);
101 1.2 mycroft return -1;
102 1.2 mycroft }
103 1.2 mycroft rdbg(dodebug, ("COPY (avoid in main)"));
104 1.2 mycroft break;
105 1.2 mycroft
106 1.2 mycroft default:
107 1.2 mycroft def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
108 1.2 mycroft rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
109 1.2 mycroft "addend = %p, contents = %p, symbol = %s",
110 1.2 mycroft (u_long)ELF_R_SYM(rela->r_info),
111 1.2 mycroft (u_long)ELF_R_TYPE(rela->r_info),
112 1.2 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
113 1.2 mycroft (void *)*where,
114 1.2 mycroft def ? defobj->strtab + def->st_name : "??"));
115 1.2 mycroft _rtld_error("%s: Unsupported relocation type %ld "
116 1.2 mycroft "in non-PLT relocations\n",
117 1.2 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
118 1.2 mycroft return -1;
119 1.2 mycroft }
120 1.2 mycroft return 0;
121 1.2 mycroft }
122