Link 32-bit and 64-bit code together in one binary

In a commentary on this issue, Unexpected behavior in simple pointer arithmetic in kernel space C-code , Michael Patch wrote: "The 64-bit ELF format supports 32-bit sections of code."

I have a working program that includes both 32- and 64-bit code and switches between them. I could never figure out how to link 32-bit and 64-bit code with the compiler together without a linker error, so all 32-bit code is written in an assembly. As the project became more complex, maintaining 32-bit assembler code became more onerous.

Here is what I have:

test32.cc compiled with -m32.
All other source files are compiled without this flag and -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 an error message:

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 64-bit object modules.

I use:
 gcc 5.4.1
 GNU ld (GNU Binutils for Ubuntu) 2.26.1

+1
source share

Source: https://habr.com/ru/post/1695293/


All Articles