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