Linking 32- and 64-bit code together into a single binary

廉价感情. 提交于 2019-12-10 20:53:55

问题


In a comment to this question, Unexpected behaviour in simple pointer arithmetics in kernel space C code, Michael Petch wrote, "The 64-bit ELF format supports 32-bit code sections."

I have a working program that includes both 32- and 64-bit code and switches between them. I have never been able to figure out how to link compiler-generated 32- and 64-bit code together without a linker error, so all the 32-bit code is written in assembly. As the project has become more complex, maintenance of the 32-bit assembly code has become more onerous.

Here is what I have:

test32.cc is compiled with -m32.
All the other source files are compiled without that flag and with -mcmodel=kernel.

In the linker script:

OUTPUT_FORMAT("elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)

In the Makefile:

LD := ld
LDFLAGS := -Map $(TARGET).map -n --script $(LDSCRIPT)
$(LD) $(LDFLAGS) -b elf32-x86-64 $(OBJS64) -b elf32-i386 $(OBJS32) -o $@

I get the error:

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

Changing OUTPUT_ARCH to i386 causes similar errors from all the 64-bit object modules.

I'm using:
gcc 5.4.1
GNU ld (GNU Binutils for Ubuntu) 2.26.1

来源:https://stackoverflow.com/questions/49473061/linking-32-and-64-bit-code-together-into-a-single-binary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!