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>
49 #ifdef HAVE_SYS_STAT_H
53 #if defined(HAVE_DLFCN_H)
57 #if defined(cygwin32_HOST_OS)
62 #ifdef HAVE_SYS_TIME_H
66 #include <sys/fcntl.h>
67 #include <sys/termios.h>
68 #include <sys/utime.h>
69 #include <sys/utsname.h>
73 #if defined(linux_HOST_OS ) || defined(freebsd_HOST_OS) || \
74 defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS ) || \
75 defined(openbsd_HOST_OS ) || \
76 ( defined(darwin_HOST_OS ) && !defined(powerpc_HOST_ARCH) ) || \
77 defined(kfreebsdgnu_HOST_OS)
78 /* Don't use mmap on powerpc-apple-darwin as mmap doesn't support
79 * reallocating but we need to allocate jump islands just after each
80 * object images. Otherwise relative branches to jump islands can fail
81 * due to 24-bits displacement overflow.
93 #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)
94 # define OBJFORMAT_ELF
95 # include <regex.h> // regex is already used by dlopen() so this is OK
96 // to use here without requiring an additional lib
97 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
98 # define OBJFORMAT_PEi386
101 #elif defined(darwin_HOST_OS)
102 # define OBJFORMAT_MACHO
104 # include <mach-o/loader.h>
105 # include <mach-o/nlist.h>
106 # include <mach-o/reloc.h>
107 #if !defined(HAVE_DLFCN_H)
108 # include <mach-o/dyld.h>
110 #if defined(powerpc_HOST_ARCH)
111 # include <mach-o/ppc/reloc.h>
113 #if defined(x86_64_HOST_ARCH)
114 # include <mach-o/x86_64/reloc.h>
118 #if defined(x86_64_HOST_ARCH) && defined(darwin_HOST_OS)
122 /* Hash table mapping symbol names to Symbol */
123 static /*Str*/HashTable *symhash;
125 /* Hash table mapping symbol names to StgStablePtr */
126 static /*Str*/HashTable *stablehash;
128 /* List of currently loaded objects */
129 ObjectCode *objects = NULL; /* initially empty */
131 static HsInt loadOc( ObjectCode* oc );
132 static ObjectCode* mkOc( char *path, char *image, int imageSize,
133 char *archiveMemberName
135 #ifdef darwin_HOST_OS
141 #if defined(OBJFORMAT_ELF)
142 static int ocVerifyImage_ELF ( ObjectCode* oc );
143 static int ocGetNames_ELF ( ObjectCode* oc );
144 static int ocResolve_ELF ( ObjectCode* oc );
145 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
146 static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
148 #elif defined(OBJFORMAT_PEi386)
149 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
150 static int ocGetNames_PEi386 ( ObjectCode* oc );
151 static int ocResolve_PEi386 ( ObjectCode* oc );
152 static void *lookupSymbolInDLLs ( unsigned char *lbl );
153 static void zapTrailingAtSign ( unsigned char *sym );
154 #elif defined(OBJFORMAT_MACHO)
155 static int ocVerifyImage_MachO ( ObjectCode* oc );
156 static int ocGetNames_MachO ( ObjectCode* oc );
157 static int ocResolve_MachO ( ObjectCode* oc );
160 static int machoGetMisalignment( FILE * );
162 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
163 static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
165 #ifdef powerpc_HOST_ARCH
166 static void machoInitSymbolsWithoutUnderscore( void );
170 /* on x86_64 we have a problem with relocating symbol references in
171 * code that was compiled without -fPIC. By default, the small memory
172 * model is used, which assumes that symbol references can fit in a
173 * 32-bit slot. The system dynamic linker makes this work for
174 * references to shared libraries by either (a) allocating a jump
175 * table slot for code references, or (b) moving the symbol at load
176 * time (and copying its contents, if necessary) for data references.
178 * We unfortunately can't tell whether symbol references are to code
179 * or data. So for now we assume they are code (the vast majority
180 * are), and allocate jump-table slots. Unfortunately this will
181 * SILENTLY generate crashing code for data references. This hack is
182 * enabled by X86_64_ELF_NONPIC_HACK.
184 * One workaround is to use shared Haskell libraries. This is
185 * coming. Another workaround is to keep the static libraries but
186 * compile them with -fPIC, because that will generate PIC references
187 * to data which can be relocated. The PIC code is still too green to
188 * do this systematically, though.
191 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
193 * Naming Scheme for Symbol Macros
195 * SymI_*: symbol is internal to the RTS. It resides in an object
196 * file/library that is statically.
197 * SymE_*: symbol is external to the RTS library. It might be linked
200 * Sym*_HasProto : the symbol prototype is imported in an include file
201 * or defined explicitly
202 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
203 * default proto extern void sym(void);
205 #define X86_64_ELF_NONPIC_HACK 1
207 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
208 * small memory model on this architecture (see gcc docs,
211 * MAP_32BIT not available on OpenBSD/amd64
213 #if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT)
214 #define TRY_MAP_32BIT MAP_32BIT
216 #define TRY_MAP_32BIT 0
220 * Due to the small memory model (see above), on x86_64 we have to map
221 * all our non-PIC object files into the low 2Gb of the address space
222 * (why 2Gb and not 4Gb? Because all addresses must be reachable
223 * using a 32-bit signed PC-relative offset). On Linux we can do this
224 * using the MAP_32BIT flag to mmap(), however on other OSs
225 * (e.g. *BSD, see #2063, and also on Linux inside Xen, see #2512), we
226 * can't do this. So on these systems, we have to pick a base address
227 * in the low 2Gb of the address space and try to allocate memory from
230 * We pick a default address based on the OS, but also make this
231 * configurable via an RTS flag (+RTS -xm)
233 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
235 #if defined(MAP_32BIT)
236 // Try to use MAP_32BIT
237 #define MMAP_32BIT_BASE_DEFAULT 0
240 #define MMAP_32BIT_BASE_DEFAULT 0x40000000
243 static void *mmap_32bit_base = (void *)MMAP_32BIT_BASE_DEFAULT;
246 /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */
247 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
248 #define MAP_ANONYMOUS MAP_ANON
251 /* -----------------------------------------------------------------------------
252 * Built-in symbols from the RTS
255 typedef struct _RtsSymbolVal {
260 #define Maybe_Stable_Names SymI_HasProto(stg_mkWeakzh) \
261 SymI_HasProto(stg_mkWeakForeignEnvzh) \
262 SymI_HasProto(stg_makeStableNamezh) \
263 SymI_HasProto(stg_finalizzeWeakzh)
265 #if !defined (mingw32_HOST_OS)
266 #define RTS_POSIX_ONLY_SYMBOLS \
267 SymI_HasProto(__hscore_get_saved_termios) \
268 SymI_HasProto(__hscore_set_saved_termios) \
269 SymI_HasProto(shutdownHaskellAndSignal) \
270 SymI_HasProto(lockFile) \
271 SymI_HasProto(unlockFile) \
272 SymI_HasProto(signal_handlers) \
273 SymI_HasProto(stg_sig_install) \
274 SymI_HasProto(rtsTimerSignal) \
275 SymI_HasProto(atexit) \
276 SymI_NeedsProto(nocldstop)
279 #if defined (cygwin32_HOST_OS)
280 #define RTS_MINGW_ONLY_SYMBOLS /**/
281 /* Don't have the ability to read import libs / archives, so
282 * we have to stupidly list a lot of what libcygwin.a
285 #define RTS_CYGWIN_ONLY_SYMBOLS \
286 SymI_HasProto(regfree) \
287 SymI_HasProto(regexec) \
288 SymI_HasProto(regerror) \
289 SymI_HasProto(regcomp) \
290 SymI_HasProto(__errno) \
291 SymI_HasProto(access) \
292 SymI_HasProto(chmod) \
293 SymI_HasProto(chdir) \
294 SymI_HasProto(close) \
295 SymI_HasProto(creat) \
297 SymI_HasProto(dup2) \
298 SymI_HasProto(fstat) \
299 SymI_HasProto(fcntl) \
300 SymI_HasProto(getcwd) \
301 SymI_HasProto(getenv) \
302 SymI_HasProto(lseek) \
303 SymI_HasProto(open) \
304 SymI_HasProto(fpathconf) \
305 SymI_HasProto(pathconf) \
306 SymI_HasProto(stat) \
308 SymI_HasProto(tanh) \
309 SymI_HasProto(cosh) \
310 SymI_HasProto(sinh) \
311 SymI_HasProto(atan) \
312 SymI_HasProto(acos) \
313 SymI_HasProto(asin) \
319 SymI_HasProto(sqrt) \
320 SymI_HasProto(localtime_r) \
321 SymI_HasProto(gmtime_r) \
322 SymI_HasProto(mktime) \
323 SymI_NeedsProto(_imp___tzname) \
324 SymI_HasProto(gettimeofday) \
325 SymI_HasProto(timezone) \
326 SymI_HasProto(tcgetattr) \
327 SymI_HasProto(tcsetattr) \
328 SymI_HasProto(memcpy) \
329 SymI_HasProto(memmove) \
330 SymI_HasProto(realloc) \
331 SymI_HasProto(malloc) \
332 SymI_HasProto(free) \
333 SymI_HasProto(fork) \
334 SymI_HasProto(lstat) \
335 SymI_HasProto(isatty) \
336 SymI_HasProto(mkdir) \
337 SymI_HasProto(opendir) \
338 SymI_HasProto(readdir) \
339 SymI_HasProto(rewinddir) \
340 SymI_HasProto(closedir) \
341 SymI_HasProto(link) \
342 SymI_HasProto(mkfifo) \
343 SymI_HasProto(pipe) \
344 SymI_HasProto(read) \
345 SymI_HasProto(rename) \
346 SymI_HasProto(rmdir) \
347 SymI_HasProto(select) \
348 SymI_HasProto(system) \
349 SymI_HasProto(write) \
350 SymI_HasProto(strcmp) \
351 SymI_HasProto(strcpy) \
352 SymI_HasProto(strncpy) \
353 SymI_HasProto(strerror) \
354 SymI_HasProto(sigaddset) \
355 SymI_HasProto(sigemptyset) \
356 SymI_HasProto(sigprocmask) \
357 SymI_HasProto(umask) \
358 SymI_HasProto(uname) \
359 SymI_HasProto(unlink) \
360 SymI_HasProto(utime) \
361 SymI_HasProto(waitpid)
363 #elif !defined(mingw32_HOST_OS)
364 #define RTS_MINGW_ONLY_SYMBOLS /**/
365 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
366 #else /* defined(mingw32_HOST_OS) */
367 #define RTS_POSIX_ONLY_SYMBOLS /**/
368 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
370 #if HAVE_GETTIMEOFDAY
371 #define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
373 #define RTS_MINGW_GETTIMEOFDAY_SYM /**/
376 #if HAVE___MINGW_VFPRINTF
377 #define RTS___MINGW_VFPRINTF_SYM SymI_HasProto(__mingw_vfprintf)
379 #define RTS___MINGW_VFPRINTF_SYM /**/
382 /* These are statically linked from the mingw libraries into the ghc
383 executable, so we have to employ this hack. */
384 #define RTS_MINGW_ONLY_SYMBOLS \
385 SymI_HasProto(stg_asyncReadzh) \
386 SymI_HasProto(stg_asyncWritezh) \
387 SymI_HasProto(stg_asyncDoProczh) \
388 SymI_HasProto(memset) \
389 SymI_HasProto(inet_ntoa) \
390 SymI_HasProto(inet_addr) \
391 SymI_HasProto(htonl) \
392 SymI_HasProto(recvfrom) \
393 SymI_HasProto(listen) \
394 SymI_HasProto(bind) \
395 SymI_HasProto(shutdown) \
396 SymI_HasProto(connect) \
397 SymI_HasProto(htons) \
398 SymI_HasProto(ntohs) \
399 SymI_HasProto(getservbyname) \
400 SymI_HasProto(getservbyport) \
401 SymI_HasProto(getprotobynumber) \
402 SymI_HasProto(getprotobyname) \
403 SymI_HasProto(gethostbyname) \
404 SymI_HasProto(gethostbyaddr) \
405 SymI_HasProto(gethostname) \
406 SymI_HasProto(strcpy) \
407 SymI_HasProto(strncpy) \
408 SymI_HasProto(abort) \
409 SymI_NeedsProto(_alloca) \
410 SymI_HasProto(isxdigit) \
411 SymI_HasProto(isupper) \
412 SymI_HasProto(ispunct) \
413 SymI_HasProto(islower) \
414 SymI_HasProto(isspace) \
415 SymI_HasProto(isprint) \
416 SymI_HasProto(isdigit) \
417 SymI_HasProto(iscntrl) \
418 SymI_HasProto(isalpha) \
419 SymI_HasProto(isalnum) \
420 SymI_HasProto(isascii) \
421 RTS___MINGW_VFPRINTF_SYM \
422 SymI_HasProto(strcmp) \
423 SymI_HasProto(memmove) \
424 SymI_HasProto(realloc) \
425 SymI_HasProto(malloc) \
427 SymI_HasProto(tanh) \
428 SymI_HasProto(cosh) \
429 SymI_HasProto(sinh) \
430 SymI_HasProto(atan) \
431 SymI_HasProto(acos) \
432 SymI_HasProto(asin) \
438 SymI_HasProto(sqrt) \
439 SymI_HasProto(powf) \
440 SymI_HasProto(tanhf) \
441 SymI_HasProto(coshf) \
442 SymI_HasProto(sinhf) \
443 SymI_HasProto(atanf) \
444 SymI_HasProto(acosf) \
445 SymI_HasProto(asinf) \
446 SymI_HasProto(tanf) \
447 SymI_HasProto(cosf) \
448 SymI_HasProto(sinf) \
449 SymI_HasProto(expf) \
450 SymI_HasProto(logf) \
451 SymI_HasProto(sqrtf) \
453 SymI_HasProto(erfc) \
454 SymI_HasProto(erff) \
455 SymI_HasProto(erfcf) \
456 SymI_HasProto(memcpy) \
457 SymI_HasProto(rts_InstallConsoleEvent) \
458 SymI_HasProto(rts_ConsoleHandlerDone) \
459 SymI_NeedsProto(mktime) \
460 SymI_NeedsProto(_imp___timezone) \
461 SymI_NeedsProto(_imp___tzname) \
462 SymI_NeedsProto(_imp__tzname) \
463 SymI_NeedsProto(_imp___iob) \
464 SymI_NeedsProto(_imp___osver) \
465 SymI_NeedsProto(localtime) \
466 SymI_NeedsProto(gmtime) \
467 SymI_NeedsProto(opendir) \
468 SymI_NeedsProto(readdir) \
469 SymI_NeedsProto(rewinddir) \
470 SymI_NeedsProto(_imp____mb_cur_max) \
471 SymI_NeedsProto(_imp___pctype) \
472 SymI_NeedsProto(__chkstk) \
473 RTS_MINGW_GETTIMEOFDAY_SYM \
474 SymI_NeedsProto(closedir)
478 #if defined(darwin_HOST_OS) && HAVE_PRINTF_LDBLSTUB
479 #define RTS_DARWIN_ONLY_SYMBOLS \
480 SymI_NeedsProto(asprintf$LDBLStub) \
481 SymI_NeedsProto(err$LDBLStub) \
482 SymI_NeedsProto(errc$LDBLStub) \
483 SymI_NeedsProto(errx$LDBLStub) \
484 SymI_NeedsProto(fprintf$LDBLStub) \
485 SymI_NeedsProto(fscanf$LDBLStub) \
486 SymI_NeedsProto(fwprintf$LDBLStub) \
487 SymI_NeedsProto(fwscanf$LDBLStub) \
488 SymI_NeedsProto(printf$LDBLStub) \
489 SymI_NeedsProto(scanf$LDBLStub) \
490 SymI_NeedsProto(snprintf$LDBLStub) \
491 SymI_NeedsProto(sprintf$LDBLStub) \
492 SymI_NeedsProto(sscanf$LDBLStub) \
493 SymI_NeedsProto(strtold$LDBLStub) \
494 SymI_NeedsProto(swprintf$LDBLStub) \
495 SymI_NeedsProto(swscanf$LDBLStub) \
496 SymI_NeedsProto(syslog$LDBLStub) \
497 SymI_NeedsProto(vasprintf$LDBLStub) \
498 SymI_NeedsProto(verr$LDBLStub) \
499 SymI_NeedsProto(verrc$LDBLStub) \
500 SymI_NeedsProto(verrx$LDBLStub) \
501 SymI_NeedsProto(vfprintf$LDBLStub) \
502 SymI_NeedsProto(vfscanf$LDBLStub) \
503 SymI_NeedsProto(vfwprintf$LDBLStub) \
504 SymI_NeedsProto(vfwscanf$LDBLStub) \
505 SymI_NeedsProto(vprintf$LDBLStub) \
506 SymI_NeedsProto(vscanf$LDBLStub) \
507 SymI_NeedsProto(vsnprintf$LDBLStub) \
508 SymI_NeedsProto(vsprintf$LDBLStub) \
509 SymI_NeedsProto(vsscanf$LDBLStub) \
510 SymI_NeedsProto(vswprintf$LDBLStub) \
511 SymI_NeedsProto(vswscanf$LDBLStub) \
512 SymI_NeedsProto(vsyslog$LDBLStub) \
513 SymI_NeedsProto(vwarn$LDBLStub) \
514 SymI_NeedsProto(vwarnc$LDBLStub) \
515 SymI_NeedsProto(vwarnx$LDBLStub) \
516 SymI_NeedsProto(vwprintf$LDBLStub) \
517 SymI_NeedsProto(vwscanf$LDBLStub) \
518 SymI_NeedsProto(warn$LDBLStub) \
519 SymI_NeedsProto(warnc$LDBLStub) \
520 SymI_NeedsProto(warnx$LDBLStub) \
521 SymI_NeedsProto(wcstold$LDBLStub) \
522 SymI_NeedsProto(wprintf$LDBLStub) \
523 SymI_NeedsProto(wscanf$LDBLStub)
525 #define RTS_DARWIN_ONLY_SYMBOLS
529 # define MAIN_CAP_SYM SymI_HasProto(MainCapability)
531 # define MAIN_CAP_SYM
534 #if !defined(mingw32_HOST_OS)
535 #define RTS_USER_SIGNALS_SYMBOLS \
536 SymI_HasProto(setIOManagerControlFd) \
537 SymI_HasProto(setIOManagerWakeupFd) \
538 SymI_HasProto(ioManagerWakeup) \
539 SymI_HasProto(blockUserSignals) \
540 SymI_HasProto(unblockUserSignals)
542 #define RTS_USER_SIGNALS_SYMBOLS \
543 SymI_HasProto(ioManagerWakeup) \
544 SymI_HasProto(sendIOManagerEvent) \
545 SymI_HasProto(readIOManagerEvent) \
546 SymI_HasProto(getIOManagerEvent) \
547 SymI_HasProto(console_handler)
550 #define RTS_LIBFFI_SYMBOLS \
551 SymE_NeedsProto(ffi_prep_cif) \
552 SymE_NeedsProto(ffi_call) \
553 SymE_NeedsProto(ffi_type_void) \
554 SymE_NeedsProto(ffi_type_float) \
555 SymE_NeedsProto(ffi_type_double) \
556 SymE_NeedsProto(ffi_type_sint64) \
557 SymE_NeedsProto(ffi_type_uint64) \
558 SymE_NeedsProto(ffi_type_sint32) \
559 SymE_NeedsProto(ffi_type_uint32) \
560 SymE_NeedsProto(ffi_type_sint16) \
561 SymE_NeedsProto(ffi_type_uint16) \
562 SymE_NeedsProto(ffi_type_sint8) \
563 SymE_NeedsProto(ffi_type_uint8) \
564 SymE_NeedsProto(ffi_type_pointer)
566 #ifdef TABLES_NEXT_TO_CODE
567 #define RTS_RET_SYMBOLS /* nothing */
569 #define RTS_RET_SYMBOLS \
570 SymI_HasProto(stg_enter_ret) \
571 SymI_HasProto(stg_gc_fun_ret) \
572 SymI_HasProto(stg_ap_v_ret) \
573 SymI_HasProto(stg_ap_f_ret) \
574 SymI_HasProto(stg_ap_d_ret) \
575 SymI_HasProto(stg_ap_l_ret) \
576 SymI_HasProto(stg_ap_n_ret) \
577 SymI_HasProto(stg_ap_p_ret) \
578 SymI_HasProto(stg_ap_pv_ret) \
579 SymI_HasProto(stg_ap_pp_ret) \
580 SymI_HasProto(stg_ap_ppv_ret) \
581 SymI_HasProto(stg_ap_ppp_ret) \
582 SymI_HasProto(stg_ap_pppv_ret) \
583 SymI_HasProto(stg_ap_pppp_ret) \
584 SymI_HasProto(stg_ap_ppppp_ret) \
585 SymI_HasProto(stg_ap_pppppp_ret)
588 /* Modules compiled with -ticky may mention ticky counters */
589 /* This list should marry up with the one in $(TOP)/includes/stg/Ticky.h */
590 #define RTS_TICKY_SYMBOLS \
591 SymI_NeedsProto(ticky_entry_ctrs) \
592 SymI_NeedsProto(top_ct) \
594 SymI_HasProto(ENT_VIA_NODE_ctr) \
595 SymI_HasProto(ENT_STATIC_THK_ctr) \
596 SymI_HasProto(ENT_DYN_THK_ctr) \
597 SymI_HasProto(ENT_STATIC_FUN_DIRECT_ctr) \
598 SymI_HasProto(ENT_DYN_FUN_DIRECT_ctr) \
599 SymI_HasProto(ENT_STATIC_CON_ctr) \
600 SymI_HasProto(ENT_DYN_CON_ctr) \
601 SymI_HasProto(ENT_STATIC_IND_ctr) \
602 SymI_HasProto(ENT_DYN_IND_ctr) \
603 SymI_HasProto(ENT_PERM_IND_ctr) \
604 SymI_HasProto(ENT_PAP_ctr) \
605 SymI_HasProto(ENT_AP_ctr) \
606 SymI_HasProto(ENT_AP_STACK_ctr) \
607 SymI_HasProto(ENT_BH_ctr) \
608 SymI_HasProto(UNKNOWN_CALL_ctr) \
609 SymI_HasProto(SLOW_CALL_v_ctr) \
610 SymI_HasProto(SLOW_CALL_f_ctr) \
611 SymI_HasProto(SLOW_CALL_d_ctr) \
612 SymI_HasProto(SLOW_CALL_l_ctr) \
613 SymI_HasProto(SLOW_CALL_n_ctr) \
614 SymI_HasProto(SLOW_CALL_p_ctr) \
615 SymI_HasProto(SLOW_CALL_pv_ctr) \
616 SymI_HasProto(SLOW_CALL_pp_ctr) \
617 SymI_HasProto(SLOW_CALL_ppv_ctr) \
618 SymI_HasProto(SLOW_CALL_ppp_ctr) \
619 SymI_HasProto(SLOW_CALL_pppv_ctr) \
620 SymI_HasProto(SLOW_CALL_pppp_ctr) \
621 SymI_HasProto(SLOW_CALL_ppppp_ctr) \
622 SymI_HasProto(SLOW_CALL_pppppp_ctr) \
623 SymI_HasProto(SLOW_CALL_OTHER_ctr) \
624 SymI_HasProto(ticky_slow_call_unevald) \
625 SymI_HasProto(SLOW_CALL_ctr) \
626 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_ctr) \
627 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_CHUNKS_ctr) \
628 SymI_HasProto(KNOWN_CALL_ctr) \
629 SymI_HasProto(KNOWN_CALL_TOO_FEW_ARGS_ctr) \
630 SymI_HasProto(KNOWN_CALL_EXTRA_ARGS_ctr) \
631 SymI_HasProto(SLOW_CALL_FUN_TOO_FEW_ctr) \
632 SymI_HasProto(SLOW_CALL_FUN_CORRECT_ctr) \
633 SymI_HasProto(SLOW_CALL_FUN_TOO_MANY_ctr) \
634 SymI_HasProto(SLOW_CALL_PAP_TOO_FEW_ctr) \
635 SymI_HasProto(SLOW_CALL_PAP_CORRECT_ctr) \
636 SymI_HasProto(SLOW_CALL_PAP_TOO_MANY_ctr) \
637 SymI_HasProto(SLOW_CALL_UNEVALD_ctr) \
638 SymI_HasProto(UPDF_OMITTED_ctr) \
639 SymI_HasProto(UPDF_PUSHED_ctr) \
640 SymI_HasProto(CATCHF_PUSHED_ctr) \
641 SymI_HasProto(UPDF_RCC_PUSHED_ctr) \
642 SymI_HasProto(UPDF_RCC_OMITTED_ctr) \
643 SymI_HasProto(UPD_SQUEEZED_ctr) \
644 SymI_HasProto(UPD_CON_IN_NEW_ctr) \
645 SymI_HasProto(UPD_CON_IN_PLACE_ctr) \
646 SymI_HasProto(UPD_PAP_IN_NEW_ctr) \
647 SymI_HasProto(UPD_PAP_IN_PLACE_ctr) \
648 SymI_HasProto(ALLOC_HEAP_ctr) \
649 SymI_HasProto(ALLOC_HEAP_tot) \
650 SymI_HasProto(ALLOC_FUN_ctr) \
651 SymI_HasProto(ALLOC_FUN_adm) \
652 SymI_HasProto(ALLOC_FUN_gds) \
653 SymI_HasProto(ALLOC_FUN_slp) \
654 SymI_HasProto(UPD_NEW_IND_ctr) \
655 SymI_HasProto(UPD_NEW_PERM_IND_ctr) \
656 SymI_HasProto(UPD_OLD_IND_ctr) \
657 SymI_HasProto(UPD_OLD_PERM_IND_ctr) \
658 SymI_HasProto(UPD_BH_UPDATABLE_ctr) \
659 SymI_HasProto(UPD_BH_SINGLE_ENTRY_ctr) \
660 SymI_HasProto(UPD_CAF_BH_UPDATABLE_ctr) \
661 SymI_HasProto(UPD_CAF_BH_SINGLE_ENTRY_ctr) \
662 SymI_HasProto(GC_SEL_ABANDONED_ctr) \
663 SymI_HasProto(GC_SEL_MINOR_ctr) \
664 SymI_HasProto(GC_SEL_MAJOR_ctr) \
665 SymI_HasProto(GC_FAILED_PROMOTION_ctr) \
666 SymI_HasProto(ALLOC_UP_THK_ctr) \
667 SymI_HasProto(ALLOC_SE_THK_ctr) \
668 SymI_HasProto(ALLOC_THK_adm) \
669 SymI_HasProto(ALLOC_THK_gds) \
670 SymI_HasProto(ALLOC_THK_slp) \
671 SymI_HasProto(ALLOC_CON_ctr) \
672 SymI_HasProto(ALLOC_CON_adm) \
673 SymI_HasProto(ALLOC_CON_gds) \
674 SymI_HasProto(ALLOC_CON_slp) \
675 SymI_HasProto(ALLOC_TUP_ctr) \
676 SymI_HasProto(ALLOC_TUP_adm) \
677 SymI_HasProto(ALLOC_TUP_gds) \
678 SymI_HasProto(ALLOC_TUP_slp) \
679 SymI_HasProto(ALLOC_BH_ctr) \
680 SymI_HasProto(ALLOC_BH_adm) \
681 SymI_HasProto(ALLOC_BH_gds) \
682 SymI_HasProto(ALLOC_BH_slp) \
683 SymI_HasProto(ALLOC_PRIM_ctr) \
684 SymI_HasProto(ALLOC_PRIM_adm) \
685 SymI_HasProto(ALLOC_PRIM_gds) \
686 SymI_HasProto(ALLOC_PRIM_slp) \
687 SymI_HasProto(ALLOC_PAP_ctr) \
688 SymI_HasProto(ALLOC_PAP_adm) \
689 SymI_HasProto(ALLOC_PAP_gds) \
690 SymI_HasProto(ALLOC_PAP_slp) \
691 SymI_HasProto(ALLOC_TSO_ctr) \
692 SymI_HasProto(ALLOC_TSO_adm) \
693 SymI_HasProto(ALLOC_TSO_gds) \
694 SymI_HasProto(ALLOC_TSO_slp) \
695 SymI_HasProto(RET_NEW_ctr) \
696 SymI_HasProto(RET_OLD_ctr) \
697 SymI_HasProto(RET_UNBOXED_TUP_ctr) \
698 SymI_HasProto(RET_SEMI_loads_avoided)
701 // On most platforms, the garbage collector rewrites references
702 // to small integer and char objects to a set of common, shared ones.
704 // We don't do this when compiling to Windows DLLs at the moment because
705 // it doesn't support cross package data references well.
707 #if defined(__PIC__) && defined(mingw32_HOST_OS)
708 #define RTS_INTCHAR_SYMBOLS
710 #define RTS_INTCHAR_SYMBOLS \
711 SymI_HasProto(stg_CHARLIKE_closure) \
712 SymI_HasProto(stg_INTLIKE_closure)
716 #define RTS_SYMBOLS \
719 SymI_HasProto(StgReturn) \
720 SymI_HasProto(stg_enter_info) \
721 SymI_HasProto(stg_gc_void_info) \
722 SymI_HasProto(__stg_gc_enter_1) \
723 SymI_HasProto(stg_gc_noregs) \
724 SymI_HasProto(stg_gc_unpt_r1_info) \
725 SymI_HasProto(stg_gc_unpt_r1) \
726 SymI_HasProto(stg_gc_unbx_r1_info) \
727 SymI_HasProto(stg_gc_unbx_r1) \
728 SymI_HasProto(stg_gc_f1_info) \
729 SymI_HasProto(stg_gc_f1) \
730 SymI_HasProto(stg_gc_d1_info) \
731 SymI_HasProto(stg_gc_d1) \
732 SymI_HasProto(stg_gc_l1_info) \
733 SymI_HasProto(stg_gc_l1) \
734 SymI_HasProto(__stg_gc_fun) \
735 SymI_HasProto(stg_gc_fun_info) \
736 SymI_HasProto(stg_gc_gen) \
737 SymI_HasProto(stg_gc_gen_info) \
738 SymI_HasProto(stg_gc_gen_hp) \
739 SymI_HasProto(stg_gc_ut) \
740 SymI_HasProto(stg_gen_yield) \
741 SymI_HasProto(stg_yield_noregs) \
742 SymI_HasProto(stg_yield_to_interpreter) \
743 SymI_HasProto(stg_gen_block) \
744 SymI_HasProto(stg_block_noregs) \
745 SymI_HasProto(stg_block_1) \
746 SymI_HasProto(stg_block_takemvar) \
747 SymI_HasProto(stg_block_putmvar) \
749 SymI_HasProto(MallocFailHook) \
750 SymI_HasProto(OnExitHook) \
751 SymI_HasProto(OutOfHeapHook) \
752 SymI_HasProto(StackOverflowHook) \
753 SymI_HasProto(addDLL) \
754 SymI_HasProto(__int_encodeDouble) \
755 SymI_HasProto(__word_encodeDouble) \
756 SymI_HasProto(__2Int_encodeDouble) \
757 SymI_HasProto(__int_encodeFloat) \
758 SymI_HasProto(__word_encodeFloat) \
759 SymI_HasProto(stg_atomicallyzh) \
760 SymI_HasProto(barf) \
761 SymI_HasProto(debugBelch) \
762 SymI_HasProto(errorBelch) \
763 SymI_HasProto(sysErrorBelch) \
764 SymI_HasProto(stg_getMaskingStatezh) \
765 SymI_HasProto(stg_maskAsyncExceptionszh) \
766 SymI_HasProto(stg_maskUninterruptiblezh) \
767 SymI_HasProto(stg_catchzh) \
768 SymI_HasProto(stg_catchRetryzh) \
769 SymI_HasProto(stg_catchSTMzh) \
770 SymI_HasProto(stg_checkzh) \
771 SymI_HasProto(closure_flags) \
772 SymI_HasProto(cmp_thread) \
773 SymI_HasProto(createAdjustor) \
774 SymI_HasProto(stg_decodeDoublezu2Intzh) \
775 SymI_HasProto(stg_decodeFloatzuIntzh) \
776 SymI_HasProto(defaultsHook) \
777 SymI_HasProto(stg_delayzh) \
778 SymI_HasProto(stg_deRefWeakzh) \
779 SymI_HasProto(stg_deRefStablePtrzh) \
780 SymI_HasProto(dirty_MUT_VAR) \
781 SymI_HasProto(stg_forkzh) \
782 SymI_HasProto(stg_forkOnzh) \
783 SymI_HasProto(forkProcess) \
784 SymI_HasProto(forkOS_createThread) \
785 SymI_HasProto(freeHaskellFunctionPtr) \
786 SymI_HasProto(getOrSetTypeableStore) \
787 SymI_HasProto(getOrSetGHCConcSignalSignalHandlerStore) \
788 SymI_HasProto(getOrSetGHCConcWindowsPendingDelaysStore) \
789 SymI_HasProto(getOrSetGHCConcWindowsIOManagerThreadStore) \
790 SymI_HasProto(getOrSetGHCConcWindowsProddingStore) \
791 SymI_HasProto(getOrSetSystemEventThreadEventManagerStore) \
792 SymI_HasProto(getOrSetSystemEventThreadIOManagerThreadStore) \
793 SymI_HasProto(genSymZh) \
794 SymI_HasProto(genericRaise) \
795 SymI_HasProto(getProgArgv) \
796 SymI_HasProto(getFullProgArgv) \
797 SymI_HasProto(getStablePtr) \
798 SymI_HasProto(hs_init) \
799 SymI_HasProto(hs_exit) \
800 SymI_HasProto(hs_set_argv) \
801 SymI_HasProto(hs_add_root) \
802 SymI_HasProto(hs_perform_gc) \
803 SymI_HasProto(hs_free_stable_ptr) \
804 SymI_HasProto(hs_free_fun_ptr) \
805 SymI_HasProto(hs_hpc_rootModule) \
806 SymI_HasProto(hs_hpc_module) \
807 SymI_HasProto(initLinker) \
808 SymI_HasProto(stg_unpackClosurezh) \
809 SymI_HasProto(stg_getApStackValzh) \
810 SymI_HasProto(stg_getSparkzh) \
811 SymI_HasProto(stg_numSparkszh) \
812 SymI_HasProto(stg_isCurrentThreadBoundzh) \
813 SymI_HasProto(stg_isEmptyMVarzh) \
814 SymI_HasProto(stg_killThreadzh) \
815 SymI_HasProto(loadArchive) \
816 SymI_HasProto(loadObj) \
817 SymI_HasProto(insertStableSymbol) \
818 SymI_HasProto(insertSymbol) \
819 SymI_HasProto(lookupSymbol) \
820 SymI_HasProto(stg_makeStablePtrzh) \
821 SymI_HasProto(stg_mkApUpd0zh) \
822 SymI_HasProto(stg_myThreadIdzh) \
823 SymI_HasProto(stg_labelThreadzh) \
824 SymI_HasProto(stg_newArrayzh) \
825 SymI_HasProto(stg_newBCOzh) \
826 SymI_HasProto(stg_newByteArrayzh) \
827 SymI_HasProto_redirect(newCAF, newDynCAF) \
828 SymI_HasProto(stg_newMVarzh) \
829 SymI_HasProto(stg_newMutVarzh) \
830 SymI_HasProto(stg_newTVarzh) \
831 SymI_HasProto(stg_noDuplicatezh) \
832 SymI_HasProto(stg_atomicModifyMutVarzh) \
833 SymI_HasProto(stg_newPinnedByteArrayzh) \
834 SymI_HasProto(stg_newAlignedPinnedByteArrayzh) \
835 SymI_HasProto(newSpark) \
836 SymI_HasProto(performGC) \
837 SymI_HasProto(performMajorGC) \
838 SymI_HasProto(prog_argc) \
839 SymI_HasProto(prog_argv) \
840 SymI_HasProto(stg_putMVarzh) \
841 SymI_HasProto(stg_raisezh) \
842 SymI_HasProto(stg_raiseIOzh) \
843 SymI_HasProto(stg_readTVarzh) \
844 SymI_HasProto(stg_readTVarIOzh) \
845 SymI_HasProto(resumeThread) \
846 SymI_HasProto(resolveObjs) \
847 SymI_HasProto(stg_retryzh) \
848 SymI_HasProto(rts_apply) \
849 SymI_HasProto(rts_checkSchedStatus) \
850 SymI_HasProto(rts_eval) \
851 SymI_HasProto(rts_evalIO) \
852 SymI_HasProto(rts_evalLazyIO) \
853 SymI_HasProto(rts_evalStableIO) \
854 SymI_HasProto(rts_eval_) \
855 SymI_HasProto(rts_getBool) \
856 SymI_HasProto(rts_getChar) \
857 SymI_HasProto(rts_getDouble) \
858 SymI_HasProto(rts_getFloat) \
859 SymI_HasProto(rts_getInt) \
860 SymI_HasProto(rts_getInt8) \
861 SymI_HasProto(rts_getInt16) \
862 SymI_HasProto(rts_getInt32) \
863 SymI_HasProto(rts_getInt64) \
864 SymI_HasProto(rts_getPtr) \
865 SymI_HasProto(rts_getFunPtr) \
866 SymI_HasProto(rts_getStablePtr) \
867 SymI_HasProto(rts_getThreadId) \
868 SymI_HasProto(rts_getWord) \
869 SymI_HasProto(rts_getWord8) \
870 SymI_HasProto(rts_getWord16) \
871 SymI_HasProto(rts_getWord32) \
872 SymI_HasProto(rts_getWord64) \
873 SymI_HasProto(rts_lock) \
874 SymI_HasProto(rts_mkBool) \
875 SymI_HasProto(rts_mkChar) \
876 SymI_HasProto(rts_mkDouble) \
877 SymI_HasProto(rts_mkFloat) \
878 SymI_HasProto(rts_mkInt) \
879 SymI_HasProto(rts_mkInt8) \
880 SymI_HasProto(rts_mkInt16) \
881 SymI_HasProto(rts_mkInt32) \
882 SymI_HasProto(rts_mkInt64) \
883 SymI_HasProto(rts_mkPtr) \
884 SymI_HasProto(rts_mkFunPtr) \
885 SymI_HasProto(rts_mkStablePtr) \
886 SymI_HasProto(rts_mkString) \
887 SymI_HasProto(rts_mkWord) \
888 SymI_HasProto(rts_mkWord8) \
889 SymI_HasProto(rts_mkWord16) \
890 SymI_HasProto(rts_mkWord32) \
891 SymI_HasProto(rts_mkWord64) \
892 SymI_HasProto(rts_unlock) \
893 SymI_HasProto(rts_unsafeGetMyCapability) \
894 SymI_HasProto(rtsSupportsBoundThreads) \
895 SymI_HasProto(rts_isProfiled) \
896 SymI_HasProto(setProgArgv) \
897 SymI_HasProto(startupHaskell) \
898 SymI_HasProto(shutdownHaskell) \
899 SymI_HasProto(shutdownHaskellAndExit) \
900 SymI_HasProto(stable_ptr_table) \
901 SymI_HasProto(stackOverflow) \
902 SymI_HasProto(stg_CAF_BLACKHOLE_info) \
903 SymI_HasProto(stg_BLACKHOLE_info) \
904 SymI_HasProto(__stg_EAGER_BLACKHOLE_info) \
905 SymI_HasProto(stg_BLOCKING_QUEUE_CLEAN_info) \
906 SymI_HasProto(stg_BLOCKING_QUEUE_DIRTY_info) \
907 SymI_HasProto(startTimer) \
908 SymI_HasProto(stg_MVAR_CLEAN_info) \
909 SymI_HasProto(stg_MVAR_DIRTY_info) \
910 SymI_HasProto(stg_IND_STATIC_info) \
911 SymI_HasProto(stg_ARR_WORDS_info) \
912 SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info) \
913 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info) \
914 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info) \
915 SymI_HasProto(stg_WEAK_info) \
916 SymI_HasProto(stg_ap_v_info) \
917 SymI_HasProto(stg_ap_f_info) \
918 SymI_HasProto(stg_ap_d_info) \
919 SymI_HasProto(stg_ap_l_info) \
920 SymI_HasProto(stg_ap_n_info) \
921 SymI_HasProto(stg_ap_p_info) \
922 SymI_HasProto(stg_ap_pv_info) \
923 SymI_HasProto(stg_ap_pp_info) \
924 SymI_HasProto(stg_ap_ppv_info) \
925 SymI_HasProto(stg_ap_ppp_info) \
926 SymI_HasProto(stg_ap_pppv_info) \
927 SymI_HasProto(stg_ap_pppp_info) \
928 SymI_HasProto(stg_ap_ppppp_info) \
929 SymI_HasProto(stg_ap_pppppp_info) \
930 SymI_HasProto(stg_ap_0_fast) \
931 SymI_HasProto(stg_ap_v_fast) \
932 SymI_HasProto(stg_ap_f_fast) \
933 SymI_HasProto(stg_ap_d_fast) \
934 SymI_HasProto(stg_ap_l_fast) \
935 SymI_HasProto(stg_ap_n_fast) \
936 SymI_HasProto(stg_ap_p_fast) \
937 SymI_HasProto(stg_ap_pv_fast) \
938 SymI_HasProto(stg_ap_pp_fast) \
939 SymI_HasProto(stg_ap_ppv_fast) \
940 SymI_HasProto(stg_ap_ppp_fast) \
941 SymI_HasProto(stg_ap_pppv_fast) \
942 SymI_HasProto(stg_ap_pppp_fast) \
943 SymI_HasProto(stg_ap_ppppp_fast) \
944 SymI_HasProto(stg_ap_pppppp_fast) \
945 SymI_HasProto(stg_ap_1_upd_info) \
946 SymI_HasProto(stg_ap_2_upd_info) \
947 SymI_HasProto(stg_ap_3_upd_info) \
948 SymI_HasProto(stg_ap_4_upd_info) \
949 SymI_HasProto(stg_ap_5_upd_info) \
950 SymI_HasProto(stg_ap_6_upd_info) \
951 SymI_HasProto(stg_ap_7_upd_info) \
952 SymI_HasProto(stg_exit) \
953 SymI_HasProto(stg_sel_0_upd_info) \
954 SymI_HasProto(stg_sel_10_upd_info) \
955 SymI_HasProto(stg_sel_11_upd_info) \
956 SymI_HasProto(stg_sel_12_upd_info) \
957 SymI_HasProto(stg_sel_13_upd_info) \
958 SymI_HasProto(stg_sel_14_upd_info) \
959 SymI_HasProto(stg_sel_15_upd_info) \
960 SymI_HasProto(stg_sel_1_upd_info) \
961 SymI_HasProto(stg_sel_2_upd_info) \
962 SymI_HasProto(stg_sel_3_upd_info) \
963 SymI_HasProto(stg_sel_4_upd_info) \
964 SymI_HasProto(stg_sel_5_upd_info) \
965 SymI_HasProto(stg_sel_6_upd_info) \
966 SymI_HasProto(stg_sel_7_upd_info) \
967 SymI_HasProto(stg_sel_8_upd_info) \
968 SymI_HasProto(stg_sel_9_upd_info) \
969 SymI_HasProto(stg_upd_frame_info) \
970 SymI_HasProto(stg_bh_upd_frame_info) \
971 SymI_HasProto(suspendThread) \
972 SymI_HasProto(stg_takeMVarzh) \
973 SymI_HasProto(stg_threadStatuszh) \
974 SymI_HasProto(stg_tryPutMVarzh) \
975 SymI_HasProto(stg_tryTakeMVarzh) \
976 SymI_HasProto(stg_unmaskAsyncExceptionszh) \
977 SymI_HasProto(unloadObj) \
978 SymI_HasProto(stg_unsafeThawArrayzh) \
979 SymI_HasProto(stg_waitReadzh) \
980 SymI_HasProto(stg_waitWritezh) \
981 SymI_HasProto(stg_writeTVarzh) \
982 SymI_HasProto(stg_yieldzh) \
983 SymI_NeedsProto(stg_interp_constr_entry) \
984 SymI_HasProto(stg_arg_bitmaps) \
985 SymI_HasProto(large_alloc_lim) \
987 SymI_HasProto(allocate) \
988 SymI_HasProto(allocateExec) \
989 SymI_HasProto(freeExec) \
990 SymI_HasProto(getAllocations) \
991 SymI_HasProto(revertCAFs) \
992 SymI_HasProto(RtsFlags) \
993 SymI_NeedsProto(rts_breakpoint_io_action) \
994 SymI_NeedsProto(rts_stop_next_breakpoint) \
995 SymI_NeedsProto(rts_stop_on_exception) \
996 SymI_HasProto(stopTimer) \
997 SymI_HasProto(n_capabilities) \
998 SymI_HasProto(stg_traceCcszh) \
999 SymI_HasProto(stg_traceEventzh) \
1000 RTS_USER_SIGNALS_SYMBOLS \
1004 // 64-bit support functions in libgcc.a
1005 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
1006 #define RTS_LIBGCC_SYMBOLS \
1007 SymI_NeedsProto(__divdi3) \
1008 SymI_NeedsProto(__udivdi3) \
1009 SymI_NeedsProto(__moddi3) \
1010 SymI_NeedsProto(__umoddi3) \
1011 SymI_NeedsProto(__muldi3) \
1012 SymI_NeedsProto(__ashldi3) \
1013 SymI_NeedsProto(__ashrdi3) \
1014 SymI_NeedsProto(__lshrdi3)
1016 #define RTS_LIBGCC_SYMBOLS
1019 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
1020 // Symbols that don't have a leading underscore
1021 // on Mac OS X. They have to receive special treatment,
1022 // see machoInitSymbolsWithoutUnderscore()
1023 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
1024 SymI_NeedsProto(saveFP) \
1025 SymI_NeedsProto(restFP)
1028 /* entirely bogus claims about types of these symbols */
1029 #define SymI_NeedsProto(vvv) extern void vvv(void);
1030 #if defined(__PIC__) && defined(mingw32_HOST_OS)
1031 #define SymE_HasProto(vvv) SymE_HasProto(vvv);
1032 #define SymE_NeedsProto(vvv) extern void _imp__ ## vvv (void);
1034 #define SymE_NeedsProto(vvv) SymI_NeedsProto(vvv);
1035 #define SymE_HasProto(vvv) SymI_HasProto(vvv)
1037 #define SymI_HasProto(vvv) /**/
1038 #define SymI_HasProto_redirect(vvv,xxx) /**/
1041 RTS_POSIX_ONLY_SYMBOLS
1042 RTS_MINGW_ONLY_SYMBOLS
1043 RTS_CYGWIN_ONLY_SYMBOLS
1044 RTS_DARWIN_ONLY_SYMBOLS
1047 #undef SymI_NeedsProto
1048 #undef SymI_HasProto
1049 #undef SymI_HasProto_redirect
1050 #undef SymE_HasProto
1051 #undef SymE_NeedsProto
1053 #ifdef LEADING_UNDERSCORE
1054 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
1056 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
1059 #define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1061 #define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1062 (void*)DLL_IMPORT_DATA_REF(vvv) },
1064 #define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
1065 #define SymE_NeedsProto(vvv) SymE_HasProto(vvv)
1067 // SymI_HasProto_redirect allows us to redirect references to one symbol to
1068 // another symbol. See newCAF/newDynCAF for an example.
1069 #define SymI_HasProto_redirect(vvv,xxx) \
1070 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1073 static RtsSymbolVal rtsSyms[] = {
1076 RTS_POSIX_ONLY_SYMBOLS
1077 RTS_MINGW_ONLY_SYMBOLS
1078 RTS_CYGWIN_ONLY_SYMBOLS
1079 RTS_DARWIN_ONLY_SYMBOLS
1082 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
1083 // dyld stub code contains references to this,
1084 // but it should never be called because we treat
1085 // lazy pointers as nonlazy.
1086 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
1088 { 0, 0 } /* sentinel */
1093 /* -----------------------------------------------------------------------------
1094 * Insert symbols into hash tables, checking for duplicates.
1097 static void ghciInsertStrHashTable ( char* obj_name,
1103 if (lookupHashTable(table, (StgWord)key) == NULL)
1105 insertStrHashTable(table, (StgWord)key, data);
1110 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
1112 "whilst processing object file\n"
1114 "This could be caused by:\n"
1115 " * Loading two different object files which export the same symbol\n"
1116 " * Specifying the same object file twice on the GHCi command line\n"
1117 " * An incorrect `package.conf' entry, causing some object to be\n"
1119 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
1126 /* -----------------------------------------------------------------------------
1127 * initialize the object linker
1131 static int linker_init_done = 0 ;
1133 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1134 static void *dl_prog_handle;
1135 static regex_t re_invalid;
1136 static regex_t re_realso;
1138 static Mutex dl_mutex; // mutex to protect dlopen/dlerror critical section
1146 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1150 IF_DEBUG(linker, debugBelch("initLinker: start\n"));
1152 /* Make initLinker idempotent, so we can call it
1153 before evey relevant operation; that means we
1154 don't need to initialise the linker separately */
1155 if (linker_init_done == 1) {
1156 IF_DEBUG(linker, debugBelch("initLinker: idempotent return\n"));
1159 linker_init_done = 1;
1162 #if defined(THREADED_RTS) && (defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO))
1163 initMutex(&dl_mutex);
1165 stablehash = allocStrHashTable();
1166 symhash = allocStrHashTable();
1168 /* populate the symbol table with stuff from the RTS */
1169 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
1170 ghciInsertStrHashTable("(GHCi built-in symbols)",
1171 symhash, sym->lbl, sym->addr);
1172 IF_DEBUG(linker, debugBelch("initLinker: inserting rts symbol %s, %p\n", sym->lbl, sym->addr));
1174 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1175 machoInitSymbolsWithoutUnderscore();
1178 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1179 # if defined(RTLD_DEFAULT)
1180 dl_prog_handle = RTLD_DEFAULT;
1182 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
1183 # endif /* RTLD_DEFAULT */
1185 compileResult = regcomp(&re_invalid,
1186 "(([^ \t()])+\\.so([^ \t:()])*):([ \t])*invalid ELF header",
1188 ASSERT( compileResult == 0 );
1189 compileResult = regcomp(&re_realso,
1190 "GROUP *\\( *(([^ )])+)",
1192 ASSERT( compileResult == 0 );
1195 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1196 if (RtsFlags.MiscFlags.linkerMemBase != 0) {
1197 // User-override for mmap_32bit_base
1198 mmap_32bit_base = (void*)RtsFlags.MiscFlags.linkerMemBase;
1202 #if defined(mingw32_HOST_OS)
1204 * These two libraries cause problems when added to the static link,
1205 * but are necessary for resolving symbols in GHCi, hence we load
1206 * them manually here.
1212 IF_DEBUG(linker, debugBelch("initLinker: done\n"));
1217 exitLinker( void ) {
1218 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1219 if (linker_init_done == 1) {
1220 regfree(&re_invalid);
1221 regfree(&re_realso);
1223 closeMutex(&dl_mutex);
1229 /* -----------------------------------------------------------------------------
1230 * Loading DLL or .so dynamic libraries
1231 * -----------------------------------------------------------------------------
1233 * Add a DLL from which symbols may be found. In the ELF case, just
1234 * do RTLD_GLOBAL-style add, so no further messing around needs to
1235 * happen in order that symbols in the loaded .so are findable --
1236 * lookupSymbol() will subsequently see them by dlsym on the program's
1237 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
1239 * In the PEi386 case, open the DLLs and put handles to them in a
1240 * linked list. When looking for a symbol, try all handles in the
1241 * list. This means that we need to load even DLLs that are guaranteed
1242 * to be in the ghc.exe image already, just so we can get a handle
1243 * to give to loadSymbol, so that we can find the symbols. For such
1244 * libraries, the LoadLibrary call should be a no-op except for returning
1249 #if defined(OBJFORMAT_PEi386)
1250 /* A record for storing handles into DLLs. */
1255 struct _OpenedDLL* next;
1260 /* A list thereof. */
1261 static OpenedDLL* opened_dlls = NULL;
1264 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1267 internal_dlopen(const char *dll_name)
1273 // omitted: RTLD_NOW
1274 // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
1276 debugBelch("internal_dlopen: dll_name = '%s'\n", dll_name));
1278 //-------------- Begin critical section ------------------
1279 // This critical section is necessary because dlerror() is not
1280 // required to be reentrant (see POSIX -- IEEE Std 1003.1-2008)
1281 // Also, the error message returned must be copied to preserve it
1284 ACQUIRE_LOCK(&dl_mutex);
1285 hdl = dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
1289 /* dlopen failed; return a ptr to the error msg. */
1291 if (errmsg == NULL) errmsg = "addDLL: unknown error";
1292 errmsg_copy = stgMallocBytes(strlen(errmsg)+1, "addDLL");
1293 strcpy(errmsg_copy, errmsg);
1294 errmsg = errmsg_copy;
1296 RELEASE_LOCK(&dl_mutex);
1297 //--------------- End critical section -------------------
1304 addDLL( char *dll_name )
1306 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1307 /* ------------------- ELF DLL loader ------------------- */
1310 regmatch_t match[NMATCH];
1313 size_t match_length;
1314 #define MAXLINE 1000
1320 IF_DEBUG(linker, debugBelch("addDLL: dll_name = '%s'\n", dll_name));
1321 errmsg = internal_dlopen(dll_name);
1323 if (errmsg == NULL) {
1327 // GHC Trac ticket #2615
1328 // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so)
1329 // contain linker scripts rather than ELF-format object code. This
1330 // code handles the situation by recognizing the real object code
1331 // file name given in the linker script.
1333 // If an "invalid ELF header" error occurs, it is assumed that the
1334 // .so file contains a linker script instead of ELF object code.
1335 // In this case, the code looks for the GROUP ( ... ) linker
1336 // directive. If one is found, the first file name inside the
1337 // parentheses is treated as the name of a dynamic library and the
1338 // code attempts to dlopen that file. If this is also unsuccessful,
1339 // an error message is returned.
1341 // see if the error message is due to an invalid ELF header
1342 IF_DEBUG(linker, debugBelch("errmsg = '%s'\n", errmsg));
1343 result = regexec(&re_invalid, errmsg, (size_t) NMATCH, match, 0);
1344 IF_DEBUG(linker, debugBelch("result = %i\n", result));
1346 // success -- try to read the named file as a linker script
1347 match_length = (size_t) stg_min((match[1].rm_eo - match[1].rm_so),
1349 strncpy(line, (errmsg+(match[1].rm_so)),match_length);
1350 line[match_length] = '\0'; // make sure string is null-terminated
1351 IF_DEBUG(linker, debugBelch ("file name = '%s'\n", line));
1352 if ((fp = fopen(line, "r")) == NULL) {
1353 return errmsg; // return original error if open fails
1355 // try to find a GROUP ( ... ) command
1356 while (fgets(line, MAXLINE, fp) != NULL) {
1357 IF_DEBUG(linker, debugBelch("input line = %s", line));
1358 if (regexec(&re_realso, line, (size_t) NMATCH, match, 0) == 0) {
1359 // success -- try to dlopen the first named file
1360 IF_DEBUG(linker, debugBelch("match%s\n",""));
1361 line[match[1].rm_eo] = '\0';
1362 errmsg = internal_dlopen(line+match[1].rm_so);
1365 // if control reaches here, no GROUP ( ... ) directive was found
1366 // and the original error message is returned to the caller
1372 # elif defined(OBJFORMAT_PEi386)
1373 /* ------------------- Win32 DLL loader ------------------- */
1381 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1383 /* See if we've already got it, and ignore if so. */
1384 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1385 if (0 == strcmp(o_dll->name, dll_name))
1389 /* The file name has no suffix (yet) so that we can try
1390 both foo.dll and foo.drv
1392 The documentation for LoadLibrary says:
1393 If no file name extension is specified in the lpFileName
1394 parameter, the default library extension .dll is
1395 appended. However, the file name string can include a trailing
1396 point character (.) to indicate that the module name has no
1399 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1400 sprintf(buf, "%s.DLL", dll_name);
1401 instance = LoadLibrary(buf);
1402 if (instance == NULL) {
1403 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1404 // KAA: allow loading of drivers (like winspool.drv)
1405 sprintf(buf, "%s.DRV", dll_name);
1406 instance = LoadLibrary(buf);
1407 if (instance == NULL) {
1408 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1409 // #1883: allow loading of unix-style libfoo.dll DLLs
1410 sprintf(buf, "lib%s.DLL", dll_name);
1411 instance = LoadLibrary(buf);
1412 if (instance == NULL) {
1419 /* Add this DLL to the list of DLLs in which to search for symbols. */
1420 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1421 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
1422 strcpy(o_dll->name, dll_name);
1423 o_dll->instance = instance;
1424 o_dll->next = opened_dlls;
1425 opened_dlls = o_dll;
1431 sysErrorBelch(dll_name);
1433 /* LoadLibrary failed; return a ptr to the error msg. */
1434 return "addDLL: could not load DLL";
1437 barf("addDLL: not implemented on this platform");
1441 /* -----------------------------------------------------------------------------
1442 * insert a stable symbol in the hash table
1446 insertStableSymbol(char* obj_name, char* key, StgPtr p)
1448 ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
1452 /* -----------------------------------------------------------------------------
1453 * insert a symbol in the hash table
1456 insertSymbol(char* obj_name, char* key, void* data)
1458 ghciInsertStrHashTable(obj_name, symhash, key, data);
1461 /* -----------------------------------------------------------------------------
1462 * lookup a symbol in the hash table
1465 lookupSymbol( char *lbl )
1468 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s\n", lbl));
1470 ASSERT(symhash != NULL);
1471 val = lookupStrHashTable(symhash, lbl);
1474 IF_DEBUG(linker, debugBelch("lookupSymbol: symbol not found\n"));
1475 # if defined(OBJFORMAT_ELF)
1476 return dlsym(dl_prog_handle, lbl);
1477 # elif defined(OBJFORMAT_MACHO)
1479 /* On OS X 10.3 and later, we use dlsym instead of the old legacy
1482 HACK: On OS X, global symbols are prefixed with an underscore.
1483 However, dlsym wants us to omit the leading underscore from the
1484 symbol name. For now, we simply strip it off here (and ONLY
1487 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s with dlsym\n", lbl));
1488 ASSERT(lbl[0] == '_');
1489 return dlsym(dl_prog_handle, lbl+1);
1491 if(NSIsSymbolNameDefined(lbl)) {
1492 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1493 return NSAddressOfSymbol(symbol);
1497 # endif /* HAVE_DLFCN_H */
1498 # elif defined(OBJFORMAT_PEi386)
1501 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1502 if (sym != NULL) { return sym; };
1504 // Also try looking up the symbol without the @N suffix. Some
1505 // DLLs have the suffixes on their symbols, some don't.
1506 zapTrailingAtSign ( (unsigned char*)lbl );
1507 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1508 if (sym != NULL) { return sym; };
1516 IF_DEBUG(linker, debugBelch("lookupSymbol: value of %s is %p\n", lbl, val));
1521 /* -----------------------------------------------------------------------------
1522 * Debugging aid: look in GHCi's object symbol tables for symbols
1523 * within DELTA bytes of the specified address, and show their names.
1526 void ghci_enquire ( char* addr );
1528 void ghci_enquire ( char* addr )
1533 const int DELTA = 64;
1538 for (oc = objects; oc; oc = oc->next) {
1539 for (i = 0; i < oc->n_symbols; i++) {
1540 sym = oc->symbols[i];
1541 if (sym == NULL) continue;
1544 a = lookupStrHashTable(symhash, sym);
1547 // debugBelch("ghci_enquire: can't find %s\n", sym);
1549 else if (addr-DELTA <= a && a <= addr+DELTA) {
1550 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1558 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1561 mmapForLinker (size_t bytes, nat flags, int fd)
1563 void *map_addr = NULL;
1566 static nat fixed = 0;
1568 IF_DEBUG(linker, debugBelch("mmapForLinker: start\n"));
1569 pagesize = getpagesize();
1570 size = ROUND_UP(bytes, pagesize);
1572 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1575 if (mmap_32bit_base != 0) {
1576 map_addr = mmap_32bit_base;
1580 IF_DEBUG(linker, debugBelch("mmapForLinker: \tprotection %#0x\n", PROT_EXEC | PROT_READ | PROT_WRITE));
1581 IF_DEBUG(linker, debugBelch("mmapForLinker: \tflags %#0x\n", MAP_PRIVATE | TRY_MAP_32BIT | fixed | flags));
1582 result = mmap(map_addr, size, PROT_EXEC|PROT_READ|PROT_WRITE,
1583 MAP_PRIVATE|TRY_MAP_32BIT|fixed|flags, fd, 0);
1585 if (result == MAP_FAILED) {
1586 sysErrorBelch("mmap %lu bytes at %p",(lnat)size,map_addr);
1587 errorBelch("Try specifying an address with +RTS -xm<addr> -RTS");
1588 stg_exit(EXIT_FAILURE);
1591 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1592 if (mmap_32bit_base != 0) {
1593 if (result == map_addr) {
1594 mmap_32bit_base = (StgWord8*)map_addr + size;
1596 if ((W_)result > 0x80000000) {
1597 // oops, we were given memory over 2Gb
1598 #if defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS)
1599 // Some platforms require MAP_FIXED. This is normally
1600 // a bad idea, because MAP_FIXED will overwrite
1601 // existing mappings.
1602 munmap(result,size);
1606 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);
1609 // hmm, we were given memory somewhere else, but it's
1610 // still under 2Gb so we can use it. Next time, ask
1611 // for memory right after the place we just got some
1612 mmap_32bit_base = (StgWord8*)result + size;
1616 if ((W_)result > 0x80000000) {
1617 // oops, we were given memory over 2Gb
1618 // ... try allocating memory somewhere else?;
1619 debugTrace(DEBUG_linker,"MAP_32BIT didn't work; gave us %lu bytes at 0x%p", bytes, result);
1620 munmap(result, size);
1622 // Set a base address and try again... (guess: 1Gb)
1623 mmap_32bit_base = (void*)0x40000000;
1629 IF_DEBUG(linker, debugBelch("mmapForLinker: mapped %lu bytes starting at %p\n", (lnat)size, result));
1630 IF_DEBUG(linker, debugBelch("mmapForLinker: done\n"));
1636 mkOc( char *path, char *image, int imageSize,
1637 char *archiveMemberName
1639 #ifdef darwin_HOST_OS
1646 IF_DEBUG(linker, debugBelch("mkOc: start\n"));
1647 oc = stgMallocBytes(sizeof(ObjectCode), "loadArchive(oc)");
1649 # if defined(OBJFORMAT_ELF)
1650 oc->formatName = "ELF";
1651 # elif defined(OBJFORMAT_PEi386)
1652 oc->formatName = "PEi386";
1653 # elif defined(OBJFORMAT_MACHO)
1654 oc->formatName = "Mach-O";
1657 barf("loadObj: not implemented on this platform");
1661 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1662 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1663 strcpy(oc->fileName, path);
1665 if (archiveMemberName) {
1666 oc->archiveMemberName = stgMallocBytes( strlen(archiveMemberName)+1, "loadObj" );
1667 strcpy(oc->archiveMemberName, archiveMemberName);
1670 oc->archiveMemberName = NULL;
1673 oc->fileSize = imageSize;
1675 oc->sections = NULL;
1676 oc->proddables = NULL;
1679 #ifdef darwin_HOST_OS
1680 oc->misalignment = misalignment;
1684 /* chain it onto the list of objects */
1688 IF_DEBUG(linker, debugBelch("mkOc: done\n"));
1693 loadArchive( char *path )
1700 size_t thisFileNameSize;
1702 size_t fileNameSize;
1703 int isObject, isGnuIndex;
1706 int gnuFileIndexSize;
1707 #if !defined(USE_MMAP) && defined(darwin_HOST_OS)
1711 IF_DEBUG(linker, debugBelch("loadArchive: start\n"));
1712 IF_DEBUG(linker, debugBelch("loadArchive: Loading archive `%s'\n", path));
1714 gnuFileIndex = NULL;
1715 gnuFileIndexSize = 0;
1718 fileName = stgMallocBytes(fileNameSize, "loadArchive(fileName)");
1720 f = fopen(path, "rb");
1722 barf("loadObj: can't read `%s'", path);
1724 n = fread ( tmp, 1, 8, f );
1725 if (strncmp(tmp, "!<arch>\n", 8) != 0) {
1726 barf("loadArchive: Not an archive: `%s'", path);
1729 IF_DEBUG(linker, debugBelch("loadArchive: loading archive contents\n"));
1732 n = fread ( fileName, 1, 16, f );
1735 IF_DEBUG(linker, debugBelch("loadArchive: EOF while reading from '%s'\n", path));
1739 barf("loadArchive: Failed reading file name from `%s'", path);
1742 #if defined(darwin_HOST_OS)
1744 if (strncmp(fileName, "!<arch>\n", 8) == 0) {
1745 IF_DEBUG(linker, debugBelch("loadArchive: found the start of another archive, breaking\n"));
1751 n = fread ( tmp, 1, 12, f );
1753 barf("loadArchive: Failed reading mod time from `%s'", path);
1754 n = fread ( tmp, 1, 6, f );
1756 barf("loadArchive: Failed reading owner from `%s'", path);
1757 n = fread ( tmp, 1, 6, f );
1759 barf("loadArchive: Failed reading group from `%s'", path);
1760 n = fread ( tmp, 1, 8, f );
1762 barf("loadArchive: Failed reading mode from `%s'", path);
1763 n = fread ( tmp, 1, 10, f );
1765 barf("loadArchive: Failed reading size from `%s'", path);
1767 for (n = 0; isdigit(tmp[n]); n++);
1769 memberSize = atoi(tmp);
1771 IF_DEBUG(linker, debugBelch("loadArchive: size of this archive member is %d\n", memberSize));
1772 n = fread ( tmp, 1, 2, f );
1773 if (strncmp(tmp, "\x60\x0A", 2) != 0)
1774 barf("loadArchive: Failed reading magic from `%s' at %ld. Got %c%c",
1775 path, ftell(f), tmp[0], tmp[1]);
1778 /* Check for BSD-variant large filenames */
1779 if (0 == strncmp(fileName, "#1/", 3)) {
1780 fileName[16] = '\0';
1781 if (isdigit(fileName[3])) {
1782 for (n = 4; isdigit(fileName[n]); n++);
1784 thisFileNameSize = atoi(fileName + 3);
1785 memberSize -= thisFileNameSize;
1786 if (thisFileNameSize >= fileNameSize) {
1787 /* Double it to avoid potentially continually
1788 increasing it by 1 */
1789 fileNameSize = thisFileNameSize * 2;
1790 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1792 n = fread ( fileName, 1, thisFileNameSize, f );
1793 if (n != (int)thisFileNameSize) {
1794 barf("loadArchive: Failed reading filename from `%s'",
1797 fileName[thisFileNameSize] = 0;
1799 /* On OS X at least, thisFileNameSize is the size of the
1800 fileName field, not the length of the fileName
1802 thisFileNameSize = strlen(fileName);
1805 barf("loadArchive: BSD-variant filename size not found while reading filename from `%s'", path);
1808 /* Check for GNU file index file */
1809 else if (0 == strncmp(fileName, "//", 2)) {
1811 thisFileNameSize = 0;
1814 /* Check for a file in the GNU file index */
1815 else if (fileName[0] == '/') {
1816 if (isdigit(fileName[1])) {
1819 for (n = 2; isdigit(fileName[n]); n++);
1821 n = atoi(fileName + 1);
1823 if (gnuFileIndex == NULL) {
1824 barf("loadArchive: GNU-variant filename without an index while reading from `%s'", path);
1826 if (n < 0 || n > gnuFileIndexSize) {
1827 barf("loadArchive: GNU-variant filename offset %d out of range [0..%d] while reading filename from `%s'", n, gnuFileIndexSize, path);
1829 if (n != 0 && gnuFileIndex[n - 1] != '\n') {
1830 barf("loadArchive: GNU-variant filename offset %d invalid (range [0..%d]) while reading filename from `%s'", n, gnuFileIndexSize, path);
1832 for (i = n; gnuFileIndex[i] != '/'; i++);
1833 thisFileNameSize = i - n;
1834 if (thisFileNameSize >= fileNameSize) {
1835 /* Double it to avoid potentially continually
1836 increasing it by 1 */
1837 fileNameSize = thisFileNameSize * 2;
1838 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1840 memcpy(fileName, gnuFileIndex + n, thisFileNameSize);
1841 fileName[thisFileNameSize] = '\0';
1843 else if (fileName[1] == ' ') {
1845 thisFileNameSize = 0;
1848 barf("loadArchive: GNU-variant filename offset not found while reading filename from `%s'", path);
1851 /* Finally, the case where the filename field actually contains
1854 /* GNU ar terminates filenames with a '/', this allowing
1855 spaces in filenames. So first look to see if there is a
1857 for (thisFileNameSize = 0;
1858 thisFileNameSize < 16;
1859 thisFileNameSize++) {
1860 if (fileName[thisFileNameSize] == '/') {
1861 fileName[thisFileNameSize] = '\0';
1865 /* If we didn't find a '/', then a space teminates the
1866 filename. Note that if we don't find one, then
1867 thisFileNameSize ends up as 16, and we already have the
1869 if (thisFileNameSize == 16) {
1870 for (thisFileNameSize = 0;
1871 thisFileNameSize < 16;
1872 thisFileNameSize++) {
1873 if (fileName[thisFileNameSize] == ' ') {
1874 fileName[thisFileNameSize] = '\0';
1882 debugBelch("loadArchive: Found member file `%s'\n", fileName));
1884 isObject = thisFileNameSize >= 2
1885 && fileName[thisFileNameSize - 2] == '.'
1886 && fileName[thisFileNameSize - 1] == 'o';
1888 IF_DEBUG(linker, debugBelch("loadArchive: \tthisFileNameSize = %d\n", (int)thisFileNameSize));
1889 IF_DEBUG(linker, debugBelch("loadArchive: \tisObject = %d\n", isObject));
1892 char *archiveMemberName;
1894 IF_DEBUG(linker, debugBelch("loadArchive: Member is an object file...loading...\n"));
1896 /* We can't mmap from the archive directly, as object
1897 files need to be 8-byte aligned but files in .ar
1898 archives are 2-byte aligned. When possible we use mmap
1899 to get some anonymous memory, as on 64-bit platforms if
1900 we use malloc then we can be given memory above 2^32.
1901 In the mmap case we're probably wasting lots of space;
1902 we could do better. */
1903 #if defined(USE_MMAP)
1904 image = mmapForLinker(memberSize, MAP_ANONYMOUS, -1);
1905 #elif defined(darwin_HOST_OS)
1907 misalignment = machoGetMisalignment(f);
1908 image = stgMallocBytes(memberSize + misalignment, "loadArchive(image)");
1909 image += misalignment;
1911 image = stgMallocBytes(memberSize, "loadArchive(image)");
1913 n = fread ( image, 1, memberSize, f );
1914 if (n != memberSize) {
1915 barf("loadArchive: error whilst reading `%s'", path);
1918 archiveMemberName = stgMallocBytes(strlen(path) + thisFileNameSize + 3,
1919 "loadArchive(file)");
1920 sprintf(archiveMemberName, "%s(%.*s)",
1921 path, (int)thisFileNameSize, fileName);
1923 oc = mkOc(path, image, memberSize, archiveMemberName
1925 #ifdef darwin_HOST_OS
1931 stgFree(archiveMemberName);
1933 if (0 == loadOc(oc)) {
1938 else if (isGnuIndex) {
1939 if (gnuFileIndex != NULL) {
1940 barf("loadArchive: GNU-variant index found, but already have an index, while reading filename from `%s'", path);
1942 IF_DEBUG(linker, debugBelch("loadArchive: Found GNU-variant file index\n"));
1944 gnuFileIndex = mmapForLinker(memberSize + 1, MAP_ANONYMOUS, -1);
1946 gnuFileIndex = stgMallocBytes(memberSize + 1, "loadArchive(image)");
1948 n = fread ( gnuFileIndex, 1, memberSize, f );
1949 if (n != memberSize) {
1950 barf("loadArchive: error whilst reading `%s'", path);
1952 gnuFileIndex[memberSize] = '/';
1953 gnuFileIndexSize = memberSize;
1956 IF_DEBUG(linker, debugBelch("loadArchive: '%s' does not appear to be an object file\n", fileName));
1957 n = fseek(f, memberSize, SEEK_CUR);
1959 barf("loadArchive: error whilst seeking by %d in `%s'",
1963 /* .ar files are 2-byte aligned */
1964 if (memberSize % 2) {
1965 IF_DEBUG(linker, debugBelch("loadArchive: trying to read one pad byte\n"));
1966 n = fread ( tmp, 1, 1, f );
1969 IF_DEBUG(linker, debugBelch("loadArchive: found EOF while reading one pad byte\n"));
1973 barf("loadArchive: Failed reading padding from `%s'", path);
1976 IF_DEBUG(linker, debugBelch("loadArchive: successfully read one pad byte\n"));
1978 IF_DEBUG(linker, debugBelch("loadArchive: reached end of archive loading while loop\n"));
1984 if (gnuFileIndex != NULL) {
1986 munmap(gnuFileIndex, gnuFileIndexSize + 1);
1988 stgFree(gnuFileIndex);
1992 IF_DEBUG(linker, debugBelch("loadArchive: done\n"));
1996 /* -----------------------------------------------------------------------------
1997 * Load an obj (populate the global symbol table, but don't resolve yet)
1999 * Returns: 1 if ok, 0 on error.
2002 loadObj( char *path )
2013 # if defined(darwin_HOST_OS)
2017 IF_DEBUG(linker, debugBelch("loadObj %s\n", path));
2021 /* debugBelch("loadObj %s\n", path ); */
2023 /* Check that we haven't already loaded this object.
2024 Ignore requests to load multiple times */
2028 for (o = objects; o; o = o->next) {
2029 if (0 == strcmp(o->fileName, path)) {
2031 break; /* don't need to search further */
2035 IF_DEBUG(linker, debugBelch(
2036 "GHCi runtime linker: warning: looks like you're trying to load the\n"
2037 "same object file twice:\n"
2039 "GHCi will ignore this, but be warned.\n"
2041 return 1; /* success */
2045 r = stat(path, &st);
2047 IF_DEBUG(linker, debugBelch("File doesn't exist\n"));
2051 fileSize = st.st_size;
2054 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
2056 #if defined(openbsd_HOST_OS)
2057 fd = open(path, O_RDONLY, S_IRUSR);
2059 fd = open(path, O_RDONLY);
2062 barf("loadObj: can't open `%s'", path);
2064 image = mmapForLinker(fileSize, 0, fd);
2068 #else /* !USE_MMAP */
2069 /* load the image into memory */
2070 f = fopen(path, "rb");
2072 barf("loadObj: can't read `%s'", path);
2074 # if defined(mingw32_HOST_OS)
2075 // TODO: We would like to use allocateExec here, but allocateExec
2076 // cannot currently allocate blocks large enough.
2077 image = VirtualAlloc(NULL, fileSize, MEM_RESERVE | MEM_COMMIT,
2078 PAGE_EXECUTE_READWRITE);
2079 # elif defined(darwin_HOST_OS)
2080 // In a Mach-O .o file, all sections can and will be misaligned
2081 // if the total size of the headers is not a multiple of the
2082 // desired alignment. This is fine for .o files that only serve
2083 // as input for the static linker, but it's not fine for us,
2084 // as SSE (used by gcc for floating point) and Altivec require
2085 // 16-byte alignment.
2086 // We calculate the correct alignment from the header before
2087 // reading the file, and then we misalign image on purpose so
2088 // that the actual sections end up aligned again.
2089 misalignment = machoGetMisalignment(f);
2090 image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
2091 image += misalignment;
2093 image = stgMallocBytes(fileSize, "loadObj(image)");
2098 n = fread ( image, 1, fileSize, f );
2100 barf("loadObj: error whilst reading `%s'", path);
2103 #endif /* USE_MMAP */
2105 oc = mkOc(path, image, fileSize, NULL
2107 #ifdef darwin_HOST_OS
2117 loadOc( ObjectCode* oc ) {
2120 IF_DEBUG(linker, debugBelch("loadOc: start\n"));
2122 # if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2123 r = ocAllocateSymbolExtras_MachO ( oc );
2125 IF_DEBUG(linker, debugBelch("loadOc: ocAllocateSymbolExtras_MachO failed\n"));
2128 # elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2129 r = ocAllocateSymbolExtras_ELF ( oc );
2131 IF_DEBUG(linker, debugBelch("loadOc: ocAllocateSymbolExtras_ELF failed\n"));
2136 /* verify the in-memory image */
2137 # if defined(OBJFORMAT_ELF)
2138 r = ocVerifyImage_ELF ( oc );
2139 # elif defined(OBJFORMAT_PEi386)
2140 r = ocVerifyImage_PEi386 ( oc );
2141 # elif defined(OBJFORMAT_MACHO)
2142 r = ocVerifyImage_MachO ( oc );
2144 barf("loadObj: no verify method");
2147 IF_DEBUG(linker, debugBelch("loadOc: ocVerifyImage_* failed\n"));
2151 /* build the symbol list for this image */
2152 # if defined(OBJFORMAT_ELF)
2153 r = ocGetNames_ELF ( oc );
2154 # elif defined(OBJFORMAT_PEi386)
2155 r = ocGetNames_PEi386 ( oc );
2156 # elif defined(OBJFORMAT_MACHO)
2157 r = ocGetNames_MachO ( oc );
2159 barf("loadObj: no getNames method");
2162 IF_DEBUG(linker, debugBelch("loadOc: ocGetNames_* failed\n"));
2166 /* loaded, but not resolved yet */
2167 oc->status = OBJECT_LOADED;
2168 IF_DEBUG(linker, debugBelch("loadOc: done.\n"));
2173 /* -----------------------------------------------------------------------------
2174 * resolve all the currently unlinked objects in memory
2176 * Returns: 1 if ok, 0 on error.
2184 IF_DEBUG(linker, debugBelch("resolveObjs: start\n"));
2187 for (oc = objects; oc; oc = oc->next) {
2188 if (oc->status != OBJECT_RESOLVED) {
2189 # if defined(OBJFORMAT_ELF)
2190 r = ocResolve_ELF ( oc );
2191 # elif defined(OBJFORMAT_PEi386)
2192 r = ocResolve_PEi386 ( oc );
2193 # elif defined(OBJFORMAT_MACHO)
2194 r = ocResolve_MachO ( oc );
2196 barf("resolveObjs: not implemented on this platform");
2198 if (!r) { return r; }
2199 oc->status = OBJECT_RESOLVED;
2202 IF_DEBUG(linker, debugBelch("resolveObjs: done\n"));
2206 /* -----------------------------------------------------------------------------
2207 * delete an object from the pool
2210 unloadObj( char *path )
2212 ObjectCode *oc, *prev;
2213 HsBool unloadedAnyObj = HS_BOOL_FALSE;
2215 ASSERT(symhash != NULL);
2216 ASSERT(objects != NULL);
2221 for (oc = objects; oc; prev = oc, oc = oc->next) {
2222 if (!strcmp(oc->fileName,path)) {
2224 /* Remove all the mappings for the symbols within this
2229 for (i = 0; i < oc->n_symbols; i++) {
2230 if (oc->symbols[i] != NULL) {
2231 removeStrHashTable(symhash, oc->symbols[i], NULL);
2239 prev->next = oc->next;
2242 // We're going to leave this in place, in case there are
2243 // any pointers from the heap into it:
2244 // #ifdef mingw32_HOST_OS
2245 // VirtualFree(oc->image);
2247 // stgFree(oc->image);
2249 stgFree(oc->fileName);
2250 stgFree(oc->symbols);
2251 stgFree(oc->sections);
2254 /* This could be a member of an archive so continue
2255 * unloading other members. */
2256 unloadedAnyObj = HS_BOOL_TRUE;
2260 if (unloadedAnyObj) {
2264 errorBelch("unloadObj: can't find `%s' to unload", path);
2269 /* -----------------------------------------------------------------------------
2270 * Sanity checking. For each ObjectCode, maintain a list of address ranges
2271 * which may be prodded during relocation, and abort if we try and write
2272 * outside any of these.
2275 addProddableBlock ( ObjectCode* oc, void* start, int size )
2278 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
2280 IF_DEBUG(linker, debugBelch("addProddableBlock: %p %p %d\n", oc, start, size));
2284 pb->next = oc->proddables;
2285 oc->proddables = pb;
2289 checkProddableBlock (ObjectCode *oc, void *addr )
2293 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
2294 char* s = (char*)(pb->start);
2295 char* e = s + pb->size - 1;
2296 char* a = (char*)addr;
2297 /* Assumes that the biggest fixup involves a 4-byte write. This
2298 probably needs to be changed to 8 (ie, +7) on 64-bit
2300 if (a >= s && (a+3) <= e) return;
2302 barf("checkProddableBlock: invalid fixup in runtime linker");
2305 /* -----------------------------------------------------------------------------
2306 * Section management.
2309 addSection ( ObjectCode* oc, SectionKind kind,
2310 void* start, void* end )
2312 Section* s = stgMallocBytes(sizeof(Section), "addSection");
2316 s->next = oc->sections;
2319 IF_DEBUG(linker, debugBelch("addSection: %p-%p (size %ld), kind %d\n",
2320 start, ((char*)end)-1, (long)end - (long)start + 1, kind ));
2324 /* --------------------------------------------------------------------------
2326 * This is about allocating a small chunk of memory for every symbol in the
2327 * object file. We make sure that the SymboLExtras are always "in range" of
2328 * limited-range PC-relative instructions on various platforms by allocating
2329 * them right next to the object code itself.
2332 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
2335 ocAllocateSymbolExtras
2337 Allocate additional space at the end of the object file image to make room
2338 for jump islands (powerpc, x86_64) and GOT entries (x86_64).
2340 PowerPC relative branch instructions have a 24 bit displacement field.
2341 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
2342 If a particular imported symbol is outside this range, we have to redirect
2343 the jump to a short piece of new code that just loads the 32bit absolute
2344 address and jumps there.
2345 On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
2348 This function just allocates space for one SymbolExtra for every
2349 undefined symbol in the object file. The code for the jump islands is
2350 filled in by makeSymbolExtra below.
2353 static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
2360 int misalignment = 0;
2361 #ifdef darwin_HOST_OS
2362 misalignment = oc->misalignment;
2368 // round up to the nearest 4
2369 aligned = (oc->fileSize + 3) & ~3;
2372 pagesize = getpagesize();
2373 n = ROUND_UP( oc->fileSize, pagesize );
2374 m = ROUND_UP( aligned + sizeof (SymbolExtra) * count, pagesize );
2376 /* we try to use spare space at the end of the last page of the
2377 * image for the jump islands, but if there isn't enough space
2378 * then we have to map some (anonymously, remembering MAP_32BIT).
2380 if( m > n ) // we need to allocate more pages
2382 oc->symbol_extras = mmapForLinker(sizeof(SymbolExtra) * count,
2387 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2390 oc->image -= misalignment;
2391 oc->image = stgReallocBytes( oc->image,
2393 aligned + sizeof (SymbolExtra) * count,
2394 "ocAllocateSymbolExtras" );
2395 oc->image += misalignment;
2397 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2398 #endif /* USE_MMAP */
2400 memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
2403 oc->symbol_extras = NULL;
2405 oc->first_symbol_extra = first;
2406 oc->n_symbol_extras = count;
2411 static SymbolExtra* makeSymbolExtra( ObjectCode* oc,
2412 unsigned long symbolNumber,
2413 unsigned long target )
2417 ASSERT( symbolNumber >= oc->first_symbol_extra
2418 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
2420 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
2422 #ifdef powerpc_HOST_ARCH
2423 // lis r12, hi16(target)
2424 extra->jumpIsland.lis_r12 = 0x3d80;
2425 extra->jumpIsland.hi_addr = target >> 16;
2427 // ori r12, r12, lo16(target)
2428 extra->jumpIsland.ori_r12_r12 = 0x618c;
2429 extra->jumpIsland.lo_addr = target & 0xffff;
2432 extra->jumpIsland.mtctr_r12 = 0x7d8903a6;
2435 extra->jumpIsland.bctr = 0x4e800420;
2437 #ifdef x86_64_HOST_ARCH
2439 static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
2440 extra->addr = target;
2441 memcpy(extra->jumpIsland, jmp, 6);
2449 /* --------------------------------------------------------------------------
2450 * PowerPC specifics (instruction cache flushing)
2451 * ------------------------------------------------------------------------*/
2453 #ifdef powerpc_HOST_ARCH
2455 ocFlushInstructionCache
2457 Flush the data & instruction caches.
2458 Because the PPC has split data/instruction caches, we have to
2459 do that whenever we modify code at runtime.
2463 ocFlushInstructionCacheFrom(void* begin, size_t length)
2465 size_t n = (length + 3) / 4;
2466 unsigned long* p = begin;
2470 __asm__ volatile ( "dcbf 0,%0\n\t"
2478 __asm__ volatile ( "sync\n\t"
2484 ocFlushInstructionCache( ObjectCode *oc )
2486 /* The main object code */
2487 ocFlushInstructionCacheFrom(oc->image + oc->misalignment, oc->fileSize);
2490 ocFlushInstructionCacheFrom(oc->symbol_extras, sizeof(SymbolExtra) * oc->n_symbol_extras);
2492 #endif /* powerpc_HOST_ARCH */
2495 /* --------------------------------------------------------------------------
2496 * PEi386 specifics (Win32 targets)
2497 * ------------------------------------------------------------------------*/
2499 /* The information for this linker comes from
2500 Microsoft Portable Executable
2501 and Common Object File Format Specification
2502 revision 5.1 January 1998
2503 which SimonM says comes from the MS Developer Network CDs.
2505 It can be found there (on older CDs), but can also be found
2508 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
2510 (this is Rev 6.0 from February 1999).
2512 Things move, so if that fails, try searching for it via
2514 http://www.google.com/search?q=PE+COFF+specification
2516 The ultimate reference for the PE format is the Winnt.h
2517 header file that comes with the Platform SDKs; as always,
2518 implementations will drift wrt their documentation.
2520 A good background article on the PE format is Matt Pietrek's
2521 March 1994 article in Microsoft System Journal (MSJ)
2522 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
2523 Win32 Portable Executable File Format." The info in there
2524 has recently been updated in a two part article in
2525 MSDN magazine, issues Feb and March 2002,
2526 "Inside Windows: An In-Depth Look into the Win32 Portable
2527 Executable File Format"
2529 John Levine's book "Linkers and Loaders" contains useful
2534 #if defined(OBJFORMAT_PEi386)
2538 typedef unsigned char UChar;
2539 typedef unsigned short UInt16;
2540 typedef unsigned int UInt32;
2547 UInt16 NumberOfSections;
2548 UInt32 TimeDateStamp;
2549 UInt32 PointerToSymbolTable;
2550 UInt32 NumberOfSymbols;
2551 UInt16 SizeOfOptionalHeader;
2552 UInt16 Characteristics;
2556 #define sizeof_COFF_header 20
2563 UInt32 VirtualAddress;
2564 UInt32 SizeOfRawData;
2565 UInt32 PointerToRawData;
2566 UInt32 PointerToRelocations;
2567 UInt32 PointerToLinenumbers;
2568 UInt16 NumberOfRelocations;
2569 UInt16 NumberOfLineNumbers;
2570 UInt32 Characteristics;
2574 #define sizeof_COFF_section 40
2581 UInt16 SectionNumber;
2584 UChar NumberOfAuxSymbols;
2588 #define sizeof_COFF_symbol 18
2593 UInt32 VirtualAddress;
2594 UInt32 SymbolTableIndex;
2599 #define sizeof_COFF_reloc 10
2602 /* From PE spec doc, section 3.3.2 */
2603 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
2604 windows.h -- for the same purpose, but I want to know what I'm
2606 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
2607 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
2608 #define MYIMAGE_FILE_DLL 0x2000
2609 #define MYIMAGE_FILE_SYSTEM 0x1000
2610 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
2611 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
2612 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
2614 /* From PE spec doc, section 5.4.2 and 5.4.4 */
2615 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
2616 #define MYIMAGE_SYM_CLASS_STATIC 3
2617 #define MYIMAGE_SYM_UNDEFINED 0
2619 /* From PE spec doc, section 4.1 */
2620 #define MYIMAGE_SCN_CNT_CODE 0x00000020
2621 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
2622 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
2624 /* From PE spec doc, section 5.2.1 */
2625 #define MYIMAGE_REL_I386_DIR32 0x0006
2626 #define MYIMAGE_REL_I386_REL32 0x0014
2629 /* We use myindex to calculate array addresses, rather than
2630 simply doing the normal subscript thing. That's because
2631 some of the above structs have sizes which are not
2632 a whole number of words. GCC rounds their sizes up to a
2633 whole number of words, which means that the address calcs
2634 arising from using normal C indexing or pointer arithmetic
2635 are just plain wrong. Sigh.
2638 myindex ( int scale, void* base, int index )
2641 ((UChar*)base) + scale * index;
2646 printName ( UChar* name, UChar* strtab )
2648 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2649 UInt32 strtab_offset = * (UInt32*)(name+4);
2650 debugBelch("%s", strtab + strtab_offset );
2653 for (i = 0; i < 8; i++) {
2654 if (name[i] == 0) break;
2655 debugBelch("%c", name[i] );
2662 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
2664 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2665 UInt32 strtab_offset = * (UInt32*)(name+4);
2666 strncpy ( (char*)dst, (char*)strtab+strtab_offset, dstSize );
2672 if (name[i] == 0) break;
2682 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
2685 /* If the string is longer than 8 bytes, look in the
2686 string table for it -- this will be correctly zero terminated.
2688 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2689 UInt32 strtab_offset = * (UInt32*)(name+4);
2690 return ((UChar*)strtab) + strtab_offset;
2692 /* Otherwise, if shorter than 8 bytes, return the original,
2693 which by defn is correctly terminated.
2695 if (name[7]==0) return name;
2696 /* The annoying case: 8 bytes. Copy into a temporary
2697 (XXX which is never freed ...)
2699 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
2701 strncpy((char*)newstr,(char*)name,8);
2706 /* Getting the name of a section is mildly tricky, so we make a
2707 function for it. Sadly, in one case we have to copy the string
2708 (when it is exactly 8 bytes long there's no trailing '\0'), so for
2709 consistency we *always* copy the string; the caller must free it
2712 cstring_from_section_name (UChar* name, UChar* strtab)
2717 int strtab_offset = strtol((char*)name+1,NULL,10);
2718 int len = strlen(((char*)strtab) + strtab_offset);
2720 newstr = stgMallocBytes(len, "cstring_from_section_symbol_name");
2721 strcpy((char*)newstr, (char*)((UChar*)strtab) + strtab_offset);
2726 newstr = stgMallocBytes(9, "cstring_from_section_symbol_name");
2728 strncpy((char*)newstr,(char*)name,8);
2734 /* Just compares the short names (first 8 chars) */
2735 static COFF_section *
2736 findPEi386SectionCalled ( ObjectCode* oc, UChar* name )
2740 = (COFF_header*)(oc->image);
2741 COFF_section* sectab
2743 ((UChar*)(oc->image))
2744 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2746 for (i = 0; i < hdr->NumberOfSections; i++) {
2749 COFF_section* section_i
2751 myindex ( sizeof_COFF_section, sectab, i );
2752 n1 = (UChar*) &(section_i->Name);
2754 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
2755 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
2756 n1[6]==n2[6] && n1[7]==n2[7])
2765 zapTrailingAtSign ( UChar* sym )
2767 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
2769 if (sym[0] == 0) return;
2771 while (sym[i] != 0) i++;
2774 while (j > 0 && my_isdigit(sym[j])) j--;
2775 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
2780 lookupSymbolInDLLs ( UChar *lbl )
2785 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
2786 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
2788 if (lbl[0] == '_') {
2789 /* HACK: if the name has an initial underscore, try stripping
2790 it off & look that up first. I've yet to verify whether there's
2791 a Rule that governs whether an initial '_' *should always* be
2792 stripped off when mapping from import lib name to the DLL name.
2794 sym = GetProcAddress(o_dll->instance, (char*)(lbl+1));
2796 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
2800 sym = GetProcAddress(o_dll->instance, (char*)lbl);
2802 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
2811 ocVerifyImage_PEi386 ( ObjectCode* oc )
2816 COFF_section* sectab;
2817 COFF_symbol* symtab;
2819 /* debugBelch("\nLOADING %s\n", oc->fileName); */
2820 hdr = (COFF_header*)(oc->image);
2821 sectab = (COFF_section*) (
2822 ((UChar*)(oc->image))
2823 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2825 symtab = (COFF_symbol*) (
2826 ((UChar*)(oc->image))
2827 + hdr->PointerToSymbolTable
2829 strtab = ((UChar*)symtab)
2830 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2832 if (hdr->Machine != 0x14c) {
2833 errorBelch("%s: Not x86 PEi386", oc->fileName);
2836 if (hdr->SizeOfOptionalHeader != 0) {
2837 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
2840 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
2841 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
2842 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
2843 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
2844 errorBelch("%s: Not a PEi386 object file", oc->fileName);
2847 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
2848 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
2849 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
2851 (int)(hdr->Characteristics));
2854 /* If the string table size is way crazy, this might indicate that
2855 there are more than 64k relocations, despite claims to the
2856 contrary. Hence this test. */
2857 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
2859 if ( (*(UInt32*)strtab) > 600000 ) {
2860 /* Note that 600k has no special significance other than being
2861 big enough to handle the almost-2MB-sized lumps that
2862 constitute HSwin32*.o. */
2863 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
2868 /* No further verification after this point; only debug printing. */
2870 IF_DEBUG(linker, i=1);
2871 if (i == 0) return 1;
2873 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
2874 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
2875 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
2878 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
2879 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
2880 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
2881 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
2882 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
2883 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
2884 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
2886 /* Print the section table. */
2888 for (i = 0; i < hdr->NumberOfSections; i++) {
2890 COFF_section* sectab_i
2892 myindex ( sizeof_COFF_section, sectab, i );
2899 printName ( sectab_i->Name, strtab );
2909 sectab_i->VirtualSize,
2910 sectab_i->VirtualAddress,
2911 sectab_i->SizeOfRawData,
2912 sectab_i->PointerToRawData,
2913 sectab_i->NumberOfRelocations,
2914 sectab_i->PointerToRelocations,
2915 sectab_i->PointerToRawData
2917 reltab = (COFF_reloc*) (
2918 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2921 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2922 /* If the relocation field (a short) has overflowed, the
2923 * real count can be found in the first reloc entry.
2925 * See Section 4.1 (last para) of the PE spec (rev6.0).
2927 COFF_reloc* rel = (COFF_reloc*)
2928 myindex ( sizeof_COFF_reloc, reltab, 0 );
2929 noRelocs = rel->VirtualAddress;
2932 noRelocs = sectab_i->NumberOfRelocations;
2936 for (; j < noRelocs; j++) {
2938 COFF_reloc* rel = (COFF_reloc*)
2939 myindex ( sizeof_COFF_reloc, reltab, j );
2941 " type 0x%-4x vaddr 0x%-8x name `",
2943 rel->VirtualAddress );
2944 sym = (COFF_symbol*)
2945 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
2946 /* Hmm..mysterious looking offset - what's it for? SOF */
2947 printName ( sym->Name, strtab -10 );
2954 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
2955 debugBelch("---START of string table---\n");
2956 for (i = 4; i < *(Int32*)strtab; i++) {
2958 debugBelch("\n"); else
2959 debugBelch("%c", strtab[i] );
2961 debugBelch("--- END of string table---\n");
2966 COFF_symbol* symtab_i;
2967 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2968 symtab_i = (COFF_symbol*)
2969 myindex ( sizeof_COFF_symbol, symtab, i );
2975 printName ( symtab_i->Name, strtab );
2984 (Int32)(symtab_i->SectionNumber),
2985 (UInt32)symtab_i->Type,
2986 (UInt32)symtab_i->StorageClass,
2987 (UInt32)symtab_i->NumberOfAuxSymbols
2989 i += symtab_i->NumberOfAuxSymbols;
2999 ocGetNames_PEi386 ( ObjectCode* oc )
3002 COFF_section* sectab;
3003 COFF_symbol* symtab;
3010 hdr = (COFF_header*)(oc->image);
3011 sectab = (COFF_section*) (
3012 ((UChar*)(oc->image))
3013 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3015 symtab = (COFF_symbol*) (
3016 ((UChar*)(oc->image))
3017 + hdr->PointerToSymbolTable
3019 strtab = ((UChar*)(oc->image))
3020 + hdr->PointerToSymbolTable
3021 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3023 /* Allocate space for any (local, anonymous) .bss sections. */
3025 for (i = 0; i < hdr->NumberOfSections; i++) {
3028 COFF_section* sectab_i
3030 myindex ( sizeof_COFF_section, sectab, i );
3032 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3034 if (0 != strcmp(secname, ".bss")) {
3041 /* sof 10/05: the PE spec text isn't too clear regarding what
3042 * the SizeOfRawData field is supposed to hold for object
3043 * file sections containing just uninitialized data -- for executables,
3044 * it is supposed to be zero; unclear what it's supposed to be
3045 * for object files. However, VirtualSize is guaranteed to be
3046 * zero for object files, which definitely suggests that SizeOfRawData
3047 * will be non-zero (where else would the size of this .bss section be
3048 * stored?) Looking at the COFF_section info for incoming object files,
3049 * this certainly appears to be the case.
3051 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
3052 * object files up until now. This turned out to bite us with ghc-6.4.1's use
3053 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
3054 * variable decls into to the .bss section. (The specific function in Q which
3055 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
3057 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
3058 /* This is a non-empty .bss section. Allocate zeroed space for
3059 it, and set its PointerToRawData field such that oc->image +
3060 PointerToRawData == addr_of_zeroed_space. */
3061 bss_sz = sectab_i->VirtualSize;
3062 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
3063 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
3064 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
3065 addProddableBlock(oc, zspace, bss_sz);
3066 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
3069 /* Copy section information into the ObjectCode. */
3071 for (i = 0; i < hdr->NumberOfSections; i++) {
3077 = SECTIONKIND_OTHER;
3078 COFF_section* sectab_i
3080 myindex ( sizeof_COFF_section, sectab, i );
3082 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3084 IF_DEBUG(linker, debugBelch("section name = %s\n", secname ));
3087 /* I'm sure this is the Right Way to do it. However, the
3088 alternative of testing the sectab_i->Name field seems to
3089 work ok with Cygwin.
3091 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
3092 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
3093 kind = SECTIONKIND_CODE_OR_RODATA;
3096 if (0==strcmp(".text",(char*)secname) ||
3097 0==strcmp(".rdata",(char*)secname)||
3098 0==strcmp(".rodata",(char*)secname))
3099 kind = SECTIONKIND_CODE_OR_RODATA;
3100 if (0==strcmp(".data",(char*)secname) ||
3101 0==strcmp(".bss",(char*)secname))
3102 kind = SECTIONKIND_RWDATA;
3104 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
3105 sz = sectab_i->SizeOfRawData;
3106 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
3108 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
3109 end = start + sz - 1;
3111 if (kind == SECTIONKIND_OTHER
3112 /* Ignore sections called which contain stabs debugging
3114 && 0 != strcmp(".stab", (char*)secname)
3115 && 0 != strcmp(".stabstr", (char*)secname)
3116 /* ignore constructor section for now */
3117 && 0 != strcmp(".ctors", (char*)secname)
3118 /* ignore section generated from .ident */
3119 && 0!= strncmp(".debug", (char*)secname, 6)
3120 /* ignore unknown section that appeared in gcc 3.4.5(?) */
3121 && 0!= strcmp(".reloc", (char*)secname)
3122 && 0 != strcmp(".rdata$zzz", (char*)secname)
3124 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", secname, oc->fileName);
3129 if (kind != SECTIONKIND_OTHER && end >= start) {
3130 addSection(oc, kind, start, end);
3131 addProddableBlock(oc, start, end - start + 1);
3137 /* Copy exported symbols into the ObjectCode. */
3139 oc->n_symbols = hdr->NumberOfSymbols;
3140 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3141 "ocGetNames_PEi386(oc->symbols)");
3142 /* Call me paranoid; I don't care. */
3143 for (i = 0; i < oc->n_symbols; i++)
3144 oc->symbols[i] = NULL;
3148 COFF_symbol* symtab_i;
3149 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
3150 symtab_i = (COFF_symbol*)
3151 myindex ( sizeof_COFF_symbol, symtab, i );
3155 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
3156 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
3157 /* This symbol is global and defined, viz, exported */
3158 /* for MYIMAGE_SYMCLASS_EXTERNAL
3159 && !MYIMAGE_SYM_UNDEFINED,
3160 the address of the symbol is:
3161 address of relevant section + offset in section
3163 COFF_section* sectabent
3164 = (COFF_section*) myindex ( sizeof_COFF_section,
3166 symtab_i->SectionNumber-1 );
3167 addr = ((UChar*)(oc->image))
3168 + (sectabent->PointerToRawData
3172 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
3173 && symtab_i->Value > 0) {
3174 /* This symbol isn't in any section at all, ie, global bss.
3175 Allocate zeroed space for it. */
3176 addr = stgCallocBytes(1, symtab_i->Value,
3177 "ocGetNames_PEi386(non-anonymous bss)");
3178 addSection(oc, SECTIONKIND_RWDATA, addr,
3179 ((UChar*)addr) + symtab_i->Value - 1);
3180 addProddableBlock(oc, addr, symtab_i->Value);
3181 /* debugBelch("BSS section at 0x%x\n", addr); */
3184 if (addr != NULL ) {
3185 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
3186 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
3187 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
3188 ASSERT(i >= 0 && i < oc->n_symbols);
3189 /* cstring_from_COFF_symbol_name always succeeds. */
3190 oc->symbols[i] = (char*)sname;
3191 ghciInsertStrHashTable(oc->fileName, symhash, (char*)sname, addr);
3195 "IGNORING symbol %d\n"
3199 printName ( symtab_i->Name, strtab );
3208 (Int32)(symtab_i->SectionNumber),
3209 (UInt32)symtab_i->Type,
3210 (UInt32)symtab_i->StorageClass,
3211 (UInt32)symtab_i->NumberOfAuxSymbols
3216 i += symtab_i->NumberOfAuxSymbols;
3225 ocResolve_PEi386 ( ObjectCode* oc )
3228 COFF_section* sectab;
3229 COFF_symbol* symtab;
3239 /* ToDo: should be variable-sized? But is at least safe in the
3240 sense of buffer-overrun-proof. */
3242 /* debugBelch("resolving for %s\n", oc->fileName); */
3244 hdr = (COFF_header*)(oc->image);
3245 sectab = (COFF_section*) (
3246 ((UChar*)(oc->image))
3247 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3249 symtab = (COFF_symbol*) (
3250 ((UChar*)(oc->image))
3251 + hdr->PointerToSymbolTable
3253 strtab = ((UChar*)(oc->image))
3254 + hdr->PointerToSymbolTable
3255 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3257 for (i = 0; i < hdr->NumberOfSections; i++) {
3258 COFF_section* sectab_i
3260 myindex ( sizeof_COFF_section, sectab, i );
3263 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
3266 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3268 /* Ignore sections called which contain stabs debugging
3270 if (0 == strcmp(".stab", (char*)secname)
3271 || 0 == strcmp(".stabstr", (char*)secname)
3272 || 0 == strcmp(".ctors", (char*)secname)
3273 || 0 == strncmp(".debug", (char*)secname, 6)
3274 || 0 == strcmp(".rdata$zzz", (char*)secname)) {
3281 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
3282 /* If the relocation field (a short) has overflowed, the
3283 * real count can be found in the first reloc entry.
3285 * See Section 4.1 (last para) of the PE spec (rev6.0).
3287 * Nov2003 update: the GNU linker still doesn't correctly
3288 * handle the generation of relocatable object files with
3289 * overflown relocations. Hence the output to warn of potential
3292 COFF_reloc* rel = (COFF_reloc*)
3293 myindex ( sizeof_COFF_reloc, reltab, 0 );
3294 noRelocs = rel->VirtualAddress;
3296 /* 10/05: we now assume (and check for) a GNU ld that is capable
3297 * of handling object files with (>2^16) of relocs.
3300 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
3305 noRelocs = sectab_i->NumberOfRelocations;
3310 for (; j < noRelocs; j++) {
3312 COFF_reloc* reltab_j
3314 myindex ( sizeof_COFF_reloc, reltab, j );
3316 /* the location to patch */
3318 ((UChar*)(oc->image))
3319 + (sectab_i->PointerToRawData
3320 + reltab_j->VirtualAddress
3321 - sectab_i->VirtualAddress )
3323 /* the existing contents of pP */
3325 /* the symbol to connect to */
3326 sym = (COFF_symbol*)
3327 myindex ( sizeof_COFF_symbol,
3328 symtab, reltab_j->SymbolTableIndex );
3331 "reloc sec %2d num %3d: type 0x%-4x "
3332 "vaddr 0x%-8x name `",
3334 (UInt32)reltab_j->Type,
3335 reltab_j->VirtualAddress );
3336 printName ( sym->Name, strtab );
3337 debugBelch("'\n" ));
3339 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
3340 COFF_section* section_sym
3341 = findPEi386SectionCalled ( oc, sym->Name );
3343 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
3346 S = ((UInt32)(oc->image))
3347 + (section_sym->PointerToRawData
3350 copyName ( sym->Name, strtab, symbol, 1000-1 );
3351 S = (UInt32) lookupSymbol( (char*)symbol );
3352 if ((void*)S != NULL) goto foundit;
3353 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3357 checkProddableBlock(oc, pP);
3358 switch (reltab_j->Type) {
3359 case MYIMAGE_REL_I386_DIR32:
3362 case MYIMAGE_REL_I386_REL32:
3363 /* Tricky. We have to insert a displacement at
3364 pP which, when added to the PC for the _next_
3365 insn, gives the address of the target (S).
3366 Problem is to know the address of the next insn
3367 when we only know pP. We assume that this
3368 literal field is always the last in the insn,
3369 so that the address of the next insn is pP+4
3370 -- hence the constant 4.
3371 Also I don't know if A should be added, but so
3372 far it has always been zero.
3374 SOF 05/2005: 'A' (old contents of *pP) have been observed
3375 to contain values other than zero (the 'wx' object file
3376 that came with wxhaskell-0.9.4; dunno how it was compiled..).
3377 So, add displacement to old value instead of asserting
3378 A to be zero. Fixes wxhaskell-related crashes, and no other
3379 ill effects have been observed.
3381 Update: the reason why we're seeing these more elaborate
3382 relocations is due to a switch in how the NCG compiles SRTs
3383 and offsets to them from info tables. SRTs live in .(ro)data,
3384 while info tables live in .text, causing GAS to emit REL32/DISP32
3385 relocations with non-zero values. Adding the displacement is
3386 the right thing to do.
3388 *pP = S - ((UInt32)pP) - 4 + A;
3391 debugBelch("%s: unhandled PEi386 relocation type %d",
3392 oc->fileName, reltab_j->Type);
3399 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
3403 #endif /* defined(OBJFORMAT_PEi386) */
3406 /* --------------------------------------------------------------------------
3408 * ------------------------------------------------------------------------*/
3410 #if defined(OBJFORMAT_ELF)
3415 #if defined(sparc_HOST_ARCH)
3416 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
3417 #elif defined(i386_HOST_ARCH)
3418 # define ELF_TARGET_386 /* Used inside <elf.h> */
3419 #elif defined(x86_64_HOST_ARCH)
3420 # define ELF_TARGET_X64_64
3424 #if !defined(openbsd_HOST_OS)
3427 /* openbsd elf has things in different places, with diff names */
3428 # include <elf_abi.h>
3429 # include <machine/reloc.h>
3430 # define R_386_32 RELOC_32
3431 # define R_386_PC32 RELOC_PC32
3434 /* If elf.h doesn't define it */
3435 # ifndef R_X86_64_PC64
3436 # define R_X86_64_PC64 24
3440 * Define a set of types which can be used for both ELF32 and ELF64
3444 #define ELFCLASS ELFCLASS64
3445 #define Elf_Addr Elf64_Addr
3446 #define Elf_Word Elf64_Word
3447 #define Elf_Sword Elf64_Sword
3448 #define Elf_Ehdr Elf64_Ehdr
3449 #define Elf_Phdr Elf64_Phdr
3450 #define Elf_Shdr Elf64_Shdr
3451 #define Elf_Sym Elf64_Sym
3452 #define Elf_Rel Elf64_Rel
3453 #define Elf_Rela Elf64_Rela
3455 #define ELF_ST_TYPE ELF64_ST_TYPE
3458 #define ELF_ST_BIND ELF64_ST_BIND
3461 #define ELF_R_TYPE ELF64_R_TYPE
3464 #define ELF_R_SYM ELF64_R_SYM
3467 #define ELFCLASS ELFCLASS32
3468 #define Elf_Addr Elf32_Addr
3469 #define Elf_Word Elf32_Word
3470 #define Elf_Sword Elf32_Sword
3471 #define Elf_Ehdr Elf32_Ehdr
3472 #define Elf_Phdr Elf32_Phdr
3473 #define Elf_Shdr Elf32_Shdr
3474 #define Elf_Sym Elf32_Sym
3475 #define Elf_Rel Elf32_Rel
3476 #define Elf_Rela Elf32_Rela
3478 #define ELF_ST_TYPE ELF32_ST_TYPE
3481 #define ELF_ST_BIND ELF32_ST_BIND
3484 #define ELF_R_TYPE ELF32_R_TYPE
3487 #define ELF_R_SYM ELF32_R_SYM
3493 * Functions to allocate entries in dynamic sections. Currently we simply
3494 * preallocate a large number, and we don't check if a entry for the given
3495 * target already exists (a linear search is too slow). Ideally these
3496 * entries would be associated with symbols.
3499 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
3500 #define GOT_SIZE 0x20000
3501 #define FUNCTION_TABLE_SIZE 0x10000
3502 #define PLT_SIZE 0x08000
3505 static Elf_Addr got[GOT_SIZE];
3506 static unsigned int gotIndex;
3507 static Elf_Addr gp_val = (Elf_Addr)got;
3510 allocateGOTEntry(Elf_Addr target)
3514 if (gotIndex >= GOT_SIZE)
3515 barf("Global offset table overflow");
3517 entry = &got[gotIndex++];
3519 return (Elf_Addr)entry;
3523 #ifdef ELF_FUNCTION_DESC
3529 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
3530 static unsigned int functionTableIndex;
3533 allocateFunctionDesc(Elf_Addr target)
3535 FunctionDesc *entry;
3537 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
3538 barf("Function table overflow");
3540 entry = &functionTable[functionTableIndex++];
3542 entry->gp = (Elf_Addr)gp_val;
3543 return (Elf_Addr)entry;
3547 copyFunctionDesc(Elf_Addr target)
3549 FunctionDesc *olddesc = (FunctionDesc *)target;
3550 FunctionDesc *newdesc;
3552 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
3553 newdesc->gp = olddesc->gp;
3554 return (Elf_Addr)newdesc;
3561 unsigned char code[sizeof(plt_code)];
3565 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
3567 PLTEntry *plt = (PLTEntry *)oc->plt;
3570 if (oc->pltIndex >= PLT_SIZE)
3571 barf("Procedure table overflow");
3573 entry = &plt[oc->pltIndex++];
3574 memcpy(entry->code, plt_code, sizeof(entry->code));
3575 PLT_RELOC(entry->code, target);
3576 return (Elf_Addr)entry;
3582 return (PLT_SIZE * sizeof(PLTEntry));
3588 * Generic ELF functions
3592 findElfSection ( void* objImage, Elf_Word sh_type )
3594 char* ehdrC = (char*)objImage;
3595 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3596 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3597 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3601 for (i = 0; i < ehdr->e_shnum; i++) {
3602 if (shdr[i].sh_type == sh_type
3603 /* Ignore the section header's string table. */
3604 && i != ehdr->e_shstrndx
3605 /* Ignore string tables named .stabstr, as they contain
3607 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3609 ptr = ehdrC + shdr[i].sh_offset;
3617 ocVerifyImage_ELF ( ObjectCode* oc )
3621 int i, j, nent, nstrtab, nsymtabs;
3625 char* ehdrC = (char*)(oc->image);
3626 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3628 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
3629 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
3630 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
3631 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
3632 errorBelch("%s: not an ELF object", oc->fileName);
3636 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
3637 errorBelch("%s: unsupported ELF format", oc->fileName);
3641 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
3642 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
3644 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
3645 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
3647 errorBelch("%s: unknown endiannness", oc->fileName);
3651 if (ehdr->e_type != ET_REL) {
3652 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
3655 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
3657 IF_DEBUG(linker,debugBelch( "Architecture is " ));
3658 switch (ehdr->e_machine) {
3659 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
3660 #ifdef EM_SPARC32PLUS
3661 case EM_SPARC32PLUS:
3663 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
3665 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
3667 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
3669 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
3670 #elif defined(EM_AMD64)
3671 case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
3673 default: IF_DEBUG(linker,debugBelch( "unknown" ));
3674 errorBelch("%s: unknown architecture (e_machine == %d)"
3675 , oc->fileName, ehdr->e_machine);
3679 IF_DEBUG(linker,debugBelch(
3680 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
3681 (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
3683 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
3685 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3687 if (ehdr->e_shstrndx == SHN_UNDEF) {
3688 errorBelch("%s: no section header string table", oc->fileName);
3691 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
3693 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3696 for (i = 0; i < ehdr->e_shnum; i++) {
3697 IF_DEBUG(linker,debugBelch("%2d: ", i ));
3698 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
3699 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
3700 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
3701 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
3702 ehdrC + shdr[i].sh_offset,
3703 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3705 if (shdr[i].sh_type == SHT_REL) {
3706 IF_DEBUG(linker,debugBelch("Rel " ));
3707 } else if (shdr[i].sh_type == SHT_RELA) {
3708 IF_DEBUG(linker,debugBelch("RelA " ));
3710 IF_DEBUG(linker,debugBelch(" "));
3713 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3717 IF_DEBUG(linker,debugBelch( "\nString tables" ));
3720 for (i = 0; i < ehdr->e_shnum; i++) {
3721 if (shdr[i].sh_type == SHT_STRTAB
3722 /* Ignore the section header's string table. */
3723 && i != ehdr->e_shstrndx
3724 /* Ignore string tables named .stabstr, as they contain
3726 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3728 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
3729 strtab = ehdrC + shdr[i].sh_offset;
3734 errorBelch("%s: no string tables, or too many", oc->fileName);
3739 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
3740 for (i = 0; i < ehdr->e_shnum; i++) {
3741 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3742 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3744 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3745 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3746 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
3748 (long)shdr[i].sh_size % sizeof(Elf_Sym)
3750 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
3751 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
3754 for (j = 0; j < nent; j++) {
3755 IF_DEBUG(linker,debugBelch(" %2d ", j ));
3756 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
3757 (int)stab[j].st_shndx,
3758 (int)stab[j].st_size,
3759 (char*)stab[j].st_value ));
3761 IF_DEBUG(linker,debugBelch("type=" ));
3762 switch (ELF_ST_TYPE(stab[j].st_info)) {
3763 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
3764 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
3765 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
3766 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
3767 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
3768 default: IF_DEBUG(linker,debugBelch("? " )); break;
3770 IF_DEBUG(linker,debugBelch(" " ));
3772 IF_DEBUG(linker,debugBelch("bind=" ));
3773 switch (ELF_ST_BIND(stab[j].st_info)) {
3774 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
3775 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
3776 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
3777 default: IF_DEBUG(linker,debugBelch("? " )); break;
3779 IF_DEBUG(linker,debugBelch(" " ));
3781 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
3785 if (nsymtabs == 0) {
3786 errorBelch("%s: didn't find any symbol tables", oc->fileName);
3793 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
3797 if (hdr->sh_type == SHT_PROGBITS
3798 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
3799 /* .text-style section */
3800 return SECTIONKIND_CODE_OR_RODATA;
3803 if (hdr->sh_type == SHT_PROGBITS
3804 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3805 /* .data-style section */
3806 return SECTIONKIND_RWDATA;
3809 if (hdr->sh_type == SHT_PROGBITS
3810 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
3811 /* .rodata-style section */
3812 return SECTIONKIND_CODE_OR_RODATA;
3815 if (hdr->sh_type == SHT_NOBITS
3816 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3817 /* .bss-style section */
3819 return SECTIONKIND_RWDATA;
3822 return SECTIONKIND_OTHER;
3827 ocGetNames_ELF ( ObjectCode* oc )
3832 char* ehdrC = (char*)(oc->image);
3833 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3834 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
3835 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3837 ASSERT(symhash != NULL);
3840 errorBelch("%s: no strtab", oc->fileName);
3845 for (i = 0; i < ehdr->e_shnum; i++) {
3846 /* Figure out what kind of section it is. Logic derived from
3847 Figure 1.14 ("Special Sections") of the ELF document
3848 ("Portable Formats Specification, Version 1.1"). */
3850 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
3852 if (is_bss && shdr[i].sh_size > 0) {
3853 /* This is a non-empty .bss section. Allocate zeroed space for
3854 it, and set its .sh_offset field such that
3855 ehdrC + .sh_offset == addr_of_zeroed_space. */
3856 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
3857 "ocGetNames_ELF(BSS)");
3858 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
3860 debugBelch("BSS section at 0x%x, size %d\n",
3861 zspace, shdr[i].sh_size);
3865 /* fill in the section info */
3866 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
3867 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
3868 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
3869 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
3872 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3874 /* copy stuff into this module's object symbol table */
3875 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3876 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3878 oc->n_symbols = nent;
3879 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3880 "ocGetNames_ELF(oc->symbols)");
3882 for (j = 0; j < nent; j++) {
3884 char isLocal = FALSE; /* avoids uninit-var warning */
3886 char* nm = strtab + stab[j].st_name;
3887 int secno = stab[j].st_shndx;
3889 /* Figure out if we want to add it; if so, set ad to its
3890 address. Otherwise leave ad == NULL. */
3892 if (secno == SHN_COMMON) {
3894 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
3896 debugBelch("COMMON symbol, size %d name %s\n",
3897 stab[j].st_size, nm);
3899 /* Pointless to do addProddableBlock() for this area,
3900 since the linker should never poke around in it. */
3903 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
3904 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
3906 /* and not an undefined symbol */
3907 && stab[j].st_shndx != SHN_UNDEF
3908 /* and not in a "special section" */
3909 && stab[j].st_shndx < SHN_LORESERVE
3911 /* and it's a not a section or string table or anything silly */
3912 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3913 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3914 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3917 /* Section 0 is the undefined section, hence > and not >=. */
3918 ASSERT(secno > 0 && secno < ehdr->e_shnum);
3920 if (shdr[secno].sh_type == SHT_NOBITS) {
3921 debugBelch(" BSS symbol, size %d off %d name %s\n",
3922 stab[j].st_size, stab[j].st_value, nm);
3925 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3926 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3929 #ifdef ELF_FUNCTION_DESC
3930 /* dlsym() and the initialisation table both give us function
3931 * descriptors, so to be consistent we store function descriptors
3932 * in the symbol table */
3933 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3934 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3936 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n",
3937 ad, oc->fileName, nm ));
3942 /* And the decision is ... */
3946 oc->symbols[j] = nm;
3949 /* Ignore entirely. */
3951 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3955 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3956 strtab + stab[j].st_name ));
3959 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3960 (int)ELF_ST_BIND(stab[j].st_info),
3961 (int)ELF_ST_TYPE(stab[j].st_info),
3962 (int)stab[j].st_shndx,
3963 strtab + stab[j].st_name
3966 oc->symbols[j] = NULL;
3975 /* Do ELF relocations which lack an explicit addend. All x86-linux
3976 relocations appear to be of this form. */
3978 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3979 Elf_Shdr* shdr, int shnum,
3980 Elf_Sym* stab, char* strtab )
3985 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3986 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3987 int target_shndx = shdr[shnum].sh_info;
3988 int symtab_shndx = shdr[shnum].sh_link;
3990 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3991 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3992 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3993 target_shndx, symtab_shndx ));
3995 /* Skip sections that we're not interested in. */
3998 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3999 if (kind == SECTIONKIND_OTHER) {
4000 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
4005 for (j = 0; j < nent; j++) {
4006 Elf_Addr offset = rtab[j].r_offset;
4007 Elf_Addr info = rtab[j].r_info;
4009 Elf_Addr P = ((Elf_Addr)targ) + offset;
4010 Elf_Word* pP = (Elf_Word*)P;
4015 StgStablePtr stablePtr;
4018 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
4019 j, (void*)offset, (void*)info ));
4021 IF_DEBUG(linker,debugBelch( " ZERO" ));
4024 Elf_Sym sym = stab[ELF_R_SYM(info)];
4025 /* First see if it is a local symbol. */
4026 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
4027 /* Yes, so we can get the address directly from the ELF symbol
4029 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
4031 (ehdrC + shdr[ sym.st_shndx ].sh_offset
4032 + stab[ELF_R_SYM(info)].st_value);
4035 symbol = strtab + sym.st_name;
4036 stablePtr = (StgStablePtr)lookupHashTable(stablehash, (StgWord)symbol);
4037 if (NULL == stablePtr) {
4038 /* No, so look up the name in our global table. */
4039 S_tmp = lookupSymbol( symbol );
4040 S = (Elf_Addr)S_tmp;
4042 stableVal = deRefStablePtr( stablePtr );
4044 S = (Elf_Addr)S_tmp;
4048 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4051 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
4054 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
4055 (void*)P, (void*)S, (void*)A ));
4056 checkProddableBlock ( oc, pP );
4060 switch (ELF_R_TYPE(info)) {
4061 # ifdef i386_HOST_ARCH
4062 case R_386_32: *pP = value; break;
4063 case R_386_PC32: *pP = value - P; break;
4066 errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
4067 oc->fileName, (lnat)ELF_R_TYPE(info));
4075 /* Do ELF relocations for which explicit addends are supplied.
4076 sparc-solaris relocations appear to be of this form. */
4078 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
4079 Elf_Shdr* shdr, int shnum,
4080 Elf_Sym* stab, char* strtab )
4083 char *symbol = NULL;
4085 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
4086 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
4087 int target_shndx = shdr[shnum].sh_info;
4088 int symtab_shndx = shdr[shnum].sh_link;
4090 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
4091 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
4092 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
4093 target_shndx, symtab_shndx ));
4095 for (j = 0; j < nent; j++) {
4096 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4097 /* This #ifdef only serves to avoid unused-var warnings. */
4098 Elf_Addr offset = rtab[j].r_offset;
4099 Elf_Addr P = targ + offset;
4101 Elf_Addr info = rtab[j].r_info;
4102 Elf_Addr A = rtab[j].r_addend;
4106 # if defined(sparc_HOST_ARCH)
4107 Elf_Word* pP = (Elf_Word*)P;
4109 # elif defined(powerpc_HOST_ARCH)
4113 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
4114 j, (void*)offset, (void*)info,
4117 IF_DEBUG(linker,debugBelch( " ZERO" ));
4120 Elf_Sym sym = stab[ELF_R_SYM(info)];
4121 /* First see if it is a local symbol. */
4122 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
4123 /* Yes, so we can get the address directly from the ELF symbol
4125 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
4127 (ehdrC + shdr[ sym.st_shndx ].sh_offset
4128 + stab[ELF_R_SYM(info)].st_value);
4129 #ifdef ELF_FUNCTION_DESC
4130 /* Make a function descriptor for this function */
4131 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
4132 S = allocateFunctionDesc(S + A);
4137 /* No, so look up the name in our global table. */
4138 symbol = strtab + sym.st_name;
4139 S_tmp = lookupSymbol( symbol );
4140 S = (Elf_Addr)S_tmp;
4142 #ifdef ELF_FUNCTION_DESC
4143 /* If a function, already a function descriptor - we would
4144 have to copy it to add an offset. */
4145 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
4146 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
4150 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4153 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
4156 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
4157 (void*)P, (void*)S, (void*)A ));
4158 /* checkProddableBlock ( oc, (void*)P ); */
4162 switch (ELF_R_TYPE(info)) {
4163 # if defined(sparc_HOST_ARCH)
4164 case R_SPARC_WDISP30:
4165 w1 = *pP & 0xC0000000;
4166 w2 = (Elf_Word)((value - P) >> 2);
4167 ASSERT((w2 & 0xC0000000) == 0);
4172 w1 = *pP & 0xFFC00000;
4173 w2 = (Elf_Word)(value >> 10);
4174 ASSERT((w2 & 0xFFC00000) == 0);
4180 w2 = (Elf_Word)(value & 0x3FF);
4181 ASSERT((w2 & ~0x3FF) == 0);
4186 /* According to the Sun documentation:
4188 This relocation type resembles R_SPARC_32, except it refers to an
4189 unaligned word. That is, the word to be relocated must be treated
4190 as four separate bytes with arbitrary alignment, not as a word
4191 aligned according to the architecture requirements.
4194 w2 = (Elf_Word)value;
4196 // SPARC doesn't do misaligned writes of 32 bit words,
4197 // so we have to do this one byte-at-a-time.
4198 char *pPc = (char*)pP;
4199 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
4200 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
4201 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
4202 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
4206 w2 = (Elf_Word)value;
4209 # elif defined(powerpc_HOST_ARCH)
4210 case R_PPC_ADDR16_LO:
4211 *(Elf32_Half*) P = value;
4214 case R_PPC_ADDR16_HI:
4215 *(Elf32_Half*) P = value >> 16;
4218 case R_PPC_ADDR16_HA:
4219 *(Elf32_Half*) P = (value + 0x8000) >> 16;
4223 *(Elf32_Word *) P = value;
4227 *(Elf32_Word *) P = value - P;
4233 if( delta << 6 >> 6 != delta )
4235 value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
4239 if( value == 0 || delta << 6 >> 6 != delta )
4241 barf( "Unable to make SymbolExtra for #%d",
4247 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
4248 | (delta & 0x3fffffc);
4252 #if x86_64_HOST_ARCH
4254 *(Elf64_Xword *)P = value;
4259 #if defined(ALWAYS_PIC)
4260 barf("R_X86_64_PC32 relocation, but ALWAYS_PIC.");
4262 StgInt64 off = value - P;
4263 if (off >= 0x7fffffffL || off < -0x80000000L) {
4264 #if X86_64_ELF_NONPIC_HACK
4265 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4267 off = pltAddress + A - P;
4269 barf("R_X86_64_PC32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4270 symbol, off, oc->fileName );
4273 *(Elf64_Word *)P = (Elf64_Word)off;
4280 StgInt64 off = value - P;
4281 *(Elf64_Word *)P = (Elf64_Word)off;
4286 #if defined(ALWAYS_PIC)
4287 barf("R_X86_64_32 relocation, but ALWAYS_PIC.");
4289 if (value >= 0x7fffffffL) {
4290 #if X86_64_ELF_NONPIC_HACK
4291 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4293 value = pltAddress + A;
4295 barf("R_X86_64_32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4296 symbol, value, oc->fileName );
4299 *(Elf64_Word *)P = (Elf64_Word)value;
4304 #if defined(ALWAYS_PIC)
4305 barf("R_X86_64_32S relocation, but ALWAYS_PIC.");
4307 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
4308 #if X86_64_ELF_NONPIC_HACK
4309 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4311 value = pltAddress + A;
4313 barf("R_X86_64_32S relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4314 symbol, value, oc->fileName );
4317 *(Elf64_Sword *)P = (Elf64_Sword)value;
4321 case R_X86_64_GOTPCREL:
4323 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
4324 StgInt64 off = gotAddress + A - P;
4325 *(Elf64_Word *)P = (Elf64_Word)off;
4329 case R_X86_64_PLT32:
4331 #if defined(ALWAYS_PIC)
4332 barf("R_X86_64_PLT32 relocation, but ALWAYS_PIC.");
4334 StgInt64 off = value - P;
4335 if (off >= 0x7fffffffL || off < -0x80000000L) {
4336 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4338 off = pltAddress + A - P;
4340 *(Elf64_Word *)P = (Elf64_Word)off;
4347 errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
4348 oc->fileName, (lnat)ELF_R_TYPE(info));
4357 ocResolve_ELF ( ObjectCode* oc )
4361 Elf_Sym* stab = NULL;
4362 char* ehdrC = (char*)(oc->image);
4363 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
4364 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
4366 /* first find "the" symbol table */
4367 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
4369 /* also go find the string table */
4370 strtab = findElfSection ( ehdrC, SHT_STRTAB );
4372 if (stab == NULL || strtab == NULL) {
4373 errorBelch("%s: can't find string or symbol table", oc->fileName);
4377 /* Process the relocation sections. */
4378 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
4379 if (shdr[shnum].sh_type == SHT_REL) {
4380 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
4381 shnum, stab, strtab );
4385 if (shdr[shnum].sh_type == SHT_RELA) {
4386 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
4387 shnum, stab, strtab );
4392 #if defined(powerpc_HOST_ARCH)
4393 ocFlushInstructionCache( oc );
4400 * PowerPC & X86_64 ELF specifics
4403 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4405 static int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
4411 ehdr = (Elf_Ehdr *) oc->image;
4412 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
4414 for( i = 0; i < ehdr->e_shnum; i++ )
4415 if( shdr[i].sh_type == SHT_SYMTAB )
4418 if( i == ehdr->e_shnum )
4420 errorBelch( "This ELF file contains no symtab" );
4424 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
4426 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
4427 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
4432 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
4435 #endif /* powerpc */
4439 /* --------------------------------------------------------------------------
4441 * ------------------------------------------------------------------------*/
4443 #if defined(OBJFORMAT_MACHO)
4446 Support for MachO linking on Darwin/MacOS X
4447 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
4449 I hereby formally apologize for the hackish nature of this code.
4450 Things that need to be done:
4451 *) implement ocVerifyImage_MachO
4452 *) add still more sanity checks.
4455 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4456 #define mach_header mach_header_64
4457 #define segment_command segment_command_64
4458 #define section section_64
4459 #define nlist nlist_64
4462 #ifdef powerpc_HOST_ARCH
4464 ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4466 struct mach_header *header = (struct mach_header *) oc->image;
4467 struct load_command *lc = (struct load_command *) (header + 1);
4470 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: start\n"));
4472 for (i = 0; i < header->ncmds; i++) {
4473 if (lc->cmd == LC_SYMTAB) {
4475 // Find out the first and last undefined external
4476 // symbol, so we don't have to allocate too many
4477 // jump islands/GOT entries.
4479 struct symtab_command *symLC = (struct symtab_command *) lc;
4480 unsigned min = symLC->nsyms, max = 0;
4481 struct nlist *nlist =
4482 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
4485 for (i = 0; i < symLC->nsyms; i++) {
4487 if (nlist[i].n_type & N_STAB) {
4489 } else if (nlist[i].n_type & N_EXT) {
4491 if((nlist[i].n_type & N_TYPE) == N_UNDF
4492 && (nlist[i].n_value == 0)) {
4506 return ocAllocateSymbolExtras(oc, max - min + 1, min);
4512 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4515 return ocAllocateSymbolExtras(oc,0,0);
4519 #ifdef x86_64_HOST_ARCH
4521 ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4523 struct mach_header *header = (struct mach_header *) oc->image;
4524 struct load_command *lc = (struct load_command *) (header + 1);
4527 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: start\n"));
4529 for (i = 0; i < header->ncmds; i++) {
4530 if (lc->cmd == LC_SYMTAB) {
4532 // Just allocate one entry for every symbol
4533 struct symtab_command *symLC = (struct symtab_command *) lc;
4535 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: allocate %d symbols\n", symLC->nsyms));
4536 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: done\n"));
4537 return ocAllocateSymbolExtras(oc, symLC->nsyms, 0);
4540 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4543 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: allocated no symbols\n"));
4544 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: done\n"));
4545 return ocAllocateSymbolExtras(oc,0,0);
4550 ocVerifyImage_MachO(ObjectCode * oc)
4552 char *image = (char*) oc->image;
4553 struct mach_header *header = (struct mach_header*) image;
4555 IF_DEBUG(linker, debugBelch("ocVerifyImage_MachO: start\n"));
4557 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4558 if(header->magic != MH_MAGIC_64) {
4559 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4560 oc->fileName, MH_MAGIC_64, header->magic);
4564 if(header->magic != MH_MAGIC) {
4565 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4566 oc->fileName, MH_MAGIC, header->magic);
4571 // FIXME: do some more verifying here
4572 IF_DEBUG(linker, debugBelch("ocVerifyImage_MachO: done\n"));
4580 struct symtab_command *symLC,
4581 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
4582 unsigned long *indirectSyms,
4583 struct nlist *nlist)
4586 size_t itemSize = 4;
4588 IF_DEBUG(linker, debugBelch("resolveImports: start\n"));
4591 int isJumpTable = 0;
4593 if (strcmp(sect->sectname,"__jump_table") == 0) {
4596 ASSERT(sect->reserved2 == itemSize);
4601 for(i=0; i*itemSize < sect->size;i++)
4603 // according to otool, reserved1 contains the first index into the indirect symbol table
4604 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
4605 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4608 IF_DEBUG(linker, debugBelch("resolveImports: resolving %s\n", nm));
4610 if ((symbol->n_type & N_TYPE) == N_UNDF
4611 && (symbol->n_type & N_EXT) && (symbol->n_value != 0)) {
4612 addr = (void*) (symbol->n_value);
4613 IF_DEBUG(linker, debugBelch("resolveImports: undefined external %s has value %p\n", nm, addr));
4615 addr = lookupSymbol(nm);
4616 IF_DEBUG(linker, debugBelch("resolveImports: looking up %s, %p\n", nm, addr));
4620 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
4627 checkProddableBlock(oc,image + sect->offset + i*itemSize);
4629 *(image + sect->offset + i * itemSize) = 0xe9; // jmp opcode
4630 *(unsigned*)(image + sect->offset + i*itemSize + 1)
4631 = (char*)addr - (image + sect->offset + i*itemSize + 5);
4636 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
4637 ((void**)(image + sect->offset))[i] = addr;
4641 IF_DEBUG(linker, debugBelch("resolveImports: done\n"));
4645 static unsigned long relocateAddress(
4648 struct section* sections,
4649 unsigned long address)
4652 IF_DEBUG(linker, debugBelch("relocateAddress: start\n"));
4653 for (i = 0; i < nSections; i++)
4655 IF_DEBUG(linker, debugBelch(" relocating address in section %d\n", i));
4656 if (sections[i].addr <= address
4657 && address < sections[i].addr + sections[i].size)
4659 return (unsigned long)oc->image
4660 + sections[i].offset + address - sections[i].addr;
4663 barf("Invalid Mach-O file:"
4664 "Address out of bounds while relocating object file");
4668 static int relocateSection(
4671 struct symtab_command *symLC, struct nlist *nlist,
4672 int nSections, struct section* sections, struct section *sect)
4674 struct relocation_info *relocs;
4677 IF_DEBUG(linker, debugBelch("relocateSection: start\n"));
4679 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
4681 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
4683 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
4685 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
4689 IF_DEBUG(linker, debugBelch("relocateSection: number of relocations: %d\n", n));
4691 relocs = (struct relocation_info*) (image + sect->reloff);
4695 #ifdef x86_64_HOST_ARCH
4696 struct relocation_info *reloc = &relocs[i];
4698 char *thingPtr = image + sect->offset + reloc->r_address;
4700 /* We shouldn't need to initialise this, but gcc on OS X 64 bit
4701 complains that it may be used uninitialized if we don't */
4704 int type = reloc->r_type;
4706 checkProddableBlock(oc,thingPtr);
4707 switch(reloc->r_length)
4710 thing = *(uint8_t*)thingPtr;
4711 baseValue = (uint64_t)thingPtr + 1;
4714 thing = *(uint16_t*)thingPtr;
4715 baseValue = (uint64_t)thingPtr + 2;
4718 thing = *(uint32_t*)thingPtr;
4719 baseValue = (uint64_t)thingPtr + 4;
4722 thing = *(uint64_t*)thingPtr;
4723 baseValue = (uint64_t)thingPtr + 8;
4726 barf("Unknown size.");
4730 debugBelch("relocateSection: length = %d, thing = %" PRId64 ", baseValue = %p\n",
4731 reloc->r_length, thing, (char *)baseValue));
4733 if (type == X86_64_RELOC_GOT
4734 || type == X86_64_RELOC_GOT_LOAD)
4736 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4737 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4739 IF_DEBUG(linker, debugBelch("relocateSection: making jump island for %s, extern = %d, X86_64_RELOC_GOT\n", nm, reloc->r_extern));
4740 ASSERT(reloc->r_extern);
4741 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, (unsigned long)lookupSymbol(nm))->addr;
4743 type = X86_64_RELOC_SIGNED;
4745 else if(reloc->r_extern)
4747 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4748 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4750 IF_DEBUG(linker, debugBelch("relocateSection: looking up external symbol %s\n", nm));
4751 IF_DEBUG(linker, debugBelch(" : type = %d\n", symbol->n_type));
4752 IF_DEBUG(linker, debugBelch(" : sect = %d\n", symbol->n_sect));
4753 IF_DEBUG(linker, debugBelch(" : desc = %d\n", symbol->n_desc));
4754 IF_DEBUG(linker, debugBelch(" : value = %p\n", (void *)symbol->n_value));
4755 if ((symbol->n_type & N_TYPE) == N_SECT) {
4756 value = relocateAddress(oc, nSections, sections,
4758 IF_DEBUG(linker, debugBelch("relocateSection, defined external symbol %s, relocated address %p\n", nm, (void *)value));
4761 value = (uint64_t) lookupSymbol(nm);
4762 IF_DEBUG(linker, debugBelch("relocateSection: external symbol %s, address %p\n", nm, (void *)value));
4767 // If the relocation is not through the global offset table
4768 // or external, then set the value to the baseValue. This
4769 // will leave displacements into the __const section
4770 // unchanged (as they ought to be).
4775 IF_DEBUG(linker, debugBelch("relocateSection: value = %p\n", (void *)value));
4777 if (type == X86_64_RELOC_BRANCH)
4779 if((int32_t)(value - baseValue) != (int64_t)(value - baseValue))
4781 ASSERT(reloc->r_extern);
4782 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, value)
4785 ASSERT((int32_t)(value - baseValue) == (int64_t)(value - baseValue));
4786 type = X86_64_RELOC_SIGNED;
4791 case X86_64_RELOC_UNSIGNED:
4792 ASSERT(!reloc->r_pcrel);
4795 case X86_64_RELOC_SIGNED:
4796 case X86_64_RELOC_SIGNED_1:
4797 case X86_64_RELOC_SIGNED_2:
4798 case X86_64_RELOC_SIGNED_4:
4799 ASSERT(reloc->r_pcrel);
4800 thing += value - baseValue;
4802 case X86_64_RELOC_SUBTRACTOR:
4803 ASSERT(!reloc->r_pcrel);
4807 barf("unkown relocation");
4810 switch(reloc->r_length)
4813 *(uint8_t*)thingPtr = thing;
4816 *(uint16_t*)thingPtr = thing;
4819 *(uint32_t*)thingPtr = thing;
4822 *(uint64_t*)thingPtr = thing;
4826 if(relocs[i].r_address & R_SCATTERED)
4828 struct scattered_relocation_info *scat =
4829 (struct scattered_relocation_info*) &relocs[i];
4833 if(scat->r_length == 2)
4835 unsigned long word = 0;
4836 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
4837 checkProddableBlock(oc,wordPtr);
4839 // Note on relocation types:
4840 // i386 uses the GENERIC_RELOC_* types,
4841 // while ppc uses special PPC_RELOC_* types.
4842 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
4843 // in both cases, all others are different.
4844 // Therefore, we use GENERIC_RELOC_VANILLA
4845 // and GENERIC_RELOC_PAIR instead of the PPC variants,
4846 // and use #ifdefs for the other types.
4848 // Step 1: Figure out what the relocated value should be
4849 if(scat->r_type == GENERIC_RELOC_VANILLA)
4851 word = *wordPtr + (unsigned long) relocateAddress(
4858 #ifdef powerpc_HOST_ARCH
4859 else if(scat->r_type == PPC_RELOC_SECTDIFF
4860 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
4861 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
4862 || scat->r_type == PPC_RELOC_HA16_SECTDIFF
4863 || scat->r_type == PPC_RELOC_LOCAL_SECTDIFF)
4865 else if(scat->r_type == GENERIC_RELOC_SECTDIFF
4866 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4869 struct scattered_relocation_info *pair =
4870 (struct scattered_relocation_info*) &relocs[i+1];
4872 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
4873 barf("Invalid Mach-O file: "
4874 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
4876 word = (unsigned long)
4877 (relocateAddress(oc, nSections, sections, scat->r_value)
4878 - relocateAddress(oc, nSections, sections, pair->r_value));
4881 #ifdef powerpc_HOST_ARCH
4882 else if(scat->r_type == PPC_RELOC_HI16
4883 || scat->r_type == PPC_RELOC_LO16
4884 || scat->r_type == PPC_RELOC_HA16
4885 || scat->r_type == PPC_RELOC_LO14)
4886 { // these are generated by label+offset things
4887 struct relocation_info *pair = &relocs[i+1];
4888 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
4889 barf("Invalid Mach-O file: "
4890 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
4892 if(scat->r_type == PPC_RELOC_LO16)
4894 word = ((unsigned short*) wordPtr)[1];
4895 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4897 else if(scat->r_type == PPC_RELOC_LO14)
4899 barf("Unsupported Relocation: PPC_RELOC_LO14");
4900 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
4901 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4903 else if(scat->r_type == PPC_RELOC_HI16)
4905 word = ((unsigned short*) wordPtr)[1] << 16;
4906 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4908 else if(scat->r_type == PPC_RELOC_HA16)
4910 word = ((unsigned short*) wordPtr)[1] << 16;
4911 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4915 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
4923 barf ("Don't know how to handle this Mach-O "
4924 "scattered relocation entry: "
4925 "object file %s; entry type %ld; "
4927 OC_INFORMATIVE_FILENAME(oc),
4933 #ifdef powerpc_HOST_ARCH
4934 if(scat->r_type == GENERIC_RELOC_VANILLA
4935 || scat->r_type == PPC_RELOC_SECTDIFF)
4937 if(scat->r_type == GENERIC_RELOC_VANILLA
4938 || scat->r_type == GENERIC_RELOC_SECTDIFF
4939 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4944 #ifdef powerpc_HOST_ARCH
4945 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
4947 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4949 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
4951 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4953 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
4955 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4956 + ((word & (1<<15)) ? 1 : 0);
4962 barf("Can't handle Mach-O scattered relocation entry "
4963 "with this r_length tag: "
4964 "object file %s; entry type %ld; "
4965 "r_length tag %ld; address %#lx\n",
4966 OC_INFORMATIVE_FILENAME(oc),
4973 else /* scat->r_pcrel */
4975 barf("Don't know how to handle *PC-relative* Mach-O "
4976 "scattered relocation entry: "
4977 "object file %s; entry type %ld; address %#lx\n",
4978 OC_INFORMATIVE_FILENAME(oc),
4985 else /* !(relocs[i].r_address & R_SCATTERED) */
4987 struct relocation_info *reloc = &relocs[i];
4988 if(reloc->r_pcrel && !reloc->r_extern)
4991 if(reloc->r_length == 2)
4993 unsigned long word = 0;
4994 #ifdef powerpc_HOST_ARCH
4995 unsigned long jumpIsland = 0;
4996 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
4997 // to avoid warning and to catch
5001 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
5002 checkProddableBlock(oc,wordPtr);
5004 if(reloc->r_type == GENERIC_RELOC_VANILLA)
5008 #ifdef powerpc_HOST_ARCH
5009 else if(reloc->r_type == PPC_RELOC_LO16)
5011 word = ((unsigned short*) wordPtr)[1];
5012 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
5014 else if(reloc->r_type == PPC_RELOC_HI16)
5016 word = ((unsigned short*) wordPtr)[1] << 16;
5017 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
5019 else if(reloc->r_type == PPC_RELOC_HA16)
5021 word = ((unsigned short*) wordPtr)[1] << 16;
5022 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
5024 else if(reloc->r_type == PPC_RELOC_BR24)
5027 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
5032 barf("Can't handle this Mach-O relocation entry "
5034 "object file %s; entry type %ld; address %#lx\n",
5035 OC_INFORMATIVE_FILENAME(oc),
5041 if(!reloc->r_extern)
5044 sections[reloc->r_symbolnum-1].offset
5045 - sections[reloc->r_symbolnum-1].addr
5052 struct nlist *symbol = &nlist[reloc->r_symbolnum];
5053 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
5054 void *symbolAddress = lookupSymbol(nm);
5057 errorBelch("\nunknown symbol `%s'", nm);
5063 #ifdef powerpc_HOST_ARCH
5064 // In the .o file, this should be a relative jump to NULL
5065 // and we'll change it to a relative jump to the symbol
5066 ASSERT(word + reloc->r_address == 0);
5067 jumpIsland = (unsigned long)
5068 &makeSymbolExtra(oc,
5070 (unsigned long) symbolAddress)
5074 offsetToJumpIsland = word + jumpIsland
5075 - (((long)image) + sect->offset - sect->addr);
5078 word += (unsigned long) symbolAddress
5079 - (((long)image) + sect->offset - sect->addr);
5083 word += (unsigned long) symbolAddress;
5087 if(reloc->r_type == GENERIC_RELOC_VANILLA)
5092 #ifdef powerpc_HOST_ARCH
5093 else if(reloc->r_type == PPC_RELOC_LO16)
5095 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
5098 else if(reloc->r_type == PPC_RELOC_HI16)
5100 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
5103 else if(reloc->r_type == PPC_RELOC_HA16)
5105 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
5106 + ((word & (1<<15)) ? 1 : 0);
5109 else if(reloc->r_type == PPC_RELOC_BR24)
5111 if((word & 0x03) != 0)
5112 barf("%s: unconditional relative branch with a displacement "
5113 "which isn't a multiple of 4 bytes: %#lx",
5114 OC_INFORMATIVE_FILENAME(oc),
5117 if((word & 0xFE000000) != 0xFE000000 &&
5118 (word & 0xFE000000) != 0x00000000)
5120 // The branch offset is too large.
5121 // Therefore, we try to use a jump island.
5124 barf("%s: unconditional relative branch out of range: "
5125 "no jump island available: %#lx",
5126 OC_INFORMATIVE_FILENAME(oc),
5130 word = offsetToJumpIsland;
5131 if((word & 0xFE000000) != 0xFE000000 &&
5132 (word & 0xFE000000) != 0x00000000)
5133 barf("%s: unconditional relative branch out of range: "
5134 "jump island out of range: %#lx",
5135 OC_INFORMATIVE_FILENAME(oc),
5138 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
5145 barf("Can't handle Mach-O relocation entry (not scattered) "
5146 "with this r_length tag: "
5147 "object file %s; entry type %ld; "
5148 "r_length tag %ld; address %#lx\n",
5149 OC_INFORMATIVE_FILENAME(oc),
5158 IF_DEBUG(linker, debugBelch("relocateSection: done\n"));
5162 static int ocGetNames_MachO(ObjectCode* oc)
5164 char *image = (char*) oc->image;
5165 struct mach_header *header = (struct mach_header*) image;
5166 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5167 unsigned i,curSymbol = 0;
5168 struct segment_command *segLC = NULL;
5169 struct section *sections;
5170 struct symtab_command *symLC = NULL;
5171 struct nlist *nlist;
5172 unsigned long commonSize = 0;
5173 char *commonStorage = NULL;
5174 unsigned long commonCounter;
5176 IF_DEBUG(linker,debugBelch("ocGetNames_MachO: start\n"));
5178 for(i=0;i<header->ncmds;i++)
5180 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
5181 segLC = (struct segment_command*) lc;
5182 else if(lc->cmd == LC_SYMTAB)
5183 symLC = (struct symtab_command*) lc;
5184 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5187 sections = (struct section*) (segLC+1);
5188 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5192 barf("ocGetNames_MachO: no segment load command");
5194 for(i=0;i<segLC->nsects;i++)
5196 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: segment %d\n", i));
5197 if (sections[i].size == 0)
5200 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
5202 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
5203 "ocGetNames_MachO(common symbols)");
5204 sections[i].offset = zeroFillArea - image;
5207 if(!strcmp(sections[i].sectname,"__text"))
5208 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
5209 (void*) (image + sections[i].offset),
5210 (void*) (image + sections[i].offset + sections[i].size));
5211 else if(!strcmp(sections[i].sectname,"__const"))
5212 addSection(oc, SECTIONKIND_RWDATA,
5213 (void*) (image + sections[i].offset),
5214 (void*) (image + sections[i].offset + sections[i].size));
5215 else if(!strcmp(sections[i].sectname,"__data"))
5216 addSection(oc, SECTIONKIND_RWDATA,
5217 (void*) (image + sections[i].offset),
5218 (void*) (image + sections[i].offset + sections[i].size));
5219 else if(!strcmp(sections[i].sectname,"__bss")
5220 || !strcmp(sections[i].sectname,"__common"))
5221 addSection(oc, SECTIONKIND_RWDATA,
5222 (void*) (image + sections[i].offset),
5223 (void*) (image + sections[i].offset + sections[i].size));
5225 addProddableBlock(oc, (void*) (image + sections[i].offset),
5229 // count external symbols defined here
5233 for(i=0;i<symLC->nsyms;i++)
5235 if(nlist[i].n_type & N_STAB)
5237 else if(nlist[i].n_type & N_EXT)
5239 if((nlist[i].n_type & N_TYPE) == N_UNDF
5240 && (nlist[i].n_value != 0))
5242 commonSize += nlist[i].n_value;
5245 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5250 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: %d external symbols\n", oc->n_symbols));
5251 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
5252 "ocGetNames_MachO(oc->symbols)");
5256 for(i=0;i<symLC->nsyms;i++)
5258 if(nlist[i].n_type & N_STAB)
5260 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5262 if(nlist[i].n_type & N_EXT)
5264 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5265 if ((nlist[i].n_desc & N_WEAK_DEF) && lookupSymbol(nm)) {
5266 // weak definition, and we already have a definition
5267 IF_DEBUG(linker, debugBelch(" weak: %s\n", nm));
5271 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting %s\n", nm));
5272 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5274 + sections[nlist[i].n_sect-1].offset
5275 - sections[nlist[i].n_sect-1].addr
5276 + nlist[i].n_value);
5277 oc->symbols[curSymbol++] = nm;
5284 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
5285 commonCounter = (unsigned long)commonStorage;
5288 for(i=0;i<symLC->nsyms;i++)
5290 if((nlist[i].n_type & N_TYPE) == N_UNDF
5291 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
5293 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5294 unsigned long sz = nlist[i].n_value;
5296 nlist[i].n_value = commonCounter;
5298 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting common symbol: %s\n", nm));
5299 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5300 (void*)commonCounter);
5301 oc->symbols[curSymbol++] = nm;
5303 commonCounter += sz;
5310 static int ocResolve_MachO(ObjectCode* oc)
5312 char *image = (char*) oc->image;
5313 struct mach_header *header = (struct mach_header*) image;
5314 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5316 struct segment_command *segLC = NULL;
5317 struct section *sections;
5318 struct symtab_command *symLC = NULL;
5319 struct dysymtab_command *dsymLC = NULL;
5320 struct nlist *nlist;
5322 IF_DEBUG(linker, debugBelch("ocResolve_MachO: start\n"));
5323 for (i = 0; i < header->ncmds; i++)
5325 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
5326 segLC = (struct segment_command*) lc;
5327 else if(lc->cmd == LC_SYMTAB)
5328 symLC = (struct symtab_command*) lc;
5329 else if(lc->cmd == LC_DYSYMTAB)
5330 dsymLC = (struct dysymtab_command*) lc;
5331 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5334 sections = (struct section*) (segLC+1);
5335 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5340 unsigned long *indirectSyms
5341 = (unsigned long*) (image + dsymLC->indirectsymoff);
5343 IF_DEBUG(linker, debugBelch("ocResolve_MachO: resolving dsymLC\n"));
5344 for (i = 0; i < segLC->nsects; i++)
5346 if( !strcmp(sections[i].sectname,"__la_symbol_ptr")
5347 || !strcmp(sections[i].sectname,"__la_sym_ptr2")
5348 || !strcmp(sections[i].sectname,"__la_sym_ptr3"))
5350 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5353 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr")
5354 || !strcmp(sections[i].sectname,"__pointers"))
5356 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5359 else if(!strcmp(sections[i].sectname,"__jump_table"))
5361 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5366 IF_DEBUG(linker, debugBelch("ocResolve_MachO: unknown section\n"));
5371 for(i=0;i<segLC->nsects;i++)
5373 IF_DEBUG(linker, debugBelch("ocResolve_MachO: relocating section %d\n", i));
5375 if (!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
5379 #if defined (powerpc_HOST_ARCH)
5380 ocFlushInstructionCache( oc );
5386 #ifdef powerpc_HOST_ARCH
5388 * The Mach-O object format uses leading underscores. But not everywhere.
5389 * There is a small number of runtime support functions defined in
5390 * libcc_dynamic.a whose name does not have a leading underscore.
5391 * As a consequence, we can't get their address from C code.
5392 * We have to use inline assembler just to take the address of a function.
5396 extern void* symbolsWithoutUnderscore[];
5398 static void machoInitSymbolsWithoutUnderscore()
5400 void **p = symbolsWithoutUnderscore;
5401 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
5403 #undef SymI_NeedsProto
5404 #define SymI_NeedsProto(x) \
5405 __asm__ volatile(".long " # x);
5407 RTS_MACHO_NOUNDERLINE_SYMBOLS
5409 __asm__ volatile(".text");
5411 #undef SymI_NeedsProto
5412 #define SymI_NeedsProto(x) \
5413 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
5415 RTS_MACHO_NOUNDERLINE_SYMBOLS
5417 #undef SymI_NeedsProto
5423 * Figure out by how much to shift the entire Mach-O file in memory
5424 * when loading so that its single segment ends up 16-byte-aligned
5426 static int machoGetMisalignment( FILE * f )
5428 struct mach_header header;
5432 int n = fread(&header, sizeof(header), 1, f);
5434 barf("machoGetMisalignment: can't read the Mach-O header");
5437 fseek(f, -sizeof(header), SEEK_CUR);
5439 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
5440 if(header.magic != MH_MAGIC_64) {
5441 barf("Bad magic. Expected: %08x, got: %08x.",
5442 MH_MAGIC_64, header.magic);
5445 if(header.magic != MH_MAGIC) {
5446 barf("Bad magic. Expected: %08x, got: %08x.",
5447 MH_MAGIC, header.magic);
5451 misalignment = (header.sizeofcmds + sizeof(header))
5454 return misalignment ? (16 - misalignment) : 0;