-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable library linking. close #13
- Loading branch information
Showing
17 changed files
with
1,919 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
/** @file LinkerObject.cpp | ||
* @author Christian R <c@ethdev.com> | ||
* @date 2015 | ||
*/ | ||
|
||
#include <libevmasm/LinkerObject.h> | ||
#include <libsolutil/CommonData.h> | ||
#include <libsolutil/Keccak256.h> | ||
|
||
using namespace std; | ||
using namespace solidity; | ||
using namespace solidity::util; | ||
using namespace solidity::evmasm; | ||
|
||
void LinkerObject::append(LinkerObject const& _other) | ||
{ | ||
for (auto const& ref: _other.linkReferences) | ||
linkReferences[ref.first + bytecode.size()] = ref.second; | ||
bytecode += _other.bytecode; | ||
} | ||
|
||
// Solidity++: 168-bit address | ||
void LinkerObject::link(map<string, h168> const& _libraryAddresses) | ||
{ | ||
std::map<size_t, std::string> remainingRefs; | ||
for (auto const& linkRef: linkReferences) | ||
if (h168 const* address = matchLibrary(linkRef.second, _libraryAddresses)) | ||
copy(address->data(), address->data() + 20, bytecode.begin() + vector<uint8_t>::difference_type(linkRef.first)); | ||
else | ||
remainingRefs.insert(linkRef); | ||
linkReferences.swap(remainingRefs); | ||
} | ||
|
||
string LinkerObject::toHex() const | ||
{ | ||
string hex = solidity::util::toHex(bytecode); | ||
for (auto const& ref: linkReferences) | ||
{ | ||
size_t pos = ref.first * 2; | ||
string hash = libraryPlaceholder(ref.second); | ||
hex[pos] = hex[pos + 1] = hex[pos + 38] = hex[pos + 39] = '_'; | ||
// Solidity++: Vite use 168-bit address, placeholder is end with '____' | ||
hex[pos + 40] = hex[pos + 41] = '_'; | ||
|
||
for (size_t i = 0; i < 36; ++i) | ||
hex[pos + 2 + i] = hash.at(i); | ||
} | ||
return hex; | ||
} | ||
|
||
string LinkerObject::libraryPlaceholder(string const& _libraryName) | ||
{ | ||
return "$" + keccak256(_libraryName).hex().substr(0, 34) + "$"; | ||
} | ||
|
||
// Solidity++: 168-bit address | ||
h168 const* | ||
LinkerObject::matchLibrary( | ||
string const& _linkRefName, | ||
map<string, h168> const& _libraryAddresses | ||
) | ||
{ | ||
auto it = _libraryAddresses.find(_linkRefName); | ||
if (it != _libraryAddresses.end()) | ||
return &it->second; | ||
// If the user did not supply a fully qualified library name, | ||
// try to match only the simple library name | ||
size_t colon = _linkRefName.find(':'); | ||
if (colon == string::npos) | ||
return nullptr; | ||
it = _libraryAddresses.find(_linkRefName.substr(colon + 1)); | ||
if (it != _libraryAddresses.end()) | ||
return &it->second; | ||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
/** @file Assembly.h | ||
* @author Gav Wood <i@gavwood.com> | ||
* @date 2014 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <libsolutil/Common.h> | ||
#include <libsolutil/FixedHash.h> | ||
|
||
namespace solidity::evmasm | ||
{ | ||
|
||
/** | ||
* Binary object that potentially still needs to be linked (i.e. addresses of other contracts | ||
* need to be filled in). | ||
*/ | ||
struct LinkerObject | ||
{ | ||
/// The bytecode. | ||
bytes bytecode; | ||
|
||
/// Map from offsets in bytecode to library identifiers. The addresses starting at those offsets | ||
/// need to be replaced by the actual addresses by the linker. | ||
std::map<size_t, std::string> linkReferences; | ||
|
||
/// Map from hashes of the identifiers of immutable variables to the full identifier of the immutable and | ||
/// to a list of offsets into the bytecode that refer to their values. | ||
std::map<u256, std::pair<std::string, std::vector<size_t>>> immutableReferences; | ||
|
||
/// Appends the bytecode of @a _other and incorporates its link references. | ||
void append(LinkerObject const& _other); | ||
|
||
/// Links the given libraries by replacing their uses in the code and removes them from the references. | ||
void link(std::map<std::string, util::h168> const& _libraryAddresses); // Solidity++: 168-bit address | ||
|
||
/// @returns a hex representation of the bytecode of the given object, replacing unlinked | ||
/// addresses by placeholders. This output is lowercase. | ||
std::string toHex() const; | ||
|
||
/// @returns a 36 character string that is used as a placeholder for the library | ||
/// address (enclosed by `__` on both sides). The placeholder is the hex representation | ||
/// of the first 18 bytes of the keccak-256 hash of @a _libraryName. | ||
static std::string libraryPlaceholder(std::string const& _libraryName); | ||
|
||
private: | ||
// Solidity++: 168-bit address | ||
static util::h168 const* matchLibrary( | ||
std::string const& _linkRefName, | ||
std::map<std::string, util::h168> const& _libraryAddresses | ||
); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.