Skip to content

Commit

Permalink
[EVM] Adding lld-c API to lld-as-library functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelKopyl committed Mar 25, 2024
1 parent 8132b36 commit 335a3d4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions lld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
)
endif()

add_subdirectory(lld-c)
add_subdirectory(Common)
add_subdirectory(tools/lld)

Expand Down
31 changes: 31 additions & 0 deletions lld/include/lld-c/LLDAsLibraryC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//==----- EVMFrameLowering.h - Define frame lowering for EVM --*- 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 to the lld-as-a-library, which can be
// used to invoke LLD functionality.
//
//===----------------------------------------------------------------------===//

#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

/** Performs linkage a operation of an object code via lld-as-a-library.
Input/output files are transferred via memory buffers. */

LLVMBool llvmLinkMemoryBuffers(LLVMMemoryBufferRef *inMemBufs, size_t numInBufs,
LLVMMemoryBufferRef *outMemBuf,
const char **lldArgs, size_t numLldArgs);

LLVM_C_EXTERN_C_END

#endif // LLD_C_LLDASLIBRARYC_H
11 changes: 11 additions & 0 deletions lld/lld-c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_lld_library(lldC
LLDAsLibraryC.cpp

${LLD_INCLUDE_DIR}/lld/Common

LINK_COMPONENTS
Core

DEPENDS
intrinsics_gen
)
30 changes: 30 additions & 0 deletions lld/lld-c/LLDAsLibraryC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "lld-c/LLDAsLibraryC.h"
#include "lld/Common/Driver.h"
#include "lld/Common/ErrorHandler.h"
#include "llvm-c/Core.h"
#include "llvm/Support/MemoryBuffer.h"

LLD_HAS_DRIVER_MEM_BUF(elf)

LLVMBool llvmLinkMemoryBuffers(LLVMMemoryBufferRef *inMemBufs, size_t numInBufs,
LLVMMemoryBufferRef *outMemBuf,
const char **lldArgs, size_t numLldArgs) {
llvm::SmallVector<llvm::MemoryBufferRef> inData(numInBufs);
for (unsigned idx = 0; idx < numInBufs; ++idx)
inData[idx] = *llvm::unwrap(inMemBufs[idx]);

llvm::SmallVector<const char *> args(numLldArgs);
for (unsigned idx = 0; idx < numLldArgs; ++idx)
args[idx] = lldArgs[idx];

llvm::SmallString<0> codeString;
llvm::raw_svector_ostream ostream(codeString);
const lld::Result s =
lld::lldMainMemBuf(inData, &ostream, args, llvm::outs(), llvm::errs(),
{{lld::Gnu, &lld::elf::linkMemBuf}});
llvm::StringRef data = ostream.str();
*outMemBuf =
LLVMCreateMemoryBufferWithMemoryRangeCopy(data.data(), data.size(), "");

return !s.retCode && s.canRunAgain;
}

0 comments on commit 335a3d4

Please sign in to comment.