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