Emitting assembler with FBC, compilling and linking with TCC

General FreeBASIC programming questions.
Julcar
Posts: 80
Joined: Oct 19, 2010 18:52
Contact:

Emitting assembler with FBC, compilling and linking with TCC

Postby Julcar » May 31, 2020 7:36

Hi,

let's say the following batch file

Code: Select all

MKDIR test
CD test
ECHO PRINT "Hello World!" > test.bas

REM this will generate test.asm file
FBC -rr -RR test.bas

REM tcc only allows *.s files for assembly
RENAME test.asm test.s

REM this generates test.o
TCC -c test.s

REM this should link test.o into test.exe
TCC -o test.exe test.o


What I am getting as result is tcc: error: undefined symbol 'main'

From what I guess, some libraries are needed, but I am clueless about which, so far, running TCC very verbose, it shows is attempting to link with libtcc1-32.a and crt1.o but still fails looking for the "main" symbol.

Any ideas? thanks.
SARG
Posts: 1196
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Emitting assembler with FBC, compilling and linking with TCC

Postby SARG » May 31, 2020 8:16

You must use -m option to define the entry point : -m <file name without extension>
Look at help file -m / -rr

Code: Select all

FBC -rr -RR test.bas -m test
Julcar
Posts: 80
Joined: Oct 19, 2010 18:52
Contact:

Re: Emitting assembler with FBC, compilling and linking with TCC

Postby Julcar » May 31, 2020 8:40

SARG wrote:You must use -m option to define the entry point : -m <file name without extension>
Look at help file -m / -rr

Code: Select all

FBC -rr -RR test.bas -m test


Tried adding the -m switch but the result is almost the same, except by the fact that now the .asm file generated has a _main: entry point which is better than nothing, but still tcc is unable to generate the executable file.
Julcar
Posts: 80
Joined: Oct 19, 2010 18:52
Contact:

Re: Emitting assembler with FBC, compilling and linking with TCC

Postby Julcar » May 31, 2020 20:46

Ok, after some research I found what is happening in the underground.

FreeBasic's assembler emitter prepares the code to be compiled with GAS and linked with LD, so generates symbols with a leading underscore, so the main entry point is stated as .globl _main: and this is a problem in TCC because it by default dont allow these format, hence when the TCC linker looks for main, it cannot be found and aborts with the above error.

Sources:
https://wiki.tcl-lang.org/page/tcc4tcl
https://lists.nongnu.org/archive/html/t ... 00037.html

TCC has an option flag to specify leading underscores to C symbols, but is not desirable as it leads errors to TCC libraries itself, so the best approch would be to avoid FreeBasic generating symbols with the leading underscore, is there an command line switch for that?

Thanks.
SARG
Posts: 1196
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Emitting assembler with FBC, compilling and linking with TCC

Postby SARG » May 31, 2020 21:44

I found the same information. And I don't think there is a way to supress the underscore.

To go forward I suggest to manually remove it and test with the modified files to see if all goes well.
Julcar
Posts: 80
Joined: Oct 19, 2010 18:52
Contact:

Re: Emitting assembler with FBC, compilling and linking with TCC

Postby Julcar » Jun 01, 2020 8:10

Well, after a day struggling with the TCC linker to find the _main symbol, I figured out what was really happening.

I opened the .o file produced by tcc -c test.s and to my surprise, it was mostly empty!

Then I researched the cause until I found this on TCC docs:

All X86 opcodes are supported. Only ATT syntax is supported


I opened test.s to see its contents:

Code: Select all

.intel_syntax noprefix


Everything now has sense.

Return to “General”

Who is online

Users browsing this forum: No registered users and 16 guests