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