]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/powerpc/powerpc32/dl-machine.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc32 / dl-machine.c
CommitLineData
052b6a6c 1/* Machine-dependent ELF dynamic relocation functions. PowerPC version.
f7a9f785 2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
052b6a6c
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
052b6a6c
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
052b6a6c 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
052b6a6c
UD
18
19#include <unistd.h>
20#include <string.h>
21#include <sys/param.h>
22#include <link.h>
a42195db 23#include <ldsodefs.h>
052b6a6c 24#include <elf/dynamic-link.h>
b6299091 25#include <dl-machine.h>
eb96ffb0 26#include <_itoa.h>
052b6a6c 27
c7693af7 28/* The value __cache_line_size is defined in dl-sysdep.c and is initialised
acd262e6 29 by _dl_sysdep_start via DL_PLATFORM_INIT. */
c7693af7 30extern int __cache_line_size attribute_hidden;
acd262e6 31
052b6a6c 32
7137f424 33/* Stuff for the PLT. */
052b6a6c 34#define PLT_INITIAL_ENTRY_WORDS 18
7137f424
GK
35#define PLT_LONGBRANCH_ENTRY_WORDS 0
36#define PLT_TRAMPOLINE_ENTRY_WORDS 6
052b6a6c
UD
37#define PLT_DOUBLE_SIZE (1<<13)
38#define PLT_ENTRY_START_WORDS(entry_number) \
7137f424
GK
39 (PLT_INITIAL_ENTRY_WORDS + (entry_number)*2 \
40 + ((entry_number) > PLT_DOUBLE_SIZE \
41 ? ((entry_number) - PLT_DOUBLE_SIZE)*2 \
42 : 0))
052b6a6c
UD
43#define PLT_DATA_START_WORDS(num_entries) PLT_ENTRY_START_WORDS(num_entries)
44
7137f424 45/* Macros to build PowerPC opcode words. */
052b6a6c 46#define OPCODE_ADDI(rd,ra,simm) \
118bad87 47 (0x38000000 | (rd) << 21 | (ra) << 16 | ((simm) & 0xffff))
052b6a6c 48#define OPCODE_ADDIS(rd,ra,simm) \
118bad87 49 (0x3c000000 | (rd) << 21 | (ra) << 16 | ((simm) & 0xffff))
052b6a6c
UD
50#define OPCODE_ADD(rd,ra,rb) \
51 (0x7c000214 | (rd) << 21 | (ra) << 16 | (rb) << 11)
118bad87
UD
52#define OPCODE_B(target) (0x48000000 | ((target) & 0x03fffffc))
53#define OPCODE_BA(target) (0x48000002 | ((target) & 0x03fffffc))
052b6a6c
UD
54#define OPCODE_BCTR() 0x4e800420
55#define OPCODE_LWZ(rd,d,ra) \
118bad87 56 (0x80000000 | (rd) << 21 | (ra) << 16 | ((d) & 0xffff))
7137f424
GK
57#define OPCODE_LWZU(rd,d,ra) \
58 (0x84000000 | (rd) << 21 | (ra) << 16 | ((d) & 0xffff))
052b6a6c
UD
59#define OPCODE_MTCTR(rd) (0x7C0903A6 | (rd) << 21)
60#define OPCODE_RLWINM(ra,rs,sh,mb,me) \
61 (0x54000000 | (rs) << 21 | (ra) << 16 | (sh) << 11 | (mb) << 6 | (me) << 1)
62
63#define OPCODE_LI(rd,simm) OPCODE_ADDI(rd,0,simm)
7137f424
GK
64#define OPCODE_ADDIS_HI(rd,ra,value) \
65 OPCODE_ADDIS(rd,ra,((value) + 0x8000) >> 16)
66#define OPCODE_LIS_HI(rd,value) OPCODE_ADDIS_HI(rd,0,value)
052b6a6c
UD
67#define OPCODE_SLWI(ra,rs,sh) OPCODE_RLWINM(ra,rs,sh,0,31-sh)
68
69
f57ae0b2
UD
70#define PPC_DCBST(where) asm volatile ("dcbst 0,%0" : : "r"(where) : "memory")
71#define PPC_SYNC asm volatile ("sync" : : : "memory")
2d09b95d 72#define PPC_ISYNC asm volatile ("sync; isync" : : : "memory")
f57ae0b2 73#define PPC_ICBI(where) asm volatile ("icbi 0,%0" : : "r"(where) : "memory")
052b6a6c
UD
74#define PPC_DIE asm volatile ("tweq 0,0")
75
76/* Use this when you've modified some code, but it won't be in the
77 instruction fetch queue (or when it doesn't matter if it is). */
78#define MODIFIED_CODE_NOQUEUE(where) \
79 do { PPC_DCBST(where); PPC_SYNC; PPC_ICBI(where); } while (0)
80/* Use this when it might be in the instruction queue. */
81#define MODIFIED_CODE(where) \
82 do { PPC_DCBST(where); PPC_SYNC; PPC_ICBI(where); PPC_ISYNC; } while (0)
83
84
85/* The idea here is that to conform to the ABI, we are supposed to try
86 to load dynamic objects between 0x10000 (we actually use 0x40000 as
87 the lower bound, to increase the chance of a memory reference from
88 a null pointer giving a segfault) and the program's load address;
89 this may allow us to use a branch instruction in the PLT rather
90 than a computed jump. The address is only used as a preference for
91 mmap, so if we get it wrong the worst that happens is that it gets
92 mapped somewhere else. */
93
94ElfW(Addr)
5ca3d19c
UD
95__elf_preferred_address (struct link_map *loader, size_t maplength,
96 ElfW(Addr) mapstartpref)
052b6a6c
UD
97{
98 ElfW(Addr) low, high;
99 struct link_map *l;
5ca3d19c 100 Lmid_t nsid;
052b6a6c
UD
101
102 /* If the object has a preference, load it there! */
103 if (mapstartpref != 0)
104 return mapstartpref;
105
106 /* Otherwise, quickly look for a suitable gap between 0x3FFFF and
107 0x70000000. 0x3FFFF is so that references off NULL pointers will
108 cause a segfault, 0x70000000 is just paranoia (it should always
2ccdea26 109 be superseded by the program's load address). */
052b6a6c
UD
110 low = 0x0003FFFF;
111 high = 0x70000000;
5ca3d19c
UD
112 for (nsid = 0; nsid < DL_NNS; ++nsid)
113 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
114 {
115 ElfW(Addr) mapstart, mapend;
116 mapstart = l->l_map_start & ~(GLRO(dl_pagesize) - 1);
117 mapend = l->l_map_end | (GLRO(dl_pagesize) - 1);
118 assert (mapend > mapstart);
119
120 /* Prefer gaps below the main executable, note that l ==
121 _dl_loaded does not work for static binaries loading
122 e.g. libnss_*.so. */
123 if ((mapend >= high || l->l_type == lt_executable)
350635a5 124 && high >= mapstart)
5ca3d19c
UD
125 high = mapstart;
126 else if (mapend >= low && low >= mapstart)
127 low = mapend;
128 else if (high >= mapend && mapstart >= low)
129 {
130 if (high - mapend >= mapstart - low)
131 low = mapend;
132 else
133 high = mapstart;
134 }
135 }
052b6a6c
UD
136
137 high -= 0x10000; /* Allow some room between objects. */
afdca0f2 138 maplength = (maplength | (GLRO(dl_pagesize) - 1)) + 1;
052b6a6c
UD
139 if (high <= low || high - low < maplength )
140 return 0;
141 return high - maplength; /* Both high and maplength are page-aligned. */
142}
143
144/* Set up the loaded object described by L so its unrelocated PLT
145 entries will jump to the on-demand fixup code in dl-runtime.c.
146 Also install a small trampoline to be used by entries that have
147 been relocated to an address too far away for a single branch. */
148
7137f424
GK
149/* There are many kinds of PLT entries:
150
151 (1) A direct jump to the actual routine, either a relative or
152 absolute branch. These are set up in __elf_machine_fixup_plt.
153
154 (2) Short lazy entries. These cover the first 8192 slots in
155 the PLT, and look like (where 'index' goes from 0 to 8191):
156
157 li %r11, index*4
158 b &plt[PLT_TRAMPOLINE_ENTRY_WORDS+1]
159
160 (3) Short indirect jumps. These replace (2) when a direct jump
161 wouldn't reach. They look the same except that the branch
162 is 'b &plt[PLT_LONGBRANCH_ENTRY_WORDS]'.
163
164 (4) Long lazy entries. These cover the slots when a short entry
165 won't fit ('index*4' overflows its field), and look like:
166
167 lis %r11, %hi(index*4 + &plt[PLT_DATA_START_WORDS])
168 lwzu %r12, %r11, %lo(index*4 + &plt[PLT_DATA_START_WORDS])
169 b &plt[PLT_TRAMPOLINE_ENTRY_WORDS]
170 bctr
171
172 (5) Long indirect jumps. These replace (4) when a direct jump
173 wouldn't reach. They look like:
174
175 lis %r11, %hi(index*4 + &plt[PLT_DATA_START_WORDS])
176 lwz %r12, %r11, %lo(index*4 + &plt[PLT_DATA_START_WORDS])
177 mtctr %r12
178 bctr
179
180 (6) Long direct jumps. These are used when thread-safety is not
181 required. They look like:
182
183 lis %r12, %hi(finaladdr)
184 addi %r12, %r12, %lo(finaladdr)
185 mtctr %r12
186 bctr
187
188
189 The lazy entries, (2) and (4), are set up here in
190 __elf_machine_runtime_setup. (1), (3), and (5) are set up in
191 __elf_machine_fixup_plt. (1), (3), and (6) can also be constructed
192 in __process_machine_rela.
193
194 The reason for the somewhat strange construction of the long
195 entries, (4) and (5), is that we need to ensure thread-safety. For
196 (1) and (3), this is obvious because only one instruction is
197 changed and the PPC architecture guarantees that aligned stores are
198 atomic. For (5), this is more tricky. When changing (4) to (5),
ded5b9b7 199 the `b' instruction is first changed to `mtctr'; this is safe
7137f424
GK
200 and is why the `lwzu' instruction is not just a simple `addi'.
201 Once this is done, and is visible to all processors, the `lwzu' can
202 safely be changed to a `lwz'. */
052b6a6c
UD
203int
204__elf_machine_runtime_setup (struct link_map *map, int lazy, int profile)
205{
206 if (map->l_info[DT_JMPREL])
207 {
208 Elf32_Word i;
b86120ed 209 Elf32_Word *plt = (Elf32_Word *) D_PTR (map, l_info[DT_PLTGOT]);
052b6a6c
UD
210 Elf32_Word num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
211 / sizeof (Elf32_Rela));
212 Elf32_Word rel_offset_words = PLT_DATA_START_WORDS (num_plt_entries);
7137f424 213 Elf32_Word data_words = (Elf32_Word) (plt + rel_offset_words);
052b6a6c 214 Elf32_Word size_modified;
7137f424 215
052b6a6c
UD
216 extern void _dl_runtime_resolve (void);
217 extern void _dl_prof_resolve (void);
052b6a6c 218
7137f424
GK
219 /* Convert the index in r11 into an actual address, and get the
220 word at that address. */
221 plt[PLT_LONGBRANCH_ENTRY_WORDS] = OPCODE_ADDIS_HI (11, 11, data_words);
222 plt[PLT_LONGBRANCH_ENTRY_WORDS + 1] = OPCODE_LWZ (11, data_words, 11);
052b6a6c 223
7137f424
GK
224 /* Call the procedure at that address. */
225 plt[PLT_LONGBRANCH_ENTRY_WORDS + 2] = OPCODE_MTCTR (11);
226 plt[PLT_LONGBRANCH_ENTRY_WORDS + 3] = OPCODE_BCTR ();
722c33bb 227
052b6a6c 228 if (lazy)
052b6a6c 229 {
7137f424 230 Elf32_Word *tramp = plt + PLT_TRAMPOLINE_ENTRY_WORDS;
b53ef01a 231 Elf32_Word dlrr;
7137f424
GK
232 Elf32_Word offset;
233
b53ef01a
AS
234#ifndef PROF
235 dlrr = (Elf32_Word) (profile
236 ? _dl_prof_resolve
237 : _dl_runtime_resolve);
70cd1f97
UD
238 if (profile && GLRO(dl_profile) != NULL
239 && _dl_name_match_p (GLRO(dl_profile), map))
7137f424
GK
240 /* This is the object we are looking for. Say that we really
241 want profiling and the timers are started. */
5688da55 242 GL(dl_profile_map) = map;
b53ef01a
AS
243#else
244 dlrr = (Elf32_Word) _dl_runtime_resolve;
245#endif
fb0dd050 246
7137f424
GK
247 /* For the long entries, subtract off data_words. */
248 tramp[0] = OPCODE_ADDIS_HI (11, 11, -data_words);
249 tramp[1] = OPCODE_ADDI (11, 11, -data_words);
fb0dd050 250
7137f424
GK
251 /* Multiply index of entry by 3 (in r11). */
252 tramp[2] = OPCODE_SLWI (12, 11, 1);
253 tramp[3] = OPCODE_ADD (11, 12, 11);
254 if (dlrr <= 0x01fffffc || dlrr >= 0xfe000000)
052b6a6c 255 {
7137f424
GK
256 /* Load address of link map in r12. */
257 tramp[4] = OPCODE_LI (12, (Elf32_Word) map);
258 tramp[5] = OPCODE_ADDIS_HI (12, 12, (Elf32_Word) map);
fb0dd050 259
7137f424
GK
260 /* Call _dl_runtime_resolve. */
261 tramp[6] = OPCODE_BA (dlrr);
052b6a6c
UD
262 }
263 else
7137f424
GK
264 {
265 /* Get address of _dl_runtime_resolve in CTR. */
266 tramp[4] = OPCODE_LI (12, dlrr);
267 tramp[5] = OPCODE_ADDIS_HI (12, 12, dlrr);
268 tramp[6] = OPCODE_MTCTR (12);
fb0dd050 269
7137f424
GK
270 /* Load address of link map in r12. */
271 tramp[7] = OPCODE_LI (12, (Elf32_Word) map);
272 tramp[8] = OPCODE_ADDIS_HI (12, 12, (Elf32_Word) map);
fb0dd050 273
7137f424
GK
274 /* Call _dl_runtime_resolve. */
275 tramp[9] = OPCODE_BCTR ();
276 }
fb0dd050 277
7137f424
GK
278 /* Set up the lazy PLT entries. */
279 offset = PLT_INITIAL_ENTRY_WORDS;
280 i = 0;
281 while (i < num_plt_entries && i < PLT_DOUBLE_SIZE)
052b6a6c
UD
282 {
283 plt[offset ] = OPCODE_LI (11, i * 4);
7137f424
GK
284 plt[offset+1] = OPCODE_B ((PLT_TRAMPOLINE_ENTRY_WORDS + 2
285 - (offset+1))
286 * 4);
287 i++;
288 offset += 2;
289 }
290 while (i < num_plt_entries)
291 {
292 plt[offset ] = OPCODE_LIS_HI (11, i * 4 + data_words);
293 plt[offset+1] = OPCODE_LWZU (12, i * 4 + data_words, 11);
294 plt[offset+2] = OPCODE_B ((PLT_TRAMPOLINE_ENTRY_WORDS
295 - (offset+2))
296 * 4);
297 plt[offset+3] = OPCODE_BCTR ();
298 i++;
299 offset += 4;
052b6a6c
UD
300 }
301 }
302
7137f424
GK
303 /* Now, we've modified code. We need to write the changes from
304 the data cache to a second-level unified cache, then make
305 sure that stale data in the instruction cache is removed.
306 (In a multiprocessor system, the effect is more complex.)
307 Most of the PLT shouldn't be in the instruction cache, but
308 there may be a little overlap at the start and the end.
052b6a6c 309
7137f424 310 Assumes that dcbst and icbi apply to lines of 16 bytes or
acd262e6
UD
311 more. Current known line sizes are 16, 32, and 128 bytes.
312 The following gets the __cache_line_size, when available. */
313
314 /* Default minimum 4 words per cache line. */
315 int line_size_words = 4;
316
c7693af7
UD
317 if (lazy && __cache_line_size != 0)
318 /* Convert bytes to words. */
319 line_size_words = __cache_line_size / 4;
052b6a6c 320
7137f424 321 size_modified = lazy ? rel_offset_words : 6;
acd262e6
UD
322 for (i = 0; i < size_modified; i += line_size_words)
323 PPC_DCBST (plt + i);
f1d34527 324 PPC_DCBST (plt + size_modified - 1);
052b6a6c 325 PPC_SYNC;
acd262e6
UD
326
327 for (i = 0; i < size_modified; i += line_size_words)
328 PPC_ICBI (plt + i);
7137f424 329 PPC_ICBI (plt + size_modified - 1);
052b6a6c
UD
330 PPC_ISYNC;
331 }
332
333 return lazy;
334}
335
b6299091 336Elf32_Addr
77799d9d 337__elf_machine_fixup_plt (struct link_map *map,
5ca3d19c 338 Elf32_Addr *reloc_addr, Elf32_Addr finaladdr)
052b6a6c 339{
7137f424 340 Elf32_Sword delta = finaladdr - (Elf32_Word) reloc_addr;
052b6a6c
UD
341 if (delta << 6 >> 6 == delta)
342 *reloc_addr = OPCODE_B (delta);
343 else if (finaladdr <= 0x01fffffc || finaladdr >= 0xfe000000)
344 *reloc_addr = OPCODE_BA (finaladdr);
345 else
346 {
7137f424
GK
347 Elf32_Word *plt, *data_words;
348 Elf32_Word index, offset, num_plt_entries;
fb0dd050 349
7137f424
GK
350 num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
351 / sizeof(Elf32_Rela));
b86120ed 352 plt = (Elf32_Word *) D_PTR (map, l_info[DT_PLTGOT]);
7137f424
GK
353 offset = reloc_addr - plt;
354 index = (offset - PLT_INITIAL_ENTRY_WORDS)/2;
355 data_words = plt + PLT_DATA_START_WORDS (num_plt_entries);
356
357 reloc_addr += 1;
358
359 if (index < PLT_DOUBLE_SIZE)
052b6a6c 360 {
7137f424
GK
361 data_words[index] = finaladdr;
362 PPC_SYNC;
fb0dd050 363 *reloc_addr = OPCODE_B ((PLT_LONGBRANCH_ENTRY_WORDS - (offset+1))
7137f424 364 * 4);
052b6a6c
UD
365 }
366 else
367 {
7137f424
GK
368 index -= (index - PLT_DOUBLE_SIZE)/2;
369
370 data_words[index] = finaladdr;
371 PPC_SYNC;
372
373 reloc_addr[1] = OPCODE_MTCTR (12);
374 MODIFIED_CODE_NOQUEUE (reloc_addr + 1);
375 PPC_SYNC;
376
377 reloc_addr[0] = OPCODE_LWZ (12,
378 (Elf32_Word) (data_words + index), 11);
052b6a6c
UD
379 }
380 }
381 MODIFIED_CODE (reloc_addr);
b6299091 382 return finaladdr;
052b6a6c
UD
383}
384
7551556f
RM
385void
386_dl_reloc_overflow (struct link_map *map,
387 const char *name,
388 Elf32_Addr *const reloc_addr,
7551556f 389 const Elf32_Sym *refsym)
c6e6c9c8
GK
390{
391 char buffer[128];
392 char *t;
393 t = stpcpy (buffer, name);
394 t = stpcpy (t, " relocation at 0x00000000");
395 _itoa_word ((unsigned) reloc_addr, t, 16, 0);
63c7a7e8 396 if (refsym)
fb0dd050
UD
397 {
398 const char *strtab;
399
400 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
401 t = stpcpy (t, " for symbol `");
63c7a7e8 402 t = stpcpy (t, strtab + refsym->st_name);
fb0dd050
UD
403 t = stpcpy (t, "'");
404 }
c6e6c9c8 405 t = stpcpy (t, " out of range");
84aafa91 406 _dl_signal_error (0, map->l_name, NULL, buffer);
c6e6c9c8
GK
407}
408
052b6a6c
UD
409void
410__process_machine_rela (struct link_map *map,
411 const Elf32_Rela *reloc,
545dbc93 412 struct link_map *sym_map,
052b6a6c
UD
413 const Elf32_Sym *sym,
414 const Elf32_Sym *refsym,
415 Elf32_Addr *const reloc_addr,
416 Elf32_Addr const finaladdr,
417 int rinfo)
418{
f8e3e9f3
AM
419 union unaligned
420 {
4cb81307
AM
421 uint16_t u2;
422 uint32_t u4;
f8e3e9f3
AM
423 } __attribute__((__packed__));
424
052b6a6c
UD
425 switch (rinfo)
426 {
427 case R_PPC_NONE:
428 return;
429
430 case R_PPC_ADDR32:
052b6a6c
UD
431 case R_PPC_GLOB_DAT:
432 case R_PPC_RELATIVE:
433 *reloc_addr = finaladdr;
434 return;
435
77799d9d
AM
436 case R_PPC_IRELATIVE:
437 *reloc_addr = ((Elf32_Addr (*) (void)) finaladdr) ();
438 return;
439
b51b47f4 440 case R_PPC_UADDR32:
f8e3e9f3 441 ((union unaligned *) reloc_addr)->u4 = finaladdr;
b51b47f4
UD
442 break;
443
052b6a6c 444 case R_PPC_ADDR24:
a1ffb40e 445 if (__glibc_unlikely (finaladdr > 0x01fffffc && finaladdr < 0xfe000000))
63c7a7e8 446 _dl_reloc_overflow (map, "R_PPC_ADDR24", reloc_addr, refsym);
118bad87 447 *reloc_addr = (*reloc_addr & 0xfc000003) | (finaladdr & 0x3fffffc);
052b6a6c
UD
448 break;
449
450 case R_PPC_ADDR16:
a1ffb40e 451 if (__glibc_unlikely (finaladdr > 0x7fff && finaladdr < 0xffff8000))
63c7a7e8 452 _dl_reloc_overflow (map, "R_PPC_ADDR16", reloc_addr, refsym);
052b6a6c
UD
453 *(Elf32_Half*) reloc_addr = finaladdr;
454 break;
455
b51b47f4 456 case R_PPC_UADDR16:
a1ffb40e 457 if (__glibc_unlikely (finaladdr > 0x7fff && finaladdr < 0xffff8000))
63c7a7e8 458 _dl_reloc_overflow (map, "R_PPC_UADDR16", reloc_addr, refsym);
f8e3e9f3 459 ((union unaligned *) reloc_addr)->u2 = finaladdr;
b51b47f4
UD
460 break;
461
052b6a6c
UD
462 case R_PPC_ADDR16_LO:
463 *(Elf32_Half*) reloc_addr = finaladdr;
464 break;
465
466 case R_PPC_ADDR16_HI:
467 *(Elf32_Half*) reloc_addr = finaladdr >> 16;
468 break;
469
470 case R_PPC_ADDR16_HA:
471 *(Elf32_Half*) reloc_addr = (finaladdr + 0x8000) >> 16;
472 break;
473
474 case R_PPC_ADDR14:
475 case R_PPC_ADDR14_BRTAKEN:
476 case R_PPC_ADDR14_BRNTAKEN:
a1ffb40e 477 if (__glibc_unlikely (finaladdr > 0x7fff && finaladdr < 0xffff8000))
63c7a7e8 478 _dl_reloc_overflow (map, "R_PPC_ADDR14", reloc_addr, refsym);
118bad87 479 *reloc_addr = (*reloc_addr & 0xffff0003) | (finaladdr & 0xfffc);
052b6a6c 480 if (rinfo != R_PPC_ADDR14)
118bad87
UD
481 *reloc_addr = ((*reloc_addr & 0xffdfffff)
482 | ((rinfo == R_PPC_ADDR14_BRTAKEN)
483 ^ (finaladdr >> 31)) << 21);
052b6a6c
UD
484 break;
485
486 case R_PPC_REL24:
487 {
7137f424 488 Elf32_Sword delta = finaladdr - (Elf32_Word) reloc_addr;
052b6a6c 489 if (delta << 6 >> 6 != delta)
63c7a7e8 490 _dl_reloc_overflow (map, "R_PPC_REL24", reloc_addr, refsym);
118bad87 491 *reloc_addr = (*reloc_addr & 0xfc000003) | (delta & 0x3fffffc);
052b6a6c
UD
492 }
493 break;
494
495 case R_PPC_COPY:
496 if (sym == NULL)
497 /* This can happen in trace mode when an object could not be
498 found. */
499 return;
500 if (sym->st_size > refsym->st_size
afdca0f2 501 || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
052b6a6c
UD
502 {
503 const char *strtab;
504
b86120ed 505 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
35fc382a 506 _dl_error_printf ("\
3cf44918 507%s: Symbol `%s' has different size in shared object, consider re-linking\n",
b9375348 508 RTLD_PROGNAME, strtab + refsym->st_name);
052b6a6c
UD
509 }
510 memcpy (reloc_addr, (char *) finaladdr, MIN (sym->st_size,
511 refsym->st_size));
512 return;
513
514 case R_PPC_REL32:
7137f424 515 *reloc_addr = finaladdr - (Elf32_Word) reloc_addr;
052b6a6c
UD
516 return;
517
518 case R_PPC_JMP_SLOT:
7137f424
GK
519 /* It used to be that elf_machine_fixup_plt was used here,
520 but that doesn't work when ld.so relocates itself
521 for the second time. On the bright side, there's
522 no need to worry about thread-safety here. */
523 {
524 Elf32_Sword delta = finaladdr - (Elf32_Word) reloc_addr;
525 if (delta << 6 >> 6 == delta)
526 *reloc_addr = OPCODE_B (delta);
527 else if (finaladdr <= 0x01fffffc || finaladdr >= 0xfe000000)
528 *reloc_addr = OPCODE_BA (finaladdr);
529 else
530 {
531 Elf32_Word *plt, *data_words;
532 Elf32_Word index, offset, num_plt_entries;
fb0dd050 533
b86120ed 534 plt = (Elf32_Word *) D_PTR (map, l_info[DT_PLTGOT]);
7137f424
GK
535 offset = reloc_addr - plt;
536
537 if (offset < PLT_DOUBLE_SIZE*2 + PLT_INITIAL_ENTRY_WORDS)
538 {
539 index = (offset - PLT_INITIAL_ENTRY_WORDS)/2;
540 num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
541 / sizeof(Elf32_Rela));
542 data_words = plt + PLT_DATA_START_WORDS (num_plt_entries);
543 data_words[index] = finaladdr;
544 reloc_addr[0] = OPCODE_LI (11, index * 4);
fb0dd050
UD
545 reloc_addr[1] = OPCODE_B ((PLT_LONGBRANCH_ENTRY_WORDS
546 - (offset+1))
7137f424
GK
547 * 4);
548 MODIFIED_CODE_NOQUEUE (reloc_addr + 1);
549 }
550 else
551 {
552 reloc_addr[0] = OPCODE_LIS_HI (12, finaladdr);
553 reloc_addr[1] = OPCODE_ADDI (12, 12, finaladdr);
554 reloc_addr[2] = OPCODE_MTCTR (12);
555 reloc_addr[3] = OPCODE_BCTR ();
556 MODIFIED_CODE_NOQUEUE (reloc_addr + 3);
557 }
558 }
559 }
560 break;
052b6a6c 561
11bf311e 562#define DO_TLS_RELOC(suffix) \
545dbc93
RM
563 case R_PPC_DTPREL##suffix: \
564 /* During relocation all TLS symbols are defined and used. \
565 Therefore the offset is already correct. */ \
566 if (sym_map != NULL) \
567 do_reloc##suffix ("R_PPC_DTPREL"#suffix, \
568 TLS_DTPREL_VALUE (sym, reloc)); \
569 break; \
570 case R_PPC_TPREL##suffix: \
571 if (sym_map != NULL) \
572 { \
573 CHECK_STATIC_TLS (map, sym_map); \
574 do_reloc##suffix ("R_PPC_TPREL"#suffix, \
575 TLS_TPREL_VALUE (sym_map, sym, reloc)); \
576 } \
577 break;
578
579 inline void do_reloc16 (const char *r_name, Elf32_Addr value)
580 {
a1ffb40e 581 if (__glibc_unlikely (value > 0x7fff && value < 0xffff8000))
63c7a7e8 582 _dl_reloc_overflow (map, r_name, reloc_addr, refsym);
545dbc93
RM
583 *(Elf32_Half *) reloc_addr = value;
584 }
585 inline void do_reloc16_LO (const char *r_name, Elf32_Addr value)
586 {
587 *(Elf32_Half *) reloc_addr = value;
588 }
589 inline void do_reloc16_HI (const char *r_name, Elf32_Addr value)
590 {
591 *(Elf32_Half *) reloc_addr = value >> 16;
592 }
593 inline void do_reloc16_HA (const char *r_name, Elf32_Addr value)
594 {
595 *(Elf32_Half *) reloc_addr = (value + 0x8000) >> 16;
596 }
597 DO_TLS_RELOC (16)
598 DO_TLS_RELOC (16_LO)
599 DO_TLS_RELOC (16_HI)
600 DO_TLS_RELOC (16_HA)
545dbc93 601
052b6a6c 602 default:
421c80d2 603 _dl_reloc_bad_type (map, rinfo, 0);
052b6a6c
UD
604 return;
605 }
606
607 MODIFIED_CODE_NOQUEUE (reloc_addr);
608}