Page 1 of 1

undefined reference to `__umoddi3'?

Posted: Thu May 05, 2022 5:18 am
by YDeeps1
I have a kernel which was built on top of the barebones setup (with the exception of using C++) and have recently tried to incorporate LAI (ACPI AML interpreter) because AML is $#!* into my OS but during compilation, I am having a few problems.

I believe the issues are due to libgcc not being linked properly?

Firstly, I am running a 32-bit kernel hence I do not have native support for 64-bit numbers (which LAI sometimes makes use of) and so g++ tries to make calls to __divmod which should solve my problem, except I am getting import errors:
undefined reference to `__udivmoddi4'
undefined reference to `__umoddi3'
I have followed the cross compiler build guide here which should also include libgcc.

How I compile my files (with make):
i686-elf-g++ -lgcc -O2 -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -fno-use-cxa-atexit -g -Wno-unused-variable -Wno-write-strings -Wno-unused-parameter -o $@ -c $<
How I link my object files (with make):
i686-elf-g++ -T $< -o ./dist/$@ -ffreestanding -O2 -nostdlib -lgcc -g $(objects)
i686-elf-g++ -v:
Using built-in specs.
COLLECT_GCC=i686-elf-g++
COLLECT_LTO_WRAPPER=/home/speedy/opt/cross/libexec/gcc/i686-elf/11.2.0/lto-wrapper
Target: i686-elf
Configured with: ../gcc/configure --target=i686-elf --prefix=/home/speedy/opt/cross --disable-nls --enable-languages=c,c++ --without-headers : (reconfigured) ../gcc/configure --target=i686-elf --prefix=/home/speedy/opt/cross --disable-nls --enable-languages=c,c++ --without-headers
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 11.2.0 (GCC)
I should also note I "ported" the LAI library to C++ (by changing extension to cpp & making void*-int casts explicit) which may be part of the problem? (I reckon it'd be easier to port it to C++ and compile it together with everything else than make exceptions for C).

Re: undefined reference to `__umoddi3'?

Posted: Thu May 05, 2022 6:38 am
by davmac314
There's no point adding -lgcc to the compilation step; you can remove it.

For linking, you need -lgcc at the end - after the object files and any other libraries. This is due to how static linking works. A library is only pulled in if it contains definitions for undefined symbols that have been encountered from previous objects / libraries.

Re: undefined reference to `__umoddi3'?

Posted: Thu May 05, 2022 7:01 am
by YDeeps1
davmac314 wrote:There's no point adding -lgcc to the compilation step; you can remove it.

For linking, you need -lgcc at the end - after the object files and any other libraries. This is due to how static linking works. A library is only pulled in if it contains definitions for undefined symbols that have been encountered from previous objects / libraries.
That worked wonders. Thank you very much! I will remember that from now on.