improve handling of the response files

* reject cyclic response files
* prepend and append command line defined in LINK and _LINK_
* reduce config wrappers
This commit is contained in:
Nikita Smith committed 2026-08-17 18:21:05 -07:00
1 parent b8574e3a0b
commit 76e267482f
9 files changed
+433 -342

No files matched your search

+41 -193
View File
@@ -130,171 +130,6 @@
// -----------------------------------------------------------------------------
internal LNK_CmdLine
lnk_make_default_cmd_line(Arena *arena, LNK_CmdLine user_cmd_line)
{
Temp scratch = scratch_begin(&arena, 1);
LNK_CmdLine cmd_line = {0};
char *default_opts[] = {
"/ALIGN:4096",
"/DEBUG:none",
"/FILEALIGN:512",
"/HIGHENTROPYVA",
"/MANIFESTUAC:\"level='asInvoker' uiAccess='false'\"",
"/NXCOMPAT",
"/LARGEADDRESSAWARE",
"/PDBALTPATH:%_RAD_PDB_PATH%",
"/PDBPAGESIZE:4096",
(char*)str8f(scratch.arena, "/HEAP:%llu,%llu", MB(1), KB(4)).str,
(char*)str8f(scratch.arena, "/STACK:%llu,%llu", MB(1), KB(4)).str,
"/RAD_BOOT_MODE:LINKER",
//"/RAD_BUILD_EXP",
"/RAD_BUILD_IMPLIB",
"/RAD_AGE:1",
"/RAD_CHECK_UNUSED_DELAY_LOAD_DLL",
"/RAD_DO_MERGE",
"/RAD_ENV_LIB",
"/RAD_EXE",
"/RAD_GUID:imageblake3",
"/RAD_LARGE_PAGES:no",
"/RAD_LINK_VER:14.0",
"/RAD_OS_VER:6.0",
"/RAD_PAGE_SIZE:4096",
"/RAD_PATH_STYLE:system",
"/RAD_PDB_HASH_TYPE_NAMES:NONE",
"/RAD_PDB_HASH_TYPE_NAME_LENGTH:8",
"/RAD_DEBUGALTPATH:%_RAD_RDI_PATH%",
"/RAD_MEMORY_MAP_FILES",
"/RAD_MAP_LINES_FOR_UNRESOLVED_SYMBOLS",
"/RAD_UNRESOLVED_SYMBOL_LIMIT:1000",
"/RAD_UNRESOLVED_SYMBOL_REF_LIMIT:10",
"/RAD_SORT_IMPORTS",
(char*)str8f(scratch.arena, "/RAD_MT_PATH:%s", LNK_MANIFEST_MERGE_TOOL_NAME).str,
(char*)str8f(scratch.arena, "/RAD_DATA_DIR_COUNT:%u", PE_DataDirectoryIndex_COUNT).str,
// Set BLAKE3 as the default to match the LLVM default.
//
// When hash kinds conflict, radlink discards any .debug$H sections
// whose hash kind does not match the selected default.
"/RAD_DEBUG_TYPE_HASH:BLAKE3",
// Use LLVM significant addresses hints for the /OPT:ICF.
"/LLVM_ADDRSIG",
// By default keep full type names, override when TPI/IPI streams overflow.
"/RAD_PDB_HASH_TYPE_NAMES:NONE",
// TODO: The ICF algorithm requires a cryptographic hash to establish
// equivalence. With xxHash and similar non-cryptographic hashes,
// the algorithm must compare each section property before
// deciding whether sections are truly identical.
"/RAD_ICF_HASH_KIND:BLAKE3",
};
char *push_opts[] = {
"/MERGE:.xdata=.rdata",
"/MERGE:.00cfg=.rdata",
// TODO: .tls must be always first contribution in .data section because compiler generates TLS relative movs
//"/MERGE:.tls=.data",
"/MERGE:.idata=.data",
"/MERGE:.didat=.data",
"/MERGE:.edata=.rdata",
"/MERGE:.RAD_LINK_PE_DEBUG_DIR=.rdata",
"/MERGE:.RAD_LINK_PE_DEBUG_DATA=.rdata",
"/RAD_REMOVE_SECTION:.debug",
"/RAD_REMOVE_SECTION:.gehcont",
"/RAD_REMOVE_SECTION:.gfids",
"/RAD_REMOVE_SECTION:.gxfg",
(char*)str8f(scratch.arena, "/RAD_WORKERS:%u", get_system_info()->logical_processor_count).str,
// errors that are too verbose in release build
(char*)str8f(scratch.arena, "/RAD_IGNORE:%d", LNK_Warning_UnknownSwitch * (BUILD_DEBUG ? -1 : 1)).str,
(char*)str8f(scratch.arena, "/RAD_IGNORE:%d", LNK_Warning_UnknownDirective * (BUILD_DEBUG ? -1 : 1)).str,
(char*)str8f(scratch.arena, "/RAD_IGNORE:%d", LNK_Error_InvalidTypeIndex * (BUILD_DEBUG ? -1 : 1)).str,
#if BUILD_DEBUG
"/RAD_LOG:debug",
"/RAD_LOG:io_write",
#else
(char*)str8f(scratch.arena, "/RAD_IGNORE:%u", LNK_Error_InvalidTypeIndex).str,
#endif
};
#define DefaultOpt(...) do { \
LNK_CmdLine parsed_cmd_line = lnk_cmd_line_from_stringf_windows_rules(arena, __VA_ARGS__); \
for EachNode(cmd, LNK_CmdOption, parsed_cmd_line.first_option) { \
if (!lnk_cmd_line_has_switch(user_cmd_line, lnk_cmd_switch_type_from_string(cmd->string))) { \
String8List value_strings = str8_list_copy(arena, &cmd->value_strings); \
lnk_cmd_line_push_option_list(arena, &cmd_line, cmd->string, value_strings); \
} \
} \
} while (0)
#define PushOpt(...) do { \
LNK_CmdLine parsed_cmd_line = lnk_cmd_line_from_stringf_windows_rules(arena, __VA_ARGS__); \
lnk_cmd_line_concat_in_place(&cmd_line, &parsed_cmd_line); \
} while (0)
if (lnk_cmd_line_has_switch(user_cmd_line, LNK_CmdSwitch_Dll)) {
DefaultOpt("/SUBSYSTEM:%S", pe_string_from_subsystem(PE_WindowsSubsystem_WINDOWS_GUI));
}
if (!lnk_cmd_line_has_switch(user_cmd_line, LNK_CmdSwitch_Brepro)) {
DefaultOpt("/RAD_TIME_STAMP:%u", get_process_start_time_unix());
}
for EachIndex(i, ArrayCount(default_opts)) {
DefaultOpt("%s", default_opts[i]);
}
for EachIndex(i, ArrayCount(push_opts)) {
PushOpt("%s", push_opts[i]);
}
// when /FORCE is specified on the command line, do not stop on these errors
if (lnk_cmd_line_has_switch(user_cmd_line, LNK_CmdSwitch_Force)) {
g_error_mode_arr[LNK_Error_UnresolvedSymbol] = LNK_ErrorMode_Continue;
g_error_mode_arr[LNK_Error_RelocationAgainstRemovedSection] = LNK_ErrorMode_Continue;
}
#undef DefaultOpt
#undef PushOpt
scratch_end(scratch);
return cmd_line;
}
internal LNK_Config *
lnk_config_from_argcv(CmdLine *cmdline)
{
Temp scratch = scratch_begin(0,0);
String8List raw_cmd_line = {0};
for (U64 i = 1; i < cmdline->argc; i += 1) { str8_list_push(scratch.arena, &raw_cmd_line, str8_cstring(cmdline->argv[i])); }
#if PROFILE_TELEMETRY
tmMessage(0, TMMF_ICON_NOTE, "Command Line: %.*s", str8_varg(str8_list_join(scratch.arena, &raw_cmd_line, &(StringJoin){ .sep = str8_lit_comp(" ") })));
#endif
// make command line
LNK_CmdLine cmd_line_msvc = {0};
{
String8List unwrapped_cmd_line = lnk_unwrap_rsp(scratch.arena, raw_cmd_line);
LNK_CmdLine user_cmd_line = lnk_cmd_line_parse_windows_rules(scratch.arena, unwrapped_cmd_line);
user_cmd_line.raw_cmd_line = raw_cmd_line;
LNK_CmdLine default_cmd_line = lnk_make_default_cmd_line(scratch.arena, user_cmd_line);
lnk_cmd_line_concat_in_place(&cmd_line_msvc, &default_cmd_line);
lnk_cmd_line_concat_in_place(&cmd_line_msvc, &user_cmd_line);
}
// init config
LNK_Config *config = lnk_config_init(cmd_line_msvc);
scratch_end(scratch);
return config;
}
internal String8
lnk_make_full_path(Arena *arena, PathStyle system_path_style, String8 work_dir, String8 path)
{
@@ -500,8 +335,9 @@ lnk_manifest_from_inputs(Arena *arena,
lnk_merge_manifest_files(mt_path, merged_manifest_path, unique_input_manifest_paths);
// read mt.exe output from disk
manifest_data = lnk_read_data_from_file_path(arena, io_flags, merged_manifest_path);
if (manifest_data.size == 0) {
B8 was_manifest_read;
manifest_data = lnk_read_data_from_file_path(arena, io_flags, merged_manifest_path, &was_manifest_read);
if (was_manifest_read == 0) {
lnk_error(LNK_Error_Mt, "unable to find mt.exe output manifest on disk, expected path \"%S\"", merged_manifest_path);
}
@@ -1228,13 +1064,17 @@ lnk_inputer_flush(Arena *arena, TP_Context *tp, LNK_Inputer *inputer, LNK_IO_Fla
ProfEnd();
ProfBegin("Load Inputs From Disk");
String8Array thin_input_datas = lnk_read_data_from_file_path_parallel(tp, inputer->arena, io_flags, thin_input_paths);
B32 is_mapped = !!(io_flags & (LNK_IO_Flags_MemoryMapFilesReadWrite|LNK_IO_Flags_MemoryMapFilesReadOnly));
B8 *thin_input_was_read = push_array(scratch.arena, B8, thin_input_paths.count);
String8Array thin_input_datas = lnk_read_data_from_file_path_parallel(tp, inputer->arena, io_flags, thin_input_paths, thin_input_was_read);
B32 is_mapped = !!(io_flags & (LNK_IO_Flags_MemoryMapFilesReadWrite|LNK_IO_Flags_MemoryMapFilesReadOnly));
for EachIndex(thin_input_idx, thin_inputs_count) {
thin_inputs[thin_input_idx]->has_disk_read_failed = thin_input_datas.v[thin_input_idx].size == 0;
thin_inputs[thin_input_idx]->owns_file_map = is_mapped && !thin_inputs[thin_input_idx]->has_disk_read_failed;
thin_inputs[thin_input_idx]->has_disk_read_failed = !thin_input_was_read[thin_input_idx];
thin_inputs[thin_input_idx]->owns_file_map = is_mapped && !thin_inputs[thin_input_idx]->has_disk_read_failed;
thin_inputs[thin_input_idx]->data = thin_input_datas.v[thin_input_idx];
}
ProfEnd();
ProfBegin("Disk Read Check");
@@ -2328,18 +2168,21 @@ lnk_link_image(TP_Context *tp, TP_Arena *arena, LNK_Config *config, LNK_Inputer
}
ProfBegin("Load .res files from disk");
for (String8Node *node = config->input_list[LNK_Input_Res].first; node != 0; node = node->next) {
String8 res_data = lnk_read_data_from_file_path(scratch.arena, config->io_flags, node->string);
if (res_data.size > 0) {
if (pe_is_res(res_data)) {
str8_list_push(scratch.arena, &res_data_list, res_data);
String8 stable_res_path = lnk_make_full_path(scratch.arena, config->path_style, config->work_dir, node->string);
str8_list_push(scratch.arena, &res_path_list, stable_res_path);
} else {
lnk_error(LNK_Error_LoadRes, "file is not of RES format: %S", node->string);
}
} else {
for EachNode(node, String8Node, config->input_list[LNK_Input_Res].first) {
B8 was_read = 0;
String8 res_data = lnk_read_data_from_file_path(scratch.arena, config->io_flags, node->string, &was_read);
if (was_read == 0) {
lnk_error(LNK_Error_LoadRes, "unable to open res file: %S", node->string);
continue;
}
if (pe_is_res(res_data)) {
str8_list_push(scratch.arena, &res_data_list, res_data);
String8 stable_res_path = lnk_make_full_path(scratch.arena, config->path_style, config->work_dir, node->string);
str8_list_push(scratch.arena, &res_path_list, stable_res_path);
} else {
lnk_error(LNK_Error_LoadRes, "file is not of RES format: %S", node->string);
}
}
ProfEnd();
@@ -2357,8 +2200,7 @@ lnk_link_image(TP_Context *tp, TP_Arena *arena, LNK_Config *config, LNK_Inputer
{
ProfBegin("Build * Linker * Obj");
String8 obj_name = str8_lit("* Linker *");
String8 raw_cmd_line = str8_list_join(scratch.arena, &config->raw_cmd_line, &(StringJoin){ str8_lit_comp(""), str8_lit_comp(" "), str8_lit_comp("") });
String8 obj_data = lnk_make_linker_coff_obj(arena->v[0], config->time_stamp, config->machine, config->work_dir, lnk_get_image_name(config), config->pdb_name, raw_cmd_line, obj_name);
String8 obj_data = lnk_make_linker_coff_obj(arena->v[0], config->time_stamp, config->machine, config->work_dir, lnk_get_image_name(config), config->pdb_name, config->raw_cmd_line, obj_name);
lnk_inputer_push_obj_linkgen(inputer, 0, obj_name, obj_data);
ProfEnd();
}
@@ -6893,9 +6735,12 @@ lnk_run_type_server(TP_Context *tp, TP_Arena *arena, LNK_Config *config)
Temp scratch = scratch_begin(arena->v, arena->count);
// push default type-server switches
lnk_config_pushf(config, "/DEBUG:GHASH");
lnk_config_pushf(config, "/NOD");
lnk_config_pushf(config, "/RAD_WRITE_TEMP_FILES");
{
LNK_CmdLine default_line = lnk_cmd_line_from_stringf_windows_rules(scratch.arena, "/DEBUG:GHASH /NOD /RAD_WRITE_TEMP_FILES");
for EachNode(cmd, LNK_CmdOption, default_line.first_option) {
lnk_apply_cmd_option_to_config(config, cmd->string, cmd->value_strings, &(LNK_Obj){0});
}
}
// load objs
U64 objs_count = 0;
@@ -7045,18 +6890,21 @@ entry_point(CmdLine *cmdline)
Temp scratch = scratch_begin(0,0);
lnk_log_begin();
LNK_Config *config = lnk_config_from_argcv(cmdline);
TP_Context *tp = tp_alloc(scratch.arena, config->worker_count, config->max_worker_count, config->shared_thread_pool_name);
TP_Arena *tp_arena = tp_arena_alloc(tp);
// init config from the command line
LNK_Config *config = lnk_config_init(cmdline->argc, cmdline->argv);
if (lnk_get_log_status(LNK_Log_Debug)) {
String8 full_cmd_line = str8_list_join(scratch.arena, &config->raw_cmd_line, &(StringJoin){ .sep = str8_lit_comp(" ") });
lnk_fprintf(stderr, "--------------------------------------------------------------------------------\n");
lnk_fprintf(stderr, "Command Line: %.*s\n", str8_varg(full_cmd_line));
lnk_fprintf(stderr, "Work Dir : %.*s\n", str8_varg(config->work_dir));
lnk_fprintf(stderr, "Command Line: %S\n", config->raw_cmd_line);
lnk_fprintf(stderr, "Work Dir : %S\n", config->work_dir);
lnk_fprintf(stderr, "--------------------------------------------------------------------------------\n");
}
// init thread pool
TP_Context *tp = tp_alloc(scratch.arena, config->worker_count, config->max_worker_count, config->shared_thread_pool_name);
TP_Arena *tp_arena = tp_arena_alloc(tp);
// pick entry point
switch (config->boot_mode) {
case LNK_BootMode_Linker: lnk_run_linker (tp, tp_arena, config); break;
case LNK_BootMode_TypeServer: lnk_run_type_server(tp, tp_arena, config); break;
-4
View File
@@ -332,10 +332,6 @@ typedef struct
String8 image_data;
} LNK_P2R;
// --- Config -----------------------------------------------------------------
internal LNK_Config * lnk_config_from_cmdline(CmdLine *cmdline);
// --- Entry Point -------------------------------------------------------------
internal void lnk_run(TP_Context *tp, TP_Arena *tp_arena, LNK_Config *config);
+11 -9
View File
@@ -129,9 +129,11 @@ internal void
lnk_cmd_line_concat_in_place(LNK_CmdLine *list, LNK_CmdLine *to_concat)
{
if (list->option_count > 0) {
list->option_count += to_concat->option_count;
list->last_option->next = to_concat->first_option;
list->last_option = to_concat->last_option;
if (to_concat->option_count > 0) {
list->option_count += to_concat->option_count;
list->last_option->next = to_concat->first_option;
list->last_option = to_concat->last_option;
}
str8_list_concat_in_place(&list->input_list, &to_concat->input_list);
str8_list_concat_in_place(&list->raw_cmd_line, &to_concat->raw_cmd_line);
} else {
@@ -147,11 +149,9 @@ lnk_cmd_line_parse_windows_rules(Arena *arena, String8List arg_list)
LNK_CmdLine cmd_line = {0};
cmd_line.raw_cmd_line = str8_list_copy(arena, &arg_list);
for (String8Node *arg_node = arg_list.first; arg_node != 0; arg_node = arg_node->next) {
for EachNode(arg_node, String8Node, arg_list.first) {
String8 arg = arg_node->string;
B32 is_option = str8_match_lit("/", arg, StringMatchFlag_RightSideSloppy) ||
str8_match_lit("-", arg, StringMatchFlag_RightSideSloppy);
if (is_option) {
if (str8_starts_with(arg, str8_lit("/")) || str8_starts_with(arg, str8_lit("-"))) {
U64 param_start_pos = str8_find_needle(arg, 0, str8_lit(":"), 0);
String8 option_name = str8_chop(arg, arg.size - param_start_pos);
@@ -162,12 +162,14 @@ lnk_cmd_line_parse_windows_rules(Arena *arena, String8List arg_list)
String8 value_string = str8_skip(arg, param_start_pos + 1);
// make value list
String8List value_list_unowned = str8_split_by_string_chars(scratch.arena, value_string, str8_lit(","), 0);
String8List value_list = str8_list_copy(arena, &value_list_unowned);
String8List value_list_temp = str8_split_by_string_chars(scratch.arena, value_string, str8_lit(","), 0);
String8List value_list = str8_list_copy(arena, &value_list_temp);
// push command
option_name = push_str8_copy(arena, option_name);
lnk_cmd_line_push_option_list(arena, &cmd_line, option_name, value_list);
} else if (str8_starts_with(arg, str8_lit("@"))) {
lnk_cmd_line_push_option_list(arena, &cmd_line, push_str8_copy(arena, arg), (String8List){0});
} else {
str8_list_push(arena, &cmd_line.input_list, push_str8_copy(arena, arg));
}
+326 -110
View File
@@ -66,49 +66,49 @@ global read_only LNK_CmdSwitch g_cmd_switch_map[] =
{ LNK_CmdSwitch_Version, 0, "VERSION", "", "Image version." },
{ LNK_CmdSwitch_WholeArchive, 0, "WHOLEARCHIVE", "[:LIBNAME]", "Force linker to pull in all objs from the specified lib." },
{ LNK_CmdSwitch_Rad_Age, 0, "RAD_AGE", ":#", "Age embeded in EXE and PDB, used to validate incremental build. Default is 1." },
{ LNK_CmdSwitch_Rad_Age, 0, "RAD_AGE", ":#", "Age embeded in EXE and PDB, used to validate incremental build. Default is 1." },
//{ LNK_CmdSwitch_Rad_BuildExp, 0, "RAD_BUILD_EXP", "[:NO]", "Build export data." },
{ LNK_CmdSwitch_Rad_BuildInfo, 0, "RAD_BUILD_INFO", "", "Print build info and exit." },
{ LNK_CmdSwitch_Rad_BuildImpLib, 0, "RAD_BUILD_IMPLIB", "[:NO]", "Build import library." },
{ LNK_CmdSwitch_Rad_CheckUnusedDelayLoadDll, 0, "RAD_CHECK_UNUSED_DELAY_LOAD_DLL", "[:NO]", "Check for unused delay load dlls." },
{ LNK_CmdSwitch_Rad_DataDirCount, 0, "RAD_DATA_DIR_COUNT", ":#", "Internal default for PE optional header data directory count." },
{ LNK_CmdSwitch_Rad_Map, 0, "RAD_MAP", ":FILENAME", "Emit file with the output image's layout description." },
{ LNK_CmdSwitch_Rad_MapLinesForUnresolvedSymbols, 0, "RAD_MAP_LINES_FOR_UNRESOLVED_SYMBOLS", "[:NO]", "Use debug info to print source file location for unresolved symbol" },
{ LNK_CmdSwitch_Rad_BuildInfo, 0, "RAD_BUILD_INFO", "", "Print build info and exit." },
{ LNK_CmdSwitch_Rad_BuildImpLib, 0, "RAD_BUILD_IMPLIB", "[:NO]", "Build import library." },
{ LNK_CmdSwitch_Rad_CheckUnusedDelayLoadDll, 0, "RAD_CHECK_UNUSED_DELAY_LOAD_DLL", "[:NO]", "Check for unused delay load dlls." },
{ LNK_CmdSwitch_Rad_DataDirCount, 0, "RAD_DATA_DIR_COUNT", ":#", "Internal default for PE optional header data directory count." },
{ LNK_CmdSwitch_Rad_Map, 0, "RAD_MAP", ":FILENAME", "Emit file with the output image's layout description." },
{ LNK_CmdSwitch_Rad_MapLinesForUnresolvedSymbols, 0, "RAD_MAP_LINES_FOR_UNRESOLVED_SYMBOLS", "[:NO]", "Use debug info to print source file location for unresolved symbol" },
{ LNK_CmdSwitch_Rad_MemoryMapFiles, 0, "RAD_MEMORY_MAP_FILES", "[:{NO|READ_ONLY|READ_WRITE}]", "When enabled, files are memory-mapped instead of being read entirely on request." },
{ LNK_CmdSwitch_Rad_BootMode, 0, "RAD_BOOT_MODE", "[:LINKER|TYPE_SERVER]", "Overrides default boot program." },
{ LNK_CmdSwitch_Rad_Debug, 0, "RAD_DEBUG", "[:NO]", "Emit RAD debug info file." },
{ LNK_CmdSwitch_Rad_DebugAltPath, 0, "RAD_DEBUGALTPATH", ":PATH", "Alternative output path for the RDI." },
{ LNK_CmdSwitch_Rad_DebugName, 0, "RAD_DEBUG_NAME", ":FILENAME", "Set file name for RAD debug info file." },
{ LNK_CmdSwitch_Rad_DelayBind, 0, "RAD_DELAY_BIND", "[:NO]", "Emit bindable imports." },
{ LNK_CmdSwitch_Rad_DoMerge, 0, "RAD_DO_MERGE", "[:NO]", "Set whether the linker should execute /MERGE." },
{ LNK_CmdSwitch_Rad_EnvLib, 0, "RAD_ENV_LIB", "[:NO]", "Collect libraries from %%LIB%% and %%LIBPATH%% varibles." },
{ LNK_CmdSwitch_Rad_Exe, 0, "RAD_EXE", "[:NO]", "Set EXE bit in the image header." },
{ LNK_CmdSwitch_Rad_BootMode, 0, "RAD_BOOT_MODE", "[:LINKER|TYPE_SERVER]", "Overrides default boot program." },
{ LNK_CmdSwitch_Rad_Debug, 0, "RAD_DEBUG", "[:NO]", "Emit RAD debug info file." },
{ LNK_CmdSwitch_Rad_DebugAltPath, 0, "RAD_DEBUGALTPATH", ":PATH", "Alternative output path for the RDI." },
{ LNK_CmdSwitch_Rad_DebugName, 0, "RAD_DEBUG_NAME", ":FILENAME", "Set file name for RAD debug info file." },
{ LNK_CmdSwitch_Rad_DelayBind, 0, "RAD_DELAY_BIND", "[:NO]", "Emit bindable imports." },
{ LNK_CmdSwitch_Rad_DoMerge, 0, "RAD_DO_MERGE", "[:NO]", "Set whether the linker should execute /MERGE." },
{ LNK_CmdSwitch_Rad_EnvLib, 0, "RAD_ENV_LIB", "[:NO]", "Collect libraries from %%LIB%% and %%LIBPATH%% varibles." },
{ LNK_CmdSwitch_Rad_Exe, 0, "RAD_EXE", "[:NO]", "Set EXE bit in the image header." },
{ LNK_CmdSwitch_Rad_Guid, 0, "RAD_GUID", ":{IMAGEBLAKE3|XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX}", "The image guid that is embeded in the debug info." },
{ LNK_CmdSwitch_Rad_LargePages, 0, "RAD_LARGE_PAGES", "[:NO]", "Disabled by default on Windows." },
{ LNK_CmdSwitch_Rad_LinkVer, 0, "RAD_LINK_VER", ":##,##", "Linker version." },
{ LNK_CmdSwitch_Rad_Log, 0, "RAD_LOG", ":{ALL,INPUT_OBJ,INPUT_LIB,IO,LINK_STATS,TIMERS}", "Loggers." },
{ LNK_CmdSwitch_Rad_MtPath, 0, "RAD_MT_PATH", ":EXEPATH", "Exe path to the manifest tool (default: " LNK_MANIFEST_MERGE_TOOL_NAME ")" },
{ LNK_CmdSwitch_Rad_OsVer, 0, "RAD_OS_VER", ":##,##", "OS version." },
{ LNK_CmdSwitch_Rad_PageSize, 0, "RAD_PAGE_SIZE", ":#", "Must be power of two." },
{ LNK_CmdSwitch_Rad_PathStyle, 0, "RAD_PATH_STYLE", ":{WindowsAbsolute|UnixAbsolute}", "Set path style in the PDB." },
{ LNK_CmdSwitch_Rad_PdbHashTypeNameLength, 0, "RAD_PDB_HASH_TYPE_NAME_LENGTH", ":#", "Number of hash bytes to use to replace type name. Default 8 bytes (Max 16)." },
{ LNK_CmdSwitch_Rad_PdbHashTypeNameMap, 0, "RAD_PDB_HASH_TYPE_NAME_MAP", ":FILENAME", "Produce map file with hash -> type name mappings." },
{ LNK_CmdSwitch_Rad_PdbHashTypeNames, 0, "RAD_PDB_HASH_TYPE_NAMES", ":{NONE|LENIENT|FULL}", "Replace type names in LF_STRUCTURE and LF_CLASS with hashes." },
{ LNK_CmdSwitch_Rad_RemoveSection, 0, "RAD_REMOVE_SECTION", ":NAME", "Removes a section from the image." },
{ LNK_CmdSwitch_Rad_SharedThreadPool, 0, "RAD_SHARED_THREAD_POOL", "[:STRING]", "Default value \"" LNK_DEFAULT_THREAD_POOL_NAME "\"" },
{ LNK_CmdSwitch_Rad_SharedThreadPoolMaxWorkers, 0, "RAD_SHARED_THREAD_POOL_MAX_WORKERS", ":#", "Set maximum number of workers in a thread pool." },
{ LNK_CmdSwitch_Rad_SortImports, 0, "RAD_SORT_IMPORTS", "[:NO]", "Sort static and delayed import tables by their order of appearance in libs, without assuming link order." },
{ LNK_CmdSwitch_Rad_IcfHashKind, 0, "RAD_ICF_HASH_KIND", "{BLAKE3|XXHASH}", "Sets hashing algorithm for /OPT:ICF." },
{ LNK_CmdSwitch_Rad_Ignore, 0, "RAD_IGNORE", ":#", "Ignore the specified RAD linker warning." },
{ LNK_CmdSwitch_Rad_ImageAltPath, 0, "RAD_IMAGEALTPATH", ":FILENAME", "Alternative name for the image" },
{ LNK_CmdSwitch_Rad_WriteTempFiles, 0, "RAD_WRITE_TEMP_FILES", "[:NO]", "When speicifed linker writes image and debug info to temporary files and renames after link is done." },
{ LNK_CmdSwitch_Rad_TimeStamp, 0, "RAD_TIME_STAMP", ":#", "Time stamp embeded in EXE and PDB." },
{ LNK_CmdSwitch_Rad_DebugTypeHash, 0, "RAD_DEBUG_TYPE_HASH", ":{BLAKE3|XXHASH}", "Sets hashing algorithm for debug type merging." },
{ LNK_CmdSwitch_Rad_UnresolvedSymbolLimit, 0, "RAD_UNRESOLVED_SYMBOL_LIMIT", ":#", "Limits number of unresolved symbol errors linker reports." },
{ LNK_CmdSwitch_Rad_UnresolvedSymbolRefLimit, 0, "RAD_UNRESOLVED_SYMBOL_REF_LIMIT", ":#", "Limit number of unresolved symbol references linker reports." },
{ LNK_CmdSwitch_Rad_Version, 0, "RAD_VERSION", "", "Print version and exit." },
{ LNK_CmdSwitch_Rad_Workers, 0, "RAD_WORKERS", ":#", "Set number of workers created in the pool. Number is capped at 1024. When /RAD_SHARED_THREAD_POOL is specified this number cant exceed /RAD_SHARED_THREAD_POOL_MAX_WORKERS." },
{ LNK_CmdSwitch_Rad_WorkDir, 0, "RAD_WORK_DIR", ":PATH", "Working directory used for stable debug paths." },
{ LNK_CmdSwitch_Rad_LargePages, 0, "RAD_LARGE_PAGES", "[:NO]", "Disabled by default on Windows." },
{ LNK_CmdSwitch_Rad_LinkVer, 0, "RAD_LINK_VER", ":##,##", "Linker version." },
{ LNK_CmdSwitch_Rad_Log, 0, "RAD_LOG", ":{ALL,INPUT_OBJ,INPUT_LIB,IO,LINK_STATS,TIMERS}", "Loggers." },
{ LNK_CmdSwitch_Rad_MtPath, 0, "RAD_MT_PATH", ":EXEPATH", "Exe path to the manifest tool (default: " LNK_MANIFEST_MERGE_TOOL_NAME ")" },
{ LNK_CmdSwitch_Rad_OsVer, 0, "RAD_OS_VER", ":##,##", "OS version." },
{ LNK_CmdSwitch_Rad_PageSize, 0, "RAD_PAGE_SIZE", ":#", "Must be power of two." },
{ LNK_CmdSwitch_Rad_PathStyle, 0, "RAD_PATH_STYLE", ":{WindowsAbsolute|UnixAbsolute}", "Set path style in the PDB." },
{ LNK_CmdSwitch_Rad_PdbHashTypeNameLength, 0, "RAD_PDB_HASH_TYPE_NAME_LENGTH", ":#", "Number of hash bytes to use to replace type name. Default 8 bytes (Max 16)." },
{ LNK_CmdSwitch_Rad_PdbHashTypeNameMap, 0, "RAD_PDB_HASH_TYPE_NAME_MAP", ":FILENAME", "Produce map file with hash -> type name mappings." },
{ LNK_CmdSwitch_Rad_PdbHashTypeNames, 0, "RAD_PDB_HASH_TYPE_NAMES", ":{NONE|LENIENT|FULL}", "Replace type names in LF_STRUCTURE and LF_CLASS with hashes." },
{ LNK_CmdSwitch_Rad_RemoveSection, 0, "RAD_REMOVE_SECTION", ":NAME", "Removes a section from the image." },
{ LNK_CmdSwitch_Rad_SharedThreadPool, 0, "RAD_SHARED_THREAD_POOL", "[:STRING]", "Default value \"" LNK_DEFAULT_THREAD_POOL_NAME "\"" },
{ LNK_CmdSwitch_Rad_SharedThreadPoolMaxWorkers, 0, "RAD_SHARED_THREAD_POOL_MAX_WORKERS", ":#", "Set maximum number of workers in a thread pool." },
{ LNK_CmdSwitch_Rad_SortImports, 0, "RAD_SORT_IMPORTS", "[:NO]", "Sort static and delayed import tables by their order of appearance in libs, without assuming link order." },
{ LNK_CmdSwitch_Rad_IcfHashKind, 0, "RAD_ICF_HASH_KIND", "{BLAKE3|XXHASH}", "Sets hashing algorithm for /OPT:ICF." },
{ LNK_CmdSwitch_Rad_Ignore, 0, "RAD_IGNORE", ":#", "Ignore the specified RAD linker warning." },
{ LNK_CmdSwitch_Rad_ImageAltPath, 0, "RAD_IMAGEALTPATH", ":FILENAME", "Alternative name for the image" },
{ LNK_CmdSwitch_Rad_WriteTempFiles, 0, "RAD_WRITE_TEMP_FILES", "[:NO]", "When speicifed linker writes image and debug info to temporary files and renames after link is done." },
{ LNK_CmdSwitch_Rad_TimeStamp, 0, "RAD_TIME_STAMP", ":#", "Time stamp embeded in EXE and PDB." },
{ LNK_CmdSwitch_Rad_DebugTypeHash, 0, "RAD_DEBUG_TYPE_HASH", ":{BLAKE3|XXHASH}", "Sets hashing algorithm for debug type merging." },
{ LNK_CmdSwitch_Rad_UnresolvedSymbolLimit, 0, "RAD_UNRESOLVED_SYMBOL_LIMIT", ":#", "Limits number of unresolved symbol errors linker reports." },
{ LNK_CmdSwitch_Rad_UnresolvedSymbolRefLimit, 0, "RAD_UNRESOLVED_SYMBOL_REF_LIMIT", ":#", "Limit number of unresolved symbol references linker reports." },
{ LNK_CmdSwitch_Rad_Version, 0, "RAD_VERSION", "", "Print version and exit." },
{ LNK_CmdSwitch_Rad_Workers, 0, "RAD_WORKERS", ":#", "Set number of workers created in the pool. Number is capped at 1024. When /RAD_SHARED_THREAD_POOL is specified this number cant exceed /RAD_SHARED_THREAD_POOL_MAX_WORKERS." },
{ LNK_CmdSwitch_Rad_WorkDir, 0, "RAD_WORK_DIR", ":PATH", "Working directory used for stable debug paths." },
{ LNK_CmdSwitch_RadTypeServer, 0, "RAD_TYPE_SERVER", ":FILENAME", "Merge types and store them in the specified file. The filename must have the .rrt extension." },
@@ -157,7 +157,7 @@ global read_only struct
internal LNK_CmdSwitchType
lnk_cmd_switch_type_from_string(String8 name)
{
for (U64 i = 0; i < ArrayCount(g_cmd_switch_map); i += 1) {
for EachElement(i, g_cmd_switch_map) {
if (str8_match_cstr(g_cmd_switch_map[i].name, name, StringMatchFlag_CaseInsensitive)) {
return g_cmd_switch_map[i].type;
}
@@ -1184,47 +1184,6 @@ lnk_print_help(void)
scratch_end(scratch);
}
internal String8List
lnk_unwrap_rsp(Arena *arena, String8List arg_list)
{
Temp scratch = scratch_begin(&arena, 1);
String8List result = {0};
for (String8Node *curr = arg_list.first; curr != 0; curr = curr->next) {
B32 is_rsp = str8_match_lit("@", curr->string, StringMatchFlag_RightSideSloppy);
if (is_rsp) {
// remove "@"
String8 name = str8_skip(curr->string, 1);
if (file_path_exists(name)) {
// read rsp from disk
String8 file = lnk_read_data_from_file_path(scratch.arena, 0, name);
file = lnk_text_file_string_from_data(scratch.arena, file);
// parse rsp
String8List rsp_args = lnk_arg_list_parse_windows_rules(scratch.arena, file);
// handle case where rsp references another rsp
String8List list = lnk_unwrap_rsp(arena, rsp_args);
// push arguments from rsp
list = str8_list_copy(arena, &list);
str8_list_concat_in_place(&result, &list);
} else {
lnk_error(LNK_Error_Cmdl, "unable to find rsp: %S", name);
}
} else {
// push regular argument
String8 str = push_str8_copy(arena, curr->string);
str8_list_push(arena, &result, str);
}
}
scratch_end(scratch);
return result;
}
internal void
lnk_apply_write_temp_files(Arena *arena, LNK_Config *config)
{
@@ -2486,8 +2445,14 @@ lnk_apply_def_file_to_config(LNK_Config *config, String8 path, LNK_Obj *obj)
Temp scratch = scratch_begin(&config->arena, 1);
// load DEF file
String8 raw_file = lnk_read_data_from_file_path(scratch.arena, config->io_flags, path);
String8 file = lnk_text_file_string_from_data(scratch.arena, raw_file);
B8 file_read = 0;
String8 raw_file = lnk_read_data_from_file_path(scratch.arena, config->io_flags, path, &file_read);
if ( ! file_read) {
lnk_error(LNK_Error_Cmdl, "failed to read DEF file: %S", path);
goto exit;
}
String8 file = lnk_text_file_string_from_data(scratch.arena, raw_file);
// parse & collect normalized DEF lines
LNK_DefFileLineList def_lines = {0};
@@ -2723,32 +2688,285 @@ lnk_apply_def_file_to_config(LNK_Config *config, String8 path, LNK_Obj *obj)
}
}
exit:;
scratch_end(scratch);
}
internal void
lnk_config_pushf(LNK_Config *config, char *fmt, ...)
internal String8List
lnk_unwrap_cmd_line(Arena *arena, String8 arg_string)
{
va_list args;
va_start(args, fmt);
LNK_CmdLine cmd_line = lnk_cmd_line_from_stringfv_windows_rules(config->arena, fmt, args);
va_end(args);
Temp scratch = scratch_begin(&arena, 1);
for EachNode(cmd, LNK_CmdOption, cmd_line.first_option) {
lnk_apply_cmd_option_to_config(config, cmd->string, cmd->value_strings, &(LNK_Obj){0});
typedef struct LNK_RspFrame LNK_RspFrame;
struct LNK_RspFrame
{
LNK_RspFrame *next;
String8 path;
String8Node *arg_cursor;
};
String8List result = {0};
String8List arg_list = lnk_arg_list_parse_windows_rules(scratch.arena, arg_string);
LNK_RspFrame *frame = &(LNK_RspFrame){ .arg_cursor = arg_list.first, .path = str8_lit("Command Line") };
while (frame) {
if (frame->arg_cursor == 0) {
frame = frame->next;
continue;
}
String8Node *curr = frame->arg_cursor;
frame->arg_cursor = curr->next;
if (str8_starts_with(curr->string, str8_lit("@"))) {
String8 file_name = str8_skip(curr->string, 1);
// error check empty rsp argument
if (file_name.size == 0) {
lnk_error(LNK_Error_Cmdl, "RSP file name must follow '@'");
continue;
}
String8 full_path = full_path_from_path(scratch.arena, file_name);
// error check for cyclic response files
{
B32 is_rsp_cyclic = 0;
for EachNode(parent, LNK_RspFrame, frame) {
if (path_match_normalized(parent->path, full_path)) {
is_rsp_cyclic = 1;
break;
}
}
if (is_rsp_cyclic) {
String8List cycle_list = {0};
for EachNode(parent, LNK_RspFrame, frame) {
str8_list_push_front(scratch.arena, &cycle_list, parent->path);
}
str8_list_push(scratch.arena, &cycle_list, full_path);
String8 cycle_string = str8_list_join(scratch.arena, &cycle_list, &(StringJoin){ .sep = str8_lit(" -> ") });
lnk_error(LNK_Error_Cmdl, "detected a cyclic RSP: %S", cycle_string);
continue;
}
}
// read rsp from disk
B8 is_file_read = 0;
String8 file_data = lnk_read_data_from_file_path(scratch.arena, 0, file_name, &is_file_read);
if ( ! is_file_read) {
lnk_error(LNK_Error_Cmdl, "unable to find RSP: %S", file_name);
continue;
}
// unapck rsp
String8 file_text = lnk_text_file_string_from_data(scratch.arena, file_data);
String8List file_args = lnk_arg_list_parse_windows_rules(scratch.arena, file_text);
// push new frame with unapcked rsp
LNK_RspFrame *new_frame = push_array(scratch.arena, LNK_RspFrame, 1);
new_frame->path = full_path;
new_frame->arg_cursor = file_args.first;
SLLStackPush(frame, new_frame);
} else {
// append normal argument
str8_list_push(arena, &result, push_str8_copy(arena, curr->string));
}
}
scratch_end(scratch);
return result;
}
internal LNK_CmdLine
lnk_make_default_cmd_line(Arena *arena, LNK_CmdLine user_cmd_line)
{
Temp scratch = scratch_begin(&arena, 1);
LNK_CmdLine cmd_line = {0};
char *default_opts[] = {
"/ALIGN:4096",
"/DEBUG:none",
"/FILEALIGN:512",
"/HIGHENTROPYVA",
"/MANIFESTUAC:\"level='asInvoker' uiAccess='false'\"",
"/NXCOMPAT",
"/LARGEADDRESSAWARE",
"/PDBALTPATH:%_RAD_PDB_PATH%",
"/PDBPAGESIZE:4096",
(char*)str8f(scratch.arena, "/HEAP:%llu,%llu", MB(1), KB(4)).str,
(char*)str8f(scratch.arena, "/STACK:%llu,%llu", MB(1), KB(4)).str,
"/RAD_BOOT_MODE:LINKER",
//"/RAD_BUILD_EXP",
"/RAD_BUILD_IMPLIB",
"/RAD_AGE:1",
"/RAD_CHECK_UNUSED_DELAY_LOAD_DLL",
"/RAD_DO_MERGE",
"/RAD_ENV_LIB",
"/RAD_EXE",
"/RAD_GUID:imageblake3",
"/RAD_LARGE_PAGES:no",
"/RAD_LINK_VER:14.0",
"/RAD_OS_VER:6.0",
"/RAD_PAGE_SIZE:4096",
"/RAD_PATH_STYLE:system",
"/RAD_PDB_HASH_TYPE_NAMES:NONE",
"/RAD_PDB_HASH_TYPE_NAME_LENGTH:8",
"/RAD_DEBUGALTPATH:%_RAD_RDI_PATH%",
"/RAD_MEMORY_MAP_FILES",
"/RAD_MAP_LINES_FOR_UNRESOLVED_SYMBOLS",
"/RAD_UNRESOLVED_SYMBOL_LIMIT:1000",
"/RAD_UNRESOLVED_SYMBOL_REF_LIMIT:10",
"/RAD_SORT_IMPORTS",
(char*)str8f(scratch.arena, "/RAD_MT_PATH:%s", LNK_MANIFEST_MERGE_TOOL_NAME).str,
(char*)str8f(scratch.arena, "/RAD_DATA_DIR_COUNT:%u", PE_DataDirectoryIndex_COUNT).str,
// Set BLAKE3 as the default to match the LLVM default.
//
// When hash kinds conflict, radlink discards any .debug$H sections
// whose hash kind does not match the selected default.
"/RAD_DEBUG_TYPE_HASH:BLAKE3",
// Use LLVM significant addresses hints for the /OPT:ICF.
"/LLVM_ADDRSIG",
// By default keep full type names, override when TPI/IPI streams overflow.
"/RAD_PDB_HASH_TYPE_NAMES:NONE",
// TODO: The ICF algorithm requires a cryptographic hash to establish
// equivalence. With xxHash and similar non-cryptographic hashes,
// the algorithm must compare each section property before
// deciding whether sections are truly identical.
"/RAD_ICF_HASH_KIND:BLAKE3",
};
char *push_opts[] = {
"/MERGE:.xdata=.rdata",
"/MERGE:.00cfg=.rdata",
// TODO: .tls must be always first contribution in .data section because compiler generates TLS relative movs
//"/MERGE:.tls=.data",
"/MERGE:.idata=.data",
"/MERGE:.didat=.data",
"/MERGE:.edata=.rdata",
"/MERGE:.RAD_LINK_PE_DEBUG_DIR=.rdata",
"/MERGE:.RAD_LINK_PE_DEBUG_DATA=.rdata",
"/RAD_REMOVE_SECTION:.debug",
"/RAD_REMOVE_SECTION:.gehcont",
"/RAD_REMOVE_SECTION:.gfids",
"/RAD_REMOVE_SECTION:.gxfg",
(char*)str8f(scratch.arena, "/RAD_WORKERS:%u", get_system_info()->logical_processor_count).str,
// errors that are too verbose in release build
(char*)str8f(scratch.arena, "/RAD_IGNORE:%d", LNK_Warning_UnknownSwitch * (BUILD_DEBUG ? -1 : 1)).str,
(char*)str8f(scratch.arena, "/RAD_IGNORE:%d", LNK_Warning_UnknownDirective * (BUILD_DEBUG ? -1 : 1)).str,
(char*)str8f(scratch.arena, "/RAD_IGNORE:%d", LNK_Error_InvalidTypeIndex * (BUILD_DEBUG ? -1 : 1)).str,
#if BUILD_DEBUG
"/RAD_LOG:debug",
"/RAD_LOG:io_write",
#else
(char*)str8f(scratch.arena, "/RAD_IGNORE:%u", LNK_Error_InvalidTypeIndex).str,
#endif
};
#define DefaultOpt(...) do { \
LNK_CmdLine parsed_cmd_line = lnk_cmd_line_from_stringf_windows_rules(arena, __VA_ARGS__); \
for EachNode(cmd, LNK_CmdOption, parsed_cmd_line.first_option) { \
if (!lnk_cmd_line_has_switch(user_cmd_line, lnk_cmd_switch_type_from_string(cmd->string))) { \
String8List value_strings = str8_list_copy(arena, &cmd->value_strings); \
lnk_cmd_line_push_option_list(arena, &cmd_line, cmd->string, value_strings); \
} \
} \
} while (0)
#define PushOpt(...) do { \
LNK_CmdLine parsed_cmd_line = lnk_cmd_line_from_stringf_windows_rules(arena, __VA_ARGS__); \
lnk_cmd_line_concat_in_place(&cmd_line, &parsed_cmd_line); \
} while (0)
if (lnk_cmd_line_has_switch(user_cmd_line, LNK_CmdSwitch_Dll)) {
DefaultOpt("/SUBSYSTEM:%S", pe_string_from_subsystem(PE_WindowsSubsystem_WINDOWS_GUI));
}
if (!lnk_cmd_line_has_switch(user_cmd_line, LNK_CmdSwitch_Brepro)) {
DefaultOpt("/RAD_TIME_STAMP:%u", get_process_start_time_unix());
}
for EachIndex(i, ArrayCount(default_opts)) {
DefaultOpt("%s", default_opts[i]);
}
for EachIndex(i, ArrayCount(push_opts)) {
PushOpt("%s", push_opts[i]);
}
// when /FORCE is specified on the command line, do not stop on these errors
if (lnk_cmd_line_has_switch(user_cmd_line, LNK_CmdSwitch_Force)) {
g_error_mode_arr[LNK_Error_UnresolvedSymbol] = LNK_ErrorMode_Continue;
g_error_mode_arr[LNK_Error_RelocationAgainstRemovedSection] = LNK_ErrorMode_Continue;
}
#undef DefaultOpt
#undef PushOpt
scratch_end(scratch);
return cmd_line;
}
internal LNK_Config *
lnk_config_init(LNK_CmdLine cmd_line)
lnk_config_init(U64 argc, char **argv)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0,0);
// load env vars
HashMap env_vars = lnk_env_vars_from_process_info(scratch.arena, get_process_info(), LNK_EnvVarRule_Batch);
// concat argv
String8 user_cmd_line;
{
String8List raw_cmd_line = {0};
for (U64 i = 1; i < argc; i += 1) {
str8_list_push(scratch.arena, &raw_cmd_line, str8_cstring(argv[i]));
}
user_cmd_line = str8_list_join(scratch.arena, &raw_cmd_line, &(StringJoin){.sep = str8_lit(" ") });
}
// concat command line from env vars
String8 cmd_line_string = {0};
{
LNK_EnvVar *link_var_pre = lnk_env_var_from_mapf(&env_vars, "LINK");
LNK_EnvVar *link_var_post = lnk_env_var_from_mapf(&env_vars, "_LINK_");
String8List list = {0};
if (link_var_pre) {
str8_list_push(scratch.arena, &list, link_var_pre->raw_value);
}
str8_list_push(scratch.arena, &list, user_cmd_line);
if (link_var_post) {
str8_list_push(scratch.arena, &list, link_var_post->raw_value);
}
cmd_line_string = str8_list_join(scratch.arena, &list, &(StringJoin){.sep = str8_lit(" ")});
}
#if PROFILE_TELEMETRY
tmMessage(0, TMMF_ICON_NOTE, "Command Line: %.*s", str8_varg(cmd_line_string));
#endif
// concat default arguments
LNK_CmdLine cmd_line = {0};
{
String8List cmd_unwrap = lnk_unwrap_cmd_line(scratch.arena, cmd_line_string);
LNK_CmdLine cmd_line_user = lnk_cmd_line_parse_windows_rules(scratch.arena, cmd_unwrap);
LNK_CmdLine cmd_line_default = lnk_make_default_cmd_line(scratch.arena, cmd_line_user);
lnk_cmd_line_concat_in_place(&cmd_line, &cmd_line_default);
lnk_cmd_line_concat_in_place(&cmd_line, &cmd_line_user);
}
Arena *arena = arena_alloc();
LNK_Config *config = push_array(arena, LNK_Config, 1);
config->arena = arena;
config->raw_cmd_line = str8_list_copy(arena, &cmd_line.raw_cmd_line);
config->raw_cmd_line = str8_copy(arena, cmd_line_string);
config->work_dir = get_current_path(arena);
config->force = lnk_cmd_line_has_switch(cmd_line, LNK_CmdSwitch_Force);
@@ -2917,10 +3135,8 @@ lnk_config_init(LNK_CmdLine cmd_line)
config->imp_lib_name = full_path_from_path(arena, config->imp_lib_name);
config->manifest_name = full_path_from_path(arena, config->manifest_name);
// set up env vars
HashMap env_vars = lnk_env_vars_from_process_info(scratch.arena, get_process_info(), LNK_EnvVarRule_Batch);
// push linker env vars
{
// define linker env vars
struct { String8 key, value; } key_value_str8_table[] = {
{ str8_lit("_pdb"), str8_skip_last_slash(config->pdb_name) },
{ str8_lit("_ext"), str8_skip_last_dot(config->out_path) },
@@ -2934,20 +3150,20 @@ lnk_config_init(LNK_CmdLine cmd_line)
}
lnk_env_var_batchf(scratch.arena, &env_vars, "%S=%S", key_value_str8_table[i].key, key_value_str8_table[i].value);
}
}
if (config->flags & LNK_ConfigFlag_EnvLib) {
// collect LIB and LIBPATH
struct { String8List *config_list; char *key; } key_str8_list[] = {
{ &config->lib_dir_list, "lib" },
{ &config->lib_dir_list, "lib_path" },
};
for EachElement(i, key_str8_list) {
LNK_EnvVar *var = lnk_env_var_from_mapf(&env_vars, key_str8_list[i].key);
if (var) {
String8List value = lnk_value_list_from_env_var(arena, var);
String8List value_copy = str8_list_copy(config->arena, &value);
str8_list_concat_in_place(key_str8_list[i].config_list, &value_copy);
}
// collect LIB and LIBPATH
if (config->flags & LNK_ConfigFlag_EnvLib) {
struct { String8List *config_list; char *key; } key_str8_list[] = {
{ &config->lib_dir_list, "lib" },
{ &config->lib_dir_list, "lib_path" },
};
for EachElement(i, key_str8_list) {
LNK_EnvVar *var = lnk_env_var_from_mapf(&env_vars, key_str8_list[i].key);
if (var) {
String8List value = lnk_value_list_from_env_var(arena, var);
String8List value_copy = str8_list_copy(config->arena, &value);
str8_list_concat_in_place(key_str8_list[i].config_list, &value_copy);
}
}
}
+3 -3
View File
@@ -360,7 +360,7 @@ typedef struct LNK_Config
String8 out_path;
String8 image_alt_path;
String8 imp_lib_name;
String8List raw_cmd_line;
String8 raw_cmd_line;
String8 pdb_name;
String8 pdb_alt_path;
String8 pdb_stripped_name;
@@ -614,6 +614,6 @@ internal void lnk_whole_archive(LNK_Config *config, String8 lib_name);
// --- Config ------------------------------------------------------------------
internal void lnk_apply_cmd_option_to_config(LNK_Config *config, String8 name, String8List value_list, struct LNK_Obj *obj);
internal void lnk_config_pushf(LNK_Config *config, char *fmt, ...);
internal LNK_Config * lnk_config_init(LNK_CmdLine cmd_line);
internal LNK_Config * lnk_config_init(U64 argc, char **argv);
+19 -7
View File
@@ -195,7 +195,17 @@ THREAD_POOL_TASK_FUNC(lnk_read_type_servers_task)
} break;
case LNK_TypeServerKind_PDB: {
// read PDB from disk
String8 msf_data = lnk_read_data_from_file_path(scratch.arena, task->config->io_flags, ts->ts_path);
B8 msf_data_read = 0;
String8 msf_data = lnk_read_data_from_file_path(scratch.arena, task->config->io_flags, ts->ts_path, &msf_data_read);
if ( ! msf_data_read) {
LNK_Obj *dep_obj = 0;
if (ts->obj_indices.count) {
dep_obj = task->obj_arr[ts->obj_indices.first->data];
}
lnk_error_obj(LNK_Error_UnableToOpenTypeServer, dep_obj, "failed to read type server from path: %S", ts->ts_path);
goto exit;
}
// check magic
if (!msf_check_magic_70(msf_data) && msf_check_magic_20(msf_data)) { goto exit; }
@@ -632,9 +642,10 @@ lnk_rrt_array_from_config(Arena *arena, LNK_Config *config)
ProfBegin("Parse RRT");
LNK_RRT_Array rrt_arr = { .v = push_array(arena, LNK_RRT, config->input_list[LNK_Input_RRT].node_count) };
for EachNode(n, String8Node, config->input_list[LNK_Input_RRT].first) {
String8 rrt_path = n->string;
String8 raw_rrt = lnk_read_data_from_file_path(arena, config->io_flags, rrt_path);
if (raw_rrt.size == 0) {
B8 was_rrt_read = 0;
String8 rrt_path = n->string;
String8 raw_rrt = lnk_read_data_from_file_path(arena, config->io_flags, rrt_path, &was_rrt_read);
if (raw_rrt.size == 0 || was_rrt_read == 0) {
lnk_error(LNK_Error_IllData, "ERROR: failed to open \"%S\"", rrt_path);
continue;
}
@@ -3392,15 +3403,16 @@ lnk_build_pdb(TP_Context *tp, TP_Arena *tp_arena, String8 image_data, LNK_Config
if (builder_flags & LNK_PDB_BuilderFlag_NATVIS) {
ProfBegin("Build NatVis");
{
String8Array natvis_file_path_arr = str8_array_from_list(scratch.arena, &config->natvis_list);
String8Array natvis_file_data_arr = lnk_read_data_from_file_path_parallel(tp, scratch.arena, config->io_flags, natvis_file_path_arr);
String8Array natvis_file_path_arr = str8_array_from_list(scratch.arena, &config->natvis_list);
B8 *natvis_was_read = push_array(scratch.arena, B8, natvis_file_path_arr.count);
String8Array natvis_file_data_arr = lnk_read_data_from_file_path_parallel(tp, scratch.arena, config->io_flags, natvis_file_path_arr, natvis_was_read);
for EachIndex(i, natvis_file_data_arr.count) {
String8 natvis_file_path = natvis_file_path_arr.v[i];
String8 natvis_file_data = natvis_file_data_arr.v[i];
// did we read the file?
if (natvis_file_data.size == 0) {
if (natvis_was_read[i] == 0 || natvis_file_data.size == 0) {
lnk_error(LNK_Warning_FileNotFound, "unable to open natvis file \"%S\"", natvis_file_path);
continue;
}
+1
View File
@@ -59,6 +59,7 @@ typedef struct
LNK_RRT *rrt;
CV_DebugH *debug_h;
} LNK_TypeServer;
typedef struct LNK_TypeServerNode { LNK_TypeServer v; struct LNK_TypeServerNode *next; } LNK_TypeServerNode;
typedef struct LNK_TypeServerList { U64 count; LNK_TypeServerNode *first, *last; } LNK_TypeServerList;
typedef struct LNK_TypeServerArray { U64 count; LNK_TypeServer *v; } LNK_TypeServerArray;
+29 -14
View File
@@ -171,11 +171,11 @@ lnk_log_read(String8 path, U64 size)
}
internal String8
lnk_read_data_from_file_path(Arena *arena, LNK_IO_Flags io_flags, String8 path)
lnk_read_data_from_file_path(Arena *arena, LNK_IO_Flags io_flags, String8 path, B8 *was_read_out)
{
Temp scratch = scratch_begin(&arena, 1);
TP_Context *single_thread_ctx = tp_alloc(scratch.arena, 1, 1, str8_zero());
String8Array data_arr = lnk_read_data_from_file_path_parallel(single_thread_ctx, arena, io_flags, (String8Array){ .count = 1, .v = &path });
String8Array data_arr = lnk_read_data_from_file_path_parallel(single_thread_ctx, arena, io_flags, (String8Array){ .count = 1, .v = &path }, was_read_out);
scratch_end(scratch);
return data_arr.v[0];
}
@@ -201,16 +201,20 @@ THREAD_POOL_TASK_FUNC(lnk_data_size_from_file_path_task)
internal
THREAD_POOL_TASK_FUNC(lnk_data_from_file_path_task)
{
LNK_DiskReader *task = raw_task;
LNK_DiskReader *task = raw_task;
File handle = task->handle_arr[task_id];
if ( ! MemoryIsZeroStruct(&handle)) {
U64 buffer_size = task->size_arr[task_id];
U8 *buffer = task->buffer + task->off_arr[task_id];
U64 read_size = lnk_read_file(&handle, buffer, buffer_size);
AssertAlways(read_size == buffer_size); // TODO: handle partial loads as invalid
File handle = task->handle_arr[task_id];
U64 buffer_size = task->size_arr[task_id];
U8 *buffer = task->buffer + task->off_arr[task_id];
task->data_arr.v[task_id] = str8(buffer, read_size);
U64 read_size = lnk_read_file(&handle, buffer, buffer_size);
Assert(read_size == buffer_size);
task->data_arr.v[task_id] = str8(buffer, read_size);
if (task->was_read) {
task->was_read[task_id] = 1;
}
}
}
internal
@@ -237,6 +241,9 @@ THREAD_POOL_TASK_FUNC(lnk_memory_map_file_task)
void *file_data = MapViewOfFile(mapping_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, file_size.QuadPart);
if (file_data) {
task->data_arr.v[task_id] = str8(file_data, file_size.QuadPart);
if (task->was_read) {
task->was_read[task_id] = 1;
}
}
CloseHandle(mapping_handle);
@@ -254,6 +261,9 @@ THREAD_POOL_TASK_FUNC(lnk_memory_map_file_task)
void *file_data = MapViewOfFile(mapping_handle, FILE_MAP_COPY, 0, 0, file_size.QuadPart);
if (file_data) {
task->data_arr.v[task_id] = str8(file_data, file_size.QuadPart);
if (task->was_read) {
task->was_read[task_id] = 1;
}
}
CloseHandle(mapping_handle);
}
@@ -268,6 +278,9 @@ THREAD_POOL_TASK_FUNC(lnk_memory_map_file_task)
void *file_data = mmap(0, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (file_data != MAP_FAILED) {
task->data_arr.v[task_id] = str8(file_data, st.st_size);
if (task->was_read) {
task->was_read[task_id] = 1;
}
}
}
close(fd);
@@ -279,7 +292,7 @@ THREAD_POOL_TASK_FUNC(lnk_memory_map_file_task)
}
internal String8Array
lnk_read_data_from_file_path_parallel(TP_Context *tp, Arena *arena, LNK_IO_Flags io_flags, String8Array path_arr)
lnk_read_data_from_file_path_parallel(TP_Context *tp, Arena *arena, LNK_IO_Flags io_flags, String8Array path_arr, B8 *was_read)
{
ProfBeginFunction();
LNK_DiskReader reader = {0};
@@ -289,13 +302,15 @@ lnk_read_data_from_file_path_parallel(TP_Context *tp, Arena *arena, LNK_IO_Flags
reader.path_arr = path_arr;
reader.data_arr.count = path_arr.count;
reader.data_arr.v = push_array(arena, String8, path_arr.count);
reader.was_read = was_read;
tp_for_parallel(tp, 0, path_arr.count, lnk_memory_map_file_task, &reader);
} else {
Temp scratch = scratch_begin(&arena,1);
reader.path_arr = path_arr;
reader.handle_arr = push_array_no_zero(scratch.arena, File, path_arr.count);
reader.size_arr = push_array_no_zero(scratch.arena, U64, path_arr.count);
reader.path_arr = path_arr;
reader.handle_arr = push_array_no_zero(scratch.arena, File, path_arr.count);
reader.size_arr = push_array_no_zero(scratch.arena, U64, path_arr.count);
reader.was_read = was_read;
// open handles and get sizes
tp_for_parallel(tp, 0, path_arr.count, lnk_data_size_from_file_path_task, &reader);
+3 -2
View File
@@ -16,6 +16,7 @@ typedef struct
U64 *size_arr;
U64 *off_arr;
U8 *buffer;
B8 *was_read;
} LNK_DiskReader;
typedef struct
@@ -50,8 +51,8 @@ internal File lnk_file_open_with_rename_permissions(String8 path);
internal B32 lnk_file_set_delete_on_close(File handle, B32 delete_file);
internal B32 lnk_file_rename(File handle, String8 new_name);
internal String8 lnk_read_data_from_file_path(Arena *arena, LNK_IO_Flags io_flags, String8 path);
internal String8Array lnk_read_data_from_file_path_parallel(TP_Context *tp, Arena *arena, LNK_IO_Flags io_flags, String8Array path_arr);
internal String8 lnk_read_data_from_file_path(Arena *arena, LNK_IO_Flags io_flags, String8 path, B8 *was_read_out);
internal String8Array lnk_read_data_from_file_path_parallel(TP_Context *tp, Arena *arena, LNK_IO_Flags io_flags, String8Array path_arr, B8 *was_read);
internal void lnk_write_data_list_to_file_path(String8 path, String8 temp_path, String8List list);
internal void lnk_write_data_to_file_path(String8 path, String8 temp_path, String8 data);