Post by Nick Clifton
Post by H. J. Lu
This patch implements the .note.GNU-property section I proposed. It
works on Linux/ia32 and Linux/ia64.
I would prefer holding off applying this patch until we have a use for
it. Otherwise it would just bit rot away over time.
As you may know, the current gcc violates the ia64 psABI by not
preserving gp across function calls. The relocatable file created by
the current gcc may not be binary compatible with the relocatable
file generated by other ia64 psABI conforming compilers. The result
is we may get random run-time failure. We'd like to have a way to
detect such a condition. Here is one way to do that using the
Marking ia64 psABI conformance in relocatable files
On page 5-2 in Itanium Software Conventions and Runtime Architecture
Guide, there are
a. On entry to a procedure, gp is guaranteed valid for that procedure.
b. At any direct procedure call, gp must be valid (for the caller). This guarantees that an import stub (see Section 8.4.1) can access the linkage table.
c. Any procedure call (indirect or direct) may modify gp - unless the call is known to be local to the load module.
d. At procedure return, gp must be valid (for the returning procedure). This allows the compiler to optimize calls known to be local (i.e., the exceptions to Rule `c').
The effect of the rules is that gp must be treated as scratch register at the point of call (i.e., it must be saved by the caller), and it must be preserved from entry to exit.
The current gcc doesn't follow Rule `d' for external tail call and gp may not be valid when call returns. It isn't a problem for gcc since gcc doesn't optimize gp for calls known to be local according to the exceptions to Rule `c'. But it may be a serious problem when mixing relocatable files compiled by gcc and by other psABI conforming compilers.
Considering 2 relocatable files, one is compiled by gcc and has the definition of a procedure, foo, which has tail call to an external procedure, bar, the other is compiled by a psABI conforming compiler and references foo, the psABI conforming compiler may skip reloading gp across calls to foo if foo is marked local by symbol visibility. When those 2 files are linked together, the resulting output may have corrupted gp when calls to bar return if bar uses a different gp.
Static executables aren't affected by this since gp is constant. This problem only happens when relocatable files are mixed together to generate dynamic executables and shared libraries. We need a way to detect such cases. We use the optional GNU property note section, ".note.GNU-property ", for compiler to store ia64 psABI compliance information for relocatable files. The contents of this note section are:
data4 .L1 - .L0
data4 .L3 - .L2
stringz "GNU property"
The valid bit in psflags is
#define NT_GNU_PROPETRTY_IA_64_PRESERVE_GP (0x1 << 0)
If this bit is set, gp is preserved in procedure from entry to exit in this relocatable file.
If a linker supports the optional GNU property note section, it should follow the rules below when processing the relocatable input for generating relocatable file, dynamic executable or shared library:
1. Linker should detect function or no-type definition with non-default visibility in executable section if the note section is missing or the NT_GNU_PROPETRTY_IA_64_PRESERVE_GP bit is not set.
2. The note section should be removed from the output when generating executable or shared library.
3. The note section should be preserved to the output relocatable file only if all input relocatable files have the note section and the bit in psflags should be set only if it is set in all input relocatable files.