Sunday, 8 January 2017

What is the "Table of Contents"?


According to the PowerPC runtime definition, all code needs to be 1) position independent, and 2) read-only. In other words, the run-time loader must be able to put a piece of code anywhere in memory and execute it, and the loader may not modify the code in any way while doing this. These conditions could yield a contradiction, since a piece of code can't know where it or any other code will be in memory, yet the loader isn't allowed to fill in jump addresses or modify the code in any other ways. The PowerPC instruction set solves part of this problem by making most branches PC-relative. (A PC-relative branch says "jump forwards (or backwards) n instructions from the current location.)
PC-relative branches allow code to run anyplace in memory, but they have drawbacks. One problem is that they only work within the current piece of code: since the current code doesn't know where the system libraries will be in memory, it can't use PC-relative branches to get there. Another drawback is that PC-relative branches have a limited range on the PowerPC: +/-8M instructions (+/-32MB) for unconditional branches, and +/-8K instructions (+/-32KB) for conditional branches.
Another problem involves locating data: since the code doesn't know where it will be loaded into memory, it doesn't know where its global variables will be stored. The code must be told how to locate its own variables in memory.


The PowerPC runtime architecture solves this problem by creating a Table Of Contents (TOC) for each fragment. (A fragment is a piece of executable code: an application, import library, or other collection of functions.) The Table of Contents is a collection of pointers that the code uses to locate its static data (including global variables), pointers to external functions, and imported globals from other fragments.
By the way, "Table Of Contents" is a somewhat misleading name, as it makes people think that a fragment's TOC describes the contents of that fragment. A better metaphor might be a "bibliography" or an "address book", as the TOC lists the globals and external functions that a fragment uses, not the fragment's contents. Also, a TOC's contents is private to its owner --- no code is expected to look in or modify another routine's TOC.


Some TOC facts

The Table of Contents is embedded in the fragment's static data area. GPR 2, also known as RTOC, points to the TOC of the currently running routine. Each fragment is allowed to have multiple TOCs --- up to 1 TOC per routine.



Reference:
https://imcs.dvfu.ru/lib.int/docs/Hardware/intro_to_ppc/ppc4_runtime4.html

No comments:

Post a Comment