+/* --------------------------------------------------------------------------
+ * PowerPC specifics (jump islands)
+ * ------------------------------------------------------------------------*/
+
+#if defined(powerpc_HOST_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 )
+{
+#ifdef USE_MMAP
+ int pagesize, n, m;
+#endif
+ int aligned;
+ int misalignment = 0;
+#if darwin_HOST_OS
+ misalignment = oc->misalignment;
+#endif
+
+ if( count > 0 )
+ {
+ // round up to the nearest 4
+ aligned = (oc->fileSize + 3) & ~3;
+
+#ifdef USE_MMAP
+ #ifndef linux_HOST_OS /* mremap is a linux extension */
+ #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
+ #endif
+
+ pagesize = getpagesize();
+ n = ROUND_UP( oc->fileSize, pagesize );
+ m = ROUND_UP( aligned + sizeof (ppcJumpIsland) * count, pagesize );
+
+ /* The effect of this mremap() call is only the ensure that we have
+ * a sufficient number of virtually contiguous pages. As returned from
+ * mremap, the pages past the end of the file are not backed. We give
+ * them a backing by using MAP_FIXED to map in anonymous pages.
+ */
+ if( (oc->image = mremap( oc->image, n, m, MREMAP_MAYMOVE )) == MAP_FAILED )
+ {
+ errorBelch( "Unable to mremap for Jump Islands\n" );
+ return 0;
+ }
+
+ if( mmap( oc->image + n, m - n, PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0 ) == MAP_FAILED )
+ {
+ errorBelch( "Unable to mmap( MAP_FIXED ) for Jump Islands\n" );
+ return 0;
+ }
+
+#else
+ oc->image -= misalignment;
+ oc->image = stgReallocBytes( oc->image,
+ misalignment +
+ aligned + sizeof (ppcJumpIsland) * count,
+ "ocAllocateJumpIslands" );
+ oc->image += misalignment;
+#endif /* USE_MMAP */
+
+ oc->jump_islands = (ppcJumpIsland *) (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