Deleted Added
full compact
reloc.c (112242) reloc.c (115396)
1/* $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $ */
2
3/*-
4 * Copyright (C) 1998 Tsubai Masanari
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 12 unchanged lines hidden (view full) ---

21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
1/* $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $ */
2
3/*-
4 * Copyright (C) 1998 Tsubai Masanari
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 12 unchanged lines hidden (view full) ---

21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $FreeBSD: head/libexec/rtld-elf/powerpc/reloc.c 112242 2003-03-14 21:10:13Z kan $
29 * $FreeBSD: head/libexec/rtld-elf/powerpc/reloc.c 115396 2003-05-29 22:58:26Z kan $
30 */
31
32#include <sys/param.h>
33#include <sys/mman.h>
34
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>

--- 12 unchanged lines hidden (view full) ---

50 * Process the R_PPC_COPY relocations
51 */
52int
53do_copy_relocations(Obj_Entry *dstobj)
54{
55 const Elf_Rela *relalim;
56 const Elf_Rela *rela;
57
30 */
31
32#include <sys/param.h>
33#include <sys/mman.h>
34
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>

--- 12 unchanged lines hidden (view full) ---

50 * Process the R_PPC_COPY relocations
51 */
52int
53do_copy_relocations(Obj_Entry *dstobj)
54{
55 const Elf_Rela *relalim;
56 const Elf_Rela *rela;
57
58 /*
58 /*
59 * COPY relocs are invalid outside of the main program
60 */
59 * COPY relocs are invalid outside of the main program
60 */
61 assert(dstobj->mainprog);
61 assert(dstobj->mainprog);
62
62
63 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
63 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
64 dstobj->relasize);
65 for (rela = dstobj->rela; rela < relalim; rela++) {
66 void *dstaddr;
67 const Elf_Sym *dstsym;
68 const char *name;
69 unsigned long hash;
70 size_t size;
71 const void *srcaddr;

--- 4 unchanged lines hidden (view full) ---

76 continue;
77 }
78
79 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
80 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
81 name = dstobj->strtab + dstsym->st_name;
82 hash = elf_hash(name);
83 size = dstsym->st_size;
64 dstobj->relasize);
65 for (rela = dstobj->rela; rela < relalim; rela++) {
66 void *dstaddr;
67 const Elf_Sym *dstsym;
68 const char *name;
69 unsigned long hash;
70 size_t size;
71 const void *srcaddr;

--- 4 unchanged lines hidden (view full) ---

76 continue;
77 }
78
79 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
80 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
81 name = dstobj->strtab + dstsym->st_name;
82 hash = elf_hash(name);
83 size = dstsym->st_size;
84
85 for (srcobj = dstobj->next; srcobj != NULL;
84
85 for (srcobj = dstobj->next; srcobj != NULL;
86 srcobj = srcobj->next) {
87 if ((srcsym = symlook_obj(name, hash, srcobj, false))
88 != NULL) {
89 break;
90 }
91 }
92
93 if (srcobj == NULL) {
94 _rtld_error("Undefined symbol \"%s\" "
95 " referenced from COPY"
96 " relocation in %s", name, dstobj->path);
97 return (-1);
98 }
86 srcobj = srcobj->next) {
87 if ((srcsym = symlook_obj(name, hash, srcobj, false))
88 != NULL) {
89 break;
90 }
91 }
92
93 if (srcobj == NULL) {
94 _rtld_error("Undefined symbol \"%s\" "
95 " referenced from COPY"
96 " relocation in %s", name, dstobj->path);
97 return (-1);
98 }
99
99
100 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value);
100 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value);
101 memcpy(dstaddr, srcaddr, size);
101 memcpy(dstaddr, srcaddr, size);
102 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
103 }
102 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
103 }
104
104
105 return (0);
106}
107
108
109/*
110 * Perform early relocation of the run-time linker image
111 */
112void

--- 13 unchanged lines hidden (view full) ---

126 break;
127 case DT_RELASZ:
128 relasz = dynp->d_un.d_val;
129 break;
130 }
131 }
132
133 /*
105 return (0);
106}
107
108
109/*
110 * Perform early relocation of the run-time linker image
111 */
112void

--- 13 unchanged lines hidden (view full) ---

126 break;
127 case DT_RELASZ:
128 relasz = dynp->d_un.d_val;
129 break;
130 }
131 }
132
133 /*
134 * Relocate these values
134 * Relocate these values
135 */
136 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
137 for (; rela < relalim; rela++) {
138 where = (Elf_Addr *)(relocbase + rela->r_offset);
139 *where = (Elf_Addr)(relocbase + rela->r_addend);
140 }
141}
142
143
144/*
135 */
136 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
137 for (; rela < relalim; rela++) {
138 where = (Elf_Addr *)(relocbase + rela->r_offset);
139 *where = (Elf_Addr)(relocbase + rela->r_addend);
140 }
141}
142
143
144/*
145 * Relocate a non-PLT object with addend.
145 * Relocate a non-PLT object with addend.
146 */
147static int
148reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
149 SymCache *cache)
150{
151 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
152 const Elf_Sym *def;
153 const Obj_Entry *defobj;
154 Elf_Addr tmp;
155
156 switch (ELF_R_TYPE(rela->r_info)) {
146 */
147static int
148reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
149 SymCache *cache)
150{
151 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
152 const Elf_Sym *def;
153 const Obj_Entry *defobj;
154 Elf_Addr tmp;
155
156 switch (ELF_R_TYPE(rela->r_info)) {
157
157
158 case R_PPC_NONE:
159 break;
160
161 case R_PPC_ADDR32: /* word32 S + A */
162 case R_PPC_GLOB_DAT: /* word32 S + A */
163 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
164 false, cache);
165 if (def == NULL) {

--- 6 unchanged lines hidden (view full) ---

172 /* Don't issue write if unnecessary; avoid COW page fault */
173 if (*where != tmp) {
174 *where = tmp;
175 }
176 break;
177
178 case R_PPC_RELATIVE: /* word32 B + A */
179 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
158 case R_PPC_NONE:
159 break;
160
161 case R_PPC_ADDR32: /* word32 S + A */
162 case R_PPC_GLOB_DAT: /* word32 S + A */
163 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
164 false, cache);
165 if (def == NULL) {

--- 6 unchanged lines hidden (view full) ---

172 /* Don't issue write if unnecessary; avoid COW page fault */
173 if (*where != tmp) {
174 *where = tmp;
175 }
176 break;
177
178 case R_PPC_RELATIVE: /* word32 B + A */
179 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
180
180
181 /* As above, don't issue write unnecessarily */
182 if (*where != tmp) {
183 *where = tmp;
184 }
185 break;
186
187 case R_PPC_COPY:
188 /*
189 * These are deferred until all other relocations
190 * have been done. All we do here is make sure
191 * that the COPY relocation is not in a shared
192 * library. They are allowed only in executable
193 * files.
194 */
195 if (!obj->mainprog) {
196 _rtld_error("%s: Unexpected R_COPY "
197 " relocation in shared library",
198 obj->path);
199 return (-1);
181 /* As above, don't issue write unnecessarily */
182 if (*where != tmp) {
183 *where = tmp;
184 }
185 break;
186
187 case R_PPC_COPY:
188 /*
189 * These are deferred until all other relocations
190 * have been done. All we do here is make sure
191 * that the COPY relocation is not in a shared
192 * library. They are allowed only in executable
193 * files.
194 */
195 if (!obj->mainprog) {
196 _rtld_error("%s: Unexpected R_COPY "
197 " relocation in shared library",
198 obj->path);
199 return (-1);
200 }
200 }
201 break;
202
203 case R_PPC_JMP_SLOT:
204 /*
205 * These will be handled by the plt/jmpslot routines
206 */
207 break;
208
209 default:
210 _rtld_error("%s: Unsupported relocation type %d"
211 " in non-PLT relocations\n", obj->path,
201 break;
202
203 case R_PPC_JMP_SLOT:
204 /*
205 * These will be handled by the plt/jmpslot routines
206 */
207 break;
208
209 default:
210 _rtld_error("%s: Unsupported relocation type %d"
211 " in non-PLT relocations\n", obj->path,
212 ELF_R_TYPE(rela->r_info));
212 ELF_R_TYPE(rela->r_info));
213 return (-1);
214 }
213 return (-1);
214 }
215 return (0);
215 return (0);
216}
217
218
219/*
220 * Process non-PLT relocations
221 */
222int
223reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)

--- 9 unchanged lines hidden (view full) ---

233 * limited amounts of stack available so we cannot use alloca().
234 */
235 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
236 if (cache == MAP_FAILED)
237 cache = NULL;
238
239 /*
240 * From the SVR4 PPC ABI:
216}
217
218
219/*
220 * Process non-PLT relocations
221 */
222int
223reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)

--- 9 unchanged lines hidden (view full) ---

233 * limited amounts of stack available so we cannot use alloca().
234 */
235 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
236 if (cache == MAP_FAILED)
237 cache = NULL;
238
239 /*
240 * From the SVR4 PPC ABI:
241 * "The PowerPC family uses only the Elf32_Rela relocation
241 * "The PowerPC family uses only the Elf32_Rela relocation
242 * entries with explicit addends."
243 */
244 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
245 for (rela = obj->rela; rela < relalim; rela++) {
246 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0)
247 goto done;
248 }
249 r = 0;

--- 21 unchanged lines hidden (view full) ---

271 if ((reloff < 0) || (reloff >= 0x8000)) {
272 return (-1);
273 }
274
275 pltresolve = obj->pltgot + 8;
276
277 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
278
242 * entries with explicit addends."
243 */
244 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
245 for (rela = obj->rela; rela < relalim; rela++) {
246 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0)
247 goto done;
248 }
249 r = 0;

--- 21 unchanged lines hidden (view full) ---

271 if ((reloff < 0) || (reloff >= 0x8000)) {
272 return (-1);
273 }
274
275 pltresolve = obj->pltgot + 8;
276
277 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
278
279 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
279 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
280 (void *)where, (void *)pltresolve, reloff, distance);
281
282 /* li r11,reloff */
283 /* b pltresolve */
284 where[0] = 0x39600000 | reloff;
285 where[1] = 0x48000000 | (distance & 0x03fffffc);
286
287 /*

--- 11 unchanged lines hidden (view full) ---

299int
300reloc_plt(Obj_Entry *obj)
301{
302 const Elf_Rela *relalim;
303 const Elf_Rela *rela;
304
305 if (obj->pltrelasize != 0) {
306
280 (void *)where, (void *)pltresolve, reloff, distance);
281
282 /* li r11,reloff */
283 /* b pltresolve */
284 where[0] = 0x39600000 | reloff;
285 where[1] = 0x48000000 | (distance & 0x03fffffc);
286
287 /*

--- 11 unchanged lines hidden (view full) ---

299int
300reloc_plt(Obj_Entry *obj)
301{
302 const Elf_Rela *relalim;
303 const Elf_Rela *rela;
304
305 if (obj->pltrelasize != 0) {
306
307 relalim = (const Elf_Rela *)((char *)obj->pltrela +
307 relalim = (const Elf_Rela *)((char *)obj->pltrela +
308 obj->pltrelasize);
309 for (rela = obj->pltrela; rela < relalim; rela++) {
310 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
311
312 if (reloc_plt_object(obj, rela) < 0) {
313 return (-1);
314 }
315 }

--- 31 unchanged lines hidden (view full) ---

347
348#if 0
349 /* PG XXX */
350 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
351 defobj->strtab + def->st_name, basename(obj->path),
352 (void *)target, basename(defobj->path));
353#endif
354
308 obj->pltrelasize);
309 for (rela = obj->pltrela; rela < relalim; rela++) {
310 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
311
312 if (reloc_plt_object(obj, rela) < 0) {
313 return (-1);
314 }
315 }

--- 31 unchanged lines hidden (view full) ---

347
348#if 0
349 /* PG XXX */
350 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
351 defobj->strtab + def->st_name, basename(obj->path),
352 (void *)target, basename(defobj->path));
353#endif
354
355 reloc_jmpslot(where, target, defobj, obj,
355 reloc_jmpslot(where, target, defobj, obj,
356 (const Elf_Rel *) rela);
357 }
358
359 obj->jmpslots_done = true;
360
361 return (0);
362}
363

--- 5 unchanged lines hidden (view full) ---

369 */
370Elf_Addr
371reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
372 const Obj_Entry *obj, const Elf_Rel *rel)
373{
374 Elf_Addr offset;
375 const Elf_Rela *rela = (const Elf_Rela *) rel;
376
356 (const Elf_Rel *) rela);
357 }
358
359 obj->jmpslots_done = true;
360
361 return (0);
362}
363

--- 5 unchanged lines hidden (view full) ---

369 */
370Elf_Addr
371reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
372 const Obj_Entry *obj, const Elf_Rel *rel)
373{
374 Elf_Addr offset;
375 const Elf_Rela *rela = (const Elf_Rela *) rel;
376
377 dbg(" reloc_jmpslot: where=%p, target=%p",
377 dbg(" reloc_jmpslot: where=%p, target=%p",
378 (void *)wherep, (void *)target);
379
380 /*
381 * At the PLT entry pointed at by `wherep', construct
382 * a direct transfer to the now fully resolved function
383 * address.
384 */
385 offset = target - (Elf_Addr)wherep;

--- 53 unchanged lines hidden (view full) ---

439 }
440
441 /*
442 * From the SVR4 PPC ABI:
443 *
444 * 'The first 18 words (72 bytes) of the PLT are reserved for
445 * use by the dynamic linker.
446 * ...
378 (void *)wherep, (void *)target);
379
380 /*
381 * At the PLT entry pointed at by `wherep', construct
382 * a direct transfer to the now fully resolved function
383 * address.
384 */
385 offset = target - (Elf_Addr)wherep;

--- 53 unchanged lines hidden (view full) ---

439 }
440
441 /*
442 * From the SVR4 PPC ABI:
443 *
444 * 'The first 18 words (72 bytes) of the PLT are reserved for
445 * use by the dynamic linker.
446 * ...
447 * 'If the executable or shared object requires N procedure
448 * linkage table entries, the link editor shall reserve 3*N
449 * words (12*N bytes) following the 18 reserved words. The
450 * first 2*N of these words are the procedure linkage table
451 * entries themselves. The static linker directs calls to bytes
452 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
447 * 'If the executable or shared object requires N procedure
448 * linkage table entries, the link editor shall reserve 3*N
449 * words (12*N bytes) following the 18 reserved words. The
450 * first 2*N of these words are the procedure linkage table
451 * entries themselves. The static linker directs calls to bytes
452 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
453 * N words (4*N bytes) are reserved for use by the dynamic linker.'
454 */
455
456 /*
457 * Copy the absolute-call assembler stub into the first part of
458 * the reserved PLT area.
459 */
460 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);

--- 31 unchanged lines hidden ---
453 * N words (4*N bytes) are reserved for use by the dynamic linker.'
454 */
455
456 /*
457 * Copy the absolute-call assembler stub into the first part of
458 * the reserved PLT area.
459 */
460 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);

--- 31 unchanged lines hidden ---