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