Skip to content

Commit b9d7416

Browse files
bhoumik03partaror
authored andcommitted
Add support for x86_64 emulation options
A test case is added that compiles and links a minimal program with a custom _start() function that performs a syscall to exit with code 5. The test requires --image-base for correct execution. Fixes #105 Signed-off-by: Bhoumik Patidar <[email protected]>
1 parent ac5ebe8 commit b9d7416

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

include/eld/Driver/x86_64LinkDriver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class x86_64LinkDriver : public GnuLdDriver {
4040

4141
virtual ~x86_64LinkDriver() {}
4242

43+
static bool isValidEmulation(llvm::StringRef Emulation );
44+
4345
// Main entry point.
4446
int link(llvm::ArrayRef<const char *> Args,
4547
llvm::ArrayRef<llvm::StringRef> ELDFlagsArgs) override;

lib/LinkerWrapper/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "eld/Driver/GnuLdDriver.h"
1212
#include "eld/Driver/HexagonLinkDriver.h"
1313
#include "eld/Driver/RISCVLinkDriver.h"
14+
#include "eld/Driver/x86_64LinkDriver.h"
1415
#include "eld/PluginAPI/DiagnosticEntry.h"
1516
#include "eld/Support/Memory.h"
1617
#include "eld/Support/TargetRegistry.h"
@@ -182,6 +183,10 @@ Driver::getFlavorAndTripleFromLinkCommand(llvm::ArrayRef<const char *> Args) {
182183
else if (EmulationTriple.getArch() == llvm::Triple::aarch64)
183184
F = Flavor::AArch64;
184185
}
186+
#endif
187+
#if defined(ELD_ENABLE_TARGET_X86_64)
188+
if (x86_64LinkDriver::isValidEmulation(Emulation))
189+
F = Flavor::x86_64;
185190
#endif
186191
if (F == Flavor::Invalid)
187192
return std::make_unique<eld::DiagnosticEntry>(

lib/LinkerWrapper/x86_64LinkDriver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,8 @@ template <class T>
161161
bool x86_64LinkDriver::processLLVMOptions(llvm::opt::InputArgList &Args) {
162162
return GnuLdDriver::processLLVMOptions<T>(Args);
163163
}
164+
165+
166+
bool x86_64LinkDriver::isValidEmulation(llvm::StringRef Emulation){
167+
return Emulation == "elf_x86_64" || Emulation == "elf_amd64";
168+
}

test/lit.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ if config.test_target == 'Hexagon':
277277

278278
if config.test_target == 'X86':
279279
config.march = ''
280-
clang = 'clang'
280+
clang = 'clang -target x86_64-linux-gnu'
281281
clangxx = 'clang++'
282282
llvmmc = 'llvm-mc'
283283
readelf = 'llvm-readelf'
284-
link = 'x86_64-link'
284+
link = 'ld.eld -m elf_x86_64'
285285
ar = 'llvm-ar'
286286
nm = 'llvm-nm'
287287
objdump = 'llvm-objdump'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#---x86_64_Emulation.test----------------- Executable --------------------#
2+
3+
BEGIN_COMMENT
4+
# Test x86_64 emulation support in eld
5+
# This test verifies that eld correctly handles the -m elf_x86_64 emulation flag.
6+
# It compiles a minimal C program with a custom _start() that performs a syscall
7+
# to exit with code 5. The program is linked using eld with --image-base.
8+
#END_COMMENT
9+
10+
#START_TEST
11+
RUN: %clang %clangopts -c %p/Inputs/1.c -o %t.1.o
12+
RUN: %link %linkopts -o %t.1.out %t.1.o --image-base=0x400000
13+
RUN: %t.1.out; echo $? > %t.code
14+
RUN: %filecheck --input-file=%t.code %s --check-prefix=EXITCODE
15+
EXITCODE: 5
16+
#END_TEST
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void _start() {
2+
long u = 5;
3+
asm (
4+
"movq $60, %%rax\n"
5+
"movq %0, %%rdi\n"
6+
"syscall\n"
7+
:
8+
: "r" (u)
9+
: "%rax", "%rdi"
10+
);
11+
}

0 commit comments

Comments
 (0)