Follow RTS symbol changes in Linker.c
[ghc.git] / rts / Linker.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2000-2004
4 *
5 * RTS Object Linker
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #if 0
10 #include "PosixSource.h"
11 #endif
12
13 /* Linux needs _GNU_SOURCE to get RTLD_DEFAULT from <dlfcn.h> and
14 MREMAP_MAYMOVE from <sys/mman.h>.
15 */
16 #if defined(__linux__) || defined(__GLIBC__)
17 #define _GNU_SOURCE 1
18 #endif
19
20 #include "Rts.h"
21 #include "HsFFI.h"
22
23 #include "sm/Storage.h"
24 #include "Stats.h"
25 #include "Hash.h"
26 #include "LinkerInternals.h"
27 #include "RtsUtils.h"
28 #include "Trace.h"
29 #include "StgPrimFloat.h" // for __int_encodeFloat etc.
30 #include "Stable.h"
31
32 #if !defined(mingw32_HOST_OS)
33 #include "posix/Signals.h"
34 #endif
35
36 // get protos for is*()
37 #include <ctype.h>
38
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif
42
43 #include <inttypes.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <assert.h>
48
49 #ifdef HAVE_SYS_STAT_H
50 #include <sys/stat.h>
51 #endif
52
53 #if defined(HAVE_DLFCN_H)
54 #include <dlfcn.h>
55 #endif
56
57 #if defined(cygwin32_HOST_OS)
58 #ifdef HAVE_DIRENT_H
59 #include <dirent.h>
60 #endif
61
62 #ifdef HAVE_SYS_TIME_H
63 #include <sys/time.h>
64 #endif
65 #include <regex.h>
66 #include <sys/fcntl.h>
67 #include <sys/termios.h>
68 #include <sys/utime.h>
69 #include <sys/utsname.h>
70 #include <sys/wait.h>
71 #endif
72
73 #if !defined(powerpc_HOST_ARCH) && \
74 ( defined(linux_HOST_OS ) || defined(freebsd_HOST_OS) || \
75 defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS ) || \
76 defined(openbsd_HOST_OS ) || defined(darwin_HOST_OS ) || \
77 defined(kfreebsdgnu_HOST_OS) || defined(gnu_HOST_OS))
78 /* Don't use mmap on powerpc_HOST_ARCH as mmap doesn't support
79 * reallocating but we need to allocate jump islands just after each
80 * object images. Otherwise relative branches to jump islands can fail
81 * due to 24-bits displacement overflow.
82 */
83 #define USE_MMAP
84 #include <fcntl.h>
85 #include <sys/mman.h>
86
87 #ifdef HAVE_UNISTD_H
88 #include <unistd.h>
89 #endif
90
91 #endif
92
93 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS) || defined(gnu_HOST_OS)
94 # define OBJFORMAT_ELF
95 # include <regex.h> // regex is already used by dlopen() so this is OK
96 // to use here without requiring an additional lib
97 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
98 # define OBJFORMAT_PEi386
99 # include <windows.h>
100 # include <math.h>
101 #elif defined(darwin_HOST_OS)
102 # define OBJFORMAT_MACHO
103 # include <regex.h>
104 # include <mach/machine.h>
105 # include <mach-o/fat.h>
106 # include <mach-o/loader.h>
107 # include <mach-o/nlist.h>
108 # include <mach-o/reloc.h>
109 #if !defined(HAVE_DLFCN_H)
110 # include <mach-o/dyld.h>
111 #endif
112 #if defined(powerpc_HOST_ARCH)
113 # include <mach-o/ppc/reloc.h>
114 #endif
115 #if defined(x86_64_HOST_ARCH)
116 # include <mach-o/x86_64/reloc.h>
117 #endif
118 #endif
119
120 #if defined(x86_64_HOST_ARCH) && defined(darwin_HOST_OS)
121 #define ALWAYS_PIC
122 #endif
123
124 /* Hash table mapping symbol names to Symbol */
125 static /*Str*/HashTable *symhash;
126
127 /* Hash table mapping symbol names to StgStablePtr */
128 static /*Str*/HashTable *stablehash;
129
130 /* List of currently loaded objects */
131 ObjectCode *objects = NULL; /* initially empty */
132
133 static HsInt loadOc( ObjectCode* oc );
134 static ObjectCode* mkOc( pathchar *path, char *image, int imageSize,
135 char *archiveMemberName
136 #ifndef USE_MMAP
137 #ifdef darwin_HOST_OS
138 , int misalignment
139 #endif
140 #endif
141 );
142
143 // Use wchar_t for pathnames on Windows (#5697)
144 #if defined(mingw32_HOST_OS)
145 #define pathcmp wcscmp
146 #define pathlen wcslen
147 #define pathopen _wfopen
148 #define pathstat _wstat
149 #define struct_stat struct _stat
150 #define open wopen
151 #define WSTR(s) L##s
152 #define PATH_FMT "S"
153 #else
154 #define pathcmp strcmp
155 #define pathlen strlen
156 #define pathopen fopen
157 #define pathstat stat
158 #define struct_stat struct stat
159 #define WSTR(s) s
160 #define PATH_FMT "s"
161 #endif
162
163 static pathchar* pathdup(pathchar *path)
164 {
165 pathchar *ret;
166 #if defined(mingw32_HOST_OS)
167 ret = wcsdup(path);
168 #else
169 /* sigh, strdup() isn't a POSIX function, so do it the long way */
170 ret = stgMallocBytes( strlen(path)+1, "loadObj" );
171 strcpy(ret, path);
172 #endif
173 return ret;
174 }
175
176
177 #if defined(OBJFORMAT_ELF)
178 static int ocVerifyImage_ELF ( ObjectCode* oc );
179 static int ocGetNames_ELF ( ObjectCode* oc );
180 static int ocResolve_ELF ( ObjectCode* oc );
181 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) || defined(arm_HOST_ARCH)
182 static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
183 #endif
184 #elif defined(OBJFORMAT_PEi386)
185 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
186 static int ocGetNames_PEi386 ( ObjectCode* oc );
187 static int ocResolve_PEi386 ( ObjectCode* oc );
188 static void *lookupSymbolInDLLs ( unsigned char *lbl );
189 static void zapTrailingAtSign ( unsigned char *sym );
190 #elif defined(OBJFORMAT_MACHO)
191 static int ocVerifyImage_MachO ( ObjectCode* oc );
192 static int ocGetNames_MachO ( ObjectCode* oc );
193 static int ocResolve_MachO ( ObjectCode* oc );
194
195 #ifndef USE_MMAP
196 static int machoGetMisalignment( FILE * );
197 #endif
198 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
199 static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
200 #endif
201 #ifdef powerpc_HOST_ARCH
202 static void machoInitSymbolsWithoutUnderscore( void );
203 #endif
204 #endif
205
206 /* on x86_64 we have a problem with relocating symbol references in
207 * code that was compiled without -fPIC. By default, the small memory
208 * model is used, which assumes that symbol references can fit in a
209 * 32-bit slot. The system dynamic linker makes this work for
210 * references to shared libraries by either (a) allocating a jump
211 * table slot for code references, or (b) moving the symbol at load
212 * time (and copying its contents, if necessary) for data references.
213 *
214 * We unfortunately can't tell whether symbol references are to code
215 * or data. So for now we assume they are code (the vast majority
216 * are), and allocate jump-table slots. Unfortunately this will
217 * SILENTLY generate crashing code for data references. This hack is
218 * enabled by X86_64_ELF_NONPIC_HACK.
219 *
220 * One workaround is to use shared Haskell libraries. This is
221 * coming. Another workaround is to keep the static libraries but
222 * compile them with -fPIC, because that will generate PIC references
223 * to data which can be relocated. The PIC code is still too green to
224 * do this systematically, though.
225 *
226 * See bug #781
227 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
228 *
229 * Naming Scheme for Symbol Macros
230 *
231 * SymI_*: symbol is internal to the RTS. It resides in an object
232 * file/library that is statically.
233 * SymE_*: symbol is external to the RTS library. It might be linked
234 * dynamically.
235 *
236 * Sym*_HasProto : the symbol prototype is imported in an include file
237 * or defined explicitly
238 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
239 * default proto extern void sym(void);
240 */
241 #define X86_64_ELF_NONPIC_HACK 1
242
243 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
244 * small memory model on this architecture (see gcc docs,
245 * -mcmodel=small).
246 *
247 * MAP_32BIT not available on OpenBSD/amd64
248 */
249 #if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT)
250 #define TRY_MAP_32BIT MAP_32BIT
251 #else
252 #define TRY_MAP_32BIT 0
253 #endif
254
255 /*
256 * Due to the small memory model (see above), on x86_64 we have to map
257 * all our non-PIC object files into the low 2Gb of the address space
258 * (why 2Gb and not 4Gb? Because all addresses must be reachable
259 * using a 32-bit signed PC-relative offset). On Linux we can do this
260 * using the MAP_32BIT flag to mmap(), however on other OSs
261 * (e.g. *BSD, see #2063, and also on Linux inside Xen, see #2512), we
262 * can't do this. So on these systems, we have to pick a base address
263 * in the low 2Gb of the address space and try to allocate memory from
264 * there.
265 *
266 * We pick a default address based on the OS, but also make this
267 * configurable via an RTS flag (+RTS -xm)
268 */
269 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
270
271 #if defined(MAP_32BIT)
272 // Try to use MAP_32BIT
273 #define MMAP_32BIT_BASE_DEFAULT 0
274 #else
275 // A guess: 1Gb.
276 #define MMAP_32BIT_BASE_DEFAULT 0x40000000
277 #endif
278
279 static void *mmap_32bit_base = (void *)MMAP_32BIT_BASE_DEFAULT;
280 #endif
281
282 /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */
283 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
284 #define MAP_ANONYMOUS MAP_ANON
285 #endif
286
287 /* -----------------------------------------------------------------------------
288 * Built-in symbols from the RTS
289 */
290
291 typedef struct _RtsSymbolVal {
292 char *lbl;
293 void *addr;
294 } RtsSymbolVal;
295
296 #define Maybe_Stable_Names SymI_HasProto(stg_mkWeakzh) \
297 SymI_HasProto(stg_mkWeakNoFinalizzerzh) \
298 SymI_HasProto(stg_mkWeakForeignEnvzh) \
299 SymI_HasProto(stg_makeStableNamezh) \
300 SymI_HasProto(stg_finalizzeWeakzh)
301
302 #if !defined (mingw32_HOST_OS)
303 #define RTS_POSIX_ONLY_SYMBOLS \
304 SymI_HasProto(__hscore_get_saved_termios) \
305 SymI_HasProto(__hscore_set_saved_termios) \
306 SymI_HasProto(shutdownHaskellAndSignal) \
307 SymI_HasProto(signal_handlers) \
308 SymI_HasProto(stg_sig_install) \
309 SymI_HasProto(rtsTimerSignal) \
310 SymI_HasProto(atexit) \
311 SymI_NeedsProto(nocldstop)
312 #endif
313
314 #if defined (cygwin32_HOST_OS)
315 #define RTS_MINGW_ONLY_SYMBOLS /**/
316 /* Don't have the ability to read import libs / archives, so
317 * we have to stupidly list a lot of what libcygwin.a
318 * exports; sigh.
319 */
320 #define RTS_CYGWIN_ONLY_SYMBOLS \
321 SymI_HasProto(regfree) \
322 SymI_HasProto(regexec) \
323 SymI_HasProto(regerror) \
324 SymI_HasProto(regcomp) \
325 SymI_HasProto(__errno) \
326 SymI_HasProto(access) \
327 SymI_HasProto(chmod) \
328 SymI_HasProto(chdir) \
329 SymI_HasProto(close) \
330 SymI_HasProto(creat) \
331 SymI_HasProto(dup) \
332 SymI_HasProto(dup2) \
333 SymI_HasProto(fstat) \
334 SymI_HasProto(fcntl) \
335 SymI_HasProto(getcwd) \
336 SymI_HasProto(getenv) \
337 SymI_HasProto(lseek) \
338 SymI_HasProto(open) \
339 SymI_HasProto(fpathconf) \
340 SymI_HasProto(pathconf) \
341 SymI_HasProto(stat) \
342 SymI_HasProto(pow) \
343 SymI_HasProto(tanh) \
344 SymI_HasProto(cosh) \
345 SymI_HasProto(sinh) \
346 SymI_HasProto(atan) \
347 SymI_HasProto(acos) \
348 SymI_HasProto(asin) \
349 SymI_HasProto(tan) \
350 SymI_HasProto(cos) \
351 SymI_HasProto(sin) \
352 SymI_HasProto(exp) \
353 SymI_HasProto(log) \
354 SymI_HasProto(sqrt) \
355 SymI_HasProto(localtime_r) \
356 SymI_HasProto(gmtime_r) \
357 SymI_HasProto(mktime) \
358 SymI_NeedsProto(_imp___tzname) \
359 SymI_HasProto(gettimeofday) \
360 SymI_HasProto(timezone) \
361 SymI_HasProto(tcgetattr) \
362 SymI_HasProto(tcsetattr) \
363 SymI_HasProto(memcpy) \
364 SymI_HasProto(memmove) \
365 SymI_HasProto(realloc) \
366 SymI_HasProto(malloc) \
367 SymI_HasProto(free) \
368 SymI_HasProto(fork) \
369 SymI_HasProto(lstat) \
370 SymI_HasProto(isatty) \
371 SymI_HasProto(mkdir) \
372 SymI_HasProto(opendir) \
373 SymI_HasProto(readdir) \
374 SymI_HasProto(rewinddir) \
375 SymI_HasProto(closedir) \
376 SymI_HasProto(link) \
377 SymI_HasProto(mkfifo) \
378 SymI_HasProto(pipe) \
379 SymI_HasProto(read) \
380 SymI_HasProto(rename) \
381 SymI_HasProto(rmdir) \
382 SymI_HasProto(select) \
383 SymI_HasProto(system) \
384 SymI_HasProto(write) \
385 SymI_HasProto(strcmp) \
386 SymI_HasProto(strcpy) \
387 SymI_HasProto(strncpy) \
388 SymI_HasProto(strerror) \
389 SymI_HasProto(sigaddset) \
390 SymI_HasProto(sigemptyset) \
391 SymI_HasProto(sigprocmask) \
392 SymI_HasProto(umask) \
393 SymI_HasProto(uname) \
394 SymI_HasProto(unlink) \
395 SymI_HasProto(utime) \
396 SymI_HasProto(waitpid)
397
398 #elif defined(mingw32_HOST_OS)
399 #define RTS_POSIX_ONLY_SYMBOLS /**/
400 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
401
402 #if HAVE_GETTIMEOFDAY
403 #define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
404 #else
405 #define RTS_MINGW_GETTIMEOFDAY_SYM /**/
406 #endif
407
408 #if HAVE___MINGW_VFPRINTF
409 #define RTS___MINGW_VFPRINTF_SYM SymI_HasProto(__mingw_vfprintf)
410 #else
411 #define RTS___MINGW_VFPRINTF_SYM /**/
412 #endif
413
414 #if defined(i386_HOST_ARCH)
415 #define RTS_WIN32_ONLY(X) X
416 #else
417 #define RTS_WIN32_ONLY(X) /**/
418 #endif
419
420 #if defined(x86_64_HOST_ARCH)
421 #define RTS_WIN64_ONLY(X) X
422 #else
423 #define RTS_WIN64_ONLY(X) /**/
424 #endif
425
426 /* These are statically linked from the mingw libraries into the ghc
427 executable, so we have to employ this hack. */
428 #define RTS_MINGW_ONLY_SYMBOLS \
429 SymI_HasProto(stg_asyncReadzh) \
430 SymI_HasProto(stg_asyncWritezh) \
431 SymI_HasProto(stg_asyncDoProczh) \
432 SymI_HasProto(getWin32ProgArgv) \
433 SymI_HasProto(setWin32ProgArgv) \
434 SymI_HasProto(memset) \
435 SymI_HasProto(inet_ntoa) \
436 SymI_HasProto(inet_addr) \
437 SymI_HasProto(htonl) \
438 SymI_HasProto(recvfrom) \
439 SymI_HasProto(listen) \
440 SymI_HasProto(bind) \
441 SymI_HasProto(shutdown) \
442 SymI_HasProto(connect) \
443 SymI_HasProto(htons) \
444 SymI_HasProto(ntohs) \
445 SymI_HasProto(getservbyname) \
446 SymI_HasProto(getservbyport) \
447 SymI_HasProto(getprotobynumber) \
448 SymI_HasProto(getprotobyname) \
449 SymI_HasProto(gethostbyname) \
450 SymI_HasProto(gethostbyaddr) \
451 SymI_HasProto(gethostname) \
452 SymI_HasProto(strcpy) \
453 SymI_HasProto(strncpy) \
454 SymI_HasProto(abort) \
455 RTS_WIN32_ONLY(SymI_NeedsProto(_alloca)) \
456 SymI_HasProto(isxdigit) \
457 SymI_HasProto(isupper) \
458 SymI_HasProto(ispunct) \
459 SymI_HasProto(islower) \
460 SymI_HasProto(isspace) \
461 SymI_HasProto(isprint) \
462 SymI_HasProto(isdigit) \
463 SymI_HasProto(iscntrl) \
464 SymI_HasProto(isalpha) \
465 SymI_HasProto(isalnum) \
466 SymI_HasProto(isascii) \
467 RTS___MINGW_VFPRINTF_SYM \
468 SymI_HasProto(strcmp) \
469 SymI_HasProto(memmove) \
470 SymI_HasProto(realloc) \
471 SymI_HasProto(malloc) \
472 SymI_HasProto(pow) \
473 SymI_HasProto(tanh) \
474 SymI_HasProto(cosh) \
475 SymI_HasProto(sinh) \
476 SymI_HasProto(atan) \
477 SymI_HasProto(acos) \
478 SymI_HasProto(asin) \
479 SymI_HasProto(tan) \
480 SymI_HasProto(cos) \
481 SymI_HasProto(sin) \
482 SymI_HasProto(exp) \
483 SymI_HasProto(log) \
484 SymI_HasProto(sqrt) \
485 SymI_HasProto(powf) \
486 SymI_HasProto(tanhf) \
487 SymI_HasProto(coshf) \
488 SymI_HasProto(sinhf) \
489 SymI_HasProto(atanf) \
490 SymI_HasProto(acosf) \
491 SymI_HasProto(asinf) \
492 SymI_HasProto(tanf) \
493 SymI_HasProto(cosf) \
494 SymI_HasProto(sinf) \
495 SymI_HasProto(expf) \
496 SymI_HasProto(logf) \
497 SymI_HasProto(sqrtf) \
498 SymI_HasProto(erf) \
499 SymI_HasProto(erfc) \
500 SymI_HasProto(erff) \
501 SymI_HasProto(erfcf) \
502 SymI_HasProto(memcpy) \
503 SymI_HasProto(rts_InstallConsoleEvent) \
504 SymI_HasProto(rts_ConsoleHandlerDone) \
505 SymI_NeedsProto(mktime) \
506 RTS_WIN32_ONLY(SymI_NeedsProto(_imp___timezone)) \
507 RTS_WIN32_ONLY(SymI_NeedsProto(_imp___tzname)) \
508 RTS_WIN32_ONLY(SymI_NeedsProto(_imp__tzname)) \
509 RTS_WIN32_ONLY(SymI_NeedsProto(_imp___iob)) \
510 RTS_WIN32_ONLY(SymI_NeedsProto(_imp___osver)) \
511 SymI_NeedsProto(localtime) \
512 SymI_NeedsProto(gmtime) \
513 SymI_NeedsProto(opendir) \
514 SymI_NeedsProto(readdir) \
515 SymI_NeedsProto(rewinddir) \
516 RTS_WIN32_ONLY(SymI_NeedsProto(_imp____mb_cur_max)) \
517 RTS_WIN32_ONLY(SymI_NeedsProto(_imp___pctype)) \
518 RTS_WIN32_ONLY(SymI_NeedsProto(__chkstk)) \
519 RTS_WIN64_ONLY(SymI_NeedsProto(__imp___iob_func)) \
520 RTS_WIN64_ONLY(SymI_NeedsProto(___chkstk_ms)) \
521 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_localeconv)) \
522 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_islower)) \
523 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_isspace)) \
524 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_isxdigit)) \
525 RTS_WIN64_ONLY(SymI_HasProto(close)) \
526 RTS_WIN64_ONLY(SymI_HasProto(read)) \
527 RTS_WIN64_ONLY(SymI_HasProto(dup)) \
528 RTS_WIN64_ONLY(SymI_HasProto(dup2)) \
529 RTS_WIN64_ONLY(SymI_HasProto(write)) \
530 SymI_NeedsProto(getpid) \
531 RTS_WIN64_ONLY(SymI_HasProto(access)) \
532 SymI_HasProto(chmod) \
533 RTS_WIN64_ONLY(SymI_HasProto(creat)) \
534 RTS_WIN64_ONLY(SymI_HasProto(umask)) \
535 SymI_HasProto(unlink) \
536 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__errno)) \
537 RTS_WIN64_ONLY(SymI_NeedsProto(ftruncate64)) \
538 RTS_WIN64_ONLY(SymI_HasProto(setmode)) \
539 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__wstat64)) \
540 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__fstat64)) \
541 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__wsopen)) \
542 RTS_WIN64_ONLY(SymI_HasProto(__imp__environ)) \
543 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetFileAttributesA)) \
544 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetFileInformationByHandle)) \
545 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetFileType)) \
546 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetLastError)) \
547 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_QueryPerformanceFrequency)) \
548 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_QueryPerformanceCounter)) \
549 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetTickCount)) \
550 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_WaitForSingleObject)) \
551 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_PeekConsoleInputA)) \
552 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_ReadConsoleInputA)) \
553 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_PeekNamedPipe)) \
554 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__isatty)) \
555 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_select)) \
556 RTS_WIN64_ONLY(SymI_HasProto(isatty)) \
557 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__get_osfhandle)) \
558 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetConsoleMode)) \
559 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_SetConsoleMode)) \
560 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_FlushConsoleInputBuffer)) \
561 RTS_WIN64_ONLY(SymI_HasProto(free)) \
562 RTS_WIN64_ONLY(SymI_NeedsProto(raise)) \
563 RTS_WIN64_ONLY(SymI_NeedsProto(_getpid)) \
564 RTS_WIN64_ONLY(SymI_HasProto(getc)) \
565 RTS_WIN64_ONLY(SymI_HasProto(ungetc)) \
566 RTS_WIN64_ONLY(SymI_HasProto(puts)) \
567 RTS_WIN64_ONLY(SymI_HasProto(putc)) \
568 RTS_WIN64_ONLY(SymI_HasProto(putchar)) \
569 RTS_WIN64_ONLY(SymI_HasProto(fputc)) \
570 RTS_WIN64_ONLY(SymI_HasProto(fread)) \
571 RTS_WIN64_ONLY(SymI_HasProto(fwrite)) \
572 RTS_WIN64_ONLY(SymI_HasProto(ferror)) \
573 RTS_WIN64_ONLY(SymI_HasProto(printf)) \
574 RTS_WIN64_ONLY(SymI_HasProto(fprintf)) \
575 RTS_WIN64_ONLY(SymI_HasProto(sprintf)) \
576 RTS_WIN64_ONLY(SymI_HasProto(vsprintf)) \
577 RTS_WIN64_ONLY(SymI_HasProto(sscanf)) \
578 RTS_WIN64_ONLY(SymI_HasProto(ldexp)) \
579 RTS_WIN64_ONLY(SymI_HasProto(strlen)) \
580 RTS_WIN64_ONLY(SymI_HasProto(strnlen)) \
581 RTS_WIN64_ONLY(SymI_HasProto(strchr)) \
582 RTS_WIN64_ONLY(SymI_HasProto(strtol)) \
583 RTS_WIN64_ONLY(SymI_HasProto(strerror)) \
584 RTS_WIN64_ONLY(SymI_HasProto(memchr)) \
585 RTS_WIN64_ONLY(SymI_HasProto(memcmp)) \
586 RTS_WIN64_ONLY(SymI_HasProto(wcscpy)) \
587 RTS_WIN64_ONLY(SymI_HasProto(wcslen)) \
588 RTS_WIN64_ONLY(SymI_HasProto(_lseeki64)) \
589 RTS_WIN64_ONLY(SymI_HasProto(_wchmod)) \
590 RTS_WIN64_ONLY(SymI_HasProto(closesocket)) \
591 RTS_WIN64_ONLY(SymI_HasProto(send)) \
592 RTS_WIN64_ONLY(SymI_HasProto(recv)) \
593 RTS_WIN64_ONLY(SymI_HasProto(bsearch)) \
594 RTS_WIN64_ONLY(SymI_HasProto(CommandLineToArgvW)) \
595 RTS_WIN64_ONLY(SymI_HasProto(CreateBitmap)) \
596 RTS_WIN64_ONLY(SymI_HasProto(CreateBitmapIndirect)) \
597 RTS_WIN64_ONLY(SymI_HasProto(CreateCompatibleBitmap)) \
598 RTS_WIN64_ONLY(SymI_HasProto(CreateDIBPatternBrushPt)) \
599 RTS_WIN64_ONLY(SymI_HasProto(CreateDIBitmap)) \
600 RTS_WIN64_ONLY(SymI_HasProto(SetBitmapDimensionEx)) \
601 RTS_WIN64_ONLY(SymI_HasProto(GetBitmapDimensionEx)) \
602 RTS_WIN64_ONLY(SymI_HasProto(GetStockObject)) \
603 RTS_WIN64_ONLY(SymI_HasProto(GetObjectW)) \
604 RTS_WIN64_ONLY(SymI_HasProto(DeleteObject)) \
605 RTS_WIN64_ONLY(SymI_HasProto(SetDIBits)) \
606 RTS_WIN64_ONLY(SymI_HasProto(GetDIBits)) \
607 RTS_WIN64_ONLY(SymI_HasProto(CreateSolidBrush)) \
608 RTS_WIN64_ONLY(SymI_HasProto(CreateHatchBrush)) \
609 RTS_WIN64_ONLY(SymI_HasProto(CreatePatternBrush)) \
610 RTS_WIN64_ONLY(SymI_HasProto(CreateFontW)) \
611 RTS_WIN64_ONLY(SymI_HasProto(AngleArc)) \
612 RTS_WIN64_ONLY(SymI_HasProto(Arc)) \
613 RTS_WIN64_ONLY(SymI_HasProto(ArcTo)) \
614 RTS_WIN64_ONLY(SymI_HasProto(BeginPath)) \
615 RTS_WIN64_ONLY(SymI_HasProto(BitBlt)) \
616 RTS_WIN64_ONLY(SymI_HasProto(CancelDC)) \
617 RTS_WIN64_ONLY(SymI_HasProto(Chord)) \
618 RTS_WIN64_ONLY(SymI_HasProto(CloseFigure)) \
619 RTS_WIN64_ONLY(SymI_HasProto(CombineRgn)) \
620 RTS_WIN64_ONLY(SymI_HasProto(CreateCompatibleDC)) \
621 RTS_WIN64_ONLY(SymI_HasProto(CreateEllipticRgn)) \
622 RTS_WIN64_ONLY(SymI_HasProto(CreateEllipticRgnIndirect)) \
623 RTS_WIN64_ONLY(SymI_HasProto(CreatePen)) \
624 RTS_WIN64_ONLY(SymI_HasProto(CreatePolygonRgn)) \
625 RTS_WIN64_ONLY(SymI_HasProto(CreateRectRgn)) \
626 RTS_WIN64_ONLY(SymI_HasProto(CreateRectRgnIndirect)) \
627 RTS_WIN64_ONLY(SymI_HasProto(CreateRoundRectRgn)) \
628 RTS_WIN64_ONLY(SymI_HasProto(DeleteDC)) \
629 RTS_WIN64_ONLY(SymI_HasProto(Ellipse)) \
630 RTS_WIN64_ONLY(SymI_HasProto(EndPath)) \
631 RTS_WIN64_ONLY(SymI_HasProto(EqualRgn)) \
632 RTS_WIN64_ONLY(SymI_HasProto(ExtSelectClipRgn)) \
633 RTS_WIN64_ONLY(SymI_HasProto(FillPath)) \
634 RTS_WIN64_ONLY(SymI_HasProto(FillRgn)) \
635 RTS_WIN64_ONLY(SymI_HasProto(FlattenPath)) \
636 RTS_WIN64_ONLY(SymI_HasProto(FrameRgn)) \
637 RTS_WIN64_ONLY(SymI_HasProto(GetArcDirection)) \
638 RTS_WIN64_ONLY(SymI_HasProto(GetBkColor)) \
639 RTS_WIN64_ONLY(SymI_HasProto(GetBkMode)) \
640 RTS_WIN64_ONLY(SymI_HasProto(GetBrushOrgEx)) \
641 RTS_WIN64_ONLY(SymI_HasProto(GetCurrentObject)) \
642 RTS_WIN64_ONLY(SymI_HasProto(GetDCOrgEx)) \
643 RTS_WIN64_ONLY(SymI_HasProto(GetGraphicsMode)) \
644 RTS_WIN64_ONLY(SymI_HasProto(GetMiterLimit)) \
645 RTS_WIN64_ONLY(SymI_HasProto(GetPolyFillMode)) \
646 RTS_WIN64_ONLY(SymI_HasProto(GetRgnBox)) \
647 RTS_WIN64_ONLY(SymI_HasProto(GetStretchBltMode)) \
648 RTS_WIN64_ONLY(SymI_HasProto(GetTextAlign)) \
649 RTS_WIN64_ONLY(SymI_HasProto(GetTextCharacterExtra)) \
650 RTS_WIN64_ONLY(SymI_HasProto(GetTextColor)) \
651 RTS_WIN64_ONLY(SymI_HasProto(GetTextExtentPoint32W)) \
652 RTS_WIN64_ONLY(SymI_HasProto(InvertRgn)) \
653 RTS_WIN64_ONLY(SymI_HasProto(LineTo)) \
654 RTS_WIN64_ONLY(SymI_HasProto(MaskBlt)) \
655 RTS_WIN64_ONLY(SymI_HasProto(MoveToEx)) \
656 RTS_WIN64_ONLY(SymI_HasProto(OffsetRgn)) \
657 RTS_WIN64_ONLY(SymI_HasProto(PaintRgn)) \
658 RTS_WIN64_ONLY(SymI_HasProto(PathToRegion)) \
659 RTS_WIN64_ONLY(SymI_HasProto(Pie)) \
660 RTS_WIN64_ONLY(SymI_HasProto(PlgBlt)) \
661 RTS_WIN64_ONLY(SymI_HasProto(PolyBezier)) \
662 RTS_WIN64_ONLY(SymI_HasProto(PolyBezierTo)) \
663 RTS_WIN64_ONLY(SymI_HasProto(Polygon)) \
664 RTS_WIN64_ONLY(SymI_HasProto(Polyline)) \
665 RTS_WIN64_ONLY(SymI_HasProto(PolylineTo)) \
666 RTS_WIN64_ONLY(SymI_HasProto(PtInRegion)) \
667 RTS_WIN64_ONLY(SymI_HasProto(Rectangle)) \
668 RTS_WIN64_ONLY(SymI_HasProto(RectInRegion)) \
669 RTS_WIN64_ONLY(SymI_HasProto(RestoreDC)) \
670 RTS_WIN64_ONLY(SymI_HasProto(RoundRect)) \
671 RTS_WIN64_ONLY(SymI_HasProto(SaveDC)) \
672 RTS_WIN64_ONLY(SymI_HasProto(SelectClipPath)) \
673 RTS_WIN64_ONLY(SymI_HasProto(SelectClipRgn)) \
674 RTS_WIN64_ONLY(SymI_HasProto(SelectObject)) \
675 RTS_WIN64_ONLY(SymI_HasProto(SelectPalette)) \
676 RTS_WIN64_ONLY(SymI_HasProto(SetArcDirection)) \
677 RTS_WIN64_ONLY(SymI_HasProto(SetBkColor)) \
678 RTS_WIN64_ONLY(SymI_HasProto(SetBkMode)) \
679 RTS_WIN64_ONLY(SymI_HasProto(SetBrushOrgEx)) \
680 RTS_WIN64_ONLY(SymI_HasProto(SetGraphicsMode)) \
681 RTS_WIN64_ONLY(SymI_HasProto(SetMiterLimit)) \
682 RTS_WIN64_ONLY(SymI_HasProto(SetPolyFillMode)) \
683 RTS_WIN64_ONLY(SymI_HasProto(SetStretchBltMode)) \
684 RTS_WIN64_ONLY(SymI_HasProto(SetTextAlign)) \
685 RTS_WIN64_ONLY(SymI_HasProto(SetTextCharacterExtra)) \
686 RTS_WIN64_ONLY(SymI_HasProto(SetTextColor)) \
687 RTS_WIN64_ONLY(SymI_HasProto(StretchBlt)) \
688 RTS_WIN64_ONLY(SymI_HasProto(StrokeAndFillPath)) \
689 RTS_WIN64_ONLY(SymI_HasProto(StrokePath)) \
690 RTS_WIN64_ONLY(SymI_HasProto(TextOutW)) \
691 RTS_WIN64_ONLY(SymI_HasProto(timeGetTime)) \
692 RTS_WIN64_ONLY(SymI_HasProto(WidenPath)) \
693 RTS_WIN64_ONLY(SymI_HasProto(GetFileSecurityW)) \
694 RTS_WIN64_ONLY(SymI_HasProto(RegCloseKey)) \
695 RTS_WIN64_ONLY(SymI_HasProto(RegConnectRegistryW)) \
696 RTS_WIN64_ONLY(SymI_HasProto(RegCreateKeyExW)) \
697 RTS_WIN64_ONLY(SymI_HasProto(RegCreateKeyW)) \
698 RTS_WIN64_ONLY(SymI_HasProto(RegDeleteKeyW)) \
699 RTS_WIN64_ONLY(SymI_HasProto(RegDeleteValueW)) \
700 RTS_WIN64_ONLY(SymI_HasProto(RegEnumKeyW)) \
701 RTS_WIN64_ONLY(SymI_HasProto(RegEnumValueW)) \
702 RTS_WIN64_ONLY(SymI_HasProto(RegFlushKey)) \
703 RTS_WIN64_ONLY(SymI_HasProto(RegLoadKeyW)) \
704 RTS_WIN64_ONLY(SymI_HasProto(RegNotifyChangeKeyValue)) \
705 RTS_WIN64_ONLY(SymI_HasProto(RegOpenKeyExW)) \
706 RTS_WIN64_ONLY(SymI_HasProto(RegOpenKeyW)) \
707 RTS_WIN64_ONLY(SymI_HasProto(RegQueryInfoKeyW)) \
708 RTS_WIN64_ONLY(SymI_HasProto(RegQueryValueExW)) \
709 RTS_WIN64_ONLY(SymI_HasProto(RegQueryValueW)) \
710 RTS_WIN64_ONLY(SymI_HasProto(RegReplaceKeyW)) \
711 RTS_WIN64_ONLY(SymI_HasProto(RegRestoreKeyW)) \
712 RTS_WIN64_ONLY(SymI_HasProto(RegSaveKeyW)) \
713 RTS_WIN64_ONLY(SymI_HasProto(RegSetValueExW)) \
714 RTS_WIN64_ONLY(SymI_HasProto(RegSetValueW)) \
715 RTS_WIN64_ONLY(SymI_HasProto(RegUnLoadKeyW)) \
716 RTS_WIN64_ONLY(SymI_NeedsProto(SHGetFolderPathW)) \
717 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_SetWindowLongPtrW)) \
718 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetWindowLongPtrW)) \
719 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_MenuItemFromPoint)) \
720 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_ChildWindowFromPoint)) \
721 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_ChildWindowFromPointEx)) \
722 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_DeleteObject)) \
723 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_UnmapViewOfFile)) \
724 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_CloseHandle)) \
725 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_FreeLibrary)) \
726 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetMessageW)) \
727 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_TranslateMessage)) \
728 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_DispatchMessageW)) \
729 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_DefWindowProcW)) \
730 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetDIBits)) \
731 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GlobalAlloc)) \
732 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GlobalFree)) \
733 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_CreateFileW)) \
734 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_WriteFile)) \
735 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_CreateCompatibleBitmap)) \
736 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_SelectObject)) \
737 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_Polygon)) \
738 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_FormatMessageW)) \
739 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__localtime64)) \
740 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__tzname)) \
741 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__timezone)) \
742 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_CreatePipe)) \
743 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_SetHandleInformation)) \
744 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetStdHandle)) \
745 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetCurrentProcess)) \
746 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_DuplicateHandle)) \
747 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_CreateProcessW)) \
748 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_TerminateProcess)) \
749 RTS_WIN64_ONLY(SymI_NeedsProto(__imp__open_osfhandle)) \
750 RTS_WIN64_ONLY(SymI_NeedsProto(__imp_GetExitCodeProcess)) \
751 RTS_MINGW_GETTIMEOFDAY_SYM \
752 SymI_NeedsProto(closedir)
753
754 #else
755 #define RTS_MINGW_ONLY_SYMBOLS /**/
756 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
757 #endif
758
759
760 #if defined(darwin_HOST_OS) && HAVE_PRINTF_LDBLSTUB
761 #define RTS_DARWIN_ONLY_SYMBOLS \
762 SymI_NeedsProto(asprintf$LDBLStub) \
763 SymI_NeedsProto(err$LDBLStub) \
764 SymI_NeedsProto(errc$LDBLStub) \
765 SymI_NeedsProto(errx$LDBLStub) \
766 SymI_NeedsProto(fprintf$LDBLStub) \
767 SymI_NeedsProto(fscanf$LDBLStub) \
768 SymI_NeedsProto(fwprintf$LDBLStub) \
769 SymI_NeedsProto(fwscanf$LDBLStub) \
770 SymI_NeedsProto(printf$LDBLStub) \
771 SymI_NeedsProto(scanf$LDBLStub) \
772 SymI_NeedsProto(snprintf$LDBLStub) \
773 SymI_NeedsProto(sprintf$LDBLStub) \
774 SymI_NeedsProto(sscanf$LDBLStub) \
775 SymI_NeedsProto(strtold$LDBLStub) \
776 SymI_NeedsProto(swprintf$LDBLStub) \
777 SymI_NeedsProto(swscanf$LDBLStub) \
778 SymI_NeedsProto(syslog$LDBLStub) \
779 SymI_NeedsProto(vasprintf$LDBLStub) \
780 SymI_NeedsProto(verr$LDBLStub) \
781 SymI_NeedsProto(verrc$LDBLStub) \
782 SymI_NeedsProto(verrx$LDBLStub) \
783 SymI_NeedsProto(vfprintf$LDBLStub) \
784 SymI_NeedsProto(vfscanf$LDBLStub) \
785 SymI_NeedsProto(vfwprintf$LDBLStub) \
786 SymI_NeedsProto(vfwscanf$LDBLStub) \
787 SymI_NeedsProto(vprintf$LDBLStub) \
788 SymI_NeedsProto(vscanf$LDBLStub) \
789 SymI_NeedsProto(vsnprintf$LDBLStub) \
790 SymI_NeedsProto(vsprintf$LDBLStub) \
791 SymI_NeedsProto(vsscanf$LDBLStub) \
792 SymI_NeedsProto(vswprintf$LDBLStub) \
793 SymI_NeedsProto(vswscanf$LDBLStub) \
794 SymI_NeedsProto(vsyslog$LDBLStub) \
795 SymI_NeedsProto(vwarn$LDBLStub) \
796 SymI_NeedsProto(vwarnc$LDBLStub) \
797 SymI_NeedsProto(vwarnx$LDBLStub) \
798 SymI_NeedsProto(vwprintf$LDBLStub) \
799 SymI_NeedsProto(vwscanf$LDBLStub) \
800 SymI_NeedsProto(warn$LDBLStub) \
801 SymI_NeedsProto(warnc$LDBLStub) \
802 SymI_NeedsProto(warnx$LDBLStub) \
803 SymI_NeedsProto(wcstold$LDBLStub) \
804 SymI_NeedsProto(wprintf$LDBLStub) \
805 SymI_NeedsProto(wscanf$LDBLStub)
806 #else
807 #define RTS_DARWIN_ONLY_SYMBOLS
808 #endif
809
810 #ifndef SMP
811 # define MAIN_CAP_SYM SymI_HasProto(MainCapability)
812 #else
813 # define MAIN_CAP_SYM
814 #endif
815
816 #if !defined(mingw32_HOST_OS)
817 #define RTS_USER_SIGNALS_SYMBOLS \
818 SymI_HasProto(setIOManagerControlFd) \
819 SymI_HasProto(setIOManagerWakeupFd) \
820 SymI_HasProto(ioManagerWakeup) \
821 SymI_HasProto(blockUserSignals) \
822 SymI_HasProto(unblockUserSignals)
823 #else
824 #define RTS_USER_SIGNALS_SYMBOLS \
825 SymI_HasProto(ioManagerWakeup) \
826 SymI_HasProto(sendIOManagerEvent) \
827 SymI_HasProto(readIOManagerEvent) \
828 SymI_HasProto(getIOManagerEvent) \
829 SymI_HasProto(console_handler)
830 #endif
831
832 #define RTS_LIBFFI_SYMBOLS \
833 SymE_NeedsProto(ffi_prep_cif) \
834 SymE_NeedsProto(ffi_call) \
835 SymE_NeedsProto(ffi_type_void) \
836 SymE_NeedsProto(ffi_type_float) \
837 SymE_NeedsProto(ffi_type_double) \
838 SymE_NeedsProto(ffi_type_sint64) \
839 SymE_NeedsProto(ffi_type_uint64) \
840 SymE_NeedsProto(ffi_type_sint32) \
841 SymE_NeedsProto(ffi_type_uint32) \
842 SymE_NeedsProto(ffi_type_sint16) \
843 SymE_NeedsProto(ffi_type_uint16) \
844 SymE_NeedsProto(ffi_type_sint8) \
845 SymE_NeedsProto(ffi_type_uint8) \
846 SymE_NeedsProto(ffi_type_pointer)
847
848 #ifdef TABLES_NEXT_TO_CODE
849 #define RTS_RET_SYMBOLS /* nothing */
850 #else
851 #define RTS_RET_SYMBOLS \
852 SymI_HasProto(stg_enter_ret) \
853 SymI_HasProto(stg_gc_fun_ret) \
854 SymI_HasProto(stg_ap_v_ret) \
855 SymI_HasProto(stg_ap_f_ret) \
856 SymI_HasProto(stg_ap_d_ret) \
857 SymI_HasProto(stg_ap_l_ret) \
858 SymI_HasProto(stg_ap_n_ret) \
859 SymI_HasProto(stg_ap_p_ret) \
860 SymI_HasProto(stg_ap_pv_ret) \
861 SymI_HasProto(stg_ap_pp_ret) \
862 SymI_HasProto(stg_ap_ppv_ret) \
863 SymI_HasProto(stg_ap_ppp_ret) \
864 SymI_HasProto(stg_ap_pppv_ret) \
865 SymI_HasProto(stg_ap_pppp_ret) \
866 SymI_HasProto(stg_ap_ppppp_ret) \
867 SymI_HasProto(stg_ap_pppppp_ret)
868 #endif
869
870 /* Modules compiled with -ticky may mention ticky counters */
871 /* This list should marry up with the one in $(TOP)/includes/stg/Ticky.h */
872 #define RTS_TICKY_SYMBOLS \
873 SymI_NeedsProto(ticky_entry_ctrs) \
874 SymI_NeedsProto(top_ct) \
875 \
876 SymI_HasProto(ENT_VIA_NODE_ctr) \
877 SymI_HasProto(ENT_STATIC_THK_ctr) \
878 SymI_HasProto(ENT_DYN_THK_ctr) \
879 SymI_HasProto(ENT_STATIC_FUN_DIRECT_ctr) \
880 SymI_HasProto(ENT_DYN_FUN_DIRECT_ctr) \
881 SymI_HasProto(ENT_STATIC_CON_ctr) \
882 SymI_HasProto(ENT_DYN_CON_ctr) \
883 SymI_HasProto(ENT_STATIC_IND_ctr) \
884 SymI_HasProto(ENT_DYN_IND_ctr) \
885 SymI_HasProto(ENT_PERM_IND_ctr) \
886 SymI_HasProto(ENT_PAP_ctr) \
887 SymI_HasProto(ENT_AP_ctr) \
888 SymI_HasProto(ENT_AP_STACK_ctr) \
889 SymI_HasProto(ENT_BH_ctr) \
890 SymI_HasProto(UNKNOWN_CALL_ctr) \
891 SymI_HasProto(SLOW_CALL_v_ctr) \
892 SymI_HasProto(SLOW_CALL_f_ctr) \
893 SymI_HasProto(SLOW_CALL_d_ctr) \
894 SymI_HasProto(SLOW_CALL_l_ctr) \
895 SymI_HasProto(SLOW_CALL_n_ctr) \
896 SymI_HasProto(SLOW_CALL_p_ctr) \
897 SymI_HasProto(SLOW_CALL_pv_ctr) \
898 SymI_HasProto(SLOW_CALL_pp_ctr) \
899 SymI_HasProto(SLOW_CALL_ppv_ctr) \
900 SymI_HasProto(SLOW_CALL_ppp_ctr) \
901 SymI_HasProto(SLOW_CALL_pppv_ctr) \
902 SymI_HasProto(SLOW_CALL_pppp_ctr) \
903 SymI_HasProto(SLOW_CALL_ppppp_ctr) \
904 SymI_HasProto(SLOW_CALL_pppppp_ctr) \
905 SymI_HasProto(SLOW_CALL_OTHER_ctr) \
906 SymI_HasProto(ticky_slow_call_unevald) \
907 SymI_HasProto(SLOW_CALL_ctr) \
908 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_ctr) \
909 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_CHUNKS_ctr) \
910 SymI_HasProto(KNOWN_CALL_ctr) \
911 SymI_HasProto(KNOWN_CALL_TOO_FEW_ARGS_ctr) \
912 SymI_HasProto(KNOWN_CALL_EXTRA_ARGS_ctr) \
913 SymI_HasProto(SLOW_CALL_FUN_TOO_FEW_ctr) \
914 SymI_HasProto(SLOW_CALL_FUN_CORRECT_ctr) \
915 SymI_HasProto(SLOW_CALL_FUN_TOO_MANY_ctr) \
916 SymI_HasProto(SLOW_CALL_PAP_TOO_FEW_ctr) \
917 SymI_HasProto(SLOW_CALL_PAP_CORRECT_ctr) \
918 SymI_HasProto(SLOW_CALL_PAP_TOO_MANY_ctr) \
919 SymI_HasProto(SLOW_CALL_UNEVALD_ctr) \
920 SymI_HasProto(UPDF_OMITTED_ctr) \
921 SymI_HasProto(UPDF_PUSHED_ctr) \
922 SymI_HasProto(CATCHF_PUSHED_ctr) \
923 SymI_HasProto(UPDF_RCC_PUSHED_ctr) \
924 SymI_HasProto(UPDF_RCC_OMITTED_ctr) \
925 SymI_HasProto(UPD_SQUEEZED_ctr) \
926 SymI_HasProto(UPD_CON_IN_NEW_ctr) \
927 SymI_HasProto(UPD_CON_IN_PLACE_ctr) \
928 SymI_HasProto(UPD_PAP_IN_NEW_ctr) \
929 SymI_HasProto(UPD_PAP_IN_PLACE_ctr) \
930 SymI_HasProto(ALLOC_HEAP_ctr) \
931 SymI_HasProto(ALLOC_HEAP_tot) \
932 SymI_HasProto(ALLOC_FUN_ctr) \
933 SymI_HasProto(ALLOC_FUN_adm) \
934 SymI_HasProto(ALLOC_FUN_gds) \
935 SymI_HasProto(ALLOC_FUN_slp) \
936 SymI_HasProto(UPD_NEW_IND_ctr) \
937 SymI_HasProto(UPD_NEW_PERM_IND_ctr) \
938 SymI_HasProto(UPD_OLD_IND_ctr) \
939 SymI_HasProto(UPD_OLD_PERM_IND_ctr) \
940 SymI_HasProto(UPD_BH_UPDATABLE_ctr) \
941 SymI_HasProto(UPD_BH_SINGLE_ENTRY_ctr) \
942 SymI_HasProto(UPD_CAF_BH_UPDATABLE_ctr) \
943 SymI_HasProto(UPD_CAF_BH_SINGLE_ENTRY_ctr) \
944 SymI_HasProto(GC_SEL_ABANDONED_ctr) \
945 SymI_HasProto(GC_SEL_MINOR_ctr) \
946 SymI_HasProto(GC_SEL_MAJOR_ctr) \
947 SymI_HasProto(GC_FAILED_PROMOTION_ctr) \
948 SymI_HasProto(ALLOC_UP_THK_ctr) \
949 SymI_HasProto(ALLOC_SE_THK_ctr) \
950 SymI_HasProto(ALLOC_THK_adm) \
951 SymI_HasProto(ALLOC_THK_gds) \
952 SymI_HasProto(ALLOC_THK_slp) \
953 SymI_HasProto(ALLOC_CON_ctr) \
954 SymI_HasProto(ALLOC_CON_adm) \
955 SymI_HasProto(ALLOC_CON_gds) \
956 SymI_HasProto(ALLOC_CON_slp) \
957 SymI_HasProto(ALLOC_TUP_ctr) \
958 SymI_HasProto(ALLOC_TUP_adm) \
959 SymI_HasProto(ALLOC_TUP_gds) \
960 SymI_HasProto(ALLOC_TUP_slp) \
961 SymI_HasProto(ALLOC_BH_ctr) \
962 SymI_HasProto(ALLOC_BH_adm) \
963 SymI_HasProto(ALLOC_BH_gds) \
964 SymI_HasProto(ALLOC_BH_slp) \
965 SymI_HasProto(ALLOC_PRIM_ctr) \
966 SymI_HasProto(ALLOC_PRIM_adm) \
967 SymI_HasProto(ALLOC_PRIM_gds) \
968 SymI_HasProto(ALLOC_PRIM_slp) \
969 SymI_HasProto(ALLOC_PAP_ctr) \
970 SymI_HasProto(ALLOC_PAP_adm) \
971 SymI_HasProto(ALLOC_PAP_gds) \
972 SymI_HasProto(ALLOC_PAP_slp) \
973 SymI_HasProto(ALLOC_TSO_ctr) \
974 SymI_HasProto(ALLOC_TSO_adm) \
975 SymI_HasProto(ALLOC_TSO_gds) \
976 SymI_HasProto(ALLOC_TSO_slp) \
977 SymI_HasProto(RET_NEW_ctr) \
978 SymI_HasProto(RET_OLD_ctr) \
979 SymI_HasProto(RET_UNBOXED_TUP_ctr) \
980 SymI_HasProto(RET_SEMI_loads_avoided)
981
982
983 // On most platforms, the garbage collector rewrites references
984 // to small integer and char objects to a set of common, shared ones.
985 //
986 // We don't do this when compiling to Windows DLLs at the moment because
987 // it doesn't support cross package data references well.
988 //
989 #if defined(COMPILING_WINDOWS_DLL)
990 #define RTS_INTCHAR_SYMBOLS
991 #else
992 #define RTS_INTCHAR_SYMBOLS \
993 SymI_HasProto(stg_CHARLIKE_closure) \
994 SymI_HasProto(stg_INTLIKE_closure)
995 #endif
996
997
998 #define RTS_SYMBOLS \
999 Maybe_Stable_Names \
1000 RTS_TICKY_SYMBOLS \
1001 SymI_HasProto(StgReturn) \
1002 SymI_HasProto(stg_enter_info) \
1003 SymI_HasProto(stg_gc_void_info) \
1004 SymI_HasProto(__stg_gc_enter_1) \
1005 SymI_HasProto(stg_gc_noregs) \
1006 SymI_HasProto(stg_gc_unpt_r1_info) \
1007 SymI_HasProto(stg_gc_unpt_r1) \
1008 SymI_HasProto(stg_gc_unbx_r1_info) \
1009 SymI_HasProto(stg_gc_unbx_r1) \
1010 SymI_HasProto(stg_gc_f1_info) \
1011 SymI_HasProto(stg_gc_f1) \
1012 SymI_HasProto(stg_gc_d1_info) \
1013 SymI_HasProto(stg_gc_d1) \
1014 SymI_HasProto(stg_gc_l1_info) \
1015 SymI_HasProto(stg_gc_l1) \
1016 SymI_HasProto(__stg_gc_fun) \
1017 SymI_HasProto(stg_gc_fun_info) \
1018 SymI_HasProto(stg_gc_gen) \
1019 SymI_HasProto(stg_gc_gen_info) \
1020 SymI_HasProto(stg_gc_gen_hp) \
1021 SymI_HasProto(stg_gc_ut) \
1022 SymI_HasProto(stg_gen_yield) \
1023 SymI_HasProto(stg_yield_noregs) \
1024 SymI_HasProto(stg_yield_to_interpreter) \
1025 SymI_HasProto(stg_gen_block) \
1026 SymI_HasProto(stg_block_noregs) \
1027 SymI_HasProto(stg_block_1) \
1028 SymI_HasProto(stg_block_takemvar) \
1029 SymI_HasProto(stg_block_putmvar) \
1030 MAIN_CAP_SYM \
1031 SymI_HasProto(MallocFailHook) \
1032 SymI_HasProto(OnExitHook) \
1033 SymI_HasProto(OutOfHeapHook) \
1034 SymI_HasProto(StackOverflowHook) \
1035 SymI_HasProto(addDLL) \
1036 SymI_HasProto(__int_encodeDouble) \
1037 SymI_HasProto(__word_encodeDouble) \
1038 SymI_HasProto(__2Int_encodeDouble) \
1039 SymI_HasProto(__int_encodeFloat) \
1040 SymI_HasProto(__word_encodeFloat) \
1041 SymI_HasProto(stg_atomicallyzh) \
1042 SymI_HasProto(barf) \
1043 SymI_HasProto(debugBelch) \
1044 SymI_HasProto(errorBelch) \
1045 SymI_HasProto(sysErrorBelch) \
1046 SymI_HasProto(stg_getMaskingStatezh) \
1047 SymI_HasProto(stg_maskAsyncExceptionszh) \
1048 SymI_HasProto(stg_maskUninterruptiblezh) \
1049 SymI_HasProto(stg_catchzh) \
1050 SymI_HasProto(stg_catchRetryzh) \
1051 SymI_HasProto(stg_catchSTMzh) \
1052 SymI_HasProto(stg_checkzh) \
1053 SymI_HasProto(closure_flags) \
1054 SymI_HasProto(cmp_thread) \
1055 SymI_HasProto(createAdjustor) \
1056 SymI_HasProto(stg_decodeDoublezu2Intzh) \
1057 SymI_HasProto(stg_decodeFloatzuIntzh) \
1058 SymI_HasProto(defaultsHook) \
1059 SymI_HasProto(stg_delayzh) \
1060 SymI_HasProto(stg_deRefWeakzh) \
1061 SymI_HasProto(stg_deRefStablePtrzh) \
1062 SymI_HasProto(dirty_MUT_VAR) \
1063 SymI_HasProto(stg_forkzh) \
1064 SymI_HasProto(stg_forkOnzh) \
1065 SymI_HasProto(forkProcess) \
1066 SymI_HasProto(forkOS_createThread) \
1067 SymI_HasProto(freeHaskellFunctionPtr) \
1068 SymI_HasProto(getOrSetGHCConcSignalSignalHandlerStore) \
1069 SymI_HasProto(getOrSetGHCConcWindowsPendingDelaysStore) \
1070 SymI_HasProto(getOrSetGHCConcWindowsIOManagerThreadStore) \
1071 SymI_HasProto(getOrSetGHCConcWindowsProddingStore) \
1072 SymI_HasProto(getOrSetSystemEventThreadEventManagerStore) \
1073 SymI_HasProto(getOrSetSystemEventThreadIOManagerThreadStore) \
1074 SymI_HasProto(getGCStats) \
1075 SymI_HasProto(getGCStatsEnabled) \
1076 SymI_HasProto(genSymZh) \
1077 SymI_HasProto(genericRaise) \
1078 SymI_HasProto(getProgArgv) \
1079 SymI_HasProto(getFullProgArgv) \
1080 SymI_HasProto(getStablePtr) \
1081 SymI_HasProto(hs_init) \
1082 SymI_HasProto(hs_exit) \
1083 SymI_HasProto(hs_set_argv) \
1084 SymI_HasProto(hs_add_root) \
1085 SymI_HasProto(hs_perform_gc) \
1086 SymI_HasProto(hs_free_stable_ptr) \
1087 SymI_HasProto(hs_free_fun_ptr) \
1088 SymI_HasProto(hs_hpc_rootModule) \
1089 SymI_HasProto(hs_hpc_module) \
1090 SymI_HasProto(initLinker) \
1091 SymI_HasProto(stg_unpackClosurezh) \
1092 SymI_HasProto(stg_getApStackValzh) \
1093 SymI_HasProto(stg_getSparkzh) \
1094 SymI_HasProto(stg_numSparkszh) \
1095 SymI_HasProto(stg_isCurrentThreadBoundzh) \
1096 SymI_HasProto(stg_isEmptyMVarzh) \
1097 SymI_HasProto(stg_killThreadzh) \
1098 SymI_HasProto(loadArchive) \
1099 SymI_HasProto(loadObj) \
1100 SymI_HasProto(insertStableSymbol) \
1101 SymI_HasProto(insertSymbol) \
1102 SymI_HasProto(lookupSymbol) \
1103 SymI_HasProto(stg_makeStablePtrzh) \
1104 SymI_HasProto(stg_mkApUpd0zh) \
1105 SymI_HasProto(stg_myThreadIdzh) \
1106 SymI_HasProto(stg_labelThreadzh) \
1107 SymI_HasProto(stg_newArrayzh) \
1108 SymI_HasProto(stg_newArrayArrayzh) \
1109 SymI_HasProto(stg_newBCOzh) \
1110 SymI_HasProto(stg_newByteArrayzh) \
1111 SymI_HasProto_redirect(newCAF, newDynCAF) \
1112 SymI_HasProto(stg_newMVarzh) \
1113 SymI_HasProto(stg_newMutVarzh) \
1114 SymI_HasProto(stg_newTVarzh) \
1115 SymI_HasProto(stg_noDuplicatezh) \
1116 SymI_HasProto(stg_atomicModifyMutVarzh) \
1117 SymI_HasProto(stg_casMutVarzh) \
1118 SymI_HasProto(stg_newPinnedByteArrayzh) \
1119 SymI_HasProto(stg_newAlignedPinnedByteArrayzh) \
1120 SymI_HasProto(newSpark) \
1121 SymI_HasProto(performGC) \
1122 SymI_HasProto(performMajorGC) \
1123 SymI_HasProto(prog_argc) \
1124 SymI_HasProto(prog_argv) \
1125 SymI_HasProto(stg_putMVarzh) \
1126 SymI_HasProto(stg_raisezh) \
1127 SymI_HasProto(stg_raiseIOzh) \
1128 SymI_HasProto(stg_readTVarzh) \
1129 SymI_HasProto(stg_readTVarIOzh) \
1130 SymI_HasProto(resumeThread) \
1131 SymI_HasProto(setNumCapabilities) \
1132 SymI_HasProto(getNumberOfProcessors) \
1133 SymI_HasProto(resolveObjs) \
1134 SymI_HasProto(stg_retryzh) \
1135 SymI_HasProto(rts_apply) \
1136 SymI_HasProto(rts_checkSchedStatus) \
1137 SymI_HasProto(rts_eval) \
1138 SymI_HasProto(rts_evalIO) \
1139 SymI_HasProto(rts_evalLazyIO) \
1140 SymI_HasProto(rts_evalStableIO) \
1141 SymI_HasProto(rts_eval_) \
1142 SymI_HasProto(rts_getBool) \
1143 SymI_HasProto(rts_getChar) \
1144 SymI_HasProto(rts_getDouble) \
1145 SymI_HasProto(rts_getFloat) \
1146 SymI_HasProto(rts_getInt) \
1147 SymI_HasProto(rts_getInt8) \
1148 SymI_HasProto(rts_getInt16) \
1149 SymI_HasProto(rts_getInt32) \
1150 SymI_HasProto(rts_getInt64) \
1151 SymI_HasProto(rts_getPtr) \
1152 SymI_HasProto(rts_getFunPtr) \
1153 SymI_HasProto(rts_getStablePtr) \
1154 SymI_HasProto(rts_getThreadId) \
1155 SymI_HasProto(rts_getWord) \
1156 SymI_HasProto(rts_getWord8) \
1157 SymI_HasProto(rts_getWord16) \
1158 SymI_HasProto(rts_getWord32) \
1159 SymI_HasProto(rts_getWord64) \
1160 SymI_HasProto(rts_lock) \
1161 SymI_HasProto(rts_mkBool) \
1162 SymI_HasProto(rts_mkChar) \
1163 SymI_HasProto(rts_mkDouble) \
1164 SymI_HasProto(rts_mkFloat) \
1165 SymI_HasProto(rts_mkInt) \
1166 SymI_HasProto(rts_mkInt8) \
1167 SymI_HasProto(rts_mkInt16) \
1168 SymI_HasProto(rts_mkInt32) \
1169 SymI_HasProto(rts_mkInt64) \
1170 SymI_HasProto(rts_mkPtr) \
1171 SymI_HasProto(rts_mkFunPtr) \
1172 SymI_HasProto(rts_mkStablePtr) \
1173 SymI_HasProto(rts_mkString) \
1174 SymI_HasProto(rts_mkWord) \
1175 SymI_HasProto(rts_mkWord8) \
1176 SymI_HasProto(rts_mkWord16) \
1177 SymI_HasProto(rts_mkWord32) \
1178 SymI_HasProto(rts_mkWord64) \
1179 SymI_HasProto(rts_unlock) \
1180 SymI_HasProto(rts_unsafeGetMyCapability) \
1181 SymI_HasProto(rtsSupportsBoundThreads) \
1182 SymI_HasProto(rts_isProfiled) \
1183 SymI_HasProto(setProgArgv) \
1184 SymI_HasProto(startupHaskell) \
1185 SymI_HasProto(shutdownHaskell) \
1186 SymI_HasProto(shutdownHaskellAndExit) \
1187 SymI_HasProto(stable_ptr_table) \
1188 SymI_HasProto(stackOverflow) \
1189 SymI_HasProto(stg_CAF_BLACKHOLE_info) \
1190 SymI_HasProto(stg_BLACKHOLE_info) \
1191 SymI_HasProto(__stg_EAGER_BLACKHOLE_info) \
1192 SymI_HasProto(stg_BLOCKING_QUEUE_CLEAN_info) \
1193 SymI_HasProto(stg_BLOCKING_QUEUE_DIRTY_info) \
1194 SymI_HasProto(startTimer) \
1195 SymI_HasProto(stg_MVAR_CLEAN_info) \
1196 SymI_HasProto(stg_MVAR_DIRTY_info) \
1197 SymI_HasProto(stg_IND_STATIC_info) \
1198 SymI_HasProto(stg_ARR_WORDS_info) \
1199 SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info) \
1200 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info) \
1201 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info) \
1202 SymI_HasProto(stg_WEAK_info) \
1203 SymI_HasProto(stg_ap_v_info) \
1204 SymI_HasProto(stg_ap_f_info) \
1205 SymI_HasProto(stg_ap_d_info) \
1206 SymI_HasProto(stg_ap_l_info) \
1207 SymI_HasProto(stg_ap_n_info) \
1208 SymI_HasProto(stg_ap_p_info) \
1209 SymI_HasProto(stg_ap_pv_info) \
1210 SymI_HasProto(stg_ap_pp_info) \
1211 SymI_HasProto(stg_ap_ppv_info) \
1212 SymI_HasProto(stg_ap_ppp_info) \
1213 SymI_HasProto(stg_ap_pppv_info) \
1214 SymI_HasProto(stg_ap_pppp_info) \
1215 SymI_HasProto(stg_ap_ppppp_info) \
1216 SymI_HasProto(stg_ap_pppppp_info) \
1217 SymI_HasProto(stg_ap_0_fast) \
1218 SymI_HasProto(stg_ap_v_fast) \
1219 SymI_HasProto(stg_ap_f_fast) \
1220 SymI_HasProto(stg_ap_d_fast) \
1221 SymI_HasProto(stg_ap_l_fast) \
1222 SymI_HasProto(stg_ap_n_fast) \
1223 SymI_HasProto(stg_ap_p_fast) \
1224 SymI_HasProto(stg_ap_pv_fast) \
1225 SymI_HasProto(stg_ap_pp_fast) \
1226 SymI_HasProto(stg_ap_ppv_fast) \
1227 SymI_HasProto(stg_ap_ppp_fast) \
1228 SymI_HasProto(stg_ap_pppv_fast) \
1229 SymI_HasProto(stg_ap_pppp_fast) \
1230 SymI_HasProto(stg_ap_ppppp_fast) \
1231 SymI_HasProto(stg_ap_pppppp_fast) \
1232 SymI_HasProto(stg_ap_1_upd_info) \
1233 SymI_HasProto(stg_ap_2_upd_info) \
1234 SymI_HasProto(stg_ap_3_upd_info) \
1235 SymI_HasProto(stg_ap_4_upd_info) \
1236 SymI_HasProto(stg_ap_5_upd_info) \
1237 SymI_HasProto(stg_ap_6_upd_info) \
1238 SymI_HasProto(stg_ap_7_upd_info) \
1239 SymI_HasProto(stg_exit) \
1240 SymI_HasProto(stg_sel_0_upd_info) \
1241 SymI_HasProto(stg_sel_10_upd_info) \
1242 SymI_HasProto(stg_sel_11_upd_info) \
1243 SymI_HasProto(stg_sel_12_upd_info) \
1244 SymI_HasProto(stg_sel_13_upd_info) \
1245 SymI_HasProto(stg_sel_14_upd_info) \
1246 SymI_HasProto(stg_sel_15_upd_info) \
1247 SymI_HasProto(stg_sel_1_upd_info) \
1248 SymI_HasProto(stg_sel_2_upd_info) \
1249 SymI_HasProto(stg_sel_3_upd_info) \
1250 SymI_HasProto(stg_sel_4_upd_info) \
1251 SymI_HasProto(stg_sel_5_upd_info) \
1252 SymI_HasProto(stg_sel_6_upd_info) \
1253 SymI_HasProto(stg_sel_7_upd_info) \
1254 SymI_HasProto(stg_sel_8_upd_info) \
1255 SymI_HasProto(stg_sel_9_upd_info) \
1256 SymI_HasProto(stg_upd_frame_info) \
1257 SymI_HasProto(stg_bh_upd_frame_info) \
1258 SymI_HasProto(suspendThread) \
1259 SymI_HasProto(stg_takeMVarzh) \
1260 SymI_HasProto(stg_threadStatuszh) \
1261 SymI_HasProto(stg_tryPutMVarzh) \
1262 SymI_HasProto(stg_tryTakeMVarzh) \
1263 SymI_HasProto(stg_unmaskAsyncExceptionszh) \
1264 SymI_HasProto(unloadObj) \
1265 SymI_HasProto(stg_unsafeThawArrayzh) \
1266 SymI_HasProto(stg_waitReadzh) \
1267 SymI_HasProto(stg_waitWritezh) \
1268 SymI_HasProto(stg_writeTVarzh) \
1269 SymI_HasProto(stg_yieldzh) \
1270 SymI_NeedsProto(stg_interp_constr_entry) \
1271 SymI_HasProto(stg_arg_bitmaps) \
1272 SymI_HasProto(large_alloc_lim) \
1273 SymI_HasProto(g0) \
1274 SymI_HasProto(allocate) \
1275 SymI_HasProto(allocateExec) \
1276 SymI_HasProto(freeExec) \
1277 SymI_HasProto(getAllocations) \
1278 SymI_HasProto(revertCAFs) \
1279 SymI_HasProto(RtsFlags) \
1280 SymI_NeedsProto(rts_breakpoint_io_action) \
1281 SymI_NeedsProto(rts_stop_next_breakpoint) \
1282 SymI_NeedsProto(rts_stop_on_exception) \
1283 SymI_HasProto(stopTimer) \
1284 SymI_HasProto(n_capabilities) \
1285 SymI_HasProto(stg_traceCcszh) \
1286 SymI_HasProto(stg_traceEventzh) \
1287 SymI_HasProto(getMonotonicNSec) \
1288 SymI_HasProto(lockFile) \
1289 SymI_HasProto(unlockFile) \
1290 SymI_NeedsProto(startProfTimer) \
1291 SymI_NeedsProto(stopProfTimer) \
1292 RTS_USER_SIGNALS_SYMBOLS \
1293 RTS_INTCHAR_SYMBOLS
1294
1295
1296 // 64-bit support functions in libgcc.a
1297 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4 && !defined(_ABIN32)
1298 #define RTS_LIBGCC_SYMBOLS \
1299 SymI_NeedsProto(__divdi3) \
1300 SymI_NeedsProto(__udivdi3) \
1301 SymI_NeedsProto(__moddi3) \
1302 SymI_NeedsProto(__umoddi3) \
1303 SymI_NeedsProto(__muldi3) \
1304 SymI_NeedsProto(__ashldi3) \
1305 SymI_NeedsProto(__ashrdi3) \
1306 SymI_NeedsProto(__lshrdi3) \
1307 SymI_NeedsProto(__fixunsdfdi)
1308 #else
1309 #define RTS_LIBGCC_SYMBOLS
1310 #endif
1311
1312 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
1313 // Symbols that don't have a leading underscore
1314 // on Mac OS X. They have to receive special treatment,
1315 // see machoInitSymbolsWithoutUnderscore()
1316 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
1317 SymI_NeedsProto(saveFP) \
1318 SymI_NeedsProto(restFP)
1319 #endif
1320
1321 /* entirely bogus claims about types of these symbols */
1322 #define SymI_NeedsProto(vvv) extern void vvv(void);
1323 #if defined(COMPILING_WINDOWS_DLL)
1324 #define SymE_HasProto(vvv) SymE_HasProto(vvv);
1325 # if defined(x86_64_HOST_ARCH)
1326 # define SymE_NeedsProto(vvv) extern void __imp_ ## vvv (void);
1327 # else
1328 # define SymE_NeedsProto(vvv) extern void _imp__ ## vvv (void);
1329 # endif
1330 #else
1331 #define SymE_NeedsProto(vvv) SymI_NeedsProto(vvv);
1332 #define SymE_HasProto(vvv) SymI_HasProto(vvv)
1333 #endif
1334 #define SymI_HasProto(vvv) /**/
1335 #define SymI_HasProto_redirect(vvv,xxx) /**/
1336 RTS_SYMBOLS
1337 RTS_RET_SYMBOLS
1338 RTS_POSIX_ONLY_SYMBOLS
1339 RTS_MINGW_ONLY_SYMBOLS
1340 RTS_CYGWIN_ONLY_SYMBOLS
1341 RTS_DARWIN_ONLY_SYMBOLS
1342 RTS_LIBGCC_SYMBOLS
1343 RTS_LIBFFI_SYMBOLS
1344 #undef SymI_NeedsProto
1345 #undef SymI_HasProto
1346 #undef SymI_HasProto_redirect
1347 #undef SymE_HasProto
1348 #undef SymE_NeedsProto
1349
1350 #ifdef LEADING_UNDERSCORE
1351 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
1352 #else
1353 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
1354 #endif
1355
1356 #define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1357 (void*)(&(vvv)) },
1358 #define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1359 (void*)DLL_IMPORT_DATA_REF(vvv) },
1360
1361 #define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
1362 #define SymE_NeedsProto(vvv) SymE_HasProto(vvv)
1363
1364 // SymI_HasProto_redirect allows us to redirect references to one symbol to
1365 // another symbol. See newCAF/newDynCAF for an example.
1366 #define SymI_HasProto_redirect(vvv,xxx) \
1367 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1368 (void*)(&(xxx)) },
1369
1370 static RtsSymbolVal rtsSyms[] = {
1371 RTS_SYMBOLS
1372 RTS_RET_SYMBOLS
1373 RTS_POSIX_ONLY_SYMBOLS
1374 RTS_MINGW_ONLY_SYMBOLS
1375 RTS_CYGWIN_ONLY_SYMBOLS
1376 RTS_DARWIN_ONLY_SYMBOLS
1377 RTS_LIBGCC_SYMBOLS
1378 RTS_LIBFFI_SYMBOLS
1379 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
1380 // dyld stub code contains references to this,
1381 // but it should never be called because we treat
1382 // lazy pointers as nonlazy.
1383 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
1384 #endif
1385 { 0, 0 } /* sentinel */
1386 };
1387
1388
1389 /* -----------------------------------------------------------------------------
1390 * Insert symbols into hash tables, checking for duplicates.
1391 */
1392
1393 static void ghciInsertStrHashTable ( pathchar* obj_name,
1394 HashTable *table,
1395 char* key,
1396 void *data
1397 )
1398 {
1399 if (lookupHashTable(table, (StgWord)key) == NULL)
1400 {
1401 insertStrHashTable(table, (StgWord)key, data);
1402 return;
1403 }
1404 debugBelch(
1405 "\n\n"
1406 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
1407 " %s\n"
1408 "whilst processing object file\n"
1409 " %" PATH_FMT "\n"
1410 "This could be caused by:\n"
1411 " * Loading two different object files which export the same symbol\n"
1412 " * Specifying the same object file twice on the GHCi command line\n"
1413 " * An incorrect `package.conf' entry, causing some object to be\n"
1414 " loaded twice.\n"
1415 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
1416 "\n",
1417 (char*)key,
1418 obj_name
1419 );
1420 stg_exit(1);
1421 }
1422 /* -----------------------------------------------------------------------------
1423 * initialize the object linker
1424 */
1425
1426
1427 static int linker_init_done = 0 ;
1428
1429 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1430 static void *dl_prog_handle;
1431 static regex_t re_invalid;
1432 static regex_t re_realso;
1433 #ifdef THREADED_RTS
1434 static Mutex dl_mutex; // mutex to protect dlopen/dlerror critical section
1435 #endif
1436 #endif
1437
1438 void
1439 initLinker( void )
1440 {
1441 RtsSymbolVal *sym;
1442 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1443 int compileResult;
1444 #endif
1445
1446 IF_DEBUG(linker, debugBelch("initLinker: start\n"));
1447
1448 /* Make initLinker idempotent, so we can call it
1449 before evey relevant operation; that means we
1450 don't need to initialise the linker separately */
1451 if (linker_init_done == 1) {
1452 IF_DEBUG(linker, debugBelch("initLinker: idempotent return\n"));
1453 return;
1454 } else {
1455 linker_init_done = 1;
1456 }
1457
1458 #if defined(THREADED_RTS) && (defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO))
1459 initMutex(&dl_mutex);
1460 #endif
1461 stablehash = allocStrHashTable();
1462 symhash = allocStrHashTable();
1463
1464 /* populate the symbol table with stuff from the RTS */
1465 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
1466 ghciInsertStrHashTable(WSTR("(GHCi built-in symbols)"),
1467 symhash, sym->lbl, sym->addr);
1468 IF_DEBUG(linker, debugBelch("initLinker: inserting rts symbol %s, %p\n", sym->lbl, sym->addr));
1469 }
1470 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1471 machoInitSymbolsWithoutUnderscore();
1472 # endif
1473
1474 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1475 # if defined(RTLD_DEFAULT)
1476 dl_prog_handle = RTLD_DEFAULT;
1477 # else
1478 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
1479 # endif /* RTLD_DEFAULT */
1480
1481 compileResult = regcomp(&re_invalid,
1482 "(([^ \t()])+\\.so([^ \t:()])*):([ \t])*(invalid ELF header|file too short)",
1483 REG_EXTENDED);
1484 if (compileResult != 0) {
1485 barf("Compiling re_invalid failed");
1486 }
1487 compileResult = regcomp(&re_realso,
1488 "(GROUP|INPUT) *\\( *([^ )]+)",
1489 REG_EXTENDED);
1490 if (compileResult != 0) {
1491 barf("Compiling re_realso failed");
1492 }
1493 # endif
1494
1495 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1496 if (RtsFlags.MiscFlags.linkerMemBase != 0) {
1497 // User-override for mmap_32bit_base
1498 mmap_32bit_base = (void*)RtsFlags.MiscFlags.linkerMemBase;
1499 }
1500 #endif
1501
1502 #if defined(mingw32_HOST_OS)
1503 /*
1504 * These two libraries cause problems when added to the static link,
1505 * but are necessary for resolving symbols in GHCi, hence we load
1506 * them manually here.
1507 */
1508 addDLL(WSTR("msvcrt"));
1509 addDLL(WSTR("kernel32"));
1510 #endif
1511
1512 IF_DEBUG(linker, debugBelch("initLinker: done\n"));
1513 return;
1514 }
1515
1516 void
1517 exitLinker( void ) {
1518 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1519 if (linker_init_done == 1) {
1520 regfree(&re_invalid);
1521 regfree(&re_realso);
1522 #ifdef THREADED_RTS
1523 closeMutex(&dl_mutex);
1524 #endif
1525 }
1526 #endif
1527 }
1528
1529 /* -----------------------------------------------------------------------------
1530 * Loading DLL or .so dynamic libraries
1531 * -----------------------------------------------------------------------------
1532 *
1533 * Add a DLL from which symbols may be found. In the ELF case, just
1534 * do RTLD_GLOBAL-style add, so no further messing around needs to
1535 * happen in order that symbols in the loaded .so are findable --
1536 * lookupSymbol() will subsequently see them by dlsym on the program's
1537 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
1538 *
1539 * In the PEi386 case, open the DLLs and put handles to them in a
1540 * linked list. When looking for a symbol, try all handles in the
1541 * list. This means that we need to load even DLLs that are guaranteed
1542 * to be in the ghc.exe image already, just so we can get a handle
1543 * to give to loadSymbol, so that we can find the symbols. For such
1544 * libraries, the LoadLibrary call should be a no-op except for returning
1545 * the handle.
1546 *
1547 */
1548
1549 #if defined(OBJFORMAT_PEi386)
1550 /* A record for storing handles into DLLs. */
1551
1552 typedef
1553 struct _OpenedDLL {
1554 pathchar* name;
1555 struct _OpenedDLL* next;
1556 HINSTANCE instance;
1557 }
1558 OpenedDLL;
1559
1560 /* A list thereof. */
1561 static OpenedDLL* opened_dlls = NULL;
1562 #endif
1563
1564 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1565
1566 static const char *
1567 internal_dlopen(const char *dll_name)
1568 {
1569 void *hdl;
1570 const char *errmsg;
1571 char *errmsg_copy;
1572
1573 // omitted: RTLD_NOW
1574 // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
1575 IF_DEBUG(linker,
1576 debugBelch("internal_dlopen: dll_name = '%s'\n", dll_name));
1577
1578 //-------------- Begin critical section ------------------
1579 // This critical section is necessary because dlerror() is not
1580 // required to be reentrant (see POSIX -- IEEE Std 1003.1-2008)
1581 // Also, the error message returned must be copied to preserve it
1582 // (see POSIX also)
1583
1584 ACQUIRE_LOCK(&dl_mutex);
1585 hdl = dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
1586
1587 errmsg = NULL;
1588 if (hdl == NULL) {
1589 /* dlopen failed; return a ptr to the error msg. */
1590 errmsg = dlerror();
1591 if (errmsg == NULL) errmsg = "addDLL: unknown error";
1592 errmsg_copy = stgMallocBytes(strlen(errmsg)+1, "addDLL");
1593 strcpy(errmsg_copy, errmsg);
1594 errmsg = errmsg_copy;
1595 }
1596 RELEASE_LOCK(&dl_mutex);
1597 //--------------- End critical section -------------------
1598
1599 return errmsg;
1600 }
1601 # endif
1602
1603 const char *
1604 addDLL( pathchar *dll_name )
1605 {
1606 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1607 /* ------------------- ELF DLL loader ------------------- */
1608
1609 #define NMATCH 5
1610 regmatch_t match[NMATCH];
1611 const char *errmsg;
1612 FILE* fp;
1613 size_t match_length;
1614 #define MAXLINE 1000
1615 char line[MAXLINE];
1616 int result;
1617
1618 initLinker();
1619
1620 IF_DEBUG(linker, debugBelch("addDLL: dll_name = '%s'\n", dll_name));
1621 errmsg = internal_dlopen(dll_name);
1622
1623 if (errmsg == NULL) {
1624 return NULL;
1625 }
1626
1627 // GHC Trac ticket #2615
1628 // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so)
1629 // contain linker scripts rather than ELF-format object code. This
1630 // code handles the situation by recognizing the real object code
1631 // file name given in the linker script.
1632 //
1633 // If an "invalid ELF header" error occurs, it is assumed that the
1634 // .so file contains a linker script instead of ELF object code.
1635 // In this case, the code looks for the GROUP ( ... ) linker
1636 // directive. If one is found, the first file name inside the
1637 // parentheses is treated as the name of a dynamic library and the
1638 // code attempts to dlopen that file. If this is also unsuccessful,
1639 // an error message is returned.
1640
1641 // see if the error message is due to an invalid ELF header
1642 IF_DEBUG(linker, debugBelch("errmsg = '%s'\n", errmsg));
1643 result = regexec(&re_invalid, errmsg, (size_t) NMATCH, match, 0);
1644 IF_DEBUG(linker, debugBelch("result = %i\n", result));
1645 if (result == 0) {
1646 // success -- try to read the named file as a linker script
1647 match_length = (size_t) stg_min((match[1].rm_eo - match[1].rm_so),
1648 MAXLINE-1);
1649 strncpy(line, (errmsg+(match[1].rm_so)),match_length);
1650 line[match_length] = '\0'; // make sure string is null-terminated
1651 IF_DEBUG(linker, debugBelch ("file name = '%s'\n", line));
1652 if ((fp = fopen(line, "r")) == NULL) {
1653 return errmsg; // return original error if open fails
1654 }
1655 // try to find a GROUP or INPUT ( ... ) command
1656 while (fgets(line, MAXLINE, fp) != NULL) {
1657 IF_DEBUG(linker, debugBelch("input line = %s", line));
1658 if (regexec(&re_realso, line, (size_t) NMATCH, match, 0) == 0) {
1659 // success -- try to dlopen the first named file
1660 IF_DEBUG(linker, debugBelch("match%s\n",""));
1661 line[match[2].rm_eo] = '\0';
1662 errmsg = internal_dlopen(line+match[2].rm_so);
1663 break;
1664 }
1665 // if control reaches here, no GROUP or INPUT ( ... ) directive
1666 // was found and the original error message is returned to the
1667 // caller
1668 }
1669 fclose(fp);
1670 }
1671 return errmsg;
1672
1673 # elif defined(OBJFORMAT_PEi386)
1674 /* ------------------- Win32 DLL loader ------------------- */
1675
1676 pathchar* buf;
1677 OpenedDLL* o_dll;
1678 HINSTANCE instance;
1679
1680 initLinker();
1681
1682 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1683
1684 /* See if we've already got it, and ignore if so. */
1685 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1686 if (0 == pathcmp(o_dll->name, dll_name))
1687 return NULL;
1688 }
1689
1690 /* The file name has no suffix (yet) so that we can try
1691 both foo.dll and foo.drv
1692
1693 The documentation for LoadLibrary says:
1694 If no file name extension is specified in the lpFileName
1695 parameter, the default library extension .dll is
1696 appended. However, the file name string can include a trailing
1697 point character (.) to indicate that the module name has no
1698 extension. */
1699
1700 buf = stgMallocBytes((pathlen(dll_name) + 10) * sizeof(wchar_t), "addDLL");
1701 swprintf(buf, L"%s.DLL", dll_name);
1702 instance = LoadLibraryW(buf);
1703 if (instance == NULL) {
1704 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1705 // KAA: allow loading of drivers (like winspool.drv)
1706 swprintf(buf, L"%s.DRV", dll_name);
1707 instance = LoadLibraryW(buf);
1708 if (instance == NULL) {
1709 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1710 // #1883: allow loading of unix-style libfoo.dll DLLs
1711 swprintf(buf, L"lib%s.DLL", dll_name);
1712 instance = LoadLibraryW(buf);
1713 if (instance == NULL) {
1714 goto error;
1715 }
1716 }
1717 }
1718 stgFree(buf);
1719
1720 /* Add this DLL to the list of DLLs in which to search for symbols. */
1721 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1722 o_dll->name = pathdup(dll_name);
1723 o_dll->instance = instance;
1724 o_dll->next = opened_dlls;
1725 opened_dlls = o_dll;
1726
1727 return NULL;
1728
1729 error:
1730 stgFree(buf);
1731 sysErrorBelch("%" PATH_FMT, dll_name);
1732
1733 /* LoadLibrary failed; return a ptr to the error msg. */
1734 return "addDLL: could not load DLL";
1735
1736 # else
1737 barf("addDLL: not implemented on this platform");
1738 # endif
1739 }
1740
1741 /* -----------------------------------------------------------------------------
1742 * insert a stable symbol in the hash table
1743 */
1744
1745 void
1746 insertStableSymbol(pathchar* obj_name, char* key, StgPtr p)
1747 {
1748 ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
1749 }
1750
1751
1752 /* -----------------------------------------------------------------------------
1753 * insert a symbol in the hash table
1754 */
1755 void
1756 insertSymbol(pathchar* obj_name, char* key, void* data)
1757 {
1758 ghciInsertStrHashTable(obj_name, symhash, key, data);
1759 }
1760
1761 /* -----------------------------------------------------------------------------
1762 * lookup a symbol in the hash table
1763 */
1764 void *
1765 lookupSymbol( char *lbl )
1766 {
1767 void *val;
1768 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s\n", lbl));
1769 initLinker() ;
1770 ASSERT(symhash != NULL);
1771 val = lookupStrHashTable(symhash, lbl);
1772
1773 if (val == NULL) {
1774 IF_DEBUG(linker, debugBelch("lookupSymbol: symbol not found\n"));
1775 # if defined(OBJFORMAT_ELF)
1776 return dlsym(dl_prog_handle, lbl);
1777 # elif defined(OBJFORMAT_MACHO)
1778 # if HAVE_DLFCN_H
1779 /* On OS X 10.3 and later, we use dlsym instead of the old legacy
1780 interface.
1781
1782 HACK: On OS X, all symbols are prefixed with an underscore.
1783 However, dlsym wants us to omit the leading underscore from the
1784 symbol name -- the dlsym routine puts it back on before searching
1785 for the symbol. For now, we simply strip it off here (and ONLY
1786 here).
1787 */
1788 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s with dlsym\n", lbl));
1789 ASSERT(lbl[0] == '_');
1790 return dlsym(dl_prog_handle, lbl + 1);
1791 # else
1792 if (NSIsSymbolNameDefined(lbl)) {
1793 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1794 return NSAddressOfSymbol(symbol);
1795 } else {
1796 return NULL;
1797 }
1798 # endif /* HAVE_DLFCN_H */
1799 # elif defined(OBJFORMAT_PEi386)
1800 void* sym;
1801
1802 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1803 if (sym != NULL) { return sym; };
1804
1805 // Also try looking up the symbol without the @N suffix. Some
1806 // DLLs have the suffixes on their symbols, some don't.
1807 zapTrailingAtSign ( (unsigned char*)lbl );
1808 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1809 if (sym != NULL) { return sym; };
1810 return NULL;
1811
1812 # else
1813 ASSERT(2+2 == 5);
1814 return NULL;
1815 # endif
1816 } else {
1817 IF_DEBUG(linker, debugBelch("lookupSymbol: value of %s is %p\n", lbl, val));
1818 return val;
1819 }
1820 }
1821
1822 /* -----------------------------------------------------------------------------
1823 * Debugging aid: look in GHCi's object symbol tables for symbols
1824 * within DELTA bytes of the specified address, and show their names.
1825 */
1826 #ifdef DEBUG
1827 void ghci_enquire ( char* addr );
1828
1829 void ghci_enquire ( char* addr )
1830 {
1831 int i;
1832 char* sym;
1833 char* a;
1834 const int DELTA = 64;
1835 ObjectCode* oc;
1836
1837 initLinker();
1838
1839 for (oc = objects; oc; oc = oc->next) {
1840 for (i = 0; i < oc->n_symbols; i++) {
1841 sym = oc->symbols[i];
1842 if (sym == NULL) continue;
1843 a = NULL;
1844 if (a == NULL) {
1845 a = lookupStrHashTable(symhash, sym);
1846 }
1847 if (a == NULL) {
1848 // debugBelch("ghci_enquire: can't find %s\n", sym);
1849 }
1850 else if (addr-DELTA <= a && a <= addr+DELTA) {
1851 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1852 }
1853 }
1854 }
1855 }
1856 #endif
1857
1858 #ifdef USE_MMAP
1859 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1860
1861 static void *
1862 mmapForLinker (size_t bytes, nat flags, int fd)
1863 {
1864 void *map_addr = NULL;
1865 void *result;
1866 int pagesize, size;
1867 static nat fixed = 0;
1868
1869 IF_DEBUG(linker, debugBelch("mmapForLinker: start\n"));
1870 pagesize = getpagesize();
1871 size = ROUND_UP(bytes, pagesize);
1872
1873 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1874 mmap_again:
1875
1876 if (mmap_32bit_base != 0) {
1877 map_addr = mmap_32bit_base;
1878 }
1879 #endif
1880
1881 IF_DEBUG(linker, debugBelch("mmapForLinker: \tprotection %#0x\n", PROT_EXEC | PROT_READ | PROT_WRITE));
1882 IF_DEBUG(linker, debugBelch("mmapForLinker: \tflags %#0x\n", MAP_PRIVATE | TRY_MAP_32BIT | fixed | flags));
1883 result = mmap(map_addr, size, PROT_EXEC|PROT_READ|PROT_WRITE,
1884 MAP_PRIVATE|TRY_MAP_32BIT|fixed|flags, fd, 0);
1885
1886 if (result == MAP_FAILED) {
1887 sysErrorBelch("mmap %" FMT_SizeT " bytes at %p",(lnat)size,map_addr);
1888 errorBelch("Try specifying an address with +RTS -xm<addr> -RTS");
1889 stg_exit(EXIT_FAILURE);
1890 }
1891
1892 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1893 if (mmap_32bit_base != 0) {
1894 if (result == map_addr) {
1895 mmap_32bit_base = (StgWord8*)map_addr + size;
1896 } else {
1897 if ((W_)result > 0x80000000) {
1898 // oops, we were given memory over 2Gb
1899 #if defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS)
1900 // Some platforms require MAP_FIXED. This is normally
1901 // a bad idea, because MAP_FIXED will overwrite
1902 // existing mappings.
1903 munmap(result,size);
1904 fixed = MAP_FIXED;
1905 goto mmap_again;
1906 #else
1907 barf("loadObj: failed to mmap() memory below 2Gb; asked for %lu bytes at %p. Try specifying an address with +RTS -xm<addr> -RTS", size, map_addr, result);
1908 #endif
1909 } else {
1910 // hmm, we were given memory somewhere else, but it's
1911 // still under 2Gb so we can use it. Next time, ask
1912 // for memory right after the place we just got some
1913 mmap_32bit_base = (StgWord8*)result + size;
1914 }
1915 }
1916 } else {
1917 if ((W_)result > 0x80000000) {
1918 // oops, we were given memory over 2Gb
1919 // ... try allocating memory somewhere else?;
1920 debugTrace(DEBUG_linker,"MAP_32BIT didn't work; gave us %lu bytes at 0x%p", bytes, result);
1921 munmap(result, size);
1922
1923 // Set a base address and try again... (guess: 1Gb)
1924 mmap_32bit_base = (void*)0x40000000;
1925 goto mmap_again;
1926 }
1927 }
1928 #endif
1929
1930 IF_DEBUG(linker, debugBelch("mmapForLinker: mapped %" FMT_SizeT " bytes starting at %p\n", (lnat)size, result));
1931 IF_DEBUG(linker, debugBelch("mmapForLinker: done\n"));
1932 return result;
1933 }
1934 #endif // USE_MMAP
1935
1936 static ObjectCode*
1937 mkOc( pathchar *path, char *image, int imageSize,
1938 char *archiveMemberName
1939 #ifndef USE_MMAP
1940 #ifdef darwin_HOST_OS
1941 , int misalignment
1942 #endif
1943 #endif
1944 ) {
1945 ObjectCode* oc;
1946
1947 IF_DEBUG(linker, debugBelch("mkOc: start\n"));
1948 oc = stgMallocBytes(sizeof(ObjectCode), "loadArchive(oc)");
1949
1950 # if defined(OBJFORMAT_ELF)
1951 oc->formatName = "ELF";
1952 # elif defined(OBJFORMAT_PEi386)
1953 oc->formatName = "PEi386";
1954 # elif defined(OBJFORMAT_MACHO)
1955 oc->formatName = "Mach-O";
1956 # else
1957 stgFree(oc);
1958 barf("loadObj: not implemented on this platform");
1959 # endif
1960
1961 oc->image = image;
1962 oc->fileName = pathdup(path);
1963
1964 if (archiveMemberName) {
1965 oc->archiveMemberName = stgMallocBytes( strlen(archiveMemberName)+1, "loadObj" );
1966 strcpy(oc->archiveMemberName, archiveMemberName);
1967 }
1968 else {
1969 oc->archiveMemberName = NULL;
1970 }
1971
1972 oc->fileSize = imageSize;
1973 oc->symbols = NULL;
1974 oc->sections = NULL;
1975 oc->proddables = NULL;
1976
1977 #ifndef USE_MMAP
1978 #ifdef darwin_HOST_OS
1979 oc->misalignment = misalignment;
1980 #endif
1981 #endif
1982
1983 /* chain it onto the list of objects */
1984 oc->next = objects;
1985 objects = oc;
1986
1987 IF_DEBUG(linker, debugBelch("mkOc: done\n"));
1988 return oc;
1989 }
1990
1991 HsInt
1992 loadArchive( pathchar *path )
1993 {
1994 ObjectCode* oc;
1995 char *image;
1996 int memberSize;
1997 FILE *f;
1998 int n;
1999 size_t thisFileNameSize;
2000 char *fileName;
2001 size_t fileNameSize;
2002 int isObject, isGnuIndex;
2003 char tmp[20];
2004 char *gnuFileIndex;
2005 int gnuFileIndexSize;
2006 #if defined(darwin_HOST_OS)
2007 int i;
2008 uint32_t nfat_arch, nfat_offset, cputype, cpusubtype;
2009 #if defined(i386_HOST_ARCH)
2010 const uint32_t mycputype = CPU_TYPE_X86;
2011 const uint32_t mycpusubtype = CPU_SUBTYPE_X86_ALL;
2012 #elif defined(x86_64_HOST_ARCH)
2013 const uint32_t mycputype = CPU_TYPE_X86_64;
2014 const uint32_t mycpusubtype = CPU_SUBTYPE_X86_64_ALL;
2015 #elif defined(powerpc_HOST_ARCH)
2016 const uint32_t mycputype = CPU_TYPE_POWERPC;
2017 const uint32_t mycpusubtype = CPU_SUBTYPE_POWERPC_ALL;
2018 #elif defined(powerpc64_HOST_ARCH)
2019 const uint32_t mycputype = CPU_TYPE_POWERPC64;
2020 const uint32_t mycpusubtype = CPU_SUBTYPE_POWERPC_ALL;
2021 #else
2022 #error Unknown Darwin architecture
2023 #endif
2024 #if !defined(USE_MMAP)
2025 int misalignment;
2026 #endif
2027 #endif
2028
2029 IF_DEBUG(linker, debugBelch("loadArchive: start\n"));
2030 IF_DEBUG(linker, debugBelch("loadArchive: Loading archive `%" PATH_FMT" '\n", path));
2031
2032 gnuFileIndex = NULL;
2033 gnuFileIndexSize = 0;
2034
2035 fileNameSize = 32;
2036 fileName = stgMallocBytes(fileNameSize, "loadArchive(fileName)");
2037
2038 f = pathopen(path, WSTR("rb"));
2039 if (!f)
2040 barf("loadObj: can't read `%s'", path);
2041
2042 /* Check if this is an archive by looking for the magic "!<arch>\n"
2043 * string. Usually, if this fails, we barf and quit. On Darwin however,
2044 * we may have a fat archive, which contains archives for more than
2045 * one architecture. Fat archives start with the magic number 0xcafebabe,
2046 * always stored big endian. If we find a fat_header, we scan through
2047 * the fat_arch structs, searching through for one for our host
2048 * architecture. If a matching struct is found, we read the offset
2049 * of our archive data (nfat_offset) and seek forward nfat_offset bytes
2050 * from the start of the file.
2051 *
2052 * A subtlety is that all of the members of the fat_header and fat_arch
2053 * structs are stored big endian, so we need to call byte order
2054 * conversion functions.
2055 *
2056 * If we find the appropriate architecture in a fat archive, we gobble
2057 * its magic "!<arch>\n" string and continue processing just as if
2058 * we had a single architecture archive.
2059 */
2060
2061 n = fread ( tmp, 1, 8, f );
2062 if (n != 8)
2063 barf("loadArchive: Failed reading header from `%s'", path);
2064 if (strncmp(tmp, "!<arch>\n", 8) != 0) {
2065
2066 #if defined(darwin_HOST_OS)
2067 /* Not a standard archive, look for a fat archive magic number: */
2068 if (ntohl(*(uint32_t *)tmp) == FAT_MAGIC) {
2069 nfat_arch = ntohl(*(uint32_t *)(tmp + 4));
2070 IF_DEBUG(linker, debugBelch("loadArchive: found a fat archive containing %d architectures\n", nfat_arch));
2071 nfat_offset = 0;
2072
2073 for (i = 0; i < (int)nfat_arch; i++) {
2074 /* search for the right arch */
2075 n = fread( tmp, 1, 20, f );
2076 if (n != 8)
2077 barf("loadArchive: Failed reading arch from `%s'", path);
2078 cputype = ntohl(*(uint32_t *)tmp);
2079 cpusubtype = ntohl(*(uint32_t *)(tmp + 4));
2080
2081 if (cputype == mycputype && cpusubtype == mycpusubtype) {
2082 IF_DEBUG(linker, debugBelch("loadArchive: found my archive in a fat archive\n"));
2083 nfat_offset = ntohl(*(uint32_t *)(tmp + 8));
2084 break;
2085 }
2086 }
2087
2088 if (nfat_offset == 0) {
2089 barf ("loadArchive: searched %d architectures, but no host arch found", (int)nfat_arch);
2090 }
2091 else {
2092 n = fseek( f, nfat_offset, SEEK_SET );
2093 if (n != 0)
2094 barf("loadArchive: Failed to seek to arch in `%s'", path);
2095 n = fread ( tmp, 1, 8, f );
2096 if (n != 8)
2097 barf("loadArchive: Failed reading header from `%s'", path);
2098 if (strncmp(tmp, "!<arch>\n", 8) != 0) {
2099 barf("loadArchive: couldn't find archive in `%s' at offset %d", path, nfat_offset);
2100 }
2101 }
2102 }
2103 else {
2104 barf("loadArchive: Neither an archive, nor a fat archive: `%s'", path);
2105 }
2106
2107 #else
2108 barf("loadArchive: Not an archive: `%s'", path);
2109 #endif
2110 }
2111
2112 IF_DEBUG(linker, debugBelch("loadArchive: loading archive contents\n"));
2113
2114 while(1) {
2115 n = fread ( fileName, 1, 16, f );
2116 if (n != 16) {
2117 if (feof(f)) {
2118 IF_DEBUG(linker, debugBelch("loadArchive: EOF while reading from '%" PATH_FMT "'\n", path));
2119 break;
2120 }
2121 else {
2122 barf("loadArchive: Failed reading file name from `%s'", path);
2123 }
2124 }
2125
2126 #if defined(darwin_HOST_OS)
2127 if (strncmp(fileName, "!<arch>\n", 8) == 0) {
2128 IF_DEBUG(linker, debugBelch("loadArchive: found the start of another archive, breaking\n"));
2129 break;
2130 }
2131 #endif
2132
2133 n = fread ( tmp, 1, 12, f );
2134 if (n != 12)
2135 barf("loadArchive: Failed reading mod time from `%s'", path);
2136 n = fread ( tmp, 1, 6, f );
2137 if (n != 6)
2138 barf("loadArchive: Failed reading owner from `%s'", path);
2139 n = fread ( tmp, 1, 6, f );
2140 if (n != 6)
2141 barf("loadArchive: Failed reading group from `%s'", path);
2142 n = fread ( tmp, 1, 8, f );
2143 if (n != 8)
2144 barf("loadArchive: Failed reading mode from `%s'", path);
2145 n = fread ( tmp, 1, 10, f );
2146 if (n != 10)
2147 barf("loadArchive: Failed reading size from `%s'", path);
2148 tmp[10] = '\0';
2149 for (n = 0; isdigit(tmp[n]); n++);
2150 tmp[n] = '\0';
2151 memberSize = atoi(tmp);
2152
2153 IF_DEBUG(linker, debugBelch("loadArchive: size of this archive member is %d\n", memberSize));
2154 n = fread ( tmp, 1, 2, f );
2155 if (n != 2)
2156 barf("loadArchive: Failed reading magic from `%s'", path);
2157 if (strncmp(tmp, "\x60\x0A", 2) != 0)
2158 barf("loadArchive: Failed reading magic from `%s' at %ld. Got %c%c",
2159 path, ftell(f), tmp[0], tmp[1]);
2160
2161 isGnuIndex = 0;
2162 /* Check for BSD-variant large filenames */
2163 if (0 == strncmp(fileName, "#1/", 3)) {
2164 fileName[16] = '\0';
2165 if (isdigit(fileName[3])) {
2166 for (n = 4; isdigit(fileName[n]); n++);
2167 fileName[n] = '\0';
2168 thisFileNameSize = atoi(fileName + 3);
2169 memberSize -= thisFileNameSize;
2170 if (thisFileNameSize >= fileNameSize) {
2171 /* Double it to avoid potentially continually
2172 increasing it by 1 */
2173 fileNameSize = thisFileNameSize * 2;
2174 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
2175 }
2176 n = fread ( fileName, 1, thisFileNameSize, f );
2177 if (n != (int)thisFileNameSize) {
2178 barf("loadArchive: Failed reading filename from `%s'",
2179 path);
2180 }
2181 fileName[thisFileNameSize] = 0;
2182
2183 /* On OS X at least, thisFileNameSize is the size of the
2184 fileName field, not the length of the fileName
2185 itself. */
2186 thisFileNameSize = strlen(fileName);
2187 }
2188 else {
2189 barf("loadArchive: BSD-variant filename size not found while reading filename from `%s'", path);
2190 }
2191 }
2192 /* Check for GNU file index file */
2193 else if (0 == strncmp(fileName, "//", 2)) {
2194 fileName[0] = '\0';
2195 thisFileNameSize = 0;
2196 isGnuIndex = 1;
2197 }
2198 /* Check for a file in the GNU file index */
2199 else if (fileName[0] == '/') {
2200 if (isdigit(fileName[1])) {
2201 int i;
2202
2203 for (n = 2; isdigit(fileName[n]); n++);
2204 fileName[n] = '\0';
2205 n = atoi(fileName + 1);
2206
2207 if (gnuFileIndex == NULL) {
2208 barf("loadArchive: GNU-variant filename without an index while reading from `%s'", path);
2209 }
2210 if (n < 0 || n > gnuFileIndexSize) {
2211 barf("loadArchive: GNU-variant filename offset %d out of range [0..%d] while reading filename from `%s'", n, gnuFileIndexSize, path);
2212 }
2213 if (n != 0 && gnuFileIndex[n - 1] != '\n') {
2214 barf("loadArchive: GNU-variant filename offset %d invalid (range [0..%d]) while reading filename from `%s'", n, gnuFileIndexSize, path);
2215 }
2216 for (i = n; gnuFileIndex[i] != '/'; i++);
2217 thisFileNameSize = i - n;
2218 if (thisFileNameSize >= fileNameSize) {
2219 /* Double it to avoid potentially continually
2220 increasing it by 1 */
2221 fileNameSize = thisFileNameSize * 2;
2222 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
2223 }
2224 memcpy(fileName, gnuFileIndex + n, thisFileNameSize);
2225 fileName[thisFileNameSize] = '\0';
2226 }
2227 else if (fileName[1] == ' ') {
2228 fileName[0] = '\0';
2229 thisFileNameSize = 0;
2230 }
2231 else {
2232 barf("loadArchive: GNU-variant filename offset not found while reading filename from `%s'", path);
2233 }
2234 }
2235 /* Finally, the case where the filename field actually contains
2236 the filename */
2237 else {
2238 /* GNU ar terminates filenames with a '/', this allowing
2239 spaces in filenames. So first look to see if there is a
2240 terminating '/'. */
2241 for (thisFileNameSize = 0;
2242 thisFileNameSize < 16;
2243 thisFileNameSize++) {
2244 if (fileName[thisFileNameSize] == '/') {
2245 fileName[thisFileNameSize] = '\0';
2246 break;
2247 }
2248 }
2249 /* If we didn't find a '/', then a space teminates the
2250 filename. Note that if we don't find one, then
2251 thisFileNameSize ends up as 16, and we already have the
2252 '\0' at the end. */
2253 if (thisFileNameSize == 16) {
2254 for (thisFileNameSize = 0;
2255 thisFileNameSize < 16;
2256 thisFileNameSize++) {
2257 if (fileName[thisFileNameSize] == ' ') {
2258 fileName[thisFileNameSize] = '\0';
2259 break;
2260 }
2261 }
2262 }
2263 }
2264
2265 IF_DEBUG(linker,
2266 debugBelch("loadArchive: Found member file `%s'\n", fileName));
2267
2268 isObject = thisFileNameSize >= 2
2269 && fileName[thisFileNameSize - 2] == '.'
2270 && fileName[thisFileNameSize - 1] == 'o';
2271
2272 IF_DEBUG(linker, debugBelch("loadArchive: \tthisFileNameSize = %d\n", (int)thisFileNameSize));
2273 IF_DEBUG(linker, debugBelch("loadArchive: \tisObject = %d\n", isObject));
2274
2275 if (isObject) {
2276 char *archiveMemberName;
2277
2278 IF_DEBUG(linker, debugBelch("loadArchive: Member is an object file...loading...\n"));
2279
2280 /* We can't mmap from the archive directly, as object
2281 files need to be 8-byte aligned but files in .ar
2282 archives are 2-byte aligned. When possible we use mmap
2283 to get some anonymous memory, as on 64-bit platforms if
2284 we use malloc then we can be given memory above 2^32.
2285 In the mmap case we're probably wasting lots of space;
2286 we could do better. */
2287 #if defined(USE_MMAP)
2288 image = mmapForLinker(memberSize, MAP_ANONYMOUS, -1);
2289 #elif defined(mingw32_HOST_OS)
2290 // TODO: We would like to use allocateExec here, but allocateExec
2291 // cannot currently allocate blocks large enough.
2292 {
2293 int offset;
2294 #if defined(x86_64_HOST_ARCH)
2295 /* We get back 8-byte aligned memory (is that guaranteed?), but
2296 the offsets to the sections within the file are all 4 mod 8
2297 (is that guaranteed?). We therefore need to offset the image
2298 by 4, so that all the pointers are 8-byte aligned, so that
2299 pointer tagging works. */
2300 offset = 4;
2301 #else
2302 offset = 0;
2303 #endif
2304 image = VirtualAlloc(NULL, memberSize + offset,
2305 MEM_RESERVE | MEM_COMMIT,
2306 PAGE_EXECUTE_READWRITE);
2307 image += offset;
2308 }
2309 #elif defined(darwin_HOST_OS)
2310 /* See loadObj() */
2311 misalignment = machoGetMisalignment(f);
2312 image = stgMallocBytes(memberSize + misalignment, "loadArchive(image)");
2313 image += misalignment;
2314 #else
2315 image = stgMallocBytes(memberSize, "loadArchive(image)");
2316 #endif
2317 n = fread ( image, 1, memberSize, f );
2318 if (n != memberSize) {
2319 barf("loadArchive: error whilst reading `%s'", path);
2320 }
2321
2322 archiveMemberName = stgMallocBytes(pathlen(path) + thisFileNameSize + 3,
2323 "loadArchive(file)");
2324 sprintf(archiveMemberName, "%" PATH_FMT "(%.*s)",
2325 path, (int)thisFileNameSize, fileName);
2326
2327 oc = mkOc(path, image, memberSize, archiveMemberName
2328 #ifndef USE_MMAP
2329 #ifdef darwin_HOST_OS
2330 , misalignment
2331 #endif
2332 #endif
2333 );
2334
2335 stgFree(archiveMemberName);
2336
2337 if (0 == loadOc(oc)) {
2338 stgFree(fileName);
2339 return 0;
2340 }
2341 }
2342 else if (isGnuIndex) {
2343 if (gnuFileIndex != NULL) {
2344 barf("loadArchive: GNU-variant index found, but already have an index, while reading filename from `%s'", path);
2345 }
2346 IF_DEBUG(linker, debugBelch("loadArchive: Found GNU-variant file index\n"));
2347 #ifdef USE_MMAP
2348 gnuFileIndex = mmapForLinker(memberSize + 1, MAP_ANONYMOUS, -1);
2349 #else
2350 gnuFileIndex = stgMallocBytes(memberSize + 1, "loadArchive(image)");
2351 #endif
2352 n = fread ( gnuFileIndex, 1, memberSize, f );
2353 if (n != memberSize) {
2354 barf("loadArchive: error whilst reading `%s'", path);
2355 }
2356 gnuFileIndex[memberSize] = '/';
2357 gnuFileIndexSize = memberSize;
2358 }
2359 else {
2360 IF_DEBUG(linker, debugBelch("loadArchive: '%s' does not appear to be an object file\n", fileName));
2361 n = fseek(f, memberSize, SEEK_CUR);
2362 if (n != 0)
2363 barf("loadArchive: error whilst seeking by %d in `%s'",
2364 memberSize, path);
2365 }
2366
2367 /* .ar files are 2-byte aligned */
2368 if (memberSize % 2) {
2369 IF_DEBUG(linker, debugBelch("loadArchive: trying to read one pad byte\n"));
2370 n = fread ( tmp, 1, 1, f );
2371 if (n != 1) {
2372 if (feof(f)) {
2373 IF_DEBUG(linker, debugBelch("loadArchive: found EOF while reading one pad byte\n"));
2374 break;
2375 }
2376 else {
2377 barf("loadArchive: Failed reading padding from `%s'", path);
2378 }
2379 }
2380 IF_DEBUG(linker, debugBelch("loadArchive: successfully read one pad byte\n"));
2381 }
2382 IF_DEBUG(linker, debugBelch("loadArchive: reached end of archive loading while loop\n"));
2383 }
2384
2385 fclose(f);
2386
2387 stgFree(fileName);
2388 if (gnuFileIndex != NULL) {
2389 #ifdef USE_MMAP
2390 munmap(gnuFileIndex, gnuFileIndexSize + 1);
2391 #else
2392 stgFree(gnuFileIndex);
2393 #endif
2394 }
2395
2396 IF_DEBUG(linker, debugBelch("loadArchive: done\n"));
2397 return 1;
2398 }
2399
2400 /* -----------------------------------------------------------------------------
2401 * Load an obj (populate the global symbol table, but don't resolve yet)
2402 *
2403 * Returns: 1 if ok, 0 on error.
2404 */
2405 HsInt
2406 loadObj( pathchar *path )
2407 {
2408 ObjectCode* oc;
2409 char *image;
2410 int fileSize;
2411 struct_stat st;
2412 int r;
2413 #ifdef USE_MMAP
2414 int fd;
2415 #else
2416 FILE *f;
2417 # if defined(darwin_HOST_OS)
2418 int misalignment;
2419 # endif
2420 #endif
2421 IF_DEBUG(linker, debugBelch("loadObj %" PATH_FMT "\n", path));
2422
2423 initLinker();
2424
2425 /* debugBelch("loadObj %s\n", path ); */
2426
2427 /* Check that we haven't already loaded this object.
2428 Ignore requests to load multiple times */
2429 {
2430 ObjectCode *o;
2431 int is_dup = 0;
2432 for (o = objects; o; o = o->next) {
2433 if (0 == pathcmp(o->fileName, path)) {
2434 is_dup = 1;
2435 break; /* don't need to search further */
2436 }
2437 }
2438 if (is_dup) {
2439 IF_DEBUG(linker, debugBelch(
2440 "GHCi runtime linker: warning: looks like you're trying to load the\n"
2441 "same object file twice:\n"
2442 " %" PATH_FMT "\n"
2443 "GHCi will ignore this, but be warned.\n"
2444 , path));
2445 return 1; /* success */
2446 }
2447 }
2448
2449 r = pathstat(path, &st);
2450 if (r == -1) {
2451 IF_DEBUG(linker, debugBelch("File doesn't exist\n"));
2452 return 0;
2453 }
2454
2455 fileSize = st.st_size;
2456
2457 #ifdef USE_MMAP
2458 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
2459
2460 #if defined(openbsd_HOST_OS)
2461 fd = open(path, O_RDONLY, S_IRUSR);
2462 #else
2463 fd = open(path, O_RDONLY);
2464 #endif
2465 if (fd == -1)
2466 barf("loadObj: can't open `%s'", path);
2467
2468 image = mmapForLinker(fileSize, 0, fd);
2469
2470 close(fd);
2471
2472 #else /* !USE_MMAP */
2473 /* load the image into memory */
2474 f = pathopen(path, WSTR("rb"));
2475 if (!f)
2476 barf("loadObj: can't read `%" PATH_FMT "'", path);
2477
2478 # if defined(mingw32_HOST_OS)
2479 // TODO: We would like to use allocateExec here, but allocateExec
2480 // cannot currently allocate blocks large enough.
2481 {
2482 int offset;
2483 #if defined(x86_64_HOST_ARCH)
2484 /* We get back 8-byte aligned memory (is that guaranteed?), but
2485 the offsets to the sections within the file are all 4 mod 8
2486 (is that guaranteed?). We therefore need to offset the image
2487 by 4, so that all the pointers are 8-byte aligned, so that
2488 pointer tagging works. */
2489 offset = 4;
2490 #else
2491 offset = 0;
2492 #endif
2493 image = VirtualAlloc(NULL, fileSize + offset, MEM_RESERVE | MEM_COMMIT,
2494 PAGE_EXECUTE_READWRITE);
2495 image += offset;
2496 }
2497 # elif defined(darwin_HOST_OS)
2498 // In a Mach-O .o file, all sections can and will be misaligned
2499 // if the total size of the headers is not a multiple of the
2500 // desired alignment. This is fine for .o files that only serve
2501 // as input for the static linker, but it's not fine for us,
2502 // as SSE (used by gcc for floating point) and Altivec require
2503 // 16-byte alignment.
2504 // We calculate the correct alignment from the header before
2505 // reading the file, and then we misalign image on purpose so
2506 // that the actual sections end up aligned again.
2507 misalignment = machoGetMisalignment(f);
2508 image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
2509 image += misalignment;
2510 # else
2511 image = stgMallocBytes(fileSize, "loadObj(image)");
2512 # endif
2513
2514 {
2515 int n;
2516 n = fread ( image, 1, fileSize, f );
2517 if (n != fileSize)
2518 barf("loadObj: error whilst reading `%s'", path);
2519 }
2520 fclose(f);
2521 #endif /* USE_MMAP */
2522
2523 oc = mkOc(path, image, fileSize, NULL
2524 #ifndef USE_MMAP
2525 #ifdef darwin_HOST_OS
2526 , misalignment
2527 #endif
2528 #endif
2529 );
2530
2531 return loadOc(oc);
2532 }
2533
2534 static HsInt
2535 loadOc( ObjectCode* oc ) {
2536 int r;
2537
2538 IF_DEBUG(linker, debugBelch("loadOc: start\n"));
2539
2540 # if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2541 r = ocAllocateSymbolExtras_MachO ( oc );
2542 if (!r) {
2543 IF_DEBUG(linker, debugBelch("loadOc: ocAllocateSymbolExtras_MachO failed\n"));
2544 return r;
2545 }
2546 # elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) || defined(arm_HOST_ARCH))
2547 r = ocAllocateSymbolExtras_ELF ( oc );
2548 if (!r) {
2549 IF_DEBUG(linker, debugBelch("loadOc: ocAllocateSymbolExtras_ELF failed\n"));
2550 return r;
2551 }
2552 #endif
2553
2554 /* verify the in-memory image */
2555 # if defined(OBJFORMAT_ELF)
2556 r = ocVerifyImage_ELF ( oc );
2557 # elif defined(OBJFORMAT_PEi386)
2558 r = ocVerifyImage_PEi386 ( oc );
2559 # elif defined(OBJFORMAT_MACHO)
2560 r = ocVerifyImage_MachO ( oc );
2561 # else
2562 barf("loadObj: no verify method");
2563 # endif
2564 if (!r) {
2565 IF_DEBUG(linker, debugBelch("loadOc: ocVerifyImage_* failed\n"));
2566 return r;
2567 }
2568
2569 /* build the symbol list for this image */
2570 # if defined(OBJFORMAT_ELF)
2571 r = ocGetNames_ELF ( oc );
2572 # elif defined(OBJFORMAT_PEi386)
2573 r = ocGetNames_PEi386 ( oc );
2574 # elif defined(OBJFORMAT_MACHO)
2575 r = ocGetNames_MachO ( oc );
2576 # else
2577 barf("loadObj: no getNames method");
2578 # endif
2579 if (!r) {
2580 IF_DEBUG(linker, debugBelch("loadOc: ocGetNames_* failed\n"));
2581 return r;
2582 }
2583
2584 /* loaded, but not resolved yet */
2585 oc->status = OBJECT_LOADED;
2586 IF_DEBUG(linker, debugBelch("loadOc: done.\n"));
2587
2588 return 1;
2589 }
2590
2591 /* -----------------------------------------------------------------------------
2592 * resolve all the currently unlinked objects in memory
2593 *
2594 * Returns: 1 if ok, 0 on error.
2595 */
2596 HsInt
2597 resolveObjs( void )
2598 {
2599 ObjectCode *oc;
2600 int r;
2601
2602 IF_DEBUG(linker, debugBelch("resolveObjs: start\n"));
2603 initLinker();
2604
2605 for (oc = objects; oc; oc = oc->next) {
2606 if (oc->status != OBJECT_RESOLVED) {
2607 # if defined(OBJFORMAT_ELF)
2608 r = ocResolve_ELF ( oc );
2609 # elif defined(OBJFORMAT_PEi386)
2610 r = ocResolve_PEi386 ( oc );
2611 # elif defined(OBJFORMAT_MACHO)
2612 r = ocResolve_MachO ( oc );
2613 # else
2614 barf("resolveObjs: not implemented on this platform");
2615 # endif
2616 if (!r) { return r; }
2617 oc->status = OBJECT_RESOLVED;
2618 }
2619 }
2620 IF_DEBUG(linker, debugBelch("resolveObjs: done\n"));
2621 return 1;
2622 }
2623
2624 /* -----------------------------------------------------------------------------
2625 * delete an object from the pool
2626 */
2627 HsInt
2628 unloadObj( pathchar *path )
2629 {
2630 ObjectCode *oc, *prev;
2631 HsBool unloadedAnyObj = HS_BOOL_FALSE;
2632
2633 ASSERT(symhash != NULL);
2634 ASSERT(objects != NULL);
2635
2636 initLinker();
2637
2638 prev = NULL;
2639 for (oc = objects; oc; prev = oc, oc = oc->next) {
2640 if (!pathcmp(oc->fileName,path)) {
2641
2642 /* Remove all the mappings for the symbols within this
2643 * object..
2644 */
2645 {
2646 int i;
2647 for (i = 0; i < oc->n_symbols; i++) {
2648 if (oc->symbols[i] != NULL) {
2649 removeStrHashTable(symhash, oc->symbols[i], NULL);
2650 }
2651 }
2652 }
2653
2654 if (prev == NULL) {
2655 objects = oc->next;
2656 } else {
2657 prev->next = oc->next;
2658 }
2659
2660 // We're going to leave this in place, in case there are
2661 // any pointers from the heap into it:
2662 // #ifdef mingw32_HOST_OS
2663 // If uncommenting, note that currently oc->image is
2664 // not the right address to free on Win64, as we added
2665 // 4 bytes of padding at the start
2666 // VirtualFree(oc->image);
2667 // #else
2668 // stgFree(oc->image);
2669 // #endif
2670 stgFree(oc->fileName);
2671 stgFree(oc->archiveMemberName);
2672 stgFree(oc->symbols);
2673 stgFree(oc->sections);
2674 stgFree(oc);
2675
2676 /* This could be a member of an archive so continue
2677 * unloading other members. */
2678 unloadedAnyObj = HS_BOOL_TRUE;
2679 }
2680 }
2681
2682 if (unloadedAnyObj) {
2683 return 1;
2684 }
2685 else {
2686 errorBelch("unloadObj: can't find `%" PATH_FMT "' to unload", path);
2687 return 0;
2688 }
2689 }
2690
2691 /* -----------------------------------------------------------------------------
2692 * Sanity checking. For each ObjectCode, maintain a list of address ranges
2693 * which may be prodded during relocation, and abort if we try and write
2694 * outside any of these.
2695 */
2696 static void
2697 addProddableBlock ( ObjectCode* oc, void* start, int size )
2698 {
2699 ProddableBlock* pb
2700 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
2701
2702 IF_DEBUG(linker, debugBelch("addProddableBlock: %p %p %d\n", oc, start, size));
2703 ASSERT(size > 0);
2704 pb->start = start;
2705 pb->size = size;
2706 pb->next = oc->proddables;
2707 oc->proddables = pb;
2708 }
2709
2710 static void
2711 checkProddableBlock (ObjectCode *oc, void *addr, size_t size )
2712 {
2713 ProddableBlock* pb;
2714
2715 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
2716 char* s = (char*)(pb->start);
2717 char* e = s + pb->size;
2718 char* a = (char*)addr;
2719 if (a >= s && (a+size) <= e) return;
2720 }
2721 barf("checkProddableBlock: invalid fixup in runtime linker: %p", addr);
2722 }
2723
2724 /* -----------------------------------------------------------------------------
2725 * Section management.
2726 */
2727 static void
2728 addSection ( ObjectCode* oc, SectionKind kind,
2729 void* start, void* end )
2730 {
2731 Section* s = stgMallocBytes(sizeof(Section), "addSection");
2732 s->start = start;
2733 s->end = end;
2734 s->kind = kind;
2735 s->next = oc->sections;
2736 oc->sections = s;
2737
2738 IF_DEBUG(linker, debugBelch("addSection: %p-%p (size %lld), kind %d\n",
2739 start, ((char*)end)-1, ((long long)(size_t)end) - ((long long)(size_t)start) + 1, kind ));
2740 }
2741
2742
2743 /* --------------------------------------------------------------------------
2744 * Symbol Extras.
2745 * This is about allocating a small chunk of memory for every symbol in the
2746 * object file. We make sure that the SymboLExtras are always "in range" of
2747 * limited-range PC-relative instructions on various platforms by allocating
2748 * them right next to the object code itself.
2749 */
2750
2751 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) || defined(arm_HOST_ARCH)
2752 #if !defined(x86_64_HOST_ARCH) || !defined(mingw32_HOST_OS)
2753
2754 /*
2755 ocAllocateSymbolExtras
2756
2757 Allocate additional space at the end of the object file image to make room
2758 for jump islands (powerpc, x86_64, arm) and GOT entries (x86_64).
2759
2760 PowerPC relative branch instructions have a 24 bit displacement field.
2761 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
2762 If a particular imported symbol is outside this range, we have to redirect
2763 the jump to a short piece of new code that just loads the 32bit absolute
2764 address and jumps there.
2765 On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
2766 to 32 bits (+-2GB).
2767
2768 This function just allocates space for one SymbolExtra for every
2769 undefined symbol in the object file. The code for the jump islands is
2770 filled in by makeSymbolExtra below.
2771 */
2772
2773 static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
2774 {
2775 #ifdef USE_MMAP
2776 int pagesize, n, m;
2777 #endif
2778 int aligned;
2779 #ifndef USE_MMAP
2780 int misalignment = 0;
2781 #ifdef darwin_HOST_OS
2782 misalignment = oc->misalignment;
2783 #endif
2784 #endif
2785
2786 if( count > 0 )
2787 {
2788 // round up to the nearest 4
2789 aligned = (oc->fileSize + 3) & ~3;
2790
2791 #ifdef USE_MMAP
2792 pagesize = getpagesize();
2793 n = ROUND_UP( oc->fileSize, pagesize );
2794 m = ROUND_UP( aligned + sizeof (SymbolExtra) * count, pagesize );
2795
2796 /* we try to use spare space at the end of the last page of the
2797 * image for the jump islands, but if there isn't enough space
2798 * then we have to map some (anonymously, remembering MAP_32BIT).
2799 */
2800 if( m > n ) // we need to allocate more pages
2801 {
2802 oc->symbol_extras = mmapForLinker(sizeof(SymbolExtra) * count,
2803 MAP_ANONYMOUS, -1);
2804 }
2805 else
2806 {
2807 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2808 }
2809 #else
2810 oc->image -= misalignment;
2811 oc->image = stgReallocBytes( oc->image,
2812 misalignment +
2813 aligned + sizeof (SymbolExtra) * count,
2814 "ocAllocateSymbolExtras" );
2815 oc->image += misalignment;
2816
2817 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2818 #endif /* USE_MMAP */
2819
2820 memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
2821 }
2822 else
2823 oc->symbol_extras = NULL;
2824
2825 oc->first_symbol_extra = first;
2826 oc->n_symbol_extras = count;
2827
2828 return 1;
2829 }
2830
2831 #endif
2832 #endif // defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) || defined(arm_HOST_ARCH)
2833
2834 #if defined(arm_HOST_ARCH)
2835
2836 static void
2837 ocFlushInstructionCache( ObjectCode *oc )
2838 {
2839 // Object code
2840 __clear_cache(oc->image, oc->image + oc->fileSize);
2841 // Jump islands
2842 __clear_cache(oc->symbol_extras, &oc->symbol_extras[oc->n_symbol_extras]);
2843 }
2844
2845 #endif
2846
2847 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
2848 #if !defined(x86_64_HOST_ARCH) || !defined(mingw32_HOST_OS)
2849
2850 static SymbolExtra* makeSymbolExtra( ObjectCode* oc,
2851 unsigned long symbolNumber,
2852 unsigned long target )
2853 {
2854 SymbolExtra *extra;
2855
2856 ASSERT( symbolNumber >= oc->first_symbol_extra
2857 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
2858
2859 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
2860
2861 #ifdef powerpc_HOST_ARCH
2862 // lis r12, hi16(target)
2863 extra->jumpIsland.lis_r12 = 0x3d80;
2864 extra->jumpIsland.hi_addr = target >> 16;
2865
2866 // ori r12, r12, lo16(target)
2867 extra->jumpIsland.ori_r12_r12 = 0x618c;
2868 extra->jumpIsland.lo_addr = target & 0xffff;
2869
2870 // mtctr r12
2871 extra->jumpIsland.mtctr_r12 = 0x7d8903a6;
2872
2873 // bctr
2874 extra->jumpIsland.bctr = 0x4e800420;
2875 #endif
2876 #ifdef x86_64_HOST_ARCH
2877 // jmp *-14(%rip)
2878 static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
2879 extra->addr = target;
2880 memcpy(extra->jumpIsland, jmp, 6);
2881 #endif
2882
2883 return extra;
2884 }
2885
2886 #endif
2887 #endif // defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
2888
2889 #ifdef arm_HOST_ARCH
2890 static SymbolExtra* makeArmSymbolExtra( ObjectCode* oc,
2891 unsigned long symbolNumber,
2892 unsigned long target,
2893 int fromThumb,
2894 int toThumb )
2895 {
2896 SymbolExtra *extra;
2897
2898 ASSERT( symbolNumber >= oc->first_symbol_extra
2899 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
2900
2901 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
2902
2903 // Make sure instruction mode bit is set properly
2904 if (toThumb)
2905 target |= 1;
2906 else
2907 target &= ~1;
2908
2909 if (!fromThumb) {
2910 // In ARM encoding:
2911 // movw r12, #0
2912 // movt r12, #0
2913 // bx r12
2914 uint32_t code[] = { 0xe300c000, 0xe340c000, 0xe12fff1c };
2915
2916 // Patch lower half-word into movw
2917 code[0] |= ((target>>12) & 0xf) << 16;
2918 code[0] |= target & 0xfff;
2919 // Patch upper half-word into movt
2920 target >>= 16;
2921 code[1] |= ((target>>12) & 0xf) << 16;
2922 code[1] |= target & 0xfff;
2923
2924 memcpy(extra->jumpIsland, code, 12);
2925
2926 } else {
2927 // In Thumb encoding:
2928 // movw r12, #0
2929 // movt r12, #0
2930 // bx r12
2931 uint16_t code[] = { 0xf240, 0x0c00,
2932 0xf2c0, 0x0c00,
2933 0x4760 };
2934
2935 // Patch lower half-word into movw
2936 code[0] |= (target>>12) & 0xf;
2937 code[0] |= ((target>>11) & 0x1) << 10;
2938 code[1] |= ((target>>8) & 0x7) << 12;
2939 code[1] |= target & 0xff;
2940 // Patch upper half-word into movt
2941 target >>= 16;
2942 code[2] |= (target>>12) & 0xf;
2943 code[2] |= ((target>>11) & 0x1) << 10;
2944 code[3] |= ((target>>8) & 0x7) << 12;
2945 code[3] |= target & 0xff;
2946
2947 memcpy(extra->jumpIsland, code, 10);
2948 }
2949
2950 return extra;
2951 }
2952 #endif // arm_HOST_ARCH
2953
2954 /* --------------------------------------------------------------------------
2955 * PowerPC specifics (instruction cache flushing)
2956 * ------------------------------------------------------------------------*/
2957
2958 #ifdef powerpc_HOST_ARCH
2959 /*
2960 ocFlushInstructionCache
2961
2962 Flush the data & instruction caches.
2963 Because the PPC has split data/instruction caches, we have to
2964 do that whenever we modify code at runtime.
2965 */
2966
2967 static void
2968 ocFlushInstructionCacheFrom(void* begin, size_t length)
2969 {
2970 size_t n = (length + 3) / 4;
2971 unsigned long* p = begin;
2972
2973 while (n--)
2974 {
2975 __asm__ volatile ( "dcbf 0,%0\n\t"
2976 "sync\n\t"
2977 "icbi 0,%0"
2978 :
2979 : "r" (p)
2980 );
2981 p++;
2982 }
2983 __asm__ volatile ( "sync\n\t"
2984 "isync"
2985 );
2986 }
2987
2988 static void
2989 ocFlushInstructionCache( ObjectCode *oc )
2990 {
2991 /* The main object code */
2992 ocFlushInstructionCacheFrom(oc->image
2993 #ifdef darwin_HOST_OS
2994 + oc->misalignment
2995 #endif
2996 , oc->fileSize);
2997
2998 /* Jump Islands */
2999 ocFlushInstructionCacheFrom(oc->symbol_extras, sizeof(SymbolExtra) * oc->n_symbol_extras);
3000 }
3001 #endif /* powerpc_HOST_ARCH */
3002
3003
3004 /* --------------------------------------------------------------------------
3005 * PEi386 specifics (Win32 targets)
3006 * ------------------------------------------------------------------------*/
3007
3008 /* The information for this linker comes from
3009 Microsoft Portable Executable
3010 and Common Object File Format Specification
3011 revision 5.1 January 1998
3012 which SimonM says comes from the MS Developer Network CDs.
3013
3014 It can be found there (on older CDs), but can also be found
3015 online at:
3016
3017 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
3018
3019 (this is Rev 6.0 from February 1999).
3020
3021 Things move, so if that fails, try searching for it via
3022
3023 http://www.google.com/search?q=PE+COFF+specification
3024
3025 The ultimate reference for the PE format is the Winnt.h
3026 header file that comes with the Platform SDKs; as always,
3027 implementations will drift wrt their documentation.
3028
3029 A good background article on the PE format is Matt Pietrek's
3030 March 1994 article in Microsoft System Journal (MSJ)
3031 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
3032 Win32 Portable Executable File Format." The info in there
3033 has recently been updated in a two part article in
3034 MSDN magazine, issues Feb and March 2002,
3035 "Inside Windows: An In-Depth Look into the Win32 Portable
3036 Executable File Format"
3037
3038 John Levine's book "Linkers and Loaders" contains useful
3039 info on PE too.
3040 */
3041
3042
3043 #if defined(OBJFORMAT_PEi386)
3044
3045
3046
3047 typedef unsigned char UChar;
3048 typedef unsigned short UInt16;
3049 typedef unsigned int UInt32;
3050 typedef int Int32;
3051 typedef unsigned long long int UInt64;
3052
3053
3054 typedef
3055 struct {
3056 UInt16 Machine;
3057 UInt16 NumberOfSections;
3058 UInt32 TimeDateStamp;
3059 UInt32 PointerToSymbolTable;
3060 UInt32 NumberOfSymbols;
3061 UInt16 SizeOfOptionalHeader;
3062 UInt16 Characteristics;
3063 }
3064 COFF_header;
3065
3066 #define sizeof_COFF_header 20
3067
3068
3069 typedef
3070 struct {
3071 UChar Name[8];
3072 UInt32 VirtualSize;
3073 UInt32 VirtualAddress;
3074 UInt32 SizeOfRawData;
3075 UInt32 PointerToRawData;
3076 UInt32 PointerToRelocations;
3077 UInt32 PointerToLinenumbers;
3078 UInt16 NumberOfRelocations;
3079 UInt16 NumberOfLineNumbers;
3080 UInt32 Characteristics;
3081 }
3082 COFF_section;
3083
3084 #define sizeof_COFF_section 40
3085
3086
3087 typedef
3088 struct {
3089 UChar Name[8];
3090 UInt32 Value;
3091 UInt16 SectionNumber;
3092 UInt16 Type;
3093 UChar StorageClass;
3094 UChar NumberOfAuxSymbols;
3095 }
3096 COFF_symbol;
3097
3098 #define sizeof_COFF_symbol 18
3099
3100
3101 typedef
3102 struct {
3103 UInt32 VirtualAddress;
3104 UInt32 SymbolTableIndex;
3105 UInt16 Type;
3106 }
3107 COFF_reloc;
3108
3109 #define sizeof_COFF_reloc 10
3110
3111
3112 /* From PE spec doc, section 3.3.2 */
3113 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
3114 windows.h -- for the same purpose, but I want to know what I'm
3115 getting, here. */
3116 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
3117 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
3118 #define MYIMAGE_FILE_DLL 0x2000
3119 #define MYIMAGE_FILE_SYSTEM 0x1000
3120 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
3121 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
3122 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
3123
3124 /* From PE spec doc, section 5.4.2 and 5.4.4 */
3125 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
3126 #define MYIMAGE_SYM_CLASS_STATIC 3
3127 #define MYIMAGE_SYM_UNDEFINED 0
3128
3129 /* From PE spec doc, section 4.1 */
3130 #define MYIMAGE_SCN_CNT_CODE 0x00000020
3131 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
3132 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
3133
3134 /* From PE spec doc, section 5.2.1 */
3135 #define MYIMAGE_REL_I386_DIR32 0x0006
3136 #define MYIMAGE_REL_I386_REL32 0x0014
3137
3138
3139 /* We use myindex to calculate array addresses, rather than
3140 simply doing the normal subscript thing. That's because
3141 some of the above structs have sizes which are not
3142 a whole number of words. GCC rounds their sizes up to a
3143 whole number of words, which means that the address calcs
3144 arising from using normal C indexing or pointer arithmetic
3145 are just plain wrong. Sigh.
3146 */
3147 static UChar *
3148 myindex ( int scale, void* base, int index )
3149 {
3150 return
3151 ((UChar*)base) + scale * index;
3152 }
3153
3154
3155 static void
3156 printName ( UChar* name, UChar* strtab )
3157 {
3158 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
3159 UInt32 strtab_offset = * (UInt32*)(name+4);
3160 debugBelch("%s", strtab + strtab_offset );
3161 } else {
3162 int i;
3163 for (i = 0; i < 8; i++) {
3164 if (name[i] == 0) break;
3165 debugBelch("%c", name[i] );
3166 }
3167 }
3168 }
3169
3170
3171 static void
3172 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
3173 {
3174 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
3175 UInt32 strtab_offset = * (UInt32*)(name+4);
3176 strncpy ( (char*)dst, (char*)strtab+strtab_offset, dstSize );
3177 dst[dstSize-1] = 0;
3178 } else {
3179 int i = 0;
3180 while (1) {
3181 if (i >= 8) break;
3182 if (name[i] == 0) break;
3183 dst[i] = name[i];
3184 i++;
3185 }
3186 dst[i] = 0;
3187 }
3188 }
3189
3190
3191 static UChar *
3192 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
3193 {
3194 UChar* newstr;
3195 /* If the string is longer than 8 bytes, look in the
3196 string table for it -- this will be correctly zero terminated.
3197 */
3198 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
3199 UInt32 strtab_offset = * (UInt32*)(name+4);
3200 return ((UChar*)strtab) + strtab_offset;
3201 }
3202 /* Otherwise, if shorter than 8 bytes, return the original,
3203 which by defn is correctly terminated.
3204 */
3205 if (name[7]==0) return name;
3206 /* The annoying case: 8 bytes. Copy into a temporary
3207 (XXX which is never freed ...)
3208 */
3209 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
3210 ASSERT(newstr);
3211 strncpy((char*)newstr,(char*)name,8);
3212 newstr[8] = 0;
3213 return newstr;
3214 }
3215
3216 /* Getting the name of a section is mildly tricky, so we make a
3217 function for it. Sadly, in one case we have to copy the string
3218 (when it is exactly 8 bytes long there's no trailing '\0'), so for
3219 consistency we *always* copy the string; the caller must free it
3220 */
3221 static char *
3222 cstring_from_section_name (UChar* name, UChar* strtab)
3223 {
3224 char *newstr;
3225
3226 if (name[0]=='/') {
3227 int strtab_offset = strtol((char*)name+1,NULL,10);
3228 int len = strlen(((char*)strtab) + strtab_offset);
3229
3230 newstr = stgMallocBytes(len+1, "cstring_from_section_symbol_name");
3231 strcpy((char*)newstr, (char*)((UChar*)strtab) + strtab_offset);
3232 return newstr;
3233 }
3234 else
3235 {
3236 newstr = stgMallocBytes(9, "cstring_from_section_symbol_name");
3237 ASSERT(newstr);
3238 strncpy((char*)newstr,(char*)name,8);
3239 newstr[8] = 0;
3240 return newstr;
3241 }
3242 }
3243
3244 /* Just compares the short names (first 8 chars) */
3245 static COFF_section *
3246 findPEi386SectionCalled ( ObjectCode* oc, UChar* name )
3247 {
3248 int i;
3249 COFF_header* hdr
3250 = (COFF_header*)(oc->image);
3251 COFF_section* sectab
3252 = (COFF_section*) (
3253 ((UChar*)(oc->image))
3254 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3255 );
3256 for (i = 0; i < hdr->NumberOfSections; i++) {
3257 UChar* n1;
3258 UChar* n2;
3259 COFF_section* section_i
3260 = (COFF_section*)
3261 myindex ( sizeof_COFF_section, sectab, i );
3262 n1 = (UChar*) &(section_i->Name);
3263 n2 = name;
3264 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
3265 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
3266 n1[6]==n2[6] && n1[7]==n2[7])
3267 return section_i;
3268 }
3269
3270 return NULL;
3271 }
3272
3273 static void
3274 zapTrailingAtSign ( UChar* sym )
3275 {
3276 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
3277 int i, j;
3278 if (sym[0] == 0) return;
3279 i = 0;
3280 while (sym[i] != 0) i++;
3281 i--;
3282 j = i;
3283 while (j > 0 && my_isdigit(sym[j])) j--;
3284 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
3285 # undef my_isdigit
3286 }
3287
3288 static void *
3289 lookupSymbolInDLLs ( UChar *lbl )
3290 {
3291 OpenedDLL* o_dll;
3292 void *sym;
3293
3294 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
3295 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
3296
3297 if (lbl[0] == '_') {
3298 /* HACK: if the name has an initial underscore, try stripping
3299 it off & look that up first. I've yet to verify whether there's
3300 a Rule that governs whether an initial '_' *should always* be
3301 stripped off when mapping from import lib name to the DLL name.
3302 */
3303 sym = GetProcAddress(o_dll->instance, (char*)(lbl+1));
3304 if (sym != NULL) {
3305 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
3306 return sym;
3307 }
3308 }
3309 sym = GetProcAddress(o_dll->instance, (char*)lbl);
3310 if (sym != NULL) {
3311 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
3312 return sym;
3313 }
3314 }
3315 return NULL;
3316 }
3317
3318
3319 static int
3320 ocVerifyImage_PEi386 ( ObjectCode* oc )
3321 {
3322 int i;
3323 UInt32 j, noRelocs;
3324 COFF_header* hdr;
3325 COFF_section* sectab;
3326 COFF_symbol* symtab;
3327 UChar* strtab;
3328 /* debugBelch("\nLOADING %s\n", oc->fileName); */
3329 hdr = (COFF_header*)(oc->image);
3330 sectab = (COFF_section*) (
3331 ((UChar*)(oc->image))
3332 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3333 );
3334 symtab = (COFF_symbol*) (
3335 ((UChar*)(oc->image))
3336 + hdr->PointerToSymbolTable
3337 );
3338 strtab = ((UChar*)symtab)
3339 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3340
3341 #if defined(i386_HOST_ARCH)
3342 if (hdr->Machine != 0x14c) {
3343 errorBelch("%" PATH_FMT ": Not x86 PEi386", oc->fileName);
3344 return 0;
3345 }
3346 #elif defined(x86_64_HOST_ARCH)
3347 if (hdr->Machine != 0x8664) {
3348 errorBelch("%" PATH_FMT ": Not x86_64 PEi386", oc->fileName);
3349 return 0;
3350 }
3351 #else
3352 errorBelch("PEi386 not supported on this arch");
3353 #endif
3354
3355 if (hdr->SizeOfOptionalHeader != 0) {
3356 errorBelch("%" PATH_FMT ": PEi386 with nonempty optional header", oc->fileName);
3357 return 0;
3358 }
3359 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
3360 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
3361 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
3362 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
3363 errorBelch("%" PATH_FMT ": Not a PEi386 object file", oc->fileName);
3364 return 0;
3365 }
3366 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
3367 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
3368 errorBelch("%" PATH_FMT ": Invalid PEi386 word size or endiannness: %d",
3369 oc->fileName,
3370 (int)(hdr->Characteristics));
3371 return 0;
3372 }
3373 /* If the string table size is way crazy, this might indicate that
3374 there are more than 64k relocations, despite claims to the
3375 contrary. Hence this test. */
3376 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
3377 #if 0
3378 if ( (*(UInt32*)strtab) > 600000 ) {
3379 /* Note that 600k has no special significance other than being
3380 big enough to handle the almost-2MB-sized lumps that
3381 constitute HSwin32*.o. */
3382 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
3383 return 0;
3384 }
3385 #endif
3386
3387 /* No further verification after this point; only debug printing. */
3388 i = 0;
3389 IF_DEBUG(linker, i=1);
3390 if (i == 0) return 1;
3391
3392 debugBelch( "sectab offset = %" FMT_Int "\n", ((UChar*)sectab) - ((UChar*)hdr) );
3393 debugBelch( "symtab offset = %" FMT_Int "\n", ((UChar*)symtab) - ((UChar*)hdr) );
3394 debugBelch( "strtab offset = %" FMT_Int "\n", ((UChar*)strtab) - ((UChar*)hdr) );
3395
3396 debugBelch("\n" );
3397 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
3398 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
3399 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
3400 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
3401 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
3402 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
3403 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
3404
3405 /* Print the section table. */
3406 debugBelch("\n" );
3407 for (i = 0; i < hdr->NumberOfSections; i++) {
3408 COFF_reloc* reltab;
3409 COFF_section* sectab_i
3410 = (COFF_section*)
3411 myindex ( sizeof_COFF_section, sectab, i );
3412 debugBelch(
3413 "\n"
3414 "section %d\n"
3415 " name `",
3416 i
3417 );
3418 printName ( sectab_i->Name, strtab );
3419 debugBelch(
3420 "'\n"
3421 " vsize %d\n"
3422 " vaddr %d\n"
3423 " data sz %d\n"
3424 " data off %d\n"
3425 " num rel %d\n"
3426 " off rel %d\n"
3427 " ptr raw 0x%x\n",
3428 sectab_i->VirtualSize,
3429 sectab_i->VirtualAddress,
3430 sectab_i->SizeOfRawData,
3431 sectab_i->PointerToRawData,
3432 sectab_i->NumberOfRelocations,
3433 sectab_i->PointerToRelocations,
3434 sectab_i->PointerToRawData
3435 );
3436 reltab = (COFF_reloc*) (
3437 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
3438 );
3439
3440 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
3441 /* If the relocation field (a short) has overflowed, the
3442 * real count can be found in the first reloc entry.
3443 *
3444 * See Section 4.1 (last para) of the PE spec (rev6.0).
3445 */
3446 COFF_reloc* rel = (COFF_reloc*)
3447 myindex ( sizeof_COFF_reloc, reltab, 0 );
3448 noRelocs = rel->VirtualAddress;
3449 j = 1;
3450 } else {
3451 noRelocs = sectab_i->NumberOfRelocations;
3452 j = 0;
3453 }
3454
3455 for (; j < noRelocs; j++) {
3456 COFF_symbol* sym;
3457 COFF_reloc* rel = (COFF_reloc*)
3458 myindex ( sizeof_COFF_reloc, reltab, j );
3459 debugBelch(
3460 " type 0x%-4x vaddr 0x%-8x name `",
3461 (UInt32)rel->Type,
3462 rel->VirtualAddress );
3463 sym = (COFF_symbol*)
3464 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
3465 /* Hmm..mysterious looking offset - what's it for? SOF */
3466 printName ( sym->Name, strtab -10 );
3467 debugBelch("'\n" );
3468 }
3469
3470 debugBelch("\n" );
3471 }
3472 debugBelch("\n" );
3473 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
3474 debugBelch("---START of string table---\n");
3475 for (i = 4; i < *(Int32*)strtab; i++) {
3476 if (strtab[i] == 0)
3477 debugBelch("\n"); else
3478 debugBelch("%c", strtab[i] );
3479 }
3480 debugBelch("--- END of string table---\n");
3481
3482 debugBelch("\n" );
3483 i = 0;
3484 while (1) {
3485 COFF_symbol* symtab_i;
3486 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
3487 symtab_i = (COFF_symbol*)
3488 myindex ( sizeof_COFF_symbol, symtab, i );
3489 debugBelch(
3490 "symbol %d\n"
3491 " name `",
3492 i
3493 );
3494 printName ( symtab_i->Name, strtab );
3495 debugBelch(
3496 "'\n"
3497 " value 0x%x\n"
3498 " 1+sec# %d\n"
3499 " type 0x%x\n"
3500 " sclass 0x%x\n"
3501 " nAux %d\n",
3502 symtab_i->Value,
3503 (Int32)(symtab_i->SectionNumber),
3504 (UInt32)symtab_i->Type,
3505 (UInt32)symtab_i->StorageClass,
3506 (UInt32)symtab_i->NumberOfAuxSymbols
3507 );
3508 i += symtab_i->NumberOfAuxSymbols;
3509 i++;
3510 }
3511
3512 debugBelch("\n" );
3513 return 1;
3514 }
3515
3516
3517 static int
3518 ocGetNames_PEi386 ( ObjectCode* oc )
3519 {
3520 COFF_header* hdr;
3521 COFF_section* sectab;
3522 COFF_symbol* symtab;
3523 UChar* strtab;
3524
3525 UChar* sname;
3526 void* addr;
3527 int i;
3528
3529 hdr = (COFF_header*)(oc->image);
3530 sectab = (COFF_section*) (
3531 ((UChar*)(oc->image))
3532 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3533 );
3534 symtab = (COFF_symbol*) (
3535 ((UChar*)(oc->image))
3536 + hdr->PointerToSymbolTable
3537 );
3538 strtab = ((UChar*)(oc->image))
3539 + hdr->PointerToSymbolTable
3540 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3541
3542 /* Allocate space for any (local, anonymous) .bss sections. */
3543
3544 for (i = 0; i < hdr->NumberOfSections; i++) {
3545 UInt32 bss_sz;
3546 UChar* zspace;
3547 COFF_section* sectab_i
3548 = (COFF_section*)
3549 myindex ( sizeof_COFF_section, sectab, i );
3550
3551 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3552
3553 if (0 != strcmp(secname, ".bss")) {
3554 stgFree(secname);
3555 continue;
3556 }
3557
3558 stgFree(secname);
3559
3560 /* sof 10/05: the PE spec text isn't too clear regarding what
3561 * the SizeOfRawData field is supposed to hold for object
3562 * file sections containing just uninitialized data -- for executables,
3563 * it is supposed to be zero; unclear what it's supposed to be
3564 * for object files. However, VirtualSize is guaranteed to be
3565 * zero for object files, which definitely suggests that SizeOfRawData
3566 * will be non-zero (where else would the size of this .bss section be
3567 * stored?) Looking at the COFF_section info for incoming object files,
3568 * this certainly appears to be the case.
3569 *
3570 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
3571 * object files up until now. This turned out to bite us with ghc-6.4.1's use
3572 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
3573 * variable decls into to the .bss section. (The specific function in Q which
3574 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
3575 */
3576 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
3577 /* This is a non-empty .bss section. Allocate zeroed space for
3578 it, and set its PointerToRawData field such that oc->image +
3579 PointerToRawData == addr_of_zeroed_space. */
3580 bss_sz = sectab_i->VirtualSize;
3581 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
3582 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
3583 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
3584 addProddableBlock(oc, zspace, bss_sz);
3585 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
3586 }
3587
3588 /* Copy section information into the ObjectCode. */
3589
3590 for (i = 0; i < hdr->NumberOfSections; i++) {
3591 UChar* start;
3592 UChar* end;
3593 UInt32 sz;
3594
3595 SectionKind kind
3596 = SECTIONKIND_OTHER;
3597 COFF_section* sectab_i
3598 = (COFF_section*)
3599 myindex ( sizeof_COFF_section, sectab, i );
3600
3601 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3602
3603 IF_DEBUG(linker, debugBelch("section name = %s\n", secname ));
3604
3605 # if 0
3606 /* I'm sure this is the Right Way to do it. However, the
3607 alternative of testing the sectab_i->Name field seems to
3608 work ok with Cygwin.
3609 */
3610 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
3611 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
3612 kind = SECTIONKIND_CODE_OR_RODATA;
3613 # endif
3614
3615 if (0==strcmp(".text",(char*)secname) ||
3616 0==strcmp(".text.startup",(char*)secname) ||
3617 0==strcmp(".rdata",(char*)secname)||
3618 0==strcmp(".rodata",(char*)secname))
3619 kind = SECTIONKIND_CODE_OR_RODATA;
3620 if (0==strcmp(".data",(char*)secname) ||
3621 0==strcmp(".bss",(char*)secname))
3622 kind = SECTIONKIND_RWDATA;
3623
3624 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
3625 sz = sectab_i->SizeOfRawData;
3626 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
3627
3628 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
3629 end = start + sz - 1;
3630
3631 if (kind == SECTIONKIND_OTHER
3632 /* Ignore sections called which contain stabs debugging
3633 information. */
3634 && 0 != strcmp(".stab", (char*)secname)
3635 && 0 != strcmp(".stabstr", (char*)secname)
3636 /* Ignore sections called which contain exception information. */
3637 && 0 != strcmp(".pdata", (char*)secname)
3638 && 0 != strcmp(".xdata", (char*)secname)
3639 /* ignore constructor section for now */
3640 && 0 != strcmp(".ctors", (char*)secname)
3641 /* ignore section generated from .ident */
3642 && 0!= strncmp(".debug", (char*)secname, 6)
3643 /* ignore unknown section that appeared in gcc 3.4.5(?) */
3644 && 0!= strcmp(".reloc", (char*)secname)
3645 && 0 != strcmp(".rdata$zzz", (char*)secname)
3646 ) {
3647 errorBelch("Unknown PEi386 section name `%s' (while processing: %" PATH_FMT")", secname, oc->fileName);
3648 stgFree(secname);
3649 return 0;
3650 }
3651
3652 if (kind != SECTIONKIND_OTHER && end >= start) {
3653 if ((((size_t)(start)) % sizeof(void *)) != 0) {
3654 barf("Misaligned section: %p", start);
3655 }
3656
3657 addSection(oc, kind, start, end);
3658 addProddableBlock(oc, start, end - start + 1);
3659 }
3660
3661 stgFree(secname);
3662 }
3663
3664 /* Copy exported symbols into the ObjectCode. */
3665
3666 oc->n_symbols = hdr->NumberOfSymbols;
3667 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3668 "ocGetNames_PEi386(oc->symbols)");
3669 /* Call me paranoid; I don't care. */
3670 for (i = 0; i < oc->n_symbols; i++)
3671 oc->symbols[i] = NULL;
3672
3673 i = 0;
3674 while (1) {
3675 COFF_symbol* symtab_i;
3676 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
3677 symtab_i = (COFF_symbol*)
3678 myindex ( sizeof_COFF_symbol, symtab, i );
3679
3680 addr = NULL;
3681
3682 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
3683 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
3684 /* This symbol is global and defined, viz, exported */
3685 /* for MYIMAGE_SYMCLASS_EXTERNAL
3686 && !MYIMAGE_SYM_UNDEFINED,
3687 the address of the symbol is:
3688 address of relevant section + offset in section
3689 */
3690 COFF_section* sectabent
3691 = (COFF_section*) myindex ( sizeof_COFF_section,
3692 sectab,
3693 symtab_i->SectionNumber-1 );
3694 addr = ((UChar*)(oc->image))
3695 + (sectabent->PointerToRawData
3696 + symtab_i->Value);
3697 }
3698 else
3699 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
3700 && symtab_i->Value > 0) {
3701 /* This symbol isn't in any section at all, ie, global bss.
3702 Allocate zeroed space for it. */
3703 addr = stgCallocBytes(1, symtab_i->Value,
3704 "ocGetNames_PEi386(non-anonymous bss)");
3705 addSection(oc, SECTIONKIND_RWDATA, addr,
3706 ((UChar*)addr) + symtab_i->Value - 1);
3707 addProddableBlock(oc, addr, symtab_i->Value);
3708 /* debugBelch("BSS section at 0x%x\n", addr); */
3709 }
3710
3711 if (addr != NULL ) {
3712 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
3713 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
3714 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
3715 ASSERT(i >= 0 && i < oc->n_symbols);
3716 /* cstring_from_COFF_symbol_name always succeeds. */
3717 oc->symbols[i] = (char*)sname;
3718 ghciInsertStrHashTable(oc->fileName, symhash, (char*)sname, addr);
3719 } else {
3720 # if 0
3721 debugBelch(
3722 "IGNORING symbol %d\n"
3723 " name `",
3724 i
3725 );
3726 printName ( symtab_i->Name, strtab );
3727 debugBelch(
3728 "'\n"
3729 " value 0x%x\n"
3730 " 1+sec# %d\n"
3731 " type 0x%x\n"
3732 " sclass 0x%x\n"
3733 " nAux %d\n",
3734 symtab_i->Value,
3735 (Int32)(symtab_i->SectionNumber),
3736 (UInt32)symtab_i->Type,
3737 (UInt32)symtab_i->StorageClass,
3738 (UInt32)symtab_i->NumberOfAuxSymbols
3739 );
3740 # endif
3741 }
3742
3743 i += symtab_i->NumberOfAuxSymbols;
3744 i++;
3745 }
3746
3747 return 1;
3748 }
3749
3750
3751 static int
3752 ocResolve_PEi386 ( ObjectCode* oc )
3753 {
3754 COFF_header* hdr;
3755 COFF_section* sectab;
3756 COFF_symbol* symtab;
3757 UChar* strtab;
3758
3759 UInt32 A;
3760 size_t S;
3761 void * pP;
3762
3763 int i;
3764 UInt32 j, noRelocs;
3765
3766 /* ToDo: should be variable-sized? But is at least safe in the
3767 sense of buffer-overrun-proof. */
3768 UChar symbol[1000];
3769 /* debugBelch("resolving for %s\n", oc->fileName); */
3770
3771 hdr = (COFF_header*)(oc->image);
3772 sectab = (COFF_section*) (
3773 ((UChar*)(oc->image))
3774 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3775 );
3776 symtab = (COFF_symbol*) (
3777 ((UChar*)(oc->image))
3778 + hdr->PointerToSymbolTable
3779 );
3780 strtab = ((UChar*)(oc->image))
3781 + hdr->PointerToSymbolTable
3782 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3783
3784 for (i = 0; i < hdr->NumberOfSections; i++) {
3785 COFF_section* sectab_i
3786 = (COFF_section*)
3787 myindex ( sizeof_COFF_section, sectab, i );
3788 COFF_reloc* reltab
3789 = (COFF_reloc*) (
3790 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
3791 );
3792
3793 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3794
3795 /* Ignore sections called which contain stabs debugging
3796 information. */
3797 if (0 == strcmp(".stab", (char*)secname)
3798 || 0 == strcmp(".stabstr", (char*)secname)
3799 || 0 == strcmp(".pdata", (char*)secname)
3800 || 0 == strcmp(".xdata", (char*)secname)
3801 || 0 == strcmp(".ctors", (char*)secname)
3802 || 0 == strncmp(".debug", (char*)secname, 6)
3803 || 0 == strcmp(".rdata$zzz", (char*)secname)) {
3804 stgFree(secname);
3805 continue;
3806 }
3807
3808 stgFree(secname);
3809
3810 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
3811 /* If the relocation field (a short) has overflowed, the
3812 * real count can be found in the first reloc entry.
3813 *
3814 * See Section 4.1 (last para) of the PE spec (rev6.0).
3815 *
3816 * Nov2003 update: the GNU linker still doesn't correctly
3817 * handle the generation of relocatable object files with
3818 * overflown relocations. Hence the output to warn of potential
3819 * troubles.
3820 */
3821 COFF_reloc* rel = (COFF_reloc*)
3822 myindex ( sizeof_COFF_reloc, reltab, 0 );
3823 noRelocs = rel->VirtualAddress;
3824
3825 /* 10/05: we now assume (and check for) a GNU ld that is capable
3826 * of handling object files with (>2^16) of relocs.
3827 */
3828 #if 0
3829 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
3830 noRelocs);
3831 #endif
3832 j = 1;
3833 } else {
3834 noRelocs = sectab_i->NumberOfRelocations;
3835 j = 0;
3836 }
3837
3838 for (; j < noRelocs; j++) {
3839 COFF_symbol* sym;
3840 COFF_reloc* reltab_j
3841 = (COFF_reloc*)
3842 myindex ( sizeof_COFF_reloc, reltab, j );
3843
3844 /* the location to patch */
3845 pP = (
3846 ((UChar*)(oc->image))
3847 + (sectab_i->PointerToRawData
3848 + reltab_j->VirtualAddress
3849 - sectab_i->VirtualAddress )
3850 );
3851 /* the existing contents of pP */
3852 A = *(UInt32*)pP;
3853 /* the symbol to connect to */
3854 sym = (COFF_symbol*)
3855 myindex ( sizeof_COFF_symbol,
3856 symtab, reltab_j->SymbolTableIndex );
3857 IF_DEBUG(linker,
3858 debugBelch(
3859 "reloc sec %2d num %3d: type 0x%-4x "
3860 "vaddr 0x%-8x name `",
3861 i, j,
3862 (UInt32)reltab_j->Type,
3863 reltab_j->VirtualAddress );
3864 printName ( sym->Name, strtab );
3865 debugBelch("'\n" ));
3866
3867 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
3868 COFF_section* section_sym
3869 = findPEi386SectionCalled ( oc, sym->Name );
3870 if (!section_sym) {
3871 errorBelch("%" PATH_FMT ": can't find section `%s'", oc->fileName, sym->Name);
3872 return 0;
3873 }
3874 S = ((size_t)(oc->image))
3875 + ((size_t)(section_sym->PointerToRawData))
3876 + ((size_t)(sym->Value));
3877 } else {
3878 copyName ( sym->Name, strtab, symbol, 1000-1 );
3879 S = (size_t) lookupSymbol( (char*)symbol );
3880 if ((void*)S != NULL) goto foundit;
3881 errorBelch("%" PATH_FMT ": unknown symbol `%s'", oc->fileName, symbol);
3882 return 0;
3883 foundit:;
3884 }
3885 /* All supported relocations write at least 4 bytes */
3886 checkProddableBlock(oc, pP, 4);
3887 switch (reltab_j->Type) {
3888 #if defined(i386_HOST_ARCH)
3889 case MYIMAGE_REL_I386_DIR32:
3890 *(UInt32 *)pP = ((UInt32)S) + A;
3891 break;
3892 case MYIMAGE_REL_I386_REL32:
3893 /* Tricky. We have to insert a displacement at
3894 pP which, when added to the PC for the _next_
3895 insn, gives the address of the target (S).
3896 Problem is to know the address of the next insn
3897 when we only know pP. We assume that this
3898 literal field is always the last in the insn,
3899 so that the address of the next insn is pP+4
3900 -- hence the constant 4.
3901 Also I don't know if A should be added, but so
3902 far it has always been zero.
3903
3904 SOF 05/2005: 'A' (old contents of *pP) have been observed
3905 to contain values other than zero (the 'wx' object file
3906 that came with wxhaskell-0.9.4; dunno how it was compiled..).
3907 So, add displacement to old value instead of asserting
3908 A to be zero. Fixes wxhaskell-related crashes, and no other
3909 ill effects have been observed.
3910
3911 Update: the reason why we're seeing these more elaborate
3912 relocations is due to a switch in how the NCG compiles SRTs
3913 and offsets to them from info tables. SRTs live in .(ro)data,
3914 while info tables live in .text, causing GAS to emit REL32/DISP32
3915 relocations with non-zero values. Adding the displacement is
3916 the right thing to do.
3917 */
3918 *(UInt32 *)pP = ((UInt32)S) + A - ((UInt32)(size_t)pP) - 4;
3919 break;
3920 #elif defined(x86_64_HOST_ARCH)
3921 case 2: /* R_X86_64_32 */
3922 case 17: /* R_X86_64_32S */
3923 {
3924 size_t v;
3925 v = S + ((size_t)A);
3926 if (v >> 32) {
3927 copyName ( sym->Name, strtab, symbol, 1000-1 );
3928 barf("R_X86_64_32[S]: High bits are set in %zx for %s",
3929 v, (char *)symbol);
3930 }
3931 *(UInt32 *)pP = (UInt32)v;
3932 break;
3933 }
3934 case 4: /* R_X86_64_PC32 */
3935 {
3936 intptr_t v;
3937 v = ((intptr_t)S) + ((intptr_t)(Int32)A) - ((intptr_t)pP) - 4;
3938 if ((v >> 32) && ((-v) >> 32)) {
3939 copyName ( sym->Name, strtab, symbol, 1000-1 );
3940 barf("R_X86_64_PC32: High bits are set in %zx for %s",
3941 v, (char *)symbol);
3942 }
3943 *(UInt32 *)pP = (UInt32)v;
3944 break;
3945 }
3946 case 1: /* R_X86_64_64 */
3947 {
3948 UInt64 A;
3949 checkProddableBlock(oc, pP, 8);
3950 A = *(UInt64*)pP;
3951 *(UInt64 *)pP = ((UInt64)S) + ((UInt64)A);
3952 break;
3953 }
3954 #endif
3955 default:
3956 debugBelch("%" PATH_FMT ": unhandled PEi386 relocation type %d",
3957 oc->fileName, reltab_j->Type);
3958 return 0;
3959 }
3960
3961 }
3962 }
3963
3964 IF_DEBUG(linker, debugBelch("completed %" PATH_FMT, oc->fileName));
3965 return 1;
3966 }
3967
3968 #endif /* defined(OBJFORMAT_PEi386) */
3969
3970
3971 /* --------------------------------------------------------------------------
3972 * ELF specifics
3973 * ------------------------------------------------------------------------*/
3974
3975 #if defined(OBJFORMAT_ELF)
3976
3977 #define FALSE 0
3978 #define TRUE 1
3979
3980 #if defined(sparc_HOST_ARCH)
3981 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
3982 #elif defined(i386_HOST_ARCH)
3983 # define ELF_TARGET_386 /* Used inside <elf.h> */
3984 #elif defined(x86_64_HOST_ARCH)
3985 # define ELF_TARGET_X64_64
3986 # define ELF_64BIT
3987 #elif defined(powerpc64_HOST_ARCH)
3988 # define ELF_64BIT
3989 #endif
3990
3991 #if !defined(openbsd_HOST_OS)
3992 # include <elf.h>
3993 #else
3994 /* openbsd elf has things in different places, with diff names */
3995 # include <elf_abi.h>
3996 # include <machine/reloc.h>
3997 # define R_386_32 RELOC_32
3998 # define R_386_PC32 RELOC_PC32
3999 #endif
4000
4001 /* If elf.h doesn't define it */
4002 # ifndef R_X86_64_PC64
4003 # define R_X86_64_PC64 24
4004 # endif
4005
4006 /*
4007 * Workaround for libc implementations (e.g. eglibc) with incomplete
4008 * relocation lists
4009 */
4010 #ifndef R_ARM_THM_CALL
4011 # define R_ARM_THM_CALL 10
4012 #endif
4013 #ifndef R_ARM_CALL
4014 # define R_ARM_CALL 28
4015 #endif
4016 #ifndef R_ARM_JUMP24
4017 # define R_ARM_JUMP24 29
4018 #endif
4019 #ifndef R_ARM_THM_JUMP24
4020 # define R_ARM_THM_JUMP24 30
4021 #endif
4022 #ifndef R_ARM_TARGET1
4023 # define R_ARM_TARGET1 38
4024 #endif
4025 #ifndef R_ARM_MOVW_ABS_NC
4026 # define R_ARM_MOVW_ABS_NC 43
4027 #endif
4028 #ifndef R_ARM_MOVT_ABS