|
| 1 | +//===- EVM.cpp ------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// EVM is a stack-based virtual machine with a word size of 256 bits intendent |
| 10 | +// for execution of smart contracts in Ethereum blockchain environment. |
| 11 | +// |
| 12 | +// Since it is baremetal programming, there's usually no loader to load |
| 13 | +// ELF files on EVMs. You are expected to link your program against address |
| 14 | +// 0 and pull out a .text section from the result using objcopy, so that you |
| 15 | +// can write the linked code to on-chip flush memory. You can do that with |
| 16 | +// the following commands: |
| 17 | +// |
| 18 | +// ld.lld -Ttext=0 -o foo foo.o |
| 19 | +// objcopy -O binary --only-section=.text foo output.bin |
| 20 | +// |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | + |
| 23 | +#include "InputFiles.h" |
| 24 | +#include "Symbols.h" |
| 25 | +#include "Target.h" |
| 26 | +#include "lld/Common/ErrorHandler.h" |
| 27 | +#include "llvm/BinaryFormat/ELF.h" |
| 28 | +#include "llvm/Support/Endian.h" |
| 29 | + |
| 30 | +using namespace llvm; |
| 31 | +using namespace llvm::object; |
| 32 | +using namespace llvm::support::endian; |
| 33 | +using namespace llvm::ELF; |
| 34 | +using namespace lld; |
| 35 | +using namespace lld::elf; |
| 36 | + |
| 37 | +namespace { |
| 38 | +class EVM final : public TargetInfo { |
| 39 | +public: |
| 40 | + uint32_t calcEFlags() const override; |
| 41 | + RelExpr getRelExpr(RelType type, const Symbol &s, |
| 42 | + const uint8_t *loc) const override; |
| 43 | + void relocate(uint8_t *loc, const Relocation &rel, |
| 44 | + uint64_t val) const override; |
| 45 | +}; |
| 46 | +} // namespace |
| 47 | + |
| 48 | +RelExpr EVM::getRelExpr(RelType type, const Symbol &s, |
| 49 | + const uint8_t *loc) const { |
| 50 | + switch (type) { |
| 51 | + case R_EVM_DATA: |
| 52 | + return R_ABS; |
| 53 | + default: |
| 54 | + error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + |
| 55 | + ") against symbol " + toString(s)); |
| 56 | + return R_NONE; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void EVM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { |
| 61 | + switch (rel.type) { |
| 62 | + case R_EVM_DATA: { |
| 63 | + if (val > std::numeric_limits<uint32_t>::max()) |
| 64 | + llvm_unreachable("R_EVM_DATA: to big relocation value"); |
| 65 | + write32be(loc, val); |
| 66 | + break; |
| 67 | + } |
| 68 | + default: |
| 69 | + llvm_unreachable("unknown relocation"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +TargetInfo *elf::getEVMTargetInfo() { |
| 74 | + static EVM target; |
| 75 | + return ⌖ |
| 76 | +} |
| 77 | + |
| 78 | +static uint32_t getEFlags(InputFile *file) { |
| 79 | + return cast<ObjFile<ELF32LE>>(file)->getObj().getHeader().e_flags; |
| 80 | +} |
| 81 | + |
| 82 | +uint32_t EVM::calcEFlags() const { |
| 83 | + assert(!ctx.objectFiles.empty()); |
| 84 | + |
| 85 | + const uint32_t flags = getEFlags(ctx.objectFiles[0]); |
| 86 | + if (auto it = std::find_if_not( |
| 87 | + ctx.objectFiles.begin(), ctx.objectFiles.end(), |
| 88 | + [flags](InputFile *f) { return flags == getEFlags(f); }); |
| 89 | + it != ctx.objectFiles.end()) |
| 90 | + error(toString(*it) + |
| 91 | + ": cannot link object files with incompatible target ISA"); |
| 92 | + |
| 93 | + return flags; |
| 94 | +} |
0 commit comments