keep COFF input metadata immutable

This commit is contained in:
Nikita Smith committed 2026-08-24 22:27:19 -07:00
1 parent 993df2d9d8
commit cf67aca7da
5 files changed
+19 -14

No files matched your search

+2 -2
View File
@@ -4940,7 +4940,7 @@ THREAD_POOL_TASK_FUNC(lnk_patch_virtual_offsets_and_sizes_in_obj_section_headers
LNK_Obj *obj = task->objs[obj_idx];
ProfBeginV("Patch Virtual Offset And Size In Section Headers [%S]", obj->path);
COFF_SectionHeader *section_table = (COFF_SectionHeader *)str8_substr(obj->data, obj->header.section_table_range).str;
COFF_SectionHeader *section_table = lnk_coff_section_table_from_obj(obj);
for (U64 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) {
COFF_SectionHeader *sect_header = &section_table[sect_idx];
if (~obj->section_flags[sect_idx] & COFF_SectionFlag_LnkRemove) {
@@ -4961,7 +4961,7 @@ THREAD_POOL_TASK_FUNC(lnk_patch_file_offsets_and_sizes_in_obj_section_headers_ta
LNK_Obj *obj = task->objs[obj_idx];
ProfBeginV("Patch File Offsets And Sizes In Obj Section Headers [%S]", obj->path);
COFF_SectionHeader *section_table = (COFF_SectionHeader *)str8_substr(obj->data, obj->header.section_table_range).str;
COFF_SectionHeader *section_table = lnk_coff_section_table_from_obj(obj);
for (U64 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) {
COFF_SectionHeader *sect_header = &section_table[sect_idx];
COFF_SectionFlags sect_flags = obj->section_flags[sect_idx];
+4 -8
View File
@@ -71,23 +71,19 @@ lnk_lib_from_data(Arena *arena, String8 data, String8 path, U64 input_idx, LNK_L
symbol_count = first_member.symbol_count;
// convert big endian offsets
for (U32 offset_idx = 0; offset_idx < symbol_count; offset_idx += 1) {
first_member.member_offsets[offset_idx] = from_be_u32(first_member.member_offsets[offset_idx]);
}
// compress member offsets to match those from the second header
{
HashTable *member_off_ht = hash_table_init(scratch.arena, (U64)((F64)first_member.symbol_count * 1.3));
for EachIndex(symbol_idx, symbol_count) {
if (!hash_table_search_u32_u32(member_off_ht, first_member.member_offsets[symbol_idx], 0)) {
hash_table_push_u32_u32(scratch.arena, member_off_ht, first_member.member_offsets[symbol_idx], member_off_ht->count);
U32 member_off = from_be_u32(first_member.member_offsets[symbol_idx]);
if (!hash_table_search_u32_u32(member_off_ht, member_off, 0)) {
hash_table_push_u32_u32(scratch.arena, member_off_ht, member_off, member_off_ht->count);
}
}
symbol_indices = push_array(arena, U16, first_member.symbol_count);
for EachIndex(symbol_idx, first_member.symbol_count) {
U32 member_off = first_member.member_offsets[symbol_idx];
U32 member_off = from_be_u32(first_member.member_offsets[symbol_idx]);
U32 member_off_idx = 0;
if (!hash_table_search_u32_u32(member_off_ht, member_off, &member_off_idx)) {
InvalidPath;
+6 -3
View File
@@ -93,6 +93,9 @@ THREAD_POOL_TASK_FUNC(lnk_obj_initer)
lnk_error_input_obj(LNK_Error_IllData, input, "corrupted file, unable to read string table");
}
COFF_SectionHeader *section_headers = push_array_no_zero(arena, COFF_SectionHeader, header.section_count_no_null);
MemoryCopy(section_headers, raw_coff_section_table.str, raw_coff_section_table.size);
//
// section table pass
// - error check headers fields
@@ -381,6 +384,7 @@ THREAD_POOL_TASK_FUNC(lnk_obj_initer)
obj->data = input->data;
obj->path = push_str8_copy(arena, input->path);
obj->header = header;
obj->section_headers = section_headers;
obj->section_flags = section_flags;
obj->symbols = symbols;
obj->comdats = comdats;
@@ -670,7 +674,7 @@ lnk_obj_get_comdat_symlink(LNK_Obj *obj, U64 section_number, LNK_ObjSymbolRef *s
internal COFF_SectionHeader *
lnk_coff_section_table_from_obj(LNK_Obj *obj)
{
return (COFF_SectionHeader *)str8_substr(obj->data, obj->header.section_table_range).str;
return obj->section_headers;
}
internal U64
@@ -773,8 +777,7 @@ lnk_coff_section_header_from_section_number(LNK_Obj *obj, U64 section_number)
{
Assert(section_number > 0);
U64 sect_idx = section_number - 1;
COFF_SectionHeader *section_table = str8_deserial_get_raw_ptr(obj->data, obj->header.section_table_range.min, dim_1u64(obj->header.section_table_range));
return &section_table[sect_idx];
return &obj->section_headers[sect_idx];
}
internal force_inline U64
+1
View File
@@ -33,6 +33,7 @@ typedef struct LNK_Obj
String8 data;
COFF_FileHeaderInfo header;
COFF_SectionHeader *section_headers;
COFF_SectionFlags *section_flags;
LNK_ObjSymbolArray symbols;
+6 -1
View File
@@ -5085,8 +5085,13 @@ TEST(first_member_header)
}));
T_Ok(t_write_entry_obj());
t_invoke_linkerf("/subsystem:console /entry:entry /out:a.exe test.lib entry.obj /include:1 /include:2 /include:3 /include:4 /include:5 /include:6 /include:7 /include:8 /include:9");
String8 lib_before = t_read_file(arena, str8_lit("test.lib"));
String8 obj_before = t_read_file(arena, str8_lit("entry.obj"));
t_invoke_linkerf("/RAD_MEMORY_MAP_FILES:READ_WRITE /subsystem:console /entry:entry /out:a.exe test.lib entry.obj /include:1 /include:2 /include:3 /include:4 /include:5 /include:6 /include:7 /include:8 /include:9");
T_Ok(g_last_exit_code == 0);
T_Ok(str8_match(lib_before, t_read_file(arena, str8_lit("test.lib")), 0));
T_Ok(str8_match(obj_before, t_read_file(arena, str8_lit("entry.obj")), 0));
}
TEST(second_member_header)