]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD: tools: unbreak resolve_sym_name() on non-GNU platforms
authorWilly Tarreau <w@1wt.eu>
Wed, 4 Mar 2020 09:19:36 +0000 (10:19 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 4 Mar 2020 11:04:07 +0000 (12:04 +0100)
resolve_sym_name() doesn't build when USE_DL is set on non-GNU platforms
because "Elf(W)" isn't defined. Since it's only used for dladdr1(), let's
refactor all this so that we can completely ifdef out that part on other
platforms. Now we have a separate function to perform the call depending
on the platform and it only returns the size when available.

src/standard.c

index 5c4daf2b5db9b7ac9fefab9221bc9be868afe1fe..78237b9102dbcaa4ce64a9d19b8c6959ba4c8e9c 100644 (file)
@@ -4335,6 +4335,27 @@ void debug_hexdump(FILE *out, const char *pfx, const char *buf,
        }
 }
 
+#ifdef USE_DL
+/* calls dladdr() or dladdr1() on <addr> and <dli>. If dladdr1 is available,
+ * also returns the symbol size in <size>, otherwise returns 0 there.
+ */
+static int dladdr_and_size(const void *addr, Dl_info *dli, size_t *size)
+{
+       int ret;
+#ifdef __USE_GNU // most detailed one
+       const ElfW(Sym) *sym;
+
+       ret = dladdr1(addr, dli, (void **)&sym, RTLD_DL_SYMENT);
+       if (ret)
+               *size = sym ? sym->st_size : 0;
+#else
+       ret = dladdr(addr, dli);
+       *size = 0;
+#endif
+       return ret;
+}
+#endif
+
 /* Tries to append to buffer <buf> some indications about the symbol at address
  * <addr> using the following form:
  *   lib:+0xoffset              (unresolvable address from lib's base)
@@ -4378,7 +4399,7 @@ void *resolve_sym_name(struct buffer *buf, const char *pfx, void *addr)
 
 #ifdef USE_DL
        Dl_info dli, dli_main;
-       const ElfW(Sym) *sym;
+       size_t size;
        const char *fname, *p;
 #endif
        int i;
@@ -4395,14 +4416,8 @@ void *resolve_sym_name(struct buffer *buf, const char *pfx, void *addr)
 
 #ifdef USE_DL
        /* Now let's try to be smarter */
-#ifdef __USE_GNU // most detailed one
-       if (!dladdr1(addr, &dli, (void **)&sym, RTLD_DL_SYMENT))
-               goto unknown;
-#else
-       if (!dladdr(addr, &dli))
+       if (!dladdr_and_size(addr, &dli, &size))
                goto unknown;
-       sym = NULL;
-#endif
 
        /* 1. prefix the library name if it's not the same object as the one
         * that contains the main function. The name is picked between last '/'
@@ -4429,8 +4444,8 @@ void *resolve_sym_name(struct buffer *buf, const char *pfx, void *addr)
                chunk_appendf(buf, "%s", dli.dli_sname);
                if (addr != dli.dli_saddr) {
                        chunk_appendf(buf, "+%#lx", (long)(addr - dli.dli_saddr));
-                       if (sym)
-                               chunk_appendf(buf, "/%#lx", (long)sym->st_size);
+                       if (size)
+                               chunk_appendf(buf, "/%#lx", (long)size);
                }
                return dli.dli_saddr;
        }