1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2000-2004
7 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
13 /* Linux needs _GNU_SOURCE to get RTLD_DEFAULT from <dlfcn.h> and
14 MREMAP_MAYMOVE from <sys/mman.h>.
16 #if defined(__linux__) || defined(__GLIBC__)
23 #include "sm/Storage.h"
26 #include "LinkerInternals.h"
29 #include "StgPrimFloat.h" // for __int_encodeFloat etc.
32 #if !defined(mingw32_HOST_OS)
33 #include "posix/Signals.h"
36 // get protos for is*()
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
52 #if defined(HAVE_DLFCN_H)
56 #if defined(cygwin32_HOST_OS)
61 #ifdef HAVE_SYS_TIME_H
65 #include <sys/fcntl.h>
66 #include <sys/termios.h>
67 #include <sys/utime.h>
68 #include <sys/utsname.h>
72 #if defined(linux_HOST_OS ) || defined(freebsd_HOST_OS) || \
73 defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS ) || \
74 defined(openbsd_HOST_OS ) || \
75 ( defined(darwin_HOST_OS ) && !defined(powerpc_HOST_ARCH) ) || \
76 defined(kfreebsdgnu_HOST_OS)
77 /* Don't use mmap on powerpc-apple-darwin as mmap doesn't support
78 * reallocating but we need to allocate jump islands just after each
79 * object images. Otherwise relative branches to jump islands can fail
80 * due to 24-bits displacement overflow.
92 #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)
93 # define OBJFORMAT_ELF
94 # include <regex.h> // regex is already used by dlopen() so this is OK
95 // to use here without requiring an additional lib
96 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
97 # define OBJFORMAT_PEi386
100 #elif defined(darwin_HOST_OS)
101 # define OBJFORMAT_MACHO
103 # include <mach-o/loader.h>
104 # include <mach-o/nlist.h>
105 # include <mach-o/reloc.h>
106 #if !defined(HAVE_DLFCN_H)
107 # include <mach-o/dyld.h>
109 #if defined(powerpc_HOST_ARCH)
110 # include <mach-o/ppc/reloc.h>
112 #if defined(x86_64_HOST_ARCH)
113 # include <mach-o/x86_64/reloc.h>
117 #if defined(x86_64_HOST_ARCH) && defined(darwin_HOST_OS)
121 /* Hash table mapping symbol names to Symbol */
122 static /*Str*/HashTable *symhash;
124 /* Hash table mapping symbol names to StgStablePtr */
125 static /*Str*/HashTable *stablehash;
127 /* List of currently loaded objects */
128 ObjectCode *objects = NULL; /* initially empty */
130 static HsInt loadOc( ObjectCode* oc );
131 static ObjectCode* mkOc( char *path, char *image, int imageSize,
132 char *archiveMemberName
134 #ifdef darwin_HOST_OS
140 #if defined(OBJFORMAT_ELF)
141 static int ocVerifyImage_ELF ( ObjectCode* oc );
142 static int ocGetNames_ELF ( ObjectCode* oc );
143 static int ocResolve_ELF ( ObjectCode* oc );
144 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
145 static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
147 #elif defined(OBJFORMAT_PEi386)
148 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
149 static int ocGetNames_PEi386 ( ObjectCode* oc );
150 static int ocResolve_PEi386 ( ObjectCode* oc );
151 static void *lookupSymbolInDLLs ( unsigned char *lbl );
152 static void zapTrailingAtSign ( unsigned char *sym );
153 #elif defined(OBJFORMAT_MACHO)
154 static int ocVerifyImage_MachO ( ObjectCode* oc );
155 static int ocGetNames_MachO ( ObjectCode* oc );
156 static int ocResolve_MachO ( ObjectCode* oc );
159 static int machoGetMisalignment( FILE * );
161 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
162 static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
164 #ifdef powerpc_HOST_ARCH
165 static void machoInitSymbolsWithoutUnderscore( void );
169 /* on x86_64 we have a problem with relocating symbol references in
170 * code that was compiled without -fPIC. By default, the small memory
171 * model is used, which assumes that symbol references can fit in a
172 * 32-bit slot. The system dynamic linker makes this work for
173 * references to shared libraries by either (a) allocating a jump
174 * table slot for code references, or (b) moving the symbol at load
175 * time (and copying its contents, if necessary) for data references.
177 * We unfortunately can't tell whether symbol references are to code
178 * or data. So for now we assume they are code (the vast majority
179 * are), and allocate jump-table slots. Unfortunately this will
180 * SILENTLY generate crashing code for data references. This hack is
181 * enabled by X86_64_ELF_NONPIC_HACK.
183 * One workaround is to use shared Haskell libraries. This is
184 * coming. Another workaround is to keep the static libraries but
185 * compile them with -fPIC, because that will generate PIC references
186 * to data which can be relocated. The PIC code is still too green to
187 * do this systematically, though.
190 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
192 * Naming Scheme for Symbol Macros
194 * SymI_*: symbol is internal to the RTS. It resides in an object
195 * file/library that is statically.
196 * SymE_*: symbol is external to the RTS library. It might be linked
199 * Sym*_HasProto : the symbol prototype is imported in an include file
200 * or defined explicitly
201 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
202 * default proto extern void sym(void);
204 #define X86_64_ELF_NONPIC_HACK 1
206 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
207 * small memory model on this architecture (see gcc docs,
210 * MAP_32BIT not available on OpenBSD/amd64
212 #if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT)
213 #define TRY_MAP_32BIT MAP_32BIT
215 #define TRY_MAP_32BIT 0
219 * Due to the small memory model (see above), on x86_64 we have to map
220 * all our non-PIC object files into the low 2Gb of the address space
221 * (why 2Gb and not 4Gb? Because all addresses must be reachable
222 * using a 32-bit signed PC-relative offset). On Linux we can do this
223 * using the MAP_32BIT flag to mmap(), however on other OSs
224 * (e.g. *BSD, see #2063, and also on Linux inside Xen, see #2512), we
225 * can't do this. So on these systems, we have to pick a base address
226 * in the low 2Gb of the address space and try to allocate memory from
229 * We pick a default address based on the OS, but also make this
230 * configurable via an RTS flag (+RTS -xm)
232 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
234 #if defined(MAP_32BIT)
235 // Try to use MAP_32BIT
236 #define MMAP_32BIT_BASE_DEFAULT 0
239 #define MMAP_32BIT_BASE_DEFAULT 0x40000000
242 static void *mmap_32bit_base = (void *)MMAP_32BIT_BASE_DEFAULT;
245 /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */
246 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
247 #define MAP_ANONYMOUS MAP_ANON
250 /* -----------------------------------------------------------------------------
251 * Built-in symbols from the RTS
254 typedef struct _RtsSymbolVal {
259 #define Maybe_Stable_Names SymI_HasProto(stg_mkWeakzh) \
260 SymI_HasProto(stg_mkWeakForeignEnvzh) \
261 SymI_HasProto(stg_makeStableNamezh) \
262 SymI_HasProto(stg_finalizzeWeakzh)
264 #if !defined (mingw32_HOST_OS)
265 #define RTS_POSIX_ONLY_SYMBOLS \
266 SymI_HasProto(__hscore_get_saved_termios) \
267 SymI_HasProto(__hscore_set_saved_termios) \
268 SymI_HasProto(shutdownHaskellAndSignal) \
269 SymI_HasProto(lockFile) \
270 SymI_HasProto(unlockFile) \
271 SymI_HasProto(signal_handlers) \
272 SymI_HasProto(stg_sig_install) \
273 SymI_HasProto(rtsTimerSignal) \
274 SymI_NeedsProto(nocldstop)
277 #if defined (cygwin32_HOST_OS)
278 #define RTS_MINGW_ONLY_SYMBOLS /**/
279 /* Don't have the ability to read import libs / archives, so
280 * we have to stupidly list a lot of what libcygwin.a
283 #define RTS_CYGWIN_ONLY_SYMBOLS \
284 SymI_HasProto(regfree) \
285 SymI_HasProto(regexec) \
286 SymI_HasProto(regerror) \
287 SymI_HasProto(regcomp) \
288 SymI_HasProto(__errno) \
289 SymI_HasProto(access) \
290 SymI_HasProto(chmod) \
291 SymI_HasProto(chdir) \
292 SymI_HasProto(close) \
293 SymI_HasProto(creat) \
295 SymI_HasProto(dup2) \
296 SymI_HasProto(fstat) \
297 SymI_HasProto(fcntl) \
298 SymI_HasProto(getcwd) \
299 SymI_HasProto(getenv) \
300 SymI_HasProto(lseek) \
301 SymI_HasProto(open) \
302 SymI_HasProto(fpathconf) \
303 SymI_HasProto(pathconf) \
304 SymI_HasProto(stat) \
306 SymI_HasProto(tanh) \
307 SymI_HasProto(cosh) \
308 SymI_HasProto(sinh) \
309 SymI_HasProto(atan) \
310 SymI_HasProto(acos) \
311 SymI_HasProto(asin) \
317 SymI_HasProto(sqrt) \
318 SymI_HasProto(localtime_r) \
319 SymI_HasProto(gmtime_r) \
320 SymI_HasProto(mktime) \
321 SymI_NeedsProto(_imp___tzname) \
322 SymI_HasProto(gettimeofday) \
323 SymI_HasProto(timezone) \
324 SymI_HasProto(tcgetattr) \
325 SymI_HasProto(tcsetattr) \
326 SymI_HasProto(memcpy) \
327 SymI_HasProto(memmove) \
328 SymI_HasProto(realloc) \
329 SymI_HasProto(malloc) \
330 SymI_HasProto(free) \
331 SymI_HasProto(fork) \
332 SymI_HasProto(lstat) \
333 SymI_HasProto(isatty) \
334 SymI_HasProto(mkdir) \
335 SymI_HasProto(opendir) \
336 SymI_HasProto(readdir) \
337 SymI_HasProto(rewinddir) \
338 SymI_HasProto(closedir) \
339 SymI_HasProto(link) \
340 SymI_HasProto(mkfifo) \
341 SymI_HasProto(pipe) \
342 SymI_HasProto(read) \
343 SymI_HasProto(rename) \
344 SymI_HasProto(rmdir) \
345 SymI_HasProto(select) \
346 SymI_HasProto(system) \
347 SymI_HasProto(write) \
348 SymI_HasProto(strcmp) \
349 SymI_HasProto(strcpy) \
350 SymI_HasProto(strncpy) \
351 SymI_HasProto(strerror) \
352 SymI_HasProto(sigaddset) \
353 SymI_HasProto(sigemptyset) \
354 SymI_HasProto(sigprocmask) \
355 SymI_HasProto(umask) \
356 SymI_HasProto(uname) \
357 SymI_HasProto(unlink) \
358 SymI_HasProto(utime) \
359 SymI_HasProto(waitpid)
361 #elif !defined(mingw32_HOST_OS)
362 #define RTS_MINGW_ONLY_SYMBOLS /**/
363 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
364 #else /* defined(mingw32_HOST_OS) */
365 #define RTS_POSIX_ONLY_SYMBOLS /**/
366 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
368 #if HAVE_GETTIMEOFDAY
369 #define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
371 #define RTS_MINGW_GETTIMEOFDAY_SYM /**/
374 #if HAVE___MINGW_VFPRINTF
375 #define RTS___MINGW_VFPRINTF_SYM SymI_HasProto(__mingw_vfprintf)
377 #define RTS___MINGW_VFPRINTF_SYM /**/
380 /* These are statically linked from the mingw libraries into the ghc
381 executable, so we have to employ this hack. */
382 #define RTS_MINGW_ONLY_SYMBOLS \
383 SymI_HasProto(stg_asyncReadzh) \
384 SymI_HasProto(stg_asyncWritezh) \
385 SymI_HasProto(stg_asyncDoProczh) \
386 SymI_HasProto(memset) \
387 SymI_HasProto(inet_ntoa) \
388 SymI_HasProto(inet_addr) \
389 SymI_HasProto(htonl) \
390 SymI_HasProto(recvfrom) \
391 SymI_HasProto(listen) \
392 SymI_HasProto(bind) \
393 SymI_HasProto(shutdown) \
394 SymI_HasProto(connect) \
395 SymI_HasProto(htons) \
396 SymI_HasProto(ntohs) \
397 SymI_HasProto(getservbyname) \
398 SymI_HasProto(getservbyport) \
399 SymI_HasProto(getprotobynumber) \
400 SymI_HasProto(getprotobyname) \
401 SymI_HasProto(gethostbyname) \
402 SymI_HasProto(gethostbyaddr) \
403 SymI_HasProto(gethostname) \
404 SymI_HasProto(strcpy) \
405 SymI_HasProto(strncpy) \
406 SymI_HasProto(abort) \
407 SymI_NeedsProto(_alloca) \
408 SymI_HasProto(isxdigit) \
409 SymI_HasProto(isupper) \
410 SymI_HasProto(ispunct) \
411 SymI_HasProto(islower) \
412 SymI_HasProto(isspace) \
413 SymI_HasProto(isprint) \
414 SymI_HasProto(isdigit) \
415 SymI_HasProto(iscntrl) \
416 SymI_HasProto(isalpha) \
417 SymI_HasProto(isalnum) \
418 SymI_HasProto(isascii) \
419 RTS___MINGW_VFPRINTF_SYM \
420 SymI_HasProto(strcmp) \
421 SymI_HasProto(memmove) \
422 SymI_HasProto(realloc) \
423 SymI_HasProto(malloc) \
425 SymI_HasProto(tanh) \
426 SymI_HasProto(cosh) \
427 SymI_HasProto(sinh) \
428 SymI_HasProto(atan) \
429 SymI_HasProto(acos) \
430 SymI_HasProto(asin) \
436 SymI_HasProto(sqrt) \
437 SymI_HasProto(powf) \
438 SymI_HasProto(tanhf) \
439 SymI_HasProto(coshf) \
440 SymI_HasProto(sinhf) \
441 SymI_HasProto(atanf) \
442 SymI_HasProto(acosf) \
443 SymI_HasProto(asinf) \
444 SymI_HasProto(tanf) \
445 SymI_HasProto(cosf) \
446 SymI_HasProto(sinf) \
447 SymI_HasProto(expf) \
448 SymI_HasProto(logf) \
449 SymI_HasProto(sqrtf) \
451 SymI_HasProto(erfc) \
452 SymI_HasProto(erff) \
453 SymI_HasProto(erfcf) \
454 SymI_HasProto(memcpy) \
455 SymI_HasProto(rts_InstallConsoleEvent) \
456 SymI_HasProto(rts_ConsoleHandlerDone) \
457 SymI_NeedsProto(mktime) \
458 SymI_NeedsProto(_imp___timezone) \
459 SymI_NeedsProto(_imp___tzname) \
460 SymI_NeedsProto(_imp__tzname) \
461 SymI_NeedsProto(_imp___iob) \
462 SymI_NeedsProto(_imp___osver) \
463 SymI_NeedsProto(localtime) \
464 SymI_NeedsProto(gmtime) \
465 SymI_NeedsProto(opendir) \
466 SymI_NeedsProto(readdir) \
467 SymI_NeedsProto(rewinddir) \
468 SymI_NeedsProto(_imp____mb_cur_max) \
469 SymI_NeedsProto(_imp___pctype) \
470 SymI_NeedsProto(__chkstk) \
471 RTS_MINGW_GETTIMEOFDAY_SYM \
472 SymI_NeedsProto(closedir)
476 #if defined(darwin_HOST_OS) && HAVE_PRINTF_LDBLSTUB
477 #define RTS_DARWIN_ONLY_SYMBOLS \
478 SymI_NeedsProto(asprintf$LDBLStub) \
479 SymI_NeedsProto(err$LDBLStub) \
480 SymI_NeedsProto(errc$LDBLStub) \
481 SymI_NeedsProto(errx$LDBLStub) \
482 SymI_NeedsProto(fprintf$LDBLStub) \
483 SymI_NeedsProto(fscanf$LDBLStub) \
484 SymI_NeedsProto(fwprintf$LDBLStub) \
485 SymI_NeedsProto(fwscanf$LDBLStub) \
486 SymI_NeedsProto(printf$LDBLStub) \
487 SymI_NeedsProto(scanf$LDBLStub) \
488 SymI_NeedsProto(snprintf$LDBLStub) \
489 SymI_NeedsProto(sprintf$LDBLStub) \
490 SymI_NeedsProto(sscanf$LDBLStub) \
491 SymI_NeedsProto(strtold$LDBLStub) \
492 SymI_NeedsProto(swprintf$LDBLStub) \
493 SymI_NeedsProto(swscanf$LDBLStub) \
494 SymI_NeedsProto(syslog$LDBLStub) \
495 SymI_NeedsProto(vasprintf$LDBLStub) \
496 SymI_NeedsProto(verr$LDBLStub) \
497 SymI_NeedsProto(verrc$LDBLStub) \
498 SymI_NeedsProto(verrx$LDBLStub) \
499 SymI_NeedsProto(vfprintf$LDBLStub) \
500 SymI_NeedsProto(vfscanf$LDBLStub) \
501 SymI_NeedsProto(vfwprintf$LDBLStub) \
502 SymI_NeedsProto(vfwscanf$LDBLStub) \
503 SymI_NeedsProto(vprintf$LDBLStub) \
504 SymI_NeedsProto(vscanf$LDBLStub) \
505 SymI_NeedsProto(vsnprintf$LDBLStub) \
506 SymI_NeedsProto(vsprintf$LDBLStub) \
507 SymI_NeedsProto(vsscanf$LDBLStub) \
508 SymI_NeedsProto(vswprintf$LDBLStub) \
509 SymI_NeedsProto(vswscanf$LDBLStub) \
510 SymI_NeedsProto(vsyslog$LDBLStub) \
511 SymI_NeedsProto(vwarn$LDBLStub) \
512 SymI_NeedsProto(vwarnc$LDBLStub) \
513 SymI_NeedsProto(vwarnx$LDBLStub) \
514 SymI_NeedsProto(vwprintf$LDBLStub) \
515 SymI_NeedsProto(vwscanf$LDBLStub) \
516 SymI_NeedsProto(warn$LDBLStub) \
517 SymI_NeedsProto(warnc$LDBLStub) \
518 SymI_NeedsProto(warnx$LDBLStub) \
519 SymI_NeedsProto(wcstold$LDBLStub) \
520 SymI_NeedsProto(wprintf$LDBLStub) \
521 SymI_NeedsProto(wscanf$LDBLStub)
523 #define RTS_DARWIN_ONLY_SYMBOLS
527 # define MAIN_CAP_SYM SymI_HasProto(MainCapability)
529 # define MAIN_CAP_SYM
532 #if !defined(mingw32_HOST_OS)
533 #define RTS_USER_SIGNALS_SYMBOLS \
534 SymI_HasProto(setIOManagerControlFd) \
535 SymI_HasProto(setIOManagerWakeupFd) \
536 SymI_HasProto(ioManagerWakeup) \
537 SymI_HasProto(blockUserSignals) \
538 SymI_HasProto(unblockUserSignals)
540 #define RTS_USER_SIGNALS_SYMBOLS \
541 SymI_HasProto(ioManagerWakeup) \
542 SymI_HasProto(sendIOManagerEvent) \
543 SymI_HasProto(readIOManagerEvent) \
544 SymI_HasProto(getIOManagerEvent) \
545 SymI_HasProto(console_handler)
548 #define RTS_LIBFFI_SYMBOLS \
549 SymE_NeedsProto(ffi_prep_cif) \
550 SymE_NeedsProto(ffi_call) \
551 SymE_NeedsProto(ffi_type_void) \
552 SymE_NeedsProto(ffi_type_float) \
553 SymE_NeedsProto(ffi_type_double) \
554 SymE_NeedsProto(ffi_type_sint64) \
555 SymE_NeedsProto(ffi_type_uint64) \
556 SymE_NeedsProto(ffi_type_sint32) \
557 SymE_NeedsProto(ffi_type_uint32) \
558 SymE_NeedsProto(ffi_type_sint16) \
559 SymE_NeedsProto(ffi_type_uint16) \
560 SymE_NeedsProto(ffi_type_sint8) \
561 SymE_NeedsProto(ffi_type_uint8) \
562 SymE_NeedsProto(ffi_type_pointer)
564 #ifdef TABLES_NEXT_TO_CODE
565 #define RTS_RET_SYMBOLS /* nothing */
567 #define RTS_RET_SYMBOLS \
568 SymI_HasProto(stg_enter_ret) \
569 SymI_HasProto(stg_gc_fun_ret) \
570 SymI_HasProto(stg_ap_v_ret) \
571 SymI_HasProto(stg_ap_f_ret) \
572 SymI_HasProto(stg_ap_d_ret) \
573 SymI_HasProto(stg_ap_l_ret) \
574 SymI_HasProto(stg_ap_n_ret) \
575 SymI_HasProto(stg_ap_p_ret) \
576 SymI_HasProto(stg_ap_pv_ret) \
577 SymI_HasProto(stg_ap_pp_ret) \
578 SymI_HasProto(stg_ap_ppv_ret) \
579 SymI_HasProto(stg_ap_ppp_ret) \
580 SymI_HasProto(stg_ap_pppv_ret) \
581 SymI_HasProto(stg_ap_pppp_ret) \
582 SymI_HasProto(stg_ap_ppppp_ret) \
583 SymI_HasProto(stg_ap_pppppp_ret)
586 /* Modules compiled with -ticky may mention ticky counters */
587 /* This list should marry up with the one in $(TOP)/includes/stg/Ticky.h */
588 #define RTS_TICKY_SYMBOLS \
589 SymI_NeedsProto(ticky_entry_ctrs) \
590 SymI_NeedsProto(top_ct) \
592 SymI_HasProto(ENT_VIA_NODE_ctr) \
593 SymI_HasProto(ENT_STATIC_THK_ctr) \
594 SymI_HasProto(ENT_DYN_THK_ctr) \
595 SymI_HasProto(ENT_STATIC_FUN_DIRECT_ctr) \
596 SymI_HasProto(ENT_DYN_FUN_DIRECT_ctr) \
597 SymI_HasProto(ENT_STATIC_CON_ctr) \
598 SymI_HasProto(ENT_DYN_CON_ctr) \
599 SymI_HasProto(ENT_STATIC_IND_ctr) \
600 SymI_HasProto(ENT_DYN_IND_ctr) \
601 SymI_HasProto(ENT_PERM_IND_ctr) \
602 SymI_HasProto(ENT_PAP_ctr) \
603 SymI_HasProto(ENT_AP_ctr) \
604 SymI_HasProto(ENT_AP_STACK_ctr) \
605 SymI_HasProto(ENT_BH_ctr) \
606 SymI_HasProto(UNKNOWN_CALL_ctr) \
607 SymI_HasProto(SLOW_CALL_v_ctr) \
608 SymI_HasProto(SLOW_CALL_f_ctr) \
609 SymI_HasProto(SLOW_CALL_d_ctr) \
610 SymI_HasProto(SLOW_CALL_l_ctr) \
611 SymI_HasProto(SLOW_CALL_n_ctr) \
612 SymI_HasProto(SLOW_CALL_p_ctr) \
613 SymI_HasProto(SLOW_CALL_pv_ctr) \
614 SymI_HasProto(SLOW_CALL_pp_ctr) \
615 SymI_HasProto(SLOW_CALL_ppv_ctr) \
616 SymI_HasProto(SLOW_CALL_ppp_ctr) \
617 SymI_HasProto(SLOW_CALL_pppv_ctr) \
618 SymI_HasProto(SLOW_CALL_pppp_ctr) \
619 SymI_HasProto(SLOW_CALL_ppppp_ctr) \
620 SymI_HasProto(SLOW_CALL_pppppp_ctr) \
621 SymI_HasProto(SLOW_CALL_OTHER_ctr) \
622 SymI_HasProto(ticky_slow_call_unevald) \
623 SymI_HasProto(SLOW_CALL_ctr) \
624 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_ctr) \
625 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_CHUNKS_ctr) \
626 SymI_HasProto(KNOWN_CALL_ctr) \
627 SymI_HasProto(KNOWN_CALL_TOO_FEW_ARGS_ctr) \
628 SymI_HasProto(KNOWN_CALL_EXTRA_ARGS_ctr) \
629 SymI_HasProto(SLOW_CALL_FUN_TOO_FEW_ctr) \
630 SymI_HasProto(SLOW_CALL_FUN_CORRECT_ctr) \
631 SymI_HasProto(SLOW_CALL_FUN_TOO_MANY_ctr) \
632 SymI_HasProto(SLOW_CALL_PAP_TOO_FEW_ctr) \
633 SymI_HasProto(SLOW_CALL_PAP_CORRECT_ctr) \
634 SymI_HasProto(SLOW_CALL_PAP_TOO_MANY_ctr) \
635 SymI_HasProto(SLOW_CALL_UNEVALD_ctr) \
636 SymI_HasProto(UPDF_OMITTED_ctr) \
637 SymI_HasProto(UPDF_PUSHED_ctr) \
638 SymI_HasProto(CATCHF_PUSHED_ctr) \
639 SymI_HasProto(UPDF_RCC_PUSHED_ctr) \
640 SymI_HasProto(UPDF_RCC_OMITTED_ctr) \
641 SymI_HasProto(UPD_SQUEEZED_ctr) \
642 SymI_HasProto(UPD_CON_IN_NEW_ctr) \
643 SymI_HasProto(UPD_CON_IN_PLACE_ctr) \
644 SymI_HasProto(UPD_PAP_IN_NEW_ctr) \
645 SymI_HasProto(UPD_PAP_IN_PLACE_ctr) \
646 SymI_HasProto(ALLOC_HEAP_ctr) \
647 SymI_HasProto(ALLOC_HEAP_tot) \
648 SymI_HasProto(ALLOC_FUN_ctr) \
649 SymI_HasProto(ALLOC_FUN_adm) \
650 SymI_HasProto(ALLOC_FUN_gds) \
651 SymI_HasProto(ALLOC_FUN_slp) \
652 SymI_HasProto(UPD_NEW_IND_ctr) \
653 SymI_HasProto(UPD_NEW_PERM_IND_ctr) \
654 SymI_HasProto(UPD_OLD_IND_ctr) \
655 SymI_HasProto(UPD_OLD_PERM_IND_ctr) \
656 SymI_HasProto(UPD_BH_UPDATABLE_ctr) \
657 SymI_HasProto(UPD_BH_SINGLE_ENTRY_ctr) \
658 SymI_HasProto(UPD_CAF_BH_UPDATABLE_ctr) \
659 SymI_HasProto(UPD_CAF_BH_SINGLE_ENTRY_ctr) \
660 SymI_HasProto(GC_SEL_ABANDONED_ctr) \
661 SymI_HasProto(GC_SEL_MINOR_ctr) \
662 SymI_HasProto(GC_SEL_MAJOR_ctr) \
663 SymI_HasProto(GC_FAILED_PROMOTION_ctr) \
664 SymI_HasProto(ALLOC_UP_THK_ctr) \
665 SymI_HasProto(ALLOC_SE_THK_ctr) \
666 SymI_HasProto(ALLOC_THK_adm) \
667 SymI_HasProto(ALLOC_THK_gds) \
668 SymI_HasProto(ALLOC_THK_slp) \
669 SymI_HasProto(ALLOC_CON_ctr) \
670 SymI_HasProto(ALLOC_CON_adm) \
671 SymI_HasProto(ALLOC_CON_gds) \
672 SymI_HasProto(ALLOC_CON_slp) \
673 SymI_HasProto(ALLOC_TUP_ctr) \
674 SymI_HasProto(ALLOC_TUP_adm) \
675 SymI_HasProto(ALLOC_TUP_gds) \
676 SymI_HasProto(ALLOC_TUP_slp) \
677 SymI_HasProto(ALLOC_BH_ctr) \
678 SymI_HasProto(ALLOC_BH_adm) \
679 SymI_HasProto(ALLOC_BH_gds) \
680 SymI_HasProto(ALLOC_BH_slp) \
681 SymI_HasProto(ALLOC_PRIM_ctr) \
682 SymI_HasProto(ALLOC_PRIM_adm) \
683 SymI_HasProto(ALLOC_PRIM_gds) \
684 SymI_HasProto(ALLOC_PRIM_slp) \
685 SymI_HasProto(ALLOC_PAP_ctr) \
686 SymI_HasProto(ALLOC_PAP_adm) \
687 SymI_HasProto(ALLOC_PAP_gds) \
688 SymI_HasProto(ALLOC_PAP_slp) \
689 SymI_HasProto(ALLOC_TSO_ctr) \
690 SymI_HasProto(ALLOC_TSO_adm) \
691 SymI_HasProto(ALLOC_TSO_gds) \
692 SymI_HasProto(ALLOC_TSO_slp) \
693 SymI_HasProto(RET_NEW_ctr) \
694 SymI_HasProto(RET_OLD_ctr) \
695 SymI_HasProto(RET_UNBOXED_TUP_ctr) \
696 SymI_HasProto(RET_SEMI_loads_avoided)
699 // On most platforms, the garbage collector rewrites references
700 // to small integer and char objects to a set of common, shared ones.
702 // We don't do this when compiling to Windows DLLs at the moment because
703 // it doesn't support cross package data references well.
705 #if defined(__PIC__) && defined(mingw32_HOST_OS)
706 #define RTS_INTCHAR_SYMBOLS
708 #define RTS_INTCHAR_SYMBOLS \
709 SymI_HasProto(stg_CHARLIKE_closure) \
710 SymI_HasProto(stg_INTLIKE_closure)
714 #define RTS_SYMBOLS \
717 SymI_HasProto(StgReturn) \
718 SymI_HasProto(stg_enter_info) \
719 SymI_HasProto(stg_gc_void_info) \
720 SymI_HasProto(__stg_gc_enter_1) \
721 SymI_HasProto(stg_gc_noregs) \
722 SymI_HasProto(stg_gc_unpt_r1_info) \
723 SymI_HasProto(stg_gc_unpt_r1) \
724 SymI_HasProto(stg_gc_unbx_r1_info) \
725 SymI_HasProto(stg_gc_unbx_r1) \
726 SymI_HasProto(stg_gc_f1_info) \
727 SymI_HasProto(stg_gc_f1) \
728 SymI_HasProto(stg_gc_d1_info) \
729 SymI_HasProto(stg_gc_d1) \
730 SymI_HasProto(stg_gc_l1_info) \
731 SymI_HasProto(stg_gc_l1) \
732 SymI_HasProto(__stg_gc_fun) \
733 SymI_HasProto(stg_gc_fun_info) \
734 SymI_HasProto(stg_gc_gen) \
735 SymI_HasProto(stg_gc_gen_info) \
736 SymI_HasProto(stg_gc_gen_hp) \
737 SymI_HasProto(stg_gc_ut) \
738 SymI_HasProto(stg_gen_yield) \
739 SymI_HasProto(stg_yield_noregs) \
740 SymI_HasProto(stg_yield_to_interpreter) \
741 SymI_HasProto(stg_gen_block) \
742 SymI_HasProto(stg_block_noregs) \
743 SymI_HasProto(stg_block_1) \
744 SymI_HasProto(stg_block_takemvar) \
745 SymI_HasProto(stg_block_putmvar) \
747 SymI_HasProto(MallocFailHook) \
748 SymI_HasProto(OnExitHook) \
749 SymI_HasProto(OutOfHeapHook) \
750 SymI_HasProto(StackOverflowHook) \
751 SymI_HasProto(addDLL) \
752 SymI_HasProto(__int_encodeDouble) \
753 SymI_HasProto(__word_encodeDouble) \
754 SymI_HasProto(__2Int_encodeDouble) \
755 SymI_HasProto(__int_encodeFloat) \
756 SymI_HasProto(__word_encodeFloat) \
757 SymI_HasProto(stg_atomicallyzh) \
758 SymI_HasProto(barf) \
759 SymI_HasProto(debugBelch) \
760 SymI_HasProto(errorBelch) \
761 SymI_HasProto(sysErrorBelch) \
762 SymI_HasProto(stg_getMaskingStatezh) \
763 SymI_HasProto(stg_maskAsyncExceptionszh) \
764 SymI_HasProto(stg_maskUninterruptiblezh) \
765 SymI_HasProto(stg_catchzh) \
766 SymI_HasProto(stg_catchRetryzh) \
767 SymI_HasProto(stg_catchSTMzh) \
768 SymI_HasProto(stg_checkzh) \
769 SymI_HasProto(closure_flags) \
770 SymI_HasProto(cmp_thread) \
771 SymI_HasProto(createAdjustor) \
772 SymI_HasProto(stg_decodeDoublezu2Intzh) \
773 SymI_HasProto(stg_decodeFloatzuIntzh) \
774 SymI_HasProto(defaultsHook) \
775 SymI_HasProto(stg_delayzh) \
776 SymI_HasProto(stg_deRefWeakzh) \
777 SymI_HasProto(stg_deRefStablePtrzh) \
778 SymI_HasProto(dirty_MUT_VAR) \
779 SymI_HasProto(stg_forkzh) \
780 SymI_HasProto(stg_forkOnzh) \
781 SymI_HasProto(forkProcess) \
782 SymI_HasProto(forkOS_createThread) \
783 SymI_HasProto(freeHaskellFunctionPtr) \
784 SymI_HasProto(getOrSetTypeableStore) \
785 SymI_HasProto(getOrSetGHCConcSignalSignalHandlerStore) \
786 SymI_HasProto(getOrSetGHCConcWindowsPendingDelaysStore) \
787 SymI_HasProto(getOrSetGHCConcWindowsIOManagerThreadStore) \
788 SymI_HasProto(getOrSetGHCConcWindowsProddingStore) \
789 SymI_HasProto(getOrSetSystemEventThreadEventManagerStore) \
790 SymI_HasProto(getOrSetSystemEventThreadIOManagerThreadStore) \
791 SymI_HasProto(genSymZh) \
792 SymI_HasProto(genericRaise) \
793 SymI_HasProto(getProgArgv) \
794 SymI_HasProto(getFullProgArgv) \
795 SymI_HasProto(getStablePtr) \
796 SymI_HasProto(hs_init) \
797 SymI_HasProto(hs_exit) \
798 SymI_HasProto(hs_set_argv) \
799 SymI_HasProto(hs_add_root) \
800 SymI_HasProto(hs_perform_gc) \
801 SymI_HasProto(hs_free_stable_ptr) \
802 SymI_HasProto(hs_free_fun_ptr) \
803 SymI_HasProto(hs_hpc_rootModule) \
804 SymI_HasProto(hs_hpc_module) \
805 SymI_HasProto(initLinker) \
806 SymI_HasProto(stg_unpackClosurezh) \
807 SymI_HasProto(stg_getApStackValzh) \
808 SymI_HasProto(stg_getSparkzh) \
809 SymI_HasProto(stg_numSparkszh) \
810 SymI_HasProto(stg_isCurrentThreadBoundzh) \
811 SymI_HasProto(stg_isEmptyMVarzh) \
812 SymI_HasProto(stg_killThreadzh) \
813 SymI_HasProto(loadArchive) \
814 SymI_HasProto(loadObj) \
815 SymI_HasProto(insertStableSymbol) \
816 SymI_HasProto(insertSymbol) \
817 SymI_HasProto(lookupSymbol) \
818 SymI_HasProto(stg_makeStablePtrzh) \
819 SymI_HasProto(stg_mkApUpd0zh) \
820 SymI_HasProto(stg_myThreadIdzh) \
821 SymI_HasProto(stg_labelThreadzh) \
822 SymI_HasProto(stg_newArrayzh) \
823 SymI_HasProto(stg_newBCOzh) \
824 SymI_HasProto(stg_newByteArrayzh) \
825 SymI_HasProto_redirect(newCAF, newDynCAF) \
826 SymI_HasProto(stg_newMVarzh) \
827 SymI_HasProto(stg_newMutVarzh) \
828 SymI_HasProto(stg_newTVarzh) \
829 SymI_HasProto(stg_noDuplicatezh) \
830 SymI_HasProto(stg_atomicModifyMutVarzh) \
831 SymI_HasProto(stg_newPinnedByteArrayzh) \
832 SymI_HasProto(stg_newAlignedPinnedByteArrayzh) \
833 SymI_HasProto(newSpark) \
834 SymI_HasProto(performGC) \
835 SymI_HasProto(performMajorGC) \
836 SymI_HasProto(prog_argc) \
837 SymI_HasProto(prog_argv) \
838 SymI_HasProto(stg_putMVarzh) \
839 SymI_HasProto(stg_raisezh) \
840 SymI_HasProto(stg_raiseIOzh) \
841 SymI_HasProto(stg_readTVarzh) \
842 SymI_HasProto(stg_readTVarIOzh) \
843 SymI_HasProto(resumeThread) \
844 SymI_HasProto(resolveObjs) \
845 SymI_HasProto(stg_retryzh) \
846 SymI_HasProto(rts_apply) \
847 SymI_HasProto(rts_checkSchedStatus) \
848 SymI_HasProto(rts_eval) \
849 SymI_HasProto(rts_evalIO) \
850 SymI_HasProto(rts_evalLazyIO) \
851 SymI_HasProto(rts_evalStableIO) \
852 SymI_HasProto(rts_eval_) \
853 SymI_HasProto(rts_getBool) \
854 SymI_HasProto(rts_getChar) \
855 SymI_HasProto(rts_getDouble) \
856 SymI_HasProto(rts_getFloat) \
857 SymI_HasProto(rts_getInt) \
858 SymI_HasProto(rts_getInt8) \
859 SymI_HasProto(rts_getInt16) \
860 SymI_HasProto(rts_getInt32) \
861 SymI_HasProto(rts_getInt64) \
862 SymI_HasProto(rts_getPtr) \
863 SymI_HasProto(rts_getFunPtr) \
864 SymI_HasProto(rts_getStablePtr) \
865 SymI_HasProto(rts_getThreadId) \
866 SymI_HasProto(rts_getWord) \
867 SymI_HasProto(rts_getWord8) \
868 SymI_HasProto(rts_getWord16) \
869 SymI_HasProto(rts_getWord32) \
870 SymI_HasProto(rts_getWord64) \
871 SymI_HasProto(rts_lock) \
872 SymI_HasProto(rts_mkBool) \
873 SymI_HasProto(rts_mkChar) \
874 SymI_HasProto(rts_mkDouble) \
875 SymI_HasProto(rts_mkFloat) \
876 SymI_HasProto(rts_mkInt) \
877 SymI_HasProto(rts_mkInt8) \
878 SymI_HasProto(rts_mkInt16) \
879 SymI_HasProto(rts_mkInt32) \
880 SymI_HasProto(rts_mkInt64) \
881 SymI_HasProto(rts_mkPtr) \
882 SymI_HasProto(rts_mkFunPtr) \
883 SymI_HasProto(rts_mkStablePtr) \
884 SymI_HasProto(rts_mkString) \
885 SymI_HasProto(rts_mkWord) \
886 SymI_HasProto(rts_mkWord8) \
887 SymI_HasProto(rts_mkWord16) \
888 SymI_HasProto(rts_mkWord32) \
889 SymI_HasProto(rts_mkWord64) \
890 SymI_HasProto(rts_unlock) \
891 SymI_HasProto(rts_unsafeGetMyCapability) \
892 SymI_HasProto(rtsSupportsBoundThreads) \
893 SymI_HasProto(rts_isProfiled) \
894 SymI_HasProto(setProgArgv) \
895 SymI_HasProto(startupHaskell) \
896 SymI_HasProto(shutdownHaskell) \
897 SymI_HasProto(shutdownHaskellAndExit) \
898 SymI_HasProto(stable_ptr_table) \
899 SymI_HasProto(stackOverflow) \
900 SymI_HasProto(stg_CAF_BLACKHOLE_info) \
901 SymI_HasProto(stg_BLACKHOLE_info) \
902 SymI_HasProto(__stg_EAGER_BLACKHOLE_info) \
903 SymI_HasProto(stg_BLOCKING_QUEUE_CLEAN_info) \
904 SymI_HasProto(stg_BLOCKING_QUEUE_DIRTY_info) \
905 SymI_HasProto(startTimer) \
906 SymI_HasProto(stg_MVAR_CLEAN_info) \
907 SymI_HasProto(stg_MVAR_DIRTY_info) \
908 SymI_HasProto(stg_IND_STATIC_info) \
909 SymI_HasProto(stg_ARR_WORDS_info) \
910 SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info) \
911 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info) \
912 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info) \
913 SymI_HasProto(stg_WEAK_info) \
914 SymI_HasProto(stg_ap_v_info) \
915 SymI_HasProto(stg_ap_f_info) \
916 SymI_HasProto(stg_ap_d_info) \
917 SymI_HasProto(stg_ap_l_info) \
918 SymI_HasProto(stg_ap_n_info) \
919 SymI_HasProto(stg_ap_p_info) \
920 SymI_HasProto(stg_ap_pv_info) \
921 SymI_HasProto(stg_ap_pp_info) \
922 SymI_HasProto(stg_ap_ppv_info) \
923 SymI_HasProto(stg_ap_ppp_info) \
924 SymI_HasProto(stg_ap_pppv_info) \
925 SymI_HasProto(stg_ap_pppp_info) \
926 SymI_HasProto(stg_ap_ppppp_info) \
927 SymI_HasProto(stg_ap_pppppp_info) \
928 SymI_HasProto(stg_ap_0_fast) \
929 SymI_HasProto(stg_ap_v_fast) \
930 SymI_HasProto(stg_ap_f_fast) \
931 SymI_HasProto(stg_ap_d_fast) \
932 SymI_HasProto(stg_ap_l_fast) \
933 SymI_HasProto(stg_ap_n_fast) \
934 SymI_HasProto(stg_ap_p_fast) \
935 SymI_HasProto(stg_ap_pv_fast) \
936 SymI_HasProto(stg_ap_pp_fast) \
937 SymI_HasProto(stg_ap_ppv_fast) \
938 SymI_HasProto(stg_ap_ppp_fast) \
939 SymI_HasProto(stg_ap_pppv_fast) \
940 SymI_HasProto(stg_ap_pppp_fast) \
941 SymI_HasProto(stg_ap_ppppp_fast) \
942 SymI_HasProto(stg_ap_pppppp_fast) \
943 SymI_HasProto(stg_ap_1_upd_info) \
944 SymI_HasProto(stg_ap_2_upd_info) \
945 SymI_HasProto(stg_ap_3_upd_info) \
946 SymI_HasProto(stg_ap_4_upd_info) \
947 SymI_HasProto(stg_ap_5_upd_info) \
948 SymI_HasProto(stg_ap_6_upd_info) \
949 SymI_HasProto(stg_ap_7_upd_info) \
950 SymI_HasProto(stg_exit) \
951 SymI_HasProto(stg_sel_0_upd_info) \
952 SymI_HasProto(stg_sel_10_upd_info) \
953 SymI_HasProto(stg_sel_11_upd_info) \
954 SymI_HasProto(stg_sel_12_upd_info) \
955 SymI_HasProto(stg_sel_13_upd_info) \
956 SymI_HasProto(stg_sel_14_upd_info) \
957 SymI_HasProto(stg_sel_15_upd_info) \
958 SymI_HasProto(stg_sel_1_upd_info) \
959 SymI_HasProto(stg_sel_2_upd_info) \
960 SymI_HasProto(stg_sel_3_upd_info) \
961 SymI_HasProto(stg_sel_4_upd_info) \
962 SymI_HasProto(stg_sel_5_upd_info) \
963 SymI_HasProto(stg_sel_6_upd_info) \
964 SymI_HasProto(stg_sel_7_upd_info) \
965 SymI_HasProto(stg_sel_8_upd_info) \
966 SymI_HasProto(stg_sel_9_upd_info) \
967 SymI_HasProto(stg_upd_frame_info) \
968 SymI_HasProto(stg_bh_upd_frame_info) \
969 SymI_HasProto(suspendThread) \
970 SymI_HasProto(stg_takeMVarzh) \
971 SymI_HasProto(stg_threadStatuszh) \
972 SymI_HasProto(stg_tryPutMVarzh) \
973 SymI_HasProto(stg_tryTakeMVarzh) \
974 SymI_HasProto(stg_unmaskAsyncExceptionszh) \
975 SymI_HasProto(unloadObj) \
976 SymI_HasProto(stg_unsafeThawArrayzh) \
977 SymI_HasProto(stg_waitReadzh) \
978 SymI_HasProto(stg_waitWritezh) \
979 SymI_HasProto(stg_writeTVarzh) \
980 SymI_HasProto(stg_yieldzh) \
981 SymI_NeedsProto(stg_interp_constr_entry) \
982 SymI_HasProto(stg_arg_bitmaps) \
983 SymI_HasProto(large_alloc_lim) \
985 SymI_HasProto(allocate) \
986 SymI_HasProto(allocateExec) \
987 SymI_HasProto(freeExec) \
988 SymI_HasProto(getAllocations) \
989 SymI_HasProto(revertCAFs) \
990 SymI_HasProto(RtsFlags) \
991 SymI_NeedsProto(rts_breakpoint_io_action) \
992 SymI_NeedsProto(rts_stop_next_breakpoint) \
993 SymI_NeedsProto(rts_stop_on_exception) \
994 SymI_HasProto(stopTimer) \
995 SymI_HasProto(n_capabilities) \
996 SymI_HasProto(stg_traceCcszh) \
997 SymI_HasProto(stg_traceEventzh) \
998 RTS_USER_SIGNALS_SYMBOLS \
1002 // 64-bit support functions in libgcc.a
1003 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
1004 #define RTS_LIBGCC_SYMBOLS \
1005 SymI_NeedsProto(__divdi3) \
1006 SymI_NeedsProto(__udivdi3) \
1007 SymI_NeedsProto(__moddi3) \
1008 SymI_NeedsProto(__umoddi3) \
1009 SymI_NeedsProto(__muldi3) \
1010 SymI_NeedsProto(__ashldi3) \
1011 SymI_NeedsProto(__ashrdi3) \
1012 SymI_NeedsProto(__lshrdi3)
1014 #define RTS_LIBGCC_SYMBOLS
1017 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
1018 // Symbols that don't have a leading underscore
1019 // on Mac OS X. They have to receive special treatment,
1020 // see machoInitSymbolsWithoutUnderscore()
1021 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
1022 SymI_NeedsProto(saveFP) \
1023 SymI_NeedsProto(restFP)
1026 /* entirely bogus claims about types of these symbols */
1027 #define SymI_NeedsProto(vvv) extern void vvv(void);
1028 #if defined(__PIC__) && defined(mingw32_HOST_OS)
1029 #define SymE_HasProto(vvv) SymE_HasProto(vvv);
1030 #define SymE_NeedsProto(vvv) extern void _imp__ ## vvv (void);
1032 #define SymE_NeedsProto(vvv) SymI_NeedsProto(vvv);
1033 #define SymE_HasProto(vvv) SymI_HasProto(vvv)
1035 #define SymI_HasProto(vvv) /**/
1036 #define SymI_HasProto_redirect(vvv,xxx) /**/
1039 RTS_POSIX_ONLY_SYMBOLS
1040 RTS_MINGW_ONLY_SYMBOLS
1041 RTS_CYGWIN_ONLY_SYMBOLS
1042 RTS_DARWIN_ONLY_SYMBOLS
1045 #undef SymI_NeedsProto
1046 #undef SymI_HasProto
1047 #undef SymI_HasProto_redirect
1048 #undef SymE_HasProto
1049 #undef SymE_NeedsProto
1051 #ifdef LEADING_UNDERSCORE
1052 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
1054 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
1057 #define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1059 #define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1060 (void*)DLL_IMPORT_DATA_REF(vvv) },
1062 #define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
1063 #define SymE_NeedsProto(vvv) SymE_HasProto(vvv)
1065 // SymI_HasProto_redirect allows us to redirect references to one symbol to
1066 // another symbol. See newCAF/newDynCAF for an example.
1067 #define SymI_HasProto_redirect(vvv,xxx) \
1068 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1071 static RtsSymbolVal rtsSyms[] = {
1074 RTS_POSIX_ONLY_SYMBOLS
1075 RTS_MINGW_ONLY_SYMBOLS
1076 RTS_CYGWIN_ONLY_SYMBOLS
1077 RTS_DARWIN_ONLY_SYMBOLS
1080 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
1081 // dyld stub code contains references to this,
1082 // but it should never be called because we treat
1083 // lazy pointers as nonlazy.
1084 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
1086 { 0, 0 } /* sentinel */
1091 /* -----------------------------------------------------------------------------
1092 * Insert symbols into hash tables, checking for duplicates.
1095 static void ghciInsertStrHashTable ( char* obj_name,
1101 if (lookupHashTable(table, (StgWord)key) == NULL)
1103 insertStrHashTable(table, (StgWord)key, data);
1108 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
1110 "whilst processing object file\n"
1112 "This could be caused by:\n"
1113 " * Loading two different object files which export the same symbol\n"
1114 " * Specifying the same object file twice on the GHCi command line\n"
1115 " * An incorrect `package.conf' entry, causing some object to be\n"
1117 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
1124 /* -----------------------------------------------------------------------------
1125 * initialize the object linker
1129 static int linker_init_done = 0 ;
1131 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1132 static void *dl_prog_handle;
1133 static regex_t re_invalid;
1134 static regex_t re_realso;
1136 static Mutex dl_mutex; // mutex to protect dlopen/dlerror critical section
1144 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1148 IF_DEBUG(linker, debugBelch("initLinker: start\n"));
1150 /* Make initLinker idempotent, so we can call it
1151 before evey relevant operation; that means we
1152 don't need to initialise the linker separately */
1153 if (linker_init_done == 1) {
1154 IF_DEBUG(linker, debugBelch("initLinker: idempotent return\n"));
1157 linker_init_done = 1;
1160 #if defined(THREADED_RTS) && (defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO))
1161 initMutex(&dl_mutex);
1163 stablehash = allocStrHashTable();
1164 symhash = allocStrHashTable();
1166 /* populate the symbol table with stuff from the RTS */
1167 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
1168 ghciInsertStrHashTable("(GHCi built-in symbols)",
1169 symhash, sym->lbl, sym->addr);
1170 IF_DEBUG(linker, debugBelch("initLinker: inserting rts symbol %s, %p\n", sym->lbl, sym->addr));
1172 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1173 machoInitSymbolsWithoutUnderscore();
1176 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1177 # if defined(RTLD_DEFAULT)
1178 dl_prog_handle = RTLD_DEFAULT;
1180 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
1181 # endif /* RTLD_DEFAULT */
1183 compileResult = regcomp(&re_invalid,
1184 "(([^ \t()])+\\.so([^ \t:()])*):([ \t])*invalid ELF header",
1186 ASSERT( compileResult == 0 );
1187 compileResult = regcomp(&re_realso,
1188 "GROUP *\\( *(([^ )])+)",
1190 ASSERT( compileResult == 0 );
1193 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1194 if (RtsFlags.MiscFlags.linkerMemBase != 0) {
1195 // User-override for mmap_32bit_base
1196 mmap_32bit_base = (void*)RtsFlags.MiscFlags.linkerMemBase;
1200 #if defined(mingw32_HOST_OS)
1202 * These two libraries cause problems when added to the static link,
1203 * but are necessary for resolving symbols in GHCi, hence we load
1204 * them manually here.
1210 IF_DEBUG(linker, debugBelch("initLinker: done\n"));
1215 exitLinker( void ) {
1216 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1217 if (linker_init_done == 1) {
1218 regfree(&re_invalid);
1219 regfree(&re_realso);
1221 closeMutex(&dl_mutex);
1227 /* -----------------------------------------------------------------------------
1228 * Loading DLL or .so dynamic libraries
1229 * -----------------------------------------------------------------------------
1231 * Add a DLL from which symbols may be found. In the ELF case, just
1232 * do RTLD_GLOBAL-style add, so no further messing around needs to
1233 * happen in order that symbols in the loaded .so are findable --
1234 * lookupSymbol() will subsequently see them by dlsym on the program's
1235 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
1237 * In the PEi386 case, open the DLLs and put handles to them in a
1238 * linked list. When looking for a symbol, try all handles in the
1239 * list. This means that we need to load even DLLs that are guaranteed
1240 * to be in the ghc.exe image already, just so we can get a handle
1241 * to give to loadSymbol, so that we can find the symbols. For such
1242 * libraries, the LoadLibrary call should be a no-op except for returning
1247 #if defined(OBJFORMAT_PEi386)
1248 /* A record for storing handles into DLLs. */
1253 struct _OpenedDLL* next;
1258 /* A list thereof. */
1259 static OpenedDLL* opened_dlls = NULL;
1262 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1265 internal_dlopen(const char *dll_name)
1271 // omitted: RTLD_NOW
1272 // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
1274 debugBelch("internal_dlopen: dll_name = '%s'\n", dll_name));
1276 //-------------- Begin critical section ------------------
1277 // This critical section is necessary because dlerror() is not
1278 // required to be reentrant (see POSIX -- IEEE Std 1003.1-2008)
1279 // Also, the error message returned must be copied to preserve it
1282 ACQUIRE_LOCK(&dl_mutex);
1283 hdl = dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
1287 /* dlopen failed; return a ptr to the error msg. */
1289 if (errmsg == NULL) errmsg = "addDLL: unknown error";
1290 errmsg_copy = stgMallocBytes(strlen(errmsg)+1, "addDLL");
1291 strcpy(errmsg_copy, errmsg);
1292 errmsg = errmsg_copy;
1294 RELEASE_LOCK(&dl_mutex);
1295 //--------------- End critical section -------------------
1302 addDLL( char *dll_name )
1304 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1305 /* ------------------- ELF DLL loader ------------------- */
1308 regmatch_t match[NMATCH];
1311 size_t match_length;
1312 #define MAXLINE 1000
1318 IF_DEBUG(linker, debugBelch("addDLL: dll_name = '%s'\n", dll_name));
1319 errmsg = internal_dlopen(dll_name);
1321 if (errmsg == NULL) {
1325 // GHC Trac ticket #2615
1326 // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so)
1327 // contain linker scripts rather than ELF-format object code. This
1328 // code handles the situation by recognizing the real object code
1329 // file name given in the linker script.
1331 // If an "invalid ELF header" error occurs, it is assumed that the
1332 // .so file contains a linker script instead of ELF object code.
1333 // In this case, the code looks for the GROUP ( ... ) linker
1334 // directive. If one is found, the first file name inside the
1335 // parentheses is treated as the name of a dynamic library and the
1336 // code attempts to dlopen that file. If this is also unsuccessful,
1337 // an error message is returned.
1339 // see if the error message is due to an invalid ELF header
1340 IF_DEBUG(linker, debugBelch("errmsg = '%s'\n", errmsg));
1341 result = regexec(&re_invalid, errmsg, (size_t) NMATCH, match, 0);
1342 IF_DEBUG(linker, debugBelch("result = %i\n", result));
1344 // success -- try to read the named file as a linker script
1345 match_length = (size_t) stg_min((match[1].rm_eo - match[1].rm_so),
1347 strncpy(line, (errmsg+(match[1].rm_so)),match_length);
1348 line[match_length] = '\0'; // make sure string is null-terminated
1349 IF_DEBUG(linker, debugBelch ("file name = '%s'\n", line));
1350 if ((fp = fopen(line, "r")) == NULL) {
1351 return errmsg; // return original error if open fails
1353 // try to find a GROUP ( ... ) command
1354 while (fgets(line, MAXLINE, fp) != NULL) {
1355 IF_DEBUG(linker, debugBelch("input line = %s", line));
1356 if (regexec(&re_realso, line, (size_t) NMATCH, match, 0) == 0) {
1357 // success -- try to dlopen the first named file
1358 IF_DEBUG(linker, debugBelch("match%s\n",""));
1359 line[match[1].rm_eo] = '\0';
1360 errmsg = internal_dlopen(line+match[1].rm_so);
1363 // if control reaches here, no GROUP ( ... ) directive was found
1364 // and the original error message is returned to the caller
1370 # elif defined(OBJFORMAT_PEi386)
1371 /* ------------------- Win32 DLL loader ------------------- */
1379 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1381 /* See if we've already got it, and ignore if so. */
1382 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1383 if (0 == strcmp(o_dll->name, dll_name))
1387 /* The file name has no suffix (yet) so that we can try
1388 both foo.dll and foo.drv
1390 The documentation for LoadLibrary says:
1391 If no file name extension is specified in the lpFileName
1392 parameter, the default library extension .dll is
1393 appended. However, the file name string can include a trailing
1394 point character (.) to indicate that the module name has no
1397 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1398 sprintf(buf, "%s.DLL", dll_name);
1399 instance = LoadLibrary(buf);
1400 if (instance == NULL) {
1401 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1402 // KAA: allow loading of drivers (like winspool.drv)
1403 sprintf(buf, "%s.DRV", dll_name);
1404 instance = LoadLibrary(buf);
1405 if (instance == NULL) {
1406 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1407 // #1883: allow loading of unix-style libfoo.dll DLLs
1408 sprintf(buf, "lib%s.DLL", dll_name);
1409 instance = LoadLibrary(buf);
1410 if (instance == NULL) {
1417 /* Add this DLL to the list of DLLs in which to search for symbols. */
1418 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1419 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
1420 strcpy(o_dll->name, dll_name);
1421 o_dll->instance = instance;
1422 o_dll->next = opened_dlls;
1423 opened_dlls = o_dll;
1429 sysErrorBelch(dll_name);
1431 /* LoadLibrary failed; return a ptr to the error msg. */
1432 return "addDLL: could not load DLL";
1435 barf("addDLL: not implemented on this platform");
1439 /* -----------------------------------------------------------------------------
1440 * insert a stable symbol in the hash table
1444 insertStableSymbol(char* obj_name, char* key, StgPtr p)
1446 ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
1450 /* -----------------------------------------------------------------------------
1451 * insert a symbol in the hash table
1454 insertSymbol(char* obj_name, char* key, void* data)
1456 ghciInsertStrHashTable(obj_name, symhash, key, data);
1459 /* -----------------------------------------------------------------------------
1460 * lookup a symbol in the hash table
1463 lookupSymbol( char *lbl )
1466 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s\n", lbl));
1468 ASSERT(symhash != NULL);
1469 val = lookupStrHashTable(symhash, lbl);
1472 IF_DEBUG(linker, debugBelch("lookupSymbol: symbol not found\n"));
1473 # if defined(OBJFORMAT_ELF)
1474 return dlsym(dl_prog_handle, lbl);
1475 # elif defined(OBJFORMAT_MACHO)
1477 /* On OS X 10.3 and later, we use dlsym instead of the old legacy
1480 HACK: On OS X, global symbols are prefixed with an underscore.
1481 However, dlsym wants us to omit the leading underscore from the
1482 symbol name. For now, we simply strip it off here (and ONLY
1485 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s with dlsym\n", lbl));
1486 ASSERT(lbl[0] == '_');
1487 return dlsym(dl_prog_handle, lbl+1);
1489 if(NSIsSymbolNameDefined(lbl)) {
1490 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1491 return NSAddressOfSymbol(symbol);
1495 # endif /* HAVE_DLFCN_H */
1496 # elif defined(OBJFORMAT_PEi386)
1499 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1500 if (sym != NULL) { return sym; };
1502 // Also try looking up the symbol without the @N suffix. Some
1503 // DLLs have the suffixes on their symbols, some don't.
1504 zapTrailingAtSign ( (unsigned char*)lbl );
1505 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1506 if (sym != NULL) { return sym; };
1514 IF_DEBUG(linker, debugBelch("lookupSymbol: value of %s is %p\n", lbl, val));
1519 /* -----------------------------------------------------------------------------
1520 * Debugging aid: look in GHCi's object symbol tables for symbols
1521 * within DELTA bytes of the specified address, and show their names.
1524 void ghci_enquire ( char* addr );
1526 void ghci_enquire ( char* addr )
1531 const int DELTA = 64;
1536 for (oc = objects; oc; oc = oc->next) {
1537 for (i = 0; i < oc->n_symbols; i++) {
1538 sym = oc->symbols[i];
1539 if (sym == NULL) continue;
1542 a = lookupStrHashTable(symhash, sym);
1545 // debugBelch("ghci_enquire: can't find %s\n", sym);
1547 else if (addr-DELTA <= a && a <= addr+DELTA) {
1548 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1556 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1559 mmapForLinker (size_t bytes, nat flags, int fd)
1561 void *map_addr = NULL;
1564 static nat fixed = 0;
1566 pagesize = getpagesize();
1567 size = ROUND_UP(bytes, pagesize);
1569 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1572 if (mmap_32bit_base != 0) {
1573 map_addr = mmap_32bit_base;
1577 result = mmap(map_addr, size, PROT_EXEC|PROT_READ|PROT_WRITE,
1578 MAP_PRIVATE|TRY_MAP_32BIT|fixed|flags, fd, 0);
1580 if (result == MAP_FAILED) {
1581 sysErrorBelch("mmap %lu bytes at %p",(lnat)size,map_addr);
1582 errorBelch("Try specifying an address with +RTS -xm<addr> -RTS");
1583 stg_exit(EXIT_FAILURE);
1586 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1587 if (mmap_32bit_base != 0) {
1588 if (result == map_addr) {
1589 mmap_32bit_base = (StgWord8*)map_addr + size;
1591 if ((W_)result > 0x80000000) {
1592 // oops, we were given memory over 2Gb
1593 #if defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS)
1594 // Some platforms require MAP_FIXED. This is normally
1595 // a bad idea, because MAP_FIXED will overwrite
1596 // existing mappings.
1597 munmap(result,size);
1601 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);
1604 // hmm, we were given memory somewhere else, but it's
1605 // still under 2Gb so we can use it. Next time, ask
1606 // for memory right after the place we just got some
1607 mmap_32bit_base = (StgWord8*)result + size;
1611 if ((W_)result > 0x80000000) {
1612 // oops, we were given memory over 2Gb
1613 // ... try allocating memory somewhere else?;
1614 debugTrace(DEBUG_linker,"MAP_32BIT didn't work; gave us %lu bytes at 0x%p", bytes, result);
1615 munmap(result, size);
1617 // Set a base address and try again... (guess: 1Gb)
1618 mmap_32bit_base = (void*)0x40000000;
1629 mkOc( char *path, char *image, int imageSize,
1630 char *archiveMemberName
1632 #ifdef darwin_HOST_OS
1639 oc = stgMallocBytes(sizeof(ObjectCode), "loadArchive(oc)");
1641 # if defined(OBJFORMAT_ELF)
1642 oc->formatName = "ELF";
1643 # elif defined(OBJFORMAT_PEi386)
1644 oc->formatName = "PEi386";
1645 # elif defined(OBJFORMAT_MACHO)
1646 oc->formatName = "Mach-O";
1649 barf("loadObj: not implemented on this platform");
1653 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1654 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1655 strcpy(oc->fileName, path);
1657 if (archiveMemberName) {
1658 oc->archiveMemberName = stgMallocBytes( strlen(archiveMemberName)+1, "loadObj" );
1659 strcpy(oc->archiveMemberName, archiveMemberName);
1662 oc->archiveMemberName = NULL;
1665 oc->fileSize = imageSize;
1667 oc->sections = NULL;
1668 oc->proddables = NULL;
1671 #ifdef darwin_HOST_OS
1672 oc->misalignment = misalignment;
1676 /* chain it onto the list of objects */
1684 loadArchive( char *path )
1691 size_t thisFileNameSize;
1693 size_t fileNameSize;
1694 int isObject, isGnuIndex;
1697 int gnuFileIndexSize;
1698 #if !defined(USE_MMAP) && defined(darwin_HOST_OS)
1702 IF_DEBUG(linker, debugBelch("loadArchive: Loading archive `%s'\n", path));
1704 gnuFileIndex = NULL;
1705 gnuFileIndexSize = 0;
1708 fileName = stgMallocBytes(fileNameSize, "loadArchive(fileName)");
1710 f = fopen(path, "rb");
1712 barf("loadObj: can't read `%s'", path);
1714 n = fread ( tmp, 1, 8, f );
1715 if (strncmp(tmp, "!<arch>\n", 8) != 0)
1716 barf("loadArchive: Not an archive: `%s'", path);
1719 n = fread ( fileName, 1, 16, f );
1725 barf("loadArchive: Failed reading file name from `%s'", path);
1728 n = fread ( tmp, 1, 12, f );
1730 barf("loadArchive: Failed reading mod time from `%s'", path);
1731 n = fread ( tmp, 1, 6, f );
1733 barf("loadArchive: Failed reading owner from `%s'", path);
1734 n = fread ( tmp, 1, 6, f );
1736 barf("loadArchive: Failed reading group from `%s'", path);
1737 n = fread ( tmp, 1, 8, f );
1739 barf("loadArchive: Failed reading mode from `%s'", path);
1740 n = fread ( tmp, 1, 10, f );
1742 barf("loadArchive: Failed reading size from `%s'", path);
1744 for (n = 0; isdigit(tmp[n]); n++);
1746 memberSize = atoi(tmp);
1747 n = fread ( tmp, 1, 2, f );
1748 if (strncmp(tmp, "\x60\x0A", 2) != 0)
1749 barf("loadArchive: Failed reading magic from `%s' at %ld. Got %c%c",
1750 path, ftell(f), tmp[0], tmp[1]);
1753 /* Check for BSD-variant large filenames */
1754 if (0 == strncmp(fileName, "#1/", 3)) {
1755 fileName[16] = '\0';
1756 if (isdigit(fileName[3])) {
1757 for (n = 4; isdigit(fileName[n]); n++);
1759 thisFileNameSize = atoi(fileName + 3);
1760 memberSize -= thisFileNameSize;
1761 if (thisFileNameSize >= fileNameSize) {
1762 /* Double it to avoid potentially continually
1763 increasing it by 1 */
1764 fileNameSize = thisFileNameSize * 2;
1765 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1767 n = fread ( fileName, 1, thisFileNameSize, f );
1768 if (n != (int)thisFileNameSize) {
1769 barf("loadArchive: Failed reading filename from `%s'",
1772 fileName[thisFileNameSize] = 0;
1775 barf("loadArchive: BSD-variant filename size not found while reading filename from `%s'", path);
1778 /* Check for GNU file index file */
1779 else if (0 == strncmp(fileName, "//", 2)) {
1781 thisFileNameSize = 0;
1784 /* Check for a file in the GNU file index */
1785 else if (fileName[0] == '/') {
1786 if (isdigit(fileName[1])) {
1789 for (n = 2; isdigit(fileName[n]); n++);
1791 n = atoi(fileName + 1);
1793 if (gnuFileIndex == NULL) {
1794 barf("loadArchive: GNU-variant filename without an index while reading from `%s'", path);
1796 if (n < 0 || n > gnuFileIndexSize) {
1797 barf("loadArchive: GNU-variant filename offset %d out of range [0..%d] while reading filename from `%s'", n, gnuFileIndexSize, path);
1799 if (n != 0 && gnuFileIndex[n - 1] != '\n') {
1800 barf("loadArchive: GNU-variant filename offset %d invalid (range [0..%d]) while reading filename from `%s'", n, gnuFileIndexSize, path);
1802 for (i = n; gnuFileIndex[i] != '/'; i++);
1803 thisFileNameSize = i - n;
1804 if (thisFileNameSize >= fileNameSize) {
1805 /* Double it to avoid potentially continually
1806 increasing it by 1 */
1807 fileNameSize = thisFileNameSize * 2;
1808 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1810 memcpy(fileName, gnuFileIndex + n, thisFileNameSize);
1811 fileName[thisFileNameSize] = '\0';
1813 else if (fileName[1] == ' ') {
1815 thisFileNameSize = 0;
1818 barf("loadArchive: GNU-variant filename offset not found while reading filename from `%s'", path);
1821 /* Finally, the case where the filename field actually contains
1824 /* GNU ar terminates filenames with a '/', this allowing
1825 spaces in filenames. So first look to see if there is a
1827 for (thisFileNameSize = 0;
1828 thisFileNameSize < 16;
1829 thisFileNameSize++) {
1830 if (fileName[thisFileNameSize] == '/') {
1831 fileName[thisFileNameSize] = '\0';
1835 /* If we didn't find a '/', then a space teminates the
1836 filename. Note that if we don't find one, then
1837 thisFileNameSize ends up as 16, and we already have the
1839 if (thisFileNameSize == 16) {
1840 for (thisFileNameSize = 0;
1841 thisFileNameSize < 16;
1842 thisFileNameSize++) {
1843 if (fileName[thisFileNameSize] == ' ') {
1844 fileName[thisFileNameSize] = '\0';
1852 debugBelch("loadArchive: Found member file `%s'\n", fileName));
1854 isObject = thisFileNameSize >= 2
1855 && fileName[thisFileNameSize - 2] == '.'
1856 && fileName[thisFileNameSize - 1] == 'o';
1859 char *archiveMemberName;
1861 IF_DEBUG(linker, debugBelch("loadArchive: Member is an object file...loading...\n"));
1863 /* We can't mmap from the archive directly, as object
1864 files need to be 8-byte aligned but files in .ar
1865 archives are 2-byte aligned. When possible we use mmap
1866 to get some anonymous memory, as on 64-bit platforms if
1867 we use malloc then we can be given memory above 2^32.
1868 In the mmap case we're probably wasting lots of space;
1869 we could do better. */
1870 #if defined(USE_MMAP)
1871 image = mmapForLinker(memberSize, MAP_ANONYMOUS, -1);
1872 #elif defined(darwin_HOST_OS)
1874 misalignment = machoGetMisalignment(f);
1875 image = stgMallocBytes(memberSize + misalignment, "loadArchive(image)");
1876 image += misalignment;
1878 image = stgMallocBytes(memberSize, "loadArchive(image)");
1880 n = fread ( image, 1, memberSize, f );
1881 if (n != memberSize) {
1882 barf("loadArchive: error whilst reading `%s'", path);
1885 archiveMemberName = stgMallocBytes(strlen(path) + thisFileNameSize + 3,
1886 "loadArchive(file)");
1887 sprintf(archiveMemberName, "%s(%.*s)",
1888 path, (int)thisFileNameSize, fileName);
1890 oc = mkOc(path, image, memberSize, archiveMemberName
1892 #ifdef darwin_HOST_OS
1898 stgFree(archiveMemberName);
1900 if (0 == loadOc(oc)) {
1905 else if (isGnuIndex) {
1906 if (gnuFileIndex != NULL) {
1907 barf("loadArchive: GNU-variant index found, but already have an index, while reading filename from `%s'", path);
1909 IF_DEBUG(linker, debugBelch("loadArchive: Found GNU-variant file index\n"));
1911 gnuFileIndex = mmapForLinker(memberSize + 1, MAP_ANONYMOUS, -1);
1913 gnuFileIndex = stgMallocBytes(memberSize + 1, "loadArchive(image)");
1915 n = fread ( gnuFileIndex, 1, memberSize, f );
1916 if (n != memberSize) {
1917 barf("loadArchive: error whilst reading `%s'", path);
1919 gnuFileIndex[memberSize] = '/';
1920 gnuFileIndexSize = memberSize;
1923 n = fseek(f, memberSize, SEEK_CUR);
1925 barf("loadArchive: error whilst seeking by %d in `%s'",
1928 /* .ar files are 2-byte aligned */
1929 if (memberSize % 2) {
1930 n = fread ( tmp, 1, 1, f );
1936 barf("loadArchive: Failed reading padding from `%s'", path);
1945 if (gnuFileIndex != NULL) {
1947 munmap(gnuFileIndex, gnuFileIndexSize + 1);
1949 stgFree(gnuFileIndex);
1956 /* -----------------------------------------------------------------------------
1957 * Load an obj (populate the global symbol table, but don't resolve yet)
1959 * Returns: 1 if ok, 0 on error.
1962 loadObj( char *path )
1973 # if defined(darwin_HOST_OS)
1977 IF_DEBUG(linker, debugBelch("loadObj %s\n", path));
1981 /* debugBelch("loadObj %s\n", path ); */
1983 /* Check that we haven't already loaded this object.
1984 Ignore requests to load multiple times */
1988 for (o = objects; o; o = o->next) {
1989 if (0 == strcmp(o->fileName, path)) {
1991 break; /* don't need to search further */
1995 IF_DEBUG(linker, debugBelch(
1996 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1997 "same object file twice:\n"
1999 "GHCi will ignore this, but be warned.\n"
2001 return 1; /* success */
2005 r = stat(path, &st);
2007 IF_DEBUG(linker, debugBelch("File doesn't exist\n"));
2011 fileSize = st.st_size;
2014 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
2016 #if defined(openbsd_HOST_OS)
2017 fd = open(path, O_RDONLY, S_IRUSR);
2019 fd = open(path, O_RDONLY);
2022 barf("loadObj: can't open `%s'", path);
2024 image = mmapForLinker(fileSize, 0, fd);
2028 #else /* !USE_MMAP */
2029 /* load the image into memory */
2030 f = fopen(path, "rb");
2032 barf("loadObj: can't read `%s'", path);
2034 # if defined(mingw32_HOST_OS)
2035 // TODO: We would like to use allocateExec here, but allocateExec
2036 // cannot currently allocate blocks large enough.
2037 image = VirtualAlloc(NULL, fileSize, MEM_RESERVE | MEM_COMMIT,
2038 PAGE_EXECUTE_READWRITE);
2039 # elif defined(darwin_HOST_OS)
2040 // In a Mach-O .o file, all sections can and will be misaligned
2041 // if the total size of the headers is not a multiple of the
2042 // desired alignment. This is fine for .o files that only serve
2043 // as input for the static linker, but it's not fine for us,
2044 // as SSE (used by gcc for floating point) and Altivec require
2045 // 16-byte alignment.
2046 // We calculate the correct alignment from the header before
2047 // reading the file, and then we misalign image on purpose so
2048 // that the actual sections end up aligned again.
2049 misalignment = machoGetMisalignment(f);
2050 image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
2051 image += misalignment;
2053 image = stgMallocBytes(fileSize, "loadObj(image)");
2058 n = fread ( image, 1, fileSize, f );
2060 barf("loadObj: error whilst reading `%s'", path);
2063 #endif /* USE_MMAP */
2065 oc = mkOc(path, image, fileSize, NULL
2067 #ifdef darwin_HOST_OS
2077 loadOc( ObjectCode* oc ) {
2080 IF_DEBUG(linker, debugBelch("loadOc\n"));
2082 # if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2083 r = ocAllocateSymbolExtras_MachO ( oc );
2085 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO failed\n"));
2088 # elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2089 r = ocAllocateSymbolExtras_ELF ( oc );
2091 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_ELF failed\n"));
2096 /* verify the in-memory image */
2097 # if defined(OBJFORMAT_ELF)
2098 r = ocVerifyImage_ELF ( oc );
2099 # elif defined(OBJFORMAT_PEi386)
2100 r = ocVerifyImage_PEi386 ( oc );
2101 # elif defined(OBJFORMAT_MACHO)
2102 r = ocVerifyImage_MachO ( oc );
2104 barf("loadObj: no verify method");
2107 IF_DEBUG(linker, debugBelch("ocVerifyImage_* failed\n"));
2111 /* build the symbol list for this image */
2112 # if defined(OBJFORMAT_ELF)
2113 r = ocGetNames_ELF ( oc );
2114 # elif defined(OBJFORMAT_PEi386)
2115 r = ocGetNames_PEi386 ( oc );
2116 # elif defined(OBJFORMAT_MACHO)
2117 r = ocGetNames_MachO ( oc );
2119 barf("loadObj: no getNames method");
2122 IF_DEBUG(linker, debugBelch("ocGetNames_* failed\n"));
2126 /* loaded, but not resolved yet */
2127 oc->status = OBJECT_LOADED;
2128 IF_DEBUG(linker, debugBelch("loadObj done.\n"));
2133 /* -----------------------------------------------------------------------------
2134 * resolve all the currently unlinked objects in memory
2136 * Returns: 1 if ok, 0 on error.
2144 IF_DEBUG(linker, debugBelch("resolveObjs: start\n"));
2147 for (oc = objects; oc; oc = oc->next) {
2148 if (oc->status != OBJECT_RESOLVED) {
2149 # if defined(OBJFORMAT_ELF)
2150 r = ocResolve_ELF ( oc );
2151 # elif defined(OBJFORMAT_PEi386)
2152 r = ocResolve_PEi386 ( oc );
2153 # elif defined(OBJFORMAT_MACHO)
2154 r = ocResolve_MachO ( oc );
2156 barf("resolveObjs: not implemented on this platform");
2158 if (!r) { return r; }
2159 oc->status = OBJECT_RESOLVED;
2162 IF_DEBUG(linker, debugBelch("resolveObjs: done\n"));
2166 /* -----------------------------------------------------------------------------
2167 * delete an object from the pool
2170 unloadObj( char *path )
2172 ObjectCode *oc, *prev;
2173 HsBool unloadedAnyObj = HS_BOOL_FALSE;
2175 ASSERT(symhash != NULL);
2176 ASSERT(objects != NULL);
2181 for (oc = objects; oc; prev = oc, oc = oc->next) {
2182 if (!strcmp(oc->fileName,path)) {
2184 /* Remove all the mappings for the symbols within this
2189 for (i = 0; i < oc->n_symbols; i++) {
2190 if (oc->symbols[i] != NULL) {
2191 removeStrHashTable(symhash, oc->symbols[i], NULL);
2199 prev->next = oc->next;
2202 // We're going to leave this in place, in case there are
2203 // any pointers from the heap into it:
2204 // #ifdef mingw32_HOST_OS
2205 // VirtualFree(oc->image);
2207 // stgFree(oc->image);
2209 stgFree(oc->fileName);
2210 stgFree(oc->symbols);
2211 stgFree(oc->sections);
2214 /* This could be a member of an archive so continue
2215 * unloading other members. */
2216 unloadedAnyObj = HS_BOOL_TRUE;
2220 if (unloadedAnyObj) {
2224 errorBelch("unloadObj: can't find `%s' to unload", path);
2229 /* -----------------------------------------------------------------------------
2230 * Sanity checking. For each ObjectCode, maintain a list of address ranges
2231 * which may be prodded during relocation, and abort if we try and write
2232 * outside any of these.
2234 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
2237 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
2238 IF_DEBUG(linker, debugBelch("addProddableBlock %p %p %d\n", oc, start, size));
2242 pb->next = oc->proddables;
2243 oc->proddables = pb;
2246 static void checkProddableBlock ( ObjectCode* oc, void* addr )
2249 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
2250 char* s = (char*)(pb->start);
2251 char* e = s + pb->size - 1;
2252 char* a = (char*)addr;
2253 /* Assumes that the biggest fixup involves a 4-byte write. This
2254 probably needs to be changed to 8 (ie, +7) on 64-bit
2256 if (a >= s && (a+3) <= e) return;
2258 barf("checkProddableBlock: invalid fixup in runtime linker");
2261 /* -----------------------------------------------------------------------------
2262 * Section management.
2264 static void addSection ( ObjectCode* oc, SectionKind kind,
2265 void* start, void* end )
2267 Section* s = stgMallocBytes(sizeof(Section), "addSection");
2271 s->next = oc->sections;
2274 debugBelch("addSection: %p-%p (size %d), kind %d\n",
2275 start, ((char*)end)-1, end - start + 1, kind );
2280 /* --------------------------------------------------------------------------
2282 * This is about allocating a small chunk of memory for every symbol in the
2283 * object file. We make sure that the SymboLExtras are always "in range" of
2284 * limited-range PC-relative instructions on various platforms by allocating
2285 * them right next to the object code itself.
2288 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
2291 ocAllocateSymbolExtras
2293 Allocate additional space at the end of the object file image to make room
2294 for jump islands (powerpc, x86_64) and GOT entries (x86_64).
2296 PowerPC relative branch instructions have a 24 bit displacement field.
2297 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
2298 If a particular imported symbol is outside this range, we have to redirect
2299 the jump to a short piece of new code that just loads the 32bit absolute
2300 address and jumps there.
2301 On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
2304 This function just allocates space for one SymbolExtra for every
2305 undefined symbol in the object file. The code for the jump islands is
2306 filled in by makeSymbolExtra below.
2309 static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
2316 int misalignment = 0;
2317 #ifdef darwin_HOST_OS
2318 misalignment = oc->misalignment;
2324 // round up to the nearest 4
2325 aligned = (oc->fileSize + 3) & ~3;
2328 pagesize = getpagesize();
2329 n = ROUND_UP( oc->fileSize, pagesize );
2330 m = ROUND_UP( aligned + sizeof (SymbolExtra) * count, pagesize );
2332 /* we try to use spare space at the end of the last page of the
2333 * image for the jump islands, but if there isn't enough space
2334 * then we have to map some (anonymously, remembering MAP_32BIT).
2336 if( m > n ) // we need to allocate more pages
2338 oc->symbol_extras = mmapForLinker(sizeof(SymbolExtra) * count,
2343 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2346 oc->image -= misalignment;
2347 oc->image = stgReallocBytes( oc->image,
2349 aligned + sizeof (SymbolExtra) * count,
2350 "ocAllocateSymbolExtras" );
2351 oc->image += misalignment;
2353 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2354 #endif /* USE_MMAP */
2356 memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
2359 oc->symbol_extras = NULL;
2361 oc->first_symbol_extra = first;
2362 oc->n_symbol_extras = count;
2367 static SymbolExtra* makeSymbolExtra( ObjectCode* oc,
2368 unsigned long symbolNumber,
2369 unsigned long target )
2373 ASSERT( symbolNumber >= oc->first_symbol_extra
2374 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
2376 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
2378 #ifdef powerpc_HOST_ARCH
2379 // lis r12, hi16(target)
2380 extra->jumpIsland.lis_r12 = 0x3d80;
2381 extra->jumpIsland.hi_addr = target >> 16;
2383 // ori r12, r12, lo16(target)
2384 extra->jumpIsland.ori_r12_r12 = 0x618c;
2385 extra->jumpIsland.lo_addr = target & 0xffff;
2388 extra->jumpIsland.mtctr_r12 = 0x7d8903a6;
2391 extra->jumpIsland.bctr = 0x4e800420;
2393 #ifdef x86_64_HOST_ARCH
2395 static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
2396 extra->addr = target;
2397 memcpy(extra->jumpIsland, jmp, 6);
2405 /* --------------------------------------------------------------------------
2406 * PowerPC specifics (instruction cache flushing)
2407 * ------------------------------------------------------------------------*/
2409 #ifdef powerpc_HOST_ARCH
2411 ocFlushInstructionCache
2413 Flush the data & instruction caches.
2414 Because the PPC has split data/instruction caches, we have to
2415 do that whenever we modify code at runtime.
2417 static void ocFlushInstructionCacheFrom(void* begin, size_t length)
2419 size_t n = (length + 3) / 4;
2420 unsigned long* p = begin;
2424 __asm__ volatile ( "dcbf 0,%0\n\t"
2432 __asm__ volatile ( "sync\n\t"
2436 static void ocFlushInstructionCache( ObjectCode *oc )
2438 /* The main object code */
2439 ocFlushInstructionCacheFrom(oc->image + oc->misalignment, oc->fileSize);
2442 ocFlushInstructionCacheFrom(oc->symbol_extras, sizeof(SymbolExtra) * oc->n_symbol_extras);
2446 /* --------------------------------------------------------------------------
2447 * PEi386 specifics (Win32 targets)
2448 * ------------------------------------------------------------------------*/
2450 /* The information for this linker comes from
2451 Microsoft Portable Executable
2452 and Common Object File Format Specification
2453 revision 5.1 January 1998
2454 which SimonM says comes from the MS Developer Network CDs.
2456 It can be found there (on older CDs), but can also be found
2459 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
2461 (this is Rev 6.0 from February 1999).
2463 Things move, so if that fails, try searching for it via
2465 http://www.google.com/search?q=PE+COFF+specification
2467 The ultimate reference for the PE format is the Winnt.h
2468 header file that comes with the Platform SDKs; as always,
2469 implementations will drift wrt their documentation.
2471 A good background article on the PE format is Matt Pietrek's
2472 March 1994 article in Microsoft System Journal (MSJ)
2473 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
2474 Win32 Portable Executable File Format." The info in there
2475 has recently been updated in a two part article in
2476 MSDN magazine, issues Feb and March 2002,
2477 "Inside Windows: An In-Depth Look into the Win32 Portable
2478 Executable File Format"
2480 John Levine's book "Linkers and Loaders" contains useful
2485 #if defined(OBJFORMAT_PEi386)
2489 typedef unsigned char UChar;
2490 typedef unsigned short UInt16;
2491 typedef unsigned int UInt32;
2498 UInt16 NumberOfSections;
2499 UInt32 TimeDateStamp;
2500 UInt32 PointerToSymbolTable;
2501 UInt32 NumberOfSymbols;
2502 UInt16 SizeOfOptionalHeader;
2503 UInt16 Characteristics;
2507 #define sizeof_COFF_header 20
2514 UInt32 VirtualAddress;
2515 UInt32 SizeOfRawData;
2516 UInt32 PointerToRawData;
2517 UInt32 PointerToRelocations;
2518 UInt32 PointerToLinenumbers;
2519 UInt16 NumberOfRelocations;
2520 UInt16 NumberOfLineNumbers;
2521 UInt32 Characteristics;
2525 #define sizeof_COFF_section 40
2532 UInt16 SectionNumber;
2535 UChar NumberOfAuxSymbols;
2539 #define sizeof_COFF_symbol 18
2544 UInt32 VirtualAddress;
2545 UInt32 SymbolTableIndex;
2550 #define sizeof_COFF_reloc 10
2553 /* From PE spec doc, section 3.3.2 */
2554 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
2555 windows.h -- for the same purpose, but I want to know what I'm
2557 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
2558 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
2559 #define MYIMAGE_FILE_DLL 0x2000
2560 #define MYIMAGE_FILE_SYSTEM 0x1000
2561 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
2562 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
2563 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
2565 /* From PE spec doc, section 5.4.2 and 5.4.4 */
2566 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
2567 #define MYIMAGE_SYM_CLASS_STATIC 3
2568 #define MYIMAGE_SYM_UNDEFINED 0
2570 /* From PE spec doc, section 4.1 */
2571 #define MYIMAGE_SCN_CNT_CODE 0x00000020
2572 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
2573 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
2575 /* From PE spec doc, section 5.2.1 */
2576 #define MYIMAGE_REL_I386_DIR32 0x0006
2577 #define MYIMAGE_REL_I386_REL32 0x0014
2580 /* We use myindex to calculate array addresses, rather than
2581 simply doing the normal subscript thing. That's because
2582 some of the above structs have sizes which are not
2583 a whole number of words. GCC rounds their sizes up to a
2584 whole number of words, which means that the address calcs
2585 arising from using normal C indexing or pointer arithmetic
2586 are just plain wrong. Sigh.
2589 myindex ( int scale, void* base, int index )
2592 ((UChar*)base) + scale * index;
2597 printName ( UChar* name, UChar* strtab )
2599 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2600 UInt32 strtab_offset = * (UInt32*)(name+4);
2601 debugBelch("%s", strtab + strtab_offset );
2604 for (i = 0; i < 8; i++) {
2605 if (name[i] == 0) break;
2606 debugBelch("%c", name[i] );
2613 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
2615 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2616 UInt32 strtab_offset = * (UInt32*)(name+4);
2617 strncpy ( (char*)dst, (char*)strtab+strtab_offset, dstSize );
2623 if (name[i] == 0) break;
2633 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
2636 /* If the string is longer than 8 bytes, look in the
2637 string table for it -- this will be correctly zero terminated.
2639 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2640 UInt32 strtab_offset = * (UInt32*)(name+4);
2641 return ((UChar*)strtab) + strtab_offset;
2643 /* Otherwise, if shorter than 8 bytes, return the original,
2644 which by defn is correctly terminated.
2646 if (name[7]==0) return name;
2647 /* The annoying case: 8 bytes. Copy into a temporary
2648 (XXX which is never freed ...)
2650 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
2652 strncpy((char*)newstr,(char*)name,8);
2657 /* Getting the name of a section is mildly tricky, so we make a
2658 function for it. Sadly, in one case we have to copy the string
2659 (when it is exactly 8 bytes long there's no trailing '\0'), so for
2660 consistency we *always* copy the string; the caller must free it
2663 cstring_from_section_name (UChar* name, UChar* strtab)
2668 int strtab_offset = strtol((char*)name+1,NULL,10);
2669 int len = strlen(((char*)strtab) + strtab_offset);
2671 newstr = stgMallocBytes(len, "cstring_from_section_symbol_name");
2672 strcpy((char*)newstr, (char*)((UChar*)strtab) + strtab_offset);
2677 newstr = stgMallocBytes(9, "cstring_from_section_symbol_name");
2679 strncpy((char*)newstr,(char*)name,8);
2685 /* Just compares the short names (first 8 chars) */
2686 static COFF_section *
2687 findPEi386SectionCalled ( ObjectCode* oc, UChar* name )
2691 = (COFF_header*)(oc->image);
2692 COFF_section* sectab
2694 ((UChar*)(oc->image))
2695 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2697 for (i = 0; i < hdr->NumberOfSections; i++) {
2700 COFF_section* section_i
2702 myindex ( sizeof_COFF_section, sectab, i );
2703 n1 = (UChar*) &(section_i->Name);
2705 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
2706 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
2707 n1[6]==n2[6] && n1[7]==n2[7])
2716 zapTrailingAtSign ( UChar* sym )
2718 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
2720 if (sym[0] == 0) return;
2722 while (sym[i] != 0) i++;
2725 while (j > 0 && my_isdigit(sym[j])) j--;
2726 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
2731 lookupSymbolInDLLs ( UChar *lbl )
2736 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
2737 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
2739 if (lbl[0] == '_') {
2740 /* HACK: if the name has an initial underscore, try stripping
2741 it off & look that up first. I've yet to verify whether there's
2742 a Rule that governs whether an initial '_' *should always* be
2743 stripped off when mapping from import lib name to the DLL name.
2745 sym = GetProcAddress(o_dll->instance, (char*)(lbl+1));
2747 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
2751 sym = GetProcAddress(o_dll->instance, (char*)lbl);
2753 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
2762 ocVerifyImage_PEi386 ( ObjectCode* oc )
2767 COFF_section* sectab;
2768 COFF_symbol* symtab;
2770 /* debugBelch("\nLOADING %s\n", oc->fileName); */
2771 hdr = (COFF_header*)(oc->image);
2772 sectab = (COFF_section*) (
2773 ((UChar*)(oc->image))
2774 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2776 symtab = (COFF_symbol*) (
2777 ((UChar*)(oc->image))
2778 + hdr->PointerToSymbolTable
2780 strtab = ((UChar*)symtab)
2781 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2783 if (hdr->Machine != 0x14c) {
2784 errorBelch("%s: Not x86 PEi386", oc->fileName);
2787 if (hdr->SizeOfOptionalHeader != 0) {
2788 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
2791 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
2792 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
2793 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
2794 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
2795 errorBelch("%s: Not a PEi386 object file", oc->fileName);
2798 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
2799 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
2800 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
2802 (int)(hdr->Characteristics));
2805 /* If the string table size is way crazy, this might indicate that
2806 there are more than 64k relocations, despite claims to the
2807 contrary. Hence this test. */
2808 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
2810 if ( (*(UInt32*)strtab) > 600000 ) {
2811 /* Note that 600k has no special significance other than being
2812 big enough to handle the almost-2MB-sized lumps that
2813 constitute HSwin32*.o. */
2814 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
2819 /* No further verification after this point; only debug printing. */
2821 IF_DEBUG(linker, i=1);
2822 if (i == 0) return 1;
2824 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
2825 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
2826 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
2829 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
2830 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
2831 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
2832 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
2833 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
2834 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
2835 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
2837 /* Print the section table. */
2839 for (i = 0; i < hdr->NumberOfSections; i++) {
2841 COFF_section* sectab_i
2843 myindex ( sizeof_COFF_section, sectab, i );
2850 printName ( sectab_i->Name, strtab );
2860 sectab_i->VirtualSize,
2861 sectab_i->VirtualAddress,
2862 sectab_i->SizeOfRawData,
2863 sectab_i->PointerToRawData,
2864 sectab_i->NumberOfRelocations,
2865 sectab_i->PointerToRelocations,
2866 sectab_i->PointerToRawData
2868 reltab = (COFF_reloc*) (
2869 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2872 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2873 /* If the relocation field (a short) has overflowed, the
2874 * real count can be found in the first reloc entry.
2876 * See Section 4.1 (last para) of the PE spec (rev6.0).
2878 COFF_reloc* rel = (COFF_reloc*)
2879 myindex ( sizeof_COFF_reloc, reltab, 0 );
2880 noRelocs = rel->VirtualAddress;
2883 noRelocs = sectab_i->NumberOfRelocations;
2887 for (; j < noRelocs; j++) {
2889 COFF_reloc* rel = (COFF_reloc*)
2890 myindex ( sizeof_COFF_reloc, reltab, j );
2892 " type 0x%-4x vaddr 0x%-8x name `",
2894 rel->VirtualAddress );
2895 sym = (COFF_symbol*)
2896 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
2897 /* Hmm..mysterious looking offset - what's it for? SOF */
2898 printName ( sym->Name, strtab -10 );
2905 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
2906 debugBelch("---START of string table---\n");
2907 for (i = 4; i < *(Int32*)strtab; i++) {
2909 debugBelch("\n"); else
2910 debugBelch("%c", strtab[i] );
2912 debugBelch("--- END of string table---\n");
2917 COFF_symbol* symtab_i;
2918 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2919 symtab_i = (COFF_symbol*)
2920 myindex ( sizeof_COFF_symbol, symtab, i );
2926 printName ( symtab_i->Name, strtab );
2935 (Int32)(symtab_i->SectionNumber),
2936 (UInt32)symtab_i->Type,
2937 (UInt32)symtab_i->StorageClass,
2938 (UInt32)symtab_i->NumberOfAuxSymbols
2940 i += symtab_i->NumberOfAuxSymbols;
2950 ocGetNames_PEi386 ( ObjectCode* oc )
2953 COFF_section* sectab;
2954 COFF_symbol* symtab;
2961 hdr = (COFF_header*)(oc->image);
2962 sectab = (COFF_section*) (
2963 ((UChar*)(oc->image))
2964 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2966 symtab = (COFF_symbol*) (
2967 ((UChar*)(oc->image))
2968 + hdr->PointerToSymbolTable
2970 strtab = ((UChar*)(oc->image))
2971 + hdr->PointerToSymbolTable
2972 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2974 /* Allocate space for any (local, anonymous) .bss sections. */
2976 for (i = 0; i < hdr->NumberOfSections; i++) {
2979 COFF_section* sectab_i
2981 myindex ( sizeof_COFF_section, sectab, i );
2983 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
2985 if (0 != strcmp(secname, ".bss")) {
2992 /* sof 10/05: the PE spec text isn't too clear regarding what
2993 * the SizeOfRawData field is supposed to hold for object
2994 * file sections containing just uninitialized data -- for executables,
2995 * it is supposed to be zero; unclear what it's supposed to be
2996 * for object files. However, VirtualSize is guaranteed to be
2997 * zero for object files, which definitely suggests that SizeOfRawData
2998 * will be non-zero (where else would the size of this .bss section be
2999 * stored?) Looking at the COFF_section info for incoming object files,
3000 * this certainly appears to be the case.
3002 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
3003 * object files up until now. This turned out to bite us with ghc-6.4.1's use
3004 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
3005 * variable decls into to the .bss section. (The specific function in Q which
3006 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
3008 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
3009 /* This is a non-empty .bss section. Allocate zeroed space for
3010 it, and set its PointerToRawData field such that oc->image +
3011 PointerToRawData == addr_of_zeroed_space. */
3012 bss_sz = sectab_i->VirtualSize;
3013 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
3014 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
3015 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
3016 addProddableBlock(oc, zspace, bss_sz);
3017 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
3020 /* Copy section information into the ObjectCode. */
3022 for (i = 0; i < hdr->NumberOfSections; i++) {
3028 = SECTIONKIND_OTHER;
3029 COFF_section* sectab_i
3031 myindex ( sizeof_COFF_section, sectab, i );
3033 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3035 IF_DEBUG(linker, debugBelch("section name = %s\n", secname ));
3038 /* I'm sure this is the Right Way to do it. However, the
3039 alternative of testing the sectab_i->Name field seems to
3040 work ok with Cygwin.
3042 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
3043 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
3044 kind = SECTIONKIND_CODE_OR_RODATA;
3047 if (0==strcmp(".text",(char*)secname) ||
3048 0==strcmp(".rdata",(char*)secname)||
3049 0==strcmp(".rodata",(char*)secname))
3050 kind = SECTIONKIND_CODE_OR_RODATA;
3051 if (0==strcmp(".data",(char*)secname) ||
3052 0==strcmp(".bss",(char*)secname))
3053 kind = SECTIONKIND_RWDATA;
3055 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
3056 sz = sectab_i->SizeOfRawData;
3057 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
3059 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
3060 end = start + sz - 1;
3062 if (kind == SECTIONKIND_OTHER
3063 /* Ignore sections called which contain stabs debugging
3065 && 0 != strcmp(".stab", (char*)secname)
3066 && 0 != strcmp(".stabstr", (char*)secname)
3067 /* ignore constructor section for now */
3068 && 0 != strcmp(".ctors", (char*)secname)
3069 /* ignore section generated from .ident */
3070 && 0!= strncmp(".debug", (char*)secname, 6)
3071 /* ignore unknown section that appeared in gcc 3.4.5(?) */
3072 && 0!= strcmp(".reloc", (char*)secname)
3073 && 0 != strcmp(".rdata$zzz", (char*)secname)
3075 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", secname, oc->fileName);
3080 if (kind != SECTIONKIND_OTHER && end >= start) {
3081 addSection(oc, kind, start, end);
3082 addProddableBlock(oc, start, end - start + 1);
3088 /* Copy exported symbols into the ObjectCode. */
3090 oc->n_symbols = hdr->NumberOfSymbols;
3091 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3092 "ocGetNames_PEi386(oc->symbols)");
3093 /* Call me paranoid; I don't care. */
3094 for (i = 0; i < oc->n_symbols; i++)
3095 oc->symbols[i] = NULL;
3099 COFF_symbol* symtab_i;
3100 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
3101 symtab_i = (COFF_symbol*)
3102 myindex ( sizeof_COFF_symbol, symtab, i );
3106 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
3107 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
3108 /* This symbol is global and defined, viz, exported */
3109 /* for MYIMAGE_SYMCLASS_EXTERNAL
3110 && !MYIMAGE_SYM_UNDEFINED,
3111 the address of the symbol is:
3112 address of relevant section + offset in section
3114 COFF_section* sectabent
3115 = (COFF_section*) myindex ( sizeof_COFF_section,
3117 symtab_i->SectionNumber-1 );
3118 addr = ((UChar*)(oc->image))
3119 + (sectabent->PointerToRawData
3123 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
3124 && symtab_i->Value > 0) {
3125 /* This symbol isn't in any section at all, ie, global bss.
3126 Allocate zeroed space for it. */
3127 addr = stgCallocBytes(1, symtab_i->Value,
3128 "ocGetNames_PEi386(non-anonymous bss)");
3129 addSection(oc, SECTIONKIND_RWDATA, addr,
3130 ((UChar*)addr) + symtab_i->Value - 1);
3131 addProddableBlock(oc, addr, symtab_i->Value);
3132 /* debugBelch("BSS section at 0x%x\n", addr); */
3135 if (addr != NULL ) {
3136 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
3137 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
3138 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
3139 ASSERT(i >= 0 && i < oc->n_symbols);
3140 /* cstring_from_COFF_symbol_name always succeeds. */
3141 oc->symbols[i] = (char*)sname;
3142 ghciInsertStrHashTable(oc->fileName, symhash, (char*)sname, addr);
3146 "IGNORING symbol %d\n"
3150 printName ( symtab_i->Name, strtab );
3159 (Int32)(symtab_i->SectionNumber),
3160 (UInt32)symtab_i->Type,
3161 (UInt32)symtab_i->StorageClass,
3162 (UInt32)symtab_i->NumberOfAuxSymbols
3167 i += symtab_i->NumberOfAuxSymbols;
3176 ocResolve_PEi386 ( ObjectCode* oc )
3179 COFF_section* sectab;
3180 COFF_symbol* symtab;
3190 /* ToDo: should be variable-sized? But is at least safe in the
3191 sense of buffer-overrun-proof. */
3193 /* debugBelch("resolving for %s\n", oc->fileName); */
3195 hdr = (COFF_header*)(oc->image);
3196 sectab = (COFF_section*) (
3197 ((UChar*)(oc->image))
3198 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3200 symtab = (COFF_symbol*) (
3201 ((UChar*)(oc->image))
3202 + hdr->PointerToSymbolTable
3204 strtab = ((UChar*)(oc->image))
3205 + hdr->PointerToSymbolTable
3206 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3208 for (i = 0; i < hdr->NumberOfSections; i++) {
3209 COFF_section* sectab_i
3211 myindex ( sizeof_COFF_section, sectab, i );
3214 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
3217 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3219 /* Ignore sections called which contain stabs debugging
3221 if (0 == strcmp(".stab", (char*)secname)
3222 || 0 == strcmp(".stabstr", (char*)secname)
3223 || 0 == strcmp(".ctors", (char*)secname)
3224 || 0 == strncmp(".debug", (char*)secname, 6)
3225 || 0 == strcmp(".rdata$zzz", (char*)secname)) {
3232 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
3233 /* If the relocation field (a short) has overflowed, the
3234 * real count can be found in the first reloc entry.
3236 * See Section 4.1 (last para) of the PE spec (rev6.0).
3238 * Nov2003 update: the GNU linker still doesn't correctly
3239 * handle the generation of relocatable object files with
3240 * overflown relocations. Hence the output to warn of potential
3243 COFF_reloc* rel = (COFF_reloc*)
3244 myindex ( sizeof_COFF_reloc, reltab, 0 );
3245 noRelocs = rel->VirtualAddress;
3247 /* 10/05: we now assume (and check for) a GNU ld that is capable
3248 * of handling object files with (>2^16) of relocs.
3251 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
3256 noRelocs = sectab_i->NumberOfRelocations;
3261 for (; j < noRelocs; j++) {
3263 COFF_reloc* reltab_j
3265 myindex ( sizeof_COFF_reloc, reltab, j );
3267 /* the location to patch */
3269 ((UChar*)(oc->image))
3270 + (sectab_i->PointerToRawData
3271 + reltab_j->VirtualAddress
3272 - sectab_i->VirtualAddress )
3274 /* the existing contents of pP */
3276 /* the symbol to connect to */
3277 sym = (COFF_symbol*)
3278 myindex ( sizeof_COFF_symbol,
3279 symtab, reltab_j->SymbolTableIndex );
3282 "reloc sec %2d num %3d: type 0x%-4x "
3283 "vaddr 0x%-8x name `",
3285 (UInt32)reltab_j->Type,
3286 reltab_j->VirtualAddress );
3287 printName ( sym->Name, strtab );
3288 debugBelch("'\n" ));
3290 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
3291 COFF_section* section_sym
3292 = findPEi386SectionCalled ( oc, sym->Name );
3294 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
3297 S = ((UInt32)(oc->image))
3298 + (section_sym->PointerToRawData
3301 copyName ( sym->Name, strtab, symbol, 1000-1 );
3302 S = (UInt32) lookupSymbol( (char*)symbol );
3303 if ((void*)S != NULL) goto foundit;
3304 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3308 checkProddableBlock(oc, pP);
3309 switch (reltab_j->Type) {
3310 case MYIMAGE_REL_I386_DIR32:
3313 case MYIMAGE_REL_I386_REL32:
3314 /* Tricky. We have to insert a displacement at
3315 pP which, when added to the PC for the _next_
3316 insn, gives the address of the target (S).
3317 Problem is to know the address of the next insn
3318 when we only know pP. We assume that this
3319 literal field is always the last in the insn,
3320 so that the address of the next insn is pP+4
3321 -- hence the constant 4.
3322 Also I don't know if A should be added, but so
3323 far it has always been zero.
3325 SOF 05/2005: 'A' (old contents of *pP) have been observed
3326 to contain values other than zero (the 'wx' object file
3327 that came with wxhaskell-0.9.4; dunno how it was compiled..).
3328 So, add displacement to old value instead of asserting
3329 A to be zero. Fixes wxhaskell-related crashes, and no other
3330 ill effects have been observed.
3332 Update: the reason why we're seeing these more elaborate
3333 relocations is due to a switch in how the NCG compiles SRTs
3334 and offsets to them from info tables. SRTs live in .(ro)data,
3335 while info tables live in .text, causing GAS to emit REL32/DISP32
3336 relocations with non-zero values. Adding the displacement is
3337 the right thing to do.
3339 *pP = S - ((UInt32)pP) - 4 + A;
3342 debugBelch("%s: unhandled PEi386 relocation type %d",
3343 oc->fileName, reltab_j->Type);
3350 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
3354 #endif /* defined(OBJFORMAT_PEi386) */
3357 /* --------------------------------------------------------------------------
3359 * ------------------------------------------------------------------------*/
3361 #if defined(OBJFORMAT_ELF)
3366 #if defined(sparc_HOST_ARCH)
3367 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
3368 #elif defined(i386_HOST_ARCH)
3369 # define ELF_TARGET_386 /* Used inside <elf.h> */
3370 #elif defined(x86_64_HOST_ARCH)
3371 # define ELF_TARGET_X64_64
3375 #if !defined(openbsd_HOST_OS)
3378 /* openbsd elf has things in different places, with diff names */
3379 # include <elf_abi.h>
3380 # include <machine/reloc.h>
3381 # define R_386_32 RELOC_32
3382 # define R_386_PC32 RELOC_PC32
3385 /* If elf.h doesn't define it */
3386 # ifndef R_X86_64_PC64
3387 # define R_X86_64_PC64 24
3391 * Define a set of types which can be used for both ELF32 and ELF64
3395 #define ELFCLASS ELFCLASS64
3396 #define Elf_Addr Elf64_Addr
3397 #define Elf_Word Elf64_Word
3398 #define Elf_Sword Elf64_Sword
3399 #define Elf_Ehdr Elf64_Ehdr
3400 #define Elf_Phdr Elf64_Phdr
3401 #define Elf_Shdr Elf64_Shdr
3402 #define Elf_Sym Elf64_Sym
3403 #define Elf_Rel Elf64_Rel
3404 #define Elf_Rela Elf64_Rela
3406 #define ELF_ST_TYPE ELF64_ST_TYPE
3409 #define ELF_ST_BIND ELF64_ST_BIND
3412 #define ELF_R_TYPE ELF64_R_TYPE
3415 #define ELF_R_SYM ELF64_R_SYM
3418 #define ELFCLASS ELFCLASS32
3419 #define Elf_Addr Elf32_Addr
3420 #define Elf_Word Elf32_Word
3421 #define Elf_Sword Elf32_Sword
3422 #define Elf_Ehdr Elf32_Ehdr
3423 #define Elf_Phdr Elf32_Phdr
3424 #define Elf_Shdr Elf32_Shdr
3425 #define Elf_Sym Elf32_Sym
3426 #define Elf_Rel Elf32_Rel
3427 #define Elf_Rela Elf32_Rela
3429 #define ELF_ST_TYPE ELF32_ST_TYPE
3432 #define ELF_ST_BIND ELF32_ST_BIND
3435 #define ELF_R_TYPE ELF32_R_TYPE
3438 #define ELF_R_SYM ELF32_R_SYM
3444 * Functions to allocate entries in dynamic sections. Currently we simply
3445 * preallocate a large number, and we don't check if a entry for the given
3446 * target already exists (a linear search is too slow). Ideally these
3447 * entries would be associated with symbols.
3450 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
3451 #define GOT_SIZE 0x20000
3452 #define FUNCTION_TABLE_SIZE 0x10000
3453 #define PLT_SIZE 0x08000
3456 static Elf_Addr got[GOT_SIZE];
3457 static unsigned int gotIndex;
3458 static Elf_Addr gp_val = (Elf_Addr)got;
3461 allocateGOTEntry(Elf_Addr target)
3465 if (gotIndex >= GOT_SIZE)
3466 barf("Global offset table overflow");
3468 entry = &got[gotIndex++];
3470 return (Elf_Addr)entry;
3474 #ifdef ELF_FUNCTION_DESC
3480 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
3481 static unsigned int functionTableIndex;
3484 allocateFunctionDesc(Elf_Addr target)
3486 FunctionDesc *entry;
3488 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
3489 barf("Function table overflow");
3491 entry = &functionTable[functionTableIndex++];
3493 entry->gp = (Elf_Addr)gp_val;
3494 return (Elf_Addr)entry;
3498 copyFunctionDesc(Elf_Addr target)
3500 FunctionDesc *olddesc = (FunctionDesc *)target;
3501 FunctionDesc *newdesc;
3503 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
3504 newdesc->gp = olddesc->gp;
3505 return (Elf_Addr)newdesc;
3512 unsigned char code[sizeof(plt_code)];
3516 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
3518 PLTEntry *plt = (PLTEntry *)oc->plt;
3521 if (oc->pltIndex >= PLT_SIZE)
3522 barf("Procedure table overflow");
3524 entry = &plt[oc->pltIndex++];
3525 memcpy(entry->code, plt_code, sizeof(entry->code));
3526 PLT_RELOC(entry->code, target);
3527 return (Elf_Addr)entry;
3533 return (PLT_SIZE * sizeof(PLTEntry));
3539 * Generic ELF functions
3543 findElfSection ( void* objImage, Elf_Word sh_type )
3545 char* ehdrC = (char*)objImage;
3546 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3547 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3548 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3552 for (i = 0; i < ehdr->e_shnum; i++) {
3553 if (shdr[i].sh_type == sh_type
3554 /* Ignore the section header's string table. */
3555 && i != ehdr->e_shstrndx
3556 /* Ignore string tables named .stabstr, as they contain
3558 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3560 ptr = ehdrC + shdr[i].sh_offset;
3568 ocVerifyImage_ELF ( ObjectCode* oc )
3572 int i, j, nent, nstrtab, nsymtabs;
3576 char* ehdrC = (char*)(oc->image);
3577 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3579 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
3580 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
3581 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
3582 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
3583 errorBelch("%s: not an ELF object", oc->fileName);
3587 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
3588 errorBelch("%s: unsupported ELF format", oc->fileName);
3592 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
3593 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
3595 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
3596 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
3598 errorBelch("%s: unknown endiannness", oc->fileName);
3602 if (ehdr->e_type != ET_REL) {
3603 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
3606 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
3608 IF_DEBUG(linker,debugBelch( "Architecture is " ));
3609 switch (ehdr->e_machine) {
3610 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
3611 #ifdef EM_SPARC32PLUS
3612 case EM_SPARC32PLUS:
3614 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
3616 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
3618 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
3620 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
3621 #elif defined(EM_AMD64)
3622 case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
3624 default: IF_DEBUG(linker,debugBelch( "unknown" ));
3625 errorBelch("%s: unknown architecture (e_machine == %d)"
3626 , oc->fileName, ehdr->e_machine);
3630 IF_DEBUG(linker,debugBelch(
3631 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
3632 (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
3634 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
3636 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3638 if (ehdr->e_shstrndx == SHN_UNDEF) {
3639 errorBelch("%s: no section header string table", oc->fileName);
3642 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
3644 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3647 for (i = 0; i < ehdr->e_shnum; i++) {
3648 IF_DEBUG(linker,debugBelch("%2d: ", i ));
3649 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
3650 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
3651 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
3652 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
3653 ehdrC + shdr[i].sh_offset,
3654 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3656 if (shdr[i].sh_type == SHT_REL) {
3657 IF_DEBUG(linker,debugBelch("Rel " ));
3658 } else if (shdr[i].sh_type == SHT_RELA) {
3659 IF_DEBUG(linker,debugBelch("RelA " ));
3661 IF_DEBUG(linker,debugBelch(" "));
3664 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3668 IF_DEBUG(linker,debugBelch( "\nString tables" ));
3671 for (i = 0; i < ehdr->e_shnum; i++) {
3672 if (shdr[i].sh_type == SHT_STRTAB
3673 /* Ignore the section header's string table. */
3674 && i != ehdr->e_shstrndx
3675 /* Ignore string tables named .stabstr, as they contain
3677 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3679 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
3680 strtab = ehdrC + shdr[i].sh_offset;
3685 errorBelch("%s: no string tables, or too many", oc->fileName);
3690 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
3691 for (i = 0; i < ehdr->e_shnum; i++) {
3692 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3693 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3695 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3696 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3697 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
3699 (long)shdr[i].sh_size % sizeof(Elf_Sym)
3701 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
3702 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
3705 for (j = 0; j < nent; j++) {
3706 IF_DEBUG(linker,debugBelch(" %2d ", j ));
3707 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
3708 (int)stab[j].st_shndx,
3709 (int)stab[j].st_size,
3710 (char*)stab[j].st_value ));
3712 IF_DEBUG(linker,debugBelch("type=" ));
3713 switch (ELF_ST_TYPE(stab[j].st_info)) {
3714 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
3715 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
3716 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
3717 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
3718 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
3719 default: IF_DEBUG(linker,debugBelch("? " )); break;
3721 IF_DEBUG(linker,debugBelch(" " ));
3723 IF_DEBUG(linker,debugBelch("bind=" ));
3724 switch (ELF_ST_BIND(stab[j].st_info)) {
3725 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
3726 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
3727 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
3728 default: IF_DEBUG(linker,debugBelch("? " )); break;
3730 IF_DEBUG(linker,debugBelch(" " ));
3732 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
3736 if (nsymtabs == 0) {
3737 errorBelch("%s: didn't find any symbol tables", oc->fileName);
3744 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
3748 if (hdr->sh_type == SHT_PROGBITS
3749 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
3750 /* .text-style section */
3751 return SECTIONKIND_CODE_OR_RODATA;
3754 if (hdr->sh_type == SHT_PROGBITS
3755 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3756 /* .data-style section */
3757 return SECTIONKIND_RWDATA;
3760 if (hdr->sh_type == SHT_PROGBITS
3761 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
3762 /* .rodata-style section */
3763 return SECTIONKIND_CODE_OR_RODATA;
3766 if (hdr->sh_type == SHT_NOBITS
3767 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3768 /* .bss-style section */
3770 return SECTIONKIND_RWDATA;
3773 return SECTIONKIND_OTHER;
3778 ocGetNames_ELF ( ObjectCode* oc )
3783 char* ehdrC = (char*)(oc->image);
3784 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3785 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
3786 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3788 ASSERT(symhash != NULL);
3791 errorBelch("%s: no strtab", oc->fileName);
3796 for (i = 0; i < ehdr->e_shnum; i++) {
3797 /* Figure out what kind of section it is. Logic derived from
3798 Figure 1.14 ("Special Sections") of the ELF document
3799 ("Portable Formats Specification, Version 1.1"). */
3801 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
3803 if (is_bss && shdr[i].sh_size > 0) {
3804 /* This is a non-empty .bss section. Allocate zeroed space for
3805 it, and set its .sh_offset field such that
3806 ehdrC + .sh_offset == addr_of_zeroed_space. */
3807 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
3808 "ocGetNames_ELF(BSS)");
3809 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
3811 debugBelch("BSS section at 0x%x, size %d\n",
3812 zspace, shdr[i].sh_size);
3816 /* fill in the section info */
3817 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
3818 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
3819 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
3820 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
3823 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3825 /* copy stuff into this module's object symbol table */
3826 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3827 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3829 oc->n_symbols = nent;
3830 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3831 "ocGetNames_ELF(oc->symbols)");
3833 for (j = 0; j < nent; j++) {
3835 char isLocal = FALSE; /* avoids uninit-var warning */
3837 char* nm = strtab + stab[j].st_name;
3838 int secno = stab[j].st_shndx;
3840 /* Figure out if we want to add it; if so, set ad to its
3841 address. Otherwise leave ad == NULL. */
3843 if (secno == SHN_COMMON) {
3845 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
3847 debugBelch("COMMON symbol, size %d name %s\n",
3848 stab[j].st_size, nm);
3850 /* Pointless to do addProddableBlock() for this area,
3851 since the linker should never poke around in it. */
3854 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
3855 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
3857 /* and not an undefined symbol */
3858 && stab[j].st_shndx != SHN_UNDEF
3859 /* and not in a "special section" */
3860 && stab[j].st_shndx < SHN_LORESERVE
3862 /* and it's a not a section or string table or anything silly */
3863 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3864 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3865 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3868 /* Section 0 is the undefined section, hence > and not >=. */
3869 ASSERT(secno > 0 && secno < ehdr->e_shnum);
3871 if (shdr[secno].sh_type == SHT_NOBITS) {
3872 debugBelch(" BSS symbol, size %d off %d name %s\n",
3873 stab[j].st_size, stab[j].st_value, nm);
3876 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3877 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3880 #ifdef ELF_FUNCTION_DESC
3881 /* dlsym() and the initialisation table both give us function
3882 * descriptors, so to be consistent we store function descriptors
3883 * in the symbol table */
3884 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3885 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3887 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n",
3888 ad, oc->fileName, nm ));
3893 /* And the decision is ... */
3897 oc->symbols[j] = nm;
3900 /* Ignore entirely. */
3902 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3906 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3907 strtab + stab[j].st_name ));
3910 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3911 (int)ELF_ST_BIND(stab[j].st_info),
3912 (int)ELF_ST_TYPE(stab[j].st_info),
3913 (int)stab[j].st_shndx,
3914 strtab + stab[j].st_name
3917 oc->symbols[j] = NULL;
3926 /* Do ELF relocations which lack an explicit addend. All x86-linux
3927 relocations appear to be of this form. */
3929 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3930 Elf_Shdr* shdr, int shnum,
3931 Elf_Sym* stab, char* strtab )
3936 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3937 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3938 int target_shndx = shdr[shnum].sh_info;
3939 int symtab_shndx = shdr[shnum].sh_link;
3941 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3942 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3943 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3944 target_shndx, symtab_shndx ));
3946 /* Skip sections that we're not interested in. */
3949 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3950 if (kind == SECTIONKIND_OTHER) {
3951 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
3956 for (j = 0; j < nent; j++) {
3957 Elf_Addr offset = rtab[j].r_offset;
3958 Elf_Addr info = rtab[j].r_info;
3960 Elf_Addr P = ((Elf_Addr)targ) + offset;
3961 Elf_Word* pP = (Elf_Word*)P;
3966 StgStablePtr stablePtr;
3969 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3970 j, (void*)offset, (void*)info ));
3972 IF_DEBUG(linker,debugBelch( " ZERO" ));
3975 Elf_Sym sym = stab[ELF_R_SYM(info)];
3976 /* First see if it is a local symbol. */
3977 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3978 /* Yes, so we can get the address directly from the ELF symbol
3980 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3982 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3983 + stab[ELF_R_SYM(info)].st_value);
3986 symbol = strtab + sym.st_name;
3987 stablePtr = (StgStablePtr)lookupHashTable(stablehash, (StgWord)symbol);
3988 if (NULL == stablePtr) {
3989 /* No, so look up the name in our global table. */
3990 S_tmp = lookupSymbol( symbol );
3991 S = (Elf_Addr)S_tmp;
3993 stableVal = deRefStablePtr( stablePtr );
3995 S = (Elf_Addr)S_tmp;
3999 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4002 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
4005 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
4006 (void*)P, (void*)S, (void*)A ));
4007 checkProddableBlock ( oc, pP );
4011 switch (ELF_R_TYPE(info)) {
4012 # ifdef i386_HOST_ARCH
4013 case R_386_32: *pP = value; break;
4014 case R_386_PC32: *pP = value - P; break;
4017 errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
4018 oc->fileName, (lnat)ELF_R_TYPE(info));
4026 /* Do ELF relocations for which explicit addends are supplied.
4027 sparc-solaris relocations appear to be of this form. */
4029 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
4030 Elf_Shdr* shdr, int shnum,
4031 Elf_Sym* stab, char* strtab )
4034 char *symbol = NULL;
4036 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
4037 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
4038 int target_shndx = shdr[shnum].sh_info;
4039 int symtab_shndx = shdr[shnum].sh_link;
4041 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
4042 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
4043 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
4044 target_shndx, symtab_shndx ));
4046 for (j = 0; j < nent; j++) {
4047 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4048 /* This #ifdef only serves to avoid unused-var warnings. */
4049 Elf_Addr offset = rtab[j].r_offset;
4050 Elf_Addr P = targ + offset;
4052 Elf_Addr info = rtab[j].r_info;
4053 Elf_Addr A = rtab[j].r_addend;
4057 # if defined(sparc_HOST_ARCH)
4058 Elf_Word* pP = (Elf_Word*)P;
4060 # elif defined(powerpc_HOST_ARCH)
4064 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
4065 j, (void*)offset, (void*)info,
4068 IF_DEBUG(linker,debugBelch( " ZERO" ));
4071 Elf_Sym sym = stab[ELF_R_SYM(info)];
4072 /* First see if it is a local symbol. */
4073 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
4074 /* Yes, so we can get the address directly from the ELF symbol
4076 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
4078 (ehdrC + shdr[ sym.st_shndx ].sh_offset
4079 + stab[ELF_R_SYM(info)].st_value);
4080 #ifdef ELF_FUNCTION_DESC
4081 /* Make a function descriptor for this function */
4082 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
4083 S = allocateFunctionDesc(S + A);
4088 /* No, so look up the name in our global table. */
4089 symbol = strtab + sym.st_name;
4090 S_tmp = lookupSymbol( symbol );
4091 S = (Elf_Addr)S_tmp;
4093 #ifdef ELF_FUNCTION_DESC
4094 /* If a function, already a function descriptor - we would
4095 have to copy it to add an offset. */
4096 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
4097 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
4101 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4104 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
4107 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
4108 (void*)P, (void*)S, (void*)A ));
4109 /* checkProddableBlock ( oc, (void*)P ); */
4113 switch (ELF_R_TYPE(info)) {
4114 # if defined(sparc_HOST_ARCH)
4115 case R_SPARC_WDISP30:
4116 w1 = *pP & 0xC0000000;
4117 w2 = (Elf_Word)((value - P) >> 2);
4118 ASSERT((w2 & 0xC0000000) == 0);
4123 w1 = *pP & 0xFFC00000;
4124 w2 = (Elf_Word)(value >> 10);
4125 ASSERT((w2 & 0xFFC00000) == 0);
4131 w2 = (Elf_Word)(value & 0x3FF);
4132 ASSERT((w2 & ~0x3FF) == 0);
4137 /* According to the Sun documentation:
4139 This relocation type resembles R_SPARC_32, except it refers to an
4140 unaligned word. That is, the word to be relocated must be treated
4141 as four separate bytes with arbitrary alignment, not as a word
4142 aligned according to the architecture requirements.
4145 w2 = (Elf_Word)value;
4147 // SPARC doesn't do misaligned writes of 32 bit words,
4148 // so we have to do this one byte-at-a-time.
4149 char *pPc = (char*)pP;
4150 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
4151 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
4152 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
4153 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
4157 w2 = (Elf_Word)value;
4160 # elif defined(powerpc_HOST_ARCH)
4161 case R_PPC_ADDR16_LO:
4162 *(Elf32_Half*) P = value;
4165 case R_PPC_ADDR16_HI:
4166 *(Elf32_Half*) P = value >> 16;
4169 case R_PPC_ADDR16_HA:
4170 *(Elf32_Half*) P = (value + 0x8000) >> 16;
4174 *(Elf32_Word *) P = value;
4178 *(Elf32_Word *) P = value - P;
4184 if( delta << 6 >> 6 != delta )
4186 value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
4190 if( value == 0 || delta << 6 >> 6 != delta )
4192 barf( "Unable to make SymbolExtra for #%d",
4198 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
4199 | (delta & 0x3fffffc);
4203 #if x86_64_HOST_ARCH
4205 *(Elf64_Xword *)P = value;
4210 #if defined(ALWAYS_PIC)
4211 barf("R_X86_64_PC32 relocation, but ALWAYS_PIC.");
4213 StgInt64 off = value - P;
4214 if (off >= 0x7fffffffL || off < -0x80000000L) {
4215 #if X86_64_ELF_NONPIC_HACK
4216 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4218 off = pltAddress + A - P;
4220 barf("R_X86_64_PC32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4221 symbol, off, oc->fileName );
4224 *(Elf64_Word *)P = (Elf64_Word)off;
4231 StgInt64 off = value - P;
4232 *(Elf64_Word *)P = (Elf64_Word)off;
4237 #if defined(ALWAYS_PIC)
4238 barf("R_X86_64_32 relocation, but ALWAYS_PIC.");
4240 if (value >= 0x7fffffffL) {
4241 #if X86_64_ELF_NONPIC_HACK
4242 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4244 value = pltAddress + A;
4246 barf("R_X86_64_32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4247 symbol, value, oc->fileName );
4250 *(Elf64_Word *)P = (Elf64_Word)value;
4255 #if defined(ALWAYS_PIC)
4256 barf("R_X86_64_32S relocation, but ALWAYS_PIC.");
4258 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
4259 #if X86_64_ELF_NONPIC_HACK
4260 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4262 value = pltAddress + A;
4264 barf("R_X86_64_32S relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4265 symbol, value, oc->fileName );
4268 *(Elf64_Sword *)P = (Elf64_Sword)value;
4272 case R_X86_64_GOTPCREL:
4274 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
4275 StgInt64 off = gotAddress + A - P;
4276 *(Elf64_Word *)P = (Elf64_Word)off;
4280 case R_X86_64_PLT32:
4282 #if defined(ALWAYS_PIC)
4283 barf("R_X86_64_PLT32 relocation, but ALWAYS_PIC.");
4285 StgInt64 off = value - P;
4286 if (off >= 0x7fffffffL || off < -0x80000000L) {
4287 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4289 off = pltAddress + A - P;
4291 *(Elf64_Word *)P = (Elf64_Word)off;
4298 errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
4299 oc->fileName, (lnat)ELF_R_TYPE(info));
4308 ocResolve_ELF ( ObjectCode* oc )
4312 Elf_Sym* stab = NULL;
4313 char* ehdrC = (char*)(oc->image);
4314 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
4315 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
4317 /* first find "the" symbol table */
4318 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
4320 /* also go find the string table */
4321 strtab = findElfSection ( ehdrC, SHT_STRTAB );
4323 if (stab == NULL || strtab == NULL) {
4324 errorBelch("%s: can't find string or symbol table", oc->fileName);
4328 /* Process the relocation sections. */
4329 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
4330 if (shdr[shnum].sh_type == SHT_REL) {
4331 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
4332 shnum, stab, strtab );
4336 if (shdr[shnum].sh_type == SHT_RELA) {
4337 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
4338 shnum, stab, strtab );
4343 #if defined(powerpc_HOST_ARCH)
4344 ocFlushInstructionCache( oc );
4351 * PowerPC & X86_64 ELF specifics
4354 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4356 static int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
4362 ehdr = (Elf_Ehdr *) oc->image;
4363 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
4365 for( i = 0; i < ehdr->e_shnum; i++ )
4366 if( shdr[i].sh_type == SHT_SYMTAB )
4369 if( i == ehdr->e_shnum )
4371 errorBelch( "This ELF file contains no symtab" );
4375 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
4377 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
4378 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
4383 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
4386 #endif /* powerpc */
4390 /* --------------------------------------------------------------------------
4392 * ------------------------------------------------------------------------*/
4394 #if defined(OBJFORMAT_MACHO)
4397 Support for MachO linking on Darwin/MacOS X
4398 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
4400 I hereby formally apologize for the hackish nature of this code.
4401 Things that need to be done:
4402 *) implement ocVerifyImage_MachO
4403 *) add still more sanity checks.
4406 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4407 #define mach_header mach_header_64
4408 #define segment_command segment_command_64
4409 #define section section_64
4410 #define nlist nlist_64
4413 #ifdef powerpc_HOST_ARCH
4414 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4416 struct mach_header *header = (struct mach_header *) oc->image;
4417 struct load_command *lc = (struct load_command *) (header + 1);
4420 for( i = 0; i < header->ncmds; i++ )
4422 if( lc->cmd == LC_SYMTAB )
4424 // Find out the first and last undefined external
4425 // symbol, so we don't have to allocate too many
4427 struct symtab_command *symLC = (struct symtab_command *) lc;
4428 unsigned min = symLC->nsyms, max = 0;
4429 struct nlist *nlist =
4430 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
4432 for(i=0;i<symLC->nsyms;i++)
4434 if(nlist[i].n_type & N_STAB)
4436 else if(nlist[i].n_type & N_EXT)
4438 if((nlist[i].n_type & N_TYPE) == N_UNDF
4439 && (nlist[i].n_value == 0))
4449 return ocAllocateSymbolExtras(oc, max - min + 1, min);
4454 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4456 return ocAllocateSymbolExtras(oc,0,0);
4459 #ifdef x86_64_HOST_ARCH
4460 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4462 struct mach_header *header = (struct mach_header *) oc->image;
4463 struct load_command *lc = (struct load_command *) (header + 1);
4466 for( i = 0; i < header->ncmds; i++ )
4468 if( lc->cmd == LC_SYMTAB )
4470 // Just allocate one entry for every symbol
4471 struct symtab_command *symLC = (struct symtab_command *) lc;
4473 return ocAllocateSymbolExtras(oc, symLC->nsyms, 0);
4476 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4478 return ocAllocateSymbolExtras(oc,0,0);
4482 static int ocVerifyImage_MachO(ObjectCode* oc)
4484 char *image = (char*) oc->image;
4485 struct mach_header *header = (struct mach_header*) image;
4487 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4488 if(header->magic != MH_MAGIC_64) {
4489 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4490 oc->fileName, MH_MAGIC_64, header->magic);
4494 if(header->magic != MH_MAGIC) {
4495 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4496 oc->fileName, MH_MAGIC, header->magic);
4500 // FIXME: do some more verifying here
4504 static int resolveImports(
4507 struct symtab_command *symLC,
4508 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
4509 unsigned long *indirectSyms,
4510 struct nlist *nlist)
4513 size_t itemSize = 4;
4515 IF_DEBUG(linker, debugBelch("resolveImports: start\n"));
4518 int isJumpTable = 0;
4519 if(!strcmp(sect->sectname,"__jump_table"))
4523 ASSERT(sect->reserved2 == itemSize);
4527 for(i=0; i*itemSize < sect->size;i++)
4529 // according to otool, reserved1 contains the first index into the indirect symbol table
4530 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
4531 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4534 IF_DEBUG(linker, debugBelch("resolveImports: resolving %s\n", nm));
4535 if ((symbol->n_type & N_TYPE) == N_UNDF
4536 && (symbol->n_type & N_EXT) && (symbol->n_value != 0)) {
4537 addr = (void*) (symbol->n_value);
4538 IF_DEBUG(linker, debugBelch("resolveImports: undefined external %s has value %p\n", nm, addr));
4540 addr = lookupSymbol(nm);
4541 IF_DEBUG(linker, debugBelch("resolveImports: looking up %s, %p\n", nm, addr));
4545 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
4553 checkProddableBlock(oc,image + sect->offset + i*itemSize);
4554 *(image + sect->offset + i*itemSize) = 0xe9; // jmp
4555 *(unsigned*)(image + sect->offset + i*itemSize + 1)
4556 = (char*)addr - (image + sect->offset + i*itemSize + 5);
4561 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
4562 ((void**)(image + sect->offset))[i] = addr;
4566 IF_DEBUG(linker, debugBelch("resolveImports: done\n"));
4570 static unsigned long relocateAddress(
4573 struct section* sections,
4574 unsigned long address)
4577 IF_DEBUG(linker, debugBelch("relocateAddress: start\n"));
4578 for (i = 0; i < nSections; i++)
4580 IF_DEBUG(linker, debugBelch(" relocating address in section %d\n", i));
4581 if (sections[i].addr <= address
4582 && address < sections[i].addr + sections[i].size)
4584 return (unsigned long)oc->image
4585 + sections[i].offset + address - sections[i].addr;
4588 barf("Invalid Mach-O file:"
4589 "Address out of bounds while relocating object file");
4593 static int relocateSection(
4596 struct symtab_command *symLC, struct nlist *nlist,
4597 int nSections, struct section* sections, struct section *sect)
4599 struct relocation_info *relocs;
4602 IF_DEBUG(linker, debugBelch("relocateSection: start\n"));
4604 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
4606 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
4608 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
4610 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
4614 IF_DEBUG(linker, debugBelch("relocateSection: number of relocations: %d\n", n));
4616 relocs = (struct relocation_info*) (image + sect->reloff);
4620 #ifdef x86_64_HOST_ARCH
4621 struct relocation_info *reloc = &relocs[i];
4623 char *thingPtr = image + sect->offset + reloc->r_address;
4625 /* We shouldn't need to initialise this, but gcc on OS X 64 bit
4626 complains that it may be used uninitialized if we don't */
4629 int type = reloc->r_type;
4631 checkProddableBlock(oc,thingPtr);
4632 switch(reloc->r_length)
4635 thing = *(uint8_t*)thingPtr;
4636 baseValue = (uint64_t)thingPtr + 1;
4639 thing = *(uint16_t*)thingPtr;
4640 baseValue = (uint64_t)thingPtr + 2;
4643 thing = *(uint32_t*)thingPtr;
4644 baseValue = (uint64_t)thingPtr + 4;
4647 thing = *(uint64_t*)thingPtr;
4648 baseValue = (uint64_t)thingPtr + 8;
4651 barf("Unknown size.");
4655 debugBelch("relocateSection: length = %d, thing = %d, baseValue = %p\n",
4656 reloc->r_length, thing, baseValue));
4658 if (type == X86_64_RELOC_GOT
4659 || type == X86_64_RELOC_GOT_LOAD)
4661 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4662 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4664 IF_DEBUG(linker, debugBelch("relocateSection: making jump island for %s, extern = %d, X86_64_RELOC_GOT\n", nm, reloc->r_extern));
4665 ASSERT(reloc->r_extern);
4666 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, (unsigned long)lookupSymbol(nm))->addr;
4668 type = X86_64_RELOC_SIGNED;
4670 else if(reloc->r_extern)
4672 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4673 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4675 IF_DEBUG(linker, debugBelch("relocateSection: looking up external symbol %s\n", nm));
4676 IF_DEBUG(linker, debugBelch(" : type = %d\n", symbol->n_type));
4677 IF_DEBUG(linker, debugBelch(" : sect = %d\n", symbol->n_sect));
4678 IF_DEBUG(linker, debugBelch(" : desc = %d\n", symbol->n_desc));
4679 IF_DEBUG(linker, debugBelch(" : value = %p\n", (void *)symbol->n_value));
4680 if ((symbol->n_type & N_TYPE) == N_SECT) {
4681 value = relocateAddress(oc, nSections, sections,
4683 IF_DEBUG(linker, debugBelch("relocateSection, defined external symbol %s, relocated address %p\n", nm, (void *)value));
4686 value = (uint64_t) lookupSymbol(nm);
4687 IF_DEBUG(linker, debugBelch("relocateSection: external symbol %s, address %p\n", nm, (void *)value));
4692 value = sections[reloc->r_symbolnum-1].offset
4693 - sections[reloc->r_symbolnum-1].addr
4697 IF_DEBUG(linker, debugBelch("relocateSection: value = %p\n", (void *)value));
4699 if (type == X86_64_RELOC_BRANCH)
4701 if((int32_t)(value - baseValue) != (int64_t)(value - baseValue))
4703 ASSERT(reloc->r_extern);
4704 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, value)
4707 ASSERT((int32_t)(value - baseValue) == (int64_t)(value - baseValue));
4708 type = X86_64_RELOC_SIGNED;
4713 case X86_64_RELOC_UNSIGNED:
4714 ASSERT(!reloc->r_pcrel);
4717 case X86_64_RELOC_SIGNED:
4718 case X86_64_RELOC_SIGNED_1:
4719 case X86_64_RELOC_SIGNED_2:
4720 case X86_64_RELOC_SIGNED_4:
4721 ASSERT(reloc->r_pcrel);
4722 thing += value - baseValue;
4724 case X86_64_RELOC_SUBTRACTOR:
4725 ASSERT(!reloc->r_pcrel);
4729 barf("unkown relocation");
4732 switch(reloc->r_length)
4735 *(uint8_t*)thingPtr = thing;
4738 *(uint16_t*)thingPtr = thing;
4741 *(uint32_t*)thingPtr = thing;
4744 *(uint64_t*)thingPtr = thing;
4748 if(relocs[i].r_address & R_SCATTERED)
4750 struct scattered_relocation_info *scat =
4751 (struct scattered_relocation_info*) &relocs[i];
4755 if(scat->r_length == 2)
4757 unsigned long word = 0;
4758 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
4759 checkProddableBlock(oc,wordPtr);
4761 // Note on relocation types:
4762 // i386 uses the GENERIC_RELOC_* types,
4763 // while ppc uses special PPC_RELOC_* types.
4764 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
4765 // in both cases, all others are different.
4766 // Therefore, we use GENERIC_RELOC_VANILLA
4767 // and GENERIC_RELOC_PAIR instead of the PPC variants,
4768 // and use #ifdefs for the other types.
4770 // Step 1: Figure out what the relocated value should be
4771 if(scat->r_type == GENERIC_RELOC_VANILLA)
4773 word = *wordPtr + (unsigned long) relocateAddress(
4780 #ifdef powerpc_HOST_ARCH
4781 else if(scat->r_type == PPC_RELOC_SECTDIFF
4782 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
4783 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
4784 || scat->r_type == PPC_RELOC_HA16_SECTDIFF
4785 || scat->r_type == PPC_RELOC_LOCAL_SECTDIFF)
4787 else if(scat->r_type == GENERIC_RELOC_SECTDIFF
4788 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4791 struct scattered_relocation_info *pair =
4792 (struct scattered_relocation_info*) &relocs[i+1];
4794 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
4795 barf("Invalid Mach-O file: "
4796 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
4798 word = (unsigned long)
4799 (relocateAddress(oc, nSections, sections, scat->r_value)
4800 - relocateAddress(oc, nSections, sections, pair->r_value));
4803 #ifdef powerpc_HOST_ARCH
4804 else if(scat->r_type == PPC_RELOC_HI16
4805 || scat->r_type == PPC_RELOC_LO16
4806 || scat->r_type == PPC_RELOC_HA16
4807 || scat->r_type == PPC_RELOC_LO14)
4808 { // these are generated by label+offset things
4809 struct relocation_info *pair = &relocs[i+1];
4810 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
4811 barf("Invalid Mach-O file: "
4812 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
4814 if(scat->r_type == PPC_RELOC_LO16)
4816 word = ((unsigned short*) wordPtr)[1];
4817 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4819 else if(scat->r_type == PPC_RELOC_LO14)
4821 barf("Unsupported Relocation: PPC_RELOC_LO14");
4822 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
4823 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4825 else if(scat->r_type == PPC_RELOC_HI16)
4827 word = ((unsigned short*) wordPtr)[1] << 16;
4828 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4830 else if(scat->r_type == PPC_RELOC_HA16)
4832 word = ((unsigned short*) wordPtr)[1] << 16;
4833 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4837 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
4845 barf ("Don't know how to handle this Mach-O "
4846 "scattered relocation entry: "
4847 "object file %s; entry type %ld; "
4849 OC_INFORMATIVE_FILENAME(oc),
4855 #ifdef powerpc_HOST_ARCH
4856 if(scat->r_type == GENERIC_RELOC_VANILLA
4857 || scat->r_type == PPC_RELOC_SECTDIFF)
4859 if(scat->r_type == GENERIC_RELOC_VANILLA
4860 || scat->r_type == GENERIC_RELOC_SECTDIFF
4861 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4866 #ifdef powerpc_HOST_ARCH
4867 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
4869 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4871 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
4873 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4875 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
4877 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4878 + ((word & (1<<15)) ? 1 : 0);
4884 barf("Can't handle Mach-O scattered relocation entry "
4885 "with this r_length tag: "
4886 "object file %s; entry type %ld; "
4887 "r_length tag %ld; address %#lx\n",
4888 OC_INFORMATIVE_FILENAME(oc),
4895 else /* scat->r_pcrel */
4897 barf("Don't know how to handle *PC-relative* Mach-O "
4898 "scattered relocation entry: "
4899 "object file %s; entry type %ld; address %#lx\n",
4900 OC_INFORMATIVE_FILENAME(oc),
4907 else /* !(relocs[i].r_address & R_SCATTERED) */
4909 struct relocation_info *reloc = &relocs[i];
4910 if(reloc->r_pcrel && !reloc->r_extern)
4913 if(reloc->r_length == 2)
4915 unsigned long word = 0;
4916 #ifdef powerpc_HOST_ARCH
4917 unsigned long jumpIsland = 0;
4918 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
4919 // to avoid warning and to catch
4923 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
4924 checkProddableBlock(oc,wordPtr);
4926 if(reloc->r_type == GENERIC_RELOC_VANILLA)
4930 #ifdef powerpc_HOST_ARCH
4931 else if(reloc->r_type == PPC_RELOC_LO16)
4933 word = ((unsigned short*) wordPtr)[1];
4934 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4936 else if(reloc->r_type == PPC_RELOC_HI16)
4938 word = ((unsigned short*) wordPtr)[1] << 16;
4939 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4941 else if(reloc->r_type == PPC_RELOC_HA16)
4943 word = ((unsigned short*) wordPtr)[1] << 16;
4944 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4946 else if(reloc->r_type == PPC_RELOC_BR24)
4949 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
4954 barf("Can't handle this Mach-O relocation entry "
4956 "object file %s; entry type %ld; address %#lx\n",
4957 OC_INFORMATIVE_FILENAME(oc),
4963 if(!reloc->r_extern)
4966 sections[reloc->r_symbolnum-1].offset
4967 - sections[reloc->r_symbolnum-1].addr
4974 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4975 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4976 void *symbolAddress = lookupSymbol(nm);
4979 errorBelch("\nunknown symbol `%s'", nm);
4985 #ifdef powerpc_HOST_ARCH
4986 // In the .o file, this should be a relative jump to NULL
4987 // and we'll change it to a relative jump to the symbol
4988 ASSERT(word + reloc->r_address == 0);
4989 jumpIsland = (unsigned long)
4990 &makeSymbolExtra(oc,
4992 (unsigned long) symbolAddress)
4996 offsetToJumpIsland = word + jumpIsland
4997 - (((long)image) + sect->offset - sect->addr);
5000 word += (unsigned long) symbolAddress
5001 - (((long)image) + sect->offset - sect->addr);
5005 word += (unsigned long) symbolAddress;
5009 if(reloc->r_type == GENERIC_RELOC_VANILLA)
5014 #ifdef powerpc_HOST_ARCH
5015 else if(reloc->r_type == PPC_RELOC_LO16)
5017 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
5020 else if(reloc->r_type == PPC_RELOC_HI16)
5022 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
5025 else if(reloc->r_type == PPC_RELOC_HA16)
5027 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
5028 + ((word & (1<<15)) ? 1 : 0);
5031 else if(reloc->r_type == PPC_RELOC_BR24)
5033 if((word & 0x03) != 0)
5034 barf("%s: unconditional relative branch with a displacement "
5035 "which isn't a multiple of 4 bytes: %#lx",
5036 OC_INFORMATIVE_FILENAME(oc),
5039 if((word & 0xFE000000) != 0xFE000000 &&
5040 (word & 0xFE000000) != 0x00000000)
5042 // The branch offset is too large.
5043 // Therefore, we try to use a jump island.
5046 barf("%s: unconditional relative branch out of range: "
5047 "no jump island available: %#lx",
5048 OC_INFORMATIVE_FILENAME(oc),
5052 word = offsetToJumpIsland;
5053 if((word & 0xFE000000) != 0xFE000000 &&
5054 (word & 0xFE000000) != 0x00000000)
5055 barf("%s: unconditional relative branch out of range: "
5056 "jump island out of range: %#lx",
5057 OC_INFORMATIVE_FILENAME(oc),
5060 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
5067 barf("Can't handle Mach-O relocation entry (not scattered) "
5068 "with this r_length tag: "
5069 "object file %s; entry type %ld; "
5070 "r_length tag %ld; address %#lx\n",
5071 OC_INFORMATIVE_FILENAME(oc),
5080 IF_DEBUG(linker, debugBelch("relocateSection: done\n"));
5084 static int ocGetNames_MachO(ObjectCode* oc)
5086 char *image = (char*) oc->image;
5087 struct mach_header *header = (struct mach_header*) image;
5088 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5089 unsigned i,curSymbol = 0;
5090 struct segment_command *segLC = NULL;
5091 struct section *sections;
5092 struct symtab_command *symLC = NULL;
5093 struct nlist *nlist;
5094 unsigned long commonSize = 0;
5095 char *commonStorage = NULL;
5096 unsigned long commonCounter;
5098 IF_DEBUG(linker,debugBelch("ocGetNames_MachO: start\n"));
5100 for(i=0;i<header->ncmds;i++)
5102 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
5103 segLC = (struct segment_command*) lc;
5104 else if(lc->cmd == LC_SYMTAB)
5105 symLC = (struct symtab_command*) lc;
5106 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5109 sections = (struct section*) (segLC+1);
5110 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5114 barf("ocGetNames_MachO: no segment load command");
5116 for(i=0;i<segLC->nsects;i++)
5118 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: segment %d\n", i));
5119 if (sections[i].size == 0)
5122 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
5124 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
5125 "ocGetNames_MachO(common symbols)");
5126 sections[i].offset = zeroFillArea - image;
5129 if(!strcmp(sections[i].sectname,"__text"))
5130 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
5131 (void*) (image + sections[i].offset),
5132 (void*) (image + sections[i].offset + sections[i].size));
5133 else if(!strcmp(sections[i].sectname,"__const"))
5134 addSection(oc, SECTIONKIND_RWDATA,
5135 (void*) (image + sections[i].offset),
5136 (void*) (image + sections[i].offset + sections[i].size));
5137 else if(!strcmp(sections[i].sectname,"__data"))
5138 addSection(oc, SECTIONKIND_RWDATA,
5139 (void*) (image + sections[i].offset),
5140 (void*) (image + sections[i].offset + sections[i].size));
5141 else if(!strcmp(sections[i].sectname,"__bss")
5142 || !strcmp(sections[i].sectname,"__common"))
5143 addSection(oc, SECTIONKIND_RWDATA,
5144 (void*) (image + sections[i].offset),
5145 (void*) (image + sections[i].offset + sections[i].size));
5147 addProddableBlock(oc, (void*) (image + sections[i].offset),
5151 // count external symbols defined here
5155 for(i=0;i<symLC->nsyms;i++)
5157 if(nlist[i].n_type & N_STAB)
5159 else if(nlist[i].n_type & N_EXT)
5161 if((nlist[i].n_type & N_TYPE) == N_UNDF
5162 && (nlist[i].n_value != 0))
5164 commonSize += nlist[i].n_value;
5167 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5172 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: %d external symbols\n", oc->n_symbols));
5173 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
5174 "ocGetNames_MachO(oc->symbols)");
5178 for(i=0;i<symLC->nsyms;i++)
5180 if(nlist[i].n_type & N_STAB)
5182 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5184 if(nlist[i].n_type & N_EXT)
5186 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5187 if ((nlist[i].n_desc & N_WEAK_DEF) && lookupSymbol(nm)) {
5188 // weak definition, and we already have a definition
5189 IF_DEBUG(linker, debugBelch(" weak: %s\n", nm));
5193 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting %s\n", nm));
5194 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5196 + sections[nlist[i].n_sect-1].offset
5197 - sections[nlist[i].n_sect-1].addr
5198 + nlist[i].n_value);
5199 oc->symbols[curSymbol++] = nm;
5206 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
5207 commonCounter = (unsigned long)commonStorage;
5210 for(i=0;i<symLC->nsyms;i++)
5212 if((nlist[i].n_type & N_TYPE) == N_UNDF
5213 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
5215 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5216 unsigned long sz = nlist[i].n_value;
5218 nlist[i].n_value = commonCounter;
5220 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting common symbol: %s\n", nm));
5221 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5222 (void*)commonCounter);
5223 oc->symbols[curSymbol++] = nm;
5225 commonCounter += sz;
5232 static int ocResolve_MachO(ObjectCode* oc)
5234 char *image = (char*) oc->image;
5235 struct mach_header *header = (struct mach_header*) image;
5236 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5238 struct segment_command *segLC = NULL;
5239 struct section *sections;
5240 struct symtab_command *symLC = NULL;
5241 struct dysymtab_command *dsymLC = NULL;
5242 struct nlist *nlist;
5244 IF_DEBUG(linker, debugBelch("ocResolve_MachO: start\n"));
5245 for (i = 0; i < header->ncmds; i++)
5247 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
5248 segLC = (struct segment_command*) lc;
5249 else if(lc->cmd == LC_SYMTAB)
5250 symLC = (struct symtab_command*) lc;
5251 else if(lc->cmd == LC_DYSYMTAB)
5252 dsymLC = (struct dysymtab_command*) lc;
5253 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5256 sections = (struct section*) (segLC+1);
5257 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5262 unsigned long *indirectSyms
5263 = (unsigned long*) (image + dsymLC->indirectsymoff);
5265 IF_DEBUG(linker, debugBelch("ocResolve_MachO: resolving dsymLC\n"));
5266 for (i = 0; i < segLC->nsects; i++)
5268 if( !strcmp(sections[i].sectname,"__la_symbol_ptr")
5269 || !strcmp(sections[i].sectname,"__la_sym_ptr2")
5270 || !strcmp(sections[i].sectname,"__la_sym_ptr3"))
5272 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5275 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr")
5276 || !strcmp(sections[i].sectname,"__pointers"))
5278 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5281 else if(!strcmp(sections[i].sectname,"__jump_table"))
5283 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5288 IF_DEBUG(linker, debugBelch("ocResolve_MachO: unknown section\n"));
5293 for(i=0;i<segLC->nsects;i++)
5295 IF_DEBUG(linker, debugBelch("ocResolve_MachO: relocating section %d\n", i));
5297 if (!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
5301 #if defined (powerpc_HOST_ARCH)
5302 ocFlushInstructionCache( oc );
5308 #ifdef powerpc_HOST_ARCH
5310 * The Mach-O object format uses leading underscores. But not everywhere.
5311 * There is a small number of runtime support functions defined in
5312 * libcc_dynamic.a whose name does not have a leading underscore.
5313 * As a consequence, we can't get their address from C code.
5314 * We have to use inline assembler just to take the address of a function.
5318 extern void* symbolsWithoutUnderscore[];
5320 static void machoInitSymbolsWithoutUnderscore()
5322 void **p = symbolsWithoutUnderscore;
5323 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
5325 #undef SymI_NeedsProto
5326 #define SymI_NeedsProto(x) \
5327 __asm__ volatile(".long " # x);
5329 RTS_MACHO_NOUNDERLINE_SYMBOLS
5331 __asm__ volatile(".text");
5333 #undef SymI_NeedsProto
5334 #define SymI_NeedsProto(x) \
5335 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
5337 RTS_MACHO_NOUNDERLINE_SYMBOLS
5339 #undef SymI_NeedsProto
5345 * Figure out by how much to shift the entire Mach-O file in memory
5346 * when loading so that its single segment ends up 16-byte-aligned
5348 static int machoGetMisalignment( FILE * f )
5350 struct mach_header header;
5354 int n = fread(&header, sizeof(header), 1, f);
5356 barf("machoGetMisalignment: can't read the Mach-O header");
5359 fseek(f, -sizeof(header), SEEK_CUR);
5361 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
5362 if(header.magic != MH_MAGIC_64) {
5363 barf("Bad magic. Expected: %08x, got: %08x.",
5364 MH_MAGIC_64, header.magic);
5367 if(header.magic != MH_MAGIC) {
5368 barf("Bad magic. Expected: %08x, got: %08x.",
5369 MH_MAGIC, header.magic);
5373 misalignment = (header.sizeofcmds + sizeof(header))
5376 return misalignment ? (16 - misalignment) : 0;