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