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