Dealing with TOC overflow: the traditional approach
rauls 1200005VCY Comments (8) Visits (14446)
In a previous posting (TOC Overflow: what is it, and why should you care?) I discussed the TOC overflow condition, which is common when developing large commercial applications on AIX.
These are some of the common solutions to this problem:
1. Change your application The most obvious solution to this problem is to change the application to reduce the number of global symbols being defined. One way to do this is by grouping some global variables into a global structure, or changing them from external to static. This can be time-consuming and error-prone, so it is not a realistic option on most large projects.
2. -bbigtoc The most frequently-used solution for TOC overflow is the bigtoc linker option. This instructs the linker to generate a TOC larger than 64K.
The problem with -bbigtoc is that TOC entries in the overflow area (outside the initial 64K) cannot be accessed with a single load based on the TOC pointer, which introduces a nontrivial performance penalty. Each access to the TOC overflow area requires a branch to offline code that adjusts the base pointer and then performs the load. Because this code is generated during link processing, it cannot be properly scheduled or optimized with the rest of the program.
To make matters worse, the programmer does not have control over which variables are located in the TOC overflow area, so there is no way to ensure that frequently accessed variables will stay out of the TOC overflow area.
On my next post on this series I'll describe some mechanisms the XLC compilers provide to deal with this issue.
PS: In order to experiment with TOC overflow, we need a program with many global variables. Attached is a shell script called TOCO
Try it with the value 16384 (or 8192 on 64-bit mode), and the program will reach TOC overflow and fail to link.
Do you find this topic interesting, or do you have additional questions? Please add comments to this post describing where you've seen TOC overflow and how do you currently deal with it.