[RFC PATCH v2 00/10] nolibc: Add static-pie support

Daniel Palmer posted 10 patches 2 days, 18 hours ago
include/uapi/linux/elf-r.h                    |  27 +++
include/uapi/linux/elf.h                      |   1 +
tools/include/nolibc/Makefile                 |   1 +
tools/include/nolibc/arch-arm.h               |  25 ++
tools/include/nolibc/arch-m68k.h              |  27 ++-
tools/include/nolibc/arch-powerpc.h           |  24 ++
tools/include/nolibc/arch-riscv.h             |  26 ++
tools/include/nolibc/arch-sh.h                |  24 ++
tools/include/nolibc/arch-x86.h               |  53 +++++
tools/include/nolibc/compiler.h               |   6 +
tools/include/nolibc/crt.h                    |  62 +++--
tools/include/nolibc/reloc.h                  | 224 ++++++++++++++++++
tools/testing/selftests/nolibc/Makefile       |   2 +-
.../testing/selftests/nolibc/Makefile.nolibc  |   8 +-
tools/testing/selftests/nolibc/run-tests.sh   |  10 +-
15 files changed, 489 insertions(+), 31 deletions(-)
create mode 100644 include/uapi/linux/elf-r.h
create mode 100644 tools/include/nolibc/reloc.h
[RFC PATCH v2 00/10] nolibc: Add static-pie support
Posted by Daniel Palmer 2 days, 18 hours ago
v2:

- This is still RFC quality
- I have gotten a few more archs to work (not crash in nolibc-test) so I have added them
  to show the that the arch specific parts are pretty small.
- This should now only add this extra code if NOLIBC_WANT_RELOC is defined, which happens
  automatically if you compile with -fpie. Should address Willy's concern with the size
  blowing up for even binaries that don't need this.

  Size difference on ppc64 is massive, I guess PIC code is big there.
  
  static:
  
  $ file nolibc-test
nolibc-test: ELF 64-bit MSB executable, 64-bit PowerPC or cisco 7500, Power ELF V1 ABI, version 1 (SYSV), statically linked, not stripped
$ size nolibc-test
   text    data     bss     dec     hex filename
  59560    3240     104   62904    f5b8 nolibc-test

  static pie:
  
  $ file nolibc-test
nolibc-test: ELF 64-bit MSB pie executable, 64-bit PowerPC or cisco 7500, Power ELF V1 ABI, version 1 (SYSV), static-pie linked, with debug_info, not stripped
  $ size nolibc-test
   text    data     bss     dec     hex filename
  67801    3656     104   71561   11789 nolibc-test
  
- Each of the archs checks that the relocations they need (just the one at the moment) is defined
  before defining the relocation defines. So this shouldn't break with older UAPI headers.
  
- I think maybe you might want to set NOLIBC_WANT_RELOC even if __pie__ is not defined,
  maybe for testing? So I didn't make it _NOLIBC_WANT_RELOC.
  
- I was worried some archs didn't work because calling the relocation functions was
  causing things like accessing the stack canary before it was accessible. So I added
  __inline__ to all of the relocation functions to force them to get inlined into _start_c().
  This might be too much. checkpatch certainly hates it.

- I think I addressed most of Thomas' code style comments, prefixing etc. Maybe added some new problems :)

- I cleaned up the REL/RELA differences a bit more so the lines of code has dropped a
  bit in reloc.h.

- I fixed working out where the dynamic section is, this fixed SuperH and PPC. I suspect 
  I can get all of the others except maybe mips working now.

- new elf UAPI header for the relocation types is still wrong, script to run the
  self-tests is still wonky.
  
Thanks in advance,

Daniel

v1: https://lore.kernel.org/lkml/20260131074440.732588-1-daniel@thingy.jp/
v0: https://lore.kernel.org/lkml/20260116122812.2421621-1-daniel@thingy.jp/

Daniel Palmer (10):
  elf: Add relocation types used by nolibc
  tools/nolibc: crt: Split _start_c() into stack-only and normal parts
  tools/nolibc: Add basic ELF self-relocation support for static PIE
  tools/nolibc: m68k: Add relocation support
  tools/nolibc: x86: Add relocation support
  tools/nolibc: riscv: Add relocation support
  tools/nolibc: arm: Add relocation support
  tools/nolibc: sh: Add relocation support
  tools/nolibc: ppc: Add relocation support
  selftests/nolibc: Add option for building with -static-pie

 include/uapi/linux/elf-r.h                    |  27 +++
 include/uapi/linux/elf.h                      |   1 +
 tools/include/nolibc/Makefile                 |   1 +
 tools/include/nolibc/arch-arm.h               |  25 ++
 tools/include/nolibc/arch-m68k.h              |  27 ++-
 tools/include/nolibc/arch-powerpc.h           |  24 ++
 tools/include/nolibc/arch-riscv.h             |  26 ++
 tools/include/nolibc/arch-sh.h                |  24 ++
 tools/include/nolibc/arch-x86.h               |  53 +++++
 tools/include/nolibc/compiler.h               |   6 +
 tools/include/nolibc/crt.h                    |  62 +++--
 tools/include/nolibc/reloc.h                  | 224 ++++++++++++++++++
 tools/testing/selftests/nolibc/Makefile       |   2 +-
 .../testing/selftests/nolibc/Makefile.nolibc  |   8 +-
 tools/testing/selftests/nolibc/run-tests.sh   |  10 +-
 15 files changed, 489 insertions(+), 31 deletions(-)
 create mode 100644 include/uapi/linux/elf-r.h
 create mode 100644 tools/include/nolibc/reloc.h

-- 
2.51.0