Skip to contentSkip to conversation

libdtrace: search both ELF symbol tables for module symbols

PATCH In review Kris Van Hees 2 participants 6 messages Oct 30 – Nov 7

lore
Share & tools
lore
#0 lore raw

PATCH 15/20 libdtrace: search both ELF symbol tables for module symbols

Keep independent symbol and address indices for SHT_SYMTAB and SHT_DYNSYM, loading strings through each table’s sh_link. Search .symtab before .dynsym and include dynamic-only names in module symbol iteration.

Preserve .symtab indices for CTF, place dynamic and external symbols in separate ID ranges, and use .dynsym for CTF when .symtab is absent. Allow offline kernel modules with known address ranges to use ELF address lookup while retaining runtime symbol lookup for loaded kernel modules.

Add synthetic 32-bit and 64-bit ELF tests for missing and individual tables, duplicate-name precedence, distinct linked string tables, public name and address lookup, symbol IDs, and iteration for user and offline kernel modules.

---
 libdtrace/dt_impl.h               |  41 +++--
 libdtrace/dt_module.c             | 281 +++++++++++++++++++-----------
 libdtrace/dt_symbol_modops.h      |  16 +-
 test/internals/elf/tst.symbols.sh |  10 ++
 test/triggers/Build               |   8 +-
 test/triggers/elf-symbols.c       | 203 +++++++++++++++++++++
 test/triggers/shared-ctf.c        |   6 +
 7 files changed, 433 insertions(+), 132 deletions(-)
 create mode 100755 test/internals/elf/tst.symbols.sh
 create mode 100644 test/triggers/elf-symbols.c
libdtrace/dt_impl.h +22 -19
index c83fedb2..adb20884 100644
@@ -115,12 +115,14 @@ typedef struct dt_intdesc {
 	uintmax_t did_limit;	/* maximum positive value held by type */
 } dt_intdesc_t;
 
+struct dt_module_symbols;
+
 typedef struct dt_modops {
-	uint_t (*do_syminit)(struct dt_module *);
-	void (*do_symsort)(struct dt_module *);
-	GElf_Sym *(*do_symname)(struct dt_module *,
+	uint_t (*do_syminit)(struct dt_module_symbols *);
+	void (*do_symsort)(struct dt_module_symbols *);
+	GElf_Sym *(*do_symname)(const struct dt_module_symbols *,
 	    const char *, GElf_Sym *, uint_t *);
-	GElf_Sym *(*do_symaddr)(struct dt_module *,
+	GElf_Sym *(*do_symaddr)(const struct dt_module_symbols *,
 	    GElf_Addr, GElf_Sym *, uint_t *);
 } dt_modops_t;
 
@@ -138,6 +140,20 @@ typedef struct dt_modsym {
 	uint_t dms_next;	/* index of next element in hash chain */
 } dt_modsym_t;
 
+/* Independent ELF symbol tables: preserve indices used by CTF. */
+typedef struct dt_module_symbols {
+	ctf_sect_t dm_symtab;
+	ctf_sect_t dm_strtab;
+	uint_t *dm_symbuckets;	/* symbol table hash buckets (chain indices) */
+	dt_modsym_t *dm_symchains; /* symbol table hash chains buffer */
+	void *dm_asmap;		/* symbol pointers sorted by value */
+	uint_t dm_symfree;	/* index of next free hash element */
+	uint_t dm_nsymbuckets;	/* number of elements in bucket array */
+	uint_t dm_nsymelems;	/* number of elements in hash table */
+	uint_t dm_asrsv;	/* actual reserved size of dm_asmap */
+	uint_t dm_aslen;	/* number of entries in dm_asmap */
+} dt_module_symbols_t;
+
 typedef struct dt_module {
 	dt_list_t dm_list;	/* list forward/back pointers */
 	char dm_name[DTRACE_MODNAMELEN]; /* string name of module */
@@ -157,7 +173,7 @@ typedef struct dt_module {
 	dt_idhash_t *dm_extern;	/* external symbol definitions */
 
 	/*
-	 * Kernel modules populate the fields below only for the sake of CTF.
+	 * Loaded kernel modules populate these fields only for the sake of CTF.
 	 * When a CTF archive is available, most are not populated at all.
 	 */
 	const dt_modops_t *dm_ops; /* pointer to data model's ops vector */
@@ -165,8 +181,7 @@ typedef struct dt_module {
 	ctf_file_t *dm_ctfp;	/* CTF container handle */
 	void *dm_ctdata_data;	/* CTF section data (dynamically allocated) */
 	ctf_sect_t dm_ctdata;	/* CTF data for module */
-	ctf_sect_t dm_symtab;	/* symbol table */
-	ctf_sect_t dm_strtab;	/* string table */
+	dt_module_symbols_t dm_symbols[2]; /* .symtab, then .dynsym */
 	uint32_t dm_btf_id;	/* BTF dict id */
 	struct dt_btf *dm_btf;	/* BTF data for module */
 
@@ -174,18 +189,6 @@ typedef struct dt_module {
 	 * Kernel modules only.
 	 */
 	dt_symtab_t *dm_kernsyms; /* module kernel symbol table */
-
-	/*
-	 * Userspace modules only.
-	 */
-	uint_t *dm_symbuckets;	/* symbol table hash buckets (chain indices) */
-	dt_modsym_t *dm_symchains; /* symbol table hash chains buffer */
-	void *dm_asmap;		/* symbol pointers sorted by value */
-	uint_t dm_symfree;	/* index of next free hash element */
-	uint_t dm_nsymbuckets;	/* number of elements in bucket array */
-	uint_t dm_nsymelems;	/* number of elements in hash table */
-	uint_t dm_asrsv;	/* actual reserved size of dm_asmap */
-	uint_t dm_aslen;	/* number of entries in dm_asmap */
 } dt_module_t;
 
 /* Handle-wide address index.  order preserves module-list precedence. */
libdtrace/dt_module.c +175 -106

381 lines not on this page · raw message

libdtrace/dt_symbol_modops.h +10 -6
index d05cb172..e5c2ceda 100644
@@ -1,6 +1,6 @@
 /*
  * Oracle Linux DTrace.
- * Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2026, Oracle and/or its affiliates. All rights reserved.
  * Licensed under the Universal Permissive License v 1.0 as shown at
  * http://oss.oracle.com/licenses/upl.
  */
@@ -18,7 +18,7 @@
 #define ELFIZE(suffix) EXJOINMID(ELF,BITS,_##suffix)
 
 static uint_t
-BITIZE(dt_module_syminit)(dt_module_t *dmp)
+BITIZE(dt_module_syminit)(dt_module_symbols_t *dmp)
 {
 	const ElfIZE(Sym) *sym = dmp->dm_symtab.cts_data;
 	const char *base = dmp->dm_strtab.cts_data;
@@ -27,7 +27,7 @@ BITIZE(dt_module_syminit)(dt_module_t *dmp)
 	uint_t asrsv = 0;
 
 	for (i = 0; i < n; i++, sym++) {
-		const char *name = base + sym->st_name;
+		const char *name;
 		uchar_t type = ELFIZE(ST_TYPE)(sym->st_info);
 
 		if (type >= STT_NUM || type == STT_SECTION)
@@ -36,6 +36,10 @@ BITIZE(dt_module_syminit)(dt_module_t *dmp)
 		if (sym->st_name == 0 || sym->st_name >= ss_size)
 			continue; /* skip null or invalid names */
 
+		name = base + sym->st_name;
+		if (memchr(name, '\0', ss_size - sym->st_name) == NULL)
+			continue; /* skip unterminated names */
+
 		if (sym->st_value != 0 &&
 		    (ELFIZE(ST_BIND)(sym->st_info) != STB_LOCAL || sym->st_size))
 			asrsv++; /* reserve space in the address map */
@@ -83,7 +87,7 @@ BITIZE(dt_module_symcomp)(const void *lp, const void *rp, void *strtabp)
 }
 
 static void
-BITIZE(dt_module_symsort)(dt_module_t *dmp)
+BITIZE(dt_module_symsort)(dt_module_symbols_t *dmp)
 {
 	ElfIZE(Sym) *symtab = (ElfIZE(Sym) *)dmp->dm_symtab.cts_data;
 	ElfIZE(Sym) **sympp = (ElfIZE(Sym) **)dmp->dm_asmap;
@@ -106,7 +110,7 @@ BITIZE(dt_module_symsort)(dt_module_t *dmp)
 }
 
 static GElf_Sym *
-BITIZE(dt_module_symname)(dt_module_t *dmp, const char *name,
+BITIZE(dt_module_symname)(const dt_module_symbols_t *dmp, const char *name,
     GElf_Sym *symp, uint_t *idp)
 {
 	const ElfIZE(Sym) *symtab = dmp->dm_symtab.cts_data;
@@ -136,7 +140,7 @@ BITIZE(dt_module_symname)(dt_module_t *dmp, const char *name,
 }
 
 static GElf_Sym *
-BITIZE(dt_module_symaddr)(dt_module_t *dmp, GElf_Addr addr,
+BITIZE(dt_module_symaddr)(const dt_module_symbols_t *dmp, GElf_Addr addr,
     GElf_Sym *symp, uint_t *idp)
 {
 	const ElfIZE(Sym) **asmap = (const ElfIZE(Sym) **)dmp->dm_asmap;
test/internals/elf/tst.symbols.sh new file +10 -0
new file mode 100755
index 00000000..2d41e4dd
@@ -0,0 +1,10 @@
+#!/bin/bash
+#
+# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
+# Licensed under the Universal Permissive License v 1.0 as shown at
+# http://oss.oracle.com/licenses/upl.
+#
+# ASSERTION: ELF-backed modules search .symtab and .dynsym independently.
+#
+
+test/triggers/elf-symbols
test/triggers/Build +7 -1
index 8d648bab..0dd546ac 100644
@@ -25,7 +25,7 @@ EXTERNAL_TRIGGERS = $(EXTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(EX
 INTERNAL_64BIT_TRIGGERS = libproc-pldd libproc-consistency libproc-sleeper \
     libproc-sleeper-pie libproc-dlmadopen libproc-lookup-by-name \
     libproc-lookup-victim libproc-execing-bkpts libproc-execing-bkpts-victim \
-    btf-cyclic-types btf-lazy-types shared-ctf module-discovery
+    btf-cyclic-types btf-lazy-types shared-ctf module-discovery elf-symbols
 INTERNAL_32BIT_TRIGGERS := libproc-sleeper-32 libproc-sleeper-pie-32
 INTERNAL_TRIGGERS = $(INTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(INTERNAL_32BIT_TRIGGERS))
 
@@ -272,3 +272,9 @@ shared-ctf_CFLAGS := -Ilibproc -Ilibdtrace -Ilibcommon -Wno-pedantic
 shared-ctf_DEPS := build-libdtrace.a build-libproc.a libcommon.a libport.a
 shared-ctf_LIBS := $(objdir)/build-libdtrace.a $(objdir)/build-libproc.a \
     $(objdir)/build-libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)
+
+# Exercise both ELF symbol tables without relying on host binaries.
+elf-symbols_CFLAGS := -Ilibproc -Ilibdtrace -Ilibcommon -Wno-pedantic
+elf-symbols_DEPS := build-libdtrace.a build-libproc.a libcommon.a libport.a
+elf-symbols_LIBS := $(objdir)/build-libdtrace.a $(objdir)/build-libproc.a \
+    $(objdir)/build-libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)
test/triggers/elf-symbols.c new file +203 -0

203 lines not on this page · raw message

test/triggers/shared-ctf.c +6 -0
index 74c55c44..5772429a 100644
@@ -229,6 +229,12 @@ check_source(const char *ctfpath, const char *btfpath, int archived,
 			CHECK(dtrace_lookup_by_type(dtp, "vmlinux", "module_test_int", &ti) == 0);
 			CHECK(ctf_lookup_by_name(parent, "module_test_int") == CTF_ERR);
 		} else {
+			GElf_Sym sym;
+
+			/* Inherited BTF must not make an absent ELF module loadable. */
+			CHECK(dtrace_lookup_by_name(dtp, "shared_ctf_test_absent",
+			    "missing_symbol", &sym, NULL) == -1);
+
 			/* A module without local BTF must import the same parent. */
 			CHECK((child = dt_module_create(dtp, "shared_ctf_test_child")) != NULL);
 			child->dm_flags |= DT_DM_KERNEL;
Signature · 1 line
2.52.0
#1 lore raw
Quoted 3 lines · Kris Van Hees

Keep independent symbol and address indices for SHT_SYMTAB and SHT_DYNSYM, loading strings through each table’s sh_link. Search .symtab before .dynsym and include dynamic-only names in module symbol iteration.

Yet noather place where one or the other strtab was overlooked. These mistakes all date right back to Solaris...

Quoted 2 lines

Preserve .symtab indices for CTF, place dynamic and external symbols in separate ID ranges, and use .dynsym for CTF when .symtab is absent. Allow

Except in old dicts (CTFv2, or v3 in the absence of the CTF_F_DYNSTR dict flag), .dynsym should be searched unconditionally for CTF. Preferring the symtab is wrong. (I suspect this code is basically never run, since it's all symtypetab stuff, and the symtypetab isn't emitted for kernel CTF.)

Signature · 1 line
NULL && (void)
#2 lore raw
Quoted 9 lines · Nick Alcock
Quoted 3 lines · Kris Van Hees

Keep independent symbol and address indices for SHT_SYMTAB and SHT_DYNSYM, loading strings through each table’s sh_link. Search .symtab before .dynsym and include dynamic-only names in module symbol iteration.

Yet noather place where one or the other strtab was overlooked. These mistakes all date right back to Solaris...

Quoted 8 lines
Quoted 2 lines

Preserve .symtab indices for CTF, place dynamic and external symbols in separate ID ranges, and use .dynsym for CTF when .symtab is absent. Allow

Except in old dicts (CTFv2, or v3 in the absence of the CTF_F_DYNSTR dict flag), .dynsym should be searched unconditionally for CTF. Preferring the symtab is wrong. (I suspect this code is basically never run, since it's all symtypetab stuff, and the symtypetab isn't emitted for kernel CTF.)

... oh wait, this is for uprobes. The symtab in use for that follows the above rules then (and libctf does the symtypetab lookup for you, if uprobes is using that. Don't implement it yourself simply because CTFv4 redoes this completely while retaining almost the same user-visible API, and you don't want to change everything twice! v4 always uses the dynsymtab, unconditionally.)

Signature · 1 line
NULL && (void)
#4 lore raw
In reply to Nick Alcock
Quoted 9 lines · Nick Alcock
Quoted 3 lines · Kris Van Hees

Keep independent symbol and address indices for SHT_SYMTAB and SHT_DYNSYM, loading strings through each tableâ?Ts sh_link. Search .symtab before .dynsym and include dynamic-only names in module symbol iteration.

Yet noather place where one or the other strtab was overlooked. These mistakes all date right back to Solaris...

Quoted 8 lines
Quoted 2 lines

Preserve .symtab indices for CTF, place dynamic and external symbols in separate ID ranges, and use .dynsym for CTF when .symtab is absent. Allow

Except in old dicts (CTFv2, or v3 in the absence of the CTF_F_DYNSTR dict flag), .dynsym should be searched unconditionally for CTF. Preferring the symtab is wrong. (I suspect this code is basically never run, since it's all symtypetab stuff, and the symtypetab isn't emitted for kernel CTF.)

R-b?

#3 lore raw
In reply to Nick Alcock
Quoted 20 lines · Nick Alcock
Quoted 9 lines · Nick Alcock
Quoted 3 lines · Kris Van Hees

Keep independent symbol and address indices for SHT_SYMTAB and SHT_DYNSYM, loading strings through each tableâ?Ts sh_link. Search .symtab before .dynsym and include dynamic-only names in module symbol iteration.

Yet noather place where one or the other strtab was overlooked. These mistakes all date right back to Solaris...

Quoted 8 lines
Quoted 2 lines

Preserve .symtab indices for CTF, place dynamic and external symbols in separate ID ranges, and use .dynsym for CTF when .symtab is absent. Allow

Except in old dicts (CTFv2, or v3 in the absence of the CTF_F_DYNSTR dict flag), .dynsym should be searched unconditionally for CTF. Preferring the symtab is wrong. (I suspect this code is basically never run, since it's all symtypetab stuff, and the symtypetab isn't emitted for kernel CTF.)

Quoted 6 lines

... oh wait, this is for uprobes. The symtab in use for that follows the above rules then (and libctf does the symtypetab lookup for you, if uprobes is using that. Don't implement it yourself simply because CTFv4 redoes this completely while retaining almost the same user-visible API, and you don't want to change everything twice! v4 always uses the dynsymtab, unconditionally.)

But I do not have CTFv4 righr now, so we need this.

#5 lore raw
In reply to Nick Alcock
Quoted 9 lines · Nick Alcock
Quoted 3 lines · Kris Van Hees

Keep independent symbol and address indices for SHT_SYMTAB and SHT_DYNSYM, loading strings through each table’s sh_link. Search .symtab before .dynsym and include dynamic-only names in module symbol iteration.

Yet noather place where one or the other strtab was overlooked. These mistakes all date right back to Solaris...

Quoted 8 lines
Quoted 2 lines

Preserve .symtab indices for CTF, place dynamic and external symbols in separate ID ranges, and use .dynsym for CTF when .symtab is absent. Allow

Except in old dicts (CTFv2, or v3 in the absence of the CTF_F_DYNSTR dict flag), .dynsym should be searched unconditionally for CTF. Preferring the symtab is wrong. (I suspect this code is basically never run, since it's all symtypetab stuff, and the symtypetab isn't emitted for kernel CTF.)

So are you saying that this should be updated (and some other existing code) to give preference to .dynsym for lookups, and fallback to .symtab if the symbol is not found in .dynsym?