Skip to content

Commit a5ea19c

Browse files
authored
Recover dynamic symbols when retained ELF sections omit the symbol table (R3.12) (#1367)
Dynamic symbols were recovered from the dynamic segment only when a file had no section headers at all. They're now recovered whenever the sections give none, and symbols, exports and imports already listed aren't added twice.
1 parent 7779fc2 commit a5ea19c

4 files changed

Lines changed: 127 additions & 6 deletions

File tree

‎crates/formats/src/elf.rs‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{FormatError, hex_addr};
77
use object::Object;
88
use serde::Serialize;
99
use serde_json::json;
10-
use std::collections::BTreeMap;
10+
use std::collections::{BTreeMap, BTreeSet};
1111

1212
const MAX_PHDRS: usize = 4096;
1313
const MAX_DYN: usize = 65_536;
@@ -309,9 +309,9 @@ pub fn enrich(data: &[u8], file: &object::File<'_>, b: &mut Binary) {
309309
"dt_flags": format!("{flags:#x}"),
310310
"dt_flags_1": format!("{flags1:#x}"),
311311
});
312-
// With no section headers `object` finds no symbols; the dynamic section still points at them.
313-
if b.sections.is_empty()
314-
&& let Some(ds) = dynamic_symbols(data, b)
312+
// Other sections may survive after the dynamic-symbol section was removed.
313+
if file.dynamic_symbols().next().is_none()
314+
&& let Some(ds) = segment_dynamic_symbols(data, &e)
315315
{
316316
add_dynamic_symbols(b, ds);
317317
}
@@ -321,7 +321,11 @@ pub fn enrich(data: &[u8], file: &object::File<'_>, b: &mut Binary) {
321321
fn add_dynamic_symbols(b: &mut Binary, ds: DynamicSymbols) {
322322
b.warnings.extend(ds.warnings.into_iter().map(|w| format!("dynamic symbols: {w}")));
323323
b.symbols_truncated |= ds.truncated;
324+
let mut known: BTreeSet<(String, u64)> = b.symbols.iter().map(|s| (s.name.clone(), s.addr)).collect();
324325
for (s, defined) in ds.exports.iter().map(|s| (s, true)).chain(ds.imports.iter().map(|s| (s, false))) {
326+
if !known.insert((s.name.clone(), s.addr)) {
327+
continue;
328+
}
325329
if b.symbols.len() >= MAX_SYMBOLS {
326330
b.symbols_truncated = true;
327331
break;
@@ -343,9 +347,17 @@ fn add_dynamic_symbols(b: &mut Binary, ds: DynamicSymbols) {
343347
dynamic: true,
344348
});
345349
}
346-
b.exports.extend(ds.exports.iter().take(MAX_IMPORTS).map(|s| Export { name: s.name.clone(), addr: s.addr }));
350+
let mut exports: BTreeSet<(String, u64)> = b.exports.iter().map(|s| (s.name.clone(), s.addr)).collect();
351+
let room = MAX_IMPORTS.saturating_sub(b.exports.len());
352+
b.exports.extend(ds.exports.iter().filter(|s| exports.insert((s.name.clone(), s.addr))).take(room).map(|s| Export { name: s.name.clone(), addr: s.addr }));
353+
let mut imports: BTreeSet<(String, Option<String>)> = b.imports.iter().map(|s| (s.name.clone(), s.library.clone())).collect();
347354
let room = MAX_IMPORTS.saturating_sub(b.imports.len());
348-
b.imports.extend(ds.imports.into_iter().take(room).map(|s| Import { name: s.name, library: s.library, ordinal: None, slot: None }));
355+
b.imports.extend(ds.imports.into_iter().filter(|s| imports.insert((s.name.clone(), s.library.clone()))).take(room).map(|s| Import {
356+
name: s.name,
357+
library: s.library,
358+
ordinal: None,
359+
slot: None,
360+
}));
349361
}
350362

351363
// ---------------------------------------------------------------------------------------------
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use craft_anything_formats::parse_binary;
2+
use craft_anything_testkit::flutter::SymbolTable;
3+
use craft_anything_testkit::sectionless::sectionless_elf;
4+
use craft_anything_testkit::{ElfArch, elf64};
5+
6+
fn put(data: &mut [u8], at: usize, width: usize, value: u64, big: bool) {
7+
let bytes = if big { value.to_be_bytes() } else { value.to_le_bytes() };
8+
let bytes = if big { &bytes[8 - width..] } else { &bytes[..width] };
9+
data[at..at + width].copy_from_slice(bytes);
10+
}
11+
12+
fn align(data: &mut Vec<u8>) {
13+
while !data.len().is_multiple_of(8) {
14+
data.push(0);
15+
}
16+
}
17+
18+
fn with_retained_sections(is64: bool, big: bool) -> Vec<u8> {
19+
let mut data = sectionless_elf(is64, big, &["dynamic_entry"], SymbolTable::DynamicHash);
20+
let address = parse_binary(&data).unwrap().exports.iter().find(|symbol| symbol.name == "dynamic_entry").unwrap().addr;
21+
let strings = b"\0kept_local\0dynamic_entry\0";
22+
let strings_at = data.len();
23+
data.extend_from_slice(strings);
24+
align(&mut data);
25+
let symbols_at = data.len();
26+
let symbol_size = if is64 { 24 } else { 16 };
27+
data.resize(symbols_at + 3 * symbol_size, 0);
28+
for (index, (name, info)) in [(1u64, 2u8), (12, 0x12)].into_iter().enumerate() {
29+
let at = symbols_at + (index + 1) * symbol_size;
30+
put(&mut data, at, 4, name, big);
31+
if is64 {
32+
data[at + 4] = info;
33+
put(&mut data, at + 6, 2, 1, big);
34+
put(&mut data, at + 8, 8, address, big);
35+
put(&mut data, at + 16, 8, 16, big);
36+
} else {
37+
put(&mut data, at + 4, 4, address, big);
38+
put(&mut data, at + 8, 4, 16, big);
39+
data[at + 12] = info;
40+
put(&mut data, at + 14, 2, 1, big);
41+
}
42+
}
43+
let names = b"\0.text\0.symtab\0.strtab\0.shstrtab\0";
44+
let names_at = data.len();
45+
data.extend_from_slice(names);
46+
align(&mut data);
47+
let sections_at = data.len();
48+
let section_size = if is64 { 64 } else { 40 };
49+
data.resize(sections_at + 5 * section_size, 0);
50+
let rows = [
51+
(1u64, 1u64, 6u64, address, address, 16u64, 0u64, 0u64, 16u64, 0u64),
52+
(7, 2, 0, 0, symbols_at as u64, (3 * symbol_size) as u64, 3, 2, 8, symbol_size as u64),
53+
(15, 3, 0, 0, strings_at as u64, strings.len() as u64, 0, 0, 1, 0),
54+
(23, 3, 0, 0, names_at as u64, names.len() as u64, 0, 0, 1, 0),
55+
];
56+
for (index, (name, kind, flags, addr, offset, size, link, info, alignment, entry_size)) in rows.into_iter().enumerate() {
57+
let at = sections_at + (index + 1) * section_size;
58+
put(&mut data, at, 4, name, big);
59+
put(&mut data, at + 4, 4, kind, big);
60+
if is64 {
61+
for (field, value) in [(8, flags), (16, addr), (24, offset), (32, size), (48, alignment), (56, entry_size)] {
62+
put(&mut data, at + field, 8, value, big);
63+
}
64+
put(&mut data, at + 40, 4, link, big);
65+
put(&mut data, at + 44, 4, info, big);
66+
} else {
67+
for (field, value) in [(8, flags), (12, addr), (16, offset), (20, size), (24, link), (28, info), (32, alignment), (36, entry_size)] {
68+
put(&mut data, at + field, 4, value, big);
69+
}
70+
}
71+
}
72+
if is64 {
73+
put(&mut data, 40, 8, sections_at as u64, big);
74+
put(&mut data, 58, 2, 64, big);
75+
put(&mut data, 60, 2, 5, big);
76+
put(&mut data, 62, 2, 4, big);
77+
} else {
78+
put(&mut data, 32, 4, sections_at as u64, big);
79+
put(&mut data, 46, 2, 40, big);
80+
put(&mut data, 48, 2, 5, big);
81+
put(&mut data, 50, 2, 4, big);
82+
}
83+
data
84+
}
85+
86+
#[test]
87+
fn retained_sections_without_dynsym_still_recover_dynamic_imports_and_exports() {
88+
for is64 in [false, true] {
89+
for big in [false, true] {
90+
let binary = parse_binary(&with_retained_sections(is64, big)).unwrap();
91+
assert!(binary.sections.iter().any(|section| section.name == ".text"));
92+
assert!(binary.symbols.iter().any(|symbol| symbol.name == "kept_local" && !symbol.dynamic));
93+
assert_eq!(binary.symbols.iter().filter(|symbol| symbol.name == "dynamic_entry").count(), 1);
94+
assert_eq!(binary.imports.iter().filter(|import| import.name == "puts").count(), 1);
95+
assert_eq!(binary.exports.iter().filter(|export| export.name == "dynamic_entry").count(), 1);
96+
}
97+
}
98+
}
99+
100+
#[test]
101+
fn complete_dynamic_sections_are_not_recovered_twice() {
102+
let (data, _) = elf64(ElfArch::X86_64, true);
103+
let binary = parse_binary(&data).unwrap();
104+
assert_eq!(binary.imports.iter().filter(|import| import.name == "puts").count(), 1);
105+
assert_eq!(binary.symbols.iter().filter(|symbol| symbol.name == "puts").count(), 1);
106+
}

‎docs/ROADMAP.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,8 @@ Owner: open · Status: in progress
15791579
— claimed: Afloat16, branch bugfix/r3.12-codesign-superblob-length-boundary-20261105
15801580
- [x] Reject out-of-range or unterminated library names in the ELF dynamic string table.
15811581
— claimed: Afloat16, branch bugfix/r3.12-elf-dynamic-names
1582+
- [x] Recover dynamic symbols through PT_DYNAMIC when an ELF retains other section headers but has no usable dynamic-symbol section.
1583+
— claimed: Afloat16, branch bugfix/r3.12-elf-partial-section-symbols-20261109
15821584
- [x] Refuse short ELF64 header and section records and handle section-table offsets at the address limit safely.
15831585
— claimed: Afloat16, branch bugfix/r3.12-sbpf-records
15841586
- [x] Keep ELF version definitions, requirements and indices within their declared section byte ranges.

‎docs/STATUS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi
8282
| Keep Go function tables and names within their section | Go function tables and names respect every containing file-backed section during scan recovery, including unnamed and overlapping sections. Recovery outside known sections keeps the whole-file extent. A named table with no readable functions allows bounded recovery while retaining its partial evidence if no replacement is found. | Synthetic byte fixtures in `go::tests` unit regressions. Cover Go 1.16 and 1.20 table entries, unterminated names, public inspect()/functions() recovery from truncated or empty named sections and interior headers, unreadable named tables with or without a readable interior replacement, unnamed entry/name boundaries, overlapping or expired extents, and recovery outside known sections or without section headers, with complete-table controls. |
8383
| Preserve complete Unicode characters in shortened .NET strings | .NET user-string limits preserve supplementary Unicode characters. | Synthetic byte fixtures in `dotnet_user_string_surrogate_limit` integration regressions. Cover supplementary characters at several limits, a zero-character limit, and unpaired surrogates followed by valid characters. |
8484
| ELF dynamic library names | Needed libraries and dynamic path names stay within DT_STRSZ when the executable declares it. | An ELF64 fixture moves the bound to the beginning and to the terminator of a needed library name. |
85+
| ELF dynamic symbols with partial section tables | PT_DYNAMIC supplies imports and exports when retained section headers omit usable dynamic symbols. Retained ordinary symbols stay available and matching facts are not duplicated. | ELF32 and ELF64 in both byte orders, retained local and duplicate symbols, and a complete dynamic-section control. |
8586
| Locate ELF core mapping names after the complete declared table | ELF core filenames follow the full declared mapping table. | Synthetic byte fixtures in `elf_core_file_name_count_cap` integration regressions. Cover 65,537 declared mappings with capped output, an impossible declared table, and a small complete mapping table. |
8687
| ELF core mapping path cursors | NT_FILE mapping paths advance by the original NUL-terminated byte strings, even when the displayed path is lossy or capped. | Invalid UTF-8 and long first paths followed by another mapping; valid UTF-8 and unterminated final-path controls. |
8788
| Keep DOS relocations inside the declared executable header | DOS relocation records stay inside the declared MZ header. | Synthetic byte fixtures in `dos_relocation_header_boundary` integration regressions. Cover records extending into the load module, a partial final record, and complete records ending exactly at the header boundary. |

0 commit comments

Comments
 (0)