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