rts/Linker.c tidyups and debug belches
[ghc-hetmet.git] / rts / Linker.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2000-2004
4  *
5  * RTS Object Linker
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #if 0
10 #include "PosixSource.h"
11 #endif
12
13 /* Linux needs _GNU_SOURCE to get RTLD_DEFAULT from <dlfcn.h> and
14    MREMAP_MAYMOVE from <sys/mman.h>.
15  */
16 #if defined(__linux__)  || defined(__GLIBC__)
17 #define _GNU_SOURCE 1
18 #endif
19
20 #include "Rts.h"
21 #include "HsFFI.h"
22
23 #include "sm/Storage.h"
24 #include "Stats.h"
25 #include "Hash.h"
26 #include "LinkerInternals.h"
27 #include "RtsUtils.h"
28 #include "Trace.h"
29 #include "StgPrimFloat.h" // for __int_encodeFloat etc.
30 #include "Stable.h"
31
32 #if !defined(mingw32_HOST_OS)
33 #include "posix/Signals.h"
34 #endif
35
36 // get protos for is*()
37 #include <ctype.h>
38
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif
42
43 #include <inttypes.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <assert.h>
48
49 #ifdef HAVE_SYS_STAT_H
50 #include <sys/stat.h>
51 #endif
52
53 #if defined(HAVE_DLFCN_H)
54 #include <dlfcn.h>
55 #endif
56
57 #if defined(cygwin32_HOST_OS)
58 #ifdef HAVE_DIRENT_H
59 #include <dirent.h>
60 #endif
61
62 #ifdef HAVE_SYS_TIME_H
63 #include <sys/time.h>
64 #endif
65 #include <regex.h>
66 #include <sys/fcntl.h>
67 #include <sys/termios.h>
68 #include <sys/utime.h>
69 #include <sys/utsname.h>
70 #include <sys/wait.h>
71 #endif
72
73 #if defined(linux_HOST_OS    ) || defined(freebsd_HOST_OS) || \
74     defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS ) || \
75     defined(openbsd_HOST_OS  ) || \
76     ( defined(darwin_HOST_OS ) && !defined(powerpc_HOST_ARCH) ) || \
77     defined(kfreebsdgnu_HOST_OS)
78 /* Don't use mmap on powerpc-apple-darwin as mmap doesn't support
79  * reallocating but we need to allocate jump islands just after each
80  * object images. Otherwise relative branches to jump islands can fail
81  * due to 24-bits displacement overflow.
82  */
83 #define USE_MMAP
84 #include <fcntl.h>
85 #include <sys/mman.h>
86
87 #ifdef HAVE_UNISTD_H
88 #include <unistd.h>
89 #endif
90
91 #endif
92
93 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
94 #  define OBJFORMAT_ELF
95 #  include <regex.h>    // regex is already used by dlopen() so this is OK
96                         // to use here without requiring an additional lib
97 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
98 #  define OBJFORMAT_PEi386
99 #  include <windows.h>
100 #  include <math.h>
101 #elif defined(darwin_HOST_OS)
102 #  define OBJFORMAT_MACHO
103 #  include <regex.h>
104 #  include <mach-o/loader.h>
105 #  include <mach-o/nlist.h>
106 #  include <mach-o/reloc.h>
107 #if !defined(HAVE_DLFCN_H)
108 #  include <mach-o/dyld.h>
109 #endif
110 #if defined(powerpc_HOST_ARCH)
111 #  include <mach-o/ppc/reloc.h>
112 #endif
113 #if defined(x86_64_HOST_ARCH)
114 #  include <mach-o/x86_64/reloc.h>
115 #endif
116 #endif
117
118 #if defined(x86_64_HOST_ARCH) && defined(darwin_HOST_OS)
119 #define ALWAYS_PIC
120 #endif
121
122 /* Hash table mapping symbol names to Symbol */
123 static /*Str*/HashTable *symhash;
124
125 /* Hash table mapping symbol names to StgStablePtr */
126 static /*Str*/HashTable *stablehash;
127
128 /* List of currently loaded objects */
129 ObjectCode *objects = NULL;     /* initially empty */
130
131 static HsInt loadOc( ObjectCode* oc );
132 static ObjectCode* mkOc( char *path, char *image, int imageSize,
133                          char *archiveMemberName
134 #ifndef USE_MMAP
135 #ifdef darwin_HOST_OS
136                        , int misalignment
137 #endif
138 #endif
139                        );
140
141 #if defined(OBJFORMAT_ELF)
142 static int ocVerifyImage_ELF    ( ObjectCode* oc );
143 static int ocGetNames_ELF       ( ObjectCode* oc );
144 static int ocResolve_ELF        ( ObjectCode* oc );
145 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
146 static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
147 #endif
148 #elif defined(OBJFORMAT_PEi386)
149 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
150 static int ocGetNames_PEi386    ( ObjectCode* oc );
151 static int ocResolve_PEi386     ( ObjectCode* oc );
152 static void *lookupSymbolInDLLs ( unsigned char *lbl );
153 static void zapTrailingAtSign   ( unsigned char *sym );
154 #elif defined(OBJFORMAT_MACHO)
155 static int ocVerifyImage_MachO    ( ObjectCode* oc );
156 static int ocGetNames_MachO       ( ObjectCode* oc );
157 static int ocResolve_MachO        ( ObjectCode* oc );
158
159 #ifndef USE_MMAP
160 static int machoGetMisalignment( FILE * );
161 #endif
162 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
163 static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
164 #endif
165 #ifdef powerpc_HOST_ARCH
166 static void machoInitSymbolsWithoutUnderscore( void );
167 #endif
168 #endif
169
170 /* on x86_64 we have a problem with relocating symbol references in
171  * code that was compiled without -fPIC.  By default, the small memory
172  * model is used, which assumes that symbol references can fit in a
173  * 32-bit slot.  The system dynamic linker makes this work for
174  * references to shared libraries by either (a) allocating a jump
175  * table slot for code references, or (b) moving the symbol at load
176  * time (and copying its contents, if necessary) for data references.
177  *
178  * We unfortunately can't tell whether symbol references are to code
179  * or data.  So for now we assume they are code (the vast majority
180  * are), and allocate jump-table slots.  Unfortunately this will
181  * SILENTLY generate crashing code for data references.  This hack is
182  * enabled by X86_64_ELF_NONPIC_HACK.
183  *
184  * One workaround is to use shared Haskell libraries.  This is
185  * coming.  Another workaround is to keep the static libraries but
186  * compile them with -fPIC, because that will generate PIC references
187  * to data which can be relocated.  The PIC code is still too green to
188  * do this systematically, though.
189  *
190  * See bug #781
191  * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
192  *
193  * Naming Scheme for Symbol Macros
194  *
195  * SymI_*: symbol is internal to the RTS. It resides in an object
196  *         file/library that is statically.
197  * SymE_*: symbol is external to the RTS library. It might be linked
198  *         dynamically.
199  *
200  * Sym*_HasProto  : the symbol prototype is imported in an include file
201  *                  or defined explicitly
202  * Sym*_NeedsProto: the symbol is undefined and we add a dummy
203  *                  default proto extern void sym(void);
204  */
205 #define X86_64_ELF_NONPIC_HACK 1
206
207 /* Link objects into the lower 2Gb on x86_64.  GHC assumes the
208  * small memory model on this architecture (see gcc docs,
209  * -mcmodel=small).
210  *
211  * MAP_32BIT not available on OpenBSD/amd64
212  */
213 #if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT)
214 #define TRY_MAP_32BIT MAP_32BIT
215 #else
216 #define TRY_MAP_32BIT 0
217 #endif
218
219 /*
220  * Due to the small memory model (see above), on x86_64 we have to map
221  * all our non-PIC object files into the low 2Gb of the address space
222  * (why 2Gb and not 4Gb?  Because all addresses must be reachable
223  * using a 32-bit signed PC-relative offset). On Linux we can do this
224  * using the MAP_32BIT flag to mmap(), however on other OSs
225  * (e.g. *BSD, see #2063, and also on Linux inside Xen, see #2512), we
226  * can't do this.  So on these systems, we have to pick a base address
227  * in the low 2Gb of the address space and try to allocate memory from
228  * there.
229  *
230  * We pick a default address based on the OS, but also make this
231  * configurable via an RTS flag (+RTS -xm)
232  */
233 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
234
235 #if defined(MAP_32BIT)
236 // Try to use MAP_32BIT
237 #define MMAP_32BIT_BASE_DEFAULT 0
238 #else
239 // A guess: 1Gb.
240 #define MMAP_32BIT_BASE_DEFAULT 0x40000000
241 #endif
242
243 static void *mmap_32bit_base = (void *)MMAP_32BIT_BASE_DEFAULT;
244 #endif
245
246 /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */
247 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
248 #define MAP_ANONYMOUS MAP_ANON
249 #endif
250
251 /* -----------------------------------------------------------------------------
252  * Built-in symbols from the RTS
253  */
254
255 typedef struct _RtsSymbolVal {
256     char   *lbl;
257     void   *addr;
258 } RtsSymbolVal;
259
260 #define Maybe_Stable_Names      SymI_HasProto(stg_mkWeakzh)                     \
261                                 SymI_HasProto(stg_mkWeakForeignEnvzh)           \
262                                 SymI_HasProto(stg_makeStableNamezh)             \
263                                 SymI_HasProto(stg_finalizzeWeakzh)
264
265 #if !defined (mingw32_HOST_OS)
266 #define RTS_POSIX_ONLY_SYMBOLS                  \
267       SymI_HasProto(__hscore_get_saved_termios) \
268       SymI_HasProto(__hscore_set_saved_termios) \
269       SymI_HasProto(shutdownHaskellAndSignal)   \
270       SymI_HasProto(lockFile)                   \
271       SymI_HasProto(unlockFile)                 \
272       SymI_HasProto(signal_handlers)            \
273       SymI_HasProto(stg_sig_install)            \
274       SymI_HasProto(rtsTimerSignal)             \
275       SymI_HasProto(atexit)                     \
276       SymI_NeedsProto(nocldstop)
277 #endif
278
279 #if defined (cygwin32_HOST_OS)
280 #define RTS_MINGW_ONLY_SYMBOLS /**/
281 /* Don't have the ability to read import libs / archives, so
282  * we have to stupidly list a lot of what libcygwin.a
283  * exports; sigh.
284  */
285 #define RTS_CYGWIN_ONLY_SYMBOLS                          \
286       SymI_HasProto(regfree)                             \
287       SymI_HasProto(regexec)                             \
288       SymI_HasProto(regerror)                            \
289       SymI_HasProto(regcomp)                             \
290       SymI_HasProto(__errno)                             \
291       SymI_HasProto(access)                              \
292       SymI_HasProto(chmod)                               \
293       SymI_HasProto(chdir)                               \
294       SymI_HasProto(close)                               \
295       SymI_HasProto(creat)                               \
296       SymI_HasProto(dup)                                 \
297       SymI_HasProto(dup2)                                \
298       SymI_HasProto(fstat)                               \
299       SymI_HasProto(fcntl)                               \
300       SymI_HasProto(getcwd)                              \
301       SymI_HasProto(getenv)                              \
302       SymI_HasProto(lseek)                               \
303       SymI_HasProto(open)                                \
304       SymI_HasProto(fpathconf)                           \
305       SymI_HasProto(pathconf)                            \
306       SymI_HasProto(stat)                                \
307       SymI_HasProto(pow)                                 \
308       SymI_HasProto(tanh)                                \
309       SymI_HasProto(cosh)                                \
310       SymI_HasProto(sinh)                                \
311       SymI_HasProto(atan)                                \
312       SymI_HasProto(acos)                                \
313       SymI_HasProto(asin)                                \
314       SymI_HasProto(tan)                                 \
315       SymI_HasProto(cos)                                 \
316       SymI_HasProto(sin)                                 \
317       SymI_HasProto(exp)                                 \
318       SymI_HasProto(log)                                 \
319       SymI_HasProto(sqrt)                                \
320       SymI_HasProto(localtime_r)                         \
321       SymI_HasProto(gmtime_r)                            \
322       SymI_HasProto(mktime)                              \
323       SymI_NeedsProto(_imp___tzname)                     \
324       SymI_HasProto(gettimeofday)                        \
325       SymI_HasProto(timezone)                            \
326       SymI_HasProto(tcgetattr)                           \
327       SymI_HasProto(tcsetattr)                           \
328       SymI_HasProto(memcpy)                              \
329       SymI_HasProto(memmove)                             \
330       SymI_HasProto(realloc)                             \
331       SymI_HasProto(malloc)                              \
332       SymI_HasProto(free)                                \
333       SymI_HasProto(fork)                                \
334       SymI_HasProto(lstat)                               \
335       SymI_HasProto(isatty)                              \
336       SymI_HasProto(mkdir)                               \
337       SymI_HasProto(opendir)                             \
338       SymI_HasProto(readdir)                             \
339       SymI_HasProto(rewinddir)                           \
340       SymI_HasProto(closedir)                            \
341       SymI_HasProto(link)                                \
342       SymI_HasProto(mkfifo)                              \
343       SymI_HasProto(pipe)                                \
344       SymI_HasProto(read)                                \
345       SymI_HasProto(rename)                              \
346       SymI_HasProto(rmdir)                               \
347       SymI_HasProto(select)                              \
348       SymI_HasProto(system)                              \
349       SymI_HasProto(write)                               \
350       SymI_HasProto(strcmp)                              \
351       SymI_HasProto(strcpy)                              \
352       SymI_HasProto(strncpy)                             \
353       SymI_HasProto(strerror)                            \
354       SymI_HasProto(sigaddset)                           \
355       SymI_HasProto(sigemptyset)                         \
356       SymI_HasProto(sigprocmask)                         \
357       SymI_HasProto(umask)                               \
358       SymI_HasProto(uname)                               \
359       SymI_HasProto(unlink)                              \
360       SymI_HasProto(utime)                               \
361       SymI_HasProto(waitpid)
362
363 #elif !defined(mingw32_HOST_OS)
364 #define RTS_MINGW_ONLY_SYMBOLS /**/
365 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
366 #else /* defined(mingw32_HOST_OS) */
367 #define RTS_POSIX_ONLY_SYMBOLS  /**/
368 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
369
370 #if HAVE_GETTIMEOFDAY
371 #define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
372 #else
373 #define RTS_MINGW_GETTIMEOFDAY_SYM /**/
374 #endif
375
376 #if HAVE___MINGW_VFPRINTF
377 #define RTS___MINGW_VFPRINTF_SYM SymI_HasProto(__mingw_vfprintf)
378 #else
379 #define RTS___MINGW_VFPRINTF_SYM /**/
380 #endif
381
382 /* These are statically linked from the mingw libraries into the ghc
383    executable, so we have to employ this hack. */
384 #define RTS_MINGW_ONLY_SYMBOLS                           \
385       SymI_HasProto(stg_asyncReadzh)                     \
386       SymI_HasProto(stg_asyncWritezh)                    \
387       SymI_HasProto(stg_asyncDoProczh)                   \
388       SymI_HasProto(memset)                              \
389       SymI_HasProto(inet_ntoa)                           \
390       SymI_HasProto(inet_addr)                           \
391       SymI_HasProto(htonl)                               \
392       SymI_HasProto(recvfrom)                            \
393       SymI_HasProto(listen)                              \
394       SymI_HasProto(bind)                                \
395       SymI_HasProto(shutdown)                            \
396       SymI_HasProto(connect)                             \
397       SymI_HasProto(htons)                               \
398       SymI_HasProto(ntohs)                               \
399       SymI_HasProto(getservbyname)                       \
400       SymI_HasProto(getservbyport)                       \
401       SymI_HasProto(getprotobynumber)                    \
402       SymI_HasProto(getprotobyname)                      \
403       SymI_HasProto(gethostbyname)                       \
404       SymI_HasProto(gethostbyaddr)                       \
405       SymI_HasProto(gethostname)                         \
406       SymI_HasProto(strcpy)                              \
407       SymI_HasProto(strncpy)                             \
408       SymI_HasProto(abort)                               \
409       SymI_NeedsProto(_alloca)                           \
410       SymI_HasProto(isxdigit)                            \
411       SymI_HasProto(isupper)                             \
412       SymI_HasProto(ispunct)                             \
413       SymI_HasProto(islower)                             \
414       SymI_HasProto(isspace)                             \
415       SymI_HasProto(isprint)                             \
416       SymI_HasProto(isdigit)                             \
417       SymI_HasProto(iscntrl)                             \
418       SymI_HasProto(isalpha)                             \
419       SymI_HasProto(isalnum)                             \
420       SymI_HasProto(isascii)                             \
421       RTS___MINGW_VFPRINTF_SYM                           \
422       SymI_HasProto(strcmp)                              \
423       SymI_HasProto(memmove)                             \
424       SymI_HasProto(realloc)                             \
425       SymI_HasProto(malloc)                              \
426       SymI_HasProto(pow)                                 \
427       SymI_HasProto(tanh)                                \
428       SymI_HasProto(cosh)                                \
429       SymI_HasProto(sinh)                                \
430       SymI_HasProto(atan)                                \
431       SymI_HasProto(acos)                                \
432       SymI_HasProto(asin)                                \
433       SymI_HasProto(tan)                                 \
434       SymI_HasProto(cos)                                 \
435       SymI_HasProto(sin)                                 \
436       SymI_HasProto(exp)                                 \
437       SymI_HasProto(log)                                 \
438       SymI_HasProto(sqrt)                                \
439       SymI_HasProto(powf)                                \
440       SymI_HasProto(tanhf)                               \
441       SymI_HasProto(coshf)                               \
442       SymI_HasProto(sinhf)                               \
443       SymI_HasProto(atanf)                               \
444       SymI_HasProto(acosf)                               \
445       SymI_HasProto(asinf)                               \
446       SymI_HasProto(tanf)                                \
447       SymI_HasProto(cosf)                                \
448       SymI_HasProto(sinf)                                \
449       SymI_HasProto(expf)                                \
450       SymI_HasProto(logf)                                \
451       SymI_HasProto(sqrtf)                               \
452       SymI_HasProto(erf)                                 \
453       SymI_HasProto(erfc)                                \
454       SymI_HasProto(erff)                                \
455       SymI_HasProto(erfcf)                               \
456       SymI_HasProto(memcpy)                              \
457       SymI_HasProto(rts_InstallConsoleEvent)             \
458       SymI_HasProto(rts_ConsoleHandlerDone)              \
459       SymI_NeedsProto(mktime)                            \
460       SymI_NeedsProto(_imp___timezone)                   \
461       SymI_NeedsProto(_imp___tzname)                     \
462       SymI_NeedsProto(_imp__tzname)                      \
463       SymI_NeedsProto(_imp___iob)                        \
464       SymI_NeedsProto(_imp___osver)                      \
465       SymI_NeedsProto(localtime)                         \
466       SymI_NeedsProto(gmtime)                            \
467       SymI_NeedsProto(opendir)                           \
468       SymI_NeedsProto(readdir)                           \
469       SymI_NeedsProto(rewinddir)                         \
470       SymI_NeedsProto(_imp____mb_cur_max)                \
471       SymI_NeedsProto(_imp___pctype)                     \
472       SymI_NeedsProto(__chkstk)                          \
473       RTS_MINGW_GETTIMEOFDAY_SYM                         \
474       SymI_NeedsProto(closedir)
475 #endif
476
477
478 #if defined(darwin_HOST_OS) && HAVE_PRINTF_LDBLSTUB
479 #define RTS_DARWIN_ONLY_SYMBOLS                             \
480      SymI_NeedsProto(asprintf$LDBLStub)                     \
481      SymI_NeedsProto(err$LDBLStub)                          \
482      SymI_NeedsProto(errc$LDBLStub)                         \
483      SymI_NeedsProto(errx$LDBLStub)                         \
484      SymI_NeedsProto(fprintf$LDBLStub)                      \
485      SymI_NeedsProto(fscanf$LDBLStub)                       \
486      SymI_NeedsProto(fwprintf$LDBLStub)                     \
487      SymI_NeedsProto(fwscanf$LDBLStub)                      \
488      SymI_NeedsProto(printf$LDBLStub)                       \
489      SymI_NeedsProto(scanf$LDBLStub)                        \
490      SymI_NeedsProto(snprintf$LDBLStub)                     \
491      SymI_NeedsProto(sprintf$LDBLStub)                      \
492      SymI_NeedsProto(sscanf$LDBLStub)                       \
493      SymI_NeedsProto(strtold$LDBLStub)                      \
494      SymI_NeedsProto(swprintf$LDBLStub)                     \
495      SymI_NeedsProto(swscanf$LDBLStub)                      \
496      SymI_NeedsProto(syslog$LDBLStub)                       \
497      SymI_NeedsProto(vasprintf$LDBLStub)                    \
498      SymI_NeedsProto(verr$LDBLStub)                         \
499      SymI_NeedsProto(verrc$LDBLStub)                        \
500      SymI_NeedsProto(verrx$LDBLStub)                        \
501      SymI_NeedsProto(vfprintf$LDBLStub)                     \
502      SymI_NeedsProto(vfscanf$LDBLStub)                      \
503      SymI_NeedsProto(vfwprintf$LDBLStub)                    \
504      SymI_NeedsProto(vfwscanf$LDBLStub)                     \
505      SymI_NeedsProto(vprintf$LDBLStub)                      \
506      SymI_NeedsProto(vscanf$LDBLStub)                       \
507      SymI_NeedsProto(vsnprintf$LDBLStub)                    \
508      SymI_NeedsProto(vsprintf$LDBLStub)                     \
509      SymI_NeedsProto(vsscanf$LDBLStub)                      \
510      SymI_NeedsProto(vswprintf$LDBLStub)                    \
511      SymI_NeedsProto(vswscanf$LDBLStub)                     \
512      SymI_NeedsProto(vsyslog$LDBLStub)                      \
513      SymI_NeedsProto(vwarn$LDBLStub)                        \
514      SymI_NeedsProto(vwarnc$LDBLStub)                       \
515      SymI_NeedsProto(vwarnx$LDBLStub)                       \
516      SymI_NeedsProto(vwprintf$LDBLStub)                     \
517      SymI_NeedsProto(vwscanf$LDBLStub)                      \
518      SymI_NeedsProto(warn$LDBLStub)                         \
519      SymI_NeedsProto(warnc$LDBLStub)                        \
520      SymI_NeedsProto(warnx$LDBLStub)                        \
521      SymI_NeedsProto(wcstold$LDBLStub)                      \
522      SymI_NeedsProto(wprintf$LDBLStub)                      \
523      SymI_NeedsProto(wscanf$LDBLStub)
524 #else
525 #define RTS_DARWIN_ONLY_SYMBOLS
526 #endif
527
528 #ifndef SMP
529 # define MAIN_CAP_SYM SymI_HasProto(MainCapability)
530 #else
531 # define MAIN_CAP_SYM
532 #endif
533
534 #if !defined(mingw32_HOST_OS)
535 #define RTS_USER_SIGNALS_SYMBOLS        \
536    SymI_HasProto(setIOManagerControlFd) \
537    SymI_HasProto(setIOManagerWakeupFd)  \
538    SymI_HasProto(ioManagerWakeup)       \
539    SymI_HasProto(blockUserSignals)      \
540    SymI_HasProto(unblockUserSignals)
541 #else
542 #define RTS_USER_SIGNALS_SYMBOLS        \
543    SymI_HasProto(ioManagerWakeup)       \
544    SymI_HasProto(sendIOManagerEvent)    \
545    SymI_HasProto(readIOManagerEvent)    \
546    SymI_HasProto(getIOManagerEvent)     \
547    SymI_HasProto(console_handler)
548 #endif
549
550 #define RTS_LIBFFI_SYMBOLS                                  \
551      SymE_NeedsProto(ffi_prep_cif)                          \
552      SymE_NeedsProto(ffi_call)                              \
553      SymE_NeedsProto(ffi_type_void)                         \
554      SymE_NeedsProto(ffi_type_float)                        \
555      SymE_NeedsProto(ffi_type_double)                       \
556      SymE_NeedsProto(ffi_type_sint64)                       \
557      SymE_NeedsProto(ffi_type_uint64)                       \
558      SymE_NeedsProto(ffi_type_sint32)                       \
559      SymE_NeedsProto(ffi_type_uint32)                       \
560      SymE_NeedsProto(ffi_type_sint16)                       \
561      SymE_NeedsProto(ffi_type_uint16)                       \
562      SymE_NeedsProto(ffi_type_sint8)                        \
563      SymE_NeedsProto(ffi_type_uint8)                        \
564      SymE_NeedsProto(ffi_type_pointer)
565
566 #ifdef TABLES_NEXT_TO_CODE
567 #define RTS_RET_SYMBOLS /* nothing */
568 #else
569 #define RTS_RET_SYMBOLS                                 \
570       SymI_HasProto(stg_enter_ret)                      \
571       SymI_HasProto(stg_gc_fun_ret)                     \
572       SymI_HasProto(stg_ap_v_ret)                       \
573       SymI_HasProto(stg_ap_f_ret)                       \
574       SymI_HasProto(stg_ap_d_ret)                       \
575       SymI_HasProto(stg_ap_l_ret)                       \
576       SymI_HasProto(stg_ap_n_ret)                       \
577       SymI_HasProto(stg_ap_p_ret)                       \
578       SymI_HasProto(stg_ap_pv_ret)                      \
579       SymI_HasProto(stg_ap_pp_ret)                      \
580       SymI_HasProto(stg_ap_ppv_ret)                     \
581       SymI_HasProto(stg_ap_ppp_ret)                     \
582       SymI_HasProto(stg_ap_pppv_ret)                    \
583       SymI_HasProto(stg_ap_pppp_ret)                    \
584       SymI_HasProto(stg_ap_ppppp_ret)                   \
585       SymI_HasProto(stg_ap_pppppp_ret)
586 #endif
587
588 /* Modules compiled with -ticky may mention ticky counters */
589 /* This list should marry up with the one in $(TOP)/includes/stg/Ticky.h */
590 #define RTS_TICKY_SYMBOLS                               \
591       SymI_NeedsProto(ticky_entry_ctrs)                 \
592       SymI_NeedsProto(top_ct)                           \
593                                                         \
594       SymI_HasProto(ENT_VIA_NODE_ctr)                   \
595       SymI_HasProto(ENT_STATIC_THK_ctr)                 \
596       SymI_HasProto(ENT_DYN_THK_ctr)                    \
597       SymI_HasProto(ENT_STATIC_FUN_DIRECT_ctr)          \
598       SymI_HasProto(ENT_DYN_FUN_DIRECT_ctr)             \
599       SymI_HasProto(ENT_STATIC_CON_ctr)                 \
600       SymI_HasProto(ENT_DYN_CON_ctr)                    \
601       SymI_HasProto(ENT_STATIC_IND_ctr)                 \
602       SymI_HasProto(ENT_DYN_IND_ctr)                    \
603       SymI_HasProto(ENT_PERM_IND_ctr)                   \
604       SymI_HasProto(ENT_PAP_ctr)                        \
605       SymI_HasProto(ENT_AP_ctr)                         \
606       SymI_HasProto(ENT_AP_STACK_ctr)                   \
607       SymI_HasProto(ENT_BH_ctr)                         \
608       SymI_HasProto(UNKNOWN_CALL_ctr)                   \
609       SymI_HasProto(SLOW_CALL_v_ctr)                    \
610       SymI_HasProto(SLOW_CALL_f_ctr)                    \
611       SymI_HasProto(SLOW_CALL_d_ctr)                    \
612       SymI_HasProto(SLOW_CALL_l_ctr)                    \
613       SymI_HasProto(SLOW_CALL_n_ctr)                    \
614       SymI_HasProto(SLOW_CALL_p_ctr)                    \
615       SymI_HasProto(SLOW_CALL_pv_ctr)                   \
616       SymI_HasProto(SLOW_CALL_pp_ctr)                   \
617       SymI_HasProto(SLOW_CALL_ppv_ctr)                  \
618       SymI_HasProto(SLOW_CALL_ppp_ctr)                  \
619       SymI_HasProto(SLOW_CALL_pppv_ctr)                 \
620       SymI_HasProto(SLOW_CALL_pppp_ctr)                 \
621       SymI_HasProto(SLOW_CALL_ppppp_ctr)                \
622       SymI_HasProto(SLOW_CALL_pppppp_ctr)               \
623       SymI_HasProto(SLOW_CALL_OTHER_ctr)                \
624       SymI_HasProto(ticky_slow_call_unevald)            \
625       SymI_HasProto(SLOW_CALL_ctr)                      \
626       SymI_HasProto(MULTI_CHUNK_SLOW_CALL_ctr)          \
627       SymI_HasProto(MULTI_CHUNK_SLOW_CALL_CHUNKS_ctr)   \
628       SymI_HasProto(KNOWN_CALL_ctr)                     \
629       SymI_HasProto(KNOWN_CALL_TOO_FEW_ARGS_ctr)        \
630       SymI_HasProto(KNOWN_CALL_EXTRA_ARGS_ctr)          \
631       SymI_HasProto(SLOW_CALL_FUN_TOO_FEW_ctr)          \
632       SymI_HasProto(SLOW_CALL_FUN_CORRECT_ctr)          \
633       SymI_HasProto(SLOW_CALL_FUN_TOO_MANY_ctr)         \
634       SymI_HasProto(SLOW_CALL_PAP_TOO_FEW_ctr)          \
635       SymI_HasProto(SLOW_CALL_PAP_CORRECT_ctr)          \
636       SymI_HasProto(SLOW_CALL_PAP_TOO_MANY_ctr)         \
637       SymI_HasProto(SLOW_CALL_UNEVALD_ctr)              \
638       SymI_HasProto(UPDF_OMITTED_ctr)                   \
639       SymI_HasProto(UPDF_PUSHED_ctr)                    \
640       SymI_HasProto(CATCHF_PUSHED_ctr)                  \
641       SymI_HasProto(UPDF_RCC_PUSHED_ctr)                \
642       SymI_HasProto(UPDF_RCC_OMITTED_ctr)               \
643       SymI_HasProto(UPD_SQUEEZED_ctr)                   \
644       SymI_HasProto(UPD_CON_IN_NEW_ctr)                 \
645       SymI_HasProto(UPD_CON_IN_PLACE_ctr)               \
646       SymI_HasProto(UPD_PAP_IN_NEW_ctr)                 \
647       SymI_HasProto(UPD_PAP_IN_PLACE_ctr)               \
648       SymI_HasProto(ALLOC_HEAP_ctr)                     \
649       SymI_HasProto(ALLOC_HEAP_tot)                     \
650       SymI_HasProto(ALLOC_FUN_ctr)                      \
651       SymI_HasProto(ALLOC_FUN_adm)                      \
652       SymI_HasProto(ALLOC_FUN_gds)                      \
653       SymI_HasProto(ALLOC_FUN_slp)                      \
654       SymI_HasProto(UPD_NEW_IND_ctr)                    \
655       SymI_HasProto(UPD_NEW_PERM_IND_ctr)               \
656       SymI_HasProto(UPD_OLD_IND_ctr)                    \
657       SymI_HasProto(UPD_OLD_PERM_IND_ctr)               \
658       SymI_HasProto(UPD_BH_UPDATABLE_ctr)               \
659       SymI_HasProto(UPD_BH_SINGLE_ENTRY_ctr)            \
660       SymI_HasProto(UPD_CAF_BH_UPDATABLE_ctr)           \
661       SymI_HasProto(UPD_CAF_BH_SINGLE_ENTRY_ctr)        \
662       SymI_HasProto(GC_SEL_ABANDONED_ctr)               \
663       SymI_HasProto(GC_SEL_MINOR_ctr)                   \
664       SymI_HasProto(GC_SEL_MAJOR_ctr)                   \
665       SymI_HasProto(GC_FAILED_PROMOTION_ctr)            \
666       SymI_HasProto(ALLOC_UP_THK_ctr)                   \
667       SymI_HasProto(ALLOC_SE_THK_ctr)                   \
668       SymI_HasProto(ALLOC_THK_adm)                      \
669       SymI_HasProto(ALLOC_THK_gds)                      \
670       SymI_HasProto(ALLOC_THK_slp)                      \
671       SymI_HasProto(ALLOC_CON_ctr)                      \
672       SymI_HasProto(ALLOC_CON_adm)                      \
673       SymI_HasProto(ALLOC_CON_gds)                      \
674       SymI_HasProto(ALLOC_CON_slp)                      \
675       SymI_HasProto(ALLOC_TUP_ctr)                      \
676       SymI_HasProto(ALLOC_TUP_adm)                      \
677       SymI_HasProto(ALLOC_TUP_gds)                      \
678       SymI_HasProto(ALLOC_TUP_slp)                      \
679       SymI_HasProto(ALLOC_BH_ctr)                       \
680       SymI_HasProto(ALLOC_BH_adm)                       \
681       SymI_HasProto(ALLOC_BH_gds)                       \
682       SymI_HasProto(ALLOC_BH_slp)                       \
683       SymI_HasProto(ALLOC_PRIM_ctr)                     \
684       SymI_HasProto(ALLOC_PRIM_adm)                     \
685       SymI_HasProto(ALLOC_PRIM_gds)                     \
686       SymI_HasProto(ALLOC_PRIM_slp)                     \
687       SymI_HasProto(ALLOC_PAP_ctr)                      \
688       SymI_HasProto(ALLOC_PAP_adm)                      \
689       SymI_HasProto(ALLOC_PAP_gds)                      \
690       SymI_HasProto(ALLOC_PAP_slp)                      \
691       SymI_HasProto(ALLOC_TSO_ctr)                      \
692       SymI_HasProto(ALLOC_TSO_adm)                      \
693       SymI_HasProto(ALLOC_TSO_gds)                      \
694       SymI_HasProto(ALLOC_TSO_slp)                      \
695       SymI_HasProto(RET_NEW_ctr)                        \
696       SymI_HasProto(RET_OLD_ctr)                        \
697       SymI_HasProto(RET_UNBOXED_TUP_ctr)                \
698       SymI_HasProto(RET_SEMI_loads_avoided)
699
700
701 // On most platforms, the garbage collector rewrites references
702 //      to small integer and char objects to a set of common, shared ones.
703 //
704 // We don't do this when compiling to Windows DLLs at the moment because
705 //      it doesn't support cross package data references well.
706 //
707 #if defined(__PIC__) && defined(mingw32_HOST_OS)
708 #define RTS_INTCHAR_SYMBOLS
709 #else
710 #define RTS_INTCHAR_SYMBOLS                             \
711       SymI_HasProto(stg_CHARLIKE_closure)               \
712       SymI_HasProto(stg_INTLIKE_closure)
713 #endif
714
715
716 #define RTS_SYMBOLS                                     \
717       Maybe_Stable_Names                                \
718       RTS_TICKY_SYMBOLS                                 \
719       SymI_HasProto(StgReturn)                          \
720       SymI_HasProto(stg_enter_info)                     \
721       SymI_HasProto(stg_gc_void_info)                   \
722       SymI_HasProto(__stg_gc_enter_1)                   \
723       SymI_HasProto(stg_gc_noregs)                      \
724       SymI_HasProto(stg_gc_unpt_r1_info)                \
725       SymI_HasProto(stg_gc_unpt_r1)                     \
726       SymI_HasProto(stg_gc_unbx_r1_info)                \
727       SymI_HasProto(stg_gc_unbx_r1)                     \
728       SymI_HasProto(stg_gc_f1_info)                     \
729       SymI_HasProto(stg_gc_f1)                          \
730       SymI_HasProto(stg_gc_d1_info)                     \
731       SymI_HasProto(stg_gc_d1)                          \
732       SymI_HasProto(stg_gc_l1_info)                     \
733       SymI_HasProto(stg_gc_l1)                          \
734       SymI_HasProto(__stg_gc_fun)                       \
735       SymI_HasProto(stg_gc_fun_info)                    \
736       SymI_HasProto(stg_gc_gen)                         \
737       SymI_HasProto(stg_gc_gen_info)                    \
738       SymI_HasProto(stg_gc_gen_hp)                      \
739       SymI_HasProto(stg_gc_ut)                          \
740       SymI_HasProto(stg_gen_yield)                      \
741       SymI_HasProto(stg_yield_noregs)                   \
742       SymI_HasProto(stg_yield_to_interpreter)           \
743       SymI_HasProto(stg_gen_block)                      \
744       SymI_HasProto(stg_block_noregs)                   \
745       SymI_HasProto(stg_block_1)                        \
746       SymI_HasProto(stg_block_takemvar)                 \
747       SymI_HasProto(stg_block_putmvar)                  \
748       MAIN_CAP_SYM                                      \
749       SymI_HasProto(MallocFailHook)                     \
750       SymI_HasProto(OnExitHook)                         \
751       SymI_HasProto(OutOfHeapHook)                      \
752       SymI_HasProto(StackOverflowHook)                  \
753       SymI_HasProto(addDLL)                             \
754       SymI_HasProto(__int_encodeDouble)                 \
755       SymI_HasProto(__word_encodeDouble)                \
756       SymI_HasProto(__2Int_encodeDouble)                \
757       SymI_HasProto(__int_encodeFloat)                  \
758       SymI_HasProto(__word_encodeFloat)                 \
759       SymI_HasProto(stg_atomicallyzh)                   \
760       SymI_HasProto(barf)                               \
761       SymI_HasProto(debugBelch)                         \
762       SymI_HasProto(errorBelch)                         \
763       SymI_HasProto(sysErrorBelch)                      \
764       SymI_HasProto(stg_getMaskingStatezh)              \
765       SymI_HasProto(stg_maskAsyncExceptionszh)          \
766       SymI_HasProto(stg_maskUninterruptiblezh)          \
767       SymI_HasProto(stg_catchzh)                        \
768       SymI_HasProto(stg_catchRetryzh)                   \
769       SymI_HasProto(stg_catchSTMzh)                     \
770       SymI_HasProto(stg_checkzh)                        \
771       SymI_HasProto(closure_flags)                      \
772       SymI_HasProto(cmp_thread)                         \
773       SymI_HasProto(createAdjustor)                     \
774       SymI_HasProto(stg_decodeDoublezu2Intzh)           \
775       SymI_HasProto(stg_decodeFloatzuIntzh)             \
776       SymI_HasProto(defaultsHook)                       \
777       SymI_HasProto(stg_delayzh)                        \
778       SymI_HasProto(stg_deRefWeakzh)                    \
779       SymI_HasProto(stg_deRefStablePtrzh)               \
780       SymI_HasProto(dirty_MUT_VAR)                      \
781       SymI_HasProto(stg_forkzh)                         \
782       SymI_HasProto(stg_forkOnzh)                       \
783       SymI_HasProto(forkProcess)                        \
784       SymI_HasProto(forkOS_createThread)                \
785       SymI_HasProto(freeHaskellFunctionPtr)             \
786       SymI_HasProto(getOrSetTypeableStore)              \
787       SymI_HasProto(getOrSetGHCConcSignalSignalHandlerStore)            \
788       SymI_HasProto(getOrSetGHCConcWindowsPendingDelaysStore)           \
789       SymI_HasProto(getOrSetGHCConcWindowsIOManagerThreadStore)         \
790       SymI_HasProto(getOrSetGHCConcWindowsProddingStore)                \
791       SymI_HasProto(getOrSetSystemEventThreadEventManagerStore)         \
792       SymI_HasProto(getOrSetSystemEventThreadIOManagerThreadStore)      \
793       SymI_HasProto(genSymZh)                           \
794       SymI_HasProto(genericRaise)                       \
795       SymI_HasProto(getProgArgv)                        \
796       SymI_HasProto(getFullProgArgv)                    \
797       SymI_HasProto(getStablePtr)                       \
798       SymI_HasProto(hs_init)                            \
799       SymI_HasProto(hs_exit)                            \
800       SymI_HasProto(hs_set_argv)                        \
801       SymI_HasProto(hs_add_root)                        \
802       SymI_HasProto(hs_perform_gc)                      \
803       SymI_HasProto(hs_free_stable_ptr)                 \
804       SymI_HasProto(hs_free_fun_ptr)                    \
805       SymI_HasProto(hs_hpc_rootModule)                  \
806       SymI_HasProto(hs_hpc_module)                      \
807       SymI_HasProto(initLinker)                         \
808       SymI_HasProto(stg_unpackClosurezh)                \
809       SymI_HasProto(stg_getApStackValzh)                \
810       SymI_HasProto(stg_getSparkzh)                     \
811       SymI_HasProto(stg_numSparkszh)                    \
812       SymI_HasProto(stg_isCurrentThreadBoundzh)         \
813       SymI_HasProto(stg_isEmptyMVarzh)                  \
814       SymI_HasProto(stg_killThreadzh)                   \
815       SymI_HasProto(loadArchive)                        \
816       SymI_HasProto(loadObj)                            \
817       SymI_HasProto(insertStableSymbol)                 \
818       SymI_HasProto(insertSymbol)                       \
819       SymI_HasProto(lookupSymbol)                       \
820       SymI_HasProto(stg_makeStablePtrzh)                \
821       SymI_HasProto(stg_mkApUpd0zh)                     \
822       SymI_HasProto(stg_myThreadIdzh)                   \
823       SymI_HasProto(stg_labelThreadzh)                  \
824       SymI_HasProto(stg_newArrayzh)                     \
825       SymI_HasProto(stg_newBCOzh)                       \
826       SymI_HasProto(stg_newByteArrayzh)                 \
827       SymI_HasProto_redirect(newCAF, newDynCAF)         \
828       SymI_HasProto(stg_newMVarzh)                      \
829       SymI_HasProto(stg_newMutVarzh)                    \
830       SymI_HasProto(stg_newTVarzh)                      \
831       SymI_HasProto(stg_noDuplicatezh)                  \
832       SymI_HasProto(stg_atomicModifyMutVarzh)           \
833       SymI_HasProto(stg_newPinnedByteArrayzh)           \
834       SymI_HasProto(stg_newAlignedPinnedByteArrayzh)    \
835       SymI_HasProto(newSpark)                           \
836       SymI_HasProto(performGC)                          \
837       SymI_HasProto(performMajorGC)                     \
838       SymI_HasProto(prog_argc)                          \
839       SymI_HasProto(prog_argv)                          \
840       SymI_HasProto(stg_putMVarzh)                      \
841       SymI_HasProto(stg_raisezh)                        \
842       SymI_HasProto(stg_raiseIOzh)                      \
843       SymI_HasProto(stg_readTVarzh)                     \
844       SymI_HasProto(stg_readTVarIOzh)                   \
845       SymI_HasProto(resumeThread)                       \
846       SymI_HasProto(resolveObjs)                        \
847       SymI_HasProto(stg_retryzh)                        \
848       SymI_HasProto(rts_apply)                          \
849       SymI_HasProto(rts_checkSchedStatus)               \
850       SymI_HasProto(rts_eval)                           \
851       SymI_HasProto(rts_evalIO)                         \
852       SymI_HasProto(rts_evalLazyIO)                     \
853       SymI_HasProto(rts_evalStableIO)                   \
854       SymI_HasProto(rts_eval_)                          \
855       SymI_HasProto(rts_getBool)                        \
856       SymI_HasProto(rts_getChar)                        \
857       SymI_HasProto(rts_getDouble)                      \
858       SymI_HasProto(rts_getFloat)                       \
859       SymI_HasProto(rts_getInt)                         \
860       SymI_HasProto(rts_getInt8)                        \
861       SymI_HasProto(rts_getInt16)                       \
862       SymI_HasProto(rts_getInt32)                       \
863       SymI_HasProto(rts_getInt64)                       \
864       SymI_HasProto(rts_getPtr)                         \
865       SymI_HasProto(rts_getFunPtr)                      \
866       SymI_HasProto(rts_getStablePtr)                   \
867       SymI_HasProto(rts_getThreadId)                    \
868       SymI_HasProto(rts_getWord)                        \
869       SymI_HasProto(rts_getWord8)                       \
870       SymI_HasProto(rts_getWord16)                      \
871       SymI_HasProto(rts_getWord32)                      \
872       SymI_HasProto(rts_getWord64)                      \
873       SymI_HasProto(rts_lock)                           \
874       SymI_HasProto(rts_mkBool)                         \
875       SymI_HasProto(rts_mkChar)                         \
876       SymI_HasProto(rts_mkDouble)                       \
877       SymI_HasProto(rts_mkFloat)                        \
878       SymI_HasProto(rts_mkInt)                          \
879       SymI_HasProto(rts_mkInt8)                         \
880       SymI_HasProto(rts_mkInt16)                        \
881       SymI_HasProto(rts_mkInt32)                        \
882       SymI_HasProto(rts_mkInt64)                        \
883       SymI_HasProto(rts_mkPtr)                          \
884       SymI_HasProto(rts_mkFunPtr)                       \
885       SymI_HasProto(rts_mkStablePtr)                    \
886       SymI_HasProto(rts_mkString)                       \
887       SymI_HasProto(rts_mkWord)                         \
888       SymI_HasProto(rts_mkWord8)                        \
889       SymI_HasProto(rts_mkWord16)                       \
890       SymI_HasProto(rts_mkWord32)                       \
891       SymI_HasProto(rts_mkWord64)                       \
892       SymI_HasProto(rts_unlock)                         \
893       SymI_HasProto(rts_unsafeGetMyCapability)          \
894       SymI_HasProto(rtsSupportsBoundThreads)            \
895       SymI_HasProto(rts_isProfiled)                     \
896       SymI_HasProto(setProgArgv)                        \
897       SymI_HasProto(startupHaskell)                     \
898       SymI_HasProto(shutdownHaskell)                    \
899       SymI_HasProto(shutdownHaskellAndExit)             \
900       SymI_HasProto(stable_ptr_table)                   \
901       SymI_HasProto(stackOverflow)                      \
902       SymI_HasProto(stg_CAF_BLACKHOLE_info)             \
903       SymI_HasProto(stg_BLACKHOLE_info)                 \
904       SymI_HasProto(__stg_EAGER_BLACKHOLE_info)         \
905       SymI_HasProto(stg_BLOCKING_QUEUE_CLEAN_info)      \
906       SymI_HasProto(stg_BLOCKING_QUEUE_DIRTY_info)      \
907       SymI_HasProto(startTimer)                         \
908       SymI_HasProto(stg_MVAR_CLEAN_info)                \
909       SymI_HasProto(stg_MVAR_DIRTY_info)                \
910       SymI_HasProto(stg_IND_STATIC_info)                \
911       SymI_HasProto(stg_ARR_WORDS_info)                 \
912       SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info)        \
913       SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info)       \
914       SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info)      \
915       SymI_HasProto(stg_WEAK_info)                      \
916       SymI_HasProto(stg_ap_v_info)                      \
917       SymI_HasProto(stg_ap_f_info)                      \
918       SymI_HasProto(stg_ap_d_info)                      \
919       SymI_HasProto(stg_ap_l_info)                      \
920       SymI_HasProto(stg_ap_n_info)                      \
921       SymI_HasProto(stg_ap_p_info)                      \
922       SymI_HasProto(stg_ap_pv_info)                     \
923       SymI_HasProto(stg_ap_pp_info)                     \
924       SymI_HasProto(stg_ap_ppv_info)                    \
925       SymI_HasProto(stg_ap_ppp_info)                    \
926       SymI_HasProto(stg_ap_pppv_info)                   \
927       SymI_HasProto(stg_ap_pppp_info)                   \
928       SymI_HasProto(stg_ap_ppppp_info)                  \
929       SymI_HasProto(stg_ap_pppppp_info)                 \
930       SymI_HasProto(stg_ap_0_fast)                      \
931       SymI_HasProto(stg_ap_v_fast)                      \
932       SymI_HasProto(stg_ap_f_fast)                      \
933       SymI_HasProto(stg_ap_d_fast)                      \
934       SymI_HasProto(stg_ap_l_fast)                      \
935       SymI_HasProto(stg_ap_n_fast)                      \
936       SymI_HasProto(stg_ap_p_fast)                      \
937       SymI_HasProto(stg_ap_pv_fast)                     \
938       SymI_HasProto(stg_ap_pp_fast)                     \
939       SymI_HasProto(stg_ap_ppv_fast)                    \
940       SymI_HasProto(stg_ap_ppp_fast)                    \
941       SymI_HasProto(stg_ap_pppv_fast)                   \
942       SymI_HasProto(stg_ap_pppp_fast)                   \
943       SymI_HasProto(stg_ap_ppppp_fast)                  \
944       SymI_HasProto(stg_ap_pppppp_fast)                 \
945       SymI_HasProto(stg_ap_1_upd_info)                  \
946       SymI_HasProto(stg_ap_2_upd_info)                  \
947       SymI_HasProto(stg_ap_3_upd_info)                  \
948       SymI_HasProto(stg_ap_4_upd_info)                  \
949       SymI_HasProto(stg_ap_5_upd_info)                  \
950       SymI_HasProto(stg_ap_6_upd_info)                  \
951       SymI_HasProto(stg_ap_7_upd_info)                  \
952       SymI_HasProto(stg_exit)                           \
953       SymI_HasProto(stg_sel_0_upd_info)                 \
954       SymI_HasProto(stg_sel_10_upd_info)                \
955       SymI_HasProto(stg_sel_11_upd_info)                \
956       SymI_HasProto(stg_sel_12_upd_info)                \
957       SymI_HasProto(stg_sel_13_upd_info)                \
958       SymI_HasProto(stg_sel_14_upd_info)                \
959       SymI_HasProto(stg_sel_15_upd_info)                \
960       SymI_HasProto(stg_sel_1_upd_info)                 \
961       SymI_HasProto(stg_sel_2_upd_info)                 \
962       SymI_HasProto(stg_sel_3_upd_info)                 \
963       SymI_HasProto(stg_sel_4_upd_info)                 \
964       SymI_HasProto(stg_sel_5_upd_info)                 \
965       SymI_HasProto(stg_sel_6_upd_info)                 \
966       SymI_HasProto(stg_sel_7_upd_info)                 \
967       SymI_HasProto(stg_sel_8_upd_info)                 \
968       SymI_HasProto(stg_sel_9_upd_info)                 \
969       SymI_HasProto(stg_upd_frame_info)                 \
970       SymI_HasProto(stg_bh_upd_frame_info)              \
971       SymI_HasProto(suspendThread)                      \
972       SymI_HasProto(stg_takeMVarzh)                     \
973       SymI_HasProto(stg_threadStatuszh)                 \
974       SymI_HasProto(stg_tryPutMVarzh)                   \
975       SymI_HasProto(stg_tryTakeMVarzh)                  \
976       SymI_HasProto(stg_unmaskAsyncExceptionszh)        \
977       SymI_HasProto(unloadObj)                          \
978       SymI_HasProto(stg_unsafeThawArrayzh)              \
979       SymI_HasProto(stg_waitReadzh)                     \
980       SymI_HasProto(stg_waitWritezh)                    \
981       SymI_HasProto(stg_writeTVarzh)                    \
982       SymI_HasProto(stg_yieldzh)                        \
983       SymI_NeedsProto(stg_interp_constr_entry)          \
984       SymI_HasProto(stg_arg_bitmaps)                    \
985       SymI_HasProto(large_alloc_lim)                    \
986       SymI_HasProto(g0)                                 \
987       SymI_HasProto(allocate)                           \
988       SymI_HasProto(allocateExec)                       \
989       SymI_HasProto(freeExec)                           \
990       SymI_HasProto(getAllocations)                     \
991       SymI_HasProto(revertCAFs)                         \
992       SymI_HasProto(RtsFlags)                           \
993       SymI_NeedsProto(rts_breakpoint_io_action)         \
994       SymI_NeedsProto(rts_stop_next_breakpoint)         \
995       SymI_NeedsProto(rts_stop_on_exception)            \
996       SymI_HasProto(stopTimer)                          \
997       SymI_HasProto(n_capabilities)                     \
998       SymI_HasProto(stg_traceCcszh)                     \
999       SymI_HasProto(stg_traceEventzh)                   \
1000       RTS_USER_SIGNALS_SYMBOLS                          \
1001       RTS_INTCHAR_SYMBOLS
1002
1003
1004 // 64-bit support functions in libgcc.a
1005 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
1006 #define RTS_LIBGCC_SYMBOLS                             \
1007       SymI_NeedsProto(__divdi3)                        \
1008       SymI_NeedsProto(__udivdi3)                       \
1009       SymI_NeedsProto(__moddi3)                        \
1010       SymI_NeedsProto(__umoddi3)                       \
1011       SymI_NeedsProto(__muldi3)                        \
1012       SymI_NeedsProto(__ashldi3)                       \
1013       SymI_NeedsProto(__ashrdi3)                       \
1014       SymI_NeedsProto(__lshrdi3)
1015 #else
1016 #define RTS_LIBGCC_SYMBOLS
1017 #endif
1018
1019 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
1020       // Symbols that don't have a leading underscore
1021       // on Mac OS X. They have to receive special treatment,
1022       // see machoInitSymbolsWithoutUnderscore()
1023 #define RTS_MACHO_NOUNDERLINE_SYMBOLS                   \
1024       SymI_NeedsProto(saveFP)                           \
1025       SymI_NeedsProto(restFP)
1026 #endif
1027
1028 /* entirely bogus claims about types of these symbols */
1029 #define SymI_NeedsProto(vvv)  extern void vvv(void);
1030 #if defined(__PIC__) && defined(mingw32_HOST_OS)
1031 #define SymE_HasProto(vvv)    SymE_HasProto(vvv);
1032 #define SymE_NeedsProto(vvv)    extern void _imp__ ## vvv (void);
1033 #else
1034 #define SymE_NeedsProto(vvv)  SymI_NeedsProto(vvv);
1035 #define SymE_HasProto(vvv)    SymI_HasProto(vvv)
1036 #endif
1037 #define SymI_HasProto(vvv) /**/
1038 #define SymI_HasProto_redirect(vvv,xxx) /**/
1039 RTS_SYMBOLS
1040 RTS_RET_SYMBOLS
1041 RTS_POSIX_ONLY_SYMBOLS
1042 RTS_MINGW_ONLY_SYMBOLS
1043 RTS_CYGWIN_ONLY_SYMBOLS
1044 RTS_DARWIN_ONLY_SYMBOLS
1045 RTS_LIBGCC_SYMBOLS
1046 RTS_LIBFFI_SYMBOLS
1047 #undef SymI_NeedsProto
1048 #undef SymI_HasProto
1049 #undef SymI_HasProto_redirect
1050 #undef SymE_HasProto
1051 #undef SymE_NeedsProto
1052
1053 #ifdef LEADING_UNDERSCORE
1054 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
1055 #else
1056 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
1057 #endif
1058
1059 #define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1060                     (void*)(&(vvv)) },
1061 #define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1062             (void*)DLL_IMPORT_DATA_REF(vvv) },
1063
1064 #define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
1065 #define SymE_NeedsProto(vvv) SymE_HasProto(vvv)
1066
1067 // SymI_HasProto_redirect allows us to redirect references to one symbol to
1068 // another symbol.  See newCAF/newDynCAF for an example.
1069 #define SymI_HasProto_redirect(vvv,xxx)   \
1070     { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1071       (void*)(&(xxx)) },
1072
1073 static RtsSymbolVal rtsSyms[] = {
1074       RTS_SYMBOLS
1075       RTS_RET_SYMBOLS
1076       RTS_POSIX_ONLY_SYMBOLS
1077       RTS_MINGW_ONLY_SYMBOLS
1078       RTS_CYGWIN_ONLY_SYMBOLS
1079       RTS_DARWIN_ONLY_SYMBOLS
1080       RTS_LIBGCC_SYMBOLS
1081       RTS_LIBFFI_SYMBOLS
1082 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
1083       // dyld stub code contains references to this,
1084       // but it should never be called because we treat
1085       // lazy pointers as nonlazy.
1086       { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
1087 #endif
1088       { 0, 0 } /* sentinel */
1089 };
1090
1091
1092
1093 /* -----------------------------------------------------------------------------
1094  * Insert symbols into hash tables, checking for duplicates.
1095  */
1096
1097 static void ghciInsertStrHashTable ( char* obj_name,
1098                                      HashTable *table,
1099                                      char* key,
1100                                      void *data
1101                                    )
1102 {
1103    if (lookupHashTable(table, (StgWord)key) == NULL)
1104    {
1105       insertStrHashTable(table, (StgWord)key, data);
1106       return;
1107    }
1108    debugBelch(
1109       "\n\n"
1110       "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
1111       "   %s\n"
1112       "whilst processing object file\n"
1113       "   %s\n"
1114       "This could be caused by:\n"
1115       "   * Loading two different object files which export the same symbol\n"
1116       "   * Specifying the same object file twice on the GHCi command line\n"
1117       "   * An incorrect `package.conf' entry, causing some object to be\n"
1118       "     loaded twice.\n"
1119       "GHCi cannot safely continue in this situation.  Exiting now.  Sorry.\n"
1120       "\n",
1121       (char*)key,
1122       obj_name
1123    );
1124    stg_exit(1);
1125 }
1126 /* -----------------------------------------------------------------------------
1127  * initialize the object linker
1128  */
1129
1130
1131 static int linker_init_done = 0 ;
1132
1133 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1134 static void *dl_prog_handle;
1135 static regex_t re_invalid;
1136 static regex_t re_realso;
1137 #ifdef THREADED_RTS
1138 static Mutex dl_mutex; // mutex to protect dlopen/dlerror critical section
1139 #endif
1140 #endif
1141
1142 void
1143 initLinker( void )
1144 {
1145     RtsSymbolVal *sym;
1146 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1147     int compileResult;
1148 #endif
1149
1150     IF_DEBUG(linker, debugBelch("initLinker: start\n"));
1151
1152     /* Make initLinker idempotent, so we can call it
1153        before evey relevant operation; that means we
1154        don't need to initialise the linker separately */
1155     if (linker_init_done == 1) {
1156         IF_DEBUG(linker, debugBelch("initLinker: idempotent return\n"));
1157         return;
1158     } else {
1159         linker_init_done = 1;
1160     }
1161
1162 #if defined(THREADED_RTS) && (defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO))
1163     initMutex(&dl_mutex);
1164 #endif
1165     stablehash = allocStrHashTable();
1166     symhash = allocStrHashTable();
1167
1168     /* populate the symbol table with stuff from the RTS */
1169     for (sym = rtsSyms; sym->lbl != NULL; sym++) {
1170         ghciInsertStrHashTable("(GHCi built-in symbols)",
1171                                symhash, sym->lbl, sym->addr);
1172         IF_DEBUG(linker, debugBelch("initLinker: inserting rts symbol %s, %p\n", sym->lbl, sym->addr));
1173     }
1174 #   if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1175     machoInitSymbolsWithoutUnderscore();
1176 #   endif
1177
1178 #   if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1179 #   if defined(RTLD_DEFAULT)
1180     dl_prog_handle = RTLD_DEFAULT;
1181 #   else
1182     dl_prog_handle = dlopen(NULL, RTLD_LAZY);
1183 #   endif /* RTLD_DEFAULT */
1184
1185     compileResult = regcomp(&re_invalid,
1186            "(([^ \t()])+\\.so([^ \t:()])*):([ \t])*invalid ELF header",
1187            REG_EXTENDED);
1188     ASSERT( compileResult == 0 );
1189     compileResult = regcomp(&re_realso,
1190            "GROUP *\\( *(([^ )])+)",
1191            REG_EXTENDED);
1192     ASSERT( compileResult == 0 );
1193 #   endif
1194
1195 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1196     if (RtsFlags.MiscFlags.linkerMemBase != 0) {
1197         // User-override for mmap_32bit_base
1198         mmap_32bit_base = (void*)RtsFlags.MiscFlags.linkerMemBase;
1199     }
1200 #endif
1201
1202 #if defined(mingw32_HOST_OS)
1203     /*
1204      * These two libraries cause problems when added to the static link,
1205      * but are necessary for resolving symbols in GHCi, hence we load
1206      * them manually here.
1207      */
1208     addDLL("msvcrt");
1209     addDLL("kernel32");
1210 #endif
1211
1212     IF_DEBUG(linker, debugBelch("initLinker: done\n"));
1213     return;
1214 }
1215
1216 void
1217 exitLinker( void ) {
1218 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1219    if (linker_init_done == 1) {
1220       regfree(&re_invalid);
1221       regfree(&re_realso);
1222 #ifdef THREADED_RTS
1223       closeMutex(&dl_mutex);
1224 #endif
1225    }
1226 #endif
1227 }
1228
1229 /* -----------------------------------------------------------------------------
1230  *                  Loading DLL or .so dynamic libraries
1231  * -----------------------------------------------------------------------------
1232  *
1233  * Add a DLL from which symbols may be found.  In the ELF case, just
1234  * do RTLD_GLOBAL-style add, so no further messing around needs to
1235  * happen in order that symbols in the loaded .so are findable --
1236  * lookupSymbol() will subsequently see them by dlsym on the program's
1237  * dl-handle.  Returns NULL if success, otherwise ptr to an err msg.
1238  *
1239  * In the PEi386 case, open the DLLs and put handles to them in a
1240  * linked list.  When looking for a symbol, try all handles in the
1241  * list.  This means that we need to load even DLLs that are guaranteed
1242  * to be in the ghc.exe image already, just so we can get a handle
1243  * to give to loadSymbol, so that we can find the symbols.  For such
1244  * libraries, the LoadLibrary call should be a no-op except for returning
1245  * the handle.
1246  *
1247  */
1248
1249 #if defined(OBJFORMAT_PEi386)
1250 /* A record for storing handles into DLLs. */
1251
1252 typedef
1253    struct _OpenedDLL {
1254       char*              name;
1255       struct _OpenedDLL* next;
1256       HINSTANCE instance;
1257    }
1258    OpenedDLL;
1259
1260 /* A list thereof. */
1261 static OpenedDLL* opened_dlls = NULL;
1262 #endif
1263
1264 #  if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1265
1266 static const char *
1267 internal_dlopen(const char *dll_name)
1268 {
1269    void *hdl;
1270    const char *errmsg;
1271    char *errmsg_copy;
1272
1273    // omitted: RTLD_NOW
1274    // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
1275    IF_DEBUG(linker,
1276       debugBelch("internal_dlopen: dll_name = '%s'\n", dll_name));
1277
1278    //-------------- Begin critical section ------------------
1279    // This critical section is necessary because dlerror() is not
1280    // required to be reentrant (see POSIX -- IEEE Std 1003.1-2008)
1281    // Also, the error message returned must be copied to preserve it
1282    // (see POSIX also)
1283
1284    ACQUIRE_LOCK(&dl_mutex);
1285    hdl = dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
1286
1287    errmsg = NULL;
1288    if (hdl == NULL) {
1289       /* dlopen failed; return a ptr to the error msg. */
1290       errmsg = dlerror();
1291       if (errmsg == NULL) errmsg = "addDLL: unknown error";
1292       errmsg_copy = stgMallocBytes(strlen(errmsg)+1, "addDLL");
1293       strcpy(errmsg_copy, errmsg);
1294       errmsg = errmsg_copy;
1295    }
1296    RELEASE_LOCK(&dl_mutex);
1297    //--------------- End critical section -------------------
1298
1299    return errmsg;
1300 }
1301 #  endif
1302
1303 const char *
1304 addDLL( char *dll_name )
1305 {
1306 #  if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1307    /* ------------------- ELF DLL loader ------------------- */
1308
1309 #define NMATCH 5
1310    regmatch_t match[NMATCH];
1311    const char *errmsg;
1312    FILE* fp;
1313    size_t match_length;
1314 #define MAXLINE 1000
1315    char line[MAXLINE];
1316    int result;
1317
1318    initLinker();
1319
1320    IF_DEBUG(linker, debugBelch("addDLL: dll_name = '%s'\n", dll_name));
1321    errmsg = internal_dlopen(dll_name);
1322
1323    if (errmsg == NULL) {
1324       return NULL;
1325    }
1326
1327    // GHC Trac ticket #2615
1328    // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so)
1329    // contain linker scripts rather than ELF-format object code. This
1330    // code handles the situation by recognizing the real object code
1331    // file name given in the linker script.
1332    //
1333    // If an "invalid ELF header" error occurs, it is assumed that the
1334    // .so file contains a linker script instead of ELF object code.
1335    // In this case, the code looks for the GROUP ( ... ) linker
1336    // directive. If one is found, the first file name inside the
1337    // parentheses is treated as the name of a dynamic library and the
1338    // code attempts to dlopen that file. If this is also unsuccessful,
1339    // an error message is returned.
1340
1341    // see if the error message is due to an invalid ELF header
1342    IF_DEBUG(linker, debugBelch("errmsg = '%s'\n", errmsg));
1343    result = regexec(&re_invalid, errmsg, (size_t) NMATCH, match, 0);
1344    IF_DEBUG(linker, debugBelch("result = %i\n", result));
1345    if (result == 0) {
1346       // success -- try to read the named file as a linker script
1347       match_length = (size_t) stg_min((match[1].rm_eo - match[1].rm_so),
1348                                  MAXLINE-1);
1349       strncpy(line, (errmsg+(match[1].rm_so)),match_length);
1350       line[match_length] = '\0'; // make sure string is null-terminated
1351       IF_DEBUG(linker, debugBelch ("file name = '%s'\n", line));
1352       if ((fp = fopen(line, "r")) == NULL) {
1353          return errmsg; // return original error if open fails
1354       }
1355       // try to find a GROUP ( ... ) command
1356       while (fgets(line, MAXLINE, fp) != NULL) {
1357          IF_DEBUG(linker, debugBelch("input line = %s", line));
1358          if (regexec(&re_realso, line, (size_t) NMATCH, match, 0) == 0) {
1359             // success -- try to dlopen the first named file
1360             IF_DEBUG(linker, debugBelch("match%s\n",""));
1361             line[match[1].rm_eo] = '\0';
1362             errmsg = internal_dlopen(line+match[1].rm_so);
1363             break;
1364          }
1365          // if control reaches here, no GROUP ( ... ) directive was found
1366          // and the original error message is returned to the caller
1367       }
1368       fclose(fp);
1369    }
1370    return errmsg;
1371
1372 #  elif defined(OBJFORMAT_PEi386)
1373    /* ------------------- Win32 DLL loader ------------------- */
1374
1375    char*      buf;
1376    OpenedDLL* o_dll;
1377    HINSTANCE  instance;
1378
1379    initLinker();
1380
1381    /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1382
1383    /* See if we've already got it, and ignore if so. */
1384    for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1385       if (0 == strcmp(o_dll->name, dll_name))
1386          return NULL;
1387    }
1388
1389    /* The file name has no suffix (yet) so that we can try
1390       both foo.dll and foo.drv
1391
1392       The documentation for LoadLibrary says:
1393         If no file name extension is specified in the lpFileName
1394         parameter, the default library extension .dll is
1395         appended. However, the file name string can include a trailing
1396         point character (.) to indicate that the module name has no
1397         extension. */
1398
1399    buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1400    sprintf(buf, "%s.DLL", dll_name);
1401    instance = LoadLibrary(buf);
1402    if (instance == NULL) {
1403        if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1404        // KAA: allow loading of drivers (like winspool.drv)
1405        sprintf(buf, "%s.DRV", dll_name);
1406        instance = LoadLibrary(buf);
1407        if (instance == NULL) {
1408            if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1409            // #1883: allow loading of unix-style libfoo.dll DLLs
1410            sprintf(buf, "lib%s.DLL", dll_name);
1411            instance = LoadLibrary(buf);
1412            if (instance == NULL) {
1413                goto error;
1414            }
1415        }
1416    }
1417    stgFree(buf);
1418
1419    /* Add this DLL to the list of DLLs in which to search for symbols. */
1420    o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1421    o_dll->name     = stgMallocBytes(1+strlen(dll_name), "addDLL");
1422    strcpy(o_dll->name, dll_name);
1423    o_dll->instance = instance;
1424    o_dll->next     = opened_dlls;
1425    opened_dlls     = o_dll;
1426
1427    return NULL;
1428
1429 error:
1430    stgFree(buf);
1431    sysErrorBelch(dll_name);
1432
1433    /* LoadLibrary failed; return a ptr to the error msg. */
1434    return "addDLL: could not load DLL";
1435
1436 #  else
1437    barf("addDLL: not implemented on this platform");
1438 #  endif
1439 }
1440
1441 /* -----------------------------------------------------------------------------
1442  * insert a stable symbol in the hash table
1443  */
1444
1445 void
1446 insertStableSymbol(char* obj_name, char* key, StgPtr p)
1447 {
1448   ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
1449 }
1450
1451
1452 /* -----------------------------------------------------------------------------
1453  * insert a symbol in the hash table
1454  */
1455 void
1456 insertSymbol(char* obj_name, char* key, void* data)
1457 {
1458   ghciInsertStrHashTable(obj_name, symhash, key, data);
1459 }
1460
1461 /* -----------------------------------------------------------------------------
1462  * lookup a symbol in the hash table
1463  */
1464 void *
1465 lookupSymbol( char *lbl )
1466 {
1467     void *val;
1468     IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s\n", lbl));
1469     initLinker() ;
1470     ASSERT(symhash != NULL);
1471     val = lookupStrHashTable(symhash, lbl);
1472
1473     if (val == NULL) {
1474         IF_DEBUG(linker, debugBelch("lookupSymbol: symbol not found\n"));
1475 #       if defined(OBJFORMAT_ELF)
1476         return dlsym(dl_prog_handle, lbl);
1477 #       elif defined(OBJFORMAT_MACHO)
1478 #       if HAVE_DLFCN_H
1479         /* On OS X 10.3 and later, we use dlsym instead of the old legacy
1480            interface.
1481
1482            HACK: On OS X, global symbols are prefixed with an underscore.
1483                  However, dlsym wants us to omit the leading underscore from the
1484                  symbol name. For now, we simply strip it off here (and ONLY
1485                  here).
1486         */
1487         IF_DEBUG(linker, debugBelch("lookupSymbol: looking up %s with dlsym\n", lbl));
1488         ASSERT(lbl[0] == '_');
1489         return dlsym(dl_prog_handle, lbl+1);
1490 #       else
1491         if(NSIsSymbolNameDefined(lbl)) {
1492             NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1493             return NSAddressOfSymbol(symbol);
1494         } else {
1495             return NULL;
1496         }
1497 #       endif /* HAVE_DLFCN_H */
1498 #       elif defined(OBJFORMAT_PEi386)
1499         void* sym;
1500
1501         sym = lookupSymbolInDLLs((unsigned char*)lbl);
1502         if (sym != NULL) { return sym; };
1503
1504         // Also try looking up the symbol without the @N suffix.  Some
1505         // DLLs have the suffixes on their symbols, some don't.
1506         zapTrailingAtSign ( (unsigned char*)lbl );
1507         sym = lookupSymbolInDLLs((unsigned char*)lbl);
1508         if (sym != NULL) { return sym; };
1509         return NULL;
1510
1511 #       else
1512         ASSERT(2+2 == 5);
1513         return NULL;
1514 #       endif
1515     } else {
1516         IF_DEBUG(linker, debugBelch("lookupSymbol: value of %s is %p\n", lbl, val));
1517         return val;
1518     }
1519 }
1520
1521 /* -----------------------------------------------------------------------------
1522  * Debugging aid: look in GHCi's object symbol tables for symbols
1523  * within DELTA bytes of the specified address, and show their names.
1524  */
1525 #ifdef DEBUG
1526 void ghci_enquire ( char* addr );
1527
1528 void ghci_enquire ( char* addr )
1529 {
1530    int   i;
1531    char* sym;
1532    char* a;
1533    const int DELTA = 64;
1534    ObjectCode* oc;
1535
1536    initLinker();
1537
1538    for (oc = objects; oc; oc = oc->next) {
1539       for (i = 0; i < oc->n_symbols; i++) {
1540          sym = oc->symbols[i];
1541          if (sym == NULL) continue;
1542          a = NULL;
1543          if (a == NULL) {
1544             a = lookupStrHashTable(symhash, sym);
1545          }
1546          if (a == NULL) {
1547              // debugBelch("ghci_enquire: can't find %s\n", sym);
1548          }
1549          else if (addr-DELTA <= a && a <= addr+DELTA) {
1550             debugBelch("%p + %3d  ==  `%s'\n", addr, (int)(a - addr), sym);
1551          }
1552       }
1553    }
1554 }
1555 #endif
1556
1557 #ifdef USE_MMAP
1558 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1559
1560 static void *
1561 mmapForLinker (size_t bytes, nat flags, int fd)
1562 {
1563    void *map_addr = NULL;
1564    void *result;
1565    int pagesize, size;
1566    static nat fixed = 0;
1567
1568    IF_DEBUG(linker, debugBelch("mmapForLinker: start\n"));
1569    pagesize = getpagesize();
1570    size = ROUND_UP(bytes, pagesize);
1571
1572 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1573 mmap_again:
1574
1575    if (mmap_32bit_base != 0) {
1576        map_addr = mmap_32bit_base;
1577    }
1578 #endif
1579
1580    IF_DEBUG(linker, debugBelch("mmapForLinker: \tprotection %#0x\n", PROT_EXEC | PROT_READ | PROT_WRITE));
1581    IF_DEBUG(linker, debugBelch("mmapForLinker: \tflags      %#0x\n", MAP_PRIVATE | TRY_MAP_32BIT | fixed | flags));
1582    result = mmap(map_addr, size, PROT_EXEC|PROT_READ|PROT_WRITE,
1583                     MAP_PRIVATE|TRY_MAP_32BIT|fixed|flags, fd, 0);
1584
1585    if (result == MAP_FAILED) {
1586        sysErrorBelch("mmap %lu bytes at %p",(lnat)size,map_addr);
1587        errorBelch("Try specifying an address with +RTS -xm<addr> -RTS");
1588        stg_exit(EXIT_FAILURE);
1589    }
1590
1591 #if !defined(ALWAYS_PIC) && defined(x86_64_HOST_ARCH)
1592    if (mmap_32bit_base != 0) {
1593        if (result == map_addr) {
1594            mmap_32bit_base = (StgWord8*)map_addr + size;
1595        } else {
1596            if ((W_)result > 0x80000000) {
1597                // oops, we were given memory over 2Gb
1598 #if defined(freebsd_HOST_OS)  || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS)
1599                // Some platforms require MAP_FIXED.  This is normally
1600                // a bad idea, because MAP_FIXED will overwrite
1601                // existing mappings.
1602                munmap(result,size);
1603                fixed = MAP_FIXED;
1604                goto mmap_again;
1605 #else
1606                barf("loadObj: failed to mmap() memory below 2Gb; asked for %lu bytes at %p.  Try specifying an address with +RTS -xm<addr> -RTS", size, map_addr, result);
1607 #endif
1608            } else {
1609                // hmm, we were given memory somewhere else, but it's
1610                // still under 2Gb so we can use it.  Next time, ask
1611                // for memory right after the place we just got some
1612                mmap_32bit_base = (StgWord8*)result + size;
1613            }
1614        }
1615    } else {
1616        if ((W_)result > 0x80000000) {
1617            // oops, we were given memory over 2Gb
1618            // ... try allocating memory somewhere else?;
1619            debugTrace(DEBUG_linker,"MAP_32BIT didn't work; gave us %lu bytes at 0x%p", bytes, result);
1620            munmap(result, size);
1621
1622            // Set a base address and try again... (guess: 1Gb)
1623            mmap_32bit_base = (void*)0x40000000;
1624            goto mmap_again;
1625        }
1626    }
1627 #endif
1628
1629    IF_DEBUG(linker, debugBelch("mmapForLinker: mapped %lu bytes starting at %p\n", (lnat)size, result));
1630    IF_DEBUG(linker, debugBelch("mmapForLinker: done\n"));
1631    return result;
1632 }
1633 #endif // USE_MMAP
1634
1635 static ObjectCode*
1636 mkOc( char *path, char *image, int imageSize,
1637       char *archiveMemberName
1638 #ifndef USE_MMAP
1639 #ifdef darwin_HOST_OS
1640     , int misalignment
1641 #endif
1642 #endif
1643     ) {
1644    ObjectCode* oc;
1645
1646    IF_DEBUG(linker, debugBelch("mkOc: start\n"));
1647    oc = stgMallocBytes(sizeof(ObjectCode), "loadArchive(oc)");
1648
1649 #  if defined(OBJFORMAT_ELF)
1650    oc->formatName = "ELF";
1651 #  elif defined(OBJFORMAT_PEi386)
1652    oc->formatName = "PEi386";
1653 #  elif defined(OBJFORMAT_MACHO)
1654    oc->formatName = "Mach-O";
1655 #  else
1656    stgFree(oc);
1657    barf("loadObj: not implemented on this platform");
1658 #  endif
1659
1660    oc->image = image;
1661    /* sigh, strdup() isn't a POSIX function, so do it the long way */
1662    oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1663    strcpy(oc->fileName, path);
1664
1665    if (archiveMemberName) {
1666        oc->archiveMemberName = stgMallocBytes( strlen(archiveMemberName)+1, "loadObj" );
1667        strcpy(oc->archiveMemberName, archiveMemberName);
1668    }
1669    else {
1670        oc->archiveMemberName = NULL;
1671    }
1672
1673    oc->fileSize          = imageSize;
1674    oc->symbols           = NULL;
1675    oc->sections          = NULL;
1676    oc->proddables        = NULL;
1677
1678 #ifndef USE_MMAP
1679 #ifdef darwin_HOST_OS
1680    oc->misalignment = misalignment;
1681 #endif
1682 #endif
1683
1684    /* chain it onto the list of objects */
1685    oc->next              = objects;
1686    objects               = oc;
1687
1688    IF_DEBUG(linker, debugBelch("mkOc: done\n"));
1689    return oc;
1690 }
1691
1692 HsInt
1693 loadArchive( char *path )
1694 {
1695     ObjectCode* oc;
1696     char *image;
1697     int memberSize;
1698     FILE *f;
1699     int n;
1700     size_t thisFileNameSize;
1701     char *fileName;
1702     size_t fileNameSize;
1703     int isObject, isGnuIndex;
1704     char tmp[12];
1705     char *gnuFileIndex;
1706     int gnuFileIndexSize;
1707 #if !defined(USE_MMAP) && defined(darwin_HOST_OS)
1708     int misalignment;
1709 #endif
1710
1711     IF_DEBUG(linker, debugBelch("loadArchive: start\n"));
1712     IF_DEBUG(linker, debugBelch("loadArchive: Loading archive `%s'\n", path));
1713
1714     gnuFileIndex = NULL;
1715     gnuFileIndexSize = 0;
1716
1717     fileNameSize = 32;
1718     fileName = stgMallocBytes(fileNameSize, "loadArchive(fileName)");
1719
1720     f = fopen(path, "rb");
1721     if (!f)
1722         barf("loadObj: can't read `%s'", path);
1723
1724     n = fread ( tmp, 1, 8, f );
1725     if (strncmp(tmp, "!<arch>\n", 8) != 0) {
1726         barf("loadArchive: Not an archive: `%s'", path);
1727     }
1728
1729     IF_DEBUG(linker, debugBelch("loadArchive: loading archive contents\n"));
1730
1731     while(1) {
1732         n = fread ( fileName, 1, 16, f );
1733         if (n != 16) {
1734             if (feof(f)) {
1735                 IF_DEBUG(linker, debugBelch("loadArchive: EOF while reading from '%s'\n", path));
1736                 break;
1737             }
1738             else {
1739                 barf("loadArchive: Failed reading file name from `%s'", path);
1740             }
1741         }
1742 #if defined(darwin_HOST_OS)
1743         else {
1744             if (strncmp(fileName, "!<arch>\n", 8) == 0) {
1745                 IF_DEBUG(linker, debugBelch("loadArchive: found the start of another archive, breaking\n"));
1746                 break;
1747             }
1748         }
1749 #endif
1750
1751         n = fread ( tmp, 1, 12, f );
1752         if (n != 12)
1753             barf("loadArchive: Failed reading mod time from `%s'", path);
1754         n = fread ( tmp, 1, 6, f );
1755         if (n != 6)
1756             barf("loadArchive: Failed reading owner from `%s'", path);
1757         n = fread ( tmp, 1, 6, f );
1758         if (n != 6)
1759             barf("loadArchive: Failed reading group from `%s'", path);
1760         n = fread ( tmp, 1, 8, f );
1761         if (n != 8)
1762             barf("loadArchive: Failed reading mode from `%s'", path);
1763         n = fread ( tmp, 1, 10, f );
1764         if (n != 10)
1765             barf("loadArchive: Failed reading size from `%s'", path);
1766         tmp[10] = '\0';
1767         for (n = 0; isdigit(tmp[n]); n++);
1768         tmp[n] = '\0';
1769         memberSize = atoi(tmp);
1770
1771         IF_DEBUG(linker, debugBelch("loadArchive: size of this archive member is %d\n", memberSize));
1772         n = fread ( tmp, 1, 2, f );
1773         if (strncmp(tmp, "\x60\x0A", 2) != 0)
1774             barf("loadArchive: Failed reading magic from `%s' at %ld. Got %c%c",
1775                  path, ftell(f), tmp[0], tmp[1]);
1776
1777         isGnuIndex = 0;
1778         /* Check for BSD-variant large filenames */
1779         if (0 == strncmp(fileName, "#1/", 3)) {
1780             fileName[16] = '\0';
1781             if (isdigit(fileName[3])) {
1782                 for (n = 4; isdigit(fileName[n]); n++);
1783                 fileName[n] = '\0';
1784                 thisFileNameSize = atoi(fileName + 3);
1785                 memberSize -= thisFileNameSize;
1786                 if (thisFileNameSize >= fileNameSize) {
1787                     /* Double it to avoid potentially continually
1788                        increasing it by 1 */
1789                     fileNameSize = thisFileNameSize * 2;
1790                     fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1791                 }
1792                 n = fread ( fileName, 1, thisFileNameSize, f );
1793                 if (n != (int)thisFileNameSize) {
1794                     barf("loadArchive: Failed reading filename from `%s'",
1795                          path);
1796                 }
1797                 fileName[thisFileNameSize] = 0;
1798
1799                 /* On OS X at least, thisFileNameSize is the size of the
1800                    fileName field, not the length of the fileName
1801                    itself. */
1802                 thisFileNameSize = strlen(fileName);
1803             }
1804             else {
1805                 barf("loadArchive: BSD-variant filename size not found while reading filename from `%s'", path);
1806             }
1807         }
1808         /* Check for GNU file index file */
1809         else if (0 == strncmp(fileName, "//", 2)) {
1810             fileName[0] = '\0';
1811             thisFileNameSize = 0;
1812             isGnuIndex = 1;
1813         }
1814         /* Check for a file in the GNU file index */
1815         else if (fileName[0] == '/') {
1816             if (isdigit(fileName[1])) {
1817                 int i;
1818
1819                 for (n = 2; isdigit(fileName[n]); n++);
1820                 fileName[n] = '\0';
1821                 n = atoi(fileName + 1);
1822
1823                 if (gnuFileIndex == NULL) {
1824                     barf("loadArchive: GNU-variant filename without an index while reading from `%s'", path);
1825                 }
1826                 if (n < 0 || n > gnuFileIndexSize) {
1827                     barf("loadArchive: GNU-variant filename offset %d out of range [0..%d] while reading filename from `%s'", n, gnuFileIndexSize, path);
1828                 }
1829                 if (n != 0 && gnuFileIndex[n - 1] != '\n') {
1830                     barf("loadArchive: GNU-variant filename offset %d invalid (range [0..%d]) while reading filename from `%s'", n, gnuFileIndexSize, path);
1831                 }
1832                 for (i = n; gnuFileIndex[i] != '/'; i++);
1833                 thisFileNameSize = i - n;
1834                 if (thisFileNameSize >= fileNameSize) {
1835                     /* Double it to avoid potentially continually
1836                        increasing it by 1 */
1837                     fileNameSize = thisFileNameSize * 2;
1838                     fileName = stgReallocBytes(fileName, fileNameSize, "loadArchive(fileName)");
1839                 }
1840                 memcpy(fileName, gnuFileIndex + n, thisFileNameSize);
1841                 fileName[thisFileNameSize] = '\0';
1842             }
1843             else if (fileName[1] == ' ') {
1844                 fileName[0] = '\0';
1845                 thisFileNameSize = 0;
1846             }
1847             else {
1848                 barf("loadArchive: GNU-variant filename offset not found while reading filename from `%s'", path);
1849             }
1850         }
1851         /* Finally, the case where the filename field actually contains
1852            the filename */
1853         else {
1854             /* GNU ar terminates filenames with a '/', this allowing
1855                spaces in filenames. So first look to see if there is a
1856                terminating '/'. */
1857             for (thisFileNameSize = 0;
1858                  thisFileNameSize < 16;
1859                  thisFileNameSize++) {
1860                 if (fileName[thisFileNameSize] == '/') {
1861                     fileName[thisFileNameSize] = '\0';
1862                     break;
1863                 }
1864             }
1865             /* If we didn't find a '/', then a space teminates the
1866                filename. Note that if we don't find one, then
1867                thisFileNameSize ends up as 16, and we already have the
1868                '\0' at the end. */
1869             if (thisFileNameSize == 16) {
1870                 for (thisFileNameSize = 0;
1871                      thisFileNameSize < 16;
1872                      thisFileNameSize++) {
1873                     if (fileName[thisFileNameSize] == ' ') {
1874                         fileName[thisFileNameSize] = '\0';
1875                         break;
1876                     }
1877                 }
1878             }
1879         }
1880
1881         IF_DEBUG(linker,
1882                  debugBelch("loadArchive: Found member file `%s'\n", fileName));
1883
1884         isObject = thisFileNameSize >= 2
1885                 && fileName[thisFileNameSize - 2] == '.'
1886                 && fileName[thisFileNameSize - 1] == 'o';
1887
1888         IF_DEBUG(linker, debugBelch("loadArchive: \tthisFileNameSize = %d\n", (int)thisFileNameSize));
1889         IF_DEBUG(linker, debugBelch("loadArchive: \tisObject = %d\n", isObject));
1890
1891         if (isObject) {
1892             char *archiveMemberName;
1893
1894             IF_DEBUG(linker, debugBelch("loadArchive: Member is an object file...loading...\n"));
1895
1896             /* We can't mmap from the archive directly, as object
1897                files need to be 8-byte aligned but files in .ar
1898                archives are 2-byte aligned. When possible we use mmap
1899                to get some anonymous memory, as on 64-bit platforms if
1900                we use malloc then we can be given memory above 2^32.
1901                In the mmap case we're probably wasting lots of space;
1902                we could do better. */
1903 #if defined(USE_MMAP)
1904             image = mmapForLinker(memberSize, MAP_ANONYMOUS, -1);
1905 #elif defined(darwin_HOST_OS)
1906             /* See loadObj() */
1907             misalignment = machoGetMisalignment(f);
1908             image = stgMallocBytes(memberSize + misalignment, "loadArchive(image)");
1909             image += misalignment;
1910 #else
1911             image = stgMallocBytes(memberSize, "loadArchive(image)");
1912 #endif
1913             n = fread ( image, 1, memberSize, f );
1914             if (n != memberSize) {
1915                 barf("loadArchive: error whilst reading `%s'", path);
1916             }
1917
1918             archiveMemberName = stgMallocBytes(strlen(path) + thisFileNameSize + 3,
1919                                                "loadArchive(file)");
1920             sprintf(archiveMemberName, "%s(%.*s)",
1921                     path, (int)thisFileNameSize, fileName);
1922
1923             oc = mkOc(path, image, memberSize, archiveMemberName
1924 #ifndef USE_MMAP
1925 #ifdef darwin_HOST_OS
1926                      , misalignment
1927 #endif
1928 #endif
1929                      );
1930
1931             stgFree(archiveMemberName);
1932
1933             if (0 == loadOc(oc)) {
1934                 stgFree(fileName);
1935                 return 0;
1936             }
1937         }
1938         else if (isGnuIndex) {
1939             if (gnuFileIndex != NULL) {
1940                 barf("loadArchive: GNU-variant index found, but already have an index, while reading filename from `%s'", path);
1941             }
1942             IF_DEBUG(linker, debugBelch("loadArchive: Found GNU-variant file index\n"));
1943 #ifdef USE_MMAP
1944             gnuFileIndex = mmapForLinker(memberSize + 1, MAP_ANONYMOUS, -1);
1945 #else
1946             gnuFileIndex = stgMallocBytes(memberSize + 1, "loadArchive(image)");
1947 #endif
1948             n = fread ( gnuFileIndex, 1, memberSize, f );
1949             if (n != memberSize) {
1950                 barf("loadArchive: error whilst reading `%s'", path);
1951             }
1952             gnuFileIndex[memberSize] = '/';
1953             gnuFileIndexSize = memberSize;
1954         }
1955         else {
1956             IF_DEBUG(linker, debugBelch("loadArchive: '%s' does not appear to be an object file\n", fileName));
1957             n = fseek(f, memberSize, SEEK_CUR);
1958             if (n != 0)
1959                 barf("loadArchive: error whilst seeking by %d in `%s'",
1960                      memberSize, path);
1961         }
1962
1963         /* .ar files are 2-byte aligned */
1964         if (memberSize % 2) {
1965             IF_DEBUG(linker, debugBelch("loadArchive: trying to read one pad byte\n"));
1966             n = fread ( tmp, 1, 1, f );
1967             if (n != 1) {
1968                 if (feof(f)) {
1969                     IF_DEBUG(linker, debugBelch("loadArchive: found EOF while reading one pad byte\n"));
1970                     break;
1971                 }
1972                 else {
1973                     barf("loadArchive: Failed reading padding from `%s'", path);
1974                 }
1975             }
1976             IF_DEBUG(linker, debugBelch("loadArchive: successfully read one pad byte\n"));
1977         }
1978         IF_DEBUG(linker, debugBelch("loadArchive: reached end of archive loading while loop\n"));
1979     }
1980
1981     fclose(f);
1982
1983     stgFree(fileName);
1984     if (gnuFileIndex != NULL) {
1985 #ifdef USE_MMAP
1986         munmap(gnuFileIndex, gnuFileIndexSize + 1);
1987 #else
1988         stgFree(gnuFileIndex);
1989 #endif
1990     }
1991
1992     IF_DEBUG(linker, debugBelch("loadArchive: done\n"));
1993     return 1;
1994 }
1995
1996 /* -----------------------------------------------------------------------------
1997  * Load an obj (populate the global symbol table, but don't resolve yet)
1998  *
1999  * Returns: 1 if ok, 0 on error.
2000  */
2001 HsInt
2002 loadObj( char *path )
2003 {
2004    ObjectCode* oc;
2005    char *image;
2006    int fileSize;
2007    struct stat st;
2008    int r;
2009 #ifdef USE_MMAP
2010    int fd;
2011 #else
2012    FILE *f;
2013 #  if defined(darwin_HOST_OS)
2014    int misalignment;
2015 #  endif
2016 #endif
2017    IF_DEBUG(linker, debugBelch("loadObj %s\n", path));
2018
2019    initLinker();
2020
2021    /* debugBelch("loadObj %s\n", path ); */
2022
2023    /* Check that we haven't already loaded this object.
2024       Ignore requests to load multiple times */
2025    {
2026        ObjectCode *o;
2027        int is_dup = 0;
2028        for (o = objects; o; o = o->next) {
2029           if (0 == strcmp(o->fileName, path)) {
2030              is_dup = 1;
2031              break; /* don't need to search further */
2032           }
2033        }
2034        if (is_dup) {
2035           IF_DEBUG(linker, debugBelch(
2036             "GHCi runtime linker: warning: looks like you're trying to load the\n"
2037             "same object file twice:\n"
2038             "   %s\n"
2039             "GHCi will ignore this, but be warned.\n"
2040             , path));
2041           return 1; /* success */
2042        }
2043    }
2044
2045    r = stat(path, &st);
2046    if (r == -1) {
2047        IF_DEBUG(linker, debugBelch("File doesn't exist\n"));
2048        return 0;
2049    }
2050
2051    fileSize = st.st_size;
2052
2053 #ifdef USE_MMAP
2054    /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
2055
2056 #if defined(openbsd_HOST_OS)
2057    fd = open(path, O_RDONLY, S_IRUSR);
2058 #else
2059    fd = open(path, O_RDONLY);
2060 #endif
2061    if (fd == -1)
2062       barf("loadObj: can't open `%s'", path);
2063
2064    image = mmapForLinker(fileSize, 0, fd);
2065
2066    close(fd);
2067
2068 #else /* !USE_MMAP */
2069    /* load the image into memory */
2070    f = fopen(path, "rb");
2071    if (!f)
2072        barf("loadObj: can't read `%s'", path);
2073
2074 #   if defined(mingw32_HOST_OS)
2075         // TODO: We would like to use allocateExec here, but allocateExec
2076         //       cannot currently allocate blocks large enough.
2077     image = VirtualAlloc(NULL, fileSize, MEM_RESERVE | MEM_COMMIT,
2078                              PAGE_EXECUTE_READWRITE);
2079 #   elif defined(darwin_HOST_OS)
2080     // In a Mach-O .o file, all sections can and will be misaligned
2081     // if the total size of the headers is not a multiple of the
2082     // desired alignment. This is fine for .o files that only serve
2083     // as input for the static linker, but it's not fine for us,
2084     // as SSE (used by gcc for floating point) and Altivec require
2085     // 16-byte alignment.
2086     // We calculate the correct alignment from the header before
2087     // reading the file, and then we misalign image on purpose so
2088     // that the actual sections end up aligned again.
2089    misalignment = machoGetMisalignment(f);
2090    image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
2091    image += misalignment;
2092 #  else
2093    image = stgMallocBytes(fileSize, "loadObj(image)");
2094 #  endif
2095
2096    {
2097        int n;
2098        n = fread ( image, 1, fileSize, f );
2099        if (n != fileSize)
2100            barf("loadObj: error whilst reading `%s'", path);
2101    }
2102    fclose(f);
2103 #endif /* USE_MMAP */
2104
2105    oc = mkOc(path, image, fileSize, NULL
2106 #ifndef USE_MMAP
2107 #ifdef darwin_HOST_OS
2108             , misalignment
2109 #endif
2110 #endif
2111             );
2112
2113    return loadOc(oc);
2114 }
2115
2116 static HsInt
2117 loadOc( ObjectCode* oc ) {
2118    int r;
2119
2120    IF_DEBUG(linker, debugBelch("loadOc: start\n"));
2121
2122 #  if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2123    r = ocAllocateSymbolExtras_MachO ( oc );
2124    if (!r) {
2125        IF_DEBUG(linker, debugBelch("loadOc: ocAllocateSymbolExtras_MachO failed\n"));
2126        return r;
2127    }
2128 #  elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
2129    r = ocAllocateSymbolExtras_ELF ( oc );
2130    if (!r) {
2131        IF_DEBUG(linker, debugBelch("loadOc: ocAllocateSymbolExtras_ELF failed\n"));
2132        return r;
2133    }
2134 #endif
2135
2136    /* verify the in-memory image */
2137 #  if defined(OBJFORMAT_ELF)
2138    r = ocVerifyImage_ELF ( oc );
2139 #  elif defined(OBJFORMAT_PEi386)
2140    r = ocVerifyImage_PEi386 ( oc );
2141 #  elif defined(OBJFORMAT_MACHO)
2142    r = ocVerifyImage_MachO ( oc );
2143 #  else
2144    barf("loadObj: no verify method");
2145 #  endif
2146    if (!r) {
2147        IF_DEBUG(linker, debugBelch("loadOc: ocVerifyImage_* failed\n"));
2148        return r;
2149    }
2150
2151    /* build the symbol list for this image */
2152 #  if defined(OBJFORMAT_ELF)
2153    r = ocGetNames_ELF ( oc );
2154 #  elif defined(OBJFORMAT_PEi386)
2155    r = ocGetNames_PEi386 ( oc );
2156 #  elif defined(OBJFORMAT_MACHO)
2157    r = ocGetNames_MachO ( oc );
2158 #  else
2159    barf("loadObj: no getNames method");
2160 #  endif
2161    if (!r) {
2162        IF_DEBUG(linker, debugBelch("loadOc: ocGetNames_* failed\n"));
2163        return r;
2164    }
2165
2166    /* loaded, but not resolved yet */
2167    oc->status = OBJECT_LOADED;
2168    IF_DEBUG(linker, debugBelch("loadOc: done.\n"));
2169
2170    return 1;
2171 }
2172
2173 /* -----------------------------------------------------------------------------
2174  * resolve all the currently unlinked objects in memory
2175  *
2176  * Returns: 1 if ok, 0 on error.
2177  */
2178 HsInt
2179 resolveObjs( void )
2180 {
2181     ObjectCode *oc;
2182     int r;
2183
2184     IF_DEBUG(linker, debugBelch("resolveObjs: start\n"));
2185     initLinker();
2186
2187     for (oc = objects; oc; oc = oc->next) {
2188         if (oc->status != OBJECT_RESOLVED) {
2189 #           if defined(OBJFORMAT_ELF)
2190             r = ocResolve_ELF ( oc );
2191 #           elif defined(OBJFORMAT_PEi386)
2192             r = ocResolve_PEi386 ( oc );
2193 #           elif defined(OBJFORMAT_MACHO)
2194             r = ocResolve_MachO ( oc );
2195 #           else
2196             barf("resolveObjs: not implemented on this platform");
2197 #           endif
2198             if (!r) { return r; }
2199             oc->status = OBJECT_RESOLVED;
2200         }
2201     }
2202     IF_DEBUG(linker, debugBelch("resolveObjs: done\n"));
2203     return 1;
2204 }
2205
2206 /* -----------------------------------------------------------------------------
2207  * delete an object from the pool
2208  */
2209 HsInt
2210 unloadObj( char *path )
2211 {
2212     ObjectCode *oc, *prev;
2213     HsBool unloadedAnyObj = HS_BOOL_FALSE;
2214
2215     ASSERT(symhash != NULL);
2216     ASSERT(objects != NULL);
2217
2218     initLinker();
2219
2220     prev = NULL;
2221     for (oc = objects; oc; prev = oc, oc = oc->next) {
2222         if (!strcmp(oc->fileName,path)) {
2223
2224             /* Remove all the mappings for the symbols within this
2225              * object..
2226              */
2227             {
2228                 int i;
2229                 for (i = 0; i < oc->n_symbols; i++) {
2230                    if (oc->symbols[i] != NULL) {
2231                        removeStrHashTable(symhash, oc->symbols[i], NULL);
2232                    }
2233                 }
2234             }
2235
2236             if (prev == NULL) {
2237                 objects = oc->next;
2238             } else {
2239                 prev->next = oc->next;
2240             }
2241
2242             // We're going to leave this in place, in case there are
2243             // any pointers from the heap into it:
2244                 // #ifdef mingw32_HOST_OS
2245                 //  VirtualFree(oc->image);
2246                 // #else
2247             //  stgFree(oc->image);
2248             // #endif
2249             stgFree(oc->fileName);
2250             stgFree(oc->symbols);
2251             stgFree(oc->sections);
2252             stgFree(oc);
2253
2254             /* This could be a member of an archive so continue
2255              * unloading other members. */
2256             unloadedAnyObj = HS_BOOL_TRUE;
2257         }
2258     }
2259
2260     if (unloadedAnyObj) {
2261         return 1;
2262     }
2263     else {
2264         errorBelch("unloadObj: can't find `%s' to unload", path);
2265         return 0;
2266     }
2267 }
2268
2269 /* -----------------------------------------------------------------------------
2270  * Sanity checking.  For each ObjectCode, maintain a list of address ranges
2271  * which may be prodded during relocation, and abort if we try and write
2272  * outside any of these.
2273  */
2274 static void
2275 addProddableBlock ( ObjectCode* oc, void* start, int size )
2276 {
2277    ProddableBlock* pb
2278       = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
2279
2280    IF_DEBUG(linker, debugBelch("addProddableBlock: %p %p %d\n", oc, start, size));
2281    ASSERT(size > 0);
2282    pb->start      = start;
2283    pb->size       = size;
2284    pb->next       = oc->proddables;
2285    oc->proddables = pb;
2286 }
2287
2288 static void
2289 checkProddableBlock (ObjectCode *oc, void *addr )
2290 {
2291    ProddableBlock* pb;
2292
2293    for (pb = oc->proddables; pb != NULL; pb = pb->next) {
2294       char* s = (char*)(pb->start);
2295       char* e = s + pb->size - 1;
2296       char* a = (char*)addr;
2297       /* Assumes that the biggest fixup involves a 4-byte write.  This
2298          probably needs to be changed to 8 (ie, +7) on 64-bit
2299          plats. */
2300       if (a >= s && (a+3) <= e) return;
2301    }
2302    barf("checkProddableBlock: invalid fixup in runtime linker");
2303 }
2304
2305 /* -----------------------------------------------------------------------------
2306  * Section management.
2307  */
2308 static void
2309 addSection ( ObjectCode* oc, SectionKind kind,
2310                          void* start, void* end )
2311 {
2312    Section* s   = stgMallocBytes(sizeof(Section), "addSection");
2313    s->start     = start;
2314    s->end       = end;
2315    s->kind      = kind;
2316    s->next      = oc->sections;
2317    oc->sections = s;
2318
2319    IF_DEBUG(linker, debugBelch("addSection: %p-%p (size %ld), kind %d\n",
2320                                start, ((char*)end)-1, (long)end - (long)start + 1, kind ));
2321 }
2322
2323
2324 /* --------------------------------------------------------------------------
2325  * Symbol Extras.
2326  * This is about allocating a small chunk of memory for every symbol in the
2327  * object file. We make sure that the SymboLExtras are always "in range" of
2328  * limited-range PC-relative instructions on various platforms by allocating
2329  * them right next to the object code itself.
2330  */
2331
2332 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
2333
2334 /*
2335   ocAllocateSymbolExtras
2336
2337   Allocate additional space at the end of the object file image to make room
2338   for jump islands (powerpc, x86_64) and GOT entries (x86_64).
2339
2340   PowerPC relative branch instructions have a 24 bit displacement field.
2341   As PPC code is always 4-byte-aligned, this yields a +-32MB range.
2342   If a particular imported symbol is outside this range, we have to redirect
2343   the jump to a short piece of new code that just loads the 32bit absolute
2344   address and jumps there.
2345   On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
2346   to 32 bits (+-2GB).
2347
2348   This function just allocates space for one SymbolExtra for every
2349   undefined symbol in the object file. The code for the jump islands is
2350   filled in by makeSymbolExtra below.
2351 */
2352
2353 static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
2354 {
2355 #ifdef USE_MMAP
2356   int pagesize, n, m;
2357 #endif
2358   int aligned;
2359 #ifndef USE_MMAP
2360   int misalignment = 0;
2361 #ifdef darwin_HOST_OS
2362   misalignment = oc->misalignment;
2363 #endif
2364 #endif
2365
2366   if( count > 0 )
2367   {
2368     // round up to the nearest 4
2369     aligned = (oc->fileSize + 3) & ~3;
2370
2371 #ifdef USE_MMAP
2372     pagesize = getpagesize();
2373     n = ROUND_UP( oc->fileSize, pagesize );
2374     m = ROUND_UP( aligned + sizeof (SymbolExtra) * count, pagesize );
2375
2376     /* we try to use spare space at the end of the last page of the
2377      * image for the jump islands, but if there isn't enough space
2378      * then we have to map some (anonymously, remembering MAP_32BIT).
2379      */
2380     if( m > n ) // we need to allocate more pages
2381     {
2382         oc->symbol_extras = mmapForLinker(sizeof(SymbolExtra) * count,
2383                                           MAP_ANONYMOUS, -1);
2384     }
2385     else
2386     {
2387         oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2388     }
2389 #else
2390     oc->image -= misalignment;
2391     oc->image = stgReallocBytes( oc->image,
2392                                  misalignment +
2393                                  aligned + sizeof (SymbolExtra) * count,
2394                                  "ocAllocateSymbolExtras" );
2395     oc->image += misalignment;
2396
2397     oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
2398 #endif /* USE_MMAP */
2399
2400     memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
2401   }
2402   else
2403     oc->symbol_extras = NULL;
2404
2405   oc->first_symbol_extra = first;
2406   oc->n_symbol_extras = count;
2407
2408   return 1;
2409 }
2410
2411 static SymbolExtra* makeSymbolExtra( ObjectCode* oc,
2412                                      unsigned long symbolNumber,
2413                                      unsigned long target )
2414 {
2415   SymbolExtra *extra;
2416
2417   ASSERT( symbolNumber >= oc->first_symbol_extra
2418         && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
2419
2420   extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
2421
2422 #ifdef powerpc_HOST_ARCH
2423   // lis r12, hi16(target)
2424   extra->jumpIsland.lis_r12     = 0x3d80;
2425   extra->jumpIsland.hi_addr     = target >> 16;
2426
2427   // ori r12, r12, lo16(target)
2428   extra->jumpIsland.ori_r12_r12 = 0x618c;
2429   extra->jumpIsland.lo_addr     = target & 0xffff;
2430
2431   // mtctr r12
2432   extra->jumpIsland.mtctr_r12   = 0x7d8903a6;
2433
2434   // bctr
2435   extra->jumpIsland.bctr        = 0x4e800420;
2436 #endif
2437 #ifdef x86_64_HOST_ARCH
2438         // jmp *-14(%rip)
2439   static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
2440   extra->addr = target;
2441   memcpy(extra->jumpIsland, jmp, 6);
2442 #endif
2443
2444   return extra;
2445 }
2446
2447 #endif
2448
2449 /* --------------------------------------------------------------------------
2450  * PowerPC specifics (instruction cache flushing)
2451  * ------------------------------------------------------------------------*/
2452
2453 #ifdef powerpc_HOST_ARCH
2454 /*
2455    ocFlushInstructionCache
2456
2457    Flush the data & instruction caches.
2458    Because the PPC has split data/instruction caches, we have to
2459    do that whenever we modify code at runtime.
2460  */
2461
2462 static void
2463 ocFlushInstructionCacheFrom(void* begin, size_t length)
2464 {
2465     size_t         n = (length + 3) / 4;
2466     unsigned long* p = begin;
2467
2468     while (n--)
2469     {
2470         __asm__ volatile ( "dcbf 0,%0\n\t"
2471                            "sync\n\t"
2472                            "icbi 0,%0"
2473                            :
2474                            : "r" (p)
2475                          );
2476         p++;
2477     }
2478     __asm__ volatile ( "sync\n\t"
2479                        "isync"
2480                      );
2481 }
2482
2483 static void
2484 ocFlushInstructionCache( ObjectCode *oc )
2485 {
2486     /* The main object code */
2487     ocFlushInstructionCacheFrom(oc->image + oc->misalignment, oc->fileSize);
2488
2489     /* Jump Islands */
2490     ocFlushInstructionCacheFrom(oc->symbol_extras, sizeof(SymbolExtra) * oc->n_symbol_extras);
2491 }
2492 #endif /* powerpc_HOST_ARCH */
2493
2494
2495 /* --------------------------------------------------------------------------
2496  * PEi386 specifics (Win32 targets)
2497  * ------------------------------------------------------------------------*/
2498
2499 /* The information for this linker comes from
2500       Microsoft Portable Executable
2501       and Common Object File Format Specification
2502       revision 5.1 January 1998
2503    which SimonM says comes from the MS Developer Network CDs.
2504
2505    It can be found there (on older CDs), but can also be found
2506    online at:
2507
2508       http://www.microsoft.com/hwdev/hardware/PECOFF.asp
2509
2510    (this is Rev 6.0 from February 1999).
2511
2512    Things move, so if that fails, try searching for it via
2513
2514       http://www.google.com/search?q=PE+COFF+specification
2515
2516    The ultimate reference for the PE format is the Winnt.h
2517    header file that comes with the Platform SDKs; as always,
2518    implementations will drift wrt their documentation.
2519
2520    A good background article on the PE format is Matt Pietrek's
2521    March 1994 article in Microsoft System Journal (MSJ)
2522    (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
2523    Win32 Portable Executable File Format." The info in there
2524    has recently been updated in a two part article in
2525    MSDN magazine, issues Feb and March 2002,
2526    "Inside Windows: An In-Depth Look into the Win32 Portable
2527    Executable File Format"
2528
2529    John Levine's book "Linkers and Loaders" contains useful
2530    info on PE too.
2531 */
2532
2533
2534 #if defined(OBJFORMAT_PEi386)
2535
2536
2537
2538 typedef unsigned char  UChar;
2539 typedef unsigned short UInt16;
2540 typedef unsigned int   UInt32;
2541 typedef          int   Int32;
2542
2543
2544 typedef
2545    struct {
2546       UInt16 Machine;
2547       UInt16 NumberOfSections;
2548       UInt32 TimeDateStamp;
2549       UInt32 PointerToSymbolTable;
2550       UInt32 NumberOfSymbols;
2551       UInt16 SizeOfOptionalHeader;
2552       UInt16 Characteristics;
2553    }
2554    COFF_header;
2555
2556 #define sizeof_COFF_header 20
2557
2558
2559 typedef
2560    struct {
2561       UChar  Name[8];
2562       UInt32 VirtualSize;
2563       UInt32 VirtualAddress;
2564       UInt32 SizeOfRawData;
2565       UInt32 PointerToRawData;
2566       UInt32 PointerToRelocations;
2567       UInt32 PointerToLinenumbers;
2568       UInt16 NumberOfRelocations;
2569       UInt16 NumberOfLineNumbers;
2570       UInt32 Characteristics;
2571    }
2572    COFF_section;
2573
2574 #define sizeof_COFF_section 40
2575
2576
2577 typedef
2578    struct {
2579       UChar  Name[8];
2580       UInt32 Value;
2581       UInt16 SectionNumber;
2582       UInt16 Type;
2583       UChar  StorageClass;
2584       UChar  NumberOfAuxSymbols;
2585    }
2586    COFF_symbol;
2587
2588 #define sizeof_COFF_symbol 18
2589
2590
2591 typedef
2592    struct {
2593       UInt32 VirtualAddress;
2594       UInt32 SymbolTableIndex;
2595       UInt16 Type;
2596    }
2597    COFF_reloc;
2598
2599 #define sizeof_COFF_reloc 10
2600
2601
2602 /* From PE spec doc, section 3.3.2 */
2603 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
2604    windows.h -- for the same purpose, but I want to know what I'm
2605    getting, here. */
2606 #define MYIMAGE_FILE_RELOCS_STRIPPED     0x0001
2607 #define MYIMAGE_FILE_EXECUTABLE_IMAGE    0x0002
2608 #define MYIMAGE_FILE_DLL                 0x2000
2609 #define MYIMAGE_FILE_SYSTEM              0x1000
2610 #define MYIMAGE_FILE_BYTES_REVERSED_HI   0x8000
2611 #define MYIMAGE_FILE_BYTES_REVERSED_LO   0x0080
2612 #define MYIMAGE_FILE_32BIT_MACHINE       0x0100
2613
2614 /* From PE spec doc, section 5.4.2 and 5.4.4 */
2615 #define MYIMAGE_SYM_CLASS_EXTERNAL       2
2616 #define MYIMAGE_SYM_CLASS_STATIC         3
2617 #define MYIMAGE_SYM_UNDEFINED            0
2618
2619 /* From PE spec doc, section 4.1 */
2620 #define MYIMAGE_SCN_CNT_CODE             0x00000020
2621 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
2622 #define MYIMAGE_SCN_LNK_NRELOC_OVFL      0x01000000
2623
2624 /* From PE spec doc, section 5.2.1 */
2625 #define MYIMAGE_REL_I386_DIR32           0x0006
2626 #define MYIMAGE_REL_I386_REL32           0x0014
2627
2628
2629 /* We use myindex to calculate array addresses, rather than
2630    simply doing the normal subscript thing.  That's because
2631    some of the above structs have sizes which are not
2632    a whole number of words.  GCC rounds their sizes up to a
2633    whole number of words, which means that the address calcs
2634    arising from using normal C indexing or pointer arithmetic
2635    are just plain wrong.  Sigh.
2636 */
2637 static UChar *
2638 myindex ( int scale, void* base, int index )
2639 {
2640    return
2641       ((UChar*)base) + scale * index;
2642 }
2643
2644
2645 static void
2646 printName ( UChar* name, UChar* strtab )
2647 {
2648    if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2649       UInt32 strtab_offset = * (UInt32*)(name+4);
2650       debugBelch("%s", strtab + strtab_offset );
2651    } else {
2652       int i;
2653       for (i = 0; i < 8; i++) {
2654          if (name[i] == 0) break;
2655          debugBelch("%c", name[i] );
2656       }
2657    }
2658 }
2659
2660
2661 static void
2662 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
2663 {
2664    if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2665       UInt32 strtab_offset = * (UInt32*)(name+4);
2666       strncpy ( (char*)dst, (char*)strtab+strtab_offset, dstSize );
2667       dst[dstSize-1] = 0;
2668    } else {
2669       int i = 0;
2670       while (1) {
2671          if (i >= 8) break;
2672          if (name[i] == 0) break;
2673          dst[i] = name[i];
2674          i++;
2675       }
2676       dst[i] = 0;
2677    }
2678 }
2679
2680
2681 static UChar *
2682 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
2683 {
2684    UChar* newstr;
2685    /* If the string is longer than 8 bytes, look in the
2686       string table for it -- this will be correctly zero terminated.
2687    */
2688    if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2689       UInt32 strtab_offset = * (UInt32*)(name+4);
2690       return ((UChar*)strtab) + strtab_offset;
2691    }
2692    /* Otherwise, if shorter than 8 bytes, return the original,
2693       which by defn is correctly terminated.
2694    */
2695    if (name[7]==0) return name;
2696    /* The annoying case: 8 bytes.  Copy into a temporary
2697       (XXX which is never freed ...)
2698    */
2699    newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
2700    ASSERT(newstr);
2701    strncpy((char*)newstr,(char*)name,8);
2702    newstr[8] = 0;
2703    return newstr;
2704 }
2705
2706 /* Getting the name of a section is mildly tricky, so we make a
2707    function for it.  Sadly, in one case we have to copy the string
2708    (when it is exactly 8 bytes long there's no trailing '\0'), so for
2709    consistency we *always* copy the string; the caller must free it
2710 */
2711 static char *
2712 cstring_from_section_name (UChar* name, UChar* strtab)
2713 {
2714     char *newstr;
2715
2716     if (name[0]=='/') {
2717         int strtab_offset = strtol((char*)name+1,NULL,10);
2718         int len = strlen(((char*)strtab) + strtab_offset);
2719
2720         newstr = stgMallocBytes(len, "cstring_from_section_symbol_name");
2721         strcpy((char*)newstr, (char*)((UChar*)strtab) + strtab_offset);
2722         return newstr;
2723     }
2724     else
2725     {
2726         newstr = stgMallocBytes(9, "cstring_from_section_symbol_name");
2727         ASSERT(newstr);
2728         strncpy((char*)newstr,(char*)name,8);
2729         newstr[8] = 0;
2730         return newstr;
2731     }
2732 }
2733
2734 /* Just compares the short names (first 8 chars) */
2735 static COFF_section *
2736 findPEi386SectionCalled ( ObjectCode* oc,  UChar* name )
2737 {
2738    int i;
2739    COFF_header* hdr
2740       = (COFF_header*)(oc->image);
2741    COFF_section* sectab
2742       = (COFF_section*) (
2743            ((UChar*)(oc->image))
2744            + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2745         );
2746    for (i = 0; i < hdr->NumberOfSections; i++) {
2747       UChar* n1;
2748       UChar* n2;
2749       COFF_section* section_i
2750          = (COFF_section*)
2751            myindex ( sizeof_COFF_section, sectab, i );
2752       n1 = (UChar*) &(section_i->Name);
2753       n2 = name;
2754       if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
2755           n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
2756           n1[6]==n2[6] && n1[7]==n2[7])
2757          return section_i;
2758    }
2759
2760    return NULL;
2761 }
2762
2763
2764 static void
2765 zapTrailingAtSign ( UChar* sym )
2766 {
2767 #  define my_isdigit(c) ((c) >= '0' && (c) <= '9')
2768    int i, j;
2769    if (sym[0] == 0) return;
2770    i = 0;
2771    while (sym[i] != 0) i++;
2772    i--;
2773    j = i;
2774    while (j > 0 && my_isdigit(sym[j])) j--;
2775    if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
2776 #  undef my_isdigit
2777 }
2778
2779 static void *
2780 lookupSymbolInDLLs ( UChar *lbl )
2781 {
2782     OpenedDLL* o_dll;
2783     void *sym;
2784
2785     for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
2786         /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
2787
2788         if (lbl[0] == '_') {
2789             /* HACK: if the name has an initial underscore, try stripping
2790                it off & look that up first. I've yet to verify whether there's
2791                a Rule that governs whether an initial '_' *should always* be
2792                stripped off when mapping from import lib name to the DLL name.
2793             */
2794             sym = GetProcAddress(o_dll->instance, (char*)(lbl+1));
2795             if (sym != NULL) {
2796                 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
2797                 return sym;
2798             }
2799         }
2800         sym = GetProcAddress(o_dll->instance, (char*)lbl);
2801         if (sym != NULL) {
2802             /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
2803             return sym;
2804            }
2805     }
2806     return NULL;
2807 }
2808
2809
2810 static int
2811 ocVerifyImage_PEi386 ( ObjectCode* oc )
2812 {
2813    int i;
2814    UInt32 j, noRelocs;
2815    COFF_header*  hdr;
2816    COFF_section* sectab;
2817    COFF_symbol*  symtab;
2818    UChar*        strtab;
2819    /* debugBelch("\nLOADING %s\n", oc->fileName); */
2820    hdr = (COFF_header*)(oc->image);
2821    sectab = (COFF_section*) (
2822                ((UChar*)(oc->image))
2823                + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2824             );
2825    symtab = (COFF_symbol*) (
2826                ((UChar*)(oc->image))
2827                + hdr->PointerToSymbolTable
2828             );
2829    strtab = ((UChar*)symtab)
2830             + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2831
2832    if (hdr->Machine != 0x14c) {
2833       errorBelch("%s: Not x86 PEi386", oc->fileName);
2834       return 0;
2835    }
2836    if (hdr->SizeOfOptionalHeader != 0) {
2837       errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
2838       return 0;
2839    }
2840    if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
2841         (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
2842         (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
2843         (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
2844       errorBelch("%s: Not a PEi386 object file", oc->fileName);
2845       return 0;
2846    }
2847    if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
2848         /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
2849       errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
2850                  oc->fileName,
2851                  (int)(hdr->Characteristics));
2852       return 0;
2853    }
2854    /* If the string table size is way crazy, this might indicate that
2855       there are more than 64k relocations, despite claims to the
2856       contrary.  Hence this test. */
2857    /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
2858 #if 0
2859    if ( (*(UInt32*)strtab) > 600000 ) {
2860       /* Note that 600k has no special significance other than being
2861          big enough to handle the almost-2MB-sized lumps that
2862          constitute HSwin32*.o. */
2863       debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
2864       return 0;
2865    }
2866 #endif
2867
2868    /* No further verification after this point; only debug printing. */
2869    i = 0;
2870    IF_DEBUG(linker, i=1);
2871    if (i == 0) return 1;
2872
2873    debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
2874    debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
2875    debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
2876
2877    debugBelch("\n" );
2878    debugBelch( "Machine:           0x%x\n", (UInt32)(hdr->Machine) );
2879    debugBelch( "# sections:        %d\n",   (UInt32)(hdr->NumberOfSections) );
2880    debugBelch( "time/date:         0x%x\n", (UInt32)(hdr->TimeDateStamp) );
2881    debugBelch( "symtab offset:     %d\n",   (UInt32)(hdr->PointerToSymbolTable) );
2882    debugBelch( "# symbols:         %d\n",   (UInt32)(hdr->NumberOfSymbols) );
2883    debugBelch( "sz of opt hdr:     %d\n",   (UInt32)(hdr->SizeOfOptionalHeader) );
2884    debugBelch( "characteristics:   0x%x\n", (UInt32)(hdr->Characteristics) );
2885
2886    /* Print the section table. */
2887    debugBelch("\n" );
2888    for (i = 0; i < hdr->NumberOfSections; i++) {
2889       COFF_reloc* reltab;
2890       COFF_section* sectab_i
2891          = (COFF_section*)
2892            myindex ( sizeof_COFF_section, sectab, i );
2893       debugBelch(
2894                 "\n"
2895                 "section %d\n"
2896                 "     name `",
2897                 i
2898               );
2899       printName ( sectab_i->Name, strtab );
2900       debugBelch(
2901                 "'\n"
2902                 "    vsize %d\n"
2903                 "    vaddr %d\n"
2904                 "  data sz %d\n"
2905                 " data off %d\n"
2906                 "  num rel %d\n"
2907                 "  off rel %d\n"
2908                 "  ptr raw 0x%x\n",
2909                 sectab_i->VirtualSize,
2910                 sectab_i->VirtualAddress,
2911                 sectab_i->SizeOfRawData,
2912                 sectab_i->PointerToRawData,
2913                 sectab_i->NumberOfRelocations,
2914                 sectab_i->PointerToRelocations,
2915                 sectab_i->PointerToRawData
2916               );
2917       reltab = (COFF_reloc*) (
2918                   ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2919                );
2920
2921       if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2922         /* If the relocation field (a short) has overflowed, the
2923          * real count can be found in the first reloc entry.
2924          *
2925          * See Section 4.1 (last para) of the PE spec (rev6.0).
2926          */
2927         COFF_reloc* rel = (COFF_reloc*)
2928                            myindex ( sizeof_COFF_reloc, reltab, 0 );
2929         noRelocs = rel->VirtualAddress;
2930         j = 1;
2931       } else {
2932         noRelocs = sectab_i->NumberOfRelocations;
2933         j = 0;
2934       }
2935
2936       for (; j < noRelocs; j++) {
2937          COFF_symbol* sym;
2938          COFF_reloc* rel = (COFF_reloc*)
2939                            myindex ( sizeof_COFF_reloc, reltab, j );
2940          debugBelch(
2941                    "        type 0x%-4x   vaddr 0x%-8x   name `",
2942                    (UInt32)rel->Type,
2943                    rel->VirtualAddress );
2944          sym = (COFF_symbol*)
2945                myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
2946          /* Hmm..mysterious looking offset - what's it for? SOF */
2947          printName ( sym->Name, strtab -10 );
2948          debugBelch("'\n" );
2949       }
2950
2951       debugBelch("\n" );
2952    }
2953    debugBelch("\n" );
2954    debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
2955    debugBelch("---START of string table---\n");
2956    for (i = 4; i < *(Int32*)strtab; i++) {
2957       if (strtab[i] == 0)
2958          debugBelch("\n"); else
2959          debugBelch("%c", strtab[i] );
2960    }
2961    debugBelch("--- END  of string table---\n");
2962
2963    debugBelch("\n" );
2964    i = 0;
2965    while (1) {
2966       COFF_symbol* symtab_i;
2967       if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2968       symtab_i = (COFF_symbol*)
2969                  myindex ( sizeof_COFF_symbol, symtab, i );
2970       debugBelch(
2971                 "symbol %d\n"
2972                 "     name `",
2973                 i
2974               );
2975       printName ( symtab_i->Name, strtab );
2976       debugBelch(
2977                 "'\n"
2978                 "    value 0x%x\n"
2979                 "   1+sec# %d\n"
2980                 "     type 0x%x\n"
2981                 "   sclass 0x%x\n"
2982                 "     nAux %d\n",
2983                 symtab_i->Value,
2984                 (Int32)(symtab_i->SectionNumber),
2985                 (UInt32)symtab_i->Type,
2986                 (UInt32)symtab_i->StorageClass,
2987                 (UInt32)symtab_i->NumberOfAuxSymbols
2988               );
2989       i += symtab_i->NumberOfAuxSymbols;
2990       i++;
2991    }
2992
2993    debugBelch("\n" );
2994    return 1;
2995 }
2996
2997
2998 static int
2999 ocGetNames_PEi386 ( ObjectCode* oc )
3000 {
3001    COFF_header*  hdr;
3002    COFF_section* sectab;
3003    COFF_symbol*  symtab;
3004    UChar*        strtab;
3005
3006    UChar* sname;
3007    void*  addr;
3008    int    i;
3009
3010    hdr = (COFF_header*)(oc->image);
3011    sectab = (COFF_section*) (
3012                ((UChar*)(oc->image))
3013                + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3014             );
3015    symtab = (COFF_symbol*) (
3016                ((UChar*)(oc->image))
3017                + hdr->PointerToSymbolTable
3018             );
3019    strtab = ((UChar*)(oc->image))
3020             + hdr->PointerToSymbolTable
3021             + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3022
3023    /* Allocate space for any (local, anonymous) .bss sections. */
3024
3025    for (i = 0; i < hdr->NumberOfSections; i++) {
3026       UInt32 bss_sz;
3027       UChar* zspace;
3028       COFF_section* sectab_i
3029          = (COFF_section*)
3030            myindex ( sizeof_COFF_section, sectab, i );
3031
3032       char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3033
3034       if (0 != strcmp(secname, ".bss")) {
3035           stgFree(secname);
3036           continue;
3037       }
3038
3039       stgFree(secname);
3040
3041       /* sof 10/05: the PE spec text isn't too clear regarding what
3042        * the SizeOfRawData field is supposed to hold for object
3043        * file sections containing just uninitialized data -- for executables,
3044        * it is supposed to be zero; unclear what it's supposed to be
3045        * for object files. However, VirtualSize is guaranteed to be
3046        * zero for object files, which definitely suggests that SizeOfRawData
3047        * will be non-zero (where else would the size of this .bss section be
3048        * stored?) Looking at the COFF_section info for incoming object files,
3049        * this certainly appears to be the case.
3050        *
3051        * => I suspect we've been incorrectly handling .bss sections in (relocatable)
3052        * object files up until now. This turned out to bite us with ghc-6.4.1's use
3053        * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
3054        * variable decls into to the .bss section. (The specific function in Q which
3055        * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
3056        */
3057       if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
3058       /* This is a non-empty .bss section.  Allocate zeroed space for
3059          it, and set its PointerToRawData field such that oc->image +
3060          PointerToRawData == addr_of_zeroed_space.  */
3061       bss_sz = sectab_i->VirtualSize;
3062       if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
3063       zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
3064       sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
3065       addProddableBlock(oc, zspace, bss_sz);
3066       /* debugBelch("BSS anon section at 0x%x\n", zspace); */
3067    }
3068
3069    /* Copy section information into the ObjectCode. */
3070
3071    for (i = 0; i < hdr->NumberOfSections; i++) {
3072       UChar* start;
3073       UChar* end;
3074       UInt32 sz;
3075
3076       SectionKind kind
3077          = SECTIONKIND_OTHER;
3078       COFF_section* sectab_i
3079          = (COFF_section*)
3080            myindex ( sizeof_COFF_section, sectab, i );
3081
3082       char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3083
3084       IF_DEBUG(linker, debugBelch("section name = %s\n", secname ));
3085
3086 #     if 0
3087       /* I'm sure this is the Right Way to do it.  However, the
3088          alternative of testing the sectab_i->Name field seems to
3089          work ok with Cygwin.
3090       */
3091       if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
3092           sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
3093          kind = SECTIONKIND_CODE_OR_RODATA;
3094 #     endif
3095
3096       if (0==strcmp(".text",(char*)secname) ||
3097           0==strcmp(".rdata",(char*)secname)||
3098           0==strcmp(".rodata",(char*)secname))
3099          kind = SECTIONKIND_CODE_OR_RODATA;
3100       if (0==strcmp(".data",(char*)secname) ||
3101           0==strcmp(".bss",(char*)secname))
3102          kind = SECTIONKIND_RWDATA;
3103
3104       ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
3105       sz = sectab_i->SizeOfRawData;
3106       if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
3107
3108       start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
3109       end   = start + sz - 1;
3110
3111       if (kind == SECTIONKIND_OTHER
3112           /* Ignore sections called which contain stabs debugging
3113              information. */
3114           && 0 != strcmp(".stab", (char*)secname)
3115           && 0 != strcmp(".stabstr", (char*)secname)
3116           /* ignore constructor section for now */
3117           && 0 != strcmp(".ctors", (char*)secname)
3118           /* ignore section generated from .ident */
3119           && 0!= strncmp(".debug", (char*)secname, 6)
3120           /* ignore unknown section that appeared in gcc 3.4.5(?) */
3121           && 0!= strcmp(".reloc", (char*)secname)
3122           && 0 != strcmp(".rdata$zzz", (char*)secname)
3123          ) {
3124          errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", secname, oc->fileName);
3125          stgFree(secname);
3126          return 0;
3127       }
3128
3129       if (kind != SECTIONKIND_OTHER && end >= start) {
3130          addSection(oc, kind, start, end);
3131          addProddableBlock(oc, start, end - start + 1);
3132       }
3133
3134       stgFree(secname);
3135    }
3136
3137    /* Copy exported symbols into the ObjectCode. */
3138
3139    oc->n_symbols = hdr->NumberOfSymbols;
3140    oc->symbols   = stgMallocBytes(oc->n_symbols * sizeof(char*),
3141                                   "ocGetNames_PEi386(oc->symbols)");
3142    /* Call me paranoid; I don't care. */
3143    for (i = 0; i < oc->n_symbols; i++)
3144       oc->symbols[i] = NULL;
3145
3146    i = 0;
3147    while (1) {
3148       COFF_symbol* symtab_i;
3149       if (i >= (Int32)(hdr->NumberOfSymbols)) break;
3150       symtab_i = (COFF_symbol*)
3151                  myindex ( sizeof_COFF_symbol, symtab, i );
3152
3153       addr  = NULL;
3154
3155       if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
3156           && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
3157          /* This symbol is global and defined, viz, exported */
3158          /* for MYIMAGE_SYMCLASS_EXTERNAL
3159                 && !MYIMAGE_SYM_UNDEFINED,
3160             the address of the symbol is:
3161                 address of relevant section + offset in section
3162          */
3163          COFF_section* sectabent
3164             = (COFF_section*) myindex ( sizeof_COFF_section,
3165                                         sectab,
3166                                         symtab_i->SectionNumber-1 );
3167          addr = ((UChar*)(oc->image))
3168                 + (sectabent->PointerToRawData
3169                    + symtab_i->Value);
3170       }
3171       else
3172       if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
3173           && symtab_i->Value > 0) {
3174          /* This symbol isn't in any section at all, ie, global bss.
3175             Allocate zeroed space for it. */
3176          addr = stgCallocBytes(1, symtab_i->Value,
3177                                "ocGetNames_PEi386(non-anonymous bss)");
3178          addSection(oc, SECTIONKIND_RWDATA, addr,
3179                         ((UChar*)addr) + symtab_i->Value - 1);
3180          addProddableBlock(oc, addr, symtab_i->Value);
3181          /* debugBelch("BSS      section at 0x%x\n", addr); */
3182       }
3183
3184       if (addr != NULL ) {
3185          sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
3186          /* debugBelch("addSymbol %p `%s \n", addr,sname);  */
3187          IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
3188          ASSERT(i >= 0 && i < oc->n_symbols);
3189          /* cstring_from_COFF_symbol_name always succeeds. */
3190          oc->symbols[i] = (char*)sname;
3191          ghciInsertStrHashTable(oc->fileName, symhash, (char*)sname, addr);
3192       } else {
3193 #        if 0
3194          debugBelch(
3195                    "IGNORING symbol %d\n"
3196                    "     name `",
3197                    i
3198                  );
3199          printName ( symtab_i->Name, strtab );
3200          debugBelch(
3201                    "'\n"
3202                    "    value 0x%x\n"
3203                    "   1+sec# %d\n"
3204                    "     type 0x%x\n"
3205                    "   sclass 0x%x\n"
3206                    "     nAux %d\n",
3207                    symtab_i->Value,
3208                    (Int32)(symtab_i->SectionNumber),
3209                    (UInt32)symtab_i->Type,
3210                    (UInt32)symtab_i->StorageClass,
3211                    (UInt32)symtab_i->NumberOfAuxSymbols
3212                  );
3213 #        endif
3214       }
3215
3216       i += symtab_i->NumberOfAuxSymbols;
3217       i++;
3218    }
3219
3220    return 1;
3221 }
3222
3223
3224 static int
3225 ocResolve_PEi386 ( ObjectCode* oc )
3226 {
3227    COFF_header*  hdr;
3228    COFF_section* sectab;
3229    COFF_symbol*  symtab;
3230    UChar*        strtab;
3231
3232    UInt32        A;
3233    UInt32        S;
3234    UInt32*       pP;
3235
3236    int i;
3237    UInt32 j, noRelocs;
3238
3239    /* ToDo: should be variable-sized?  But is at least safe in the
3240       sense of buffer-overrun-proof. */
3241    UChar symbol[1000];
3242    /* debugBelch("resolving for %s\n", oc->fileName); */
3243
3244    hdr = (COFF_header*)(oc->image);
3245    sectab = (COFF_section*) (
3246                ((UChar*)(oc->image))
3247                + sizeof_COFF_header + hdr->SizeOfOptionalHeader
3248             );
3249    symtab = (COFF_symbol*) (
3250                ((UChar*)(oc->image))
3251                + hdr->PointerToSymbolTable
3252             );
3253    strtab = ((UChar*)(oc->image))
3254             + hdr->PointerToSymbolTable
3255             + hdr->NumberOfSymbols * sizeof_COFF_symbol;
3256
3257    for (i = 0; i < hdr->NumberOfSections; i++) {
3258       COFF_section* sectab_i
3259          = (COFF_section*)
3260            myindex ( sizeof_COFF_section, sectab, i );
3261       COFF_reloc* reltab
3262          = (COFF_reloc*) (
3263               ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
3264            );
3265
3266       char *secname = cstring_from_section_name(sectab_i->Name, strtab);
3267
3268       /* Ignore sections called which contain stabs debugging
3269          information. */
3270       if (0 == strcmp(".stab", (char*)secname)
3271           || 0 == strcmp(".stabstr", (char*)secname)
3272           || 0 == strcmp(".ctors", (char*)secname)
3273           || 0 == strncmp(".debug", (char*)secname, 6)
3274           || 0 == strcmp(".rdata$zzz", (char*)secname)) {
3275           stgFree(secname);
3276           continue;
3277       }
3278
3279       stgFree(secname);
3280
3281       if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
3282         /* If the relocation field (a short) has overflowed, the
3283          * real count can be found in the first reloc entry.
3284          *
3285          * See Section 4.1 (last para) of the PE spec (rev6.0).
3286          *
3287          * Nov2003 update: the GNU linker still doesn't correctly
3288          * handle the generation of relocatable object files with
3289          * overflown relocations. Hence the output to warn of potential
3290          * troubles.
3291          */
3292         COFF_reloc* rel = (COFF_reloc*)
3293                            myindex ( sizeof_COFF_reloc, reltab, 0 );
3294         noRelocs = rel->VirtualAddress;
3295
3296         /* 10/05: we now assume (and check for) a GNU ld that is capable
3297          * of handling object files with (>2^16) of relocs.
3298          */
3299 #if 0
3300         debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
3301                    noRelocs);
3302 #endif
3303         j = 1;
3304       } else {
3305         noRelocs = sectab_i->NumberOfRelocations;
3306         j = 0;
3307       }
3308
3309
3310       for (; j < noRelocs; j++) {
3311          COFF_symbol* sym;
3312          COFF_reloc* reltab_j
3313             = (COFF_reloc*)
3314               myindex ( sizeof_COFF_reloc, reltab, j );
3315
3316          /* the location to patch */
3317          pP = (UInt32*)(
3318                  ((UChar*)(oc->image))
3319                  + (sectab_i->PointerToRawData
3320                     + reltab_j->VirtualAddress
3321                     - sectab_i->VirtualAddress )
3322               );
3323          /* the existing contents of pP */
3324          A = *pP;
3325          /* the symbol to connect to */
3326          sym = (COFF_symbol*)
3327                myindex ( sizeof_COFF_symbol,
3328                          symtab, reltab_j->SymbolTableIndex );
3329          IF_DEBUG(linker,
3330                   debugBelch(
3331                             "reloc sec %2d num %3d:  type 0x%-4x   "
3332                             "vaddr 0x%-8x   name `",
3333                             i, j,
3334                             (UInt32)reltab_j->Type,
3335                             reltab_j->VirtualAddress );
3336                             printName ( sym->Name, strtab );
3337                             debugBelch("'\n" ));
3338
3339          if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
3340             COFF_section* section_sym
3341                = findPEi386SectionCalled ( oc, sym->Name );
3342             if (!section_sym) {
3343                errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
3344                return 0;
3345             }
3346             S = ((UInt32)(oc->image))
3347                 + (section_sym->PointerToRawData
3348                    + sym->Value);
3349          } else {
3350             copyName ( sym->Name, strtab, symbol, 1000-1 );
3351             S = (UInt32) lookupSymbol( (char*)symbol );
3352             if ((void*)S != NULL) goto foundit;
3353             errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3354             return 0;
3355            foundit:;
3356          }
3357          checkProddableBlock(oc, pP);
3358          switch (reltab_j->Type) {
3359             case MYIMAGE_REL_I386_DIR32:
3360                *pP = A + S;
3361                break;
3362             case MYIMAGE_REL_I386_REL32:
3363                /* Tricky.  We have to insert a displacement at
3364                   pP which, when added to the PC for the _next_
3365                   insn, gives the address of the target (S).
3366                   Problem is to know the address of the next insn
3367                   when we only know pP.  We assume that this
3368                   literal field is always the last in the insn,
3369                   so that the address of the next insn is pP+4
3370                   -- hence the constant 4.
3371                   Also I don't know if A should be added, but so
3372                   far it has always been zero.
3373
3374                   SOF 05/2005: 'A' (old contents of *pP) have been observed
3375                   to contain values other than zero (the 'wx' object file
3376                   that came with wxhaskell-0.9.4; dunno how it was compiled..).
3377                   So, add displacement to old value instead of asserting
3378                   A to be zero. Fixes wxhaskell-related crashes, and no other
3379                   ill effects have been observed.
3380
3381                   Update: the reason why we're seeing these more elaborate
3382                   relocations is due to a switch in how the NCG compiles SRTs
3383                   and offsets to them from info tables. SRTs live in .(ro)data,
3384                   while info tables live in .text, causing GAS to emit REL32/DISP32
3385                   relocations with non-zero values. Adding the displacement is
3386                   the right thing to do.
3387                */
3388                *pP = S - ((UInt32)pP) - 4 + A;
3389                break;
3390             default:
3391                debugBelch("%s: unhandled PEi386 relocation type %d",
3392                      oc->fileName, reltab_j->Type);
3393                return 0;
3394          }
3395
3396       }
3397    }
3398
3399    IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
3400    return 1;
3401 }
3402
3403 #endif /* defined(OBJFORMAT_PEi386) */
3404
3405
3406 /* --------------------------------------------------------------------------
3407  * ELF specifics
3408  * ------------------------------------------------------------------------*/
3409
3410 #if defined(OBJFORMAT_ELF)
3411
3412 #define FALSE 0
3413 #define TRUE  1
3414
3415 #if defined(sparc_HOST_ARCH)
3416 #  define ELF_TARGET_SPARC  /* Used inside <elf.h> */
3417 #elif defined(i386_HOST_ARCH)
3418 #  define ELF_TARGET_386    /* Used inside <elf.h> */
3419 #elif defined(x86_64_HOST_ARCH)
3420 #  define ELF_TARGET_X64_64
3421 #  define ELF_64BIT
3422 #endif
3423
3424 #if !defined(openbsd_HOST_OS)
3425 #  include <elf.h>
3426 #else
3427 /* openbsd elf has things in different places, with diff names */
3428 #  include <elf_abi.h>
3429 #  include <machine/reloc.h>
3430 #  define R_386_32    RELOC_32
3431 #  define R_386_PC32  RELOC_PC32
3432 #endif
3433
3434 /* If elf.h doesn't define it */
3435 #  ifndef R_X86_64_PC64
3436 #    define R_X86_64_PC64 24
3437 #  endif
3438
3439 /*
3440  * Define a set of types which can be used for both ELF32 and ELF64
3441  */
3442
3443 #ifdef ELF_64BIT
3444 #define ELFCLASS    ELFCLASS64
3445 #define Elf_Addr    Elf64_Addr
3446 #define Elf_Word    Elf64_Word
3447 #define Elf_Sword   Elf64_Sword
3448 #define Elf_Ehdr    Elf64_Ehdr
3449 #define Elf_Phdr    Elf64_Phdr
3450 #define Elf_Shdr    Elf64_Shdr
3451 #define Elf_Sym     Elf64_Sym
3452 #define Elf_Rel     Elf64_Rel
3453 #define Elf_Rela    Elf64_Rela
3454 #ifndef ELF_ST_TYPE
3455 #define ELF_ST_TYPE ELF64_ST_TYPE
3456 #endif
3457 #ifndef ELF_ST_BIND
3458 #define ELF_ST_BIND ELF64_ST_BIND
3459 #endif
3460 #ifndef ELF_R_TYPE
3461 #define ELF_R_TYPE  ELF64_R_TYPE
3462 #endif
3463 #ifndef ELF_R_SYM
3464 #define ELF_R_SYM   ELF64_R_SYM
3465 #endif
3466 #else
3467 #define ELFCLASS    ELFCLASS32
3468 #define Elf_Addr    Elf32_Addr
3469 #define Elf_Word    Elf32_Word
3470 #define Elf_Sword   Elf32_Sword
3471 #define Elf_Ehdr    Elf32_Ehdr
3472 #define Elf_Phdr    Elf32_Phdr
3473 #define Elf_Shdr    Elf32_Shdr
3474 #define Elf_Sym     Elf32_Sym
3475 #define Elf_Rel     Elf32_Rel
3476 #define Elf_Rela    Elf32_Rela
3477 #ifndef ELF_ST_TYPE
3478 #define ELF_ST_TYPE ELF32_ST_TYPE
3479 #endif
3480 #ifndef ELF_ST_BIND
3481 #define ELF_ST_BIND ELF32_ST_BIND
3482 #endif
3483 #ifndef ELF_R_TYPE
3484 #define ELF_R_TYPE  ELF32_R_TYPE
3485 #endif
3486 #ifndef ELF_R_SYM
3487 #define ELF_R_SYM   ELF32_R_SYM
3488 #endif
3489 #endif
3490
3491
3492 /*
3493  * Functions to allocate entries in dynamic sections.  Currently we simply
3494  * preallocate a large number, and we don't check if a entry for the given
3495  * target already exists (a linear search is too slow).  Ideally these
3496  * entries would be associated with symbols.
3497  */
3498
3499 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
3500 #define GOT_SIZE            0x20000
3501 #define FUNCTION_TABLE_SIZE 0x10000
3502 #define PLT_SIZE            0x08000
3503
3504 #ifdef ELF_NEED_GOT
3505 static Elf_Addr got[GOT_SIZE];
3506 static unsigned int gotIndex;
3507 static Elf_Addr gp_val = (Elf_Addr)got;
3508
3509 static Elf_Addr
3510 allocateGOTEntry(Elf_Addr target)
3511 {
3512    Elf_Addr *entry;
3513
3514    if (gotIndex >= GOT_SIZE)
3515       barf("Global offset table overflow");
3516
3517    entry = &got[gotIndex++];
3518    *entry = target;
3519    return (Elf_Addr)entry;
3520 }
3521 #endif
3522
3523 #ifdef ELF_FUNCTION_DESC
3524 typedef struct {
3525    Elf_Addr ip;
3526    Elf_Addr gp;
3527 } FunctionDesc;
3528
3529 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
3530 static unsigned int functionTableIndex;
3531
3532 static Elf_Addr
3533 allocateFunctionDesc(Elf_Addr target)
3534 {
3535    FunctionDesc *entry;
3536
3537    if (functionTableIndex >= FUNCTION_TABLE_SIZE)
3538       barf("Function table overflow");
3539
3540    entry = &functionTable[functionTableIndex++];
3541    entry->ip = target;
3542    entry->gp = (Elf_Addr)gp_val;
3543    return (Elf_Addr)entry;
3544 }
3545
3546 static Elf_Addr
3547 copyFunctionDesc(Elf_Addr target)
3548 {
3549    FunctionDesc *olddesc = (FunctionDesc *)target;
3550    FunctionDesc *newdesc;
3551
3552    newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
3553    newdesc->gp = olddesc->gp;
3554    return (Elf_Addr)newdesc;
3555 }
3556 #endif
3557
3558 #ifdef ELF_NEED_PLT
3559
3560 typedef struct {
3561    unsigned char code[sizeof(plt_code)];
3562 } PLTEntry;
3563
3564 static Elf_Addr
3565 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
3566 {
3567    PLTEntry *plt = (PLTEntry *)oc->plt;
3568    PLTEntry *entry;
3569
3570    if (oc->pltIndex >= PLT_SIZE)
3571       barf("Procedure table overflow");
3572
3573    entry = &plt[oc->pltIndex++];
3574    memcpy(entry->code, plt_code, sizeof(entry->code));
3575    PLT_RELOC(entry->code, target);
3576    return (Elf_Addr)entry;
3577 }
3578
3579 static unsigned int
3580 PLTSize(void)
3581 {
3582    return (PLT_SIZE * sizeof(PLTEntry));
3583 }
3584 #endif
3585
3586
3587 /*
3588  * Generic ELF functions
3589  */
3590
3591 static char *
3592 findElfSection ( void* objImage, Elf_Word sh_type )
3593 {
3594    char* ehdrC = (char*)objImage;
3595    Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3596    Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3597    char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3598    char* ptr = NULL;
3599    int i;
3600
3601    for (i = 0; i < ehdr->e_shnum; i++) {
3602       if (shdr[i].sh_type == sh_type
3603           /* Ignore the section header's string table. */
3604           && i != ehdr->e_shstrndx
3605           /* Ignore string tables named .stabstr, as they contain
3606              debugging info. */
3607           && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3608          ) {
3609          ptr = ehdrC + shdr[i].sh_offset;
3610          break;
3611       }
3612    }
3613    return ptr;
3614 }
3615
3616 static int
3617 ocVerifyImage_ELF ( ObjectCode* oc )
3618 {
3619    Elf_Shdr* shdr;
3620    Elf_Sym*  stab;
3621    int i, j, nent, nstrtab, nsymtabs;
3622    char* sh_strtab;
3623    char* strtab;
3624
3625    char*     ehdrC = (char*)(oc->image);
3626    Elf_Ehdr* ehdr  = (Elf_Ehdr*)ehdrC;
3627
3628    if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
3629        ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
3630        ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
3631        ehdr->e_ident[EI_MAG3] != ELFMAG3) {
3632       errorBelch("%s: not an ELF object", oc->fileName);
3633       return 0;
3634    }
3635
3636    if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
3637       errorBelch("%s: unsupported ELF format", oc->fileName);
3638       return 0;
3639    }
3640
3641    if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
3642        IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
3643    } else
3644    if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
3645        IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
3646    } else {
3647        errorBelch("%s: unknown endiannness", oc->fileName);
3648        return 0;
3649    }
3650
3651    if (ehdr->e_type != ET_REL) {
3652       errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
3653       return 0;
3654    }
3655    IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
3656
3657    IF_DEBUG(linker,debugBelch( "Architecture is " ));
3658    switch (ehdr->e_machine) {
3659       case EM_386:   IF_DEBUG(linker,debugBelch( "x86" )); break;
3660 #ifdef EM_SPARC32PLUS
3661       case EM_SPARC32PLUS:
3662 #endif
3663       case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
3664 #ifdef EM_IA_64
3665       case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
3666 #endif
3667       case EM_PPC:   IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
3668 #ifdef EM_X86_64
3669       case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
3670 #elif defined(EM_AMD64)
3671       case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
3672 #endif
3673       default:       IF_DEBUG(linker,debugBelch( "unknown" ));
3674                      errorBelch("%s: unknown architecture (e_machine == %d)"
3675                                 , oc->fileName, ehdr->e_machine);
3676                      return 0;
3677    }
3678
3679    IF_DEBUG(linker,debugBelch(
3680              "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
3681              (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize  ));
3682
3683    ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
3684
3685    shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3686
3687    if (ehdr->e_shstrndx == SHN_UNDEF) {
3688       errorBelch("%s: no section header string table", oc->fileName);
3689       return 0;
3690    } else {
3691       IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
3692                           ehdr->e_shstrndx));
3693       sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3694    }
3695
3696    for (i = 0; i < ehdr->e_shnum; i++) {
3697       IF_DEBUG(linker,debugBelch("%2d:  ", i ));
3698       IF_DEBUG(linker,debugBelch("type=%2d  ", (int)shdr[i].sh_type ));
3699       IF_DEBUG(linker,debugBelch("size=%4d  ", (int)shdr[i].sh_size ));
3700       IF_DEBUG(linker,debugBelch("offs=%4d  ", (int)shdr[i].sh_offset ));
3701       IF_DEBUG(linker,debugBelch("  (%p .. %p)  ",
3702                ehdrC + shdr[i].sh_offset,
3703                       ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3704
3705       if (shdr[i].sh_type == SHT_REL) {
3706           IF_DEBUG(linker,debugBelch("Rel  " ));
3707       } else if (shdr[i].sh_type == SHT_RELA) {
3708           IF_DEBUG(linker,debugBelch("RelA " ));
3709       } else {
3710           IF_DEBUG(linker,debugBelch("     "));
3711       }
3712       if (sh_strtab) {
3713           IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3714       }
3715    }
3716
3717    IF_DEBUG(linker,debugBelch( "\nString tables" ));
3718    strtab = NULL;
3719    nstrtab = 0;
3720    for (i = 0; i < ehdr->e_shnum; i++) {
3721       if (shdr[i].sh_type == SHT_STRTAB
3722           /* Ignore the section header's string table. */
3723           && i != ehdr->e_shstrndx
3724           /* Ignore string tables named .stabstr, as they contain
3725              debugging info. */
3726           && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3727          ) {
3728          IF_DEBUG(linker,debugBelch("   section %d is a normal string table", i ));
3729          strtab = ehdrC + shdr[i].sh_offset;
3730          nstrtab++;
3731       }
3732    }
3733    if (nstrtab != 1) {
3734       errorBelch("%s: no string tables, or too many", oc->fileName);
3735       return 0;
3736    }
3737
3738    nsymtabs = 0;
3739    IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
3740    for (i = 0; i < ehdr->e_shnum; i++) {
3741       if (shdr[i].sh_type != SHT_SYMTAB) continue;
3742       IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3743       nsymtabs++;
3744       stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3745       nent = shdr[i].sh_size / sizeof(Elf_Sym);
3746       IF_DEBUG(linker,debugBelch( "   number of entries is apparently %d (%ld rem)\n",
3747                nent,
3748                (long)shdr[i].sh_size % sizeof(Elf_Sym)
3749              ));
3750       if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
3751          errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
3752          return 0;
3753       }
3754       for (j = 0; j < nent; j++) {
3755          IF_DEBUG(linker,debugBelch("   %2d  ", j ));
3756          IF_DEBUG(linker,debugBelch("  sec=%-5d  size=%-3d  val=%5p  ",
3757                              (int)stab[j].st_shndx,
3758                              (int)stab[j].st_size,
3759                              (char*)stab[j].st_value ));
3760
3761          IF_DEBUG(linker,debugBelch("type=" ));
3762          switch (ELF_ST_TYPE(stab[j].st_info)) {
3763             case STT_NOTYPE:  IF_DEBUG(linker,debugBelch("notype " )); break;
3764             case STT_OBJECT:  IF_DEBUG(linker,debugBelch("object " )); break;
3765             case STT_FUNC  :  IF_DEBUG(linker,debugBelch("func   " )); break;
3766             case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
3767             case STT_FILE:    IF_DEBUG(linker,debugBelch("file   " )); break;
3768             default:          IF_DEBUG(linker,debugBelch("?      " )); break;
3769          }
3770          IF_DEBUG(linker,debugBelch("  " ));
3771
3772          IF_DEBUG(linker,debugBelch("bind=" ));
3773          switch (ELF_ST_BIND(stab[j].st_info)) {
3774             case STB_LOCAL :  IF_DEBUG(linker,debugBelch("local " )); break;
3775             case STB_GLOBAL:  IF_DEBUG(linker,debugBelch("global" )); break;
3776             case STB_WEAK  :  IF_DEBUG(linker,debugBelch("weak  " )); break;
3777             default:          IF_DEBUG(linker,debugBelch("?     " )); break;
3778          }
3779          IF_DEBUG(linker,debugBelch("  " ));
3780
3781          IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
3782       }
3783    }
3784
3785    if (nsymtabs == 0) {
3786       errorBelch("%s: didn't find any symbol tables", oc->fileName);
3787       return 0;
3788    }
3789
3790    return 1;
3791 }
3792
3793 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
3794 {
3795     *is_bss = FALSE;
3796
3797     if (hdr->sh_type == SHT_PROGBITS
3798         && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
3799         /* .text-style section */
3800         return SECTIONKIND_CODE_OR_RODATA;
3801     }
3802
3803     if (hdr->sh_type == SHT_PROGBITS
3804             && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3805             /* .data-style section */
3806             return SECTIONKIND_RWDATA;
3807     }
3808
3809     if (hdr->sh_type == SHT_PROGBITS
3810         && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
3811         /* .rodata-style section */
3812         return SECTIONKIND_CODE_OR_RODATA;
3813     }
3814
3815     if (hdr->sh_type == SHT_NOBITS
3816         && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3817         /* .bss-style section */
3818         *is_bss = TRUE;
3819         return SECTIONKIND_RWDATA;
3820     }
3821
3822     return SECTIONKIND_OTHER;
3823 }
3824
3825
3826 static int
3827 ocGetNames_ELF ( ObjectCode* oc )
3828 {
3829    int i, j, k, nent;
3830    Elf_Sym* stab;
3831
3832    char*     ehdrC    = (char*)(oc->image);
3833    Elf_Ehdr* ehdr     = (Elf_Ehdr*)ehdrC;
3834    char*     strtab   = findElfSection ( ehdrC, SHT_STRTAB );
3835    Elf_Shdr* shdr     = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3836
3837    ASSERT(symhash != NULL);
3838
3839    if (!strtab) {
3840       errorBelch("%s: no strtab", oc->fileName);
3841       return 0;
3842    }
3843
3844    k = 0;
3845    for (i = 0; i < ehdr->e_shnum; i++) {
3846       /* Figure out what kind of section it is.  Logic derived from
3847          Figure 1.14 ("Special Sections") of the ELF document
3848          ("Portable Formats Specification, Version 1.1"). */
3849       int         is_bss = FALSE;
3850       SectionKind kind   = getSectionKind_ELF(&shdr[i], &is_bss);
3851
3852       if (is_bss && shdr[i].sh_size > 0) {
3853          /* This is a non-empty .bss section.  Allocate zeroed space for
3854             it, and set its .sh_offset field such that
3855             ehdrC + .sh_offset == addr_of_zeroed_space.  */
3856          char* zspace = stgCallocBytes(1, shdr[i].sh_size,
3857                                        "ocGetNames_ELF(BSS)");
3858          shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
3859          /*
3860          debugBelch("BSS section at 0x%x, size %d\n",
3861                          zspace, shdr[i].sh_size);
3862          */
3863       }
3864
3865       /* fill in the section info */
3866       if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
3867          addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
3868          addSection(oc, kind, ehdrC + shdr[i].sh_offset,
3869                         ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
3870       }
3871
3872       if (shdr[i].sh_type != SHT_SYMTAB) continue;
3873
3874       /* copy stuff into this module's object symbol table */
3875       stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3876       nent = shdr[i].sh_size / sizeof(Elf_Sym);
3877
3878       oc->n_symbols = nent;
3879       oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3880                                    "ocGetNames_ELF(oc->symbols)");
3881
3882       for (j = 0; j < nent; j++) {
3883
3884          char  isLocal = FALSE; /* avoids uninit-var warning */
3885          char* ad      = NULL;
3886          char* nm      = strtab + stab[j].st_name;
3887          int   secno   = stab[j].st_shndx;
3888
3889          /* Figure out if we want to add it; if so, set ad to its
3890             address.  Otherwise leave ad == NULL. */
3891
3892          if (secno == SHN_COMMON) {
3893             isLocal = FALSE;
3894             ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
3895             /*
3896             debugBelch("COMMON symbol, size %d name %s\n",
3897                             stab[j].st_size, nm);
3898             */
3899             /* Pointless to do addProddableBlock() for this area,
3900                since the linker should never poke around in it. */
3901          }
3902          else
3903          if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
3904                 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
3905               )
3906               /* and not an undefined symbol */
3907               && stab[j].st_shndx != SHN_UNDEF
3908               /* and not in a "special section" */
3909               && stab[j].st_shndx < SHN_LORESERVE
3910               &&
3911               /* and it's a not a section or string table or anything silly */
3912               ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3913                 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3914                 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3915               )
3916             ) {
3917             /* Section 0 is the undefined section, hence > and not >=. */
3918             ASSERT(secno > 0 && secno < ehdr->e_shnum);
3919             /*
3920             if (shdr[secno].sh_type == SHT_NOBITS) {
3921                debugBelch("   BSS symbol, size %d off %d name %s\n",
3922                                stab[j].st_size, stab[j].st_value, nm);
3923             }
3924             */
3925             ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3926             if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3927                isLocal = TRUE;
3928             } else {
3929 #ifdef ELF_FUNCTION_DESC
3930                /* dlsym() and the initialisation table both give us function
3931                 * descriptors, so to be consistent we store function descriptors
3932                 * in the symbol table */
3933                if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3934                    ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3935 #endif
3936                IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p  %s %s\n",
3937                                       ad, oc->fileName, nm ));
3938                isLocal = FALSE;
3939             }
3940          }
3941
3942          /* And the decision is ... */
3943
3944          if (ad != NULL) {
3945             ASSERT(nm != NULL);
3946             oc->symbols[j] = nm;
3947             /* Acquire! */
3948             if (isLocal) {
3949                /* Ignore entirely. */
3950             } else {
3951                ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3952             }
3953          } else {
3954             /* Skip. */
3955             IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3956                                    strtab + stab[j].st_name ));
3957             /*
3958             debugBelch(
3959                     "skipping   bind = %d,  type = %d,  shndx = %d   `%s'\n",
3960                     (int)ELF_ST_BIND(stab[j].st_info),
3961                     (int)ELF_ST_TYPE(stab[j].st_info),
3962                     (int)stab[j].st_shndx,
3963                     strtab + stab[j].st_name
3964                    );
3965             */
3966             oc->symbols[j] = NULL;
3967          }
3968
3969       }
3970    }
3971
3972    return 1;
3973 }
3974
3975 /* Do ELF relocations which lack an explicit addend.  All x86-linux
3976    relocations appear to be of this form. */
3977 static int
3978 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3979                          Elf_Shdr* shdr, int shnum,
3980                          Elf_Sym*  stab, char* strtab )
3981 {
3982    int j;
3983    char *symbol;
3984    Elf_Word* targ;
3985    Elf_Rel*  rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3986    int         nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3987    int target_shndx = shdr[shnum].sh_info;
3988    int symtab_shndx = shdr[shnum].sh_link;
3989
3990    stab  = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3991    targ  = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3992    IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3993                           target_shndx, symtab_shndx ));
3994
3995    /* Skip sections that we're not interested in. */
3996    {
3997        int is_bss;
3998        SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3999        if (kind == SECTIONKIND_OTHER) {
4000            IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
4001            return 1;
4002        }
4003    }
4004
4005    for (j = 0; j < nent; j++) {
4006       Elf_Addr offset = rtab[j].r_offset;
4007       Elf_Addr info   = rtab[j].r_info;
4008
4009       Elf_Addr  P  = ((Elf_Addr)targ) + offset;
4010       Elf_Word* pP = (Elf_Word*)P;
4011       Elf_Addr  A  = *pP;
4012       Elf_Addr  S;
4013       void*     S_tmp;
4014       Elf_Addr  value;
4015       StgStablePtr stablePtr;
4016       StgPtr stableVal;
4017
4018       IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
4019                              j, (void*)offset, (void*)info ));
4020       if (!info) {
4021          IF_DEBUG(linker,debugBelch( " ZERO" ));
4022          S = 0;
4023       } else {
4024          Elf_Sym sym = stab[ELF_R_SYM(info)];
4025          /* First see if it is a local symbol. */
4026          if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
4027             /* Yes, so we can get the address directly from the ELF symbol
4028                table. */
4029             symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
4030             S = (Elf_Addr)
4031                 (ehdrC + shdr[ sym.st_shndx ].sh_offset
4032                        + stab[ELF_R_SYM(info)].st_value);
4033
4034          } else {
4035             symbol = strtab + sym.st_name;
4036             stablePtr = (StgStablePtr)lookupHashTable(stablehash, (StgWord)symbol);
4037             if (NULL == stablePtr) {
4038               /* No, so look up the name in our global table. */
4039               S_tmp = lookupSymbol( symbol );
4040               S = (Elf_Addr)S_tmp;
4041             } else {
4042               stableVal = deRefStablePtr( stablePtr );
4043               S_tmp = stableVal;
4044               S = (Elf_Addr)S_tmp;
4045             }
4046          }
4047          if (!S) {
4048             errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4049             return 0;
4050          }
4051          IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
4052       }
4053
4054       IF_DEBUG(linker,debugBelch( "Reloc: P = %p   S = %p   A = %p\n",
4055                              (void*)P, (void*)S, (void*)A ));
4056       checkProddableBlock ( oc, pP );
4057
4058       value = S + A;
4059
4060       switch (ELF_R_TYPE(info)) {
4061 #        ifdef i386_HOST_ARCH
4062          case R_386_32:   *pP = value;     break;
4063          case R_386_PC32: *pP = value - P; break;
4064 #        endif
4065          default:
4066             errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
4067                   oc->fileName, (lnat)ELF_R_TYPE(info));
4068             return 0;
4069       }
4070
4071    }
4072    return 1;
4073 }
4074
4075 /* Do ELF relocations for which explicit addends are supplied.
4076    sparc-solaris relocations appear to be of this form. */
4077 static int
4078 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
4079                           Elf_Shdr* shdr, int shnum,
4080                           Elf_Sym*  stab, char* strtab )
4081 {
4082    int j;
4083    char *symbol = NULL;
4084    Elf_Addr targ;
4085    Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
4086    int         nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
4087    int target_shndx = shdr[shnum].sh_info;
4088    int symtab_shndx = shdr[shnum].sh_link;
4089
4090    stab  = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
4091    targ  = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
4092    IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
4093                           target_shndx, symtab_shndx ));
4094
4095    for (j = 0; j < nent; j++) {
4096 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4097       /* This #ifdef only serves to avoid unused-var warnings. */
4098       Elf_Addr  offset = rtab[j].r_offset;
4099       Elf_Addr  P      = targ + offset;
4100 #endif
4101       Elf_Addr  info   = rtab[j].r_info;
4102       Elf_Addr  A      = rtab[j].r_addend;
4103       Elf_Addr  S;
4104       void*     S_tmp;
4105       Elf_Addr  value;
4106 #     if defined(sparc_HOST_ARCH)
4107       Elf_Word* pP = (Elf_Word*)P;
4108       Elf_Word  w1, w2;
4109 #     elif defined(powerpc_HOST_ARCH)
4110       Elf_Sword delta;
4111 #     endif
4112
4113       IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p)   ",
4114                              j, (void*)offset, (void*)info,
4115                                 (void*)A ));
4116       if (!info) {
4117          IF_DEBUG(linker,debugBelch( " ZERO" ));
4118          S = 0;
4119       } else {
4120          Elf_Sym sym = stab[ELF_R_SYM(info)];
4121          /* First see if it is a local symbol. */
4122          if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
4123             /* Yes, so we can get the address directly from the ELF symbol
4124                table. */
4125             symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
4126             S = (Elf_Addr)
4127                 (ehdrC + shdr[ sym.st_shndx ].sh_offset
4128                        + stab[ELF_R_SYM(info)].st_value);
4129 #ifdef ELF_FUNCTION_DESC
4130             /* Make a function descriptor for this function */
4131             if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
4132                S = allocateFunctionDesc(S + A);
4133                A = 0;
4134             }
4135 #endif
4136          } else {
4137             /* No, so look up the name in our global table. */
4138             symbol = strtab + sym.st_name;
4139             S_tmp = lookupSymbol( symbol );
4140             S = (Elf_Addr)S_tmp;
4141
4142 #ifdef ELF_FUNCTION_DESC
4143             /* If a function, already a function descriptor - we would
4144                have to copy it to add an offset. */
4145             if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
4146                errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
4147 #endif
4148          }
4149          if (!S) {
4150            errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
4151            return 0;
4152          }
4153          IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
4154       }
4155
4156       IF_DEBUG(linker,debugBelch("Reloc: P = %p   S = %p   A = %p\n",
4157                                         (void*)P, (void*)S, (void*)A ));
4158       /* checkProddableBlock ( oc, (void*)P ); */
4159
4160       value = S + A;
4161
4162       switch (ELF_R_TYPE(info)) {
4163 #        if defined(sparc_HOST_ARCH)
4164          case R_SPARC_WDISP30:
4165             w1 = *pP & 0xC0000000;
4166             w2 = (Elf_Word)((value - P) >> 2);
4167             ASSERT((w2 & 0xC0000000) == 0);
4168             w1 |= w2;
4169             *pP = w1;
4170             break;
4171          case R_SPARC_HI22:
4172             w1 = *pP & 0xFFC00000;
4173             w2 = (Elf_Word)(value >> 10);
4174             ASSERT((w2 & 0xFFC00000) == 0);
4175             w1 |= w2;
4176             *pP = w1;
4177             break;
4178          case R_SPARC_LO10:
4179             w1 = *pP & ~0x3FF;
4180             w2 = (Elf_Word)(value & 0x3FF);
4181             ASSERT((w2 & ~0x3FF) == 0);
4182             w1 |= w2;
4183             *pP = w1;
4184             break;
4185
4186          /* According to the Sun documentation:
4187             R_SPARC_UA32
4188             This relocation type resembles R_SPARC_32, except it refers to an
4189             unaligned word. That is, the word to be relocated must be treated
4190             as four separate bytes with arbitrary alignment, not as a word
4191             aligned according to the architecture requirements.
4192          */
4193          case R_SPARC_UA32:
4194             w2  = (Elf_Word)value;
4195
4196             // SPARC doesn't do misaligned writes of 32 bit words,
4197             //       so we have to do this one byte-at-a-time.
4198             char *pPc   = (char*)pP;
4199             pPc[0]      = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
4200             pPc[1]      = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
4201             pPc[2]      = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
4202             pPc[3]      = (char) ((Elf_Word)(w2 & 0x000000ff));
4203             break;
4204
4205          case R_SPARC_32:
4206             w2 = (Elf_Word)value;
4207             *pP = w2;
4208             break;
4209 #        elif defined(powerpc_HOST_ARCH)
4210          case R_PPC_ADDR16_LO:
4211             *(Elf32_Half*) P = value;
4212             break;
4213
4214          case R_PPC_ADDR16_HI:
4215             *(Elf32_Half*) P = value >> 16;
4216             break;
4217
4218          case R_PPC_ADDR16_HA:
4219             *(Elf32_Half*) P = (value + 0x8000) >> 16;
4220             break;
4221
4222          case R_PPC_ADDR32:
4223             *(Elf32_Word *) P = value;
4224             break;
4225
4226          case R_PPC_REL32:
4227             *(Elf32_Word *) P = value - P;
4228             break;
4229
4230          case R_PPC_REL24:
4231             delta = value - P;
4232
4233             if( delta << 6 >> 6 != delta )
4234             {
4235                value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
4236                                         ->jumpIsland);
4237                delta = value - P;
4238
4239                if( value == 0 || delta << 6 >> 6 != delta )
4240                {
4241                   barf( "Unable to make SymbolExtra for #%d",
4242                         ELF_R_SYM(info) );
4243                   return 0;
4244                }
4245             }
4246
4247             *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
4248                                           | (delta & 0x3fffffc);
4249             break;
4250 #        endif
4251
4252 #if x86_64_HOST_ARCH
4253       case R_X86_64_64:
4254           *(Elf64_Xword *)P = value;
4255           break;
4256
4257       case R_X86_64_PC32:
4258       {
4259 #if defined(ALWAYS_PIC)
4260           barf("R_X86_64_PC32 relocation, but ALWAYS_PIC.");
4261 #else
4262           StgInt64 off = value - P;
4263           if (off >= 0x7fffffffL || off < -0x80000000L) {
4264 #if X86_64_ELF_NONPIC_HACK
4265               StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4266                                                 -> jumpIsland;
4267               off = pltAddress + A - P;
4268 #else
4269               barf("R_X86_64_PC32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4270                    symbol, off, oc->fileName );
4271 #endif
4272           }
4273           *(Elf64_Word *)P = (Elf64_Word)off;
4274 #endif
4275           break;
4276       }
4277
4278       case R_X86_64_PC64:
4279       {
4280           StgInt64 off = value - P;
4281           *(Elf64_Word *)P = (Elf64_Word)off;
4282           break;
4283       }
4284
4285       case R_X86_64_32:
4286 #if defined(ALWAYS_PIC)
4287           barf("R_X86_64_32 relocation, but ALWAYS_PIC.");
4288 #else
4289           if (value >= 0x7fffffffL) {
4290 #if X86_64_ELF_NONPIC_HACK
4291               StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4292                                                 -> jumpIsland;
4293               value = pltAddress + A;
4294 #else
4295               barf("R_X86_64_32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4296                    symbol, value, oc->fileName );
4297 #endif
4298           }
4299           *(Elf64_Word *)P = (Elf64_Word)value;
4300 #endif
4301           break;
4302
4303       case R_X86_64_32S:
4304 #if defined(ALWAYS_PIC)
4305           barf("R_X86_64_32S relocation, but ALWAYS_PIC.");
4306 #else
4307           if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
4308 #if X86_64_ELF_NONPIC_HACK
4309               StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4310                                                 -> jumpIsland;
4311               value = pltAddress + A;
4312 #else
4313               barf("R_X86_64_32S relocation out of range: %s = %p\nRecompile %s with -fPIC.",
4314                    symbol, value, oc->fileName );
4315 #endif
4316           }
4317           *(Elf64_Sword *)P = (Elf64_Sword)value;
4318 #endif
4319           break;
4320
4321       case R_X86_64_GOTPCREL:
4322       {
4323           StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
4324           StgInt64 off = gotAddress + A - P;
4325           *(Elf64_Word *)P = (Elf64_Word)off;
4326           break;
4327       }
4328
4329       case R_X86_64_PLT32:
4330       {
4331 #if defined(ALWAYS_PIC)
4332           barf("R_X86_64_PLT32 relocation, but ALWAYS_PIC.");
4333 #else
4334           StgInt64 off = value - P;
4335           if (off >= 0x7fffffffL || off < -0x80000000L) {
4336               StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
4337                                                     -> jumpIsland;
4338               off = pltAddress + A - P;
4339           }
4340           *(Elf64_Word *)P = (Elf64_Word)off;
4341 #endif
4342           break;
4343       }
4344 #endif
4345
4346          default:
4347             errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
4348                   oc->fileName, (lnat)ELF_R_TYPE(info));
4349             return 0;
4350       }
4351
4352    }
4353    return 1;
4354 }
4355
4356 static int
4357 ocResolve_ELF ( ObjectCode* oc )
4358 {
4359    char *strtab;
4360    int   shnum, ok;
4361    Elf_Sym*  stab  = NULL;
4362    char*     ehdrC = (char*)(oc->image);
4363    Elf_Ehdr* ehdr  = (Elf_Ehdr*) ehdrC;
4364    Elf_Shdr* shdr  = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
4365
4366    /* first find "the" symbol table */
4367    stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
4368
4369    /* also go find the string table */
4370    strtab = findElfSection ( ehdrC, SHT_STRTAB );
4371
4372    if (stab == NULL || strtab == NULL) {
4373       errorBelch("%s: can't find string or symbol table", oc->fileName);
4374       return 0;
4375    }
4376
4377    /* Process the relocation sections. */
4378    for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
4379       if (shdr[shnum].sh_type == SHT_REL) {
4380          ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
4381                                        shnum, stab, strtab );
4382          if (!ok) return ok;
4383       }
4384       else
4385       if (shdr[shnum].sh_type == SHT_RELA) {
4386          ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
4387                                         shnum, stab, strtab );
4388          if (!ok) return ok;
4389       }
4390    }
4391
4392 #if defined(powerpc_HOST_ARCH)
4393    ocFlushInstructionCache( oc );
4394 #endif
4395
4396    return 1;
4397 }
4398
4399 /*
4400  * PowerPC & X86_64 ELF specifics
4401  */
4402
4403 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
4404
4405 static int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
4406 {
4407   Elf_Ehdr *ehdr;
4408   Elf_Shdr* shdr;
4409   int i;
4410
4411   ehdr = (Elf_Ehdr *) oc->image;
4412   shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
4413
4414   for( i = 0; i < ehdr->e_shnum; i++ )
4415     if( shdr[i].sh_type == SHT_SYMTAB )
4416       break;
4417
4418   if( i == ehdr->e_shnum )
4419   {
4420     errorBelch( "This ELF file contains no symtab" );
4421     return 0;
4422   }
4423
4424   if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
4425   {
4426     errorBelch( "The entry size (%d) of the symtab isn't %d\n",
4427       (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
4428
4429     return 0;
4430   }
4431
4432   return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
4433 }
4434
4435 #endif /* powerpc */
4436
4437 #endif /* ELF */
4438
4439 /* --------------------------------------------------------------------------
4440  * Mach-O specifics
4441  * ------------------------------------------------------------------------*/
4442
4443 #if defined(OBJFORMAT_MACHO)
4444
4445 /*
4446   Support for MachO linking on Darwin/MacOS X
4447   by Wolfgang Thaller (wolfgang.thaller@gmx.net)
4448
4449   I hereby formally apologize for the hackish nature of this code.
4450   Things that need to be done:
4451   *) implement ocVerifyImage_MachO
4452   *) add still more sanity checks.
4453 */
4454
4455 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4456 #define mach_header mach_header_64
4457 #define segment_command segment_command_64
4458 #define section section_64
4459 #define nlist nlist_64
4460 #endif
4461
4462 #ifdef powerpc_HOST_ARCH
4463 static int
4464 ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4465 {
4466     struct mach_header *header = (struct mach_header *) oc->image;
4467     struct load_command *lc = (struct load_command *) (header + 1);
4468     unsigned i;
4469
4470     IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: start\n"));
4471
4472     for (i = 0; i < header->ncmds; i++) {   
4473         if (lc->cmd == LC_SYMTAB) {
4474
4475                 // Find out the first and last undefined external
4476                 // symbol, so we don't have to allocate too many
4477             // jump islands/GOT entries.
4478
4479             struct symtab_command *symLC = (struct symtab_command *) lc;
4480             unsigned min = symLC->nsyms, max = 0;
4481             struct nlist *nlist =
4482                 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
4483                       : NULL;
4484
4485             for (i = 0; i < symLC->nsyms; i++) {
4486
4487                 if (nlist[i].n_type & N_STAB) {
4488                     ;
4489                 } else if (nlist[i].n_type & N_EXT) {
4490
4491                     if((nlist[i].n_type & N_TYPE) == N_UNDF
4492                         && (nlist[i].n_value == 0)) {
4493
4494                         if (i < min) {
4495                             min = i;
4496                         }
4497
4498                         if (i > max) {
4499                             max = i;
4500                     }
4501                 }
4502             }
4503             }
4504
4505             if (max >= min) {
4506                 return ocAllocateSymbolExtras(oc, max - min + 1, min);
4507             }
4508
4509             break;
4510         }
4511
4512         lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4513     }
4514
4515     return ocAllocateSymbolExtras(oc,0,0);
4516 }
4517
4518 #endif
4519 #ifdef x86_64_HOST_ARCH
4520 static int
4521 ocAllocateSymbolExtras_MachO(ObjectCode* oc)
4522 {
4523     struct mach_header *header = (struct mach_header *) oc->image;
4524     struct load_command *lc = (struct load_command *) (header + 1);
4525     unsigned i;
4526
4527     IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: start\n"));
4528
4529     for (i = 0; i < header->ncmds; i++) {   
4530         if (lc->cmd == LC_SYMTAB) {
4531
4532                 // Just allocate one entry for every symbol
4533             struct symtab_command *symLC = (struct symtab_command *) lc;
4534
4535             IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: allocate %d symbols\n", symLC->nsyms));
4536             IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: done\n"));
4537             return ocAllocateSymbolExtras(oc, symLC->nsyms, 0);
4538         }
4539
4540         lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
4541     }
4542
4543     IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: allocated no symbols\n"));
4544     IF_DEBUG(linker, debugBelch("ocAllocateSymbolExtras_MachO: done\n"));
4545     return ocAllocateSymbolExtras(oc,0,0);
4546 }
4547 #endif
4548
4549 static int
4550 ocVerifyImage_MachO(ObjectCode * oc)
4551 {
4552     char *image = (char*) oc->image;
4553     struct mach_header *header = (struct mach_header*) image;
4554
4555     IF_DEBUG(linker, debugBelch("ocVerifyImage_MachO: start\n"));
4556
4557 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
4558     if(header->magic != MH_MAGIC_64) {
4559         errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4560                    oc->fileName, MH_MAGIC_64, header->magic);
4561         return 0;
4562     }
4563 #else
4564     if(header->magic != MH_MAGIC) {
4565         errorBelch("%s: Bad magic. Expected: %08x, got: %08x.\n",
4566                    oc->fileName, MH_MAGIC, header->magic);
4567         return 0;
4568     }
4569 #endif
4570
4571     // FIXME: do some more verifying here
4572     IF_DEBUG(linker, debugBelch("ocVerifyImage_MachO: done\n"));
4573     return 1;
4574 }
4575
4576 static int
4577 resolveImports(
4578     ObjectCode* oc,
4579     char *image,
4580     struct symtab_command *symLC,
4581     struct section *sect,    // ptr to lazy or non-lazy symbol pointer section
4582     unsigned long *indirectSyms,
4583     struct nlist *nlist)
4584 {
4585     unsigned i;
4586     size_t itemSize = 4;
4587
4588     IF_DEBUG(linker, debugBelch("resolveImports: start\n"));
4589
4590 #if i386_HOST_ARCH
4591     int isJumpTable = 0;
4592
4593     if (strcmp(sect->sectname,"__jump_table") == 0) {
4594         isJumpTable = 1;
4595         itemSize = 5;
4596         ASSERT(sect->reserved2 == itemSize);
4597     }
4598
4599 #endif
4600
4601     for(i=0; i*itemSize < sect->size;i++)
4602     {
4603         // according to otool, reserved1 contains the first index into the indirect symbol table
4604         struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
4605         char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4606         void *addr = NULL;
4607
4608         IF_DEBUG(linker, debugBelch("resolveImports: resolving %s\n", nm));
4609
4610         if ((symbol->n_type & N_TYPE) == N_UNDF
4611             && (symbol->n_type & N_EXT) && (symbol->n_value != 0)) {
4612             addr = (void*) (symbol->n_value);
4613             IF_DEBUG(linker, debugBelch("resolveImports: undefined external %s has value %p\n", nm, addr));
4614         } else {
4615             addr = lookupSymbol(nm);
4616             IF_DEBUG(linker, debugBelch("resolveImports: looking up %s, %p\n", nm, addr));
4617         }
4618         if (!addr)
4619         {
4620             errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
4621             return 0;
4622         }
4623         ASSERT(addr);
4624
4625 #if i386_HOST_ARCH
4626         if (isJumpTable) {
4627             checkProddableBlock(oc,image + sect->offset + i*itemSize);
4628
4629             *(image + sect->offset + i * itemSize) = 0xe9; // jmp opcode
4630             *(unsigned*)(image + sect->offset + i*itemSize + 1)
4631                 = (char*)addr - (image + sect->offset + i*itemSize + 5);
4632         }
4633         else
4634 #endif
4635         {
4636             checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
4637             ((void**)(image + sect->offset))[i] = addr;
4638         }
4639     }
4640
4641     IF_DEBUG(linker, debugBelch("resolveImports: done\n"));
4642     return 1;
4643 }
4644
4645 static unsigned long relocateAddress(
4646     ObjectCode* oc,
4647     int nSections,
4648     struct section* sections,
4649     unsigned long address)
4650 {
4651     int i;
4652     IF_DEBUG(linker, debugBelch("relocateAddress: start\n"));
4653     for (i = 0; i < nSections; i++)
4654     {
4655             IF_DEBUG(linker, debugBelch("    relocating address in section %d\n", i));
4656         if (sections[i].addr <= address
4657             && address < sections[i].addr + sections[i].size)
4658         {
4659             return (unsigned long)oc->image
4660                     + sections[i].offset + address - sections[i].addr;
4661         }
4662     }
4663     barf("Invalid Mach-O file:"
4664          "Address out of bounds while relocating object file");
4665     return 0;
4666 }
4667
4668 static int relocateSection(
4669     ObjectCode* oc,
4670     char *image,
4671     struct symtab_command *symLC, struct nlist *nlist,
4672     int nSections, struct section* sections, struct section *sect)
4673 {
4674     struct relocation_info *relocs;
4675     int i, n;
4676
4677     IF_DEBUG(linker, debugBelch("relocateSection: start\n"));
4678
4679     if(!strcmp(sect->sectname,"__la_symbol_ptr"))
4680         return 1;
4681     else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
4682         return 1;
4683     else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
4684         return 1;
4685     else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
4686         return 1;
4687
4688     n = sect->nreloc;
4689     IF_DEBUG(linker, debugBelch("relocateSection: number of relocations: %d\n", n));
4690
4691     relocs = (struct relocation_info*) (image + sect->reloff);
4692
4693     for(i=0;i<n;i++)
4694     {
4695 #ifdef x86_64_HOST_ARCH
4696         struct relocation_info *reloc = &relocs[i];
4697
4698         char    *thingPtr = image + sect->offset + reloc->r_address;
4699         uint64_t thing;
4700         /* We shouldn't need to initialise this, but gcc on OS X 64 bit
4701            complains that it may be used uninitialized if we don't */
4702         uint64_t value = 0;
4703         uint64_t baseValue;
4704         int type = reloc->r_type;
4705
4706         checkProddableBlock(oc,thingPtr);
4707         switch(reloc->r_length)
4708         {
4709             case 0:
4710                 thing = *(uint8_t*)thingPtr;
4711                 baseValue = (uint64_t)thingPtr + 1;
4712                 break;
4713             case 1:
4714                 thing = *(uint16_t*)thingPtr;
4715                 baseValue = (uint64_t)thingPtr + 2;
4716                 break;
4717             case 2:
4718                 thing = *(uint32_t*)thingPtr;
4719                 baseValue = (uint64_t)thingPtr + 4;
4720                 break;
4721             case 3:
4722                 thing = *(uint64_t*)thingPtr;
4723                 baseValue = (uint64_t)thingPtr + 8;
4724                 break;
4725             default:
4726                 barf("Unknown size.");
4727         }
4728
4729         IF_DEBUG(linker,
4730                  debugBelch("relocateSection: length = %d, thing = %" PRId64 ", baseValue = %p\n",
4731                             reloc->r_length, thing, (char *)baseValue));
4732
4733         if (type == X86_64_RELOC_GOT
4734            || type == X86_64_RELOC_GOT_LOAD)
4735         {
4736             struct nlist *symbol = &nlist[reloc->r_symbolnum];
4737             char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4738
4739             IF_DEBUG(linker, debugBelch("relocateSection: making jump island for %s, extern = %d, X86_64_RELOC_GOT\n", nm, reloc->r_extern));
4740             ASSERT(reloc->r_extern);
4741             value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, (unsigned long)lookupSymbol(nm))->addr;
4742
4743             type = X86_64_RELOC_SIGNED;
4744         }
4745         else if(reloc->r_extern)
4746         {
4747             struct nlist *symbol = &nlist[reloc->r_symbolnum];
4748             char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4749
4750             IF_DEBUG(linker, debugBelch("relocateSection: looking up external symbol %s\n", nm));
4751             IF_DEBUG(linker, debugBelch("               : type  = %d\n", symbol->n_type));
4752             IF_DEBUG(linker, debugBelch("               : sect  = %d\n", symbol->n_sect));
4753             IF_DEBUG(linker, debugBelch("               : desc  = %d\n", symbol->n_desc));
4754             IF_DEBUG(linker, debugBelch("               : value = %p\n", (void *)symbol->n_value));
4755             if ((symbol->n_type & N_TYPE) == N_SECT) {
4756                 value = relocateAddress(oc, nSections, sections,
4757                                         symbol->n_value);
4758                 IF_DEBUG(linker, debugBelch("relocateSection, defined external symbol %s, relocated address %p\n", nm, (void *)value));
4759             }
4760             else {
4761                 value = (uint64_t) lookupSymbol(nm);
4762                 IF_DEBUG(linker, debugBelch("relocateSection: external symbol %s, address %p\n", nm, (void *)value));
4763             }
4764         }
4765         else
4766         {
4767             // If the relocation is not through the global offset table
4768             // or external, then set the value to the baseValue.  This
4769             // will leave displacements into the __const section
4770             // unchanged (as they ought to be).
4771
4772             value = baseValue;
4773         }
4774
4775         IF_DEBUG(linker, debugBelch("relocateSection: value = %p\n", (void *)value));
4776
4777         if (type == X86_64_RELOC_BRANCH)
4778         {
4779             if((int32_t)(value - baseValue) != (int64_t)(value - baseValue))
4780             {
4781                 ASSERT(reloc->r_extern);
4782                 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, value)
4783                                         -> jumpIsland;
4784             }
4785             ASSERT((int32_t)(value - baseValue) == (int64_t)(value - baseValue));
4786             type = X86_64_RELOC_SIGNED;
4787         }
4788
4789         switch(type)
4790         {
4791             case X86_64_RELOC_UNSIGNED:
4792                 ASSERT(!reloc->r_pcrel);
4793                 thing += value;
4794                 break;
4795             case X86_64_RELOC_SIGNED:
4796             case X86_64_RELOC_SIGNED_1:
4797             case X86_64_RELOC_SIGNED_2:
4798             case X86_64_RELOC_SIGNED_4:
4799                 ASSERT(reloc->r_pcrel);
4800                 thing += value - baseValue;
4801                 break;
4802             case X86_64_RELOC_SUBTRACTOR:
4803                 ASSERT(!reloc->r_pcrel);
4804                 thing -= value;
4805                 break;
4806             default:
4807                 barf("unkown relocation");
4808         }
4809
4810         switch(reloc->r_length)
4811         {
4812             case 0:
4813                 *(uint8_t*)thingPtr = thing;
4814                 break;
4815             case 1:
4816                 *(uint16_t*)thingPtr = thing;
4817                 break;
4818             case 2:
4819                 *(uint32_t*)thingPtr = thing;
4820                 break;
4821             case 3:
4822                 *(uint64_t*)thingPtr = thing;
4823                 break;
4824         }
4825 #else
4826         if(relocs[i].r_address & R_SCATTERED)
4827         {
4828             struct scattered_relocation_info *scat =
4829                 (struct scattered_relocation_info*) &relocs[i];
4830
4831             if(!scat->r_pcrel)
4832             {
4833                 if(scat->r_length == 2)
4834                 {
4835                     unsigned long word = 0;
4836                     unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
4837                     checkProddableBlock(oc,wordPtr);
4838
4839                     // Note on relocation types:
4840                     // i386 uses the GENERIC_RELOC_* types,
4841                     // while ppc uses special PPC_RELOC_* types.
4842                     // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
4843                     // in both cases, all others are different.
4844                     // Therefore, we use GENERIC_RELOC_VANILLA
4845                     // and GENERIC_RELOC_PAIR instead of the PPC variants,
4846                     // and use #ifdefs for the other types.
4847
4848                     // Step 1: Figure out what the relocated value should be
4849                     if (scat->r_type == GENERIC_RELOC_VANILLA) {
4850                         word = *wordPtr
4851                              + (unsigned long) relocateAddress(oc,
4852                                                                 nSections,
4853                                                                 sections,
4854                                                                 scat->r_value)
4855                                         - scat->r_value;
4856                     }
4857 #ifdef powerpc_HOST_ARCH
4858                     else if(scat->r_type == PPC_RELOC_SECTDIFF
4859                         || scat->r_type == PPC_RELOC_LO16_SECTDIFF
4860                         || scat->r_type == PPC_RELOC_HI16_SECTDIFF
4861                         || scat->r_type == PPC_RELOC_HA16_SECTDIFF
4862                         || scat->r_type == PPC_RELOC_LOCAL_SECTDIFF)
4863 #else
4864                     else if(scat->r_type == GENERIC_RELOC_SECTDIFF
4865                         || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4866 #endif
4867                     {
4868                         struct scattered_relocation_info *pair =
4869                                 (struct scattered_relocation_info*) &relocs[i+1];
4870
4871                         if (!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR) {
4872                             barf("Invalid Mach-O file: "
4873                                  "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
4874                         }
4875
4876                         word = (unsigned long)
4877                                (relocateAddress(oc, nSections, sections, scat->r_value)
4878                               - relocateAddress(oc, nSections, sections, pair->r_value));
4879                         i++;
4880                     }
4881 #ifdef powerpc_HOST_ARCH
4882                     else if(scat->r_type == PPC_RELOC_HI16
4883                          || scat->r_type == PPC_RELOC_LO16
4884                          || scat->r_type == PPC_RELOC_HA16
4885                          || scat->r_type == PPC_RELOC_LO14)
4886                     {   // these are generated by label+offset things
4887                         struct relocation_info *pair = &relocs[i+1];
4888
4889                         if ((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR) {
4890                             barf("Invalid Mach-O file: "
4891                                  "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
4892                         }
4893
4894                         if(scat->r_type == PPC_RELOC_LO16)
4895                         {
4896                             word = ((unsigned short*) wordPtr)[1];
4897                             word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4898                         }
4899                         else if(scat->r_type == PPC_RELOC_LO14)
4900                         {
4901                             barf("Unsupported Relocation: PPC_RELOC_LO14");
4902                             word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
4903                             word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4904                         }
4905                         else if(scat->r_type == PPC_RELOC_HI16)
4906                         {
4907                             word = ((unsigned short*) wordPtr)[1] << 16;
4908                             word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4909                         }
4910                         else if(scat->r_type == PPC_RELOC_HA16)
4911                         {
4912                             word = ((unsigned short*) wordPtr)[1] << 16;
4913                             word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4914                         }
4915
4916
4917                         word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
4918                                                 - scat->r_value;
4919
4920                         i++;
4921                     }
4922  #endif
4923                     else {
4924                         barf ("Don't know how to handle this Mach-O "
4925                               "scattered relocation entry: "
4926                               "object file %s; entry type %ld; "
4927                               "address %#lx\n",
4928                               OC_INFORMATIVE_FILENAME(oc),
4929                               scat->r_type,
4930                               scat->r_address);
4931                         return 0;
4932                      }
4933
4934 #ifdef powerpc_HOST_ARCH
4935                     if(scat->r_type == GENERIC_RELOC_VANILLA
4936                         || scat->r_type == PPC_RELOC_SECTDIFF)
4937 #else
4938                     if(scat->r_type == GENERIC_RELOC_VANILLA
4939                         || scat->r_type == GENERIC_RELOC_SECTDIFF
4940                         || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4941 #endif
4942                     {
4943                         *wordPtr = word;
4944                     }
4945 #ifdef powerpc_HOST_ARCH
4946                     else if (scat->r_type == PPC_RELOC_LO16_SECTDIFF
4947                           || scat->r_type == PPC_RELOC_LO16)
4948                     {
4949                         ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4950                     }
4951                     else if (scat->r_type == PPC_RELOC_HI16_SECTDIFF
4952                           || scat->r_type == PPC_RELOC_HI16)
4953                     {
4954                         ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4955                     }
4956                     else if (scat->r_type == PPC_RELOC_HA16_SECTDIFF
4957                           || scat->r_type == PPC_RELOC_HA16)
4958                     {
4959                         ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4960                             + ((word & (1<<15)) ? 1 : 0);
4961                     }
4962 #endif
4963                 }
4964                 else
4965                 {
4966                     barf("Can't handle Mach-O scattered relocation entry "
4967                          "with this r_length tag: "
4968                          "object file %s; entry type %ld; "
4969                          "r_length tag %ld; address %#lx\n",
4970                          OC_INFORMATIVE_FILENAME(oc),
4971                          scat->r_type,
4972                          scat->r_length,
4973                          scat->r_address);
4974                     return 0;
4975                 }
4976             }
4977             else /* scat->r_pcrel */
4978             {
4979                 barf("Don't know how to handle *PC-relative* Mach-O "
4980                      "scattered relocation entry: "
4981                      "object file %s; entry type %ld; address %#lx\n",
4982                      OC_INFORMATIVE_FILENAME(oc),
4983                      scat->r_type,
4984                      scat->r_address);
4985                return 0;
4986             }
4987
4988         }
4989         else /* !(relocs[i].r_address & R_SCATTERED) */
4990         {
4991             struct relocation_info *reloc = &relocs[i];
4992             if (reloc->r_pcrel && !reloc->r_extern) {
4993                 IF_DEBUG(linker, debugBelch("relocateSection: pc relative but not external, skipping\n"));
4994                 continue;
4995             }
4996
4997             if (reloc->r_length == 2) {
4998                 unsigned long word = 0;
4999 #ifdef powerpc_HOST_ARCH
5000                 unsigned long jumpIsland = 0;
5001                 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
5002                                                       // to avoid warning and to catch
5003                                                       // bugs.
5004 #endif
5005
5006                 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
5007                 checkProddableBlock(oc,wordPtr);
5008
5009                 if (reloc->r_type == GENERIC_RELOC_VANILLA) {
5010                     word = *wordPtr;
5011                 }
5012 #ifdef powerpc_HOST_ARCH
5013                 else if (reloc->r_type == PPC_RELOC_LO16) {
5014                     word = ((unsigned short*) wordPtr)[1];
5015                     word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
5016                 }
5017                 else if (reloc->r_type == PPC_RELOC_HI16) {
5018                     word = ((unsigned short*) wordPtr)[1] << 16;
5019                     word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
5020                 }
5021                 else if (reloc->r_type == PPC_RELOC_HA16) {
5022                     word = ((unsigned short*) wordPtr)[1] << 16;
5023                     word += ((short)relocs[i+1].r_address & (short)0xFFFF);
5024                 }
5025                 else if (reloc->r_type == PPC_RELOC_BR24) {
5026                     word = *wordPtr;
5027                     word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
5028                 }
5029 #endif
5030                 else {
5031                     barf("Can't handle this Mach-O relocation entry "
5032                          "(not scattered): "
5033                          "object file %s; entry type %ld; address %#lx\n",
5034                          OC_INFORMATIVE_FILENAME(oc),
5035                          reloc->r_type,
5036                          reloc->r_address);
5037                     return 0;
5038                 }
5039
5040                 if (!reloc->r_extern) {
5041                     long delta = sections[reloc->r_symbolnum-1].offset
5042                         - sections[reloc->r_symbolnum-1].addr
5043                         + ((long) image);
5044
5045                     word += delta;
5046                 }
5047                 else {
5048                     struct nlist *symbol = &nlist[reloc->r_symbolnum];
5049                     char *nm = image + symLC->stroff + symbol->n_un.n_strx;
5050                     void *symbolAddress = lookupSymbol(nm);
5051
5052                     if (!symbolAddress) {
5053                         errorBelch("\nunknown symbol `%s'", nm);
5054                         return 0;
5055                     }
5056
5057                     if (reloc->r_pcrel) {  
5058 #ifdef powerpc_HOST_ARCH
5059                             // In the .o file, this should be a relative jump to NULL
5060                             // and we'll change it to a relative jump to the symbol
5061                         ASSERT(word + reloc->r_address == 0);
5062                         jumpIsland = (unsigned long)
5063                                         &makeSymbolExtra(oc,
5064                                                          reloc->r_symbolnum,
5065                                                          (unsigned long) symbolAddress)
5066                                          -> jumpIsland;
5067                         if (jumpIsland != 0) {
5068                             offsetToJumpIsland = word + jumpIsland
5069                                 - (((long)image) + sect->offset - sect->addr);
5070                         }
5071 #endif
5072                         word += (unsigned long) symbolAddress
5073                                 - (((long)image) + sect->offset - sect->addr);
5074                     }
5075                     else {
5076                         word += (unsigned long) symbolAddress;
5077                     }
5078                 }
5079
5080                 if (reloc->r_type == GENERIC_RELOC_VANILLA) {
5081                     *wordPtr = word;
5082                     continue;
5083                 }
5084 #ifdef powerpc_HOST_ARCH
5085                 else if(reloc->r_type == PPC_RELOC_LO16)
5086                 {
5087                     ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
5088                     i++;
5089                     continue;
5090                 }
5091                 else if(reloc->r_type == PPC_RELOC_HI16)
5092                 {
5093                     ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
5094                     i++;
5095                     continue;
5096                 }
5097                 else if(reloc->r_type == PPC_RELOC_HA16)
5098                 {
5099                     ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
5100                         + ((word & (1<<15)) ? 1 : 0);
5101                     i++;
5102                     continue;
5103                 }
5104                 else if(reloc->r_type == PPC_RELOC_BR24)
5105                 {
5106                     if ((word & 0x03) != 0) {
5107                         barf("%s: unconditional relative branch with a displacement "
5108                              "which isn't a multiple of 4 bytes: %#lx",
5109                              OC_INFORMATIVE_FILENAME(oc),
5110                              word);
5111                     }
5112
5113                     if((word & 0xFE000000) != 0xFE000000 &&
5114                         (word & 0xFE000000) != 0x00000000) {
5115                         // The branch offset is too large.
5116                         // Therefore, we try to use a jump island.
5117                         if (jumpIsland == 0) {
5118                             barf("%s: unconditional relative branch out of range: "
5119                                  "no jump island available: %#lx",
5120                                  OC_INFORMATIVE_FILENAME(oc),
5121                                  word);
5122                         }
5123
5124                         word = offsetToJumpIsland;
5125
5126                         if((word & 0xFE000000) != 0xFE000000 &&
5127                             (word & 0xFE000000) != 0x00000000) {
5128                             barf("%s: unconditional relative branch out of range: "
5129                                  "jump island out of range: %#lx",
5130                                  OC_INFORMATIVE_FILENAME(oc),
5131                                  word);
5132                     }
5133                     }
5134                     *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
5135                     continue;
5136                 }
5137 #endif
5138             }
5139             else
5140             {
5141                  barf("Can't handle Mach-O relocation entry (not scattered) "
5142                       "with this r_length tag: "
5143                       "object file %s; entry type %ld; "
5144                       "r_length tag %ld; address %#lx\n",
5145                       OC_INFORMATIVE_FILENAME(oc),
5146                       reloc->r_type,
5147                       reloc->r_length,
5148                       reloc->r_address);
5149                  return 0;
5150             }
5151         }
5152 #endif
5153     }
5154
5155     IF_DEBUG(linker, debugBelch("relocateSection: done\n"));
5156     return 1;
5157 }
5158
5159 static int
5160 ocGetNames_MachO(ObjectCode* oc)
5161 {
5162     char *image = (char*) oc->image;
5163     struct mach_header *header = (struct mach_header*) image;
5164     struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5165     unsigned i,curSymbol = 0;
5166     struct segment_command *segLC = NULL;
5167     struct section *sections;
5168     struct symtab_command *symLC = NULL;
5169     struct nlist *nlist;
5170     unsigned long commonSize = 0;
5171     char    *commonStorage = NULL;
5172     unsigned long commonCounter;
5173
5174     IF_DEBUG(linker,debugBelch("ocGetNames_MachO: start\n"));
5175
5176     for(i=0;i<header->ncmds;i++)
5177     {
5178         if (lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64) {
5179             segLC = (struct segment_command*) lc;
5180         }
5181         else if (lc->cmd == LC_SYMTAB) {
5182             symLC = (struct symtab_command*) lc;
5183         }
5184
5185         lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5186     }
5187
5188     sections = (struct section*) (segLC+1);
5189     nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5190                   : NULL;
5191
5192     if (!segLC) {
5193         barf("ocGetNames_MachO: no segment load command");
5194     }
5195
5196     IF_DEBUG(linker, debugBelch("ocGetNames_MachO: will load %d sections\n", segLC->nsects));
5197     for(i=0;i<segLC->nsects;i++)
5198     {
5199         IF_DEBUG(linker, debugBelch("ocGetNames_MachO: section %d\n", i));
5200
5201         if (sections[i].size == 0) {
5202             IF_DEBUG(linker, debugBelch("ocGetNames_MachO: found a zero length section, skipping\n"));
5203             continue;
5204         }
5205
5206         if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
5207         {
5208             char * zeroFillArea = stgCallocBytes(1,sections[i].size,
5209                                       "ocGetNames_MachO(common symbols)");
5210             sections[i].offset = zeroFillArea - image;
5211         }
5212
5213         if (!strcmp(sections[i].sectname,"__text")) {
5214
5215             IF_DEBUG(linker, debugBelch("ocGetNames_MachO: adding __text section\n"));
5216             addSection(oc, SECTIONKIND_CODE_OR_RODATA,
5217                 (void*) (image + sections[i].offset),
5218                 (void*) (image + sections[i].offset + sections[i].size));
5219         }
5220         else if (!strcmp(sections[i].sectname,"__const")) {
5221
5222             IF_DEBUG(linker, debugBelch("ocGetNames_MachO: adding __const section\n"));
5223             addSection(oc, SECTIONKIND_RWDATA,
5224                 (void*) (image + sections[i].offset),
5225                 (void*) (image + sections[i].offset + sections[i].size));
5226         }    
5227         else if (!strcmp(sections[i].sectname,"__data")) {
5228
5229             IF_DEBUG(linker, debugBelch("ocGetNames_MachO: adding __data section\n"));
5230             addSection(oc, SECTIONKIND_RWDATA,
5231                 (void*) (image + sections[i].offset),
5232                 (void*) (image + sections[i].offset + sections[i].size));
5233         }
5234         else if(!strcmp(sections[i].sectname,"__bss")
5235                 || !strcmp(sections[i].sectname,"__common")) {
5236
5237             IF_DEBUG(linker, debugBelch("ocGetNames_MachO: adding __bss section\n"));
5238             addSection(oc, SECTIONKIND_RWDATA,
5239                 (void*) (image + sections[i].offset),
5240                 (void*) (image + sections[i].offset + sections[i].size));
5241         }
5242         addProddableBlock(oc,
5243                           (void *) (image + sections[i].offset),
5244                                         sections[i].size);
5245     }
5246
5247         // count external symbols defined here
5248     oc->n_symbols = 0;
5249     if (symLC) {
5250         for (i = 0; i < symLC->nsyms; i++) {
5251             if (nlist[i].n_type & N_STAB) {
5252                 ;
5253             }
5254             else if(nlist[i].n_type & N_EXT)
5255             {
5256                 if((nlist[i].n_type & N_TYPE) == N_UNDF
5257                     && (nlist[i].n_value != 0))
5258                 {
5259                     commonSize += nlist[i].n_value;
5260                     oc->n_symbols++;
5261                 }
5262                 else if((nlist[i].n_type & N_TYPE) == N_SECT)
5263                     oc->n_symbols++;
5264             }
5265         }
5266     }
5267     IF_DEBUG(linker, debugBelch("ocGetNames_MachO: %d external symbols\n", oc->n_symbols));
5268     oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
5269                                    "ocGetNames_MachO(oc->symbols)");
5270
5271     if(symLC)
5272     {
5273         for(i=0;i<symLC->nsyms;i++)
5274         {
5275             if(nlist[i].n_type & N_STAB)
5276                 ;
5277             else if((nlist[i].n_type & N_TYPE) == N_SECT)
5278             {
5279                 if(nlist[i].n_type & N_EXT)
5280                 {
5281                     char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5282                     if ((nlist[i].n_desc & N_WEAK_DEF) && lookupSymbol(nm)) {
5283                         // weak definition, and we already have a definition
5284                         IF_DEBUG(linker, debugBelch("    weak: %s\n", nm));
5285                     }
5286                     else
5287                     {
5288                             IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting %s\n", nm));
5289                             ghciInsertStrHashTable(oc->fileName, symhash, nm,
5290                                                     image
5291                                                     + sections[nlist[i].n_sect-1].offset
5292                                                     - sections[nlist[i].n_sect-1].addr
5293                                                     + nlist[i].n_value);
5294                             oc->symbols[curSymbol++] = nm;
5295                     }
5296                 }
5297                 else
5298                 {
5299                     IF_DEBUG(linker, debugBelch("ocGetNames_MachO: \t...not external, skipping\n"));
5300                 }
5301             }
5302             else
5303             {
5304                 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: \t...not defined in this section, skipping\n"));
5305             }
5306         }
5307     }
5308
5309     commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
5310     commonCounter = (unsigned long)commonStorage;
5311
5312     if (symLC) {
5313         for (i = 0; i < symLC->nsyms; i++) {
5314             if((nlist[i].n_type & N_TYPE) == N_UNDF
5315              && (nlist[i].n_type & N_EXT)
5316              && (nlist[i].n_value != 0)) {
5317
5318                 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
5319                 unsigned long sz = nlist[i].n_value;
5320
5321                 nlist[i].n_value = commonCounter;
5322
5323                 IF_DEBUG(linker, debugBelch("ocGetNames_MachO: inserting common symbol: %s\n", nm));
5324                 ghciInsertStrHashTable(oc->fileName, symhash, nm,
5325                                        (void*)commonCounter);
5326                 oc->symbols[curSymbol++] = nm;
5327
5328                 commonCounter += sz;
5329             }
5330         }
5331     }
5332
5333     IF_DEBUG(linker, debugBelch("ocGetNames_MachO: done\n"));
5334     return 1;
5335 }
5336
5337 static int
5338 ocResolve_MachO(ObjectCode* oc)
5339 {
5340     char *image = (char*) oc->image;
5341     struct mach_header *header = (struct mach_header*) image;
5342     struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
5343     unsigned i;
5344     struct segment_command *segLC = NULL;
5345     struct section *sections;
5346     struct symtab_command *symLC = NULL;
5347     struct dysymtab_command *dsymLC = NULL;
5348     struct nlist *nlist;
5349
5350     IF_DEBUG(linker, debugBelch("ocResolve_MachO: start\n"));
5351     for (i = 0; i < header->ncmds; i++)
5352     {
5353         if (lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64) {
5354             segLC = (struct segment_command*) lc;
5355             IF_DEBUG(linker, debugBelch("ocResolve_MachO: found a 32 or 64 bit segment load command\n"));
5356         }
5357         else if (lc->cmd == LC_SYMTAB) {
5358             symLC = (struct symtab_command*) lc;
5359             IF_DEBUG(linker, debugBelch("ocResolve_MachO: found a symbol table load command\n"));
5360         }
5361         else if (lc->cmd == LC_DYSYMTAB) {
5362             dsymLC = (struct dysymtab_command*) lc;
5363             IF_DEBUG(linker, debugBelch("ocResolve_MachO: found a dynamic symbol table load command\n"));
5364         }
5365
5366         lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
5367     }
5368
5369     sections = (struct section*) (segLC+1);
5370     nlist = symLC ? (struct nlist*) (image + symLC->symoff)
5371                   : NULL;
5372
5373     if(dsymLC)
5374     {
5375         unsigned long *indirectSyms
5376             = (unsigned long*) (image + dsymLC->indirectsymoff);
5377
5378         IF_DEBUG(linker, debugBelch("ocResolve_MachO: resolving dsymLC\n"));
5379         for (i = 0; i < segLC->nsects; i++)
5380         {
5381             if(    !strcmp(sections[i].sectname,"__la_symbol_ptr")
5382                 || !strcmp(sections[i].sectname,"__la_sym_ptr2")
5383                 || !strcmp(sections[i].sectname,"__la_sym_ptr3"))
5384             {
5385                 if(!resolveImports(oc,image,symLC,&sections[i],indirectSyms,nlist))
5386                     return 0;
5387             }
5388             else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr")
5389                 ||  !strcmp(sections[i].sectname,"__pointers"))
5390             {
5391                 if(!resolveImports(oc,image,symLC,&sections[i],indirectSyms,nlist))
5392                     return 0;
5393             }
5394             else if(!strcmp(sections[i].sectname,"__jump_table"))
5395             {
5396                 if(!resolveImports(oc,image,symLC,&sections[i],indirectSyms,nlist))
5397                     return 0;
5398             }
5399             else
5400             {
5401                 IF_DEBUG(linker, debugBelch("ocResolve_MachO: unknown section\n"));
5402             }
5403         }
5404     }
5405
5406     for(i=0;i<segLC->nsects;i++)
5407     {
5408             IF_DEBUG(linker, debugBelch("ocResolve_MachO: relocating section %d\n", i));
5409
5410         if (!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,&sections[i]))
5411             return 0;
5412     }
5413
5414 #if defined (powerpc_HOST_ARCH)
5415     ocFlushInstructionCache( oc );
5416 #endif
5417
5418     return 1;
5419 }
5420
5421 #ifdef powerpc_HOST_ARCH
5422 /*
5423  * The Mach-O object format uses leading underscores. But not everywhere.
5424  * There is a small number of runtime support functions defined in
5425  * libcc_dynamic.a whose name does not have a leading underscore.
5426  * As a consequence, we can't get their address from C code.
5427  * We have to use inline assembler just to take the address of a function.
5428  * Yuck.
5429  */
5430
5431 extern void* symbolsWithoutUnderscore[];
5432
5433 static void
5434 machoInitSymbolsWithoutUnderscore(void)
5435 {
5436     void **p = symbolsWithoutUnderscore;
5437     __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
5438
5439 #undef SymI_NeedsProto
5440 #define SymI_NeedsProto(x)  \
5441     __asm__ volatile(".long " # x);
5442
5443     RTS_MACHO_NOUNDERLINE_SYMBOLS
5444
5445     __asm__ volatile(".text");
5446
5447 #undef SymI_NeedsProto
5448 #define SymI_NeedsProto(x)  \
5449     ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
5450
5451     RTS_MACHO_NOUNDERLINE_SYMBOLS
5452
5453 #undef SymI_NeedsProto
5454 }
5455 #endif
5456
5457 #ifndef USE_MMAP
5458 /*
5459  * Figure out by how much to shift the entire Mach-O file in memory
5460  * when loading so that its single segment ends up 16-byte-aligned
5461  */
5462 static int
5463 machoGetMisalignment( FILE * f )
5464 {
5465     struct mach_header header;
5466     int misalignment;
5467
5468     {
5469         int n = fread(&header, sizeof(header), 1, f);
5470         if (n != 1) {
5471             barf("machoGetMisalignment: can't read the Mach-O header");
5472         }
5473     }
5474     fseek(f, -sizeof(header), SEEK_CUR);
5475
5476 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
5477     if(header.magic != MH_MAGIC_64) {
5478         barf("Bad magic. Expected: %08x, got: %08x.",
5479              MH_MAGIC_64, header.magic);
5480     }
5481 #else
5482     if(header.magic != MH_MAGIC) {
5483         barf("Bad magic. Expected: %08x, got: %08x.",
5484              MH_MAGIC, header.magic);
5485     }
5486 #endif
5487
5488     misalignment = (header.sizeofcmds + sizeof(header))
5489                     & 0xF;
5490
5491     return misalignment ? (16 - misalignment) : 0;
5492 }
5493 #endif
5494
5495 #endif