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) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS) || defined(darwin_HOST_OS)
83 #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)
84 # define OBJFORMAT_ELF
85 # include <regex.h> // regex is already used by dlopen() so this is OK
86 // to use here without requiring an additional lib
87 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
88 # define OBJFORMAT_PEi386
91 #elif defined(darwin_HOST_OS)
92 # define OBJFORMAT_MACHO
94 # include <mach-o/loader.h>
95 # include <mach-o/nlist.h>
96 # include <mach-o/reloc.h>
97 #if !defined(HAVE_DLFCN_H)
98 # include <mach-o/dyld.h>
100 #if defined(powerpc_HOST_ARCH)
101 # include <mach-o/ppc/reloc.h>
103 #if defined(x86_64_HOST_ARCH)
104 # include <mach-o/x86_64/reloc.h>
108 #if defined(x86_64_HOST_ARCH) && defined(darwin_HOST_OS)
112 /* Hash table mapping symbol names to Symbol */
113 static /*Str*/HashTable *symhash;
115 /* Hash table mapping symbol names to StgStablePtr */
116 static /*Str*/HashTable *stablehash;
118 /* List of currently loaded objects */
119 ObjectCode *objects = NULL; /* initially empty */
121 static HsInt loadOc( ObjectCode* oc );
122 static ObjectCode* mkOc( char *path, char *image, int imageSize,
123 char *archiveMemberName
125 #ifdef darwin_HOST_OS
131 #if defined(OBJFORMAT_ELF)
132 static int ocVerifyImage_ELF ( ObjectCode* oc );
133 static int ocGetNames_ELF ( ObjectCode* oc );
134 static int ocResolve_ELF ( ObjectCode* oc );
135 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
136 static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
138 #elif defined(OBJFORMAT_PEi386)
139 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
140 static int ocGetNames_PEi386 ( ObjectCode* oc );
141 static int ocResolve_PEi386 ( ObjectCode* oc );
142 static void *lookupSymbolInDLLs ( unsigned char *lbl );
143 static void zapTrailingAtSign ( unsigned char *sym );
144 #elif defined(OBJFORMAT_MACHO)
145 static int ocVerifyImage_MachO ( ObjectCode* oc );
146 static int ocGetNames_MachO ( ObjectCode* oc );
147 static int ocResolve_MachO ( ObjectCode* oc );
150 static int machoGetMisalignment( FILE * );
152 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
153 static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
155 #ifdef powerpc_HOST_ARCH
156 static void machoInitSymbolsWithoutUnderscore( void );
160 /* on x86_64 we have a problem with relocating symbol references in
161 * code that was compiled without -fPIC. By default, the small memory
162 * model is used, which assumes that symbol references can fit in a
163 * 32-bit slot. The system dynamic linker makes this work for
164 * references to shared libraries by either (a) allocating a jump
165 * table slot for code references, or (b) moving the symbol at load
166 * time (and copying its contents, if necessary) for data references.
168 * We unfortunately can't tell whether symbol references are to code
169 * or data. So for now we assume they are code (the vast majority
170 * are), and allocate jump-table slots. Unfortunately this will
171 * SILENTLY generate crashing code for data references. This hack is
172 * enabled by X86_64_ELF_NONPIC_HACK.
174 * One workaround is to use shared Haskell libraries. This is
175 * coming. Another workaround is to keep the static libraries but
176 * compile them with -fPIC, because that will generate PIC references
177 * to data which can be relocated. The PIC code is still too green to
178 * do this systematically, though.
181 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
183 * Naming Scheme for Symbol Macros
185 * SymI_*: symbol is internal to the RTS. It resides in an object
186 * file/library that is statically.
187 * SymE_*: symbol is external to the RTS library. It might be linked
190 * Sym*_HasProto : the symbol prototype is imported in an include file
191 * or defined explicitly
192 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
193 * default proto extern void sym(void);
195 #define X86_64_ELF_NONPIC_HACK 1
197 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
198 * small memory model on this architecture (see gcc docs,
201 * MAP_32BIT not available on OpenBSD/amd64
203 #if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT)
204 #define TRY_MAP_32BIT MAP_32BIT
206 #define TRY_MAP_32BIT 0
210 * Due to the small memory model (see above), on x86_64 we have to map
211 * all our non-PIC object files into the low 2Gb of the address space
212 * (why 2Gb and not 4Gb? Because all addresses must be reachable
213 * using a 32-bit signed PC-relative offset). On Linux we can do this
214 * using the MAP_32BIT flag to mmap(), however on other OSs
215 * (e.g. *BSD, see #2063, and also on Linux inside Xen, see #2512), we
216 * can't do this. So on these systems, we have to pick a base address
217 * in the low 2Gb of the address space and try to allocate memory from
220 * We pick a default address based on the OS, but also make this
221 * configurable via an RTS flag (+RTS -xm)
223 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
225 #if defined(MAP_32BIT)
226 // Try to use MAP_32BIT
227 #define MMAP_32BIT_BASE_DEFAULT 0
230 #define MMAP_32BIT_BASE_DEFAULT 0x40000000
233 static void *mmap_32bit_base = (void *)MMAP_32BIT_BASE_DEFAULT;
236 /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */
237 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
238 #define MAP_ANONYMOUS MAP_ANON
241 /* -----------------------------------------------------------------------------
242 * Built-in symbols from the RTS
245 typedef struct _RtsSymbolVal {
250 #define Maybe_Stable_Names SymI_HasProto(stg_mkWeakzh) \
251 SymI_HasProto(stg_mkWeakForeignEnvzh) \
252 SymI_HasProto(stg_makeStableNamezh) \
253 SymI_HasProto(stg_finalizzeWeakzh)
255 #if !defined (mingw32_HOST_OS)
256 #define RTS_POSIX_ONLY_SYMBOLS \
257 SymI_HasProto(__hscore_get_saved_termios) \
258 SymI_HasProto(__hscore_set_saved_termios) \
259 SymI_HasProto(shutdownHaskellAndSignal) \
260 SymI_HasProto(lockFile) \
261 SymI_HasProto(unlockFile) \
262 SymI_HasProto(signal_handlers) \
263 SymI_HasProto(stg_sig_install) \
264 SymI_NeedsProto(nocldstop)
267 #if defined (cygwin32_HOST_OS)
268 #define RTS_MINGW_ONLY_SYMBOLS /**/
269 /* Don't have the ability to read import libs / archives, so
270 * we have to stupidly list a lot of what libcygwin.a
273 #define RTS_CYGWIN_ONLY_SYMBOLS \
274 SymI_HasProto(regfree) \
275 SymI_HasProto(regexec) \
276 SymI_HasProto(regerror) \
277 SymI_HasProto(regcomp) \
278 SymI_HasProto(__errno) \
279 SymI_HasProto(access) \
280 SymI_HasProto(chmod) \
281 SymI_HasProto(chdir) \
282 SymI_HasProto(close) \
283 SymI_HasProto(creat) \
285 SymI_HasProto(dup2) \
286 SymI_HasProto(fstat) \
287 SymI_HasProto(fcntl) \
288 SymI_HasProto(getcwd) \
289 SymI_HasProto(getenv) \
290 SymI_HasProto(lseek) \
291 SymI_HasProto(open) \
292 SymI_HasProto(fpathconf) \
293 SymI_HasProto(pathconf) \
294 SymI_HasProto(stat) \
296 SymI_HasProto(tanh) \
297 SymI_HasProto(cosh) \
298 SymI_HasProto(sinh) \
299 SymI_HasProto(atan) \
300 SymI_HasProto(acos) \
301 SymI_HasProto(asin) \
307 SymI_HasProto(sqrt) \
308 SymI_HasProto(localtime_r) \
309 SymI_HasProto(gmtime_r) \
310 SymI_HasProto(mktime) \
311 SymI_NeedsProto(_imp___tzname) \
312 SymI_HasProto(gettimeofday) \
313 SymI_HasProto(timezone) \
314 SymI_HasProto(tcgetattr) \
315 SymI_HasProto(tcsetattr) \
316 SymI_HasProto(memcpy) \
317 SymI_HasProto(memmove) \
318 SymI_HasProto(realloc) \
319 SymI_HasProto(malloc) \
320 SymI_HasProto(free) \
321 SymI_HasProto(fork) \
322 SymI_HasProto(lstat) \
323 SymI_HasProto(isatty) \
324 SymI_HasProto(mkdir) \
325 SymI_HasProto(opendir) \
326 SymI_HasProto(readdir) \
327 SymI_HasProto(rewinddir) \
328 SymI_HasProto(closedir) \
329 SymI_HasProto(link) \
330 SymI_HasProto(mkfifo) \
331 SymI_HasProto(pipe) \
332 SymI_HasProto(read) \
333 SymI_HasProto(rename) \
334 SymI_HasProto(rmdir) \
335 SymI_HasProto(select) \
336 SymI_HasProto(system) \
337 SymI_HasProto(write) \
338 SymI_HasProto(strcmp) \
339 SymI_HasProto(strcpy) \
340 SymI_HasProto(strncpy) \
341 SymI_HasProto(strerror) \
342 SymI_HasProto(sigaddset) \
343 SymI_HasProto(sigemptyset) \
344 SymI_HasProto(sigprocmask) \
345 SymI_HasProto(umask) \
346 SymI_HasProto(uname) \
347 SymI_HasProto(unlink) \
348 SymI_HasProto(utime) \
349 SymI_HasProto(waitpid)
351 #elif !defined(mingw32_HOST_OS)
352 #define RTS_MINGW_ONLY_SYMBOLS /**/
353 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
354 #else /* defined(mingw32_HOST_OS) */
355 #define RTS_POSIX_ONLY_SYMBOLS /**/
356 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
358 #if HAVE_GETTIMEOFDAY
359 #define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
361 #define RTS_MINGW_GETTIMEOFDAY_SYM /**/
364 #if HAVE___MINGW_VFPRINTF
365 #define RTS___MINGW_VFPRINTF_SYM SymI_HasProto(__mingw_vfprintf)
367 #define RTS___MINGW_VFPRINTF_SYM /**/
370 /* These are statically linked from the mingw libraries into the ghc
371 executable, so we have to employ this hack. */
372 #define RTS_MINGW_ONLY_SYMBOLS \
373 SymI_HasProto(stg_asyncReadzh) \
374 SymI_HasProto(stg_asyncWritezh) \
375 SymI_HasProto(stg_asyncDoProczh) \
376 SymI_HasProto(memset) \
377 SymI_HasProto(inet_ntoa) \
378 SymI_HasProto(inet_addr) \
379 SymI_HasProto(htonl) \
380 SymI_HasProto(recvfrom) \
381 SymI_HasProto(listen) \
382 SymI_HasProto(bind) \
383 SymI_HasProto(shutdown) \
384 SymI_HasProto(connect) \
385 SymI_HasProto(htons) \
386 SymI_HasProto(ntohs) \
387 SymI_HasProto(getservbyname) \
388 SymI_HasProto(getservbyport) \
389 SymI_HasProto(getprotobynumber) \
390 SymI_HasProto(getprotobyname) \
391 SymI_HasProto(gethostbyname) \
392 SymI_HasProto(gethostbyaddr) \
393 SymI_HasProto(gethostname) \
394 SymI_HasProto(strcpy) \
395 SymI_HasProto(strncpy) \
396 SymI_HasProto(abort) \
397 SymI_NeedsProto(_alloca) \
398 SymI_HasProto(isxdigit) \
399 SymI_HasProto(isupper) \
400 SymI_HasProto(ispunct) \
401 SymI_HasProto(islower) \
402 SymI_HasProto(isspace) \
403 SymI_HasProto(isprint) \
404 SymI_HasProto(isdigit) \
405 SymI_HasProto(iscntrl) \
406 SymI_HasProto(isalpha) \
407 SymI_HasProto(isalnum) \
408 SymI_HasProto(isascii) \
409 RTS___MINGW_VFPRINTF_SYM \
410 SymI_HasProto(strcmp) \
411 SymI_HasProto(memmove) \
412 SymI_HasProto(realloc) \
413 SymI_HasProto(malloc) \
415 SymI_HasProto(tanh) \
416 SymI_HasProto(cosh) \
417 SymI_HasProto(sinh) \
418 SymI_HasProto(atan) \
419 SymI_HasProto(acos) \
420 SymI_HasProto(asin) \
426 SymI_HasProto(sqrt) \
427 SymI_HasProto(powf) \
428 SymI_HasProto(tanhf) \
429 SymI_HasProto(coshf) \
430 SymI_HasProto(sinhf) \
431 SymI_HasProto(atanf) \
432 SymI_HasProto(acosf) \
433 SymI_HasProto(asinf) \
434 SymI_HasProto(tanf) \
435 SymI_HasProto(cosf) \
436 SymI_HasProto(sinf) \
437 SymI_HasProto(expf) \
438 SymI_HasProto(logf) \
439 SymI_HasProto(sqrtf) \
441 SymI_HasProto(erfc) \
442 SymI_HasProto(erff) \
443 SymI_HasProto(erfcf) \
444 SymI_HasProto(memcpy) \
445 SymI_HasProto(rts_InstallConsoleEvent) \
446 SymI_HasProto(rts_ConsoleHandlerDone) \
447 SymI_NeedsProto(mktime) \
448 SymI_NeedsProto(_imp___timezone) \
449 SymI_NeedsProto(_imp___tzname) \
450 SymI_NeedsProto(_imp__tzname) \
451 SymI_NeedsProto(_imp___iob) \
452 SymI_NeedsProto(_imp___osver) \
453 SymI_NeedsProto(localtime) \
454 SymI_NeedsProto(gmtime) \
455 SymI_NeedsProto(opendir) \
456 SymI_NeedsProto(readdir) \
457 SymI_NeedsProto(rewinddir) \
458 SymI_NeedsProto(_imp____mb_cur_max) \
459 SymI_NeedsProto(_imp___pctype) \
460 SymI_NeedsProto(__chkstk) \
461 RTS_MINGW_GETTIMEOFDAY_SYM \
462 SymI_NeedsProto(closedir)
466 #if defined(darwin_HOST_OS) && HAVE_PRINTF_LDBLSTUB
467 #define RTS_DARWIN_ONLY_SYMBOLS \
468 SymI_NeedsProto(asprintf$LDBLStub) \
469 SymI_NeedsProto(err$LDBLStub) \
470 SymI_NeedsProto(errc$LDBLStub) \
471 SymI_NeedsProto(errx$LDBLStub) \
472 SymI_NeedsProto(fprintf$LDBLStub) \
473 SymI_NeedsProto(fscanf$LDBLStub) \
474 SymI_NeedsProto(fwprintf$LDBLStub) \
475 SymI_NeedsProto(fwscanf$LDBLStub) \
476 SymI_NeedsProto(printf$LDBLStub) \
477 SymI_NeedsProto(scanf$LDBLStub) \
478 SymI_NeedsProto(snprintf$LDBLStub) \
479 SymI_NeedsProto(sprintf$LDBLStub) \
480 SymI_NeedsProto(sscanf$LDBLStub) \
481 SymI_NeedsProto(strtold$LDBLStub) \
482 SymI_NeedsProto(swprintf$LDBLStub) \
483 SymI_NeedsProto(swscanf$LDBLStub) \
484 SymI_NeedsProto(syslog$LDBLStub) \
485 SymI_NeedsProto(vasprintf$LDBLStub) \
486 SymI_NeedsProto(verr$LDBLStub) \
487 SymI_NeedsProto(verrc$LDBLStub) \
488 SymI_NeedsProto(verrx$LDBLStub) \
489 SymI_NeedsProto(vfprintf$LDBLStub) \
490 SymI_NeedsProto(vfscanf$LDBLStub) \
491 SymI_NeedsProto(vfwprintf$LDBLStub) \
492 SymI_NeedsProto(vfwscanf$LDBLStub) \
493 SymI_NeedsProto(vprintf$LDBLStub) \
494 SymI_NeedsProto(vscanf$LDBLStub) \
495 SymI_NeedsProto(vsnprintf$LDBLStub) \
496 SymI_NeedsProto(vsprintf$LDBLStub) \
497 SymI_NeedsProto(vsscanf$LDBLStub) \
498 SymI_NeedsProto(vswprintf$LDBLStub) \
499 SymI_NeedsProto(vswscanf$LDBLStub) \
500 SymI_NeedsProto(vsyslog$LDBLStub) \
501 SymI_NeedsProto(vwarn$LDBLStub) \
502 SymI_NeedsProto(vwarnc$LDBLStub) \
503 SymI_NeedsProto(vwarnx$LDBLStub) \
504 SymI_NeedsProto(vwprintf$LDBLStub) \
505 SymI_NeedsProto(vwscanf$LDBLStub) \
506 SymI_NeedsProto(warn$LDBLStub) \
507 SymI_NeedsProto(warnc$LDBLStub) \
508 SymI_NeedsProto(warnx$LDBLStub) \
509 SymI_NeedsProto(wcstold$LDBLStub) \
510 SymI_NeedsProto(wprintf$LDBLStub) \
511 SymI_NeedsProto(wscanf$LDBLStub)
513 #define RTS_DARWIN_ONLY_SYMBOLS
517 # define MAIN_CAP_SYM SymI_HasProto(MainCapability)
519 # define MAIN_CAP_SYM
522 #if !defined(mingw32_HOST_OS)
523 #define RTS_USER_SIGNALS_SYMBOLS \
524 SymI_HasProto(setIOManagerControlFd) \
525 SymI_HasProto(setIOManagerWakeupFd) \
526 SymI_HasProto(ioManagerWakeup) \
527 SymI_HasProto(blockUserSignals) \
528 SymI_HasProto(unblockUserSignals)
530 #define RTS_USER_SIGNALS_SYMBOLS \
531 SymI_HasProto(ioManagerWakeup) \
532 SymI_HasProto(sendIOManagerEvent) \
533 SymI_HasProto(readIOManagerEvent) \
534 SymI_HasProto(getIOManagerEvent) \
535 SymI_HasProto(console_handler)
538 #define RTS_LIBFFI_SYMBOLS \
539 SymE_NeedsProto(ffi_prep_cif) \
540 SymE_NeedsProto(ffi_call) \
541 SymE_NeedsProto(ffi_type_void) \
542 SymE_NeedsProto(ffi_type_float) \
543 SymE_NeedsProto(ffi_type_double) \
544 SymE_NeedsProto(ffi_type_sint64) \
545 SymE_NeedsProto(ffi_type_uint64) \
546 SymE_NeedsProto(ffi_type_sint32) \
547 SymE_NeedsProto(ffi_type_uint32) \
548 SymE_NeedsProto(ffi_type_sint16) \
549 SymE_NeedsProto(ffi_type_uint16) \
550 SymE_NeedsProto(ffi_type_sint8) \
551 SymE_NeedsProto(ffi_type_uint8) \
552 SymE_NeedsProto(ffi_type_pointer)
554 #ifdef TABLES_NEXT_TO_CODE
555 #define RTS_RET_SYMBOLS /* nothing */
557 #define RTS_RET_SYMBOLS \
558 SymI_HasProto(stg_enter_ret) \
559 SymI_HasProto(stg_gc_fun_ret) \
560 SymI_HasProto(stg_ap_v_ret) \
561 SymI_HasProto(stg_ap_f_ret) \
562 SymI_HasProto(stg_ap_d_ret) \
563 SymI_HasProto(stg_ap_l_ret) \
564 SymI_HasProto(stg_ap_n_ret) \
565 SymI_HasProto(stg_ap_p_ret) \
566 SymI_HasProto(stg_ap_pv_ret) \
567 SymI_HasProto(stg_ap_pp_ret) \
568 SymI_HasProto(stg_ap_ppv_ret) \
569 SymI_HasProto(stg_ap_ppp_ret) \
570 SymI_HasProto(stg_ap_pppv_ret) \
571 SymI_HasProto(stg_ap_pppp_ret) \
572 SymI_HasProto(stg_ap_ppppp_ret) \
573 SymI_HasProto(stg_ap_pppppp_ret)
576 /* Modules compiled with -ticky may mention ticky counters */
577 /* This list should marry up with the one in $(TOP)/includes/stg/Ticky.h */
578 #define RTS_TICKY_SYMBOLS \
579 SymI_NeedsProto(ticky_entry_ctrs) \
580 SymI_NeedsProto(top_ct) \
582 SymI_HasProto(ENT_VIA_NODE_ctr) \
583 SymI_HasProto(ENT_STATIC_THK_ctr) \
584 SymI_HasProto(ENT_DYN_THK_ctr) \
585 SymI_HasProto(ENT_STATIC_FUN_DIRECT_ctr) \
586 SymI_HasProto(ENT_DYN_FUN_DIRECT_ctr) \
587 SymI_HasProto(ENT_STATIC_CON_ctr) \
588 SymI_HasProto(ENT_DYN_CON_ctr) \
589 SymI_HasProto(ENT_STATIC_IND_ctr) \
590 SymI_HasProto(ENT_DYN_IND_ctr) \
591 SymI_HasProto(ENT_PERM_IND_ctr) \
592 SymI_HasProto(ENT_PAP_ctr) \
593 SymI_HasProto(ENT_AP_ctr) \
594 SymI_HasProto(ENT_AP_STACK_ctr) \
595 SymI_HasProto(ENT_BH_ctr) \
596 SymI_HasProto(UNKNOWN_CALL_ctr) \
597 SymI_HasProto(SLOW_CALL_v_ctr) \
598 SymI_HasProto(SLOW_CALL_f_ctr) \
599 SymI_HasProto(SLOW_CALL_d_ctr) \
600 SymI_HasProto(SLOW_CALL_l_ctr) \
601 SymI_HasProto(SLOW_CALL_n_ctr) \
602 SymI_HasProto(SLOW_CALL_p_ctr) \
603 SymI_HasProto(SLOW_CALL_pv_ctr) \
604 SymI_HasProto(SLOW_CALL_pp_ctr) \
605 SymI_HasProto(SLOW_CALL_ppv_ctr) \
606 SymI_HasProto(SLOW_CALL_ppp_ctr) \
607 SymI_HasProto(SLOW_CALL_pppv_ctr) \
608 SymI_HasProto(SLOW_CALL_pppp_ctr) \
609 SymI_HasProto(SLOW_CALL_ppppp_ctr) \
610 SymI_HasProto(SLOW_CALL_pppppp_ctr) \
611 SymI_HasProto(SLOW_CALL_OTHER_ctr) \
612 SymI_HasProto(ticky_slow_call_unevald) \
613 SymI_HasProto(SLOW_CALL_ctr) \
614 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_ctr) \
615 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_CHUNKS_ctr) \
616 SymI_HasProto(KNOWN_CALL_ctr) \
617 SymI_HasProto(KNOWN_CALL_TOO_FEW_ARGS_ctr) \
618 SymI_HasProto(KNOWN_CALL_EXTRA_ARGS_ctr) \
619 SymI_HasProto(SLOW_CALL_FUN_TOO_FEW_ctr) \
620 SymI_HasProto(SLOW_CALL_FUN_CORRECT_ctr) \
621 SymI_HasProto(SLOW_CALL_FUN_TOO_MANY_ctr) \
622 SymI_HasProto(SLOW_CALL_PAP_TOO_FEW_ctr) \
623 SymI_HasProto(SLOW_CALL_PAP_CORRECT_ctr) \
624 SymI_HasProto(SLOW_CALL_PAP_TOO_MANY_ctr) \
625 SymI_HasProto(SLOW_CALL_UNEVALD_ctr) \
626 SymI_HasProto(UPDF_OMITTED_ctr) \
627 SymI_HasProto(UPDF_PUSHED_ctr) \
628 SymI_HasProto(CATCHF_PUSHED_ctr) \
629 SymI_HasProto(UPDF_RCC_PUSHED_ctr) \
630 SymI_HasProto(UPDF_RCC_OMITTED_ctr) \
631 SymI_HasProto(UPD_SQUEEZED_ctr) \
632 SymI_HasProto(UPD_CON_IN_NEW_ctr) \
633 SymI_HasProto(UPD_CON_IN_PLACE_ctr) \
634 SymI_HasProto(UPD_PAP_IN_NEW_ctr) \
635 SymI_HasProto(UPD_PAP_IN_PLACE_ctr) \
636 SymI_HasProto(ALLOC_HEAP_ctr) \
637 SymI_HasProto(ALLOC_HEAP_tot) \
638 SymI_HasProto(ALLOC_FUN_ctr) \
639 SymI_HasProto(ALLOC_FUN_adm) \
640 SymI_HasProto(ALLOC_FUN_gds) \
641 SymI_HasProto(ALLOC_FUN_slp) \
642 SymI_HasProto(UPD_NEW_IND_ctr) \
643 SymI_HasProto(UPD_NEW_PERM_IND_ctr) \
644 SymI_HasProto(UPD_OLD_IND_ctr) \
645 SymI_HasProto(UPD_OLD_PERM_IND_ctr) \
646 SymI_HasProto(UPD_BH_UPDATABLE_ctr) \
647 SymI_HasProto(UPD_BH_SINGLE_ENTRY_ctr) \
648 SymI_HasProto(UPD_CAF_BH_UPDATABLE_ctr) \
649 SymI_HasProto(UPD_CAF_BH_SINGLE_ENTRY_ctr) \
650 SymI_HasProto(GC_SEL_ABANDONED_ctr) \
651 SymI_HasProto(GC_SEL_MINOR_ctr) \
652 SymI_HasProto(GC_SEL_MAJOR_ctr) \
653 SymI_HasProto(GC_FAILED_PROMOTION_ctr) \
654 SymI_HasProto(ALLOC_UP_THK_ctr) \
655 SymI_HasProto(ALLOC_SE_THK_ctr) \
656 SymI_HasProto(ALLOC_THK_adm) \
657 SymI_HasProto(ALLOC_THK_gds) \
658 SymI_HasProto(ALLOC_THK_slp) \
659 SymI_HasProto(ALLOC_CON_ctr) \
660 SymI_HasProto(ALLOC_CON_adm) \
661 SymI_HasProto(ALLOC_CON_gds) \
662 SymI_HasProto(ALLOC_CON_slp) \
663 SymI_HasProto(ALLOC_TUP_ctr) \
664 SymI_HasProto(ALLOC_TUP_adm) \
665 SymI_HasProto(ALLOC_TUP_gds) \
666 SymI_HasProto(ALLOC_TUP_slp) \
667 SymI_HasProto(ALLOC_BH_ctr) \
668 SymI_HasProto(ALLOC_BH_adm) \
669 SymI_HasProto(ALLOC_BH_gds) \
670 SymI_HasProto(ALLOC_BH_slp) \
671 SymI_HasProto(ALLOC_PRIM_ctr) \
672 SymI_HasProto(ALLOC_PRIM_adm) \
673 SymI_HasProto(ALLOC_PRIM_gds) \
674 SymI_HasProto(ALLOC_PRIM_slp) \
675 SymI_HasProto(ALLOC_PAP_ctr) \
676 SymI_HasProto(ALLOC_PAP_adm) \
677 SymI_HasProto(ALLOC_PAP_gds) \
678 SymI_HasProto(ALLOC_PAP_slp) \
679 SymI_HasProto(ALLOC_TSO_ctr) \
680 SymI_HasProto(ALLOC_TSO_adm) \
681 SymI_HasProto(ALLOC_TSO_gds) \
682 SymI_HasProto(ALLOC_TSO_slp) \
683 SymI_HasProto(RET_NEW_ctr) \
684 SymI_HasProto(RET_OLD_ctr) \
685 SymI_HasProto(RET_UNBOXED_TUP_ctr) \
686 SymI_HasProto(RET_SEMI_loads_avoided)
689 // On most platforms, the garbage collector rewrites references
690 // to small integer and char objects to a set of common, shared ones.
692 // We don't do this when compiling to Windows DLLs at the moment because
693 // it doesn't support cross package data references well.
695 #if defined(__PIC__) && defined(mingw32_HOST_OS)
696 #define RTS_INTCHAR_SYMBOLS
698 #define RTS_INTCHAR_SYMBOLS \
699 SymI_HasProto(stg_CHARLIKE_closure) \
700 SymI_HasProto(stg_INTLIKE_closure)
704 #define RTS_SYMBOLS \
707 SymI_HasProto(StgReturn) \
708 SymI_HasProto(stg_enter_info) \
709 SymI_HasProto(stg_gc_void_info) \
710 SymI_HasProto(__stg_gc_enter_1) \
711 SymI_HasProto(stg_gc_noregs) \
712 SymI_HasProto(stg_gc_unpt_r1_info) \
713 SymI_HasProto(stg_gc_unpt_r1) \
714 SymI_HasProto(stg_gc_unbx_r1_info) \
715 SymI_HasProto(stg_gc_unbx_r1) \
716 SymI_HasProto(stg_gc_f1_info) \
717 SymI_HasProto(stg_gc_f1) \
718 SymI_HasProto(stg_gc_d1_info) \
719 SymI_HasProto(stg_gc_d1) \
720 SymI_HasProto(stg_gc_l1_info) \
721 SymI_HasProto(stg_gc_l1) \
722 SymI_HasProto(__stg_gc_fun) \
723 SymI_HasProto(stg_gc_fun_info) \
724 SymI_HasProto(stg_gc_gen) \
725 SymI_HasProto(stg_gc_gen_info) \
726 SymI_HasProto(stg_gc_gen_hp) \
727 SymI_HasProto(stg_gc_ut) \
728 SymI_HasProto(stg_gen_yield) \
729 SymI_HasProto(stg_yield_noregs) \
730 SymI_HasProto(stg_yield_to_interpreter) \
731 SymI_HasProto(stg_gen_block) \
732 SymI_HasProto(stg_block_noregs) \
733 SymI_HasProto(stg_block_1) \
734 SymI_HasProto(stg_block_takemvar) \
735 SymI_HasProto(stg_block_putmvar) \
737 SymI_HasProto(MallocFailHook) \
738 SymI_HasProto(OnExitHook) \
739 SymI_HasProto(OutOfHeapHook) \
740 SymI_HasProto(StackOverflowHook) \
741 SymI_HasProto(addDLL) \
742 SymI_HasProto(__int_encodeDouble) \
743 SymI_HasProto(__word_encodeDouble) \
744 SymI_HasProto(__2Int_encodeDouble) \
745 SymI_HasProto(__int_encodeFloat) \
746 SymI_HasProto(__word_encodeFloat) \
747 SymI_HasProto(stg_atomicallyzh) \
748 SymI_HasProto(barf) \
749 SymI_HasProto(debugBelch) \
750 SymI_HasProto(errorBelch) \
751 SymI_HasProto(sysErrorBelch) \
752 SymI_HasProto(stg_getMaskingStatezh) \
753 SymI_HasProto(stg_maskAsyncExceptionszh) \
754 SymI_HasProto(stg_maskUninterruptiblezh) \
755 SymI_HasProto(stg_catchzh) \
756 SymI_HasProto(stg_catchRetryzh) \
757 SymI_HasProto(stg_catchSTMzh) \
758 SymI_HasProto(stg_checkzh) \
759 SymI_HasProto(closure_flags) \
760 SymI_HasProto(cmp_thread) \
761 SymI_HasProto(createAdjustor) \
762 SymI_HasProto(stg_decodeDoublezu2Intzh) \
763 SymI_HasProto(stg_decodeFloatzuIntzh) \
764 SymI_HasProto(defaultsHook) \
765 SymI_HasProto(stg_delayzh) \
766 SymI_HasProto(stg_deRefWeakzh) \
767 SymI_HasProto(stg_deRefStablePtrzh) \
768 SymI_HasProto(dirty_MUT_VAR) \
769 SymI_HasProto(stg_forkzh) \
770 SymI_HasProto(stg_forkOnzh) \
771 SymI_HasProto(forkProcess) \
772 SymI_HasProto(forkOS_createThread) \
773 SymI_HasProto(freeHaskellFunctionPtr) \
774 SymI_HasProto(getOrSetTypeableStore) \
775 SymI_HasProto(getOrSetGHCConcSignalSignalHandlerStore) \
776 SymI_HasProto(getOrSetGHCConcWindowsPendingDelaysStore) \
777 SymI_HasProto(getOrSetGHCConcWindowsIOManagerThreadStore) \
778 SymI_HasProto(getOrSetGHCConcWindowsProddingStore) \
779 SymI_HasProto(getOrSetSystemEventThreadEventManagerStore) \
780 SymI_HasProto(getOrSetSystemEventThreadIOManagerThreadStore) \
781 SymI_HasProto(genSymZh) \
782 SymI_HasProto(genericRaise) \
783 SymI_HasProto(getProgArgv) \
784 SymI_HasProto(getFullProgArgv) \
785 SymI_HasProto(getStablePtr) \
786 SymI_HasProto(hs_init) \
787 SymI_HasProto(hs_exit) \
788 SymI_HasProto(hs_set_argv) \
789 SymI_HasProto(hs_add_root) \
790 SymI_HasProto(hs_perform_gc) \
791 SymI_HasProto(hs_free_stable_ptr) \
792 SymI_HasProto(hs_free_fun_ptr) \
793 SymI_HasProto(hs_hpc_rootModule) \
794 SymI_HasProto(hs_hpc_module) \
795 SymI_HasProto(initLinker) \
796 SymI_HasProto(stg_unpackClosurezh) \
797 SymI_HasProto(stg_getApStackValzh) \
798 SymI_HasProto(stg_getSparkzh) \
799 SymI_HasProto(stg_numSparkszh) \
800 SymI_HasProto(stg_isCurrentThreadBoundzh) \
801 SymI_HasProto(stg_isEmptyMVarzh) \
802 SymI_HasProto(stg_killThreadzh) \
803 SymI_HasProto(loadArchive) \
804 SymI_HasProto(loadObj) \
805 SymI_HasProto(insertStableSymbol) \
806 SymI_HasProto(insertSymbol) \
807 SymI_HasProto(lookupSymbol) \
808 SymI_HasProto(stg_makeStablePtrzh) \
809 SymI_HasProto(stg_mkApUpd0zh) \
810 SymI_HasProto(stg_myThreadIdzh) \
811 SymI_HasProto(stg_labelThreadzh) \
812 SymI_HasProto(stg_newArrayzh) \
813 SymI_HasProto(stg_newBCOzh) \
814 SymI_HasProto(stg_newByteArrayzh) \
815 SymI_HasProto_redirect(newCAF, newDynCAF) \
816 SymI_HasProto(stg_newMVarzh) \
817 SymI_HasProto(stg_newMutVarzh) \
818 SymI_HasProto(stg_newTVarzh) \
819 SymI_HasProto(stg_noDuplicatezh) \
820 SymI_HasProto(stg_atomicModifyMutVarzh) \
821 SymI_HasProto(stg_newPinnedByteArrayzh) \
822 SymI_HasProto(stg_newAlignedPinnedByteArrayzh) \
823 SymI_HasProto(newSpark) \
824 SymI_HasProto(performGC) \
825 SymI_HasProto(performMajorGC) \
826 SymI_HasProto(prog_argc) \
827 SymI_HasProto(prog_argv) \
828 SymI_HasProto(stg_putMVarzh) \
829 SymI_HasProto(stg_raisezh) \
830 SymI_HasProto(stg_raiseIOzh) \
831 SymI_HasProto(stg_readTVarzh) \
832 SymI_HasProto(stg_readTVarIOzh) \
833 SymI_HasProto(resumeThread) \
834 SymI_HasProto(resolveObjs) \
835 SymI_HasProto(stg_retryzh) \
836 SymI_HasProto(rts_apply) \
837 SymI_HasProto(rts_checkSchedStatus) \
838 SymI_HasProto(rts_eval) \
839 SymI_HasProto(rts_evalIO) \
840 SymI_HasProto(rts_evalLazyIO) \
841 SymI_HasProto(rts_evalStableIO) \
842 SymI_HasProto(rts_eval_) \
843 SymI_HasProto(rts_getBool) \
844 SymI_HasProto(rts_getChar) \
845 SymI_HasProto(rts_getDouble) \
846 SymI_HasProto(rts_getFloat) \
847 SymI_HasProto(rts_getInt) \
848 SymI_HasProto(rts_getInt8) \
849 SymI_HasProto(rts_getInt16) \
850 SymI_HasProto(rts_getInt32) \
851 SymI_HasProto(rts_getInt64) \
852 SymI_HasProto(rts_getPtr) \
853 SymI_HasProto(rts_getFunPtr) \
854 SymI_HasProto(rts_getStablePtr) \
855 SymI_HasProto(rts_getThreadId) \
856 SymI_HasProto(rts_getWord) \
857 SymI_HasProto(rts_getWord8) \
858 SymI_HasProto(rts_getWord16) \
859 SymI_HasProto(rts_getWord32) \
860 SymI_HasProto(rts_getWord64) \
861 SymI_HasProto(rts_lock) \
862 SymI_HasProto(rts_mkBool) \
863 SymI_HasProto(rts_mkChar) \
864 SymI_HasProto(rts_mkDouble) \
865 SymI_HasProto(rts_mkFloat) \
866 SymI_HasProto(rts_mkInt) \
867 SymI_HasProto(rts_mkInt8) \
868 SymI_HasProto(rts_mkInt16) \
869 SymI_HasProto(rts_mkInt32) \
870 SymI_HasProto(rts_mkInt64) \
871 SymI_HasProto(rts_mkPtr) \
872 SymI_HasProto(rts_mkFunPtr) \
873 SymI_HasProto(rts_mkStablePtr) \
874 SymI_HasProto(rts_mkString) \
875 SymI_HasProto(rts_mkWord) \
876 SymI_HasProto(rts_mkWord8) \
877 SymI_HasProto(rts_mkWord16) \
878 SymI_HasProto(rts_mkWord32) \
879 SymI_HasProto(rts_mkWord64) \
880 SymI_HasProto(rts_unlock) \
881 SymI_HasProto(rts_unsafeGetMyCapability) \
882 SymI_HasProto(rtsSupportsBoundThreads) \
883 SymI_HasProto(rts_isProfiled) \
884 SymI_HasProto(setProgArgv) \
885 SymI_HasProto(startupHaskell) \
886 SymI_HasProto(shutdownHaskell) \
887 SymI_HasProto(shutdownHaskellAndExit) \
888 SymI_HasProto(stable_ptr_table) \
889 SymI_HasProto(stackOverflow) \
890 SymI_HasProto(stg_CAF_BLACKHOLE_info) \
891 SymI_HasProto(stg_BLACKHOLE_info) \
892 SymI_HasProto(__stg_EAGER_BLACKHOLE_info) \
893 SymI_HasProto(stg_BLOCKING_QUEUE_CLEAN_info) \
894 SymI_HasProto(stg_BLOCKING_QUEUE_DIRTY_info) \
895 SymI_HasProto(startTimer) \
896 SymI_HasProto(stg_MVAR_CLEAN_info) \
897 SymI_HasProto(stg_MVAR_DIRTY_info) \
898 SymI_HasProto(stg_IND_STATIC_info) \
899 SymI_HasProto(stg_ARR_WORDS_info) \
900 SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info) \
901 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info) \
902 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info) \
903 SymI_HasProto(stg_WEAK_info) \
904 SymI_HasProto(stg_ap_v_info) \
905 SymI_HasProto(stg_ap_f_info) \
906 SymI_HasProto(stg_ap_d_info) \
907 SymI_HasProto(stg_ap_l_info) \
908 SymI_HasProto(stg_ap_n_info) \
909 SymI_HasProto(stg_ap_p_info) \
910 SymI_HasProto(stg_ap_pv_info) \
911 SymI_HasProto(stg_ap_pp_info) \
912 SymI_HasProto(stg_ap_ppv_info) \
913 SymI_HasProto(stg_ap_ppp_info) \
914 SymI_HasProto(stg_ap_pppv_info) \
915 SymI_HasProto(stg_ap_pppp_info) \
916 SymI_HasProto(stg_ap_ppppp_info) \
917 SymI_HasProto(stg_ap_pppppp_info) \
918 SymI_HasProto(stg_ap_0_fast) \
919 SymI_HasProto(stg_ap_v_fast) \
920 SymI_HasProto(stg_ap_f_fast) \
921 SymI_HasProto(stg_ap_d_fast) \
922 SymI_HasProto(stg_ap_l_fast) \
923 SymI_HasProto(stg_ap_n_fast) \
924 SymI_HasProto(stg_ap_p_fast) \
925 SymI_HasProto(stg_ap_pv_fast) \
926 SymI_HasProto(stg_ap_pp_fast) \
927 SymI_HasProto(stg_ap_ppv_fast) \
928 SymI_HasProto(stg_ap_ppp_fast) \
929 SymI_HasProto(stg_ap_pppv_fast) \
930 SymI_HasProto(stg_ap_pppp_fast) \
931 SymI_HasProto(stg_ap_ppppp_fast) \
932 SymI_HasProto(stg_ap_pppppp_fast) \
933 SymI_HasProto(stg_ap_1_upd_info) \
934 SymI_HasProto(stg_ap_2_upd_info) \
935 SymI_HasProto(stg_ap_3_upd_info) \
936 SymI_HasProto(stg_ap_4_upd_info) \
937 SymI_HasProto(stg_ap_5_upd_info) \
938 SymI_HasProto(stg_ap_6_upd_info) \
939 SymI_HasProto(stg_ap_7_upd_info) \
940 SymI_HasProto(stg_exit) \
941 SymI_HasProto(stg_sel_0_upd_info) \
942 SymI_HasProto(stg_sel_10_upd_info) \
943 SymI_HasProto(stg_sel_11_upd_info) \
944 SymI_HasProto(stg_sel_12_upd_info) \
945 SymI_HasProto(stg_sel_13_upd_info) \
946 SymI_HasProto(stg_sel_14_upd_info) \
947 SymI_HasProto(stg_sel_15_upd_info) \
948 SymI_HasProto(stg_sel_1_upd_info) \
949 SymI_HasProto(stg_sel_2_upd_info) \
950 SymI_HasProto(stg_sel_3_upd_info) \
951 SymI_HasProto(stg_sel_4_upd_info) \
952 SymI_HasProto(stg_sel_5_upd_info) \
953 SymI_HasProto(stg_sel_6_upd_info) \
954 SymI_HasProto(stg_sel_7_upd_info) \
955 SymI_HasProto(stg_sel_8_upd_info) \
956 SymI_HasProto(stg_sel_9_upd_info) \
957 SymI_HasProto(stg_upd_frame_info) \
958 SymI_HasProto(stg_bh_upd_frame_info) \
959 SymI_HasProto(suspendThread) \
960 SymI_HasProto(stg_takeMVarzh) \
961 SymI_HasProto(stg_threadStatuszh) \
962 SymI_HasProto(stg_tryPutMVarzh) \
963 SymI_HasProto(stg_tryTakeMVarzh) \
964 SymI_HasProto(stg_unmaskAsyncExceptionszh) \
965 SymI_HasProto(unloadObj) \
966 SymI_HasProto(stg_unsafeThawArrayzh) \
967 SymI_HasProto(stg_waitReadzh) \
968 SymI_HasProto(stg_waitWritezh) \
969 SymI_HasProto(stg_writeTVarzh) \
970 SymI_HasProto(stg_yieldzh) \
971 SymI_NeedsProto(stg_interp_constr_entry) \
972 SymI_HasProto(stg_arg_bitmaps) \
973 SymI_HasProto(alloc_blocks_lim) \
975 SymI_HasProto(allocate) \
976 SymI_HasProto(allocateExec) \
977 SymI_HasProto(freeExec) \
978 SymI_HasProto(getAllocations) \
979 SymI_HasProto(revertCAFs) \
980 SymI_HasProto(RtsFlags) \
981 SymI_NeedsProto(rts_breakpoint_io_action) \
982 SymI_NeedsProto(rts_stop_next_breakpoint) \
983 SymI_NeedsProto(rts_stop_on_exception) \
984 SymI_HasProto(stopTimer) \
985 SymI_HasProto(n_capabilities) \
986 SymI_HasProto(stg_traceCcszh) \
987 SymI_HasProto(stg_traceEventzh) \
988 RTS_USER_SIGNALS_SYMBOLS \
992 // 64-bit support functions in libgcc.a
993 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
994 #define RTS_LIBGCC_SYMBOLS \
995 SymI_NeedsProto(__divdi3) \
996 SymI_NeedsProto(__udivdi3) \
997 SymI_NeedsProto(__moddi3) \
998 SymI_NeedsProto(__umoddi3) \
999 SymI_NeedsProto(__muldi3) \
1000 SymI_NeedsProto(__ashldi3) \
1001 SymI_NeedsProto(__ashrdi3) \
1002 SymI_NeedsProto(__lshrdi3)
1004 #define RTS_LIBGCC_SYMBOLS
1007 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
1008 // Symbols that don't have a leading underscore
1009 // on Mac OS X. They have to receive special treatment,
1010 // see machoInitSymbolsWithoutUnderscore()
1011 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
1012 SymI_NeedsProto(saveFP) \
1013 SymI_NeedsProto(restFP)
1016 /* entirely bogus claims about types of these symbols */
1017 #define SymI_NeedsProto(vvv) extern void vvv(void);
1018 #if defined(__PIC__) && defined(mingw32_HOST_OS)
1019 #define SymE_HasProto(vvv) SymE_HasProto(vvv);
1020 #define SymE_NeedsProto(vvv) extern void _imp__ ## vvv (void);
1022 #define SymE_NeedsProto(vvv) SymI_NeedsProto(vvv);
1023 #define SymE_HasProto(vvv) SymI_HasProto(vvv)
1025 #define SymI_HasProto(vvv) /**/
1026 #define SymI_HasProto_redirect(vvv,xxx) /**/
1029 RTS_POSIX_ONLY_SYMBOLS
1030 RTS_MINGW_ONLY_SYMBOLS
1031 RTS_CYGWIN_ONLY_SYMBOLS
1032 RTS_DARWIN_ONLY_SYMBOLS
1035 #undef SymI_NeedsProto
1036 #undef SymI_HasProto
1037 #undef SymI_HasProto_redirect
1038 #undef SymE_HasProto
1039 #undef SymE_NeedsProto
1041 #ifdef LEADING_UNDERSCORE
1042 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
1044 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
1047 #define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1049 #define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1050 (void*)DLL_IMPORT_DATA_REF(vvv) },
1052 #define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
1053 #define SymE_NeedsProto(vvv) SymE_HasProto(vvv)
1055 // SymI_HasProto_redirect allows us to redirect references to one symbol to
1056 // another symbol. See newCAF/newDynCAF for an example.
1057 #define SymI_HasProto_redirect(vvv,xxx) \
1058 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1061 static RtsSymbolVal rtsSyms[] = {
1064 RTS_POSIX_ONLY_SYMBOLS
1065 RTS_MINGW_ONLY_SYMBOLS
1066 RTS_CYGWIN_ONLY_SYMBOLS
1067 RTS_DARWIN_ONLY_SYMBOLS
1070 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
1071 // dyld stub code contains references to this,
1072 // but it should never be called because we treat
1073 // lazy pointers as nonlazy.
1074 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
1076 { 0, 0 } /* sentinel */
1081 /* -----------------------------------------------------------------------------
1082 * Insert symbols into hash tables, checking for duplicates.
1085 static void ghciInsertStrHashTable ( char* obj_name,
1091 if (lookupHashTable(table, (StgWord)key) == NULL)
1093 insertStrHashTable(table, (StgWord)key, data);
1098 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
1100 "whilst processing object file\n"
1102 "This could be caused by:\n"
1103 " * Loading two different object files which export the same symbol\n"
1104 " * Specifying the same object file twice on the GHCi command line\n"
1105 " * An incorrect `package.conf' entry, causing some object to be\n"
1107 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
1114 /* -----------------------------------------------------------------------------
1115 * initialize the object linker
1119 static int linker_init_done = 0 ;
1121 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1122 static void *dl_prog_handle;
1123 static regex_t re_invalid;
1124 static regex_t re_realso;
1126 static Mutex dl_mutex; // mutex to protect dlopen/dlerror critical section
1134 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1138 IF_DEBUG(linker, debugBelch("initLinker: start\n"));
1140 /* Make initLinker idempotent, so we can call it
1141 before evey relevant operation; that means we
1142 don't need to initialise the linker separately */
1143 if (linker_init_done == 1) {
1144 IF_DEBUG(linker, debugBelch("initLinker: idempotent return\n"));
1147 linker_init_done = 1;
1150 #if defined(THREADED_RTS) && (defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO))
1151 initMutex(&dl_mutex);
1153 stablehash = allocStrHashTable();
1154 symhash = allocStrHashTable();
1156 /* populate the symbol table with stuff from the RTS */
1157 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
1158 ghciInsertStrHashTable("(GHCi built-in symbols)",
1159 symhash, sym->lbl, sym->addr);
1160 IF_DEBUG(linker, debugBelch("initLinker: inserting rts symbol %s, %p\n", sym->lbl, sym->addr));
1162 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1163 machoInitSymbolsWithoutUnderscore();
1166 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1167 # if defined(RTLD_DEFAULT)
1168 dl_prog_handle = RTLD_DEFAULT;
1170 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
1171 # endif /* RTLD_DEFAULT */
1173 compileResult = regcomp(&re_invalid,
1174 "(([^ \t()])+\\.so([^ \t:()])*):([ \t])*invalid ELF header",
1176 ASSERT( compileResult == 0 );
1177 compileResult = regcomp(&re_realso,
1178 "GROUP *\\( *(([^ )])+)",
1180 ASSERT( compileResult == 0 );
1183 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1184 if (RtsFlags.MiscFlags.linkerMemBase != 0) {
1185 // User-override for mmap_32bit_base
1186 mmap_32bit_base = (void*)RtsFlags.MiscFlags.linkerMemBase;
1190 #if defined(mingw32_HOST_OS)
1192 * These two libraries cause problems when added to the static link,
1193 * but are necessary for resolving symbols in GHCi, hence we load
1194 * them manually here.
1200 IF_DEBUG(linker, debugBelch("initLinker: done\n"));
1205 exitLinker( void ) {
1206 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1207 if (linker_init_done == 1) {
1208 regfree(&re_invalid);
1209 regfree(&re_realso);
1211 closeMutex(&dl_mutex);
1217 /* -----------------------------------------------------------------------------
1218 * Loading DLL or .so dynamic libraries
1219 * -----------------------------------------------------------------------------
1221 * Add a DLL from which symbols may be found. In the ELF case, just
1222 * do RTLD_GLOBAL-style add, so no further messing around needs to
1223 * happen in order that symbols in the loaded .so are findable --
1224 * lookupSymbol() will subsequently see them by dlsym on the program's
1225 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
1227 * In the PEi386 case, open the DLLs and put handles to them in a
1228 * linked list. When looking for a symbol, try all handles in the
1229 * list. This means that we need to load even DLLs that are guaranteed
1230 * to be in the ghc.exe image already, just so we can get a handle
1231 * to give to loadSymbol, so that we can find the symbols. For such
1232 * libraries, the LoadLibrary call should be a no-op except for returning
1237 #if defined(OBJFORMAT_PEi386)
1238 /* A record for storing handles into DLLs. */
1243 struct _OpenedDLL* next;
1248 /* A list thereof. */
1249 static OpenedDLL* opened_dlls = NULL;
1252 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1255 internal_dlopen(const char *dll_name)
1261 // omitted: RTLD_NOW
1262 // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
1264 debugBelch("internal_dlopen: dll_name = '%s'\n", dll_name));
1266 //-------------- Begin critical section ------------------
1267 // This critical section is necessary because dlerror() is not
1268 // required to be reentrant (see POSIX -- IEEE Std 1003.1-2008)
1269 // Also, the error message returned must be copied to preserve it
1272 ACQUIRE_LOCK(&dl_mutex);
1273 hdl = dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
1277 /* dlopen failed; return a ptr to the error msg. */
1279 if (errmsg == NULL) errmsg = "addDLL: unknown error";
1280 errmsg_copy = stgMallocBytes(strlen(errmsg)+1, "addDLL");
1281 strcpy(errmsg_copy, errmsg);
1282 errmsg = errmsg_copy;
1284 RELEASE_LOCK(&dl_mutex);
1285 //--------------- End critical section -------------------
1292 addDLL( char *dll_name )
1294 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1295 /* ------------------- ELF DLL loader ------------------- */
1298 regmatch_t match[NMATCH];
1301 size_t match_length;
1302 #define MAXLINE 1000
1308 IF_DEBUG(linker, debugBelch("addDLL: dll_name = '%s'\n", dll_name));
1309 errmsg = internal_dlopen(dll_name);
1311 if (errmsg == NULL) {
1315 // GHC Trac ticket #2615
1316 // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so)
1317 // contain linker scripts rather than ELF-format object code. This
1318 // code handles the situation by recognizing the real object code
1319 // file name given in the linker script.
1321 // If an "invalid ELF header" error occurs, it is assumed that the
1322 // .so file contains a linker script instead of ELF object code.
1323 // In this case, the code looks for the GROUP ( ... ) linker
1324 // directive. If one is found, the first file name inside the
1325 // parentheses is treated as the name of a dynamic library and the
1326 // code attempts to dlopen that file. If this is also unsuccessful,
1327 // an error message is returned.
1329 // see if the error message is due to an invalid ELF header
1330 IF_DEBUG(linker, debugBelch("errmsg = '%s'\n", errmsg));
1331 result = regexec(&re_invalid, errmsg, (size_t) NMATCH, match, 0);
1332 IF_DEBUG(linker, debugBelch("result = %i\n", result));
1334 // success -- try to read the named file as a linker script
1335 match_length = (size_t) stg_min((match[1].rm_eo - match[1].rm_so),
1337 strncpy(line, (errmsg+(match[1].rm_so)),match_length);
1338 line[match_length] = '\0'; // make sure string is null-terminated
1339 IF_DEBUG(linker, debugBelch ("file name = '%s'\n", line));
1340 if ((fp = fopen(line, "r")) == NULL) {
1341 return errmsg; // return original error if open fails
1343 // try to find a GROUP ( ... ) command
1344 while (fgets(line, MAXLINE, fp) != NULL) {
1345 IF_DEBUG(linker, debugBelch("input line = %s", line));
1346 if (regexec(&re_realso, line, (size_t) NMATCH, match, 0) == 0) {
1347 // success -- try to dlopen the first named file
1348 IF_DEBUG(linker, debugBelch("match%s\n",""));
1349 line[match[1].rm_eo] = '\0';
1350 errmsg = internal_dlopen(line+match[1].rm_so);
1353 // if control reaches here, no GROUP ( ... ) directive was found
1354 // and the original error message is returned to the caller
1360 # elif defined(OBJFORMAT_PEi386)
1361 /* ------------------- Win32 DLL loader ------------------- */
1369 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1371 /* See if we've already got it, and ignore if so. */
1372 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1373 if (0 == strcmp(o_dll->name, dll_name))
1377 /* The file name has no suffix (yet) so that we can try
1378 both foo.dll and foo.drv
1380 The documentation for LoadLibrary says:
1381 If no file name extension is specified in the lpFileName
1382 parameter, the default library extension .dll is
1383 appended. However, the file name string can include a trailing
1384 point character (.) to indicate that the module name has no
1387 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1388 sprintf(buf, "%s.DLL", dll_name);
1389 instance = LoadLibrary(buf);
1390 if (instance == NULL) {
1391 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1392 // KAA: allow loading of drivers (like winspool.drv)
1393 sprintf(buf, "%s.DRV", dll_name);
1394 instance = LoadLibrary(buf);
1395 if (instance == NULL) {
1396 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1397 // #1883: allow loading of unix-style libfoo.dll DLLs
1398 sprintf(buf, "lib%s.DLL", dll_name);
1399 instance = LoadLibrary(buf);
1400 if (instance == NULL) {
1407 /* Add this DLL to the list of DLLs in which to search for symbols. */
1408 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1409 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
1410 strcpy(o_dll->name, dll_name);
1411 o_dll->instance = instance;
1412 o_dll->next = opened_dlls;
1413 opened_dlls = o_dll;
1419 sysErrorBelch(dll_name);
1421 /* LoadLibrary failed; return a ptr to the error msg. */
1422 return "addDLL: could not load DLL";
1425 barf("addDLL: not implemented on this platform");
1429 /* -----------------------------------------------------------------------------
1430 * insert a stable symbol in the hash table
1434 insertStableSymbol(char* obj_name, char* key, StgPtr p)
1436 ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
1440 /* -----------------------------------------------------------------------------
1441 * insert a symbol in the hash table
1444 insertSymbol(char* obj_name, char* key, void* data)
1446 ghciInsertStrHashTable(obj_name, symhash, key, data);
1449 /* -----------------------------------------------------------------------------
1450 * lookup a symbol in the hash table
1453 lookupSymbol( char *lbl )
1456 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s\n", lbl));
1458 ASSERT(symhash != NULL);
1459 val = lookupStrHashTable(symhash, lbl);
1462 IF_DEBUG(linker, debugBelch("lookupSymbol: symbol not found\n"));
1463 # if defined(OBJFORMAT_ELF)
1464 return dlsym(dl_prog_handle, lbl);
1465 # elif defined(OBJFORMAT_MACHO)
1467 /* On OS X 10.3 and later, we use dlsym instead of the old legacy
1470 HACK: On OS X, global symbols are prefixed with an underscore.
1471 However, dlsym wants us to omit the leading underscore from the
1472 symbol name. For now, we simply strip it off here (and ONLY
1475 IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s with dlsym\n", lbl));
1476 ASSERT(lbl[0] == '_');
1477 return dlsym(dl_prog_handle, lbl+1);
1479 if(NSIsSymbolNameDefined(lbl)) {
1480 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1481 return NSAddressOfSymbol(symbol);
1485 # endif /* HAVE_DLFCN_H */
1486 # elif defined(OBJFORMAT_PEi386)
1489 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1490 if (sym != NULL) { return sym; };
1492 // Also try looking up the symbol without the @N suffix. Some
1493 // DLLs have the suffixes on their symbols, some don't.
1494 zapTrailingAtSign ( (unsigned char*)lbl );
1495 sym = lookupSymbolInDLLs((unsigned char*)lbl);
1496 if (sym != NULL) { return sym; };
1504 IF_DEBUG(linker, debugBelch("lookupSymbol: value of %s is %p\n", lbl, val));
1509 /* -----------------------------------------------------------------------------
1510 * Debugging aid: look in GHCi's object symbol tables for symbols
1511 * within DELTA bytes of the specified address, and show their names.
1514 void ghci_enquire ( char* addr );
1516 void ghci_enquire ( char* addr )
1521 const int DELTA = 64;
1526 for (oc = objects; oc; oc = oc->next) {
1527 for (i = 0; i < oc->n_symbols; i++) {
1528 sym = oc->symbols[i];
1529 if (sym == NULL) continue;
1532 a = lookupStrHashTable(symhash, sym);
1535 // debugBelch("ghci_enquire: can't find %s\n", sym);
1537 else if (addr-DELTA <= a && a <= addr+DELTA) {
1538 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1546 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1549 mmapForLinker (size_t bytes, nat flags, int fd)
1551 void *map_addr = NULL;
1554 static nat fixed = 0;
1556 pagesize = getpagesize();
1557 size = ROUND_UP(bytes, pagesize);
1559 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1562 if (mmap_32bit_base != 0) {
1563 map_addr = mmap_32bit_base;
1567 result = mmap(map_addr, size, PROT_EXEC|PROT_READ|PROT_WRITE,
1568 MAP_PRIVATE|TRY_MAP_32BIT|fixed|flags, fd, 0);
1570 if (result == MAP_FAILED) {
1571 sysErrorBelch("mmap %lu bytes at %p",(lnat)size,map_addr);
1572 errorBelch("Try specifying an address with +RTS -xm<addr> -RTS");
1573 stg_exit(EXIT_FAILURE);
1576 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1577 if (mmap_32bit_base != 0) {
1578 if (result == map_addr) {
1579 mmap_32bit_base = (StgWord8*)map_addr + size;
1581 if ((W_)result > 0x80000000) {
1582 // oops, we were given memory over 2Gb
1583 #if defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS)
1584 // Some platforms require MAP_FIXED. This is normally
1585 // a bad idea, because MAP_FIXED will overwrite
1586 // existing mappings.
1587 munmap(result,size);
1591 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);
1594 // hmm, we were given memory somewhere else, but it's
1595 // still under 2Gb so we can use it. Next time, ask
1596 // for memory right after the place we just got some
1597 mmap_32bit_base = (StgWord8*)result + size;
1601 if ((W_)result > 0x80000000) {
1602 // oops, we were given memory over 2Gb
1603 // ... try allocating memory somewhere else?;
1604 debugTrace(DEBUG_linker,"MAP_32BIT didn't work; gave us %lu bytes at 0x%p", bytes, result);
1605 munmap(result, size);
1607 // Set a base address and try again... (guess: 1Gb)
1608 mmap_32bit_base = (void*)0x40000000;
1619 mkOc( char *path, char *image, int imageSize,
1620 char *archiveMemberName
1622 #ifdef darwin_HOST_OS
1629 oc = stgMallocBytes(sizeof(ObjectCode), "loadArchive(oc)");
1631 # if defined(OBJFORMAT_ELF)
1632 oc->formatName = "ELF";
1633 # elif defined(OBJFORMAT_PEi386)
1634 oc->formatName = "PEi386";
1635 # elif defined(OBJFORMAT_MACHO)
1636 oc->formatName = "Mach-O";
1639 barf("loadObj: not implemented on this platform");
1643 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1644 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1645 strcpy(oc->fileName, path);
1647 if (archiveMemberName) {
1648 oc->archiveMemberName = stgMallocBytes( strlen(archiveMemberName)+1, "loadObj" );
1649 strcpy(oc->archiveMemberName, archiveMemberName);
1652 oc->archiveMemberName = NULL;
1655 oc->fileSize = imageSize;
1657 oc->sections = NULL;
1658 oc->proddables = NULL;
1661 #ifdef darwin_HOST_OS
1662 oc->misalignment = misalignment;
1666 /* chain it onto the list of objects */
1674 loadArchive( char *path )
1681 size_t fileNameSize;
1687 IF_DEBUG(linker, debugBelch("loadArchive: Loading archive `%s'\n", path));
1690 file = stgMallocBytes(fileSize, "loadArchive(file)");
1692 f = fopen(path, "rb");
1694 barf("loadObj: can't read `%s'", path);
1696 n = fread ( tmp, 1, 8, f );
1697 if (strncmp(tmp, "!<arch>\n", 8) != 0)
1698 barf("loadArchive: Not an archive: `%s'", path);
1701 n = fread ( file, 1, 16, f );
1707 barf("loadArchive: Failed reading file name from `%s'", path);
1710 n = fread ( tmp, 1, 12, f );
1712 barf("loadArchive: Failed reading mod time from `%s'", path);
1713 n = fread ( tmp, 1, 6, f );
1715 barf("loadArchive: Failed reading owner from `%s'", path);
1716 n = fread ( tmp, 1, 6, f );
1718 barf("loadArchive: Failed reading group from `%s'", path);
1719 n = fread ( tmp, 1, 8, f );
1721 barf("loadArchive: Failed reading mode from `%s'", path);
1722 n = fread ( tmp, 1, 10, f );
1724 barf("loadArchive: Failed reading size from `%s'", path);
1726 for (n = 0; isdigit(tmp[n]); n++);
1728 imageSize = atoi(tmp);
1729 n = fread ( tmp, 1, 2, f );
1730 if (strncmp(tmp, "\x60\x0A", 2) != 0)
1731 barf("loadArchive: Failed reading magic from `%s' at %ld. Got %c%c", path, ftell(f), tmp[0], tmp[1]);
1733 /* Check for BSD-variant large filenames */
1734 if (0 == strncmp(file, "#1/", 3)) {
1736 for (n = 3; isdigit(file[n]); n++);
1738 fileNameSize = atoi(file + 3);
1739 imageSize -= fileNameSize;
1740 if (fileNameSize > fileSize) {
1741 /* Double it to avoid potentially continually
1742 increasing it by 1 */
1743 fileSize = fileNameSize * 2;
1744 file = stgReallocBytes(file, fileSize, "loadArchive(file)");
1746 n = fread ( file, 1, fileNameSize, f );
1747 if (n != (int)fileNameSize)
1748 barf("loadArchive: Failed reading filename from `%s'", path);
1754 IF_DEBUG(linker, debugBelch("loadArchive: Found member file `%s'\n", file));
1757 for (n = 0; n < (int)fileNameSize - 1; n++) {
1758 if ((file[n] == '.') && (file[n + 1] == 'o')) {
1765 char *archiveMemberName;
1767 IF_DEBUG(linker, debugBelch("loadArchive: Member is an object file...loading...\n"));
1769 /* We can't mmap from the archive directly, as object
1770 files need to be 8-byte aligned but files in .ar
1771 archives are 2-byte aligned. When possible we use mmap
1772 to get some anonymous memory, as on 64-bit platforms if
1773 we use malloc then we can be given memory above 2^32.
1774 In the mmap case we're probably wasting lots of space;
1775 we could do better. */
1777 image = mmapForLinker(imageSize, MAP_ANONYMOUS, -1);
1779 image = stgMallocBytes(imageSize, "loadArchive(image)");
1781 n = fread ( image, 1, imageSize, f );
1783 barf("loadObj: error whilst reading `%s'", path);
1785 archiveMemberName = stgMallocBytes(strlen(path) + fileNameSize + 3, "loadArchive(file)");
1786 sprintf(archiveMemberName, "%s(%.*s)", path, (int)fileNameSize, file);
1788 oc = mkOc(path, image, imageSize, archiveMemberName
1790 #ifdef darwin_HOST_OS
1796 stgFree(archiveMemberName);
1798 if (0 == loadOc(oc)) {
1804 n = fseek(f, imageSize, SEEK_CUR);
1806 barf("loadArchive: error whilst seeking by %d in `%s'",
1809 /* .ar files are 2-byte aligned */
1810 if (imageSize % 2) {
1811 n = fread ( tmp, 1, 1, f );
1817 barf("loadArchive: Failed reading padding from `%s'", path);
1829 /* -----------------------------------------------------------------------------
1830 * Load an obj (populate the global symbol table, but don't resolve yet)
1832 * Returns: 1 if ok, 0 on error.
1835 loadObj( char *path )
1847 IF_DEBUG(linker, debugBelch("loadObj %s\n", path));
1851 /* debugBelch("loadObj %s\n", path ); */
1853 /* Check that we haven't already loaded this object.
1854 Ignore requests to load multiple times */
1858 for (o = objects; o; o = o->next) {
1859 if (0 == strcmp(o->fileName, path)) {
1861 break; /* don't need to search further */
1865 IF_DEBUG(linker, debugBelch(
1866 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1867 "same object file twice:\n"
1869 "GHCi will ignore this, but be warned.\n"
1871 return 1; /* success */
1875 r = stat(path, &st);
1877 IF_DEBUG(linker, debugBelch("File doesn't exist\n"));
1881 fileSize = st.st_size;
1884 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1886 #if defined(openbsd_HOST_OS)
1887 fd = open(path, O_RDONLY, S_IRUSR);
1889 fd = open(path, O_RDONLY);
1892 barf("loadObj: can't open `%s'", path);
1894 image = mmapForLinker(fileSize, 0, fd);
1898 #else /* !USE_MMAP */
1899 /* load the image into memory */
1900 f = fopen(path, "rb");
1902 barf("loadObj: can't read `%s'", path);
1904 # if defined(mingw32_HOST_OS)
1905 // TODO: We would like to use allocateExec here, but allocateExec
1906 // cannot currently allocate blocks large enough.
1907 image = VirtualAlloc(NULL, fileSize, MEM_RESERVE | MEM_COMMIT,
1908 PAGE_EXECUTE_READWRITE);
1909 # elif defined(darwin_HOST_OS)
1910 // In a Mach-O .o file, all sections can and will be misaligned
1911 // if the total size of the headers is not a multiple of the
1912 // desired alignment. This is fine for .o files that only serve
1913 // as input for the static linker, but it's not fine for us,
1914 // as SSE (used by gcc for floating point) and Altivec require
1915 // 16-byte alignment.
1916 // We calculate the correct alignment from the header before
1917 // reading the file, and then we misalign image on purpose so
1918 // that the actual sections end up aligned again.
1919 misalignment = machoGetMisalignment(f);
1920 image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
1921 image += misalignment;
1923 image = stgMallocBytes(fileSize, "loadObj(image)");
1928 n = fread ( image, 1, fileSize, f );
1930 barf("loadObj: error whilst reading `%s'", path);
1933 #endif /* USE_MMAP */
1935 oc = mkOc(path, image, fileSize, NULL
1937 #ifdef darwin_HOST_OS
1947 loadOc( ObjectCode* oc ) {
1950 IF_DEBUG(linker, debugBelch("loadOc\n"));
1952 # if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
1953 r = ocAllocateSymbolExtras_MachO ( oc );
1955 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO failed\n"));
1958 # elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
1959 r = ocAllocateSymbolExtras_ELF ( oc );
1961 IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_ELF failed\n"));
1966 /* verify the in-memory image */
1967 # if defined(OBJFORMAT_ELF)
1968 r = ocVerifyImage_ELF ( oc );
1969 # elif defined(OBJFORMAT_PEi386)
1970 r = ocVerifyImage_PEi386 ( oc );
1971 # elif defined(OBJFORMAT_MACHO)
1972 r = ocVerifyImage_MachO ( oc );
1974 barf("loadObj: no verify method");
1977 IF_DEBUG(linker, debugBelch("ocVerifyImage_* failed\n"));
1981 /* build the symbol list for this image */
1982 # if defined(OBJFORMAT_ELF)
1983 r = ocGetNames_ELF ( oc );
1984 # elif defined(OBJFORMAT_PEi386)
1985 r = ocGetNames_PEi386 ( oc );
1986 # elif defined(OBJFORMAT_MACHO)
1987 r = ocGetNames_MachO ( oc );
1989 barf("loadObj: no getNames method");
1992 IF_DEBUG(linker, debugBelch("ocGetNames_* failed\n"));
1996 /* loaded, but not resolved yet */
1997 oc->status = OBJECT_LOADED;
1998 IF_DEBUG(linker, debugBelch("loadObj done.\n"));
2003 /* -----------------------------------------------------------------------------
2004 * resolve all the currently unlinked objects in memory
2006 * Returns: 1 if ok, 0 on error.
2014 IF_DEBUG(linker, debugBelch("resolveObjs: start\n"));
2017 for (oc = objects; oc; oc = oc->next) {
2018 if (oc->status != OBJECT_RESOLVED) {
2019 # if defined(OBJFORMAT_ELF)
2020 r = ocResolve_ELF ( oc );
2021 # elif defined(OBJFORMAT_PEi386)
2022 r = ocResolve_PEi386 ( oc );
2023 # elif defined(OBJFORMAT_MACHO)
2024 r = ocResolve_MachO ( oc );
2026 barf("resolveObjs: not implemented on this platform");
2028 if (!r) { return r; }
2029 oc->status = OBJECT_RESOLVED;
2032 IF_DEBUG(linker, debugBelch("resolveObjs: done\n"));
2036 /* -----------------------------------------------------------------------------
2037 * delete an object from the pool
2040 unloadObj( char *path )
2042 ObjectCode *oc, *prev;
2043 HsBool unloadedAnyObj = HS_BOOL_FALSE;
2045 ASSERT(symhash != NULL);
2046 ASSERT(objects != NULL);
2051 for (oc = objects; oc; prev = oc, oc = oc->next) {
2052 if (!strcmp(oc->fileName,path)) {
2054 /* Remove all the mappings for the symbols within this
2059 for (i = 0; i < oc->n_symbols; i++) {
2060 if (oc->symbols[i] != NULL) {
2061 removeStrHashTable(symhash, oc->symbols[i], NULL);
2069 prev->next = oc->next;
2072 // We're going to leave this in place, in case there are
2073 // any pointers from the heap into it:
2074 // #ifdef mingw32_HOST_OS
2075 // VirtualFree(oc->image);
2077 // stgFree(oc->image);
2079 stgFree(oc->fileName);
2080 stgFree(oc->symbols);
2081 stgFree(oc->sections);
2084 /* This could be a member of an archive so continue
2085 * unloading other members. */
2086 unloadedAnyObj = HS_BOOL_TRUE;
2090 if (unloadedAnyObj) {
2094 errorBelch("unloadObj: can't find `%s' to unload", path);
2099 /* -----------------------------------------------------------------------------
2100 * Sanity checking. For each ObjectCode, maintain a list of address ranges
2101 * which may be prodded during relocation, and abort if we try and write
2102 * outside any of these.
2104 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
2107 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
2108 IF_DEBUG(linker, debugBelch("addProddableBlock %p %p %d\n", oc, start, size));
2112 pb->next = oc->proddables;
2113 oc->proddables = pb;
2116 static void checkProddableBlock ( ObjectCode* oc, void* addr )
2119 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
2120 char* s = (char*)(pb->start);
2121 char* e = s + pb->size - 1;
2122 char* a = (char*)addr;
2123 /* Assumes that the biggest fixup involves a 4-byte write. This
2124 probably needs to be changed to 8 (ie, +7) on 64-bit
2126 if (a >= s && (a+3) <= e) return;
2128 barf("checkProddableBlock: invalid fixup in runtime linker");
2131 /* -----------------------------------------------------------------------------
2132 * Section management.
2134 static void addSection ( ObjectCode* oc, SectionKind kind,
2135 void* start, void* end )
2137 Section* s = stgMallocBytes(sizeof(Section), "addSection");
2141 s->next = oc->sections;
2144 debugBelch("addSection: %p-%p (size %d), kind %d\n",
2145 start, ((char*)end)-1, end - start + 1, kind );
2150 /* --------------------------------------------------------------------------
2152 * This is about allocating a small chunk of memory for every symbol in the
2153 * object file. We make sure that the SymboLExtras are always "in range" of
2154 * limited-range PC-relative instructions on various platforms by allocating
2155 * them right next to the object code itself.
2158 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
2161 ocAllocateSymbolExtras
2163 Allocate additional space at the end of the object file image to make room
2164 for jump islands (powerpc, x86_64) and GOT entries (x86_64).
2166 PowerPC relative branch instructions have a 24 bit displacement field.
2167 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
2168 If a particular imported symbol is outside this range, we have to redirect
2169 the jump to a short piece of new code that just loads the 32bit absolute
2170 address and jumps there.
2171 On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
2174 This function just allocates space for one SymbolExtra for every
2175 undefined symbol in the object file. The code for the jump islands is
2176 filled in by makeSymbolExtra below.
2179 static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
2186 int misalignment = 0;
2187 #ifdef darwin_HOST_OS
2188 misalignment = oc->misalignment;
2194 // round up to the nearest 4
2195 aligned = (oc->fileSize + 3) & ~3;
2198 pagesize = getpagesize();
2199 n = ROUND_UP( oc->fileSize, pagesize );
2200 m = ROUND_UP( aligned + sizeof (SymbolExtra) * count, pagesize );
2202 /* we try to use spare space at the end of the last page of the
2203 * image for the jump islands, but if there isn't enough space
2204 * then we have to map some (anonymously, remembering MAP_32BIT).
2206 if( m > n ) // we need to allocate more pages
2208 oc->symbol_extras = mmapForLinker(sizeof(SymbolExtra) * count,
2213 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2216 oc->image -= misalignment;
2217 oc->image = stgReallocBytes( oc->image,
2219 aligned + sizeof (SymbolExtra) * count,
2220 "ocAllocateSymbolExtras" );
2221 oc->image += misalignment;
2223 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2224 #endif /* USE_MMAP */
2226 memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
2229 oc->symbol_extras = NULL;
2231 oc->first_symbol_extra = first;
2232 oc->n_symbol_extras = count;
2237 static SymbolExtra* makeSymbolExtra( ObjectCode* oc,
2238 unsigned long symbolNumber,
2239 unsigned long target )
2243 ASSERT( symbolNumber >= oc->first_symbol_extra
2244 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
2246 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
2248 #ifdef powerpc_HOST_ARCH
2249 // lis r12, hi16(target)
2250 extra->jumpIsland.lis_r12 = 0x3d80;
2251 extra->jumpIsland.hi_addr = target >> 16;
2253 // ori r12, r12, lo16(target)
2254 extra->jumpIsland.ori_r12_r12 = 0x618c;
2255 extra->jumpIsland.lo_addr = target & 0xffff;
2258 extra->jumpIsland.mtctr_r12 = 0x7d8903a6;
2261 extra->jumpIsland.bctr = 0x4e800420;
2263 #ifdef x86_64_HOST_ARCH
2265 static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
2266 extra->addr = target;
2267 memcpy(extra->jumpIsland, jmp, 6);
2275 /* --------------------------------------------------------------------------
2276 * PowerPC specifics (instruction cache flushing)
2277 * ------------------------------------------------------------------------*/
2279 #ifdef powerpc_HOST_ARCH
2281 ocFlushInstructionCache
2283 Flush the data & instruction caches.
2284 Because the PPC has split data/instruction caches, we have to
2285 do that whenever we modify code at runtime.
2288 static void ocFlushInstructionCache( ObjectCode *oc )
2290 int n = (oc->fileSize + sizeof( SymbolExtra ) * oc->n_symbol_extras + 3) / 4;
2291 unsigned long *p = (unsigned long *) oc->image;
2295 __asm__ volatile ( "dcbf 0,%0\n\t"
2303 __asm__ volatile ( "sync\n\t"
2309 /* --------------------------------------------------------------------------
2310 * PEi386 specifics (Win32 targets)
2311 * ------------------------------------------------------------------------*/
2313 /* The information for this linker comes from
2314 Microsoft Portable Executable
2315 and Common Object File Format Specification
2316 revision 5.1 January 1998
2317 which SimonM says comes from the MS Developer Network CDs.
2319 It can be found there (on older CDs), but can also be found
2322 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
2324 (this is Rev 6.0 from February 1999).
2326 Things move, so if that fails, try searching for it via
2328 http://www.google.com/search?q=PE+COFF+specification
2330 The ultimate reference for the PE format is the Winnt.h
2331 header file that comes with the Platform SDKs; as always,
2332 implementations will drift wrt their documentation.
2334 A good background article on the PE format is Matt Pietrek's
2335 March 1994 article in Microsoft System Journal (MSJ)
2336 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
2337 Win32 Portable Executable File Format." The info in there
2338 has recently been updated in a two part article in
2339 MSDN magazine, issues Feb and March 2002,
2340 "Inside Windows: An In-Depth Look into the Win32 Portable
2341 Executable File Format"
2343 John Levine's book "Linkers and Loaders" contains useful
2348 #if defined(OBJFORMAT_PEi386)
2352 typedef unsigned char UChar;
2353 typedef unsigned short UInt16;
2354 typedef unsigned int UInt32;
2361 UInt16 NumberOfSections;
2362 UInt32 TimeDateStamp;
2363 UInt32 PointerToSymbolTable;
2364 UInt32 NumberOfSymbols;
2365 UInt16 SizeOfOptionalHeader;
2366 UInt16 Characteristics;
2370 #define sizeof_COFF_header 20
2377 UInt32 VirtualAddress;
2378 UInt32 SizeOfRawData;
2379 UInt32 PointerToRawData;
2380 UInt32 PointerToRelocations;
2381 UInt32 PointerToLinenumbers;
2382 UInt16 NumberOfRelocations;
2383 UInt16 NumberOfLineNumbers;
2384 UInt32 Characteristics;
2388 #define sizeof_COFF_section 40
2395 UInt16 SectionNumber;
2398 UChar NumberOfAuxSymbols;
2402 #define sizeof_COFF_symbol 18
2407 UInt32 VirtualAddress;
2408 UInt32 SymbolTableIndex;
2413 #define sizeof_COFF_reloc 10
2416 /* From PE spec doc, section 3.3.2 */
2417 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
2418 windows.h -- for the same purpose, but I want to know what I'm
2420 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
2421 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
2422 #define MYIMAGE_FILE_DLL 0x2000
2423 #define MYIMAGE_FILE_SYSTEM 0x1000
2424 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
2425 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
2426 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
2428 /* From PE spec doc, section 5.4.2 and 5.4.4 */
2429 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
2430 #define MYIMAGE_SYM_CLASS_STATIC 3
2431 #define MYIMAGE_SYM_UNDEFINED 0
2433 /* From PE spec doc, section 4.1 */
2434 #define MYIMAGE_SCN_CNT_CODE 0x00000020
2435 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
2436 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
2438 /* From PE spec doc, section 5.2.1 */
2439 #define MYIMAGE_REL_I386_DIR32 0x0006
2440 #define MYIMAGE_REL_I386_REL32 0x0014
2443 /* We use myindex to calculate array addresses, rather than
2444 simply doing the normal subscript thing. That's because
2445 some of the above structs have sizes which are not
2446 a whole number of words. GCC rounds their sizes up to a
2447 whole number of words, which means that the address calcs
2448 arising from using normal C indexing or pointer arithmetic
2449 are just plain wrong. Sigh.
2452 myindex ( int scale, void* base, int index )
2455 ((UChar*)base) + scale * index;
2460 printName ( UChar* name, UChar* strtab )
2462 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2463 UInt32 strtab_offset = * (UInt32*)(name+4);
2464 debugBelch("%s", strtab + strtab_offset );
2467 for (i = 0; i < 8; i++) {
2468 if (name[i] == 0) break;
2469 debugBelch("%c", name[i] );
2476 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
2478 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2479 UInt32 strtab_offset = * (UInt32*)(name+4);
2480 strncpy ( (char*)dst, (char*)strtab+strtab_offset, dstSize );
2486 if (name[i] == 0) break;
2496 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
2499 /* If the string is longer than 8 bytes, look in the
2500 string table for it -- this will be correctly zero terminated.
2502 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2503 UInt32 strtab_offset = * (UInt32*)(name+4);
2504 return ((UChar*)strtab) + strtab_offset;
2506 /* Otherwise, if shorter than 8 bytes, return the original,
2507 which by defn is correctly terminated.
2509 if (name[7]==0) return name;
2510 /* The annoying case: 8 bytes. Copy into a temporary
2511 (XXX which is never freed ...)
2513 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
2515 strncpy((char*)newstr,(char*)name,8);
2520 /* Getting the name of a section is mildly tricky, so we make a
2521 function for it. Sadly, in one case we have to copy the string
2522 (when it is exactly 8 bytes long there's no trailing '\0'), so for
2523 consistency we *always* copy the string; the caller must free it
2526 cstring_from_section_name (UChar* name, UChar* strtab)
2531 int strtab_offset = strtol((char*)name+1,NULL,10);
2532 int len = strlen(((char*)strtab) + strtab_offset);
2534 newstr = stgMallocBytes(len, "cstring_from_section_symbol_name");
2535 strcpy((char*)newstr, (char*)((UChar*)strtab) + strtab_offset);
2540 newstr = stgMallocBytes(9, "cstring_from_section_symbol_name");
2542 strncpy((char*)newstr,(char*)name,8);
2548 /* Just compares the short names (first 8 chars) */
2549 static COFF_section *
2550 findPEi386SectionCalled ( ObjectCode* oc, UChar* name )
2554 = (COFF_header*)(oc->image);
2555 COFF_section* sectab
2557 ((UChar*)(oc->image))
2558 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2560 for (i = 0; i < hdr->NumberOfSections; i++) {
2563 COFF_section* section_i
2565 myindex ( sizeof_COFF_section, sectab, i );
2566 n1 = (UChar*) &(section_i->Name);
2568 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
2569 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
2570 n1[6]==n2[6] && n1[7]==n2[7])
2579 zapTrailingAtSign ( UChar* sym )
2581 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
2583 if (sym[0] == 0) return;
2585 while (sym[i] != 0) i++;
2588 while (j > 0 && my_isdigit(sym[j])) j--;
2589 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
2594 lookupSymbolInDLLs ( UChar *lbl )
2599 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
2600 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
2602 if (lbl[0] == '_') {
2603 /* HACK: if the name has an initial underscore, try stripping
2604 it off & look that up first. I've yet to verify whether there's
2605 a Rule that governs whether an initial '_' *should always* be
2606 stripped off when mapping from import lib name to the DLL name.
2608 sym = GetProcAddress(o_dll->instance, (char*)(lbl+1));
2610 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
2614 sym = GetProcAddress(o_dll->instance, (char*)lbl);
2616 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
2625 ocVerifyImage_PEi386 ( ObjectCode* oc )
2630 COFF_section* sectab;
2631 COFF_symbol* symtab;
2633 /* debugBelch("\nLOADING %s\n", oc->fileName); */
2634 hdr = (COFF_header*)(oc->image);
2635 sectab = (COFF_section*) (
2636 ((UChar*)(oc->image))
2637 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2639 symtab = (COFF_symbol*) (
2640 ((UChar*)(oc->image))
2641 + hdr->PointerToSymbolTable
2643 strtab = ((UChar*)symtab)
2644 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2646 if (hdr->Machine != 0x14c) {
2647 errorBelch("%s: Not x86 PEi386", oc->fileName);
2650 if (hdr->SizeOfOptionalHeader != 0) {
2651 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
2654 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
2655 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
2656 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
2657 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
2658 errorBelch("%s: Not a PEi386 object file", oc->fileName);
2661 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
2662 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
2663 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
2665 (int)(hdr->Characteristics));
2668 /* If the string table size is way crazy, this might indicate that
2669 there are more than 64k relocations, despite claims to the
2670 contrary. Hence this test. */
2671 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
2673 if ( (*(UInt32*)strtab) > 600000 ) {
2674 /* Note that 600k has no special significance other than being
2675 big enough to handle the almost-2MB-sized lumps that
2676 constitute HSwin32*.o. */
2677 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
2682 /* No further verification after this point; only debug printing. */
2684 IF_DEBUG(linker, i=1);
2685 if (i == 0) return 1;
2687 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
2688 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
2689 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
2692 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
2693 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
2694 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
2695 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
2696 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
2697 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
2698 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
2700 /* Print the section table. */
2702 for (i = 0; i < hdr->NumberOfSections; i++) {
2704 COFF_section* sectab_i
2706 myindex ( sizeof_COFF_section, sectab, i );
2713 printName ( sectab_i->Name, strtab );
2723 sectab_i->VirtualSize,
2724 sectab_i->VirtualAddress,
2725 sectab_i->SizeOfRawData,
2726 sectab_i->PointerToRawData,
2727 sectab_i->NumberOfRelocations,
2728 sectab_i->PointerToRelocations,
2729 sectab_i->PointerToRawData
2731 reltab = (COFF_reloc*) (
2732 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2735 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2736 /* If the relocation field (a short) has overflowed, the
2737 * real count can be found in the first reloc entry.
2739 * See Section 4.1 (last para) of the PE spec (rev6.0).
2741 COFF_reloc* rel = (COFF_reloc*)
2742 myindex ( sizeof_COFF_reloc, reltab, 0 );
2743 noRelocs = rel->VirtualAddress;
2746 noRelocs = sectab_i->NumberOfRelocations;
2750 for (; j < noRelocs; j++) {
2752 COFF_reloc* rel = (COFF_reloc*)
2753 myindex ( sizeof_COFF_reloc, reltab, j );
2755 " type 0x%-4x vaddr 0x%-8x name `",
2757 rel->VirtualAddress );
2758 sym = (COFF_symbol*)
2759 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
2760 /* Hmm..mysterious looking offset - what's it for? SOF */
2761 printName ( sym->Name, strtab -10 );
2768 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
2769 debugBelch("---START of string table---\n");
2770 for (i = 4; i < *(Int32*)strtab; i++) {
2772 debugBelch("\n"); else
2773 debugBelch("%c", strtab[i] );
2775 debugBelch("--- END of string table---\n");
2780 COFF_symbol* symtab_i;
2781 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2782 symtab_i = (COFF_symbol*)
2783 myindex ( sizeof_COFF_symbol, symtab, i );
2789 printName ( symtab_i->Name, strtab );
2798 (Int32)(symtab_i->SectionNumber),
2799 (UInt32)symtab_i->Type,
2800 (UInt32)symtab_i->StorageClass,
2801 (UInt32)symtab_i->NumberOfAuxSymbols
2803 i += symtab_i->NumberOfAuxSymbols;
2813 ocGetNames_PEi386 ( ObjectCode* oc )
2816 COFF_section* sectab;
2817 COFF_symbol* symtab;
2824 hdr = (COFF_header*)(oc->image);
2825 sectab = (COFF_section*) (
2826 ((UChar*)(oc->image))
2827 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2829 symtab = (COFF_symbol*) (
2830 ((UChar*)(oc->image))
2831 + hdr->PointerToSymbolTable
2833 strtab = ((UChar*)(oc->image))
2834 + hdr->PointerToSymbolTable
2835 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2837 /* Allocate space for any (local, anonymous) .bss sections. */
2839 for (i = 0; i < hdr->NumberOfSections; i++) {
2842 COFF_section* sectab_i
2844 myindex ( sizeof_COFF_section, sectab, i );
2846 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
2848 if (0 != strcmp(secname, ".bss")) {
2855 /* sof 10/05: the PE spec text isn't too clear regarding what
2856 * the SizeOfRawData field is supposed to hold for object
2857 * file sections containing just uninitialized data -- for executables,
2858 * it is supposed to be zero; unclear what it's supposed to be
2859 * for object files. However, VirtualSize is guaranteed to be
2860 * zero for object files, which definitely suggests that SizeOfRawData
2861 * will be non-zero (where else would the size of this .bss section be
2862 * stored?) Looking at the COFF_section info for incoming object files,
2863 * this certainly appears to be the case.
2865 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
2866 * object files up until now. This turned out to bite us with ghc-6.4.1's use
2867 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
2868 * variable decls into to the .bss section. (The specific function in Q which
2869 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
2871 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
2872 /* This is a non-empty .bss section. Allocate zeroed space for
2873 it, and set its PointerToRawData field such that oc->image +
2874 PointerToRawData == addr_of_zeroed_space. */
2875 bss_sz = sectab_i->VirtualSize;
2876 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
2877 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
2878 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
2879 addProddableBlock(oc, zspace, bss_sz);
2880 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
2883 /* Copy section information into the ObjectCode. */
2885 for (i = 0; i < hdr->NumberOfSections; i++) {
2891 = SECTIONKIND_OTHER;
2892 COFF_section* sectab_i
2894 myindex ( sizeof_COFF_section, sectab, i );
2896 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
2898 IF_DEBUG(linker, debugBelch("section name = %s\n", secname ));
2901 /* I'm sure this is the Right Way to do it. However, the
2902 alternative of testing the sectab_i->Name field seems to
2903 work ok with Cygwin.
2905 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
2906 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
2907 kind = SECTIONKIND_CODE_OR_RODATA;
2910 if (0==strcmp(".text",(char*)secname) ||
2911 0==strcmp(".rdata",(char*)secname)||
2912 0==strcmp(".rodata",(char*)secname))
2913 kind = SECTIONKIND_CODE_OR_RODATA;
2914 if (0==strcmp(".data",(char*)secname) ||
2915 0==strcmp(".bss",(char*)secname))
2916 kind = SECTIONKIND_RWDATA;
2918 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
2919 sz = sectab_i->SizeOfRawData;
2920 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
2922 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
2923 end = start + sz - 1;
2925 if (kind == SECTIONKIND_OTHER
2926 /* Ignore sections called which contain stabs debugging
2928 && 0 != strcmp(".stab", (char*)secname)
2929 && 0 != strcmp(".stabstr", (char*)secname)
2930 /* ignore constructor section for now */
2931 && 0 != strcmp(".ctors", (char*)secname)
2932 /* ignore section generated from .ident */
2933 && 0!= strncmp(".debug", (char*)secname, 6)
2934 /* ignore unknown section that appeared in gcc 3.4.5(?) */
2935 && 0!= strcmp(".reloc", (char*)secname)
2936 && 0 != strcmp(".rdata$zzz", (char*)secname)
2938 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", secname, oc->fileName);
2943 if (kind != SECTIONKIND_OTHER && end >= start) {
2944 addSection(oc, kind, start, end);
2945 addProddableBlock(oc, start, end - start + 1);
2951 /* Copy exported symbols into the ObjectCode. */
2953 oc->n_symbols = hdr->NumberOfSymbols;
2954 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2955 "ocGetNames_PEi386(oc->symbols)");
2956 /* Call me paranoid; I don't care. */
2957 for (i = 0; i < oc->n_symbols; i++)
2958 oc->symbols[i] = NULL;
2962 COFF_symbol* symtab_i;
2963 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2964 symtab_i = (COFF_symbol*)
2965 myindex ( sizeof_COFF_symbol, symtab, i );
2969 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
2970 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
2971 /* This symbol is global and defined, viz, exported */
2972 /* for MYIMAGE_SYMCLASS_EXTERNAL
2973 && !MYIMAGE_SYM_UNDEFINED,
2974 the address of the symbol is:
2975 address of relevant section + offset in section
2977 COFF_section* sectabent
2978 = (COFF_section*) myindex ( sizeof_COFF_section,
2980 symtab_i->SectionNumber-1 );
2981 addr = ((UChar*)(oc->image))
2982 + (sectabent->PointerToRawData
2986 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
2987 && symtab_i->Value > 0) {
2988 /* This symbol isn't in any section at all, ie, global bss.
2989 Allocate zeroed space for it. */
2990 addr = stgCallocBytes(1, symtab_i->Value,
2991 "ocGetNames_PEi386(non-anonymous bss)");
2992 addSection(oc, SECTIONKIND_RWDATA, addr,
2993 ((UChar*)addr) + symtab_i->Value - 1);
2994 addProddableBlock(oc, addr, symtab_i->Value);
2995 /* debugBelch("BSS section at 0x%x\n", addr); */
2998 if (addr != NULL ) {
2999 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
3000 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
3001 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
3002 ASSERT(i >= 0 && i < oc->n_symbols);
3003 /* cstring_from_COFF_symbol_name always succeeds. */
3004 oc->symbols[i] = (char*)sname;
3005 ghciInsertStrHashTable(oc->fileName, symhash, (char*)sname, addr);
3009 "IGNORING symbol %d\n"
3013 printName ( symtab_i->Name, strtab );
3022 (Int32)(symtab_i->SectionNumber),
3023 (UInt32)symtab_i->Type,
3024 (UInt32)symtab_i->StorageClass,
3025 (UInt32)symtab_i->NumberOfAuxSymbols
3030 i += symtab_i->NumberOfAuxSymbols;
3039 ocResolve_PEi386 ( ObjectCode* oc )
3042 COFF_section* sectab;
3043 COFF_symbol* symtab;
3053 /* ToDo: should be variable-sized? But is at least safe in the
3054 sense of buffer-overrun-proof. */
3056 /* debugBelch("resolving for %s\n", oc->fileName); */
3058 hdr = (COFF_header*)(oc->image);
3059 sectab = (COFF_section*) (
3060 ((UChar*)(oc->image))
3061 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3063 symtab = (COFF_symbol*) (
3064 ((UChar*)(oc->image))
3065 + hdr->PointerToSymbolTable
3067 strtab = ((UChar*)(oc->image))
3068 + hdr->PointerToSymbolTable
3069 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3071 for (i = 0; i < hdr->NumberOfSections; i++) {
3072 COFF_section* sectab_i
3074 myindex ( sizeof_COFF_section, sectab, i );
3077 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
3080 char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3082 /* Ignore sections called which contain stabs debugging
3084 if (0 == strcmp(".stab", (char*)secname)
3085 || 0 == strcmp(".stabstr", (char*)secname)
3086 || 0 == strcmp(".ctors", (char*)secname)
3087 || 0 == strncmp(".debug", (char*)secname, 6)
3088 || 0 == strcmp(".rdata$zzz", (char*)secname)) {
3095 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
3096 /* If the relocation field (a short) has overflowed, the
3097 * real count can be found in the first reloc entry.
3099 * See Section 4.1 (last para) of the PE spec (rev6.0).
3101 * Nov2003 update: the GNU linker still doesn't correctly
3102 * handle the generation of relocatable object files with
3103 * overflown relocations. Hence the output to warn of potential
3106 COFF_reloc* rel = (COFF_reloc*)
3107 myindex ( sizeof_COFF_reloc, reltab, 0 );
3108 noRelocs = rel->VirtualAddress;
3110 /* 10/05: we now assume (and check for) a GNU ld that is capable
3111 * of handling object files with (>2^16) of relocs.
3114 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
3119 noRelocs = sectab_i->NumberOfRelocations;
3124 for (; j < noRelocs; j++) {
3126 COFF_reloc* reltab_j
3128 myindex ( sizeof_COFF_reloc, reltab, j );
3130 /* the location to patch */
3132 ((UChar*)(oc->image))
3133 + (sectab_i->PointerToRawData
3134 + reltab_j->VirtualAddress
3135 - sectab_i->VirtualAddress )
3137 /* the existing contents of pP */
3139 /* the symbol to connect to */
3140 sym = (COFF_symbol*)
3141 myindex ( sizeof_COFF_symbol,
3142 symtab, reltab_j->SymbolTableIndex );
3145 "reloc sec %2d num %3d: type 0x%-4x "
3146 "vaddr 0x%-8x name `",
3148 (UInt32)reltab_j->Type,
3149 reltab_j->VirtualAddress );
3150 printName ( sym->Name, strtab );
3151 debugBelch("'\n" ));
3153 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
3154 COFF_section* section_sym
3155 = findPEi386SectionCalled ( oc, sym->Name );
3157 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
3160 S = ((UInt32)(oc->image))
3161 + (section_sym->PointerToRawData
3164 copyName ( sym->Name, strtab, symbol, 1000-1 );
3165 S = (UInt32) lookupSymbol( (char*)symbol );
3166 if ((void*)S != NULL) goto foundit;
3167 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3171 checkProddableBlock(oc, pP);
3172 switch (reltab_j->Type) {
3173 case MYIMAGE_REL_I386_DIR32:
3176 case MYIMAGE_REL_I386_REL32:
3177 /* Tricky. We have to insert a displacement at
3178 pP which, when added to the PC for the _next_
3179 insn, gives the address of the target (S).
3180 Problem is to know the address of the next insn
3181 when we only know pP. We assume that this
3182 literal field is always the last in the insn,
3183 so that the address of the next insn is pP+4
3184 -- hence the constant 4.
3185 Also I don't know if A should be added, but so
3186 far it has always been zero.
3188 SOF 05/2005: 'A' (old contents of *pP) have been observed
3189 to contain values other than zero (the 'wx' object file
3190 that came with wxhaskell-0.9.4; dunno how it was compiled..).
3191 So, add displacement to old value instead of asserting
3192 A to be zero. Fixes wxhaskell-related crashes, and no other
3193 ill effects have been observed.
3195 Update: the reason why we're seeing these more elaborate
3196 relocations is due to a switch in how the NCG compiles SRTs
3197 and offsets to them from info tables. SRTs live in .(ro)data,
3198 while info tables live in .text, causing GAS to emit REL32/DISP32
3199 relocations with non-zero values. Adding the displacement is
3200 the right thing to do.
3202 *pP = S - ((UInt32)pP) - 4 + A;
3205 debugBelch("%s: unhandled PEi386 relocation type %d",
3206 oc->fileName, reltab_j->Type);
3213 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
3217 #endif /* defined(OBJFORMAT_PEi386) */
3220 /* --------------------------------------------------------------------------
3222 * ------------------------------------------------------------------------*/
3224 #if defined(OBJFORMAT_ELF)
3229 #if defined(sparc_HOST_ARCH)
3230 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
3231 #elif defined(i386_HOST_ARCH)
3232 # define ELF_TARGET_386 /* Used inside <elf.h> */
3233 #elif defined(x86_64_HOST_ARCH)
3234 # define ELF_TARGET_X64_64
3238 #if !defined(openbsd_HOST_OS)
3241 /* openbsd elf has things in different places, with diff names */
3242 # include <elf_abi.h>
3243 # include <machine/reloc.h>
3244 # define R_386_32 RELOC_32
3245 # define R_386_PC32 RELOC_PC32
3248 /* If elf.h doesn't define it */
3249 # ifndef R_X86_64_PC64
3250 # define R_X86_64_PC64 24
3254 * Define a set of types which can be used for both ELF32 and ELF64
3258 #define ELFCLASS ELFCLASS64
3259 #define Elf_Addr Elf64_Addr
3260 #define Elf_Word Elf64_Word
3261 #define Elf_Sword Elf64_Sword
3262 #define Elf_Ehdr Elf64_Ehdr
3263 #define Elf_Phdr Elf64_Phdr
3264 #define Elf_Shdr Elf64_Shdr
3265 #define Elf_Sym Elf64_Sym
3266 #define Elf_Rel Elf64_Rel
3267 #define Elf_Rela Elf64_Rela
3269 #define ELF_ST_TYPE ELF64_ST_TYPE
3272 #define ELF_ST_BIND ELF64_ST_BIND
3275 #define ELF_R_TYPE ELF64_R_TYPE
3278 #define ELF_R_SYM ELF64_R_SYM
3281 #define ELFCLASS ELFCLASS32
3282 #define Elf_Addr Elf32_Addr
3283 #define Elf_Word Elf32_Word
3284 #define Elf_Sword Elf32_Sword
3285 #define Elf_Ehdr Elf32_Ehdr
3286 #define Elf_Phdr Elf32_Phdr
3287 #define Elf_Shdr Elf32_Shdr
3288 #define Elf_Sym Elf32_Sym
3289 #define Elf_Rel Elf32_Rel
3290 #define Elf_Rela Elf32_Rela
3292 #define ELF_ST_TYPE ELF32_ST_TYPE
3295 #define ELF_ST_BIND ELF32_ST_BIND
3298 #define ELF_R_TYPE ELF32_R_TYPE
3301 #define ELF_R_SYM ELF32_R_SYM
3307 * Functions to allocate entries in dynamic sections. Currently we simply
3308 * preallocate a large number, and we don't check if a entry for the given
3309 * target already exists (a linear search is too slow). Ideally these
3310 * entries would be associated with symbols.
3313 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
3314 #define GOT_SIZE 0x20000
3315 #define FUNCTION_TABLE_SIZE 0x10000
3316 #define PLT_SIZE 0x08000
3319 static Elf_Addr got[GOT_SIZE];
3320 static unsigned int gotIndex;
3321 static Elf_Addr gp_val = (Elf_Addr)got;
3324 allocateGOTEntry(Elf_Addr target)
3328 if (gotIndex >= GOT_SIZE)
3329 barf("Global offset table overflow");
3331 entry = &got[gotIndex++];
3333 return (Elf_Addr)entry;
3337 #ifdef ELF_FUNCTION_DESC
3343 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
3344 static unsigned int functionTableIndex;
3347 allocateFunctionDesc(Elf_Addr target)
3349 FunctionDesc *entry;
3351 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
3352 barf("Function table overflow");
3354 entry = &functionTable[functionTableIndex++];
3356 entry->gp = (Elf_Addr)gp_val;
3357 return (Elf_Addr)entry;
3361 copyFunctionDesc(Elf_Addr target)
3363 FunctionDesc *olddesc = (FunctionDesc *)target;
3364 FunctionDesc *newdesc;
3366 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
3367 newdesc->gp = olddesc->gp;
3368 return (Elf_Addr)newdesc;
3375 unsigned char code[sizeof(plt_code)];
3379 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
3381 PLTEntry *plt = (PLTEntry *)oc->plt;
3384 if (oc->pltIndex >= PLT_SIZE)
3385 barf("Procedure table overflow");
3387 entry = &plt[oc->pltIndex++];
3388 memcpy(entry->code, plt_code, sizeof(entry->code));
3389 PLT_RELOC(entry->code, target);
3390 return (Elf_Addr)entry;
3396 return (PLT_SIZE * sizeof(PLTEntry));
3402 * Generic ELF functions
3406 findElfSection ( void* objImage, Elf_Word sh_type )
3408 char* ehdrC = (char*)objImage;
3409 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3410 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3411 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3415 for (i = 0; i < ehdr->e_shnum; i++) {
3416 if (shdr[i].sh_type == sh_type
3417 /* Ignore the section header's string table. */
3418 && i != ehdr->e_shstrndx
3419 /* Ignore string tables named .stabstr, as they contain
3421 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3423 ptr = ehdrC + shdr[i].sh_offset;
3431 ocVerifyImage_ELF ( ObjectCode* oc )
3435 int i, j, nent, nstrtab, nsymtabs;
3439 char* ehdrC = (char*)(oc->image);
3440 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3442 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
3443 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
3444 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
3445 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
3446 errorBelch("%s: not an ELF object", oc->fileName);
3450 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
3451 errorBelch("%s: unsupported ELF format", oc->fileName);
3455 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
3456 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
3458 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
3459 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
3461 errorBelch("%s: unknown endiannness", oc->fileName);
3465 if (ehdr->e_type != ET_REL) {
3466 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
3469 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
3471 IF_DEBUG(linker,debugBelch( "Architecture is " ));
3472 switch (ehdr->e_machine) {
3473 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
3474 #ifdef EM_SPARC32PLUS
3475 case EM_SPARC32PLUS:
3477 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
3479 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
3481 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
3483 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
3484 #elif defined(EM_AMD64)
3485 case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
3487 default: IF_DEBUG(linker,debugBelch( "unknown" ));
3488 errorBelch("%s: unknown architecture (e_machine == %d)"
3489 , oc->fileName, ehdr->e_machine);
3493 IF_DEBUG(linker,debugBelch(
3494 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
3495 (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
3497 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
3499 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3501 if (ehdr->e_shstrndx == SHN_UNDEF) {
3502 errorBelch("%s: no section header string table", oc->fileName);
3505 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
3507 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3510 for (i = 0; i < ehdr->e_shnum; i++) {
3511 IF_DEBUG(linker,debugBelch("%2d: ", i ));
3512 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
3513 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
3514 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
3515 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
3516 ehdrC + shdr[i].sh_offset,
3517 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3519 if (shdr[i].sh_type == SHT_REL) {
3520 IF_DEBUG(linker,debugBelch("Rel " ));
3521 } else if (shdr[i].sh_type == SHT_RELA) {
3522 IF_DEBUG(linker,debugBelch("RelA " ));
3524 IF_DEBUG(linker,debugBelch(" "));
3527 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3531 IF_DEBUG(linker,debugBelch( "\nString tables" ));
3534 for (i = 0; i < ehdr->e_shnum; i++) {
3535 if (shdr[i].sh_type == SHT_STRTAB
3536 /* Ignore the section header's string table. */
3537 && i != ehdr->e_shstrndx
3538 /* Ignore string tables named .stabstr, as they contain
3540 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3542 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
3543 strtab = ehdrC + shdr[i].sh_offset;
3548 errorBelch("%s: no string tables, or too many", oc->fileName);
3553 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
3554 for (i = 0; i < ehdr->e_shnum; i++) {
3555 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3556 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3558 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3559 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3560 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
3562 (long)shdr[i].sh_size % sizeof(Elf_Sym)
3564 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
3565 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
3568 for (j = 0; j < nent; j++) {
3569 IF_DEBUG(linker,debugBelch(" %2d ", j ));
3570 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
3571 (int)stab[j].st_shndx,
3572 (int)stab[j].st_size,
3573 (char*)stab[j].st_value ));
3575 IF_DEBUG(linker,debugBelch("type=" ));
3576 switch (ELF_ST_TYPE(stab[j].st_info)) {
3577 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
3578 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
3579 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
3580 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
3581 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
3582 default: IF_DEBUG(linker,debugBelch("? " )); break;
3584 IF_DEBUG(linker,debugBelch(" " ));
3586 IF_DEBUG(linker,debugBelch("bind=" ));
3587 switch (ELF_ST_BIND(stab[j].st_info)) {
3588 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
3589 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
3590 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
3591 default: IF_DEBUG(linker,debugBelch("? " )); break;
3593 IF_DEBUG(linker,debugBelch(" " ));
3595 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
3599 if (nsymtabs == 0) {
3600 errorBelch("%s: didn't find any symbol tables", oc->fileName);
3607 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
3611 if (hdr->sh_type == SHT_PROGBITS
3612 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
3613 /* .text-style section */
3614 return SECTIONKIND_CODE_OR_RODATA;
3617 if (hdr->sh_type == SHT_PROGBITS
3618 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3619 /* .data-style section */
3620 return SECTIONKIND_RWDATA;
3623 if (hdr->sh_type == SHT_PROGBITS
3624 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
3625 /* .rodata-style section */
3626 return SECTIONKIND_CODE_OR_RODATA;
3629 if (hdr->sh_type == SHT_NOBITS
3630 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3631 /* .bss-style section */
3633 return SECTIONKIND_RWDATA;
3636 return SECTIONKIND_OTHER;
3641 ocGetNames_ELF ( ObjectCode* oc )
3646 char* ehdrC = (char*)(oc->image);
3647 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3648 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
3649 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3651 ASSERT(symhash != NULL);
3654 errorBelch("%s: no strtab", oc->fileName);
3659 for (i = 0; i < ehdr->e_shnum; i++) {
3660 /* Figure out what kind of section it is. Logic derived from
3661 Figure 1.14 ("Special Sections") of the ELF document
3662 ("Portable Formats Specification, Version 1.1"). */
3664 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
3666 if (is_bss && shdr[i].sh_size > 0) {
3667 /* This is a non-empty .bss section. Allocate zeroed space for
3668 it, and set its .sh_offset field such that
3669 ehdrC + .sh_offset == addr_of_zeroed_space. */
3670 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
3671 "ocGetNames_ELF(BSS)");
3672 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
3674 debugBelch("BSS section at 0x%x, size %d\n",
3675 zspace, shdr[i].sh_size);
3679 /* fill in the section info */
3680 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
3681 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
3682 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
3683 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
3686 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3688 /* copy stuff into this module's object symbol table */
3689 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3690 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3692 oc->n_symbols = nent;
3693 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3694 "ocGetNames_ELF(oc->symbols)");
3696 for (j = 0; j < nent; j++) {
3698 char isLocal = FALSE; /* avoids uninit-var warning */
3700 char* nm = strtab + stab[j].st_name;
3701 int secno = stab[j].st_shndx;
3703 /* Figure out if we want to add it; if so, set ad to its
3704 address. Otherwise leave ad == NULL. */
3706 if (secno == SHN_COMMON) {
3708 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
3710 debugBelch("COMMON symbol, size %d name %s\n",
3711 stab[j].st_size, nm);
3713 /* Pointless to do addProddableBlock() for this area,
3714 since the linker should never poke around in it. */
3717 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
3718 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
3720 /* and not an undefined symbol */
3721 && stab[j].st_shndx != SHN_UNDEF
3722 /* and not in a "special section" */
3723 && stab[j].st_shndx < SHN_LORESERVE
3725 /* and it's a not a section or string table or anything silly */
3726 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3727 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3728 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3731 /* Section 0 is the undefined section, hence > and not >=. */
3732 ASSERT(secno > 0 && secno < ehdr->e_shnum);
3734 if (shdr[secno].sh_type == SHT_NOBITS) {
3735 debugBelch(" BSS symbol, size %d off %d name %s\n",
3736 stab[j].st_size, stab[j].st_value, nm);
3739 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3740 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3743 #ifdef ELF_FUNCTION_DESC
3744 /* dlsym() and the initialisation table both give us function
3745 * descriptors, so to be consistent we store function descriptors
3746 * in the symbol table */
3747 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3748 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3750 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n",
3751 ad, oc->fileName, nm ));
3756 /* And the decision is ... */
3760 oc->symbols[j] = nm;
3763 /* Ignore entirely. */
3765 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3769 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3770 strtab + stab[j].st_name ));
3773 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3774 (int)ELF_ST_BIND(stab[j].st_info),
3775 (int)ELF_ST_TYPE(stab[j].st_info),
3776 (int)stab[j].st_shndx,
3777 strtab + stab[j].st_name
3780 oc->symbols[j] = NULL;
3789 /* Do ELF relocations which lack an explicit addend. All x86-linux
3790 relocations appear to be of this form. */
3792 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3793 Elf_Shdr* shdr, int shnum,
3794 Elf_Sym* stab, char* strtab )
3799 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3800 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3801 int target_shndx = shdr[shnum].sh_info;
3802 int symtab_shndx = shdr[shnum].sh_link;
3804 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3805 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3806 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3807 target_shndx, symtab_shndx ));
3809 /* Skip sections that we're not interested in. */
3812 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3813 if (kind == SECTIONKIND_OTHER) {
3814 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
3819 for (j = 0; j < nent; j++) {
3820 Elf_Addr offset = rtab[j].r_offset;
3821 Elf_Addr info = rtab[j].r_info;
3823 Elf_Addr P = ((Elf_Addr)targ) + offset;
3824 Elf_Word* pP = (Elf_Word*)P;
3829 StgStablePtr stablePtr;
3832 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3833 j, (void*)offset, (void*)info ));
3835 IF_DEBUG(linker,debugBelch( " ZERO" ));
3838 Elf_Sym sym = stab[ELF_R_SYM(info)];
3839 /* First see if it is a local symbol. */
3840 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3841 /* Yes, so we can get the address directly from the ELF symbol
3843 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3845 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3846 + stab[ELF_R_SYM(info)].st_value);
3849 symbol = strtab + sym.st_name;
3850 stablePtr = (StgStablePtr)lookupHashTable(stablehash, (StgWord)symbol);
3851 if (NULL == stablePtr) {
3852 /* No, so look up the name in our global table. */
3853 S_tmp = lookupSymbol( symbol );
3854 S = (Elf_Addr)S_tmp;
3856 stableVal = deRefStablePtr( stablePtr );
3858 S = (Elf_Addr)S_tmp;
3862 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3865 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
3868 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
3869 (void*)P, (void*)S, (void*)A ));
3870 checkProddableBlock ( oc, pP );
3874 switch (ELF_R_TYPE(info)) {
3875 # ifdef i386_HOST_ARCH
3876 case R_386_32: *pP = value; break;
3877 case R_386_PC32: *pP = value - P; break;
3880 errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
3881 oc->fileName, (lnat)ELF_R_TYPE(info));
3889 /* Do ELF relocations for which explicit addends are supplied.
3890 sparc-solaris relocations appear to be of this form. */
3892 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
3893 Elf_Shdr* shdr, int shnum,
3894 Elf_Sym* stab, char* strtab )
3897 char *symbol = NULL;
3899 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
3900 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
3901 int target_shndx = shdr[shnum].sh_info;
3902 int symtab_shndx = shdr[shnum].sh_link;
3904 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3905 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
3906 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3907 target_shndx, symtab_shndx ));
3909 for (j = 0; j < nent; j++) {
3910 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
3911 /* This #ifdef only serves to avoid unused-var warnings. */
3912 Elf_Addr offset = rtab[j].r_offset;
3913 Elf_Addr P = targ + offset;
3915 Elf_Addr info = rtab[j].r_info;
3916 Elf_Addr A = rtab[j].r_addend;
3920 # if defined(sparc_HOST_ARCH)
3921 Elf_Word* pP = (Elf_Word*)P;
3923 # elif defined(powerpc_HOST_ARCH)
3927 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
3928 j, (void*)offset, (void*)info,
3931 IF_DEBUG(linker,debugBelch( " ZERO" ));
3934 Elf_Sym sym = stab[ELF_R_SYM(info)];
3935 /* First see if it is a local symbol. */
3936 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3937 /* Yes, so we can get the address directly from the ELF symbol
3939 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3941 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3942 + stab[ELF_R_SYM(info)].st_value);
3943 #ifdef ELF_FUNCTION_DESC
3944 /* Make a function descriptor for this function */
3945 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
3946 S = allocateFunctionDesc(S + A);
3951 /* No, so look up the name in our global table. */
3952 symbol = strtab + sym.st_name;
3953 S_tmp = lookupSymbol( symbol );
3954 S = (Elf_Addr)S_tmp;
3956 #ifdef ELF_FUNCTION_DESC
3957 /* If a function, already a function descriptor - we would
3958 have to copy it to add an offset. */
3959 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
3960 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
3964 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3967 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
3970 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
3971 (void*)P, (void*)S, (void*)A ));
3972 /* checkProddableBlock ( oc, (void*)P ); */
3976 switch (ELF_R_TYPE(info)) {
3977 # if defined(sparc_HOST_ARCH)
3978 case R_SPARC_WDISP30:
3979 w1 = *pP & 0xC0000000;
3980 w2 = (Elf_Word)((value - P) >> 2);
3981 ASSERT((w2 & 0xC0000000) == 0);
3986 w1 = *pP & 0xFFC00000;
3987 w2 = (Elf_Word)(value >> 10);
3988 ASSERT((w2 & 0xFFC00000) == 0);
3994 w2 = (Elf_Word)(value & 0x3FF);
3995 ASSERT((w2 & ~0x3FF) == 0);
4000 /* According to the Sun documentation:
4002 This relocation type resembles R_SPARC_32, except it refers to an
4003 unaligned word. That is, the word to be relocated must be treated
4004 as four separate bytes with arbitrary alignment, not as a word
4005 aligned according to the architecture requirements.
4008 w2 = (Elf_Word)value;
4010 // SPARC doesn't do misaligned writes of 32 bit words,
4011 // so we have to do this one byte-at-a-time.
4012 char *pPc = (char*)pP;
4013 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
4014 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
4015 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
4016 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
4020 w2 = (Elf_Word)value;
4023 # elif defined(powerpc_HOST_ARCH)
4024 case R_PPC_ADDR16_LO:
4025 *(Elf32_Half*) P = value;
4028 case R_PPC_ADDR16_HI:
4029 *(Elf32_Half*) P = value >> 16;
4032 case R_PPC_ADDR16_HA:
4033 *(Elf32_Half*) P = (value + 0x8000) >> 16;
4037 *(Elf32_Word *) P = value;
4041 *(Elf32_Word *) P = value - P;
4047 if( delta << 6 >> 6 != delta )
4049 value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
4053 if( value == 0 || delta << 6 >> 6 != delta )
4055 barf( "Unable to make SymbolExtra for #%d",
4061 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
4062 | (delta & 0x3fffffc);
4066 #if x86_64_HOST_ARCH
4068 *(Elf64_Xword *)P = value;
4073 #if defined(ALWAYS_PIC)
4074 barf("R_X86_64_PC32 relocation, but ALWAYS_PIC.");
4076 StgInt64 off = value - P;
4077 if (off >= 0x7fffffffL || off < -0x80000000L) {
4078 #if X86_64_ELF_NONPIC_HACK
4079 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4081 off = pltAddress + A - P;
4083 barf("R_X86_64_PC32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4084 symbol, off, oc->fileName );
4087 *(Elf64_Word *)P = (Elf64_Word)off;
4094 StgInt64 off = value - P;
4095 *(Elf64_Word *)P = (Elf64_Word)off;
4100 #if defined(ALWAYS_PIC)
4101 barf("R_X86_64_32 relocation, but ALWAYS_PIC.");
4103 if (value >= 0x7fffffffL) {
4104 #if X86_64_ELF_NONPIC_HACK
4105 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4107 value = pltAddress + A;
4109 barf("R_X86_64_32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4110 symbol, value, oc->fileName );
4113 *(Elf64_Word *)P = (Elf64_Word)value;
4118 #if defined(ALWAYS_PIC)
4119 barf("R_X86_64_32S relocation, but ALWAYS_PIC.");
4121 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
4122 #if X86_64_ELF_NONPIC_HACK
4123 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4125 value = pltAddress + A;
4127 barf("R_X86_64_32S relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4128 symbol, value, oc->fileName );
4131 *(Elf64_Sword *)P = (Elf64_Sword)value;
4135 case R_X86_64_GOTPCREL:
4137 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
4138 StgInt64 off = gotAddress + A - P;
4139 *(Elf64_Word *)P = (Elf64_Word)off;
4143 case R_X86_64_PLT32:
4145 #if defined(ALWAYS_PIC)
4146 barf("R_X86_64_PLT32 relocation, but ALWAYS_PIC.");
4148 StgInt64 off = value - P;
4149 if (off >= 0x7fffffffL || off < -0x80000000L) {
4150 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4152 off = pltAddress + A - P;
4154 *(Elf64_Word *)P = (Elf64_Word)off;
4161 errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
4162 oc->fileName, (lnat)ELF_R_TYPE(info));
4171 ocResolve_ELF ( ObjectCode* oc )
4175 Elf_Sym* stab = NULL;
4176 char* ehdrC = (char*)(oc->image);
4177 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
4178 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
4180 /* first find "the" symbol table */
4181 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
4183 /* also go find the string table */
4184 strtab = findElfSection ( ehdrC, SHT_STRTAB );
4186 if (stab == NULL || strtab == NULL) {
4187 errorBelch("%s: can't find string or symbol table", oc->fileName);
4191 /* Process the relocation sections. */
4192 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
4193 if (shdr[shnum].sh_type == SHT_REL) {
4194 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
4195 shnum, stab, strtab );
4199 if (shdr[shnum].sh_type == SHT_RELA) {
4200 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
4201 shnum, stab, strtab );
4206 #if defined(powerpc_HOST_ARCH)
4207 ocFlushInstructionCache( oc );
4214 * PowerPC & X86_64 ELF specifics
4217 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4219 static int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
4225 ehdr = (Elf_Ehdr *) oc->image;
4226 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
4228 for( i = 0; i < ehdr->e_shnum; i++ )
4229 if( shdr[i].sh_type == SHT_SYMTAB )
4232 if( i == ehdr->e_shnum )
4234 errorBelch( "This ELF file contains no symtab" );
4238 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
4240 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
4241 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
4246 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
4249 #endif /* powerpc */
4253 /* --------------------------------------------------------------------------
4255 * ------------------------------------------------------------------------*/
4257 #if defined(OBJFORMAT_MACHO)
4260 Support for MachO linking on Darwin/MacOS X
4261 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
4263 I hereby formally apologize for the hackish nature of this code.
4264 Things that need to be done:
4265 *) implement ocVerifyImage_MachO
4266 *) add still more sanity checks.
4269 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4270 #define mach_header mach_header_64
4271 #define segment_command segment_command_64
4272 #define section section_64
4273 #define nlist nlist_64
4276 #ifdef powerpc_HOST_ARCH
4277 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4279 struct mach_header *header = (struct mach_header *) oc->image;
4280 struct load_command *lc = (struct load_command *) (header + 1);
4283 for( i = 0; i < header->ncmds; i++ )
4285 if( lc->cmd == LC_SYMTAB )
4287 // Find out the first and last undefined external
4288 // symbol, so we don't have to allocate too many
4290 struct symtab_command *symLC = (struct symtab_command *) lc;
4291 unsigned min = symLC->nsyms, max = 0;
4292 struct nlist *nlist =
4293 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
4295 for(i=0;i<symLC->nsyms;i++)
4297 if(nlist[i].n_type & N_STAB)
4299 else if(nlist[i].n_type & N_EXT)
4301 if((nlist[i].n_type & N_TYPE) == N_UNDF
4302 && (nlist[i].n_value == 0))
4312 return ocAllocateSymbolExtras(oc, max - min + 1, min);
4317 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4319 return ocAllocateSymbolExtras(oc,0,0);
4322 #ifdef x86_64_HOST_ARCH
4323 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4325 struct mach_header *header = (struct mach_header *) oc->image;
4326 struct load_command *lc = (struct load_command *) (header + 1);
4329 for( i = 0; i < header->ncmds; i++ )
4331 if( lc->cmd == LC_SYMTAB )
4333 // Just allocate one entry for every symbol
4334 struct symtab_command *symLC = (struct symtab_command *) lc;
4336 return ocAllocateSymbolExtras(oc, symLC->nsyms, 0);
4339 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4341 return ocAllocateSymbolExtras(oc,0,0);
4345 static int ocVerifyImage_MachO(ObjectCode* oc)
4347 char *image = (char*) oc->image;
4348 struct mach_header *header = (struct mach_header*) image;
4350 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4351 if(header->magic != MH_MAGIC_64) {
4352 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4353 oc->fileName, MH_MAGIC_64, header->magic);
4357 if(header->magic != MH_MAGIC) {
4358 errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4359 oc->fileName, MH_MAGIC, header->magic);
4363 // FIXME: do some more verifying here
4367 static int resolveImports(
4370 struct symtab_command *symLC,
4371 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
4372 unsigned long *indirectSyms,
4373 struct nlist *nlist)
4376 size_t itemSize = 4;
4378 IF_DEBUG(linker, debugBelch("resolveImports: start\n"));
4381 int isJumpTable = 0;
4382 if(!strcmp(sect->sectname,"__jump_table"))
4386 ASSERT(sect->reserved2 == itemSize);
4390 for(i=0; i*itemSize < sect->size;i++)
4392 // according to otool, reserved1 contains the first index into the indirect symbol table
4393 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
4394 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4397 IF_DEBUG(linker, debugBelch("resolveImports: resolving %s\n", nm));
4398 if ((symbol->n_type & N_TYPE) == N_UNDF
4399 && (symbol->n_type & N_EXT) && (symbol->n_value != 0)) {
4400 addr = (void*) (symbol->n_value);
4401 IF_DEBUG(linker, debugBelch("resolveImports: undefined external %s has value %p\n", nm, addr));
4403 addr = lookupSymbol(nm);
4404 IF_DEBUG(linker, debugBelch("resolveImports: looking up %s, %p\n", nm, addr));
4408 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
4416 checkProddableBlock(oc,image + sect->offset + i*itemSize);
4417 *(image + sect->offset + i*itemSize) = 0xe9; // jmp
4418 *(unsigned*)(image + sect->offset + i*itemSize + 1)
4419 = (char*)addr - (image + sect->offset + i*itemSize + 5);
4424 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
4425 ((void**)(image + sect->offset))[i] = addr;
4429 IF_DEBUG(linker, debugBelch("resolveImports: done\n"));
4433 static unsigned long relocateAddress(
4436 struct section* sections,
4437 unsigned long address)
4440 IF_DEBUG(linker, debugBelch("relocateAddress: start\n"));
4441 for (i = 0; i < nSections; i++)
4443 IF_DEBUG(linker, debugBelch(" relocating address in section %d\n", i));
4444 if (sections[i].addr <= address
4445 && address < sections[i].addr + sections[i].size)
4447 return (unsigned long)oc->image
4448 + sections[i].offset + address - sections[i].addr;
4451 barf("Invalid Mach-O file:"
4452 "Address out of bounds while relocating object file");
4456 static int relocateSection(
4459 struct symtab_command *symLC, struct nlist *nlist,
4460 int nSections, struct section* sections, struct section *sect)
4462 struct relocation_info *relocs;
4465 IF_DEBUG(linker, debugBelch("relocateSection: start\n"));
4467 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
4469 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
4471 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
4473 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
4477 IF_DEBUG(linker, debugBelch("relocateSection: number of relocations: %d\n", n));
4479 relocs = (struct relocation_info*) (image + sect->reloff);
4483 #ifdef x86_64_HOST_ARCH
4484 struct relocation_info *reloc = &relocs[i];
4486 char *thingPtr = image + sect->offset + reloc->r_address;
4488 /* We shouldn't need to initialise this, but gcc on OS X 64 bit
4489 complains that it may be used uninitialized if we don't */
4492 int type = reloc->r_type;
4494 checkProddableBlock(oc,thingPtr);
4495 switch(reloc->r_length)
4498 thing = *(uint8_t*)thingPtr;
4499 baseValue = (uint64_t)thingPtr + 1;
4502 thing = *(uint16_t*)thingPtr;
4503 baseValue = (uint64_t)thingPtr + 2;
4506 thing = *(uint32_t*)thingPtr;
4507 baseValue = (uint64_t)thingPtr + 4;
4510 thing = *(uint64_t*)thingPtr;
4511 baseValue = (uint64_t)thingPtr + 8;
4514 barf("Unknown size.");
4518 debugBelch("relocateSection: length = %d, thing = %d, baseValue = %p\n",
4519 reloc->r_length, thing, baseValue));
4521 if (type == X86_64_RELOC_GOT
4522 || type == X86_64_RELOC_GOT_LOAD)
4524 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4525 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4527 IF_DEBUG(linker, debugBelch("relocateSection: making jump island for %s, extern = %d, X86_64_RELOC_GOT\n", nm, reloc->r_extern));
4528 ASSERT(reloc->r_extern);
4529 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, (unsigned long)lookupSymbol(nm))->addr;
4531 type = X86_64_RELOC_SIGNED;
4533 else if(reloc->r_extern)
4535 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4536 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4538 IF_DEBUG(linker, debugBelch("relocateSection: looking up external symbol %s\n", nm));
4539 IF_DEBUG(linker, debugBelch(" : type = %d\n", symbol->n_type));
4540 IF_DEBUG(linker, debugBelch(" : sect = %d\n", symbol->n_sect));
4541 IF_DEBUG(linker, debugBelch(" : desc = %d\n", symbol->n_desc));
4542 IF_DEBUG(linker, debugBelch(" : value = %d\n", symbol->n_value));
4543 if ((symbol->n_type & N_TYPE) == N_SECT) {
4544 value = relocateAddress(oc, nSections, sections,
4546 IF_DEBUG(linker, debugBelch("relocateSection, defined external symbol %s, relocated address %p\n", nm, value));
4549 value = (uint64_t) lookupSymbol(nm);
4550 IF_DEBUG(linker, debugBelch("relocateSection: external symbol %s, address %p\n", nm, value));
4555 value = sections[reloc->r_symbolnum-1].offset
4556 - sections[reloc->r_symbolnum-1].addr
4560 IF_DEBUG(linker, debugBelch("relocateSection: value = %p\n", value));
4562 if (type == X86_64_RELOC_BRANCH)
4564 if((int32_t)(value - baseValue) != (int64_t)(value - baseValue))
4566 ASSERT(reloc->r_extern);
4567 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, value)
4570 ASSERT((int32_t)(value - baseValue) == (int64_t)(value - baseValue));
4571 type = X86_64_RELOC_SIGNED;
4576 case X86_64_RELOC_UNSIGNED:
4577 ASSERT(!reloc->r_pcrel);
4580 case X86_64_RELOC_SIGNED:
4581 case X86_64_RELOC_SIGNED_1:
4582 case X86_64_RELOC_SIGNED_2:
4583 case X86_64_RELOC_SIGNED_4:
4584 ASSERT(reloc->r_pcrel);
4585 thing += value - baseValue;
4587 case X86_64_RELOC_SUBTRACTOR:
4588 ASSERT(!reloc->r_pcrel);
4592 barf("unkown relocation");
4595 switch(reloc->r_length)
4598 *(uint8_t*)thingPtr = thing;
4601 *(uint16_t*)thingPtr = thing;
4604 *(uint32_t*)thingPtr = thing;
4607 *(uint64_t*)thingPtr = thing;
4611 if(relocs[i].r_address & R_SCATTERED)
4613 struct scattered_relocation_info *scat =
4614 (struct scattered_relocation_info*) &relocs[i];
4618 if(scat->r_length == 2)
4620 unsigned long word = 0;
4621 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
4622 checkProddableBlock(oc,wordPtr);
4624 // Note on relocation types:
4625 // i386 uses the GENERIC_RELOC_* types,
4626 // while ppc uses special PPC_RELOC_* types.
4627 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
4628 // in both cases, all others are different.
4629 // Therefore, we use GENERIC_RELOC_VANILLA
4630 // and GENERIC_RELOC_PAIR instead of the PPC variants,
4631 // and use #ifdefs for the other types.
4633 // Step 1: Figure out what the relocated value should be
4634 if(scat->r_type == GENERIC_RELOC_VANILLA)
4636 word = *wordPtr + (unsigned long) relocateAddress(
4643 #ifdef powerpc_HOST_ARCH
4644 else if(scat->r_type == PPC_RELOC_SECTDIFF
4645 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
4646 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
4647 || scat->r_type == PPC_RELOC_HA16_SECTDIFF
4648 || scat->r_type == PPC_RELOC_LOCAL_SECTDIFF)
4650 else if(scat->r_type == GENERIC_RELOC_SECTDIFF
4651 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4654 struct scattered_relocation_info *pair =
4655 (struct scattered_relocation_info*) &relocs[i+1];
4657 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
4658 barf("Invalid Mach-O file: "
4659 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
4661 word = (unsigned long)
4662 (relocateAddress(oc, nSections, sections, scat->r_value)
4663 - relocateAddress(oc, nSections, sections, pair->r_value));
4666 #ifdef powerpc_HOST_ARCH
4667 else if(scat->r_type == PPC_RELOC_HI16
4668 || scat->r_type == PPC_RELOC_LO16
4669 || scat->r_type == PPC_RELOC_HA16
4670 || scat->r_type == PPC_RELOC_LO14)
4671 { // these are generated by label+offset things
4672 struct relocation_info *pair = &relocs[i+1];
4673 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
4674 barf("Invalid Mach-O file: "
4675 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
4677 if(scat->r_type == PPC_RELOC_LO16)
4679 word = ((unsigned short*) wordPtr)[1];
4680 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4682 else if(scat->r_type == PPC_RELOC_LO14)
4684 barf("Unsupported Relocation: PPC_RELOC_LO14");
4685 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
4686 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4688 else if(scat->r_type == PPC_RELOC_HI16)
4690 word = ((unsigned short*) wordPtr)[1] << 16;
4691 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4693 else if(scat->r_type == PPC_RELOC_HA16)
4695 word = ((unsigned short*) wordPtr)[1] << 16;
4696 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4700 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
4708 barf ("Don't know how to handle this Mach-O "
4709 "scattered relocation entry: "
4710 "object file %s; entry type %ld; "
4712 OC_INFORMATIVE_FILENAME(oc),
4718 #ifdef powerpc_HOST_ARCH
4719 if(scat->r_type == GENERIC_RELOC_VANILLA
4720 || scat->r_type == PPC_RELOC_SECTDIFF)
4722 if(scat->r_type == GENERIC_RELOC_VANILLA
4723 || scat->r_type == GENERIC_RELOC_SECTDIFF
4724 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4729 #ifdef powerpc_HOST_ARCH
4730 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
4732 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4734 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
4736 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4738 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
4740 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4741 + ((word & (1<<15)) ? 1 : 0);
4747 barf("Can't handle Mach-O scattered relocation entry "
4748 "with this r_length tag: "
4749 "object file %s; entry type %ld; "
4750 "r_length tag %ld; address %#lx\n",
4751 OC_INFORMATIVE_FILENAME(oc),
4758 else /* scat->r_pcrel */
4760 barf("Don't know how to handle *PC-relative* Mach-O "
4761 "scattered relocation entry: "
4762 "object file %s; entry type %ld; address %#lx\n",
4763 OC_INFORMATIVE_FILENAME(oc),
4770 else /* !(relocs[i].r_address & R_SCATTERED) */
4772 struct relocation_info *reloc = &relocs[i];
4773 if(reloc->r_pcrel && !reloc->r_extern)
4776 if(reloc->r_length == 2)
4778 unsigned long word = 0;
4779 #ifdef powerpc_HOST_ARCH
4780 unsigned long jumpIsland = 0;
4781 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
4782 // to avoid warning and to catch
4786 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
4787 checkProddableBlock(oc,wordPtr);
4789 if(reloc->r_type == GENERIC_RELOC_VANILLA)
4793 #ifdef powerpc_HOST_ARCH
4794 else if(reloc->r_type == PPC_RELOC_LO16)
4796 word = ((unsigned short*) wordPtr)[1];
4797 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4799 else if(reloc->r_type == PPC_RELOC_HI16)
4801 word = ((unsigned short*) wordPtr)[1] << 16;
4802 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4804 else if(reloc->r_type == PPC_RELOC_HA16)
4806 word = ((unsigned short*) wordPtr)[1] << 16;
4807 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4809 else if(reloc->r_type == PPC_RELOC_BR24)
4812 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
4817 barf("Can't handle this Mach-O relocation entry "
4819 "object file %s; entry type %ld; address %#lx\n",
4820 OC_INFORMATIVE_FILENAME(oc),
4826 if(!reloc->r_extern)
4829 sections[reloc->r_symbolnum-1].offset
4830 - sections[reloc->r_symbolnum-1].addr
4837 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4838 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4839 void *symbolAddress = lookupSymbol(nm);
4842 errorBelch("\nunknown symbol `%s'", nm);
4848 #ifdef powerpc_HOST_ARCH
4849 // In the .o file, this should be a relative jump to NULL
4850 // and we'll change it to a relative jump to the symbol
4851 ASSERT(word + reloc->r_address == 0);
4852 jumpIsland = (unsigned long)
4853 &makeSymbolExtra(oc,
4855 (unsigned long) symbolAddress)
4859 offsetToJumpIsland = word + jumpIsland
4860 - (((long)image) + sect->offset - sect->addr);
4863 word += (unsigned long) symbolAddress
4864 - (((long)image) + sect->offset - sect->addr);
4868 word += (unsigned long) symbolAddress;
4872 if(reloc->r_type == GENERIC_RELOC_VANILLA)
4877 #ifdef powerpc_HOST_ARCH
4878 else if(reloc->r_type == PPC_RELOC_LO16)
4880 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4883 else if(reloc->r_type == PPC_RELOC_HI16)
4885 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4888 else if(reloc->r_type == PPC_RELOC_HA16)
4890 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4891 + ((word & (1<<15)) ? 1 : 0);
4894 else if(reloc->r_type == PPC_RELOC_BR24)
4896 if((word & 0x03) != 0)
4897 barf("%s: unconditional relative branch with a displacement "
4898 "which isn't a multiple of 4 bytes: %#lx",
4899 OC_INFORMATIVE_FILENAME(oc),
4902 if((word & 0xFE000000) != 0xFE000000 &&
4903 (word & 0xFE000000) != 0x00000000)
4905 // The branch offset is too large.
4906 // Therefore, we try to use a jump island.
4909 barf("%s: unconditional relative branch out of range: "
4910 "no jump island available: %#lx",
4911 OC_INFORMATIVE_FILENAME(oc),
4915 word = offsetToJumpIsland;
4916 if((word & 0xFE000000) != 0xFE000000 &&
4917 (word & 0xFE000000) != 0x00000000)
4918 barf("%s: unconditional relative branch out of range: "
4919 "jump island out of range: %#lx",
4920 OC_INFORMATIVE_FILENAME(oc),
4923 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
4930 barf("Can't handle Mach-O relocation entry (not scattered) "
4931 "with this r_length tag: "
4932 "object file %s; entry type %ld; "
4933 "r_length tag %ld; address %#lx\n",
4934 OC_INFORMATIVE_FILENAME(oc),
4943 IF_DEBUG(linker, debugBelch("relocateSection: done\n"));
4947 static int ocGetNames_MachO(ObjectCode* oc)
4949 char *image = (char*) oc->image;
4950 struct mach_header *header = (struct mach_header*) image;
4951 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4952 unsigned i,curSymbol = 0;
4953 struct segment_command *segLC = NULL;
4954 struct section *sections;
4955 struct symtab_command *symLC = NULL;
4956 struct nlist *nlist;
4957 unsigned long commonSize = 0;
4958 char *commonStorage = NULL;
4959 unsigned long commonCounter;
4961 IF_DEBUG(linker,debugBelch("ocGetNames_MachO: start\n"));
4963 for(i=0;i<header->ncmds;i++)
4965 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
4966 segLC = (struct segment_command*) lc;
4967 else if(lc->cmd == LC_SYMTAB)
4968 symLC = (struct symtab_command*) lc;
4969 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4972 sections = (struct section*) (segLC+1);
4973 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4977 barf("ocGetNames_MachO: no segment load command");
4979 for(i=0;i<segLC->nsects;i++)
4981 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: segment %d\n", i));
4982 if (sections[i].size == 0)
4985 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
4987 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
4988 "ocGetNames_MachO(common symbols)");
4989 sections[i].offset = zeroFillArea - image;
4992 if(!strcmp(sections[i].sectname,"__text"))
4993 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
4994 (void*) (image + sections[i].offset),
4995 (void*) (image + sections[i].offset + sections[i].size));
4996 else if(!strcmp(sections[i].sectname,"__const"))
4997 addSection(oc, SECTIONKIND_RWDATA,
4998 (void*) (image + sections[i].offset),
4999 (void*) (image + sections[i].offset + sections[i].size));
5000 else if(!strcmp(sections[i].sectname,"__data"))
5001 addSection(oc, SECTIONKIND_RWDATA,
5002 (void*) (image + sections[i].offset),
5003 (void*) (image + sections[i].offset + sections[i].size));
5004 else if(!strcmp(sections[i].sectname,"__bss")
5005 || !strcmp(sections[i].sectname,"__common"))
5006 addSection(oc, SECTIONKIND_RWDATA,
5007 (void*) (image + sections[i].offset),
5008 (void*) (image + sections[i].offset + sections[i].size));
5010 addProddableBlock(oc, (void*) (image + sections[i].offset),
5014 // count external symbols defined here
5018 for(i=0;i<symLC->nsyms;i++)
5020 if(nlist[i].n_type & N_STAB)
5022 else if(nlist[i].n_type & N_EXT)
5024 if((nlist[i].n_type & N_TYPE) == N_UNDF
5025 && (nlist[i].n_value != 0))
5027 commonSize += nlist[i].n_value;
5030 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5035 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: %d external symbols\n", oc->n_symbols));
5036 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
5037 "ocGetNames_MachO(oc->symbols)");
5041 for(i=0;i<symLC->nsyms;i++)
5043 if(nlist[i].n_type & N_STAB)
5045 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5047 if(nlist[i].n_type & N_EXT)
5049 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5050 if ((nlist[i].n_desc & N_WEAK_DEF) && lookupSymbol(nm)) {
5051 // weak definition, and we already have a definition
5052 IF_DEBUG(linker, debugBelch(" weak: %s\n", nm));
5056 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting %s\n", nm));
5057 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5059 + sections[nlist[i].n_sect-1].offset
5060 - sections[nlist[i].n_sect-1].addr
5061 + nlist[i].n_value);
5062 oc->symbols[curSymbol++] = nm;
5069 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
5070 commonCounter = (unsigned long)commonStorage;
5073 for(i=0;i<symLC->nsyms;i++)
5075 if((nlist[i].n_type & N_TYPE) == N_UNDF
5076 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
5078 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5079 unsigned long sz = nlist[i].n_value;
5081 nlist[i].n_value = commonCounter;
5083 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting common symbol: %s\n", nm));
5084 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5085 (void*)commonCounter);
5086 oc->symbols[curSymbol++] = nm;
5088 commonCounter += sz;
5095 static int ocResolve_MachO(ObjectCode* oc)
5097 char *image = (char*) oc->image;
5098 struct mach_header *header = (struct mach_header*) image;
5099 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5101 struct segment_command *segLC = NULL;
5102 struct section *sections;
5103 struct symtab_command *symLC = NULL;
5104 struct dysymtab_command *dsymLC = NULL;
5105 struct nlist *nlist;
5107 IF_DEBUG(linker, debugBelch("ocResolve_MachO: start\n"));
5108 for (i = 0; i < header->ncmds; i++)
5110 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
5111 segLC = (struct segment_command*) lc;
5112 else if(lc->cmd == LC_SYMTAB)
5113 symLC = (struct symtab_command*) lc;
5114 else if(lc->cmd == LC_DYSYMTAB)
5115 dsymLC = (struct dysymtab_command*) lc;
5116 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5119 sections = (struct section*) (segLC+1);
5120 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5125 unsigned long *indirectSyms
5126 = (unsigned long*) (image + dsymLC->indirectsymoff);
5128 IF_DEBUG(linker, debugBelch("ocResolve_MachO: resolving dsymLC\n"));
5129 for (i = 0; i < segLC->nsects; i++)
5131 if( !strcmp(sections[i].sectname,"__la_symbol_ptr")
5132 || !strcmp(sections[i].sectname,"__la_sym_ptr2")
5133 || !strcmp(sections[i].sectname,"__la_sym_ptr3"))
5135 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5138 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr")
5139 || !strcmp(sections[i].sectname,"__pointers"))
5141 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5144 else if(!strcmp(sections[i].sectname,"__jump_table"))
5146 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
5151 IF_DEBUG(linker, debugBelch("ocResolve_MachO: unknown section\n"));
5156 for(i=0;i<segLC->nsects;i++)
5158 IF_DEBUG(linker, debugBelch("ocResolve_MachO: relocating section %d\n", i));
5160 if (!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
5164 #if defined (powerpc_HOST_ARCH)
5165 ocFlushInstructionCache( oc );
5171 #ifdef powerpc_HOST_ARCH
5173 * The Mach-O object format uses leading underscores. But not everywhere.
5174 * There is a small number of runtime support functions defined in
5175 * libcc_dynamic.a whose name does not have a leading underscore.
5176 * As a consequence, we can't get their address from C code.
5177 * We have to use inline assembler just to take the address of a function.
5181 extern void* symbolsWithoutUnderscore[];
5183 static void machoInitSymbolsWithoutUnderscore()
5185 void **p = symbolsWithoutUnderscore;
5186 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
5188 #undef SymI_NeedsProto
5189 #define SymI_NeedsProto(x) \
5190 __asm__ volatile(".long " # x);
5192 RTS_MACHO_NOUNDERLINE_SYMBOLS
5194 __asm__ volatile(".text");
5196 #undef SymI_NeedsProto
5197 #define SymI_NeedsProto(x) \
5198 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
5200 RTS_MACHO_NOUNDERLINE_SYMBOLS
5202 #undef SymI_NeedsProto
5208 * Figure out by how much to shift the entire Mach-O file in memory
5209 * when loading so that its single segment ends up 16-byte-aligned
5211 static int machoGetMisalignment( FILE * f )
5213 struct mach_header header;
5216 fread(&header, sizeof(header), 1, f);
5219 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
5220 if(header.magic != MH_MAGIC_64) {
5221 errorBelch("Bad magic. Expected: %08x, got: %08x.\n",
5222 MH_MAGIC_64, header->magic);
5226 if(header.magic != MH_MAGIC) {
5227 errorBelch("Bad magic. Expected: %08x, got: %08x.\n",
5228 MH_MAGIC, header->magic);
5233 misalignment = (header.sizeofcmds + sizeof(header))
5236 return misalignment ? (16 - misalignment) : 0;