Skip to content

[EVM] Add a custom linker and drop lld dependency - #234

Open
PavelKopyl wants to merge 2 commits into
mainfrom
kpv-custom-linker
Open

PavelKopyl wants to merge 2 commits into
mainfrom
kpv-custom-linker

Conversation

@PavelKopyl

@PavelKopyl PavelKopyl commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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.

NomicFoundation/solx#923

@PavelKopyl
PavelKopyl marked this pull request as draft November 12, 2026 00:29
@PavelKopyl
PavelKopyl requested a balanced review from Copilot November 12, 2026 00:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unchecked layout arithmetic and incorrect relocation bounds can cause heap corruption or overwrite metadata, while insufficient object validation and missing C API visibility annotations can produce invalid output or prevent client linking.

3 open issues
What changed in this PR

This PR replaces the EVM backend’s lld-based linking path with a dedicated LLVM component that assembles contract objects, resolves relocations, and produces bytecode.

Changes:

  • Adds LLVMEVMLinker and moves metadata handling into LLVMAssembleEVM.
  • Replaces five 32-bit library-address relocations with one 160-bit relocation.
  • Moves linker tests into LLVM and removes EVM-specific lld and ObjCopy support.
File Description
llvm/​utils/​gn/​build/​write_library_dependencies.py Removes lld component entries.
llvm/​unittests/​Target/​EVM/​Inputs/​undefDeployIr.ll Adds unresolved deployment-symbol fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​undefDeployedIr.ll Adds unresolved runtime-symbol fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​R_deployed.ll Adds nested runtime dependency fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​R_deploy.ll Adds nested deployment dependency fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​deployIr.ll Adds deployment linking fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​deployedIr.ll Adds runtime linking fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​D_deployed.ll Adds runtime immutable fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​D_deploy.ll Adds deployment size/offset fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​A_deployed.ll Adds runtime library-reference fixture.
llvm/​unittests/​Target/​EVM/​Inputs/​A_deploy.ll Adds deployment dependency fixture.
llvm/​unittests/​Target/​EVM/​EVMLinkerTest.cpp Migrates tests to the new API.
llvm/​unittests/​Target/​EVM/​CMakeLists.txt Registers linker tests and fixtures.
llvm/​test/​MC/​EVM/​data-linker-symbol-relocs.ll Checks single 160-bit relocations.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMTargetStreamer.h Removes wide-symbol streaming interfaces.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMTargetStreamer.cpp Removes split-relocation emission.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMMCTargetDesc.h Removes indexed-symbol declarations.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMMCTargetDesc.cpp Removes indexed-symbol helpers.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMMCExpr.cpp Removes trailing whitespace only.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMMCCodeEmitter.cpp Selects 160-bit fixups for library addresses.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMFixupKinds.h Defines the 160-bit fixup.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMELFObjectWriter.cpp Maps the new fixup to its relocation.
llvm/​lib/​Target/​EVM/​MCTargetDesc/​EVMAsmBackend.cpp Keeps 160-bit fixups unresolved for linking.
llvm/​lib/​Target/​EVM/​Linker/​EVMLinkerQueries.cpp Implements object query and disposal APIs.
llvm/​lib/​Target/​EVM/​Linker/​EVMLinker.cpp Implements assembly, relocation, and output generation.
llvm/​lib/​Target/​EVM/​Linker/​CMakeLists.txt Defines the linker component.
llvm/​lib/​Target/​EVM/​EVMAsmPrinter.cpp Emits one symbol per library address.
llvm/​lib/​Target/​EVM/​CMakeLists.txt Includes the linker subdirectory.
llvm/​lib/​Support/​FileOutputBuffer.cpp Removes EVM-specific stream buffers.
llvm/​lib/​ObjCopy/​ObjCopyC/​ObjCopyC.cpp Deletes the metadata-insertion wrapper.
llvm/​lib/​ObjCopy/​ObjCopyC/​CMakeLists.txt Removes the ObjCopy C component.
llvm/​lib/​ObjCopy/​CMakeLists.txt Stops including ObjCopyC.
llvm/​include/​llvm/​Support/​FileOutputBuffer.h Removes the stream-buffer factory.
llvm/​include/​llvm/​BinaryFormat/​ELFRelocs/​EVM.def Defines R_EVM_DATA_160.
llvm/​include/​llvm-c/​ObjCopy.h Removes the metadata-insertion API.
llvm/​include/​llvm-c/​EVMLinker.h Exposes the linker API and metadata parameters.
llvm/​cmake/​modules/​LLVM-Build.cmake Removes injected lld dependencies.
lld/​unittests/​EVM/​CMakeLists.txt Removes the old EVM test target.
lld/​unittests/​CMakeLists.txt Stops including EVM tests.
lld/​test/​lit.cfg.py Removes EVM-specific test exclusions.
lld/​lld-c/​LLDAsLibraryC.cpp Deletes the lld-based EVM implementation.
lld/​lld-c/​CMakeLists.txt Removes the lld C wrapper target.
lld/​include/​lld/​Common/​Driver.h Removes memory-buffer driver interfaces.
lld/​ELF/​Writer.cpp Removes EVM assembly and memory-output paths.
lld/​ELF/​Target.h Removes the EVM target declaration.
lld/​ELF/​Target.cpp Removes EVM target dispatch.
lld/​ELF/​Options.td Removes --evm-assembly.
lld/​ELF/​InputSection.cpp Removes EVM-specific relocation filtering.
lld/​ELF/​InputFiles.cpp Removes memory-buffer input and EVM bitcode handling.
lld/​ELF/​Driver.cpp Removes EVM-specific driver behavior.
lld/​ELF/​Config.h Removes EVM and memory-buffer settings.
lld/​ELF/​CMakeLists.txt Stops building the EVM target.
lld/​ELF/​Arch/​EVM.cpp Deletes the lld EVM target implementation.
lld/​Common/​DriverDispatcher.cpp Removes memory-buffer dispatch entry points.
lld/​CMakeLists.txt Removes C wrapper build and header installation.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A missing shared header blocks compilation, while incorrect section-name indexing, inadequate relocation bounds checks, and missing C API visibility annotations can corrupt output or prevent dynamic consumers from resolving symbols.

6 open issues

Review effort: Balanced

Comment thread llvm/include/llvm-c/EVMLinker.h Resolved
@PavelKopyl
PavelKopyl force-pushed the kpv-custom-linker branch 2 times, most recently from 033b390 to ddb9298 Compare November 13, 2026 20:08
@PavelKopyl
PavelKopyl requested a balanced review from Copilot November 13, 2026 20:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The public C API and runtime/deploy contract changes require human integration validation because production callers and the frontend supplying the new evm.runtime flag are absent from this checkout.

2 open issues
4 resolved since last review

Review effort: Balanced

Also write the system git config for darker.
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.

@abinavpp abinavpp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazing! thank you! just one question, otherwise lgtm!

* 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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could LLVMAssembleEVM take the library addresses and resolve them in the same relocation pass, so that we can remove LLVMLinkEVM? It would only need the library names and addresses as extra arguments; the relocation loop it already runs would write them.

As far as I can see, nothing in solx consumes a partially linked ELF, which I think is the only case that needs linking as a separate phase. Is that right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants