zoltan at bendor.com.au
Tue Mar 15 07:47:28 EDT 2011
> If I use a division operator or a modulo division operator in a
> static function, all compiles well. If I change the static function
> into a global function, I get following errors : Undefined reference
> to '__aeabi_idiv' Undefined reference to '__aeabi_idivmod'
Those are the division routines in gcc's libgcc.a library. Your problem
> CFLAGS = -Wall -Os -nostdlib -Wl,-Ttext=0xA0000000
-nostdlib tells the compiler not to link against the standard
libraries, which include libgcc.a.
> %.elf : $(SRCS)
> $(CC) $(CFLAGS) $^ -o $@
If you add a -lgcc to the line above, it should compile.
> I never encountered this problem before in other projects and I don't
> see why this is working in static functions and not in global
Possibly because if you have static functions, then the compiler can
inline them. If after the inlining it can work out that you are
dividing with a constant, then it won't pull in the division routines,
but substitute division with a multiplication, two shifts and an
addition (the cost of calculating the constant to multiply with and
the shift amounts involves, among other things, a 64-bit division, but
that all happens in compile time - at runtime it's just the mul, shift,
add, which is orders of magnitue faster as well as smaller than a real
division). If you have a global function, gcc must assume that it can
be called with arbitrary arguments from other C files, therefore it must
do real (i.e. runtime) division.
More information about the barebox