Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
6e4cdfc
PROF-16674 skip TLS, ABS and undefined symbols when loading symbol ta…
zhengyu123 Nov 15, 2026
3715f08
PROF-16674 stop the dynamic array at DT_NULL and size PLT entries fro…
zhengyu123 Nov 15, 2026
36ee3f7
PROF-16674 trim ELF parser comments, use named casts, test section, f…
zhengyu123 Nov 18, 2026
acf5977
PROF-16674 keep SHN_XINDEX symbols, clamp relative-relocation skip wi…
zhengyu123 Nov 18, 2026
080f661
PROF-16674 test the relative-relocation overflow and DT_PLTREL finger…
zhengyu123 Nov 18, 2026
0d32d86
PROF-16674 point the strAt() bound test one past the real string-tabl…
zhengyu123 Nov 18, 2026
4b61746
PROF-16674 test .dynsym without relocation tags, REL overflow and the…
zhengyu123 Nov 18, 2026
364bc7a
PROF-16674 skip a DT_RELA table whose DT_RELAENT is smaller than Elf6…
zhengyu123 Nov 18, 2026
eaee154
PROF-16674 validate DT_RELAENT where it is used, so the PLT fallback …
zhengyu123 Nov 18, 2026
405e6b4
Merge branch 'main' into zgu/PROF-16674-elf-dynamic
jbachorik Nov 20, 2026
91cf092
PROF-16674 test the DT_PLTREL=DT_REL and DT_RELENT fallback branches …
zhengyu123 Nov 20, 2026
1e84dc3
PROF-16674 pltEntrySize tests: distinguish strides, find tags by name…
zhengyu123 Nov 20, 2026
52717ce
PROF-16674 pltEntrySize tests: assert the fingerprint stride too and …
zhengyu123 Nov 20, 2026
248b610
PROF-16674 pltEntrySize tests: check the fingerprint on negative rows…
zhengyu123 Nov 20, 2026
bf600b4
PROF-16674 pltEntrySize tests: drop a duplicate include and keep the …
zhengyu123 Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 117 additions & 74 deletions ddprof-lib/src/main/cpp/symbols_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ class ElfParser {
return _header->e_type == ET_EXEC ? NULL : _vaddr_diff;
}

char* dyn_ptr(ElfDyn* dyn) {
char* dyn_ptr(const ElfDyn* dyn) {
// GNU dynamic linker relocates pointers in the dynamic section, while musl doesn't.
// Also, [vdso] is not relocated, and its vaddr may differ from the load address.
if (_relocate_dyn || (_base != NULL && (char*)dyn->d_un.d_ptr < _base)) {
Expand Down Expand Up @@ -609,73 +609,110 @@ void ElfParser::calcVirtualLoadAddress() {
_vaddr_diff = _base;
}

// DT_RELAENT, or 0 (skip the table) when smaller than an Elf64_Rela: a shorter
// stride would read one entry's r_addend and the next r_offset as r_offset/r_info.
static size_t relaEntrySize(size_t relaent) {
return relaent >= sizeof(Elf64_Rela) ? relaent : 0;
}

// Size of one DT_JMPREL entry, from DT_PLTREL (gABI): a library with only PLT
// relocations has no DT_RELENT/DT_RELAENT. Without DT_PLTREL, fall back to a valid
// DT_RELAENT, else DT_RELENT; 0 means unknown or invalid.
static size_t pltEntrySize(uint64_t pltrel, size_t relent, size_t relaent) {
if (pltrel == DT_REL) return sizeof(Elf64_Rel);
Comment thread
zhengyu123 marked this conversation as resolved.
Resolved
if (pltrel == DT_RELA) return sizeof(Elf64_Rela);
return relaent != 0 ? relaEntrySize(relaent) : relent;
Comment thread
zhengyu123 marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 pltEntrySize() silently falls back to relaent/relent when DT_PLTREL is present but holds any value other than DT_REL/DT_RELA (e.g. a corrupted or nonstandard image).

A bogus DT_PLTREL is exactly the case where the stride is unknown, yet the code guesses one and walks the untrusted DT_JMPREL table at that guessed stride anyway, both in parseDynamicSection() (wrong imports can be recorded: a misaligned r_offset + a coincidentally resolvable symbol name registers a bogus addImport slot for LibraryPatcher) and in hashImportLayout() (fingerprint of garbage). The PR notes acknowledge this ("A DT_PLTREL value other than REL/RELA falls back silently"), but for a table whose entry type is explicitly declared and wrong, the safe gABI behavior is to skip the table, as relaEntrySize() already does for undersized DT_RELAENT.

  • (a) Treat an out-of-range DT_PLTREL as invalid: if (pltrel != 0 && pltrel != DT_REL && pltrel != DT_RELA) return 0; before the fallback, so the table is skipped the same way relaEntrySize() skips undersized entries.
  • (b) Alternatively, keep the fallback but log a warning once per image, so a nonstandard DT_PLTREL is diagnosable instead of silently mis-strided.
  • Add a test mirroring pltRelOverridesRelaent with DT_PLTREL = 0x1234 asserting the PLT table is skipped in both the parse and the fingerprint.
Evidence
static size_t pltEntrySize(uint64_t pltrel, size_t relent, size_t relaent) {
    if (pltrel == DT_REL) return sizeof(Elf64_Rel);
    if (pltrel == DT_RELA) return sizeof(Elf64_Rela);
    return relaent != 0 ? relaEntrySize(relaent) : relent;
}

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4 No test in this PR would detect: using relent instead of a valid relaent for the fallback stride in pltEntrySize() when DT_PLTREL is absent in pltEntrySize

Add a test with DT_PLTREL absent and a valid DT_RELAENT that differs from DT_RELENT, then assert the PLT import is parsed and its relocation table affects the fingerprint.

Evidence
return relaent != 0 ? relaEntrySize(relaent) : relent;

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

}

// Bytes of the DT_RELACOUNT/DT_RELCOUNT leading relative relocations, clamped to
// the table size without multiplying first, so a huge count cannot wrap.
static size_t relativeBytes(size_t count, size_t ent, size_t size) {
return ent != 0 && count < size / ent ? count * ent : size;
}

void ElfParser::parseDynamicSection() {
ElfProgramHeader* dynamic = findProgramHeader(PT_DYNAMIC);
if (dynamic != NULL) {
const char* symtab = NULL;
const char* strtab = NULL;
char* jmprel = NULL;
char* rela = NULL;
char* rel = NULL;
uint64_t pltrel = 0;
size_t pltrelsz = 0;
size_t relsz = 0;
size_t relent = 0;
size_t relcount = 0;
size_t relasz = 0, relsz = 0;
size_t relaent = 0, relent = 0;
size_t relacount = 0, relcount = 0;
size_t syment = 0;
size_t strsz = 0;
uint32_t nsyms = 0;

const char* dyn_start = at(dynamic);
// at(dynamic) is NULL when dynamic->p_vaddr == 0 - same null-base
// pointer-arithmetic UB as the other fixes in this file.
const char* dyn_end = (const char*)((uintptr_t)dyn_start + dynamic->p_memsz);
for (ElfDyn* dyn = (ElfDyn*)dyn_start; dyn < (ElfDyn*)dyn_end; dyn++) {
switch (dyn->d_tag) {
// Whole entries only: a p_memsz that is not a multiple of sizeof(ElfDyn)
// must not make the loop read a partial one.
size_t dyn_count = dynamic->p_memsz / sizeof(ElfDyn);
const ElfDyn* dyn = reinterpret_cast<const ElfDyn*>(dyn_start);
// The array ends at the first DT_NULL; slots after it (patchelf, prelink)
// are padding and must not override real tags.
for (size_t i = 0; i < dyn_count && dyn[i].d_tag != DT_NULL; i++) {
switch (dyn[i].d_tag) {
case DT_SYMTAB:
symtab = dyn_ptr(dyn);
symtab = dyn_ptr(&dyn[i]);
break;
case DT_STRTAB:
strtab = dyn_ptr(dyn);
strtab = dyn_ptr(&dyn[i]);
break;
case DT_SYMENT:
syment = dyn->d_un.d_val;
syment = dyn[i].d_un.d_val;
break;
case DT_STRSZ:
strsz = dyn->d_un.d_val;
strsz = dyn[i].d_un.d_val;
break;
case DT_HASH:
nsyms = ((uint32_t*)dyn_ptr(dyn))[1];
nsyms = reinterpret_cast<uint32_t*>(dyn_ptr(&dyn[i]))[1];
break;
case DT_GNU_HASH:
if (nsyms == 0) {
nsyms = getSymbolCount((uint32_t*)dyn_ptr(dyn));
nsyms = getSymbolCount(reinterpret_cast<uint32_t*>(dyn_ptr(&dyn[i])));
}
break;
case DT_JMPREL:
jmprel = dyn_ptr(dyn);
jmprel = dyn_ptr(&dyn[i]);
break;
case DT_PLTRELSZ:
pltrelsz = dyn->d_un.d_val;
pltrelsz = dyn[i].d_un.d_val;
break;
case DT_PLTREL:
pltrel = dyn[i].d_un.d_val;
break;
case DT_RELA:
rela = dyn_ptr(&dyn[i]);
break;
case DT_REL:
rel = dyn_ptr(dyn);
rel = dyn_ptr(&dyn[i]);
break;
case DT_RELASZ:
relasz = dyn[i].d_un.d_val;
break;
case DT_RELSZ:
relsz = dyn->d_un.d_val;
relsz = dyn[i].d_un.d_val;
break;
case DT_RELAENT:
relaent = dyn[i].d_un.d_val;
break;
case DT_RELENT:
relent = dyn->d_un.d_val;
relent = dyn[i].d_un.d_val;
break;
case DT_RELACOUNT:
relacount = dyn[i].d_un.d_val;
break;
case DT_RELCOUNT:
relcount = dyn->d_un.d_val;
relcount = dyn[i].d_un.d_val;
break;
}
}

if (symtab == NULL || strtab == NULL || syment == 0 || relent == 0) {
if (symtab == NULL || strtab == NULL || syment == 0) {
return;
}

Expand All @@ -699,43 +736,33 @@ void ElfParser::parseDynamicSection() {
// though the intent is just "sym addresses are already absolute". Do the
// addition in integer space, same fix as the .plt case above.
uintptr_t base_addr = (uintptr_t)this->base();
// Smaller entries cannot hold r_offset and r_info (hashImportLayout()
// skips them too). Walking only while a whole entry fits keeps offs
// within the table, so offs += relent cannot wrap.
if (jmprel != NULL && pltrelsz != 0 && relent >= sizeof(ElfRelocation)) {
// Parse .rela.plt table
for (size_t offs = 0; relent <= pltrelsz - offs; offs += relent) {
ElfRelocation r = relocationAt(jmprel + offs);
// Records one table's imports after its leading `skip` bytes of relative relocations.
// The skip guard (callers pass skip <= size) and walking only while a whole entry
// fits keep offs within the table. only_got_slots keeps GLOB_DAT/ABS64 entries.
auto addImports = [&](char* table, size_t size, size_t ent, size_t skip, bool only_got_slots) {
if (table == NULL || size == 0 || ent < sizeof(ElfRelocation) || skip > size) return;
for (size_t offs = skip; ent <= size - offs; offs += ent) {
ElfRelocation r = relocationAt(table + offs);
if (only_got_slots && ELF_R_TYPE(r.r_info) != R_GLOB_DAT && ELF_R_TYPE(r.r_info) != R_ABS64) {
continue;
}
uint32_t st_name = symbolNameAt(symtab + ELF_R_SYM(r.r_info) * syment);
if (st_name != 0) {
const char* sym_name = strAt(strtab, strsz, st_name);
if (sym_name != NULL) {
_cc->addImport((void**)(base_addr + r.r_offset), sym_name);
_cc->addImport(reinterpret_cast<void**>(base_addr + r.r_offset), sym_name);
}
}
}
}
};

if (rel != NULL && relsz != 0 && relent >= sizeof(ElfRelocation)) {
// Relocation entries for imports can be found in .rela.dyn, for example
// if a shared library is built without PLT (-fno-plt). However, if both
// entries exist, addImport saves them both.
// The leading DT_RELACOUNT entries are relative relocations, without
// a symbol; clamp the start as hashImportLayout() does.
size_t start = relcount * relent < relsz ? relcount * relent : relsz;
for (size_t offs = start; relent <= relsz - offs; offs += relent) {
ElfRelocation r = relocationAt(rel + offs);
if (ELF_R_TYPE(r.r_info) == R_GLOB_DAT || ELF_R_TYPE(r.r_info) == R_ABS64) {
uint32_t st_name = symbolNameAt(symtab + ELF_R_SYM(r.r_info) * syment);
if (st_name != 0) {
const char* sym_name = strAt(strtab, strsz, st_name);
if (sym_name != NULL) {
_cc->addImport((void**)(base_addr + r.r_offset), sym_name);
}
}
}
}
}
// .rela.plt / .rel.plt
addImports(jmprel, pltrelsz, pltEntrySize(pltrel, relent, relaent), 0, false);
// Imports can also be in .rela.dyn / .rel.dyn, for example if a shared
// library is built without PLT (-fno-plt). If both tables exist,
// addImport saves them both.
addImports(rela, relasz, relaEntrySize(relaent), relativeBytes(relacount, relaent, relasz), true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4 For the .rela.dyn table, the stride is taken from DT_RELAENT and the table is skipped entirely when the tag is absent (relaEntrySize(0) returns 0).

Per the gABI the Elf64_Rela entry size for a DT_RELA table is implied by the format, and DT_RELAENT is not required to be present; a spec-valid image with DT_RELA/DT_RELASZ but no DT_RELAENT silently loses all its R_GLOB_DAT/R_ABS64 imports (e.g. pthread_create for a -fno-plt library), which LibraryPatcher then cannot patch. The same applies to .rel.dyn with a missing DT_RELENT. This is not a regression (the old code required a non-zero relent too), and every mainstream linker emits the *ENT tags, so it only bites hand-built or stripped-tag images — but the PR's own rationale for the DT_JMPREL fix was exactly that a table must not depend on an entry-size tag being present.

  • Default relaent to sizeof(Elf64_Rela) (and relent to sizeof(Elf64_Rel)) when the tag is absent, using the tag value only when present and >= the implied size — mirroring how pltEntrySize() now derives the DT_JMPREL stride without relying on the *ENT tags.
  • Or document explicitly (comment + PR notes) that DT_RELAENT/DT_RELENT absence is treated as invalid by design, so the asymmetry with DT_JMPREL is intentional.
Evidence
addImports(rela, relasz, relaEntrySize(relaent), relativeBytes(relacount, relaent, relasz), true);
        addImports(rel, relsz, relent, relativeBytes(relcount, relent, relsz), true);

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

addImports(rel, relsz, relent, relativeBytes(relcount, relent, relsz), true);
}
}

Expand Down Expand Up @@ -983,6 +1010,23 @@ bool ElfParser::loadSymbolsUsingDebugLink() {
return result;
}

// Whether the symbol defines an object at an in-image st_value (gABI "Symbol Values").
// Not SHN_UNDEF (defined elsewhere), reserved indices other than SHN_XINDEX (absolute
// value or alignment), STT_TLS (TLS offset) or STT_SECTION/STT_FILE.
static bool isDefinedInImageSymbol(const ElfSymbol& entry) {
if (entry.st_shndx == SHN_UNDEF || (entry.st_shndx >= SHN_LORESERVE && entry.st_shndx != SHN_XINDEX)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 SHN_XINDEX is not itself evidence that a symbol is defined in the image: it signals that the actual section index is stored in the associated SHT_SYMTAB_SHNDX table.

This filter accepts every such symbol without resolving that index, including entries whose actual index is SHN_UNDEF or another special index, and can add a non-address value to the code cache.

Resolve SHN_XINDEX through the section's associated SHT_SYMTAB_SHNDX data before deciding whether the symbol is an in-image definition; if that table is unavailable or invalid, skip the symbol.

Evidence
if (entry.st_shndx == SHN_UNDEF || (entry.st_shndx >= SHN_LORESERVE && entry.st_shndx != SHN_XINDEX)) {

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

return false;
}
switch (ELF64_ST_TYPE(entry.st_info)) {
case STT_TLS:
case STT_SECTION:
case STT_FILE:
return false;
default:
return true;
}
}

void ElfParser::loadSymbolTable(const char* symbols, size_t total_size, size_t ent_size, const char* strings, size_t strings_size) {
// A stride smaller than one symbol entry would never advance past (or would
// re-read) an entry; reject it to avoid an infinite loop / over-read.
Expand All @@ -1000,7 +1044,7 @@ void ElfParser::loadSymbolTable(const char* symbols, size_t total_size, size_t e
// aliasing mapped bytes through a typed pointer.
ElfSymbol entry;
memcpy(&entry, symbols + off, sizeof(entry));
if (entry.st_name != 0 && entry.st_value != 0) {
if (entry.st_name != 0 && entry.st_value != 0 && isDefinedInImageSymbol(entry)) {
// Resolve the name through the bounded string table; a bad st_name
// offset (or unterminated string) drops the symbol instead of reading
// out of bounds.
Expand Down Expand Up @@ -1425,46 +1469,45 @@ bool hashImportLayout(const LoadedImage& image, u64& hash) {
uintptr_t dyn_start = image.address(dynamic->p_vaddr);
if (!image.readable(dyn_start, dynamic->p_filesz)) return false;

uintptr_t symtab = 0, strtab = 0, jmprel = 0, rel = 0;
size_t syment = 0, strsz = 0, pltrelsz = 0, relsz = 0, relent = 0, relcount = 0;
uintptr_t symtab = 0, strtab = 0, jmprel = 0, rela = 0, rel = 0;
uint64_t pltrel = 0;
size_t syment = 0, strsz = 0, pltrelsz = 0, relasz = 0, relsz = 0;
size_t relaent = 0, relent = 0, relacount = 0, relcount = 0;
size_t count = dynamic->p_filesz / sizeof(ElfDyn);
for (const ElfDyn* dyn = (const ElfDyn*)dyn_start; count-- > 0 && dyn->d_tag != DT_NULL; dyn++) {
switch (dyn->d_tag) {
case DT_SYMTAB: symtab = image.dynamicPointer(dyn->d_un.d_ptr); break;
case DT_STRTAB: strtab = image.dynamicPointer(dyn->d_un.d_ptr); break;
case DT_JMPREL: jmprel = image.dynamicPointer(dyn->d_un.d_ptr); break;
case DT_RELA:
case DT_RELA: rela = image.dynamicPointer(dyn->d_un.d_ptr); break;
case DT_REL: rel = image.dynamicPointer(dyn->d_un.d_ptr); break;
case DT_SYMENT: syment = dyn->d_un.d_val; break;
case DT_STRSZ: strsz = dyn->d_un.d_val; break;
case DT_PLTRELSZ: pltrelsz = dyn->d_un.d_val; break;
case DT_RELASZ:
case DT_PLTREL: pltrel = dyn->d_un.d_val; break;
case DT_RELASZ: relasz = dyn->d_un.d_val; break;
case DT_RELSZ: relsz = dyn->d_un.d_val; break;
case DT_RELAENT:
case DT_RELAENT: relaent = dyn->d_un.d_val; break;
case DT_RELENT: relent = dyn->d_un.d_val; break;
case DT_RELACOUNT:
case DT_RELACOUNT: relacount = dyn->d_un.d_val; break;
case DT_RELCOUNT: relcount = dyn->d_un.d_val; break;
}
}
// ElfParser::parseDynamicSection() records no imports without these.
if (symtab == 0 || strtab == 0 || syment == 0 || relent < sizeof(ElfRelocation)) return true;
if (symtab == 0 || strtab == 0 || syment == 0) return true;
if (strsz == 0 || !image.readable(strtab, strsz)) return false;

if (jmprel != 0 && pltrelsz != 0) {
if (!image.readable(jmprel, pltrelsz)
|| !hashSymbolRelocations(image, jmprel, pltrelsz, relent, symtab, syment, strtab, strsz, hash)) {
return false;
}
}
if (rel != 0 && relsz != 0) {
if (!image.readable(rel, relsz)) return false;
// The leading DT_RELACOUNT entries are relative relocations, without a symbol.
size_t skip = relcount * relent < relsz ? relcount * relent : relsz;
if (!hashSymbolRelocations(image, rel + skip, relsz - skip, relent, symtab, syment, strtab, strsz, hash)) {
return false;
}
}
return true;
// Same tables and entry sizes as parseDynamicSection(). Entries smaller than
// sizeof(ElfRelocation) are skipped; the leading `skip` bytes are relative
// relocations, without a symbol; the skip guard is defensive (callers pass skip <= size).
auto hashTable = [&](uintptr_t table, size_t size, size_t ent, size_t skip) {
if (table == 0 || size == 0 || ent < sizeof(ElfRelocation) || skip > size) return true;
if (!image.readable(table, size)) return false;
return hashSymbolRelocations(image, table + skip, size - skip, ent, symtab, syment, strtab, strsz, hash);
};
return hashTable(jmprel, pltrelsz, pltEntrySize(pltrel, relent, relaent), 0)
&& hashTable(rela, relasz, relaEntrySize(relaent), relativeBytes(relacount, relaent, relasz))
&& hashTable(rel, relsz, relent, relativeBytes(relcount, relent, relsz));
}

} // namespace
Expand Down
Loading
Loading