mirror of
https://github.com/Ed94/raddebugger.git
synced 2026-11-17 19:50:12 -08:00
radlink: implement COFF_ImportBy_Undecorate
This commit is contained in:
1 parent
3e7b11fd1b
commit
000997d3af
3 files changed
+157
-4
No files matched your search
@@ -4909,6 +4909,146 @@ TEST(communal_var_vs_regular_comdat)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(import_undecorate)
|
||||
{
|
||||
// Keep the decorated COFF symbols, but use the undecorated DLL export name
|
||||
// in the hint/name table. An empty ILT/IAT still links, but calls through its
|
||||
// jump thunk reach address zero at runtime.
|
||||
struct {
|
||||
char *symbol_name;
|
||||
char *export_name;
|
||||
} cases[] = {
|
||||
{ "?CppImport@@YAHXZ", "CppImport" },
|
||||
{ "_StdcallImport@8", "StdcallImport" },
|
||||
{ "@FastcallImport@8", "FastcallImport" },
|
||||
{ "PlainImport", "PlainImport" },
|
||||
};
|
||||
struct {
|
||||
char *args;
|
||||
B32 delayed;
|
||||
} opts[] = {
|
||||
{ "/opt:ref", 0 },
|
||||
{ "/opt:noref", 0 },
|
||||
{ "/opt:ref /delayload:undecorate.dll", 1 },
|
||||
{ "/opt:noref /delayload:undecorate.dll", 1 },
|
||||
};
|
||||
|
||||
for EachElement(case_idx, cases) {
|
||||
String8 symbol_name = str8_cstring(cases[case_idx].symbol_name);
|
||||
String8 iat_name = str8f(arena, "__imp_%S", symbol_name);
|
||||
U16 hint = 17;
|
||||
|
||||
T_Ok(t_write_def_lib("undecorate.lib", (T_COFF_DefLib){
|
||||
.emit_second_member = 1,
|
||||
.members = (T_COFF_DefLibMember[]){
|
||||
{
|
||||
.type = T_COFF_DefLibMember_Import,
|
||||
.import = { "undecorate.dll", cases[case_idx].symbol_name,
|
||||
COFF_ImportBy_Undecorate, COFF_ImportHeader_Code,
|
||||
.hit_or_ordinal = hint },
|
||||
},
|
||||
{0},
|
||||
},
|
||||
}));
|
||||
T_Ok(t_write_def_obj("entry.obj", (T_COFF_DefObj){
|
||||
.sections = (T_COFF_DefSection[]){
|
||||
{
|
||||
"text", ".text",
|
||||
str8_lit_comp(
|
||||
"\x48\x83\xEC\x28" // sub rsp,28h
|
||||
"\xE8\0\0\0\0" // call decorated function's jump thunk
|
||||
"\xFF\x15\0\0\0\0" // call [__imp_decorated_function]
|
||||
"\x48\x83\xC4\x28\xC3" // add rsp,28h; ret
|
||||
),
|
||||
.flags = "rx:code",
|
||||
.relocs = (T_COFF_DefReloc[]){
|
||||
T_COFF_DefReloc(X64_Rel32, 5, cases[case_idx].symbol_name),
|
||||
T_COFF_DefReloc(X64_Rel32, 11, (char *)iat_name.str),
|
||||
{0},
|
||||
},
|
||||
},
|
||||
{0},
|
||||
},
|
||||
.symbols = (T_COFF_DefSymbol[]){
|
||||
T_COFF_DefSymbol_ExternFunc("entry", "text", 0),
|
||||
// Link-only test: the delay helper only needs to resolve to a ret.
|
||||
T_COFF_DefSymbol_ExternFunc("__delayLoadHelper2", "text", 19),
|
||||
T_COFF_DefSymbol_UndefFunc(cases[case_idx].symbol_name),
|
||||
T_COFF_DefSymbol_Undef((char *)iat_name.str),
|
||||
{0},
|
||||
},
|
||||
}));
|
||||
|
||||
for EachElement(opt_idx, opts) {
|
||||
t_infof("Undecorate import %S -> %s (%s)\n", symbol_name, cases[case_idx].export_name, opts[opt_idx].args);
|
||||
t_invoke_linkerf("/nodefaultlib /subsystem:console /entry:entry /out:undecorate.exe %s entry.obj undecorate.lib", opts[opt_idx].args);
|
||||
T_Ok(g_last_exit_code == 0);
|
||||
|
||||
String8 image = t_read_file(arena, str8_lit("undecorate.exe"));
|
||||
PE_BinInfo bin = pe_bin_info_from_data(arena, image);
|
||||
COFF_SectionHeader *sections = (COFF_SectionHeader *)(image.str + bin.section_table_range.min);
|
||||
PE_ParsedImport *import = 0;
|
||||
U64 iat_voff = 0, ilt_voff = 0;
|
||||
|
||||
if (opts[opt_idx].delayed) {
|
||||
T_Ok(bin.data_dir_count > PE_DataDirectoryIndex_DELAY_IMPORT);
|
||||
PE_ParsedDelayImportTable imports = pe_delay_imports_from_data(arena, bin.is_pe32, bin.section_count, sections, image, bin.data_dir_franges[PE_DataDirectoryIndex_DELAY_IMPORT]);
|
||||
T_Ok(imports.count == 1);
|
||||
T_Ok(str8_match(imports.v[0].name, str8_lit("undecorate.dll"), 0));
|
||||
T_Ok(imports.v[0].import_count == 1);
|
||||
import = &imports.v[0].imports[0];
|
||||
iat_voff = imports.v[0].iat_voff;
|
||||
ilt_voff = imports.v[0].name_table_voff;
|
||||
} else {
|
||||
T_Ok(bin.data_dir_count > PE_DataDirectoryIndex_IMPORT);
|
||||
PE_ParsedStaticImportTable imports = pe_static_imports_from_data(arena, bin.is_pe32, bin.section_count, sections, image, bin.data_dir_franges[PE_DataDirectoryIndex_IMPORT]);
|
||||
T_Ok(imports.count == 1);
|
||||
T_Ok(str8_match(imports.v[0].name, str8_lit("undecorate.dll"), 0));
|
||||
T_Ok(imports.v[0].import_count == 1);
|
||||
import = &imports.v[0].imports[0];
|
||||
iat_voff = imports.v[0].import_address_table_voff;
|
||||
ilt_voff = imports.v[0].import_name_table_voff;
|
||||
}
|
||||
T_Ok(import->type == PE_ParsedImport_Name);
|
||||
|
||||
// Delay-load linkers may replace the advisory import hint with zero.
|
||||
if (!opts[opt_idx].delayed) {
|
||||
T_Ok(import->u.name.hint == hint);
|
||||
}
|
||||
T_Ok(str8_match(import->u.name.string, str8_cstring(cases[case_idx].export_name), 0));
|
||||
|
||||
// Both call forms must resolve to the same populated IAT slot.
|
||||
U64 entry_off = pe_foff_from_voff(image, &bin, bin.entry_point);
|
||||
S32 call_disp = 0, iat_disp = 0, thunk_disp = 0;
|
||||
T_Ok(str8_deserial_read_struct(image, entry_off + 5, &call_disp) == sizeof(call_disp));
|
||||
T_Ok(str8_deserial_read_struct(image, entry_off + 11, &iat_disp) == sizeof(iat_disp));
|
||||
|
||||
U64 thunk_voff = (U64)((S64)bin.entry_point + 9 + call_disp);
|
||||
U64 thunk_off = pe_foff_from_voff(image, &bin, thunk_voff);
|
||||
T_Ok(str8_match(str8_substr(image, rng_1u64(thunk_off, thunk_off + 2)), str8_lit("\xFF\x25"), 0));
|
||||
T_Ok(str8_deserial_read_struct(image, thunk_off + 2, &thunk_disp) == sizeof(thunk_disp));
|
||||
T_Ok((U64)((S64)bin.entry_point + 15 + iat_disp) == iat_voff);
|
||||
T_Ok((U64)((S64)thunk_voff + 6 + thunk_disp) == iat_voff);
|
||||
|
||||
U64 iat_off = pe_foff_from_voff(image, &bin, iat_voff);
|
||||
U64 ilt_off = pe_foff_from_voff(image, &bin, ilt_voff);
|
||||
U64 iat_entry = 0, iat_end = 1, ilt_entry = 0;
|
||||
T_Ok(str8_deserial_read_struct(image, iat_off, &iat_entry) == sizeof(iat_entry));
|
||||
T_Ok(str8_deserial_read_struct(image, iat_off + 8, &iat_end) == sizeof(iat_end));
|
||||
T_Ok(str8_deserial_read_struct(image, ilt_off, &ilt_entry) == sizeof(ilt_entry));
|
||||
T_Ok(iat_entry != 0 && iat_end == 0);
|
||||
|
||||
if (opts[opt_idx].delayed) {
|
||||
// Delay-load IAT entries start at the load thunk, rather than the name.
|
||||
T_Ok(iat_entry >= bin.image_base);
|
||||
T_Ok(pe_foff_from_voff(image, &bin, iat_entry - bin.image_base) != 0);
|
||||
} else {
|
||||
T_Ok(iat_entry == ilt_entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(link_large_import_object)
|
||||
{
|
||||
// Each named import creates three sections in the synthesized DLL object.
|
||||
|
||||
@@ -17,6 +17,16 @@ pe_make_import_header_list_push(Arena *arena, PE_MakeImportList *list, PE_MakeIm
|
||||
return node;
|
||||
}
|
||||
|
||||
internal String8
|
||||
pe_undecorate_import_name(String8 name)
|
||||
{
|
||||
// COFF IMPORT_NAME_UNDECORATE strips one prefix character and the suffix starting at the first '@'
|
||||
if (name.size && (name.str[0] == '?' || name.str[0] == '@' || name.str[0] == '_')) {
|
||||
name = str8_skip(name, 1);
|
||||
}
|
||||
return str8_prefix(name, str8_find_needle(name, 0, str8_lit("@"), 0));
|
||||
}
|
||||
|
||||
internal COFF_ObjSymbol *
|
||||
pe_make_indirect_jump_thunk_x64(COFF_ObjWriter *obj_writer, COFF_ObjSection *code_sect, COFF_ObjSymbol *iat_symbol, String8 thunk_name)
|
||||
{
|
||||
@@ -286,11 +296,13 @@ pe_make_import_dll_obj_static(Arena *arena, COFF_TimeStamp time_stamp, COFF_Mach
|
||||
str8_list_push(obj_writer->arena, &ilt_sect->data, ordinal_data);
|
||||
str8_list_push(obj_writer->arena, &iat_sect->data, ordinal_data);
|
||||
} break;
|
||||
case COFF_ImportBy_Undecorate:
|
||||
case COFF_ImportBy_Name: {
|
||||
COFF_ObjSection *int_sect = coff_obj_writer_push_section(obj_writer, str8_lit(".idata$6"), PE_IDATA_SECTION_FLAGS|COFF_SectionFlag_Align2Bytes|comdat_flags, str8_zero());
|
||||
coff_obj_writer_push_symbol_associative(obj_writer, int_sect, iat_sect);
|
||||
COFF_ObjSymbol *int_symbol = coff_obj_writer_push_symbol_static(obj_writer, int_sect->name, 0, int_sect);
|
||||
String8 int_data = coff_make_import_lookup(obj_writer->arena, import_header.hint_or_ordinal, import_header.func_name);
|
||||
String8 lookup_name = import_header.import_by == COFF_ImportBy_Undecorate ? pe_undecorate_import_name(import_header.func_name) : import_header.func_name;
|
||||
String8 int_data = coff_make_import_lookup(obj_writer->arena, import_header.hint_or_ordinal, lookup_name);
|
||||
str8_list_push(obj_writer->arena, &int_sect->data, int_data);
|
||||
str8_list_push_aligner(obj_writer->arena, &int_sect->data, 0, 2);
|
||||
|
||||
@@ -302,7 +314,6 @@ pe_make_import_dll_obj_static(Arena *arena, COFF_TimeStamp time_stamp, COFF_Mach
|
||||
str8_list_push(obj_writer->arena, &ilt_sect->data, str8_array(import_entry, import_size));
|
||||
str8_list_push(obj_writer->arena, &iat_sect->data, str8_array(import_entry, import_size));
|
||||
} break;
|
||||
case COFF_ImportBy_Undecorate: { NotImplemented; } break;
|
||||
case COFF_ImportBy_NameNoPrefix: { NotImplemented; } break;
|
||||
default: { InvalidPath; } break;
|
||||
}
|
||||
@@ -461,9 +472,11 @@ pe_make_import_dll_obj_delayed(Arena *arena, COFF_TimeStamp time_stamp, COFF_Mac
|
||||
coff_obj_writer_section_push_reloc_addr(obj_writer, uiat_sect, uiat_offset, load_thunk_symbol);
|
||||
}
|
||||
} break;
|
||||
case COFF_ImportBy_Undecorate:
|
||||
case COFF_ImportBy_Name: {
|
||||
// put together name look up entry
|
||||
String8 int_data = coff_make_import_lookup(obj_writer->arena, import_header.hint_or_ordinal, import_header.func_name);
|
||||
String8 lookup_name = import_header.import_by == COFF_ImportBy_Undecorate ? pe_undecorate_import_name(import_header.func_name) : import_header.func_name;
|
||||
String8 int_data = coff_make_import_lookup(obj_writer->arena, import_header.hint_or_ordinal, lookup_name);
|
||||
U64 int_data_offset = int_sect->data.total_size;
|
||||
str8_list_push(obj_writer->arena, &int_sect->data, int_data);
|
||||
|
||||
@@ -503,7 +516,6 @@ pe_make_import_dll_obj_delayed(Arena *arena, COFF_TimeStamp time_stamp, COFF_Mac
|
||||
coff_obj_writer_section_push_reloc_addr(obj_writer, uiat_sect, uiat_data_offset, load_thunk_symbol);
|
||||
}
|
||||
} break;
|
||||
case COFF_ImportBy_Undecorate: { NotImplemented; } break;
|
||||
case COFF_ImportBy_NameNoPrefix: { NotImplemented; } break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ typedef struct PE_MakeImportList
|
||||
|
||||
internal void pe_make_import_header_list_push_node(PE_MakeImportList *list, PE_MakeImportNode *node);
|
||||
internal PE_MakeImportNode * pe_make_import_header_list_push(Arena *arena, PE_MakeImportList *list, PE_MakeImport header);
|
||||
internal String8 pe_undecorate_import_name(String8 name);
|
||||
|
||||
internal COFF_ObjSymbol * pe_make_indirect_jump_thunk_x64(COFF_ObjWriter *obj_writer, COFF_ObjSection *code_sect, COFF_ObjSymbol *iat_symbol, String8 thunk_name);
|
||||
internal COFF_ObjSymbol * pe_make_load_thunk_x64(COFF_ObjWriter *obj_writer, COFF_ObjSection *code_sect, COFF_ObjSymbol *imp_addr_ptr, COFF_ObjSymbol *tail_merge, String8 func_name);
|
||||
|
||||
Reference in new issue
Block a user