2015-09-10 10:02:18 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-09-10 10:02:18 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-09-10 10:02:18 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-09-10 10:02:18 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-09-10 10:02:18 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-09-10 10:02:18 +00:00
|
|
|
/** @file LinkerObject.cpp
|
|
|
|
* @author Christian R <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libevmasm/LinkerObject.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
#include <libsolutil/Keccak256.h>
|
2015-09-10 10:02:18 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::evmasm;
|
2015-09-10 10:02:18 +00:00
|
|
|
|
|
|
|
void LinkerObject::append(LinkerObject const& _other)
|
|
|
|
{
|
|
|
|
for (auto const& ref: _other.linkReferences)
|
|
|
|
linkReferences[ref.first + bytecode.size()] = ref.second;
|
|
|
|
bytecode += _other.bytecode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerObject::link(map<string, h160> const& _libraryAddresses)
|
|
|
|
{
|
|
|
|
std::map<size_t, std::string> remainingRefs;
|
|
|
|
for (auto const& linkRef: linkReferences)
|
2017-01-18 16:41:36 +00:00
|
|
|
if (h160 const* address = matchLibrary(linkRef.second, _libraryAddresses))
|
2020-06-02 13:10:14 +00:00
|
|
|
copy(address->data(), address->data() + 20, bytecode.begin() + vector<uint8_t>::difference_type(linkRef.first));
|
2015-09-10 10:02:18 +00:00
|
|
|
else
|
2017-01-18 16:41:36 +00:00
|
|
|
remainingRefs.insert(linkRef);
|
2015-09-10 10:02:18 +00:00
|
|
|
linkReferences.swap(remainingRefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
string LinkerObject::toHex() const
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
string hex = solidity::util::toHex(bytecode);
|
2015-09-10 10:02:18 +00:00
|
|
|
for (auto const& ref: linkReferences)
|
|
|
|
{
|
|
|
|
size_t pos = ref.first * 2;
|
2018-10-04 12:55:02 +00:00
|
|
|
string hash = libraryPlaceholder(ref.second);
|
2015-09-10 10:02:18 +00:00
|
|
|
hex[pos] = hex[pos + 1] = hex[pos + 38] = hex[pos + 39] = '_';
|
|
|
|
for (size_t i = 0; i < 36; ++i)
|
2018-10-04 12:55:02 +00:00
|
|
|
hex[pos + 2 + i] = hash.at(i);
|
2015-09-10 10:02:18 +00:00
|
|
|
}
|
|
|
|
return hex;
|
|
|
|
}
|
2017-01-18 16:41:36 +00:00
|
|
|
|
2018-10-04 12:55:02 +00:00
|
|
|
string LinkerObject::libraryPlaceholder(string const& _libraryName)
|
|
|
|
{
|
2018-10-08 13:08:12 +00:00
|
|
|
return "$" + keccak256(_libraryName).hex().substr(0, 34) + "$";
|
2018-10-04 12:55:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:41:36 +00:00
|
|
|
h160 const*
|
|
|
|
LinkerObject::matchLibrary(
|
|
|
|
string const& _linkRefName,
|
|
|
|
map<string, h160> const& _libraryAddresses
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto it = _libraryAddresses.find(_linkRefName);
|
|
|
|
if (it != _libraryAddresses.end())
|
|
|
|
return &it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-12-14 14:52:05 +00:00
|
|
|
|
|
|
|
bool LinkerObject::operator<(LinkerObject const& _other) const
|
|
|
|
{
|
|
|
|
return tie(this->bytecode, this->linkReferences, this->immutableReferences) <
|
|
|
|
tie(_other.bytecode, _other.linkReferences, _other.immutableReferences);
|
|
|
|
}
|