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>.
23 #include "sm/Storage.h"
26 #include "LinkerInternals.h"
29 #include "StgPrimFloat.h" // for __int_encodeFloat etc.
32 #if !defined(mingw32_HOST_OS)
33 #include "posix/Signals.h"
36 // get protos for is*()
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
52 #if defined(HAVE_DLFCN_H)
56 #if defined(cygwin32_HOST_OS)
61 #ifdef HAVE_SYS_TIME_H
65 #include <sys/fcntl.h>
66 #include <sys/termios.h>
67 #include <sys/utime.h>
68 #include <sys/utsname.h>
72 #if defined(linux_HOST_OS ) || defined(freebsd_HOST_OS) || \
73 defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS ) || \
74 defined(openbsd_HOST_OS ) || \
75 ( defined(darwin_HOST_OS ) && !defined(powerpc_HOST_ARCH) )
76 /* Don't use mmap on powerpc-apple-darwin as mmap doesn't support
77 * reallocating but we need to allocate jump islands just after each
78 * object images. Otherwise relative branches to jump islands can fail
79 * due to 24-bits displacement overflow.
91 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
92 # define OBJFORMAT_ELF
93 # include <regex.h> // regex is already used by dlopen() so this is OK
94 // to use here without requiring an additional lib
95 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
96 # define OBJFORMAT_PEi386
99 #elif defined(darwin_HOST_OS)
100 # define OBJFORMAT_MACHO
102 # include <mach-o/loader.h>
103 # include <mach-o/nlist.h>
104 # include <mach-o/reloc.h>
105 #if !defined(HAVE_DLFCN_H)
106 # include <mach-o/dyld.h>
108 #if defined(powerpc_HOST_ARCH)
109 # include <mach-o/ppc/reloc.h>
111 #if defined(x86_64_HOST_ARCH)
112 # include <mach-o/x86_64/reloc.h>
116 #if defined(x86_64_HOST_ARCH) && defined(darwin_HOST_OS)
120 /* Hash table mapping symbol names to Symbol */
121 static /*Str*/HashTable *symhash;
123 /* Hash table mapping symbol names to StgStablePtr */
124 static /*Str*/HashTable *stablehash;
126 /* List of currently loaded objects */
127 ObjectCode *objects = NULL; /* initially empty */
129 static HsInt loadOc( ObjectCode* oc );
130 static ObjectCode* mkOc( char *path, char *image, int imageSize,
131 char *archiveMemberName
133 #ifdef darwin_HOST_OS
139 #if defined(OBJFORMAT_ELF)
140 static int ocVerifyImage_ELF ( ObjectCode* oc );
141 static int ocGetNames_ELF ( ObjectCode* oc );
142 static int ocResolve_ELF ( ObjectCode* oc );
143 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
144 static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
146 #elif defined(OBJFORMAT_PEi386)
147 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
148 static int ocGetNames_PEi386 ( ObjectCode* oc );
149 static int ocResolve_PEi386 ( ObjectCode* oc );
150 static void *lookupSymbolInDLLs ( unsigned char *lbl );
151 static void zapTrailingAtSign ( unsigned char *sym );
152 #elif defined(OBJFORMAT_MACHO)
153 static int ocVerifyImage_MachO ( ObjectCode* oc );
154 static int ocGetNames_MachO ( ObjectCode* oc );
155 static int ocResolve_MachO ( ObjectCode* oc );
158 static int machoGetMisalignment( FILE * );
160 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
161 static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
163 #ifdef powerpc_HOST_ARCH
164 static void machoInitSymbolsWithoutUnderscore( void );
168 /* on x86_64 we have a problem with relocating symbol references in
169 * code that was compiled without -fPIC. By default, the small memory
170 * model is used, which assumes that symbol references can fit in a
171 * 32-bit slot. The system dynamic linker makes this work for
172 * references to shared libraries by either (a) allocating a jump
173 * table slot for code references, or (b) moving the symbol at load
174 * time (and copying its contents, if necessary) for data references.
176 * We unfortunately can't tell whether symbol references are to code
177 * or data. So for now we assume they are code (the vast majority
178 * are), and allocate jump-table slots. Unfortunately this will
179 * SILENTLY generate crashing code for data references. This hack is
180 * enabled by X86_64_ELF_NONPIC_HACK.
182 * One workaround is to use shared Haskell libraries. This is
183 * coming. Another workaround is to keep the static libraries but
184 * compile them with -fPIC, because that will generate PIC references
185 * to data which can be relocated. The PIC code is still too green to
186 * do this systematically, though.
189 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
191 * Naming Scheme for Symbol Macros
193 * SymI_*: symbol is internal to the RTS. It resides in an object
194 * file/library that is statically.
195 * SymE_*: symbol is external to the RTS library. It might be linked
198 * Sym*_HasProto : the symbol prototype is imported in an include file
199 * or defined explicitly
200 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
201 * default proto extern void sym(void);
203 #define X86_64_ELF_NONPIC_HACK 1
205 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
206 * small memory model on this architecture (see gcc docs,
209 * MAP_32BIT not available on OpenBSD/amd64
211 #if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT)
212 #define TRY_MAP_32BIT MAP_32BIT
214 #define TRY_MAP_32BIT 0
218 * Due to the small memory model (see above), on x86_64 we have to map
219 * all our non-PIC object files into the low 2Gb of the address space
220 * (why 2Gb and not 4Gb? Because all addresses must be reachable
221 * using a 32-bit signed PC-relative offset). On Linux we can do this
222 * using the MAP_32BIT flag to mmap(), however on other OSs
223 * (e.g. *BSD, see #2063, and also on Linux inside Xen, see #2512), we
224 * can't do this. So on these systems, we have to pick a base address
225 * in the low 2Gb of the address space and try to allocate memory from
228 * We pick a default address based on the OS, but also make this
229 * configurable via an RTS flag (+RTS -xm)
231 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
233 #if defined(MAP_32BIT)
234 // Try to use MAP_32BIT
235 #define MMAP_32BIT_BASE_DEFAULT 0
238 #define MMAP_32BIT_BASE_DEFAULT 0x40000000
241 static void *mmap_32bit_base = (void *)MMAP_32BIT_BASE_DEFAULT;
244 /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */
245 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
246 #define MAP_ANONYMOUS MAP_ANON
249 /* -----------------------------------------------------------------------------
250 * Built-in symbols from the RTS
253 typedef struct _RtsSymbolVal {
258 #define Maybe_Stable_Names SymI_HasProto(stg_mkWeakzh) \
259 SymI_HasProto(stg_mkWeakForeignEnvzh) \
260 SymI_HasProto(stg_makeStableNamezh) \
261 SymI_HasProto(stg_finalizzeWeakzh)
263 #if !defined (mingw32_HOST_OS)
264 #define RTS_POSIX_ONLY_SYMBOLS \
265 SymI_HasProto(__hscore_get_saved_termios) \
266 SymI_HasProto(__hscore_set_saved_termios) \
267 SymI_HasProto(shutdownHaskellAndSignal) \
268 SymI_HasProto(lockFile) \
269 SymI_HasProto(unlockFile) \
270 SymI_HasProto(signal_handlers) \
271 SymI_HasProto(stg_sig_install) \
272 SymI_HasProto(rtsTimerSignal) \
273 SymI_NeedsProto(nocldstop)
276 #if defined (cygwin32_HOST_OS)
277 #define RTS_MINGW_ONLY_SYMBOLS /**/
278 /* Don't have the ability to read import libs / archives, so
279 * we have to stupidly list a lot of what libcygwin.a
282 #define RTS_CYGWIN_ONLY_SYMBOLS \
283 SymI_HasProto(regfree) \
284 SymI_HasProto(regexec) \
285 SymI_HasProto(regerror) \
286 SymI_HasProto(regcomp) \
287 SymI_HasProto(__errno) \
288 SymI_HasProto(access) \
289 SymI_HasProto(chmod) \
290 SymI_HasProto(chdir) \
291 SymI_HasProto(close) \
292 SymI_HasProto(creat) \
294 SymI_HasProto(dup2) \
295 SymI_HasProto(fstat) \
296 SymI_HasProto(fcntl) \
297 SymI_HasProto(getcwd) \
298 SymI_HasProto(getenv) \
299 SymI_HasProto(lseek) \
300 SymI_HasProto(open) \
301 SymI_HasProto(fpathconf) \
302 SymI_HasProto(pathconf) \
303 SymI_HasProto(stat) \
305 SymI_HasProto(tanh) \
306 SymI_HasProto(cosh) \
307 SymI_HasProto(sinh) \
308 SymI_HasProto(atan) \
309 SymI_HasProto(acos) \
310 SymI_HasProto(asin) \
316 SymI_HasProto(sqrt) \
317 SymI_HasProto(localtime_r) \
318 SymI_HasProto(gmtime_r) \
319 SymI_HasProto(mktime) \
320 SymI_NeedsProto(_imp___tzname) \
321 SymI_HasProto(gettimeofday) \
322 SymI_HasProto(timezone) \
323 SymI_HasProto(tcgetattr) \
324 SymI_HasProto(tcsetattr) \
325 SymI_HasProto(memcpy) \
326 SymI_HasProto(memmove) \
327 SymI_HasProto(realloc) \
328 SymI_HasProto(malloc) \
329 SymI_HasProto(free) \
330 SymI_HasProto(fork) \
331 SymI_HasProto(lstat) \
332 SymI_HasProto(isatty) \
333 SymI_HasProto(mkdir) \
334 SymI_HasProto(opendir) \
335 SymI_HasProto(readdir) \
336 SymI_HasProto(rewinddir) \
337 SymI_HasProto(closedir) \
338 SymI_HasProto(link) \
339 SymI_HasProto(mkfifo) \
340 SymI_HasProto(pipe) \
341 SymI_HasProto(read) \
342 SymI_HasProto(rename) \
343 SymI_HasProto(rmdir) \
344 SymI_HasProto(select) \
345 SymI_HasProto(system) \
346 SymI_HasProto(write) \
347 SymI_HasProto(strcmp) \
348 SymI_HasProto(strcpy) \
349 SymI_HasProto(strncpy) \
350 SymI_HasProto(strerror) \
351 SymI_HasProto(sigaddset) \
352 SymI_HasProto(sigemptyset) \
353 SymI_HasProto(sigprocmask) \
354 SymI_HasProto(umask) \
355 SymI_HasProto(uname) \
356 SymI_HasProto(unlink) \
357 SymI_HasProto(utime) \
358 SymI_HasProto(waitpid)
360 #elif !defined(mingw32_HOST_OS)
361 #define RTS_MINGW_ONLY_SYMBOLS /**/
362 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
363 #else /* defined(mingw32_HOST_OS) */
364 #define RTS_POSIX_ONLY_SYMBOLS /**/
365 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
367 #if HAVE_GETTIMEOFDAY
368 #define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
370 #define RTS_MINGW_GETTIMEOFDAY_SYM /**/
373 #if HAVE___MINGW_VFPRINTF
374 #define RTS___MINGW_VFPRINTF_SYM SymI_HasProto(__mingw_vfprintf)
376 #define RTS___MINGW_VFPRINTF_SYM /**/
379 /* These are statically linked from the mingw libraries into the ghc
380 executable, so we have to employ this hack. */
381 #define RTS_MINGW_ONLY_SYMBOLS \
382 SymI_HasProto(stg_asyncReadzh) \
383 SymI_HasProto(stg_asyncWritezh) \
384 SymI_HasProto(stg_asyncDoProczh) \
385 SymI_HasProto(memset) \
386 SymI_HasProto(inet_ntoa) \
387 SymI_HasProto(inet_addr) \
388 SymI_HasProto(htonl) \
389 SymI_HasProto(recvfrom) \
390 SymI_HasProto(listen) \
391 SymI_HasProto(bind) \
392 SymI_HasProto(shutdown) \
393 SymI_HasProto(connect) \
394 SymI_HasProto(htons) \
395 SymI_HasProto(ntohs) \
396 SymI_HasProto(getservbyname) \
397 SymI_HasProto(getservbyport) \
398 SymI_HasProto(getprotobynumber) \
399 SymI_HasProto(getprotobyname) \
400 SymI_HasProto(gethostbyname) \
401 SymI_HasProto(gethostbyaddr) \
402 SymI_HasProto(gethostname) \
403 SymI_HasProto(strcpy) \
404 SymI_HasProto(strncpy) \
405 SymI_HasProto(abort) \
406 SymI_NeedsProto(_alloca) \
407 SymI_HasProto(isxdigit) \
408 SymI_HasProto(isupper) \
409 SymI_HasProto(ispunct) \
410 SymI_HasProto(islower) \
411 SymI_HasProto(isspace) \
412 SymI_HasProto(isprint) \
413 SymI_HasProto(isdigit) \
414 SymI_HasProto(iscntrl) \
415 SymI_HasProto(isalpha) \
416 SymI_HasProto(isalnum) \
417 SymI_HasProto(isascii) \
418 RTS___MINGW_VFPRINTF_SYM \
419 SymI_HasProto(strcmp) \
420 SymI_HasProto(memmove) \
421 SymI_HasProto(realloc) \
422 SymI_HasProto(malloc) \
424 SymI_HasProto(tanh) \
425 SymI_HasProto(cosh) \
426 SymI_HasProto(sinh) \
427 SymI_HasProto(atan) \
428 SymI_HasProto(acos) \
429 SymI_HasProto(asin) \
435 SymI_HasProto(sqrt) \
436 SymI_HasProto(powf) \
437 SymI_HasProto(tanhf) \
438 SymI_HasProto(coshf) \
439 SymI_HasProto(sinhf) \
440 SymI_HasProto(atanf) \
441 SymI_HasProto(acosf) \
442 SymI_HasProto(asinf) \
443 SymI_HasProto(tanf) \
444 SymI_HasProto(cosf) \
445 SymI_HasProto(sinf) \
446 SymI_HasProto(expf) \
447 SymI_HasProto(logf) \
448 SymI_HasProto(sqrtf) \
450 SymI_HasProto(erfc) \
451 SymI_HasProto(erff) \
452 SymI_HasProto(erfcf) \
453 SymI_HasProto(memcpy) \
454 SymI_HasProto(rts_InstallConsoleEvent) \
455 SymI_HasProto(rts_ConsoleHandlerDone) \
456 SymI_NeedsProto(mktime) \
457 SymI_NeedsProto(_imp___timezone) \
458 SymI_NeedsProto(_imp___tzname) \
459 SymI_NeedsProto(_imp__tzname) \
460 SymI_NeedsProto(_imp___iob) \
461 SymI_NeedsProto(_imp___osver) \
462 SymI_NeedsProto(localtime) \
463 SymI_NeedsProto(gmtime) \
464 SymI_NeedsProto(opendir) \
465 SymI_NeedsProto(readdir) \
466 SymI_NeedsProto(rewinddir) \
467 SymI_NeedsProto(_imp____mb_cur_max) \
468 SymI_NeedsProto(_imp___pctype) \
469 SymI_NeedsProto(__chkstk) \
470 RTS_MINGW_GETTIMEOFDAY_SYM \
471 SymI_NeedsProto(closedir)
475 #if defined(darwin_HOST_OS) && HAVE_PRINTF_LDBLSTUB
476 #define RTS_DARWIN_ONLY_SYMBOLS \
477 SymI_NeedsProto(asprintf$LDBLStub) \
478 SymI_NeedsProto(err$LDBLStub) \
479 SymI_NeedsProto(errc$LDBLStub) \
480 SymI_NeedsProto(errx$LDBLStub) \
481 SymI_NeedsProto(fprintf$LDBLStub) \
482 SymI_NeedsProto(fscanf$LDBLStub) \
483 SymI_NeedsProto(fwprintf$LDBLStub) \
484 SymI_NeedsProto(fwscanf$LDBLStub) \
485 SymI_NeedsProto(printf$LDBLStub) \
486 SymI_NeedsProto(scanf$LDBLStub) \
487 SymI_NeedsProto(snprintf$LDBLStub) \
488 SymI_NeedsProto(sprintf$LDBLStub) \
489 SymI_NeedsProto(sscanf$LDBLStub) \
490 SymI_NeedsProto(strtold$LDBLStub) \
491 SymI_NeedsProto(swprintf$LDBLStub) \
492 SymI_NeedsProto(swscanf$LDBLStub) \
493 SymI_NeedsProto(syslog$LDBLStub) \
494 SymI_NeedsProto(vasprintf$LDBLStub) \
495 SymI_NeedsProto(verr$LDBLStub) \
496 SymI_NeedsProto(verrc$LDBLStub) \
497 SymI_NeedsProto(verrx$LDBLStub) \
498 SymI_NeedsProto(vfprintf$LDBLStub) \
499 SymI_NeedsProto(vfscanf$LDBLStub) \
500 SymI_NeedsProto(vfwprintf$LDBLStub) \
501 SymI_NeedsProto(vfwscanf$LDBLStub) \
502 SymI_NeedsProto(vprintf$LDBLStub) \
503 SymI_NeedsProto(vscanf$LDBLStub) \
504 SymI_NeedsProto(vsnprintf$LDBLStub) \
505 SymI_NeedsProto(vsprintf$LDBLStub) \
506 SymI_NeedsProto(vsscanf$LDBLStub) \
507 SymI_NeedsProto(vswprintf$LDBLStub) \
508 SymI_NeedsProto(vswscanf$LDBLStub) \
509 SymI_NeedsProto(vsyslog$LDBLStub) \
510 SymI_NeedsProto(vwarn$LDBLStub) \
511 SymI_NeedsProto(vwarnc$LDBLStub) \
512 SymI_NeedsProto(vwarnx$LDBLStub) \
513 SymI_NeedsProto(vwprintf$LDBLStub) \
514 SymI_NeedsProto(vwscanf$LDBLStub) \
515 SymI_NeedsProto(warn$LDBLStub) \
516 SymI_NeedsProto(warnc$LDBLStub) \
517 SymI_NeedsProto(warnx$LDBLStub) \
518 SymI_NeedsProto(wcstold$LDBLStub) \
519 SymI_NeedsProto(wprintf$LDBLStub) \
520 SymI_NeedsProto(wscanf$LDBLStub)
522 #define RTS_DARWIN_ONLY_SYMBOLS
526 # define MAIN_CAP_SYM SymI_HasProto(MainCapability)
528 # define MAIN_CAP_SYM
531 #if !defined(mingw32_HOST_OS)
532 #define RTS_USER_SIGNALS_SYMBOLS \
533 SymI_HasProto(setIOManagerControlFd) \
534 SymI_HasProto(setIOManagerWakeupFd) \
535 SymI_HasProto(ioManagerWakeup) \
536 SymI_HasProto(blockUserSignals) \
537 SymI_HasProto(unblockUserSignals)
539 #define RTS_USER_SIGNALS_SYMBOLS \
540 SymI_HasProto(ioManagerWakeup) \
541 SymI_HasProto(sendIOManagerEvent) \
542 SymI_HasProto(readIOManagerEvent) \
543 SymI_HasProto(getIOManagerEvent) \
544 SymI_HasProto(console_handler)
547 #define RTS_LIBFFI_SYMBOLS \
548 SymE_NeedsProto(ffi_prep_cif) \
549 SymE_NeedsProto(ffi_call) \
550 SymE_NeedsProto(ffi_type_void) \
551 SymE_NeedsProto(ffi_type_float) \
552 SymE_NeedsProto(ffi_type_double) \
553 SymE_NeedsProto(ffi_type_sint64) \
554 SymE_NeedsProto(ffi_type_uint64) \
555 SymE_NeedsProto(ffi_type_sint32) \
556 SymE_NeedsProto(ffi_type_uint32) \
557 SymE_NeedsProto(ffi_type_sint16) \
558 SymE_NeedsProto(ffi_type_uint16) \
559 SymE_NeedsProto(ffi_type_sint8) \
560 SymE_NeedsProto(ffi_type_uint8) \
561 SymE_NeedsProto(ffi_type_pointer)
563 #ifdef TABLES_NEXT_TO_CODE
564 #define RTS_RET_SYMBOLS /* nothing */
566 #define RTS_RET_SYMBOLS \
567 SymI_HasProto(stg_enter_ret) \
568 SymI_HasProto(stg_gc_fun_ret) \
569 SymI_HasProto(stg_ap_v_ret) \
570 SymI_HasProto(stg_ap_f_ret) \
571 SymI_HasProto(stg_ap_d_ret) \
572 SymI_HasProto(stg_ap_l_ret) \
573 SymI_HasProto(stg_ap_n_ret) \
574 SymI_HasProto(stg_ap_p_ret) \
575 SymI_HasProto(stg_ap_pv_ret) \
576 SymI_HasProto(stg_ap_pp_ret) \
577 SymI_HasProto(stg_ap_ppv_ret) \
578 SymI_HasProto(stg_ap_ppp_ret) \
579 SymI_HasProto(stg_ap_pppv_ret) \
580 SymI_HasProto(stg_ap_pppp_ret) \
581 SymI_HasProto(stg_ap_ppppp_ret) \
582 SymI_HasProto(stg_ap_pppppp_ret)
585 /* Modules compiled with -ticky may mention ticky counters */
586 /* This list should marry up with the one in $(TOP)/includes/stg/Ticky.h */
587 #define RTS_TICKY_SYMBOLS \
588 SymI_NeedsProto(ticky_entry_ctrs) \
589 SymI_NeedsProto(top_ct) \
591 SymI_HasProto(ENT_VIA_NODE_ctr) \
592 SymI_HasProto(ENT_STATIC_THK_ctr) \
593 SymI_HasProto(ENT_DYN_THK_ctr) \
594 SymI_HasProto(ENT_STATIC_FUN_DIRECT_ctr) \
595 SymI_HasProto(ENT_DYN_FUN_DIRECT_ctr) \
596 SymI_HasProto(ENT_STATIC_CON_ctr) \
597 SymI_HasProto(ENT_DYN_CON_ctr) \
598 SymI_HasProto(ENT_STATIC_IND_ctr) \
599 SymI_HasProto(ENT_DYN_IND_ctr) \
600 SymI_HasProto(ENT_PERM_IND_ctr) \
601 SymI_HasProto(ENT_PAP_ctr) \
602 SymI_HasProto(ENT_AP_ctr) \
603 SymI_HasProto(ENT_AP_STACK_ctr) \
604 SymI_HasProto(ENT_BH_ctr) \
605 SymI_HasProto(UNKNOWN_CALL_ctr) \
606 SymI_HasProto(SLOW_CALL_v_ctr) \
607 SymI_HasProto(SLOW_CALL_f_ctr) \
608 SymI_HasProto(SLOW_CALL_d_ctr) \
609 SymI_HasProto(SLOW_CALL_l_ctr) \
610 SymI_HasProto(SLOW_CALL_n_ctr) \
611 SymI_HasProto(SLOW_CALL_p_ctr) \
612 SymI_HasProto(SLOW_CALL_pv_ctr) \
613 SymI_HasProto(SLOW_CALL_pp_ctr) \
614 SymI_HasProto(SLOW_CALL_ppv_ctr) \
615 SymI_HasProto(SLOW_CALL_ppp_ctr) \
616 SymI_HasProto(SLOW_CALL_pppv_ctr) \
617 SymI_HasProto(SLOW_CALL_pppp_ctr) \
618 SymI_HasProto(SLOW_CALL_ppppp_ctr) \
619 SymI_HasProto(SLOW_CALL_pppppp_ctr) \
620 SymI_HasProto(SLOW_CALL_OTHER_ctr) \
621 SymI_HasProto(ticky_slow_call_unevald) \
622 SymI_HasProto(SLOW_CALL_ctr) \
623 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_ctr) \
624 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_CHUNKS_ctr) \
625 SymI_HasProto(KNOWN_CALL_ctr) \
626 SymI_HasProto(KNOWN_CALL_TOO_FEW_ARGS_ctr) \
627 SymI_HasProto(KNOWN_CALL_EXTRA_ARGS_ctr) \
628 SymI_HasProto(SLOW_CALL_FUN_TOO_FEW_ctr) \
629 SymI_HasProto(SLOW_CALL_FUN_CORRECT_ctr) \
630 SymI_HasProto(SLOW_CALL_FUN_TOO_MANY_ctr) \
631 SymI_HasProto(SLOW_CALL_PAP_TOO_FEW_ctr) \
632 SymI_HasProto(SLOW_CALL_PAP_CORRECT_ctr) \
633 SymI_HasProto(SLOW_CALL_PAP_TOO_MANY_ctr) \
634 SymI_HasProto(SLOW_CALL_UNEVALD_ctr) \
635 SymI_HasProto(UPDF_OMITTED_ctr) \
636 SymI_HasProto(UPDF_PUSHED_ctr) \
637 SymI_HasProto(CATCHF_PUSHED_ctr) \
638 SymI_HasProto(UPDF_RCC_PUSHED_ctr) \
639 SymI_HasProto(UPDF_RCC_OMITTED_ctr) \
640 SymI_HasProto(UPD_SQUEEZED_ctr) \
641 SymI_HasProto(UPD_CON_IN_NEW_ctr) \
642 SymI_HasProto(UPD_CON_IN_PLACE_ctr) \
643 SymI_HasProto(UPD_PAP_IN_NEW_ctr) \
644 SymI_HasProto(UPD_PAP_IN_PLACE_ctr) \
645 SymI_HasProto(ALLOC_HEAP_ctr) \
646 SymI_HasProto(ALLOC_HEAP_tot) \
647 SymI_HasProto(ALLOC_FUN_ctr) \
648 SymI_HasProto(ALLOC_FUN_adm) \
649 SymI_HasProto(ALLOC_FUN_gds) \
650 SymI_HasProto(ALLOC_FUN_slp) \
651 SymI_HasProto(UPD_NEW_IND_ctr) \
652 SymI_HasProto(UPD_NEW_PERM_IND_ctr) \
653 SymI_HasProto(UPD_OLD_IND_ctr) \
654 SymI_HasProto(UPD_OLD_PERM_IND_ctr) \
655 SymI_HasProto(UPD_BH_UPDATABLE_ctr) \
656 SymI_HasProto(UPD_BH_SINGLE_ENTRY_ctr) \
657 SymI_HasProto(UPD_CAF_BH_UPDATABLE_ctr) \
658 SymI_HasProto(UPD_CAF_BH_SINGLE_ENTRY_ctr) \
659 SymI_HasProto(GC_SEL_ABANDONED_ctr) \
660 SymI_HasProto(GC_SEL_MINOR_ctr) \
661 SymI_HasProto(GC_SEL_MAJOR_ctr) \
662 SymI_HasProto(GC_FAILED_PROMOTION_ctr) \
663 SymI_HasProto(ALLOC_UP_THK_ctr) \
664 SymI_HasProto(ALLOC_SE_THK_ctr) \
665 SymI_HasProto(ALLOC_THK_adm) \
666 SymI_HasProto(ALLOC_THK_gds) \
667 SymI_HasProto(ALLOC_THK_slp) \
668 SymI_HasProto(ALLOC_CON_ctr) \
669 SymI_HasProto(ALLOC_CON_adm) \
670 SymI_HasProto(ALLOC_CON_gds) \
671 SymI_HasProto(ALLOC_CON_slp) \
672 SymI_HasProto(ALLOC_TUP_ctr) \
673 SymI_HasProto(ALLOC_TUP_adm) \
674 SymI_HasProto(ALLOC_TUP_gds) \
675 SymI_HasProto(ALLOC_TUP_slp) \
676 SymI_HasProto(ALLOC_BH_ctr) \
677 SymI_HasProto(ALLOC_BH_adm) \
678 SymI_HasProto(ALLOC_BH_gds) \
679 SymI_HasProto(ALLOC_BH_slp) \
680 SymI_HasProto(ALLOC_PRIM_ctr) \
681 SymI_HasProto(ALLOC_PRIM_adm) \
682 SymI_HasProto(ALLOC_PRIM_gds) \
683 SymI_HasProto(ALLOC_PRIM_slp) \
684 SymI_HasProto(ALLOC_PAP_ctr) \
685 SymI_HasProto(ALLOC_PAP_adm) \
686 SymI_HasProto(ALLOC_PAP_gds) \
687 SymI_HasProto(ALLOC_PAP_slp) \
688 SymI_HasProto(ALLOC_TSO_ctr) \
689 SymI_HasProto(ALLOC_TSO_adm) \
690 SymI_HasProto(ALLOC_TSO_gds) \
691 SymI_HasProto(ALLOC_TSO_slp) \
692 SymI_HasProto(RET_NEW_ctr) \
693 SymI_HasProto(RET_OLD_ctr) \
694 SymI_HasProto(RET_UNBOXED_TUP_ctr) \
695 SymI_HasProto(RET_SEMI_loads_avoided)
698 // On most platforms, the garbage collector rewrites references
699 // to small integer and char objects to a set of common, shared ones.
701 // We don't do this when compiling to Windows DLLs at the moment because
702 // it doesn't support cross package data references well.
704 #if defined(__PIC__) && defined(mingw32_HOST_OS)
705 #define RTS_INTCHAR_SYMBOLS
707 #define RTS_INTCHAR_SYMBOLS \
708 SymI_HasProto(stg_CHARLIKE_closure) \
709 SymI_HasProto(stg_INTLIKE_closure)
713 #define RTS_SYMBOLS \
716 SymI_HasProto(StgReturn) \
717 SymI_HasProto(stg_enter_info) \
718 SymI_HasProto(stg_gc_void_info) \
719 SymI_HasProto(__stg_gc_enter_1) \
720 SymI_HasProto(stg_gc_noregs) \
721 SymI_HasProto(stg_gc_unpt_r1_info) \
722 SymI_HasProto(stg_gc_unpt_r1) \
723 SymI_HasProto(stg_gc_unbx_r1_info) \
724 SymI_HasProto(stg_gc_unbx_r1) \
725 SymI_HasProto(stg_gc_f1_info) \
726 SymI_HasProto(stg_gc_f1) \
727 SymI_HasProto(stg_gc_d1_info) \
728 SymI_HasProto(stg_gc_d1) \
729 SymI_HasProto(stg_gc_l1_info) \
730 SymI_HasProto(stg_gc_l1) \
731 SymI_HasProto(__stg_gc_fun) \
732 SymI_HasProto(stg_gc_fun_info) \
733 SymI_HasProto(stg_gc_gen) \
734 SymI_HasProto(stg_gc_gen_info) \
735 SymI_HasProto(stg_gc_gen_hp) \
736 SymI_HasProto(stg_gc_ut) \
737 SymI_HasProto(stg_gen_yield) \
738 SymI_HasProto(stg_yield_noregs) \
739 SymI_HasProto(stg_yield_to_interpreter) \
740 SymI_HasProto(stg_gen_block) \
741 SymI_HasProto(stg_block_noregs) \
742 SymI_HasProto(stg_block_1) \
743 SymI_HasProto(stg_block_takemvar) \
744 SymI_HasProto(stg_block_putmvar) \
746 SymI_HasProto(MallocFailHook) \
747 SymI_HasProto(OnExitHook) \
748 SymI_HasProto(OutOfHeapHook) \
749 SymI_HasProto(StackOverflowHook) \
750 SymI_HasProto(addDLL) \
751 SymI_HasProto(__int_encodeDouble) \
752 SymI_HasProto(__word_encodeDouble) \
753 SymI_HasProto(__2Int_encodeDouble) \
754 SymI_HasProto(__int_encodeFloat) \
755 SymI_HasProto(__word_encodeFloat) \
756 SymI_HasProto(stg_atomicallyzh) \
757 SymI_HasProto(barf) \
758 SymI_HasProto(debugBelch) \
759 SymI_HasProto(errorBelch) \
760 SymI_HasProto(sysErrorBelch) \
761 SymI_HasProto(stg_getMaskingStatezh) \
762 SymI_HasProto(stg_maskAsyncExceptionszh) \
763 SymI_HasProto(stg_maskUninterruptiblezh) \
764 SymI_HasProto(stg_catchzh) \
765 SymI_HasProto(stg_catchRetryzh) \
766 SymI_HasProto(stg_catchSTMzh) \
767 SymI_HasProto(stg_checkzh) \
768 SymI_HasProto(closure_flags) \
769 SymI_HasProto(cmp_thread) \
770 SymI_HasProto(createAdjustor) \
771 SymI_HasProto(stg_decodeDoublezu2Intzh) \
772 SymI_HasProto(stg_decodeFloatzuIntzh) \
773 SymI_HasProto(defaultsHook) \
774 SymI_HasProto(stg_delayzh) \
775 SymI_HasProto(stg_deRefWeakzh) \
776 SymI_HasProto(stg_deRefStablePtrzh) \
777 SymI_HasProto(dirty_MUT_VAR) \
778 SymI_HasProto(stg_forkzh) \
779 SymI_HasProto(stg_forkOnzh) \
780 SymI_HasProto(forkProcess) \
781 SymI_HasProto(forkOS_createThread) \
782 SymI_HasProto(freeHaskellFunctionPtr) \
783 SymI_HasProto(getOrSetTypeableStore) \
784 SymI_HasProto(getOrSetGHCConcSignalSignalHandlerStore) \
785 SymI_HasProto(getOrSetGHCConcWindowsPendingDelaysStore) \
786 SymI_HasProto(getOrSetGHCConcWindowsIOManagerThreadStore) \
787 SymI_HasProto(getOrSetGHCConcWindowsProddingStore) \
788 SymI_HasProto(getOrSetSystemEventThreadEventManagerStore) \
789 SymI_HasProto(getOrSetSystemEventThreadIOManagerThreadStore) \
790 SymI_HasProto(genSymZh) \
791 SymI_HasProto(genericRaise) \
792 SymI_HasProto(getProgArgv) \
793 SymI_HasProto(getFullProgArgv) \
794 SymI_HasProto(getStablePtr) \
795 SymI_HasProto(hs_init) \
796 SymI_HasProto(hs_exit) \
797 SymI_HasProto(hs_set_argv) \
798 SymI_HasProto(hs_add_root) \
799 SymI_HasProto(hs_perform_gc) \
800 SymI_HasProto(hs_free_stable_ptr) \
801 SymI_HasProto(hs_free_fun_ptr) \
802 SymI_HasProto(hs_hpc_rootModule) \
803 SymI_HasProto(hs_hpc_module) \
804 SymI_HasProto(initLinker) \
805 SymI_HasProto(stg_unpackClosurezh) \
806 SymI_HasProto(stg_getApStackValzh) \
807 SymI_HasProto(stg_getSparkzh) \
808 SymI_HasProto(stg_numSparkszh) \
809 SymI_HasProto(stg_isCurrentThreadBoundzh) \
810 SymI_HasProto(stg_isEmptyMVarzh) \
811 SymI_HasProto(stg_killThreadzh) \
812 SymI_HasProto(loadArchive) \
813 SymI_HasProto(loadObj) \
814 SymI_HasProto(insertStableSymbol) \
815 SymI_HasProto(insertSymbol) \
816 SymI_HasProto(lookupSymbol) \
817 SymI_HasProto(stg_makeStablePtrzh) \
818 SymI_HasProto(stg_mkApUpd0zh) \
819 SymI_HasProto(stg_myThreadIdzh) \
820 SymI_HasProto(stg_labelThreadzh) \
821 SymI_HasProto(stg_newArrayzh) \
822 SymI_HasProto(stg_newBCOzh) \
823 SymI_HasProto(stg_newByteArrayzh) \
824 SymI_HasProto_redirect(newCAF, newDynCAF) \
825 SymI_HasProto(stg_newMVarzh) \
826 SymI_HasProto(stg_newMutVarzh) \
827 SymI_HasProto(stg_newTVarzh) \
828 SymI_HasProto(stg_noDuplicatezh) \
829 SymI_HasProto(stg_atomicModifyMutVarzh) \
830 SymI_HasProto(stg_newPinnedByteArrayzh) \
831 SymI_HasProto(stg_newAlignedPinnedByteArrayzh) \
832 SymI_HasProto(newSpark) \
833 SymI_HasProto(performGC) \
834 SymI_HasProto(performMajorGC) \
835 SymI_HasProto(prog_argc) \
836 SymI_HasProto(prog_argv) \
837 SymI_HasProto(stg_putMVarzh) \
838 SymI_HasProto(stg_raisezh) \
839 SymI_HasProto(stg_raiseIOzh) \
840 SymI_HasProto(stg_readTVarzh) \
841 SymI_HasProto(stg_readTVarIOzh) \
842 SymI_HasProto(resumeThread) \
843 SymI_HasProto(resolveObjs) \
844 SymI_HasProto(stg_retryzh) \
845 SymI_HasProto(rts_apply) \
846 SymI_HasProto(rts_checkSchedStatus) \
847 SymI_HasProto(rts_eval) \
848 SymI_HasProto(rts_evalIO) \
849 SymI_HasProto(rts_evalLazyIO) \
850 SymI_HasProto(rts_evalStableIO) \
851 SymI_HasProto(rts_eval_) \
852 SymI_HasProto(rts_getBool) \
853 SymI_HasProto(rts_getChar) \
854 SymI_HasProto(rts_getDouble) \
855 SymI_HasProto(rts_getFloat) \
856 SymI_HasProto(rts_getInt) \
857 SymI_HasProto(rts_getInt8) \
858 SymI_HasProto(rts_getInt16) \
859 SymI_HasProto(rts_getInt32) \
860 SymI_HasProto(rts_getInt64) \
861 SymI_HasProto(rts_getPtr) \
862 SymI_HasProto(rts_getFunPtr) \
863 SymI_HasProto(rts_getStablePtr) \
864 SymI_HasProto(rts_getThreadId) \
865 SymI_HasProto(rts_getWord) \
866 SymI_HasProto(rts_getWord8) \
867 SymI_HasProto(rts_getWord16) \
868 SymI_HasProto(rts_getWord32) \
869 SymI_HasProto(rts_getWord64) \
870 SymI_HasProto(rts_lock) \
871 SymI_HasProto(rts_mkBool) \
872 SymI_HasProto(rts_mkChar) \
873 SymI_HasProto(rts_mkDouble) \
874 SymI_HasProto(rts_mkFloat) \
875 SymI_HasProto(rts_mkInt) \
876 SymI_HasProto(rts_mkInt8) \
877 SymI_HasProto(rts_mkInt16) \
878 SymI_HasProto(rts_mkInt32) \
879 SymI_HasProto(rts_mkInt64) \
880 SymI_HasProto(rts_mkPtr) \
881 SymI_HasProto(rts_mkFunPtr) \
882 SymI_HasProto(rts_mkStablePtr) \
883 SymI_HasProto(rts_mkString) \
884 SymI_HasProto(rts_mkWord) \
885 SymI_HasProto(rts_mkWord8) \
886 SymI_HasProto(rts_mkWord16) \
887 SymI_HasProto(rts_mkWord32) \
888 SymI_HasProto(rts_mkWord64) \
889 SymI_HasProto(rts_unlock) \
890 SymI_HasProto(rts_unsafeGetMyCapability) \
891 SymI_HasProto(rtsSupportsBoundThreads) \
892 SymI_HasProto(rts_isProfiled) \
893 SymI_HasProto(setProgArgv) \
894 SymI_HasProto(startupHaskell) \
895 SymI_HasProto(shutdownHaskell) \
896 SymI_HasProto(shutdownHaskellAndExit) \
897 SymI_HasProto(stable_ptr_table) \
898 SymI_HasProto(stackOverflow) \
899 SymI_HasProto(stg_CAF_BLACKHOLE_info) \
900 SymI_HasProto(stg_BLACKHOLE_info) \
901 SymI_HasProto(__stg_EAGER_BLACKHOLE_info) \
902 SymI_HasProto(stg_BLOCKING_QUEUE_CLEAN_info) \
903 SymI_HasProto(stg_BLOCKING_QUEUE_DIRTY_info) \
904 SymI_HasProto(startTimer) \
905 SymI_HasProto(stg_MVAR_CLEAN_info) \
906 SymI_HasProto(stg_MVAR_DIRTY_info) \
907 SymI_HasProto(stg_IND_STATIC_info) \
908 SymI_HasProto(stg_ARR_WORDS_info) \
909 SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info) \
910 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info) \
911 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info) \
912 SymI_HasProto(stg_WEAK_info) \
913 SymI_HasProto(stg_ap_v_info) \
914 SymI_HasProto(stg_ap_f_info) \
915 SymI_HasProto(stg_ap_d_info) \
916 SymI_HasProto(stg_ap_l_info) \
917 SymI_HasProto(stg_ap_n_info) \
918 SymI_HasProto(stg_ap_p_info) \
919 SymI_HasProto(stg_ap_pv_info) \
920 SymI_HasProto(stg_ap_pp_info) \
921 SymI_HasProto(stg_ap_ppv_info) \
922 SymI_HasProto(stg_ap_ppp_info) \
923 SymI_HasProto(stg_ap_pppv_info) \
924 SymI_HasProto(stg_ap_pppp_info) \
925 SymI_HasProto(stg_ap_ppppp_info) \
926 SymI_HasProto(stg_ap_pppppp_info) \
927 SymI_HasProto(stg_ap_0_fast) \
928 SymI_HasProto(stg_ap_v_fast) \
929 SymI_HasProto(stg_ap_f_fast) \
930 SymI_HasProto(stg_ap_d_fast) \
931 SymI_HasProto(stg_ap_l_fast) \
932 SymI_HasProto(stg_ap_n_fast) \
933 SymI_HasProto(stg_ap_p_fast) \
934 SymI_HasProto(stg_ap_pv_fast) \
935 SymI_HasProto(stg_ap_pp_fast) \
936 SymI_HasProto(stg_ap_ppv_fast) \
937 SymI_HasProto(stg_ap_ppp_fast) \
938 SymI_HasProto(stg_ap_pppv_fast) \
939 SymI_HasProto(stg_ap_pppp_fast) \
940 SymI_HasProto(stg_ap_ppppp_fast) \
941 SymI_HasProto(stg_ap_pppppp_fast) \
942 SymI_HasProto(stg_ap_1_upd_info) \
943 SymI_HasProto(stg_ap_2_upd_info) \
944 SymI_HasProto(stg_ap_3_upd_info) \
945 SymI_HasProto(stg_ap_4_upd_info) \
946 SymI_HasProto(stg_ap_5_upd_info) \
947 SymI_HasProto(stg_ap_6_upd_info) \
948 SymI_HasProto(stg_ap_7_upd_info) \
949 SymI_HasProto(stg_exit) \
950 SymI_HasProto(stg_sel_0_upd_info) \
951 SymI_HasProto(stg_sel_10_upd_info) \
952 SymI_HasProto(stg_sel_11_upd_info) \
953 SymI_HasProto(stg_sel_12_upd_info) \
954 SymI_HasProto(stg_sel_13_upd_info) \
955 SymI_HasProto(stg_sel_14_upd_info) \
956 SymI_HasProto(stg_sel_15_upd_info) \
957 SymI_HasProto(stg_sel_1_upd_info) \
958 SymI_HasProto(stg_sel_2_upd_info) \
959 SymI_HasProto(stg_sel_3_upd_info) \
960 SymI_HasProto(stg_sel_4_upd_info) \
961 SymI_HasProto(stg_sel_5_upd_info) \
962 SymI_HasProto(stg_sel_6_upd_info) \
963 SymI_HasProto(stg_sel_7_upd_info) \
964 SymI_HasProto(stg_sel_8_upd_info) \
965 SymI_HasProto(stg_sel_9_upd_info) \
966 SymI_HasProto(stg_upd_frame_info) \
967 SymI_HasProto(stg_bh_upd_frame_info) \
968 SymI_HasProto(suspendThread) \
969 SymI_HasProto(stg_takeMVarzh) \
970 SymI_HasProto(stg_threadStatuszh) \
971 SymI_HasProto(stg_tryPutMVarzh) \
972 SymI_HasProto(stg_tryTakeMVarzh) \
973 SymI_HasProto(stg_unmaskAsyncExceptionszh) \
974 SymI_HasProto(unloadObj) \
975 SymI_HasProto(stg_unsafeThawArrayzh) \
976 SymI_HasProto(stg_waitReadzh) \
977 SymI_HasProto(stg_waitWritezh) \
978 SymI_HasProto(stg_writeTVarzh) \
979 SymI_HasProto(stg_yieldzh) \
980 SymI_NeedsProto(stg_interp_constr_entry) \
981 SymI_HasProto(stg_arg_bitmaps) \
982 SymI_HasProto(alloc_blocks_lim) \
984 SymI_HasProto(allocate) \
985 SymI_HasProto(allocateExec) \
986 SymI_HasProto(freeExec) \
987 SymI_HasProto(getAllocations) \
988 SymI_HasProto(revertCAFs) \
989 SymI_HasProto(RtsFlags) \
990 SymI_NeedsProto(rts_breakpoint_io_action) \
991 SymI_NeedsProto(rts_stop_next_breakpoint) \
992 SymI_NeedsProto(rts_stop_on_exception) \
993 SymI_HasProto(stopTimer) \
994 SymI_HasProto(n_capabilities) \
995 SymI_HasProto(stg_traceCcszh) \
996 SymI_HasProto(stg_traceEventzh) \
997 RTS_USER_SIGNALS_SYMBOLS \
1001 // 64-bit support functions in libgcc.a
1002 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
1003 #define RTS_LIBGCC_SYMBOLS \
1004 SymI_NeedsProto(__divdi3) \
1005 SymI_NeedsProto(__udivdi3) \
1006 SymI_NeedsProto(__moddi3) \
1007 SymI_NeedsProto(__umoddi3) \
1008 SymI_NeedsProto(__muldi3) \
1009 SymI_NeedsProto(__ashldi3) \
1010 SymI_NeedsProto(__ashrdi3) \
1011 SymI_NeedsProto(__lshrdi3)
1013 #define RTS_LIBGCC_SYMBOLS
1016 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
1017 // Symbols that don't have a leading underscore
1018 // on Mac OS X. They have to receive special treatment,
1019 // see machoInitSymbolsWithoutUnderscore()
1020 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
1021 SymI_NeedsProto(saveFP) \
1022 SymI_NeedsProto(restFP)
1025 /* entirely bogus claims about types of these symbols */
1026 #define SymI_NeedsProto(vvv) extern void vvv(void);
1027 #if defined(__PIC__) && defined(mingw32_HOST_OS)
1028 #define SymE_HasProto(vvv) SymE_HasProto(vvv);
1029 #define SymE_NeedsProto(vvv) extern void _imp__ ## vvv (void);
1031 #define SymE_NeedsProto(vvv) SymI_NeedsProto(vvv);
1032 #define SymE_HasProto(vvv) SymI_HasProto(vvv)
1034 #define SymI_HasProto(vvv) /**/
1035 #define SymI_HasProto_redirect(vvv,xxx) /**/
1038 RTS_POSIX_ONLY_SYMBOLS
1039 RTS_MINGW_ONLY_SYMBOLS
1040 RTS_CYGWIN_ONLY_SYMBOLS
1041 RTS_DARWIN_ONLY_SYMBOLS
1044 #undef SymI_NeedsProto
1045 #undef SymI_HasProto
1046 #undef SymI_HasProto_redirect
1047 #undef SymE_HasProto
1048 #undef SymE_NeedsProto
1050 #ifdef LEADING_UNDERSCORE
1051 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
1053 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
1056 #define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1058 #define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1059 (void*)DLL_IMPORT_DATA_REF(vvv) },
1061 #define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
1062 #define SymE_NeedsProto(vvv) SymE_HasProto(vvv)
1064 // SymI_HasProto_redirect allows us to redirect references to one symbol to
1065 // another symbol. See newCAF/newDynCAF for an example.
1066 #define SymI_HasProto_redirect(vvv,xxx) \
1067 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1070 static RtsSymbolVal rtsSyms[] = {
1073 RTS_POSIX_ONLY_SYMBOLS
1074 RTS_MINGW_ONLY_SYMBOLS
1075 RTS_CYGWIN_ONLY_SYMBOLS
1076 RTS_DARWIN_ONLY_SYMBOLS
1079 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
1080 // dyld stub code contains references to this,
1081 // but it should never be called because we treat
1082 // lazy pointers as nonlazy.
1083 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
1085 { 0, 0 } /* sentinel */
1090 /* -----------------------------------------------------------------------------
1091 * Insert symbols into hash tables, checking for duplicates.
1094 static void ghciInsertStrHashTable ( char* obj_name,
1100 if (lookupHashTable(table, (StgWord)key) == NULL)
1102 insertStrHashTable(table, (StgWord)key, data);
1107 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
1109 "whilst processing object file\n"
1111 "This could be caused by:\n"
1112 " * Loading two different object files which export the same symbol\n"
1113 " * Specifying the same object file twice on the GHCi command line\n"
1114 " * An incorrect `package.conf' entry, causing some object to be\n"
1116 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
1123 /* -----------------------------------------------------------------------------
1124 * initialize the object linker
1128 static int linker_init_done = 0 ;
1130 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1131 static void *dl_prog_handle;
1132 static regex_t re_invalid;
1133 static regex_t re_realso;
1135 static Mutex dl_mutex; // mutex to protect dlopen/dlerror critical section
1143 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1147 IF_DEBUG(linker, debugBelch("initLinker: start\n"));
1149 /* Make initLinker idempotent, so we can call it
1150 before evey relevant operation; that means we
1151 don't need to initialise the linker separately */
1152 if (linker_init_done == 1) {
1153 IF_DEBUG(linker, debugBelch("initLinker: idempotent return\n"));
1156 linker_init_done = 1;
1159 #if defined(THREADED_RTS) && (defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO))
1160 initMutex(&dl_mutex);
1162 stablehash = allocStrHashTable();
1163 symhash = allocStrHashTable();
1165 /* populate the symbol table with stuff from the RTS */
1166 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
1167 ghciInsertStrHashTable("(GHCi built-in symbols)",
1168 symhash, sym->lbl, sym->addr);
1169 IF_DEBUG(linker, debugBelch("initLinker: inserting rts symbol %s, %p\n", sym->lbl, sym->addr));
1171 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1172 machoInitSymbolsWithoutUnderscore();
1175 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1176 # if defined(RTLD_DEFAULT)
1177 dl_prog_handle = RTLD_DEFAULT;
1179 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
1180 # endif /* RTLD_DEFAULT */
1182 compileResult = regcomp(&re_invalid,
1183 "(([^ \t()])+\\.so([^ \t:()])*):([ \t])*invalid ELF header",
1185 ASSERT( compileResult == 0 );
1186 compileResult = regcomp(&re_realso,
1187 "GROUP *\\( *(([^ )])+)",
1189 ASSERT( compileResult == 0 );
1192 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1193 if (RtsFlags.MiscFlags.linkerMemBase != 0) {
1194 // User-override for mmap_32bit_base
1195 mmap_32bit_base = (void*)RtsFlags.MiscFlags.linkerMemBase;
1199 #if defined(mingw32_HOST_OS)
1201 * These two libraries cause problems when added to the static link,
1202 * but are necessary for resolving symbols in GHCi, hence we load
1203 * them manually here.
1209 IF_DEBUG(linker, debugBelch("initLinker: done\n"));
1214 exitLinker( void ) {
1215 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1216 if (linker_init_done == 1) {
1217 regfree(&re_invalid);
1218 regfree(&re_realso);
1220 closeMutex(&dl_mutex);
1226 /* -----------------------------------------------------------------------------
1227 * Loading DLL or .so dynamic libraries
1228 * -----------------------------------------------------------------------------
1230 * Add a DLL from which symbols may be found. In the ELF case, just
1231 * do RTLD_GLOBAL-style add, so no further messing around needs to
1232 * happen in order that symbols in the loaded .so are findable --
1233 * lookupSymbol() will subsequently see them by dlsym on the program's
1234 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
1236 * In the PEi386 case, open the DLLs and put handles to them in a
1237 * linked list. When looking for a symbol, try all handles in the
1238 * list. This means that we need to load even DLLs that are guaranteed
1239 * to be in the ghc.exe image already, just so we can get a handle
1240 * to give to loadSymbol, so that we can find the symbols. For such
1241 * libraries, the LoadLibrary call should be a no-op except for returning
1246 #if defined(OBJFORMAT_PEi386)
1247 /* A record for storing handles into DLLs. */
1252 struct _OpenedDLL* next;
1257 /* A list thereof. */
1258 static OpenedDLL* opened_dlls = NULL;
1261 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1264 internal_dlopen(const char *dll_name)
1270 // omitted: RTLD_NOW
1271 // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
1273 debugBelch("internal_dlopen: dll_name = '%s'\n", dll_name));
1275 //-------------- Begin critical section ------------------
1276 // This critical section is necessary because dlerror() is not
1277 // required to be reentrant (see POSIX -- IEEE Std 1003.1-2008)
1278 // Also, the error message returned must be copied to preserve it
1281 ACQUIRE_LOCK(&dl_mutex);
1282 hdl = dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
1286 /* dlopen failed; return a ptr to the error msg. */
1288 if (errmsg == NULL) errmsg = "addDLL: unknown error";
1289 errmsg_copy = stgMallocBytes(strlen(errmsg)+1, "addDLL");
1290 strcpy(errmsg_copy, errmsg);
1291 errmsg = errmsg_copy;
1293 RELEASE_LOCK(&dl_mutex);
1294 //--------------- End critical section -------------------
1301 addDLL( char *dll_name )
1303 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1304 /* ------------------- ELF DLL loader ------------------- */
1307 regmatch_t match[NMATCH];
1310 size_t match_length;
1311 #define MAXLINE 1000
1317 IF_DEBUG(linker, debugBelch("addDLL: dll_name = '%s'\n", dll_name));
1318 errmsg = internal_dlopen(dll_name);
1320 if (errmsg == NULL) {
1324 // GHC Trac ticket #2615
1325 // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so)
1326 // contain linker scripts rather than ELF-format object code. This
1327 // code handles the situation by recognizing the real object code
1328 // file name given in the linker script.
1330 // If an "invalid ELF header" error occurs, it is assumed that the
1331 // .so file contains a linker script instead of ELF object code.
1332 // In this case, the code looks for the GROUP ( ... ) linker
1333 // directive. If one is found, the first file name inside the
1334 // parentheses is treated as the name of a dynamic library and the
1335 // code attempts to dlopen that file. If this is also unsuccessful,
1336 // an error message is returned.
1338 // see if the error message is due to an invalid ELF header
1339 IF_DEBUG(linker, debugBelch("errmsg = '%s'\n", errmsg));
1340 result = regexec(&re_invalid, errmsg, (size_t) NMATCH, match, 0);
1341 IF_DEBUG(linker, debugBelch("result = %i\n", result));
1343 // success -- try to read the named file as a linker script
1344 match_length = (size_t) stg_min((match[1].rm_eo - match[1].rm_so),
1346 strncpy(line, (errmsg+(match[1].rm_so)),match_length);
1347 line[match_length] = '\0'; // make sure string is null-terminated
1348 IF_DEBUG(linker, debugBelch ("file name = '%s'\n", line));
1349 if ((fp = fopen(line, "r")) == NULL) {
1350 return errmsg; // return original error if open fails
1352 // try to find a GROUP ( ... ) command
1353 while (fgets(line, MAXLINE, fp) != NULL) {
1354 IF_DEBUG(linker, debugBelch("input line = %s", line));
1355 if (regexec(&re_realso, line, (size_t) NMATCH, match, 0) == 0) {
1356 // success -- try to dlopen the first named file
1357 IF_DEBUG(linker, debugBelch("match%s\n",""));
1358 line[match[1].rm_eo] = '\0';
1359 errmsg = internal_dlopen(line+match[1].rm_so);
1362 // if control reaches here, no GROUP ( ... ) directive was found
1363 // and the original error message is returned to the caller
1369 # elif defined(OBJFORMAT_PEi386)
1370 /* ------------------- Win32 DLL loader ------------------- */
1378 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1380 /* See if we've already got it, and ignore if so. */
1381 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1382 if (0 == strcmp(o_dll->name, dll_name))
1386 /* The file name has no suffix (yet) so that we can try
1387 both foo.dll and foo.drv
1389 The documentation for LoadLibrary says:
1390 If no file name extension is specified in the lpFileName
1391 parameter, the default library extension .dll is
1392 appended. However, the file name string can include a trailing
1393 point character (.) to indicate that the module name has no
1396 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1397 sprintf(buf, "%s.DLL", dll_name);
1398 instance = LoadLibrary(buf);
1399 if (instance == NULL) {
1400 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1401 // KAA: allow loading of drivers (like winspool.drv)
1402 sprintf(buf, "%s.DRV", dll_name);
1403 instance = LoadLibrary(buf);
1404 if (instance == NULL) {
1405 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1406 // #1883: allow loading of unix-style libfoo.dll DLLs
1407 sprintf(buf, "lib%s.DLL", dll_name);
1408 instance = LoadLibrary(buf);
1409 if (instance == NULL) {
1416 /* Add this DLL to the list of DLLs in which to search for symbols. */
1417 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1418 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
1419 strcpy(o_dll->name, dll_name);
1420 o_dll->instance = instance;
1421 o_dll->next = opened_dlls;
1422 opened_dlls = o_dll;
1428 sysErrorBelch(dll_name);
1430 /* LoadLibrary failed; return a ptr to the error msg. */
1431 return "addDLL: could not load DLL";
1434 barf("addDLL: not implemented on this platform");
1438 /* -----------------------------------------------------------------------------
1439 * insert a stable symbol in the hash table
1443 insertStableSymbol(char* obj_name, char* key, StgPtr p)
1445 ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
1449 /* -----------------------------------------------------------------------------
1450 * insert a symbol in the hash table
1453 insertSymbol(char* obj_name, char* key, void* data)
1455 ghciInsertStrHashTable(obj_name, symhash, key, data);
1458 /* -----------------------------------------------------------------------------
1459 * lookup a symbol in the hash table
1462 lookupSymbol( char *lbl )
1465 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s\n", lbl));
1467 ASSERT(symhash != NULL);
1468 val = lookupStrHashTable(symhash, lbl);
1471 IF_DEBUG(linker, debugBelch("lookupSymbol: symbol not found\n"));
1472 # if defined(OBJFORMAT_ELF)
1473 return dlsym(dl_prog_handle, lbl);
1474 # elif defined(OBJFORMAT_MACHO)
1476 /* On OS X 10.3 and later, we use dlsym instead of the old legacy
1479 HACK: On OS X, global symbols are prefixed with an underscore.
1480 However, dlsym wants us to omit the leading underscore from the
1481 symbol name. For now, we simply strip it off here (and ONLY
1484 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s with dlsym\n", lbl));
1485 ASSERT(lbl[0] == '_');
1486 return dlsym(dl_prog_handle, lbl+1);
1488 if(NSIsSymbolNameDefined(lbl)) {
1489 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1490 return NSAddressOfSymbol(symbol);
1494 # endif /* HAVE_DLFCN_H */
1495 # elif defined(OBJFORMAT_PEi386)
1498 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1499 if (sym != NULL) { return sym; };
1501 // Also try looking up the symbol without the @N suffix. Some
1502 // DLLs have the suffixes on their symbols, some don't.
1503 zapTrailingAtSign ( (unsigned char*)lbl );
1504 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1505 if (sym != NULL) { return sym; };
1513 IF_DEBUG(linker, debugBelch("lookupSymbol: value of %s is %p\n", lbl, val));
1518 /* -----------------------------------------------------------------------------
1519 * Debugging aid: look in GHCi's object symbol tables for symbols
1520 * within DELTA bytes of the specified address, and show their names.
1523 void ghci_enquire ( char* addr );
1525 void ghci_enquire ( char* addr )
1530 const int DELTA = 64;
1535 for (oc = objects; oc; oc = oc->next) {
1536 for (i = 0; i < oc->n_symbols; i++) {
1537 sym = oc->symbols[i];
1538 if (sym == NULL) continue;
1541 a = lookupStrHashTable(symhash, sym);
1544 // debugBelch("ghci_enquire: can't find %s\n", sym);
1546 else if (addr-DELTA <= a && a <= addr+DELTA) {
1547 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1555 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1558 mmapForLinker (size_t bytes, nat flags, int fd)
1560 void *map_addr = NULL;
1563 static nat fixed = 0;
1565 pagesize = getpagesize();
1566 size = ROUND_UP(bytes, pagesize);
1568 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1571 if (mmap_32bit_base != 0) {
1572 map_addr = mmap_32bit_base;
1576 result = mmap(map_addr, size, PROT_EXEC|PROT_READ|PROT_WRITE,
1577 MAP_PRIVATE|TRY_MAP_32BIT|fixed|flags, fd, 0);
1579 if (result == MAP_FAILED) {
1580 sysErrorBelch("mmap %lu bytes at %p",(lnat)size,map_addr);
1581 errorBelch("Try specifying an address with +RTS -xm<addr> -RTS");
1582 stg_exit(EXIT_FAILURE);
1585 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1586 if (mmap_32bit_base != 0) {
1587 if (result == map_addr) {
1588 mmap_32bit_base = (StgWord8*)map_addr + size;
1590 if ((W_)result > 0x80000000) {
1591 // oops, we were given memory over 2Gb
1592 #if defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS)
1593 // Some platforms require MAP_FIXED. This is normally
1594 // a bad idea, because MAP_FIXED will overwrite
1595 // existing mappings.
1596 munmap(result,size);
1600 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);
1603 // hmm, we were given memory somewhere else, but it's
1604 // still under 2Gb so we can use it. Next time, ask
1605 // for memory right after the place we just got some
1606 mmap_32bit_base = (StgWord8*)result + size;
1610 if ((W_)result > 0x80000000) {
1611 // oops, we were given memory over 2Gb
1612 // ... try allocating memory somewhere else?;
1613 debugTrace(DEBUG_linker,"MAP_32BIT didn't work; gave us %lu bytes at 0x%p", bytes, result);
1614 munmap(result, size);
1616 // Set a base address and try again... (guess: 1Gb)
1617 mmap_32bit_base = (void*)0x40000000;
1628 mkOc( char *path, char *image, int imageSize,
1629 char *archiveMemberName
1631 #ifdef darwin_HOST_OS
1638 oc = stgMallocBytes(sizeof(ObjectCode), "loadArchive(oc)");
1640 # if defined(OBJFORMAT_ELF)
1641 oc->formatName = "ELF";
1642 # elif defined(OBJFORMAT_PEi386)
1643 oc->formatName = "PEi386";
1644 # elif defined(OBJFORMAT_MACHO)
1645 oc->formatName = "Mach-O";
1648 barf("loadObj: not implemented on this platform");
1652 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1653 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1654 strcpy(oc->fileName, path);
1656 if (archiveMemberName) {
1657 oc->archiveMemberName = stgMallocBytes( strlen(archiveMemberName)+1, "loadObj" );
1658 strcpy(oc->archiveMemberName, archiveMemberName);
1661 oc->archiveMemberName = NULL;
1664 oc->fileSize = imageSize;
1666 oc->sections = NULL;
1667 oc->proddables = NULL;
1670 #ifdef darwin_HOST_OS
1671 oc->misalignment = misalignment;
1675 /* chain it onto the list of objects */
1683 loadArchive( char *path )
1690 size_t thisFileNameSize;
1692 size_t fileNameSize;
1693 int isObject, isGnuIndex;
1696 int gnuFileIndexSize;
1697 #if !defined(USE_MMAP) && defined(darwin_HOST_OS)
1701 IF_DEBUG(linker, debugBelch("loadArchive: Loading archive `%s'\n", path));
1703 gnuFileIndex = NULL;
1704 gnuFileIndexSize = 0;
1707 fileName = stgMallocBytes(fileNameSize, "loadArchive(fileName)");
1709 f = fopen(path, "rb");
1711 barf("loadObj: can't read `%s'", path);
1713 n = fread ( tmp, 1, 8, f );
1714 if (strncmp(tmp, "!<arch>\n", 8) != 0)
1715 barf("loadArchive: Not an archive: `%s'", path);
1718 n = fread ( fileName, 1, 16, f );
1724 barf("loadArchive: Failed reading file name from `%s'", path);
1727 n = fread ( tmp, 1, 12, f );
1729 barf("loadArchive: Failed reading mod time from `%s'", path);
1730 n = fread ( tmp, 1, 6, f );
1732 barf("loadArchive: Failed reading owner from `%s'", path);
1733 n = fread ( tmp, 1, 6, f );
1735 barf("loadArchive: Failed reading group from `%s'", path);
1736 n = fread ( tmp, 1, 8, f );
1738 barf("loadArchive: Failed reading mode from `%s'", path);
1739 n = fread ( tmp, 1, 10, f );
1741 barf("loadArchive: Failed reading size from `%s'", path);
1743 for (n = 0; isdigit(tmp[n]); n++);
1745 memberSize = atoi(tmp);
1746 n = fread ( tmp, 1, 2, f );
1747 if (strncmp(tmp, "\x60\x0A", 2) != 0)
1748 barf("loadArchive: Failed reading magic from `%s' at %ld. Got %c%c",
1749 path, ftell(f), tmp[0], tmp[1]);
1752 /* Check for BSD-variant large filenames */
1753 if (0 == strncmp(fileName, "#1/", 3)) {
1754 fileName[16] = '\0';
1755 if (isdigit(fileName[3])) {
1756 for (n = 4; isdigit(fileName[n]); n++);
1758 thisFileNameSize = atoi(fileName + 3);
1759 memberSize -= thisFileNameSize;
1760 if (thisFileNameSize >= fileNameSize) {
1761 /* Double it to avoid potentially continually
1762 increasing it by 1 */
1763 fileNameSize = thisFileNameSize * 2;
1764 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1766 n = fread ( fileName, 1, thisFileNameSize, f );
1767 if (n != (int)thisFileNameSize) {
1768 barf("loadArchive: Failed reading filename from `%s'",
1771 fileName[thisFileNameSize] = 0;
1774 barf("loadArchive: BSD-variant filename size not found while reading filename from `%s'", path);
1777 /* Check for GNU file index file */
1778 else if (0 == strncmp(fileName, "//", 2)) {
1780 thisFileNameSize = 0;
1783 /* Check for a file in the GNU file index */
1784 else if (fileName[0] == '/') {
1785 if (isdigit(fileName[1])) {
1788 for (n = 2; isdigit(fileName[n]); n++);
1790 n = atoi(fileName + 1);
1792 if (gnuFileIndex == NULL) {
1793 barf("loadArchive: GNU-variant filename without an index while reading from `%s'", path);
1795 if (n < 0 || n > gnuFileIndexSize) {
1796 barf("loadArchive: GNU-variant filename offset %d out of range [0..%d] while reading filename from `%s'", n, gnuFileIndexSize, path);
1798 if (n != 0 && gnuFileIndex[n - 1] != '\n') {
1799 barf("loadArchive: GNU-variant filename offset %d invalid (range [0..%d]) while reading filename from `%s'", n, gnuFileIndexSize, path);
1801 for (i = n; gnuFileIndex[i] != '/'; i++);
1802 thisFileNameSize = i - n;
1803 if (thisFileNameSize >= fileNameSize) {
1804 /* Double it to avoid potentially continually
1805 increasing it by 1 */
1806 fileNameSize = thisFileNameSize * 2;
1807 fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1809 memcpy(fileName, gnuFileIndex + n, thisFileNameSize);
1810 fileName[thisFileNameSize] = '\0';
1812 else if (fileName[1] == ' ') {
1814 thisFileNameSize = 0;
1817 barf("loadArchive: GNU-variant filename offset not found while reading filename from `%s'", path);
1820 /* Finally, the case where the filename field actually contains
1823 /* GNU ar terminates filenames with a '/', this allowing
1824 spaces in filenames. So first look to see if there is a
1826 for (thisFileNameSize = 0;
1827 thisFileNameSize < 16;
1828 thisFileNameSize++) {
1829 if (fileName[thisFileNameSize] == '/') {
1830 fileName[thisFileNameSize] = '\0';
1834 /* If we didn't find a '/', then a space teminates the
1835 filename. Note that if we don't find one, then
1836 thisFileNameSize ends up as 16, and we already have the
1838 if (thisFileNameSize == 16) {
1839 for (thisFileNameSize = 0;
1840 thisFileNameSize < 16;
1841 thisFileNameSize++) {
1842 if (fileName[thisFileNameSize] == ' ') {
1843 fileName[thisFileNameSize] = '\0';
1851 debugBelch("loadArchive: Found member file `%s'\n", fileName));
1853 isObject = thisFileNameSize >= 2
1854 && fileName[thisFileNameSize - 2] == '.'
1855 && fileName[thisFileNameSize - 1] == 'o';
1858 char *archiveMemberName;
1860 IF_DEBUG(linker, debugBelch("loadArchive: Member is an object file...loading...\n"));
1862 /* We can't mmap from the archive directly, as object
1863 files need to be 8-byte aligned but files in .ar
1864 archives are 2-byte aligned. When possible we use mmap
1865 to get some anonymous memory, as on 64-bit platforms if
1866 we use malloc then we can be given memory above 2^32.
1867 In the mmap case we're probably wasting lots of space;
1868 we could do better. */
1869 #if defined(USE_MMAP)
1870 image = mmapForLinker(memberSize, MAP_ANONYMOUS, -1);
1871 #elif defined(darwin_HOST_OS)
1873 misalignment = machoGetMisalignment(f);
1874 image = stgMallocBytes(memberSize + misalignment, "loadArchive(image)");
1875 image += misalignment;
1877 image = stgMallocBytes(memberSize, "loadArchive(image)");
1879 n = fread ( image, 1, memberSize, f );
1880 if (n != memberSize) {
1881 barf("loadArchive: error whilst reading `%s'", path);
1884 archiveMemberName = stgMallocBytes(strlen(path) + thisFileNameSize + 3,
1885 "loadArchive(file)");
1886 sprintf(archiveMemberName, "%s(%.*s)",
1887 path, (int)thisFileNameSize, fileName);
1889 oc = mkOc(path, image, memberSize, archiveMemberName
1891 #ifdef darwin_HOST_OS
1897 stgFree(archiveMemberName);
1899 if (0 == loadOc(oc)) {
1904 else if (isGnuIndex) {
1905 if (gnuFileIndex != NULL) {
1906 barf("loadArchive: GNU-variant index found, but already have an index, while reading filename from `%s'", path);
1908 IF_DEBUG(linker, debugBelch("loadArchive: Found GNU-variant file index\n"));
1910 gnuFileIndex = mmapForLinker(memberSize + 1, MAP_ANONYMOUS, -1);
1912 gnuFileIndex = stgMallocBytes(memberSize + 1, "loadArchive(image)");
1914 n = fread ( gnuFileIndex, 1, memberSize, f );
1915 if (n != memberSize) {
1916 barf("loadArchive: error whilst reading `%s'", path);
1918 gnuFileIndex[memberSize] = '/';
1919 gnuFileIndexSize = memberSize;
1922 n = fseek(f, memberSize, SEEK_CUR);
1924 barf("loadArchive: error whilst seeking by %d in `%s'",
1927 /* .ar files are 2-byte aligned */
1928 if (memberSize % 2) {
1929 n = fread ( tmp, 1, 1, f );
1935 barf("loadArchive: Failed reading padding from `%s'", path);
1944 if (gnuFileIndex != NULL) {
1946 munmap(gnuFileIndex, gnuFileIndexSize + 1);
1948 stgFree(gnuFileIndex);
1955 /* -----------------------------------------------------------------------------
1956 * Load an obj (populate the global symbol table, but don't resolve yet)
1958 * Returns: 1 if ok, 0 on error.
1961 loadObj( char *path )
1972 # if defined(darwin_HOST_OS)
1976 IF_DEBUG(linker, debugBelch("loadObj %s\n", path));
1980 /* debugBelch("loadObj %s\n", path ); */
1982 /* Check that we haven't already loaded this object.
1983 Ignore requests to load multiple times */
1987 for (o = objects; o; o = o->next) {
1988 if (0 == strcmp(o->fileName, path)) {
1990 break; /* don't need to search further */
1994 IF_DEBUG(linker, debugBelch(
1995 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1996 "same object file twice:\n"
1998 "GHCi will ignore this, but be warned.\n"
2000 return 1; /* success */
2004 r = stat(path, &st);
2006 IF_DEBUG(linker, debugBelch("File doesn't exist\n"));
2010 fileSize = st.st_size;
2013 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
2015 #if defined(openbsd_HOST_OS)
2016 fd = open(path, O_RDONLY, S_IRUSR);
2018 fd = open(path, O_RDONLY);
2021 barf("loadObj: can't open `%s'", path);
2023 image = mmapForLinker(fileSize, 0, fd);
2027 #else /* !USE_MMAP */
2028 /* load the image into memory */
2029 f = fopen(path, "rb");
2031 barf("loadObj: can't read `%s'", path);
2033 # if defined(mingw32_HOST_OS)
2034 // TODO: We would like to use allocateExec here, but allocateExec
2035 // cannot currently allocate blocks large enough.
2036 image = VirtualAlloc(NULL, fileSize, MEM_RESERVE | MEM_COMMIT,
2037 PAGE_EXECUTE_READWRITE);
2038 # elif defined(darwin_HOST_OS)
2039 // In a Mach-O .o file, all sections can and will be misaligned
2040 // if the total size of the headers is not a multiple of the
2041 // desired alignment. This is fine for .o files that only serve
2042 // as input for the static linker, but it's not fine for us,
2043 // as SSE (used by gcc for floating point) and Altivec require
2044 // 16-byte alignment.
2045 // We calculate the correct alignment from the header before
2046 // reading the file, and then we misalign image on purpose so
2047 // that the actual sections end up aligned again.
2048 misalignment = machoGetMisalignment(f);
2049 image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
2050 image += misalignment;
2052 image = stgMallocBytes(fileSize, "loadObj(image)");
2057 n = fread ( image, 1, fileSize, f );
2059 barf("loadObj: error whilst reading `%s'", path);
2062 #endif /* USE_MMAP */
2064 oc = mkOc(path, image, fileSize, NULL
2066 #ifdef darwin_HOST_OS
2076 loadOc( ObjectCode* oc ) {
2079 IF_DEBUG(linker, debugBelch("loadOc\n"));
2081 # if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2082 r = ocAllocateSymbolExtras_MachO ( oc );
2084 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO failed\n"));
2087 # elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2088 r = ocAllocateSymbolExtras_ELF ( oc );
2090 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_ELF failed\n"));
2095 /* verify the in-memory image */
2096 # if defined(OBJFORMAT_ELF)
2097 r = ocVerifyImage_ELF ( oc );
2098 # elif defined(OBJFORMAT_PEi386)
2099 r = ocVerifyImage_PEi386 ( oc );
2100 # elif defined(OBJFORMAT_MACHO)
2101 r = ocVerifyImage_MachO ( oc );
2103 barf("loadObj: no verify method");
2106 IF_DEBUG(linker, debugBelch("ocVerifyImage_* failed\n"));
2110 /* build the symbol list for this image */
2111 # if defined(OBJFORMAT_ELF)
2112 r = ocGetNames_ELF ( oc );
2113 # elif defined(OBJFORMAT_PEi386)
2114 r = ocGetNames_PEi386 ( oc );
2115 # elif defined(OBJFORMAT_MACHO)
2116 r = ocGetNames_MachO ( oc );
2118 barf("loadObj: no getNames method");
2121 IF_DEBUG(linker, debugBelch("ocGetNames_* failed\n"));
2125 /* loaded, but not resolved yet */
2126 oc->status = OBJECT_LOADED;
2127 IF_DEBUG(linker, debugBelch("loadObj done.\n"));
2132 /* -----------------------------------------------------------------------------
2133 * resolve all the currently unlinked objects in memory
2135 * Returns: 1 if ok, 0 on error.
2143 IF_DEBUG(linker, debugBelch("resolveObjs: start\n"));
2146 for (oc = objects; oc; oc = oc->next) {
2147 if (oc->status != OBJECT_RESOLVED) {
2148 # if defined(OBJFORMAT_ELF)
2149 r = ocResolve_ELF ( oc );
2150 # elif defined(OBJFORMAT_PEi386)
2151 r = ocResolve_PEi386 ( oc );
2152 # elif defined(OBJFORMAT_MACHO)
2153 r = ocResolve_MachO ( oc );
2155 barf("resolveObjs: not implemented on this platform");
2157 if (!r) { return r; }
2158 oc->status = OBJECT_RESOLVED;
2161 IF_DEBUG(linker, debugBelch("resolveObjs: done\n"));
2165 /* -----------------------------------------------------------------------------
2166 * delete an object from the pool
2169 unloadObj( char *path )
2171 ObjectCode *oc, *prev;
2172 HsBool unloadedAnyObj = HS_BOOL_FALSE;
2174 ASSERT(symhash != NULL);
2175 ASSERT(objects != NULL);
2180 for (oc = objects; oc; prev = oc, oc = oc->next) {
2181 if (!strcmp(oc->fileName,path)) {
2183 /* Remove all the mappings for the symbols within this
2188 for (i = 0; i < oc->n_symbols; i++) {
2189 if (oc->symbols[i] != NULL) {
2190 removeStrHashTable(symhash, oc->symbols[i], NULL);
2198 prev->next = oc->next;
2201 // We're going to leave this in place, in case there are
2202 // any pointers from the heap into it:
2203 // #ifdef mingw32_HOST_OS
2204 // VirtualFree(oc->image);
2206 // stgFree(oc->image);
2208 stgFree(oc->fileName);
2209 stgFree(oc->symbols);
2210 stgFree(oc->sections);
2213 /* This could be a member of an archive so continue
2214 * unloading other members. */
2215 unloadedAnyObj = HS_BOOL_TRUE;
2219 if (unloadedAnyObj) {
2223 errorBelch("unloadObj: can't find `%s' to unload", path);
2228 /* -----------------------------------------------------------------------------
2229 * Sanity checking. For each ObjectCode, maintain a list of address ranges
2230 * which may be prodded during relocation, and abort if we try and write
2231 * outside any of these.
2233 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
2236 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
2237 IF_DEBUG(linker, debugBelch("addProddableBlock %p %p %d\n", oc, start, size));
2241 pb->next = oc->proddables;
2242 oc->proddables = pb;
2245 static void checkProddableBlock ( ObjectCode* oc, void* addr )
2248 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
2249 char* s = (char*)(pb->start);
2250 char* e = s + pb->size - 1;
2251 char* a = (char*)addr;
2252 /* Assumes that the biggest fixup involves a 4-byte write. This
2253 probably needs to be changed to 8 (ie, +7) on 64-bit
2255 if (a >= s && (a+3) <= e) return;
2257 barf("checkProddableBlock: invalid fixup in runtime linker");
2260 /* -----------------------------------------------------------------------------
2261 * Section management.
2263 static void addSection ( ObjectCode* oc, SectionKind kind,
2264 void* start, void* end )
2266 Section* s = stgMallocBytes(sizeof(Section), "addSection");
2270 s->next = oc->sections;
2273 debugBelch("addSection: %p-%p (size %d), kind %d\n",
2274 start, ((char*)end)-1, end - start + 1, kind );
2279 /* --------------------------------------------------------------------------
2281 * This is about allocating a small chunk of memory for every symbol in the
2282 * object file. We make sure that the SymboLExtras are always "in range" of
2283 * limited-range PC-relative instructions on various platforms by allocating
2284 * them right next to the object code itself.
2287 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
2290 ocAllocateSymbolExtras
2292 Allocate additional space at the end of the object file image to make room
2293 for jump islands (powerpc, x86_64) and GOT entries (x86_64).
2295 PowerPC relative branch instructions have a 24 bit displacement field.
2296 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
2297 If a particular imported symbol is outside this range, we have to redirect
2298 the jump to a short piece of new code that just loads the 32bit absolute
2299 address and jumps there.
2300 On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
2303 This function just allocates space for one SymbolExtra for every
2304 undefined symbol in the object file. The code for the jump islands is
2305 filled in by makeSymbolExtra below.
2308 static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
2315 int misalignment = 0;
2316 #ifdef darwin_HOST_OS
2317 misalignment = oc->misalignment;
2323 // round up to the nearest 4
2324 aligned = (oc->fileSize + 3) & ~3;
2327 pagesize = getpagesize();
2328 n = ROUND_UP( oc->fileSize, pagesize );
2329 m = ROUND_UP( aligned + sizeof (SymbolExtra) * count, pagesize );
2331 /* we try to use spare space at the end of the last page of the
2332 * image for the jump islands, but if there isn't enough space
2333 * then we have to map some (anonymously, remembering MAP_32BIT).
2335 if( m > n ) // we need to allocate more pages
2337 oc->symbol_extras = mmapForLinker(sizeof(SymbolExtra) * count,
2342 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2345 oc->image -= misalignment;
2346 oc->image = stgReallocBytes( oc->image,
2348 aligned + sizeof (SymbolExtra) * count,
2349 "ocAllocateSymbolExtras" );
2350 oc->image += misalignment;
2352 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2353 #endif /* USE_MMAP */
2355 memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
2358 oc->symbol_extras = NULL;
2360 oc->first_symbol_extra = first;
2361 oc->n_symbol_extras = count;
2366 static SymbolExtra* makeSymbolExtra( ObjectCode* oc,
2367 unsigned long symbolNumber,
2368 unsigned long target )
2372 ASSERT( symbolNumber >= oc->first_symbol_extra
2373 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
2375 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
2377 #ifdef powerpc_HOST_ARCH
2378 // lis r12, hi16(target)
2379 extra->jumpIsland.lis_r12 = 0x3d80;
2380 extra->jumpIsland.hi_addr = target >> 16;
2382 // ori r12, r12, lo16(target)
2383 extra->jumpIsland.ori_r12_r12 = 0x618c;
2384 extra->jumpIsland.lo_addr = target & 0xffff;
2387 extra->jumpIsland.mtctr_r12 = 0x7d8903a6;
2390 extra->jumpIsland.bctr = 0x4e800420;
2392 #ifdef x86_64_HOST_ARCH
2394 static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
2395 extra->addr = target;
2396 memcpy(extra->jumpIsland, jmp, 6);
2404 /* --------------------------------------------------------------------------
2405 * PowerPC specifics (instruction cache flushing)
2406 * ------------------------------------------------------------------------*/
2408 #ifdef powerpc_HOST_ARCH
2410 ocFlushInstructionCache
2412 Flush the data & instruction caches.
2413 Because the PPC has split data/instruction caches, we have to
2414 do that whenever we modify code at runtime.
2416 static void ocFlushInstructionCacheFrom(void* begin, size_t length)
2418 size_t n = (length + 3) / 4;
2419 unsigned long* p = begin;
2423 __asm__ volatile ( "dcbf 0,%0\n\t"
2431 __asm__ volatile ( "sync\n\t"
2435 static void ocFlushInstructionCache( ObjectCode *oc )
2437 /* The main object code */
2438 ocFlushInstructionCacheFrom(oc->image + oc->misalignment, oc->fileSize);
2441 ocFlushInstructionCacheFrom(oc->symbol_extras, sizeof(SymbolExtra) * oc->n_symbol_extras);
2445 /* --------------------------------------------------------------------------
2446 * PEi386 specifics (Win32 targets)
2447 * ------------------------------------------------------------------------*/
2449 /* The information for this linker comes from
2450 Microsoft Portable Executable
2451 and Common Object File Format Specification
2452 revision 5.1 January 1998
2453 which SimonM says comes from the MS Developer Network CDs.
2455 It can be found there (on older CDs), but can also be found
2458 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
2460 (this is Rev 6.0 from February 1999).
2462 Things move, so if that fails, try searching for it via
2464 http://www.google.com/search?q=PE+COFF+specification
2466 The ultimate reference for the PE format is the Winnt.h
2467 header file that comes with the Platform SDKs; as always,
2468 implementations will drift wrt their documentation.
2470 A good background article on the PE format is Matt Pietrek's
2471 March 1994 article in Microsoft System Journal (MSJ)
2472 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
2473 Win32 Portable Executable File Format." The info in there
2474 has recently been updated in a two part article in
2475 MSDN magazine, issues Feb and March 2002,
2476 "Inside Windows: An In-Depth Look into the Win32 Portable
2477 Executable File Format"
2479 John Levine's book "Linkers and Loaders" contains useful
2484 #if defined(OBJFORMAT_PEi386)
2488 typedef unsigned char UChar;
2489 typedef unsigned short UInt16;
2490 typedef unsigned int UInt32;
2497 UInt16 NumberOfSections;
2498 UInt32 TimeDateStamp;
2499 UInt32 PointerToSymbolTable;
2500 UInt32 NumberOfSymbols;
2501 UInt16 SizeOfOptionalHeader;
2502 UInt16 Characteristics;
2506 #define sizeof_COFF_header 20
2513 UInt32 VirtualAddress;
2514 UInt32 SizeOfRawData;
2515 UInt32 PointerToRawData;
2516 UInt32 PointerToRelocations;
2517 UInt32 PointerToLinenumbers;
2518 UInt16 NumberOfRelocations;
2519 UInt16 NumberOfLineNumbers;
2520 UInt32 Characteristics;
2524 #define sizeof_COFF_section 40
2531 UInt16 SectionNumber;
2534 UChar NumberOfAuxSymbols;
2538 #define sizeof_COFF_symbol 18
2543 UInt32 VirtualAddress;
2544 UInt32 SymbolTableIndex;
2549 #define sizeof_COFF_reloc 10
2552 /* From PE spec doc, section 3.3.2 */
2553 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
2554 windows.h -- for the same purpose, but I want to know what I'm
2556 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
2557 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
2558 #define MYIMAGE_FILE_DLL 0x2000
2559 #define MYIMAGE_FILE_SYSTEM 0x1000
2560 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
2561 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
2562 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
2564 /* From PE spec doc, section 5.4.2 and 5.4.4 */
2565 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
2566 #define MYIMAGE_SYM_CLASS_STATIC 3
2567 #define MYIMAGE_SYM_UNDEFINED 0
2569 /* From PE spec doc, section 4.1 */
2570 #define MYIMAGE_SCN_CNT_CODE 0x00000020
2571 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
2572 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
2574 /* From PE spec doc, section 5.2.1 */
2575 #define MYIMAGE_REL_I386_DIR32 0x0006
2576 #define MYIMAGE_REL_I386_REL32 0x0014
2579 /* We use myindex to calculate array addresses, rather than
2580 simply doing the normal subscript thing. That's because
2581 some of the above structs have sizes which are not
2582 a whole number of words. GCC rounds their sizes up to a
2583 whole number of words, which means that the address calcs
2584 arising from using normal C indexing or pointer arithmetic
2585 are just plain wrong. Sigh.
2588 myindex ( int scale, void* base, int index )
2591 ((UChar*)base) + scale * index;
2596 printName ( UChar* name, UChar* strtab )
2598 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2599 UInt32 strtab_offset = * (UInt32*)(name+4);
2600 debugBelch("%s", strtab + strtab_offset );
2603 for (i = 0; i < 8; i++) {
2604 if (name[i] == 0) break;
2605 debugBelch("%c", name[i] );
2612 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
2614 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2615 UInt32 strtab_offset = * (UInt32*)(name+4);
2616 strncpy ( (char*)dst, (char*)strtab+strtab_offset, dstSize );
2622 if (name[i] == 0) break;
2632 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
2635 /* If the string is longer than 8 bytes, look in the
2636 string table for it -- this will be correctly zero terminated.
2638 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2639 UInt32 strtab_offset = * (UInt32*)(name+4);
2640 return ((UChar*)strtab) + strtab_offset;
2642 /* Otherwise, if shorter than 8 bytes, return the original,
2643 which by defn is correctly terminated.
2645 if (name[7]==0) return name;
2646 /* The annoying case: 8 bytes. Copy into a temporary
2647 (XXX which is never freed ...)
2649 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
2651 strncpy((char*)newstr,(char*)name,8);
2656 /* Getting the name of a section is mildly tricky, so we make a
2657 function for it. Sadly, in one case we have to copy the string
2658 (when it is exactly 8 bytes long there's no trailing '\0'), so for
2659 consistency we *always* copy the string; the caller must free it
2662 cstring_from_section_name (UChar* name, UChar* strtab)
2667 int strtab_offset = strtol((char*)name+1,NULL,10);
2668 int len = strlen(((char*)strtab) + strtab_offset);
2670 newstr = stgMallocBytes(len, "cstring_from_section_symbol_name");
2671 strcpy((char*)newstr, (char*)((UChar*)strtab) + strtab_offset);
2676 newstr = stgMallocBytes(9, "cstring_from_section_symbol_name");
2678 strncpy((char*)newstr,(char*)name,8);
2684 /* Just compares the short names (first 8 chars) */
2685 static COFF_section *
2686 findPEi386SectionCalled ( ObjectCode* oc, UChar* name )
2690 = (COFF_header*)(oc->image);
2691 COFF_section* sectab
2693 ((UChar*)(oc->image))
2694 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2696 for (i = 0; i < hdr->NumberOfSections; i++) {
2699 COFF_section* section_i
2701 myindex ( sizeof_COFF_section, sectab, i );
2702 n1 = (UChar*) &(section_i->Name);
2704 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
2705 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
2706 n1[6]==n2[6] && n1[7]==n2[7])
2715 zapTrailingAtSign ( UChar* sym )
2717 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
2719 if (sym[0] == 0) return;
2721 while (sym[i] != 0) i++;
2724 while (j > 0 && my_isdigit(sym[j])) j--;
2725 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
2730 lookupSymbolInDLLs ( UChar *lbl )
2735 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
2736 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
2738 if (lbl[0] == '_') {
2739 /* HACK: if the name has an initial underscore, try stripping
2740 it off & look that up first. I've yet to verify whether there's
2741 a Rule that governs whether an initial '_' *should always* be
2742 stripped off when mapping from import lib name to the DLL name.
2744 sym = GetProcAddress(o_dll->instance, (char*)(lbl+1));
2746 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
2750 sym = GetProcAddress(o_dll->instance, (char*)lbl);
2752 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
2761 ocVerifyImage_PEi386 ( ObjectCode* oc )
2766 COFF_section* sectab;
2767 COFF_symbol* symtab;
2769 /* debugBelch("\nLOADING %s\n", oc->fileName); */
2770 hdr = (COFF_header*)(oc->image);
2771 sectab = (COFF_section*) (
2772 ((UChar*)(oc->image))
2773 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2775 symtab = (COFF_symbol*) (
2776 ((UChar*)(oc->image))
2777 + hdr->PointerToSymbolTable
2779 strtab = ((UChar*)symtab)
2780 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2782 if (hdr->Machine != 0x14c) {
2783 errorBelch("%s: Not x86 PEi386", oc->fileName);
2786 if (hdr->SizeOfOptionalHeader != 0) {
2787 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
2790 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
2791 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
2792 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
2793 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
2794 errorBelch("%s: Not a PEi386 object file", oc->fileName);
2797 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
2798 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
2799 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
2801 (int)(hdr->Characteristics));
2804 /* If the string table size is way crazy, this might indicate that
2805 there are more than 64k relocations, despite claims to the
2806 contrary. Hence this test. */
2807 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
2809 if ( (*(UInt32*)strtab) > 600000 ) {
2810 /* Note that 600k has no special significance other than being
2811 big enough to handle the almost-2MB-sized lumps that
2812 constitute HSwin32*.o. */
2813 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
2818 /* No further verification after this point; only debug printing. */
2820 IF_DEBUG(linker, i=1);
2821 if (i == 0) return 1;
2823 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
2824 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
2825 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
2828 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
2829 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
2830 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
2831 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
2832 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
2833 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
2834 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
2836 /* Print the section table. */
2838 for (i = 0; i < hdr->NumberOfSections; i++) {
2840 COFF_section* sectab_i
2842 myindex ( sizeof_COFF_section, sectab, i );
2849 printName ( sectab_i->Name, strtab );
2859 sectab_i->VirtualSize,
2860 sectab_i->VirtualAddress,
2861 sectab_i->SizeOfRawData,
2862 sectab_i->PointerToRawData,
2863 sectab_i->NumberOfRelocations,
2864 sectab_i->PointerToRelocations,
2865 sectab_i->PointerToRawData
2867 reltab = (COFF_reloc*) (
2868 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2871 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2872 /* If the relocation field (a short) has overflowed, the
2873 * real count can be found in the first reloc entry.
2875 * See Section 4.1 (last para) of the PE spec (rev6.0).
2877 COFF_reloc* rel = (COFF_reloc*)
2878 myindex ( sizeof_COFF_reloc, reltab, 0 );
2879 noRelocs = rel->VirtualAddress;
2882 noRelocs = sectab_i->NumberOfRelocations;
2886 for (; j < noRelocs; j++) {
2888 COFF_reloc* rel = (COFF_reloc*)
2889 myindex ( sizeof_COFF_reloc, reltab, j );
2891 " type 0x%-4x vaddr 0x%-8x name `",
2893 rel->VirtualAddress );
2894 sym = (COFF_symbol*)
2895 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
2896 /* Hmm..mysterious looking offset - what's it for? SOF */
2897 printName ( sym->Name, strtab -10 );
2904 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
2905 debugBelch("---START of string table---\n");
2906 for (i = 4; i < *(Int32*)strtab; i++) {
2908 debugBelch("\n"); else
2909 debugBelch("%c", strtab[i] );
2911 debugBelch("--- END of string table---\n");
2916 COFF_symbol* symtab_i;
2917 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2918 symtab_i = (COFF_symbol*)
2919 myindex ( sizeof_COFF_symbol, symtab, i );
2925 printName ( symtab_i->Name, strtab );
2934 (Int32)(symtab_i->SectionNumber),
2935 (UInt32)symtab_i->Type,
2936 (UInt32)symtab_i->StorageClass,
2937 (UInt32)symtab_i->NumberOfAuxSymbols
2939 i += symtab_i->NumberOfAuxSymbols;
2949 ocGetNames_PEi386 ( ObjectCode* oc )
2952 COFF_section* sectab;
2953 COFF_symbol* symtab;
2960 hdr = (COFF_header*)(oc->image);
2961 sectab = (COFF_section*) (
2962 ((UChar*)(oc->image))
2963 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2965 symtab = (COFF_symbol*) (
2966 ((UChar*)(oc->image))
2967 + hdr->PointerToSymbolTable
2969 strtab = ((UChar*)(oc->image))
2970 + hdr->PointerToSymbolTable
2971 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2973 /* Allocate space for any (local, anonymous) .bss sections. */
2975 for (i = 0; i < hdr->NumberOfSections; i++) {
2978 COFF_section* sectab_i
2980 myindex ( sizeof_COFF_section, sectab, i );
2982 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
2984 if (0 != strcmp(secname, ".bss")) {
2991 /* sof 10/05: the PE spec text isn't too clear regarding what
2992 * the SizeOfRawData field is supposed to hold for object
2993 * file sections containing just uninitialized data -- for executables,
2994 * it is supposed to be zero; unclear what it's supposed to be
2995 * for object files. However, VirtualSize is guaranteed to be
2996 * zero for object files, which definitely suggests that SizeOfRawData
2997 * will be non-zero (where else would the size of this .bss section be
2998 * stored?) Looking at the COFF_section info for incoming object files,
2999 * this certainly appears to be the case.
3001 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
3002 * object files up until now. This turned out to bite us with ghc-6.4.1's use
3003 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
3004 * variable decls into to the .bss section. (The specific function in Q which
3005 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
3007 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
3008 /* This is a non-empty .bss section. Allocate zeroed space for
3009 it, and set its PointerToRawData field such that oc->image +
3010 PointerToRawData == addr_of_zeroed_space. */
3011 bss_sz = sectab_i->VirtualSize;
3012 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
3013 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
3014 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
3015 addProddableBlock(oc, zspace, bss_sz);
3016 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
3019 /* Copy section information into the ObjectCode. */
3021 for (i = 0; i < hdr->NumberOfSections; i++) {
3027 = SECTIONKIND_OTHER;
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_DEBUG(linker, debugBelch("section name = %s\n", secname ));
3037 /* I'm sure this is the Right Way to do it. However, the
3038 alternative of testing the sectab_i->Name field seems to
3039 work ok with Cygwin.
3041 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
3042 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
3043 kind = SECTIONKIND_CODE_OR_RODATA;
3046 if (0==strcmp(".text",(char*)secname) ||
3047 0==strcmp(".rdata",(char*)secname)||
3048 0==strcmp(".rodata",(char*)secname))
3049 kind = SECTIONKIND_CODE_OR_RODATA;
3050 if (0==strcmp(".data",(char*)secname) ||
3051 0==strcmp(".bss",(char*)secname))
3052 kind = SECTIONKIND_RWDATA;
3054 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
3055 sz = sectab_i->SizeOfRawData;
3056 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
3058 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
3059 end = start + sz - 1;
3061 if (kind == SECTIONKIND_OTHER
3062 /* Ignore sections called which contain stabs debugging
3064 && 0 != strcmp(".stab", (char*)secname)
3065 && 0 != strcmp(".stabstr", (char*)secname)
3066 /* ignore constructor section for now */
3067 && 0 != strcmp(".ctors", (char*)secname)
3068 /* ignore section generated from .ident */
3069 && 0!= strncmp(".debug", (char*)secname, 6)
3070 /* ignore unknown section that appeared in gcc 3.4.5(?) */
3071 && 0!= strcmp(".reloc", (char*)secname)
3072 && 0 != strcmp(".rdata$zzz", (char*)secname)
3074 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", secname, oc->fileName);
3079 if (kind != SECTIONKIND_OTHER && end >= start) {
3080 addSection(oc, kind, start, end);
3081 addProddableBlock(oc, start, end - start + 1);
3087 /* Copy exported symbols into the ObjectCode. */
3089 oc->n_symbols = hdr->NumberOfSymbols;
3090 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3091 "ocGetNames_PEi386(oc->symbols)");
3092 /* Call me paranoid; I don't care. */
3093 for (i = 0; i < oc->n_symbols; i++)
3094 oc->symbols[i] = NULL;
3098 COFF_symbol* symtab_i;
3099 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
3100 symtab_i = (COFF_symbol*)
3101 myindex ( sizeof_COFF_symbol, symtab, i );
3105 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
3106 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
3107 /* This symbol is global and defined, viz, exported */
3108 /* for MYIMAGE_SYMCLASS_EXTERNAL
3109 && !MYIMAGE_SYM_UNDEFINED,
3110 the address of the symbol is:
3111 address of relevant section + offset in section
3113 COFF_section* sectabent
3114 = (COFF_section*) myindex ( sizeof_COFF_section,
3116 symtab_i->SectionNumber-1 );
3117 addr = ((UChar*)(oc->image))
3118 + (sectabent->PointerToRawData
3122 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
3123 && symtab_i->Value > 0) {
3124 /* This symbol isn't in any section at all, ie, global bss.
3125 Allocate zeroed space for it. */
3126 addr = stgCallocBytes(1, symtab_i->Value,
3127 "ocGetNames_PEi386(non-anonymous bss)");
3128 addSection(oc, SECTIONKIND_RWDATA, addr,
3129 ((UChar*)addr) + symtab_i->Value - 1);
3130 addProddableBlock(oc, addr, symtab_i->Value);
3131 /* debugBelch("BSS section at 0x%x\n", addr); */
3134 if (addr != NULL ) {
3135 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
3136 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
3137 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
3138 ASSERT(i >= 0 && i < oc->n_symbols);
3139 /* cstring_from_COFF_symbol_name always succeeds. */
3140 oc->symbols[i] = (char*)sname;
3141 ghciInsertStrHashTable(oc->fileName, symhash, (char*)sname, addr);
3145 "IGNORING symbol %d\n"
3149 printName ( symtab_i->Name, strtab );
3158 (Int32)(symtab_i->SectionNumber),
3159 (UInt32)symtab_i->Type,
3160 (UInt32)symtab_i->StorageClass,
3161 (UInt32)symtab_i->NumberOfAuxSymbols
3166 i += symtab_i->NumberOfAuxSymbols;
3175 ocResolve_PEi386 ( ObjectCode* oc )
3178 COFF_section* sectab;
3179 COFF_symbol* symtab;
3189 /* ToDo: should be variable-sized? But is at least safe in the
3190 sense of buffer-overrun-proof. */
3192 /* debugBelch("resolving for %s\n", oc->fileName); */
3194 hdr = (COFF_header*)(oc->image);
3195 sectab = (COFF_section*) (
3196 ((UChar*)(oc->image))
3197 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3199 symtab = (COFF_symbol*) (
3200 ((UChar*)(oc->image))
3201 + hdr->PointerToSymbolTable
3203 strtab = ((UChar*)(oc->image))
3204 + hdr->PointerToSymbolTable
3205 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3207 for (i = 0; i < hdr->NumberOfSections; i++) {
3208 COFF_section* sectab_i
3210 myindex ( sizeof_COFF_section, sectab, i );
3213 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
3216 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3218 /* Ignore sections called which contain stabs debugging
3220 if (0 == strcmp(".stab", (char*)secname)
3221 || 0 == strcmp(".stabstr", (char*)secname)
3222 || 0 == strcmp(".ctors", (char*)secname)
3223 || 0 == strncmp(".debug", (char*)secname, 6)
3224 || 0 == strcmp(".rdata$zzz", (char*)secname)) {
3231 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
3232 /* If the relocation field (a short) has overflowed, the
3233 * real count can be found in the first reloc entry.
3235 * See Section 4.1 (last para) of the PE spec (rev6.0).
3237 * Nov2003 update: the GNU linker still doesn't correctly
3238 * handle the generation of relocatable object files with
3239 * overflown relocations. Hence the output to warn of potential
3242 COFF_reloc* rel = (COFF_reloc*)
3243 myindex ( sizeof_COFF_reloc, reltab, 0 );
3244 noRelocs = rel->VirtualAddress;
3246 /* 10/05: we now assume (and check for) a GNU ld that is capable
3247 * of handling object files with (>2^16) of relocs.
3250 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
3255 noRelocs = sectab_i->NumberOfRelocations;
3260 for (; j < noRelocs; j++) {
3262 COFF_reloc* reltab_j
3264 myindex ( sizeof_COFF_reloc, reltab, j );
3266 /* the location to patch */
3268 ((UChar*)(oc->image))
3269 + (sectab_i->PointerToRawData
3270 + reltab_j->VirtualAddress
3271 - sectab_i->VirtualAddress )
3273 /* the existing contents of pP */
3275 /* the symbol to connect to */
3276 sym = (COFF_symbol*)
3277 myindex ( sizeof_COFF_symbol,
3278 symtab, reltab_j->SymbolTableIndex );
3281 "reloc sec %2d num %3d: type 0x%-4x "
3282 "vaddr 0x%-8x name `",
3284 (UInt32)reltab_j->Type,
3285 reltab_j->VirtualAddress );
3286 printName ( sym->Name, strtab );
3287 debugBelch("'\n" ));
3289 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
3290 COFF_section* section_sym
3291 = findPEi386SectionCalled ( oc, sym->Name );
3293 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
3296 S = ((UInt32)(oc->image))
3297 + (section_sym->PointerToRawData
3300 copyName ( sym->Name, strtab, symbol, 1000-1 );
3301 S = (UInt32) lookupSymbol( (char*)symbol );
3302 if ((void*)S != NULL) goto foundit;
3303 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3307 checkProddableBlock(oc, pP);
3308 switch (reltab_j->Type) {
3309 case MYIMAGE_REL_I386_DIR32:
3312 case MYIMAGE_REL_I386_REL32:
3313 /* Tricky. We have to insert a displacement at
3314 pP which, when added to the PC for the _next_
3315 insn, gives the address of the target (S).
3316 Problem is to know the address of the next insn
3317 when we only know pP. We assume that this
3318 literal field is always the last in the insn,
3319 so that the address of the next insn is pP+4
3320 -- hence the constant 4.
3321 Also I don't know if A should be added, but so
3322 far it has always been zero.
3324 SOF 05/2005: 'A' (old contents of *pP) have been observed
3325 to contain values other than zero (the 'wx' object file
3326 that came with wxhaskell-0.9.4; dunno how it was compiled..).
3327 So, add displacement to old value instead of asserting
3328 A to be zero. Fixes wxhaskell-related crashes, and no other
3329 ill effects have been observed.
3331 Update: the reason why we're seeing these more elaborate
3332 relocations is due to a switch in how the NCG compiles SRTs
3333 and offsets to them from info tables. SRTs live in .(ro)data,
3334 while info tables live in .text, causing GAS to emit REL32/DISP32
3335 relocations with non-zero values. Adding the displacement is
3336 the right thing to do.
3338 *pP = S - ((UInt32)pP) - 4 + A;
3341 debugBelch("%s: unhandled PEi386 relocation type %d",
3342 oc->fileName, reltab_j->Type);
3349 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
3353 #endif /* defined(OBJFORMAT_PEi386) */
3356 /* --------------------------------------------------------------------------
3358 * ------------------------------------------------------------------------*/
3360 #if defined(OBJFORMAT_ELF)
3365 #if defined(sparc_HOST_ARCH)
3366 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
3367 #elif defined(i386_HOST_ARCH)
3368 # define ELF_TARGET_386 /* Used inside <elf.h> */
3369 #elif defined(x86_64_HOST_ARCH)
3370 # define ELF_TARGET_X64_64
3374 #if !defined(openbsd_HOST_OS)
3377 /* openbsd elf has things in different places, with diff names */
3378 # include <elf_abi.h>
3379 # include <machine/reloc.h>
3380 # define R_386_32 RELOC_32
3381 # define R_386_PC32 RELOC_PC32
3384 /* If elf.h doesn't define it */
3385 # ifndef R_X86_64_PC64
3386 # define R_X86_64_PC64 24
3390 * Define a set of types which can be used for both ELF32 and ELF64
3394 #define ELFCLASS ELFCLASS64
3395 #define Elf_Addr Elf64_Addr
3396 #define Elf_Word Elf64_Word
3397 #define Elf_Sword Elf64_Sword
3398 #define Elf_Ehdr Elf64_Ehdr
3399 #define Elf_Phdr Elf64_Phdr
3400 #define Elf_Shdr Elf64_Shdr
3401 #define Elf_Sym Elf64_Sym
3402 #define Elf_Rel Elf64_Rel
3403 #define Elf_Rela Elf64_Rela
3405 #define ELF_ST_TYPE ELF64_ST_TYPE
3408 #define ELF_ST_BIND ELF64_ST_BIND
3411 #define ELF_R_TYPE ELF64_R_TYPE
3414 #define ELF_R_SYM ELF64_R_SYM
3417 #define ELFCLASS ELFCLASS32
3418 #define Elf_Addr Elf32_Addr
3419 #define Elf_Word Elf32_Word
3420 #define Elf_Sword Elf32_Sword
3421 #define Elf_Ehdr Elf32_Ehdr
3422 #define Elf_Phdr Elf32_Phdr
3423 #define Elf_Shdr Elf32_Shdr
3424 #define Elf_Sym Elf32_Sym
3425 #define Elf_Rel Elf32_Rel
3426 #define Elf_Rela Elf32_Rela
3428 #define ELF_ST_TYPE ELF32_ST_TYPE
3431 #define ELF_ST_BIND ELF32_ST_BIND
3434 #define ELF_R_TYPE ELF32_R_TYPE
3437 #define ELF_R_SYM ELF32_R_SYM
3443 * Functions to allocate entries in dynamic sections. Currently we simply
3444 * preallocate a large number, and we don't check if a entry for the given
3445 * target already exists (a linear search is too slow). Ideally these
3446 * entries would be associated with symbols.
3449 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
3450 #define GOT_SIZE 0x20000
3451 #define FUNCTION_TABLE_SIZE 0x10000
3452 #define PLT_SIZE 0x08000
3455 static Elf_Addr got[GOT_SIZE];
3456 static unsigned int gotIndex;
3457 static Elf_Addr gp_val = (Elf_Addr)got;
3460 allocateGOTEntry(Elf_Addr target)
3464 if (gotIndex >= GOT_SIZE)
3465 barf("Global offset table overflow");
3467 entry = &got[gotIndex++];
3469 return (Elf_Addr)entry;
3473 #ifdef ELF_FUNCTION_DESC
3479 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
3480 static unsigned int functionTableIndex;
3483 allocateFunctionDesc(Elf_Addr target)
3485 FunctionDesc *entry;
3487 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
3488 barf("Function table overflow");
3490 entry = &functionTable[functionTableIndex++];
3492 entry->gp = (Elf_Addr)gp_val;
3493 return (Elf_Addr)entry;
3497 copyFunctionDesc(Elf_Addr target)
3499 FunctionDesc *olddesc = (FunctionDesc *)target;
3500 FunctionDesc *newdesc;
3502 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
3503 newdesc->gp = olddesc->gp;
3504 return (Elf_Addr)newdesc;
3511 unsigned char code[sizeof(plt_code)];
3515 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
3517 PLTEntry *plt = (PLTEntry *)oc->plt;
3520 if (oc->pltIndex >= PLT_SIZE)
3521 barf("Procedure table overflow");
3523 entry = &plt[oc->pltIndex++];
3524 memcpy(entry->code, plt_code, sizeof(entry->code));
3525 PLT_RELOC(entry->code, target);
3526 return (Elf_Addr)entry;
3532 return (PLT_SIZE * sizeof(PLTEntry));
3538 * Generic ELF functions
3542 findElfSection ( void* objImage, Elf_Word sh_type )
3544 char* ehdrC = (char*)objImage;
3545 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3546 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3547 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3551 for (i = 0; i < ehdr->e_shnum; i++) {
3552 if (shdr[i].sh_type == sh_type
3553 /* Ignore the section header's string table. */
3554 && i != ehdr->e_shstrndx
3555 /* Ignore string tables named .stabstr, as they contain
3557 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3559 ptr = ehdrC + shdr[i].sh_offset;
3567 ocVerifyImage_ELF ( ObjectCode* oc )
3571 int i, j, nent, nstrtab, nsymtabs;
3575 char* ehdrC = (char*)(oc->image);
3576 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3578 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
3579 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
3580 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
3581 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
3582 errorBelch("%s: not an ELF object", oc->fileName);
3586 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
3587 errorBelch("%s: unsupported ELF format", oc->fileName);
3591 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
3592 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
3594 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
3595 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
3597 errorBelch("%s: unknown endiannness", oc->fileName);
3601 if (ehdr->e_type != ET_REL) {
3602 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
3605 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
3607 IF_DEBUG(linker,debugBelch( "Architecture is " ));
3608 switch (ehdr->e_machine) {
3609 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
3610 #ifdef EM_SPARC32PLUS
3611 case EM_SPARC32PLUS:
3613 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
3615 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
3617 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
3619 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
3620 #elif defined(EM_AMD64)
3621 case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
3623 default: IF_DEBUG(linker,debugBelch( "unknown" ));
3624 errorBelch("%s: unknown architecture (e_machine == %d)"
3625 , oc->fileName, ehdr->e_machine);
3629 IF_DEBUG(linker,debugBelch(
3630 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
3631 (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
3633 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
3635 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3637 if (ehdr->e_shstrndx == SHN_UNDEF) {
3638 errorBelch("%s: no section header string table", oc->fileName);
3641 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
3643 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3646 for (i = 0; i < ehdr->e_shnum; i++) {
3647 IF_DEBUG(linker,debugBelch("%2d: ", i ));
3648 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
3649 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
3650 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
3651 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
3652 ehdrC + shdr[i].sh_offset,
3653 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3655 if (shdr[i].sh_type == SHT_REL) {
3656 IF_DEBUG(linker,debugBelch("Rel " ));
3657 } else if (shdr[i].sh_type == SHT_RELA) {
3658 IF_DEBUG(linker,debugBelch("RelA " ));
3660 IF_DEBUG(linker,debugBelch(" "));
3663 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3667 IF_DEBUG(linker,debugBelch( "\nString tables" ));
3670 for (i = 0; i < ehdr->e_shnum; i++) {
3671 if (shdr[i].sh_type == SHT_STRTAB
3672 /* Ignore the section header's string table. */
3673 && i != ehdr->e_shstrndx
3674 /* Ignore string tables named .stabstr, as they contain
3676 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3678 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
3679 strtab = ehdrC + shdr[i].sh_offset;
3684 errorBelch("%s: no string tables, or too many", oc->fileName);
3689 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
3690 for (i = 0; i < ehdr->e_shnum; i++) {
3691 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3692 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3694 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3695 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3696 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
3698 (long)shdr[i].sh_size % sizeof(Elf_Sym)
3700 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
3701 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
3704 for (j = 0; j < nent; j++) {
3705 IF_DEBUG(linker,debugBelch(" %2d ", j ));
3706 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
3707 (int)stab[j].st_shndx,
3708 (int)stab[j].st_size,
3709 (char*)stab[j].st_value ));
3711 IF_DEBUG(linker,debugBelch("type=" ));
3712 switch (ELF_ST_TYPE(stab[j].st_info)) {
3713 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
3714 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
3715 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
3716 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
3717 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
3718 default: IF_DEBUG(linker,debugBelch("? " )); break;
3720 IF_DEBUG(linker,debugBelch(" " ));
3722 IF_DEBUG(linker,debugBelch("bind=" ));
3723 switch (ELF_ST_BIND(stab[j].st_info)) {
3724 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
3725 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
3726 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
3727 default: IF_DEBUG(linker,debugBelch("? " )); break;
3729 IF_DEBUG(linker,debugBelch(" " ));
3731 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
3735 if (nsymtabs == 0) {
3736 errorBelch("%s: didn't find any symbol tables", oc->fileName);
3743 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
3747 if (hdr->sh_type == SHT_PROGBITS
3748 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
3749 /* .text-style section */
3750 return SECTIONKIND_CODE_OR_RODATA;
3753 if (hdr->sh_type == SHT_PROGBITS
3754 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3755 /* .data-style section */
3756 return SECTIONKIND_RWDATA;
3759 if (hdr->sh_type == SHT_PROGBITS
3760 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
3761 /* .rodata-style section */
3762 return SECTIONKIND_CODE_OR_RODATA;
3765 if (hdr->sh_type == SHT_NOBITS
3766 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3767 /* .bss-style section */
3769 return SECTIONKIND_RWDATA;
3772 return SECTIONKIND_OTHER;
3777 ocGetNames_ELF ( ObjectCode* oc )
3782 char* ehdrC = (char*)(oc->image);
3783 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3784 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
3785 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3787 ASSERT(symhash != NULL);
3790 errorBelch("%s: no strtab", oc->fileName);
3795 for (i = 0; i < ehdr->e_shnum; i++) {
3796 /* Figure out what kind of section it is. Logic derived from
3797 Figure 1.14 ("Special Sections") of the ELF document
3798 ("Portable Formats Specification, Version 1.1"). */
3800 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
3802 if (is_bss && shdr[i].sh_size > 0) {
3803 /* This is a non-empty .bss section. Allocate zeroed space for
3804 it, and set its .sh_offset field such that
3805 ehdrC + .sh_offset == addr_of_zeroed_space. */
3806 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
3807 "ocGetNames_ELF(BSS)");
3808 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
3810 debugBelch("BSS section at 0x%x, size %d\n",
3811 zspace, shdr[i].sh_size);
3815 /* fill in the section info */
3816 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
3817 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
3818 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
3819 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
3822 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3824 /* copy stuff into this module's object symbol table */
3825 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3826 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3828 oc->n_symbols = nent;
3829 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3830 "ocGetNames_ELF(oc->symbols)");
3832 for (j = 0; j < nent; j++) {
3834 char isLocal = FALSE; /* avoids uninit-var warning */
3836 char* nm = strtab + stab[j].st_name;
3837 int secno = stab[j].st_shndx;
3839 /* Figure out if we want to add it; if so, set ad to its
3840 address. Otherwise leave ad == NULL. */
3842 if (secno == SHN_COMMON) {
3844 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
3846 debugBelch("COMMON symbol, size %d name %s\n",
3847 stab[j].st_size, nm);
3849 /* Pointless to do addProddableBlock() for this area,
3850 since the linker should never poke around in it. */
3853 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
3854 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
3856 /* and not an undefined symbol */
3857 && stab[j].st_shndx != SHN_UNDEF
3858 /* and not in a "special section" */
3859 && stab[j].st_shndx < SHN_LORESERVE
3861 /* and it's a not a section or string table or anything silly */
3862 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3863 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3864 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3867 /* Section 0 is the undefined section, hence > and not >=. */
3868 ASSERT(secno > 0 && secno < ehdr->e_shnum);
3870 if (shdr[secno].sh_type == SHT_NOBITS) {
3871 debugBelch(" BSS symbol, size %d off %d name %s\n",
3872 stab[j].st_size, stab[j].st_value, nm);
3875 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3876 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3879 #ifdef ELF_FUNCTION_DESC
3880 /* dlsym() and the initialisation table both give us function
3881 * descriptors, so to be consistent we store function descriptors
3882 * in the symbol table */
3883 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3884 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3886 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n",
3887 ad, oc->fileName, nm ));
3892 /* And the decision is ... */
3896 oc->symbols[j] = nm;
3899 /* Ignore entirely. */
3901 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3905 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3906 strtab + stab[j].st_name ));
3909 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3910 (int)ELF_ST_BIND(stab[j].st_info),
3911 (int)ELF_ST_TYPE(stab[j].st_info),
3912 (int)stab[j].st_shndx,
3913 strtab + stab[j].st_name
3916 oc->symbols[j] = NULL;
3925 /* Do ELF relocations which lack an explicit addend. All x86-linux
3926 relocations appear to be of this form. */
3928 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3929 Elf_Shdr* shdr, int shnum,
3930 Elf_Sym* stab, char* strtab )
3935 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3936 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3937 int target_shndx = shdr[shnum].sh_info;
3938 int symtab_shndx = shdr[shnum].sh_link;
3940 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3941 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3942 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3943 target_shndx, symtab_shndx ));
3945 /* Skip sections that we're not interested in. */
3948 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3949 if (kind == SECTIONKIND_OTHER) {
3950 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
3955 for (j = 0; j < nent; j++) {
3956 Elf_Addr offset = rtab[j].r_offset;
3957 Elf_Addr info = rtab[j].r_info;
3959 Elf_Addr P = ((Elf_Addr)targ) + offset;
3960 Elf_Word* pP = (Elf_Word*)P;
3965 StgStablePtr stablePtr;
3968 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3969 j, (void*)offset, (void*)info ));
3971 IF_DEBUG(linker,debugBelch( " ZERO" ));
3974 Elf_Sym sym = stab[ELF_R_SYM(info)];
3975 /* First see if it is a local symbol. */
3976 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3977 /* Yes, so we can get the address directly from the ELF symbol
3979 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3981 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3982 + stab[ELF_R_SYM(info)].st_value);
3985 symbol = strtab + sym.st_name;
3986 stablePtr = (StgStablePtr)lookupHashTable(stablehash, (StgWord)symbol);
3987 if (NULL == stablePtr) {
3988 /* No, so look up the name in our global table. */
3989 S_tmp = lookupSymbol( symbol );
3990 S = (Elf_Addr)S_tmp;
3992 stableVal = deRefStablePtr( stablePtr );
3994 S = (Elf_Addr)S_tmp;
3998 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4001 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
4004 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
4005 (void*)P, (void*)S, (void*)A ));
4006 checkProddableBlock ( oc, pP );
4010 switch (ELF_R_TYPE(info)) {
4011 # ifdef i386_HOST_ARCH
4012 case R_386_32: *pP = value; break;
4013 case R_386_PC32: *pP = value - P; break;
4016 errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
4017 oc->fileName, (lnat)ELF_R_TYPE(info));
4025 /* Do ELF relocations for which explicit addends are supplied.
4026 sparc-solaris relocations appear to be of this form. */
4028 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
4029 Elf_Shdr* shdr, int shnum,
4030 Elf_Sym* stab, char* strtab )
4033 char *symbol = NULL;
4035 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
4036 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
4037 int target_shndx = shdr[shnum].sh_info;
4038 int symtab_shndx = shdr[shnum].sh_link;
4040 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
4041 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
4042 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
4043 target_shndx, symtab_shndx ));
4045 for (j = 0; j < nent; j++) {
4046 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4047 /* This #ifdef only serves to avoid unused-var warnings. */
4048 Elf_Addr offset = rtab[j].r_offset;
4049 Elf_Addr P = targ + offset;
4051 Elf_Addr info = rtab[j].r_info;
4052 Elf_Addr A = rtab[j].r_addend;
4056 # if defined(sparc_HOST_ARCH)
4057 Elf_Word* pP = (Elf_Word*)P;
4059 # elif defined(powerpc_HOST_ARCH)
4063 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
4064 j, (void*)offset, (void*)info,
4067 IF_DEBUG(linker,debugBelch( " ZERO" ));
4070 Elf_Sym sym = stab[ELF_R_SYM(info)];
4071 /* First see if it is a local symbol. */
4072 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
4073 /* Yes, so we can get the address directly from the ELF symbol
4075 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
4077 (ehdrC + shdr[ sym.st_shndx ].sh_offset
4078 + stab[ELF_R_SYM(info)].st_value);
4079 #ifdef ELF_FUNCTION_DESC
4080 /* Make a function descriptor for this function */
4081 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
4082 S = allocateFunctionDesc(S + A);
4087 /* No, so look up the name in our global table. */
4088 symbol = strtab + sym.st_name;
4089 S_tmp = lookupSymbol( symbol );
4090 S = (Elf_Addr)S_tmp;
4092 #ifdef ELF_FUNCTION_DESC
4093 /* If a function, already a function descriptor - we would
4094 have to copy it to add an offset. */
4095 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
4096 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
4100 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4103 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
4106 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
4107 (void*)P, (void*)S, (void*)A ));
4108 /* checkProddableBlock ( oc, (void*)P ); */
4112 switch (ELF_R_TYPE(info)) {
4113 # if defined(sparc_HOST_ARCH)
4114 case R_SPARC_WDISP30:
4115 w1 = *pP & 0xC0000000;
4116 w2 = (Elf_Word)((value - P) >> 2);
4117 ASSERT((w2 & 0xC0000000) == 0);
4122 w1 = *pP & 0xFFC00000;
4123 w2 = (Elf_Word)(value >> 10);
4124 ASSERT((w2 & 0xFFC00000) == 0);
4130 w2 = (Elf_Word)(value & 0x3FF);
4131 ASSERT((w2 & ~0x3FF) == 0);
4136 /* According to the Sun documentation:
4138 This relocation type resembles R_SPARC_32, except it refers to an
4139 unaligned word. That is, the word to be relocated must be treated
4140 as four separate bytes with arbitrary alignment, not as a word
4141 aligned according to the architecture requirements.
4144 w2 = (Elf_Word)value;
4146 // SPARC doesn't do misaligned writes of 32 bit words,
4147 // so we have to do this one byte-at-a-time.
4148 char *pPc = (char*)pP;
4149 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
4150 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
4151 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
4152 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
4156 w2 = (Elf_Word)value;
4159 # elif defined(powerpc_HOST_ARCH)
4160 case R_PPC_ADDR16_LO:
4161 *(Elf32_Half*) P = value;
4164 case R_PPC_ADDR16_HI:
4165 *(Elf32_Half*) P = value >> 16;
4168 case R_PPC_ADDR16_HA:
4169 *(Elf32_Half*) P = (value + 0x8000) >> 16;
4173 *(Elf32_Word *) P = value;
4177 *(Elf32_Word *) P = value - P;
4183 if( delta << 6 >> 6 != delta )
4185 value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
4189 if( value == 0 || delta << 6 >> 6 != delta )
4191 barf( "Unable to make SymbolExtra for #%d",
4197 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
4198 | (delta & 0x3fffffc);
4202 #if x86_64_HOST_ARCH
4204 *(Elf64_Xword *)P = value;
4209 #if defined(ALWAYS_PIC)
4210 barf("R_X86_64_PC32 relocation, but ALWAYS_PIC.");
4212 StgInt64 off = value - P;
4213 if (off >= 0x7fffffffL || off < -0x80000000L) {
4214 #if X86_64_ELF_NONPIC_HACK
4215 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4217 off = pltAddress + A - P;
4219 barf("R_X86_64_PC32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4220 symbol, off, oc->fileName );
4223 *(Elf64_Word *)P = (Elf64_Word)off;
4230 StgInt64 off = value - P;
4231 *(Elf64_Word *)P = (Elf64_Word)off;
4236 #if defined(ALWAYS_PIC)
4237 barf("R_X86_64_32 relocation, but ALWAYS_PIC.");
4239 if (value >= 0x7fffffffL) {
4240 #if X86_64_ELF_NONPIC_HACK
4241 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4243 value = pltAddress + A;
4245 barf("R_X86_64_32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4246 symbol, value, oc->fileName );
4249 *(Elf64_Word *)P = (Elf64_Word)value;
4254 #if defined(ALWAYS_PIC)
4255 barf("R_X86_64_32S relocation, but ALWAYS_PIC.");
4257 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
4258 #if X86_64_ELF_NONPIC_HACK
4259 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4261 value = pltAddress + A;
4263 barf("R_X86_64_32S relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4264 symbol, value, oc->fileName );
4267 *(Elf64_Sword *)P = (Elf64_Sword)value;
4271 case R_X86_64_GOTPCREL:
4273 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
4274 StgInt64 off = gotAddress + A - P;
4275 *(Elf64_Word *)P = (Elf64_Word)off;
4279 case R_X86_64_PLT32:
4281 #if defined(ALWAYS_PIC)
4282 barf("R_X86_64_PLT32 relocation, but ALWAYS_PIC.");
4284 StgInt64 off = value - P;
4285 if (off >= 0x7fffffffL || off < -0x80000000L) {
4286 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4288 off = pltAddress + A - P;
4290 *(Elf64_Word *)P = (Elf64_Word)off;
4297 errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
4298 oc->fileName, (lnat)ELF_R_TYPE(info));
4307 ocResolve_ELF ( ObjectCode* oc )
4311 Elf_Sym* stab = NULL;
4312 char* ehdrC = (char*)(oc->image);
4313 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
4314 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
4316 /* first find "the" symbol table */
4317 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
4319 /* also go find the string table */
4320 strtab = findElfSection ( ehdrC, SHT_STRTAB );
4322 if (stab == NULL || strtab == NULL) {
4323 errorBelch("%s: can't find string or symbol table", oc->fileName);
4327 /* Process the relocation sections. */
4328 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
4329 if (shdr[shnum].sh_type == SHT_REL) {
4330 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
4331 shnum, stab, strtab );
4335 if (shdr[shnum].sh_type == SHT_RELA) {
4336 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
4337 shnum, stab, strtab );
4342 #if defined(powerpc_HOST_ARCH)
4343 ocFlushInstructionCache( oc );
4350 * PowerPC & X86_64 ELF specifics
4353 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4355 static int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
4361 ehdr = (Elf_Ehdr *) oc->image;
4362 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
4364 for( i = 0; i < ehdr->e_shnum; i++ )
4365 if( shdr[i].sh_type == SHT_SYMTAB )
4368 if( i == ehdr->e_shnum )
4370 errorBelch( "This ELF file contains no symtab" );
4374 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
4376 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
4377 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
4382 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
4385 #endif /* powerpc */
4389 /* --------------------------------------------------------------------------
4391 * ------------------------------------------------------------------------*/
4393 #if defined(OBJFORMAT_MACHO)
4396 Support for MachO linking on Darwin/MacOS X
4397 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
4399 I hereby formally apologize for the hackish nature of this code.
4400 Things that need to be done:
4401 *) implement ocVerifyImage_MachO
4402 *) add still more sanity checks.
4405 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4406 #define mach_header mach_header_64
4407 #define segment_command segment_command_64
4408 #define section section_64
4409 #define nlist nlist_64
4412 #ifdef powerpc_HOST_ARCH
4413 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4415 struct mach_header *header = (struct mach_header *) oc->image;
4416 struct load_command *lc = (struct load_command *) (header + 1);
4419 for( i = 0; i < header->ncmds; i++ )
4421 if( lc->cmd == LC_SYMTAB )
4423 // Find out the first and last undefined external
4424 // symbol, so we don't have to allocate too many
4426 struct symtab_command *symLC = (struct symtab_command *) lc;
4427 unsigned min = symLC->nsyms, max = 0;
4428 struct nlist *nlist =
4429 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
4431 for(i=0;i<symLC->nsyms;i++)
4433 if(nlist[i].n_type & N_STAB)
4435 else if(nlist[i].n_type & N_EXT)
4437 if((nlist[i].n_type & N_TYPE) == N_UNDF
4438 && (nlist[i].n_value == 0))
4448 return ocAllocateSymbolExtras(oc, max - min + 1, min);
4453 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4455 return ocAllocateSymbolExtras(oc,0,0);
4458 #ifdef x86_64_HOST_ARCH
4459 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4461 struct mach_header *header = (struct mach_header *) oc->image;
4462 struct load_command *lc = (struct load_command *) (header + 1);
4465 for( i = 0; i < header->ncmds; i++ )
4467 if( lc->cmd == LC_SYMTAB )
4469 // Just allocate one entry for every symbol
4470 struct symtab_command *symLC = (struct symtab_command *) lc;
4472 return ocAllocateSymbolExtras(oc, symLC->nsyms, 0);
4475 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4477 return ocAllocateSymbolExtras(oc,0,0);
4481 static int ocVerifyImage_MachO(ObjectCode* oc)
4483 char *image = (char*) oc->image;
4484 struct mach_header *header = (struct mach_header*) image;
4486 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4487 if(header->magic != MH_MAGIC_64) {
4488 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4489 oc->fileName, MH_MAGIC_64, header->magic);
4493 if(header->magic != MH_MAGIC) {
4494 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4495 oc->fileName, MH_MAGIC, header->magic);
4499 // FIXME: do some more verifying here
4503 static int resolveImports(
4506 struct symtab_command *symLC,
4507 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
4508 unsigned long *indirectSyms,
4509 struct nlist *nlist)
4512 size_t itemSize = 4;
4514 IF_DEBUG(linker, debugBelch("resolveImports: start\n"));
4517 int isJumpTable = 0;
4518 if(!strcmp(sect->sectname,"__jump_table"))
4522 ASSERT(sect->reserved2 == itemSize);
4526 for(i=0; i*itemSize < sect->size;i++)
4528 // according to otool, reserved1 contains the first index into the indirect symbol table
4529 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
4530 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4533 IF_DEBUG(linker, debugBelch("resolveImports: resolving %s\n", nm));
4534 if ((symbol->n_type & N_TYPE) == N_UNDF
4535 && (symbol->n_type & N_EXT) && (symbol->n_value != 0)) {
4536 addr = (void*) (symbol->n_value);
4537 IF_DEBUG(linker, debugBelch("resolveImports: undefined external %s has value %p\n", nm, addr));
4539 addr = lookupSymbol(nm);
4540 IF_DEBUG(linker, debugBelch("resolveImports: looking up %s, %p\n", nm, addr));
4544 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
4552 checkProddableBlock(oc,image + sect->offset + i*itemSize);
4553 *(image + sect->offset + i*itemSize) = 0xe9; // jmp
4554 *(unsigned*)(image + sect->offset + i*itemSize + 1)
4555 = (char*)addr - (image + sect->offset + i*itemSize + 5);
4560 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
4561 ((void**)(image + sect->offset))[i] = addr;
4565 IF_DEBUG(linker, debugBelch("resolveImports: done\n"));
4569 static unsigned long relocateAddress(
4572 struct section* sections,
4573 unsigned long address)
4576 IF_DEBUG(linker, debugBelch("relocateAddress: start\n"));
4577 for (i = 0; i < nSections; i++)
4579 IF_DEBUG(linker, debugBelch(" relocating address in section %d\n", i));
4580 if (sections[i].addr <= address
4581 && address < sections[i].addr + sections[i].size)
4583 return (unsigned long)oc->image
4584 + sections[i].offset + address - sections[i].addr;
4587 barf("Invalid Mach-O file:"
4588 "Address out of bounds while relocating object file");
4592 static int relocateSection(
4595 struct symtab_command *symLC, struct nlist *nlist,
4596 int nSections, struct section* sections, struct section *sect)
4598 struct relocation_info *relocs;
4601 IF_DEBUG(linker, debugBelch("relocateSection: start\n"));
4603 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
4605 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
4607 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
4609 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
4613 IF_DEBUG(linker, debugBelch("relocateSection: number of relocations: %d\n", n));
4615 relocs = (struct relocation_info*) (image + sect->reloff);
4619 #ifdef x86_64_HOST_ARCH
4620 struct relocation_info *reloc = &relocs[i];
4622 char *thingPtr = image + sect->offset + reloc->r_address;
4624 /* We shouldn't need to initialise this, but gcc on OS X 64 bit
4625 complains that it may be used uninitialized if we don't */
4628 int type = reloc->r_type;
4630 checkProddableBlock(oc,thingPtr);
4631 switch(reloc->r_length)
4634 thing = *(uint8_t*)thingPtr;
4635 baseValue = (uint64_t)thingPtr + 1;
4638 thing = *(uint16_t*)thingPtr;
4639 baseValue = (uint64_t)thingPtr + 2;
4642 thing = *(uint32_t*)thingPtr;
4643 baseValue = (uint64_t)thingPtr + 4;
4646 thing = *(uint64_t*)thingPtr;
4647 baseValue = (uint64_t)thingPtr + 8;
4650 barf("Unknown size.");
4654 debugBelch("relocateSection: length = %d, thing = %d, baseValue = %p\n",
4655 reloc->r_length, thing, baseValue));
4657 if (type == X86_64_RELOC_GOT
4658 || type == X86_64_RELOC_GOT_LOAD)
4660 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4661 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4663 IF_DEBUG(linker, debugBelch("relocateSection: making jump island for %s, extern = %d, X86_64_RELOC_GOT\n", nm, reloc->r_extern));
4664 ASSERT(reloc->r_extern);
4665 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, (unsigned long)lookupSymbol(nm))->addr;
4667 type = X86_64_RELOC_SIGNED;
4669 else if(reloc->r_extern)
4671 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4672 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4674 IF_DEBUG(linker, debugBelch("relocateSection: looking up external symbol %s\n", nm));
4675 IF_DEBUG(linker, debugBelch(" : type = %d\n", symbol->n_type));
4676 IF_DEBUG(linker, debugBelch(" : sect = %d\n", symbol->n_sect));
4677 IF_DEBUG(linker, debugBelch(" : desc = %d\n", symbol->n_desc));
4678 IF_DEBUG(linker, debugBelch(" : value = %d\n", symbol->n_value));
4679 if ((symbol->n_type & N_TYPE) == N_SECT) {
4680 value = relocateAddress(oc, nSections, sections,
4682 IF_DEBUG(linker, debugBelch("relocateSection, defined external symbol %s, relocated address %p\n", nm, value));
4685 value = (uint64_t) lookupSymbol(nm);
4686 IF_DEBUG(linker, debugBelch("relocateSection: external symbol %s, address %p\n", nm, value));
4691 value = sections[reloc->r_symbolnum-1].offset
4692 - sections[reloc->r_symbolnum-1].addr
4696 IF_DEBUG(linker, debugBelch("relocateSection: value = %p\n", value));
4698 if (type == X86_64_RELOC_BRANCH)
4700 if((int32_t)(value - baseValue) != (int64_t)(value - baseValue))
4702 ASSERT(reloc->r_extern);
4703 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, value)
4706 ASSERT((int32_t)(value - baseValue) == (int64_t)(value - baseValue));
4707 type = X86_64_RELOC_SIGNED;
4712 case X86_64_RELOC_UNSIGNED:
4713 ASSERT(!reloc->r_pcrel);
4716 case X86_64_RELOC_SIGNED:
4717 case X86_64_RELOC_SIGNED_1:
4718 case X86_64_RELOC_SIGNED_2:
4719 case X86_64_RELOC_SIGNED_4:
4720 ASSERT(reloc->r_pcrel);
4721 thing += value - baseValue;
4723 case X86_64_RELOC_SUBTRACTOR:
4724 ASSERT(!reloc->r_pcrel);
4728 barf("unkown relocation");
4731 switch(reloc->r_length)
4734 *(uint8_t*)thingPtr = thing;
4737 *(uint16_t*)thingPtr = thing;
4740 *(uint32_t*)thingPtr = thing;
4743 *(uint64_t*)thingPtr = thing;
4747 if(relocs[i].r_address & R_SCATTERED)
4749 struct scattered_relocation_info *scat =
4750 (struct scattered_relocation_info*) &relocs[i];
4754 if(scat->r_length == 2)
4756 unsigned long word = 0;
4757 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
4758 checkProddableBlock(oc,wordPtr);
4760 // Note on relocation types:
4761 // i386 uses the GENERIC_RELOC_* types,
4762 // while ppc uses special PPC_RELOC_* types.
4763 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
4764 // in both cases, all others are different.
4765 // Therefore, we use GENERIC_RELOC_VANILLA
4766 // and GENERIC_RELOC_PAIR instead of the PPC variants,
4767 // and use #ifdefs for the other types.
4769 // Step 1: Figure out what the relocated value should be
4770 if(scat->r_type == GENERIC_RELOC_VANILLA)
4772 word = *wordPtr + (unsigned long) relocateAddress(
4779 #ifdef powerpc_HOST_ARCH
4780 else if(scat->r_type == PPC_RELOC_SECTDIFF
4781 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
4782 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
4783 || scat->r_type == PPC_RELOC_HA16_SECTDIFF
4784 || scat->r_type == PPC_RELOC_LOCAL_SECTDIFF)
4786 else if(scat->r_type == GENERIC_RELOC_SECTDIFF
4787 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4790 struct scattered_relocation_info *pair =
4791 (struct scattered_relocation_info*) &relocs[i+1];
4793 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
4794 barf("Invalid Mach-O file: "
4795 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
4797 word = (unsigned long)
4798 (relocateAddress(oc, nSections, sections, scat->r_value)
4799 - relocateAddress(oc, nSections, sections, pair->r_value));
4802 #ifdef powerpc_HOST_ARCH
4803 else if(scat->r_type == PPC_RELOC_HI16
4804 || scat->r_type == PPC_RELOC_LO16
4805 || scat->r_type == PPC_RELOC_HA16
4806 || scat->r_type == PPC_RELOC_LO14)
4807 { // these are generated by label+offset things
4808 struct relocation_info *pair = &relocs[i+1];
4809 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
4810 barf("Invalid Mach-O file: "
4811 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
4813 if(scat->r_type == PPC_RELOC_LO16)
4815 word = ((unsigned short*) wordPtr)[1];
4816 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4818 else if(scat->r_type == PPC_RELOC_LO14)
4820 barf("Unsupported Relocation: PPC_RELOC_LO14");
4821 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
4822 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4824 else if(scat->r_type == PPC_RELOC_HI16)
4826 word = ((unsigned short*) wordPtr)[1] << 16;
4827 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4829 else if(scat->r_type == PPC_RELOC_HA16)
4831 word = ((unsigned short*) wordPtr)[1] << 16;
4832 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4836 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
4844 barf ("Don't know how to handle this Mach-O "
4845 "scattered relocation entry: "
4846 "object file %s; entry type %ld; "
4848 OC_INFORMATIVE_FILENAME(oc),
4854 #ifdef powerpc_HOST_ARCH
4855 if(scat->r_type == GENERIC_RELOC_VANILLA
4856 || scat->r_type == PPC_RELOC_SECTDIFF)
4858 if(scat->r_type == GENERIC_RELOC_VANILLA
4859 || scat->r_type == GENERIC_RELOC_SECTDIFF
4860 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4865 #ifdef powerpc_HOST_ARCH
4866 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
4868 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4870 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
4872 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4874 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
4876 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4877 + ((word & (1<<15)) ? 1 : 0);
4883 barf("Can't handle Mach-O scattered relocation entry "
4884 "with this r_length tag: "
4885 "object file %s; entry type %ld; "
4886 "r_length tag %ld; address %#lx\n",
4887 OC_INFORMATIVE_FILENAME(oc),
4894 else /* scat->r_pcrel */
4896 barf("Don't know how to handle *PC-relative* Mach-O "
4897 "scattered relocation entry: "
4898 "object file %s; entry type %ld; address %#lx\n",
4899 OC_INFORMATIVE_FILENAME(oc),
4906 else /* !(relocs[i].r_address & R_SCATTERED) */
4908 struct relocation_info *reloc = &relocs[i];
4909 if(reloc->r_pcrel && !reloc->r_extern)
4912 if(reloc->r_length == 2)
4914 unsigned long word = 0;
4915 #ifdef powerpc_HOST_ARCH
4916 unsigned long jumpIsland = 0;
4917 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
4918 // to avoid warning and to catch
4922 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
4923 checkProddableBlock(oc,wordPtr);
4925 if(reloc->r_type == GENERIC_RELOC_VANILLA)
4929 #ifdef powerpc_HOST_ARCH
4930 else if(reloc->r_type == PPC_RELOC_LO16)
4932 word = ((unsigned short*) wordPtr)[1];
4933 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4935 else if(reloc->r_type == PPC_RELOC_HI16)
4937 word = ((unsigned short*) wordPtr)[1] << 16;
4938 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4940 else if(reloc->r_type == PPC_RELOC_HA16)
4942 word = ((unsigned short*) wordPtr)[1] << 16;
4943 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4945 else if(reloc->r_type == PPC_RELOC_BR24)
4948 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
4953 barf("Can't handle this Mach-O relocation entry "
4955 "object file %s; entry type %ld; address %#lx\n",
4956 OC_INFORMATIVE_FILENAME(oc),
4962 if(!reloc->r_extern)
4965 sections[reloc->r_symbolnum-1].offset
4966 - sections[reloc->r_symbolnum-1].addr
4973 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4974 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4975 void *symbolAddress = lookupSymbol(nm);
4978 errorBelch("\nunknown symbol `%s'", nm);
4984 #ifdef powerpc_HOST_ARCH
4985 // In the .o file, this should be a relative jump to NULL
4986 // and we'll change it to a relative jump to the symbol
4987 ASSERT(word + reloc->r_address == 0);
4988 jumpIsland = (unsigned long)
4989 &makeSymbolExtra(oc,
4991 (unsigned long) symbolAddress)
4995 offsetToJumpIsland = word + jumpIsland
4996 - (((long)image) + sect->offset - sect->addr);
4999 word += (unsigned long) symbolAddress
5000 - (((long)image) + sect->offset - sect->addr);
5004 word += (unsigned long) symbolAddress;
5008 if(reloc->r_type == GENERIC_RELOC_VANILLA)
5013 #ifdef powerpc_HOST_ARCH
5014 else if(reloc->r_type == PPC_RELOC_LO16)
5016 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
5019 else if(reloc->r_type == PPC_RELOC_HI16)
5021 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
5024 else if(reloc->r_type == PPC_RELOC_HA16)
5026 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
5027 + ((word & (1<<15)) ? 1 : 0);
5030 else if(reloc->r_type == PPC_RELOC_BR24)
5032 if((word & 0x03) != 0)
5033 barf("%s: unconditional relative branch with a displacement "
5034 "which isn't a multiple of 4 bytes: %#lx",
5035 OC_INFORMATIVE_FILENAME(oc),
5038 if((word & 0xFE000000) != 0xFE000000 &&
5039 (word & 0xFE000000) != 0x00000000)
5041 // The branch offset is too large.
5042 // Therefore, we try to use a jump island.
5045 barf("%s: unconditional relative branch out of range: "
5046 "no jump island available: %#lx",
5047 OC_INFORMATIVE_FILENAME(oc),
5051 word = offsetToJumpIsland;
5052 if((word & 0xFE000000) != 0xFE000000 &&
5053 (word & 0xFE000000) != 0x00000000)
5054 barf("%s: unconditional relative branch out of range: "
5055 "jump island out of range: %#lx",
5056 OC_INFORMATIVE_FILENAME(oc),
5059 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
5066 barf("Can't handle Mach-O relocation entry (not scattered) "
5067 "with this r_length tag: "
5068 "object file %s; entry type %ld; "
5069 "r_length tag %ld; address %#lx\n",
5070 OC_INFORMATIVE_FILENAME(oc),
5079 IF_DEBUG(linker, debugBelch("relocateSection: done\n"));
5083 static int ocGetNames_MachO(ObjectCode* oc)
5085 char *image = (char*) oc->image;
5086 struct mach_header *header = (struct mach_header*) image;
5087 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5088 unsigned i,curSymbol = 0;
5089 struct segment_command *segLC = NULL;
5090 struct section *sections;
5091 struct symtab_command *symLC = NULL;
5092 struct nlist *nlist;
5093 unsigned long commonSize = 0;
5094 char *commonStorage = NULL;
5095 unsigned long commonCounter;
5097 IF_DEBUG(linker,debugBelch("ocGetNames_MachO: start\n"));
5099 for(i=0;i<header->ncmds;i++)
5101 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
5102 segLC = (struct segment_command*) lc;
5103 else if(lc->cmd == LC_SYMTAB)
5104 symLC = (struct symtab_command*) lc;
5105 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5108 sections = (struct section*) (segLC+1);
5109 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5113 barf("ocGetNames_MachO: no segment load command");
5115 for(i=0;i<segLC->nsects;i++)
5117 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: segment %d\n", i));
5118 if (sections[i].size == 0)
5121 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
5123 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
5124 "ocGetNames_MachO(common symbols)");
5125 sections[i].offset = zeroFillArea - image;
5128 if(!strcmp(sections[i].sectname,"__text"))
5129 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
5130 (void*) (image + sections[i].offset),
5131 (void*) (image + sections[i].offset + sections[i].size));
5132 else if(!strcmp(sections[i].sectname,"__const"))
5133 addSection(oc, SECTIONKIND_RWDATA,
5134 (void*) (image + sections[i].offset),
5135 (void*) (image + sections[i].offset + sections[i].size));
5136 else if(!strcmp(sections[i].sectname,"__data"))
5137 addSection(oc, SECTIONKIND_RWDATA,
5138 (void*) (image + sections[i].offset),
5139 (void*) (image + sections[i].offset + sections[i].size));
5140 else if(!strcmp(sections[i].sectname,"__bss")
5141 || !strcmp(sections[i].sectname,"__common"))
5142 addSection(oc, SECTIONKIND_RWDATA,
5143 (void*) (image + sections[i].offset),
5144 (void*) (image + sections[i].offset + sections[i].size));
5146 addProddableBlock(oc, (void*) (image + sections[i].offset),
5150 // count external symbols defined here
5154 for(i=0;i<symLC->nsyms;i++)
5156 if(nlist[i].n_type & N_STAB)
5158 else if(nlist[i].n_type & N_EXT)
5160 if((nlist[i].n_type & N_TYPE) == N_UNDF
5161 && (nlist[i].n_value != 0))
5163 commonSize += nlist[i].n_value;
5166 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5171 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: %d external symbols\n", oc->n_symbols));
5172 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
5173 "ocGetNames_MachO(oc->symbols)");
5177 for(i=0;i<symLC->nsyms;i++)
5179 if(nlist[i].n_type & N_STAB)
5181 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5183 if(nlist[i].n_type & N_EXT)
5185 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5186 if ((nlist[i].n_desc & N_WEAK_DEF) && lookupSymbol(nm)) {
5187 // weak definition, and we already have a definition
5188 IF_DEBUG(linker, debugBelch(" weak: %s\n", nm));
5192 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting %s\n", nm));
5193 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5195 + sections[nlist[i].n_sect-1].offset
5196 - sections[nlist[i].n_sect-1].addr
5197 + nlist[i].n_value);
5198 oc->symbols[curSymbol++] = nm;
5205 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
5206 commonCounter = (unsigned long)commonStorage;
5209 for(i=0;i<symLC->nsyms;i++)
5211 if((nlist[i].n_type & N_TYPE) == N_UNDF
5212 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
5214 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5215 unsigned long sz = nlist[i].n_value;
5217 nlist[i].n_value = commonCounter;
5219 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting common symbol: %s\n", nm));
5220 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5221 (void*)commonCounter);
5222 oc->symbols[curSymbol++] = nm;
5224 commonCounter += sz;
5231 static int ocResolve_MachO(ObjectCode* oc)
5233 char *image = (char*) oc->image;
5234 struct mach_header *header = (struct mach_header*) image;
5235 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5237 struct segment_command *segLC = NULL;
5238 struct section *sections;
5239 struct symtab_command *symLC = NULL;
5240 struct dysymtab_command *dsymLC = NULL;
5241 struct nlist *nlist;
5243 IF_DEBUG(linker, debugBelch("ocResolve_MachO: start\n"));
5244 for (i = 0; i < header->ncmds; i++)
5246 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
5247 segLC = (struct segment_command*) lc;
5248 else if(lc->cmd == LC_SYMTAB)
5249 symLC = (struct symtab_command*) lc;
5250 else if(lc->cmd == LC_DYSYMTAB)
5251 dsymLC = (struct dysymtab_command*) lc;
5252 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5255 sections = (struct section*) (segLC+1);
5256 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5261 unsigned long *indirectSyms
5262 = (unsigned long*) (image + dsymLC->indirectsymoff);
5264 IF_DEBUG(linker, debugBelch("ocResolve_MachO: resolving dsymLC\n"));
5265 for (i = 0; i < segLC->nsects; i++)
5267 if( !strcmp(sections[i].sectname,"__la_symbol_ptr")
5268 || !strcmp(sections[i].sectname,"__la_sym_ptr2")
5269 || !strcmp(sections[i].sectname,"__la_sym_ptr3"))
5271 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5274 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr")
5275 || !strcmp(sections[i].sectname,"__pointers"))
5277 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5280 else if(!strcmp(sections[i].sectname,"__jump_table"))
5282 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5287 IF_DEBUG(linker, debugBelch("ocResolve_MachO: unknown section\n"));
5292 for(i=0;i<segLC->nsects;i++)
5294 IF_DEBUG(linker, debugBelch("ocResolve_MachO: relocating section %d\n", i));
5296 if (!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
5300 #if defined (powerpc_HOST_ARCH)
5301 ocFlushInstructionCache( oc );
5307 #ifdef powerpc_HOST_ARCH
5309 * The Mach-O object format uses leading underscores. But not everywhere.
5310 * There is a small number of runtime support functions defined in
5311 * libcc_dynamic.a whose name does not have a leading underscore.
5312 * As a consequence, we can't get their address from C code.
5313 * We have to use inline assembler just to take the address of a function.
5317 extern void* symbolsWithoutUnderscore[];
5319 static void machoInitSymbolsWithoutUnderscore()
5321 void **p = symbolsWithoutUnderscore;
5322 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
5324 #undef SymI_NeedsProto
5325 #define SymI_NeedsProto(x) \
5326 __asm__ volatile(".long " # x);
5328 RTS_MACHO_NOUNDERLINE_SYMBOLS
5330 __asm__ volatile(".text");
5332 #undef SymI_NeedsProto
5333 #define SymI_NeedsProto(x) \
5334 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
5336 RTS_MACHO_NOUNDERLINE_SYMBOLS
5338 #undef SymI_NeedsProto
5344 * Figure out by how much to shift the entire Mach-O file in memory
5345 * when loading so that its single segment ends up 16-byte-aligned
5347 static int machoGetMisalignment( FILE * f )
5349 struct mach_header header;
5353 int n = fread(&header, sizeof(header), 1, f);
5355 barf("machoGetMisalignment: can't read the Mach-O header");
5358 fseek(f, -sizeof(header), SEEK_CUR);
5360 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
5361 if(header.magic != MH_MAGIC_64) {
5362 barf("Bad magic. Expected: %08x, got: %08x.",
5363 MH_MAGIC_64, header.magic);
5366 if(header.magic != MH_MAGIC) {
5367 barf("Bad magic. Expected: %08x, got: %08x.",
5368 MH_MAGIC, header.magic);
5372 misalignment = (header.sizeofcmds + sizeof(header))
5375 return misalignment ? (16 - misalignment) : 0;