test for machine compatibility

This commit is contained in:
Nikita Smith
2025-06-25 10:53:22 -07:00
committed by Ryan Fleury
parent b7a06f3f7f
commit b709640ddc
+76
View File
@@ -311,6 +311,81 @@ t_push_rdata_section(COFF_ObjWriter *obj_writer, String8 data)
return coff_obj_writer_push_section(obj_writer, str8_lit(".rdata"), PE_RDATA_SECTION_FLAGS, data); return coff_obj_writer_push_section(obj_writer, str8_lit(".rdata"), PE_RDATA_SECTION_FLAGS, data);
} }
////////////////////////////////
internal T_Result
t_machine_compat_check(void)
{
Temp scratch = scratch_begin(0,0);
T_Result result = T_Result_Fail;
{
COFF_ObjWriter *obj_writer = coff_obj_writer_alloc(0, COFF_MachineType_Unknown);
t_push_data_section(obj_writer, str8_lit("unknown"));
String8 obj = coff_obj_writer_serialize(scratch.arena, obj_writer);
coff_obj_writer_release(&obj_writer);
if (!t_write_file(str8_lit("unknown.obj"), obj)) {
goto exit;
}
}
{
COFF_ObjWriter *obj_writer = coff_obj_writer_alloc(0, COFF_MachineType_X64);
t_push_data_section(obj_writer, str8_lit("x64"));
String8 obj = coff_obj_writer_serialize(scratch.arena, obj_writer);
coff_obj_writer_release(&obj_writer);
if (!t_write_file(str8_lit("x64.obj"), obj)) {
goto exit;
}
}
{
COFF_ObjWriter *obj_writer = coff_obj_writer_alloc(0, COFF_MachineType_Arm64);
t_push_data_section(obj_writer, str8_lit("arm64"));
String8 obj = coff_obj_writer_serialize(scratch.arena, obj_writer);
coff_obj_writer_release(&obj_writer);
if (!t_write_file(str8_lit("arm64.obj"), obj)) {
goto exit;
}
}
{
COFF_ObjWriter *obj_writer = coff_obj_writer_alloc(0, COFF_MachineType_X64);
U8 text[] = { 0xC3 };
COFF_ObjSection *text_sect = t_push_text_section(obj_writer, str8_array_fixed(text));
coff_obj_writer_push_symbol_extern(obj_writer, str8_lit("my_entry"), 0, text_sect);
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;
linker_exit_code = t_invoke_linkerf("/subsystem:console /entry:my_entry /out:a.exe entry.obj unknown.obj x64.obj");
if (linker_exit_code != 0) {
goto exit;
}
linker_exit_code = t_invoke_linkerf("/subsystem:console /entry:my_entry /out:a.exe entry.obj unknown.obj x64.obj arm64.obj");
if (linker_exit_code == 0) {
goto exit;
}
linker_exit_code = t_invoke_linkerf("/subsystem:console /entry:my_entry /out:a.exe /machine:amd64 arm64.obj entry.obj");
if (linker_exit_code == 0) {
goto exit;
}
result = T_Result_Pass;
exit:;
scratch_end(scratch);
return result;
}
internal T_Result internal T_Result
t_simple_link_test(void) t_simple_link_test(void)
{ {
@@ -1889,6 +1964,7 @@ entry_point(CmdLine *cmdline)
char *label; char *label;
T_Result (*r)(void); T_Result (*r)(void);
} target_array[] = { } target_array[] = {
{ "machine_compat_check", t_machine_compat_check },
{ "simple_link_test", t_simple_link_test }, { "simple_link_test", t_simple_link_test },
{ "undef_section", t_undef_section }, { "undef_section", t_undef_section },
{ "undef_reloc_section", t_undef_reloc_section }, { "undef_reloc_section", t_undef_reloc_section },