Linker.c 145 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 154 155
/* 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
 */
#define X86_64_ELF_NONPIC_HACK 1
156

157 158 159 160
/* -----------------------------------------------------------------------------
 * Built-in symbols from the RTS
 */

161 162 163 164 165 166
typedef struct _RtsSymbolVal {
    char   *lbl;
    void   *addr;
} RtsSymbolVal;


167 168 169 170 171 172 173 174
#if !defined(PAR)
#define Maybe_Stable_Names      SymX(mkWeakzh_fast)			\
      				SymX(makeStableNamezh_fast)		\
      				SymX(finalizzeWeakzh_fast)
#else
/* These are not available in GUM!!! -- HWL */
#define Maybe_Stable_Names
#endif
175

176
#if !defined (mingw32_HOST_OS)
177
#define RTS_POSIX_ONLY_SYMBOLS                  \
Simon Marlow's avatar
Simon Marlow committed
178
      SymX(shutdownHaskellAndSignal)		\
179 180
      Sym(lockFile)                             \
      Sym(unlockFile)                           \
181
      SymX(signal_handlers)			\
182 183
      SymX(stg_sig_install)			\
      Sym(nocldstop)
sof's avatar
sof committed
184
#endif
185

186
#if defined (cygwin32_HOST_OS)
sof's avatar
sof committed
187 188 189
#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
190
 * exports; sigh.
sof's avatar
sof committed
191 192 193 194 195 196 197 198 199 200 201 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
 */
#define RTS_CYGWIN_ONLY_SYMBOLS                 \
      SymX(regfree)                             \
      SymX(regexec)                             \
      SymX(regerror)                            \
      SymX(regcomp)                             \
      SymX(__errno)                             \
      SymX(access)                              \
      SymX(chmod)                               \
      SymX(chdir)                               \
      SymX(close)                               \
      SymX(creat)                               \
      SymX(dup)                                 \
      SymX(dup2)                                \
      SymX(fstat)                               \
      SymX(fcntl)                               \
      SymX(getcwd)                              \
      SymX(getenv)                              \
      SymX(lseek)                               \
      SymX(open)                                \
      SymX(fpathconf)                           \
      SymX(pathconf)                            \
      SymX(stat)                                \
      SymX(pow)                                 \
      SymX(tanh)                                \
      SymX(cosh)                                \
      SymX(sinh)                                \
      SymX(atan)                                \
      SymX(acos)                                \
      SymX(asin)                                \
      SymX(tan)                                 \
      SymX(cos)                                 \
      SymX(sin)                                 \
      SymX(exp)                                 \
      SymX(log)                                 \
      SymX(sqrt)                                \
      SymX(localtime_r)                         \
      SymX(gmtime_r)                            \
      SymX(mktime)                              \
      Sym(_imp___tzname)                        \
      SymX(gettimeofday)                        \
      SymX(timezone)                            \
      SymX(tcgetattr)                           \
      SymX(tcsetattr)                           \
      SymX(memcpy)                              \
      SymX(memmove)                             \
      SymX(realloc)                             \
      SymX(malloc)                              \
      SymX(free)                                \
      SymX(fork)                                \
      SymX(lstat)                               \
      SymX(isatty)                              \
      SymX(mkdir)                               \
      SymX(opendir)                             \
      SymX(readdir)                             \
      SymX(rewinddir)                           \
      SymX(closedir)                            \
      SymX(link)                                \
      SymX(mkfifo)                              \
      SymX(pipe)                                \
      SymX(read)                                \
      SymX(rename)                              \
      SymX(rmdir)                               \
      SymX(select)                              \
      SymX(system)                              \
      SymX(write)                               \
      SymX(strcmp)                              \
      SymX(strcpy)                              \
      SymX(strncpy)                             \
      SymX(strerror)                            \
      SymX(sigaddset)                           \
      SymX(sigemptyset)                         \
      SymX(sigprocmask)                         \
      SymX(umask)                               \
      SymX(uname)                               \
      SymX(unlink)                              \
      SymX(utime)                               \
268
      SymX(waitpid)
269

270
#elif !defined(mingw32_HOST_OS)
sof's avatar
sof committed
271 272
#define RTS_MINGW_ONLY_SYMBOLS /**/
#define RTS_CYGWIN_ONLY_SYMBOLS /**/
273
#else /* defined(mingw32_HOST_OS) */
sof's avatar
sof committed
274 275
#define RTS_POSIX_ONLY_SYMBOLS  /**/
#define RTS_CYGWIN_ONLY_SYMBOLS /**/
276

277 278 279 280
/* Extra syms gen'ed by mingw-2's gcc-3.2: */
#if __GNUC__>=3
#define RTS_MINGW_EXTRA_SYMS                    \
      Sym(_imp____mb_cur_max)                   \
281
      Sym(_imp___pctype)
282 283 284 285
#else
#define RTS_MINGW_EXTRA_SYMS
#endif

286 287 288 289 290 291
#if HAVE_GETTIMEOFDAY
#define RTS_MINGW_GETTIMEOFDAY_SYM Sym(gettimeofday)
#else
#define RTS_MINGW_GETTIMEOFDAY_SYM /**/
#endif

292 293
/* These are statically linked from the mingw libraries into the ghc
   executable, so we have to employ this hack. */
294
#define RTS_MINGW_ONLY_SYMBOLS                  \
sof's avatar
sof committed
295 296
      SymX(asyncReadzh_fast)			\
      SymX(asyncWritezh_fast)			\
sof's avatar
sof committed
297
      SymX(asyncDoProczh_fast)			\
298
      SymX(memset)                              \
299 300 301 302 303 304 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
      SymX(inet_ntoa)                           \
      SymX(inet_addr)                           \
      SymX(htonl)                               \
      SymX(recvfrom)                            \
      SymX(listen)                              \
      SymX(bind)                                \
      SymX(shutdown)                            \
      SymX(connect)                             \
      SymX(htons)                               \
      SymX(ntohs)                               \
      SymX(getservbyname)                       \
      SymX(getservbyport)                       \
      SymX(getprotobynumber)                    \
      SymX(getprotobyname)                      \
      SymX(gethostbyname)                       \
      SymX(gethostbyaddr)                       \
      SymX(gethostname)                         \
      SymX(strcpy)                              \
      SymX(strncpy)                             \
      SymX(abort)                               \
      Sym(_alloca)                              \
      Sym(isxdigit)                             \
      Sym(isupper)                              \
      Sym(ispunct)                              \
      Sym(islower)                              \
      Sym(isspace)                              \
      Sym(isprint)                              \
      Sym(isdigit)                              \
      Sym(iscntrl)                              \
      Sym(isalpha)                              \
      Sym(isalnum)                              \
      SymX(strcmp)                              \
331 332 333
      SymX(memmove)                             \
      SymX(realloc)                             \
      SymX(malloc)                              \
334 335 336 337 338 339 340 341 342 343 344 345 346
      SymX(pow)                                 \
      SymX(tanh)                                \
      SymX(cosh)                                \
      SymX(sinh)                                \
      SymX(atan)                                \
      SymX(acos)                                \
      SymX(asin)                                \
      SymX(tan)                                 \
      SymX(cos)                                 \
      SymX(sin)                                 \
      SymX(exp)                                 \
      SymX(log)                                 \
      SymX(sqrt)                                \
347 348 349 350 351 352 353 354 355 356 357 358 359
      SymX(powf)                                 \
      SymX(tanhf)                                \
      SymX(coshf)                                \
      SymX(sinhf)                                \
      SymX(atanf)                                \
      SymX(acosf)                                \
      SymX(asinf)                                \
      SymX(tanf)                                 \
      SymX(cosf)                                 \
      SymX(sinf)                                 \
      SymX(expf)                                 \
      SymX(logf)                                 \
      SymX(sqrtf)                                \
360
      SymX(memcpy)                              \
sof's avatar
sof committed
361 362
      SymX(rts_InstallConsoleEvent)             \
      SymX(rts_ConsoleHandlerDone)              \
363
      Sym(mktime)                               \
364
      Sym(_imp___timezone)                      \
365
      Sym(_imp___tzname)                        \
366
      Sym(_imp__tzname)                         \
367
      Sym(_imp___iob)                           \
sof's avatar
sof committed
368
      Sym(_imp___osver)                         \
369 370 371 372
      Sym(localtime)                            \
      Sym(gmtime)                               \
      Sym(opendir)                              \
      Sym(readdir)                              \
sof's avatar
sof committed
373
      Sym(rewinddir)                            \
374
      RTS_MINGW_EXTRA_SYMS                      \
375
      RTS_MINGW_GETTIMEOFDAY_SYM		\
376
      Sym(closedir)
377
#endif
378

379 380 381 382 383 384 385 386 387 388 389 390 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
#if defined(darwin_TARGET_OS) && HAVE_PRINTF_LDBLSTUB
#define RTS_DARWIN_ONLY_SYMBOLS			\
     Sym(asprintf$LDBLStub)                     \
     Sym(err$LDBLStub)                          \
     Sym(errc$LDBLStub)                         \
     Sym(errx$LDBLStub)                         \
     Sym(fprintf$LDBLStub)                      \
     Sym(fscanf$LDBLStub)                       \
     Sym(fwprintf$LDBLStub)                     \
     Sym(fwscanf$LDBLStub)                      \
     Sym(printf$LDBLStub)                       \
     Sym(scanf$LDBLStub)                        \
     Sym(snprintf$LDBLStub)                     \
     Sym(sprintf$LDBLStub)                      \
     Sym(sscanf$LDBLStub)                       \
     Sym(strtold$LDBLStub)                      \
     Sym(swprintf$LDBLStub)                     \
     Sym(swscanf$LDBLStub)                      \
     Sym(syslog$LDBLStub)                       \
     Sym(vasprintf$LDBLStub)                    \
     Sym(verr$LDBLStub)                         \
     Sym(verrc$LDBLStub)                        \
     Sym(verrx$LDBLStub)                        \
     Sym(vfprintf$LDBLStub)                     \
     Sym(vfscanf$LDBLStub)                      \
     Sym(vfwprintf$LDBLStub)                    \
     Sym(vfwscanf$LDBLStub)                     \
     Sym(vprintf$LDBLStub)                      \
     Sym(vscanf$LDBLStub)                       \
     Sym(vsnprintf$LDBLStub)                    \
     Sym(vsprintf$LDBLStub)                     \
     Sym(vsscanf$LDBLStub)                      \
     Sym(vswprintf$LDBLStub)                    \
     Sym(vswscanf$LDBLStub)                     \
     Sym(vsyslog$LDBLStub)                      \
     Sym(vwarn$LDBLStub)                        \
     Sym(vwarnc$LDBLStub)                       \
     Sym(vwarnx$LDBLStub)                       \
     Sym(vwprintf$LDBLStub)                     \
     Sym(vwscanf$LDBLStub)                      \
     Sym(warn$LDBLStub)                         \
     Sym(warnc$LDBLStub)                        \
     Sym(warnx$LDBLStub)                        \
     Sym(wcstold$LDBLStub)                      \
     Sym(wprintf$LDBLStub)                      \
     Sym(wscanf$LDBLStub)
#else
#define RTS_DARWIN_ONLY_SYMBOLS
#endif

sof's avatar
sof committed
429 430 431 432 433
#ifndef SMP
# define MAIN_CAP_SYM SymX(MainCapability)
#else
# define MAIN_CAP_SYM
#endif
434

sof's avatar
sof committed
435 436 437 438
#if !defined(mingw32_HOST_OS)
#define RTS_USER_SIGNALS_SYMBOLS \
   SymX(setIOManagerPipe)
#else
Simon Marlow's avatar
Simon Marlow committed
439 440 441 442 443
#define RTS_USER_SIGNALS_SYMBOLS \
   SymX(sendIOManagerEvent) \
   SymX(readIOManagerEvent) \
   SymX(getIOManagerEvent) \
   SymX(console_handler)
sof's avatar
sof committed
444 445
#endif

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
#define RTS_LIBFFI_SYMBOLS                      \
     Sym(ffi_prep_cif)                          \
     Sym(ffi_call)                              \
     Sym(ffi_type_void)                         \
     Sym(ffi_type_float)                        \
     Sym(ffi_type_double)                       \
     Sym(ffi_type_sint64)                       \
     Sym(ffi_type_uint64)                       \
     Sym(ffi_type_sint32)                       \
     Sym(ffi_type_uint32)                       \
     Sym(ffi_type_sint16)                       \
     Sym(ffi_type_uint16)                       \
     Sym(ffi_type_sint8)                        \
     Sym(ffi_type_uint8)                        \
     Sym(ffi_type_pointer)

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
#ifdef TABLES_NEXT_TO_CODE
#define RTS_RET_SYMBOLS /* nothing */
#else
#define RTS_RET_SYMBOLS 			\
      SymX(stg_enter_ret)			\
      SymX(stg_gc_fun_ret)			\
      SymX(stg_ap_v_ret)			\
      SymX(stg_ap_f_ret)			\
      SymX(stg_ap_d_ret)			\
      SymX(stg_ap_l_ret)			\
      SymX(stg_ap_n_ret)			\
      SymX(stg_ap_p_ret)			\
      SymX(stg_ap_pv_ret)			\
      SymX(stg_ap_pp_ret)			\
      SymX(stg_ap_ppv_ret)			\
      SymX(stg_ap_ppp_ret)			\
      SymX(stg_ap_pppv_ret)			\
      SymX(stg_ap_pppp_ret)			\
      SymX(stg_ap_ppppp_ret)			\
      SymX(stg_ap_pppppp_ret)
#endif

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
/* On Windows, we link libgmp.a statically into libHSrts.dll */
#ifdef mingw32_HOST_OS
#define GMP_SYMS				\
      SymX(__gmpz_cmp)				\
      SymX(__gmpz_cmp_si)			\
      SymX(__gmpz_cmp_ui)			\
      SymX(__gmpz_get_si)			\
      SymX(__gmpz_get_ui)
#else
#define GMP_SYMS				\
      SymExtern(__gmpz_cmp)			\
      SymExtern(__gmpz_cmp_si)			\
      SymExtern(__gmpz_cmp_ui)			\
      SymExtern(__gmpz_get_si)			\
      SymExtern(__gmpz_get_ui)
#endif

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

799
#ifdef SUPPORT_LONG_LONGS
800
#define RTS_LONG_LONG_SYMS			\
801 802
      SymX(int64ToIntegerzh_fast)		\
      SymX(word64ToIntegerzh_fast)
803 804 805 806
#else
#define RTS_LONG_LONG_SYMS /* nothing */
#endif

807 808 809
// 64-bit support functions in libgcc.a
#if defined(__GNUC__) && SIZEOF_VOID_P <= 4
#define RTS_LIBGCC_SYMBOLS			\
810 811 812
      Sym(__divdi3)                             \
      Sym(__udivdi3)                            \
      Sym(__moddi3)                             \
813
      Sym(__umoddi3)				\
814
      Sym(__muldi3)				\
815 816 817 818
      Sym(__ashldi3)				\
      Sym(__ashrdi3)				\
      Sym(__lshrdi3)				\
      Sym(__eprintf)
819
#elif defined(ia64_HOST_ARCH)
820 821 822 823 824 825 826
#define RTS_LIBGCC_SYMBOLS			\
      Sym(__divdi3)				\
      Sym(__udivdi3)                            \
      Sym(__moddi3)				\
      Sym(__umoddi3)				\
      Sym(__divsf3)				\
      Sym(__divdf3)
827 828 829 830
#else
#define RTS_LIBGCC_SYMBOLS
#endif

831
#if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
832 833 834 835 836 837
      // 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		\
      Sym(saveFP)				\
      Sym(restFP)
838
#endif
839 840

/* entirely bogus claims about types of these symbols */
841
#define Sym(vvv)  extern void vvv(void);
842
#if defined(__PIC__) && defined(mingw32_TARGET_OS)
843 844 845 846
#define SymExtern(vvv)  extern void _imp__ ## vvv (void);
#else
#define SymExtern(vvv)  SymX(vvv)
#endif
847
#define SymX(vvv) /**/
848
#define SymX_redirect(vvv,xxx) /**/
849
RTS_SYMBOLS
850
RTS_RET_SYMBOLS
851
RTS_LONG_LONG_SYMS
852
RTS_POSIX_ONLY_SYMBOLS
853
RTS_MINGW_ONLY_SYMBOLS
sof's avatar
sof committed
854
RTS_CYGWIN_ONLY_SYMBOLS
855
RTS_DARWIN_ONLY_SYMBOLS
856
RTS_LIBGCC_SYMBOLS
857
RTS_LIBFFI_SYMBOLS
858 859
#undef Sym
#undef SymX
860
#undef SymX_redirect
861
#undef SymExtern
862 863 864 865 866 867 868 869 870 871

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

#define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
                    (void*)(&(vvv)) },
#define SymX(vvv) Sym(vvv)
872
#define SymExtern(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
873
	    (void*)DLL_IMPORT_DATA_REF(vvv) },
874

875 876 877 878 879 880
// SymX_redirect allows us to redirect references to one symbol to
// another symbol.  See newCAF/newDynCAF for an example.
#define SymX_redirect(vvv,xxx) \
    { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
      (void*)(&(xxx)) },

881
static RtsSymbolVal rtsSyms[] = {
882
      RTS_SYMBOLS
883
      RTS_RET_SYMBOLS
884
      RTS_LONG_LONG_SYMS
885
      RTS_POSIX_ONLY_SYMBOLS
886
      RTS_MINGW_ONLY_SYMBOLS
sof's avatar
sof committed
887
      RTS_CYGWIN_ONLY_SYMBOLS
888
      RTS_DARWIN_ONLY_SYMBOLS
889
      RTS_LIBGCC_SYMBOLS
890
      RTS_LIBFFI_SYMBOLS
891 892 893 894
#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.
895
      { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
896
#endif
897 898 899
      { 0, 0 } /* sentinel */
};

900 901


902 903 904
/* -----------------------------------------------------------------------------
 * Insert symbols into hash tables, checking for duplicates.
 */
905

906 907
static void ghciInsertStrHashTable ( char* obj_name,
                                     HashTable *table,
908
                                     char* key,
909 910 911 912 913 914 915 916
                                     void *data
				   )
{
   if (lookupHashTable(table, (StgWord)key) == NULL)
   {
      insertStrHashTable(table, (StgWord)key, data);
      return;
   }
917
   debugBelch(
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
      "\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);
}
935 936 937
/* -----------------------------------------------------------------------------
 * initialize the object linker
 */
938 939 940 941


static int linker_init_done = 0 ;

942
#if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
943
static void *dl_prog_handle;
944
#endif
945 946 947 948

void
initLinker( void )
{
949
    RtsSymbolVal *sym;
950

951 952 953 954 955 956 957
    /* 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;
    }

958
    stablehash = allocStrHashTable();
959 960 961 962
    symhash = allocStrHashTable();

    /* populate the symbol table with stuff from the RTS */
    for (sym = rtsSyms; sym->lbl != NULL; sym++) {
963 964
	ghciInsertStrHashTable("(GHCi built-in symbols)",
                               symhash, sym->lbl, sym->addr);
965
    }
966
#   if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
967 968 969
    machoInitSymbolsWithoutUnderscore();
#   endif

970
#   if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
971
#   if defined(RTLD_DEFAULT)
972 973
    dl_prog_handle = RTLD_DEFAULT;
#   else
974
    dl_prog_handle = dlopen(NULL, RTLD_LAZY);
975
#   endif /* RTLD_DEFAULT */
976
#   endif
977 978
}

979
/* -----------------------------------------------------------------------------
980 981 982
 *                  Loading DLL or .so dynamic libraries
 * -----------------------------------------------------------------------------
 *
983 984 985 986
 * 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
987 988
 * dl-handle.  Returns NULL if success, otherwise ptr to an err msg.
 *
989
 * In the PEi386 case, open the DLLs and put handles to them in a
990
 * linked list.  When looking for a symbol, try all handles in the
991 992 993 994 995
 * 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.
996
 *
997
 */
998 999 1000 1001 1002 1003

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

typedef
   struct _OpenedDLL {
1004
      char*              name;
1005 1006
      struct _OpenedDLL* next;
      HINSTANCE instance;
1007
   }
1008 1009 1010 1011 1012 1013
   OpenedDLL;

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

1014
const char *
1015
addDLL( char *dll_name )
1016
{
1017
#  if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1018
   /* ------------------- ELF DLL loader ------------------- */
1019
   void *hdl;
1020
   const char *errmsg;
1021

1022 1023
   initLinker();

1024 1025 1026
   // 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
1027

1028 1029 1030 1031 1032 1033 1034 1035
   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;
   }
1036 1037
   /*NOTREACHED*/

1038
#  elif defined(OBJFORMAT_PEi386)
1039
   /* ------------------- Win32 DLL loader ------------------- */
1040

1041
   char*      buf;
1042
   OpenedDLL* o_dll;
1043
   HINSTANCE  instance;
1044

1045 1046
   initLinker();

1047
   /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1048 1049 1050 1051 1052

   /* 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;
1053 1054
   }

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
   /* 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. */

1065
   buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1066 1067 1068
   sprintf(buf, "%s.DLL", dll_name);
   instance = LoadLibrary(buf);
   if (instance == NULL) {
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
       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;
           }
       }
1082
   }
sof's avatar
sof committed
1083
   stgFree(buf);
1084

1085
   /* Add this DLL to the list of DLLs in which to search for symbols. */
1086
   o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1087 1088
   o_dll->name     = stgMallocBytes(1+strlen(dll_name), "addDLL");
   strcpy(o_dll->name, dll_name);
1089
   o_dll->instance = instance;
1090 1091
   o_dll->next     = opened_dlls;
   opened_dlls     = o_dll;
1092 1093

   return NULL;
1094 1095 1096
#  else
   barf("addDLL: not implemented on this platform");
#  endif
1097 1098 1099 1100 1101 1102 1103

error:
   stgFree(buf);
   sysErrorBelch(dll_name);
               
   /* LoadLibrary failed; return a ptr to the error msg. */
   return "addDLL: could not load DLL";
1104 1105
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115