mbox series

[0/2] Implement single global definition

Message ID 20210620230544.391025-1-hjl.tools@gmail.com
Headers show
Series Implement single global definition | expand

Message

H.J. Lu June 20, 2021, 11:05 p.m. UTC
On systems with copy relocation:
* A copy in executable is created for the definition in a shared library
at run-time by ld.so.
* The copy is referenced by executable and shared libraries.
* Executable can access the copy directly.

Issues are:
* Overhead of a copy, time and space, may be visible at run-time.
* Read-only data in the shared library becomes read-write copy in
executable at run-time.
* Local access to data with the STV_PROTECTED visibility in the shared
library must use GOT.

On systems without function descriptor, function pointers vary depending
on where and how the functions are defined.
* If the function is defined in executable, it can be the address of
function body.
* If the function, including the function with STV_PROTECTED visibility,
is defined in the shared library, it can be the address of the PLT entry
in executable or shared library.

Issues are:
* The address of function body may not be used as its function pointer.
* ld.so needs to search loaded shared libraries for the function pointer
of the function with STV_PROTECTED visibility.

Here is a proposal to remove copy relocation and use canonical function
pointer:

1. Accesses, including in PIE and non-PIE, to undefined symbols must
use GOT.
  a. Linker may optimize out GOT access if the data is defined in PIE or
  non-PIE.
2. Read-only data in the shared library remain read-only at run-time
3. Address of global data with the STV_PROTECTED visibility in the shared
library is the address of data body.
  a. Can use IP-relative access.
  b. May need GOT without IP-relative access.
4. For systems without function descriptor,
  a. All global function pointers of undefined functions in PIE and
  non-PIE must use GOT.  Linker may optimize out GOT access if the
  function is defined in PIE or non-PIE.
  b. Function pointer of functions with the STV_PROTECTED visibility in
  executable and shared library is the address of function body.
   i. Can use IP-relative access.
   ii. May need GOT without IP-relative access.
   iii. Branches to undefined functions may use PLT.
5. Single global definition marker:

Add GNU_PROPERTY_1_NEEDED:

#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO

to indicate the needed properties by the object file.

Add GNU_PROPERTY_1_NEEDED_SINGLE_GLOBAL_DEFINITION:

#define GNU_PROPERTY_1_NEEDED_SINGLE_GLOBAL_DEFINITION (1U << 0)

to indicate that the object file requires canonical function pointers and
cannot be used with copy relocation.

  a. Copy relocation should be disallowed at link-time and run-time.
  b. Canonical function pointers are required at link-time and run-tima

Add a compiler option, -fsingle-global-definition:

1. Always to use GOT to access undefined symbols, including in PIE and
non-PIE.  This is safe to do and does not break the ABI.
2. In executable and shared library, for symbols with the STV_PROTECTED
visibility:
  a. The address of data symbol is the address of data body.
  b. For systems without function descriptor, the function pointer is
  the address of function body.
These break the ABI and resulting shared libraries may not be compatible
with executables which are not compiled with -fsingle-global-definition.
3. Generate a single global definition marker in relocatable objects.

H.J. Lu (2):
  Add -fsingle-global-definition
  Add TARGET_ASM_EMIT_GNU_PROPERTY_NOTE

 gcc/common.opt                            |  4 ++
 gcc/config.in                             |  6 +++
 gcc/config/i386/gnu-property.c            | 31 -------------
 gcc/config/i386/i386-protos.h             |  2 +-
 gcc/config/i386/i386.c                    | 52 ++++++++++++++++------
 gcc/configure                             | 42 ++++++++++++++++--
 gcc/configure.ac                          | 20 +++++++++
 gcc/doc/invoke.texi                       |  8 +++-
 gcc/doc/tm.texi                           |  5 +++
 gcc/doc/tm.texi.in                        |  2 +
 gcc/output.h                              |  2 +
 gcc/target.def                            |  8 ++++
 gcc/testsuite/g++.dg/pr35513-1.C          | 25 +++++++++++
 gcc/testsuite/g++.dg/pr35513-2.C          | 53 +++++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr35513-1.c | 16 +++++++
 gcc/testsuite/gcc.target/i386/pr35513-2.c | 15 +++++++
 gcc/testsuite/gcc.target/i386/pr35513-3.c | 15 +++++++
 gcc/testsuite/gcc.target/i386/pr35513-4.c | 15 +++++++
 gcc/testsuite/gcc.target/i386/pr35513-5.c | 15 +++++++
 gcc/testsuite/gcc.target/i386/pr35513-6.c | 14 ++++++
 gcc/testsuite/gcc.target/i386/pr35513-7.c | 15 +++++++
 gcc/testsuite/gcc.target/i386/pr35513-8.c | 41 ++++++++++++++++++
 gcc/toplev.c                              |  3 ++
 gcc/varasm.c                              | 47 ++++++++++++++++++++
 24 files changed, 406 insertions(+), 50 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr35513-1.C
 create mode 100644 gcc/testsuite/g++.dg/pr35513-2.C
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-3.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-4.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-5.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-6.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-7.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr35513-8.c