// Copyright (c) Epic Games Tools // Licensed under the MIT license (https://opensource.org/license/mit/) internal String8 lnk_loc_from_obj(Arena *arena, LNK_Obj *obj) { String8 obj_path = str8_skip_last_slash(obj ? obj->path : str8_lit("RADLINK")); String8 lib_path = str8_skip_last_slash(lnk_obj_get_lib_path(obj)); String8 result; if (lib_path.size) { result = push_str8f(arena, "%S(%S)", lib_path, obj_path); } else { result = push_str8_copy(arena, obj_path); } return result; } internal void lnk_error_obj(LNK_ErrorCode code, LNK_Obj *obj, char *fmt, ...) { va_list args; va_start(args, fmt); String8 obj_path = obj ? obj->path : str8_zero(); String8 lib_path = lnk_obj_get_lib_path(obj); lnk_error_with_loc_fv(code, obj_path, lib_path, fmt, args); va_end(args); } internal void lnk_error_input_obj(LNK_ErrorCode code, LNK_Input *input, char *fmt, ...) { va_list args; va_start(args, fmt); LNK_LibMemberRef *link_member = input->link_member; LNK_Lib *link_lib = link_member ? link_member->lib : 0; lnk_error_with_loc_fv(code, input->path, link_lib ? link_lib->path : str8_zero(), fmt, args); va_end(args); } internal LNK_Obj ** lnk_array_from_obj_list(Arena *arena, LNK_ObjList list) { LNK_Obj **arr = push_array_no_zero(arena, LNK_Obj *, list.count); U64 idx = 0; for (LNK_ObjNode *node = list.first; node != 0; node = node->next, ++idx) { arr[idx] = &node->data; } return arr; } internal THREAD_POOL_TASK_FUNC(lnk_obj_initer) { LNK_ObjIniter *task = raw_task; LNK_Input *input = task->inputs[task_id]; LNK_Obj *obj = &task->objs[task_id].data; //ProfBeginV("Init Obj [%S%s%S]", input->lib_path, (input->lib_path.size ? ": " : 0), input->path); // // parse obj header // COFF_FileHeaderInfo header = coff_file_header_info_from_data(input->data); // // set & check machine compatibility // if (header.machine != COFF_MachineType_Unknown) { COFF_MachineType current_machine = ins_atomic_u32_eval_cond_assign(&task->machine, header.machine, COFF_MachineType_Unknown); if (current_machine != COFF_MachineType_Unknown && current_machine != header.machine) { lnk_error_input_obj(LNK_Error_IncompatibleMachine, input, "conflicting machine types expected %S but got %S", coff_string_from_machine_type(current_machine), coff_string_from_machine_type(header.machine)); } } // // extract COFF info // String8 raw_coff_section_table = str8_substr(input->data, header.section_table_range); String8 raw_coff_symbol_table = str8_substr(input->data, header.symbol_table_range); String8 raw_coff_string_table = str8_substr(input->data, header.string_table_range); // // error check section table / symbol table / string table // if (raw_coff_section_table.size != dim_1u64(header.section_table_range)) { lnk_error_input_obj(LNK_Error_IllData, input, "corrupted file, unable to read section header table"); } if (raw_coff_symbol_table.size != dim_1u64(header.symbol_table_range)) { lnk_error_input_obj(LNK_Error_IllData, input, "corrupted file, unable to read symbol table"); } if (raw_coff_string_table.size != dim_1u64(header.string_table_range)) { lnk_error_input_obj(LNK_Error_IllData, input, "corrupted file, unable to read string table"); } // // section table pass // - error check headers fields // - collect section header flags // - find debug info and meta-data leaders // COFF_SectionHeader *coff_section_table = (COFF_SectionHeader *)raw_coff_section_table.str; COFF_SectionFlags *section_flags = push_array_no_zero(arena, COFF_SectionFlags, header.section_count_no_null); U32 debug_t_sect_idx = ~0; U32 debug_p_sect_idx = ~0; U32 debug_h_sect_idx = ~0; U32 llvm_addrsig_sect_idx = ~0; for (U64 sect_idx = 0; sect_idx < header.section_count_no_null; sect_idx += 1) { COFF_SectionHeader *coff_sect_header = &coff_section_table[sect_idx]; section_flags[sect_idx] = coff_sect_header->flags & ~3; // linker reserves low 2 bits for internal flags String8 sect_name = coff_name_from_section_header(raw_coff_string_table, coff_sect_header); if (str8_starts_with(sect_name, str8_lit(".debug$"))) { section_flags[sect_idx] |= LNK_SECTION_FLAG_DEBUG; } if (str8_ends_with(sect_name, str8_lit("$fo$"), 0) || str8_ends_with(sect_name, str8_lit("$fo_rvas$"), 0) || str8_ends_with(sect_name, str8_lit("$fo_bdd$"), 0)) { section_flags[sect_idx] |= COFF_SectionFlag_LnkInfo; } if (task->find_debug_t) { if (str8_match(sect_name, str8_lit(".debug$T"), 0)) { debug_t_sect_idx = sect_idx; } else if (str8_match(sect_name, str8_lit(".debug$P"), 0)) { debug_p_sect_idx = sect_idx; } else if (str8_match(sect_name, str8_lit(".debug$H"), 0)) { debug_h_sect_idx = sect_idx; } } if (task->find_llvm_addrsig && llvm_addrsig_sect_idx == max_U32 && str8_match(sect_name, str8_lit(".llvm_addrsig"), 0)) { llvm_addrsig_sect_idx = sect_idx; } if (~section_flags[sect_idx] & COFF_SectionFlag_CntUninitializedData) { if (coff_sect_header->fsize > 0) { Rng1U64 sect_range = rng_1u64(coff_sect_header->foff, coff_sect_header->foff + coff_sect_header->fsize); if (contains_1u64(header.header_range, coff_sect_header->foff) || (coff_sect_header->fsize > 0 && contains_1u64(header.header_range, sect_range.max-1))) { lnk_error_input_obj(LNK_Error_IllData, input, "header (%S No. %#llx) defines out of bounds section data (file offsets point into file header)", sect_name, sect_idx+1); } if (contains_1u64(header.section_table_range, coff_sect_header->foff) || (coff_sect_header->fsize > 0 && contains_1u64(header.section_table_range, sect_range.max-1))) { lnk_error_input_obj(LNK_Error_IllData, input, "header (%S No. %#llx) defines out of bounds section data (file offsets point into section header table)", sect_name, sect_idx+1); } if (contains_1u64(header.symbol_table_range, coff_sect_header->foff) || (coff_sect_header->fsize > 0 && contains_1u64(header.symbol_table_range, sect_range.max-1))) { lnk_error_input_obj(LNK_Error_IllData, input, "header (%S No. %#llx) defines out of bounds section data (file offsets point into symbol table)", sect_name, sect_idx+1); } if (dim_1u64(sect_range) != coff_sect_header->fsize) { lnk_error_input_obj(LNK_Error_IllData, input, "header (%S No. %#llx) defines out of bounds section data", sect_name, sect_idx+1); } } } } // // error check symbol table and cache name lengths for primary symbols // U64 primary_symbol_count = 0; { COFF_ParsedSymbol symbol; for (U64 symbol_idx = 0; symbol_idx < header.symbol_count; symbol_idx += 1 + symbol.aux_symbol_count) { symbol = coff_parse_symbol_no_name(header, raw_coff_symbol_table, symbol_idx); primary_symbol_count += 1; } } U64 name_block_count = CeilIntegerDiv(header.symbol_count, 64); LNK_SymbolNameCache symbol_name_cache = { .masks = push_array(arena, U64, name_block_count), .block_bases = push_array_no_zero(arena, U32, name_block_count), .name_sizes = push_array_no_zero(arena, U32, primary_symbol_count), }; { COFF_SectionHeader *section_table = (COFF_SectionHeader *)str8_substr(input->data, header.section_table_range).str; U64 next_block = 0; U32 name_count = 0; COFF_ParsedSymbol symbol; for (U64 symbol_idx = 0; symbol_idx < header.symbol_count; symbol_idx += (1 + symbol.aux_symbol_count)) { symbol = coff_parse_symbol(header, raw_coff_string_table, raw_coff_symbol_table, symbol_idx); U64 block_idx = symbol_idx >> 6; while (next_block <= block_idx) { symbol_name_cache.block_bases[next_block++] = name_count; } if (symbol.name.size) { symbol_name_cache.masks[block_idx] |= 1ull << (symbol_idx & 63); symbol_name_cache.name_sizes[name_count++] = safe_cast_u32(symbol.name.size); } COFF_SymbolValueInterpType interp = coff_interp_symbol(symbol.section_number, symbol.value, symbol.storage_class); if (interp == COFF_SymbolValueInterp_Regular) { if (symbol.section_number == 0 || symbol.section_number > header.section_count_no_null) { lnk_error_input_obj(LNK_Error_IllData, input, "symbol %S (No. 0x%x) points to an out of bounds section 0x%x", symbol.name, symbol_idx, symbol.section_number); } if (symbol.storage_class == COFF_SymStorageClass_Static && symbol.aux_symbol_count > 0) { COFF_ComdatSelectType select; U32 section_number = 0; coff_parse_secdef(symbol, header.is_big_obj, &select, §ion_number, 0, 0); if (select == COFF_ComdatSelect_Associative) { if (section_number == 0 || section_number > header.section_count_no_null) { lnk_error_input_obj(LNK_Error_IllData, input, "section definition symbol %S (No. 0x%x) associates with an out of bounds section 0x%x", symbol.name, symbol_idx, symbol.section_number); } } } } } while (next_block < name_block_count) { symbol_name_cache.block_bases[next_block++] = name_count; } } // // create symbol links to COMDAT sections // U32 *comdats; { comdats = push_array_no_zero(arena, U32, header.section_count_no_null); MemorySet(comdats, 0xff, header.section_count_no_null * sizeof(comdats[0])); COFF_ParsedSymbol symbol; for (U64 symbol_idx = 0; symbol_idx < header.symbol_count; symbol_idx += (1 + symbol.aux_symbol_count)) { symbol = coff_parse_symbol_no_name(header, raw_coff_symbol_table, symbol_idx); COFF_SymbolValueInterpType interp = coff_interp_symbol(symbol.section_number, symbol.value, symbol.storage_class); if (interp == COFF_SymbolValueInterp_Regular) { if (symbol.storage_class == COFF_SymStorageClass_Static) { if (symbol.section_number > 0 && symbol.section_number <= header.section_count_no_null) { COFF_SectionHeader *sect_header = &coff_section_table[symbol.section_number-1]; if (section_flags[symbol.section_number-1] & COFF_SectionFlag_LnkCOMDAT) { if (symbol.aux_symbol_count) { U32 section_length = 0; coff_parse_secdef(symbol, header.is_big_obj, 0, 0, §ion_length, 0); if (sect_header->fsize == section_length) { if (comdats[symbol.section_number-1] == ~0) { comdats[symbol.section_number-1] = symbol_idx; } else { lnk_error_input_obj(LNK_Error_IllData, input, "section definition symbo (No. 0x%llx) tries to ovewrite comdat", symbol_idx); } } else { lnk_error_input_obj(LNK_Error_IllData, input, "section size specified by section definition symbol (No 0x%llx) doesn't match size in section header (No. 0x%x); expected 0x%x got 0x%x", symbol_idx, symbol.section_number, section_length, sect_header->fsize); } } } } else { lnk_error_input_obj(LNK_Error_IllData, input, "section definition symbol (No. 0x%llx) has out of bounds section number 0x%x", symbol_idx, symbol.section_number); } } } } } // // COMDAT loop checker // { Temp scratch = scratch_begin(&arena, 1); HashTable *visited_sections = hash_table_init(scratch.arena, 32); for (U64 sect_idx = 0; sect_idx < header.section_count_no_null; sect_idx += 1) { for (U32 curr_section = sect_idx;;) { U32 symbol_idx = comdats[curr_section]; // is section COMDAT? if (symbol_idx == max_U32) { break; } // extract COMDAT info for current section COFF_ParsedSymbol symbol = coff_parse_symbol_no_name(header, raw_coff_symbol_table, symbol_idx); COFF_ComdatSelectType select = COFF_ComdatSelect_Null; U32 section_number = 0; coff_parse_secdef(symbol, header.is_big_obj, &select, §ion_number, 0, 0); if (select != COFF_ComdatSelect_Associative) { // section terminates at non-associative COMDAT -- no loop break; } // was section visited? -- loop found if (hash_table_search_u64(visited_sections, curr_section)) { COFF_ParsedSymbol symbol = coff_parse_symbol(header, raw_coff_string_table, raw_coff_symbol_table, comdats[sect_idx]); lnk_error_input_obj(LNK_Error_AssociativeLoop, input, "section symbol %S (No. 0x%x) does not terminate on a non-associate COMDAT symbol", symbol.name, comdats[sect_idx]); break; } // track visited sections hash_table_push_u64_u64(scratch.arena, visited_sections, curr_section, 0); // follow association Assert(section_number > 0); curr_section = section_number-1; } // purge hash table for next run hash_table_purge(visited_sections); } scratch_end(scratch); } // // collect sections associations // U32Node **associated_sections = push_array(arena, U32Node *, header.section_count_no_null + 1); { COFF_ParsedSymbol symbol; for (U32 symbol_idx = 0; symbol_idx < header.symbol_count; symbol_idx += (1 + symbol.aux_symbol_count)) { symbol = coff_parse_symbol_no_name(header, raw_coff_symbol_table, symbol_idx); COFF_SymbolValueInterpType interp = coff_interp_from_parsed_symbol(symbol); if (interp == COFF_SymbolValueInterp_Regular && symbol.storage_class == COFF_SymStorageClass_Static && symbol.aux_symbol_count > 0) { COFF_ComdatSelectType selection = COFF_ComdatSelect_Null; U32 section_number = 0; coff_parse_secdef(symbol, header.is_big_obj, &selection, §ion_number, 0, 0); if (selection != COFF_ComdatSelect_Associative) { continue; } U32Node *associated_node = push_array(arena, U32Node, 1); associated_node->data = symbol.section_number; SLLStackPush(associated_sections[section_number], associated_node); } } } B8 hotpatch = 0; if (header.machine == COFF_MachineType_X64) { hotpatch = 1; } // // extract obj features from compile symbol in .debug$S // else { Temp scratch = scratch_begin(&arena, 1); CV_Symbol comp_symbol = {0}; for EachIndex(sect_idx, header.section_count_no_null) { COFF_SectionHeader *sect_header = &coff_section_table[sect_idx]; if (section_flags[sect_idx] & LNK_SECTION_FLAG_DEBUG) { String8 name = str8_cstring_capped(sect_header->name, sect_header->name+sizeof(sect_header->name)); if (str8_match(name, str8_lit(".debug$S"), 0)) { Temp temp = temp_begin(scratch.arena); String8 debug_s_data = str8_substr(input->data, rng_1u64(sect_header->foff, sect_header->foff+sect_header->fsize)); CV_DebugS debug_s = cv_debug_s_from_data(temp.arena, debug_s_data); for EachNode(symbols_n, String8Node, debug_s.data_list[CV_C13SubSectionIdxKind_Symbols].first) { for (U64 cursor = 0, count = 0; cursor < symbols_n->string.size && count < 2; count += 1) { CV_SymbolHeader symbol_header; TryReadBreak(str8_deserial_read_struct(symbols_n->string, cursor, &symbol_header), cursor); if (symbol_header.kind == CV_SymKind_COMPILE3) { String8 raw_symbol = str8_substr(symbols_n->string, r1u64(cursor, cursor + symbol_header.size + sizeof(CV_SymSize))); comp_symbol = cv_symbol_from_ptr(raw_symbol.str); goto found_comp_symbol; } cursor += symbol_header.size + sizeof(CV_SymSize); cursor = AlignPow2(cursor, CV_SymbolAlign); } } temp_end(temp); } } } found_comp_symbol:; if (comp_symbol.kind == CV_SymKind_COMPILE3 && comp_symbol.data.size >= sizeof(CV_SymCompile3)) { CV_SymCompile3 *comp = (CV_SymCompile3 *)comp_symbol.data.str; hotpatch = !!(comp->flags & CV_Compile3Flag_HotPatch); } scratch_end(scratch); } // fill out obj obj->data = input->data; obj->path = push_str8_copy(arena, input->path); obj->header = header; obj->section_flags = section_flags; obj->symbol_name_cache = symbol_name_cache; obj->comdats = comdats; obj->exclude_from_debug_info = input->exclude_from_debug_info; obj->hotpatch = hotpatch; obj->associated_sections = associated_sections; obj->self = &task->objs[task_id]; obj->link_member = input->link_member; obj->debug_t_sect_idx = debug_t_sect_idx; obj->debug_p_sect_idx = debug_p_sect_idx; obj->debug_h_sect_idx = debug_h_sect_idx; obj->llvm_addrsig_sect_idx = llvm_addrsig_sect_idx; } internal LNK_ObjNode * lnk_obj_from_input_many(TP_Context *tp, TP_Arena *arena, LNK_Config *config, U64 inputs_count, LNK_Input **inputs) { LNK_ObjNode *objs = 0; if (inputs_count) { objs = push_array(arena->v[0], LNK_ObjNode, inputs_count); LNK_ObjIniter task = { .inputs = inputs, .objs = objs, .machine = config->machine, .find_debug_t = lnk_do_debug_info(config), .find_llvm_addrsig = config->opt_icf == LNK_SwitchState_Yes, }; tp_for_parallel(tp, arena, inputs_count, lnk_obj_initer, &task); } return objs; } internal LNK_ObjNode * lnk_obj_from_input(Arena *arena, LNK_Config *config, LNK_Input *input) { Temp scratch = scratch_begin(&arena, 1); TP_Context *tp = tp_alloc(scratch.arena, 1, 1, str8_zero()); TP_Arena tp_arena = { .count = 1, .v = &arena }; LNK_ObjNode *result = lnk_obj_from_input_many(tp, &tp_arena, config, 1, &input); scratch_end(scratch); return result; } internal void lnk_obj_list_push_node_many(LNK_ObjList *list, U64 count, LNK_ObjNode *nodes) { for EachIndex(i, count) { DLLPushBack(list->first, list->last, &nodes[i]); } list->count += count; } internal void lnk_obj_list_push_node(LNK_ObjList *list, LNK_ObjNode *node) { lnk_obj_list_push_node_many(list, 1, node); } internal THREAD_POOL_TASK_FUNC(lnk_input_coff_symbol_table) { LNK_InputCoffSymbolTable *task = raw_task; LNK_Obj *obj = task->objs[task_id]; COFF_ParsedSymbol symbol = {0}; for (U64 symbol_idx = 0; symbol_idx < obj->header.symbol_count; symbol_idx += (1 + symbol.aux_symbol_count)) { symbol = lnk_parsed_symbol_from_coff_symbol_idx_no_name(obj, symbol_idx); COFF_SymbolValueInterpType interp = coff_interp_from_parsed_symbol(symbol); LNK_SymbolSearchType search_type = lnk_symbol_search_type_from_coff(obj, symbol, interp); switch (interp) { case COFF_SymbolValueInterp_Regular: { if (symbol.storage_class == COFF_SymStorageClass_External) { LNK_ObjSection section = lnk_obj_section_from_section_number(obj, symbol.section_number); if (*section.flags & COFF_SectionFlag_LnkRemove) { break; } LNK_Symbol *defn = lnk_make_symbol(arena, lnk_symbol_name_from_coff_symbol_idx(obj, symbol_idx), obj, symbol_idx, search_type); lnk_symbol_table_push_(task->symtab, arena, worker_id, defn); } } break; case COFF_SymbolValueInterp_Weak: { LNK_Symbol *defn = lnk_make_symbol(arena, lnk_symbol_name_from_coff_symbol_idx(obj, symbol_idx), obj, symbol_idx, search_type); lnk_symbol_table_push_(task->symtab, arena, worker_id, defn); } break; case COFF_SymbolValueInterp_Undefined: { if (symbol.storage_class == COFF_SymStorageClass_External) { LNK_Symbol *defn = lnk_make_symbol(arena, lnk_symbol_name_from_coff_symbol_idx(obj, symbol_idx), obj, symbol_idx, search_type); lnk_symbol_table_push_(task->symtab, arena, worker_id, defn); } } break; case COFF_SymbolValueInterp_Common: { LNK_Symbol *defn = lnk_make_symbol(arena, lnk_symbol_name_from_coff_symbol_idx(obj, symbol_idx), obj, symbol_idx, search_type); lnk_symbol_table_push_(task->symtab, arena, worker_id, defn); } break; case COFF_SymbolValueInterp_Abs: { if (symbol.storage_class == COFF_SymStorageClass_External) { LNK_Symbol *defn = lnk_make_symbol(arena, lnk_symbol_name_from_coff_symbol_idx(obj, symbol_idx), obj, symbol_idx, search_type); lnk_symbol_table_push_(task->symtab, arena, worker_id, defn); } } break; case COFF_SymbolValueInterp_Debug: { // not used } break; default: { InvalidPath; } break; } } } internal LNK_ObjSymbolRef * lnk_symlinks_from_obj(Arena *arena, LNK_SymbolTable *symtab, LNK_Obj *obj) { LNK_ObjSymbolRef *symlinks = push_array(arena, LNK_ObjSymbolRef, obj->header.section_count_no_null + 1); COFF_ParsedSymbol symbol = {0}; for (U64 symbol_idx = 0; symbol_idx < obj->header.symbol_count; symbol_idx += (1 + symbol.aux_symbol_count)) { symbol = lnk_parsed_symbol_from_coff_symbol_idx_no_name(obj, symbol_idx); COFF_SymbolValueInterpType interp = coff_interp_from_parsed_symbol(symbol); if (interp != COFF_SymbolValueInterp_Regular) { continue; } COFF_SectionFlags section_flags = obj->section_flags[symbol.section_number - 1]; if (~section_flags & COFF_SectionFlag_LnkCOMDAT) { continue; } LNK_ObjSymbolRef *symlink = &symlinks[symbol.section_number]; // external symbols if (symbol.storage_class == COFF_SymStorageClass_External && symbol.aux_symbol_count == 0) { String8 symbol_name = lnk_symbol_name_from_coff_symbol_idx(obj, symbol_idx); B32 can_set_symlink = (symlink->obj == 0 || symbol.value == 0); if (!can_set_symlink && symlink->obj == obj) { COFF_ParsedSymbol leader = lnk_parsed_symbol_from_coff_symbol_idx_no_name(symlink->obj, symlink->symbol_idx); B32 leader_is_same_section = leader.section_number == symbol.section_number; B32 leader_is_static_anchor = (leader_is_same_section && leader.storage_class == COFF_SymStorageClass_Static && leader.aux_symbol_count == 0); B32 leader_is_vftable = str8_starts_with(lnk_symbol_name_from_coff_symbol_idx(symlink->obj, symlink->symbol_idx), str8_lit(MSCRT_VFTABLE_SYMBOL_PREFIX)); B32 current_is_vftable = str8_starts_with(symbol_name, str8_lit(MSCRT_VFTABLE_SYMBOL_PREFIX)); // prefer public symbols to local static anchors; prefer vftable public // symbols to other public symbols so ICF keeps vftables in their own color space can_set_symlink = (leader_is_static_anchor || (leader_is_same_section && current_is_vftable && !leader_is_vftable)); } if (can_set_symlink) { LNK_SymbolHashTrie *link_symbol = lnk_symbol_table_search_(symtab, symbol_name); if (link_symbol) { *symlink = lnk_ref_from_symbol(link_symbol->symbol); } } } // static symbols else if (symbol.storage_class == COFF_SymStorageClass_Static) { if (symbol.aux_symbol_count == 0) { if (symlink->obj == 0) { *symlink = (LNK_ObjSymbolRef){ obj, symbol_idx }; } } } } return symlinks; } internal U32List lnk_obj_collect_associated_sections(Arena *arena, LNK_Obj *obj, U32 root_section, COFF_SectionFlags skip_flags) { Temp scratch = scratch_begin(&arena, 1); // track each child before enqueueing it because COFF associations can cycle HashMap seen_hm = {0}; U32List queue = {0}; U32Node root_n = { root_section }; u32_list_push_node(&queue, &root_n); hash_map_push_u64_u64(scratch.arena, &seen_hm, root_section, 1); // walk the complete descendant chain because associated COMDATs can nest for EachNode(parent_n, U32Node, queue.first) { for EachNode(associated_n, U32Node, obj->associated_sections[parent_n->data]) { U32 child_section = associated_n->data; if (child_section == 0) { continue; } if (hash_map_search_u64_u64(&seen_hm, child_section)) { continue; } if (obj->section_flags[child_section - 1] & skip_flags) { continue; } hash_map_push_u64_u64(scratch.arena, &seen_hm, child_section, 1); u32_list_push(arena, &queue, child_section); } } // return only child sections so callers choose whether the root participates U32List result = {0}; if (queue.count > 1) { result.first = queue.first->next; result.last = queue.last; result.count = queue.count - 1; } scratch_end(scratch); return result; } internal THREAD_POOL_TASK_FUNC(lnk_assign_comdat_symlinks_task) { LNK_InputCoffSymbolTable *task = raw_task; LNK_Obj *obj = task->objs[task_id]; obj->symlinks = lnk_symlinks_from_obj(arena, task->symtab, obj); } internal void lnk_assign_comdat_symlinks(TP_Context *tp, TP_Arena *arena, LNK_SymbolTable *symtab, U64 objs_count, LNK_Obj **objs) { ProfBeginFunction(); LNK_InputCoffSymbolTable task = { .symtab = symtab, .objs = objs }; tp_for_parallel(tp, arena, objs_count, lnk_assign_comdat_symlinks_task, &task); ProfEnd(); } internal void lnk_push_obj_symbols(TP_Context *tp, TP_Arena *arena, LNK_SymbolTable *symtab, U64 objs_count, LNK_Obj **objs) { ProfBeginFunction(); LNK_InputCoffSymbolTable task = { .symtab = symtab, .objs = objs }; tp_for_parallel(tp, arena, objs_count, lnk_input_coff_symbol_table, &task); ProfEnd(); } internal COFF_ParsedSymbol lnk_obj_match_symbol(LNK_Obj *obj, String8 match_name) { COFF_ParsedSymbol symbol; for (U64 symbol_idx = 0; symbol_idx < obj->header.symbol_count; symbol_idx += (1 + symbol.aux_symbol_count)) { symbol = lnk_parsed_symbol_from_coff_symbol_idx(obj, symbol_idx); if (str8_match(symbol.name, match_name, 0)) { return symbol; } } return (COFF_ParsedSymbol){0}; } internal MSCRT_FeatFlags lnk_obj_get_features(LNK_Obj *obj) { return lnk_obj_match_symbol(obj, str8_lit("@feat.00")).value; } internal U32 lnk_obj_get_comp_id(LNK_Obj *obj) { return lnk_obj_match_symbol(obj, str8_lit("@comp.id")).value; } internal U32 lnk_obj_get_vol_md(LNK_Obj *obj) { return lnk_obj_match_symbol(obj, str8_lit("@vol.md")).value; } internal LNK_Lib * lnk_obj_get_lib(LNK_Obj *obj) { return obj->link_member ? obj->link_member->lib : 0; } internal String8 lnk_obj_get_lib_path(LNK_Obj *obj) { String8 lib_path = {0}; if (obj) { LNK_Lib *lib = lnk_obj_get_lib(obj); lib_path = lib ? lib->path : str8_zero(); } return lib_path; } internal U32 lnk_obj_get_removed_section_number(LNK_Obj *obj) { return obj->header.is_big_obj ? LNK_REMOVED_SECTION_NUMBER_32 : LNK_REMOVED_SECTION_NUMBER_16; } internal B32 lnk_obj_get_comdat_symlink(LNK_Obj *obj, U64 section_number, LNK_ObjSymbolRef *symlink_out) { LNK_ObjSymbolRef symlink = obj->symlinks[section_number]; B32 is_valid = symlink.obj != 0; if (is_valid && symlink_out) { *symlink_out = symlink; } return is_valid; } 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; } internal U64 lnk_obj_sect_idx_from_section_number(LNK_Obj *obj, U64 section_number) { Assert(1 <= section_number && section_number <= obj->header.section_count_no_null); return section_number-1; } internal U64 lnk_obj_section_number_from_sect_idx(LNK_Obj *obj, U64 sect_idx) { Assert(sect_idx < obj->header.section_count_no_null); return sect_idx+1; } internal String8 lnk_obj_section_name_from_sect_idx(LNK_Obj *obj, U64 sect_idx) { COFF_SectionHeader *section_header = &lnk_coff_section_table_from_obj(obj)[sect_idx]; return coff_name_from_section_header(lnk_coff_string_table_from_obj(obj), section_header); } internal String8 lnk_obj_section_name_from_section_number(LNK_Obj *obj, U64 section_number) { Assert(section_number > 0); return lnk_obj_section_name_from_sect_idx(obj, section_number-1); } internal LNK_ObjSection lnk_obj_section_from_sect_idx(LNK_Obj *obj, U64 sect_idx) { Assert(sect_idx < obj->header.section_count_no_null); LNK_ObjSection section = {0}; section.obj = obj; section.sect_idx = sect_idx; section.section_number = sect_idx+1; section.header = &lnk_coff_section_table_from_obj(obj)[sect_idx]; section.flags = &obj->section_flags[sect_idx]; section.vrange = rng_1u64(section.header->voff, section.header->voff + section.header->vsize); section.frange = rng_1u64(section.header->foff, section.header->foff + section.header->fsize); section.reloc_count = section.header->reloc_count; return section; } internal LNK_ObjSection lnk_obj_section_from_section_number(LNK_Obj *obj, U64 section_number) { U64 sect_idx = lnk_obj_sect_idx_from_section_number(obj, section_number); return lnk_obj_section_from_sect_idx(obj, sect_idx); } internal COFF_RelocArray lnk_coff_relocs_from_section_header(LNK_Obj *obj, COFF_SectionHeader *section_header) { COFF_RelocInfo reloc_info = coff_reloc_info_from_section_header(obj->data, section_header); COFF_Reloc *relocs = (COFF_Reloc *)(obj->data.str + reloc_info.array_off); COFF_RelocArray result = { .count = reloc_info.count, .v = relocs }; return result; } internal String8 lnk_coff_string_table_from_obj(LNK_Obj *obj) { return str8_substr(obj->data, obj->header.string_table_range); } internal String8 lnk_coff_symbol_table_from_obj(LNK_Obj *obj) { return str8_substr(obj->data, obj->header.symbol_table_range); } internal COFF_RelocArray lnk_coff_reloc_info_from_section_number(LNK_Obj *obj, U64 section_number) { LNK_ObjSection section = lnk_obj_section_from_section_number(obj, section_number); COFF_RelocInfo reloc_info = coff_reloc_info_from_section_header(obj->data, section.header); COFF_Reloc *relocs = str8_deserial_get_raw_ptr(obj->data, reloc_info.array_off, sizeof(*relocs)*reloc_info.count); COFF_RelocArray result = { .count = reloc_info.count, .v = relocs }; return result; } internal B32 lnk_try_comdat_props_from_section_number(LNK_Obj *obj, U32 section_number, COFF_ComdatSelectType *select_out, U32 *section_number_out, U32 *section_length_out, U32 *check_sum_out) { Assert(section_number > 0); U32 symbol_idx = obj->comdats[section_number-1]; if (symbol_idx != max_U32) { COFF_ParsedSymbol secdef = lnk_parsed_symbol_from_coff_symbol_idx_no_name(obj, symbol_idx); coff_parse_secdef(secdef, obj->header.is_big_obj, select_out, section_number_out, section_length_out, check_sum_out); return 1; } return 0; } internal COFF_SectionHeader * 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 §ion_table[sect_idx]; } internal force_inline COFF_ParsedSymbol lnk_parsed_symbol_from_coff_symbol_idx_no_name(LNK_Obj *obj, U64 symbol_idx) { return coff_parse_symbol_no_name(obj->header, lnk_coff_symbol_table_from_obj(obj), safe_cast_u32(symbol_idx)); } internal force_inline String8 lnk_symbol_name_from_coff_symbol_idx(LNK_Obj *obj, U64 symbol_idx) { String8 result = {0}; U64 block_idx = symbol_idx >> 6; U64 bit = 1ull << (symbol_idx & 63); U64 mask = obj->symbol_name_cache.masks[block_idx]; if (mask & bit) { U64 name_idx = obj->symbol_name_cache.block_bases[block_idx] + count_bits_set64(mask & (bit - 1)); U64 name_size = obj->symbol_name_cache.name_sizes[name_idx]; String8 symbol_table = lnk_coff_symbol_table_from_obj(obj); COFF_SymbolName *name = obj->header.is_big_obj ? &((COFF_Symbol32 *)symbol_table.str)[symbol_idx].name : &((COFF_Symbol16 *)symbol_table.str)[symbol_idx].name; U8 *name_ptr = name->long_name.zeroes == 0 ? obj->data.str + obj->header.string_table_range.min + name->long_name.string_table_offset : name->short_name; result = str8(name_ptr, name_size); } return result; } internal force_inline COFF_ParsedSymbol lnk_parsed_symbol_from_coff_symbol_idx(LNK_Obj *obj, U64 symbol_idx) { COFF_ParsedSymbol result = lnk_parsed_symbol_from_coff_symbol_idx_no_name(obj, symbol_idx); result.name = lnk_symbol_name_from_coff_symbol_idx(obj, symbol_idx); return result; } internal THREAD_POOL_TASK_FUNC(lnk_collect_obj_chunks_task) { LNK_SectionCollector *task = raw_task; LNK_Obj *obj = task->objs[task_id]; for EachIndex(sect_idx, obj->header.section_count_no_null) { LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); if (*section.flags & COFF_SectionFlag_LnkRemove && !task->collect_discarded) { continue; } String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); if (str8_match(section_name, task->name, 0)) { String8 section_data = str8_substr(obj->data, section.frange); str8_list_push(arena, &task->out_lists[task_id], section_data); } } } internal String8List * lnk_collect_obj_sections(TP_Context *tp, TP_Arena *arena, U64 objs_count, LNK_Obj **objs, String8 name, B32 collect_discarded) { LNK_SectionCollector task = {0}; task.objs = objs; task.name = name; task.collect_discarded = collect_discarded; task.out_lists = push_array(arena->v[0], String8List, objs_count); tp_for_parallel(tp, arena, objs_count, lnk_collect_obj_chunks_task, &task); return task.out_lists; } internal B32 lnk_obj_is_before(void *raw_a, void *raw_b) { LNK_Obj *a = raw_a, *b = raw_b; return a->input_idx < b->input_idx; } internal void lnk_parse_msvc_linker_directive(Arena *arena, LNK_Obj *obj, LNK_DirectiveInfo *directive_info, String8 buffer) { Temp scratch = scratch_begin(&arena, 1); String8 to_parse; { local_persist const U8 bom_sig[] = { 0xEF, 0xBB, 0xBF }; local_persist const U8 ascii_sig[] = { 0x20, 0x20, 0x20 }; if (MemoryMatch(buffer.str, &bom_sig[0], sizeof(bom_sig))) { to_parse = str8_zero(); lnk_error_obj(LNK_Error_IllData, obj, "TODO: support for BOM encoding"); } else if (MemoryMatch(buffer.str, &ascii_sig[0], sizeof(ascii_sig))) { to_parse = str8_skip(buffer, sizeof(ascii_sig)); } else { to_parse = buffer; } } String8List arg_list = lnk_arg_list_parse_windows_rules(scratch.arena, to_parse); LNK_CmdLine cmd_line = lnk_cmd_line_parse_windows_rules(scratch.arena, arg_list); for (LNK_CmdOption *opt = cmd_line.first_option; opt != 0; opt = opt->next) { LNK_CmdSwitch *cmd_switch = lnk_cmd_switch_from_string(opt->string); if (cmd_switch == 0) { lnk_error_obj(LNK_Warning_UnknownDirective, obj, "unknown directive \"%S\"", opt->string); continue; } if (!cmd_switch->is_legal_directive) { lnk_error_obj(LNK_Warning_IllegalDirective, obj, "illegal directive \"%S\"", opt->string); continue; } LNK_Directive *directive = push_array_no_zero(arena, LNK_Directive, 1); directive->next = 0; directive->id = str8_cstring(cmd_switch->name); directive->value = push_str8_copy(arena, opt->value); LNK_DirectiveList *directive_list = &directive_info->v[cmd_switch->type]; SLLQueuePush(directive_list->first, directive_list->last, directive); ++directive_list->count; } scratch_end(scratch); } internal String8List lnk_raw_directives_from_obj(Arena *arena, LNK_Obj *obj) { String8List drectve_data = {0}; for (U64 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) { LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); if (*section.flags & COFF_SectionFlag_LnkInfo) { String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); if (str8_match(section_name, str8_lit(".drectve"), 0)) { if (*section.flags & COFF_SectionFlag_CntUninitializedData) { lnk_error_obj(LNK_Error_IllData, obj, ".drectve section header has flag COFF_SectionFlag_CntUninitializedData"); break; } if (dim_1u64(section.frange) < 3) { lnk_error_obj(LNK_Error_IllData, obj, "not enough bytes to parse .drectve"); break; } if (section.reloc_count > 0) { lnk_error_obj(LNK_Error_IllData, obj, ".drectve must not have relocations"); break; } str8_list_push(arena, &drectve_data, str8_substr(obj->data, section.frange)); } } } return drectve_data; } internal LNK_DirectiveInfo lnk_directive_info_from_raw_directives(Arena *arena, LNK_Obj *obj, String8List raw_directives) { LNK_DirectiveInfo directive_info = {0}; for (String8Node *drectve_n = raw_directives.first; drectve_n != 0; drectve_n = drectve_n->next) { lnk_parse_msvc_linker_directive(arena, obj, &directive_info, drectve_n->string); } return directive_info; } internal CV_DebugS lnk_debug_s_from_obj(Arena *arena, LNK_Obj *obj) { Temp scratch = scratch_begin(&arena, 1); String8List raw_debug_s = {0}; { for EachIndex(sect_idx, obj->header.section_count_no_null) { LNK_ObjSection section = lnk_obj_section_from_sect_idx(obj, sect_idx); String8 section_name = lnk_obj_section_name_from_sect_idx(obj, sect_idx); if (str8_match(section_name, str8_lit(".debug$S"), 0)) { String8 debug_s = str8_substr(obj->data, section.frange); str8_list_push(scratch.arena, &raw_debug_s, debug_s); } } } CV_DebugS debug_s = {0}; { for (String8Node *node = raw_debug_s.first; node != 0; node = node->next) { // parse & merge sub sections CV_DebugS ds = cv_debug_s_from_data(arena, node->string); cv_debug_s_concat_in_place(&debug_s, &ds); // make sure there is one string table String8List string_data_list = cv_sub_section_from_debug_s(debug_s, CV_C13SubSectionKind_StringTable); if (string_data_list.node_count > 1) { break; } // make sure there is one file checksum table String8List checksum_data_list = cv_sub_section_from_debug_s(debug_s, CV_C13SubSectionKind_FileChksms); if (checksum_data_list.node_count > 1) { continue; } } } scratch_end(scratch); return debug_s; }