public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Exception handling tables for function generated on the fly
       [not found] <48A1B059.1040400@quarendon.net>
@ 2008-08-12 17:42 ` David Daney
  2008-08-12 17:58   ` Dave Korn
  0 siblings, 1 reply; 5+ messages in thread
From: David Daney @ 2008-08-12 17:42 UTC (permalink / raw)
  To: Tom Quarendon; +Cc: gcc, gcc help

Questions like this should probably go to gcc-help@gcc.gnu.org, but...

Tom Quarendon wrote:
> I'm porting some code that does a kind of JIT to translate a user script 
> into a dynamically created function for execution, but am having trouble 
> porting this to GCC and the way it implementes exceptions.
> 
> Lets say I've got
> int doPUT() {
> throw IOException;
> }
> 
> int doGET() {
> throw IOException
> }
> 
> and I want to magic up a function by writing (intel x86) instructions 
> into memory that does the same as if I'd done
> 
> int magic() {
> doPUT();
> doGET();
> return 0;
> }
> 

You don't say how you get them into memory.  Are you building a shared library and then loading it with dlopen()?


> I then want to call my magic function as in
> 
> int main() {
> // magic up my function in memory containing calls to doGET and doPUT.
> try {
> // call my magic'd function
> }
> catch (IOException) {
> // Report the exception
> }
> }
> 
> 
> If I do this I get std::terminate called from __cxa_throw. Researching 
> this it seems that I somehow need to register some exception handling 
> tables to correspond to the "magic" function to enable the exception 
> handler to allow the exception to propagate through.
> 
> I'd welcome any pointers to where I might be able to get some 
> information on this. I've looked at the C++ ABI documentation which 
> helps a bit, and I've found some information on the format that the 
> tables need to be in (and indeed I've looked at the assembler generated 
> by the gcc compiler if I code up "magic" and compile it directly), but I 
> don't yet see quite how to put it all together.
> 

If you pass -funwind-tables to gcc it will generate the necessary unwinding information.  If you put the code in a shared library and dlopen() it it should just work.

If you are loading the code some other way, then you may have to call some of the __register_frame* family of functions (in libgcc) passing pointers to the appropriate .eh_frame sections of the generated code.


> I imagine that GCJ has do to this ind of thing?
> 

g++ as well.

David Daney

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Exception handling tables for function generated on the fly
  2008-08-12 17:42 ` Exception handling tables for function generated on the fly David Daney
@ 2008-08-12 17:58   ` Dave Korn
  2008-08-12 18:06     ` David Daney
  0 siblings, 1 reply; 5+ messages in thread
From: Dave Korn @ 2008-08-12 17:58 UTC (permalink / raw)
  To: 'David Daney', 'Tom Quarendon'; +Cc: gcc, 'gcc help'

David Daney wrote on 12 August 2008 18:19:

> Questions like this should probably go to gcc-help@gcc.gnu.org

  Questions about deep compiler internals and EH abis?  Seems a bit intense
for the where's-the-any-key list to me...

>> I'm porting some code that does a kind of JIT 

> You don't say how you get them into memory.  Are you building a shared
> library and then loading it with dlopen()? 

  "JIT" normally implies poking opcodes straight into memory at runtime.
Otherwise it wouldn't be Just-In-Time compilation, it would be
Way-Ages-In-Advance compilation!

> If you pass -funwind-tables to gcc it will generate the necessary
> unwinding information.  If you put the code in a shared library and
> dlopen() it it should just work.  

  No compiler.  No library.  No dlopen.
 
> If you are loading the code some other way

  http://en.wikipedia.org/wiki/Just-in-time_compilation

> then you may have to call
> some of the __register_frame* family of functions (in libgcc) passing
> pointers to the appropriate .eh_frame sections of the generated code.  

  Yes.  The OP's question is "How do I generate .eh_frame data at runtime
for an arbitrary function that has no throws and no catches but may call
functions that throw".

  Tom, did you read the Dwarf spec?  That should have more info than just
the abi docs.


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Exception handling tables for function generated on the fly
  2008-08-12 17:58   ` Dave Korn
@ 2008-08-12 18:06     ` David Daney
  2008-08-12 18:08       ` David Daney
  2008-08-12 18:41       ` Dave Korn
  0 siblings, 2 replies; 5+ messages in thread
From: David Daney @ 2008-08-12 18:06 UTC (permalink / raw)
  To: Dave Korn; +Cc: 'Tom Quarendon', gcc, 'gcc help'

Dave Korn wrote:
> David Daney wrote on 12 August 2008 18:19:
> 
>> Questions like this should probably go to gcc-help@gcc.gnu.org
> 
>   Questions about deep compiler internals and EH abis?  Seems a bit intense
> for the where's-the-any-key list to me...
> 

gcc@ is for questions about development of GCC.

gcc-help@ is for everything else.
.
.
.

>> If you pass -funwind-tables to gcc it will generate the necessary
>> unwinding information. 
.
.
.
>   Yes.  The OP's question is "How do I generate .eh_frame data at runtime
> for an arbitrary function that has no throws and no catches but may call
> functions that throw".
> 

Which is why I recommended passing -funwind-tables, which does exactly that.

David Daney

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Exception handling tables for function generated on the fly
  2008-08-12 18:06     ` David Daney
@ 2008-08-12 18:08       ` David Daney
  2008-08-12 18:41       ` Dave Korn
  1 sibling, 0 replies; 5+ messages in thread
From: David Daney @ 2008-08-12 18:08 UTC (permalink / raw)
  To: Dave Korn; +Cc: 'Tom Quarendon', 'gcc help'

David Daney wrote:
> Dave Korn wrote:
>
>>   Yes.  The OP's question is "How do I generate .eh_frame data at runtime
>> for an arbitrary function that has no throws and no catches but may call
>> functions that throw".
>>
> 

Well I guess I may have misunderstood.  If you are not using gcc to generate the code, then indeed you do need to generate your own .eh_frame section as Dave Korn indicated.

David Daney

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Exception handling tables for function generated on the fly
  2008-08-12 18:06     ` David Daney
  2008-08-12 18:08       ` David Daney
@ 2008-08-12 18:41       ` Dave Korn
  1 sibling, 0 replies; 5+ messages in thread
From: Dave Korn @ 2008-08-12 18:41 UTC (permalink / raw)
  To: 'David Daney'; +Cc: 'Tom Quarendon', gcc, 'gcc help'

David Daney wrote on 12 August 2008 18:58:

>>   Yes.  The OP's question is "How do I generate .eh_frame data at runtime
>> for an arbitrary function that has no throws and no catches but may call
>> functions that throw". 
>> 
> 
> Which is why I recommended passing -funwind-tables, which does exactly
> that. 

  Passing it *to what*?  There is no compiler involved: from the OP:

> I want to magic up a function by writing (intel x86) instructions
> into memory that does the same as if I'd done

  You appear to keep missing the "JIT" aspect of this question.  JIT is not
usually done by writing a C file and invoking the compiler using system()
TTBOMK.

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-08-12 18:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <48A1B059.1040400@quarendon.net>
2008-08-12 17:42 ` Exception handling tables for function generated on the fly David Daney
2008-08-12 17:58   ` Dave Korn
2008-08-12 18:06     ` David Daney
2008-08-12 18:08       ` David Daney
2008-08-12 18:41       ` Dave Korn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).