Skip to content

How to build for i686-unknown-linux-gnu? #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kpp opened this issue Sep 27, 2022 · 27 comments
Closed

How to build for i686-unknown-linux-gnu? #223

kpp opened this issue Sep 27, 2022 · 27 comments
Labels
bug Something isn't working

Comments

@kpp
Copy link

kpp commented Sep 27, 2022

Hello, I would like to build a x86 only binary. I tried to build libgccjit multiple times but I failed. So I got errors like:

libgccjit.so: error: gcc_jit_types value unsupported on this target: 32

or

libgccjit.so: error: gcc_jit_block_end_with_return: mismatching types: return of (unsigned long long)1 (type: unsigned long long) in function _RINvNtCs1TI45VtEMUH_4core3mem7size_ofaEB4_ (return type: __uint32_t)

Can you please help me?

@antoyo
Copy link
Contributor

antoyo commented Sep 28, 2022

Hi.
Do you use my fork of gcc?

The type 32 is GCC_JIT_TYPE_INT128_T, which I'm assuming isn't supported on your 32-bit target, so it seems the check to see if the type is supported doesn't work in this case.

For the second error, it could just be a bug in libgccjit: are those 2 types (unsigned long long and __uint32_t) of the same size of that target?

@kpp
Copy link
Author

kpp commented Sep 28, 2022

Yes, I use your fork. Here is how I configure it:

../configure --enable-host-shared --enable-languages=jit,c --disable-bootstrap --enable-multilib --target=x86_64-pc-linux-gnu --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --enable-multiarch --prefix=$(pwd)/../install

Also I set TARGET_TRIPLE="i686-unknown-linux-gnu" and context.add_command_line_option("-m32");. Here is the output:

   Compiling rustc-std-workspace-core v1.99.0 (/home/kpp/rustc_codegen_gcc/build_sysroot/sysroot_src/library/rustc-std-workspace-core)
libgccjit.so: error: gcc_jit_types value unsupported on this target: 32
thread 'rustc' panicked at 'file "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/rustc_std_workspace_core-e1b756f39c847da4.rustc_std_workspace_core.6f3949db-cgu.0.rcgu.o" for gnu builder: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/archive.rs:168:92
stack backtrace:

@antoyo
Copy link
Contributor

antoyo commented Sep 28, 2022

Did you add check_context.add_command_line_option("-m32"); here as well?

@kpp
Copy link
Author

kpp commented Sep 28, 2022

No. I did right now, here are new errors:

   Compiling rustc-std-workspace-core v1.99.0 (/home/kpp/rustc_codegen_gcc/build_sysroot/sysroot_src/library/rustc-std-workspace-core)
libgccjit.so: error: gcc_jit_block_end_with_return: mismatching types: return of (unsigned long long)1 (type: unsigned long long) in function _RINvNtCs1TI45VtEMUH_4core3mem7size_ofaEB4_ (return type: __uint32_t)
thread 'rustc' panicked at 'gcc_jit_block_end_with_return: mismatching types: return of (unsigned long long)1 (type: unsigned long long) in function _RINvNtCs1TI45VtEMUH_4core3mem7size_ofaEB4_ (return type: __uint32_t)', /home/kpp/.cargo/git/checkouts/gccjit.rs-13c2e290f2fb9e4d/f30cc2b/src/block.rs:250:17
stack backtrace:

@antoyo
Copy link
Contributor

antoyo commented Sep 28, 2022

This problem is likely to be caused by this code.

You can probably replace LongLong by Int and ULongLong by UInt and remove those asserts.

In the long term, those would need to be replaced by SSizeT (which doesn't exist yet) and SizeT.

@kpp
Copy link
Author

kpp commented Sep 28, 2022

Okay, new errors (several thousand repeating lines):

/tmp/libgccjit-oJAriW/fake.s: Assembler messages:
/tmp/libgccjit-oJAriW/fake.s:12: Error: operand type mismatch for `push'
/tmp/libgccjit-oJAriW/fake.s:17: Error: operand type mismatch for `push'
/tmp/libgccjit-oJAriW/fake.s:19: Error: register save offset not a multiple of 8
/tmp/libgccjit-oJAriW/fake.s:37: Error: operand type mismatch for `push'
/tmp/libgccjit-oJAriW/fake.s:84: Error: operand type mismatch for `push'
/tmp/libgccjit-oJAriW/fake.s:89: Error: operand type mismatch for `push'
/tmp/libgccjit-oJAriW/fake.s:91: Error: register save offset not a multiple of 8
...
/tmp/libgccjit-IywSo2/fake.s:14904: Error: operand size mismatch for `push'
/tmp/libgccjit-IywSo2/fake.s:14905: Error: operand size mismatch for `push'
libgccjit.so: error: error invoking gcc driver
thread 'rustc' panicked at 'file "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/core-f19b364bfdaf6643.core.0944ad2d-cgu.0.rcgu.o" for gnu builder: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/archive.rs:168:92
stack backtrace:

@antoyo
Copy link
Contributor

antoyo commented Sep 28, 2022

Try adding:

context.add_driver_option("-m32");

(also keep the add_command_line_option("-m32") that you already have)

@kpp
Copy link
Author

kpp commented Sep 28, 2022

Nice. New errors:

libgccjit.so: error: bitcast with types of different sizes
input expression (size: 64):
 <parm_decl 0x7f58ca295a80 param0
    type <integer_type 0x7f58ca0eef18 long long unsigned int public unsigned DI
        size <integer_cst 0x7f58ca146438 constant 64>
        unit-size <integer_cst 0x7f58ca146450 constant 8>
        align:64 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type 0x7f58ca0eef18 precision:64 min <integer_cst 0x7f58ca146720 0> max <integer_cst 0x7f58cb99f720 18446744073709551615>
        pointer_to_this <pointer_type 0x7f58ca15a1f8>>
    visited unsigned DI (null):0:0 size <integer_cst 0x7f58ca146438 64> unit-size <integer_cst 0x7f58ca146450 8>
    align:64 warn_if_not_align:0 context <function_decl 0x7f58c9ea8c00 _RNvMs8_NtCs1TI45VtEMUH_4core3numy10swap_bytes> arg-type <integer_type 0x7f58ca0eef18 long long unsigned int>>
requested type (size: 32):
 <integer_type 0x7f58ca0b5d20 long unsigned int public unsigned SI
    size <integer_cst 0x7f58ca1463d8 type <integer_type 0x7f58ca0d31f8 bitsizetype> constant 32>
    unit-size <integer_cst 0x7f58ca1463f0 type <integer_type 0x7f58ca0d3150 sizetype> constant 4>
    align:32 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type 0x7f58ca0b5d20 precision:32 min <integer_cst 0x7f58ca1466c0 0> max <integer_cst 0x7f58ca1466a8 4294967295>>
thread 'rustc' panicked at 'file "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/object-abfd65b48f7bd446.object.e0ea076a-cgu.9.rcgu.o" for gnu builder: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/archive.rs:168:92
stack backtrace:

End the rest:

error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-Wl,--version-script=/tmp/rustcAurIFR/list" "-m32" "/tmp/rustcAurIFR/symbols.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.0.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.1.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.10.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.11.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.12.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.13.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.14.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.15.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.2.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.3.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.4.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.5.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.6.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.7.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.8.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.9.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.1nq9x2lpcwwyuotn.rcgu.rmeta" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.58bouipdls2chlo4.rcgu.o" "-Wl,--as-needed" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/debug/deps" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/build/compiler_builtins-9972fde6461c1c91/out" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/sysroot/lib/rustlib/i686-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libpanic_abort-d5ce9804cef363fd.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libobject-abfd65b48f7bd446.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libmemchr-4f66604b38da23a1.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libaddr2line-f3329d10e621719d.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libgimli-998519a3aed8bee2.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/librustc_demangle-a1f8e35e1a587050.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libstd_detect-2437a603efac1c77.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libhashbrown-ca28e33777a5456e.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libminiz_oxide-b76206b5cc0f54dc.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libadler-005e7ee1a9654d21.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/librustc_std_workspace_alloc-decda90df4114b6f.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libunwind-83b2388ad53bb671.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libcfg_if-fd7048576cf190a7.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/liblibc-828b4581b4d80112.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/liballoc-ecac7f3e180dbdcf.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/librustc_std_workspace_core-e1b756f39c847da4.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libcore-f19b364bfdaf6643.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libcompiler_builtins-2722113640365b95.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/sysroot/lib/rustlib/i686-unknown-linux-gnu/lib" "-o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libstd.so" "-shared" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: cc: error: /home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.13.rcgu.o: No such file or directory
          

error: could not compile `std` due to previous error

@antoyo
Copy link
Contributor

antoyo commented Sep 28, 2022

Try changing this line to the following:

arg = self.cx.context.new_cast(None, arg, param_type);

@kpp
Copy link
Author

kpp commented Sep 28, 2022

error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-Wl,--version-script=/tmp/rustcEiQ1ed/list" "-m32" "/tmp/rustcEiQ1ed/symbols.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.0.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.1.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.10.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.11.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.12.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.13.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.14.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.15.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.2.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.3.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.4.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.5.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.6.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.7.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.8.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.std.22f73aca-cgu.9.rcgu.o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.1nq9x2lpcwwyuotn.rcgu.rmeta" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.58bouipdls2chlo4.rcgu.o" "-Wl,--as-needed" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/debug/deps" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/build/compiler_builtins-9972fde6461c1c91/out" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/sysroot/lib/rustlib/i686-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libpanic_abort-d5ce9804cef363fd.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libobject-abfd65b48f7bd446.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libmemchr-4f66604b38da23a1.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libaddr2line-f3329d10e621719d.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libgimli-998519a3aed8bee2.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/librustc_demangle-a1f8e35e1a587050.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libstd_detect-2437a603efac1c77.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libhashbrown-ca28e33777a5456e.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libminiz_oxide-b76206b5cc0f54dc.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libadler-005e7ee1a9654d21.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/librustc_std_workspace_alloc-decda90df4114b6f.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libunwind-83b2388ad53bb671.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libcfg_if-fd7048576cf190a7.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/liblibc-828b4581b4d80112.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/liballoc-ecac7f3e180dbdcf.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/librustc_std_workspace_core-e1b756f39c847da4.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libcore-f19b364bfdaf6643.rlib" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libcompiler_builtins-2722113640365b95.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/kpp/rustc_codegen_gcc/build_sysroot/sysroot/lib/rustlib/i686-unknown-linux-gnu/lib" "-o" "/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/libstd.so" "-shared" "-Wl,-zrelro,-znow" "-nodefaultlibs"

  = note: /usr/bin/ld: i386:x86-64 architecture of input file `/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.58bouipdls2chlo4.rcgu.o' is incompatible with i386 output
          collect2: error: ld returned 1 exit status
          

error: could not compile `std` due to previous error

@antoyo
Copy link
Contributor

antoyo commented Sep 28, 2022

Can you please try running ./clean_all.sh and try compiling the sysroot again in case you have leftover object files in the wrong format?

@kpp
Copy link
Author

kpp commented Sep 28, 2022

No, didn't help.

@kpp
Copy link
Author

kpp commented Sep 28, 2022

$ objdump -S /home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.58bouipdls2chlo4.rcgu.o 

/home/kpp/rustc_codegen_gcc/build_sysroot/target/i686-unknown-linux-gnu/debug/deps/std.58bouipdls2chlo4.rcgu.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <__rust_alloc>:
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	48 83 ec 10          	sub    $0x10,%rsp
   8:	89 7d fc             	mov    %edi,-0x4(%rbp)
   b:	89 75 f8             	mov    %esi,-0x8(%rbp)
   e:	8b 55 f8             	mov    -0x8(%rbp),%edx
  11:	8b 45 fc             	mov    -0x4(%rbp),%eax
  14:	89 d6                	mov    %edx,%esi
  16:	89 c7                	mov    %eax,%edi
  18:	e8 00 00 00 00       	callq  1d <__rust_alloc+0x1d>
  1d:	c9                   	leaveq 
  1e:	c3                   	retq   

Which is defined in build_sysroot/sysroot_src/library/alloc/src/alloc.rs

@kpp
Copy link
Author

kpp commented Sep 28, 2022

Adding -m32 here worked!

@antoyo
Copy link
Contributor

antoyo commented Sep 28, 2022

Nice!

We should keep this issue open to have a list of what needs to be fixed and also to think about how to support multiarch easily.

@kpp
Copy link
Author

kpp commented Sep 28, 2022

I created a working ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV) binary!

I will try to reduce the number of changed lines and post the patch.

@kpp
Copy link
Author

kpp commented Sep 28, 2022

diff --git a/config.sh b/config.sh
index b25e215..18574f2 100644
--- a/config.sh
+++ b/config.sh
@@ -20,8 +20,9 @@ else
 fi
 
 HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ")
-TARGET_TRIPLE=$HOST_TRIPLE
+#TARGET_TRIPLE=$HOST_TRIPLE
 #TARGET_TRIPLE="m68k-unknown-linux-gnu"
+TARGET_TRIPLE="i686-unknown-linux-gnu"
 
 linker=''
 RUN_WRAPPER=''
@@ -33,6 +34,8 @@ if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
       # We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
       linker='-Clinker=aarch64-linux-gnu-gcc'
       RUN_WRAPPER='qemu-aarch64 -L /usr/aarch64-linux-gnu'
+   elif [[ "$TARGET_TRIPLE" == "i686-unknown-linux-gnu" ]]; then
+      : # do nothing
    else
       echo "Unknown non-native platform"
    fi
diff --git a/src/back/write.rs b/src/back/write.rs
index efcf18d..e640fbe 100644
--- a/src/back/write.rs
+++ b/src/back/write.rs
@@ -14,6 +14,8 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, _diag_han
     let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &*module.name);
     {
         let context = &module.module_llvm.context;
+        context.add_command_line_option("-m32");
+        context.add_driver_option("-m32");
 
         let module_name = module.name.clone();
         let module_name = Some(&module_name[..]);
diff --git a/src/base.rs b/src/base.rs
index 8cc9581..fb8bd88 100644
--- a/src/base.rs
+++ b/src/base.rs
@@ -98,7 +98,7 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_
         context.add_command_line_option("-mpclmul");
         context.add_command_line_option("-mfma");
         context.add_command_line_option("-mfma4");
-        context.add_command_line_option("-m64");
+        context.add_command_line_option("-m32");
         context.add_command_line_option("-mbmi");
         context.add_command_line_option("-mgfni");
         context.add_command_line_option("-mavxvnni");
diff --git a/src/context.rs b/src/context.rs
index 2699559..056352a 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -161,13 +161,13 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
         let ulonglong_type = context.new_c_type(CType::ULongLong);
         let sizet_type = context.new_c_type(CType::SizeT);
 
-        let isize_type = context.new_c_type(CType::LongLong);
-        let usize_type = context.new_c_type(CType::ULongLong);
+        let isize_type = context.new_c_type(CType::Int);
+        let usize_type = context.new_c_type(CType::UInt);
         let bool_type = context.new_type::<bool>();
 
         // TODO(antoyo): only have those assertions on x86_64.
-        assert_eq!(isize_type.get_size(), i64_type.get_size());
-        assert_eq!(usize_type.get_size(), u64_type.get_size());
+        assert_eq!(isize_type.get_size(), i32_type.get_size());
+        assert_eq!(usize_type.get_size(), u32_type.get_size());
 
         let mut functions = FxHashMap::default();
         let builtins = [
diff --git a/src/int.rs b/src/int.rs
index 0c5dab0..5fd4925 100644
--- a/src/int.rs
+++ b/src/int.rs
@@ -524,7 +524,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
         // when having proper sized integer types.
         let param_type = bswap.get_param(0).to_rvalue().get_type();
         if param_type != arg_type {
-            arg = self.bitcast(arg, param_type);
+            arg = self.cx.context.new_cast(None, arg, param_type);
         }
         self.cx.context.new_call(None, bswap, &[arg])
     }
diff --git a/src/lib.rs b/src/lib.rs
index e43ee5c..8fb5823 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -104,6 +104,7 @@ impl CodegenBackend for GccCodegenBackend {
         let temp_dir = TempDir::new().expect("cannot create temporary directory");
         let temp_file = temp_dir.into_path().join("result.asm");
         let check_context = Context::default();
+        check_context.add_command_line_option("-m32");
         check_context.set_print_errors_to_stderr(false);
         let _int128_ty = check_context.new_c_type(CType::UInt128t);
         // NOTE: we cannot just call compile() as this would require other files than libgccjit.so.

@antoyo antoyo added the bug Something isn't working label Sep 28, 2022
@kpp
Copy link
Author

kpp commented Nov 19, 2022

@antoyo I found out "int $$0x80" fails with:

/tmp/libgccjit-BW7WyB/fake.s: Assembler messages:
/tmp/libgccjit-BW7WyB/fake.s:14: Error: operand size mismatch for `int'
libgccjit.so: error: error invoking gcc driver

whereas "int 0x80" works fine.

MRE:

fn main() {
    unsafe {
        core::arch::asm!(
            "int $$0x80", // whereas "int 0x80" works
        );
    }
}

@bjorn3
Copy link
Member

bjorn3 commented Nov 19, 2022

int $0x80 is AT&T syntax, isn't it? It shouldn't work with the Intel syntax, right? In any case GAS doesn't accept it either, so it is completely fine to reject it. From the inline asm rfc and the rust reference:

Assembly code that does not conform to the GAS syntax will result in assembler-specific behavior.

@kpp
Copy link
Author

kpp commented Nov 19, 2022

That's how I got here: https://github.com/jasonwhite/syscalls/blob/288d146210fd378999d0f825ffef9cb3a002f38e/src/syscall/x86.rs#L28. Why is it working with the LLVM backend but not with yours?

att_syntax does not help either, maybe because of context.add_command_line_option("-masm=intel").

@bjorn3
Copy link
Member

bjorn3 commented Nov 19, 2022

LLVM tries to be compatible with GCC/GAS, but because of a license incompatibility (Apache-2.0 WITH LLVM-exception vs GPL-3.0) LLVM can't copy the parser code from GAS and instead has to write it from scratch while attempting to stay compatible with all weirdness. This doesn't always work. For example at some point LLVM didn't accept jmp 1b in intel syntax rather than interpreting it as jumping to label 1 backwards. (rust-lang/rust#48797)

att_syntax does not help either, maybe because of context.add_command_line_option("-masm=intel").

The att_syntax flag should be implemented based on a look at the source.

(by the way @antoyo is the author of cg_gcc. I'm the author of cg_clif.)

@antoyo
Copy link
Contributor

antoyo commented Nov 19, 2022

att_syntax does not help either, maybe because of context.add_command_line_option("-masm=intel").

The -masm=intel should not be an issue to use att_syntax.

One thing that could be causing an issues is if the LLVM flag --x86-asm-syntax=att is used as it is not supported.

That would help if you would post the complete /tmp/libgccjit-BW7WyB/fake.s file. That might require setting CG_GCCJIT_KEEP_INTERMEDIATES =1 or another environment variable.

@kpp
Copy link
Author

kpp commented Nov 19, 2022

intermediate files written to /tmp/libgccjit-FCujtf
/tmp/libgccjit-TeTOpc/fake.s: Assembler messages:
/tmp/libgccjit-TeTOpc/fake.s:14: Error: operand size mismatch for `int'
libgccjit.so: error: error invoking gcc driver
/tmp/libgccjit-TeTOpc/fake.s
	.file	"fake.c"
	.intel_syntax noprefix
	.text
.Ltext0:
	.cfi_sections	.debug_frame
	.file 1 "/tmp/libgccjit-TeTOpc/fake.c"
	.section	.text._RNvCsiACka6b3C2a_13refbig_struct4main,"ax",@progbits
	.type	_RNvCsiACka6b3C2a_13refbig_struct4main, @function
_RNvCsiACka6b3C2a_13refbig_struct4main:
.LFB9:
	.cfi_startproc
.L18:
#APP
	int $$0x80
#NO_APP
	ret
	.cfi_endproc
.LFE9:
	.size	_RNvCsiACka6b3C2a_13refbig_struct4main, .-_RNvCsiACka6b3C2a_13refbig_struct4main
	.section	.text.main,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LVL0:
.LFB10:
	.cfi_startproc
.L20:
	push	ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	mov	ebp, esp
	.cfi_def_cfa_register 5
	sub	esp, 12
	push	DWORD PTR [ebp+12]
	push	DWORD PTR [ebp+8]
	push	OFFSET FLAT:_RNvCsiACka6b3C2a_13refbig_struct4main
	call	_RINvNtCsgoyGFMMcn3w_3std2rt10lang_startuECsiACka6b3C2a_13refbig_struct
.LVL1:
	leave
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
.LFE10:
	.size	main, .-main
	.section	.text.__x86.get_pc_thunk.ax,"axG",@progbits,__x86.get_pc_thunk.ax,comdat
	.globl	__x86.get_pc_thunk.ax
	.hidden	__x86.get_pc_thunk.ax
	.type	__x86.get_pc_thunk.ax, @function
__x86.get_pc_thunk.ax:
.LFB11:
	.cfi_startproc
	mov	eax, DWORD PTR [esp]
	ret
	.cfi_endproc
.LFE11:
	.text
.Letext0:
	.section	.debug_info,"",@progbits
.Ldebug_info0:
	.long	0xab
	.value	0x5
	.byte	0x1
	.byte	0x4
	.long	.Ldebug_abbrev0
	.uleb128 0x3
	.long	.LASF3
	.byte	0x2
	.long	.LASF4
	.long	.LASF5
	.long	.LLRL0
	.long	0
	.long	.Ldebug_line0
	.uleb128 0x4
	.long	.LASF6
	.long	0x6a
	.long	.LFB10
	.long	.LFE10-.LFB10
	.uleb128 0x1
	.byte	0x9c
	.long	0x6a
	.uleb128 0x1
	.long	.LASF0
	.long	0x6a
	.uleb128 0x2
	.byte	0x91
	.sleb128 0
	.uleb128 0x1
	.long	.LASF1
	.long	0x71
	.uleb128 0x2
	.byte	0x91
	.sleb128 4
	.uleb128 0x5
	.long	.LASF7
	.long	0x6a
	.uleb128 0x1
	.byte	0x50
	.uleb128 0x6
	.long	.LVL1
	.long	0xa5
	.byte	0
	.uleb128 0x7
	.byte	0x4
	.byte	0x5
	.string	"int"
	.uleb128 0x2
	.long	0x76
	.uleb128 0x2
	.long	0x7b
	.uleb128 0x8
	.byte	0x1
	.byte	0x8
	.long	.LASF2
	.uleb128 0x9
	.long	.LASF8
	.long	.LFB9
	.long	.LFE9-.LFB9
	.uleb128 0x1
	.byte	0x9c
	.long	0x9f
	.uleb128 0xa
	.long	.LASF9
	.long	0x9f
	.byte	0
	.uleb128 0xb
	.long	.LASF10
	.byte	0
	.uleb128 0xc
	.long	.LASF11
	.long	.LASF11
	.byte	0
	.section	.debug_abbrev,"",@progbits
.Ldebug_abbrev0:
	.uleb128 0x1
	.uleb128 0x5
	.byte	0
	.uleb128 0x3
	.uleb128 0xe
	.uleb128 0x49
	.uleb128 0x13
	.uleb128 0x2
	.uleb128 0x18
	.byte	0
	.byte	0
	.uleb128 0x2
	.uleb128 0xf
	.byte	0
	.uleb128 0xb
	.uleb128 0x21
	.sleb128 4
	.uleb128 0x49
	.uleb128 0x13
	.byte	0
	.byte	0
	.uleb128 0x3
	.uleb128 0x11
	.byte	0x1
	.uleb128 0x25
	.uleb128 0xe
	.uleb128 0x13
	.uleb128 0xb
	.uleb128 0x3
	.uleb128 0xe
	.uleb128 0x1b
	.uleb128 0xe
	.uleb128 0x55
	.uleb128 0x17
	.uleb128 0x11
	.uleb128 0x1
	.uleb128 0x10
	.uleb128 0x17
	.byte	0
	.byte	0
	.uleb128 0x4
	.uleb128 0x2e
	.byte	0x1
	.uleb128 0x3f
	.uleb128 0x19
	.uleb128 0x3
	.uleb128 0xe
	.uleb128 0x27
	.uleb128 0x19
	.uleb128 0x49
	.uleb128 0x13
	.uleb128 0x34
	.uleb128 0x19
	.uleb128 0x11
	.uleb128 0x1
	.uleb128 0x12
	.uleb128 0x6
	.uleb128 0x40
	.uleb128 0x18
	.uleb128 0x7a
	.uleb128 0x19
	.uleb128 0x1
	.uleb128 0x13
	.byte	0
	.byte	0
	.uleb128 0x5
	.uleb128 0x34
	.byte	0
	.uleb128 0x3
	.uleb128 0xe
	.uleb128 0x49
	.uleb128 0x13
	.uleb128 0x2
	.uleb128 0x18
	.byte	0
	.byte	0
	.uleb128 0x6
	.uleb128 0x48
	.byte	0
	.uleb128 0x7d
	.uleb128 0x1
	.uleb128 0x7f
	.uleb128 0x13
	.byte	0
	.byte	0
	.uleb128 0x7
	.uleb128 0x24
	.byte	0
	.uleb128 0xb
	.uleb128 0xb
	.uleb128 0x3e
	.uleb128 0xb
	.uleb128 0x3
	.uleb128 0x8
	.byte	0
	.byte	0
	.uleb128 0x8
	.uleb128 0x24
	.byte	0
	.uleb128 0xb
	.uleb128 0xb
	.uleb128 0x3e
	.uleb128 0xb
	.uleb128 0x3
	.uleb128 0xe
	.byte	0
	.byte	0
	.uleb128 0x9
	.uleb128 0x2e
	.byte	0x1
	.uleb128 0x3
	.uleb128 0xe
	.uleb128 0x27
	.uleb128 0x19
	.uleb128 0x34
	.uleb128 0x19
	.uleb128 0x11
	.uleb128 0x1
	.uleb128 0x12
	.uleb128 0x6
	.uleb128 0x40
	.uleb128 0x18
	.uleb128 0x7a
	.uleb128 0x19
	.uleb128 0x1
	.uleb128 0x13
	.byte	0
	.byte	0
	.uleb128 0xa
	.uleb128 0x34
	.byte	0
	.uleb128 0x3
	.uleb128 0xe
	.uleb128 0x49
	.uleb128 0x13
	.byte	0
	.byte	0
	.uleb128 0xb
	.uleb128 0x13
	.byte	0
	.uleb128 0x3
	.uleb128 0xe
	.uleb128 0xb
	.uleb128 0xb
	.byte	0
	.byte	0
	.uleb128 0xc
	.uleb128 0x2e
	.byte	0
	.uleb128 0x3f
	.uleb128 0x19
	.uleb128 0x3c
	.uleb128 0x19
	.uleb128 0x6e
	.uleb128 0xe
	.uleb128 0x3
	.uleb128 0xe
	.byte	0
	.byte	0
	.byte	0
	.section	.debug_aranges,"",@progbits
	.long	0x24
	.value	0x2
	.long	.Ldebug_info0
	.byte	0x4
	.byte	0
	.value	0
	.value	0
	.long	.LFB9
	.long	.LFE9-.LFB9
	.long	.LFB10
	.long	.LFE10-.LFB10
	.long	0
	.long	0
	.section	.debug_rnglists,"",@progbits
.Ldebug_ranges0:
	.long	.Ldebug_ranges3-.Ldebug_ranges2
.Ldebug_ranges2:
	.value	0x5
	.byte	0x4
	.byte	0
	.long	0
.LLRL0:
	.byte	0x7
	.long	.LFB9
	.uleb128 .LFE9-.LFB9
	.byte	0x7
	.long	.LFB10
	.uleb128 .LFE10-.LFB10
	.byte	0
.Ldebug_ranges3:
	.section	.debug_line,"",@progbits
.Ldebug_line0:
	.section	.debug_str,"MS",@progbits,1
.LASF4:
	.string	"/tmp/libgccjit-TeTOpc/fake.c"
.LASF0:
	.string	"param0"
.LASF1:
	.string	"param1"
.LASF10:
	.string	"struct"
.LASF7:
	.string	"ptrReturnValue6"
.LASF8:
	.string	"_RNvCsiACka6b3C2a_13refbig_struct4main"
.LASF9:
	.string	"undefined"
.LASF5:
	.string	"/home/kpp/rustmustdie/article/refbig_struct"
.LASF11:
	.string	"_RINvNtCsgoyGFMMcn3w_3std2rt10lang_startuECsiACka6b3C2a_13refbig_struct"
.LASF2:
	.string	"unsigned char"
.LASF3:
	.string	"libgccjit 13.0.0 20220828 (experimental) -O0 -g -mtune=generic -march=x86-64 -masm=intel -fno-pic -fno-asynchronous-unwind-tables -Os -fno-var-tracking-assignments -fno-semantic-interposition -fno-strict-aliasing -ffunction-sections -fdata-sections -m32"
.LASF6:
	.string	"main"
	.ident	"GCC: (GNU) 13.0.0 20220828 (experimental)"
	.section	.note.GNU-stack,"",@progbits

@antoyo
Copy link
Contributor

antoyo commented Nov 19, 2022

Yeah, there's nothing that indicates that it should use the AT&T syntax.
It would be interesting to compare the LLVM IR and Asm output of the same Rust code compiled with LLVM.

@kpp
Copy link
Author

kpp commented Nov 19, 2022

Here's your LLVM IR.

#[inline(never)]
#[no_mangle]
unsafe fn some_syscall() {
  core::arch::asm!(
      "int $$0x80",
  );
}

turns into

; Function Attrs: noinline nonlazybind uwtable
define dso_local void @some_syscall() unnamed_addr #0 !dbg !179 {
start:
  call void asm sideeffect alignstack inteldialect "int $$$$0x80", "~{dirflag},~{fpsr},~{flags},~{memory}"(), !dbg !180, !srcloc !181
  br label %bb1, !dbg !180

bb1:                                              ; preds = %start
  ret void, !dbg !182
}

@antoyo
Copy link
Contributor

antoyo commented Nov 19, 2022

So, it seems to me that @bjorn3 is right:

LLVM tries to be compatible with GCC/GAS, but because of a license incompatibility (Apache-2.0 WITH LLVM-exception vs GPL-3.0) LLVM can't copy the parser code from GAS and instead has to write it from scratch while attempting to stay compatible with all weirdness.

As such, this is an incompatibility between the LLVM and GCC codegen and won't be fixed. The library source code should probably be fixed instead.

(Linking the issue you opened for posterity.)

@antoyo
Copy link
Contributor

antoyo commented Dec 21, 2022

@kpp: Can we close this issue or did you have any other issues?

If we can close it, I'll open a new issue with the content of this comment.

@kpp
Copy link
Author

kpp commented Dec 24, 2022

Yes! Thank you for your help!

@kpp kpp closed this as completed Dec 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants