From a787e705948255e1ff2e1b70c6d8c5160e892fa3 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 2 Sep 2016 01:16:03 +0200 Subject: Fix linking for libraries with underscores. --- solc/CommandLineInterface.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'solc') diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index fbef56f0..48e7578f 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -778,37 +778,38 @@ void CommandLineInterface::actOnInput() bool CommandLineInterface::link() { + map librariesReplacements; + for (auto const& library: m_libraries) + { + string const& name = library.first; + string replacement = "__"; + for (size_t i = 0; i < 36; ++i) + replacement.push_back(i < name.size() ? name[i] : '_'); + replacement += "__"; + librariesReplacements[replacement] = library.second; + } for (auto& src: m_sourceCodes) { auto end = src.second.end(); for (auto it = src.second.begin(); it != end;) { while (it != end && *it != '_') ++it; - auto insertStart = it; - while (it != end && *it == '_') ++it; - auto nameStart = it; - while (it != end && *it != '_') ++it; - auto nameEnd = it; - while (it != end && *it == '_') ++it; - auto insertEnd = it; - - if (insertStart == end) - break; - - if (insertEnd - insertStart != 40) + if (it == end) break; + if (end - it < 40) { - cerr << "Error in binary object file " << src.first << " at position " << (insertStart - src.second.begin()) << endl; + cerr << "Error in binary object file " << src.first << " at position " << (end - src.second.begin()) << endl; return false; } - string name(nameStart, nameEnd); - if (m_libraries.count(name)) + string name(it, it + 40); + if (librariesReplacements.count(name)) { - string hexStr(toHex(m_libraries.at(name).asBytes())); - copy(hexStr.begin(), hexStr.end(), insertStart); + string hexStr(toHex(librariesReplacements.at(name).asBytes())); + copy(hexStr.begin(), hexStr.end(), it); } else cerr << "Reference \"" << name << "\" in file \"" << src.first << "\" still unresolved." << endl; + it += 40; } } return true; -- cgit v1.2.3 From f869f25b842f4c800621197f5c1c3521732da5c6 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 6 Sep 2016 11:12:55 +0200 Subject: More comments about size constants. --- solc/CommandLineInterface.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'solc') diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 48e7578f..d87429b5 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -778,12 +778,17 @@ void CommandLineInterface::actOnInput() bool CommandLineInterface::link() { + // Map from how the libraries will be named inside the bytecode to their addresses. map librariesReplacements; + size_t const placeholderSize = 40; // 20 bytes or 40 hex characters for (auto const& library: m_libraries) { string const& name = library.first; + // Library placeholders are 40 hex digits (20 bytes) that start and end with '__'. + // This leaves 36 characters for the library name, while too short library names are + // padded on the right with '_' and too long names are truncated. string replacement = "__"; - for (size_t i = 0; i < 36; ++i) + for (size_t i = 0; i < placeholderSize - 4; ++i) replacement.push_back(i < name.size() ? name[i] : '_'); replacement += "__"; librariesReplacements[replacement] = library.second; @@ -795,13 +800,13 @@ bool CommandLineInterface::link() { while (it != end && *it != '_') ++it; if (it == end) break; - if (end - it < 40) + if (end - it < placeholderSize) { cerr << "Error in binary object file " << src.first << " at position " << (end - src.second.begin()) << endl; return false; } - string name(it, it + 40); + string name(it, it + placeholderSize); if (librariesReplacements.count(name)) { string hexStr(toHex(librariesReplacements.at(name).asBytes())); @@ -809,7 +814,7 @@ bool CommandLineInterface::link() } else cerr << "Reference \"" << name << "\" in file \"" << src.first << "\" still unresolved." << endl; - it += 40; + it += placeholderSize; } } return true; -- cgit v1.2.3 From 8c315a18c9ed7ad8b1fdec53d21e425137e89079 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 6 Sep 2016 11:57:21 +0200 Subject: Fix compiler error. --- solc/CommandLineInterface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'solc') diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index d87429b5..f0a34632 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -780,7 +780,7 @@ bool CommandLineInterface::link() { // Map from how the libraries will be named inside the bytecode to their addresses. map librariesReplacements; - size_t const placeholderSize = 40; // 20 bytes or 40 hex characters + int const placeholderSize = 40; // 20 bytes or 40 hex characters for (auto const& library: m_libraries) { string const& name = library.first; -- cgit v1.2.3