1 /* ====================================================================
2 * Copyright (c) 2005 The OpenSSL Project. Rights for redistribution
3 * and usage in source and binary forms are granted according to the
13 const void *FIPS_text_start(void);
14 const void *FIPS_text_end(void);
18 #if !defined(POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION)
19 # if (defined(__sun) && (defined(__sparc) || defined(__sparcv9))) || \
20 (defined(__sgi) && (defined(__mips) || defined(mips))) || \
21 (defined(__osf__) && defined(__alpha)) || \
22 (defined(__linux) && (defined(__arm) || defined(__arm__))) || \
23 (defined(__i386) || defined(__i386__)) || \
24 (defined(__x86_64) || defined(__x86_64__)) || \
25 (defined(vax) || defined(__vax__))
26 # define POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION
30 #if !defined(FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE)
31 # if (defined(__ANDROID__) && (defined(__arm__) || defined(__arm) || \
32 defined(__i386__)|| defined(__i386))) || \
33 (defined(__vxworks) && (defined(__ppc__) || defined(__ppc) || \
34 defined(__mips__)|| defined(__mips))) || \
35 (defined(__linux) && ((defined(__PPC__) && !defined(__PPC64__)) || \
36 defined(__arm__) || defined(__arm))) || \
37 (defined(_WIN32) && defined(_MSC_VER))
38 # define FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE
42 #if defined(__xlC__) && __xlC__>=0x600 && (defined(_POWER) || defined(_ARCH_PPC))
43 static void *instruction_pointer_xlc(void);
44 # pragma mc_func instruction_pointer_xlc {\
45 "7c0802a6" /* mflr r0 */ \
46 "48000005" /* bl $+4 */ \
47 "7c6802a6" /* mflr r3 */ \
48 "7c0803a6" /* mtlr r0 */ }
49 # pragma reg_killed_by instruction_pointer_xlc gr0 gr3
50 # define INSTRUCTION_POINTER_IMPLEMENTED(ret) (ret=instruction_pointer_xlc());
54 # define FIPS_ref_point FIPS_text_start
55 # ifdef FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE
56 # define instruction_pointer FIPS_text_startX
58 /* Some compilers put string literals into a separate segment. As we
59 * are mostly interested to hash AES tables in .rodata, we declare
60 * reference points accordingly. In case you wonder, the values are
61 * big-endian encoded variable names, just to prevent these arrays
62 * from being merged by linker. */
63 # if defined(_MSC_VER)
64 # pragma code_seg("fipstx")
66 __declspec(allocate("fipstx"))
67 const unsigned int FIPS_text_startX[]=
68 { 0x46495053, 0x5f746578, 0x745f7374, 0x61727458 };
69 # pragma const_seg("fipsro$a")
71 __declspec(allocate("fipsro$a"))
73 const unsigned int FIPS_rodata_start[]=
74 { 0x46495053, 0x5f726f64, 0x6174615f, 0x73746172 };
76 # define FIPS_ref_point FIPS_text_end
77 # ifdef FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE
78 # define instruction_pointer FIPS_text_endX
80 # if defined(_MSC_VER)
81 # pragma code_seg("fipstx$z")
83 __declspec(allocate("fipstx$z"))
84 const unsigned int FIPS_text_endX[]=
85 { 0x46495053, 0x5f746578, 0x745f656e, 0x64585b5d };
86 # pragma const_seg("fipsro$z")
88 __declspec(allocate("fipsro$z"))
90 const unsigned int FIPS_rodata_end[]=
91 { 0x46495053, 0x5f726f64, 0x6174615f, 0x656e645b };
94 #if !defined(_MSC_VER) || !defined(instruction_pointer)
96 * I declare reference function as static in order to avoid certain
97 * pitfalls in -dynamic linker behaviour...
99 static void *instruction_pointer(void)
101 /* These are ABI-neutral CPU-specific snippets. ABI-neutrality means
102 * that they are designed to work under any OS running on particular
103 * CPU, which is why you don't find any #ifdef THIS_OR_THAT_OS in
105 #if defined(INSTRUCTION_POINTER_IMPLEMENTED)
106 INSTRUCTION_POINTER_IMPLEMENTED(ret);
107 #elif defined(__GNUC__) && __GNUC__>=2
108 # if defined(__alpha) || defined(__alpha__)
109 # define INSTRUCTION_POINTER_IMPLEMENTED
110 __asm __volatile ( "br %0,1f\n1:" : "=r"(ret) );
111 # elif defined(__i386) || defined(__i386__)
112 # define INSTRUCTION_POINTER_IMPLEMENTED
113 __asm __volatile ( "call 1f\n1: popl %0" : "=r"(ret) );
114 ret = (void *)((size_t)ret&~3UL); /* align for better performance */
115 # elif defined(__ia64) || defined(__ia64__)
116 # define INSTRUCTION_POINTER_IMPLEMENTED
117 __asm __volatile ( "mov %0=ip" : "=r"(ret) );
118 # elif defined(__hppa) || defined(__hppa__) || defined(__pa_risc)
119 # define INSTRUCTION_POINTER_IMPLEMENTED
120 __asm __volatile ( "blr %%r0,%0\n\tnop" : "=r"(ret) );
121 ret = (void *)((size_t)ret&~3UL); /* mask privilege level */
122 # elif defined(__mips) || defined(__mips__)
123 # define INSTRUCTION_POINTER_IMPLEMENTED
125 __asm __volatile ( "move %1,$31\n\t" /* save ra */
128 "move $31,%1" /* restore ra */
129 : "=r"(ret),"=r"(scratch) );
130 # elif defined(__ppc__) || defined(__ppc) || \
131 defined(__powerpc) || defined(__powerpc__) || \
132 defined(__POWERPC__) || defined(_POWER) || defined(__PPC__) || \
133 defined(__PPC64__) || defined(__ppc64__) || defined(__powerpc64__)
134 # define INSTRUCTION_POINTER_IMPLEMENTED
136 __asm __volatile ( "mfspr %1,8\n\t" /* save lr */
138 "mfspr %0,8\n\t" /* mflr ret */
139 "mtspr 8,%1" /* restore lr */
140 : "=r"(ret),"=r"(scratch) );
141 # elif defined(__s390__) || defined(__s390x__)
142 # define INSTRUCTION_POINTER_IMPLEMENTED
143 __asm __volatile ( "bras %0,1f\n1:" : "=r"(ret) );
144 ret = (void *)((size_t)ret&~3UL);
145 # elif defined(__sparc) || defined(__sparc__) || defined(__sparcv9)
146 # define INSTRUCTION_POINTER_IMPLEMENTED
148 __asm __volatile ( "mov %%o7,%1\n\t"
152 : "=r"(ret),"=r"(scratch) );
153 # elif defined(__x86_64) || defined(__x86_64__)
154 # define INSTRUCTION_POINTER_IMPLEMENTED
155 __asm __volatile ( "leaq 0(%%rip),%0" : "=r"(ret) );
156 ret = (void *)((size_t)ret&~3UL); /* align for better performance */
157 # elif defined(__arm) || defined(__arm__)
158 # define INSTRUCTION_POINTER_IMPLEMENTED
159 __asm __volatile ( "sub %0,pc,#8" : "=r"(ret) );
161 #elif defined(__DECC) && defined(__alpha)
162 # define INSTRUCTION_POINTER_IMPLEMENTED
163 ret = (void *)(size_t)asm("br %v0,1f\n1:");
164 #elif defined(_MSC_VER) && defined(_M_IX86)
165 # define INSTRUCTION_POINTER_IMPLEMENTED
172 ret = (void *)((size_t)scratch&~3UL);
179 * This function returns pointer to an instruction in the vicinity of
180 * its entry point, but not outside this object module. This guarantees
181 * that sequestered code is covered...
183 const void *FIPS_ref_point()
185 #if defined(FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE)
186 # if defined(__thumb__) || defined(__thumb)
187 return (void *)((size_t)instruction_pointer&~1);
189 return (void *)instruction_pointer;
191 #elif defined(INSTRUCTION_POINTER_IMPLEMENTED)
192 return instruction_pointer();
193 /* Below we essentially cover vendor compilers which do not support
194 * inline assembler... */
196 struct { void *ip,*gp,*env; } *p = (void *)instruction_pointer;
198 #elif defined(_HPUX_SOURCE)
199 # if defined(__hppa) || defined(__hppa__)
200 struct { void *i[4]; } *p = (void *)FIPS_ref_point;
202 if (sizeof(p) == 8) /* 64-bit */
204 else if ((size_t)p & 2)
205 { p = (void *)((size_t)p&~3UL);
210 # elif defined(__ia64) || defined(__ia64__)
211 struct { unsigned long long ip,gp; } *p=(void *)instruction_pointer;
212 return (void *)(size_t)p->ip;
214 #elif (defined(__VMS) || defined(VMS)) && !(defined(vax) || defined(__vax__))
215 /* applies to both alpha and ia64 */
216 struct { unsigned __int64 opaque,ip; } *p=(void *)instruction_pointer;
217 return (void *)(size_t)p->ip;
218 #elif defined(__VOS__)
219 /* applies to both pa-risc and ia32 */
220 struct { void *dp,*ip,*gp; } *p = (void *)instruction_pointer;
222 #elif defined(_WIN32)
223 # if defined(_WIN64) && defined(_M_IA64)
224 struct { void *ip,*gp; } *p = (void *)FIPS_ref_point;
227 return (void *)FIPS_ref_point;
230 * In case you wonder why there is no #ifdef __linux. All Linux targets
231 * are GCC-based and therefore are covered by instruction_pointer above
232 * [well, some are covered by by the one below]...
234 #elif defined(POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION)
235 return (void *)instruction_pointer;