1 /* Machine-dependent ELF dynamic relocation inline functions.  PA-RISC version.
2    Copyright (C) 1995-2021 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   /* If we don't have a PLT we can just skip all this... */
180   if (__builtin_expect (l->l_info[DT_JMPREL] == NULL,0))
181     return lazy;
182 
183   /* All paths use these values */
184   l_addr = l->l_addr;
185   jmprel = D_PTR(l, l_info[DT_JMPREL]);
186   end_jmprel = jmprel + l->l_info[DT_PLTRELSZ]->d_un.d_val;
187 
188   extern void _dl_runtime_resolve (void);
189   extern void _dl_runtime_profile (void);
190 
191   /* Linking lazily */
192   if (lazy)
193     {
194       /* FIXME: Search for the got, but backwards through the relocs, technically we should
195 	 find it on the first try. However, assuming the relocs got out of order the
196 	 routine is made a bit more robust by searching them all in case of failure. */
197       for (iplt = (end_jmprel - sizeof (Elf32_Rela)); iplt >= jmprel; iplt -= sizeof (Elf32_Rela))
198 	{
199 
200 	  reloc = (const Elf32_Rela *) iplt;
201 	  r_type = ELF32_R_TYPE (reloc->r_info);
202 	  r_sym = ELF32_R_SYM (reloc->r_info);
203 
204 	  got = (Elf32_Addr *) (reloc->r_offset + l_addr + PLT_ENTRY_SIZE + SIZEOF_PLT_STUB);
205 
206 	  /* If we aren't an IPLT, and we aren't NONE then it's a bad reloc */
207 	  if (__builtin_expect (r_type != R_PARISC_IPLT, 0))
208 	    {
209 	      if (__builtin_expect (r_type != R_PARISC_NONE, 0))
210 		_dl_reloc_bad_type (l, r_type, 1);
211 	      continue;
212 	    }
213 
214 	  /* Check for the plt_stub that binutils placed here for us
215 	     to use with _dl_runtime_resolve  */
216 	  if (got[-2] != sig.i[0] || got[-1] != sig.i[1])
217 	    {
218 	      got = NULL; /* Not the stub... keep looking */
219 	    }
220 	  else
221 	    {
222 	      /* Found the GOT! */
223 	      register Elf32_Addr ltp __asm__ ("%r19");
224 
225 	      /* Identify this shared object. Second entry in the got. */
226 	      got[1] = (Elf32_Addr) l;
227 
228 	      /* This function will be called to perform the relocation. */
229 	      if (__builtin_expect (!profile, 1))
230 		{
231 		  /* If a static application called us, then _dl_runtime_resolve is not
232 		     a function descriptor, but the *real* address of the function... */
233 		  if((unsigned long) &_dl_runtime_resolve & 3)
234 		    {
235 		      got[-2] = (Elf32_Addr) ((struct fdesc *)
236 				  ((unsigned long) &_dl_runtime_resolve & ~3))->ip;
237 		    }
238 		  else
239 		    {
240 		      /* Static executable! */
241 		      got[-2] = (Elf32_Addr) &_dl_runtime_resolve;
242 		    }
243 		}
244 	      else
245 		{
246 		  if (GLRO(dl_profile) != NULL
247 		      && _dl_name_match_p (GLRO(dl_profile), l))
248 		    {
249 		      /* This is the object we are looking for.  Say that
250 			 we really want profiling and the timers are
251 			 started.  */
252 		      GL(dl_profile_map) = l;
253 		    }
254 
255 		  if((unsigned long) &_dl_runtime_profile & 3)
256 		    {
257 		      got[-2] = (Elf32_Addr) ((struct fdesc *)
258 				  ((unsigned long) &_dl_runtime_profile & ~3))->ip;
259 		    }
260 		  else
261 		    {
262 		      /* Static executable */
263 		      got[-2] = (Elf32_Addr) &_dl_runtime_profile;
264 		    }
265 		}
266 	      /* Plunk in the gp of this function descriptor so we
267 		 can make the call to _dl_runtime_xxxxxx */
268 	      got[-1] = ltp;
269 	      break;
270 	      /* Done looking for the GOT, and stub is setup */
271 	    } /* else we found the GOT */
272 	} /* for, walk the relocs backwards */
273 
274       if(!got)
275 	return 0; /* No lazy linking for you! */
276 
277       /* Process all the relocs, now that we know the GOT... */
278       for (iplt = jmprel; iplt < end_jmprel; iplt += sizeof (Elf32_Rela))
279 	{
280 	  reloc = (const Elf32_Rela *) iplt;
281 	  r_type = ELF32_R_TYPE (reloc->r_info);
282 	  r_sym = ELF32_R_SYM (reloc->r_info);
283 
284 	  if (__builtin_expect (r_type == R_PARISC_IPLT, 1))
285 	    {
286 	      fptr = (struct fdesc *) (reloc->r_offset + l_addr);
287 	      if (r_sym != 0)
288 		{
289 		  /* Relocate the pointer to the stub.  */
290 		  fptr->ip = (Elf32_Addr) got - GOT_FROM_PLT_STUB;
291 
292 		  /* Instead of the LTP value, we put the reloc offset
293 		     here.  The trampoline code will load the proper
294 		     LTP and pass the reloc offset to the fixup
295 		     function.  */
296 		  fptr->gp = (iplt - jmprel) | PA_GP_RELOC;
297 		} /* r_sym != 0 */
298 	      else
299 		{
300 		  /* Relocate this *ABS* entry.  */
301 		  fptr->ip = reloc->r_addend + l_addr;
302 		  fptr->gp = D_PTR (l, l_info[DT_PLTGOT]);
303 		}
304 	    } /* r_type == R_PARISC_IPLT */
305 	} /* for all the relocations */
306     } /* if lazy */
307   else
308     {
309       for (iplt = jmprel; iplt < end_jmprel; iplt += sizeof (Elf32_Rela))
310 	{
311 	  reloc = (const Elf32_Rela *) iplt;
312 	  r_type = ELF32_R_TYPE (reloc->r_info);
313 	  r_sym = ELF32_R_SYM (reloc->r_info);
314 
315 	  if (__builtin_expect ((r_type == R_PARISC_IPLT) && (r_sym == 0), 1))
316 	    {
317 	      fptr = (struct fdesc *) (reloc->r_offset + l_addr);
318 	      /* Relocate this *ABS* entry, set only the gp, the rest is set later
319 		 when elf_machine_rela_relative is called (WITHOUT the linkmap)  */
320 	      fptr->gp = D_PTR (l, l_info[DT_PLTGOT]);
321 	    } /* r_type == R_PARISC_IPLT */
322 	} /* for all the relocations */
323     }
324   return lazy;
325 }
326 
327 
328 /* Names of the architecture-specific auditing callback functions.  */
329 #define ARCH_LA_PLTENTER hppa_gnu_pltenter
330 #define ARCH_LA_PLTEXIT hppa_gnu_pltexit
331 
332 /* Adjust DL_STACK_END to get value we want in __libc_stack_end.  */
333 #define DL_STACK_END(cookie) \
334   ((void *) (((long) (cookie)) + 0x160))
335 
336 /* Initial entry point code for the dynamic linker.
337    The C function `_dl_start' is the real entry point;
338    its return value is the user program's entry point.  */
339 
340 #define RTLD_START \
341 /* Set up dp for any non-PIC lib constructors that may be called.  */	\
342 static struct link_map * __attribute__((used))				\
343 set_dp (struct link_map *map)						\
344 {									\
345   register Elf32_Addr dp asm ("%r27");					\
346   dp = D_PTR (map, l_info[DT_PLTGOT]);					\
347   asm volatile ("" : : "r" (dp));					\
348   return map;								\
349 }									\
350 									\
351 asm (									\
352 "	.text\n"							\
353 "	.globl _start\n"						\
354 "	.type _start,@function\n"					\
355 "_start:\n"								\
356 	/* The kernel does not give us an initial stack frame. */	\
357 "	ldo	64(%sp),%sp\n"						\
358 	/* Save the relevant arguments (yes, those are the correct	\
359 	   registers, the kernel is weird) in their stack slots. */	\
360 "	stw	%r25,-40(%sp)\n" /* argc */				\
361 "	stw	%r24,-44(%sp)\n" /* argv */				\
362 									\
363 	/* We need the LTP, and we need it now.				\
364 	   $PIC_pcrel$0 points 8 bytes past the current instruction,	\
365 	   just like a branch reloc.  This sequence gets us the		\
366 	   runtime address of _DYNAMIC. */				\
367 "	bl	0f,%r19\n"						\
368 "	addil	L'_DYNAMIC - ($PIC_pcrel$0 - 1),%r19\n"			\
369 "0:	ldo	R'_DYNAMIC - ($PIC_pcrel$0 - 5)(%r1),%r26\n"		\
370 									\
371 	/* The link time address is stored in the first entry of the	\
372 	   GOT.  */							\
373 "	addil	L'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 9),%r19\n"	\
374 "	ldw	R'_GLOBAL_OFFSET_TABLE_ - ($PIC_pcrel$0 - 13)(%r1),%r20\n" \
375 									\
376 "	sub	%r26,%r20,%r20\n"	/* Calculate load offset */	\
377 									\
378 	/* Rummage through the dynamic entries, looking for		\
379 	   DT_PLTGOT.  */						\
380 "	ldw,ma	8(%r26),%r19\n"						\
381 "1:	cmpib,=,n 3,%r19,2f\n"	/* tag == DT_PLTGOT? */			\
382 "	cmpib,<>,n 0,%r19,1b\n"						\
383 "	ldw,ma	8(%r26),%r19\n"						\
384 									\
385 	/* Uh oh!  We didn't find one.  Abort. */			\
386 "	iitlbp	%r0,(%sr0,%r0)\n"					\
387 									\
388 "2:	ldw	-4(%r26),%r19\n"	/* Found it, load value. */	\
389 "	add	%r19,%r20,%r19\n"	/* And add the load offset. */	\
390 									\
391 	/* Our initial stack layout is rather different from everyone	\
392 	   else's due to the unique PA-RISC ABI.  As far as I know it	\
393 	   looks like this:						\
394 									\
395 	   -----------------------------------  (this frame created above) \
396 	   |         32 bytes of magic       |				\
397 	   |---------------------------------|				\
398 	   | 32 bytes argument/sp save area  |				\
399 	   |---------------------------------|  ((current->mm->env_end)	\
400 	   |         N bytes of slack        |	 + 63 & ~63)		\
401 	   |---------------------------------|				\
402 	   |      envvar and arg strings     |				\
403 	   |---------------------------------|				\
404 	   |	    ELF auxiliary info	     |				\
405 	   |         (up to 28 words)        |				\
406 	   |---------------------------------|				\
407 	   |  Environment variable pointers  |				\
408 	   |         upwards to NULL	     |				\
409 	   |---------------------------------|				\
410 	   |        Argument pointers        |				\
411 	   |         upwards to NULL	     |				\
412 	   |---------------------------------|				\
413 	   |          argc (1 word)          |				\
414 	   -----------------------------------				\
415 									\
416 	  So, obviously, we can't just pass %sp to _dl_start.  That's	\
417 	  okay, argv-4 will do just fine.				\
418 									\
419 	  The pleasant part of this is that if we need to skip		\
420 	  arguments we can just decrement argc and move argv, because	\
421 	  the stack pointer is utterly unrelated to the location of	\
422 	  the environment and argument vectors. */			\
423 									\
424 	/* This is always within range so we'll be okay. */		\
425 "	bl	_dl_start,%rp\n"					\
426 "	ldo	-4(%r24),%r26\n"					\
427 									\
428 "	.globl _dl_start_user\n"					\
429 "	.type _dl_start_user,@function\n"				\
430 "_dl_start_user:\n"							\
431 	/* Save the entry point in %r3. */				\
432 "	copy	%ret0,%r3\n"						\
433 									\
434 	/* See if we were called as a command with the executable file	\
435 	   name as an extra leading argument. */			\
436 "	addil	LT'_dl_skip_args,%r19\n"				\
437 "	ldw	RT'_dl_skip_args(%r1),%r20\n"				\
438 "	ldw	0(%r20),%r20\n"						\
439 									\
440 "	ldw	-40(%sp),%r25\n"	/* argc */			\
441 "	comib,=	0,%r20,.Lnofix\n"	/* FIXME: Mispredicted branch */\
442 "	ldw	-44(%sp),%r24\n"	/* argv (delay slot) */		\
443 									\
444 "	sub	%r25,%r20,%r25\n"					\
445 "	stw	%r25,-40(%sp)\n"					\
446 "	sh2add	%r20,%r24,%r24\n"					\
447 "	stw	%r24,-44(%sp)\n"					\
448 									\
449 ".Lnofix:\n"								\
450 "	addil	LT'_rtld_local,%r19\n"					\
451 "	ldw	RT'_rtld_local(%r1),%r26\n"				\
452 "	bl	set_dp, %r2\n"						\
453 "	ldw	0(%r26),%r26\n"						\
454 									\
455 	/* Call _dl_init(_dl_loaded, argc, argv, envp). */		\
456 "	copy	%r28,%r26\n"						\
457 									\
458 	/* envp = argv + argc + 1 */					\
459 "	sh2add	%r25,%r24,%r23\n"					\
460 "	bl	_dl_init,%r2\n"						\
461 "	ldo	4(%r23),%r23\n"	/* delay slot */			\
462 									\
463 	/* Reload argc, argv to the registers start.S expects.  */	\
464 "	ldw	-40(%sp),%r25\n"					\
465 "	ldw	-44(%sp),%r24\n"					\
466 									\
467 	/* _dl_fini is a local function in the loader, so we construct	\
468 	   a false OPD here and pass this to the application.  */	\
469 	/* FIXME: Should be able to use P%, and LR RR to have the	\
470 	   the linker construct a proper OPD.  */			\
471 "	.section .data\n"						\
472 "__dl_fini_plabel:\n"							\
473 "	.word	_dl_fini\n"						\
474 "	.word	0xdeadbeef\n"						\
475 "	.previous\n"							\
476 									\
477 	/* %r3 contains a function pointer, we need to mask out the	\
478 	   lower bits and load the gp and jump address. */		\
479 "	depi	0,31,2,%r3\n"						\
480 "	ldw	0(%r3),%r2\n"						\
481 "	addil	LT'__dl_fini_plabel,%r19\n"				\
482 "	ldw	RT'__dl_fini_plabel(%r1),%r23\n"			\
483 "	stw	%r19,4(%r23)\n"						\
484 "	ldw	4(%r3),%r19\n"	/* load the object's gp */		\
485 "	bv	%r0(%r2)\n"						\
486 "	depi	2,31,2,%r23\n"	/* delay slot */			\
487 );
488 
489 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
490    a TLS variable, so references should not be allowed to define the value.
491    ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
492    of the main executable's symbols, as for a COPY reloc.  */
493 #if !defined RTLD_BOOTSTRAP
494 # define elf_machine_type_class(type)				\
495   ((((type) == R_PARISC_IPLT					\
496   || (type) == R_PARISC_EPLT					\
497   || (type) == R_PARISC_TLS_DTPMOD32				\
498   || (type) == R_PARISC_TLS_DTPOFF32				\
499   || (type) == R_PARISC_TLS_TPREL32)				\
500   * ELF_RTYPE_CLASS_PLT)					\
501   | (((type) == R_PARISC_COPY) * ELF_RTYPE_CLASS_COPY))
502 #else
503 #define elf_machine_type_class(type)				\
504  ((((type) == R_PARISC_IPLT					\
505    || (type) == R_PARISC_EPLT)					\
506    * ELF_RTYPE_CLASS_PLT)					\
507    | (((type) == R_PARISC_COPY) * ELF_RTYPE_CLASS_COPY))
508 #endif
509 
510 /* Used by the runtime in fixup to figure out if reloc is *really* PLT */
511 #define ELF_MACHINE_JMP_SLOT R_PARISC_IPLT
512 #define ELF_MACHINE_SIZEOF_JMP_SLOT PLT_ENTRY_SIZE
513 
514 /* Return the address of the entry point. */
515 #define ELF_MACHINE_START_ADDRESS(map, start)			\
516 ({								\
517 	ElfW(Addr) addr;					\
518 	DL_DT_FUNCTION_ADDRESS(map, start, static, addr)	\
519 	addr;							\
520 })
521 
522 /* We define an initialization functions.  This is called very early in
523  *    _dl_sysdep_start.  */
524 #define DL_PLATFORM_INIT dl_platform_init ()
525 
526 static inline void __attribute__ ((unused))
dl_platform_init(void)527 dl_platform_init (void)
528 {
529 	if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
530 	/* Avoid an empty string which would disturb us.  */
531 		GLRO(dl_platform) = NULL;
532 }
533 
534 #endif /* !dl_machine_h */
535 
536 /* These are only actually used where RESOLVE_MAP is defined, anyway. */
537 #ifdef RESOLVE_MAP
538 
539 #define reassemble_21(as21) \
540   (  (((as21) & 0x100000) >> 20) \
541    | (((as21) & 0x0ffe00) >> 8) \
542    | (((as21) & 0x000180) << 7) \
543    | (((as21) & 0x00007c) << 14) \
544    | (((as21) & 0x000003) << 12))
545 
546 #define reassemble_14(as14) \
547   (  (((as14) & 0x1fff) << 1) \
548    | (((as14) & 0x2000) >> 13))
549 
550 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)551 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
552 		  const Elf32_Rela *reloc,
553 		  const Elf32_Sym *sym,
554 		  const struct r_found_version *version,
555 		  void *const reloc_addr_arg,
556 		  int skip_ifunc)
557 {
558   Elf32_Addr *const reloc_addr = reloc_addr_arg;
559   const Elf32_Sym *const refsym = sym;
560   unsigned long const r_type = ELF32_R_TYPE (reloc->r_info);
561   struct link_map *sym_map;
562   Elf32_Addr value;
563 
564 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC && !defined SHARED
565   /* This is defined in rtld.c, but nowhere in the static libc.a; make the
566      reference weak so static programs can still link.  This declaration
567      cannot be done when compiling rtld.c (i.e.  #ifdef RTLD_BOOTSTRAP)
568      because rtld.c contains the common defn for _dl_rtld_map, which is
569      incompatible with a weak decl in the same file.  */
570   weak_extern (GL(dl_rtld_map));
571 # endif
572 
573   /* RESOLVE_MAP will return a null value for undefined syms, and
574      non-null for all other syms.  In particular, relocs with no
575      symbol (symbol index of zero), also called *ABS* relocs, will be
576      resolved to MAP.  (The first entry in a symbol table is all
577      zeros, and an all zero Elf32_Sym has a binding of STB_LOCAL.)
578      See RESOLVE_MAP definition in elf/dl-reloc.c  */
579 # ifdef RTLD_BOOTSTRAP
580   sym_map = map;
581 # else
582   sym_map = RESOLVE_MAP (map, scope, &sym, version, r_type);
583 # endif
584 
585   if (sym_map)
586     {
587       value = SYMBOL_ADDRESS (sym_map, sym, true);
588       value += reloc->r_addend;
589     }
590   else
591     value = 0;
592 
593   switch (r_type)
594     {
595     case R_PARISC_DIR32:
596       /* .eh_frame can have unaligned relocs.  */
597       if ((unsigned long) reloc_addr_arg & 3)
598 	{
599 	  char *rel_addr = (char *) reloc_addr_arg;
600 	  rel_addr[0] = value >> 24;
601 	  rel_addr[1] = value >> 16;
602 	  rel_addr[2] = value >> 8;
603 	  rel_addr[3] = value;
604 	  return;
605 	}
606       break;
607 
608     case R_PARISC_DIR21L:
609       {
610 	unsigned int insn = *(unsigned int *)reloc_addr;
611 	value = (SYMBOL_ADDRESS (sym_map, sym, true)
612 		 + ((reloc->r_addend + 0x1000) & -0x2000));
613 	value = value >> 11;
614 	insn = (insn &~ 0x1fffff) | reassemble_21 (value);
615 	*(unsigned int *)reloc_addr = insn;
616       }
617       return;
618 
619     case R_PARISC_DIR14R:
620       {
621 	unsigned int insn = *(unsigned int *)reloc_addr;
622 	value = ((SYMBOL_ADDRESS (sym_map, sym, true) & 0x7ff)
623 		 + (((reloc->r_addend & 0x1fff) ^ 0x1000) - 0x1000));
624 	insn = (insn &~ 0x3fff) | reassemble_14 (value);
625 	*(unsigned int *)reloc_addr = insn;
626       }
627       return;
628 
629     case R_PARISC_PLABEL32:
630       /* Easy rule: If there is a symbol and it is global, then we
631 	 need to make a dynamic function descriptor.  Otherwise we
632 	 have the address of a PLT slot for a local symbol which we
633 	 know to be unique. */
634       if (sym == NULL
635 	  || sym_map == NULL
636 	  || ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
637 	{
638 	  break;
639 	}
640       /* Set bit 30 to indicate to $$dyncall that this is a PLABEL.
641 	 We have to do this outside of the generic function descriptor
642 	 code, since it doesn't know about our requirement for setting
643 	 protection bits */
644       value = (Elf32_Addr)((unsigned int)_dl_make_fptr (sym_map, sym, value) | 2);
645       break;
646 
647     case R_PARISC_PLABEL21L:
648     case R_PARISC_PLABEL14R:
649       {
650 	unsigned int insn = *(unsigned int *)reloc_addr;
651 
652 	if (__builtin_expect (sym == NULL, 0))
653 	  break;
654 
655 	value = (Elf32_Addr)((unsigned int)_dl_make_fptr (sym_map, sym, value) | 2);
656 
657 	if (r_type == R_PARISC_PLABEL21L)
658 	  {
659 	    value >>= 11;
660 	    insn = (insn &~ 0x1fffff) | reassemble_21 (value);
661 	  }
662 	else
663 	  {
664 	    value &= 0x7ff;
665 	    insn = (insn &~ 0x3fff) | reassemble_14 (value);
666 	  }
667 
668 	*(unsigned int *)reloc_addr = insn;
669       }
670       return;
671 
672     case R_PARISC_IPLT:
673       if (__builtin_expect (sym_map != NULL, 1))
674 	{
675 	  elf_machine_fixup_plt (NULL, sym_map, NULL, NULL, reloc, reloc_addr,
676 				 DL_FIXUP_MAKE_VALUE(sym_map, value));
677 	}
678       else
679 	{
680 	  /* If we get here, it's a (weak) undefined sym.  */
681 	  elf_machine_fixup_plt (NULL, map, NULL, NULL, reloc, reloc_addr,
682 				 DL_FIXUP_MAKE_VALUE(map, value));
683 	}
684       return;
685 
686     case R_PARISC_COPY:
687       if (__builtin_expect (sym == NULL, 0))
688 	/* This can happen in trace mode if an object could not be
689 	   found.  */
690 	break;
691       if (__builtin_expect (sym->st_size > refsym->st_size, 0)
692 	  || (__builtin_expect (sym->st_size < refsym->st_size, 0)
693 	      && __builtin_expect (GLRO(dl_verbose), 0)))
694 	{
695 	  const char *strtab;
696 
697 	  strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
698 	  _dl_error_printf ("%s: Symbol `%s' has different size in shared object, "
699 			    "consider re-linking\n",
700 			    RTLD_PROGNAME, strtab + refsym->st_name);
701 	}
702       memcpy (reloc_addr_arg, (void *) value,
703 	      MIN (sym->st_size, refsym->st_size));
704       return;
705 
706 #if !defined RTLD_BOOTSTRAP
707     case R_PARISC_TLS_DTPMOD32:
708       value = sym_map->l_tls_modid;
709       break;
710 
711     case R_PARISC_TLS_DTPOFF32:
712       /* During relocation all TLS symbols are defined and used.
713 	 Therefore the offset is already correct.  */
714       if (sym != NULL)
715 	*reloc_addr = sym->st_value + reloc->r_addend;
716       return;
717 
718     case R_PARISC_TLS_TPREL32:
719       /* The offset is negative, forward from the thread pointer */
720       if (sym != NULL)
721 	{
722 	  CHECK_STATIC_TLS (map, sym_map);
723 	  value = sym_map->l_tls_offset + sym->st_value + reloc->r_addend;
724 	}
725       break;
726 #endif	/* use TLS */
727 
728     case R_PARISC_NONE:	/* Alright, Wilbur. */
729       return;
730 
731     default:
732       _dl_reloc_bad_type (map, r_type, 0);
733     }
734 
735   *reloc_addr = value;
736 }
737 
738 /* hppa doesn't have an R_PARISC_RELATIVE reloc, but uses relocs with
739    ELF32_R_SYM (info) == 0 for a similar purpose.  */
740 static void __attribute__((always_inline))
elf_machine_rela_relative(Elf32_Addr l_addr,const Elf32_Rela * reloc,void * const reloc_addr_arg)741 elf_machine_rela_relative (Elf32_Addr l_addr,
742 			   const Elf32_Rela *reloc,
743 			   void *const reloc_addr_arg)
744 {
745   unsigned long const r_type = ELF32_R_TYPE (reloc->r_info);
746   Elf32_Addr *const reloc_addr = reloc_addr_arg;
747   static char msgbuf[] = { "Unknown" };
748   struct link_map map;
749   Elf32_Addr value;
750 
751   value = l_addr + reloc->r_addend;
752 
753   if (ELF32_R_SYM (reloc->r_info) != 0){
754     _dl_error_printf ("%s: In elf_machine_rela_relative "
755 		      "ELF32_R_SYM (reloc->r_info) != 0. Aborting.",
756 		      RTLD_PROGNAME);
757     ABORT_INSTRUCTION;  /* Crash. */
758   }
759 
760   switch (r_type)
761     {
762     case R_PARISC_DIR32:
763       /* .eh_frame can have unaligned relocs.  */
764       if ((unsigned long) reloc_addr_arg & 3)
765 	{
766 	  char *rel_addr = (char *) reloc_addr_arg;
767 	  rel_addr[0] = value >> 24;
768 	  rel_addr[1] = value >> 16;
769 	  rel_addr[2] = value >> 8;
770 	  rel_addr[3] = value;
771 	  return;
772 	}
773       break;
774 
775     case R_PARISC_PLABEL32:
776       break;
777 
778     case R_PARISC_IPLT: /* elf_machine_runtime_setup already set gp */
779       break;
780 
781     case R_PARISC_NONE:
782       return;
783 
784     default: /* Bad reloc, map unknown (really it's the current map) */
785       map.l_name = msgbuf;
786       _dl_reloc_bad_type (&map, r_type, 0);
787       return;
788     }
789 
790   *reloc_addr = value;
791 }
792 
793 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)794 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
795 		      Elf32_Addr l_addr, const Elf32_Rela *reloc,
796 		      int skip_ifunc)
797 {
798   /* We don't have anything to do here.  elf_machine_runtime_setup has
799      done all the relocs already.  */
800 }
801 
802 #endif /* RESOLVE_MAP */
803