diff mbox series

[v2] elf: Rewrite long RESOLVE_MAP macro to a debug friendly function

Message ID a7db26e408d2ae707329e699fe29061cfb6d75df.camel@guriev.su
State New
Headers show
Series [v2] elf: Rewrite long RESOLVE_MAP macro to a debug friendly function | expand

Commit Message

Nicholas Guriev May 3, 2022, 7:14 p.m. UTC
On Пн, 2022-05-02 at 14:20 -0700, Fangrui Song wrote:
> Use `lookup_t _lr = ` ?
I was going to leave most of the formatting as it was, including local
variables definitions. But here is a version with joined lr declaration
and initialization. The line became too long, and to fit 79-symbol limit
I put another variable undef_name. I also removed the useless underscore
in the variable names.
-- >8 --

A static function that may be inlined is way better to find where exactly a
crash happens. So one can step into the function with GDB. The macro still
remains for compatibility with other code.

Signed-off-by: Nicholas Guriev <nicholas@guriev.su>
---
 elf/dl-reloc.c | 56 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 34 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c
index 771a34bd14..106d458f8b 100644
--- a/elf/dl-reloc.c
+++ b/elf/dl-reloc.c
@@ -162,29 +162,41 @@  _dl_nothread_init_static_tls (struct link_map *map)
 }
 #endif /* !PTHREAD_IN_LIBC */
 
+static lookup_t
+_dl_resolve_map (lookup_t l, struct r_scope_elem *scope[], const ElfW(Sym) **ref,
+		 const struct r_found_version *version, unsigned long int r_type)
+{
+  if (ELFW(ST_BIND) ((*ref)->st_info) == STB_LOCAL
+      || __glibc_unlikely (dl_symbol_visibility_binds_local_p (*ref)))
+    return l;
+
+  if (__glibc_unlikely (*ref == l->l_lookup_cache.sym)
+      && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class)
+    {
+      bump_num_cache_relocations ();
+      *ref = l->l_lookup_cache.ret;
+    }
+  else
+    {
+      int tc = elf_machine_type_class (r_type);
+      l->l_lookup_cache.type_class = tc;
+      l->l_lookup_cache.sym = *ref;
+      const char *undef_name
+	= (const char *) D_PTR (l, l_info[DT_STRTAB]) + (*ref)->st_name;
+      const struct r_found_version *v = NULL;
+      if (version != NULL && version->hash != 0)
+	v = version;
+      lookup_t lr = _dl_lookup_symbol_x (undef_name, l, ref, scope, v, tc,
+					 DL_LOOKUP_ADD_DEPENDENCY
+					 | DL_LOOKUP_FOR_RELOCATE, NULL);
+      l->l_lookup_cache.ret = *ref;
+      l->l_lookup_cache.value = lr;
+    }
+  return l->l_lookup_cache.value;
+}
+
 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code.  */
-#define RESOLVE_MAP(l, scope, ref, version, r_type)			      \
-    ((ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL			      \
-      && __glibc_likely (!dl_symbol_visibility_binds_local_p (*ref)))	      \
-     ? ((__glibc_unlikely ((*ref) == l->l_lookup_cache.sym)		      \
-	 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class)  \
-	? (bump_num_cache_relocations (),				      \
-	   (*ref) = l->l_lookup_cache.ret,				      \
-	   l->l_lookup_cache.value)					      \
-	: ({ lookup_t _lr;						      \
-	     int _tc = elf_machine_type_class (r_type);			      \
-	     l->l_lookup_cache.type_class = _tc;			      \
-	     l->l_lookup_cache.sym = (*ref);				      \
-	     const struct r_found_version *v = NULL;			      \
-	     if ((version) != NULL && (version)->hash != 0)		      \
-	       v = (version);						      \
-	     _lr = _dl_lookup_symbol_x ((const char *) D_PTR (l, l_info[DT_STRTAB]) + (*ref)->st_name, \
-					l, (ref), scope, v, _tc,	      \
-					DL_LOOKUP_ADD_DEPENDENCY	      \
-					| DL_LOOKUP_FOR_RELOCATE, NULL);      \
-	     l->l_lookup_cache.ret = (*ref);				      \
-	     l->l_lookup_cache.value = _lr; }))				      \
-     : l)
+#define RESOLVE_MAP _dl_resolve_map
 
 #include "dynamic-link.h"