| 1 | 764 | jeremybenn | /* descriptor-pa32-hpux.h - Given a function pointer, extract and return the
 | 
      
         | 2 |  |  |    actual code address of the corresponding function.
 | 
      
         | 3 |  |  |  
 | 
      
         | 4 |  |  |    This is done by checking if the plabel bit is set. If it's not set,
 | 
      
         | 5 |  |  |    return the function pointer.  If it's set, mask it off and extract
 | 
      
         | 6 |  |  |    the address from the function descriptor.  This address may point
 | 
      
         | 7 |  |  |    to an export stub.  If so, extract the branch target from the stub
 | 
      
         | 8 |  |  |    and return it.  Otherwise, the address from the function descriptor
 | 
      
         | 9 |  |  |    is returned.
 | 
      
         | 10 |  |  |  
 | 
      
         | 11 |  |  |    Copyright (C) 2006  Free Software Foundation
 | 
      
         | 12 |  |  |  
 | 
      
         | 13 |  |  |    This file is part of libgcj.
 | 
      
         | 14 |  |  |  
 | 
      
         | 15 |  |  | This software is copyrighted work licensed under the terms of the
 | 
      
         | 16 |  |  | Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
 | 
      
         | 17 |  |  | details.  */
 | 
      
         | 18 |  |  |  
 | 
      
         | 19 |  |  | #define UNWRAP_FUNCTION_DESCRIPTOR pa_unwrap_function_descriptor
 | 
      
         | 20 |  |  |  
 | 
      
         | 21 |  |  | #ifdef __cplusplus
 | 
      
         | 22 |  |  | extern "C" {
 | 
      
         | 23 |  |  | #endif
 | 
      
         | 24 |  |  |  
 | 
      
         | 25 |  |  | /* Extract bit field from word using HP's numbering (MSB = 0).  */
 | 
      
         | 26 |  |  | #define GET_FIELD(X, FROM, TO)                                  \
 | 
      
         | 27 |  |  |   ((X) >> (31 - (TO)) & ((1 << ((TO) - (FROM) + 1)) - 1))
 | 
      
         | 28 |  |  |  
 | 
      
         | 29 |  |  | static inline int
 | 
      
         | 30 |  |  | sign_extend (int x, int len)
 | 
      
         | 31 |  |  | {
 | 
      
         | 32 |  |  |   int signbit = (1 << (len - 1));
 | 
      
         | 33 |  |  |   int mask = (signbit << 1) - 1;
 | 
      
         | 34 |  |  |   return ((x & mask) ^ signbit) - signbit;
 | 
      
         | 35 |  |  | }
 | 
      
         | 36 |  |  |  
 | 
      
         | 37 |  |  | /* Extract a 17-bit signed constant from branch instructions.  */
 | 
      
         | 38 |  |  | static inline int
 | 
      
         | 39 |  |  | extract_17 (unsigned word)
 | 
      
         | 40 |  |  | {
 | 
      
         | 41 |  |  |   return sign_extend (GET_FIELD (word, 19, 28)
 | 
      
         | 42 |  |  |                       | GET_FIELD (word, 29, 29) << 10
 | 
      
         | 43 |  |  |                       | GET_FIELD (word, 11, 15) << 11
 | 
      
         | 44 |  |  |                       | (word & 0x1) << 16, 17);
 | 
      
         | 45 |  |  | }
 | 
      
         | 46 |  |  |  
 | 
      
         | 47 |  |  | /* Extract a 22-bit signed constant from branch instructions.  */
 | 
      
         | 48 |  |  | static inline int
 | 
      
         | 49 |  |  | extract_22 (unsigned word)
 | 
      
         | 50 |  |  | {
 | 
      
         | 51 |  |  |   return sign_extend (GET_FIELD (word, 19, 28)
 | 
      
         | 52 |  |  |                       | GET_FIELD (word, 29, 29) << 10
 | 
      
         | 53 |  |  |                       | GET_FIELD (word, 11, 15) << 11
 | 
      
         | 54 |  |  |                       | GET_FIELD (word, 6, 10) << 16
 | 
      
         | 55 |  |  |                       | (word & 0x1) << 21, 22);
 | 
      
         | 56 |  |  | }
 | 
      
         | 57 |  |  |  
 | 
      
         | 58 |  |  | static void *
 | 
      
         | 59 |  |  | pa_unwrap_function_descriptor (void *addr)
 | 
      
         | 60 |  |  | {
 | 
      
         | 61 |  |  |   unsigned int *tmp_addr;
 | 
      
         | 62 |  |  |  
 | 
      
         | 63 |  |  |   /* Check if plabel bit is set in function pointer.  */
 | 
      
         | 64 |  |  |   if (!((unsigned int) addr & 2))
 | 
      
         | 65 |  |  |     return addr;
 | 
      
         | 66 |  |  |  
 | 
      
         | 67 |  |  |   tmp_addr = *(unsigned int **) ((unsigned int) addr & ~3);
 | 
      
         | 68 |  |  |  
 | 
      
         | 69 |  |  |   /* If TMP_ADDR points to an export stub, adjust it so that it points
 | 
      
         | 70 |  |  |      to the branch target of the stub.  */
 | 
      
         | 71 |  |  |   if ((*tmp_addr & 0xffe0e002) == 0xe8400000            /* bl x,r2 */
 | 
      
         | 72 |  |  |       && *(tmp_addr + 1) == 0x08000240                  /* nop */
 | 
      
         | 73 |  |  |       && *(tmp_addr + 2) == 0x4bc23fd1                  /* ldw -18(sp),rp */
 | 
      
         | 74 |  |  |       && *(tmp_addr + 3) == 0x004010a1                  /* ldsid (rp),r1 */
 | 
      
         | 75 |  |  |       && *(tmp_addr + 4) == 0x00011820                  /* mtsp r1,sr0 */
 | 
      
         | 76 |  |  |       && *(tmp_addr + 5) == 0xe0400002)                 /* be,n 0(sr0,rp) */
 | 
      
         | 77 |  |  |     /* Extract target address from PA 1.x 17-bit branch.  */
 | 
      
         | 78 |  |  |     tmp_addr += extract_17 (*tmp_addr) + 2;
 | 
      
         | 79 |  |  |   else if ((*tmp_addr & 0xfc00e002) == 0xe800a000       /* b,l x,r2 */
 | 
      
         | 80 |  |  |            && *(tmp_addr + 1) == 0x08000240             /* nop */
 | 
      
         | 81 |  |  |            && *(tmp_addr + 2) == 0x4bc23fd1             /* ldw -18(sp),rp */
 | 
      
         | 82 |  |  |            && *(tmp_addr + 3) == 0xe840d002)            /* bve,n (rp) */
 | 
      
         | 83 |  |  |     /* Extract target address from PA 2.0 22-bit branch.  */
 | 
      
         | 84 |  |  |     tmp_addr += extract_22 (*tmp_addr) + 2;
 | 
      
         | 85 |  |  |  
 | 
      
         | 86 |  |  |   return (void *) tmp_addr;
 | 
      
         | 87 |  |  | }
 | 
      
         | 88 |  |  |  
 | 
      
         | 89 |  |  | #ifdef __cplusplus
 | 
      
         | 90 |  |  | }
 | 
      
         | 91 |  |  | #endif
 |