Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / gcc / config / pa / fptr.c
blob18d90ae74437054335585121b86ea7fdc713c466
1 /* Subroutine for function pointer canonicalization on PA-RISC with ELF32.
2 Copyright 2002, 2003, 2004, 2007, 2009 Free Software Foundation, Inc.
3 Contributed by John David Anglin (dave.anglin@nrc.ca).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 for more details.
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
31 /* WARNING: The code is this function depends on internal and undocumented
32 details of the GNU linker and dynamic loader as implemented for parisc
33 linux. */
35 /* This MUST match the defines sysdeps/hppa/dl-machine.h and
36 bfd/elf32-hppa.c. */
37 #define GOT_FROM_PLT_STUB (4*4)
39 /* List of byte offsets in _dl_runtime_resolve to search for "bl" branches.
40 The first "bl" branch instruction found MUST be a call to fixup. See
41 the define for TRAMPOLINE_TEMPLATE in sysdeps/hppa/dl-machine.h. If
42 the trampoline template is changed, the list must be appropriately
43 updated. The offset of -4 allows for a magic branch at the start of
44 the template should it be necessary to change the current branch
45 position. */
46 #define NOFFSETS 2
47 static int fixup_branch_offset[NOFFSETS] = { 32, -4 };
49 #define GET_FIELD(X, FROM, TO) \
50 ((X) >> (31 - (TO)) & ((1 << ((TO) - (FROM) + 1)) - 1))
51 #define SIGN_EXTEND(VAL,BITS) \
52 ((int) ((VAL) >> ((BITS) - 1) ? (-1 << (BITS)) | (VAL) : (VAL)))
54 struct link_map;
55 typedef int (*fptr_t) (void);
56 typedef int (*fixup_t) (struct link_map *, unsigned int);
57 extern unsigned int _GLOBAL_OFFSET_TABLE_;
59 /* __canonicalize_funcptr_for_compare must be hidden so that it is not
60 placed in the dynamic symbol table. Like millicode functions, it
61 must be linked into all binaries in order access the got table of
62 that binary. However, we don't use the millicode calling convention
63 and the routine must be a normal function so that it can be compiled
64 as pic code. */
65 unsigned int __canonicalize_funcptr_for_compare (fptr_t)
66 __attribute__ ((visibility ("hidden")));
68 unsigned int
69 __canonicalize_funcptr_for_compare (fptr_t fptr)
71 static unsigned int fixup_plabel[2];
72 static fixup_t fixup;
73 unsigned int *plabel, *got;
75 /* -1 and page 0 are special. -1 is used in crtend to mark the end of
76 a list of function pointers. Also return immediately if the plabel
77 bit is not set in the function pointer. In this case, the function
78 pointer points directly to the function. */
79 if ((int) fptr == -1 || (unsigned int) fptr < 4096 || !((int) fptr & 2))
80 return (unsigned int) fptr;
82 /* The function pointer points to a function descriptor (plabel). If
83 the plabel hasn't been resolved, the first word of the plabel points
84 to the entry of the PLT stub just before the global offset table.
85 The second word in the plabel contains the relocation offset for the
86 function. */
87 plabel = (unsigned int *) ((unsigned int) fptr & ~3);
88 got = (unsigned int *) (plabel[0] + GOT_FROM_PLT_STUB);
90 /* Return the address of the function if the plabel has been resolved. */
91 if (got != &_GLOBAL_OFFSET_TABLE_)
92 return plabel[0];
94 /* Initialize our plabel for calling fixup if we haven't done so already.
95 This code needs to be thread safe but we don't have to be too careful
96 as the result is invariant. */
97 if (!fixup)
99 int i;
100 unsigned int *iptr;
102 /* Find the first "bl" branch in the offset search list. This is a
103 call to fixup or a magic branch to fixup at the beginning of the
104 trampoline template. The fixup function does the actual runtime
105 resolution of function descriptors. We only look for "bl" branches
106 with a 17-bit pc-relative displacement. */
107 for (i = 0; i < NOFFSETS; i++)
109 iptr = (unsigned int *) (got[-2] + fixup_branch_offset[i]);
110 if ((*iptr & 0xfc00e000) == 0xe8000000)
111 break;
114 /* This should not happen... */
115 if (i == NOFFSETS)
116 return ~0;
118 /* Extract the 17-bit displacement from the instruction. */
119 iptr += SIGN_EXTEND (GET_FIELD (*iptr, 19, 28) |
120 GET_FIELD (*iptr, 29, 29) << 10 |
121 GET_FIELD (*iptr, 11, 15) << 11 |
122 GET_FIELD (*iptr, 31, 31) << 16, 17);
124 /* Build a plabel for an indirect call to fixup. */
125 fixup_plabel[0] = (unsigned int) iptr + 8; /* address of fixup */
126 fixup_plabel[1] = got[-1]; /* ltp for fixup */
127 fixup = (fixup_t) ((int) fixup_plabel | 3);
130 /* Call fixup to resolve the function address. got[1] contains the
131 link_map pointer and plabel[1] the relocation offset. */
132 fixup ((struct link_map *) got[1], plabel[1]);
134 return plabel[0];