Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly/Linking problem with nasm and ld

I have a sample assembly file that I compile with nasm:

nasm -f elf syscall.asm 

This generates a syscall.o file. I try to link it with ld:

ld -o syscall syscall.o

The ld command fails with the following error:

ld: i386 architecture of input file `syscall.o' is incompatible with i386:x86-64 output

However, if I do

ld -o syscall syscall.o -melf_i386

the command succeeds and I get a syscall executable.

Figuring out that nasm is not generating object code in x86-64 format I added "BITS 64" directive to the beginning of the syscall.asm file.

Then attempting to assemble syscall.asm with nasm gave the following error:

error: elf output format does not support 64-bit code

That seems strange because doing "file /usr/bin/nasm" on my terminal gives:

/usr/bin/nasm: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

My 64-bit Fedora Core 11 has the latest version of nasm installed and my CPU is Intel Core 2 Duo E7200.

[EDIT]

My question is how do I get nasm to emit object files that is compatible with i386:x86-64.

like image 716
ardsrk Avatar asked Feb 11 '10 18:02

ardsrk


People also ask

Is Nasm an assembly language?

NASM uses a variant of Intel assembly syntax instead of AT&T syntax.

How to write Program in nasm?

Writing our programIn our . text section we tell the kernel where to begin execution by providing it with a global label _start: to denote the programs entry point. We will be using the system call sys_write to output our message to the console window. This function is assigned OPCODE 4 in the Linux System Call Table.


1 Answers

Try using elf64 as the output format.

Example run:

$ cat elf64.asm
section .text
        jmp [rax]
$ nasm -f elf64 elf64.asm
$ objdump -Sr elf64.o

elf64.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <.text>:
   0:   ff 20                   jmpq   *(%rax)
like image 136
Chris Jester-Young Avatar answered Sep 19 '22 12:09

Chris Jester-Young