clean up pass over directives handler; preparing to collapse code paths

This commit is contained in:
Nikita Smith
2025-07-28 18:40:51 -07:00
parent 0b904f4e24
commit 18f83521e4
5 changed files with 60 additions and 74 deletions
+16 -25
View File
@@ -1488,18 +1488,15 @@ lnk_build_link_context(TP_Context *tp, TP_Arena *tp_arena, LNK_Config *config)
ProfBegin("Handle Directives");
for EachIndex(obj_idx, obj_node_arr.count) {
LNK_Obj *obj = &obj_node_arr.v[obj_idx].data;
String8List raw_directives = lnk_raw_directives_from_obj(scratch.arena, obj);
LNK_DirectiveInfo directive_info = lnk_directive_info_from_raw_directives(scratch.arena, obj, raw_directives);
LNK_Obj *obj = &obj_node_arr.v[obj_idx].data;
String8List raw_directives = lnk_raw_directives_from_obj(scratch.arena, obj);
LNK_DirectiveInfo directive_info = lnk_directive_info_from_raw_directives(scratch.arena, obj, raw_directives);
// /EXPORT
{
for (LNK_Directive *dir = directive_info.v[LNK_CmdSwitch_Export].first; dir != 0; dir = dir->next) {
PE_ExportParse export_parse = {0};
lnk_parse_export_directive_ex(scratch.arena, dir->value_list, obj->path, lnk_obj_get_lib_path(obj), &export_parse);
lnk_push_export(tp_arena->v[0], export_ht, &export_symbol_list, &include_symbol_list, export_parse);
}
for (LNK_Directive *dir = directive_info.v[LNK_CmdSwitch_Export].first; dir != 0; dir = dir->next) {
PE_ExportParse export_parse = {0};
lnk_parse_export_directive_ex(scratch.arena, dir->value_list, obj->path, lnk_obj_get_lib_path(obj), &export_parse);
lnk_push_export(tp_arena->v[0], export_ht, &export_symbol_list, &include_symbol_list, export_parse);
}
// /INCLUDESYMBOL
@@ -1511,13 +1508,11 @@ lnk_build_link_context(TP_Context *tp, TP_Arena *tp_arena, LNK_Config *config)
for (LNK_Directive *dir = directive_info.v[LNK_CmdSwitch_Merge].first; dir != 0; dir = dir->next) {
for (String8Node *value_n = dir->value_list.first; value_n != 0; value_n = value_n->next) {
LNK_MergeDirective merge_dir;
if (lnk_parse_merge_directive(value_n->string, &merge_dir)) {
if (lnk_parse_merge_directive(value_n->string, obj->path, lnk_obj_get_lib_path(obj), &merge_dir)) {
merge_dir.src = push_str8_copy(tp_arena->v[0], merge_dir.src);
merge_dir.dst = push_str8_copy(tp_arena->v[0], merge_dir.dst);
lnk_merge_directive_list_push(tp_arena->v[0], &config->merge_list, merge_dir);
} else {
lnk_error_obj(LNK_Warning_IllData, obj, "can't parse merge directive \"%S\"", value_n->string);
}
}
}
}
@@ -1537,17 +1532,13 @@ lnk_build_link_context(TP_Context *tp, TP_Arena *tp_arena, LNK_Config *config)
}
// /ALTERNATENAME
{
for (LNK_Directive *dir = directive_info.v[LNK_CmdSwitch_AlternateName].first; dir != 0; dir = dir->next) {
for (String8Node *string_n = dir->value_list.first; string_n != 0; string_n = string_n->next) {
LNK_AltName alt_name;
if (lnk_parse_alt_name_directive(string_n->string, &alt_name)) {
alt_name.from = push_str8_copy(tp_arena->v[0], alt_name.from);
alt_name.to = push_str8_copy(tp_arena->v[0], alt_name.to);
lnk_symbol_table_push_alt_name(symtab, obj, alt_name.from, alt_name.to);
} else {
lnk_error_obj(LNK_Error_Cmdl, obj, "syntax error in \"%S\", expected format \"FROM=TO\"", string_n->string);
}
for (LNK_Directive *dir = directive_info.v[LNK_CmdSwitch_AlternateName].first; dir != 0; dir = dir->next) {
for (String8Node *string_n = dir->value_list.first; string_n != 0; string_n = string_n->next) {
LNK_AltName alt_name;
if (lnk_parse_alt_name_directive(string_n->string, obj->path, lnk_obj_get_lib_path(obj), &alt_name)) {
alt_name.from = push_str8_copy(tp_arena->v[0], alt_name.from);
alt_name.to = push_str8_copy(tp_arena->v[0], alt_name.to);
lnk_symbol_table_push_alt_name(symtab, obj, alt_name.from, alt_name.to);
}
}
}
-4
View File
@@ -129,8 +129,6 @@ lnk_cmd_line_push_option_if_not_present(Arena *arena, LNK_CmdLine *cmd_line, cha
internal LNK_CmdLine
lnk_cmd_line_parse_windows_rules(Arena *arena, String8List arg_list)
{
Temp scratch = scratch_begin(&arena, 1);
LNK_CmdLine cmd_line = {0};
cmd_line.raw_cmd_line = str8_list_copy(arena, &arg_list);
@@ -157,8 +155,6 @@ lnk_cmd_line_parse_windows_rules(Arena *arena, String8List arg_list)
str8_list_push(arena, &cmd_line.input_list, arg);
}
}
scratch_end(scratch);
return cmd_line;
}
+12 -13
View File
@@ -601,15 +601,16 @@ lnk_cmd_switch_parse_string_copy(Arena *arena, String8 obj_path, String8 lib_pat
}
internal B32
lnk_parse_alt_name_directive(String8 input, LNK_AltName *alt_out)
lnk_parse_alt_name_directive(String8 string, String8 obj_path, String8 lib_path, LNK_AltName *alt_out)
{
Temp scratch = scratch_begin(0,0);
B32 is_parse_ok = 0;
String8List pair = str8_split_by_string_chars(scratch.arena, input, str8_lit("="), 0);
String8List pair = str8_split_by_string_chars(scratch.arena, string, str8_lit("="), 0);
if (pair.node_count == 2) {
alt_out->from = pair.first->string;
alt_out->to = pair.last->string;
is_parse_ok = 1;
alt_out->to = pair.last->string;
} else {
lnk_error_cmd_switch(LNK_Error_Cmdl, obj_path, lib_path, LNK_CmdSwitch_AlternateName, "syntax error in \"%S\", expected format \"FROM=TO\"", string);
}
scratch_end(scratch);
return is_parse_ok;
@@ -745,18 +746,18 @@ lnk_merge_directive_list_push(Arena *arena, LNK_MergeDirectiveList *list, LNK_Me
}
internal B32
lnk_parse_merge_directive(String8 string, LNK_MergeDirective *out)
lnk_parse_merge_directive(String8 string, String8 obj_path, String8 lib_path, LNK_MergeDirective *out)
{
Temp scratch = scratch_begin(0, 0);
B32 is_parse_ok = 0;
String8List list = str8_split_by_string_chars(scratch.arena, string, str8_lit("="), 0);
if (list.node_count == 2) {
out->src = list.first->string;
out->dst = list.last->string;
is_parse_ok = 1;
} else {
lnk_error_cmd_switch(LNK_Warning_InvalidMergeDirectiveFormat, obj_path, lib_path, LNK_CmdSwitch_Merge, "unable to parse merge directive, expected format \"/MERGE:FROM=TO\" but got \"%S\"", string);
}
scratch_end(scratch);
return is_parse_ok;
}
@@ -1094,15 +1095,13 @@ lnk_apply_cmd_option_to_config(Arena *arena, LNK_Config *config, String8 cmd_nam
case LNK_CmdSwitch_AlternateName: {
if (value_strings.node_count == 1) {
LNK_AltName alt_name;
if (lnk_parse_alt_name_directive(value_strings.first->string, &alt_name)) {
if (lnk_parse_alt_name_directive(value_strings.first->string, obj_path, lib_path, &alt_name)) {
alt_name.from = push_str8_copy(arena, alt_name.from);
alt_name.to = push_str8_copy(arena, alt_name.to);
LNK_AltNameNode *alt_name_n = push_array(arena, LNK_AltNameNode, 1);
alt_name_n->data = alt_name;
SLLQueuePush(config->alt_name_list.first, config->alt_name_list.last, alt_name_n);
config->alt_name_list.count += 1;
} else {
lnk_error_cmd_switch(LNK_Error_Cmdl, obj_path, lib_path, cmd_switch, "syntax error in \"%S\", expected format \"FROM=TO\"", value_strings.first->string);
}
} else {
lnk_error_cmd_switch(LNK_Error_Cmdl, obj_path, lib_path, cmd_switch, "invalid number of parameters");
@@ -1463,10 +1462,10 @@ lnk_apply_cmd_option_to_config(Arena *arena, LNK_Config *config, String8 cmd_nam
case LNK_CmdSwitch_Merge: {
if (value_strings.node_count == 1) {
LNK_MergeDirective merge = {0};
if (lnk_parse_merge_directive(push_str8_copy(arena, value_strings.first->string), &merge)) {
if (lnk_parse_merge_directive(value_strings.first->string, obj_path, lib_path, &merge)) {
merge.src = push_str8_copy(arena, merge.src);
merge.dst = push_str8_copy(arena, merge.dst);
lnk_merge_directive_list_push(arena, &config->merge_list, merge);
} else {
lnk_error_cmd_switch(LNK_Warning_InvalidMergeDirectiveFormat, obj_path, lib_path, cmd_switch, "unable to parse merge directive, expected format \"/MERGE:FROM=TO\" but got \"%S\"", value_strings.first->string);
}
} else {
lnk_error_cmd_switch(LNK_Error_Cmdl, obj_path, lib_path, cmd_switch, "invalid number of parameters %d", value_strings.node_count);
+2 -2
View File
@@ -556,11 +556,11 @@ internal void lnk_cmd_switch_set_flag_64 (String8 obj_path, String8 lib_path,
internal B32 lnk_cmd_switch_parse_string (String8 obj_path, String8 lib_path, LNK_CmdSwitchType cmd_switch, String8List value_strings, String8 *string_out);
internal void lnk_cmd_switch_parse_string_copy(Arena *arena, String8 obj_path, String8 lib_path, LNK_CmdSwitchType cmd_switch, String8List value_strings, String8 *string_out);
internal B32 lnk_parse_alt_name_directive(String8 input, LNK_AltName *alt_out);
internal B32 lnk_parse_alt_name_directive(String8 string, String8 obj_path, String8 lib_path, LNK_AltName *alt_out);
internal B32 lnk_parse_export_directive_ex(Arena *arena, String8List directive, String8 obj_path, String8 lib_path, PE_ExportParse *export_out);
internal B32 lnk_parse_export_directive(Arena *arena, String8 directive, String8 obj_path, String8 lib_path, PE_ExportParse *export_out);
internal LNK_MergeDirectiveNode * lnk_merge_directive_list_push(Arena *arena, LNK_MergeDirectiveList *list, LNK_MergeDirective data);
internal B32 lnk_parse_merge_directive(String8 string, LNK_MergeDirective *parse_out);
internal B32 lnk_parse_merge_directive(String8 string, String8 obj_path, String8 lib_path, LNK_MergeDirective *parse_out);
// --- Getters -----------------------------------------------------------------
+30 -30
View File
@@ -630,36 +630,6 @@ lnk_obj_is_before(void *raw_a, void *raw_b)
return a->input_idx < b->input_idx;
}
internal String8List
lnk_raw_directives_from_obj(Arena *arena, LNK_Obj *obj)
{
COFF_SectionHeader *section_table = lnk_coff_section_table_from_obj(obj);
String8List drectve_data = {0};
for (U64 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) {
COFF_SectionHeader *sect_header = &section_table[sect_idx];
if (sect_header->flags & COFF_SectionFlag_LnkInfo) {
String8 sect_name = str8_cstring_capped(sect_header->name, sect_header->name + sizeof(sect_header->name));
if (str8_match(sect_name, str8_lit(".drectve"), 0)) {
if (sect_header->flags & COFF_SectionFlag_CntUninitializedData) {
lnk_error_obj(LNK_Error_IllData, obj, ".drectve section header has flag COFF_SectionFlag_CntUninitializedData");
break;
}
if (sect_header->fsize < 3) {
lnk_error_obj(LNK_Error_IllData, obj, "not enough bytes to parse .drectve");
break;
}
if (sect_header->reloc_count > 0) {
lnk_error_obj(LNK_Error_IllData, obj, ".drectve must not have relocations");
break;
}
Rng1U64 sect_range = rng_1u64(sect_header->foff, sect_header->foff + sect_header->fsize);
str8_list_push(arena, &drectve_data, str8_substr(obj->data, sect_range));
}
}
}
return drectve_data;
}
internal void
lnk_parse_msvc_linker_directive(Arena *arena, LNK_Obj *obj, LNK_DirectiveInfo *directive_info, String8 buffer)
{
@@ -707,6 +677,36 @@ lnk_parse_msvc_linker_directive(Arena *arena, LNK_Obj *obj, LNK_DirectiveInfo *d
scratch_end(scratch);
}
internal String8List
lnk_raw_directives_from_obj(Arena *arena, LNK_Obj *obj)
{
COFF_SectionHeader *section_table = lnk_coff_section_table_from_obj(obj);
String8List drectve_data = {0};
for (U64 sect_idx = 0; sect_idx < obj->header.section_count_no_null; sect_idx += 1) {
COFF_SectionHeader *sect_header = &section_table[sect_idx];
if (sect_header->flags & COFF_SectionFlag_LnkInfo) {
String8 sect_name = str8_cstring_capped(sect_header->name, sect_header->name + sizeof(sect_header->name));
if (str8_match(sect_name, str8_lit(".drectve"), 0)) {
if (sect_header->flags & COFF_SectionFlag_CntUninitializedData) {
lnk_error_obj(LNK_Error_IllData, obj, ".drectve section header has flag COFF_SectionFlag_CntUninitializedData");
break;
}
if (sect_header->fsize < 3) {
lnk_error_obj(LNK_Error_IllData, obj, "not enough bytes to parse .drectve");
break;
}
if (sect_header->reloc_count > 0) {
lnk_error_obj(LNK_Error_IllData, obj, ".drectve must not have relocations");
break;
}
Rng1U64 sect_range = rng_1u64(sect_header->foff, sect_header->foff + sect_header->fsize);
str8_list_push(arena, &drectve_data, str8_substr(obj->data, sect_range));
}
}
}
return drectve_data;
}
internal LNK_DirectiveInfo
lnk_directive_info_from_raw_directives(Arena *arena, LNK_Obj *obj, String8List raw_directives)
{