Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the assembly:
ASM
    extern  printf
    global  hello
    
    section .text
hello:
    push    message
    call    printf
    add     esp, 8
    ret
    
    section .data
message:
    db      'Hello, World', 10, 0


And calling:
nasm -f win64 hello.asm

Works fine, but the linking to the C file doesn't:
gcc main.c hello.obj -o hello.exe


It gives me:
hello.obj:hello.asm:(.text+0x1): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `.data'
collect2.exe: error: ld returned 1 exit status


This is the C file:
C++
#include <stdio.h>
void hello();

int main() {
  hello();
  getchar();
  return 0;
}


What I have tried:

I have tried keeping the "message" thing in the .text section, .rodata, and now .data. They all say this. I also tried nasm -f win32 but at linking it said it didn't support something with i386.
Posted
Updated 24-Nov-22 5:53am
v2

Try this :
ASM
db      'Hello, World', 12, 0
I am guessing the problem is the string is twelve characters long, not ten.
 
Share this answer
 
Comments
Richard MacCutchan 24-Nov-22 11:44am    
The 10 (0x0A) at the end is a newline character, the zero is the end of the string.
It looks like you have created a 64-bit object module from the assembly code. And you hve created a 32-bit module from the C code. When you try to link the two the addresses in the assembly portion are too long. Change the assembler to generate 32-bit code, or the compiler to generate 64-bit.
 
Share this answer
 
Comments
CodeSie Programming 24-Nov-22 12:00pm    
I'll try this, thank you!
By the way, this is 64 bits version of your program. It will work only on Windows because Windows and Linux 64 bits assembly have different calling convention. In addition, Windows requires shadow space on stack before calling another function.

ASM
    extern  printf
    global  hello
    
    section .text
hello:
    push rbp
    mov rbp,rsp
    sub rsp,32 ; Shadow space required by windows

    mov rcx,message ; or lea rcx,[rel message]
    xor rax,rax
    call    printf

    add     rsp, 32 ; we remove shadow space
    pop rbp ; we restore rbp
    ret
    
    section .data
message:
    db      'Hello, World', 10, 0


You can keep the command as in your post

Shell
nasm -f win64 hello.asm -o hello.o
gcc main.c hello.o -o hello.exe
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900