1/*
2  13 Dec '05
3  Linker no longer used - apart from mymalloc().
4  Instead, simply compile and link switchback.c with test_xxx.c, e.g.:
5  ./> (cd .. && make EXTRA_CFLAGS="-m64" libvex_ppc64_linux.a) && gcc -m64 -Wall -O -g -o switchback switchback.c linker.c ../libvex_ppc64_linux.a test_bzip2.c
6*/
7
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <assert.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <unistd.h>
15#include <elf.h>
16#include <fcntl.h>
17#include <string.h>
18//#include <malloc.h>
19
20#include "linker.h"
21
22#include "../pub/libvex_basictypes.h"
23
24#if 0
25#define IF_DEBUG(x,y) /* */
26static int debug_linker = 0;
27#endif
28
29
30#if defined(__x86_64__)
31#   define x86_64_TARGET_ARCH
32#elif defined(__i386__)
33#   define i386_TARGET_ARCH
34#elif defined (__powerpc__)
35#   define ppc32_TARGET_ARCH
36#elif defined(__aarch64__)
37#   define arm64_TARGET_ARCH
38#else
39#   error "Unknown arch"
40#endif
41
42
43#if 0
44#define CALLOC_MAX 10000000
45static HChar calloc_area[CALLOC_MAX];
46static UInt calloc_used = 0;
47static void* calloc_below2G ( Int n, Int m )
48{
49   void* p;
50   int i;
51   while ((calloc_used % 16) > 0) calloc_used++;
52   assert(calloc_used + n*m < CALLOC_MAX);
53   p = &calloc_area[calloc_used];
54   for (i = 0; i < n*m; i++)
55     calloc_area[calloc_used+i] = 0;
56   calloc_used += n*m;
57   return p;
58}
59#endif
60
61#define MYMALLOC_MAX 50*1000*1000
62static HChar mymalloc_area[MYMALLOC_MAX];
63static UInt  mymalloc_used = 0;
64void* mymalloc ( Int n )
65{
66   void* p;
67#if defined(__powerpc64__) || defined(__aarch64__)
68   while ((ULong)(mymalloc_area+mymalloc_used) & 0xFFF)
69#else
70   while ((UInt)(mymalloc_area+mymalloc_used) & 0xFFF)
71#endif
72      mymalloc_used++;
73   assert(mymalloc_used+n < MYMALLOC_MAX);
74   p = (void*)(&mymalloc_area[mymalloc_used]);
75   mymalloc_used += n;
76   //   printf("mymalloc(%d) = %p\n", n, p);
77   return p;
78}
79
80void myfree ( void* p )
81{
82}
83
84
85
86
87
88
89
90#if 0
91///////////////////////////////////////////////////////////////////
92///////////////////////////////////////////////////////////////////
93///////////////////////////////////////////////////////////////////
94//
95// TYPES
96
97#define FALSE 0
98#define TRUE  1
99
100typedef enum { OBJECT_LOADED, OBJECT_RESOLVED } OStatus;
101
102
103#define N_FIXUP_PAGES 1
104
105
106/* Indication of section kinds for loaded objects.  Needed by
107   the GC for deciding whether or not a pointer on the stack
108   is a code pointer.
109*/
110typedef
111   enum { SECTIONKIND_CODE_OR_RODATA,
112          SECTIONKIND_RWDATA,
113          SECTIONKIND_OTHER,
114          SECTIONKIND_NOINFOAVAIL }
115   SectionKind;
116
117typedef
118   struct _Section {
119      void* start;
120      void* end;
121      SectionKind kind;
122      struct _Section* next;
123   }
124   Section;
125
126typedef
127   struct _ProddableBlock {
128      void* start;
129      int   size;
130      struct _ProddableBlock* next;
131   }
132   ProddableBlock;
133
134/* Top-level structure for an object module.  One of these is allocated
135 * for each object file in use.
136 */
137typedef struct _ObjectCode {
138    OStatus    status;
139    char*      fileName;
140    int        fileSize;
141    char*      formatName;            /* eg "ELF32", "DLL", "COFF", etc. */
142
143    /* An array containing ptrs to all the symbol names copied from
144       this object into the global symbol hash table.  This is so that
145       we know which parts of the latter mapping to nuke when this
146       object is removed from the system. */
147    char**     symbols;
148    int        n_symbols;
149
150    /* ptr to malloc'd lump of memory holding the obj file */
151    void*      image;
152
153    /* Fixup area for long-distance jumps. */
154    char*      fixup;
155    int        fixup_used;
156    int        fixup_size;
157
158    /* The section-kind entries for this object module.  Linked
159       list. */
160    Section* sections;
161
162    /* A private hash table for local symbols. */
163    /* HashTable* */ void* lochash;
164
165    /* Allow a chain of these things */
166    struct _ObjectCode * next;
167
168    /* SANITY CHECK ONLY: a list of the only memory regions which may
169       safely be prodded during relocation.  Any attempt to prod
170       outside one of these is an error in the linker. */
171    ProddableBlock* proddables;
172
173} ObjectCode;
174
175/*
176 * Define a set of types which can be used for both ELF32 and ELF64
177 */
178
179#if VEX_HOST_WORDSIZE == 8
180#define ELFCLASS    ELFCLASS64
181#define Elf_Addr    Elf64_Addr
182#define Elf_Word    Elf64_Word
183#define Elf_Sword   Elf64_Sword
184#define Elf_Ehdr    Elf64_Ehdr
185#define Elf_Phdr    Elf64_Phdr
186#define Elf_Shdr    Elf64_Shdr
187#define Elf_Sym     Elf64_Sym
188#define Elf_Rel     Elf64_Rel
189#define Elf_Rela    Elf64_Rela
190#define ELF_ST_TYPE ELF64_ST_TYPE
191#define ELF_ST_BIND ELF64_ST_BIND
192#define ELF_R_TYPE  ELF64_R_TYPE
193#define ELF_R_SYM   ELF64_R_SYM
194#else
195#define ELFCLASS    ELFCLASS32
196#define Elf_Addr    Elf32_Addr
197#define Elf_Word    Elf32_Word
198#define Elf_Sword   Elf32_Sword
199#define Elf_Ehdr    Elf32_Ehdr
200#define Elf_Phdr    Elf32_Phdr
201#define Elf_Shdr    Elf32_Shdr
202#define Elf_Sym     Elf32_Sym
203#define Elf_Rel     Elf32_Rel
204#define Elf_Rela    Elf32_Rela
205#ifndef ELF_ST_TYPE
206#define ELF_ST_TYPE ELF32_ST_TYPE
207#endif
208#ifndef ELF_ST_BIND
209#define ELF_ST_BIND ELF32_ST_BIND
210#endif
211#ifndef ELF_R_TYPE
212#define ELF_R_TYPE  ELF32_R_TYPE
213#endif
214#ifndef ELF_R_SYM
215#define ELF_R_SYM   ELF32_R_SYM
216#endif
217#endif
218
219
220
221
222///////////////////////////////////////////////////////////////////
223///////////////////////////////////////////////////////////////////
224///////////////////////////////////////////////////////////////////
225//
226// PARANOIA
227
228/* -----------------------------------------------------------------------
229 * Sanity checking.  For each ObjectCode, maintain a list of address ranges
230 * which may be prodded during relocation, and abort if we try and write
231 * outside any of these.
232 */
233static void addProddableBlock ( ObjectCode* oc, void* start, int size )
234{
235   ProddableBlock* pb
236      = mymalloc(sizeof(ProddableBlock));
237   if (debug_linker)
238      fprintf(stderr, "aPB oc=%p %p %d   (%p .. %p)\n", oc, start, size,
239	      start, ((char*)start)+size-1 );
240   assert(size > 0);
241   pb->start      = start;
242   pb->size       = size;
243   pb->next       = oc->proddables;
244   oc->proddables = pb;
245}
246
247static void checkProddableBlock ( ObjectCode* oc, void* addr )
248{
249   ProddableBlock* pb;
250   for (pb = oc->proddables; pb != NULL; pb = pb->next) {
251      char* s = (char*)(pb->start);
252      char* e = s + pb->size - 1;
253      char* a = (char*)addr;
254      /* Assumes that the biggest fixup involves a 4-byte write.  This
255         probably needs to be changed to 8 (ie, +7) on 64-bit
256         plats. */
257      if (a >= s && (a+3) <= e) return;
258   }
259   fprintf(stderr,
260           "checkProddableBlock: invalid fixup %p in runtime linker\n",
261           addr);
262   exit(1);
263}
264
265
266
267///////////////////////////////////////////////////////////////////
268///////////////////////////////////////////////////////////////////
269///////////////////////////////////////////////////////////////////
270//
271// String->Addr mappings
272
273typedef
274   struct { char* mp_name; void* mp_addr; }
275   Maplet;
276
277typedef
278   struct {
279      int sm_size;
280      int sm_used;
281      Maplet* maplets;
282   }
283   StringMap;
284
285static StringMap* new_StringMap ( void )
286{
287   StringMap* sm = mymalloc(sizeof(StringMap));
288   sm->sm_size = 10;
289   sm->sm_used = 0;
290   sm->maplets = mymalloc(10 * sizeof(Maplet));
291   return sm;
292}
293
294static void delete_StringMap ( StringMap* sm )
295{
296   assert(sm->maplets != NULL);
297   myfree(sm->maplets);
298   sm->maplets = NULL;
299   myfree(sm);
300}
301
302static void ensure_StringMap ( StringMap* sm )
303{
304   int i;
305   Maplet* mp2;
306   assert(sm->maplets != NULL);
307   if (sm->sm_used < sm->sm_size)
308     return;
309   sm->sm_size *= 2;
310   mp2 = mymalloc(sm->sm_size * sizeof(Maplet));
311   for (i = 0; i < sm->sm_used; i++)
312      mp2[i] = sm->maplets[i];
313   myfree(sm->maplets);
314   sm->maplets = mp2;
315}
316
317static void* search_StringMap ( StringMap* sm, char* name )
318{
319   int i;
320   for (i = 0; i < sm->sm_used; i++)
321      if (0 == strcmp(name, sm->maplets[i].mp_name))
322         return sm->maplets[i].mp_addr;
323   return NULL;
324}
325
326static void addto_StringMap ( StringMap* sm, char* name, void* addr )
327{
328   ensure_StringMap(sm);
329   sm->maplets[sm->sm_used].mp_name = name;
330   sm->maplets[sm->sm_used].mp_addr = addr;
331   sm->sm_used++;
332}
333
334static void paranoid_addto_StringMap ( StringMap* sm, char* name, void* addr )
335{
336   if (0)
337       fprintf(stderr, "paranoid_addto_StringMap(%s,%p)\n", name, addr);
338   if (search_StringMap(sm,name) != NULL) {
339      fprintf(stderr, "duplicate: paranoid_addto_StringMap(%s,%p)\n", name, addr);
340      exit(1);
341   }
342   addto_StringMap(sm,name,addr);
343}
344
345
346///////////////////////////////////////////////////////////////////
347///////////////////////////////////////////////////////////////////
348///////////////////////////////////////////////////////////////////
349//
350// Top-level linker control.
351
352StringMap*  global_symbol_table = NULL;
353ObjectCode* global_object_list = NULL;
354
355static void initLinker ( void )
356{
357   if (global_symbol_table != NULL)
358      return;
359   global_symbol_table = new_StringMap();
360}
361
362
363
364///////////////////////////////////////////////////////////////////
365///////////////////////////////////////////////////////////////////
366///////////////////////////////////////////////////////////////////
367//
368// SYMBOL TABLE(s)
369
370/* -----------------------------------------------------------------
371 * lookup a symbol in the global symbol table
372 */
373static
374void * lookupSymbol( char *lbl )
375{
376   void *val;
377   initLinker() ;
378   assert(global_symbol_table != NULL);
379   val = search_StringMap(global_symbol_table, lbl);
380   return val;
381}
382
383
384///////////////////////////////////////////////////////////////////
385///////////////////////////////////////////////////////////////////
386///////////////////////////////////////////////////////////////////
387//
388// HELPERS
389
390/*
391 * Generic ELF functions
392 */
393
394static char *
395findElfSection ( void* objImage, Elf_Word sh_type )
396{
397   char* ehdrC = (char*)objImage;
398   Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
399   Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
400   char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
401   char* ptr = NULL;
402   int i;
403
404   for (i = 0; i < ehdr->e_shnum; i++) {
405      if (shdr[i].sh_type == sh_type
406          /* Ignore the section header's string table. */
407          && i != ehdr->e_shstrndx
408	  /* Ignore string tables named .stabstr, as they contain
409             debugging info. */
410          && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
411         ) {
412         ptr = ehdrC + shdr[i].sh_offset;
413         break;
414      }
415   }
416   return ptr;
417}
418
419#ifdef arm_TARGET_ARCH
420static
421char* alloc_fixup_bytes ( ObjectCode* oc, int nbytes )
422{
423   char* res;
424   assert(nbytes % 4 == 0);
425   assert(nbytes > 0);
426   res = &(oc->fixup[oc->fixup_used]);
427   oc->fixup_used += nbytes;
428   if (oc->fixup_used >= oc->fixup_size) {
429     fprintf(stderr, "fixup area too small for %s\n", oc->fileName);
430     exit(1);
431   }
432   return res;
433}
434#endif
435
436
437///////////////////////////////////////////////////////////////////
438///////////////////////////////////////////////////////////////////
439///////////////////////////////////////////////////////////////////
440//
441// RESOLVE
442
443static
444void* lookup_magic_hacks ( char* sym )
445{
446   if (0==strcmp(sym, "printf")) return (void*)(&printf);
447   return NULL;
448}
449
450#ifdef arm_TARGET_ARCH
451static
452void arm_notify_new_code ( char* start, int length )
453{
454  __asm __volatile ("mov r1, %0\n\t"
455                    "mov r2, %1\n\t"
456                    "mov r3, %2\n\t"
457                    "swi 0x9f0002\n\t"
458                    :
459                    : "ir" (start), "ir" (length), "ir" (0) );
460}
461
462
463static
464void gen_armle_goto ( char* fixup, char* dstP )
465{
466  Elf_Word w = (Elf_Word)dstP;
467  /*
468   2                    .text
469   3 0000 04F01FE5              ldr     pc, value
470   4 0004 44332211      value:  .word   0x11223344
471   */
472  fprintf(stderr,"at %p generating jump to %p\n", fixup, dstP );
473  fixup[0] = 0x04; fixup[1] = 0xF0; fixup[2] = 0x1F; fixup[3] = 0xE5;
474  fixup[4] = w & 0xFF; w >>= 8;
475  fixup[5] = w & 0xFF; w >>= 8;
476  fixup[6] = w & 0xFF; w >>= 8;
477  fixup[7] = w & 0xFF; w >>= 8;
478  arm_notify_new_code(fixup, 8);
479}
480#endif /* arm_TARGET_ARCH */
481
482
483#ifdef ppc32_TARGET_ARCH
484static void invalidate_icache(void *ptr, int nbytes)
485{
486   unsigned long startaddr = (unsigned long) ptr;
487   unsigned long endaddr = startaddr + nbytes;
488   unsigned long addr;
489   unsigned long cls = 16; //VG_(cache_line_size);
490
491   startaddr &= ~(cls - 1);
492   for (addr = startaddr; addr < endaddr; addr += cls)
493      asm volatile("dcbst 0,%0" : : "r" (addr));
494   asm volatile("sync");
495   for (addr = startaddr; addr < endaddr; addr += cls)
496      asm volatile("icbi 0,%0" : : "r" (addr));
497   asm volatile("sync; isync");
498}
499
500static UInt compute_ppc_HA ( UInt x ) {
501   return 0xFFFF & ( (x >> 16) + ((x & 0x8000) ? 1 : 0) );
502}
503static UInt compute_ppc_LO ( UInt x ) {
504   return 0xFFFF & x;
505}
506static UInt compute_ppc_HI ( UInt x ) {
507   return 0xFFFF & (x >> 16);
508}
509#endif /* ppc32_TARGET_ARCH */
510
511
512/* Do ELF relocations which lack an explicit addend.  All x86-linux
513   relocations appear to be of this form. */
514static int
515do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
516                         Elf_Shdr* shdr, int shnum,
517                         Elf_Sym*  stab, char* strtab )
518{
519   int j;
520   char *symbol = NULL;
521   Elf_Word* targ;
522   Elf_Rel*  rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
523   int         nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
524   int target_shndx = shdr[shnum].sh_info;
525   int symtab_shndx = shdr[shnum].sh_link;
526
527   stab  = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
528   targ  = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
529   IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
530                          target_shndx, symtab_shndx ));
531
532   for (j = 0; j < nent; j++) {
533      Elf_Addr offset = rtab[j].r_offset;
534      Elf_Addr info   = rtab[j].r_info;
535
536      Elf_Addr  P  = ((Elf_Addr)targ) + offset;
537      Elf_Word* pP = (Elf_Word*)P;
538      Elf_Addr  A  = *pP;
539      Elf_Addr  S;
540      Elf_Addr  value;
541
542      IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
543                             j, (void*)offset, (void*)info ));
544      if (!info) {
545         IF_DEBUG(linker,belch( " ZERO" ));
546         S = 0;
547      } else {
548         Elf_Sym sym = stab[ELF_R_SYM(info)];
549	 /* First see if it is a local symbol. */
550         if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
551            /* Yes, so we can get the address directly from the ELF symbol
552               table. */
553            symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
554            S = (Elf_Addr)
555                (ehdrC + shdr[ sym.st_shndx ].sh_offset
556                       + stab[ELF_R_SYM(info)].st_value);
557
558	 } else {
559            /* No, so look up the name in our global table. */
560            symbol = strtab + sym.st_name;
561            S = (Elf_Addr)lookupSymbol( symbol );
562	 }
563         if (!S) {
564            S = (Elf_Addr)lookup_magic_hacks(symbol);
565         }
566         if (!S) {
567            fprintf(stderr,"%s: unknown symbol `%s'\n",
568                           oc->fileName, symbol);
569	    return 0;
570         }
571         if (debug_linker>1)
572            fprintf(stderr, "\n`%s' resolves to %p\n", symbol, (void*)S );
573      }
574
575      if (debug_linker>1)
576         fprintf(stderr, "Reloc: P = %p   S = %p   A = %p\n",
577			     (void*)P, (void*)S, (void*)A );
578      checkProddableBlock ( oc, pP );
579
580      value = S + A;
581
582      switch (ELF_R_TYPE(info)) {
583#        ifdef i386_TARGET_ARCH
584         case R_386_32:   *pP = value;     break;
585         case R_386_PC32: *pP = value - P; break;
586#        endif
587#        ifdef arm_TARGET_ARCH
588         case R_ARM_PC24: {
589	    Elf_Word w, delta, deltaTop8;
590	    /* Generate a jump sequence into the fixup area
591	       and branch to that instead. */
592 	    char* fixup = alloc_fixup_bytes(oc, 8);
593            /* First of all, figure out where we're really trying to
594               jump to. */
595            // compensate for pc+8 bias
596            Elf_Word real_dst = (A & 0x00FFFFFF) + 2;
597	    // sign-extend 24-to-32 of real_dst
598            if (real_dst & 0x00800000)
599               real_dst |= 0xFF000000;
600            else
601               real_dst &= 0x00FFFFFF;
602
603            real_dst <<= 2;
604	    real_dst += S;
605
606	    gen_armle_goto(fixup, (char*)real_dst);
607
608	    /* Delta is in bytes .. */
609            delta = (((Elf_Word)fixup) - ((Elf_Word)pP) - 8);
610            deltaTop8 = (delta >> 24) & 0xFF;
611            if (deltaTop8 != 0 && deltaTop8 != 0xFF) {
612	      fprintf(stderr,"R_ARM_PC24: out of range delta 0x%x for %s\n",
613		      delta, symbol);
614	      exit(1);
615	    }
616            delta >>= 2;
617	    w = *pP;
618            w &= 0xFF000000;
619            w |= (0x00FFFFFF & delta );
620            *pP = w;
621	    break;
622         }
623         case R_ARM_ABS32:
624	    *pP = value;
625	    break;
626#        endif
627         default:
628            fprintf(stderr,
629                    "%s: unhandled ELF relocation(Rel) type %d\n\n",
630		    oc->fileName, (Int)ELF_R_TYPE(info));
631            return 0;
632      }
633
634   }
635   return 1;
636}
637
638/* Do ELF relocations for which explicit addends are supplied.
639   sparc-solaris relocations appear to be of this form. */
640static int
641do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
642                          Elf_Shdr* shdr, int shnum,
643                          Elf_Sym*  stab, char* strtab )
644{
645   int j;
646   char *symbol;
647   Elf_Addr targ;
648   Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
649   int         nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
650   int target_shndx = shdr[shnum].sh_info;
651   int symtab_shndx = shdr[shnum].sh_link;
652
653   stab  = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
654   targ  = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
655   IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
656                          target_shndx, symtab_shndx ));
657
658   for (j = 0; j < nent; j++) {
659#if defined(DEBUG) || defined(sparc_TARGET_ARCH)  \
660                   || defined(ia64_TARGET_ARCH)   \
661                   || defined(x86_64_TARGET_ARCH) \
662                   || defined(ppc32_TARGET_ARCH)
663      /* This #ifdef only serves to avoid unused-var warnings. */
664      Elf_Addr  offset = rtab[j].r_offset;
665      Elf_Addr  P      = targ + offset;
666#endif
667      Elf_Addr  info   = rtab[j].r_info;
668      Elf_Addr  A      = rtab[j].r_addend;
669      Elf_Addr  S =0;
670      Elf_Addr  value;
671#     if defined(sparc_TARGET_ARCH)
672      Elf_Word* pP = (Elf_Word*)P;
673      Elf_Word  w1, w2;
674#     endif
675#     if defined(ia64_TARGET_ARCH)
676      Elf64_Xword *pP = (Elf64_Xword *)P;
677      Elf_Addr addr;
678#     endif
679#     if defined(x86_64_TARGET_ARCH)
680      ULong* pP = (ULong*)P;
681#     endif
682#     if defined(ppc32_TARGET_ARCH)
683      Int sI, sI2;
684      Elf_Word* pP = (Elf_Word*)P;
685#     endif
686
687      IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p)   ",
688                             j, (void*)offset, (void*)info,
689                                (void*)A ));
690      if (!info) {
691         IF_DEBUG(linker,belch( " ZERO" ));
692         S = 0;
693      } else {
694         Elf_Sym sym = stab[ELF_R_SYM(info)];
695	 /* First see if it is a local symbol. */
696         if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
697            /* Yes, so we can get the address directly from the ELF symbol
698               table. */
699            symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
700            S = (Elf_Addr)
701                (ehdrC + shdr[ sym.st_shndx ].sh_offset
702                       + stab[ELF_R_SYM(info)].st_value);
703#ifdef ELF_FUNCTION_DESC
704	    /* Make a function descriptor for this function */
705            if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
706               S = allocateFunctionDesc(S + A);
707       	       A = 0;
708            }
709#endif
710	 } else {
711            /* No, so look up the name in our global table. */
712            symbol = strtab + sym.st_name;
713            S = (Elf_Addr)lookupSymbol( symbol );
714
715#ifdef ELF_FUNCTION_DESC
716	    /* If a function, already a function descriptor - we would
717	       have to copy it to add an offset. */
718            if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
719               belch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
720#endif
721	 }
722         if (!S) {
723	   fprintf(stderr,"%s: unknown symbol `%s'\n", oc->fileName, symbol);
724	   return 0;
725         }
726         if (0)
727            fprintf(stderr, "`%s' resolves to %p\n", symbol, (void*)S );
728      }
729
730#if 0
731         fprintf ( stderr, "Reloc: offset = %p   P = %p   S = %p   A = %p\n",
732                           (void*)offset, (void*)P, (void*)S, (void*)A );
733#endif
734
735      /* checkProddableBlock ( oc, (void*)P ); */
736
737      value = S + A;
738
739      switch (ELF_R_TYPE(info)) {
740#        if defined(sparc_TARGET_ARCH)
741         case R_SPARC_WDISP30:
742            w1 = *pP & 0xC0000000;
743            w2 = (Elf_Word)((value - P) >> 2);
744            ASSERT((w2 & 0xC0000000) == 0);
745            w1 |= w2;
746            *pP = w1;
747            break;
748         case R_SPARC_HI22:
749            w1 = *pP & 0xFFC00000;
750            w2 = (Elf_Word)(value >> 10);
751            ASSERT((w2 & 0xFFC00000) == 0);
752            w1 |= w2;
753            *pP = w1;
754            break;
755         case R_SPARC_LO10:
756            w1 = *pP & ~0x3FF;
757            w2 = (Elf_Word)(value & 0x3FF);
758            ASSERT((w2 & ~0x3FF) == 0);
759            w1 |= w2;
760            *pP = w1;
761            break;
762         /* According to the Sun documentation:
763            R_SPARC_UA32
764            This relocation type resembles R_SPARC_32, except it refers to an
765            unaligned word. That is, the word to be relocated must be treated
766            as four separate bytes with arbitrary alignment, not as a word
767            aligned according to the architecture requirements.
768
769            (JRS: which means that freeloading on the R_SPARC_32 case
770            is probably wrong, but hey ...)
771         */
772         case R_SPARC_UA32:
773         case R_SPARC_32:
774            w2 = (Elf_Word)value;
775            *pP = w2;
776            break;
777#        endif
778#        if defined(ia64_TARGET_ARCH)
779	 case R_IA64_DIR64LSB:
780	 case R_IA64_FPTR64LSB:
781	    *pP = value;
782	    break;
783	 case R_IA64_PCREL64LSB:
784	    *pP = value - P;
785	    break;
786	 case R_IA64_SEGREL64LSB:
787	    addr = findElfSegment(ehdrC, value);
788	    *pP = value - addr;
789	    break;
790	 case R_IA64_GPREL22:
791	    ia64_reloc_gprel22(P, value);
792	    break;
793	 case R_IA64_LTOFF22:
794	 case R_IA64_LTOFF22X:
795	 case R_IA64_LTOFF_FPTR22:
796	    addr = allocateGOTEntry(value);
797	    ia64_reloc_gprel22(P, addr);
798	    break;
799	 case R_IA64_PCREL21B:
800	    ia64_reloc_pcrel21(P, S, oc);
801	    break;
802	 case R_IA64_LDXMOV:
803	    /* This goes with R_IA64_LTOFF22X and points to the load to
804	       convert into a move.  We don't implement relaxation. */
805	    break;
806#        endif
807#        if defined(x86_64_TARGET_ARCH)
808         case R_X86_64_64: /* 1 *//* Direct 64 bit  */
809            *((ULong*)pP) = (ULong)(S + A);
810            break;
811         case R_X86_64_PC32: /* 2 *//* PC relative 32 bit signed */
812            *((UInt*)pP) = (UInt)(S + A - P);
813            break;
814         case R_X86_64_32: /* 10 *//* Direct 32 bit zero extended */
815            *((UInt*)pP) = (UInt)(S + A);
816            break;
817         case R_X86_64_32S: /* 11 *//* Direct 32 bit sign extended */
818            *((UInt*)pP) = (UInt)(S + A);
819            break;
820#        endif
821#        if defined(ppc32_TARGET_ARCH)
822         case R_PPC_ADDR32: /* 1 *//* 32bit absolute address */
823            *((UInt*)pP) = S+A;
824            invalidate_icache(pP,4);
825            break;
826         case R_PPC_ADDR16_LO: /* 4 *//* lower 16bit of absolute address */
827            *((UInt*)pP) &= 0x0000FFFF;
828            *((UInt*)pP) |= 0xFFFF0000 & (compute_ppc_LO(S+A) << 16);
829            invalidate_icache(pP,4);
830            break;
831         case R_PPC_ADDR16_HA: /* 6 *//* adjusted high 16bit */
832            *((UInt*)pP) &= 0x0000FFFF;
833            *((UInt*)pP) |= 0xFFFF0000 & (compute_ppc_HA(S+A) << 16);
834            invalidate_icache(pP,4);
835            break;
836         case R_PPC_REL24: /* 10 *//* PC relative 26 bit */
837            sI = S+A-P;
838	    sI >>= 2;
839	    /* the top 9 bits of sI must be the same (all 0s or
840	       all 1s) for this to be valid; else we have to fail. */
841            sI2 = sI >> 23; /* 23 == 32 - 9 */
842            if (sI2 != 0 && sI2 != 0xFFFFFFFF) {
843               fprintf(stderr, "%s: R_PPC_REL24 relocation failed\n", oc->fileName );
844	       return 0;
845            }
846            *((UInt*)pP) &= ~(0x00FFFFFF << 2);
847            *((UInt*)pP) |= (0xFFFFFF & sI) << 2;
848           invalidate_icache(pP,4);
849            break;
850         case R_PPC_REL32: /* 26 */
851            *((UInt*)pP) = S+A-P;
852            invalidate_icache(pP,4);
853            break;
854#        endif
855         default:
856            fprintf(stderr,
857                    "%s: unhandled ELF relocation(RelA) type %d\n",
858		    oc->fileName, (Int)ELF_R_TYPE(info));
859            return 0;
860      }
861
862   }
863   return 1;
864}
865
866
867static int
868ocResolve_ELF ( ObjectCode* oc )
869{
870   char *strtab;
871   int   shnum, ok;
872   Elf_Sym*  stab  = NULL;
873   char*     ehdrC = (char*)(oc->image);
874   Elf_Ehdr* ehdr  = (Elf_Ehdr*) ehdrC;
875   Elf_Shdr* shdr  = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
876   char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
877
878   /* first find "the" symbol table */
879   stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
880
881   /* also go find the string table */
882   strtab = findElfSection ( ehdrC, SHT_STRTAB );
883
884   if (stab == NULL || strtab == NULL) {
885      fprintf(stderr,"%s: can't find string or symbol table\n", oc->fileName);
886      return 0;
887   }
888
889   /* Process the relocation sections. */
890   for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
891
892      /* Skip sections called ".rel.stab".  These appear to contain
893         relocation entries that, when done, make the stabs debugging
894         info point at the right places.  We ain't interested in all
895         dat jazz, mun. */
896      if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
897         continue;
898
899      if (shdr[shnum].sh_type == SHT_REL ) {
900         ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
901                                       shnum, stab, strtab );
902         if (!ok) return ok;
903      }
904      else
905      if (shdr[shnum].sh_type == SHT_RELA) {
906         ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
907                                        shnum, stab, strtab );
908         if (!ok) return ok;
909      }
910   }
911
912   /* Free the local symbol table; we won't need it again. */
913   delete_StringMap(oc->lochash);
914   oc->lochash = NULL;
915
916   return 1;
917}
918
919
920///////////////////////////////////////////////////////////////////
921///////////////////////////////////////////////////////////////////
922///////////////////////////////////////////////////////////////////
923//
924// VERIFY
925
926static int
927ocVerifyImage_ELF ( ObjectCode* oc )
928{
929   Elf_Shdr* shdr;
930   Elf_Sym*  stab;
931   int i, j, nent, nstrtab, nsymtabs;
932   char* sh_strtab;
933   char* strtab;
934
935   char*     ehdrC = (char*)(oc->image);
936   Elf_Ehdr* ehdr  = (Elf_Ehdr*)ehdrC;
937
938   if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
939       ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
940       ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
941       ehdr->e_ident[EI_MAG3] != ELFMAG3) {
942      fprintf(stderr,"%s: not an ELF object\n", oc->fileName);
943      return 0;
944   }
945
946   if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
947      fprintf(stderr,"%s: unsupported ELF format\n", oc->fileName);
948      return 0;
949   }
950
951   if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
952      if (debug_linker)
953         fprintf(stderr, "Is little-endian\n" );
954   } else
955   if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
956       if (debug_linker)
957          fprintf(stderr, "Is big-endian\n" );
958   } else {
959       fprintf(stderr,"%s: unknown endiannness\n", oc->fileName);
960       return 0;
961   }
962
963   if (ehdr->e_type != ET_REL) {
964      fprintf(stderr,"%s: not a relocatable object (.o) file\n", oc->fileName);
965      return 0;
966   }
967   if (debug_linker)
968      fprintf(stderr, "Is a relocatable object (.o) file\n" );
969
970   if (debug_linker)
971      fprintf(stderr, "Architecture is " );
972   switch (ehdr->e_machine) {
973      case EM_386:    if (debug_linker) fprintf(stderr, "x86\n" ); break;
974      case EM_SPARC:  if (debug_linker) fprintf(stderr, "sparc\n" ); break;
975      case EM_ARM:    if (debug_linker) fprintf(stderr, "arm\n" ); break;
976#ifdef EM_IA_64
977      case EM_IA_64:  if (debug_linker) fprintf(stderr, "ia64\n" ); break;
978#endif
979      case EM_X86_64: if (debug_linker) fprintf(stderr, "x86_64\n" ); break;
980      case EM_PPC:    if (debug_linker) fprintf(stderr, "ppc\n" ); break;
981      default:        if (debug_linker) fprintf(stderr, "unknown\n" );
982                      fprintf(stderr,"%s: unknown architecture\n", oc->fileName);
983                      return 0;
984   }
985
986   if (debug_linker>1) fprintf(stderr,
987             "\nSection header table: start %lld, n_entries %d, ent_size %d\n",
988             (Long)ehdr->e_shoff,
989             ehdr->e_shnum, ehdr->e_shentsize  );
990
991   assert (ehdr->e_shentsize == sizeof(Elf_Shdr));
992
993   shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
994
995   if (ehdr->e_shstrndx == SHN_UNDEF) {
996      fprintf(stderr,"%s: no section header string table\n", oc->fileName);
997      return 0;
998   } else {
999      if (debug_linker>1)
1000         fprintf(stderr, "Section header string table is section %d\n",
1001                          ehdr->e_shstrndx);
1002      sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1003   }
1004
1005   for (i = 0; i < ehdr->e_shnum; i++) {
1006      if (debug_linker>1) fprintf(stderr, "%2d:  ", i );
1007      if (debug_linker>1) fprintf(stderr, "type=%2d  ", (int)shdr[i].sh_type );
1008      if (debug_linker>1) fprintf(stderr, "size=%4d  ", (int)shdr[i].sh_size );
1009      if (debug_linker>1) fprintf(stderr, "offs=%4d  ", (int)shdr[i].sh_offset );
1010      if (debug_linker>1) fprintf(stderr, "  (%p .. %p)  ",
1011               ehdrC + shdr[i].sh_offset,
1012		      ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
1013
1014      if (shdr[i].sh_type == SHT_REL) {
1015	  if (debug_linker>1) fprintf(stderr, "Rel  " );
1016      } else if (shdr[i].sh_type == SHT_RELA) {
1017	  if (debug_linker>1) fprintf(stderr, "RelA " );
1018      } else {
1019	  if (debug_linker>1) fprintf(stderr,"     ");
1020      }
1021      if (sh_strtab) {
1022	  if (debug_linker>1) fprintf(stderr, "sname=%s\n",
1023             sh_strtab + shdr[i].sh_name );
1024      }
1025   }
1026
1027   if (debug_linker>1) fprintf(stderr, "\nString tables\n" );
1028   strtab = NULL;
1029   nstrtab = 0;
1030   for (i = 0; i < ehdr->e_shnum; i++) {
1031      if (shdr[i].sh_type == SHT_STRTAB
1032          /* Ignore the section header's string table. */
1033          && i != ehdr->e_shstrndx
1034	  /* Ignore string tables named .stabstr, as they contain
1035             debugging info. */
1036          && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
1037         ) {
1038         if (debug_linker>1)
1039            fprintf(stderr,"   section %d is a normal string table\n", i );
1040         strtab = ehdrC + shdr[i].sh_offset;
1041         nstrtab++;
1042      }
1043   }
1044   if (nstrtab != 1) {
1045      fprintf(stderr,"%s: no string tables, or too many\n", oc->fileName);
1046      return 0;
1047   }
1048
1049   nsymtabs = 0;
1050   if (debug_linker>1) fprintf(stderr, "\nSymbol tables\n" );
1051   for (i = 0; i < ehdr->e_shnum; i++) {
1052      if (shdr[i].sh_type != SHT_SYMTAB) continue;
1053      if (debug_linker>1) fprintf(stderr, "section %d is a symbol table\n", i );
1054      nsymtabs++;
1055      stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
1056      nent = shdr[i].sh_size / sizeof(Elf_Sym);
1057      if (debug_linker>1) fprintf(stderr,
1058            "   number of entries is apparently %d (%lld rem)\n",
1059               nent,
1060               (Long)(shdr[i].sh_size % sizeof(Elf_Sym))
1061             );
1062      if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
1063         fprintf(stderr,"%s: non-integral number of symbol table entries\n",
1064                        oc->fileName);
1065         return 0;
1066      }
1067      for (j = 0; j < nent; j++) {
1068         if (debug_linker>1) fprintf(stderr, "   %2d  ", j );
1069         if (debug_linker>1) fprintf(stderr, "  sec=%-5d  size=%-3d  val=%5p  ",
1070                             (int)stab[j].st_shndx,
1071                             (int)stab[j].st_size,
1072                             (char*)stab[j].st_value );
1073
1074         if (debug_linker>1) fprintf(stderr, "type=" );
1075         switch (ELF_ST_TYPE(stab[j].st_info)) {
1076            case STT_NOTYPE:  if (debug_linker>1) fprintf(stderr, "notype " ); break;
1077            case STT_OBJECT:  if (debug_linker>1) fprintf(stderr, "object " ); break;
1078            case STT_FUNC  :  if (debug_linker>1) fprintf(stderr, "func   " ); break;
1079            case STT_SECTION: if (debug_linker>1) fprintf(stderr, "section" ); break;
1080            case STT_FILE:    if (debug_linker>1) fprintf(stderr, "file   " ); break;
1081            default:          if (debug_linker>1) fprintf(stderr, "?      " ); break;
1082         }
1083         if (debug_linker>1) fprintf(stderr, "  " );
1084
1085         if (debug_linker>1) fprintf(stderr, "bind=" );
1086         switch (ELF_ST_BIND(stab[j].st_info)) {
1087            case STB_LOCAL :  if (debug_linker>1) fprintf(stderr, "local " ); break;
1088            case STB_GLOBAL:  if (debug_linker>1) fprintf(stderr, "global" ); break;
1089            case STB_WEAK  :  if (debug_linker>1) fprintf(stderr, "weak  " ); break;
1090            default:          if (debug_linker>1) fprintf(stderr, "?     " ); break;
1091         }
1092         if (debug_linker>1) fprintf(stderr, "  " );
1093
1094         if (debug_linker>1) fprintf(stderr, "name=%s\n", strtab + stab[j].st_name );
1095      }
1096   }
1097
1098   if (nsymtabs == 0) {
1099      fprintf(stderr,"%s: didn't find any symbol tables\n", oc->fileName);
1100      return 0;
1101   }
1102
1103   return 1;
1104}
1105
1106
1107///////////////////////////////////////////////////////////////////
1108///////////////////////////////////////////////////////////////////
1109///////////////////////////////////////////////////////////////////
1110//
1111// GETNAMES
1112
1113static int
1114ocGetNames_ELF ( ObjectCode* oc )
1115{
1116   int i, j, k, nent;
1117   Elf_Sym* stab;
1118
1119   char*     ehdrC     = (char*)(oc->image);
1120   Elf_Ehdr* ehdr      = (Elf_Ehdr*)ehdrC;
1121   char*     strtab    = findElfSection ( ehdrC, SHT_STRTAB );
1122   Elf_Shdr* shdr      = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
1123
1124   char*     sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1125   char*     sec_name;
1126
1127   assert(global_symbol_table != NULL);
1128
1129   if (!strtab) {
1130      fprintf(stderr,"%s: no strtab\n", oc->fileName);
1131      return 0;
1132   }
1133
1134   k = 0;
1135   for (i = 0; i < ehdr->e_shnum; i++) {
1136      /* Figure out what kind of section it is.  Logic derived from
1137         Figure 1.14 ("Special Sections") of the ELF document
1138         ("Portable Formats Specification, Version 1.1"). */
1139      Elf_Shdr    hdr    = shdr[i];
1140      SectionKind kind   = SECTIONKIND_OTHER;
1141      int         is_bss = FALSE;
1142
1143      if (hdr.sh_type == SHT_PROGBITS
1144          && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
1145         /* .text-style section */
1146         kind = SECTIONKIND_CODE_OR_RODATA;
1147      }
1148      else
1149      if (hdr.sh_type == SHT_PROGBITS
1150          && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
1151         /* .data-style section */
1152         kind = SECTIONKIND_RWDATA;
1153      }
1154      else
1155      if (hdr.sh_type == SHT_PROGBITS
1156          && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
1157         /* .rodata-style section */
1158         kind = SECTIONKIND_CODE_OR_RODATA;
1159      }
1160      else
1161      if (hdr.sh_type == SHT_NOBITS
1162          && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
1163         /* .bss-style section */
1164         kind = SECTIONKIND_RWDATA;
1165         is_bss = TRUE;
1166      }
1167
1168      if (is_bss && shdr[i].sh_size > 0) {
1169         /* This is a non-empty .bss section.  Allocate zeroed space for
1170            it, and set its .sh_offset field such that
1171            ehdrC + .sh_offset == addr_of_zeroed_space.  */
1172         char* zspace = calloc(1, shdr[i].sh_size);
1173         shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
1174	 if (1)
1175         fprintf(stderr, "BSS section at %p, size %lld\n",
1176                         zspace, (Long)shdr[i].sh_size);
1177      }
1178
1179      /* When loading objects compiled with -g, it seems there are
1180	 relocations in various debug-info sections.  So we'd better
1181	 tell addProddableBlock to allow those bits to be prodded. */
1182      //fprintf(stderr, "ZZZZZZZZZZ %s\n", sh_strtab + hdr.sh_name);
1183      sec_name = sh_strtab + shdr[i].sh_name;
1184      if (kind == SECTIONKIND_OTHER
1185          && (0 == strcmp(".debug_info", sec_name)
1186              || 0 == strcmp(".debug_line", sec_name)
1187              || 0 == strcmp(".debug_pubnames", sec_name)
1188              || 0 == strcmp(".debug_aranges", sec_name)
1189              || 0 == strcmp(".debug_frame", sec_name))) {
1190         kind = SECTIONKIND_CODE_OR_RODATA;
1191      }
1192
1193      /* fill in the section info */
1194      if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
1195         addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
1196         //addSection(oc, kind, ehdrC + shdr[i].sh_offset,
1197         //               ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
1198      }
1199
1200      if (shdr[i].sh_type != SHT_SYMTAB) continue;
1201
1202      /* copy stuff into this module's object symbol table */
1203      stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
1204      nent = shdr[i].sh_size / sizeof(Elf_Sym);
1205
1206      oc->n_symbols = nent;
1207      oc->symbols = mymalloc(oc->n_symbols * sizeof(char*));
1208
1209      for (j = 0; j < nent; j++) {
1210
1211         char  isLocal = FALSE; /* avoids uninit-var warning */
1212         char* ad      = NULL;
1213         char* nm      = strtab + stab[j].st_name;
1214         int   secno   = stab[j].st_shndx;
1215
1216	 /* Figure out if we want to add it; if so, set ad to its
1217            address.  Otherwise leave ad == NULL. */
1218
1219         if (secno == SHN_COMMON) {
1220            isLocal = FALSE;
1221#           if defined(__x86_64__)
1222            ad = calloc_below2G(1, stab[j].st_size);
1223#           else
1224            ad = calloc(1, stab[j].st_size);
1225#           endif
1226    //	    assert( (Addr)ad < 0xF0000000ULL );
1227
1228	    if (0)
1229            fprintf(stderr, "COMMON symbol, size %lld name %s  allocd %p\n",
1230                            (Long)stab[j].st_size, nm, ad);
1231	    /* Pointless to do addProddableBlock() for this area,
1232               since the linker should never poke around in it. */
1233	 }
1234         else
1235         if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
1236                || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
1237              )
1238              /* and not an undefined symbol */
1239              && stab[j].st_shndx != SHN_UNDEF
1240	      /* and not in a "special section" */
1241              && stab[j].st_shndx < SHN_LORESERVE
1242              &&
1243	      /* and it's a not a section or string table or anything silly */
1244              ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1245                ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1246                ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1247              )
1248            ) {
1249	    /* Section 0 is the undefined section, hence > and not >=. */
1250            assert(secno > 0 && secno < ehdr->e_shnum);
1251	    /*
1252            if (shdr[secno].sh_type == SHT_NOBITS) {
1253               fprintf(stderr, "   BSS symbol, size %d off %d name %s\n",
1254                               stab[j].st_size, stab[j].st_value, nm);
1255            }
1256            */
1257            ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
1258            if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1259               isLocal = TRUE;
1260            } else {
1261#ifdef ELF_FUNCTION_DESC
1262               /* dlsym() and the initialisation table both give us function
1263		* descriptors, so to be consistent we store function descriptors
1264		* in the symbol table */
1265               if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
1266                   ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
1267#endif
1268               if (0|| debug_linker)
1269                   fprintf(stderr, "addOTabName(GLOB): %10p  %s %s\n",
1270                                      ad, oc->fileName, nm );
1271               isLocal = FALSE;
1272            }
1273         }
1274
1275         /* And the decision is ... */
1276
1277         if (ad != NULL) {
1278            assert(nm != NULL);
1279	    oc->symbols[j] = nm;
1280            /* Acquire! */
1281            if (isLocal) {
1282               /* Ignore entirely. */
1283            } else {
1284	      //ghciInsertStrHashTable(oc->fileName, global_symbol_table, nm, ad);
1285	      paranoid_addto_StringMap(global_symbol_table, nm, ad);
1286            }
1287         } else {
1288            /* Skip. */
1289            if (debug_linker>1) fprintf(stderr, "skipping `%s'\n",
1290                                   strtab + stab[j].st_name );
1291            /*
1292            fprintf(stderr,
1293                    "skipping   bind = %d,  type = %d,  shndx = %d   `%s'\n",
1294                    (int)ELF_ST_BIND(stab[j].st_info),
1295                    (int)ELF_ST_TYPE(stab[j].st_info),
1296                    (int)stab[j].st_shndx,
1297                    strtab + stab[j].st_name
1298                   );
1299            */
1300            oc->symbols[j] = NULL;
1301         }
1302
1303      }
1304   }
1305
1306   return 1;
1307}
1308
1309
1310///////////////////////////////////////////////////////////////////
1311///////////////////////////////////////////////////////////////////
1312///////////////////////////////////////////////////////////////////
1313//
1314// TOP-LEVEL CONTROL OF THE LINKER
1315
1316
1317/* ---------------------------------------------------------------------
1318 * Load an obj (populate the global symbol table, but don't resolve yet)
1319 *
1320 * Returns: 1 if ok, 0 on error.
1321 */
1322static
1323int loadObj( char *path )
1324{
1325   ObjectCode* oc;
1326   struct stat st;
1327   int r;
1328   int fd, pagesize;
1329   char* p;
1330
1331   initLinker();
1332
1333   fprintf(stderr, "==== loadObj %s ====\n", path );
1334
1335   /* Check that we haven't already loaded this object.  */
1336   {
1337       ObjectCode *o;
1338       int is_dup = 0;
1339       for (o = global_object_list; o; o = o->next) {
1340          if (0 == strcmp(o->fileName, path))
1341             is_dup = 1;
1342       }
1343       if (is_dup) {
1344	 fprintf(stderr,
1345            "\n\n"
1346            "GHCi runtime linker: warning: looks like you're trying to load the\n"
1347            "same object file twice:\n"
1348            "   %s\n"
1349            , path);
1350	 exit(1);
1351       }
1352   }
1353
1354   oc = mymalloc(sizeof(ObjectCode));
1355
1356   oc->formatName = "ELF";
1357
1358   r = stat(path, &st);
1359   if (r == -1) { return 0; }
1360
1361   /* sigh, strdup() isn't a POSIX function, so do it the long way */
1362   oc->fileName = mymalloc( strlen(path)+1 );
1363   strcpy(oc->fileName, path);
1364
1365   oc->fileSize          = st.st_size;
1366   oc->symbols           = NULL;
1367   oc->sections          = NULL;
1368   oc->lochash           = new_StringMap();
1369   oc->proddables        = NULL;
1370   oc->fixup             = NULL;
1371   oc->fixup_used        = 0;
1372   oc->fixup_size        = 0;
1373
1374   /* chain it onto the list of objects */
1375   oc->next              = global_object_list;
1376   global_object_list    = oc;
1377
1378   fd = open(path, O_RDONLY);
1379   if (fd == -1) {
1380      fprintf(stderr,"loadObj: can't open `%s'\n", path);
1381      exit(1);
1382   }
1383
1384   /* Allocate a 1-page area just prior to the image, so we can put
1385      fixup code fragments there.  Used for doing R_ARM_PC24
1386      relocations for jump distances > 64M. */
1387
1388   pagesize = getpagesize();
1389   //   p = memalign(pagesize, N_FIXUP_PAGES * pagesize
1390   //                          + oc->fileSize);
1391   p = mymalloc(N_FIXUP_PAGES * pagesize + oc->fileSize);
1392   if (0) fprintf(stderr,"XXXX p = %p\n", p);
1393   if (p == NULL) {
1394      fprintf(stderr,"loadObj: failed to allocate space for `%s'\n", path);
1395      exit(1);
1396   }
1397
1398   oc->fixup = p;
1399   oc->fixup_size = N_FIXUP_PAGES * pagesize;
1400   oc->fixup_used = 0;
1401   oc->image = &(p[ oc->fixup_size ]);
1402
1403   r = read(fd, oc->image, oc->fileSize);
1404   if (r != oc->fileSize) {
1405      fprintf(stderr,"loadObj: failed to read `%s'\n", path);
1406      exit(1);
1407   }
1408
1409   fprintf(stderr, "loaded %s at %p (fixup = %p)\n",
1410                   oc->fileName, oc->image, oc->fixup );
1411
1412   close(fd);
1413
1414   /* verify the in-memory image */
1415   r = ocVerifyImage_ELF ( oc );
1416   if (!r) { return r; }
1417
1418   /* build the symbol list for this image */
1419   r = ocGetNames_ELF ( oc );
1420   if (!r) { return r; }
1421
1422   /* loaded, but not resolved yet */
1423   oc->status = OBJECT_LOADED;
1424
1425#ifdef ppc32_TARGET_ARCH
1426   invalidate_icache(oc->image, oc->fileSize);
1427#endif
1428
1429   return 1;
1430}
1431
1432
1433
1434/* ---------------------------------------------------------------------------
1435 * resolve all the currently unlinked objects in memory
1436 *
1437 * Returns: 1 if ok, 0 on error.
1438 */
1439static
1440int resolveObjs( void )
1441{
1442    ObjectCode *oc;
1443    int r;
1444
1445    initLinker();
1446
1447    for (oc = global_object_list; oc; oc = oc->next) {
1448	if (oc->status != OBJECT_RESOLVED) {
1449	    r = ocResolve_ELF ( oc );
1450	    if (!r) { return r; }
1451	    oc->status = OBJECT_RESOLVED;
1452	}
1453    }
1454    return 1;
1455}
1456
1457
1458/* ---------------------------------------------------------------------------
1459 * Top-level linker.
1460 */
1461
1462/* Load and link a bunch of .o's, and return the address of
1463   'entry'.  Or NULL if something borks.
1464*/
1465void* linker_top_level_LINK ( int n_object_names, char** object_names )
1466{
1467   int   r, i;
1468   void* mainp;
1469
1470   initLinker();
1471   for (i = 0; i < n_object_names; i++) {
1472      //fprintf(stderr, "linkloop %d %s\n", i, object_names[i] );
1473      r = loadObj( object_names[i] );
1474      if (r != 1) return NULL;
1475   }
1476   r = resolveObjs();
1477   if (r != 1) return NULL;
1478   mainp = search_StringMap ( global_symbol_table, "entry" );
1479   if (mainp == NULL) return NULL;
1480   printf("switchback: Linker: success!\n");
1481   return mainp;
1482}
1483
1484
1485#endif
1486