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