0

I want to have a function _say_hi() in SASM, but it won't link in my C program:

SECTION .DATA
    hello:     db 'Hello world!',10
    helloLen:  equ $-hello

SECTION .TEXT
    GLOBAL _say_hi

_say_hi:
    mov eax,4            ; write()
    mov ebx,1            ; STDOUT
    mov ecx,hello
    mov edx,helloLen
    int 80h                 ; Interrupt
    ret                        ; Return control

These are my questions

1) Can I run the assembly code if I change the function name? Because when I hit Run, it says:

[00:10:21] Build started...
[00:10:21] Warning! Errors have occurred in the build:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib32/Scrt1.o: In function `_start':
(.text+0x28): undefined reference to `main'
collect2: error: ld returned 1 exit status

Then when I try to link it with my C program:

#include <stdio.h>

int main(int argc, char *argv[]) {
    extern say_hi();
    say_hi();
}

I get:

jorgee@jorgee-Aspire-5830TG:~/Desktop/Orga2$ gcc src/main.c asm/test.o -o main
src/main.c: In function ‘main’:
src/main.c:4:9: warning: type defaults to ‘int’ in declaration of ‘say_hi’ [-Wimplicit-int]
  extern say_hi();
         ^~~~~~
/usr/bin/ld: asm/test.o: relocation R_X86_64_32 against `.DATA' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
  • `extern void say_hi(void);`. How do you assemble? – Antti Haapala -- Слава Україні Nov 26 '18 at 03:22
  • SASM is not an assembler. NASM is. – Antti Haapala -- Слава Україні Nov 26 '18 at 03:23
  • 1
    Also you're building 64-bit exe; can't really use `int 0x80` - https://stackoverflow.com/questions/46087730/what-happens-if-you-use-the-32-bit-int-0x80-linux-abi-in-64-bit-code – Antti Haapala -- Слава Україні Nov 26 '18 at 03:25
  • You're doing a bunch of stuff wrong- build a 32-bit non-PIE executable (instead of 64-bit PIE, which is causing the relocation problems), and don't prefix symbol names with `_`. Linux/ELF uses C symbol names directly in asm. And you're declaring `say_hi` inside `main`, which is weird. – Peter Cordes Nov 26 '18 at 03:30
  • @PeterCordes I was just following a tutorial. I wish to use SIMD instructions, so I guess I should set SASM to work with 64 bits? Could you help me? – JorgeeFG Nov 26 '18 at 03:31
  • I just can't find a tutorial on calling a SASM/NASM function from C :( – JorgeeFG Nov 26 '18 at 03:34
  • Well you're writing 32-bit code. (Using the 32-bit ABI). Using 32-bit absolute addressing in 64-bit mode is fine (and the most efficient choice for putting an address in a register) in x86-64 non-PIE executables, but gcc defaults to PIE on modern distros. [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427) So you have 2 options: write 64-bit code that lets you use all 16 XMM/YMM registers, or write obsolete 32-bit code. For now, do whichever one your tutorial is doing, or find a different tutorial. – Peter Cordes Nov 26 '18 at 03:39
  • See also https://stackoverflow.com/tags/x86/info – Peter Cordes Nov 26 '18 at 03:40

0 Answers0