Thank you for helping. I use objcopy to transform 32 bit object into 64 bit, and then link it intoother 64 bit objects. Now, I can call my function from 32 bit code part(.code32) of head_64.S (mixed 32/64 bit code). However, in my C code, I cannot access data which is allocated in .data or .bss section. Since there is an offset between compile addressand load address, I guess that I need to have my C code deal with theoffset. In Linux kernel source: arch/x86/boot/compressed/head_64.S, that offset is calculated by the following snippet: leal (BP_scratch+4)(%esi), %esp call 1f1: popl %ebp subl $1b, %ebp So, the offset is saved in ebp register. I think that my Code might workif I can have it aware the offset. But how can I have the C code add anoffset to all variables in .data and .bss sections ? -minskey ---------------------------------------- > From: iant@xxxxxxxxxx > To: minskey_guo@xxxxxxxxxxx > CC: gcc-help@xxxxxxxxxxx > Subject: Re: link 32bit .o with 64bit .o > Date: Mon, 11 Apr 2011 13:47:58 -0700 > > GuoMinskey writes: > > > On x86 platform, is there any means to link 32 bit ELF output into 64 bit ? > > In Linux kernel, arch/x86/boot/compressed/head_64.S, starts from32-bit code and enables CPU to jump into 64-bit. So, that file is assembled into 64 bit ELF object with mixed 32/64 bit code. > > Now, I want to call a routine from somewhere in the 32bit code part of head_64.S. And I write the routine by C language, say test.c and compile it with "gcc -m32 -c -o test.o". How can I link mytest.o to head_64.o ? Or I have to use assembly language to rewritemy routine and embed it directly into head_64.S source code ? > > On x86, 32-bit code can not call 64-bit code, nor vice-versa. The Linux > kernel uses some tricks to start up in 32-bit mode, convert to 64-bit > mode, and branch to 64-bit code as it converts. You can't do the same > tricks yourself; only the kernel can do them. > > So, the answer is that you can link 32-bit and 64-bit code together > easily enough, using the linker's -b option as the kernel build does, > but you can't do anything useful with the result. > > Ian