when tools that you trust go wrong

For other topics related to the FreeBASIC project or its community.
srvaldez
Posts: 2734
Joined: Sep 25, 2005 21:54

when tools that you trust go wrong

Postby srvaldez » Feb 05, 2021 1:32

I had taken a couple of months break from building FB for my use, then yesterday I decided to do it again
I launch the VM that I use for that purpose and find that my build scripts were gone along with binutils-2.35, it's a mystery to me how things could just disappear, also missing were some toolchains.
a new version of binutils had been released, v2.36, so why not build and use the latest ?, well FB built just fine but the x64 version would fail on the tests with the error
E:\dev108\520x64\fbc-master\bin\win64\ld.exe: warning: E:\dev108\520x64\fbc-master\lib\win64\fbextra.x contains output sections; did you forget -T?
./quirk/inline-asm.o:fake:(.text+0xc2): relocation truncated to fit: R_X86_64_32S against `.text'
make[2]: *** [unit-tests.mk:178: build_tests] Error 1

so I install the missing toolchain with gcc 8.3 from Equation Solution and then I get this error
C:\msys64\mingw64\bin\ar.exe: src/rtlib/ob: No such file or directory
make: *** [makefile:640: lib/freebasic/win64/libfb.a] Error 1

it took me a while to remember that the make.exe that comes from Equation Solution is buggy so deleting it solves the problem because then the shell will use the msys2 make
reverting to binutils-2.35 solves the problem with the tests
marcov
Posts: 3162
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: when tools that you trust go wrong

Postby marcov » Feb 05, 2021 8:09

How does the loading of the constant look like in quirk asm ?

Usually such relocation errors are due to not using RIP relative addressing on 64-bit as mandated by the ABI?
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: when tools that you trust go wrong

Postby Cretin Ho » Feb 05, 2021 9:59

Don't use Equation GCC. They link everything statically and make the binary size bloated.

I suggest TDM-GCC, MinGW-w64 based:

https://jmeubank.github.io/tdm-gcc/download/

Don't use MinGW.org based version.
srvaldez
Posts: 2734
Joined: Sep 25, 2005 21:54

Re: when tools that you trust go wrong

Postby srvaldez » Feb 05, 2021 13:15

marcov wrote:How does the loading of the constant look like in quirk asm ?

Usually such relocation errors are due to not using RIP relative addressing on 64-bit as mandated by the ABI?

hi marcov, this is what the file "inline-asm.bas" found in fbc-master\tests\quirk\ looks like
this code is from the FB source distribution, I post it here for convenience and for examination, I hope that's ok with the developers, my only motivation is to find out if there's a problem with the code

Code: Select all

#include "fbcunit.bi"

#if __FB_ASM__ = "intel"

dim shared f1calls as integer

sub quirk_inline_asm_f1 cdecl alias "quirk_inline_asm_f1"()
   f1calls += 1
end sub

private sub test_proc
   CU_ASSERT( f1calls = 0 )
   asm call quirk_inline_asm_f1
   CU_ASSERT( f1calls = 1 )

   dim f1address as any ptr
   asm
      #ifdef __FB_64BIT__
         mov rax, offset quirk_inline_asm_f1
         mov [f1address], rax
      #else
         mov eax, offset quirk_inline_asm_f1
         mov [f1address], eax
      #endif
   end asm
   CU_ASSERT( f1address = @quirk_inline_asm_f1 )

   goto label2
label1:
   goto label3
label2:
   asm jmp label1
label3:

   '' Inline ASM can contain double quotes etc., and strings in the inline
   '' ASM can contain escape sequences...
   '' This requires the backends to take special care when emitting.
   asm
      jmp ignore
      .ascii $"testing double-quoted string literal, even with \""embedded\"" double quotes and null terminator\0"
      ignore:
   end asm
end sub

SUITE( fbc_tests.quirk.inline_asm )

   '' assuming these tests need to be module level

   TEST( default )
      test_proc
   END_TEST

END_SUITE

#endif
marcov
Posts: 3162
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: when tools that you trust go wrong

Postby marcov » Feb 05, 2021 19:42

srvaldez wrote:[code]
#ifdef __FB_64BIT__
mov rax, offset quirk_inline_asm_f1
mov [f1address], rax


Try to replace the last with
mov [rip+f1address], rax

or

mov [rip][f1address], rax
srvaldez
Posts: 2734
Joined: Sep 25, 2005 21:54

Re: when tools that you trust go wrong

Postby srvaldez » Feb 05, 2021 20:49

no good, f1address translates to -64[rbp] and adding [rip] gives me this Error: `[rip]-64[rbp]' is not a valid base/index expression
SARG
Posts: 1266
Joined: May 27, 2005 7:15
Location: FRANCE

Re: when tools that you trust go wrong

Postby SARG » Feb 05, 2021 22:17

f1address is local so its address is an offset linked to rbp.
Try

Code: Select all

lea rax, quirk_inline_asm_f1[rip] 

or which is equivalent

Code: Select all

 mov rax, offset quirk_inline_asm_f1[rip] 
srvaldez
Posts: 2734
Joined: Sep 25, 2005 21:54

Re: when tools that you trust go wrong

Postby srvaldez » Feb 05, 2021 22:25

hi SARG
I am shutting down for the weekend, will give a try as soon as I can
but your suggestion looks good
srvaldez
Posts: 2734
Joined: Sep 25, 2005 21:54

Re: when tools that you trust go wrong

Postby srvaldez » Feb 07, 2021 2:08

SARG wrote:f1address is local so its address is an offset linked to rbp.
Try

Code: Select all

lea rax, quirk_inline_asm_f1[rip] 

or which is equivalent

Code: Select all

 mov rax, offset quirk_inline_asm_f1[rip] 

either works with gas64, no hiccups
however the tests fail at this point
/e/dev108/830x64/fbc-master/tests/../fbc.exe -i /e/dev108/830x64/fbc-master/tests/../inc -c -w 3 -i fbcunit/inc -m fbc-tests -mt -gen gas64 wstring/wz-conv.bas
/e/dev108/830x64/fbc-master/tests/../fbc.exe -i /e/dev108/830x64/fbc-master/tests/../inc -l fbcunit -l user32 -p fbcunit/lib -x fbc-tests.exe @unit-tests-obj.lst ./fbc-tests.o
./pretest/compile_with_fbcunit.o() warning 20(3): Object files or libraries with mixed multithreading (-mt) options
./gfx/rgb_qb.o() warning 21(3): Object files or libraries with mixed language (-lang) options
E:\dev108\830x64\fbc-master\bin\win64\ld.exe: warning: E:\dev108\830x64\fbc-master\lib\win64\fbextra.x contains output sections; did you forget -T?
./compound/select_const.o:fake:(.text+0xb4): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x1bd): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x2c4): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x3d5): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x53e): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x5d4): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x659): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x6e0): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x767): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x863): relocation truncated to fit: R_X86_64_32S against `.data'
./compound/select_const.o:fake:(.text+0x902): additional relocation overflows omitted from the output
make[2]: *** [unit-tests.mk:178: build_tests] Error 1
make[2]: Leaving directory '/e/dev108/830x64/fbc-master/tests'
make[1]: *** [Makefile:158: unit-tests] Error 2
make[1]: Leaving directory '/e/dev108/830x64/fbc-master/tests'
make: *** [makefile:783: unit-tests] Error 2

this was using gas64
Last edited by srvaldez on Feb 07, 2021 2:57, edited 3 times in total.
srvaldez
Posts: 2734
Joined: Sep 25, 2005 21:54

Re: when tools that you trust go wrong

Postby srvaldez » Feb 07, 2021 2:19

@SARG
if I use

Code: Select all

   lea rax, quirk_inline_asm_f1[rip] 

then all tests pass using gcc

if I use

Code: Select all

   mov rax, offset quirk_inline_asm_f1[rip] 

then it fails with gcc
jj2007
Posts: 2171
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: when tools that you trust go wrong

Postby jj2007 » Feb 07, 2021 9:24

srvaldez wrote:if I use

Code: Select all

   mov rax, offset quirk_inline_asm_f1[rip] 

then it fails with gcc
Not surprisingly - lea and offset are not compatible to each other. The lea instruction allows indexation to rip (or any other register) - and that's not possible with offset:

Code: Select all

lea rax, somelocation[rip]
mov rax, offset somelocation

However, you can extract a value using an index register:

Code: Select all

.data
somelocation   dd 123, 456
.code
  mov ecx, 4
  mov eax, somelocation[ecx]
eax will be 456
SARG
Posts: 1266
Joined: May 27, 2005 7:15
Location: FRANCE

Re: when tools that you trust go wrong

Postby SARG » Feb 07, 2021 10:05

@srvaldez Could you give details about your environment and tools used.

@jj2007 You are right. I have just discovered 'offset' with the srvaldez's post. So removing [rip] it's working fine when compiled with gas64.

Code: Select all

sub foo()
end sub

dim as integer adr
asm
   lea rax, foo[rip]
   mov [adr], rax   
end asm
print adr
asm
   mov rax, offset foo
   mov [adr], rax   
end asm
print adr
sleep
srvaldez
Posts: 2734
Joined: Sep 25, 2005 21:54

Re: when tools that you trust go wrong

Postby srvaldez » Feb 07, 2021 10:40

@SARG
my system is Windows 10 pro build 19042
I use the msys2 shell https://www.msys2.org/ with different gcc toolchains, but in this case I used the gcc toolchain with gcc-8.3 from Equation Solution http://www.equation.com/servlet/equation.cmd?fa=fortran
I deleted the buggy make.exe that comes with the toolchain and use the msys2 make
also, for this test I replaced the binutils that came with the toolchain with binutils-2.36, version 2.35 has no complaints about the tests suite
jj2007
Posts: 2171
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: when tools that you trust go wrong

Postby jj2007 » Feb 07, 2021 11:59

SARG wrote:@jj2007 You are right. I have just discovered 'offset' with the srvaldez's post. So removing [rip] it's working fine when compiled with gas64
You can move the offset directly into [adr]:

Code: Select all

sub foo()
end sub

dim as integer adr
asm
   lea rax, foo[rip]
   mov [adr], rax   
end asm
print adr
asm
   mov [adr], offset foo   ' <<<<<<<<<<<<<<<
end asm
print adr
sleep
SARG
Posts: 1266
Joined: May 27, 2005 7:15
Location: FRANCE

Re: when tools that you trust go wrong

Postby SARG » Feb 07, 2021 13:06

@jj2007 Thanks for the tip.

@srvaldez Maybe the default is position-independent with 2.36. This will be confirmed (unless i'm wrong) by adding -no-pie for linking when running the test suite.
edit : Using select as const there is a jump table with non independent addresses which could explain the errors.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests