Linker.c 151 KB
Newer Older
1 2
/* -----------------------------------------------------------------------------
 *
3
 * (c) The GHC Team, 2000-2004
4 5 6 7 8
 *
 * RTS Object Linker
 *
 * ---------------------------------------------------------------------------*/

sof's avatar
sof committed
9
#if 0
10
#include "PosixSource.h"
sof's avatar
sof committed
11
#endif
12

13
/* Linux needs _GNU_SOURCE to get RTLD_DEFAULT from <dlfcn.h> and
14 15
   MREMAP_MAYMOVE from <sys/mman.h>.
 */
16 17 18 19
#ifdef __linux__
#define _GNU_SOURCE
#endif

20 21 22 23 24
#include "Rts.h"
#include "RtsFlags.h"
#include "HsFFI.h"
#include "Hash.h"
#include "Linker.h"
25
#include "LinkerInternals.h"
26
#include "RtsUtils.h"
27
#include "Schedule.h"
28
#include "Sparks.h"
ei@vuokko.info's avatar
ei@vuokko.info committed
29
#include "RtsTypeable.h"
30
#include "Timer.h"
31

32
#ifdef HAVE_SYS_TYPES_H
33
#include <sys/types.h>
34 35
#endif

36 37 38
#include <stdlib.h>
#include <string.h>

39
#ifdef HAVE_SYS_STAT_H
40
#include <sys/stat.h>
41
#endif
42

43
#if defined(HAVE_DLFCN_H)
44
#include <dlfcn.h>
45
#endif
46

47
#if defined(cygwin32_HOST_OS)
sof's avatar
sof committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <regex.h>
#include <sys/fcntl.h>
#include <sys/termios.h>
#include <sys/utime.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#endif

63
#if defined(ia64_HOST_ARCH) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
64 65 66
#define USE_MMAP
#include <fcntl.h>
#include <sys/mman.h>
dons's avatar
dons committed
67

68
#if defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
dons's avatar
dons committed
69 70 71 72 73
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif

74 75
#endif

76
#if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
77
#  define OBJFORMAT_ELF
78
#elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
79
#  define OBJFORMAT_PEi386
80
#  include <windows.h>
sof's avatar
sof committed
81
#  include <math.h>
82
#elif defined(darwin_HOST_OS)
83 84 85 86
#  define OBJFORMAT_MACHO
#  include <mach-o/loader.h>
#  include <mach-o/nlist.h>
#  include <mach-o/reloc.h>
87
#if !defined(HAVE_DLFCN_H)
88
#  include <mach-o/dyld.h>
89
#endif
90 91 92
#if defined(powerpc_HOST_ARCH)
#  include <mach-o/ppc/reloc.h>
#endif
93 94 95
#if defined(x86_64_HOST_ARCH)
#  include <mach-o/x86_64/reloc.h>
#endif
96 97
#endif

98
/* Hash table mapping symbol names to Symbol */
99
static /*Str*/HashTable *symhash;
100

101 102 103
/* Hash table mapping symbol names to StgStablePtr */
static /*Str*/HashTable *stablehash;

104 105 106
/* List of currently loaded objects */
ObjectCode *objects = NULL;	/* initially empty */

107
#if defined(OBJFORMAT_ELF)
108 109 110
static int ocVerifyImage_ELF    ( ObjectCode* oc );
static int ocGetNames_ELF       ( ObjectCode* oc );
static int ocResolve_ELF        ( ObjectCode* oc );
111
#if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
112
static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
113
#endif
114
#elif defined(OBJFORMAT_PEi386)
115 116 117
static int ocVerifyImage_PEi386 ( ObjectCode* oc );
static int ocGetNames_PEi386    ( ObjectCode* oc );
static int ocResolve_PEi386     ( ObjectCode* oc );
118 119 120 121
#elif defined(OBJFORMAT_MACHO)
static int ocVerifyImage_MachO    ( ObjectCode* oc );
static int ocGetNames_MachO       ( ObjectCode* oc );
static int ocResolve_MachO        ( ObjectCode* oc );
122

123
static int machoGetMisalignment( FILE * );
124 125 126
#if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
#endif
127
#ifdef powerpc_HOST_ARCH
128
static void machoInitSymbolsWithoutUnderscore( void );
129
#endif
130
#endif
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* on x86_64 we have a problem with relocating symbol references in
 * code that was compiled without -fPIC.  By default, the small memory
 * model is used, which assumes that symbol references can fit in a
 * 32-bit slot.  The system dynamic linker makes this work for
 * references to shared libraries by either (a) allocating a jump
 * table slot for code references, or (b) moving the symbol at load
 * time (and copying its contents, if necessary) for data references.
 *
 * We unfortunately can't tell whether symbol references are to code
 * or data.  So for now we assume they are code (the vast majority
 * are), and allocate jump-table slots.  Unfortunately this will
 * SILENTLY generate crashing code for data references.  This hack is
 * enabled by X86_64_ELF_NONPIC_HACK.
 * 
 * One workaround is to use shared Haskell libraries.  This is
 * coming.  Another workaround is to keep the static libraries but
 * compile them with -fPIC, because that will generate PIC references
 * to data which can be relocated.  The PIC code is still too green to
 * do this systematically, though.
 *
 * See bug #781
 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
154 155 156 157 158 159 160 161 162 163 164 165
 *
 * Naming Scheme for Symbol Macros
 *
 * SymI_*: symbol is internal to the RTS. It resides in an object
 *         file/library that is statically.
 * SymE_*: symbol is external to the RTS library. It might be linked
 *         dynamically.
 *
 * Sym*_HasProto  : the symbol prototype is imported in an include file
 *                  or defined explicitly
 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
 *                  default proto extern void sym(void);
166 167
 */
#define X86_64_ELF_NONPIC_HACK 1
168

169 170 171 172
/* -----------------------------------------------------------------------------
 * Built-in symbols from the RTS
 */

173 174 175 176 177
typedef struct _RtsSymbolVal {
    char   *lbl;
    void   *addr;
} RtsSymbolVal;

178
#if !defined(PAR)
179 180 181
#define Maybe_Stable_Names      SymI_HasProto(mkWeakzh_fast)			\
      				SymI_HasProto(makeStableNamezh_fast)		\
      				SymI_HasProto(finalizzeWeakzh_fast)
182 183 184 185
#else
/* These are not available in GUM!!! -- HWL */
#define Maybe_Stable_Names
#endif
186

187
#if !defined (mingw32_HOST_OS)
188
#define RTS_POSIX_ONLY_SYMBOLS                  \
189 190 191 192 193 194
      SymI_HasProto(shutdownHaskellAndSignal)	\
      SymI_NeedsProto(lockFile)                 \
      SymI_NeedsProto(unlockFile)               \
      SymI_HasProto(signal_handlers)		\
      SymI_HasProto(stg_sig_install)		\
      SymI_NeedsProto(nocldstop)
sof's avatar
sof committed
195
#endif
196

197
#if defined (cygwin32_HOST_OS)
sof's avatar
sof committed
198 199 200
#define RTS_MINGW_ONLY_SYMBOLS /**/
/* Don't have the ability to read import libs / archives, so
 * we have to stupidly list a lot of what libcygwin.a
201
 * exports; sigh.
sof's avatar
sof committed
202
 */
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#define RTS_CYGWIN_ONLY_SYMBOLS                          \
      SymI_HasProto(regfree)                             \
      SymI_HasProto(regexec)                             \
      SymI_HasProto(regerror)                            \
      SymI_HasProto(regcomp)                             \
      SymI_HasProto(__errno)                             \
      SymI_HasProto(access)                              \
      SymI_HasProto(chmod)                               \
      SymI_HasProto(chdir)                               \
      SymI_HasProto(close)                               \
      SymI_HasProto(creat)                               \
      SymI_HasProto(dup)                                 \
      SymI_HasProto(dup2)                                \
      SymI_HasProto(fstat)                               \
      SymI_HasProto(fcntl)                               \
      SymI_HasProto(getcwd)                              \
      SymI_HasProto(getenv)                              \
      SymI_HasProto(lseek)                               \
      SymI_HasProto(open)                                \
      SymI_HasProto(fpathconf)                           \
      SymI_HasProto(pathconf)                            \
      SymI_HasProto(stat)                                \
      SymI_HasProto(pow)                                 \
      SymI_HasProto(tanh)                                \
      SymI_HasProto(cosh)                                \
      SymI_HasProto(sinh)                                \
      SymI_HasProto(atan)                                \
      SymI_HasProto(acos)                                \
      SymI_HasProto(asin)                                \
      SymI_HasProto(tan)                                 \
      SymI_HasProto(cos)                                 \
      SymI_HasProto(sin)                                 \
      SymI_HasProto(exp)                                 \
      SymI_HasProto(log)                                 \
      SymI_HasProto(sqrt)                                \
      SymI_HasProto(localtime_r)                         \
      SymI_HasProto(gmtime_r)                            \
      SymI_HasProto(mktime)                              \
      SymI_NeedsProto(_imp___tzname)                     \
      SymI_HasProto(gettimeofday)                        \
      SymI_HasProto(timezone)                            \
      SymI_HasProto(tcgetattr)                           \
      SymI_HasProto(tcsetattr)                           \
      SymI_HasProto(memcpy)                              \
      SymI_HasProto(memmove)                             \
      SymI_HasProto(realloc)                             \
      SymI_HasProto(malloc)                              \
      SymI_HasProto(free)                                \
      SymI_HasProto(fork)                                \
      SymI_HasProto(lstat)                               \
      SymI_HasProto(isatty)                              \
      SymI_HasProto(mkdir)                               \
      SymI_HasProto(opendir)                             \
      SymI_HasProto(readdir)                             \
      SymI_HasProto(rewinddir)                           \
      SymI_HasProto(closedir)                            \
      SymI_HasProto(link)                                \
      SymI_HasProto(mkfifo)                              \
      SymI_HasProto(pipe)                                \
      SymI_HasProto(read)                                \
      SymI_HasProto(rename)                              \
      SymI_HasProto(rmdir)                               \
      SymI_HasProto(select)                              \
      SymI_HasProto(system)                              \
      SymI_HasProto(write)                               \
      SymI_HasProto(strcmp)                              \
      SymI_HasProto(strcpy)                              \
      SymI_HasProto(strncpy)                             \
      SymI_HasProto(strerror)                            \
      SymI_HasProto(sigaddset)                           \
      SymI_HasProto(sigemptyset)                         \
      SymI_HasProto(sigprocmask)                         \
      SymI_HasProto(umask)                               \
      SymI_HasProto(uname)                               \
      SymI_HasProto(unlink)                              \
      SymI_HasProto(utime)                               \
      SymI_HasProto(waitpid)
280

281
#elif !defined(mingw32_HOST_OS)
sof's avatar
sof committed
282 283
#define RTS_MINGW_ONLY_SYMBOLS /**/
#define RTS_CYGWIN_ONLY_SYMBOLS /**/
284
#else /* defined(mingw32_HOST_OS) */
sof's avatar
sof committed
285 286
#define RTS_POSIX_ONLY_SYMBOLS  /**/
#define RTS_CYGWIN_ONLY_SYMBOLS /**/
287

288 289 290
/* Extra syms gen'ed by mingw-2's gcc-3.2: */
#if __GNUC__>=3
#define RTS_MINGW_EXTRA_SYMS                    \
291 292
      SymI_NeedsProto(_imp____mb_cur_max)       \
      SymI_NeedsProto(_imp___pctype)
293 294 295 296
#else
#define RTS_MINGW_EXTRA_SYMS
#endif

297
#if HAVE_GETTIMEOFDAY
298
#define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
299 300 301 302
#else
#define RTS_MINGW_GETTIMEOFDAY_SYM /**/
#endif

303 304
/* These are statically linked from the mingw libraries into the ghc
   executable, so we have to employ this hack. */
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
#define RTS_MINGW_ONLY_SYMBOLS                           \
      SymI_HasProto(asyncReadzh_fast)			 \
      SymI_HasProto(asyncWritezh_fast)			 \
      SymI_HasProto(asyncDoProczh_fast)			 \
      SymI_HasProto(memset)                              \
      SymI_HasProto(inet_ntoa)                           \
      SymI_HasProto(inet_addr)                           \
      SymI_HasProto(htonl)                               \
      SymI_HasProto(recvfrom)                            \
      SymI_HasProto(listen)                              \
      SymI_HasProto(bind)                                \
      SymI_HasProto(shutdown)                            \
      SymI_HasProto(connect)                             \
      SymI_HasProto(htons)                               \
      SymI_HasProto(ntohs)                               \
      SymI_HasProto(getservbyname)                       \
      SymI_HasProto(getservbyport)                       \
      SymI_HasProto(getprotobynumber)                    \
      SymI_HasProto(getprotobyname)                      \
      SymI_HasProto(gethostbyname)                       \
      SymI_HasProto(gethostbyaddr)                       \
      SymI_HasProto(gethostname)                         \
      SymI_HasProto(strcpy)                              \
      SymI_HasProto(strncpy)                             \
      SymI_HasProto(abort)                               \
      SymI_NeedsProto(_alloca)                           \
      SymI_NeedsProto(isxdigit)                          \
      SymI_NeedsProto(isupper)                           \
      SymI_NeedsProto(ispunct)                           \
      SymI_NeedsProto(islower)                           \
      SymI_NeedsProto(isspace)                           \
      SymI_NeedsProto(isprint)                           \
      SymI_NeedsProto(isdigit)                           \
      SymI_NeedsProto(iscntrl)                           \
      SymI_NeedsProto(isalpha)                           \
      SymI_NeedsProto(isalnum)                           \
      SymI_HasProto(strcmp)                              \
      SymI_HasProto(memmove)                             \
      SymI_HasProto(realloc)                             \
      SymI_HasProto(malloc)                              \
      SymI_HasProto(pow)                                 \
      SymI_HasProto(tanh)                                \
      SymI_HasProto(cosh)                                \
      SymI_HasProto(sinh)                                \
      SymI_HasProto(atan)                                \
      SymI_HasProto(acos)                                \
      SymI_HasProto(asin)                                \
      SymI_HasProto(tan)                                 \
      SymI_HasProto(cos)                                 \
      SymI_HasProto(sin)                                 \
      SymI_HasProto(exp)                                 \
      SymI_HasProto(log)                                 \
      SymI_HasProto(sqrt)                                \
      SymI_HasProto(powf)                                \
      SymI_HasProto(tanhf)                               \
      SymI_HasProto(coshf)                               \
      SymI_HasProto(sinhf)                               \
      SymI_HasProto(atanf)                               \
      SymI_HasProto(acosf)                               \
      SymI_HasProto(asinf)                               \
      SymI_HasProto(tanf)                                \
      SymI_HasProto(cosf)                                \
      SymI_HasProto(sinf)                                \
      SymI_HasProto(expf)                                \
      SymI_HasProto(logf)                                \
      SymI_HasProto(sqrtf)                               \
      SymI_HasProto(memcpy)                              \
      SymI_HasProto(rts_InstallConsoleEvent)             \
      SymI_HasProto(rts_ConsoleHandlerDone)              \
      SymI_NeedsProto(mktime)                            \
      SymI_NeedsProto(_imp___timezone)                   \
      SymI_NeedsProto(_imp___tzname)                     \
      SymI_NeedsProto(_imp__tzname)                      \
      SymI_NeedsProto(_imp___iob)                        \
      SymI_NeedsProto(_imp___osver)                      \
      SymI_NeedsProto(localtime)                         \
      SymI_NeedsProto(gmtime)                            \
      SymI_NeedsProto(opendir)                           \
      SymI_NeedsProto(readdir)                           \
      SymI_NeedsProto(rewinddir)                         \
      RTS_MINGW_EXTRA_SYMS                               \
      RTS_MINGW_GETTIMEOFDAY_SYM		         \
      SymI_NeedsProto(closedir)
388
#endif
389

390
#if defined(darwin_TARGET_OS) && HAVE_PRINTF_LDBLSTUB
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
#define RTS_DARWIN_ONLY_SYMBOLS			            \
     SymI_NeedsProto(asprintf$LDBLStub)                     \
     SymI_NeedsProto(err$LDBLStub)                          \
     SymI_NeedsProto(errc$LDBLStub)                         \
     SymI_NeedsProto(errx$LDBLStub)                         \
     SymI_NeedsProto(fprintf$LDBLStub)                      \
     SymI_NeedsProto(fscanf$LDBLStub)                       \
     SymI_NeedsProto(fwprintf$LDBLStub)                     \
     SymI_NeedsProto(fwscanf$LDBLStub)                      \
     SymI_NeedsProto(printf$LDBLStub)                       \
     SymI_NeedsProto(scanf$LDBLStub)                        \
     SymI_NeedsProto(snprintf$LDBLStub)                     \
     SymI_NeedsProto(sprintf$LDBLStub)                      \
     SymI_NeedsProto(sscanf$LDBLStub)                       \
     SymI_NeedsProto(strtold$LDBLStub)                      \
     SymI_NeedsProto(swprintf$LDBLStub)                     \
     SymI_NeedsProto(swscanf$LDBLStub)                      \
     SymI_NeedsProto(syslog$LDBLStub)                       \
     SymI_NeedsProto(vasprintf$LDBLStub)                    \
     SymI_NeedsProto(verr$LDBLStub)                         \
     SymI_NeedsProto(verrc$LDBLStub)                        \
     SymI_NeedsProto(verrx$LDBLStub)                        \
     SymI_NeedsProto(vfprintf$LDBLStub)                     \
     SymI_NeedsProto(vfscanf$LDBLStub)                      \
     SymI_NeedsProto(vfwprintf$LDBLStub)                    \
     SymI_NeedsProto(vfwscanf$LDBLStub)                     \
     SymI_NeedsProto(vprintf$LDBLStub)                      \
     SymI_NeedsProto(vscanf$LDBLStub)                       \
     SymI_NeedsProto(vsnprintf$LDBLStub)                    \
     SymI_NeedsProto(vsprintf$LDBLStub)                     \
     SymI_NeedsProto(vsscanf$LDBLStub)                      \
     SymI_NeedsProto(vswprintf$LDBLStub)                    \
     SymI_NeedsProto(vswscanf$LDBLStub)                     \
     SymI_NeedsProto(vsyslog$LDBLStub)                      \
     SymI_NeedsProto(vwarn$LDBLStub)                        \
     SymI_NeedsProto(vwarnc$LDBLStub)                       \
     SymI_NeedsProto(vwarnx$LDBLStub)                       \
     SymI_NeedsProto(vwprintf$LDBLStub)                     \
     SymI_NeedsProto(vwscanf$LDBLStub)                      \
     SymI_NeedsProto(warn$LDBLStub)                         \
     SymI_NeedsProto(warnc$LDBLStub)                        \
     SymI_NeedsProto(warnx$LDBLStub)                        \
     SymI_NeedsProto(wcstold$LDBLStub)                      \
     SymI_NeedsProto(wprintf$LDBLStub)                      \
     SymI_NeedsProto(wscanf$LDBLStub)
436 437 438 439
#else
#define RTS_DARWIN_ONLY_SYMBOLS
#endif

sof's avatar
sof committed
440
#ifndef SMP
441
# define MAIN_CAP_SYM SymI_HasProto(MainCapability)
sof's avatar
sof committed
442 443 444
#else
# define MAIN_CAP_SYM
#endif
445

sof's avatar
sof committed
446 447
#if !defined(mingw32_HOST_OS)
#define RTS_USER_SIGNALS_SYMBOLS \
448
   SymI_HasProto(setIOManagerPipe)
sof's avatar
sof committed
449
#else
450 451 452 453 454
#define RTS_USER_SIGNALS_SYMBOLS     \
   SymI_HasProto(sendIOManagerEvent) \
   SymI_HasProto(readIOManagerEvent) \
   SymI_HasProto(getIOManagerEvent)  \
   SymI_HasProto(console_handler)
sof's avatar
sof committed
455 456
#endif

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
#define RTS_LIBFFI_SYMBOLS                                  \
     SymE_NeedsProto(ffi_prep_cif)                          \
     SymE_NeedsProto(ffi_call)                              \
     SymE_NeedsProto(ffi_type_void)                         \
     SymE_NeedsProto(ffi_type_float)                        \
     SymE_NeedsProto(ffi_type_double)                       \
     SymE_NeedsProto(ffi_type_sint64)                       \
     SymE_NeedsProto(ffi_type_uint64)                       \
     SymE_NeedsProto(ffi_type_sint32)                       \
     SymE_NeedsProto(ffi_type_uint32)                       \
     SymE_NeedsProto(ffi_type_sint16)                       \
     SymE_NeedsProto(ffi_type_uint16)                       \
     SymE_NeedsProto(ffi_type_sint8)                        \
     SymE_NeedsProto(ffi_type_uint8)                        \
     SymE_NeedsProto(ffi_type_pointer)
472

473 474 475
#ifdef TABLES_NEXT_TO_CODE
#define RTS_RET_SYMBOLS /* nothing */
#else
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
#define RTS_RET_SYMBOLS 		                \
      SymI_HasProto(stg_enter_ret)			\
      SymI_HasProto(stg_gc_fun_ret)			\
      SymI_HasProto(stg_ap_v_ret)			\
      SymI_HasProto(stg_ap_f_ret)			\
      SymI_HasProto(stg_ap_d_ret)			\
      SymI_HasProto(stg_ap_l_ret)			\
      SymI_HasProto(stg_ap_n_ret)			\
      SymI_HasProto(stg_ap_p_ret)			\
      SymI_HasProto(stg_ap_pv_ret)			\
      SymI_HasProto(stg_ap_pp_ret)			\
      SymI_HasProto(stg_ap_ppv_ret)			\
      SymI_HasProto(stg_ap_ppp_ret)			\
      SymI_HasProto(stg_ap_pppv_ret)			\
      SymI_HasProto(stg_ap_pppp_ret)			\
      SymI_HasProto(stg_ap_ppppp_ret)			\
      SymI_HasProto(stg_ap_pppppp_ret)
493 494
#endif

495 496
/* On Windows, we link libgmp.a statically into libHSrts.dll */
#ifdef mingw32_HOST_OS
497 498 499 500 501 502
#define GMP_SYMS				        \
      SymI_HasProto(__gmpz_cmp)				\
      SymI_HasProto(__gmpz_cmp_si)			\
      SymI_HasProto(__gmpz_cmp_ui)			\
      SymI_HasProto(__gmpz_get_si)			\
      SymI_HasProto(__gmpz_get_ui)
503
#else
504 505 506 507 508 509
#define GMP_SYMS				        \
      SymE_HasProto(__gmpz_cmp)			        \
      SymE_HasProto(__gmpz_cmp_si)			\
      SymE_HasProto(__gmpz_cmp_ui)			\
      SymE_HasProto(__gmpz_get_si)			\
      SymE_HasProto(__gmpz_get_ui)
510 511
#endif

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
#define RTS_SYMBOLS				        \
      Maybe_Stable_Names			        \
      SymI_HasProto(StgReturn)				\
      SymI_HasProto(stg_enter_info)			\
      SymI_HasProto(stg_gc_void_info)			\
      SymI_HasProto(__stg_gc_enter_1)			\
      SymI_HasProto(stg_gc_noregs)			\
      SymI_HasProto(stg_gc_unpt_r1_info)		\
      SymI_HasProto(stg_gc_unpt_r1)			\
      SymI_HasProto(stg_gc_unbx_r1_info)		\
      SymI_HasProto(stg_gc_unbx_r1)			\
      SymI_HasProto(stg_gc_f1_info)			\
      SymI_HasProto(stg_gc_f1)				\
      SymI_HasProto(stg_gc_d1_info)			\
      SymI_HasProto(stg_gc_d1)				\
      SymI_HasProto(stg_gc_l1_info)			\
      SymI_HasProto(stg_gc_l1)				\
      SymI_HasProto(__stg_gc_fun)			\
      SymI_HasProto(stg_gc_fun_info)			\
      SymI_HasProto(stg_gc_gen)				\
      SymI_HasProto(stg_gc_gen_info)			\
      SymI_HasProto(stg_gc_gen_hp)			\
      SymI_HasProto(stg_gc_ut)				\
      SymI_HasProto(stg_gen_yield)			\
      SymI_HasProto(stg_yield_noregs)			\
      SymI_HasProto(stg_yield_to_interpreter)		\
      SymI_HasProto(stg_gen_block)			\
      SymI_HasProto(stg_block_noregs)			\
      SymI_HasProto(stg_block_1)			\
      SymI_HasProto(stg_block_takemvar)			\
      SymI_HasProto(stg_block_putmvar)			\
      MAIN_CAP_SYM                                      \
      SymI_HasProto(MallocFailHook)			\
      SymI_HasProto(OnExitHook)				\
      SymI_HasProto(OutOfHeapHook)			\
      SymI_HasProto(StackOverflowHook)			\
      SymI_HasProto(__encodeDouble)			\
      SymI_HasProto(__encodeFloat)			\
      SymI_HasProto(addDLL)               		\
      GMP_SYMS					        \
      SymI_HasProto(__int_encodeDouble)			\
      SymI_HasProto(__word_encodeDouble)		\
      SymI_HasProto(__2Int_encodeDouble)		\
      SymI_HasProto(__int_encodeFloat)			\
      SymI_HasProto(__word_encodeFloat)			\
      SymI_HasProto(andIntegerzh_fast)			\
      SymI_HasProto(atomicallyzh_fast)			\
      SymI_HasProto(barf)				\
      SymI_HasProto(debugBelch)				\
      SymI_HasProto(errorBelch)				\
      SymI_HasProto(asyncExceptionsBlockedzh_fast)	\
      SymI_HasProto(blockAsyncExceptionszh_fast)	\
      SymI_HasProto(catchzh_fast)			\
      SymI_HasProto(catchRetryzh_fast)			\
      SymI_HasProto(catchSTMzh_fast)			\
      SymI_HasProto(checkzh_fast)                       \
      SymI_HasProto(closure_flags)                      \
      SymI_HasProto(cmp_thread)				\
      SymI_HasProto(cmpIntegerzh_fast)	        	\
      SymI_HasProto(cmpIntegerIntzh_fast)	      	\
      SymI_HasProto(complementIntegerzh_fast)		\
      SymI_HasProto(createAdjustor)			\
      SymI_HasProto(decodeDoublezh_fast)		\
      SymI_HasProto(decodeFloatzh_fast)			\
      SymI_HasProto(decodeDoublezu2Intzh_fast)		\
      SymI_HasProto(decodeFloatzuIntzh_fast)		\
      SymI_HasProto(defaultsHook)			\
      SymI_HasProto(delayzh_fast)			\
      SymI_HasProto(deRefWeakzh_fast)			\
      SymI_HasProto(deRefStablePtrzh_fast)		\
      SymI_HasProto(dirty_MUT_VAR)			\
      SymI_HasProto(divExactIntegerzh_fast)		\
      SymI_HasProto(divModIntegerzh_fast)		\
      SymI_HasProto(forkzh_fast)			\
      SymI_HasProto(forkOnzh_fast)			\
      SymI_HasProto(forkProcess)			\
      SymI_HasProto(forkOS_createThread)		\
      SymI_HasProto(freeHaskellFunctionPtr)		\
      SymI_HasProto(freeStablePtr)		        \
      SymI_HasProto(getOrSetTypeableStore)		\
      SymI_HasProto(gcdIntegerzh_fast)			\
      SymI_HasProto(gcdIntegerIntzh_fast)		\
      SymI_HasProto(gcdIntzh_fast)			\
      SymI_HasProto(genSymZh)				\
      SymI_HasProto(genericRaise)			\
      SymI_HasProto(getProgArgv)			\
      SymI_HasProto(getFullProgArgv)			\
      SymI_HasProto(getStablePtr)			\
      SymI_HasProto(hs_init)				\
      SymI_HasProto(hs_exit)				\
      SymI_HasProto(hs_set_argv)			\
      SymI_HasProto(hs_add_root)			\
      SymI_HasProto(hs_perform_gc)			\
      SymI_HasProto(hs_free_stable_ptr)			\
      SymI_HasProto(hs_free_fun_ptr)			\
      SymI_HasProto(hs_hpc_rootModule)		        \
      SymI_HasProto(initLinker)				\
      SymI_HasProto(unpackClosurezh_fast)               \
      SymI_HasProto(getApStackValzh_fast)               \
611
      SymI_HasProto(getSparkzh_fast)                    \
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
      SymI_HasProto(int2Integerzh_fast)			\
      SymI_HasProto(integer2Intzh_fast)			\
      SymI_HasProto(integer2Wordzh_fast)		\
      SymI_HasProto(isCurrentThreadBoundzh_fast)	\
      SymI_HasProto(isDoubleDenormalized)		\
      SymI_HasProto(isDoubleInfinite)			\
      SymI_HasProto(isDoubleNaN)			\
      SymI_HasProto(isDoubleNegativeZero)		\
      SymI_HasProto(isEmptyMVarzh_fast)			\
      SymI_HasProto(isFloatDenormalized)		\
      SymI_HasProto(isFloatInfinite)			\
      SymI_HasProto(isFloatNaN)				\
      SymI_HasProto(isFloatNegativeZero)		\
      SymI_HasProto(killThreadzh_fast)			\
      SymI_HasProto(loadObj)          			\
      SymI_HasProto(insertStableSymbol) 		\
      SymI_HasProto(insertSymbol)     			\
      SymI_HasProto(lookupSymbol)     			\
      SymI_HasProto(makeStablePtrzh_fast)		\
      SymI_HasProto(minusIntegerzh_fast)		\
      SymI_HasProto(mkApUpd0zh_fast)			\
      SymI_HasProto(myThreadIdzh_fast)			\
      SymI_HasProto(labelThreadzh_fast)                 \
      SymI_HasProto(newArrayzh_fast)			\
      SymI_HasProto(newBCOzh_fast)			\
      SymI_HasProto(newByteArrayzh_fast)		\
      SymI_HasProto_redirect(newCAF, newDynCAF)		\
      SymI_HasProto(newMVarzh_fast)			\
      SymI_HasProto(newMutVarzh_fast)			\
      SymI_HasProto(newTVarzh_fast)			\
      SymI_HasProto(noDuplicatezh_fast)			\
      SymI_HasProto(atomicModifyMutVarzh_fast)		\
      SymI_HasProto(newPinnedByteArrayzh_fast)		\
      SymI_HasProto(newSpark)				\
      SymI_HasProto(orIntegerzh_fast)			\
      SymI_HasProto(performGC)				\
      SymI_HasProto(performMajorGC)			\
      SymI_HasProto(plusIntegerzh_fast)			\
      SymI_HasProto(prog_argc)				\
      SymI_HasProto(prog_argv)				\
      SymI_HasProto(putMVarzh_fast)			\
      SymI_HasProto(quotIntegerzh_fast)			\
      SymI_HasProto(quotRemIntegerzh_fast)		\
      SymI_HasProto(raisezh_fast)			\
      SymI_HasProto(raiseIOzh_fast)			\
      SymI_HasProto(readTVarzh_fast)			\
      SymI_HasProto(readTVarIOzh_fast)			\
      SymI_HasProto(remIntegerzh_fast)			\
      SymI_HasProto(resetNonBlockingFd)			\
      SymI_HasProto(resumeThread)			\
      SymI_HasProto(resolveObjs)                        \
      SymI_HasProto(retryzh_fast)                       \
      SymI_HasProto(rts_apply)				\
      SymI_HasProto(rts_checkSchedStatus)		\
      SymI_HasProto(rts_eval)				\
      SymI_HasProto(rts_evalIO)				\
      SymI_HasProto(rts_evalLazyIO)			\
      SymI_HasProto(rts_evalStableIO)			\
      SymI_HasProto(rts_eval_)				\
      SymI_HasProto(rts_getBool)			\
      SymI_HasProto(rts_getChar)			\
      SymI_HasProto(rts_getDouble)			\
      SymI_HasProto(rts_getFloat)			\
      SymI_HasProto(rts_getInt)				\
      SymI_HasProto(rts_getInt8)			\
      SymI_HasProto(rts_getInt16)			\
      SymI_HasProto(rts_getInt32)			\
      SymI_HasProto(rts_getInt64)			\
      SymI_HasProto(rts_getPtr)				\
      SymI_HasProto(rts_getFunPtr)			\
      SymI_HasProto(rts_getStablePtr)			\
      SymI_HasProto(rts_getThreadId)			\
      SymI_HasProto(rts_getWord)			\
      SymI_HasProto(rts_getWord8)			\
      SymI_HasProto(rts_getWord16)			\
      SymI_HasProto(rts_getWord32)			\
      SymI_HasProto(rts_getWord64)			\
      SymI_HasProto(rts_lock)				\
      SymI_HasProto(rts_mkBool)				\
      SymI_HasProto(rts_mkChar)				\
      SymI_HasProto(rts_mkDouble)			\
      SymI_HasProto(rts_mkFloat)			\
      SymI_HasProto(rts_mkInt)				\
      SymI_HasProto(rts_mkInt8)				\
      SymI_HasProto(rts_mkInt16)			\
      SymI_HasProto(rts_mkInt32)			\
      SymI_HasProto(rts_mkInt64)			\
      SymI_HasProto(rts_mkPtr)				\
      SymI_HasProto(rts_mkFunPtr)			\
      SymI_HasProto(rts_mkStablePtr)			\
      SymI_HasProto(rts_mkString)			\
      SymI_HasProto(rts_mkWord)				\
      SymI_HasProto(rts_mkWord8)			\
      SymI_HasProto(rts_mkWord16)			\
      SymI_HasProto(rts_mkWord32)			\
      SymI_HasProto(rts_mkWord64)			\
      SymI_HasProto(rts_unlock)				\
      SymI_HasProto(rtsSupportsBoundThreads)		\
      SymI_HasProto(__hscore_get_saved_termios)		\
      SymI_HasProto(__hscore_set_saved_termios)		\
      SymI_HasProto(setProgArgv)			\
      SymI_HasProto(startupHaskell)			\
      SymI_HasProto(shutdownHaskell)			\
      SymI_HasProto(shutdownHaskellAndExit)		\
      SymI_HasProto(stable_ptr_table)			\
      SymI_HasProto(stackOverflow)			\
      SymI_HasProto(stg_CAF_BLACKHOLE_info)		\
      SymI_HasProto(awakenBlockedQueue)			\
      SymI_HasProto(startTimer)                         \
      SymI_HasProto(stg_CHARLIKE_closure)		\
      SymI_HasProto(stg_MVAR_CLEAN_info)		\
      SymI_HasProto(stg_MVAR_DIRTY_info)		\
      SymI_HasProto(stg_IND_STATIC_info)		\
      SymI_HasProto(stg_INTLIKE_closure)		\
      SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info)	\
      SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info)	\
      SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info)	\
      SymI_HasProto(stg_WEAK_info)                      \
      SymI_HasProto(stg_ap_v_info)			\
      SymI_HasProto(stg_ap_f_info)			\
      SymI_HasProto(stg_ap_d_info)			\
      SymI_HasProto(stg_ap_l_info)			\
      SymI_HasProto(stg_ap_n_info)			\
      SymI_HasProto(stg_ap_p_info)			\
      SymI_HasProto(stg_ap_pv_info)			\
      SymI_HasProto(stg_ap_pp_info)			\
      SymI_HasProto(stg_ap_ppv_info)			\
      SymI_HasProto(stg_ap_ppp_info)			\
      SymI_HasProto(stg_ap_pppv_info)			\
      SymI_HasProto(stg_ap_pppp_info)			\
      SymI_HasProto(stg_ap_ppppp_info)			\
      SymI_HasProto(stg_ap_pppppp_info)			\
      SymI_HasProto(stg_ap_0_fast)			\
      SymI_HasProto(stg_ap_v_fast)			\
      SymI_HasProto(stg_ap_f_fast)			\
      SymI_HasProto(stg_ap_d_fast)			\
      SymI_HasProto(stg_ap_l_fast)			\
      SymI_HasProto(stg_ap_n_fast)			\
      SymI_HasProto(stg_ap_p_fast)			\
      SymI_HasProto(stg_ap_pv_fast)			\
      SymI_HasProto(stg_ap_pp_fast)			\
      SymI_HasProto(stg_ap_ppv_fast)			\
      SymI_HasProto(stg_ap_ppp_fast)			\
      SymI_HasProto(stg_ap_pppv_fast)			\
      SymI_HasProto(stg_ap_pppp_fast)			\
      SymI_HasProto(stg_ap_ppppp_fast)			\
      SymI_HasProto(stg_ap_pppppp_fast)			\
      SymI_HasProto(stg_ap_1_upd_info)			\
      SymI_HasProto(stg_ap_2_upd_info)			\
      SymI_HasProto(stg_ap_3_upd_info)			\
      SymI_HasProto(stg_ap_4_upd_info)			\
      SymI_HasProto(stg_ap_5_upd_info)			\
      SymI_HasProto(stg_ap_6_upd_info)			\
      SymI_HasProto(stg_ap_7_upd_info)			\
      SymI_HasProto(stg_exit)				\
      SymI_HasProto(stg_sel_0_upd_info)			\
      SymI_HasProto(stg_sel_10_upd_info)		\
      SymI_HasProto(stg_sel_11_upd_info)		\
      SymI_HasProto(stg_sel_12_upd_info)		\
      SymI_HasProto(stg_sel_13_upd_info)		\
      SymI_HasProto(stg_sel_14_upd_info)		\
      SymI_HasProto(stg_sel_15_upd_info)		\
      SymI_HasProto(stg_sel_1_upd_info)			\
      SymI_HasProto(stg_sel_2_upd_info)			\
      SymI_HasProto(stg_sel_3_upd_info)			\
      SymI_HasProto(stg_sel_4_upd_info)			\
      SymI_HasProto(stg_sel_5_upd_info)			\
      SymI_HasProto(stg_sel_6_upd_info)			\
      SymI_HasProto(stg_sel_7_upd_info)			\
      SymI_HasProto(stg_sel_8_upd_info)			\
      SymI_HasProto(stg_sel_9_upd_info)			\
      SymI_HasProto(stg_upd_frame_info)			\
      SymI_HasProto(suspendThread)			\
      SymI_HasProto(takeMVarzh_fast)			\
      SymI_HasProto(threadStatuszh_fast)		\
      SymI_HasProto(timesIntegerzh_fast)		\
      SymI_HasProto(tryPutMVarzh_fast)			\
      SymI_HasProto(tryTakeMVarzh_fast)			\
      SymI_HasProto(unblockAsyncExceptionszh_fast)	\
      SymI_HasProto(unloadObj)                          \
      SymI_HasProto(unsafeThawArrayzh_fast)		\
      SymI_HasProto(waitReadzh_fast)			\
      SymI_HasProto(waitWritezh_fast)			\
      SymI_HasProto(word2Integerzh_fast)		\
      SymI_HasProto(writeTVarzh_fast)			\
      SymI_HasProto(xorIntegerzh_fast)			\
      SymI_HasProto(yieldzh_fast)                       \
      SymI_NeedsProto(stg_interp_constr_entry)          \
      SymI_HasProto(allocateExec)	                \
      SymI_HasProto(freeExec)		                \
      SymI_HasProto(getAllocations)                     \
      SymI_HasProto(revertCAFs)                         \
      SymI_HasProto(RtsFlags)                           \
      SymI_NeedsProto(rts_breakpoint_io_action)		\
      SymI_NeedsProto(rts_stop_next_breakpoint)		\
      SymI_NeedsProto(rts_stop_on_exception)		\
      SymI_HasProto(stopTimer)				\
      SymI_HasProto(n_capabilities)			\
sof's avatar
sof committed
810
      RTS_USER_SIGNALS_SYMBOLS
811

812
#ifdef SUPPORT_LONG_LONGS
813 814 815
#define RTS_LONG_LONG_SYMS			        \
      SymI_HasProto(int64ToIntegerzh_fast)		\
      SymI_HasProto(word64ToIntegerzh_fast)
816 817 818 819
#else
#define RTS_LONG_LONG_SYMS /* nothing */
#endif

820 821
// 64-bit support functions in libgcc.a
#if defined(__GNUC__) && SIZEOF_VOID_P <= 4
822 823 824 825 826 827 828 829 830 831
#define RTS_LIBGCC_SYMBOLS			       \
      SymI_NeedsProto(__divdi3)                        \
      SymI_NeedsProto(__udivdi3)                       \
      SymI_NeedsProto(__moddi3)                        \
      SymI_NeedsProto(__umoddi3)		       \
      SymI_NeedsProto(__muldi3)			       \
      SymI_NeedsProto(__ashldi3)		       \
      SymI_NeedsProto(__ashrdi3)		       \
      SymI_NeedsProto(__lshrdi3)		       \
      SymI_NeedsProto(__eprintf)
832
#elif defined(ia64_HOST_ARCH)
833 834 835 836 837 838 839
#define RTS_LIBGCC_SYMBOLS			        \
      SymI_NeedsProto(__divdi3)				\
      SymI_NeedsProto(__udivdi3)                        \
      SymI_NeedsProto(__moddi3)				\
      SymI_NeedsProto(__umoddi3)			\
      SymI_NeedsProto(__divsf3)				\
      SymI_NeedsProto(__divdf3)
840 841 842 843
#else
#define RTS_LIBGCC_SYMBOLS
#endif

844
#if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
845 846 847 848
      // Symbols that don't have a leading underscore
      // on Mac OS X. They have to receive special treatment,
      // see machoInitSymbolsWithoutUnderscore()
#define RTS_MACHO_NOUNDERLINE_SYMBOLS		\
849 850
      SymI_NeedsProto(saveFP)				\
      SymI_NeedsProto(restFP)
851
#endif
852 853

/* entirely bogus claims about types of these symbols */
854
#define SymI_NeedsProto(vvv)  extern void vvv(void);
855
#if defined(__PIC__) && defined(mingw32_TARGET_OS)
856 857
#define SymE_HasProto(vvv)    SymE_HasProto(vvv);
#define SymE_NeedsProto(vvv)    extern void _imp__ ## vvv (void);
858
#else
859 860
#define SymE_NeedsProto(vvv)  SymI_NeedsProto(vvv);
#define SymE_HasProto(vvv)    SymI_HasProto(vvv)
861
#endif
862 863
#define SymI_HasProto(vvv) /**/
#define SymI_HasProto_redirect(vvv,xxx) /**/
864
RTS_SYMBOLS
865
RTS_RET_SYMBOLS
866
RTS_LONG_LONG_SYMS
867
RTS_POSIX_ONLY_SYMBOLS
868
RTS_MINGW_ONLY_SYMBOLS
sof's avatar
sof committed
869
RTS_CYGWIN_ONLY_SYMBOLS
870
RTS_DARWIN_ONLY_SYMBOLS
871
RTS_LIBGCC_SYMBOLS
872
RTS_LIBFFI_SYMBOLS
873 874 875 876 877
#undef SymI_NeedsProto
#undef SymI_HasProto
#undef SymI_HasProto_redirect
#undef SymE_HasProto
#undef SymE_NeedsProto
878 879 880 881 882 883 884

#ifdef LEADING_UNDERSCORE
#define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
#else
#define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
#endif

885
#define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
886
                    (void*)(&(vvv)) },
887
#define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
888
	    (void*)DLL_IMPORT_DATA_REF(vvv) },
889

890 891 892 893
#define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
#define SymE_NeedsProto(vvv) SymE_HasProto(vvv)

// SymI_HasProto_redirect allows us to redirect references to one symbol to
894
// another symbol.  See newCAF/newDynCAF for an example.
895
#define SymI_HasProto_redirect(vvv,xxx) \
896 897 898
    { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
      (void*)(&(xxx)) },

899
static RtsSymbolVal rtsSyms[] = {
900
      RTS_SYMBOLS
901
      RTS_RET_SYMBOLS
902
      RTS_LONG_LONG_SYMS
903
      RTS_POSIX_ONLY_SYMBOLS
904
      RTS_MINGW_ONLY_SYMBOLS
sof's avatar
sof committed
905
      RTS_CYGWIN_ONLY_SYMBOLS
906
      RTS_DARWIN_ONLY_SYMBOLS
907
      RTS_LIBGCC_SYMBOLS
908
      RTS_LIBFFI_SYMBOLS
909 910 911 912
#if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
      // dyld stub code contains references to this,
      // but it should never be called because we treat
      // lazy pointers as nonlazy.
913
      { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
914
#endif
915 916 917
      { 0, 0 } /* sentinel */
};

918 919


920 921 922
/* -----------------------------------------------------------------------------
 * Insert symbols into hash tables, checking for duplicates.
 */
923

924 925
static void ghciInsertStrHashTable ( char* obj_name,
                                     HashTable *table,
926
                                     char* key,
927 928 929 930 931 932 933 934
                                     void *data
				   )
{
   if (lookupHashTable(table, (StgWord)key) == NULL)
   {
      insertStrHashTable(table, (StgWord)key, data);
      return;
   }
935
   debugBelch(
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
      "\n\n"
      "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
      "   %s\n"
      "whilst processing object file\n"
      "   %s\n"
      "This could be caused by:\n"
      "   * Loading two different object files which export the same symbol\n"
      "   * Specifying the same object file twice on the GHCi command line\n"
      "   * An incorrect `package.conf' entry, causing some object to be\n"
      "     loaded twice.\n"
      "GHCi cannot safely continue in this situation.  Exiting now.  Sorry.\n"
      "\n",
      (char*)key,
      obj_name
   );
   exit(1);
}
953 954 955
/* -----------------------------------------------------------------------------
 * initialize the object linker
 */
956 957 958 959


static int linker_init_done = 0 ;

960
#if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
961
static void *dl_prog_handle;
962
#endif
963 964 965 966

void
initLinker( void )
{
967
    RtsSymbolVal *sym;
968

969 970 971 972 973 974 975
    /* Make initLinker idempotent, so we can call it
       before evey relevant operation; that means we
       don't need to initialise the linker separately */
    if (linker_init_done == 1) { return; } else {
      linker_init_done = 1;
    }

976
    stablehash = allocStrHashTable();
977 978 979 980
    symhash = allocStrHashTable();

    /* populate the symbol table with stuff from the RTS */
    for (sym = rtsSyms; sym->lbl != NULL; sym++) {
981 982
	ghciInsertStrHashTable("(GHCi built-in symbols)",
                               symhash, sym->lbl, sym->addr);
983
    }
984
#   if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
985 986 987
    machoInitSymbolsWithoutUnderscore();
#   endif

988
#   if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
989
#   if defined(RTLD_DEFAULT)
990 991
    dl_prog_handle = RTLD_DEFAULT;
#   else
992
    dl_prog_handle = dlopen(NULL, RTLD_LAZY);
993
#   endif /* RTLD_DEFAULT */
994
#   endif
995 996
}

997
/* -----------------------------------------------------------------------------
998 999 1000
 *                  Loading DLL or .so dynamic libraries
 * -----------------------------------------------------------------------------
 *
1001 1002 1003 1004
 * Add a DLL from which symbols may be found.  In the ELF case, just
 * do RTLD_GLOBAL-style add, so no further messing around needs to
 * happen in order that symbols in the loaded .so are findable --
 * lookupSymbol() will subsequently see them by dlsym on the program's
1005 1006
 * dl-handle.  Returns NULL if success, otherwise ptr to an err msg.
 *
1007
 * In the PEi386 case, open the DLLs and put handles to them in a
1008
 * linked list.  When looking for a symbol, try all handles in the
1009 1010 1011 1012 1013
 * list.  This means that we need to load even DLLs that are guaranteed
 * to be in the ghc.exe image already, just so we can get a handle
 * to give to loadSymbol, so that we can find the symbols.  For such
 * libraries, the LoadLibrary call should be a no-op except for returning
 * the handle.
1014
 *
1015
 */
1016 1017 1018 1019 1020 1021

#if defined(OBJFORMAT_PEi386)
/* A record for storing handles into DLLs. */

typedef
   struct _OpenedDLL {
1022
      char*              name;
1023 1024
      struct _OpenedDLL* next;
      HINSTANCE instance;
1025
   }
1026 1027 1028 1029 1030 1031
   OpenedDLL;

/* A list thereof. */
static OpenedDLL* opened_dlls = NULL;
#endif

1032
const char *
1033
addDLL( char *dll_name )
1034
{
1035
#  if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1036
   /* ------------------- ELF DLL loader ------------------- */
1037
   void *hdl;
1038
   const char *errmsg;
1039

1040 1041
   initLinker();

1042 1043 1044
   // omitted: RTLD_NOW
   // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
   hdl= dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
dons's avatar
dons committed
1045

1046 1047 1048 1049 1050 1051 1052 1053
   if (hdl == NULL) {
      /* dlopen failed; return a ptr to the error msg. */
      errmsg = dlerror();
      if (errmsg == NULL) errmsg = "addDLL: unknown error";
      return errmsg;
   } else {
      return NULL;
   }
1054 1055
   /*NOTREACHED*/

1056
#  elif defined(OBJFORMAT_PEi386)
1057
   /* ------------------- Win32 DLL loader ------------------- */
1058

1059
   char*      buf;
1060
   OpenedDLL* o_dll;
1061
   HINSTANCE  instance;
1062

1063 1064
   initLinker();

1065
   /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1066 1067 1068 1069 1070

   /* See if we've already got it, and ignore if so. */
   for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
      if (0 == strcmp(o_dll->name, dll_name))
         return NULL;
1071 1072
   }

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
   /* The file name has no suffix (yet) so that we can try
      both foo.dll and foo.drv

      The documentation for LoadLibrary says:
      	If no file name extension is specified in the lpFileName
      	parameter, the default library extension .dll is
      	appended. However, the file name string can include a trailing
      	point character (.) to indicate that the module name has no
      	extension. */

1083
   buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1084 1085 1086
   sprintf(buf, "%s.DLL", dll_name);
   instance = LoadLibrary(buf);
   if (instance == NULL) {
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
       if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
       // KAA: allow loading of drivers (like winspool.drv)
       sprintf(buf, "%s.DRV", dll_name);
       instance = LoadLibrary(buf);
       if (instance == NULL) {
           if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
           // #1883: allow loading of unix-style libfoo.dll DLLs
           sprintf(buf, "lib%s.DLL", dll_name);
           instance = LoadLibrary(buf);
           if (instance == NULL) {
               goto error;
           }
       }
1100
   }
sof's avatar
sof committed
1101
   stgFree(buf);
1102

1103
   /* Add this DLL to the list of DLLs in which to search for symbols. */
1104
   o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1105 1106
   o_dll->name     = stgMallocBytes(1+strlen(dll_name), "addDLL");
   strcpy(o_dll->name, dll_name);
1107
   o_dll->instance = instance;
1108 1109
   o_dll->next     = opened_dlls;
   opened_dlls     = o_dll;
1110 1111

   return NULL;
1112 1113 1114 1115

error:
   stgFree(buf);
   sysErrorBelch(dll_name);
Simon Marlow's avatar
Simon Marlow committed
1116

1117 1118
   /* LoadLibrary failed; return a ptr to the error msg. */
   return "addDLL: could not load DLL";
Simon Marlow's avatar
Simon Marlow committed
1119 1120 1121 1122

#  else
   barf("addDLL: not implemented on this platform");
#  endif
1123 1124
}

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
/* -----------------------------------------------------------------------------
 * insert a stable symbol in the hash table
 */

void
insertStableSymbol(char* obj_name, char* key, StgPtr p)
{
  ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
}


/* -----------------------------------------------------------------------------
 * insert a symbol in the hash table
 */
void
insertSymbol(char* obj_name, char* key, void* data)
{
  ghciInsertStrHashTable(obj_name, symhash, key, data);
}

1145 1146
/* -----------------------------------------------------------------------------
 * lookup a symbol in the hash table
1147
 */
1148 1149 1150
void *
lookupSymbol( char *lbl )
{
1151
    void *val;
1152
    initLinker() ;
1153
    ASSERT(symhash != NULL);
1154 1155 1156
    val = lookupStrHashTable(symhash, lbl);

    if (val == NULL) {
1157
#       if defined(OBJFORMAT_ELF)
1158
	return dlsym(dl_prog_handle, lbl);
1159
#       elif defined(OBJFORMAT_MACHO)
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
#       if HAVE_DLFCN_H
        /* On OS X 10.3 and later, we use dlsym instead of the old legacy
           interface.

           HACK: On OS X, global symbols are prefixed with an underscore.
                 However, dlsym wants us to omit the leading underscore from the
                 symbol name. For now, we simply strip it off here (and ONLY
                 here).
        */
        ASSERT(lbl[0] == '_');
        return dlsym(dl_prog_handle, lbl+1);
#       else
1172 1173 1174 1175 1176 1177
	if(NSIsSymbolNameDefined(lbl)) {
	    NSSymbol symbol = NSLookupAndBindSymbol(lbl);
	    return NSAddressOfSymbol(symbol);
	} else {
	    return NULL;
	}
1178
#       endif /* HAVE_DLFCN_H */
1179
#       elif defined(OBJFORMAT_PEi386)
1180 1181 1182
        OpenedDLL* o_dll;
        void* sym;
        for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1183
	  /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
1184 1185 1186 1187 1188 1189 1190
           if (lbl[0] == '_') {
              /* HACK: if the name has an initial underscore, try stripping
                 it off & look that up first. I've yet to verify whether there's
                 a Rule that governs whether an initial '_' *should always* be
                 stripped off when mapping from import lib name to the DLL name.
              */
              sym = GetProcAddress(o_dll->instance, (lbl+1));
sof's avatar
sof committed
1191
              if (sym != NULL) {
1192
		/*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
sof's avatar
sof committed
1193
		return sym;
1194
	      }
1195
           }
1196
           sym = GetProcAddress(o_dll->instance, lbl);
sof's avatar
sof committed
1197
           if (sym != NULL) {
1198
	     /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
sof's avatar
sof committed
1199 1200
	     return sym;
	   }
1201
        }
1202
        return NULL;
ken's avatar
ken committed
1203 1204 1205
#       else
        ASSERT(2+2 == 5);
        return NULL;
1206
#       endif
1207
    } else {
1208
	return val;
1209 1210 1211
    }
}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/* -----------------------------------------------------------------------------
 * Debugging aid: look in GHCi's object symbol tables for symbols
 * within DELTA bytes of the specified address, and show their names.
 */
#ifdef DEBUG
void ghci_enquire ( char* addr );

void ghci_enquire ( char* addr )
{
   int   i;
   char* sym;
   char* a;
   const int DELTA = 64;
   ObjectCode* oc;
1226 1227 1228

   initLinker();

1229 1230 1231 1232 1233
   for (oc = objects; oc; oc = oc->next) {
      for (i = 0; i < oc->n_symbols; i++) {
         sym = oc->symbols[i];
         if (sym == NULL) continue;
         a = NULL;
1234
         if (a == NULL) {
1235
            a = lookupStrHashTable(symhash, sym);
1236
	 }
1237
         if (a == NULL) {
1238
	     // debugBelch("ghci_enquire: can't find %s\n", sym);
1239
         }
1240
         else if (addr-DELTA <= a && a <= addr+DELTA) {
1241
            debugBelch("%p + %3d  ==  `%s'\n", addr, (int)(a - addr), sym);
1242 1243 1244 1245 1246 1247
         }
      }
   }
}
#endif

1248
#ifdef ia64_HOST_ARCH
1249 1250
static unsigned int PLTSize(void);
#endif
1251

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
/* -----------------------------------------------------------------------------
 * Load an obj (populate the global symbol table, but don't resolve yet)
 *
 * Returns: 1 if ok, 0 on error.
 */
HsInt
loadObj( char *path )
{
   ObjectCode* oc;
   struct stat st;
   int r, n;
1263 1264
#ifdef USE_MMAP
   int fd, pagesize;
1265
   void *map_addr = NULL;
1266
#else
1267
   FILE *f;
1268
#endif
1269 1270
   initLinker();

1271
   /* debugBelch("loadObj %s\n", path ); */
1272

1273
   /* Check that we haven't already loaded this object.
dons's avatar
dons committed
1274
      Ignore requests to load multiple times */
1275
   {
1276
       ObjectCode *o;
1277 1278
       int is_dup = 0;
       for (o = objects; o; o = o->next) {
dons's avatar
dons committed
1279
          if (0 == strcmp(o->fileName, path)) {
1280
             is_dup = 1;
dons's avatar
dons committed
1281 1282
             break; /* don't need to search further */
          }
1283 1284
       }
       if (is_dup) {
1285
          IF_DEBUG(linker, debugBelch(
1286 1287 1288
            "GHCi runtime linker: warning: looks like you're trying to load the\n"
            "same object file twice:\n"
            "   %s\n"
dons's avatar
dons committed
1289 1290 1291
            "GHCi will ignore this, but be warned.\n"
            , path));
          return 1; /* success */
1292
       }
1293 1294 1295 1296
   }

   oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");

1297
#  if defined(OBJFORMAT_ELF)
1298
   oc->formatName = "ELF";
1299
#  elif defined(OBJFORMAT_PEi386)
1300
   oc->formatName = "PEi386";
1301 1302
#  elif defined(OBJFORMAT_MACHO)
   oc->formatName = "Mach-O";
1303
#  else
sof's avatar
sof committed
1304
   stgFree(oc);
1305 1306 1307 1308 1309 1310
   barf("loadObj: not implemented on this platform");
#  endif

   r = stat(path, &st);
   if (r == -1) { return 0; }

1311
   /* sigh, strdup() isn't a POSIX function, so do it the long way */
1312 1313 1314
   oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
   strcpy(oc->fileName, path);

1315 1316 1317
   oc->fileSize          = st.st_size;
   oc->symbols           = NULL;
   oc->sections          = NULL;
1318
   oc->proddables        = NULL;
1319 1320