striping of stdio information in llvm-gcc and clang


I have the following test code print.c:

#include <stdio.h>

int main(){
  fprintf(stderr, "Error!\n");
  return 0;

I did the following on linux to cross-compile it for Sparc architecture:
clang -m32 -c print.c -emit-llvm -o print.bc
(or llvm-gcc -m32 -c print.c -emit-llvm -o print.bc)

llvm-dis < print.bc -o print2.bc

llc -march=sparc -mcpu=v8 print2.bc -o print_s.s

if I compile it on Sparc using gcc 3.4
gcc print_s.s -o print_linux

it gives me the following error:
Undefined first referenced
symbol in file
stderr /var/tmp//ccGa4Zxh.o
ld: fatal: Symbol referencing errors. No output written to print_linux
collect2: ld returned 1 exit status

I compiled the same code on Sparc directly, the .s file is the
following without 'stderr'

  .file "print.c"
  .section ".rodata"
  .align 8
  .asciz "Error!\n"
  .section ".text"
  .align 4
  .global main
  .type main, #function
  .proc 04
  save %sp, -112, %sp
  sethi %hi(__iob+32), %g1
  or %g1, %lo(__iob+32), %o0
  sethi %hi(.LLC0), %g1
  or %g1, %lo(.LLC0), %o1
  call fprintf, 0
  mov 0, %g1
  mov %g1, %i0
  .size main, .-main
  .ident "GCC: (GNU) 3.4.6"

While the .s file generated by llc gives the following code with
'stderr' as a symbol.

  .file "print2.bc"
  .globl main
  .align 4
  .type main,@function
main: ! @main
! BB#0:
  save %sp, -104, %sp
  sethi 0, %i0
  sethi %hi(stderr), %l0
  st %i0, [%fp+-4]
  ld [%l0+%lo(stderr)], %o0
  sethi %hi(.L.str), %l0
  add %l0, %lo(.L.str), %o1
  call fprintf
  restore %g0, %g0, %g0
  .size main, .Ltmp0-main

  .type .L.str,@object ! @.str
  .section .rodata.str1.1,"aMS",@progbits,1
  .asciz "Error!\n"
  .size .L.str, 8

Apparently the 'stderr' is not resolved in the linking stage. I tried
adding '-lstdio' in the gcc link stage but it does not work.

Anyone has any idea about how this problem can be resolved?



This step is encoding platform-specific information into print.bc; you
can't just use llc to generate output for an arbitrary architecture
and expect it to work.

clang's cross-compilation support isn't really mature; you might be
able to get away with specifying "-ccc-host-triple sparc-sun-solaris
-nostdinc -I /path/to/sparc/headers/".


Please read that as "clang's cross-compilation support is not magical".
It's not a question of maturity that different Operation Systems and
architectures have incompatible libc ABIs encoded in the system headers.



Hi Joerg,

How do I find /path/to/sparc/headers/?

Many hanks,


If you have a copy of the Solaris environment, just use --sysroot.
E.g. --sysroot=/solaris-install should look for
/solaris-install/usr/include etc.