From 4324eec89ef088467ea90b9339a8b3c0177ef943 Mon Sep 17 00:00:00 2001 From: Seeky Date: Tue, 22 Jun 2021 17:08:07 +0100 Subject: [PATCH 01/12] elf2rel: Add support for linking against other rels Symbols for other rel modules can now be added to lst files with the syntax addr:symbolName?moduleId,sectionId --- ttyd-tools/elf2rel/elf2rel.cpp | 45 +++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 86871eb6..7d319a4c 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -13,9 +13,16 @@ #include #include -std::map loadSymbolMap(const std::string &filename) +struct SymbolLocation { - std::map outputMap; + uint32_t moduleId; // 0 means dol + uint32_t targetSection; // OSLink ignores for dol + uint32_t addr; +}; + +std::map loadSymbolMap(const std::string &filename) +{ + std::map outputMap; std::ifstream inputStream(filename); for (std::string line; std::getline(inputStream, line); ) @@ -28,14 +35,35 @@ std::map loadSymbolMap(const std::string &filename) continue; } - size_t index = line.find_first_of(':'); + // dol symbols: addr:symbolName + // rel symbols: addr:symbolName?moduleId,sectionId + size_t index = line.find_first_of(':'); // addr-name separator + size_t index2 = line.find_first_of('?', index); // name-module separator + size_t index3 = line.find_first_of(',', index2); // module-section separator + bool dol = (index2 == std::string::npos) || (index3 == std::string::npos); - std::string name = line.substr(index + 1); + std::string name; + if (dol) + name = line.substr(index + 1); + else + name = line.substr(index + 1, index2 - (index + 1)); boost::trim_left(name); uint32_t addr = strtoul(line.substr(0, index).c_str(), nullptr, 16); - outputMap[name] = addr; + uint32_t moduleId; + if (dol) + moduleId = 0; + else + moduleId = strtoul(line.substr(index2 + 1, index3).c_str(), nullptr, 10); + + uint32_t sectionId; + if (dol) + sectionId = 0; + else + sectionId = strtoul(line.substr(index3 + 1).c_str(), nullptr, 10); + + outputMap[name] = { moduleId, sectionId, addr }; } return outputMap; @@ -382,10 +410,9 @@ int main(int argc, char **argv) { // Known external! resolved = true; - - rel.moduleID = 0; - rel.targetSection = 0; // #todo-elf2rel: Check if this is important - rel.addend = static_cast(addend + it->second); + rel.moduleID = it->second.moduleId; + rel.targetSection = it->second.targetSection; + rel.addend = static_cast(addend + it->second.addr); } } From 713fd45010c9947bf8d5317e660b96efe1a99773 Mon Sep 17 00:00:00 2001 From: Seeky Date: Tue, 22 Jun 2021 17:12:43 +0100 Subject: [PATCH 02/12] Minor comment correction --- ttyd-tools/elf2rel/elf2rel.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 7d319a4c..7934ef7d 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -36,7 +36,7 @@ std::map loadSymbolMap(const std::string &filename) } // dol symbols: addr:symbolName - // rel symbols: addr:symbolName?moduleId,sectionId + // rel symbols: offset:symbolName?moduleId,sectionId size_t index = line.find_first_of(':'); // addr-name separator size_t index2 = line.find_first_of('?', index); // name-module separator size_t index3 = line.find_first_of(',', index2); // module-section separator @@ -584,4 +584,4 @@ int main(int argc, char **argv) outputStream.write(reinterpret_cast(outputBuffer.data()), outputBuffer.size()); return 0; -} \ No newline at end of file +} From a29650f829c20f50b7b7d598098f05ae0754ef01 Mon Sep 17 00:00:00 2001 From: Seeky Date: Wed, 23 Jun 2021 12:13:38 +0100 Subject: [PATCH 03/12] Correct moduleId substr length --- ttyd-tools/elf2rel/elf2rel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 7934ef7d..36b659f7 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -55,7 +55,7 @@ std::map loadSymbolMap(const std::string &filename) if (dol) moduleId = 0; else - moduleId = strtoul(line.substr(index2 + 1, index3).c_str(), nullptr, 10); + moduleId = strtoul(line.substr(index2 + 1, index3 - (index2 + 1)).c_str(), nullptr, 10); uint32_t sectionId; if (dol) From a2fee9498f10a880817dc3ed3572938b03311553 Mon Sep 17 00:00:00 2001 From: Seeky Date: Wed, 23 Jun 2021 12:46:32 +0100 Subject: [PATCH 04/12] elf2rel: hex support for section and module ids Requested by Zephiles --- ttyd-tools/elf2rel/elf2rel.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 36b659f7..efd779ac 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -53,15 +53,31 @@ std::map loadSymbolMap(const std::string &filename) uint32_t moduleId; if (dol) + { moduleId = 0; + } else - moduleId = strtoul(line.substr(index2 + 1, index3 - (index2 + 1)).c_str(), nullptr, 10); + { + std::string moduleIdStr = line.substr(index2 + 1, index3 - (index2 + 1)); + if (moduleIdStr.substr(0, 2) == "0x") + moduleId = strtoul(moduleIdStr.substr(2).c_str(), nullptr, 16); + else + moduleId = strtoul(moduleIdStr.c_str(), nullptr, 10); + } uint32_t sectionId; if (dol) + { sectionId = 0; + } else - sectionId = strtoul(line.substr(index3 + 1).c_str(), nullptr, 10); + { + std::string sectionIdStr = line.substr(index3 + 1); + if (sectionIdStr.substr(0, 2) == "0x") + sectionId = strtoul(sectionIdStr.substr(2).c_str(), nullptr, 16); + else + sectionId = strtoul(sectionIdStr.c_str(), nullptr, 10); + } outputMap[name] = { moduleId, sectionId, addr }; } From 4a548059eca3558273a0fe44b881e250a5984048 Mon Sep 17 00:00:00 2001 From: Seeky Date: Fri, 27 Aug 2021 17:29:24 +0100 Subject: [PATCH 05/12] elf2rel: new module & section syntax --- ttyd-tools/elf2rel/elf2rel.cpp | 53 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index efd779ac..c7f2b5af 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -36,47 +36,44 @@ std::map loadSymbolMap(const std::string &filename) } // dol symbols: addr:symbolName - // rel symbols: offset:symbolName?moduleId,sectionId - size_t index = line.find_first_of(':'); // addr-name separator - size_t index2 = line.find_first_of('?', index); // name-module separator - size_t index3 = line.find_first_of(',', index2); // module-section separator - bool dol = (index2 == std::string::npos) || (index3 == std::string::npos); - + // rel symbols: module,section,offset:symbolName + + uint32_t moduleId, sectionId, addr; std::string name; - if (dol) - name = line.substr(index + 1); - else - name = line.substr(index + 1, index2 - (index + 1)); - boost::trim_left(name); - uint32_t addr = strtoul(line.substr(0, index).c_str(), nullptr, 16); + // Find name after colon separator, always present + size_t colonIndex = line.find_first_of(':'); + name = line.substr(colonIndex + 1); + boost::trim_left(name); - uint32_t moduleId; - if (dol) + // Handle comma separators, if present + // Commas can be included in mangled symbol names, so this has to be limited to before the name + std::string locInfo = line.substr(0, colonIndex); + size_t commaIndex1 = line.find_first_of(','); + if (commaIndex1 == std::string::npos) { moduleId = 0; + sectionId = 0; + addr = std::stoul(locInfo, nullptr, 16); } else { - std::string moduleIdStr = line.substr(index2 + 1, index3 - (index2 + 1)); + size_t commaIndex2 = line.find_first_of(',', commaIndex1 + 1); + + std::string moduleIdStr = locInfo.substr(0, commaIndex1); if (moduleIdStr.substr(0, 2) == "0x") - moduleId = strtoul(moduleIdStr.substr(2).c_str(), nullptr, 16); + moduleId = std::stoul(moduleIdStr.substr(2), nullptr, 16); else - moduleId = strtoul(moduleIdStr.c_str(), nullptr, 10); - } + moduleId = std::stoul(moduleIdStr, nullptr, 10); - uint32_t sectionId; - if (dol) - { - sectionId = 0; - } - else - { - std::string sectionIdStr = line.substr(index3 + 1); + std::string sectionIdStr = locInfo.substr(commaIndex1 + 1, commaIndex2 - commaIndex1); if (sectionIdStr.substr(0, 2) == "0x") - sectionId = strtoul(sectionIdStr.substr(2).c_str(), nullptr, 16); + sectionId = std::stoul(sectionIdStr.substr(2), nullptr, 16); else - sectionId = strtoul(sectionIdStr.c_str(), nullptr, 10); + sectionId = std::stoul(sectionIdStr, nullptr, 10); + + std::string addrStr = locInfo.substr(commaIndex2 + 1); + addr = std::stoul(addrStr, nullptr, 16); } outputMap[name] = { moduleId, sectionId, addr }; From 7aa9526f294b4904dad38e5717364cf1c1764eb7 Mon Sep 17 00:00:00 2001 From: Seeky Date: Sat, 28 Aug 2021 11:01:41 +0100 Subject: [PATCH 06/12] elf2rel: correct fixed linking --- ttyd-tools/elf2rel/elf2rel.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index c7f2b5af..71658ddb 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -441,10 +441,25 @@ int main(int argc, char **argv) } } + // Returns whether a module should be placed at the end of relocations for trimming + auto getModuleDelay = [moduleID](uint32_t id) + { + if (id == 0 || id == moduleID) + return 1; + else + return 0; + }; + // Sort relocations std::sort(allRelocations.begin(), allRelocations.end(), - [](const Relocation &left, const Relocation &right) + [&](const Relocation &left, const Relocation &right) { + // Relocations against the dol & this module need to be placed last for trimming with OSLinkFixed + int delayLeft = getModuleDelay(left.moduleID); + int delayRight = getModuleDelay(right.moduleID); + if (delayLeft != delayRight) + return delayLeft < delayRight; + return std::tuple(left.moduleID, left.section, left.offset) < std::tuple(right.moduleID, right.section, right.offset); }); @@ -482,6 +497,7 @@ int main(int argc, char **argv) int currentModuleID = -1; int currentSectionIndex = -1; int currentOffset = 0; + int fixedRelocationsSize = 0; while (!allRelocations.empty()) { Relocation nextRel = allRelocations.front(); @@ -520,6 +536,11 @@ int main(int argc, char **argv) writeRelocation(outputBuffer, 0, R_DOLPHIN_END, 0, 0); } + // If the next module ID was forced to the back and the current one wasn't, + // then this is the end of the relocations included in the fixed size + if (getModuleDelay(nextRel.moduleID) > getModuleDelay(currentModuleID)) + fixedRelocationsSize = outputBuffer.size() - relocationOffset; + currentModuleID = nextRel.moduleID; currentSectionIndex = -1; writeImportInfo(importInfoBuffer, currentModuleID, outputBuffer.size()); @@ -570,6 +591,11 @@ int main(int argc, char **argv) } writeRelocation(outputBuffer, 0, R_DOLPHIN_END, 0, 0); + // If the final module referenced isn't forced to the back, then all + // relocations must be included in the fixed size + if (getModuleDelay(currentModuleID) == 0) + fixedRelocationsSize = outputBuffer.size() - relocationOffset; + // Write final import infos int importInfoSize = importInfoBuffer.size(); std::copy(importInfoBuffer.begin(), importInfoBuffer.end(), outputBuffer.begin() + importInfoOffset); @@ -589,7 +615,7 @@ int main(int argc, char **argv) prologOffset, epilogOffset, unresolvedOffset, maxAlign, maxBssAlign, - relocationOffset); + relocationOffset + fixedRelocationsSize); std::copy(headerBuffer.begin(), headerBuffer.end(), outputBuffer.begin()); // Write final REL file From 3ffa409923e51a866cbb7251f1bb9528d738fd56 Mon Sep 17 00:00:00 2001 From: Seeky Date: Sat, 28 Aug 2021 11:06:53 +0100 Subject: [PATCH 07/12] elf2rel: match current bracket style --- ttyd-tools/elf2rel/elf2rel.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 71658ddb..0dbf46e8 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -62,15 +62,23 @@ std::map loadSymbolMap(const std::string &filename) std::string moduleIdStr = locInfo.substr(0, commaIndex1); if (moduleIdStr.substr(0, 2) == "0x") + { moduleId = std::stoul(moduleIdStr.substr(2), nullptr, 16); + } else + { moduleId = std::stoul(moduleIdStr, nullptr, 10); + } std::string sectionIdStr = locInfo.substr(commaIndex1 + 1, commaIndex2 - commaIndex1); if (sectionIdStr.substr(0, 2) == "0x") + { sectionId = std::stoul(sectionIdStr.substr(2), nullptr, 16); + } else + { sectionId = std::stoul(sectionIdStr, nullptr, 10); + } std::string addrStr = locInfo.substr(commaIndex2 + 1); addr = std::stoul(addrStr, nullptr, 16); @@ -445,9 +453,13 @@ int main(int argc, char **argv) auto getModuleDelay = [moduleID](uint32_t id) { if (id == 0 || id == moduleID) + { return 1; + } else + { return 0; + } }; // Sort relocations @@ -458,7 +470,9 @@ int main(int argc, char **argv) int delayLeft = getModuleDelay(left.moduleID); int delayRight = getModuleDelay(right.moduleID); if (delayLeft != delayRight) + { return delayLeft < delayRight; + } return std::tuple(left.moduleID, left.section, left.offset) < std::tuple(right.moduleID, right.section, right.offset); @@ -539,7 +553,9 @@ int main(int argc, char **argv) // If the next module ID was forced to the back and the current one wasn't, // then this is the end of the relocations included in the fixed size if (getModuleDelay(nextRel.moduleID) > getModuleDelay(currentModuleID)) + { fixedRelocationsSize = outputBuffer.size() - relocationOffset; + } currentModuleID = nextRel.moduleID; currentSectionIndex = -1; @@ -594,7 +610,9 @@ int main(int argc, char **argv) // If the final module referenced isn't forced to the back, then all // relocations must be included in the fixed size if (getModuleDelay(currentModuleID) == 0) + { fixedRelocationsSize = outputBuffer.size() - relocationOffset; + } // Write final import infos int importInfoSize = importInfoBuffer.size(); From fc899c9b0607a30132b807ecf0aba59a7f1691f5 Mon Sep 17 00:00:00 2001 From: Seeky <58006653+SeekyCt@users.noreply.github.com> Date: Wed, 16 Feb 2022 16:43:26 +0000 Subject: [PATCH 08/12] elf2rel: Fix comma search --- ttyd-tools/elf2rel/elf2rel.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 0dbf46e8..f901a337 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -49,7 +49,7 @@ std::map loadSymbolMap(const std::string &filename) // Handle comma separators, if present // Commas can be included in mangled symbol names, so this has to be limited to before the name std::string locInfo = line.substr(0, colonIndex); - size_t commaIndex1 = line.find_first_of(','); + size_t commaIndex1 = locInfo.find_first_of(','); if (commaIndex1 == std::string::npos) { moduleId = 0; @@ -58,7 +58,7 @@ std::map loadSymbolMap(const std::string &filename) } else { - size_t commaIndex2 = line.find_first_of(',', commaIndex1 + 1); + size_t commaIndex2 = locInfo.find_first_of(',', commaIndex1 + 1); std::string moduleIdStr = locInfo.substr(0, commaIndex1); if (moduleIdStr.substr(0, 2) == "0x") From 85cc9e5fdaffb71be2adeed0dc15e6fe7b7b5dc3 Mon Sep 17 00:00:00 2001 From: ComplexPlane Date: Thu, 17 Feb 2022 10:49:38 -0500 Subject: [PATCH 09/12] Regex symbol parsing --- ttyd-tools/elf2rel/elf2rel.cpp | 58 ++++++++-------------------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index f901a337..94860bb1 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -12,6 +12,11 @@ #include #include #include +#include + +// dol symbols: addr:symbolName +// rel symbols: module,section,offset:symbolName +static std::regex SYMBOL_RE("(([a-fA-F0-9]{1,8}),([a-fA-F0-9]{1,8}),)?([a-fA-F0-9]{1,8}):(.+)"); struct SymbolLocation { @@ -35,54 +40,17 @@ std::map loadSymbolMap(const std::string &filename) continue; } - // dol symbols: addr:symbolName - // rel symbols: module,section,offset:symbolName - - uint32_t moduleId, sectionId, addr; - std::string name; - - // Find name after colon separator, always present - size_t colonIndex = line.find_first_of(':'); - name = line.substr(colonIndex + 1); - boost::trim_left(name); - - // Handle comma separators, if present - // Commas can be included in mangled symbol names, so this has to be limited to before the name - std::string locInfo = line.substr(0, colonIndex); - size_t commaIndex1 = locInfo.find_first_of(','); - if (commaIndex1 == std::string::npos) + std::smatch match; + if (!std::regex_match(line, match, SYMBOL_RE)) { - moduleId = 0; - sectionId = 0; - addr = std::stoul(locInfo, nullptr, 16); + std::cerr << "Invalid symbol: " << line << std::endl; + continue; } - else - { - size_t commaIndex2 = locInfo.find_first_of(',', commaIndex1 + 1); - - std::string moduleIdStr = locInfo.substr(0, commaIndex1); - if (moduleIdStr.substr(0, 2) == "0x") - { - moduleId = std::stoul(moduleIdStr.substr(2), nullptr, 16); - } - else - { - moduleId = std::stoul(moduleIdStr, nullptr, 10); - } - std::string sectionIdStr = locInfo.substr(commaIndex1 + 1, commaIndex2 - commaIndex1); - if (sectionIdStr.substr(0, 2) == "0x") - { - sectionId = std::stoul(sectionIdStr.substr(2), nullptr, 16); - } - else - { - sectionId = std::stoul(sectionIdStr, nullptr, 10); - } - - std::string addrStr = locInfo.substr(commaIndex2 + 1); - addr = std::stoul(addrStr, nullptr, 16); - } + uint32_t moduleId = match[1].matched ? std::stoul(match[2], nullptr, 16) : 0; + uint32_t sectionId = match[1].matched ? std::stoul(match[3], nullptr, 16) : 0; + uint32_t addr = std::stoul(match[4], nullptr, 16); + std::string name = match[5]; outputMap[name] = { moduleId, sectionId, addr }; } From 771d403f15948cb8385605738711cc9cb546ae26 Mon Sep 17 00:00:00 2001 From: Seeky <58006653+SeekyCt@users.noreply.github.com> Date: Sun, 12 Jun 2022 17:00:28 +0100 Subject: [PATCH 10/12] Add missing include for linux --- ttyd-tools/elf2rel/elf2rel.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ttyd-tools/elf2rel/elf2rel.h b/ttyd-tools/elf2rel/elf2rel.h index 3c8ce096..367ab0a1 100644 --- a/ttyd-tools/elf2rel/elf2rel.h +++ b/ttyd-tools/elf2rel/elf2rel.h @@ -4,6 +4,7 @@ #pragma once #include +#include enum RelRelocationType { From 56cac020d274d085948e7871cf1f30c29d607eec Mon Sep 17 00:00:00 2001 From: Seeky <58006653+SeekyCt@users.noreply.github.com> Date: Sun, 12 Jun 2022 17:17:24 +0100 Subject: [PATCH 11/12] Redo parsing --- ttyd-tools/elf2rel/elf2rel.cpp | 73 +++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 94860bb1..9cf571b3 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -12,11 +12,6 @@ #include #include #include -#include - -// dol symbols: addr:symbolName -// rel symbols: module,section,offset:symbolName -static std::regex SYMBOL_RE("(([a-fA-F0-9]{1,8}),([a-fA-F0-9]{1,8}),)?([a-fA-F0-9]{1,8}):(.+)"); struct SymbolLocation { @@ -25,6 +20,60 @@ struct SymbolLocation uint32_t addr; }; +void trimAll(std::vector &strs) +{ + for (std::string &str : strs) + { + boost::trim(str); + } +} + +bool parseInt(const std::string &str, uint32_t &out, int base=0) +{ + size_t len; + out = std::stoul(str, &len, base); + return len == str.length(); +} + +// dol symbols: addr:symbolName +// rel symbols: module,section,offset:symbolName +// module and section can be prefixed with 0x for hex or 0 for octal, addr/offset is always hex +bool parseSymbol(const std::string &line, SymbolLocation &sym, std::string &name) +{ + // Split around colon + std::vector colonParts; + boost::split(colonParts, line, boost::is_any_of(":")); + if (colonParts.size() != 2) + { + return false; + } + trimAll(colonParts); + name = colonParts[1]; + + // Split first part around commas + std::vector commaParts; + boost::split(commaParts, colonParts[0], boost::is_any_of(",")); + if (commaParts.size() == 1) + { + // Dol + sym.moduleId = 0; + sym.targetSection = 0; + return parseInt(commaParts[0], sym.addr, 16); + } + else if (commaParts.size() == 3) + { + // Other rel + trimAll(commaParts); + return parseInt(commaParts[0], sym.moduleId) + && parseInt(commaParts[1], sym.targetSection) + && parseInt(commaParts[2], sym.addr, 16); + } + else + { + return false; + } +} + std::map loadSymbolMap(const std::string &filename) { std::map outputMap; @@ -40,19 +89,15 @@ std::map loadSymbolMap(const std::string &filename) continue; } - std::smatch match; - if (!std::regex_match(line, match, SYMBOL_RE)) + // Try parse line + SymbolLocation sym; + std::string name; + if (!parseSymbol(line, sym, name)) { std::cerr << "Invalid symbol: " << line << std::endl; continue; } - - uint32_t moduleId = match[1].matched ? std::stoul(match[2], nullptr, 16) : 0; - uint32_t sectionId = match[1].matched ? std::stoul(match[3], nullptr, 16) : 0; - uint32_t addr = std::stoul(match[4], nullptr, 16); - std::string name = match[5]; - - outputMap[name] = { moduleId, sectionId, addr }; + outputMap[name] = sym; } return outputMap; From d32b68b298a22346feb0264d0dcdd3b1e61b49d8 Mon Sep 17 00:00:00 2001 From: Seeky <58006653+SeekyCt@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:42:59 +0100 Subject: [PATCH 12/12] Catch parseInt invalid_argument --- ttyd-tools/elf2rel/elf2rel.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ttyd-tools/elf2rel/elf2rel.cpp b/ttyd-tools/elf2rel/elf2rel.cpp index 9cf571b3..ea984cf3 100644 --- a/ttyd-tools/elf2rel/elf2rel.cpp +++ b/ttyd-tools/elf2rel/elf2rel.cpp @@ -30,9 +30,15 @@ void trimAll(std::vector &strs) bool parseInt(const std::string &str, uint32_t &out, int base=0) { - size_t len; - out = std::stoul(str, &len, base); - return len == str.length(); + try { + size_t len; + out = std::stoul(str, &len, base); + return len == str.length(); + } + catch (std::invalid_argument) + { + return false; + } } // dol symbols: addr:symbolName