16

I have a 64-bit version of llvm-gcc, but I want to be able to build both 32-bit and 64-bit binaries. Is there a flag for this? I tried passing -m32 (which works on the regular gcc), but I get an error message like this:

[jay@andesite]$ llvm-gcc -m32 test.c -o test
Warning: Generation of 64-bit code for a 32-bit processor requested.
Warning: 64-bit processors all have at least SSE2.
/tmp/cchzYo9t.s: Assembler messages:
/tmp/cchzYo9t.s:8: Error: bad register name `%rbp'
/tmp/cchzYo9t.s:9: Error: bad register name `%rsp'
...

This is backwards; I want to generate 32-bit code for a 64-bit processor!

I'm running llvm-gcc 4.2, the one that comes with Ubuntu 9.04 x86-64.


EDIT: Here is the relevant part of the output when I run llvm-gcc with the -v flag:

[jay@andesite]$ llvm-gcc -v -m32 test.c -o test.bc
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../llvm-gcc4.2-2.2.source/configure --host=x86_64-linux-gnu --build=x86_64-linux-gnu --prefix=/usr/lib/llvm/gcc-4.2 --enable-languages=c,c++ --program-prefix=llvm- --enable-llvm=/usr/lib/llvm --enable-threads --disable-nls --disable-shared --disable-multilib --disable-bootstrap
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5546) (LLVM build)
 /usr/lib/llvm/gcc-4.2/libexec/gcc/x86_64-linux-gnu/4.2.1/cc1 -quiet -v -imultilib . test.c -quiet -dumpbase test.c -m32 -mtune=generic -auxbase test -version -o /tmp/ccw6TZY6.s

I looked in /usr/lib/llvm/gcc-4.2/libexec/gcc hoping to find another binary, but the only directory there is x86_64-linux-gnu. I will probably look at compiling llvm-gcc from source with appropriate options next.

Agent_9191
  • 7,216
  • 5
  • 33
  • 57
Jay Conrod
  • 28,943
  • 19
  • 98
  • 110
  • The error message you're getting is really odd; can you compile with -v and post the full argument list that llvm-gcc is actually using? – Stephen Canon Sep 24 '09 at 22:20

3 Answers3

8

Try setting:

export CFLAGS="-m32"
export LDFLAGS="-m32"

before compiling...

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
4

Could you try this series of commands and see if it works? Theoretically if you provided llvm-gcc with the -m32 option these steps should be taken by llvm-gcc, but maybe it's not working correctly, so let's make all the steps explicit:

llvm-gcc -m32 -emit-llvm test.c -c -o test.bc
llc test.bc -march=x86 -o test.S
gcc test.S -m32 -o test

This should be the sequence of steps (or something similar) that llvm-gcc performs implicitly, but it looks like in your case it's emitting 64bit assembly for some reason, then trying to assemble and link it for 32bit.

Falaina
  • 6,625
  • 29
  • 31
  • This almost does what I want, but not quite. Although it does in fact produce a 32-bit binary, my test program says sizeof(void*) is 8, which is very worrisome. – Jay Conrod Sep 25 '09 at 18:18
  • Oops, you also have to pass an m32 to llvm gcc too. Try the new set of commands. – Falaina Oct 01 '09 at 01:28
  • 1
    That's the problem though. llvm-gcc is ignoring -m32. – Jay Conrod Oct 01 '09 at 01:41
  • 1
    the initial -m32 is ignored, so test.bc has 64 bit items. From that point on the tools work they just want solutions for 64 bit functions. The gcc -m32 at the end will complain about a library for some 64 bit function not being found, depending on the target and compiler/cross compiler – old_timer Oct 02 '09 at 22:28
3

I had the same problem, llvm-gcc ignores the flags, the only solution that worked was to switch from llvm-gcc to clang which does respect the -m32. That or switch to a 32 bit operating system for llvm-gcc work.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Hmm, I will try out clang tomorrow. – Jay Conrod Oct 01 '09 at 01:48
  • 1
    I didnt use the cutting edge clang, or at least dont think I did as I had problems with it, I have been using whatever release_26 stands for. svn co http://llvm.org/svn/llvm-project/llvm/branches/release_26/ llvm cd llvm cd tools svn co http://llvm.org/svn/llvm-project/cfe/branches/release_26/ clang I basically started here: http://clang.llvm.org/get_started.html – old_timer Oct 01 '09 at 03:47
  • hmmm, formmatting is hosed in the last comment, sorry – old_timer Oct 01 '09 at 03:48