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