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