From 447c561aaa8cc9cbe5fec16923e5f3237e11cc64 Mon Sep 17 00:00:00 2001 From: Sylvie <35663410+Rangi42@users.noreply.github.com> Date: Sun, 3 Mar 2024 21:16:36 -0500 Subject: [PATCH] Use `std::variant` for symbol values (#1330) --- include/link/symbol.hpp | 20 +++++++++------- src/link/object.cpp | 38 ++++++++++++++++++------------ src/link/output.cpp | 8 +++++-- src/link/patch.cpp | 16 ++++++++----- src/link/sdas_obj.cpp | 52 ++++++++++++++++++++++++++++++----------- src/link/symbol.cpp | 3 +++ 6 files changed, 92 insertions(+), 45 deletions(-) diff --git a/include/link/symbol.hpp b/include/link/symbol.hpp index 20f4fab2..b625f228 100644 --- a/include/link/symbol.hpp +++ b/include/link/symbol.hpp @@ -8,12 +8,20 @@ #include #include +#include #include "linkdefs.hpp" struct FileStackNode; struct Section; +struct Label { + int32_t sectionID; + int32_t offset; + // Extra info computed during linking + Section *section; +}; + struct Symbol { // Info contained in the object files std::string name; @@ -21,14 +29,10 @@ struct Symbol { char const *objFileName; FileStackNode const *src; int32_t lineNo; - int32_t sectionID; - union { - // Both types must be identical - int32_t offset; - int32_t value; - }; - // Extra info computed during linking - Section *section; + std::variant< + int32_t, // Constants just have a numeric value + Label // Label values refer to an offset within a specific section + > data; }; void sym_AddSymbol(Symbol &symbol); diff --git a/src/link/object.cpp b/src/link/object.cpp index 864b85ac..c24f55da 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -178,18 +178,28 @@ static void readSymbol(FILE *file, Symbol &symbol, char const *fileName, if (symbol.type != SYMTYPE_IMPORT) { symbol.objFileName = fileName; uint32_t nodeID; - tryReadlong(nodeID, file, "%s: Cannot read \"%s\"'s node ID: %s", fileName, symbol.name.c_str()); symbol.src = &fileNodes[nodeID]; tryReadlong(symbol.lineNo, file, "%s: Cannot read \"%s\"'s line number: %s", fileName, symbol.name.c_str()); - tryReadlong(symbol.sectionID, file, "%s: Cannot read \"%s\"'s section ID: %s", + int32_t sectionID, value; + tryReadlong(sectionID, file, "%s: Cannot read \"%s\"'s section ID: %s", fileName, symbol.name.c_str()); - tryReadlong(symbol.offset, file, "%s: Cannot read \"%s\"'s value: %s", + tryReadlong(value, file, "%s: Cannot read \"%s\"'s value: %s", fileName, symbol.name.c_str()); + if (sectionID == -1) { + symbol.data = value; + } else { + symbol.data = Label{ + .sectionID = sectionID, + .offset = value, + // Set the `.section` later based on the `.sectionID` + .section = nullptr, + }; + } } else { - symbol.sectionID = -1; + symbol.data = -1; } } @@ -330,11 +340,13 @@ static void readSection(FILE *file, Section §ion, char const *fileName, static void linkSymToSect(Symbol &symbol, Section §ion) { uint32_t a = 0, b = section.symbols.size(); + int32_t symbolOffset = std::get