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