1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2    written by Alexandre Oliva <aoliva@redhat.com>
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 License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9 
10 In addition to the permissions in the GNU Lesser General Public
11 License, the Free Software Foundation gives you unlimited
12 permission to link the compiled version of this file with other
13 programs, and to distribute those programs without any restriction
14 coming from the use of this file.  (The GNU Lesser General Public
15 License restrictions do apply in other respects; for example, they
16 cover modification of the file, and distribution when not linked
17 into another program.)
18 
19 The GNU C Library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 Library General Public License for more details.
23 
24 You should have received a copy of the GNU Lesser General Public
25 License along with the GNU C Library; see the file COPYING.LIB.  If
26 not, see <http://www.gnu.org/licenses/>.  */
27 
28 #ifdef __FRV_FDPIC__
29 
30 #include <sys/types.h>
31 #include <link.h>
32 
33 /* This file is to be compiled into crt object files, to enable
34    executables to easily self-relocate.  */
35 
36 /* Compute the runtime address of pointer in the range [p,e), and then
37    map the pointer pointed by it.  */
38 static __always_inline void ***
reloc_range_indirect(void *** p,void *** e,const struct elf32_fdpic_loadmap * map)39 reloc_range_indirect (void ***p, void ***e,
40 		      const struct elf32_fdpic_loadmap *map)
41 {
42   while (p < e)
43     {
44       void *ptr = __reloc_pointer (*p, map);
45       if (ptr)
46 	{
47 	  void *pt;
48 	  if ((long)ptr & 3)
49 	    __builtin_memcpy(&pt, ptr, sizeof(pt));
50 	  else
51 	    pt = *(void**)ptr;
52 	  pt = __reloc_pointer (pt, map);
53 	  if ((long)ptr & 3)
54 	    __builtin_memcpy(ptr, &pt, sizeof(pt));
55 	  else
56 	    *(void**)ptr = pt;
57 	}
58       p++;
59     }
60   return p;
61 }
62 
63 /* Call __reloc_range_indirect for the given range except for the last
64    entry, whose contents are only relocated.  It's expected to hold
65    the GOT value.  */
66 attribute_hidden void*
__self_reloc(const struct elf32_fdpic_loadmap * map,void *** p,void *** e)67 __self_reloc (const struct elf32_fdpic_loadmap *map,
68 	      void ***p, void ***e)
69 {
70   p = reloc_range_indirect (p, e-1, map);
71 
72   if (p >= e)
73     return (void*)-1;
74 
75   return __reloc_pointer (*p, map);
76 }
77 #endif /* __FRV_FDPIC__ */
78