match LINK resource section layout

This commit is contained in:
Nikita Smith committed 2026-08-25 00:10:49 -07:00
1 parent a11e2bf57b
commit f6116bbede
2 files changed
+106 -145

No files matched your search

+103 -142
View File
@@ -212,14 +212,14 @@ lnk_make_linker_manifest(Arena *arena,
String8List srl = {0}; String8List srl = {0};
str8_serial_begin(scratch.arena, &srl); str8_serial_begin(scratch.arena, &srl);
str8_serial_push_string(scratch.arena, &srl, str8_lit( str8_serial_push_string(scratch.arena, &srl, str8_lit(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\r\n"
"<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">\r\n")); "<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>\r\n"));
if (manifest_uac) { if (manifest_uac) {
String8 uac = push_str8f(scratch.arena, String8 uac = push_str8f(scratch.arena,
" <trustInfo xmlns=\"urn:schemas-microsoft-com:asm.v3\">\r\n" " <trustInfo xmlns=\"urn:schemas-microsoft-com:asm.v3\">\r\n"
" <security>\r\n" " <security>\r\n"
" <requestedPrivileges>\r\n" " <requestedPrivileges>\r\n"
" <requestedExecutionLevel level=%S uiAccess=%S></requestedExecutionLevel>\r\n" " <requestedExecutionLevel level=%S uiAccess=%S />\r\n"
" </requestedPrivileges>\r\n" " </requestedPrivileges>\r\n"
" </security>\r\n" " </security>\r\n"
" </trustInfo>\r\n", " </trustInfo>\r\n",
@@ -231,7 +231,7 @@ lnk_make_linker_manifest(Arena *arena,
String8 dep = push_str8f(scratch.arena, String8 dep = push_str8f(scratch.arena,
" <dependency>\r\n" " <dependency>\r\n"
" <dependentAssembly>\r\n" " <dependentAssembly>\r\n"
" <assemblyIdentity %S></assemblyIdentity>\r\n" " <assemblyIdentity %S />\r\n"
" </dependentAssembly>\r\n" " </dependentAssembly>\r\n"
" </dependency>\r\n", " </dependency>\r\n",
node->string); node->string);
@@ -400,156 +400,117 @@ lnk_serialize_pe_resource_tree(COFF_ObjWriter *obj_writer, PE_ResourceDir *root_
ProfBeginFunction(); ProfBeginFunction();
Temp scratch = scratch_begin(0, 0); Temp scratch = scratch_begin(0, 0);
struct Stack { struct QueueNode {
struct Stack *next; struct QueueNode *next;
U64 arr_idx; PE_Resource *res;
U64 res_idx[2]; COFF_ResourceDirEntry *coff_entry;
PE_ResourceArray res_arr[2];
COFF_ResourceDirEntry *coff_entry_arr[2];
}; };
struct Stack *stack = push_array(scratch.arena, struct Stack, 1); struct QueueNode *queue_first = 0;
// init stack struct QueueNode *queue_last = 0;
{
PE_Resource *root_wrapper = push_array(scratch.arena, PE_Resource, 1);
root_wrapper->id.type = COFF_ResourceIDType_Number;
root_wrapper->id.u.number = 0;
root_wrapper->kind = PE_ResDataKind_DIR;
root_wrapper->u.dir = root_dir;
COFF_ResourceDirEntry *root_dir = push_array(scratch.arena, COFF_ResourceDirEntry, 1); PE_Resource *root_wrapper = push_array(scratch.arena, PE_Resource, 1);
root_wrapper->id.type = COFF_ResourceIDType_Number;
root_wrapper->id.u.number = 0;
root_wrapper->kind = PE_ResDataKind_DIR;
root_wrapper->u.dir = root_dir;
stack->res_arr[0].count = 1; COFF_ResourceDirEntry *root_entry = push_array(scratch.arena, COFF_ResourceDirEntry, 1);
stack->res_arr[0].v = root_wrapper; struct QueueNode *root_node = push_array(scratch.arena, struct QueueNode, 1);
root_node->res = root_wrapper;
stack->coff_entry_arr[0] = root_dir; root_node->coff_entry = root_entry;
stack->coff_entry_arr[1] = 0; SLLQueuePush(queue_first, queue_last, root_node);
}
COFF_ObjSection *rsrc1 = coff_obj_writer_push_section(obj_writer, str8_lit(".rsrc$01"), PE_RSRC1_SECTION_FLAGS, str8_zero()); COFF_ObjSection *rsrc1 = coff_obj_writer_push_section(obj_writer, str8_lit(".rsrc$01"), PE_RSRC1_SECTION_FLAGS, str8_zero());
COFF_ObjSection *rsrc2 = coff_obj_writer_push_section(obj_writer, str8_lit(".rsrc$02"), PE_RSRC2_SECTION_FLAGS, str8_zero()); COFF_ObjSection *rsrc2 = coff_obj_writer_push_section(obj_writer, str8_lit(".rsrc$02"), PE_RSRC2_SECTION_FLAGS, str8_zero());
for (; stack; ) { for (struct QueueNode *node = queue_first; node != 0; node = queue_first) {
for (; stack->arr_idx < ArrayCount(stack->res_arr); stack->arr_idx += 1) { SLLQueuePop(queue_first, queue_last);
for (; stack->res_idx[stack->arr_idx] < stack->res_arr[stack->arr_idx].count; ) { PE_Resource *res = node->res;
U64 res_idx = stack->res_idx[stack->arr_idx]++;
PE_Resource *res = &stack->res_arr[stack->arr_idx].v[res_idx];
{ node->coff_entry->id.data_entry_offset = safe_cast_u32(rsrc1->data.total_size);
COFF_ResourceDirEntry *coff_entry = &stack->coff_entry_arr[stack->arr_idx][res_idx]; if (res->kind == PE_ResDataKind_DIR) {
node->coff_entry->id.data_entry_offset |= COFF_Resource_SubDirFlag;
}
switch (res->kind) {
case PE_ResDataKind_DIR: {
COFF_ResourceDirTable *dir_header = push_array(obj_writer->arena, COFF_ResourceDirTable, 1);
dir_header->characteristics = res->u.dir->characteristics;
dir_header->time_stamp = res->u.dir->time_stamp;
dir_header->major_version = res->u.dir->major_version;
dir_header->minor_version = res->u.dir->minor_version;
dir_header->name_entry_count = res->u.dir->named_list.count;
dir_header->id_entry_count = res->u.dir->id_list.count;
PE_ResourceArray named_array;
PE_ResourceArray id_array;
{
Temp scratch2 = scratch_begin(&scratch.arena, 1);
named_array = pe_resource_list_to_array(scratch2.arena, &res->u.dir->named_list);
id_array = pe_resource_list_to_array(scratch2.arena, &res->u.dir->id_list);
PE_ResourcePtrArray named_ptr_array = pe_resource_ptr_from_array(scratch2.arena, named_array);
PE_ResourcePtrArray id_ptr_array = pe_resource_ptr_from_array(scratch2.arena, id_array);
radsort(named_ptr_array.v, named_ptr_array.count, lnk_res_string_id_is_before);
radsort(id_ptr_array.v, id_ptr_array.count, lnk_res_number_id_is_before);
named_array = pe_resource_from_ptr_array(scratch.arena, named_ptr_array);
id_array = pe_resource_from_ptr_array(scratch.arena, id_ptr_array);
scratch_end(scratch2);
}
// assign entry data offset COFF_ResourceDirEntry *named_entries = push_array(obj_writer->arena, COFF_ResourceDirEntry, named_array.count);
coff_entry->id.data_entry_offset = safe_cast_u32(rsrc1->data.total_size); COFF_ResourceDirEntry *id_entries = push_array(obj_writer->arena, COFF_ResourceDirEntry, id_array.count);
str8_list_push(obj_writer->arena, &rsrc1->data, str8_struct(dir_header));
// set directory flag str8_list_push(obj_writer->arena, &rsrc1->data, str8_array(named_entries, named_array.count));
if (res->kind == PE_ResDataKind_DIR) { str8_list_push(obj_writer->arena, &rsrc1->data, str8_array(id_entries, id_array.count));
coff_entry->id.data_entry_offset |= COFF_Resource_SubDirFlag;
} for (U64 i = 0; i < named_array.count; i += 1) {
} PE_Resource *child = &named_array.v[i];
COFF_ResourceDirEntry *entry = &named_entries[i];
switch (res->kind) { U32 name_off = safe_cast_u32(rsrc1->data.total_size);
case PE_ResDataKind_DIR: { String8 name = coff_resource_string_from_str8(obj_writer->arena, child->id.u.string);
// fill out directory header str8_list_push(obj_writer->arena, &rsrc1->data, name);
COFF_ResourceDirTable *dir_header = push_array(obj_writer->arena, COFF_ResourceDirTable, 1); entry->name.offset = COFF_Resource_SubDirFlag | name_off;
dir_header->characteristics = res->u.dir->characteristics;
dir_header->time_stamp = res->u.dir->time_stamp; struct QueueNode *child_node = push_array(scratch.arena, struct QueueNode, 1);
dir_header->major_version = res->u.dir->major_version; child_node->res = child;
dir_header->minor_version = res->u.dir->minor_version; child_node->coff_entry = entry;
dir_header->name_entry_count = res->u.dir->named_list.count; SLLQueuePush(queue_first, queue_last, child_node);
dir_header->id_entry_count = res->u.dir->id_list.count; }
for (U64 i = 0; i < id_array.count; i += 1) {
// sort input resources PE_Resource *child = &id_array.v[i];
PE_ResourceArray named_array; COFF_ResourceDirEntry *entry = &id_entries[i];
PE_ResourceArray id_array; entry->name.id = child->id.u.number;
{
Temp scratch2 = scratch_begin(&scratch.arena, 1); struct QueueNode *child_node = push_array(scratch.arena, struct QueueNode, 1);
child_node->res = child;
named_array = pe_resource_list_to_array(scratch2.arena, &res->u.dir->named_list); child_node->coff_entry = entry;
id_array = pe_resource_list_to_array(scratch2.arena, &res->u.dir->id_list); SLLQueuePush(queue_first, queue_last, child_node);
}
PE_ResourcePtrArray named_ptr_array = pe_resource_ptr_from_array(scratch2.arena, named_array); } break;
PE_ResourcePtrArray id_ptr_array = pe_resource_ptr_from_array(scratch2.arena, id_array);
case PE_ResDataKind_COFF_RESOURCE: {
radsort(named_ptr_array.v, named_ptr_array.count, lnk_res_string_id_is_before); COFF_ResourceDataEntry *coff_res = push_array(obj_writer->arena, COFF_ResourceDataEntry, 1);
radsort(id_ptr_array.v, id_ptr_array.count, lnk_res_number_id_is_before); coff_res->data_size = res->u.coff_res.data.size;
coff_res->data_voff = 0;
named_array = pe_resource_from_ptr_array(scratch.arena, named_ptr_array); coff_res->code_page = 0;
id_array = pe_resource_from_ptr_array(scratch.arena, id_ptr_array);
if (res->u.coff_res.data.size >= sizeof(U32)) {
scratch_end(scratch2); U32 resdat_off = safe_cast_u32(rsrc2->data.total_size);
} COFF_ObjSymbol *resdat = coff_obj_writer_push_symbol_static(obj_writer, str8_lit("resdat"), resdat_off, rsrc2);
U64 apply_off = rsrc1->data.total_size + OffsetOf(COFF_ResourceDataEntry, data_voff);
coff_obj_writer_section_push_reloc_voff(obj_writer, rsrc1, safe_cast_u32(apply_off), resdat);
}
// allocate COFF entries str8_list_push(obj_writer->arena, &rsrc1->data, str8_struct(coff_res));
COFF_ResourceDirEntry *named_entries = push_array(obj_writer->arena, COFF_ResourceDirEntry, named_array.count); str8_list_push(obj_writer->arena, &rsrc2->data, res->u.coff_res.data);
COFF_ResourceDirEntry *id_entries = push_array(obj_writer->arena, COFF_ResourceDirEntry, id_array.count); str8_list_push_aligner(obj_writer->arena, &rsrc2->data, 0, 8);
} break;
// push header and entries case PE_ResDataKind_NULL: break;
str8_list_push(obj_writer->arena, &rsrc1->data, str8_struct(dir_header)); case PE_ResDataKind_COFF_LEAF: InvalidPath;
str8_list_push(obj_writer->arena, &rsrc1->data, str8_array(named_entries, named_array.count));
str8_list_push(obj_writer->arena, &rsrc1->data, str8_array(id_entries, id_array.count));
// fill out named ids
for (U64 i = 0; i < named_array.count; i += 1) {
PE_Resource src = named_array.v[i];
COFF_ResourceDirEntry *dst = &named_entries[i];
// append resource name
U32 res_name_off = safe_cast_u32(rsrc1->data.total_size);
String8 res_name = coff_resource_string_from_str8(obj_writer->arena, res->id.u.string);
str8_list_push(obj_writer->arena, &rsrc1->data, res_name);
// not sure why high bit has to be turned on here since number id and string id entries are
// in separate arrays but windows doesn't treat name offset like string without this bit.
dst->name.offset = (1 << 31) | res_name_off;
}
// fill out number ids
for (U64 i = 0; i < id_array.count; i += 1) {
PE_Resource src = id_array.v[i];
COFF_ResourceDirEntry *dst = &id_entries[i];
dst->name.id = src.id.u.number;
}
// fill out sub directory stack frame
struct Stack *frame = push_array(scratch.arena, struct Stack, 1);
frame->res_arr[0] = named_array;
frame->res_arr[1] = id_array;
frame->coff_entry_arr[0] = named_entries;
frame->coff_entry_arr[1] = id_entries;
SLLStackPush(stack, frame);
} goto yield; // recurse to sub directory
case PE_ResDataKind_COFF_RESOURCE: {
// fill out resource header
COFF_ResourceDataEntry *coff_res = push_array(obj_writer->arena, COFF_ResourceDataEntry, 1);
coff_res->data_size = res->u.coff_res.data.size;
coff_res->data_voff = 0; // relocated
coff_res->code_page = 0; // TODO: whats this for?
if (res->u.coff_res.data.size >= sizeof(U32)) {
// emit symbol for resource data
U32 resdat_off = safe_cast_u32(rsrc2->data.total_size);
COFF_ObjSymbol *resdat = coff_obj_writer_push_symbol_static(obj_writer, str8_lit("resdat"), resdat_off, rsrc2);
// emit reloc for 'data_voff'
U64 apply_off = rsrc1->data.total_size + OffsetOf(COFF_ResourceDataEntry, data_voff);
U32 apply_off32 = safe_cast_u32(apply_off);
coff_obj_writer_section_push_reloc_voff(obj_writer, rsrc1, apply_off32, resdat);
}
// push resource entry & data
str8_list_push(obj_writer->arena, &rsrc1->data, str8_struct(coff_res));
str8_list_push(obj_writer->arena, &rsrc2->data, res->u.coff_res.data);
} break;
case PE_ResDataKind_NULL: break;
// we must not have this resource node here, it is used to represent on-disk version of entry
case PE_ResDataKind_COFF_LEAF: InvalidPath;
}
}
} }
SLLStackPop(stack);
yield:;
} }
scratch_end(scratch); scratch_end(scratch);
+3 -3
View File
@@ -10,9 +10,9 @@
#define PE_BSS_SECTION_FLAGS (COFF_SectionFlag_CntUninitializedData|COFF_SectionFlag_MemRead|COFF_SectionFlag_MemWrite) #define PE_BSS_SECTION_FLAGS (COFF_SectionFlag_CntUninitializedData|COFF_SectionFlag_MemRead|COFF_SectionFlag_MemWrite)
#define PE_IDATA_SECTION_FLAGS PE_DATA_SECTION_FLAGS #define PE_IDATA_SECTION_FLAGS PE_DATA_SECTION_FLAGS
#define PE_DEBUG_DIR_SECTION_FLAGS PE_RDATA_SECTION_FLAGS #define PE_DEBUG_DIR_SECTION_FLAGS PE_RDATA_SECTION_FLAGS
#define PE_RSRC_SECTION_FLAGS PE_DATA_SECTION_FLAGS #define PE_RSRC_SECTION_FLAGS PE_RDATA_SECTION_FLAGS
#define PE_RSRC1_SECTION_FLAGS (PE_DATA_SECTION_FLAGS | COFF_SectionFlag_Align4Bytes) #define PE_RSRC1_SECTION_FLAGS PE_RDATA_SECTION_FLAGS
#define PE_RSRC2_SECTION_FLAGS (PE_DATA_SECTION_FLAGS | COFF_SectionFlag_Align4Bytes) #define PE_RSRC2_SECTION_FLAGS PE_RDATA_SECTION_FLAGS
#define PE_XDATA_SECTION_FLAGS PE_RDATA_SECTION_FLAGS #define PE_XDATA_SECTION_FLAGS PE_RDATA_SECTION_FLAGS
#define PE_PDATA_SECTION_FLAGS PE_RDATA_SECTION_FLAGS #define PE_PDATA_SECTION_FLAGS PE_RDATA_SECTION_FLAGS
#define PE_EDATA_SECTION_FLAGS PE_RDATA_SECTION_FLAGS #define PE_EDATA_SECTION_FLAGS PE_RDATA_SECTION_FLAGS