在链接一些对整数类型执行除法和模运算的代码时long long
我收到以下两个错误:
util.c:(.text+0x1af): undefined reference to '__divdi3'
util.c:(.text+0x1ef): undefined reference to '__moddi3'
我也尝试过使用unsigned long long
,但这会导致以下错误:
util.c:(.text+0x1af): undefined reference to '__udivdi3'
util.c:(.text+0x1ef): undefined reference to '__umoddi3'
更换long long
with int
or long
解决了问题,但我需要使用unsigned long long
.
我使用以下命令行来编译和链接程序:
gcc -ffreestanding -c kernel/util.c -o kernel/util.o
ld -o kernel32.bin -Ttext 0x500 kernel/util.o kernel/kernel.o --oformat binary
这是函数:
char* itoa(unsigned long long i, char b[]){
if (i == 0){
b[0] = '0';
b[1] = '\0';
return b;
}
char const digit[] = "0123456789";
char* p = b;
if (i < 0){
*p++ = '-';
i *= -1;
}
unsigned long long shifter = i;
while (shifter){
++p;
shifter = shifter / 10;
}
*p = '\0';
while (i){
*--p = digit[i % 10];
i = i / 10;
}
return b;
}
显然,编译器正在引用__udivdi3
除以整数,但链接器找不到它..
顺便说一句,该二进制文件将用作 32 位操作系统,因此缺少许多标准库
编辑:我正在使用 gcc 4.8.4 和 ld 2.24