链接问题:输入文件 *.o 的 i386:x86-64 架构与 i386 输出不兼容

发布于 2024-10-19 20:12:31 字数 3164 浏览 5 评论 0原文

当我将 osdev 移动到 Linux 时,我在链接时遇到了问题,它之前在 cygwin 下运行的 gcc 3.5.* 和 binutils 2.18(编译为输出 x86_64-elf)上工作。

以下是信息:

gcc -v


Using built-in specs.
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5) 



ld-v


GNU ld (GNU Binutils for Ubuntu) 2.20.51-system.20100908


ld -help的一部分


....
ld: supported targets: elf32-i386 a.out-i386-linux pei-i386 elf32-little elf32-big elf64-x86-64 pei-x86-64 elf64-l1om elf64-little elf64-big plugin srec symbolsrec verilog tekhex binary ihex trad-core
....




The code:

main.c

void main(){
/**some code here**/
}


start.asm

[bits 64]
global start
extern main
start:
call main


link.ld

OUTPUT_FORMAT(elf64-x86-64);
offset = 0x1000000;
ENTRY(start);
SECTIONS{
. = offset;
.text : AT(ADDR(.text) - offset){
_code = .;
*(.text)
(.rodata)
. = ALIGN(4096);}
.data : AT(ADDR(.data) - offset){
_data = .;
*(.data)
. = ALIGN(4096);}
.bss : AT(ADDR(.bss) - offset){
_bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4096);}
_end = .;
/DISCARD/ :
{*(.comment)}
}


build.sh

nasm -f elf64 -o start.o start.asm
echo .
echo .
echo .
gcc -m64 -ffreestanding -nostdlib -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -c -o main.o main.c
echo .
echo .
echo .
ld -nostdlib -nodefaultlibs -T link.ld -o out.elf start.o main.o



terminal output

.
.
.
.
.
.
ld: i386:x86-64 architecture of input file 'start.o' is incompatible with i386 output
ld: i386:x86-64 architecture of input file 'main.o' is incompatible with i386 output


I don't understand why it displays 'incompatible with i386 output' when there's OUTPUT_FORMAT(elf64-x86-64); in my linker script specifying the targeted output..

I got a problem in linking when I moved my osdev to linux where it worked previously on gcc 3.5.* and binutils 2.18 (compiled to output x86_64-elf) running under cygwin.

Here are the infos:

gcc -v


Using built-in specs.
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5) 

ld -v


GNU ld (GNU Binutils for Ubuntu) 2.20.51-system.20100908


part of ld -help


....
ld: supported targets: elf32-i386 a.out-i386-linux pei-i386 elf32-little elf32-big elf64-x86-64 pei-x86-64 elf64-l1om elf64-little elf64-big plugin srec symbolsrec verilog tekhex binary ihex trad-core
....

The code:

main.c


void main(){
/**some code here**/
}

start.asm


[bits 64]
global start
extern main
start:
call main

link.ld


OUTPUT_FORMAT(elf64-x86-64);
offset = 0x1000000;
ENTRY(start);
SECTIONS{
. = offset;
.text : AT(ADDR(.text) - offset){
_code = .;
*(.text)
(.rodata)
. = ALIGN(4096);}
.data : AT(ADDR(.data) - offset){
_data = .;
*(.data)
. = ALIGN(4096);}
.bss : AT(ADDR(.bss) - offset){
_bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4096);}
_end = .;
/DISCARD/ :
{*(.comment)}
}

build.sh


nasm -f elf64 -o start.o start.asm
echo .
echo .
echo .
gcc -m64 -ffreestanding -nostdlib -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -c -o main.o main.c
echo .
echo .
echo .
ld -nostdlib -nodefaultlibs -T link.ld -o out.elf start.o main.o

terminal output


.
.
.
.
.
.
ld: i386:x86-64 architecture of input file 'start.o' is incompatible with i386 output
ld: i386:x86-64 architecture of input file 'main.o' is incompatible with i386 output

I don't understand why it displays 'incompatible with i386 output' when there's OUTPUT_FORMAT(elf64-x86-64); in my linker script specifying the targeted output..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2024-10-26 20:12:31

试试这个:

LDEMULATION="elf_x86_64"
ld -nostdlib -nodefaultlibs -T link.ld -o out.elf start.o main.o

Try this:

LDEMULATION="elf_x86_64"
ld -nostdlib -nodefaultlibs -T link.ld -o out.elf start.o main.o
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文