coff: parse long section names encoded with LLVM/GNU format

This commit is contained in:
Nikita Smith authored and Ryan Fleury committed 2026-09-10 14:28:35 -07:00
1 parent fd6e2bb6b2
commit 1673303d56
2 files changed
+81 -4

No files matched your search

+34 -4
View File
@@ -55,11 +55,41 @@ internal String8
coff_name_from_section_header(String8 string_table, COFF_SectionHeader *header)
{
String8 name = str8_cstring_capped(header->name, header->name + sizeof(header->name));
if (name.str[0] == '/') {
String8 name_off_str = str8_skip(name, 1);
U64 name_off = u64_from_str8(name_off_str, 10);
name = str8_cstring_capped(string_table.str + name_off, string_table.str+string_table.size);
if (name.size > 0 && name.str[0] == '/') {
U64 name_off = max_U64;
// LLVM/GNU Base64 offset encoding, most significant digit first
if (name.size > 1 && name.str[1] == '/') {
if (name.size > 2) {
name_off = 0;
for (U64 i = 2; i < name.size; ++i) {
U8 c = name.str[i];
U64 digit = 0;
if (char_is_upper(c)) { digit = c - 'A'; }
else if (char_is_lower(c)) { digit = c - 'a' + 26; }
else if (char_is_digit(c, 10)) { digit = c - '0' + 52; }
else if (c == '+') { digit = 62; }
else if (c == '/') { digit = 63; }
else {
name_off = max_U64;
break;
}
name_off = (name_off << 6) | digit;
}
}
} else {
// parse standard COFF long name encoding
name_off = u64_from_str8(str8_skip(name, 1), 10);
}
if (name_off < string_table.size) {
name = str8_cstring_capped(string_table.str + name_off, string_table.str+string_table.size);
} else {
name = str8_zero();
}
}
return name;
}
+47
View File
@@ -9940,6 +9940,53 @@ TEST(asan_discarded_associative_metadata)
}
}
TEST(coff_section_name_offsets)
{
String8 expected = str8_lit(".lcovfun$M");
String8 encoded[] = {
str8_lit("//AAAAAE"), str8_lit("//AAAAA+"), str8_lit("//AAAAA/"),
str8_lit("//AAAABA"), str8_lit("//AAmJZ/"), str8_lit("//AAmJaA"),
str8_lit("//AAyPM2"),
};
U64 offsets[] = {4, 62, 63, 64, 9999999, 10000000, 13169462};
String8 table = str8(push_array(arena, U8, 13169462 + 32), 13169462 + 32);
for EachElement(i, offsets) {
MemoryCopy(table.str + offsets[i], expected.str, expected.size + 1);
COFF_SectionHeader header = {0};
MemoryCopy(header.name, encoded[i].str, encoded[i].size);
T_Ok(str8_match(coff_name_from_section_header(table, &header), expected, 0));
if (offsets[i] <= 9999999) {
String8 decimal = str8f(arena, "/%llu", offsets[i]);
MemoryZeroStruct(&header);
MemoryCopy(header.name, decimal.str, decimal.size);
T_Ok(str8_match(coff_name_from_section_header(table, &header), expected, 0));
}
}
String8 invalid[] = {
str8_lit("//AAAAA!"),
str8_lit("//"),
str8_lit("////////"),
str8_lit("//AAAAAE"), // offset exactly at the end of the short table below
str8_lit("/9999999"),
};
String8 short_table = str8_prefix(table, 4);
for EachElement(i, invalid) {
COFF_SectionHeader header = {0};
MemoryCopy(header.name, invalid[i].str, invalid[i].size);
T_Ok(coff_name_from_section_header(short_table, &header).size == 0);
}
COFF_SectionHeader inline_header = {0};
MemoryCopy(inline_header.name, ".text$mn", 8);
T_Ok(str8_match(coff_name_from_section_header(str8_zero(), &inline_header), str8_lit(".text$mn"), 0));
MemoryZeroStruct(&inline_header);
T_Ok(coff_name_from_section_header(str8_zero(), &inline_header).size == 0);
}
#if 0
TEST(defer_imp_link)
{