Re: Porting to Darwin

talksmall <[email protected]> Wed, 18 Jun 2008 12:31:19 -0700 (PDT)
Newsgroups gmane.comp.lang.smalltalk.strongtalk
Message-ID <834d0937-3fff-4233-8d1c-7521a0e69398@z66g2000hsc.googlegroups.com>


On Jun 18, 2:07 am, Brian de Alwis <[email protected]> wrote:
> Hi Steve.  You're right -- I was using the wrong image.  Using your
> previously-provided image for Unix with the VM compiled with -
> mstackalign, running with +TraceLookup, confirms that I'm definitely
> running lots of ST code.  If I run with '-script ...' I'll eventually
> break into the built-in debugger as the unix image fails when it tries
> to load libc.so.6.  (Perhaps we should change the ST code to catch the
> DLL lookup failure and log the failure instead?)

Actually, a failure to find a shared library (DLL or .so) results in
an assertion failure being logged in the console. So does a failure to
find a named function within a library. The problem here is that we
are very early in the image start up code, and if it can't load libc
(or equivalent) then if can't open the test script, and since there is
no UI yet for non-Windows it can't do diddly! One issue is that the
Foreign Function interface in Smalltalk (see this page on the Wiki -
http://code.google.com/p/strongtalk/wiki/ForeignFunctions ) doesn't
provide a Smalltalk fail block for DLL lookup failures. It just
assumes that the DLL is available. If the DLL is missing, it is
essentially a pretty major error. Going forward we should probably add
some generic failure code to throw an appropriate Smalltalk exception.
We can then add an exception handler to handle this in the normal way,
when it makes sense.

>
> Linking Darwin's equivalent to that name (/usr/lib/libSystem.B.dylib)
> causes a bus error and with rampant corruption of the stack.  I
> suspect this is occurring as the stack isn't aligned, and my stack-
> alignment patch above doesn't seem to do the right thing.  I'll try
> poking at this some more tonight.
>
> Brian.

The -mstackrealign flag is fine for the code being compiled by GCC,
but it won't do anything for code not compiled with that setting. -
mstackrealign alters the prologue and epilogue of the functions
compiled by the compiler to align the arguments appropriately. Since,
presumably, your library has not been compiled with these options it
won't do the realignment which results in the failure.

To fix this we need to update the code that calls the DLL to ensure
that the arguments are aligned. To that end I've created the following
patch to the DLL call routine to align the stack. Can you apply it and
try it out? Since the resulting code still appears to work in Windows
(I tried it without any issues), if this works for you we may as well
keep it in the mainline code. Note that I have commented out the
pushing of ebx in line with Hyungjip's comments re position
independent code.

Let me know how you get on.

Regards, Steve

Index: stubRoutines.cpp
===================================================================
--- stubRoutines.cpp	(revision 139)
+++ stubRoutines.cpp	(working copy)
@@ -509,6 +509,14 @@
   //slr mod: push a fake stack frame to support cdecl calls
   masm->enter();
   //slr mod end
+  // following is to allow 16-byte stack alignment for Darwin (OSX)
+  masm->movl(eax, ebx);
+  masm->negl(eax);
+  masm->leal(eax, Address(esp, eax, Address::times_4)); // esp - 4 x
nargs
+  masm->andl(eax, 0xf); // padding required for 16-byte alignment
+  masm->subl(esp, eax); // align stack
+  // end stack alignment mod
+
   masm->testl(ebx, ebx);			// if number of arguments != 0 then
   masm->jcc(MacroAssembler::notZero, loop_entry);// convert arguments

@@ -823,6 +831,7 @@

   masm->pushl(edi);	// save registers for C calling convetion
   masm->pushl(esi);
+  //masm->pushl(ebx);

   // reset last Delta frame
   masm->reset_last_Delta_frame();
@@ -863,6 +872,7 @@

  masm->bind(_return);
   masm->leal(esp, Address(ebp, -4*oopSize));
+  //masm->popl(ebx);
   masm->popl(esi);	// restore registers for C calling convetion
   masm->popl(edi);
   masm->popl(Address((int)&last_Delta_sp,
relocInfo::external_word_type)); // reset _last_Delta_sp

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Strongtalk-general" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/strongtalk-general?hl=en
-~----------~----~----~----~------~----~------~--~---