OSDN Git Service

1a86728a34508d75e27f158c0a7c4932316d2af8
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / frv / crtreloc.c
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, write to the Free Software Foundation, Inc., 675 Mass Ave,
27 Cambridge, MA 02139, USA.  */
28
29 #include <sys/types.h>
30 #include <link.h>
31
32 /* This file is to be compiled into crt object files, to enable
33    executables to easily self-relocate.  */
34
35 /* Compute the runtime address of pointer in the range [p,e), and then
36    map the pointer pointed by it.  */
37 inline static void ***
38 reloc_range_indirect (void ***p, void ***e,
39                       const struct elf32_fdpic_loadmap *map)
40 {
41   while (p < e)
42     {
43       void *ptr = __reloc_pointer (*p, map);
44       if (ptr)
45         {
46           void *pt;
47           if ((long)ptr & 3)
48             __builtin_memcpy(&pt, ptr, sizeof(pt));
49           else
50             pt = *(void**)ptr;
51           pt = __reloc_pointer (pt, map);
52           if ((long)ptr & 3)
53             __builtin_memcpy(ptr, &pt, sizeof(pt));
54           else
55             *(void**)ptr = pt;
56         }
57       p++;
58     }
59   return p;
60 }
61
62 /* Call __reloc_range_indirect for the given range except for the last
63    entry, whose contents are only relocated.  It's expected to hold
64    the GOT value.  */
65 void* attribute_hidden
66 __self_reloc (const struct elf32_fdpic_loadmap *map,
67               void ***p, void ***e)
68 {
69   p = reloc_range_indirect (p, e-1, map);
70
71   if (p >= e)
72     return (void*)-1;
73   
74   return __reloc_pointer (*p, map);
75 }
76
77 #if 0
78 /* These are other functions that might be useful, but that we don't
79    need.  */
80
81 /* Remap pointers in [p,e).  */
82 inline static void**
83 reloc_range (void **p, void **e,
84              const struct elf32_fdpic_loadmap *map)
85 {
86   while (p < e)
87     {
88       *p = __reloc_pointer (*p, map);
89       p++;
90     }
91   return p;
92 }
93
94 /* Remap p, adjust e by the same offset, then map the pointers in the
95    range determined by them.  */
96 void attribute_hidden
97 __reloc_range (const struct elf32_fdpic_loadmap *map,
98                void **p, void **e)
99 {
100   void **old = p;
101
102   p = __reloc_pointer (p, map);
103   e += p - old;
104   reloc_range (p, e, map);
105 }
106
107 /* Remap p, adjust e by the same offset, then map pointers referenced
108    by the (unadjusted) pointers in the range.  Return the relocated
109    value of the last pointer in the range.  */
110 void* attribute_hidden
111 __reloc_range_indirect (const struct elf32_fdpic_loadmap *map,
112                         void ***p, void ***e)
113 {
114   void ***old = p;
115
116   p = __reloc_pointer (p, map);
117   e += p - old;
118   return reloc_range_indirect (p, e, map);
119 }
120 #endif