3

I am trying to call malloc in Visual Studio Community 2019 assembly but I keep getting undefined reference to symbol malloc.

mov rcx,10h
call malloc

Does not compile as I get the undefined reference to malloc I have even tried it with _malloc with the same issue. Am I missing some sort of include?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
BackSpace7777777
  • 159
  • 3
  • 13
  • 1
    This would be a better question if you quoted the *exact* error message, so it was clear it was from the assembler, not at link time. (If you declared `extern malloc` but then didn't link with the C library, you'd expect some kind of undefined reference when linking, after assembling successfully.) – Peter Cordes Oct 21 '20 at 03:20

1 Answers1

2

This was solved by putting an extern in the data section

.data
extern malloc: proc
.code 
;Some code;
BackSpace7777777
  • 159
  • 3
  • 13
  • If this actually solved your problem you should accept your answer! – Marco Bonelli Oct 21 '20 at 00:59
  • I have to wait 2 days – BackSpace7777777 Oct 21 '20 at 01:57
  • Oh, my bad then. I forgot the wait time was so long. Never mind. – Marco Bonelli Oct 21 '20 at 01:57
  • 1
    Are you sure the `extern malloc: proc` has to be in `.data`? That seems odd, unless that creates a placeholder for dynamic linking (a pointer for indirect call) at the position you declare it. I don't use MASM, but in other assemblers for other OSes, e.g. NASM, `extern malloc` could be anywhere and is just a directive that has no interaction with the current section. – Peter Cordes Oct 21 '20 at 03:19