I have simple assembly files created by NASM. I want to link them with tcc
. For debugging I want to use printf()
in my assembly code. But when I do so, tcc
fails with tcc: undefined symbol 'printf'
.
Here is a minimal example code to reproduce the error:
extern printf
hello: db "Hello world!",0
global main
main:
push hello
call printf
pop eax
ret
Console:
nasm -felf hello.asm
tcc hello.o
tcc: undefined symbol 'printf'
When I use gcc hello.o
everything works fine, so it has to be a tcc specific problem. How do I get this to work with tcc?
Edit: I'm using a Windows version of NASM and TCC to generate 32-bit Windows executables.
It appears that TCC requires specific type information on functions that are external linkage like
printf
. By default NASM creates references to symbols with a NOTYPE attribute in the ELF objects. This appears to confuse TCC as it seems to expect external function symbols to be marked with a FUNCTION type.I discovered this by taking the simple C program:
and compiling it to an object file (TCC uses ELF objects by default) with a command like:
This generates
simple.o
. I happened to use OBJDUMP to display the assembly code and ELF headers. I didn't see anything unusual in the code but the symbol table in the headers showed a difference. If you use the program READELF you can get a detailed dump of the symbols.Of particular interest is the symbol table entry for
printf
:If you were to dump the ELF headers for your
hello.o
object you'd seem something similar to this:Notice how the symbol
printf
inhello.o
differs from the one insimple.o
above. NASM defines labels by default using NOTYPE attribute rather than a FUNCTION .Use YASM instead of NASM
I don't know of any way to resolve the problem in NASM since I don't know a way to force it to use a FUNCTION type and not NOTYPE on a symbol defined as
extern
. I changed the type in a hex editor and it linked and ran as expected.One alternative is to download YASM (a rewrite of NASM). For the most part NASM and YASM work the same. YASM's command line is mostly compatible with NASM so you should be able to use it as a direct replacement. YASM has an extra feature that allows you to specify the type of a symbol with the
type
directive:You'd only have to add an extra line of type information to your assembly code for each external function you use. Your assembly code could be modified to look like:
It should compile and link with this: