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