Re: Modifying GCC's emitted assembly code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Nov 20, 2011, at 1:55 AM, Ian Lance Taylor wrote:

> Amittai Aviram <amittai.aviram@xxxxxxxx> writes:
> 
>> I have been creating a modified version of GCC's libgomp library to support a special version of OpenMP.  In order for it to work properly, at the end of the main function in an OpenMP program, I have to insert a call to a special clean-up function (say, "clean_up()"), just before the "return" statement.  (I define my clean_up function in my version of libgomp.)  I would like to modify GCC so that it inserts the call to clean_u automatically, just before the code representing the return statement, such as the RET instruction in x86 assembly code.  How might I go about this?
>> 
>> Alternatively, it might work if GCC inserted the function call into the boilerplate code of __exit.  How might I enable GCC to do that?
> 
> Would it work to just use atexit?  If that works, perhaps you could just
> add a global constructor which calls atexit with your cleanup function.
> 
> Ian


Thanks a lot, Ian!  That works!  However, I had to do a bit of extra hacking.  At first, for some reason, my clean-up function was being called twice.  Since it did some clean-up operations that depended on data structures in a mapped file, and then unmapped the file afterwards, the second call was causing a segmentation fault (dereferencing a null pointer).  I got around this problem through a simple hack:  my library now has a boolean variable set to false on startup; the clean-up function checks the value and returns if the variable is true; and it sets it to true at the end, before returning.  However, I am still wondering why the code gets run twice, requiring this hack.

This is a multiprocess program, where I fork a bunch of child processes in a "start" function and I join them back in the clean-up function.  The "start" function calls atexit to register the clean-up function--after it forks the child processes.  I see no reason why the child processes would ever register the clean-up function themselves, since they exit before they can come back into the "start" code that has the "atexit" statement.  However, just for good measure, I also test for the process ID; if it's not 0, the clean-up function returns without doing anything.  But this did not prevent the clean-up code from running twice.

Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram@xxxxxxxx
http://www.amittai.com




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux