From a2740be4c00d1c2084d88cdf9cb29825be7d4492 Mon Sep 17 00:00:00 2001 From: Pavel Kopyl Date: Mon, 16 Nov 2026 12:21:40 +0100 Subject: [PATCH 1/2] [EVM] Exclude lint checks the linker code contradicts Also write the system git config for darker. --- .github/workflows/regression-tests.yml | 7 ++++++- llvm/.clang-tidy | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/regression-tests.yml b/.github/workflows/regression-tests.yml index 1ffa76954d88..5c1a0d499c2f 100644 --- a/.github/workflows/regression-tests.yml +++ b/.github/workflows/regression-tests.yml @@ -131,8 +131,13 @@ jobs: fetch-depth: ${{ needs.prepare-test-matrix.outputs.fetch_depth }} repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }} + # The global config lands in this step's $HOME. darker spawns git with + # its own idea of the home directory and misses it, so also write the + # HOME-independent system config. - name: Mark everything as safe for Git - run: git config --global --add safe.directory '*' + run: | + git config --global --add safe.directory '*' + git config --system --add safe.directory '*' - name: Define previous commit id: previous-commit diff --git a/llvm/.clang-tidy b/llvm/.clang-tidy index 617d2d1b1b1b..a13c7196fea9 100644 --- a/llvm/.clang-tidy +++ b/llvm/.clang-tidy @@ -4,9 +4,11 @@ Checks: > bugprone-*, -bugprone-easily-swappable-parameters, -bugprone-implicit-widening-of-multiplication-result, + -bugprone-multi-level-implicit-pointer-conversion, -bugprone-narrowing-conversions, -bugprone-unchecked-optional-access, cert-*, + -cert-int09-c, clang-analyzer-*, -clang-analyzer-optin.cplusplus.UninitializedObject, concurrency-*, @@ -17,13 +19,17 @@ Checks: > -cppcoreguidelines-avoid-non-const-global-variables, -cppcoreguidelines-macro-usage, -cppcoreguidelines-narrowing-conversions, + -cppcoreguidelines-no-malloc, -cppcoreguidelines-owning-memory, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, + -cppcoreguidelines-pro-bounds-avoid-unchecked-container-access, -cppcoreguidelines-pro-bounds-constant-array-index, -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-type-const-cast, + -cppcoreguidelines-pro-type-reinterpret-cast, -cppcoreguidelines-pro-type-static-cast-downcast, -cppcoreguidelines-pro-type-union-access, + -cppcoreguidelines-use-enum-class, hicpp-*, -hicpp-braces-around-statements, -hicpp-named-parameter, @@ -47,10 +53,12 @@ Checks: > modernize-use-override, modernize-use-using, performance-*, + -performance-enum-size, portability-*, readability-*, -readability-braces-around-statements, -readability-convert-member-functions-to-static, + -readability-enum-initial-value, -readability-function-cognitive-complexity, -readability-identifier-length, -readability-identifier-naming, From 4c072b2ae85cc82b013dbcbd501f9f4e127ca291 Mon Sep 17 00:00:00 2001 From: Pavel Kopyl Date: Tue, 3 Nov 2026 01:18:02 +0100 Subject: [PATCH 2/2] [EVM] Add a custom linker and drop lld dependency Assembling and linking an EVM contract requires very little: each input is a relocatable ELF32 big-endian object with a single .text section, and the output is their concatenation with all relocations applied. The new LLVMEVMLinker component library does exactly that. The C API has changed: - LLVMAssembleEVM and LLVMLinkEVM return the output buffer and report no errors. Every failure is treated as an internal error. - LLVMAssembleEVM takes the metadata bytes and appends them after the code. - LLVMAssembleEVM no longer takes the code segment kind. The object carries it: the 'evm.runtime' module flag lowers to the new EF_EVM_RUNTIME bit in e_flags and the linker propagates it. - LLVMDisposeUndefinedReferences is now LLVMDisposeUndefinedReferencesEVM. A library address is now represented by a single undefined '__linker_symbol' with one 20-byte R_EVM_DATA_160 relocation per use, rather than five 32-bit sub-symbols. The linker tests have been moved to llvm/unittests/Target/EVM. --- lld/CMakeLists.txt | 5 - lld/Common/DriverDispatcher.cpp | 76 -- lld/ELF/Arch/EVM.cpp | 94 -- lld/ELF/CMakeLists.txt | 1 - lld/ELF/Config.h | 8 - lld/ELF/Driver.cpp | 65 +- lld/ELF/InputFiles.cpp | 36 +- lld/ELF/InputSection.cpp | 39 - lld/ELF/Options.td | 2 - lld/ELF/Target.cpp | 4 - lld/ELF/Target.h | 3 - lld/ELF/Writer.cpp | 27 - lld/include/lld-c/LLDAsLibraryC.h | 142 --- lld/include/lld/Common/Driver.h | 40 - lld/lld-c/CMakeLists.txt | 19 - lld/lld-c/LLDAsLibraryC.cpp | 869 ------------------ lld/test/lit.cfg.py | 8 - lld/unittests/CMakeLists.txt | 3 - lld/unittests/EVM/CMakeLists.txt | 17 - lld/unittests/EVM/LLDTest.cpp | 357 ------- llvm/cmake/modules/LLVM-Build.cmake | 13 +- llvm/include/llvm-c/EVMLinker.h | 137 +++ llvm/include/llvm-c/ObjCopy.h | 31 - llvm/include/llvm/BinaryFormat/ELF.h | 7 + .../llvm/BinaryFormat/ELFRelocs/EVM.def | 1 + llvm/include/llvm/Support/FileOutputBuffer.h | 11 - llvm/lib/ObjCopy/CMakeLists.txt | 1 - llvm/lib/ObjCopy/ObjCopyC/CMakeLists.txt | 14 - llvm/lib/ObjCopy/ObjCopyC/ObjCopyC.cpp | 96 -- llvm/lib/Support/FileOutputBuffer.cpp | 50 - llvm/lib/Target/EVM/CMakeLists.txt | 1 + llvm/lib/Target/EVM/EVMAsmPrinter.cpp | 45 +- llvm/lib/Target/EVM/Linker/CMakeLists.txt | 13 + llvm/lib/Target/EVM/Linker/EVMLinker.cpp | 677 ++++++++++++++ .../Target/EVM/Linker/EVMLinkerQueries.cpp | 215 +++++ llvm/lib/Target/EVM/Linker/EVMLinkerUtils.h | 41 + .../Target/EVM/MCTargetDesc/EVMAsmBackend.cpp | 31 +- .../EVM/MCTargetDesc/EVMELFObjectWriter.cpp | 2 + .../Target/EVM/MCTargetDesc/EVMFixupKinds.h | 3 + .../EVM/MCTargetDesc/EVMMCCodeEmitter.cpp | 4 +- .../lib/Target/EVM/MCTargetDesc/EVMMCExpr.cpp | 1 - .../EVM/MCTargetDesc/EVMMCTargetDesc.cpp | 18 +- .../Target/EVM/MCTargetDesc/EVMMCTargetDesc.h | 2 - .../EVM/MCTargetDesc/EVMTargetStreamer.cpp | 53 -- .../EVM/MCTargetDesc/EVMTargetStreamer.h | 10 - llvm/test/MC/EVM/data-linker-symbol-relocs.ll | 12 +- llvm/unittests/Target/EVM/CMakeLists.txt | 13 + llvm/unittests/Target/EVM/EVMLinkerTest.cpp | 530 +++++++++++ .../unittests/Target}/EVM/Inputs/A_deploy.ll | 0 .../Target}/EVM/Inputs/A_deployed.ll | 2 + .../unittests/Target}/EVM/Inputs/D_deploy.ll | 0 .../Target}/EVM/Inputs/D_deployed.ll | 2 + .../unittests/Target}/EVM/Inputs/R_deploy.ll | 0 .../Target}/EVM/Inputs/R_deployed.ll | 2 + .../unittests/Target}/EVM/Inputs/deployIr.ll | 0 .../Target}/EVM/Inputs/deployedIr.ll | 2 + .../Target}/EVM/Inputs/undefDeployIr.ll | 0 .../Target}/EVM/Inputs/undefDeployedIr.ll | 2 + .../gn/build/write_library_dependencies.py | 14 +- 59 files changed, 1718 insertions(+), 2153 deletions(-) delete mode 100644 lld/ELF/Arch/EVM.cpp delete mode 100644 lld/include/lld-c/LLDAsLibraryC.h delete mode 100644 lld/lld-c/CMakeLists.txt delete mode 100644 lld/lld-c/LLDAsLibraryC.cpp delete mode 100644 lld/unittests/EVM/CMakeLists.txt delete mode 100644 lld/unittests/EVM/LLDTest.cpp create mode 100644 llvm/include/llvm-c/EVMLinker.h delete mode 100644 llvm/include/llvm-c/ObjCopy.h delete mode 100644 llvm/lib/ObjCopy/ObjCopyC/CMakeLists.txt delete mode 100644 llvm/lib/ObjCopy/ObjCopyC/ObjCopyC.cpp create mode 100644 llvm/lib/Target/EVM/Linker/CMakeLists.txt create mode 100644 llvm/lib/Target/EVM/Linker/EVMLinker.cpp create mode 100644 llvm/lib/Target/EVM/Linker/EVMLinkerQueries.cpp create mode 100644 llvm/lib/Target/EVM/Linker/EVMLinkerUtils.h create mode 100644 llvm/unittests/Target/EVM/EVMLinkerTest.cpp rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/A_deploy.ll (100%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/A_deployed.ll (85%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/D_deploy.ll (100%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/D_deployed.ll (78%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/R_deploy.ll (100%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/R_deployed.ll (92%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/deployIr.ll (100%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/deployedIr.ll (85%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/undefDeployIr.ll (100%) rename {lld/unittests => llvm/unittests/Target}/EVM/Inputs/undefDeployedIr.ll (85%) diff --git a/lld/CMakeLists.txt b/lld/CMakeLists.txt index 084943a7ada9..80e25204a65e 100644 --- a/lld/CMakeLists.txt +++ b/lld/CMakeLists.txt @@ -187,8 +187,6 @@ include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include ) - -add_subdirectory(lld-c) add_subdirectory(Common) add_subdirectory(tools/lld) @@ -212,9 +210,6 @@ add_custom_target(lld-headers) set_target_properties(lld-headers PROPERTIES FOLDER "Misc") if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) install(DIRECTORY include/lld - # EVM local begin - include/lld-c - # EVM local end DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT lld-headers FILES_MATCHING diff --git a/lld/Common/DriverDispatcher.cpp b/lld/Common/DriverDispatcher.cpp index 61db42c8466c..34f0ed24b3df 100644 --- a/lld/Common/DriverDispatcher.cpp +++ b/lld/Common/DriverDispatcher.cpp @@ -149,22 +149,6 @@ static Driver whichDriver(llvm::SmallVectorImpl &argsV, return it->d; } -// EVM local begin -static DriverMemBuf whichDriver(llvm::SmallVectorImpl &argsV, - llvm::ArrayRef drivers) { - Flavor f = parseFlavor(argsV); - const auto *const it = - llvm::find_if(drivers, [=](auto &driverdef) { return driverdef.f == f; }); - if (it == drivers.end()) { - // Driver is invalid or not available in this build. - return [](llvm::ArrayRef, llvm::raw_pwrite_stream *, - llvm::ArrayRef, llvm::raw_ostream &, - llvm::raw_ostream &, bool, bool) { return false; }; - } - return it->d; -} -// EVM local end - namespace lld { bool inTestOutputDisabled = false; @@ -189,35 +173,6 @@ int unsafeLldMain(llvm::ArrayRef args, return r; } - -// EVM local begin -/// Universal linker main(). This linker emulates the gnu, darwin, or -/// windows linker based on the argv[0] or -flavor option. -int unsafeLldMainMemBuf(llvm::ArrayRef inBuffers, - llvm::raw_pwrite_stream *outBuffer, - llvm::ArrayRef args, - llvm::raw_ostream &stdoutOS, - llvm::raw_ostream &stderrOS, - llvm::ArrayRef drivers, - bool exitEarly) { - SmallVector argsV(make_range(args.begin(), args.end())); - DriverMemBuf d = whichDriver(argsV, drivers); - // Run the driver. If an error occurs, false will be returned. - const int r = !d(inBuffers, outBuffer, argsV, stdoutOS, stderrOS, exitEarly, - inTestOutputDisabled); - // At this point 'r' is either 1 for error, and 0 for no error. - - // Call exit() if we can to avoid calling destructors. - if (exitEarly) - exitLld(r); - - // Delete the global context and clear the global context pointer, so that it - // cannot be accessed anymore. - CommonLinkerContext::destroy(); - - return r; -} -// EVM local end } // namespace lld Result lld::lldMain(llvm::ArrayRef args, @@ -245,34 +200,3 @@ Result lld::lldMain(llvm::ArrayRef args, } return {r, /*canRunAgain=*/true}; } - -// EVM local begin -Result lld::lldMainMemBuf(llvm::ArrayRef inBuffers, - llvm::raw_pwrite_stream *outBuffer, - llvm::ArrayRef args, - llvm::raw_ostream &stdoutOS, - llvm::raw_ostream &stderrOS, - llvm::ArrayRef drivers) { - int r = 0; - { - // The crash recovery is here only to be able to recover from arbitrary - // control flow when fatal() is called (through setjmp/longjmp or - // __try/__except). - llvm::CrashRecoveryContext crc; - if (!crc.RunSafely([&]() { - r = unsafeLldMainMemBuf(inBuffers, outBuffer, args, stdoutOS, - stderrOS, drivers, /*exitEarly=*/false); - })) - return {crc.RetCode, /*canRunAgain=*/false}; - } - - // Cleanup memory and reset everything back in pristine condition. This path - // is only taken when LLD is in test, or when it is used as a library. - llvm::CrashRecoveryContext crc; - if (!crc.RunSafely([]() { CommonLinkerContext::destroy(); })) { - // The memory is corrupted beyond any possible recovery. - return {r, /*canRunAgain=*/false}; - } - return {r, /*canRunAgain=*/true}; -} -// EVM local end diff --git a/lld/ELF/Arch/EVM.cpp b/lld/ELF/Arch/EVM.cpp deleted file mode 100644 index e087120a1233..000000000000 --- a/lld/ELF/Arch/EVM.cpp +++ /dev/null @@ -1,94 +0,0 @@ -//===- EVM.cpp ------------------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// EVM is a stack-based virtual machine with a word size of 256 bits intendent -// for execution of smart contracts in Ethereum blockchain environment. -// -// Since it is baremetal programming, there's usually no loader to load -// ELF files on EVMs. You are expected to link your program against address -// 0 and pull out a .text section from the result using objcopy, so that you -// can write the linked code to on-chip flush memory. You can do that with -// the following commands: -// -// ld.lld -Ttext=0 -o foo foo.o -// objcopy -O binary --only-section=.text foo output.bin -// -//===----------------------------------------------------------------------===// - -#include "InputFiles.h" -#include "Symbols.h" -#include "Target.h" -#include "lld/Common/ErrorHandler.h" -#include "llvm/BinaryFormat/ELF.h" -#include "llvm/Support/Endian.h" - -using namespace llvm; -using namespace llvm::object; -using namespace llvm::support::endian; -using namespace llvm::ELF; -using namespace lld; -using namespace lld::elf; - -namespace { -class EVM final : public TargetInfo { -public: - EVM(Ctx &); - uint32_t calcEFlags() const override; - RelExpr getRelExpr(RelType type, const Symbol &s, - const uint8_t *loc) const override; - void relocate(uint8_t *loc, const Relocation &rel, - uint64_t val) const override; -}; -} // namespace - -EVM::EVM(Ctx &ctx) : TargetInfo(ctx) {} - -RelExpr EVM::getRelExpr(RelType type, const Symbol &s, - const uint8_t *loc) const { - switch (type) { - case R_EVM_DATA: - return R_ABS; - case R_EVM_NONE: - return R_NONE; - default: - Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation (" << type.v - << ") against symbol " << &s; - return R_NONE; - } -} - -void EVM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { - switch (rel.type) { - case R_EVM_DATA: { - write32be(loc, static_cast(val)); - break; - } - default: - llvm_unreachable("unknown relocation"); - } -} - -void elf::setEVMTargetInfo(Ctx &ctx) { ctx.target.reset(new EVM(ctx)); } - -static uint32_t getEFlags(InputFile *file) { - return cast>(file)->getObj().getHeader().e_flags; -} - -uint32_t EVM::calcEFlags() const { - assert(!ctx.objectFiles.empty()); - - const uint32_t flags = getEFlags(ctx.objectFiles[0]); - if (auto it = std::find_if_not( - ctx.objectFiles.begin(), ctx.objectFiles.end(), - [flags](InputFile *f) { return flags == getEFlags(f); }); - it != ctx.objectFiles.end()) - Err(ctx) << *it - << ": cannot link object files with incompatible target ISA"; - - return flags; -} diff --git a/lld/ELF/CMakeLists.txt b/lld/ELF/CMakeLists.txt index 1821709a6a83..ec3f6382282b 100644 --- a/lld/ELF/CMakeLists.txt +++ b/lld/ELF/CMakeLists.txt @@ -24,7 +24,6 @@ add_lld_library(lldELF Arch/AMDGPU.cpp Arch/ARM.cpp Arch/AVR.cpp - Arch/EVM.cpp Arch/Hexagon.cpp Arch/LoongArch.cpp Arch/Mips.cpp diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 7567ef9f2b33..d9639b06ca4b 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -220,11 +220,6 @@ struct Config { llvm::CachePruningPolicy thinLTOCachePolicy; llvm::SetVector dependencyFiles; // for --dependency-file llvm::StringMap sectionStartMap; - // EVM local begin - llvm::ArrayRef inBuffers; - llvm::raw_pwrite_stream *outBuffer; - bool useIOMemoryBuffers = false; - // EVM local end llvm::StringRef bfdname; llvm::StringRef chroot; llvm::StringRef dependencyFile; @@ -372,9 +367,6 @@ struct Config { bool relaxGP; bool relocatable; bool resolveGroups; - // EVM local begin - bool evmAssembly = false; - // EVM local end bool relrGlibc = false; bool relrPackDynRelocs = false; llvm::DenseSet saveTempsArgs; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index f0a5226308df..21d228eda647 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -51,9 +51,6 @@ #include "llvm/Config/llvm-config.h" #include "llvm/LTO/LTO.h" #include "llvm/Object/Archive.h" -// EVM local begin -#include "llvm/Object/ELF.h" -// EVM local end #include "llvm/Object/IRObjectFile.h" #include "llvm/Remarks/HotnessThresholdParser.h" #include "llvm/Support/CommandLine.h" @@ -144,40 +141,6 @@ bool link(ArrayRef args, llvm::raw_ostream &stdoutOS, return errCount(ctx) == 0; } - -// EVM local begin -bool linkMemBuf(ArrayRef inBuffers, - raw_pwrite_stream *outBuffer, ArrayRef args, - llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, - bool exitEarly, bool disableOutput) { - // This driver-specific context will be freed later by unsafeLldMainMemBuf(). - auto *context = new Ctx; - Ctx &ctx = *context; - - context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); - context->e.logName = args::getFilenameWithoutExe(args[0]); - context->e.errorLimitExceededMsg = - "too many errors emitted, stopping now (use " - "--error-limit=0 to see all errors)"; - - LinkerScript script(ctx); - ctx.script = &script; - ctx.symAux.emplace_back(); - ctx.symtab = std::make_unique(ctx); - - ctx.partitions.clear(); - ctx.partitions.emplace_back(ctx); - - ctx.arg.progName = args[0]; - ctx.arg.inBuffers = inBuffers; - ctx.arg.outBuffer = outBuffer; - ctx.arg.useIOMemoryBuffers = true; - - ctx.driver.linkerMain(args); - - return errCount(ctx) == 0; -} -// EVM local end } // namespace elf } // namespace lld @@ -2004,14 +1967,6 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) { } else { ErrAlways(ctx) << "cannot find version script " << arg->getValue(); } - - // EVM local begin - if (args.hasArg(OPT_evm_assembly)) { - ctx.arg.evmAssembly = true; - ctx.arg.unresolvedSymbols = UnresolvedPolicy::Ignore; - ctx.arg.emitRelocs = true; - } - // EVM local end } // Some Config members do not directly correspond to any particular @@ -2084,19 +2039,13 @@ static void setConfigs(Ctx &ctx, opt::InputArgList &args) { (!ctx.arg.entry.empty() || (!ctx.arg.shared && !ctx.arg.relocatable)); if (ctx.arg.entry.empty() && !ctx.arg.relocatable) ctx.arg.entry = ctx.arg.emachine == EM_MIPS ? "__start" : "_start"; - if (!ctx.arg.useIOMemoryBuffers && ctx.arg.outputFile.empty()) // EVM local + if (ctx.arg.outputFile.empty()) ctx.arg.outputFile = "a.out"; - // EVM local begin - if (ctx.arg.useIOMemoryBuffers && !ctx.arg.outputFile.empty()) - error("specification of an out file name has no effect, as the memory " - "buffer will be used instead"); - // EVM local end - // Fail early if the output file or map file is not writable. If a user has a // long link, e.g. due to a large LTO link, they do not wish to run it and // find that it failed because there was a mistake in their command-line. - if (!ctx.arg.useIOMemoryBuffers) { // EVM local + { llvm::TimeTraceScope timeScope("Create output files"); if (auto e = tryCreateFile(ctx.arg.outputFile)) ErrAlways(ctx) << "cannot open output file " << ctx.arg.outputFile << ": " @@ -2107,7 +2056,7 @@ static void setConfigs(Ctx &ctx, opt::InputArgList &args) { if (auto e = tryCreateFile(ctx.arg.whyExtract)) ErrAlways(ctx) << "cannot open --why-extract= file " << ctx.arg.whyExtract << ": " << e.message(); - } // EVM local + } } static bool isFormatBinary(Ctx &ctx, StringRef s) { @@ -2149,13 +2098,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) { } case OPT_script: case OPT_default_script: - // EVM local begin - if (ctx.arg.useIOMemoryBuffers) { - if (std::optional mb = readFile(ctx, arg->getValue())) - readLinkerScript(ctx, *mb); - break; - // EVM local end - } else if (std::optional path = + if (std::optional path = searchScript(ctx, arg->getValue())) { if (std::optional mb = readFile(ctx, *path)) { if (arg->getOption().matches(OPT_default_script)) { diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index ca640cdb0f7a..62b54f8217bc 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -250,31 +250,15 @@ std::optional elf::readFile(Ctx &ctx, StringRef path) { Log(ctx) << path; ctx.arg.dependencyFiles.insert(llvm::CachedHashString(path)); - // EVM local begin - MemoryBufferRef mbref; - if (ctx.arg.useIOMemoryBuffers) { - unsigned idx = 0; - if (path.getAsInteger(10, idx)) { - error(path + ": path should be an index"); - return std::nullopt; - } - if (idx >= ctx.arg.inBuffers.size()) { - error("memory buffer index is out of range"); - return std::nullopt; - } - mbref = ctx.arg.inBuffers[idx]; - // Don't take MB ownership, because it's owned externally. - } else { - auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false, - /*RequiresNullTerminator=*/false); - if (auto ec = mbOrErr.getError()) { - ErrAlways(ctx) << "cannot open " << path << ": " << ec.message(); - return std::nullopt; - } - mbref = (*mbOrErr)->getMemBufferRef(); - ctx.memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership + auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false, + /*RequiresNullTerminator=*/false); + if (auto ec = mbOrErr.getError()) { + ErrAlways(ctx) << "cannot open " << path << ": " << ec.message(); + return std::nullopt; } - // EVM local end + + MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef(); + ctx.memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership if (ctx.tar) ctx.tar->append(relativeToRoot(path), mbref.getBuffer()); @@ -1749,10 +1733,6 @@ static uint16_t getBitcodeMachineKind(Ctx &ctx, StringRef path, return t.isOSIAMCU() ? EM_IAMCU : EM_386; case Triple::x86_64: return EM_X86_64; - // EVM local begin - case Triple::evm: - return EM_EVM; - // EVM local end default: ErrAlways(ctx) << path << ": could not infer e_machine from bitcode target triple " diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 3648f3338354..68e3feb1bd04 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -481,45 +481,6 @@ void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf, auto *p = reinterpret_cast(buf); buf += sizeof(RelTy); - // EVM local begin - // The --evm-assembly mode implies the --emit-relocs option, which - // instructs the linker to emit both applied relocations - // (for defined symbols) and unresolved relocations - // (for undefined symbols). We do not need to retain the applied - // relocations, as their symbols will be removed from the output file. - // To remove them from the '.rela' section, we would need to create a - // new synthetic section, copy only the necessary relocations there, - // and map that section to the output file. However, a simpler approach - // seems to be to use the original relocation sections and change the type - // of unnecessary relocations to 'NONE'. Please note that we cannot simply - // omit them from the output file, as by the time 'copyRelocations' is - // called, the ELF header (which describes section sizes) has already been - // emitted. Therefore, we cannot alter the number of relocations in the - // output - // '.rela' section. - // For example, the resulting '.rela' sections might look like: - // - // Relocation section '.rela.text' at offset 0x1038 contains 9 entries: - // - // Offset Info Type Sym. Value Symbol's Name + Addend - // 00000000 00000000 R_EVM_NONE 0 - // 00000000 00000000 R_EVM_NONE 0 - // 00000000 00000000 R_EVM_NONE 0 - // 00000000 00000000 R_EVM_NONE 0 - // 0000001f 00000601 R_EVM_DATA 00000000 __linker_symbol__0 + 0 - // 00000023 00000701 R_EVM_DATA 00000000 __linker_symbol__1 + 0 - // 00000027 00000801 R_EVM_DATA 00000000 __linker_symbol__2 + 0 - // 0000002b 00000901 R_EVM_DATA 00000000 __linker_symbol__3 + 0 - // 0000002f 00000a01 R_EVM_DATA 00000000 __linker_symbol__4 + 0 - // - if (ctx.arg.evmAssembly && sym.isDefined()) { - p->r_offset = 0; - p->r_addend = 0; - p->setSymbolAndType(0, 0, false); - continue; - } - // EVM local end - if (RelTy::HasAddend) p->r_addend = rel.addend; diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td index ff19256142d0..cc91680550b4 100644 --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -444,8 +444,6 @@ defm rpath: Eq<"rpath", "Add a DT_RUNPATH to the output">; def relocatable: F<"relocatable">, HelpText<"Create relocatable object file">; -def evm_assembly: F<"evm-assembly">, HelpText<"Create evm assembly object file">; - defm retain_symbols_file: Eq<"retain-symbols-file", "Retain only the symbols listed in the file">, MetaVarName<"">; diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp index d47a669a690d..ad7d57d30668 100644 --- a/lld/ELF/Target.cpp +++ b/lld/ELF/Target.cpp @@ -82,10 +82,6 @@ void elf::setTarget(Ctx &ctx) { return setSystemZTargetInfo(ctx); case EM_X86_64: return setX86_64TargetInfo(ctx); - // EVM local begin - case EM_EVM: - return setEVMTargetInfo(ctx); - // EVM local end default: Fatal(ctx) << "unsupported e_machine value: " << ctx.arg.emachine; } diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h index eae541844a47..93f15920bfed 100644 --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -195,9 +195,6 @@ void setSPARCV9TargetInfo(Ctx &); void setSystemZTargetInfo(Ctx &); void setX86TargetInfo(Ctx &); void setX86_64TargetInfo(Ctx &); -// EVM local begin -void setEVMTargetInfo(Ctx &); -// EVM local end struct ErrorPlace { InputSectionBase *isec; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index d1d4cf0d7f73..15909daf51ab 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2868,10 +2868,6 @@ static uint16_t getELFType(Ctx &ctx) { return ET_DYN; if (ctx.arg.relocatable) return ET_REL; - // EVM local begin - if (ctx.arg.evmAssembly) - return ET_REL; - // EVM local end return ET_EXEC; } @@ -2930,29 +2926,6 @@ template void Writer::openFile() { return; } - // EVM local begin - if (ctx.arg.useIOMemoryBuffers) { - // Create a special buffer that outputs its content to the - // raw memory stream (config->outBuffer) instead of a file. - // The ugly point in this design is that the buffer type is derived - // from FileOutputBuffer, but the file management functionality - // is not used. Probably this is fine, as otherwise we will need - // much more changes in the lld code. - Expected> bufferOrErr = - FileOutputBuffer::create(fileSize, *ctx.arg.outBuffer); - - if (!bufferOrErr) { - error("failed to create memory buffer for the result: " + - llvm::toString(bufferOrErr.takeError())); - return; - } - - buffer = std::move(*bufferOrErr); - ctx.bufferStart = buffer->getBufferStart(); - return; - } - // EVM local end - unlinkAsync(ctx.arg.outputFile); unsigned flags = 0; if (!ctx.arg.relocatable) diff --git a/lld/include/lld-c/LLDAsLibraryC.h b/lld/include/lld-c/LLDAsLibraryC.h deleted file mode 100644 index ec3144ca14b5..000000000000 --- a/lld/include/lld-c/LLDAsLibraryC.h +++ /dev/null @@ -1,142 +0,0 @@ -//==--------- LLDAsLibraryC.h - LLD Public C Interface --------*- C++ -*----==// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This header declares the following EVM C interfaces: -// - 'LLVMLinkEVM' -// The interface to the LLD linker functionality (via lld-as-a-library) -// -// - 'LLVMIsELF' -// Checks if the given memory buffer contains an ELF EVM object file -// -// - 'LLVMGetUndefinedSymbolsEVM' -// Returns an array of undefined linker symbols -// -// - 'LLVMDisposeUndefinedSymbolsEVM' -// Disposes an array returned by the 'LLVMGetUndefinedSymbolsEVM' -// -// These functions use a notion of the 'Linker Symbol' which is generalization -// of a usual ELF global symbol. The main difference is that 'Linker Symbol' -// has a 20-byte value, whereas the maximum value width of a usual ELF symbol -// is just 8 bytes. In order to represent a 20-byte symbol value with its -// relocation, initial 'Linker Symbol' is split into five sub-symbols -// which are usual 32-bit ELF symbols. This split is performed by the LLVM MC -// layer. For example, if the codegen needs to represent a 20-byte relocatable -// value associated with the symbol 'symbol_id', the MC layer sequentially -// (in the binary layout) emits the following undefined symbols: -// -// '__linker_symbol_id_0' -// '__linker_symbol_id_1' -// '__linker_symbol_id_2' -// '__linker_symbol_id_3' -// '__linker_symbol_id_4' -// -// with associated 32-bit relocations. Each sub-symbol name is formed by -// prepending '__linker' and appending '_[0-4]'. MC layer also sets the -// ELF::STO_EVM_LINKER_SYMBOL flag in the 'st_other' field in the symbol -// table entry to distinguish such symbols from all others. -// In EVM, only these symbols are allowed to be undefined in an object -// code. All other cases must be treated as unreachable and denote a bug -// in the FE/LLVM codegen/Linker implementation. -// 'Linker Symbols' are resolved, i.e they receive their final 20-byte -// values, at the linkage stage when calling LLVMLinkEVM. -// For this, the 'LLVMLinkEVM' has parameters: -// - \p linkerSymbolNames, array of null-terminated linker symbol names -// - \p linkerSymbolValues, array of symbol values -// -// For example, if linkerSymbolNames[0] points to a string 'symbol_id', -// it takes the linkerSymbolValues[0] value which is 20-byte array -// 0xAAAAAAAABB.....EEEEEEEE) and creates five symbol definitions in -// a linker script: -// -// "__linker_symbol_id_0" = 0xAAAAAAAA -// "__linker_symbol_id_1" = 0xBBBBBBBB -// "__linker_symbol_id_2" = 0xCCCCCCCC -// "__linker_symbol_id_3" = 0xDDDDDDDD -// "__linker_symbol_id_4" = 0xEEEEEEEE -// -//===----------------------------------------------------------------------===// - -#ifndef LLD_C_LLDASLIBRARYC_H -#define LLD_C_LLDASLIBRARYC_H - -#include "llvm-c/ExternC.h" -#include "llvm-c/Types.h" - -LLVM_C_EXTERN_C_BEGIN - -// Currently, the size of a linker symbol is limited to 20 bytes, as its the -// only usage is to represent Ethereum addresses which are of 160 bit width. -#define LINKER_SYMBOL_SIZE 20 - -/** Returns true if the \p inBuffer contains an EVM ELF object file. */ -LLVMBool LLVMIsELFEVM(LLVMMemoryBufferRef inBuffer); - -/** - * Performs the following steps, which are based on Ethereum's - * Assembly::assemble() logic: - * - Concatenates the .text sections of input ELF files referenced - * by __dataoffset* symbols from the first file. - * - Resolves undefined __dataoffset* and __datasize* symbols. - * - Gathers all undefined linker symbols (library references) from - * all files. - * - Ensures that the first file does not load and set - * immutables simultaneously. - * - * \p codeSegment, 0 means the first file has a deploy code, - * 1 - runtime code; - * \p inBuffers - relocatable ELF files to be assembled - * \p inBuffersIDs - their IDs - * \p outBuffer - resulting relocatable object file */ -LLVMBool LLVMAssembleEVM(uint64_t codeSegment, - const LLVMMemoryBufferRef inBuffers[], - const char *const inBuffersIDs[], - uint64_t inBuffersNum, LLVMMemoryBufferRef *outBuffer, - char **errorMessage); - -/** Returns an array of undefined linker symbol names - * from the ELF object provided in \p inBuffer. */ -void LLVMGetUndefinedReferencesEVM(LLVMMemoryBufferRef inBuffer, - char ***linkerSymbols, - uint64_t *numLinkerSymbols); - -/** Disposes of the array of symbols returned by the - * 'LLVMGetUndefinedReferences' function. */ -void LLVMDisposeUndefinedReferences(char *symbolNames[], uint64_t numSymbols); - -/** Resolves undefined linker symbols in the ELF object file \p inBuffer. - * Returns the ELF object file if any linker symbols remain unresolved; - * otherwise, returns the bytecode. */ -LLVMBool LLVMLinkEVM(LLVMMemoryBufferRef inBuffer, - LLVMMemoryBufferRef *outBuffer, - const char *const *linkerSymbolNames, - const char linkerSymbolValues[][LINKER_SYMBOL_SIZE], - uint64_t numLinkerSymbols, char **errorMessage); - -/** Returns the immutable symbol names and their offsets from the ELF - * object file provided in \p inBuffer. */ -uint64_t LLVMGetImmutablesEVM(LLVMMemoryBufferRef inBuffer, - char ***immutableIDs, - uint64_t **immutableOffsets); - -/** Returns an array of offsets of the linker symbol relocations form the - * ELF object file provided in \p inBuffer. */ -uint64_t LLVMGetSymbolOffsetsEVM(LLVMMemoryBufferRef inBuffer, - const char *symbolName, - uint64_t **symbolOffsets); - -/** Disposes of the immutable names and their offsets returned by - * 'LLVMGetImmutablesEVM'. */ -void LLVMDisposeImmutablesEVM(char **immutableIDs, uint64_t *immutableOffsets, - uint64_t numOfImmutables); - -/** Releases the array of linker symbol offsets. */ -void LLVMDisposeSymbolOffsetsEVM(uint64_t *offsets); - -LLVM_C_EXTERN_C_END - -#endif // LLD_C_LLDASLIBRARYC_H diff --git a/lld/include/lld/Common/Driver.h b/lld/include/lld/Common/Driver.h index 35792ad4cbe7..8520e6e7e257 100644 --- a/lld/include/lld/Common/Driver.h +++ b/lld/include/lld/Common/Driver.h @@ -10,7 +10,6 @@ #define LLD_COMMON_DRIVER_H #include "llvm/ADT/ArrayRef.h" -#include "llvm/Support/MemoryBufferRef.h" #include "llvm/Support/raw_ostream.h" namespace lld { @@ -26,24 +25,11 @@ enum Flavor { using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &, llvm::raw_ostream &, bool, bool); -// EVM local begin -using DriverMemBuf = bool (*)(llvm::ArrayRef inBuffers, - llvm::raw_pwrite_stream *outBuffer, - llvm::ArrayRef, llvm::raw_ostream &, - llvm::raw_ostream &, bool, bool); -// EVM local end - struct DriverDef { Flavor f; Driver d; }; -// EVM local begin -struct DriverDefMemBuf { - Flavor f; - DriverMemBuf d; -}; -// EVM local begin struct Result { int retCode; bool canRunAgain; @@ -57,13 +43,6 @@ struct Result { // by cleanup. Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, llvm::ArrayRef drivers); -// EVM local begin -Result lldMainMemBuf(llvm::ArrayRef inBuffers, - llvm::raw_pwrite_stream *outBuffer, - llvm::ArrayRef args, - llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, - llvm::ArrayRef drivers); -// EVM local end } // namespace lld // With this macro, library users must specify which drivers they use, provide @@ -77,19 +56,6 @@ Result lldMainMemBuf(llvm::ArrayRef inBuffers, } \ } -// EVM local begin -#define LLD_HAS_DRIVER_MEM_BUF(name) \ - namespace lld { \ - namespace name { \ - bool linkMemBuf(llvm::ArrayRef inBuffers, \ - llvm::raw_pwrite_stream *outBuffer, \ - llvm::ArrayRef args, \ - llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, \ - bool exitEarly, bool disableOutput); \ - } \ - } -// EVM local end - // An array which declares that all LLD drivers are linked in your executable. // Must be used along with LLD_HAS_DRIVERS. See examples in LLD unittests. #define LLD_ALL_DRIVERS \ @@ -100,10 +66,4 @@ Result lldMainMemBuf(llvm::ArrayRef inBuffers, } \ } -// EVM local begin -#define LLD_ALL_DRIVERS_MEM_BUF \ - { \ - { lld::Gnu, &lld::elf::linkMemBuf } \ - } -// EVM local end #endif diff --git a/lld/lld-c/CMakeLists.txt b/lld/lld-c/CMakeLists.txt deleted file mode 100644 index 1ab04cd41b0b..000000000000 --- a/lld/lld-c/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -add_lld_library(lldC - LLDAsLibraryC.cpp - - ${LLD_INCLUDE_DIR}/lld/Common - - LINK_COMPONENTS - Core - EVMDesc - ObjCopy - Object - Support - - LINK_LIBS - lldCommon - lldELF - - DEPENDS - intrinsics_gen - ) diff --git a/lld/lld-c/LLDAsLibraryC.cpp b/lld/lld-c/LLDAsLibraryC.cpp deleted file mode 100644 index e4553f61b1d0..000000000000 --- a/lld/lld-c/LLDAsLibraryC.cpp +++ /dev/null @@ -1,869 +0,0 @@ -#include "lld-c/LLDAsLibraryC.h" -#include "lld/Common/Driver.h" -#include "lld/Common/ErrorHandler.h" -#include "llvm-c/Core.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/ADT/StringSet.h" -#include "llvm/BinaryFormat/ELF.h" -#include "llvm/ObjCopy/CommonConfig.h" -#include "llvm/ObjCopy/ConfigManager.h" -#include "llvm/ObjCopy/ObjCopy.h" -#include "llvm/Object/Binary.h" -#include "llvm/Object/ELFObjectFile.h" -#include "llvm/Object/ObjectFile.h" -#include "llvm/Support/Alignment.h" -#include "llvm/Support/Error.h" -#include "llvm/Support/FormatVariadic.h" -#include "llvm/Support/MemoryBuffer.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace llvm; -using namespace object; -using namespace objcopy; - -LLD_HAS_DRIVER_MEM_BUF(elf) - -namespace llvm { -namespace EVM { -std::string getLinkerSymbolHash(StringRef SymName); -bool isLinkerSymbolName(StringRef Name); -std::string getLinkerSymbolName(StringRef SymName); -bool isSymbolSectionName(StringRef Name); -std::string getSymbolSectionName(StringRef Name); -std::string getSymbolIndexedName(StringRef Name, unsigned SubIdx); -std::string getDataSizeSymbol(StringRef SymbolName); -std::string getNonIndexedSymbolName(StringRef Name); -bool isDataOffsetSymbolName(StringRef Name); -std::string getDataOffsetSymbol(StringRef Name); -std::string extractDataOffseteName(StringRef SymbolName); -bool isLoadImmutableSymbolName(StringRef Name); -std::string getImmutableId(StringRef Name); -} // namespace EVM -} // namespace llvm - -enum class ReferenceSymbolType { Linker }; - -constexpr static unsigned subSymbolRelocSize = sizeof(uint32_t); - -static std::mutex lldMutex; - -/// Returns reference to the section content. The section is expected -/// the be present in the file. -static StringRef getSectionContent(const ObjectFile &file, - StringRef sectionName) { - section_iterator si = std::find_if(file.section_begin(), file.section_end(), - [§ionName](const SectionRef &sec) { - StringRef curSecName = - cantFail(sec.getName()); - return curSecName == sectionName; - }); - if (si == file.section_end()) - report_fatal_error(Twine("lld: expected ") + sectionName + - " in object file"); - - return cantFail(si->getContents()); -} - -static std::string getLinkerSubSymbolName(StringRef name, unsigned idx) { - return EVM::getSymbolIndexedName( - EVM::getLinkerSymbolName(EVM::getLinkerSymbolHash(name)), idx); -} - -/// Returns true if the object file \p file contains any other undefined -/// linker dependency symbols besides those passed in \p linkerSymbolNames. -static bool hasUndefinedReferenceSymbols(ObjectFile &file, - const char *const *linkerSymbolNames, - uint64_t numLinkerSymbols) { - StringSet<> symbolsToBeDefined; - // Create a set of possible symbols from the 'linkerSymbolNames' array. - for (unsigned symIdx = 0; symIdx < numLinkerSymbols; ++symIdx) { - for (unsigned subSymIdx = 0; - subSymIdx < LINKER_SYMBOL_SIZE / subSymbolRelocSize; ++subSymIdx) { - std::string subSymName = - getLinkerSubSymbolName(linkerSymbolNames[symIdx], subSymIdx); - if (!symbolsToBeDefined.insert(subSymName).second) - report_fatal_error(Twine("lld: duplicating reference symbol ") + - subSymName + "in object file"); - } - } - - for (const SymbolRef &sym : file.symbols()) { - uint32_t symFlags = cantFail(sym.getFlags()); - uint8_t other = ELFSymbolRef(sym).getOther(); - if ((other == ELF::STO_EVM_REFERENCE_SYMBOL) && - (symFlags & object::SymbolRef::SF_Undefined)) { - StringRef symName = cantFail(sym.getName()); - if (!symbolsToBeDefined.contains(symName)) - return true; - } - } - return false; -} - -/// Returns a string with the symbol definitions passed in -/// \p linkerSymbolValues. For each name from the \p linkerSymbolNames array it -/// creates five symbol definitions. For example, if the linkerSymbolNames[0] -/// points to a string 'symbol_id', it takes the linkerSymbolValues[0] value -/// (which is 20 byte array: 0xAAAAAAAABB.....EEEEEEEE) and creates five symbol -/// definitions: -/// -/// __linker_symbol_id_0 = 0xAAAAAAAA -/// __linker_symbol_id_1 = 0xBBBBBBBB -/// __linker_symbol_id_2 = 0xCCCCCCCC -/// __linker_symbol_id_3 = 0xDDDDDDDD -/// __linker_symbol_id_4 = 0xEEEEEEEE -/// -static std::string -createSymbolDefinitions(const char *const *linkerSymbolNames, - const char linkerSymbolValues[][LINKER_SYMBOL_SIZE], - uint64_t numLinkerSymbols) { - auto getSymbolDef = - [](StringRef symName, const char *symVal, size_t symSize, - std::function subSymNameFunc) - -> std::string { - std::string symbolDefBuf; - raw_string_ostream symbolDef(symbolDefBuf); - SmallString<128> hexStrSymbolVal; - toHex(ArrayRef(reinterpret_cast(symVal), symSize), - /*LowerCase*/ false, hexStrSymbolVal); - for (unsigned idx = 0; idx < symSize / subSymbolRelocSize; ++idx) { - symbolDef << subSymNameFunc(symName, idx); - symbolDef << " = 0x"; - symbolDef << hexStrSymbolVal - .substr(2 * subSymbolRelocSize * idx, - 2 * subSymbolRelocSize) - .str(); - symbolDef << ";\n"; - } - return symbolDef.str(); - }; - - std::string symbolsDefBuf; - raw_string_ostream symbolsDef(symbolsDefBuf); - for (uint64_t symNum = 0; symNum < numLinkerSymbols; ++symNum) - symbolsDef << getSymbolDef(linkerSymbolNames[symNum], - linkerSymbolValues[symNum], LINKER_SYMBOL_SIZE, - &getLinkerSubSymbolName); - - return symbolsDef.str(); -} - -/// Returns an array of names of undefined reference symbols in the -/// ELF object. -static char **LLVMGetUndefinedSymbols(const ObjectFile *oFile, - uint64_t *numSymbols, - ReferenceSymbolType symbolType) { - StringSet<> undefSymbols; - StringSet<> undefSubSymbols; - for (const SymbolRef &sym : oFile->symbols()) { - uint32_t symFlags = cantFail(sym.getFlags()); - uint8_t other = ELFSymbolRef(sym).getOther(); - if ((other == ELF::STO_EVM_REFERENCE_SYMBOL) && - (symFlags & object::SymbolRef::SF_Undefined)) { - StringRef subName = cantFail(sym.getName()); - if (symbolType == ReferenceSymbolType::Linker && - EVM::isLinkerSymbolName(subName)) { - undefSubSymbols.insert(subName); - std::string symName = EVM::getNonIndexedSymbolName(subName); - undefSymbols.insert(symName); - } - } - } - - *numSymbols = undefSymbols.size(); - if (!undefSymbols.size()) - return nullptr; - - char **undefSymbolNames = reinterpret_cast( - std::malloc(undefSymbols.size() * sizeof(char *))); - unsigned undefSymIdx = 0; - for (const StringSet<>::value_type &entry : undefSymbols) { - StringRef symName = entry.first(); - // Check that 'undefSubSymbols' forms a set of groups each consisting of - // five or eight sub-symbols depending on the symbol type. - for (unsigned idx = 0; idx < LINKER_SYMBOL_SIZE / subSymbolRelocSize; - idx++) { - std::string subSymName = EVM::getSymbolIndexedName(symName, idx); - if (!undefSubSymbols.contains(subSymName)) { - report_fatal_error(Twine("lld: missing reference symbol ") + - subSymName); - } - } - std::string secName = EVM::getSymbolSectionName(symName); - undefSymbolNames[undefSymIdx++] = - strdup(getSectionContent(*oFile, secName).str().c_str()); - } - - // Sort the returned names in lexicographical order. - std::sort( - undefSymbolNames, undefSymbolNames + *numSymbols, - [](const char *s1, const char *s2) { return std::strcmp(s1, s2) < 0; }); - - return undefSymbolNames; -} - -/// Disposes an array with symbols returned by the -/// LLVMGetUndefinedReferences* functions. -void LLVMDisposeUndefinedReferences(char *symbolNames[], uint64_t numSymbols) { - for (unsigned idx = 0; idx < numSymbols; ++idx) - std::free(symbolNames[idx]); - std::free(symbolNames); -} - -//----------------------------------------------------------------------------// - -/// Create linker script with linker symbol definitions. -static std::string createEVMLinkerSymbolsDefinitions( - const char *const *linkerSymbolNames, - const char linkerSymbolValues[][LINKER_SYMBOL_SIZE], - uint64_t numLinkerSymbols) { - return createSymbolDefinitions(linkerSymbolNames, linkerSymbolValues, - numLinkerSymbols); -} - -/// Returns true if the \p inBuffer contains an EVM ELF object file. -LLVMBool LLVMIsELFEVM(LLVMMemoryBufferRef inBuffer) { - Expected> inBinaryOrErr = - ELFObjectFile::create(unwrap(inBuffer)->getMemBufferRef()); - if (!inBinaryOrErr) { - handleAllErrors(inBinaryOrErr.takeError(), [](const ErrorInfoBase &EI) {}); - return false; - } - return inBinaryOrErr.get().getArch() == Triple::evm; -} - -/// Returns an array of undefined linker symbol names -/// from the ELF object provided in \p inBuffer. -void LLVMGetUndefinedReferencesEVM(LLVMMemoryBufferRef inBuffer, - char ***linkerSymbols, - uint64_t *numLinkerSymbols) { - assert(linkerSymbols && numLinkerSymbols); - *linkerSymbols = nullptr; - *numLinkerSymbols = 0; - if (!LLVMIsELFEVM(inBuffer)) - return; - - std::unique_ptr inBinary = - cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); - const auto *oFile = static_cast(inBinary.get()); - - *linkerSymbols = LLVMGetUndefinedSymbols(oFile, numLinkerSymbols, - ReferenceSymbolType::Linker); -} - -/// Resolves undefined linker symbols in the ELF object file \p inBuffer. -/// Returns the ELF object file if any linker symbols remain unresolved; -/// otherwise, returns the bytecode. -LLVMBool LLVMLinkEVM(LLVMMemoryBufferRef inBuffer, - LLVMMemoryBufferRef *outBuffer, - const char *const *linkerSymbolNames, - const char linkerSymbolValues[][LINKER_SYMBOL_SIZE], - uint64_t numLinkerSymbols, char **errorMessage) { - SmallVector localInMemBufRefs(2); - localInMemBufRefs[0] = *unwrap(inBuffer); - - *outBuffer = nullptr; - if (!LLVMIsELFEVM(inBuffer)) { - *errorMessage = strdup("Input binary is not an EVM ELF file"); - return true; - } - - std::unique_ptr inBinary = - cantFail(createBinary(localInMemBufRefs[0])); - assert(inBinary->isObject()); - bool shouldEmitRelocatable = - hasUndefinedReferenceSymbols(*static_cast(inBinary.get()), - linkerSymbolNames, numLinkerSymbols); - - std::string linkerScript = createEVMLinkerSymbolsDefinitions( - linkerSymbolNames, linkerSymbolValues, numLinkerSymbols); - - std::unique_ptr linkerScriptBuf = - MemoryBuffer::getMemBuffer(linkerScript, "1"); - - localInMemBufRefs[1] = linkerScriptBuf->getMemBufferRef(); - - SmallVector lldArgs; - lldArgs.push_back("ld.lld"); - - // Push the name of the linker script - '1'. - lldArgs.push_back("-T"); - lldArgs.push_back("1"); - - // Push the name of the input object file - '0'. - lldArgs.push_back("0"); - - // If all symbols are resolved, strip the ELF format and emit the final - // bytecode. Otherwise, emit an ELF relocatable file. - if (shouldEmitRelocatable) - lldArgs.push_back("--relocatable"); - else - lldArgs.push_back("--oformat=binary"); - - // The input is one small object: the thread pool costs more than it saves. - lldArgs.push_back("--threads=1"); - - SmallString<0> codeString; - raw_svector_ostream ostream(codeString); - SmallString<0> errorString; - raw_svector_ostream errorOstream(errorString); - - // Lld-as-a-library is not thread-safe due to its global state, - // so we need to protect it from concurrent access by multiple threads. - std::unique_lock lock(lldMutex); - const lld::Result s = - lld::lldMainMemBuf(localInMemBufRefs, &ostream, lldArgs, outs(), - errorOstream, {{lld::Gnu, &lld::elf::linkMemBuf}}); - lock.unlock(); - - bool ret = !s.retCode && s.canRunAgain; - if (!ret) { - *errorMessage = strdup(errorString.c_str()); - return true; - } - - StringRef data = ostream.str(); - *outBuffer = LLVMCreateMemoryBufferWithMemoryRangeCopy(data.data(), - data.size(), "result"); - - return false; -} - -/// Finds symbol name sections and adds them to the \p namesMap. -/// A symbol name section has the following format: -/// -/// .symbol_name__linker_symbol__$[0-9a-f]{64}$__ -/// -static void getSymbolNameSections(const ObjectFile *oFile, const char *fileID, - StringMap> &namesMap) { - [[maybe_unused]] SmallVector sectionNames; - for (const SectionRef &sec : oFile->sections()) { - StringRef secName = cantFail(sec.getName()); - if (EVM::isSymbolSectionName(secName)) { - namesMap[secName].push_back(fileID); -#ifndef NDEBUG - sectionNames.push_back(secName); -#endif // NDEBUG - } - } - -#ifndef NDEBUG - // Verify that undefined linker symbols exist for the name sections. - StringMap expectedSymNames; - for (StringRef secName : sectionNames) { - StringRef refName = getSectionContent(*oFile, secName); - for (unsigned idx = 0; idx < LINKER_SYMBOL_SIZE / subSymbolRelocSize; - ++idx) { - std::string symName = getLinkerSubSymbolName(refName, idx); - expectedSymNames[symName]++; - } - } - - StringMap actualSymNames; - for (const SymbolRef &sym : oFile->symbols()) { - section_iterator symSec = cantFail(sym.getSection()); - // Undefined symbol has no related section. - if (symSec != oFile->section_end()) - continue; - - StringRef symName = cantFail(sym.getName()); - if (EVM::isLinkerSymbolName(symName)) - actualSymNames[symName]++; - } - assert(actualSymNames == expectedSymNames); -#endif // NDEBUG -} - -/// Creates a linker script to generate an 'assembly' ELF object file. -/// Here is an example of the script: -/// -/// ENTRY(0); -/// SECTIONS { -/// . = 0; -/// .text : SUBALIGN(1) { -/// __datasize__$12c297efd8baf$__ = 268; -/// __dataoffset__$b5e66d52578d$__ = ABSOLUTE(.); -/// __$b5e66d52578d$__(.text); -/// __dataoffset__$12c297efd8baf$__ = ABSOLUTE(.); -/// __$12c297efd8baf$__(.text); -/// __$b5e66d52578d$__(.metadata) -/// __datasize__$b5e66d52578d$__ = ABSOLUTE(.); -/// } -/// /DISCARD/ : { -/// } -/// } -/// VERSION { -/// { local: -/// __datasize__$12c297efd8baf$__; -/// __dataoffset__$b5e66d52578d$__; -/// __dataoffset__$12c297efd8baf$__; -/// __datasize__$b5e66d52578d$__; -/// }; -/// }; - -static std::string createEVMAssembeScript(ArrayRef memBufs, - ArrayRef depIDs, - const StringSet<> &depsToLink) { - assert(memBufs.size() == depIDs.size()); - - // Maps symbol name section to an array of object IDs that contain it. - StringMap> symbolNameSectionsMap; - - auto getDataSizeName = [](StringRef name) { - return EVM::getDataSizeSymbol(EVM::getLinkerSymbolHash(name)); - }; - - // A set of all the defined symbols in the script that should be removed - // from the symbol table. To achieve this, we set their visibility - // to 'local'. These symbols will later be removed using the 'objcopy' API. - std::string definedSymbolsBuf; - raw_string_ostream definedSymbols(definedSymbolsBuf); - - std::unique_ptr firstBinary = - cantFail(createBinary(unwrap(memBufs[0])->getMemBufferRef())); - const auto *firstObjFile = static_cast(firstBinary.get()); - getSymbolNameSections(firstObjFile, depIDs[0], symbolNameSectionsMap); - - std::string textSectionBuf; - raw_string_ostream textSection(textSectionBuf); - - // A set of all the defined symbols in the script that should be removed - // from the symbol table. To achieve this, we set their visibility to - // 'local'. These symbols will later be removed using the 'objcopy' API. - for (unsigned idx = 1; idx < memBufs.size(); ++idx) { - std::unique_ptr binary = - cantFail(createBinary(unwrap(memBufs[idx])->getMemBufferRef())); - const auto *objFile = static_cast(binary.get()); - std::string bufIdHash = EVM::getLinkerSymbolHash(depIDs[idx]); - if (depsToLink.count(bufIdHash)) - getSymbolNameSections(objFile, depIDs[idx], symbolNameSectionsMap); - - for (const SectionRef &sec : objFile->sections()) { - if (sec.isText()) { - assert(cantFail(sec.getName()) == ".text"); - std::string sym = getDataSizeName(depIDs[idx]); - definedSymbols << sym << ";\n"; - textSection << sym << " = " << sec.getSize() << ";\n"; - break; - } - } - } - - // __dataoffset_Id_1 = .; - // Id_1(.text); - // __dataoffset_Id_2 = .; - // Id_2(.text); - // ... - // __dataoffset_Id_N = .; - // Id_N(.text); - for (unsigned idx = 0; idx < memBufs.size(); ++idx) { - std::string bufIdHash = EVM::getLinkerSymbolHash(depIDs[idx]); - // Do not link the dependency if it's not referenced via - // __dataoffset. - if (idx != 0 && !depsToLink.count(bufIdHash)) - continue; - - std::string sym = EVM::getDataOffsetSymbol(bufIdHash); - definedSymbols << sym << ";\n"; - textSection << sym << " = ABSOLUTE(.);\n"; - textSection << bufIdHash << "(.text);\n"; - } - - // Append matadata section of the first object (if any). - textSection << EVM::getLinkerSymbolHash(depIDs[0]) << "(.metadata);\n"; - - // Define a symbol whose value is the total size of the output - // '.text' section. - std::string firstDataSizeSym = getDataSizeName(depIDs[0]); - definedSymbols << firstDataSizeSym << ";\n"; - textSection << firstDataSizeSym << " = ABSOLUTE(.);\n"; - - // When assembling multiple ELF files that reference the same library, - // the files will have identical (name and content) sections: - // - // .symbol_name__linker_symbol__$[0-9a-f]{64}$__ - // - // By default, the linker will concatenate the sections, which is not desired. - // We need to retain only one section with the original content in the output - // file. To achieve this, we discard all duplicate input sections and keep - // just one. - std::string discardSectionsBuf; - raw_string_ostream discardSections(discardSectionsBuf); - for (const auto &[secName, IDs] : symbolNameSectionsMap) { - // We need to remove all symbol name sections, retaining just one - // (regardless of which one). The sections to be removed should be placed - // in the /DISCARD/ output section. - assert(IDs.size() > 0); - for (StringRef id : drop_begin(IDs)) { - std::string idHash = EVM::getLinkerSymbolHash(id); - discardSections << idHash << '(' << secName << ");\n"; - } - } - - std::string script = - formatv(R"(ENTRY(0); -SECTIONS { - . = 0; - .text : SUBALIGN(1) { -{0} - } -/DISCARD/ : { -{2} -} -} -VERSION { - { local: -{1} - }; -};)", - textSection.str(), definedSymbols.str(), discardSections.str()); - - return script; -} - -/// Removes all local symbols from the object file, except for undefined -/// linker symbols and immutables from the first file. -/// This is done using the 'objcopy' API. -static LLVMMemoryBufferRef removeLocalSymbols(LLVMMemoryBufferRef inBuffer, - StringSet<> firstFileImmutables) { - std::unique_ptr inBinary = - cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); - auto *oFile = static_cast(inBinary.get()); - - StringSet<> undefReferenceSymbols; - for (const SymbolRef &sym : oFile->symbols()) { - uint32_t symFlags = cantFail(sym.getFlags()); - uint8_t other = ELFSymbolRef(sym).getOther(); - if ((other == ELF::STO_EVM_REFERENCE_SYMBOL) && - (symFlags & object::SymbolRef::SF_Undefined)) { - undefReferenceSymbols.insert(cantFail(sym.getName())); - } - } - - auto errCallback = [](Error E) { - report_fatal_error(StringRef(toString(std::move(E)))); - return Error::success(); - }; - - // Create an 'objcopy' configuration that removes all local - // symbols while explicitly retaining the necessary ones. - ConfigManager Config; - Config.Common.DiscardMode = DiscardType::All; - for (const StringSet<>::value_type &entry : - llvm::concat::value_type>(undefReferenceSymbols, - firstFileImmutables)) { - if (Error E = Config.Common.SymbolsToKeep.addMatcher(NameOrPattern::create( - entry.first(), MatchStyle::Literal, errCallback))) - report_fatal_error(StringRef(toString(std::move(E)))); - } - - SmallString<0> dataVector; - raw_svector_ostream outStream(dataVector); - - if (Error Err = objcopy::executeObjcopyOnBinary(Config, *oFile, outStream)) - report_fatal_error(StringRef(toString(std::move(Err)))); - - MemoryBufferRef buffer(StringRef(dataVector.data(), dataVector.size()), ""); - Expected> result = createBinary(buffer); - - // Check the copied file. - if (!result) - report_fatal_error(StringRef(toString(result.takeError()))); - - StringRef data = outStream.str(); - StringRef bufName = unwrap(inBuffer)->getBufferIdentifier(); - return LLVMCreateMemoryBufferWithMemoryRangeCopy(data.data(), data.size(), - bufName.str().c_str()); -} - -/// Checks if the ELF file contains any undefined symbols, aside from -/// those used to represent library addresses. -static void getUndefinedNonRefSymbols(LLVMMemoryBufferRef inBuffer, - SmallVectorImpl &undefSyms) { - std::unique_ptr inBinary = - cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); - auto *oFile = static_cast(inBinary.get()); - - for (const SymbolRef &sym : oFile->symbols()) { - uint32_t symFlags = cantFail(sym.getFlags()); - uint8_t other = ELFSymbolRef(sym).getOther(); - if ((other != ELF::STO_EVM_REFERENCE_SYMBOL) && - (symFlags & object::SymbolRef::SF_Undefined)) - undefSyms.push_back(cantFail(sym.getName())); - } -} - -/// Returns an array of offsets for the linker symbol relocations. -/// These are the symbol offsets in the generated bytecode. -uint64_t LLVMGetSymbolOffsetsEVM(LLVMMemoryBufferRef inBuffer, - const char *symbolName, - uint64_t **symbolOffsets) { - std::unique_ptr inBinary = - cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); - auto *elfFile = static_cast(inBinary.get()); - - SmallVector Offsets; - // The relocation of a linker symbol is expressed as a sequence of - // five consecutive 32-bit relocations. We need only the first one. - std::string subSymName = getLinkerSubSymbolName(symbolName, 0); - for (const ELFSectionRef relocsSec : elfFile->sections()) { - // Find the relocation section. It should be .rela.text. - if (relocsSec.relocations().empty() || - !cantFail(relocsSec.getRelocatedSection())->isText()) - continue; - - // Check relocations of the specified type. - for (const ELFRelocationRef rel : relocsSec.relocations()) { - if (rel.getType() != ELF::R_EVM_DATA) - continue; - - elf_symbol_iterator sym = rel.getSymbol(); - uint32_t symFlags = cantFail(sym->getFlags()); - StringRef symName = cantFail(sym->getName()); - if ((sym->getOther() == llvm::ELF::STO_EVM_REFERENCE_SYMBOL) && - (symFlags & object::SymbolRef::SF_Undefined) && - (symName == subSymName)) - Offsets.push_back(rel.getOffset()); - } - } - - *symbolOffsets = reinterpret_cast( - std::malloc(Offsets.size() * sizeof(uint64_t))); - - sort(Offsets); - copy(Offsets, *symbolOffsets); - - return Offsets.size(); -} - -/// Performs the following steps, which are based on Ethereum's -/// Assembly::assemble() logic: -/// - Concatenates the .text sections of input ELF files referenced -/// by __dataoffset* symbols from the first file. -/// - Resolves undefined __dataoffset* and __datasize* symbols. -/// - Gathers all undefined linker symbols (library references) from -/// all files. -/// - Ensures that the first file does not load and set -/// immutables simultaneously. -/// -/// \p codeSegment, 0 means the first file has a deploy code, -/// 1 - runtime code; -/// \p inBuffers - relocatable ELF files to be assembled -/// \p inBuffersIDs - their IDs -/// \p outBuffer - resulting relocatable object file -LLVMBool LLVMAssembleEVM(uint64_t codeSegment, - const LLVMMemoryBufferRef inBuffers[], - const char *const inBuffersIDs[], - uint64_t inBuffersNum, LLVMMemoryBufferRef *outBuffer, - char **errorMessage) { - SmallVector localInMemBufRefs(inBuffersNum + 1); - SmallVector> localInMemBufs(inBuffersNum + 1); - - // TODO: #740. Verify that the object files contain sections with the original - // inBuffersIDs, i.e. before the hash is applied. - for (unsigned idx = 0; idx < inBuffersNum; ++idx) { - MemoryBufferRef ref = *unwrap(inBuffers[idx]); - // We need to copy the buffers to change their names, as the linking - // process depends on them. - localInMemBufs[idx] = MemoryBuffer::getMemBufferCopy( - ref.getBuffer(), EVM::getLinkerSymbolHash(inBuffersIDs[idx])); - localInMemBufRefs[idx] = localInMemBufs[idx]->getMemBufferRef(); - } - - std::unique_ptr firstBinary = - cantFail(createBinary(localInMemBufRefs[0])); - const auto *firstObjFile = static_cast(firstBinary.get()); - - // Retrieve the object names referenced by the first file. - // Retrieve the immutable symbols from the first file. - StringSet<> firstLoadImmutables; - StringSet<> firstDataOffsetRefs; - for (const SymbolRef &sym : firstObjFile->symbols()) { - StringRef symName = cantFail(sym.getName()); - if (EVM::isLoadImmutableSymbolName(symName)) - firstLoadImmutables.insert(symName); - else if (EVM::isDataOffsetSymbolName(symName)) { - std::string objName = EVM::extractDataOffseteName(symName); - firstDataOffsetRefs.insert(objName); - } - } - - // Check if the first (idx == 1) dependency file contains loadimmutable - // symbols. - // A 'codeSegment' value of 0 indicates that the first file contains a - // deploy code, which implies that the first dependency is the corresponding - // runtime code. - bool depHasLoadImmutable = false; - if (codeSegment == 0 && inBuffersNum > 1) { - std::unique_ptr binary = - cantFail(createBinary(localInMemBufRefs[1])); - const auto *oFile = static_cast(binary.get()); - - for (const SymbolRef &sym : oFile->symbols()) { - section_iterator symSec = cantFail(sym.getSection()); - if (symSec == oFile->section_end()) - continue; - - StringRef symName = cantFail(sym.getName()); - if (EVM::isLoadImmutableSymbolName(symName)) - depHasLoadImmutable = true; - } - } - - if (firstLoadImmutables.size() > 0 && depHasLoadImmutable) - report_fatal_error("lld: assembly both sets up and loads immutables"); - - std::string linkerScript = createEVMAssembeScript( - ArrayRef(inBuffers, inBuffersNum), ArrayRef(inBuffersIDs, inBuffersNum), - firstDataOffsetRefs); - - std::unique_ptr scriptBuf = - MemoryBuffer::getMemBuffer(linkerScript, "script.x"); - localInMemBufRefs[inBuffersNum] = scriptBuf->getMemBufferRef(); - - SmallVector lldArgs; - lldArgs.push_back("ld.lld"); - - // Use file name remapping (a linker feature) to replace file names with - // indexes in the array of memory buffers. - const std::string remapStr("--remap-inputs="); - SmallVector args; - for (unsigned idx = 0; idx < inBuffersNum; ++idx) { - std::string idHash = EVM::getLinkerSymbolHash(inBuffersIDs[idx]); - if (idx > 0 && !firstDataOffsetRefs.count(idHash)) - continue; - - args.emplace_back(remapStr + idHash + "=" + std::to_string(idx)); - args.emplace_back(std::to_string(idx)); - } - - args.emplace_back("-T"); - args.emplace_back(std::to_string(inBuffersNum)); - - for (const std::string &arg : args) - lldArgs.push_back(arg.c_str()); - - lldArgs.push_back("--evm-assembly"); - // The inputs are a few small objects: the thread pool costs more than it - // saves. - lldArgs.push_back("--threads=1"); - - SmallString<0> codeString; - raw_svector_ostream ostream(codeString); - SmallString<0> errorString; - raw_svector_ostream errorOstream(errorString); - - // Lld-as-a-library is not thread-safe due to its global state, - // so we need to protect it from concurrent access by multiple threads. - std::unique_lock lock(lldMutex); - const lld::Result s = - lld::lldMainMemBuf(localInMemBufRefs, &ostream, lldArgs, outs(), - errorOstream, {{lld::Gnu, &lld::elf::linkMemBuf}}); - lock.unlock(); - - bool ret = !s.retCode && s.canRunAgain; - if (!ret) { - *errorMessage = strdup(errorString.c_str()); - return true; - } - - StringRef data = ostream.str(); - LLVMMemoryBufferRef Tmp = LLVMCreateMemoryBufferWithMemoryRangeCopy( - data.data(), data.size(), - EVM::getLinkerSymbolHash(inBuffersIDs[0]).c_str()); - - SmallVector undefSyms; - getUndefinedNonRefSymbols(Tmp, undefSyms); - if (!undefSyms.empty()) { - std::string storage; - raw_string_ostream errMsg(storage); - for (StringRef name : undefSyms) - errMsg << "non-ref undefined symbol: " << name << '\n'; - - *errorMessage = strdup(errMsg.str().c_str()); - return true; - } - - *outBuffer = removeLocalSymbols(Tmp, firstLoadImmutables); - LLVMDisposeMemoryBuffer(Tmp); - - return false; -} - -/// Returns the immutable symbol names and their offsets from the ELF -/// object file provided in \p inBuffer. -uint64_t LLVMGetImmutablesEVM(LLVMMemoryBufferRef inBuffer, - char ***immutableIDs, - uint64_t **immutableOffsets) { - std::unique_ptr inBinary = - cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); - const auto *objFile = static_cast(inBinary.get()); - - // Maps immutable IDs to their references in the object code. - StringMap> immutablesMap; - for (const SymbolRef &sym : objFile->symbols()) { - section_iterator symSec = cantFail(sym.getSection()); - if (symSec == objFile->section_end()) - continue; - - StringRef symName = cantFail(sym.getName()); - if (EVM::isLoadImmutableSymbolName(symName)) { - std::string Id = EVM::getImmutableId(symName); - uint64_t symOffset = cantFail(sym.getValue()); - // The symbol points to the beginning of a PUSH32 instruction. - // We have to add 1 (opcode size) to get offset to the PUSH32 - // instruction operand. - immutablesMap[Id].push_back(symOffset + 1); - } - } - - if (immutablesMap.empty()) { - *immutableIDs = nullptr; - *immutableOffsets = nullptr; - return 0; - } - - uint64_t numOfImmutables = 0; - for (const auto &[id, offsets] : immutablesMap) { - numOfImmutables += offsets.size(); - }; - - *immutableIDs = - reinterpret_cast(std::malloc(numOfImmutables * sizeof(char *))); - *immutableOffsets = reinterpret_cast( - std::malloc(numOfImmutables * sizeof(uint64_t))); - - unsigned idx = 0; - for (const auto &[id, offsets] : immutablesMap) { - for (uint64_t offset : offsets) { - assert(idx < numOfImmutables); - (*immutableIDs)[idx] = strdup(id.str().c_str()); - (*immutableOffsets)[idx++] = offset; - } - } - - return numOfImmutables; -} - -/// Disposes of the immutable names and their offsets returned by -/// 'LLVMGetImmutablesEVM'. -void LLVMDisposeImmutablesEVM(char **immutableIDs, uint64_t *immutableOffsets, - uint64_t numOfImmutables) { - for (unsigned idx = 0; idx < numOfImmutables; ++idx) - std::free(immutableIDs[idx]); - - std::free(immutableIDs); - std::free(immutableOffsets); -} - -void LLVMDisposeSymbolOffsetsEVM(uint64_t *offsets) { std::free(offsets); } diff --git a/lld/test/lit.cfg.py b/lld/test/lit.cfg.py index b7dee94ad697..10f556567cdc 100644 --- a/lld/test/lit.cfg.py +++ b/lld/test/lit.cfg.py @@ -171,11 +171,3 @@ # ELF tests expect the default target for ld.lld to be ELF. if config.ld_lld_default_mingw: config.excludes.append("ELF") - -# EVM local begin -if config.target_triple.startswith("evm"): - config.excludes.append("wasm") - config.excludes.append("MachO") - config.excludes.append("COFF") - config.excludes.append("MinGW") -# EVM local end diff --git a/lld/unittests/CMakeLists.txt b/lld/unittests/CMakeLists.txt index 25cdf788504c..ffaea3f20783 100644 --- a/lld/unittests/CMakeLists.txt +++ b/lld/unittests/CMakeLists.txt @@ -4,6 +4,3 @@ endfunction() add_subdirectory(AsLibAll) add_subdirectory(AsLibELF) -# EVM local begin -add_subdirectory(EVM) -# EVM local End diff --git a/lld/unittests/EVM/CMakeLists.txt b/lld/unittests/EVM/CMakeLists.txt deleted file mode 100644 index 6514cde4f0f4..000000000000 --- a/lld/unittests/EVM/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -add_lld_unittests(EVMLLDTests - LLDTest.cpp -) - -target_link_libraries(EVMLLDTests - PRIVATE - lldCommon - lldC - lldELF - LLVMCore - LLVMEVMCodeGen - LLVMEVMInfo - LLVMEVMDesc - LLVMIRReader - LLVMObjCopyC - LLVMTarget -) diff --git a/lld/unittests/EVM/LLDTest.cpp b/lld/unittests/EVM/LLDTest.cpp deleted file mode 100644 index 78d71e2969db..000000000000 --- a/lld/unittests/EVM/LLDTest.cpp +++ /dev/null @@ -1,357 +0,0 @@ -//===---------- llvm/unittests/MC/AssemblerTest.cpp -----------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "lld-c/LLDAsLibraryC.h" -#include "llvm-c/Core.h" -#include "llvm-c/IRReader.h" -#include "llvm-c/ObjCopy.h" -#include "llvm-c/TargetMachine.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/FileUtilities.h" -#include "llvm/Support/KECCAK.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/Path.h" -#include "llvm/Support/raw_ostream.h" -#include "gtest/gtest.h" -#include - -using namespace llvm; - -static std::string expand(const char *Path) { - llvm::SmallString<256> ThisPath; - ThisPath.append(getenv("LLD_SRC_DIR")); - llvm::sys::path::append(ThisPath, "unittests", "EVM", "Inputs", Path); - std::cerr << "Full path: " << ThisPath.str().str() << '\n'; - return ThisPath.str().str(); -} - -class LLDCTest : public testing::Test { - void SetUp() override { - LLVMInitializeEVMTargetInfo(); - LLVMInitializeEVMTarget(); - LLVMInitializeEVMTargetMC(); - LLVMInitializeEVMAsmPrinter(); - - LLVMTargetRef Target = 0; - const char *Triple = "evm"; - char *ErrMsg = 0; - if (LLVMGetTargetFromTriple(Triple, &Target, &ErrMsg)) { - FAIL() << "Failed to create target from the triple (" << Triple - << "): " << ErrMsg; - return; - } - ASSERT_TRUE(Target); - - // Construct a TargetMachine. - TM = - LLVMCreateTargetMachine(Target, Triple, "", "", LLVMCodeGenLevelDefault, - LLVMRelocDefault, LLVMCodeModelDefault); - - Context = LLVMContextCreate(); - } - - void TearDown() override { - LLVMDisposeTargetMachine(TM); - LLVMContextDispose(Context); - } - -public: - LLVMMemoryBufferRef link(LLVMMemoryBufferRef InAssembly, const char *Name, - const char *LinkerSyms[], - const char LinkerSymVals[][20], uint64_t NumSyms) { - char *ErrMsg = nullptr; - LLVMMemoryBufferRef OutAssembly = nullptr; - if (LLVMLinkEVM(InAssembly, &OutAssembly, LinkerSyms, LinkerSymVals, - NumSyms, &ErrMsg)) { - LLVMDisposeMessage(ErrMsg); - exit(1); - } - return OutAssembly; - } - - LLVMMemoryBufferRef addmd(LLVMMemoryBufferRef InObj) { - char *ErrMsg = nullptr; - LLVMMemoryBufferRef Out; - if (LLVMAddMetadata(InObj, MD.data(), MD.size(), &Out, &ErrMsg)) { - LLVMDisposeMessage(ErrMsg); - exit(1); - } - - return Out; - } - - LLVMMemoryBufferRef assemble(uint64_t codeSegment, - const std::vector &Objs, - const std::vector &IDs) { - char *ErrMsg = nullptr; - LLVMMemoryBufferRef OutAssembly = nullptr; - if (LLVMAssembleEVM(codeSegment, Objs.data(), IDs.data(), Objs.size(), - &OutAssembly, &ErrMsg)) { - LLVMDisposeMessage(ErrMsg); - exit(1); - } - EXPECT_TRUE(LLVMIsELFEVM(OutAssembly)); - return OutAssembly; - } - - LLVMMemoryBufferRef compileIR(const char *FileName) { - auto Buf = MemoryBuffer::getFile(expand(FileName), /*IsText=*/false, - /*RequiresNullTerminator=*/false); - if (auto EC = Buf.getError()) - exit(1); - - LLVMMemoryBufferRef IrBuf = llvm::wrap(Buf.get().release()); - char *ErrMsg = nullptr; - LLVMModuleRef Module; - if (LLVMParseIRInContext(Context, IrBuf, &Module, &ErrMsg)) { - LLVMDisposeMessage(ErrMsg); - exit(1); - } - - // Run CodeGen to produce the buffers. - LLVMMemoryBufferRef Result; - if (LLVMTargetMachineEmitToMemoryBuffer(TM, Module, LLVMObjectFile, &ErrMsg, - &Result)) { - LLVMDisposeModule(Module); - LLVMDisposeMessage(ErrMsg); - exit(1); - } - LLVMDisposeModule(Module); - return Result; - } - - LLVMTargetMachineRef TM; - LLVMContextRef Context; - std::array MD = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, - 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, - 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20}; -}; - -TEST_F(LLDCTest, IterativeLinkage) { - LLVMMemoryBufferRef DeployObjMemBuffer = compileIR("deployIr.ll"); - LLVMMemoryBufferRef DeployedObjMemBuffer = compileIR("deployedIr.ll"); - - EXPECT_TRUE(LLVMIsELFEVM(DeployObjMemBuffer)); - EXPECT_TRUE(LLVMIsELFEVM(DeployedObjMemBuffer)); - - const char *LinkerSymbol[2] = {"library_id", "library_id2"}; - const char LinkerSymbolVal[2][20] = { - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {'\x9f', 0x1E, '\xBB', '\xF1', 7, 7, 7, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}; - StringRef SymVal1(LinkerSymbolVal[0], 20); - StringRef SymVal2(LinkerSymbolVal[1], 20); - - const char *InIDs[] = {"Test_26", "Test_26_deployed"}; - std::array InData = {DeployObjMemBuffer, - DeployedObjMemBuffer}; - LLVMMemoryBufferRef InMemBuf = nullptr; - LLVMMemoryBufferRef OutMemBuf = nullptr; - char *ErrMsg = nullptr; - - // Assemble deploy with deployed. - { - if (LLVMAssembleEVM(/*codeSegment=*/0, InData.data(), InIDs, 2, &OutMemBuf, - &ErrMsg)) { - FAIL() << "Failed to assemble:" << ErrMsg; - LLVMDisposeMessage(ErrMsg); - return; - } - EXPECT_TRUE(LLVMIsELFEVM(OutMemBuf)); - std::swap(OutMemBuf, InMemBuf); - } - - // Check load immutable references. - { - char **ImmutableIDs = nullptr; - uint64_t *ImmutableOffsets = nullptr; - uint64_t ImmCount = - LLVMGetImmutablesEVM(InData[1], &ImmutableIDs, &ImmutableOffsets); - EXPECT_TRUE(ImmCount == 1); - EXPECT_TRUE(std::strcmp(ImmutableIDs[0], "id") == 0); - LLVMDisposeImmutablesEVM(ImmutableIDs, ImmutableOffsets, ImmCount); - } - - // No linker symbol definitions are provided, so we have to receive ELF - // object file. - if (LLVMLinkEVM(InMemBuf, &OutMemBuf, nullptr, nullptr, 0, &ErrMsg)) { - FAIL() << "Failed to link:" << ErrMsg; - LLVMDisposeMessage(ErrMsg); - return; - } - - EXPECT_TRUE(LLVMIsELFEVM(OutMemBuf)); - - char **UndefLinkerSyms = nullptr; - uint64_t NumLinkerUndefs = 0; - - LLVMGetUndefinedReferencesEVM(OutMemBuf, &UndefLinkerSyms, &NumLinkerUndefs); - - EXPECT_TRUE(NumLinkerUndefs == 2); - EXPECT_TRUE((std::strcmp(UndefLinkerSyms[0], LinkerSymbol[0]) == 0)); - EXPECT_TRUE((std::strcmp(UndefLinkerSyms[1], LinkerSymbol[1]) == 0)); - LLVMDisposeUndefinedReferences(UndefLinkerSyms, NumLinkerUndefs); - - std::swap(OutMemBuf, InMemBuf); - LLVMDisposeMemoryBuffer(OutMemBuf); - - // The first linker symbol definitions is provided, so we still have - // to receive an ELF object file - if (LLVMLinkEVM(InMemBuf, &OutMemBuf, LinkerSymbol, LinkerSymbolVal, 1, - &ErrMsg)) { - FAIL() << "Failed to link:" << ErrMsg; - LLVMDisposeMessage(ErrMsg); - return; - } - - EXPECT_TRUE(LLVMIsELFEVM(OutMemBuf)); - - LLVMGetUndefinedReferencesEVM(OutMemBuf, &UndefLinkerSyms, &NumLinkerUndefs); - EXPECT_TRUE(NumLinkerUndefs == 1); - EXPECT_TRUE((std::strcmp(UndefLinkerSyms[0], LinkerSymbol[1]) == 0)); - LLVMDisposeUndefinedReferences(UndefLinkerSyms, NumLinkerUndefs); - - std::swap(OutMemBuf, InMemBuf); - LLVMDisposeMemoryBuffer(OutMemBuf); - - // Both linker symbol definitions are provided, so we have to receive - // a bytecode. - if (LLVMLinkEVM(InMemBuf, &OutMemBuf, LinkerSymbol, LinkerSymbolVal, 2, - &ErrMsg)) { - FAIL() << "Failed to link:" << ErrMsg; - LLVMDisposeMessage(ErrMsg); - return; - } - - EXPECT_TRUE(!LLVMIsELFEVM(OutMemBuf)); - - LLVMGetUndefinedReferencesEVM(OutMemBuf, &UndefLinkerSyms, &NumLinkerUndefs); - EXPECT_TRUE(NumLinkerUndefs == 0); - LLVMDisposeUndefinedReferences(UndefLinkerSyms, NumLinkerUndefs); - - StringRef DeployBin(LLVMGetBufferStart(OutMemBuf), - LLVMGetBufferSize(OutMemBuf)); - - EXPECT_TRUE(DeployBin.find(SymVal1) != StringRef::npos); - EXPECT_TRUE(DeployBin.find(SymVal2) != StringRef::npos); - - LLVMDisposeMemoryBuffer(OutMemBuf); - LLVMDisposeMemoryBuffer(InMemBuf); -} - -TEST_F(LLDCTest, Assembly) { - const char *LinkerSymbol[2] = {"unused_library_id", "library_id"}; - const char LinkerSymbolVal[2][20] = { - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9}}; - - LLVMMemoryBufferRef A_deploy_obj = compileIR("A_deploy.ll"); - LLVMMemoryBufferRef A_deployed_obj = compileIR("A_deployed.ll"); - LLVMMemoryBufferRef D_deploy_obj = compileIR("D_deploy.ll"); - LLVMMemoryBufferRef D_deployed_obj = compileIR("D_deployed.ll"); - LLVMMemoryBufferRef R_deploy_obj = compileIR("R_deploy.ll"); - LLVMMemoryBufferRef R_deployed_obj = compileIR("R_deployed.ll"); - - // A assemble. - LLVMMemoryBufferRef A_deployed_obj_md = addmd(A_deployed_obj); - LLVMMemoryBufferRef A_assembly_deployed = - assemble(/*codeSegment=*/1, {A_deployed_obj_md}, {"A_38_deployed"}); - LLVMMemoryBufferRef A_assembly = - assemble(/*codeSegment=*/0, {A_deploy_obj, A_assembly_deployed}, - {"A_38", "A_38_deployed"}); - - // D assemble. - LLVMMemoryBufferRef D_deployed_obj_md = addmd(D_deployed_obj); - LLVMMemoryBufferRef D_assembly_deployed = - assemble(/*codeSegment=*/1, {D_deployed_obj_md}, {"D_38_deployed"}); - LLVMMemoryBufferRef D_assembly = - assemble(/*codeSegment=*/0, {D_deploy_obj, D_assembly_deployed}, - {"D_51", "D_51_deployed"}); - - // R_deployed assemble. - // A_assembly is not required here, but we add it intentionaly to check - // that it will be ignored (the total number of library reference is 3). - LLVMMemoryBufferRef R_deployed_obj_md = addmd(R_deployed_obj); - LLVMMemoryBufferRef R_deployed_assemble = - assemble(/*codeSegment=*/1, - {R_deployed_obj_md, D_assembly, A_assembly, A_assembly_deployed}, - {"R_107_deployed", "D_51", "A_38", "A_38.A_38_deployed"}); - - // R assemble. - LLVMMemoryBufferRef R_assembly = assemble( - /*codeSegment=*/0, - {R_deploy_obj, R_deployed_assemble, A_assembly, A_assembly_deployed}, - {"R_107", "R_107_deployed", "A_38", "A_38.A_38_deployed"}); - - // Get linker symbols offsets - uint64_t *SymOffsets = nullptr; - uint64_t NumOffsets = - LLVMGetSymbolOffsetsEVM(R_assembly, LinkerSymbol[1], &SymOffsets); - EXPECT_TRUE(NumOffsets == 3); - LLVMDisposeSymbolOffsetsEVM(SymOffsets); - - // Linking with no linker symbols. - LLVMMemoryBufferRef TmpAssembly = link(R_assembly, "R", nullptr, nullptr, 0); - EXPECT_TRUE(LLVMIsELFEVM(TmpAssembly)); - - // Linking with unused linker symbol. It has no effect. - LLVMMemoryBufferRef TmpAssembly2 = - link(TmpAssembly, "R", LinkerSymbol, LinkerSymbolVal, 1); - EXPECT_TRUE(LLVMIsELFEVM(TmpAssembly2)); - - // Linking with both linker symbols. The library reference should be resolved - // and resulting object is a final bytecode. - LLVMMemoryBufferRef Bytecode = - link(TmpAssembly2, "R", LinkerSymbol, LinkerSymbolVal, 2); - EXPECT_TRUE(!LLVMIsELFEVM(Bytecode)); - - char **UndefLinkerSyms = nullptr; - uint64_t NumLinkerUndefs = 0; - - LLVMGetUndefinedReferencesEVM(Bytecode, &UndefLinkerSyms, &NumLinkerUndefs); - EXPECT_TRUE(NumLinkerUndefs == 0); - LLVMDisposeUndefinedReferences(UndefLinkerSyms, NumLinkerUndefs); - - StringRef Binary(LLVMGetBufferStart(Bytecode), LLVMGetBufferSize(Bytecode)); - - StringRef LibAddr(LinkerSymbolVal[1], 20); - EXPECT_TRUE(Binary.count(LibAddr) == 3); - EXPECT_TRUE(Binary.count(StringRef(MD.data(), MD.size())) == 5); - - LLVMDisposeMemoryBuffer(A_deploy_obj); - LLVMDisposeMemoryBuffer(A_deployed_obj); - LLVMDisposeMemoryBuffer(D_deploy_obj); - LLVMDisposeMemoryBuffer(D_deployed_obj); - LLVMDisposeMemoryBuffer(R_deploy_obj); - LLVMDisposeMemoryBuffer(R_deployed_obj); - LLVMDisposeMemoryBuffer(TmpAssembly); - LLVMDisposeMemoryBuffer(TmpAssembly2); - LLVMDisposeMemoryBuffer(Bytecode); -} - -TEST_F(LLDCTest, UndefNonRefSymbols) { - LLVMMemoryBufferRef DeployObj = compileIR("undefDeployIr.ll"); - LLVMMemoryBufferRef DeployedObj = compileIR("undefDeployedIr.ll"); - - EXPECT_TRUE(LLVMIsELFEVM(DeployObj)); - EXPECT_TRUE(LLVMIsELFEVM(DeployedObj)); - - const std::array InObjs = {DeployObj, DeployedObj}; - const std::array IDs = {"Test_26", "Test_26_deployed"}; - LLVMMemoryBufferRef OutObj = nullptr; - char *ErrMsg = nullptr; - EXPECT_TRUE(LLVMAssembleEVM(/*codeSegment=*/0, InObjs.data(), IDs.data(), - IDs.size(), &OutObj, &ErrMsg)); - EXPECT_TRUE(StringRef(ErrMsg).contains("non-ref undefined symbol:")); - LLVMDisposeMessage(ErrMsg); - - LLVMDisposeMemoryBuffer(DeployObj); - LLVMDisposeMemoryBuffer(DeployedObj); -} diff --git a/llvm/cmake/modules/LLVM-Build.cmake b/llvm/cmake/modules/LLVM-Build.cmake index 5e401fb4987e..525186cbbb19 100644 --- a/llvm/cmake/modules/LLVM-Build.cmake +++ b/llvm/cmake/modules/LLVM-Build.cmake @@ -17,8 +17,6 @@ function(LLVMBuildGenerateCFragment) endforeach() list(LENGTH llvmbuild_components llvmbuild_components_size) - # EVM local begin - math(EXPR components_size "${llvmbuild_components_size} + 4") file(WRITE ${ARG_OUTPUT} " struct AvailableComponent { @@ -32,10 +30,9 @@ function(LLVMBuildGenerateCFragment) bool IsInstalled; /// The list of libraries required when linking this component. - const char *RequiredLibraries[${components_size}]; - } AvailableComponents[${components_size}] = { + const char *RequiredLibraries[${llvmbuild_components_size}]; + } AvailableComponents[${llvmbuild_components_size}] = { ") - # EVM local end foreach(llvmbuild_component ${llvmbuild_components}) if(llvmbuild_component STREQUAL "all") @@ -76,12 +73,6 @@ function(LLVMBuildGenerateCFragment) foreach(llvmbuild_centry ${llvmbuild_centries}) file(APPEND ${ARG_OUTPUT} "${llvmbuild_centry}") endforeach() - # EVM local begin - file(APPEND ${ARG_OUTPUT} "{ \"lldcommon\", \"lldCommon\", true, {} },\n") - file(APPEND ${ARG_OUTPUT} "{ \"lldelf\", \"lldELF\", true, {} },\n") - file(APPEND ${ARG_OUTPUT} "{ \"lldc\", \"lldC\", true, {} },\n") - file(APPEND ${ARG_OUTPUT} "{ \"lld\", nullptr, true, {\"lldcommon\", \"lldelf\", \"lldc\"} },\n") - # EVM local end file(APPEND ${ARG_OUTPUT} "};") endfunction() diff --git a/llvm/include/llvm-c/EVMLinker.h b/llvm/include/llvm-c/EVMLinker.h new file mode 100644 index 000000000000..43d02a16608a --- /dev/null +++ b/llvm/include/llvm-c/EVMLinker.h @@ -0,0 +1,137 @@ +//===------- EVMLinker.h - EVM linker public C interface -------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This header declares the EVM C interfaces: +// - 'LLVMAssembleEVM' +// Lays out a contract's objects and resolves the symbols the layout defines +// +// - 'LLVMLinkEVM' +// Resolves the library addresses and produces the bytecode +// +// - 'LLVMIsELFEVM' +// Checks if the given memory buffer contains an ELF EVM object file +// +// - 'LLVMGetUndefinedReferencesEVM' +// Returns an array of undefined linker symbols +// +// - 'LLVMDisposeUndefinedReferencesEVM' +// Disposes an array returned by the 'LLVMGetUndefinedReferencesEVM' +// +// These functions use a notion of the 'Linker Symbol': a library whose +// 20-byte address is unknown at compile time. For a library named 'name', +// the LLVM MC layer emits one undefined ELF symbol +// +// '__linker_symbol__$keccak256(name)$__' +// +// with one R_EVM_DATA_160 relocation per use, which covers the 20 bytes of +// a PUSH20 immediate. The MC layer also sets the +// ELF::STO_EVM_REFERENCE_SYMBOL flag in the 'st_other' field of the symbol +// table entry to distinguish such symbols from all others. +// In EVM, only these symbols are allowed to be undefined in an object +// code. All other cases must be treated as unreachable and denote a bug +// in the FE/LLVM codegen/Linker implementation. +// 'Linker Symbols' are resolved, i.e they receive their final 20-byte +// values, at the linkage stage when calling LLVMLinkEVM. +// For this, the 'LLVMLinkEVM' has parameters: +// - \p linkerSymbolNames, array of null-terminated linker symbol names +// - \p linkerSymbolValues, array of symbol values +// +// For example, if linkerSymbolNames[0] points to a string 'name', the 20-byte +// linkerSymbolValues[0] value is written over every relocation of the symbol +// '__linker_symbol__$keccak256(name)$__'. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_C_EVMLINKER_H +#define LLVM_C_EVMLINKER_H + +#include "llvm-c/ExternC.h" +#include "llvm-c/Types.h" +#include "llvm-c/Visibility.h" + +LLVM_C_EXTERN_C_BEGIN + +// Currently, the size of a linker symbol is limited to 20 bytes, as its the +// only usage is to represent Ethereum addresses which are of 160 bit width. +#define LLVM_EVM_LINKER_SYMBOL_SIZE 20 + +/** Returns true if the \p inBuffer contains an EVM ELF object file. */ +LLVM_C_ABI LLVMBool LLVMIsELFEVM(LLVMMemoryBufferRef inBuffer); + +/** + * Performs the following steps, which are based on Ethereum's + * Assembly::assemble() logic: + * - Concatenates the .text sections of input ELF files referenced + * by __dataoffset* symbols from the first file, and appends + * \p metadata to them. + * - Resolves undefined __dataoffset* and __datasize* symbols. + * - Gathers the undefined linker symbols (library references) of + * the files the layout includes. + * - Ensures that the first file does not load and set + * immutables simultaneously. + * + * Returns the resulting relocatable object file. Every failure is a bug in + * the compiler or in the driver, so it aborts instead of returning. + * + * Whether an object holds deploy or runtime code is read from its ELF + * header: the compiler sets EF_EVM_RUNTIME in 'e_flags' for the runtime + * code segment, from the 'evm.runtime' module flag. + * + * \p inBuffers - relocatable ELF files to be assembled, the first of which + * is the object being assembled + * \p inBuffersIDs - their IDs + * \p metadata - the bytes that end the code, \p metadataSize of them, + * which is 0 when there are none */ +LLVM_C_ABI LLVMMemoryBufferRef LLVMAssembleEVM( + const LLVMMemoryBufferRef inBuffers[], const char *const inBuffersIDs[], + uint64_t inBuffersNum, const char *metadata, uint64_t metadataSize); + +/** Returns an array of undefined linker symbol names + * from the ELF object provided in \p inBuffer. */ +LLVM_C_ABI void LLVMGetUndefinedReferencesEVM(LLVMMemoryBufferRef inBuffer, + char ***linkerSymbols, + uint64_t *numLinkerSymbols); + +/** Disposes of the array of symbols returned by the + * 'LLVMGetUndefinedReferencesEVM' function. */ +LLVM_C_ABI void LLVMDisposeUndefinedReferencesEVM(char *symbolNames[], + uint64_t numSymbols); + +/** Resolves undefined linker symbols in the ELF object file \p inBuffer. + * Returns the ELF object file if any linker symbols remain unresolved; + * otherwise, returns the bytecode. Every failure is a bug in the compiler + * or in the driver, so it aborts instead of returning. */ +LLVM_C_ABI LLVMMemoryBufferRef +LLVMLinkEVM(LLVMMemoryBufferRef inBuffer, const char *const *linkerSymbolNames, + const char linkerSymbolValues[][LLVM_EVM_LINKER_SYMBOL_SIZE], + uint64_t numLinkerSymbols); + +/** Returns the immutable symbol names and their offsets from the ELF + * object file provided in \p inBuffer. */ +LLVM_C_ABI uint64_t LLVMGetImmutablesEVM(LLVMMemoryBufferRef inBuffer, + char ***immutableIDs, + uint64_t **immutableOffsets); + +/** Returns an array of offsets of the linker symbol relocations form the + * ELF object file provided in \p inBuffer. */ +LLVM_C_ABI uint64_t LLVMGetSymbolOffsetsEVM(LLVMMemoryBufferRef inBuffer, + const char *symbolName, + uint64_t **symbolOffsets); + +/** Disposes of the immutable names and their offsets returned by + * 'LLVMGetImmutablesEVM'. */ +LLVM_C_ABI void LLVMDisposeImmutablesEVM(char **immutableIDs, + uint64_t *immutableOffsets, + uint64_t numOfImmutables); + +/** Releases the array of linker symbol offsets. */ +LLVM_C_ABI void LLVMDisposeSymbolOffsetsEVM(uint64_t *offsets); + +LLVM_C_EXTERN_C_END + +#endif // LLVM_C_EVMLINKER_H diff --git a/llvm/include/llvm-c/ObjCopy.h b/llvm/include/llvm-c/ObjCopy.h deleted file mode 100644 index a48565cbcb0f..000000000000 --- a/llvm/include/llvm-c/ObjCopy.h +++ /dev/null @@ -1,31 +0,0 @@ -//==--------- ObjCopy.h - ObjCopy Public C Interface ----------*- C++ -*----==// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This header declares the C interface for the llvm-objcopy functionality. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_C_OBJCOPY_H -#define LLVM_C_OBJCOPY_H - -#include "llvm-c/ExternC.h" -#include "llvm-c/Types.h" - -LLVM_C_EXTERN_C_BEGIN - -/** Creates new section '.metadata' in the input object file \p InBuffer - * and puts there the data pointed to by the \p MetadataPtr. The result is - * returned via \p OutBuffer. In case of an error the function returns 'true' - * and an error message is passes via \p ErrorMessage. The message should be - * disposed by LLVMDisposeMessage. **/ -LLVMBool LLVMAddMetadata(LLVMMemoryBufferRef InBuffer, const char *MetadataPtr, - uint64_t MetadataSize, LLVMMemoryBufferRef *OutBuffer, - char **ErrorMessage); -LLVM_C_EXTERN_C_END - -#endif // LLVM_C_OBJCOPY_H diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h index 7c9bd5b4fac8..3bae9dd46371 100644 --- a/llvm/include/llvm/BinaryFormat/ELF.h +++ b/llvm/include/llvm/BinaryFormat/ELF.h @@ -1102,6 +1102,13 @@ enum { #include "ELFRelocs/Xtensa.def" }; +// EVM specific e_flags. +enum : unsigned { + // The object holds the runtime code segment of a contract. Without the + // flag it holds the deploy code segment. + EF_EVM_RUNTIME = 0x1, +}; + // Special values for the st_other field in the symbol table entry for EVM. enum { // Symbol denotes a sub symbol of a wide reference. diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/EVM.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/EVM.def index 003b02562b86..dd458c3531b1 100644 --- a/llvm/include/llvm/BinaryFormat/ELFRelocs/EVM.def +++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/EVM.def @@ -5,3 +5,4 @@ ELF_RELOC(R_EVM_NONE, 0) ELF_RELOC(R_EVM_DATA, 1) +ELF_RELOC(R_EVM_DATA_160, 2) diff --git a/llvm/include/llvm/Support/FileOutputBuffer.h b/llvm/include/llvm/Support/FileOutputBuffer.h index ae7bd85ca847..20999f408e32 100644 --- a/llvm/include/llvm/Support/FileOutputBuffer.h +++ b/llvm/include/llvm/Support/FileOutputBuffer.h @@ -48,17 +48,6 @@ class FileOutputBuffer { LLVM_ABI static Expected> create(StringRef FilePath, size_t Size, unsigned Flags = 0); - // EVM local begin - /// Factory method to create an OutputBuffer object which manages a read/write - /// buffer of the specified size. When committed, the buffer will be written - /// to the stream p\ Out. - /// Please note, even though the returned buffer type is derived from - /// FileOutputBuffer, the functionality related to file management - /// is not used and completely ignored. - static Expected> - create(size_t Size, raw_pwrite_stream &Out); - // EVM local end - /// Returns a pointer to the start of the buffer. virtual uint8_t *getBufferStart() const = 0; diff --git a/llvm/lib/ObjCopy/CMakeLists.txt b/llvm/lib/ObjCopy/CMakeLists.txt index ce9e6faaeb0d..5079ff23ef0b 100644 --- a/llvm/lib/ObjCopy/CMakeLists.txt +++ b/llvm/lib/ObjCopy/CMakeLists.txt @@ -82,4 +82,3 @@ add_llvm_component_library(LLVMObjCopy MC ) -add_subdirectory(ObjCopyC) diff --git a/llvm/lib/ObjCopy/ObjCopyC/CMakeLists.txt b/llvm/lib/ObjCopy/ObjCopyC/CMakeLists.txt deleted file mode 100644 index 271f8e40c26f..000000000000 --- a/llvm/lib/ObjCopy/ObjCopyC/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -add_llvm_component_library(LLVMObjCopyC - ObjCopyC.cpp - - DEPENDS - intrinsics_gen - - LINK_COMPONENTS - BinaryFormat - Core - MC - ObjCopy - Object - Support -) diff --git a/llvm/lib/ObjCopy/ObjCopyC/ObjCopyC.cpp b/llvm/lib/ObjCopy/ObjCopyC/ObjCopyC.cpp deleted file mode 100644 index 20a0dae6d18e..000000000000 --- a/llvm/lib/ObjCopy/ObjCopyC/ObjCopyC.cpp +++ /dev/null @@ -1,96 +0,0 @@ -//===-------- ObjCopyC.cpp - ObjCopy Public C Interface ---------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file declares the C interface for the llv-objcopy functionality. -// -//===----------------------------------------------------------------------===// - -#include "llvm-c/Core.h" -#include "llvm-c/ObjCopy.h" -#include "llvm-c/Object.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ObjCopy/ConfigManager.h" -#include "llvm/ObjCopy/ObjCopy.h" -#include "llvm/Object/Binary.h" -#include "llvm/Object/ObjectFile.h" -#include "llvm/Support/MemoryBuffer.h" - -#include -#include - -using namespace llvm; -using namespace object; -using namespace objcopy; - -#ifndef NDEBUG -static void checkSectionData(ObjectFile &File, StringRef SectionName, - StringRef SectionData) { - for (const object::SectionRef &Sec : File.sections()) { - StringRef CurSecName = cantFail(Sec.getName()); - if (CurSecName == SectionName) { - StringRef CurSecData = cantFail(Sec.getContents()); - assert(Sec.getSize() == SectionData.size()); - assert(memcmp(CurSecData.data(), SectionData.data(), - SectionData.size()) == 0); - } - } -} -#endif // NDEBUG - -LLVMBool LLVMAddMetadata(LLVMMemoryBufferRef InBuffer, const char *MetadataPtr, - uint64_t MetadataSize, LLVMMemoryBufferRef *OutBuffer, - char **ErrorMessage) { - if (!MetadataSize) { - *OutBuffer = nullptr; - return false; - } - - StringRef MDSectionName = ".metadata"; - - std::unique_ptr MDSectionBuffer = MemoryBuffer::getMemBuffer( - StringRef(MetadataPtr, MetadataSize), MDSectionName, false); - - Expected> InObjOrErr( - createBinary(unwrap(InBuffer)->getMemBufferRef())); - if (!InObjOrErr) - llvm_unreachable("Cannot create Binary object from the memory buffer"); - - ConfigManager Config; - Config.Common.AddSection.emplace_back(MDSectionName, - std::move(MDSectionBuffer)); - - // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) - SectionFlagsUpdate SFU = {MDSectionName, SecReadonly | SecNoload}; - [[maybe_unused]] auto It = - Config.Common.SetSectionFlags.try_emplace(SFU.Name, SFU); - assert(It.second); - - SmallString<0> BufferString; - raw_svector_ostream OutStream(BufferString); - if (Error Err = objcopy::executeObjcopyOnBinary(Config, *InObjOrErr.get(), - OutStream)) { - *ErrorMessage = strdup(toString(std::move(Err)).c_str()); - return true; - } - - // Create output buffer and copy there the object code. - llvm::StringRef Data = BufferString.str(); - *OutBuffer = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), - Data.size(), "result"); - -#ifndef NDEBUG - // Check that copied file has the new section. - Expected> Result = - createBinary(llvm::unwrap(*OutBuffer)->getMemBufferRef()); - assert(Result && (*Result)->isObject()); - checkSectionData(*static_cast((*Result).get()), MDSectionName, - StringRef(MetadataPtr, MetadataSize)); -#endif // NDEBUG - - return false; -} diff --git a/llvm/lib/Support/FileOutputBuffer.cpp b/llvm/lib/Support/FileOutputBuffer.cpp index f6300f499112..c8b035986fb5 100644 --- a/llvm/lib/Support/FileOutputBuffer.cpp +++ b/llvm/lib/Support/FileOutputBuffer.cpp @@ -112,43 +112,6 @@ class InMemoryBuffer : public FileOutputBuffer { size_t BufferSize; unsigned Mode; }; - -// EVM local begin -// A FileOutputBuffer which keeps data in memory and writes to the final -// output stream on commit(). -// Note, the file management functionality, derived from FileOutputBuffer, -// is not supposed to be used. -class InMemoryStreamBuffer : public FileOutputBuffer { -public: - InMemoryStreamBuffer(MemoryBlock Buf, std::size_t BufSize, - raw_pwrite_stream &Out) - : FileOutputBuffer("<<"), Buffer(Buf), BufferSize(BufSize), - OutStream(Out) {} - - uint8_t *getBufferStart() const override { - return static_cast(Buffer.base()); - } - - uint8_t *getBufferEnd() const override { - return static_cast(Buffer.base()) + BufferSize; - } - - size_t getBufferSize() const override { return BufferSize; } - - Error commit() override { - OutStream << StringRef(static_cast(Buffer.base()), - BufferSize); - OutStream.flush(); - return Error::success(); - } - -private: - // Buffer may actually contain a larger memory block than BufferSize - OwningMemoryBlock Buffer; - size_t BufferSize; - raw_pwrite_stream &OutStream; -}; -// EVM local end } // namespace static Expected> @@ -191,19 +154,6 @@ createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) { std::move(MappedFile)); } -// EVM local begin -// Create an instance of InMemoryStreamBuffer. -Expected> -FileOutputBuffer::create(size_t Size, raw_pwrite_stream &Out) { - std::error_code EC; - MemoryBlock MB = Memory::allocateMappedMemory( - Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC); - if (EC) - return errorCodeToError(EC); - return std::make_unique(MB, Size, Out); -} -// EVM local end - // Create an instance of FileOutputBuffer. Expected> FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) { diff --git a/llvm/lib/Target/EVM/CMakeLists.txt b/llvm/lib/Target/EVM/CMakeLists.txt index 9505e3f328df..257fe166940a 100644 --- a/llvm/lib/Target/EVM/CMakeLists.txt +++ b/llvm/lib/Target/EVM/CMakeLists.txt @@ -106,5 +106,6 @@ add_llvm_target(EVMCodeGen ) add_subdirectory(Disassembler) +add_subdirectory(Linker) add_subdirectory(MCTargetDesc) add_subdirectory(TargetInfo) diff --git a/llvm/lib/Target/EVM/EVMAsmPrinter.cpp b/llvm/lib/Target/EVM/EVMAsmPrinter.cpp index c6df64c5ed12..cc654e32cfa3 100644 --- a/llvm/lib/Target/EVM/EVMAsmPrinter.cpp +++ b/llvm/lib/Target/EVM/EVMAsmPrinter.cpp @@ -27,11 +27,14 @@ #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Module.h" #include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCELFObjectWriter.h" #include "llvm/MC/MCInst.h" #include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" +#include "llvm/MC/MCSymbolELF.h" #include "llvm/MC/TargetRegistry.h" using namespace llvm; @@ -335,37 +338,29 @@ void EVMAsmPrinter::emitAssemblySymbol(const MachineInstr *MI) { // -> // PUSH20_S @"__linker_symbol__$KECCAK256(BaseSymbol)$__" // -// .section ".symbol_name__$KECCAK256(BaseName)$__","S",@progbits +// .section +// ".symbol_name__linker_symbol__$KECCAK256(BaseName)$__","S",@progbits // .ascii "~ \".%%^ [];,<.>? .sol:GreaterHelper" void EVMAsmPrinter::emitWideRelocatableSymbol(const MachineInstr *MI) { - constexpr unsigned LinkerSymbolSize = 20; MCSymbol *BaseSymbol = MI->getOperand(0).getMCSymbol(); StringRef BaseSymbolName = BaseSymbol->getName(); - std::string BaseSymbolNameHash = EVM::getLinkerSymbolHash(BaseSymbolName); - std::string SymbolName = EVM::getLinkerSymbolName(BaseSymbolNameHash); + std::string SymbolName = EVM::getLinkerSymbolName(BaseSymbolName); + + // This represents a library address symbol which is 20-bytes wide. It can + // only be defined by the linker, which the st_other flag says. The MC layer + // emits a single 20-byte relocation for it. + auto *Symbol = cast(OutContext.getOrCreateSymbol(SymbolName)); + Symbol->setOther(ELF::STO_EVM_REFERENCE_SYMBOL); MCInst MCI; - // This represents a library address symbol which is 20-bytes wide. MCI.setOpcode(EVM::PUSH20_S); - auto *Symbol = OutContext.getOrCreateSymbol(BaseSymbolNameHash); MCOperand MCOp = MCOperand::createExpr( MCSymbolRefExpr::create(Symbol, EVM::S_DATA, OutContext)); MCI.addOperand(MCOp); + EmitToStreamer(*OutStreamer, MCI); - if (!WideRelocSymbolsSet.contains(SymbolName)) { - for (unsigned Idx = 0; Idx < LinkerSymbolSize / sizeof(uint32_t); ++Idx) { - std::string SubSymName = EVM::getSymbolIndexedName(SymbolName, Idx); - if (OutContext.lookupSymbol(SubSymName)) - report_fatal_error(Twine("MC: duplicating reference sub-symbol ") + - SubSymName); - } - } - - auto *TS = static_cast(OutStreamer->getTargetStreamer()); - TS->emitWideRelocatableSymbol(MCI, SymbolName, LinkerSymbolSize); - - // The linker symbol and the related section already exist, so just exit. + // The related section already exists, so just exit. if (WideRelocSymbolsSet.contains(SymbolName)) return; @@ -459,6 +454,18 @@ void EVMAsmPrinter::emitEndOfAsmFile(Module &) { void EVMAsmPrinter::emitStartOfAsmFile(Module &M) { createDataSectionBuffer(M); DataSectionSymbol = OutContext.getOrCreateSymbol("code_data_section"); + + // Lower the code segment kind the frontend recorded in the 'evm.runtime' + // module flag into the ELF header, so the linker reads it from the object. + // Only an object streamer has an assembler, and on EVM its writer is + // always the ELF one. + const auto *RuntimeFlag = + mdconst::extract_or_null(M.getModuleFlag("evm.runtime")); + MCAssembler *Assembler = OutStreamer->getAssemblerPtr(); + if (RuntimeFlag && !RuntimeFlag->isZero() && Assembler) { + auto &W = static_cast(Assembler->getWriter()); + W.setELFHeaderEFlags(W.getELFHeaderEFlags() | ELF::EF_EVM_RUNTIME); + } } void EVMAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) { diff --git a/llvm/lib/Target/EVM/Linker/CMakeLists.txt b/llvm/lib/Target/EVM/Linker/CMakeLists.txt new file mode 100644 index 000000000000..336194c6e14e --- /dev/null +++ b/llvm/lib/Target/EVM/Linker/CMakeLists.txt @@ -0,0 +1,13 @@ +add_llvm_component_library(LLVMEVMLinker + EVMLinker.cpp + EVMLinkerQueries.cpp + + LINK_COMPONENTS + EVMDesc + MC + Object + Support + + ADD_TO_COMPONENT + EVM + ) diff --git a/llvm/lib/Target/EVM/Linker/EVMLinker.cpp b/llvm/lib/Target/EVM/Linker/EVMLinker.cpp new file mode 100644 index 000000000000..4b203dd72fbb --- /dev/null +++ b/llvm/lib/Target/EVM/Linker/EVMLinker.cpp @@ -0,0 +1,677 @@ +//===--- EVMLinker.cpp - EVM assembling and linking -----------*- C++ -*---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Implements 'LLVMAssembleEVM' and 'LLVMLinkEVM'. +// +// Every input is a relocatable ELF32 big-endian object holding one '.text' +// section. There are two relocation types: R_EVM_DATA, which writes a 32-bit +// big-endian absolute value, and R_EVM_DATA_160, which writes a 20-byte +// library address. The output is the concatenation of those sections at +// address zero, followed by the contract's metadata. +// +// Assembling lays the sections out, defines the '__dataoffset' and '__datasize' +// symbols the layout determines, and applies every relocation it can resolve. +// A library address is not known then, so the relocation against its +// '__linker_symbol' is carried into the output together with the section +// naming the library. Linking substitutes the addresses it is given and +// produces the bytecode once none is missing. +// +// The output object format +// ------------------------ +// Every object written here is a relocatable ELF32 big-endian file (ET_REL, +// EM_EVM) holding these sections, in this order: +// +// [0] null The all-zero section the ELF reference requires at +// index 0 (SHN_UNDEF). +// [1] .text The bytecode: the laid-out code of every included +// object, followed by the metadata. Unresolved +// library addresses are zero bytes in it. +// [2] .rela.text Present only while library addresses remain +// unresolved. One R_EVM_DATA_160 record per use: +// 'r_offset' names the 20 address bytes in '.text' +// and 'r_info' names the library's symbol. The addend +// is always zero. +// ... .symbol_name One per unresolved library. The section name +// carries the mangled '__linker_symbol__$hash$__' +// and the contents the library's source name, which +// 'LLVMGetUndefinedReferencesEVM' reports. +// ... .symtab The null symbol, then one local symbol per +// immutable load site ('__load_immutable__.', +// 'st_value' is the PUSH32 offset in '.text'), then +// one global undefined symbol per unresolved library, +// sorted by name and marked with +// STO_EVM_REFERENCE_SYMBOL in 'st_other'. +// ... .strtab The symbol names. +// ... .shstrtab The section names, referenced by 'e_shstrndx'. +// +// The relations between them are the standard ELF ones. '.rela.text' links +// to '.symtab' ('sh_link') and applies to '.text' ('sh_info'). '.symtab' +// links to '.strtab' and counts its locals in 'sh_info'. The immutable +// symbols point into '.text' through 'st_shndx', and the library symbols are +// SHN_UNDEF because only linking defines them. +// +// The layout is: the ELF header at offset zero, the section contents in +// header order, each at the next offset aligned to its 'sh_addralign' (4 for +// '.rela.text' and '.symtab', 1 for the rest), and the section header table +// at 'e_shoff', word-aligned, at the end of the file. +// +// Once the last library address resolves, the ELF wrapper disappears: the +// output of 'LLVMLinkEVM' is then the raw '.text' bytes. +// +//===----------------------------------------------------------------------===// + +#include "llvm-c/EVMLinker.h" +#include "EVMLinkerUtils.h" +#include "MCTargetDesc/EVMMCTargetDesc.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/StringTableBuilder.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ELFTypes.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" + +#include +#include +#include + +using namespace llvm; +using namespace llvm::object; +using namespace llvm::support::endian; + +namespace { + +/// The bytes a resolved relocation writes, big-endian. The buffer fits the +/// widest kind, a 20-byte library address. +using RelocValue = std::array; + +/// A 20-byte library address, big-endian. +using LibraryAddress = RelocValue; + +/// A relocation that resolved, to be written into the output '.text': +/// the first \p Size bytes of \p Value, big-endian. It holds either a +/// 20-byte library address or a 4-byte data offset or size. +struct Reloc { + uint64_t Offset; + uint64_t Size; + RelocValue Value; + + Reloc(uint64_t Offset, uint64_t Size, const RelocValue &Value) + : Offset(Offset), Size(Size), Value(Value) {} +}; + +/// A library address relocation that did not resolve, to be carried into +/// the output's '.rela.text'. +struct LibraryReloc { + /// Offset in the output '.text'. + uint64_t Offset; + /// The '__linker_symbol' it refers to. + std::string Sym; + + LibraryReloc(uint64_t Offset, StringRef Sym) + : Offset(Offset), Sym(Sym.str()) {} +}; + +/// A '.symbol_name...' section, whose contents is the library's source name. +struct NameSection { + StringRef Name; + StringRef Data; + + NameSection(StringRef Name, StringRef Data) : Name(Name), Data(Data) {} +}; + +/// An immutable load site of the output object: a local '.text' symbol. +struct Immutable { + std::string Name; + uint32_t Value; + + Immutable(StringRef Name, uint32_t Value) : Name(Name.str()), Value(Value) {} +}; + +/// A part of the output '.text', copied from an input. +struct OutTextSegment { + StringRef Data; + uint64_t Offset; + + OutTextSegment(StringRef Data, uint64_t Offset) + : Data(Data), Offset(Offset) {} +}; + +/// Everything the output is made of. +struct Output { + SmallVector Segments; + uint64_t TextSize = 0; + uint32_t EFlags = 0; + SmallVector Relocs; + SmallVector LibraryRelocs; + SmallVector NameSections; + SmallVector Immutables; +}; + +/// One input object, read in place out of the caller's buffer. +struct Input { + std::unique_ptr Bin; + const ObjectFile *File = nullptr; + std::string IdHash; + /// '.text' contents, empty when absent. + StringRef Text; + /// Where '.text' lands in the output. + uint64_t OutTextOffset = 0; + /// Whether the layout includes this object. + bool Included = false; +}; + +} // namespace + +/// Returns the relocation section targeting '.text', or the end iterator. +static section_iterator findTextRelocations(const ObjectFile &File) { + for (section_iterator It = File.section_begin(), End = File.section_end(); + It != End; ++It) { + if (It->relocations().empty()) + continue; + Expected Target = It->getRelocatedSection(); + if (!Target) { + consumeError(Target.takeError()); + continue; + } + if (*Target != End && cantFail((*Target)->getName()) == ".text") + return It; + } + return File.section_end(); +} + +/// Returns true when \p File holds a runtime code segment, which the +/// compiler marks in the ELF header. +static bool isRuntimeObject(const ObjectFile &File) { + return cast(&File)->getPlatformFlags() & + ELF::EF_EVM_RUNTIME; +} + +/// Records a 20-byte library address in \p LibraryAddresses, keyed by the +/// '__linker_symbol' name the MC layer emits the relocation against. +static void defineLinkerSymbol(StringMap &LibraryAddresses, + StringRef Name, const char *Value) { + LibraryAddress Addr; + std::memcpy(Addr.data(), Value, LLVM_EVM_LINKER_SYMBOL_SIZE); + LibraryAddresses[EVM::getLinkerSymbolName(Name)] = Addr; +} + +/// Resolves a library address relocation: 20 bytes at \p Where, written when +/// \p LibraryAddresses defines the symbol, carried into the output when it +/// does not or when it is null. +static void +resolveLibraryAddress(const StringMap *LibraryAddresses, + const SymbolRef &Sym, const RelocationRef &Rel, + uint64_t Where, Output &Out) { + assert(EVM::isReferenceSymbol(Sym) && + "library address relocation against a non-ref symbol"); + assert(cantFail(ELFRelocationRef(Rel).getAddend()) == 0 && + "library address relocation with an addend"); + const StringRef SymName = cantFail(Sym.getName()); + if (LibraryAddresses) { + const auto It = LibraryAddresses->find(SymName); + if (It != LibraryAddresses->end()) { + Out.Relocs.emplace_back(Where, LLVM_EVM_LINKER_SYMBOL_SIZE, It->second); + return; + } + } + Out.LibraryRelocs.emplace_back(Where, SymName); +} + +/// Resolves a '__dataoffset' or '__datasize' relocation: a 32-bit value +/// \p SymValues defined from the layout, written at \p Where. A miss means +/// the code references an object the driver did not pass, which is a bug in +/// the driver. +static void resolveDataSymbol(const StringMap &SymValues, + const SymbolRef &Sym, const RelocationRef &Rel, + uint64_t Where, Output &Out) { + const StringRef SymName = cantFail(Sym.getName()); + const auto It = SymValues.find(SymName); + if (It == SymValues.end()) + report_fatal_error(Twine("non-ref undefined symbol: ") + SymName); + assert(cantFail(ELFRelocationRef(Rel).getAddend()) == 0 && + "data relocation with an addend"); + RelocValue Value; + write32be(Value.data(), It->second); + Out.Relocs.emplace_back(Where, sizeof(uint32_t), Value); +} + +/// Resolves the relocations of \p In, placed at its offset in the output, +/// against \p SymValues or \p LibraryAddresses, which is null at assembly +/// time, when no library address is known yet. +static void resolve(const Input &In, const StringMap &SymValues, + const StringMap *LibraryAddresses, + Output &Out) { + const section_iterator Rela = findTextRelocations(*In.File); + if (Rela == In.File->section_end()) + return; + + for (const RelocationRef &Rel : Rela->relocations()) { + const uint64_t Type = Rel.getType(); + assert((Type == ELF::R_EVM_DATA || Type == ELF::R_EVM_DATA_160) && + "unexpected relocation type"); + const symbol_iterator Sym = Rel.getSymbol(); + assert(Sym != In.File->symbol_end() && "relocation without a symbol"); + assert((cantFail(Sym->getFlags()) & SymbolRef::SF_Undefined) && + "relocation against a defined symbol"); + const uint64_t Size = Type == ELF::R_EVM_DATA_160 + ? LLVM_EVM_LINKER_SYMBOL_SIZE + : sizeof(uint32_t); + // The patched bytes must lie inside the object's own code. The sum + // cannot overflow: 'r_offset' is 32 bits and 'Size' is at most 20. + if (Rel.getOffset() + Size > In.Text.size()) + report_fatal_error("relocation out of range"); + const uint64_t Where = In.OutTextOffset + Rel.getOffset(); + + if (Type == ELF::R_EVM_DATA_160) + resolveLibraryAddress(LibraryAddresses, *Sym, Rel, Where, Out); + else + resolveDataSymbol(SymValues, *Sym, Rel, Where, Out); + } +} + +/// Collects the '.symbol_name...' sections of \p File into \p Out, skipping +/// those already there. Several objects referring to the same library carry +/// the same section, and the output holds one of each. +static void collectNameSections(const ObjectFile &File, StringSet<> &Seen, + Output &Out) { + for (const SectionRef &Sec : File.sections()) { + const StringRef Name = cantFail(Sec.getName()); + if (!EVM::isSymbolSectionName(Name) || !Seen.insert(Name).second) + continue; + Out.NameSections.emplace_back(Name, cantFail(Sec.getContents())); + } +} + +/// Collects the immutable symbols of \p File into \p Out. +static void collectImmutables(const ObjectFile &File, Output &Out) { + for (const SymbolRef &Sym : File.symbols()) { + const StringRef Name = cantFail(Sym.getName()); + if (EVM::isLoadImmutableSymbolName(Name)) + Out.Immutables.emplace_back( + Name, static_cast(cantFail(Sym.getValue()))); + } +} + +/// Copies the segments of the output '.text' to \p Text and writes the +/// resolved relocations over them. +static void writeText(char *Text, const Output &Out) { + for (const OutTextSegment &S : Out.Segments) { + if (!S.Data.empty()) + std::memcpy(Text + S.Offset, S.Data.data(), S.Data.size()); + } + for (const Reloc &R : Out.Relocs) + std::memcpy(Text + R.Offset, R.Value.data(), R.Size); +} + +/// Writes the bytecode of \p Out, which has nothing left unresolved. +static std::unique_ptr writeBytecode(const Output &Out, + StringRef Name) { + std::unique_ptr Buf = + WritableMemoryBuffer::getNewUninitMemBuffer(Out.TextSize, Name); + writeText(Buf->getBufferStart(), Out); + return Buf; +} + +/// Writes \p Out as a relocatable ELF32 big-endian EVM object: '.text', its +/// relocations, the sections naming the libraries they refer to, the symbol +/// table and the string tables. The records are the endian-aware structs of +/// ELFTypes.h, so a plain byte write of them is big-endian. +/// +/// The symbol table holds the immutable symbols, which are local, and then one +/// undefined symbol per library still to be resolved, which is global. +/// +/// The file has the shape the ELF reference defines for a relocatable object. +static std::unique_ptr writeObject(const Output &Out, + StringRef Name) { + using ELFT = object::ELF32BE; + using Elf_Ehdr = ELFT::Ehdr; + using Elf_Shdr = ELFT::Shdr; + using Elf_Sym = ELFT::Sym; + using Elf_Rela = ELFT::Rela; + + // The symbols. The ELF reference requires index 0 to be the all-zero null + // symbol (STN_UNDEF) and every local symbol to precede every global one. + std::vector Symbols(1); + std::memset(Symbols.data(), 0, sizeof(Elf_Sym)); + StringTableBuilder StrTab(StringTableBuilder::ELF); + auto AddSymbol = [&](StringRef SymName) -> Elf_Sym & { + StrTab.add(SymName); + Elf_Sym &Sym = Symbols.emplace_back(); + std::memset(&Sym, 0, sizeof(Elf_Sym)); + return Sym; + }; + for (const Immutable &Imm : Out.Immutables) { + Elf_Sym &Sym = AddSymbol(Imm.Name); + Sym.setBindingAndType(ELF::STB_LOCAL, ELF::STT_NOTYPE); + Sym.st_value = Imm.Value; + } + const unsigned NumLocals = Symbols.size(); + + SmallVector LibrarySymbols; + StringSet<> SeenLibrarySymbols; + for (const LibraryReloc &Rel : Out.LibraryRelocs) { + if (SeenLibrarySymbols.insert(Rel.Sym).second) + LibrarySymbols.push_back(Rel.Sym); + } + sort(LibrarySymbols); + StringMap SymbolIndex; + for (const StringRef LibrarySymbol : LibrarySymbols) { + SymbolIndex[LibrarySymbol] = Symbols.size(); + Elf_Sym &Sym = AddSymbol(LibrarySymbol); + Sym.setBindingAndType(ELF::STB_GLOBAL, ELF::STT_NOTYPE); + Sym.st_other = ELF::STO_EVM_REFERENCE_SYMBOL; + } + // 'st_name' is not a string but an offset into '.strtab', known once the + // table is final. + StrTab.finalizeInOrder(); + uint32_t SymIdx = 1; + for (const Immutable &Imm : Out.Immutables) + Symbols[SymIdx++].st_name = StrTab.getOffset(Imm.Name); + for (const StringRef LibrarySymbol : LibrarySymbols) + Symbols[SymIdx++].st_name = StrTab.getOffset(LibrarySymbol); + + // The relocation records of the library addresses still to be resolved. + // 'r_offset' says where in '.text' to patch, and 'r_info' packs the symbol + // table index with the relocation type. + std::vector Relas; + for (const LibraryReloc &LibRel : Out.LibraryRelocs) { + Elf_Rela &Rel = Relas.emplace_back(); + Rel.r_offset = static_cast(LibRel.Offset); + Rel.setSymbolAndType(SymbolIndex.lookup(LibRel.Sym), ELF::R_EVM_DATA_160, + /*IsMips64EL=*/false); + Rel.r_addend = 0; + } + const bool HasRelocs = !Relas.empty(); + + // The name sections of the libraries still referred to. + StringSet<> WantedNames; + for (const StringRef LibrarySymbol : LibrarySymbols) + WantedNames.insert(EVM::getSymbolSectionName(LibrarySymbol)); + SmallVector WantedSections; + for (const NameSection &Sec : Out.NameSections) { + if (WantedNames.contains(Sec.Name)) + WantedSections.push_back(&Sec); + } + + // The section headers, in file order, and the string table naming them. + // The ELF reference requires index 0 to be the all-zero null section + // (SHN_UNDEF). + SmallVector Sections; + SmallVector SectionNames; + StringTableBuilder ShStrTab(StringTableBuilder::ELF); + auto AddSection = [&](StringRef SecName, uint32_t Type, uint32_t Flags, + uint32_t Size, uint32_t Align, + uint32_t EntSize) -> Elf_Shdr & { + ShStrTab.add(SecName); + SectionNames.push_back(SecName); + Elf_Shdr &Sec = Sections.emplace_back(); + std::memset(&Sec, 0, sizeof(Elf_Shdr)); + Sec.sh_type = Type; + Sec.sh_flags = Flags; + Sec.sh_size = Size; + Sec.sh_addralign = Align; + Sec.sh_entsize = EntSize; + return Sec; + }; + + // The all-zero null section the ELF reference requires at index 0: its + // type is SHT_NULL and every field of it is zero. + AddSection("", ELF::SHT_NULL, 0, 0, 0, 0); + + AddSection(".text", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR, + static_cast(Out.TextSize), 1, 0); + const uint32_t TextIdx = Sections.size() - 1; + + // The immutable symbols point into the output '.text'. + for (Elf_Sym &Sym : MutableArrayRef(Symbols).slice(1, NumLocals - 1)) + Sym.st_shndx = static_cast(TextIdx); + + uint32_t RelaIdx = 0; + if (HasRelocs) { + Elf_Shdr &Rela = + AddSection(".rela.text", ELF::SHT_RELA, ELF::SHF_INFO_LINK, + static_cast(Relas.size() * sizeof(Elf_Rela)), 4, + sizeof(Elf_Rela)); + // For a relocation section, 'sh_info' holds the index of the section + // to which the relocations apply. + Rela.sh_info = TextIdx; + RelaIdx = Sections.size() - 1; + } + + const uint32_t FirstNameIdx = Sections.size(); + for (const NameSection *Sec : WantedSections) + AddSection(Sec->Name, ELF::SHT_PROGBITS, ELF::SHF_STRINGS, + static_cast(Sec->Data.size()), 1, 0); + + Elf_Shdr &SymTab = + AddSection(".symtab", ELF::SHT_SYMTAB, 0, + static_cast(Symbols.size() * sizeof(Elf_Sym)), 4, + sizeof(Elf_Sym)); + const uint32_t SymTabIdx = Sections.size() - 1; + // For a symbol table, 'sh_info' holds the index of the first non-local + // symbol. The locals come first, so it equals their count. + SymTab.sh_info = NumLocals; + // For a relocation section, 'sh_link' holds the index of the associated + // symbol table, which is only known now. + if (HasRelocs) + Sections[RelaIdx].sh_link = SymTabIdx; + + AddSection(".strtab", ELF::SHT_STRTAB, 0, + static_cast(StrTab.getSize()), 1, 0); + const uint32_t StrTabIdx = Sections.size() - 1; + // For a symbol table, 'sh_link' holds the index of the string table with + // the symbol names. + Sections[SymTabIdx].sh_link = StrTabIdx; + + Elf_Shdr &ShStrTabSec = AddSection(".shstrtab", ELF::SHT_STRTAB, 0, 0, 1, 0); + const uint32_t ShStrTabIdx = Sections.size() - 1; + ShStrTab.finalizeInOrder(); + ShStrTabSec.sh_size = static_cast(ShStrTab.getSize()); + // 'sh_name' is an offset into '.shstrtab', which names every section, + // itself included. + for (auto [Sec, SecName] : + zip_equal(drop_begin(Sections), drop_begin(SectionNames))) + Sec.sh_name = ShStrTab.getOffset(SecName); + + // The layout: each section lands at the next offset aligned to its + // 'sh_addralign', and the section header table follows the last one, + // word-aligned, at what becomes 'e_shoff'. + uint32_t Offset = sizeof(Elf_Ehdr); + for (Elf_Shdr &Sec : drop_begin(Sections)) { + Offset = alignTo(Offset, Sec.sh_addralign); + Sec.sh_offset = Offset; + Offset += Sec.sh_size; + } + const uint32_t ShOff = alignTo(Offset, 4); + + // The ELF header: a big-endian 32-bit relocatable object for the EVM + // machine. + Elf_Ehdr Header; + std::memset(&Header, 0, sizeof(Header)); + Header.e_ident[ELF::EI_MAG0] = 0x7f; + Header.e_ident[ELF::EI_MAG1] = 'E'; + Header.e_ident[ELF::EI_MAG2] = 'L'; + Header.e_ident[ELF::EI_MAG3] = 'F'; + Header.e_ident[ELF::EI_CLASS] = ELF::ELFCLASS32; + Header.e_ident[ELF::EI_DATA] = ELF::ELFDATA2MSB; + Header.e_ident[ELF::EI_VERSION] = ELF::EV_CURRENT; + Header.e_ident[ELF::EI_OSABI] = ELF::ELFOSABI_STANDALONE; + Header.e_type = ELF::ET_REL; + Header.e_machine = ELF::EM_EVM; + Header.e_version = ELF::EV_CURRENT; + Header.e_flags = Out.EFlags; + Header.e_shoff = ShOff; + Header.e_ehsize = sizeof(Elf_Ehdr); + Header.e_shentsize = sizeof(Elf_Shdr); + Header.e_shnum = static_cast(Sections.size()); + Header.e_shstrndx = static_cast(ShStrTabIdx); + + // Write it out: the structs and tables as bytes, padding in the gaps. + SmallString<1024> Bytes; + raw_svector_ostream OS(Bytes); + auto PadTo = [&](uint64_t Target) { OS.write_zeros(Target - OS.tell()); }; + auto WriteStruct = [&](const auto &Record) { + // The ELFTypes.h records store every field in file byte order with no + // padding, so their memory representation is the file representation. + OS.write(reinterpret_cast(&Record), sizeof(Record)); + }; + WriteStruct(Header); + + PadTo(Sections[TextIdx].sh_offset); + SmallVector Text(Out.TextSize); + writeText(Text.data(), Out); + OS.write(Text.data(), Text.size()); + + if (HasRelocs) { + PadTo(Sections[RelaIdx].sh_offset); + for (const Elf_Rela &Rel : Relas) + WriteStruct(Rel); + } + + for (const auto &[Idx, Sec] : enumerate(WantedSections)) { + PadTo(Sections[FirstNameIdx + Idx].sh_offset); + OS << Sec->Data; + } + + PadTo(Sections[SymTabIdx].sh_offset); + for (const Elf_Sym &Sym : Symbols) + WriteStruct(Sym); + + PadTo(Sections[StrTabIdx].sh_offset); + StrTab.write(OS); + + PadTo(Sections[ShStrTabIdx].sh_offset); + ShStrTab.write(OS); + + PadTo(ShOff); + for (const Elf_Shdr &Sec : Sections) + WriteStruct(Sec); + assert(Bytes.size() == ShOff + (Sections.size() * sizeof(Elf_Shdr))); + + std::unique_ptr Buf = + WritableMemoryBuffer::getNewUninitMemBuffer(Bytes.size(), Name); + std::memcpy(Buf->getBufferStart(), Bytes.data(), Bytes.size()); + return Buf; +} + +LLVMMemoryBufferRef LLVMAssembleEVM(const LLVMMemoryBufferRef inBuffers[], + const char *const inBuffersIDs[], + uint64_t inBuffersNum, const char *metadata, + uint64_t metadataSize) { + if (inBuffersNum == 0) + report_fatal_error("assembly: no input"); + + // Read every input in place. Nothing is copied here: the ELF reader keeps + // references into the caller's buffers. + SmallVector Inputs(inBuffersNum); + for (auto [Idx, In] : enumerate(Inputs)) { + In.IdHash = EVM::getLinkerSymbolHash(inBuffersIDs[Idx]); + In.Bin = cantFail(createBinary(*unwrap(inBuffers[Idx]))); + In.File = cast(In.Bin.get()); + In.Text = EVM::findSection(*In.File, ".text"); + } + + // The first object names the dependencies it reaches through '__dataoffset'; + // the layout includes those and nothing else. + StringSet<> Referenced; + bool FirstLoadsImmutables = false; + for (const SymbolRef &Sym : Inputs.front().File->symbols()) { + const StringRef Name = cantFail(Sym.getName()); + if (EVM::isLoadImmutableSymbolName(Name)) + FirstLoadsImmutables = true; + else if (EVM::isDataOffsetSymbolName(Name)) + Referenced.insert(EVM::extractDataOffseteName(Name)); + } + + // Deploy code cannot load immutables: nothing patches the deploy code + // before it runs, so a load site in it is a codegen bug. The frontend + // lowers creation-time immutable reads through memory instead. + if (!isRuntimeObject(*Inputs.front().File) && FirstLoadsImmutables) + report_fatal_error("assembly: the deploy code loads immutables"); + + // Lay the sections out at address zero, in input order, and define the + // '__dataoffset' and '__datasize' symbols the layout determines. The + // metadata follows the code, and the first object's '__datasize' covers it + // all. + Output Out; + Out.EFlags = cast(Inputs.front().File)->getPlatformFlags(); + StringMap SymValues; + for (auto [Idx, In] : enumerate(Inputs)) { + if (Idx != 0) { + SymValues[EVM::getDataSizeSymbol(In.IdHash)] = + static_cast(In.Text.size()); + if (!Referenced.contains(In.IdHash)) + continue; + } + In.Included = true; + In.OutTextOffset = Out.TextSize; + SymValues[EVM::getDataOffsetSymbol(In.IdHash)] = + static_cast(Out.TextSize); + Out.Segments.emplace_back(In.Text, Out.TextSize); + Out.TextSize += In.Text.size(); + } + Out.Segments.emplace_back(StringRef(metadata, metadataSize), Out.TextSize); + Out.TextSize += metadataSize; + // Every offset of the output object must fit in an ELF32 field. Half the + // range leaves room for everything that follows '.text' in the file. + if (Out.TextSize > UINT32_MAX / 2) + report_fatal_error("assembly: code too large for ELF32"); + SymValues[EVM::getDataSizeSymbol(Inputs.front().IdHash)] = + static_cast(Out.TextSize); + + StringSet<> SeenNameSections; + for (const Input &In : Inputs) { + if (!In.Included) + continue; + resolve(In, SymValues, /*LibraryAddresses=*/nullptr, Out); + collectNameSections(*In.File, SeenNameSections, Out); + } + collectImmutables(*Inputs.front().File, Out); + + return wrap(writeObject(Out, Inputs.front().IdHash).release()); +} + +LLVMMemoryBufferRef +LLVMLinkEVM(LLVMMemoryBufferRef inBuffer, const char *const *linkerSymbolNames, + const char linkerSymbolValues[][LLVM_EVM_LINKER_SYMBOL_SIZE], + uint64_t numLinkerSymbols) { + Input In; + In.Bin = cantFail(createBinary(*unwrap(inBuffer))); + In.File = cast(In.Bin.get()); + In.Text = EVM::findSection(*In.File, ".text"); + + StringMap LibraryAddresses; + for (uint64_t Idx = 0; Idx < numLinkerSymbols; ++Idx) + defineLinkerSymbol(LibraryAddresses, linkerSymbolNames[Idx], + linkerSymbolValues[Idx]); + + Output Out; + Out.EFlags = cast(In.File)->getPlatformFlags(); + Out.Segments.emplace_back(In.Text, 0); + Out.TextSize = In.Text.size(); + const StringMap SymValues; + resolve(In, SymValues, &LibraryAddresses, Out); + + if (Out.LibraryRelocs.empty()) + return wrap(writeBytecode(Out, "result").release()); + + StringSet<> Seen; + collectNameSections(*In.File, Seen, Out); + collectImmutables(*In.File, Out); + return wrap(writeObject(Out, "result").release()); +} diff --git a/llvm/lib/Target/EVM/Linker/EVMLinkerQueries.cpp b/llvm/lib/Target/EVM/Linker/EVMLinkerQueries.cpp new file mode 100644 index 000000000000..a2f665e78912 --- /dev/null +++ b/llvm/lib/Target/EVM/Linker/EVMLinkerQueries.cpp @@ -0,0 +1,215 @@ +//===-- EVMLinkerQueries.cpp - Queries over EVM ELF objects ----*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Implements the query functions of the EVM linker C API. They inspect an +// ELF object without changing it: 'LLVMIsELFEVM', +// 'LLVMGetUndefinedReferencesEVM', 'LLVMGetSymbolOffsetsEVM' and +// 'LLVMGetImmutablesEVM', together with their dispose counterparts. +// +// The C API hands arrays to the caller, who frees them through the dispose +// functions, so the allocations here are malloc and strdup by contract. +// +//===----------------------------------------------------------------------===// + +#include "llvm-c/EVMLinker.h" + +#include "EVMLinkerUtils.h" +#include "MCTargetDesc/EVMMCTargetDesc.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/Object/Binary.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBuffer.h" + +#include +#include +#include +#include +#include + +using namespace llvm; +using namespace object; + +/// Returns the names of the undefined linker symbols of \p File, as the +/// library source names their '.symbol_name...' sections carry, sorted. +static char **getUndefinedSymbolNames(const ObjectFile &File, + uint64_t *NumSymbols) { + StringSet<> UndefSymbols; + for (const SymbolRef &Sym : File.symbols()) { + if (EVM::isReferenceSymbol(Sym) && + (cantFail(Sym.getFlags()) & SymbolRef::SF_Undefined)) { + const StringRef SymName = cantFail(Sym.getName()); + assert(EVM::isLinkerSymbolName(SymName)); + UndefSymbols.insert(SymName); + } + } + + *NumSymbols = UndefSymbols.size(); + if (UndefSymbols.empty()) + return nullptr; + + char **UndefSymbolNames = reinterpret_cast( + std::malloc(UndefSymbols.size() * sizeof(char *))); + unsigned UndefSymIdx = 0; + for (const StringSet<>::value_type &Entry : UndefSymbols) { + const StringRef SymName = Entry.first(); + const StringRef SourceName = + EVM::findSection(File, EVM::getSymbolSectionName(SymName)); + assert(!SourceName.empty() && "missing library name section"); + UndefSymbolNames[UndefSymIdx++] = strdup(SourceName.str().c_str()); + } + + // Sort the returned names in lexicographical order. + llvm::sort( + UndefSymbolNames, UndefSymbolNames + *NumSymbols, + [](const char *S1, const char *S2) { return std::strcmp(S1, S2) < 0; }); + + return UndefSymbolNames; +} + +/// Disposes an array with symbols returned by the +/// LLVMGetUndefinedReferencesEVM function. +void LLVMDisposeUndefinedReferencesEVM(char *symbolNames[], + uint64_t numSymbols) { + for (unsigned Idx = 0; Idx < numSymbols; ++Idx) + std::free(symbolNames[Idx]); + std::free(symbolNames); +} + +/// Returns true if the \p inBuffer contains an EVM ELF object file. +LLVMBool LLVMIsELFEVM(LLVMMemoryBufferRef inBuffer) { + Expected> InBinaryOrErr = + ELFObjectFile::create(unwrap(inBuffer)->getMemBufferRef()); + if (!InBinaryOrErr) { + consumeError(InBinaryOrErr.takeError()); + return false; + } + return InBinaryOrErr.get().getArch() == Triple::evm; +} + +/// Returns an array of undefined linker symbol names +/// from the ELF object provided in \p inBuffer. +void LLVMGetUndefinedReferencesEVM(LLVMMemoryBufferRef inBuffer, + char ***linkerSymbols, + uint64_t *numLinkerSymbols) { + assert(linkerSymbols && numLinkerSymbols); + *linkerSymbols = nullptr; + *numLinkerSymbols = 0; + if (!LLVMIsELFEVM(inBuffer)) + return; + + std::unique_ptr InBinary = + cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); + const auto &File = *cast(InBinary.get()); + + *linkerSymbols = getUndefinedSymbolNames(File, numLinkerSymbols); +} + +/// Returns an array of offsets for the linker symbol relocations. +/// These are the symbol offsets in the generated bytecode. +uint64_t LLVMGetSymbolOffsetsEVM(LLVMMemoryBufferRef inBuffer, + const char *symbolName, + uint64_t **symbolOffsets) { + std::unique_ptr InBinary = + cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); + const auto &File = *cast(InBinary.get()); + + SmallVector Offsets; + const std::string LinkerSymName = EVM::getLinkerSymbolName(symbolName); + for (const ELFSectionRef RelocsSec : File.sections()) { + // Find the relocation section. It should be .rela.text. + if (RelocsSec.relocations().empty() || + !cantFail(RelocsSec.getRelocatedSection())->isText()) + continue; + + // A library address relocation names the symbol we look for. + for (const ELFRelocationRef Rel : RelocsSec.relocations()) { + if (Rel.getType() != ELF::R_EVM_DATA_160) + continue; + if (cantFail(Rel.getSymbol()->getName()) == LinkerSymName) + Offsets.push_back(Rel.getOffset()); + } + } + + *symbolOffsets = reinterpret_cast( + std::malloc(Offsets.size() * sizeof(uint64_t))); + + llvm::sort(Offsets); + llvm::copy(Offsets, *symbolOffsets); + + return Offsets.size(); +} + +/// Returns the immutable symbol names and their offsets from the ELF +/// object file provided in \p inBuffer. +uint64_t LLVMGetImmutablesEVM(LLVMMemoryBufferRef inBuffer, + char ***immutableIDs, + uint64_t **immutableOffsets) { + std::unique_ptr InBinary = + cantFail(createBinary(unwrap(inBuffer)->getMemBufferRef())); + const auto &File = *cast(InBinary.get()); + + // Maps immutable IDs to their references in the object code. + StringMap> ImmutablesMap; + for (const SymbolRef &Sym : File.symbols()) { + const StringRef SymName = cantFail(Sym.getName()); + if (!EVM::isLoadImmutableSymbolName(SymName)) + continue; + const uint64_t SymOffset = cantFail(Sym.getValue()); + // The symbol points to the beginning of a PUSH32 instruction. + // We have to add 1 (opcode size) to get offset to the PUSH32 + // instruction operand. + ImmutablesMap[EVM::getImmutableId(SymName)].push_back(SymOffset + 1); + } + + if (ImmutablesMap.empty()) { + *immutableIDs = nullptr; + *immutableOffsets = nullptr; + return 0; + } + + uint64_t NumOfImmutables = 0; + for (const auto &Entry : ImmutablesMap) + NumOfImmutables += Entry.second.size(); + + *immutableIDs = + reinterpret_cast(std::malloc(NumOfImmutables * sizeof(char *))); + *immutableOffsets = reinterpret_cast( + std::malloc(NumOfImmutables * sizeof(uint64_t))); + + unsigned Idx = 0; + for (const auto &[Id, Offsets] : ImmutablesMap) { + for (const uint64_t Offset : Offsets) { + assert(Idx < NumOfImmutables); + (*immutableIDs)[Idx] = strdup(Id.str().c_str()); + (*immutableOffsets)[Idx++] = Offset; + } + } + + return NumOfImmutables; +} + +/// Releases the array of linker symbol offsets returned by +/// 'LLVMGetSymbolOffsetsEVM'. +void LLVMDisposeSymbolOffsetsEVM(uint64_t *offsets) { std::free(offsets); } + +/// Disposes of the immutable names and their offsets returned by +/// 'LLVMGetImmutablesEVM'. +void LLVMDisposeImmutablesEVM(char **immutableIDs, uint64_t *immutableOffsets, + uint64_t numOfImmutables) { + for (unsigned Idx = 0; Idx < numOfImmutables; ++Idx) + std::free(immutableIDs[Idx]); + + std::free(immutableIDs); + std::free(immutableOffsets); +} diff --git a/llvm/lib/Target/EVM/Linker/EVMLinkerUtils.h b/llvm/lib/Target/EVM/Linker/EVMLinkerUtils.h new file mode 100644 index 000000000000..99d89845c06c --- /dev/null +++ b/llvm/lib/Target/EVM/Linker/EVMLinkerUtils.h @@ -0,0 +1,41 @@ +//===-- EVMLinkerUtils.h - Shared helpers of the EVM linker -----*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Helpers shared by EVMLinker.cpp and EVMLinkerQueries.cpp. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_EVM_LINKER_EVMLINKERUTILS_H +#define LLVM_LIB_TARGET_EVM_LINKER_EVMLINKERUTILS_H + +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/Object/ELFObjectFile.h" + +namespace llvm { +namespace EVM { + +/// Returns the contents of the named section, or an empty reference when it +/// is absent. +inline StringRef findSection(const object::ObjectFile &File, StringRef Name) { + for (const object::SectionRef &Sec : File.sections()) { + if (cantFail(Sec.getName()) == Name) + return cantFail(Sec.getContents()); + } + return StringRef(); +} + +/// Returns true when \p Sym is a library address symbol, which only the +/// linker can define. +inline bool isReferenceSymbol(const object::SymbolRef &Sym) { + return object::ELFSymbolRef(Sym).getOther() == ELF::STO_EVM_REFERENCE_SYMBOL; +} + +} // namespace EVM +} // namespace llvm + +#endif // LLVM_LIB_TARGET_EVM_LINKER_EVMLINKERUTILS_H diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMAsmBackend.cpp b/llvm/lib/Target/EVM/MCTargetDesc/EVMAsmBackend.cpp index ea92d5d8c945..969e43489aa2 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMAsmBackend.cpp +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMAsmBackend.cpp @@ -90,17 +90,20 @@ bool EVMAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count, MCFixupKindInfo EVMAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { const static std::array Infos = { - {// This table *must* be in the order that the fixup_* kinds are defined - // in EVMFixupKinds.h. - // - // Name Offset Size Flags - // (bits) (bits) - {"fixup_SecRel_i32", 0, 8 * 4, 0}, - {"fixup_SecRel_i24", 0, 8 * 3, 0}, - {"fixup_SecRel_i16", 0, 8 * 2, 0}, - {"fixup_SecRel_i8", 0, 8 * 1, 0}, - {"fixup_Data_i32", 0, 8 * 4, 0} - }}; + { + // This table *must* be in the order that the fixup_* kinds + // are defined in EVMFixupKinds.h. + // + // Name Offset Size Flags + // (bits) (bits) + {"fixup_SecRel_i32", 0, 8 * 4, 0}, + {"fixup_SecRel_i24", 0, 8 * 3, 0}, + {"fixup_SecRel_i16", 0, 8 * 2, 0}, + {"fixup_SecRel_i8", 0, 8 * 1, 0}, + {"fixup_Data_i32", 0, 8 * 4, 0}, + {"fixup_Data_i160", 0, 8 * 20, 0}, + }, + }; // Allow the generic 4-byte fixup kind, which is used // in debug info. @@ -135,7 +138,7 @@ std::optional EVMAsmBackend::evaluateFixup(const MCFragment &F, // The following fixups should be emited as relocations, // as they can only be resolved at link time. - if (FixUpKind == EVM::fixup_Data_i32) + if (FixUpKind == EVM::fixup_Data_i32 || FixUpKind == EVM::fixup_Data_i160) return false; Value = Target.getConstant(); @@ -201,8 +204,8 @@ bool EVMAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, uint64_t Value, bool Resolved) const { unsigned FixUpKind = Fixup.getKind(); - // The following fixups shouls always be emited as relocations, - // as they can only be resolved at linking time. + // The following fixup should always be emited as a relocation, + // as it can only be resolved at linking time. if (FixUpKind == EVM::fixup_Data_i32) return false; diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMELFObjectWriter.cpp b/llvm/lib/Target/EVM/MCTargetDesc/EVMELFObjectWriter.cpp index e41d0747cace..0fbbcc3cd0a2 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMELFObjectWriter.cpp +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMELFObjectWriter.cpp @@ -58,6 +58,8 @@ unsigned EVMELFObjectWriter::getRelocType(const MCFixup &Fixup, llvm_unreachable("Unexpected EVM fixup"); case EVM::fixup_Data_i32: return ELF::R_EVM_DATA; + case EVM::fixup_Data_i160: + return ELF::R_EVM_DATA_160; } } diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMFixupKinds.h b/llvm/lib/Target/EVM/MCTargetDesc/EVMFixupKinds.h index ed5ea473e603..898739c0fe6a 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMFixupKinds.h +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMFixupKinds.h @@ -19,6 +19,9 @@ enum Fixups { fixup_SecRel_i16, // 16-bit unsigned fixup_SecRel_i8, // 8-bit unsigned fixup_Data_i32, // 32-bit unsigned + fixup_Data_i160, // 160-bit library address, never + // resolved by MC, always a + // relocation // Marker LastTargetFixupKind, diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCCodeEmitter.cpp b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCCodeEmitter.cpp index 1e6587703ceb..8fad2f632479 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCCodeEmitter.cpp +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCCodeEmitter.cpp @@ -52,8 +52,10 @@ class EVMMCCodeEmitter final : public MCCodeEmitter { }; EVM::Fixups getFixupForOpc(unsigned Opcode, EVM::Specifier Spec) { + // A data symbol is either a 20-byte library address pushed whole, or a + // 32-bit data offset or size. if (Spec == EVM::S_DATA) - return EVM::fixup_Data_i32; + return Opcode == EVM::PUSH20_S ? EVM::fixup_Data_i160 : EVM::fixup_Data_i32; switch (Opcode) { default: diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCExpr.cpp b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCExpr.cpp index 6751f0901641..bba4f76d6aa4 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCExpr.cpp +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCExpr.cpp @@ -42,4 +42,3 @@ StringRef EVM::getSpecifierName(Specifier S) { return "evm_data"; } } - diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.cpp b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.cpp index 0700022c5d30..98cf65676e0c 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.cpp +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.cpp @@ -126,11 +126,6 @@ std::string EVM::getLinkerSymbolHash(StringRef SymName) { return (Twine("__$") + HexHash + "$__").str(); } -// Returns concatenation of the \p Name with the \p SubIdx. -std::string EVM::getSymbolIndexedName(StringRef Name, unsigned SubIdx) { - return (Twine(Name) + std::to_string(SubIdx)).str(); -} - // Returns concatenation of '.symbol_name' with the \p Name. std::string EVM::getSymbolSectionName(StringRef Name) { return (Twine(".symbol_name") + Name).str(); @@ -142,17 +137,10 @@ bool EVM::isSymbolSectionName(StringRef Name) { return Name.find(".symbol_name") == 0; } -// Strips index from the \p Name. -std::string EVM::getNonIndexedSymbolName(StringRef Name) { - Regex suffixRegex(R"(.*[0-4]$)"); - if (!suffixRegex.match(Name)) - report_fatal_error("Unexpected indexed symbol name"); - - return Name.drop_back().str(); -} - +// Returns the '__linker_symbol__$keccak256(Name)$__' symbol for the library +// source name \p Name. std::string EVM::getLinkerSymbolName(StringRef Name) { - return (Twine("__linker_symbol") + Name).str(); + return (Twine("__linker_symbol") + getLinkerSymbolHash(Name)).str(); } bool EVM::isLinkerSymbolName(StringRef Name) { diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.h b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.h index c3b9765b8765..ff8fca9d168a 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.h +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMMCTargetDesc.h @@ -39,8 +39,6 @@ std::unique_ptr createEVMELFObjectWriter(uint8_t OSABI); namespace EVM { std::string getLinkerSymbolHash(StringRef SymName); -std::string getSymbolIndexedName(StringRef Name, unsigned SubIdx); -std::string getNonIndexedSymbolName(StringRef Name); bool isSymbolSectionName(StringRef Name); std::string getSymbolSectionName(StringRef Name); bool isLinkerSymbolName(StringRef Name); diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.cpp b/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.cpp index c775613abaff..5918e2d177e7 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.cpp +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.cpp @@ -11,13 +11,7 @@ //===----------------------------------------------------------------------===// #include "MCTargetDesc/EVMTargetStreamer.h" -#include "EVMFixupKinds.h" -#include "EVMMCTargetDesc.h" #include "llvm/BinaryFormat/ELF.h" -#include "llvm/MC/MCAssembler.h" -#include "llvm/MC/MCCodeEmitter.h" -#include "llvm/MC/MCContext.h" -#include "llvm/MC/MCELFStreamer.h" #include "llvm/MC/MCSymbolELF.h" #include "llvm/Support/Casting.h" @@ -37,52 +31,5 @@ void EVMTargetStreamer::emitLabel(MCSymbol *Symbol) { EVMTargetAsmStreamer::EVMTargetAsmStreamer(MCStreamer &S) : EVMTargetStreamer(S) {} -void EVMTargetAsmStreamer::emitWideRelocatableSymbol(const MCInst &PushInst, - StringRef SymbolName, - unsigned SymbolSize) { - MCContext &Ctx = Streamer.getContext(); - const MCSubtargetInfo *STI = Ctx.getSubtargetInfo(); - Streamer.emitInstruction(PushInst, *STI); -} - EVMTargetObjStreamer::EVMTargetObjStreamer(MCStreamer &S) : EVMTargetStreamer(S) {} - -// Emits a PUSH instruction with relocatable symbol of the size up to 32 bytes. -// The symbol value is represented as an array of 4-byte relocatable -// sub-symbols. -void EVMTargetObjStreamer::emitWideRelocatableSymbol(const MCInst &PushInst, - StringRef SymbolName, - unsigned SymbolSize) { - if (SymbolSize > 32) - report_fatal_error("MC: relocatable symbol size exceeds 32 bytes"); - - // The code below is based on the MCObjectStreamer::emitInstToFragment() - // implementation. - MCContext &Ctx = Streamer.getContext(); - const MCSubtargetInfo *STI = Ctx.getSubtargetInfo(); - auto &S = static_cast(Streamer); - - auto *DF = Ctx.allocFragment(); - S.insert(DF); - SmallString<128> Code; - SmallVector Fixups; - S.getAssembler().getEmitter().encodeInstruction(PushInst, Code, Fixups, *STI); - DF->appendContents(Code); - - // Emit 4-byte fixups to cover a wide symbol value. - assert(DF->getContents().size() == SymbolSize + 1 /* opcode byte */); - assert(!((DF->getContents().size() - 1) % 4)); - for (unsigned Idx = 0; Idx < SymbolSize / sizeof(uint32_t); ++Idx) { - std::string SubSymName = EVM::getSymbolIndexedName(SymbolName, Idx); - auto *Sym = cast(Ctx.getOrCreateSymbol(SubSymName)); - Sym->setOther(ELF::STO_EVM_REFERENCE_SYMBOL); - const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); - S.visitUsedExpr(*Expr); - - assert(SymbolSize > Idx * 4); - // The byte index of start of the relocation is always 1, as - // we need to skip the instruction opcode which is always one byte. - DF->addFixup(MCFixup::create((Idx * 4) + 1, Expr, EVM::fixup_Data_i32)); - } -} diff --git a/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.h b/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.h index b6eca5053a63..d5dbf604cad3 100644 --- a/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.h +++ b/llvm/lib/Target/EVM/MCTargetDesc/EVMTargetStreamer.h @@ -26,10 +26,6 @@ class EVMTargetStreamer : public MCTargetStreamer { EVMTargetStreamer &operator=(EVMTargetStreamer &&) = delete; void emitLabel(MCSymbol *Symbol) override; - - virtual void emitWideRelocatableSymbol(const MCInst &PushInst, - StringRef SymbolName, - unsigned SymbolSize) {}; }; /// This part is for ASCII assembly output @@ -40,9 +36,6 @@ class EVMTargetAsmStreamer final : public EVMTargetStreamer { EVMTargetAsmStreamer(EVMTargetAsmStreamer &&) = delete; EVMTargetAsmStreamer &operator=(const EVMTargetAsmStreamer &) = delete; EVMTargetAsmStreamer &operator=(EVMTargetAsmStreamer &&) = delete; - - void emitWideRelocatableSymbol(const MCInst &PushInst, StringRef SymbolName, - unsigned SymbolSize) override; }; // This part is for EVM object output @@ -53,9 +46,6 @@ class EVMTargetObjStreamer final : public EVMTargetStreamer { EVMTargetObjStreamer(EVMTargetObjStreamer &&) = delete; EVMTargetObjStreamer &operator=(const EVMTargetObjStreamer &) = delete; EVMTargetObjStreamer &operator=(EVMTargetObjStreamer &&) = delete; - - void emitWideRelocatableSymbol(const MCInst &PushInst, StringRef SymbolName, - unsigned SymbolSize) override; }; } // namespace llvm diff --git a/llvm/test/MC/EVM/data-linker-symbol-relocs.ll b/llvm/test/MC/EVM/data-linker-symbol-relocs.ll index efac51802a69..a0e8be9fc9ca 100644 --- a/llvm/test/MC/EVM/data-linker-symbol-relocs.ll +++ b/llvm/test/MC/EVM/data-linker-symbol-relocs.ll @@ -4,18 +4,10 @@ ; CHECK: RELOCATION RECORDS FOR [.text]: ; CHECK-NEXT: OFFSET TYPE VALUE -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__0 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__1 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__2 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__3 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__4 +; CHECK-NEXT: {{d*}} R_EVM_DATA_160 __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__ ; CHECK-NEXT: {{d*}} R_EVM_DATA __dataoffset__$0336fd807c0716a535e520df5b63ecc41ba7984875fdfa2241fcf3c8d0107e26$__ ; CHECK-NEXT: {{d*}} R_EVM_DATA __datasize__$0336fd807c0716a535e520df5b63ecc41ba7984875fdfa2241fcf3c8d0107e26$__ -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__0 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__1 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__2 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__3 -; CHECK-NEXT: {{d*}} R_EVM_DATA __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__4 +; CHECK-NEXT: {{d*}} R_EVM_DATA_160 __linker_symbol__$65c539c969b08d62684b6c973d3f0f4f37a85c2d0cbe39fffa3022cb69aac23a$__ ; CHECK-NEXT: {{d*}} R_EVM_DATA __dataoffset__$0336fd807c0716a535e520df5b63ecc41ba7984875fdfa2241fcf3c8d0107e26$__ ; CHECK-NEXT: {{d*}} R_EVM_DATA __datasize__$0336fd807c0716a535e520df5b63ecc41ba7984875fdfa2241fcf3c8d0107e26$__ diff --git a/llvm/unittests/Target/EVM/CMakeLists.txt b/llvm/unittests/Target/EVM/CMakeLists.txt index f34aefdd02a9..3a3ae02c5788 100644 --- a/llvm/unittests/Target/EVM/CMakeLists.txt +++ b/llvm/unittests/Target/EVM/CMakeLists.txt @@ -7,22 +7,35 @@ set(LLVM_LINK_COMPONENTS EVM EVMDesc EVMInfo + EVMLinker CodeGen Core + IRReader MC MIRParser + Object SelectionDAG Support Target TargetParser ) +# EVMLinkerTest locates its input files through +# llvm::unittest::getInputFileDirectory(). +set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +configure_file( + ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in + ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt) + add_llvm_target_unittest(EVMTests BytecodeSize.cpp EmitToMemoryBufferWithDbg.cpp + EVMLinkerTest.cpp SpillRegion.cpp StackShuffler.cpp StackModel.cpp ) +target_link_libraries(EVMTests PRIVATE LLVMTestingSupport) + set_property(TARGET EVMTests PROPERTY FOLDER "Tests/UnitTests/TargetTests") diff --git a/llvm/unittests/Target/EVM/EVMLinkerTest.cpp b/llvm/unittests/Target/EVM/EVMLinkerTest.cpp new file mode 100644 index 000000000000..6fb621d51793 --- /dev/null +++ b/llvm/unittests/Target/EVM/EVMLinkerTest.cpp @@ -0,0 +1,530 @@ +//===---------- llvm/unittests/Target/EVM/EVMLinkerTest.cpp ---------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm-c/EVMLinker.h" +#include "llvm-c/Core.h" +#include "llvm-c/IRReader.h" +#include "llvm-c/TargetMachine.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Testing/Support/SupportHelpers.h" +#include "gtest/gtest.h" +#include + +using namespace llvm; + +extern const char *TestMainArgv0; + +static std::string expand(const char *Path) { + llvm::SmallString<128> ThisPath = + llvm::unittest::getInputFileDirectory(TestMainArgv0); + llvm::sys::path::append(ThisPath, Path); + return ThisPath.str().str(); +} + +/// Parses \p Buf as an ELF object and returns a copy of its '.text' contents. +static std::string textSection(LLVMMemoryBufferRef Buf) { + using namespace llvm::object; + MemoryBufferRef Ref( + StringRef(LLVMGetBufferStart(Buf), LLVMGetBufferSize(Buf)), "object"); + std::unique_ptr Bin = cantFail(createBinary(Ref)); + const auto *File = cast(Bin.get()); + for (const SectionRef &Sec : File->sections()) { + if (cantFail(Sec.getName()) == ".text") + return cantFail(Sec.getContents()).str(); + } + return {}; +} + +/// Returns the big-endian bytes of \p V. +static std::string be32(uint32_t V) { + std::string S(4, '\0'); + llvm::support::endian::write32be(S.data(), V); + return S; +} + +namespace { + +class EVMLinkerTest : public testing::Test { +protected: + void SetUp() override { + LLVMInitializeEVMTargetInfo(); + LLVMInitializeEVMTarget(); + LLVMInitializeEVMTargetMC(); + LLVMInitializeEVMAsmPrinter(); + + LLVMTargetRef Target = nullptr; + const char *Triple = "evm"; + char *ErrMsg = nullptr; + if (LLVMGetTargetFromTriple(Triple, &Target, &ErrMsg)) { + FAIL() << "Failed to create target from the triple (" << Triple + << "): " << ErrMsg; + return; + } + ASSERT_TRUE(Target); + + // Construct a TargetMachine. + TM = + LLVMCreateTargetMachine(Target, Triple, "", "", LLVMCodeGenLevelDefault, + LLVMRelocDefault, LLVMCodeModelDefault); + + Context = LLVMContextCreate(); + } + + void TearDown() override { + LLVMDisposeTargetMachine(TM); + LLVMContextDispose(Context); + } + +public: + LLVMMemoryBufferRef link(LLVMMemoryBufferRef InAssembly, const char *Name, + const char *const *LinkerSyms, + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) + const char (*LinkerSymVals)[20], uint64_t NumSyms) { + return LLVMLinkEVM(InAssembly, LinkerSyms, LinkerSymVals, NumSyms); + } + + LLVMMemoryBufferRef assemble(const std::vector &Objs, + const std::vector &IDs, + bool WithMD = false) { + LLVMMemoryBufferRef OutAssembly = + LLVMAssembleEVM(Objs.data(), IDs.data(), Objs.size(), + WithMD ? MD.data() : nullptr, WithMD ? MD.size() : 0); + EXPECT_TRUE(LLVMIsELFEVM(OutAssembly)); + return OutAssembly; + } + + LLVMMemoryBufferRef compileIR(const char *FileName) const { + auto Buf = MemoryBuffer::getFile(expand(FileName), /*IsText=*/false, + /*RequiresNullTerminator=*/false); + if (auto EC = Buf.getError()) + exit(1); + + LLVMMemoryBufferRef IrBuf = llvm::wrap(Buf.get().release()); + char *ErrMsg = nullptr; + LLVMModuleRef Module = nullptr; + if (LLVMParseIRInContext(Context, IrBuf, &Module, &ErrMsg)) { + LLVMDisposeMessage(ErrMsg); + exit(1); + } + + // Run CodeGen to produce the buffers. + LLVMMemoryBufferRef Result = nullptr; + if (LLVMTargetMachineEmitToMemoryBuffer(TM, Module, LLVMObjectFile, &ErrMsg, + &Result)) { + LLVMDisposeModule(Module); + LLVMDisposeMessage(ErrMsg); + exit(1); + } + LLVMDisposeModule(Module); + return Result; + } + + LLVMTargetMachineRef TM = nullptr; + LLVMContextRef Context = nullptr; + std::array MD = { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, + 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, + 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, + }; +}; + +TEST_F(EVMLinkerTest, IterativeLinkage) { + LLVMMemoryBufferRef DeployObjMemBuffer = compileIR("deployIr.ll"); + LLVMMemoryBufferRef DeployedObjMemBuffer = compileIR("deployedIr.ll"); + + EXPECT_TRUE(LLVMIsELFEVM(DeployObjMemBuffer)); + EXPECT_TRUE(LLVMIsELFEVM(DeployedObjMemBuffer)); + + const std::array LinkerSymbol = { + "library_id", + "library_id2", + }; + // The C API takes 'const char (*)[20]', which no std::array converts to. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) + const char LinkerSymbolVal[2][20] = { + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + { + '\x9f', 0x1E, '\xBB', '\xF1', 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + }, + }; + StringRef SymVal1(LinkerSymbolVal[0], 20); + StringRef SymVal2(LinkerSymbolVal[1], 20); + + const std::array InIDs = {"Test_26", "Test_26_deployed"}; + std::array InData = { + DeployObjMemBuffer, + DeployedObjMemBuffer, + }; + LLVMMemoryBufferRef InMemBuf = nullptr; + LLVMMemoryBufferRef OutMemBuf = nullptr; + + // Assemble deploy with deployed. + { + OutMemBuf = LLVMAssembleEVM(InData.data(), InIDs.data(), 2, nullptr, 0); + EXPECT_TRUE(LLVMIsELFEVM(OutMemBuf)); + std::swap(OutMemBuf, InMemBuf); + } + + // Check load immutable references. + { + char **ImmutableIDs = nullptr; + uint64_t *ImmutableOffsets = nullptr; + uint64_t ImmCount = + LLVMGetImmutablesEVM(InData[1], &ImmutableIDs, &ImmutableOffsets); + EXPECT_TRUE(ImmCount == 1); + EXPECT_TRUE(std::strcmp(ImmutableIDs[0], "id") == 0); + LLVMDisposeImmutablesEVM(ImmutableIDs, ImmutableOffsets, ImmCount); + } + + // No linker symbol definitions are provided, so we have to receive ELF + // object file. + OutMemBuf = LLVMLinkEVM(InMemBuf, nullptr, nullptr, 0); + EXPECT_TRUE(LLVMIsELFEVM(OutMemBuf)); + + char **UndefLinkerSyms = nullptr; + uint64_t NumLinkerUndefs = 0; + + LLVMGetUndefinedReferencesEVM(OutMemBuf, &UndefLinkerSyms, &NumLinkerUndefs); + + EXPECT_TRUE(NumLinkerUndefs == 2); + EXPECT_TRUE((std::strcmp(UndefLinkerSyms[0], LinkerSymbol[0]) == 0)); + EXPECT_TRUE((std::strcmp(UndefLinkerSyms[1], LinkerSymbol[1]) == 0)); + LLVMDisposeUndefinedReferencesEVM(UndefLinkerSyms, NumLinkerUndefs); + + std::swap(OutMemBuf, InMemBuf); + LLVMDisposeMemoryBuffer(OutMemBuf); + + // The first linker symbol definitions is provided, so we still have + // to receive an ELF object file + OutMemBuf = LLVMLinkEVM(InMemBuf, LinkerSymbol.data(), LinkerSymbolVal, 1); + EXPECT_TRUE(LLVMIsELFEVM(OutMemBuf)); + + LLVMGetUndefinedReferencesEVM(OutMemBuf, &UndefLinkerSyms, &NumLinkerUndefs); + EXPECT_TRUE(NumLinkerUndefs == 1); + EXPECT_TRUE((std::strcmp(UndefLinkerSyms[0], LinkerSymbol[1]) == 0)); + LLVMDisposeUndefinedReferencesEVM(UndefLinkerSyms, NumLinkerUndefs); + + std::swap(OutMemBuf, InMemBuf); + LLVMDisposeMemoryBuffer(OutMemBuf); + + // Both linker symbol definitions are provided, so we have to receive + // a bytecode. + OutMemBuf = LLVMLinkEVM(InMemBuf, LinkerSymbol.data(), LinkerSymbolVal, 2); + EXPECT_TRUE(!LLVMIsELFEVM(OutMemBuf)); + + LLVMGetUndefinedReferencesEVM(OutMemBuf, &UndefLinkerSyms, &NumLinkerUndefs); + EXPECT_TRUE(NumLinkerUndefs == 0); + LLVMDisposeUndefinedReferencesEVM(UndefLinkerSyms, NumLinkerUndefs); + + StringRef DeployBin(LLVMGetBufferStart(OutMemBuf), + LLVMGetBufferSize(OutMemBuf)); + + EXPECT_TRUE(DeployBin.contains(SymVal1)); + EXPECT_TRUE(DeployBin.contains(SymVal2)); + + LLVMDisposeMemoryBuffer(OutMemBuf); + LLVMDisposeMemoryBuffer(InMemBuf); +} + +TEST_F(EVMLinkerTest, Assembly) { + const std::array LinkerSymbol = { + "unused_library_id", + "library_id", + }; + // The C API takes 'const char (*)[20]', which no std::array converts to. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) + const char LinkerSymbolVal[2][20] = { + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9}, + }; + + LLVMMemoryBufferRef A_deploy_obj = compileIR("A_deploy.ll"); + LLVMMemoryBufferRef A_deployed_obj = compileIR("A_deployed.ll"); + LLVMMemoryBufferRef D_deploy_obj = compileIR("D_deploy.ll"); + LLVMMemoryBufferRef D_deployed_obj = compileIR("D_deployed.ll"); + LLVMMemoryBufferRef R_deploy_obj = compileIR("R_deploy.ll"); + LLVMMemoryBufferRef R_deployed_obj = compileIR("R_deployed.ll"); + + // A assemble. + LLVMMemoryBufferRef A_assembly_deployed = + assemble({A_deployed_obj}, {"A_38_deployed"}, + /*WithMD=*/true); + LLVMMemoryBufferRef A_assembly = + assemble({A_deploy_obj, A_assembly_deployed}, {"A_38", "A_38_deployed"}); + + // D assemble. + LLVMMemoryBufferRef D_assembly_deployed = + assemble({D_deployed_obj}, {"D_38_deployed"}, + /*WithMD=*/true); + LLVMMemoryBufferRef D_assembly = + assemble({D_deploy_obj, D_assembly_deployed}, {"D_51", "D_51_deployed"}); + + // R_deployed assemble. + // A_assembly is not required here, but we add it intentionaly to check + // that it will be ignored (the total number of library reference is 3). + LLVMMemoryBufferRef R_deployed_assemble = + assemble({R_deployed_obj, D_assembly, A_assembly, A_assembly_deployed}, + {"R_107_deployed", "D_51", "A_38", "A_38.A_38_deployed"}, + /*WithMD=*/true); + + // R assemble. + LLVMMemoryBufferRef R_assembly = assemble( + {R_deploy_obj, R_deployed_assemble, A_assembly, A_assembly_deployed}, + {"R_107", "R_107_deployed", "A_38", "A_38.A_38_deployed"}); + + // Get linker symbols offsets + uint64_t *SymOffsets = nullptr; + uint64_t NumOffsets = + LLVMGetSymbolOffsetsEVM(R_assembly, LinkerSymbol[1], &SymOffsets); + EXPECT_TRUE(NumOffsets == 3); + LLVMDisposeSymbolOffsetsEVM(SymOffsets); + + // Linking with no linker symbols. + LLVMMemoryBufferRef TmpAssembly = link(R_assembly, "R", nullptr, nullptr, 0); + EXPECT_TRUE(LLVMIsELFEVM(TmpAssembly)); + + // Linking with unused linker symbol. It has no effect. + LLVMMemoryBufferRef TmpAssembly2 = + link(TmpAssembly, "R", LinkerSymbol.data(), LinkerSymbolVal, 1); + EXPECT_TRUE(LLVMIsELFEVM(TmpAssembly2)); + + // Linking with both linker symbols. The library reference should be resolved + // and resulting object is a final bytecode. + LLVMMemoryBufferRef Bytecode = + link(TmpAssembly2, "R", LinkerSymbol.data(), LinkerSymbolVal, 2); + EXPECT_TRUE(!LLVMIsELFEVM(Bytecode)); + + char **UndefLinkerSyms = nullptr; + uint64_t NumLinkerUndefs = 0; + + LLVMGetUndefinedReferencesEVM(Bytecode, &UndefLinkerSyms, &NumLinkerUndefs); + EXPECT_TRUE(NumLinkerUndefs == 0); + LLVMDisposeUndefinedReferencesEVM(UndefLinkerSyms, NumLinkerUndefs); + + StringRef Binary(LLVMGetBufferStart(Bytecode), LLVMGetBufferSize(Bytecode)); + + StringRef LibAddr(LinkerSymbolVal[1], 20); + EXPECT_TRUE(Binary.count(LibAddr) == 3); + EXPECT_TRUE(Binary.count(StringRef(MD.data(), MD.size())) == 5); + + LLVMDisposeMemoryBuffer(A_deploy_obj); + LLVMDisposeMemoryBuffer(A_deployed_obj); + LLVMDisposeMemoryBuffer(D_deploy_obj); + LLVMDisposeMemoryBuffer(D_deployed_obj); + LLVMDisposeMemoryBuffer(R_deploy_obj); + LLVMDisposeMemoryBuffer(R_deployed_obj); + LLVMDisposeMemoryBuffer(TmpAssembly); + LLVMDisposeMemoryBuffer(TmpAssembly2); + LLVMDisposeMemoryBuffer(Bytecode); +} + +#if GTEST_HAS_DEATH_TEST +// A '__dataoffset' or '__datasize' of an object the driver did not pass is a +// driver bug, and the linker aborts on it. +TEST_F(EVMLinkerTest, UndefNonRefSymbols) { + LLVMMemoryBufferRef DeployObj = compileIR("undefDeployIr.ll"); + LLVMMemoryBufferRef DeployedObj = compileIR("undefDeployedIr.ll"); + + EXPECT_TRUE(LLVMIsELFEVM(DeployObj)); + EXPECT_TRUE(LLVMIsELFEVM(DeployedObj)); + + const std::array InObjs = {DeployObj, DeployedObj}; + const std::array IDs = {"Test_26", "Test_26_deployed"}; + // The EXPECT_DEATH expansion uses 'goto' and unbraced 'if'. + // NOLINTBEGIN(cppcoreguidelines-avoid-goto) + // NOLINTBEGIN(readability-inconsistent-ifelse-braces) + EXPECT_DEATH( + LLVMAssembleEVM(InObjs.data(), IDs.data(), IDs.size(), nullptr, 0), + "non-ref undefined symbol:"); + // NOLINTEND(readability-inconsistent-ifelse-braces) + // NOLINTEND(cppcoreguidelines-avoid-goto) + + LLVMDisposeMemoryBuffer(DeployObj); + LLVMDisposeMemoryBuffer(DeployedObj); +} +#endif + +// Pins the values the linker writes: the big-endian '__dataoffset' and +// '__datasize' immediates, the bytecode length, the metadata tail and the +// offsets 'LLVMGetSymbolOffsetsEVM' reports. +TEST_F(EVMLinkerTest, RelocationValues) { + LLVMMemoryBufferRef DeployObj = compileIR("deployIr.ll"); + LLVMMemoryBufferRef DeployedObj = compileIR("deployedIr.ll"); + const std::string DeployText = textSection(DeployObj); + const std::string DeployedText = textSection(DeployedObj); + const StringRef Metadata(MD.data(), MD.size()); + + // The runtime segment is its code plus the metadata. + LLVMMemoryBufferRef Runtime = + assemble({DeployedObj}, {"Test_26_deployed"}, /*WithMD=*/true); + const std::string RuntimeText = textSection(Runtime); + EXPECT_EQ(RuntimeText.size(), DeployedText.size() + MD.size()); + EXPECT_EQ(StringRef(RuntimeText).take_back(MD.size()), Metadata); + + // The deploy segment embeds it right after the deploy code. + LLVMMemoryBufferRef Assembled = + assemble({DeployObj, Runtime}, {"Test_26", "Test_26_deployed"}); + const std::string AssembledText = textSection(Assembled); + EXPECT_EQ(AssembledText.size(), DeployText.size() + RuntimeText.size()); + + // '__dataoffset' and '__datasize' resolve to the runtime's offset and + // size, written big-endian into PUSH4 immediates. + const std::string DataOffset = + std::string(1, '\x63') + be32(DeployText.size()); + const std::string DataSize = + std::string(1, '\x63') + be32(RuntimeText.size()); + EXPECT_NE(AssembledText.find(DataOffset), std::string::npos); + EXPECT_NE(AssembledText.find(DataSize), std::string::npos); + + uint64_t *SymOffsets = nullptr; + uint64_t NumOffsets = + LLVMGetSymbolOffsetsEVM(Assembled, "library_id", &SymOffsets); + ASSERT_EQ(NumOffsets, 2U); + EXPECT_LT(SymOffsets[0], SymOffsets[1]); + + const std::array LinkerSymbol = { + "library_id", + "library_id2", + }; + // The C API takes 'const char (*)[20]', which no std::array converts to. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) + const char LinkerSymbolVal[2][20] = { + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9}, + }; + LLVMMemoryBufferRef Bytecode = + link(Assembled, "Test_26", LinkerSymbol.data(), LinkerSymbolVal, 2); + EXPECT_TRUE(!LLVMIsELFEVM(Bytecode)); + const StringRef Bin(LLVMGetBufferStart(Bytecode), + LLVMGetBufferSize(Bytecode)); + + // The bytecode is exactly the code plus the metadata, nothing after it. + EXPECT_EQ(Bin.size(), AssembledText.size()); + EXPECT_EQ(Bin.take_back(MD.size()), Metadata); + + // Each reported offset holds the 20 bytes of the library address. + for (uint64_t Idx = 0; Idx < NumOffsets; ++Idx) { + ASSERT_LE(SymOffsets[Idx] + 20, Bin.size()); + EXPECT_EQ(Bin.substr(SymOffsets[Idx], 20), + StringRef(LinkerSymbolVal[0], 20)); + } + LLVMDisposeSymbolOffsetsEVM(SymOffsets); + + LLVMDisposeMemoryBuffer(DeployObj); + LLVMDisposeMemoryBuffer(DeployedObj); + LLVMDisposeMemoryBuffer(Runtime); + LLVMDisposeMemoryBuffer(Assembled); + LLVMDisposeMemoryBuffer(Bytecode); +} + +// Pins the immutable offsets: they point at the PUSH32 immediate, one byte +// past the opcode, both in the raw object and in the assembled one. +TEST_F(EVMLinkerTest, ImmutableOffsets) { + LLVMMemoryBufferRef DeployedObj = compileIR("deployedIr.ll"); + const std::string Text = textSection(DeployedObj); + + char **Ids = nullptr; + uint64_t *Offsets = nullptr; + uint64_t Count = LLVMGetImmutablesEVM(DeployedObj, &Ids, &Offsets); + ASSERT_EQ(Count, 1U); + EXPECT_STREQ(Ids[0], "id"); + ASSERT_GT(Offsets[0], 0U); + ASSERT_LE(Offsets[0] + 32, Text.size()); + EXPECT_EQ(static_cast(Text[Offsets[0] - 1]), 0x7F); // PUSH32 + const uint64_t RawOffset = Offsets[0]; + LLVMDisposeImmutablesEVM(Ids, Offsets, Count); + + // The assembled runtime object carries the same immutable symbol. + LLVMMemoryBufferRef Runtime = + assemble({DeployedObj}, {"Test_26_deployed"}, /*WithMD=*/true); + const std::string RuntimeText = textSection(Runtime); + Count = LLVMGetImmutablesEVM(Runtime, &Ids, &Offsets); + ASSERT_EQ(Count, 1U); + EXPECT_STREQ(Ids[0], "id"); + EXPECT_EQ(Offsets[0], RawOffset); + EXPECT_EQ(static_cast(RuntimeText[Offsets[0] - 1]), 0x7F); + LLVMDisposeImmutablesEVM(Ids, Offsets, Count); + + LLVMDisposeMemoryBuffer(DeployedObj); + LLVMDisposeMemoryBuffer(Runtime); +} + +// Pins the layout fields of the object the linker writes: section order, the +// symbol table's locals count and link, the relocation section's shape and +// the aligned offsets. +TEST_F(EVMLinkerTest, AssembledObjectLayout) { + using namespace llvm::object; + LLVMMemoryBufferRef DeployObj = compileIR("deployIr.ll"); + LLVMMemoryBufferRef DeployedObj = compileIR("deployedIr.ll"); + LLVMMemoryBufferRef Runtime = + assemble({DeployedObj}, {"Test_26_deployed"}, /*WithMD=*/true); + LLVMMemoryBufferRef Assembled = + assemble({DeployObj, Runtime}, {"Test_26", "Test_26_deployed"}); + + MemoryBufferRef Ref( + StringRef(LLVMGetBufferStart(Assembled), LLVMGetBufferSize(Assembled)), + "assembled"); + ELFObjectFile Obj = cantFail(ELFObjectFile::create(Ref)); + const ELFFile &EF = Obj.getELFFile(); + EXPECT_EQ(EF.getHeader().e_ehsize, sizeof(ELF::Elf32_Ehdr)); + EXPECT_EQ(EF.getHeader().e_shoff % 4, 0U); + // The deploy assembly keeps the deploy kind, the runtime one its flag. + EXPECT_EQ(EF.getHeader().e_flags, 0U); + MemoryBufferRef RuntimeRef( + StringRef(LLVMGetBufferStart(Runtime), LLVMGetBufferSize(Runtime)), + "runtime"); + ELFObjectFile RuntimeObj = + cantFail(ELFObjectFile::create(RuntimeRef)); + EXPECT_EQ(RuntimeObj.getELFFile().getHeader().e_flags, + unsigned(ELF::EF_EVM_RUNTIME)); + + const auto Sections = cantFail(EF.sections()); + SmallVector Names; + for (const auto &Sec : Sections) + Names.push_back(cantFail(EF.getSectionName(Sec)).str()); + ASSERT_EQ(Names.size(), 8U); + EXPECT_EQ(Names[1], ".text"); + EXPECT_EQ(Names[2], ".rela.text"); + EXPECT_TRUE(StringRef(Names[3]).starts_with(".symbol_name")); + EXPECT_TRUE(StringRef(Names[4]).starts_with(".symbol_name")); + EXPECT_EQ(Names[5], ".symtab"); + EXPECT_EQ(Names[6], ".strtab"); + EXPECT_EQ(Names[7], ".shstrtab"); + + const auto &Rela = Sections[2]; + EXPECT_EQ(Rela.sh_type, static_cast(ELF::SHT_RELA)); + EXPECT_EQ(Rela.sh_link, 5U); + EXPECT_EQ(Rela.sh_info, 1U); + EXPECT_EQ(Rela.sh_addralign, 4U); + EXPECT_EQ(Rela.sh_entsize, sizeof(ELF::Elf32_Rela)); + EXPECT_EQ(Rela.sh_offset % 4, 0U); + + const auto &SymTab = Sections[5]; + EXPECT_EQ(SymTab.sh_type, static_cast(ELF::SHT_SYMTAB)); + // One local: the null symbol. The deploy object loads no immutables. + EXPECT_EQ(SymTab.sh_info, 1U); + EXPECT_EQ(SymTab.sh_link, 6U); + EXPECT_EQ(SymTab.sh_addralign, 4U); + EXPECT_EQ(SymTab.sh_entsize, sizeof(ELF::Elf32_Sym)); + EXPECT_EQ(SymTab.sh_offset % 4, 0U); + + LLVMDisposeMemoryBuffer(DeployObj); + LLVMDisposeMemoryBuffer(DeployedObj); + LLVMDisposeMemoryBuffer(Runtime); + LLVMDisposeMemoryBuffer(Assembled); +} + +} // namespace diff --git a/lld/unittests/EVM/Inputs/A_deploy.ll b/llvm/unittests/Target/EVM/Inputs/A_deploy.ll similarity index 100% rename from lld/unittests/EVM/Inputs/A_deploy.ll rename to llvm/unittests/Target/EVM/Inputs/A_deploy.ll diff --git a/lld/unittests/EVM/Inputs/A_deployed.ll b/llvm/unittests/Target/EVM/Inputs/A_deployed.ll similarity index 85% rename from lld/unittests/EVM/Inputs/A_deployed.ll rename to llvm/unittests/Target/EVM/Inputs/A_deployed.ll index 134e349b975b..1a26121c009f 100644 --- a/lld/unittests/EVM/Inputs/A_deployed.ll +++ b/llvm/unittests/Target/EVM/Inputs/A_deployed.ll @@ -11,3 +11,5 @@ define i256 @runtime() { } !1 = !{!"A_38_deployed"} !2 = !{!"library_id"} +!llvm.module.flags = !{!100} +!100 = !{i32 1, !"evm.runtime", i32 1} diff --git a/lld/unittests/EVM/Inputs/D_deploy.ll b/llvm/unittests/Target/EVM/Inputs/D_deploy.ll similarity index 100% rename from lld/unittests/EVM/Inputs/D_deploy.ll rename to llvm/unittests/Target/EVM/Inputs/D_deploy.ll diff --git a/lld/unittests/EVM/Inputs/D_deployed.ll b/llvm/unittests/Target/EVM/Inputs/D_deployed.ll similarity index 78% rename from lld/unittests/EVM/Inputs/D_deployed.ll rename to llvm/unittests/Target/EVM/Inputs/D_deployed.ll index cc0d99edb63a..54c44b8d44d3 100644 --- a/lld/unittests/EVM/Inputs/D_deployed.ll +++ b/llvm/unittests/Target/EVM/Inputs/D_deployed.ll @@ -7,3 +7,5 @@ define i256 @runtime() { ret i256 %res } !1 = !{!"40"} +!llvm.module.flags = !{!100} +!100 = !{i32 1, !"evm.runtime", i32 1} diff --git a/lld/unittests/EVM/Inputs/R_deploy.ll b/llvm/unittests/Target/EVM/Inputs/R_deploy.ll similarity index 100% rename from lld/unittests/EVM/Inputs/R_deploy.ll rename to llvm/unittests/Target/EVM/Inputs/R_deploy.ll diff --git a/lld/unittests/EVM/Inputs/R_deployed.ll b/llvm/unittests/Target/EVM/Inputs/R_deployed.ll similarity index 92% rename from lld/unittests/EVM/Inputs/R_deployed.ll rename to llvm/unittests/Target/EVM/Inputs/R_deployed.ll index 97b4b99057c0..45a5ad0e0cc3 100644 --- a/lld/unittests/EVM/Inputs/R_deployed.ll +++ b/llvm/unittests/Target/EVM/Inputs/R_deployed.ll @@ -24,3 +24,5 @@ define i256 @get_initcode() { !1 = !{!"53"} !2 = !{!"A_38.A_38_deployed"} !3 = !{!"D_51"} +!llvm.module.flags = !{!100} +!100 = !{i32 1, !"evm.runtime", i32 1} diff --git a/lld/unittests/EVM/Inputs/deployIr.ll b/llvm/unittests/Target/EVM/Inputs/deployIr.ll similarity index 100% rename from lld/unittests/EVM/Inputs/deployIr.ll rename to llvm/unittests/Target/EVM/Inputs/deployIr.ll diff --git a/lld/unittests/EVM/Inputs/deployedIr.ll b/llvm/unittests/Target/EVM/Inputs/deployedIr.ll similarity index 85% rename from lld/unittests/EVM/Inputs/deployedIr.ll rename to llvm/unittests/Target/EVM/Inputs/deployedIr.ll index e1ec57393ad1..47186fa84aa8 100644 --- a/lld/unittests/EVM/Inputs/deployedIr.ll +++ b/llvm/unittests/Target/EVM/Inputs/deployedIr.ll @@ -11,3 +11,5 @@ define i256 @foo() { } !1 = !{!"library_id2"} !2 = !{!"id"} +!llvm.module.flags = !{!100} +!100 = !{i32 1, !"evm.runtime", i32 1} diff --git a/lld/unittests/EVM/Inputs/undefDeployIr.ll b/llvm/unittests/Target/EVM/Inputs/undefDeployIr.ll similarity index 100% rename from lld/unittests/EVM/Inputs/undefDeployIr.ll rename to llvm/unittests/Target/EVM/Inputs/undefDeployIr.ll diff --git a/lld/unittests/EVM/Inputs/undefDeployedIr.ll b/llvm/unittests/Target/EVM/Inputs/undefDeployedIr.ll similarity index 85% rename from lld/unittests/EVM/Inputs/undefDeployedIr.ll rename to llvm/unittests/Target/EVM/Inputs/undefDeployedIr.ll index e1ec57393ad1..47186fa84aa8 100644 --- a/lld/unittests/EVM/Inputs/undefDeployedIr.ll +++ b/llvm/unittests/Target/EVM/Inputs/undefDeployedIr.ll @@ -11,3 +11,5 @@ define i256 @foo() { } !1 = !{!"library_id2"} !2 = !{!"id"} +!llvm.module.flags = !{!100} +!100 = !{i32 1, !"evm.runtime", i32 1} diff --git a/llvm/utils/gn/build/write_library_dependencies.py b/llvm/utils/gn/build/write_library_dependencies.py index 00a50918e11c..ba1a8a015731 100644 --- a/llvm/utils/gn/build/write_library_dependencies.py +++ b/llvm/utils/gn/build/write_library_dependencies.py @@ -18,12 +18,10 @@ bool IsInstalled; /// The list of libraries required when linking this component. - /// EVM local begin - const char *RequiredLibraries[88]; - } AvailableComponents[88] = { - /// EVM local end + const char *RequiredLibraries[84]; + } AvailableComponents[84] = { { "aggressiveinstcombine", "LLVMAggressiveInstCombine", true, {"analysis", "core", "support", "transformutils"} }, -{ "all", nullptr, true, {"demangle", "support", "tablegen", "core", "fuzzmutate", "filecheck", "interfacestub", "irreader", "codegen", "selectiondag", "asmprinter", "mirparser", "globalisel", "binaryformat", "bitreader", "bitwriter", "bitstreamreader", "dwarflinker", "extensions", "frontendopenmp", "transformutils", "instrumentation", "aggressiveinstcombine", "instcombine", "scalaropts", "ipo", "vectorize", "objcarcopts", "coroutines", "cfguard", "linker", "analysis", "lto", "mc", "mcparser", "mcdisassembler", "mca", "object", "objectyaml", "option", "remarks", "debuginfodwarf", "debuginfogsym", "debuginfomsf", "debuginfocodeview", "debuginfopdb", "symbolize", "executionengine", "interpreter", "jitlink", "mcjit", "orcjit", "orcshared", "orctargetprocess", "runtimedyld", "target", "asmparser", "lineeditor", "profiledata", "coverage", "passes", "textapi", "dlltooldriver", "libdriver", "xray", "windowsmanifest", "lld"} }, +{ "all", nullptr, true, {"demangle", "support", "tablegen", "core", "fuzzmutate", "filecheck", "interfacestub", "irreader", "codegen", "selectiondag", "asmprinter", "mirparser", "globalisel", "binaryformat", "bitreader", "bitwriter", "bitstreamreader", "dwarflinker", "extensions", "frontendopenmp", "transformutils", "instrumentation", "aggressiveinstcombine", "instcombine", "scalaropts", "ipo", "vectorize", "objcarcopts", "coroutines", "cfguard", "linker", "analysis", "lto", "mc", "mcparser", "mcdisassembler", "mca", "object", "objectyaml", "option", "remarks", "debuginfodwarf", "debuginfogsym", "debuginfomsf", "debuginfocodeview", "debuginfopdb", "symbolize", "executionengine", "interpreter", "jitlink", "mcjit", "orcjit", "orcshared", "orctargetprocess", "runtimedyld", "target", "asmparser", "lineeditor", "profiledata", "coverage", "passes", "textapi", "dlltooldriver", "libdriver", "xray", "windowsmanifest"} }, { "all-targets", nullptr, true, {} }, { "analysis", "LLVMAnalysis", true, {"binaryformat", "core", "object", "profiledata", "support"} }, { "asmparser", "LLVMAsmParser", true, {"binaryformat", "core", "support"} }, @@ -93,12 +91,6 @@ { "vectorize", "LLVMVectorize", true, {"analysis", "core", "support", "transformutils"} }, { "windowsmanifest", "LLVMWindowsManifest", true, {"support"} }, { "xray", "LLVMXRay", true, {"support", "object"} }, -/// EVM local begin -{ "lldcommon", "lldCommon", true, {} }, -{ "lldelf", "lldELF", true, {} }, -{ "lldc", "lldC", true, {} }, -{ "lld", "null", true, {"lldcommon", "lldelf", "lldc"} }, -/// EVM local end }; """