1 /* Machine-dependent ELF dynamic relocation inline functions.  PA-RISC version.
2    Copyright (C) 1995-2022 Free Software Foundation, Inc.
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
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.
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
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef dl_machine_h
20 #define dl_machine_h 1
21 
22 #define ELF_MACHINE_NAME "hppa"
23 
24 #include <sys/param.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <link.h>
28 #include <errno.h>
29 #include <dl-fptr.h>
30 #include <abort-instr.h>
31 #include <tls.h>
32 #include <dl-static-tls.h>
33 #include <dl-machine-rel.h>
34 
35 /* These two definitions must match the definition of the stub in
36    bfd/elf32-hppa.c (see plt_stub[]).
37 
38    a. Define the size of the *entire* stub we place at the end of the PLT
39    table (right up against the GOT).
40 
41    b. Define the number of bytes back from the GOT to the entry point of
42    the PLT stub. You see the PLT stub must be entered in the middle
43    so it can depwi to find it's own address (long jump stub)
44 
45    c. Define the size of a single PLT entry so we can jump over the
46    last entry to get the stub address */
47 
48 #define SIZEOF_PLT_STUB (7*4)
49 #define GOT_FROM_PLT_STUB (4*4)
50 #define PLT_ENTRY_SIZE (2*4)
51 
52 /* The gp slot in the function descriptor contains the relocation offset
53    before resolution.  To distinguish between a resolved gp value and an
54    unresolved relocation offset we set an unused bit in the relocation
55    offset.  This would allow us to do a synchronzied two word update
56    using this bit (interlocked update), but instead of waiting for the
57    update we simply recompute the gp value given that we know the ip.  */
58 #define PA_GP_RELOC 1
59 
60 /* Initialize the function descriptor table before relocations */
61 static inline void
__hppa_init_bootstrap_fdesc_table(struct link_map * map)62 __hppa_init_bootstrap_fdesc_table (struct link_map *map)
63 {
64   ElfW(Addr) *boot_table;
65 
66   /* Careful: this will be called before got has been relocated... */
67   ELF_MACHINE_LOAD_ADDRESS(boot_table,_dl_boot_fptr_table);
68 
69   map->l_mach.fptr_table_len = ELF_MACHINE_BOOT_FPTR_TABLE_LEN;
70   map->l_mach.fptr_table = boot_table;
71 }
72 
73 #define ELF_MACHINE_BEFORE_RTLD_RELOC(map, dynamic_info)	\
74 	__hppa_init_bootstrap_fdesc_table (map);		\
75 	_dl_fptr_init();
76 
77 /* Return nonzero iff ELF header is compatible with the running host.  */
78 static inline int
elf_machine_matches_host(const Elf32_Ehdr * ehdr)79 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
80 {
81   return ehdr->e_machine == EM_PARISC;
82 }
83 
84 /* Return the link-time address of _DYNAMIC.  */
85 static inline Elf32_Addr
86 elf_machine_dynamic (void) __attribute__ ((const));
87 
88 static inline Elf32_Addr
elf_machine_dynamic(void)89 elf_machine_dynamic (void)
90 {
91   Elf32_Addr dynamic;
92 
93   asm ("bl	1f,%0\n"
94 "	addil	L'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 1),%0\n"
95 "1:	ldw	R'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 5)(%%r1),%0\n"
96        : "=r" (dynamic) : : "r1");
97 
98   return dynamic;
99 }
100 
101 /* Return the run-time load address of the shared object.  */
102 static inline Elf32_Addr
103 elf_machine_load_address (void) __attribute__ ((const));
104 
105 static inline Elf32_Addr
elf_machine_load_address(void)106 elf_machine_load_address (void)
107 {
108   Elf32_Addr dynamic;
109 
110   asm (
111 "	bl	1f,%0\n"
112 "	addil	L'_DYNAMIC - ($PIC_pcrel$0 - 1),%0\n"
113 "1:	ldo	R'_DYNAMIC - ($PIC_pcrel$0 - 5)(%%r1),%0\n"
114    : "=r" (dynamic) : : "r1");
115 
116   return dynamic - elf_machine_dynamic ();
117 }
118 
119 /* Fixup a PLT entry to bounce directly to the function at VALUE. */
120 static inline struct fdesc __attribute__ ((always_inline))
elf_machine_fixup_plt(struct link_map * map,lookup_t t,const ElfW (Sym)* refsym,const ElfW (Sym)* sym,const Elf32_Rela * reloc,Elf32_Addr * reloc_addr,struct fdesc value)121 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
122 		       const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
123 		       const Elf32_Rela *reloc,
124 		       Elf32_Addr *reloc_addr, struct fdesc value)
125 {
126   volatile Elf32_Addr *rfdesc = reloc_addr;
127   /* map is the link_map for the caller, t is the link_map for the object
128      being called */
129 
130   /* We would like the function descriptor to be double word aligned.  This
131      helps performance (ip and gp then reside on the same cache line) and
132      we can update the pair atomically with a single store.  The linker
133      now ensures this alignment but we still have to handle old code.  */
134   if ((unsigned int)reloc_addr & 7)
135     {
136       /* Need to ensure that the gp is visible before the code
137          entry point is updated */
138       rfdesc[1] = value.gp;
139       atomic_full_barrier();
140       rfdesc[0] = value.ip;
141     }
142   else
143     {
144       /* Update pair atomically with floating point store.  */
145       union { ElfW(Word) v[2]; double d; } u;
146 
147       u.v[0] = value.ip;
148       u.v[1] = value.gp;
149       *(volatile double *)rfdesc = u.d;
150     }
151   return value;
152 }
153 
154 /* Return the final value of a plt relocation.  */
155 static inline struct fdesc
elf_machine_plt_value(struct link_map * map,const Elf32_Rela * reloc,struct fdesc value)156 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
157 		       struct fdesc value)
158 {
159   /* We are rela only, return a function descriptor as a plt entry. */
160   return (struct fdesc) { value.ip + reloc->r_addend, value.gp };
161 }
162 
163 /* Set up the loaded object described by L so its unrelocated PLT
164    entries will jump to the on-demand fixup code in dl-runtime.c.  */
165 
166 static inline int
elf_machine_runtime_setup(struct link_map * l,struct r_scope_elem * scope[],int lazy,int profile)167 elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
168 			   int lazy, int profile)
169 {
170   Elf32_Addr *got = NULL;
171   Elf32_Addr l_addr, iplt, jmprel, end_jmprel, r_type, r_sym;
172   const Elf32_Rela *reloc;
173   struct fdesc *fptr;
174   static union {
175     unsigned char c[8];
176     Elf32_Addr i[2];
177   } sig = {{0x00,0xc0,0xff,0xee, 0xde,0xad,0xbe,0xef}};
178 
179   /* Initialize dp register for main executable.  */
180   if (l->l_main_map)
181     {
182       register Elf32_Addr dp asm ("%r27");
183 
184       dp = D_PTR (l, l_info[DT_PLTGOT]);
185       asm volatile ("" : : "r" (dp));
186     }
187 
188   /* If we don't have a PLT we can just skip all this... */
189   if (__builtin_expect (l->l_info[DT_JMPREL] == NULL,0))
190     return lazy;
191 
192   /* All paths use these values */
193   l_addr = l->l_addr;
194   jmprel = D_PTR(l, l_info[DT_JMPREL]);
195   end_jmprel = jmprel + l->l_info[DT_PLTRELSZ]->d_un.d_val;
196 
197   extern void _dl_runtime_resolve (void);
198   extern void _dl_runtime_profile (void);
199 
200   /* Linking lazily */
201   if (lazy)
202     {
203       /* FIXME: Search for the got, but backwards through the relocs, technically we should
204 	 find it on the first try. However, assuming the relocs got out of order the
205 	 routine is made a bit more robust by searching them all in case of failure. */
206       for (iplt = (end_jmprel - sizeof (Elf32_Rela)); iplt >= jmprel; iplt -= sizeof (Elf32_Rela))
207 	{
208 
209 	  reloc = (const Elf32_Rela *) iplt;
210 	  r_type = ELF32_R_TYPE (reloc->r_info);
211 	  r_sym = ELF32_R_SYM (reloc->r_info);
212 
213 	  got = (Elf32_Addr *) (reloc->r_offset + l_addr + PLT_ENTRY_SIZE + SIZEOF_PLT_STUB);
214 
215 	  /* If we aren't an IPLT, and we aren't NONE then it's a bad reloc */
216 	  if (__builtin_expect (r_type != R_PARISC_IPLT, 0))
217 	    {
218 	      if (__builtin_expect (r_type != R_PARISC_NONE, 0))
219 		_dl_reloc_bad_type (l, r_type, 1);
220 	      continue;
221 	    }
222 
223 	  /* Check for the plt_stub that binutils placed here for us
224 	     to use with _dl_runtime_resolve  */
225 	  if (got[-2] != sig.i[0] || got[-1] != sig.i[1])
226 	    {
227 	      got = NULL; /* Not the stub... keep looking */
228 	    }
229 	  else
230 	    {
231 	      /* Found the GOT! */
232 	      register Elf32_Addr ltp __asm__ ("%r19");
233 
234 	      /* Identify this shared object. Second entry in the got. */
235 	      got[1] = (Elf32_Addr) l;
236 
237 	      /* This function will be called to perform the relocation. */
238 	      if (__builtin_expect (!profile, 1))
239 		{
240 		  /* If a static application called us, then _dl_runtime_resolve is not
241 		     a function descriptor, but the *real* address of the function... */
242 		  if((unsigned long) &_dl_runtime_resolve & 3)
243 		    {
244 		      got[-2] = (Elf32_Addr) ((struct fdesc *)
245 				  ((unsigned long) &_dl_runtime_resolve & ~3))->ip;
246 		    }
247 		  else
248 		    {
249 		      /* Static executable! */
250 		      got[-2] = (Elf32_Addr) &_dl_runtime_resolve;
251 		    }
252 		}
253 	      else
254 		{
255 		  if (GLRO(dl_profile) != NULL
256 		      && _dl_name_match_p (GLRO(dl_profile), l))
257 		    {
258 		      /* This is the object we are looking for.  Say that
259 			 we really want profiling and the timers are
260 			 started.  */
261 		      GL(dl_profile_map) = l;
262 		    }
263 
264 		  if((unsigned long) &_dl_runtime_profile & 3)
265 		    {
266 		      got[-2] = (Elf32_Addr) ((struct fdesc *)
267 				  ((unsigned long) &_dl_runtime_profile & ~3))->ip;
268 		    }
269 		  else
270 		    {
271 		      /* Static executable */
272 		      got[-2] = (Elf32_Addr) &_dl_runtime_profile;
273 		    }
274 		}
275 	      /* Plunk in the gp of this function descriptor so we
276 		 can make the call to _dl_runtime_xxxxxx */
277 	      got[-1] = ltp;
278 	      break;
279 	      /* Done looking for the GOT, and stub is setup */
280 	    } /* else we found the GOT */
281 	} /* for, walk the relocs backwards */
282 
283       if(!got)
284 	return 0; /* No lazy linking for you! */
285 
286       /* Process all the relocs, now that we know the GOT... */
287       for (iplt = jmprel; iplt < end_jmprel; iplt += sizeof (Elf32_Rela))
288 	{
289 	  reloc = (const Elf32_Rela *) iplt;
290 	  r_type = ELF32_R_TYPE (reloc->r_info);
291 	  r_sym = ELF32_R_SYM (reloc->r_info);
292 
293 	  if (__builtin_expect (r_type == R_PARISC_IPLT, 1))
294 	    {
295 	      fptr = (struct fdesc *) (reloc->r_offset + l_addr);
296 	      if (r_sym != 0)
297 		{
298 		  /* Relocate the pointer to the stub.  */
299 		  fptr->ip = (Elf32_Addr) got - GOT_FROM_PLT_STUB;
300 
301 		  /* Instead of the LTP value, we put the reloc offset
302 		     here.  The trampoline code will load the proper
303 		     LTP and pass the reloc offset to the fixup
304 		     function.  */
305 		  fptr->gp = (iplt - jmprel) | PA_GP_RELOC;
306 		} /* r_sym != 0 */
307 	      else
308 		{
309 		  /* Relocate this *ABS* entry.  */
310 		  fptr->ip = reloc->r_addend + l_addr;
311 		  fptr->gp = D_PTR (l, l_info[DT_PLTGOT]);
312 		}
313 	    } /* r_type == R_PARISC_IPLT */
314 	} /* for all the relocations */
315     } /* if lazy */
316   else
317     {
318       for (iplt = jmprel; iplt < end_jmprel; iplt += sizeof (Elf32_Rela))
319 	{
320 	  reloc = (const Elf32_Rela *) iplt;
321 	  r_type = ELF32_R_TYPE (reloc->r_info);
322 	  r_sym = ELF32_R_SYM (reloc->r_info);
323 
324 	  if (__builtin_expect ((r_type == R_PARISC_IPLT) && (r_sym == 0), 1))
325 	    {
326 	      fptr = (struct fdesc *) (reloc->r_offset + l_addr);
327 	      /* Relocate this *ABS* entry, set only the gp, the rest is set later
328 		 when elf_machine_rela_relative is called (WITHOUT the linkmap)  */
329 	      fptr->gp = D_PTR (l, l_info[DT_PLTGOT]);
330 	    } /* r_type == R_PARISC_IPLT */
331 	} /* for all the relocations */
332     }
333   return lazy;
334 }
335 
336 
337 /* Names of the architecture-specific auditing callback functions.  */
338 #define ARCH_LA_PLTENTER hppa_gnu_pltenter
339 #define ARCH_LA_PLTEXIT hppa_gnu_pltexit
340 
341 /* Adjust DL_STACK_END to get value we want in __libc_stack_end.  */
342 #define DL_STACK_END(cookie) \
343   ((void *) (((long) (cookie)) + 0x160))
344 
345 /* Initial entry point code for the dynamic linker.
346    The C function `_dl_start' is the real entry point;
347    its return value is the user program's entry point.  */
348 
349 #define RTLD_START \
350 asm (									\
351 "	.text\n"							\
352 "	.globl _start\n"						\
353 "	.type _start,@function\n"					\
354 "_start:\n"								\
355 	/* The kernel does not give us an initial stack frame. */	\
356 "	ldo	64(%sp),%sp\n"						\
357 									\
358 	/* We need the LTP, and we need it now.				\
359 	   $PIC_pcrel$0 points 8 bytes past the current instruction,	\
360 	   just like a branch reloc.  This sequence gets us the		\
361 	   runtime address of _DYNAMIC. */				\
362 "	bl	0f,%r19\n"						\
363 "	addil	L'_DYNAMIC - ($PIC_pcrel$0 - 1),%r19\n"			\
364 "0:	ldo	R'_DYNAMIC - ($PIC_pcrel$0 - 5)(%r1),%r26\n"		\
365 									\
366 	/* The link time address is stored in the first entry of the	\
367 	   GOT.  */							\
368 "	addil	L'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 9),%r19\n"	\
369 "	ldw	R'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 13)(%r1),%r20\n" \
370 									\
371 "	sub	%r26,%r20,%r20\n"	/* Calculate load offset */	\
372 									\
373 	/* Rummage through the dynamic entries, looking for		\
374 	   DT_PLTGOT.  */						\
375 "	ldw,ma	8(%r26),%r19\n"						\
376 "1:	cmpib,=,n 3,%r19,2f\n"	/* tag == DT_PLTGOT? */			\
377 "	cmpib,<>,n 0,%r19,1b\n"						\
378 "	ldw,ma	8(%r26),%r19\n"						\
379 									\
380 	/* Uh oh!  We didn't find one.  Abort. */			\
381 "	iitlbp	%r0,(%sr0,%r0)\n"					\
382 									\
383 "2:	ldw	-4(%r26),%r19\n"	/* Found it, load value. */	\
384 "	add	%r19,%r20,%r19\n"	/* And add the load offset. */	\
385 									\
386 	/* Our initial stack layout is rather different from everyone	\
387 	   else's due to the unique PA-RISC ABI.  As far as I know it	\
388 	   looks like this:						\
389 									\
390 	   -----------------------------------  (this frame created above) \
391 	   |         32 bytes of magic       |				\
392 	   |---------------------------------|				\
393 	   | 32 bytes argument/sp save area  |				\
394 	   |---------------------------------|  ((current->mm->env_end)	\
395 	   |         N bytes of slack        |	 + 63 & ~63)		\
396 	   |---------------------------------|				\
397 	   |      envvar and arg strings     |				\
398 	   |---------------------------------|				\
399 	   |	    ELF auxiliary info	     |				\
400 	   |         (up to 28 words)        |				\
401 	   |---------------------------------|				\
402 	   |  Environment variable pointers  |				\
403 	   |         upwards to NULL	     |				\
404 	   |---------------------------------|				\
405 	   |        Argument pointers        |				\
406 	   |         upwards to NULL	     |				\
407 	   |---------------------------------|				\
408 	   |          argc (1 word)          |				\
409 	   -----------------------------------				\
410 									\
411 	  So, obviously, we can't just pass %sp to _dl_start.  That's	\
412 	  okay, argv-4 will do just fine.				\
413 									\
414 	  This is always within range so we'll be okay. */		\
415 "	bl	_dl_start,%rp\n"					\
416 "	ldo	-4(%r24),%r26\n"					\
417 									\
418 "	.globl _dl_start_user\n"					\
419 "	.type _dl_start_user,@function\n"				\
420 "_dl_start_user:\n"							\
421 	/* Save the entry point in %r3. */				\
422 "	copy	%ret0,%r3\n"						\
423 									\
424 	/* The loader adjusts argc, argv, env, and the aux vectors	\
425 	   directly on the stack to remove any arguments used for	\
426 	   direct loader invocation.  Thus, argc and argv must be	\
427 	   reloaded from from _dl_argc and _dl_argv.  */		\
428 									\
429 	/* Load argc from _dl_argc.  */					\
430 "	addil	LT'_dl_argc,%r19\n"					\
431 "	ldw	RT'_dl_argc(%r1),%r20\n"				\
432 "	ldw	0(%r20),%r25\n"						\
433 "	stw	%r25,-40(%sp)\n"					\
434 									\
435 	/* Same for argv with _dl_argv.  */				\
436 "	addil	LT'_dl_argv,%r19\n"					\
437 "	ldw	RT'_dl_argv(%r1),%r20\n"				\
438 "	ldw	0(%r20),%r24\n"						\
439 "	stw	%r24,-44(%sp)\n"					\
440 									\
441 	/* Call _dl_init(main_map, argc, argv, envp). */		\
442 "	addil	LT'_rtld_local,%r19\n"					\
443 "	ldw	RT'_rtld_local(%r1),%r26\n"				\
444 "	ldw	0(%r26),%r26\n"						\
445 									\
446 	/* envp = argv + argc + 1 */					\
447 "	sh2add	%r25,%r24,%r23\n"					\
448 "	bl	_dl_init,%r2\n"						\
449 "	ldo	4(%r23),%r23\n"	/* delay slot */			\
450 									\
451 	/* Reload argc, argv to the registers start.S expects.  */	\
452 "	ldw	-40(%sp),%r25\n"					\
453 "	ldw	-44(%sp),%r24\n"					\
454 									\
455 	/* _dl_fini is a local function in the loader, so we construct	\
456 	   a false OPD here and pass this to the application.  */	\
457 	/* FIXME: Should be able to use P%, and LR RR to have the	\
458 	   the linker construct a proper OPD.  */			\
459 "	.section .data\n"						\
460 "__dl_fini_plabel:\n"							\
461 "	.word	_dl_fini\n"						\
462 "	.word	0xdeadbeef\n"						\
463 "	.previous\n"							\
464 									\
465 	/* %r3 contains a function pointer, we need to mask out the	\
466 	   lower bits and load the gp and jump address. */		\
467 "	depi	0,31,2,%r3\n"						\
468 "	ldw	0(%r3),%r2\n"						\
469 "	addil	LT'__dl_fini_plabel,%r19\n"				\
470 "	ldw	RT'__dl_fini_plabel(%r1),%r23\n"			\
471 "	stw	%r19,4(%r23)\n"						\
472 "	ldw	4(%r3),%r19\n"	/* load the object's gp */		\
473 "	bv	%r0(%r2)\n"						\
474 "	depi	2,31,2,%r23\n"	/* delay slot */			\
475 );
476 
477 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
478    a TLS variable, so references should not be allowed to define the value.
479    ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
480    of the main executable's symbols, as for a COPY reloc.  */
481 #if !defined RTLD_BOOTSTRAP
482 # define elf_machine_type_class(type)				\
483   ((((type) == R_PARISC_IPLT					\
484   || (type) == R_PARISC_EPLT					\
485   || (type) == R_PARISC_TLS_DTPMOD32				\
486   || (type) == R_PARISC_TLS_DTPOFF32				\
487   || (type) == R_PARISC_TLS_TPREL32)				\
488   * ELF_RTYPE_CLASS_PLT)					\
489   | (((type) == R_PARISC_COPY) * ELF_RTYPE_CLASS_COPY))
490 #else
491 #define elf_machine_type_class(type)				\
492  ((((type) == R_PARISC_IPLT					\
493    || (type) == R_PARISC_EPLT)					\
494    * ELF_RTYPE_CLASS_PLT)					\
495    | (((type) == R_PARISC_COPY) * ELF_RTYPE_CLASS_COPY))
496 #endif
497 
498 /* Used by the runtime in fixup to figure out if reloc is *really* PLT */
499 #define ELF_MACHINE_JMP_SLOT R_PARISC_IPLT
500 #define ELF_MACHINE_SIZEOF_JMP_SLOT PLT_ENTRY_SIZE
501 
502 /* Return the address of the entry point. */
503 #define ELF_MACHINE_START_ADDRESS(map, start)			\
504 ({								\
505 	ElfW(Addr) addr;					\
506 	DL_DT_FUNCTION_ADDRESS(map, start, static, addr)	\
507 	addr;							\
508 })
509 
510 /* We define an initialization functions.  This is called very early in
511  *    _dl_sysdep_start.  */
512 #define DL_PLATFORM_INIT dl_platform_init ()
513 
514 static inline void __attribute__ ((unused))
dl_platform_init(void)515 dl_platform_init (void)
516 {
517 	if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
518 	/* Avoid an empty string which would disturb us.  */
519 		GLRO(dl_platform) = NULL;
520 }
521 
522 #endif /* !dl_machine_h */
523 
524 /* These are only actually used where RESOLVE_MAP is defined, anyway. */
525 #ifdef RESOLVE_MAP
526 
527 #define reassemble_21(as21) \
528   (  (((as21) & 0x100000) >> 20) \
529    | (((as21) & 0x0ffe00) >> 8) \
530    | (((as21) & 0x000180) << 7) \
531    | (((as21) & 0x00007c) << 14) \
532    | (((as21) & 0x000003) << 12))
533 
534 #define reassemble_14(as14) \
535   (  (((as14) & 0x1fff) << 1) \
536    | (((as14) & 0x2000) >> 13))
537 
538 static void __attribute__((always_inline))
elf_machine_rela(struct link_map * map,struct r_scope_elem * scope[],const Elf32_Rela * reloc,const Elf32_Sym * sym,const struct r_found_version * version,void * const reloc_addr_arg,int skip_ifunc)539 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
540 		  const Elf32_Rela *reloc,
541 		  const Elf32_Sym *sym,
542 		  const struct r_found_version *version,
543 		  void *const reloc_addr_arg,
544 		  int skip_ifunc)
545 {
546   Elf32_Addr *const reloc_addr = reloc_addr_arg;
547   const Elf32_Sym *const refsym = sym;
548   unsigned long const r_type = ELF32_R_TYPE (reloc->r_info);
549   struct link_map *sym_map;
550   Elf32_Addr value;
551 
552   /* RESOLVE_MAP will return a null value for undefined syms, and
553      non-null for all other syms.  In particular, relocs with no
554      symbol (symbol index of zero), also called *ABS* relocs, will be
555      resolved to MAP.  (The first entry in a symbol table is all
556      zeros, and an all zero Elf32_Sym has a binding of STB_LOCAL.)
557      See RESOLVE_MAP definition in elf/dl-reloc.c  */
558 # ifdef RTLD_BOOTSTRAP
559   sym_map = map;
560 # else
561   sym_map = RESOLVE_MAP (map, scope, &sym, version, r_type);
562 # endif
563 
564   if (sym_map)
565     {
566       value = SYMBOL_ADDRESS (sym_map, sym, true);
567       value += reloc->r_addend;
568     }
569   else
570     value = 0;
571 
572   switch (r_type)
573     {
574     case R_PARISC_DIR32:
575       /* .eh_frame can have unaligned relocs.  */
576       if ((unsigned long) reloc_addr_arg & 3)
577 	{
578 	  char *rel_addr = (char *) reloc_addr_arg;
579 	  rel_addr[0] = value >> 24;
580 	  rel_addr[1] = value >> 16;
581 	  rel_addr[2] = value >> 8;
582 	  rel_addr[3] = value;
583 	  return;
584 	}
585       break;
586 
587     case R_PARISC_DIR21L:
588       {
589 	unsigned int insn = *(unsigned int *)reloc_addr;
590 	value = (SYMBOL_ADDRESS (sym_map, sym, true)
591 		 + ((reloc->r_addend + 0x1000) & -0x2000));
592 	value = value >> 11;
593 	insn = (insn &~ 0x1fffff) | reassemble_21 (value);
594 	*(unsigned int *)reloc_addr = insn;
595       }
596       return;
597 
598     case R_PARISC_DIR14R:
599       {
600 	unsigned int insn = *(unsigned int *)reloc_addr;
601 	value = ((SYMBOL_ADDRESS (sym_map, sym, true) & 0x7ff)
602 		 + (((reloc->r_addend & 0x1fff) ^ 0x1000) - 0x1000));
603 	insn = (insn &~ 0x3fff) | reassemble_14 (value);
604 	*(unsigned int *)reloc_addr = insn;
605       }
606       return;
607 
608     case R_PARISC_PLABEL32:
609       /* Easy rule: If there is a symbol and it is global, then we
610 	 need to make a dynamic function descriptor.  Otherwise we
611 	 have the address of a PLT slot for a local symbol which we
612 	 know to be unique. */
613       if (sym == NULL
614 	  || sym_map == NULL
615 	  || ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
616 	{
617 	  break;
618 	}
619       /* Set bit 30 to indicate to $$dyncall that this is a PLABEL.
620 	 We have to do this outside of the generic function descriptor
621 	 code, since it doesn't know about our requirement for setting
622 	 protection bits */
623       value = (Elf32_Addr)((unsigned int)_dl_make_fptr (sym_map, sym, value) | 2);
624       break;
625 
626     case R_PARISC_PLABEL21L:
627     case R_PARISC_PLABEL14R:
628       {
629 	unsigned int insn = *(unsigned int *)reloc_addr;
630 
631 	if (__builtin_expect (sym == NULL, 0))
632 	  break;
633 
634 	value = (Elf32_Addr)((unsigned int)_dl_make_fptr (sym_map, sym, value) | 2);
635 
636 	if (r_type == R_PARISC_PLABEL21L)
637 	  {
638 	    value >>= 11;
639 	    insn = (insn &~ 0x1fffff) | reassemble_21 (value);
640 	  }
641 	else
642 	  {
643 	    value &= 0x7ff;
644 	    insn = (insn &~ 0x3fff) | reassemble_14 (value);
645 	  }
646 
647 	*(unsigned int *)reloc_addr = insn;
648       }
649       return;
650 
651     case R_PARISC_IPLT:
652       if (__builtin_expect (sym_map != NULL, 1))
653 	{
654 	  elf_machine_fixup_plt (NULL, sym_map, NULL, NULL, reloc, reloc_addr,
655 				 DL_FIXUP_MAKE_VALUE(sym_map, value));
656 	}
657       else
658 	{
659 	  /* If we get here, it's a (weak) undefined sym.  */
660 	  elf_machine_fixup_plt (NULL, map, NULL, NULL, reloc, reloc_addr,
661 				 DL_FIXUP_MAKE_VALUE(map, value));
662 	}
663       return;
664 
665     case R_PARISC_COPY:
666       if (__builtin_expect (sym == NULL, 0))
667 	/* This can happen in trace mode if an object could not be
668 	   found.  */
669 	break;
670       if (__builtin_expect (sym->st_size > refsym->st_size, 0)
671 	  || (__builtin_expect (sym->st_size < refsym->st_size, 0)
672 	      && __builtin_expect (GLRO(dl_verbose), 0)))
673 	{
674 	  const char *strtab;
675 
676 	  strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
677 	  _dl_error_printf ("%s: Symbol `%s' has different size in shared object, "
678 			    "consider re-linking\n",
679 			    RTLD_PROGNAME, strtab + refsym->st_name);
680 	}
681       memcpy (reloc_addr_arg, (void *) value,
682 	      MIN (sym->st_size, refsym->st_size));
683       return;
684 
685 #if !defined RTLD_BOOTSTRAP
686     case R_PARISC_TLS_DTPMOD32:
687       value = sym_map->l_tls_modid;
688       break;
689 
690     case R_PARISC_TLS_DTPOFF32:
691       /* During relocation all TLS symbols are defined and used.
692 	 Therefore the offset is already correct.  */
693       if (sym != NULL)
694 	*reloc_addr = sym->st_value + reloc->r_addend;
695       return;
696 
697     case R_PARISC_TLS_TPREL32:
698       /* The offset is negative, forward from the thread pointer */
699       if (sym != NULL)
700 	{
701 	  CHECK_STATIC_TLS (map, sym_map);
702 	  value = sym_map->l_tls_offset + sym->st_value + reloc->r_addend;
703 	}
704       break;
705 #endif	/* use TLS */
706 
707     case R_PARISC_NONE:	/* Alright, Wilbur. */
708       return;
709 
710     default:
711       _dl_reloc_bad_type (map, r_type, 0);
712     }
713 
714   *reloc_addr = value;
715 }
716 
717 /* hppa doesn't have an R_PARISC_RELATIVE reloc, but uses relocs with
718    ELF32_R_SYM (info) == 0 for a similar purpose.  */
719 static void __attribute__((always_inline))
elf_machine_rela_relative(Elf32_Addr l_addr,const Elf32_Rela * reloc,void * const reloc_addr_arg)720 elf_machine_rela_relative (Elf32_Addr l_addr,
721 			   const Elf32_Rela *reloc,
722 			   void *const reloc_addr_arg)
723 {
724   unsigned long const r_type = ELF32_R_TYPE (reloc->r_info);
725   Elf32_Addr *const reloc_addr = reloc_addr_arg;
726   static char msgbuf[] = { "Unknown" };
727   struct link_map map;
728   Elf32_Addr value;
729 
730   value = l_addr + reloc->r_addend;
731 
732   if (ELF32_R_SYM (reloc->r_info) != 0){
733     _dl_error_printf ("%s: In elf_machine_rela_relative "
734 		      "ELF32_R_SYM (reloc->r_info) != 0. Aborting.",
735 		      RTLD_PROGNAME);
736     ABORT_INSTRUCTION;  /* Crash. */
737   }
738 
739   switch (r_type)
740     {
741     case R_PARISC_DIR32:
742       /* .eh_frame can have unaligned relocs.  */
743       if ((unsigned long) reloc_addr_arg & 3)
744 	{
745 	  char *rel_addr = (char *) reloc_addr_arg;
746 	  rel_addr[0] = value >> 24;
747 	  rel_addr[1] = value >> 16;
748 	  rel_addr[2] = value >> 8;
749 	  rel_addr[3] = value;
750 	  return;
751 	}
752       break;
753 
754     case R_PARISC_PLABEL32:
755       break;
756 
757     case R_PARISC_IPLT: /* elf_machine_runtime_setup already set gp */
758       break;
759 
760     case R_PARISC_NONE:
761       return;
762 
763     default: /* Bad reloc, map unknown (really it's the current map) */
764       map.l_name = msgbuf;
765       _dl_reloc_bad_type (&map, r_type, 0);
766       return;
767     }
768 
769   *reloc_addr = value;
770 }
771 
772 static void __attribute__((always_inline))
elf_machine_lazy_rel(struct link_map * map,struct r_scope_elem * scope[],Elf32_Addr l_addr,const Elf32_Rela * reloc,int skip_ifunc)773 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
774 		      Elf32_Addr l_addr, const Elf32_Rela *reloc,
775 		      int skip_ifunc)
776 {
777   /* We don't have anything to do here.  elf_machine_runtime_setup has
778      done all the relocs already.  */
779 }
780 
781 #endif /* RESOLVE_MAP */
782