X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FLinker.c;h=5058294c358ca99ebfae7be05cdb04a12c86c28b;hb=004ecb3959530fcdc01fe9102dc3aeb9e1adb1f1;hp=d6024ac86f70b656ae69e59bee979bd3b5d5b8d4;hpb=95ca6bff6fc9918203173b442192d9298ef9757a;p=ghc-hetmet.git diff --git a/ghc/rts/Linker.c b/ghc/rts/Linker.c index d6024ac..5058294 100644 --- a/ghc/rts/Linker.c +++ b/ghc/rts/Linker.c @@ -96,6 +96,9 @@ ObjectCode *objects = NULL; /* initially empty */ static int ocVerifyImage_ELF ( ObjectCode* oc ); static int ocGetNames_ELF ( ObjectCode* oc ); static int ocResolve_ELF ( ObjectCode* oc ); +#if defined(powerpc_TARGET_ARCH) +static int ocAllocateJumpIslands_ELF ( ObjectCode* oc ); +#endif #elif defined(OBJFORMAT_PEi386) static int ocVerifyImage_PEi386 ( ObjectCode* oc ); static int ocGetNames_PEi386 ( ObjectCode* oc ); @@ -411,6 +414,7 @@ typedef struct _RtsSymbolVal { SymX(gcdIntegerIntzh_fast) \ SymX(gcdIntzh_fast) \ SymX(genSymZh) \ + SymX(genericRaise) \ SymX(getProgArgv) \ SymX(getStablePtr) \ SymX(initLinker) \ @@ -1097,6 +1101,9 @@ loadObj( char *path ) # if defined(OBJFORMAT_MACHO) r = ocAllocateJumpIslands_MachO ( oc ); if (!r) { return r; } +# elif defined(OBJFORMAT_ELF) && defined(powerpc_TARGET_ARCH) + r = ocAllocateJumpIslands_ELF ( oc ); + if (!r) { return r; } #endif /* verify the in-memory image */ @@ -1264,6 +1271,112 @@ static void addSection ( ObjectCode* oc, SectionKind kind, } +/* -------------------------------------------------------------------------- + * PowerPC specifics (jump islands) + * ------------------------------------------------------------------------*/ + +#if defined(powerpc_TARGET_ARCH) + +/* + ocAllocateJumpIslands + + Allocate additional space at the end of the object file image to make room + for jump islands. + + PowerPC relative branch instructions have a 24 bit displacement field. + As PPC code is always 4-byte-aligned, this yields a +-32MB range. + If a particular imported symbol is outside this range, we have to redirect + the jump to a short piece of new code that just loads the 32bit absolute + address and jumps there. + This function just allocates space for one 16 byte ppcJumpIsland for every + undefined symbol in the object file. The code for the islands is filled in by + makeJumpIsland below. +*/ + +static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first ) +{ + int aligned; + + if( count > 0 ) + { +#ifdef USE_MMAP + #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined +#endif + // round up to the nearest 4 + aligned = (oc->fileSize + 3) & ~3; + + oc->image = stgReallocBytes( oc->image, + aligned + sizeof( ppcJumpIsland ) * count, + "ocAllocateJumpIslands" ); + oc->jump_islands = (ppcJumpIsland *) (((char *) oc->image) + aligned); + memset( oc->jump_islands, 0, sizeof( ppcJumpIsland ) * count ); + } + else + oc->jump_islands = NULL; + + oc->island_start_symbol = first; + oc->n_islands = count; + + return 1; +} + +static unsigned long makeJumpIsland( ObjectCode* oc, + unsigned long symbolNumber, + unsigned long target ) +{ + ppcJumpIsland *island; + + if( symbolNumber < oc->island_start_symbol || + symbolNumber - oc->island_start_symbol > oc->n_islands) + return 0; + + island = &oc->jump_islands[symbolNumber - oc->island_start_symbol]; + + // lis r12, hi16(target) + island->lis_r12 = 0x3d80; + island->hi_addr = target >> 16; + + // ori r12, r12, lo16(target) + island->ori_r12_r12 = 0x618c; + island->lo_addr = target & 0xffff; + + // mtctr r12 + island->mtctr_r12 = 0x7d8903a6; + + // bctr + island->bctr = 0x4e800420; + + return (unsigned long) island; +} + +/* + ocFlushInstructionCache + + Flush the data & instruction caches. + Because the PPC has split data/instruction caches, we have to + do that whenever we modify code at runtime. + */ + +static void ocFlushInstructionCache( ObjectCode *oc ) +{ + int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4; + unsigned long *p = (unsigned long *) oc->image; + + while( n-- ) + { + __asm__ volatile ( "dcbf 0,%0\n\t" + "sync\n\t" + "icbi 0,%0" + : + : "r" (p) + ); + p++; + } + __asm__ volatile ( "sync\n\t" + "isync" + ); +} +#endif /* -------------------------------------------------------------------------- * PEi386 specifics (Win32 targets) @@ -2042,7 +2155,7 @@ ocResolve_PEi386 ( ObjectCode* oc ) *pP = S - ((UInt32)pP) - 4; break; default: - debugBelch(("%s: unhandled PEi386 relocation type %d", + debugBelch("%s: unhandled PEi386 relocation type %d", oc->fileName, reltab_j->Type); return 0; } @@ -2351,6 +2464,7 @@ ocVerifyImage_ELF ( ObjectCode* oc ) #ifdef EM_IA_64 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break; #endif + case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break; default: IF_DEBUG(linker,debugBelch( "unknown" )); errorBelch("%s: unknown architecture", oc->fileName); return 0; @@ -2748,7 +2862,7 @@ do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC, target_shndx, symtab_shndx )); for (j = 0; j < nent; j++) { -#if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH) +#if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH) || defined(powerpc_TARGET_ARCH) /* This #ifdef only serves to avoid unused-var warnings. */ Elf_Addr offset = rtab[j].r_offset; Elf_Addr P = targ + offset; @@ -2764,6 +2878,8 @@ do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC, # elif defined(ia64_TARGET_ARCH) Elf64_Xword *pP = (Elf64_Xword *)P; Elf_Addr addr; +# elif defined(powerpc_TARGET_ARCH) + Elf_Sword delta; # endif IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ", @@ -2881,6 +2997,46 @@ do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC, /* This goes with R_IA64_LTOFF22X and points to the load to * convert into a move. We don't implement relaxation. */ break; +# elif defined(powerpc_TARGET_ARCH) + case R_PPC_ADDR16_LO: + *(Elf32_Half*) P = value; + break; + + case R_PPC_ADDR16_HI: + *(Elf32_Half*) P = value >> 16; + break; + + case R_PPC_ADDR16_HA: + *(Elf32_Half*) P = (value + 0x8000) >> 16; + break; + + case R_PPC_ADDR32: + *(Elf32_Word *) P = value; + break; + + case R_PPC_REL32: + *(Elf32_Word *) P = value - P; + break; + + case R_PPC_REL24: + delta = value - P; + + if( delta << 6 >> 6 != delta ) + { + value = makeJumpIsland( oc, ELF_R_SYM(info), value ); + delta = value - P; + + if( value == 0 || delta << 6 >> 6 != delta ) + { + barf( "Unable to make ppcJumpIsland for #%d", + ELF_R_SYM(info) ); + return 0; + } + } + + *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003) + | (delta & 0x3fffffc); + break; # endif default: errorBelch("%s: unhandled ELF relocation(RelA) type %d\n", @@ -2941,6 +3097,10 @@ ocResolve_ELF ( ObjectCode* oc ) freeHashTable(oc->lochash, NULL); oc->lochash = NULL; +#if defined(powerpc_TARGET_ARCH) + ocFlushInstructionCache( oc ); +#endif + return 1; } @@ -3036,6 +3196,44 @@ ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc) #endif /* ia64 */ +/* + * PowerPC ELF specifics + */ + +#ifdef powerpc_TARGET_ARCH + +static int ocAllocateJumpIslands_ELF( ObjectCode *oc ) +{ + Elf_Ehdr *ehdr; + Elf_Shdr* shdr; + int i; + + ehdr = (Elf_Ehdr *) oc->image; + shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff ); + + for( i = 0; i < ehdr->e_shnum; i++ ) + if( shdr[i].sh_type == SHT_SYMTAB ) + break; + + if( i == ehdr->e_shnum ) + { + errorBelch( "This ELF file contains no symtab" ); + return 0; + } + + if( shdr[i].sh_entsize != sizeof( Elf_Sym ) ) + { + errorBelch( "The entry size (%d) of the symtab isn't %d\n", + shdr[i].sh_entsize, sizeof( Elf_Sym ) ); + + return 0; + } + + return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 ); +} + +#endif /* powerpc */ + #endif /* ELF */ /* -------------------------------------------------------------------------- @@ -3054,57 +3252,25 @@ ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc) *) add still more sanity checks. */ - -/* - ocAllocateJumpIslands_MachO - - Allocate additional space at the end of the object file image to make room - for jump islands. - - PowerPC relative branch instructions have a 24 bit displacement field. - As PPC code is always 4-byte-aligned, this yields a +-32MB range. - If a particular imported symbol is outside this range, we have to redirect - the jump to a short piece of new code that just loads the 32bit absolute - address and jumps there. - This function just allocates space for one 16 byte jump island for every - undefined symbol in the object file. The code for the islands is filled in by - makeJumpIsland below. -*/ - -static const int islandSize = 16; - static int ocAllocateJumpIslands_MachO(ObjectCode* oc) { - char *image = (char*) oc->image; - struct mach_header *header = (struct mach_header*) image; - struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header)); + struct mach_header *header = (struct mach_header *) oc->image; + struct load_command *lc = (struct load_command *) (header + 1); unsigned i; - for(i=0;incmds;i++) + for( i = 0; i < header->ncmds; i++ ) { - if(lc->cmd == LC_DYSYMTAB) + if( lc->cmd == LC_DYSYMTAB ) { - struct dysymtab_command *dsymLC = (struct dysymtab_command*) lc; - unsigned long nundefsym = dsymLC->nundefsym; - oc->island_start_symbol = dsymLC->iundefsym; - oc->n_islands = nundefsym; + struct dysymtab_command *dsymLC = (struct dysymtab_command *) lc; - if(nundefsym > 0) - { -#ifdef USE_MMAP - #error ocAllocateJumpIslands_MachO doesnt want USE_MMAP to be defined -#else - oc->image = stgReallocBytes( - image, oc->fileSize + islandSize * nundefsym, - "ocAllocateJumpIslands_MachO"); -#endif - oc->jump_islands = oc->image + oc->fileSize; - memset(oc->jump_islands, 0, islandSize * nundefsym); - } + if( !ocAllocateJumpIslands( oc, dsymLC->nundefsym, + dsymLC->iundefsym ) ) + return 0; break; // there can be only one LC_DSYMTAB } - lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize ); + lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize ); } return 1; } @@ -3152,31 +3318,6 @@ static int resolveImports( return 1; } -static void* makeJumpIsland( - ObjectCode* oc, - unsigned long symbolNumber, - void* target) -{ - if(symbolNumber < oc->island_start_symbol || - symbolNumber - oc->island_start_symbol > oc->n_islands) - return NULL; - symbolNumber -= oc->island_start_symbol; - - void *island = (void*) ((char*)oc->jump_islands + islandSize * symbolNumber); - unsigned long *p = (unsigned long*) island; - - // lis r12, hi16(target) - *p++ = 0x3d800000 | ( ((unsigned long) target) >> 16 ); - // ori r12, r12, lo16(target) - *p++ = 0x618c0000 | ( ((unsigned long) target) & 0xFFFF ); - // mtctr r12 - *p++ = 0x7d8903a6; - // bctr - *p++ = 0x4e800420; - - return (void*) island; -} - static char* relocateAddress( ObjectCode* oc, int nSections, @@ -3232,7 +3373,12 @@ static int relocateSection( // Step 1: Figure out what the relocated value should be if(scat->r_type == GENERIC_RELOC_VANILLA) { - word = scat->r_value + sect->offset + ((long) image); + word = *wordPtr + (unsigned long) relocateAddress( + oc, + nSections, + sections, + scat->r_value) + - scat->r_value; } else if(scat->r_type == PPC_RELOC_SECTDIFF || scat->r_type == PPC_RELOC_LO16_SECTDIFF @@ -3352,7 +3498,7 @@ static int relocateSection( else if(reloc->r_type == PPC_RELOC_BR24) { word = *wordPtr; - word = (word & 0x03FFFFFC) | (word & 0x02000000) ? 0xFC000000 : 0; + word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0); } @@ -3378,9 +3524,11 @@ static int relocateSection( if(reloc->r_pcrel) { - ASSERT(word == 0); + // In the .o file, this should be a relative jump to NULL + // and we'll change it to a jump to a relative jump to the symbol + ASSERT(-word == reloc->r_address); word = symbolAddress; - jumpIsland = (long) makeJumpIsland(oc,reloc->r_symbolnum,(void*)word); + jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,word); word -= ((long)image) + sect->offset + reloc->r_address; if(jumpIsland != 0) { @@ -3506,68 +3654,86 @@ static int ocGetNames_MachO(ObjectCode* oc) // count external symbols defined here oc->n_symbols = 0; - for(i=dsymLC->iextdefsym;iiextdefsym+dsymLC->nextdefsym;i++) + if(dsymLC) { - if((nlist[i].n_type & N_TYPE) == N_SECT) - oc->n_symbols++; + for(i = dsymLC->iextdefsym; + i < dsymLC->iextdefsym + dsymLC->nextdefsym; + i++) + { + if((nlist[i].n_type & N_TYPE) == N_SECT) + oc->n_symbols++; + } } - for(i=0;insyms;i++) + if(symLC) { - if((nlist[i].n_type & N_TYPE) == N_UNDF + for(i=0;insyms;i++) + { + if((nlist[i].n_type & N_TYPE) == N_UNDF && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0)) - { - commonSize += nlist[i].n_value; - oc->n_symbols++; - } + { + commonSize += nlist[i].n_value; + oc->n_symbols++; + } + } } oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*), "ocGetNames_MachO(oc->symbols)"); - // insert symbols into hash table - for(i=dsymLC->iextdefsym,curSymbol=0;iiextdefsym+dsymLC->nextdefsym;i++) - { - if((nlist[i].n_type & N_TYPE) == N_SECT) - { - char *nm = image + symLC->stroff + nlist[i].n_un.n_strx; - ghciInsertStrHashTable(oc->fileName, symhash, nm, image + - sections[nlist[i].n_sect-1].offset - - sections[nlist[i].n_sect-1].addr - + nlist[i].n_value); - oc->symbols[curSymbol++] = nm; - } - } - - // insert local symbols into lochash - for(i=dsymLC->ilocalsym;iilocalsym+dsymLC->nlocalsym;i++) + if(dsymLC) { - if((nlist[i].n_type & N_TYPE) == N_SECT) - { - char *nm = image + symLC->stroff + nlist[i].n_un.n_strx; - ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image + - sections[nlist[i].n_sect-1].offset - - sections[nlist[i].n_sect-1].addr - + nlist[i].n_value); - } + // insert symbols into hash table + for(i = dsymLC->iextdefsym, curSymbol = 0; + i < dsymLC->iextdefsym + dsymLC->nextdefsym; + i++) + { + if((nlist[i].n_type & N_TYPE) == N_SECT) + { + char *nm = image + symLC->stroff + nlist[i].n_un.n_strx; + ghciInsertStrHashTable(oc->fileName, symhash, nm, + image + + sections[nlist[i].n_sect-1].offset + - sections[nlist[i].n_sect-1].addr + + nlist[i].n_value); + oc->symbols[curSymbol++] = nm; + } + } + + // insert local symbols into lochash + for(i=dsymLC->ilocalsym;iilocalsym+dsymLC->nlocalsym;i++) + { + if((nlist[i].n_type & N_TYPE) == N_SECT) + { + char *nm = image + symLC->stroff + nlist[i].n_un.n_strx; + ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, + image + + sections[nlist[i].n_sect-1].offset + - sections[nlist[i].n_sect-1].addr + + nlist[i].n_value); + } + } } - commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)"); commonCounter = (unsigned long)commonStorage; - for(i=0;insyms;i++) + if(symLC) { - if((nlist[i].n_type & N_TYPE) == N_UNDF - && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0)) - { - char *nm = image + symLC->stroff + nlist[i].n_un.n_strx; - unsigned long sz = nlist[i].n_value; + for(i=0;insyms;i++) + { + if((nlist[i].n_type & N_TYPE) == N_UNDF + && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0)) + { + char *nm = image + symLC->stroff + nlist[i].n_un.n_strx; + unsigned long sz = nlist[i].n_value; - nlist[i].n_value = commonCounter; + nlist[i].n_value = commonCounter; - ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter); - oc->symbols[curSymbol++] = nm; + ghciInsertStrHashTable(oc->fileName, symhash, nm, + (void*)commonCounter); + oc->symbols[curSymbol++] = nm; - commonCounter += sz; - } + commonCounter += sz; + } + } } return 1; } @@ -3583,7 +3749,6 @@ static int ocResolve_MachO(ObjectCode* oc) struct symtab_command *symLC = NULL; struct dysymtab_command *dsymLC = NULL; struct nlist *nlist; - unsigned long *indirectSyms; for(i=0;incmds;i++) { @@ -3597,7 +3762,8 @@ static int ocResolve_MachO(ObjectCode* oc) } sections = (struct section*) (segLC+1); - nlist = (struct nlist*) (image + symLC->symoff); + nlist = symLC ? (struct nlist*) (image + symLC->symoff) + : NULL; for(i=0;insects;i++) { @@ -3607,15 +3773,19 @@ static int ocResolve_MachO(ObjectCode* oc) nl_ptrs = §ions[i]; } - indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff); - - if(la_ptrs) - if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist)) - return 0; - if(nl_ptrs) - if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist)) - return 0; - + if(dsymLC) + { + unsigned long *indirectSyms + = (unsigned long*) (image + dsymLC->indirectsymoff); + + if(la_ptrs) + if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist)) + return 0; + if(nl_ptrs) + if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist)) + return 0; + } + for(i=0;insects;i++) { if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i])) @@ -3626,22 +3796,10 @@ static int ocResolve_MachO(ObjectCode* oc) freeHashTable(oc->lochash, NULL); oc->lochash = NULL; - /* - Flush the data & instruction caches. - Because the PPC has split data/instruction caches, we have to - do that whenever we modify code at runtime. - */ - { - int n = (oc->fileSize + islandSize * oc->n_islands) / 4; - unsigned long *p = (unsigned long*)oc->image; - while(n--) - { - __asm__ volatile ("dcbf 0,%0\n\tsync\n\ticbi 0,%0" - : : "r" (p)); - p++; - } - __asm__ volatile ("sync\n\tisync"); - } +#if defined (powerpc_TARGET_ARCH) + ocFlushInstructionCache( oc ); +#endif + return 1; }