1

How do I compile/link malloc in assembly when using object files with GCC?
I'm doing something like this:

global test
extern malloc
section .text

test:
    mov rdi, 40
    call malloc
    ret

#include <iostream>
extern "C" void*
test(void);

int
main()
{
    std::cout << test() << "\n";
    return 0;
}

How do I compile this? Doing this:

nasm -f elf64 test.asm
g++ test.o test.cpp

Makes gcc (or ld rather) throw this error:

/usr/bin/ld: test.o: warning: relocation against `malloc@@GLIBC_2.2.5' in read-only section `.text'
/usr/bin/ld: test.o: relocation R_X86_64_PC32 against symbol `malloc@@GLIBC_2.2.5' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ari
  • 31
  • 4

0 Answers0