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