From 5544862ebedb02e75b6b333c2d44ad9eb51ea36e Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 22 Aug 2021 16:29:16 -0700 Subject: elf: Avoid nested functions in the loader (aarch64, x86-64) [BZ #27220] dynamic-link.h is included more than once in some elf/ files (rtld.c, dl-conflict.c, dl-reloc.c, dl-reloc-static-pie.c) and uses GCC nested functions. This harms readability and the nested functions usage is the biggest obstacle prevents CC=clang (which doesn't support the feature). To un-nest elf_machine_rela, the key idea is to pass the variable in the containing scope as an extra argument. Stan Shebs implemented this in the google/grte/v5-2.27/master branch. This patch squashes and cleans up the commits. This patch just fixes aarch64 and x86-64: they use the `#ifndef NESTING` branch to avoid nested functions. The `#ifdef NESTING` branch is used by all other ports whose dl-machine.h files haven't been migrated. For the time being, there is some duplicated code. `#ifdef NESTING` dispatches can be removed in the future when all arches are migrated. I can fix powerpc64, riscv, and other arches subsequently. Migrating all ports at once is just too risky. Also, appreciate help from arch maintainers. Tested on {aarch64,x86_64}-linux-gnu (!NESTING) and powerpc64le-linux-gnu (NESTING). --- elf/dl-reloc-static-pie.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'elf/dl-reloc-static-pie.c') diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index d5bd2f31e9..0ab4613021 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -23,6 +23,13 @@ #include #include "dynamic-link.h" +#ifndef NESTING +# define STATIC_PIE_BOOTSTRAP +# define BOOTSTRAP_MAP (main_map) +# define RESOLVE_MAP(sym, version, flags) BOOTSTRAP_MAP +# include "dynamic-link.h" +#endif /* n NESTING */ + /* Relocate static executable with PIE. */ void @@ -30,10 +37,12 @@ _dl_relocate_static_pie (void) { struct link_map *main_map = _dl_get_dl_main_map (); +#ifdef NESTING # define STATIC_PIE_BOOTSTRAP # define BOOTSTRAP_MAP (main_map) # define RESOLVE_MAP(sym, version, flags) BOOTSTRAP_MAP # include "dynamic-link.h" +#endif /* NESTING */ /* Figure out the run-time load address of static PIE. */ main_map->l_addr = elf_machine_load_address (); @@ -48,7 +57,11 @@ _dl_relocate_static_pie (void) /* Relocate ourselves so we can do normal function calls and data access using the global offset table. */ - ELF_DYNAMIC_RELOCATE (main_map, 0, 0, 0); + ELF_DYNAMIC_RELOCATE (main_map, 0, 0, 0 +#ifndef NESTING + , main_map +#endif + ); main_map->l_relocated = 1; /* Initialize _r_debug. */ -- cgit v1.2.1