How to build rust for i686 on x86_64 to create OS

Hello everyone, I'm newbie.

I would like to re-create my operating system (mopp/Axel) using Rust.
To do this work, I referred some posts in internet.
Embedded Rust Right Now!

I understood that building libcore is needed for target architecture (my OS is only x86_32).
First, I built and installed rust-nightly from github source.
Then, I tried building only libcore using below command.

rustc -C opt-level=2 -Z no-landing-pads --target=i686-unknown-linux-gnu -g --out-dir ./build/ ./repo/rust/src/libcore/lib.rs

I succeeded and got libcore for i686.
I think that this is enough to create OS.
But, this process is little bit complicated.

Next, I tried building rustc can emit x86_64 and i686 binary.
I did install i686-nptl-linux-gnu-cc using crosstool-NG, clone Rust source, make it.
But, I could not build.

My configure option is here.

./configure --prefix=$HOME/.mopp/ --target=i686-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --build=x86_64-unknown-linux-gnu

Then, I got this error messages.

make: jemalloc
cd "i686-unknown-linux-gnu/rt/jemalloc"; "/home/mopp/repo/rust/src/jemalloc/configure"  --with-jemalloc-prefix=je_ --disable-fill  --build=x86_64-unknown-linux-gnu --host=i686-unknown-linux-gnu CC="gcc -m32 " AR="ar" RANLIB="ar s" CPPFLAGS="-I /home/mopp/repo/rust/src/rt/" EXTRA_CFLAGS="-g1 -ffunction-sections -fdata-sections"
checking for xsltproc... /usr/bin/xsltproc
checking for i686-unknown-linux-gnu-gcc... gcc -m32 
checking whether the C compiler works... no
configure: error: in `/home/mopp/repo/rust/i686-unknown-linux-gnu/rt/jemalloc':
configure: error: C compiler cannot create executables
See `config.log' for more details
/home/mopp/repo/rust/mk/rt.mk:351: recipe for target 'i686-unknown-linux-gnu/rt/jemalloc/lib/libjemalloc_pic.a' failed
make: *** [i686-unknown-linux-gnu/rt/jemalloc/lib/libjemalloc_pic.a] Error 77

This error messages may be caused by unavailable 32bit compiler.
I have already cross-compiled gcc, but I don't know how tell its location to configure and make.

Do I need to complete building ?
If anybody was success, could you write how to ?

I'm very sorry for my English and long post.
Thanks.

This type of error from gcc usually means you mistyped a compiler flag or the flag is simply not supported. So check your CFLAGS including the -m32.

Thank for your advice.
I turned on VERBOSE=1 and try again.
Then, CC includes -m32 flag.

cd "i686-unknown-linux-gnu/rt/jemalloc"; "/home/mopp/repo/rust/src/jemalloc/configure"  --with-jemalloc-prefix=je_ --disable-fill  --build=x86_64-unknown-linux-gnu --host=i686-unknown-linux-gnu CC="gcc -m32 " AR="ar" RANLIB="ar s" CPPFLAGS="-I /home/mopp/repo/rust/src/rt/" EXTRA_CFLAGS="-g1 -ffunction-sections -fdata-sections"
checking for xsltproc... /usr/bin/xsltproc
checking for i686-unknown-linux-gnu-gcc... gcc -m32 
checking whether the C compiler works... no
configure: error: in `/home/mopp/repo/rust/i686-unknown-linux-gnu/rt/jemalloc':
configure: error: C compiler cannot create executables
See `config.log' for more details
/home/mopp/repo/rust/mk/rt.mk:351: recipe for target 'i686-unknown-linux-gnu/rt/jemalloc/lib/libjemalloc_pic.a' failed
make: *** [i686-unknown-linux-gnu/rt/jemalloc/lib/libjemalloc_pic.a] Error 77

It seems that my gcc cannot emit 32bit execution file.
Then, I tried compiling simple C program.
It could not compile.
Is this gcc in my environment problem ?
Also, Is gcc, that can emit 32-bit and 64-bit binary, needed ?

I don't see any new info, you'd better paste the full gcc comand line from config.log . However if you want to build 32-bit binaries on x86_64 you definitely need a working cross-compiler (multilib, etc)

This problem is my mistake !
I'm very stupid !!!
And I forgot the word "multilib" completely.

I installed multilib/gcc-multilib (I'm using Arch Linux)
Therefor, I succeeded (╹◡╹)b
And I got 32bit-elf binary.

Thank you very much, @PeteVine !

FYI, I could not build rust using clang and llvm (3.6.2) because it say "cannot find utility header"
While, gcc-multilib can build.

Don't mention it, that was easy :smile:

I can see you're a low-level guy - does your system support old x86 processors? (non-SSE2 and older)

EDIT:
I tested your kernel on my arm system with qemu and it ran ok down to pentium2 - I'm trying to build an i686 P2 rustc as the default distribution defaults to P4. Do you think you might need it yourself or is it going to be all about bare metal?

Thank you for testing my OS.

My low-level knowledges are not so high (This is one of my hobbies).
My OS supports old x86 currently.
When compiling my OS, I passed i386 options to ld and clang.
But, I did not set cpu option of qemu.

Ah, I'm very sorry if I don't understand your question well.
What do you mean P2 and P4, Pentium 2 and 4 ?
I'm planning that I re-implement overall based on exist C source using rust.
Also, re-implemented OS will support old x86.

Yes, I meant pentiums - your kernel didn't work in i586 emulated environment (required p2) and rustc could trip you up there if you're going to rely on default settings.

1 Like

Ok, I will check my kernel in i586 later.
Thank a lot :smile:

Before creating own OS, I'm going to learn features of Rust (e.g., trait, crate, memory management, etc).
If I have good result about creating OS or any question, I would post in this site.