test for relocation against removed COMDAT

This commit is contained in:
Nikita Smith
2025-06-25 10:53:24 -07:00
committed by Ryan Fleury
parent ad3e6e0b79
commit ba326924ee
2 changed files with 64 additions and 5 deletions
+1 -1
View File
@@ -2987,7 +2987,7 @@ THREAD_POOL_TASK_FUNC(lnk_obj_reloc_patcher)
if (symbol.section_number == LNK_REMOVED_SECTION_NUMBER_16 || symbol.section_number == LNK_REMOVED_SECTION_NUMBER_32) { if (symbol.section_number == LNK_REMOVED_SECTION_NUMBER_16 || symbol.section_number == LNK_REMOVED_SECTION_NUMBER_32) {
if (!lnk_is_coff_section_debug(obj, sect_idx)) { if (!lnk_is_coff_section_debug(obj, sect_idx)) {
String8 sect_name = coff_name_from_section_header(string_table, &section_table[sect_idx]); String8 sect_name = coff_name_from_section_header(string_table, &section_table[sect_idx]);
lnk_error_obj(LNK_Error_RelocationAgainstRemovedSection, obj, "relocating against symbol that is in a removed section (symbol: %S, reloc-section: %S 0x%llx, reloc: 0x%llx)", symbol.name, sect_name, sect_idx+1, reloc_idx); lnk_error_obj(LNK_Error_RelocationAgainstRemovedSection, obj, "relocating against symbol that is in a removed section (symbol: %S, reloc-section: %S %llxh, reloc-index: %llxh)", symbol.name, sect_name, sect_idx+1, reloc_idx);
} }
continue; continue;
} }
+63 -4
View File
@@ -2739,7 +2739,7 @@ exit:;
} }
internal T_Result internal T_Result
t_comdat_test(void) t_comdat_with_offset(void)
{ {
Temp scratch = scratch_begin(0,0); Temp scratch = scratch_begin(0,0);
T_Result result = T_Result_Fail; T_Result result = T_Result_Fail;
@@ -2782,6 +2782,65 @@ t_comdat_test(void)
} }
int linker_exit_code = t_invoke_linkerf("/subsystem:console /entry:entry /out:a.exe a.obj b.obj entry.obj"); int linker_exit_code = t_invoke_linkerf("/subsystem:console /entry:entry /out:a.exe a.obj b.obj entry.obj");
if (linker_exit_code != 0) { goto exit; }
exit:;
result = T_Result_Pass;
scratch_end(scratch);
return result;
}
internal T_Result
t_reloc_against_removed_comdat(void)
{
Temp scratch = scratch_begin(0,0);
T_Result result = T_Result_Fail;
{
COFF_ObjWriter *obj_writer = coff_obj_writer_alloc(0, COFF_MachineType_X64);
U8 a[] = "1Hello, World!";
COFF_ObjSection *sect = coff_obj_writer_push_section(obj_writer, str8_lit(".rdata"), PE_RDATA_SECTION_FLAGS|COFF_SectionFlag_LnkCOMDAT, str8_array_fixed(a));
coff_obj_writer_push_symbol_secdef(obj_writer, sect, COFF_ComdatSelect_Largest);
coff_obj_writer_push_symbol_extern(obj_writer, str8_lit("TEST"), 1, sect);
String8 obj = coff_obj_writer_serialize(scratch.arena, obj_writer);
coff_obj_writer_release(&obj_writer);
if (!t_write_file(str8_lit("a.obj"), obj)) { goto exit; }
}
{
COFF_ObjWriter *obj_writer = coff_obj_writer_alloc(0, COFF_MachineType_X64);
U8 a[] = "H";
COFF_ObjSection *comdat_sect = coff_obj_writer_push_section(obj_writer, str8_lit(".rdata"), PE_RDATA_SECTION_FLAGS|COFF_SectionFlag_LnkCOMDAT, str8_array_fixed(a));
coff_obj_writer_push_symbol_secdef(obj_writer, comdat_sect, COFF_ComdatSelect_Largest);
coff_obj_writer_push_symbol_extern(obj_writer, str8_lit("TEST"), 1, comdat_sect);
COFF_ObjSymbol *static_symbol = coff_obj_writer_push_symbol_static(obj_writer, str8_lit("STATIC"), 2, comdat_sect);
U8 rdata[4] = {0};
COFF_ObjSection *regular_sect = coff_obj_writer_push_section(obj_writer, str8_lit(".rdata"), PE_RDATA_SECTION_FLAGS, str8_array_fixed(rdata));
coff_obj_writer_section_push_reloc_voff(obj_writer, regular_sect, 0, static_symbol);
String8 obj = coff_obj_writer_serialize(scratch.arena, obj_writer);
coff_obj_writer_release(&obj_writer);
if (!t_write_file(str8_lit("b.obj"), obj)) { goto exit; }
}
{
COFF_ObjWriter *obj_writer = coff_obj_writer_alloc(0, COFF_MachineType_X64);
U8 text[] = {
0x48, 0xC7, 0xC0, 0x00, 0x00, 0x00, 0x00, // mov rax, $imm
0xC3 // ret
};
COFF_ObjSection *sect = coff_obj_writer_push_section(obj_writer, str8_lit(".text"), PE_TEXT_SECTION_FLAGS, str8_array_fixed(text));
coff_obj_writer_push_symbol_extern(obj_writer, str8_lit("entry"), 0, sect);
COFF_ObjSymbol *symbol = coff_obj_writer_push_symbol_undef(obj_writer, str8_lit("TEST"));
coff_obj_writer_section_push_reloc_voff(obj_writer, sect, 3, symbol);
String8 obj = coff_obj_writer_serialize(scratch.arena, obj_writer);
coff_obj_writer_release(&obj_writer);
if (!t_write_file(str8_lit("entry.obj"), obj)) { goto exit; }
}
int linker_exit_code = t_invoke_linkerf("/subsystem:console /entry:entry /out:a.exe a.obj c.obj entry.obj");
if (linker_exit_code != LNK_Error_RelocationAgainstRemovedSection) { goto exit; }
exit:; exit:;
result = T_Result_Pass; result = T_Result_Pass;
@@ -3618,8 +3677,7 @@ entry_point(CmdLine *cmdline)
// Targets // Targets
// //
static struct { static struct {
char *label; char *label; T_Result (*r)(void);
T_Result (*r)(void);
} target_array[] = { } target_array[] = {
{ "simple_link_test", t_simple_link_test }, { "simple_link_test", t_simple_link_test },
{ "machine_compat_check", t_machine_compat_check }, { "machine_compat_check", t_machine_compat_check },
@@ -3652,7 +3710,8 @@ entry_point(CmdLine *cmdline)
{ "comdat_associative_loop", t_comdat_associative_loop }, { "comdat_associative_loop", t_comdat_associative_loop },
{ "comdat_associative_non_comdat", t_comdat_associative_non_comdat }, { "comdat_associative_non_comdat", t_comdat_associative_non_comdat },
{ "comdat_associative_out_of_bounds", t_comdat_associative_out_of_bounds }, { "comdat_associative_out_of_bounds", t_comdat_associative_out_of_bounds },
{ "comdat_test", t_comdat_test }, { "comdat_with_offset", t_comdat_with_offset },
{ "reloc_against_removed_comdat", t_reloc_against_removed_comdat },
{ "alt_name", t_alt_name }, { "alt_name", t_alt_name },
{ "include", t_include }, { "include", t_include },
{ "communal_var_vs_regular_comdat", t_communal_var_vs_regular_comdat }, { "communal_var_vs_regular_comdat", t_communal_var_vs_regular_comdat },