RE: How to compile and link 32 bit c code with 64 bit c code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2010-01-08 at 09:44 +0800, Qing Wang wrote:
> Thank Andrew.
> For how to change modes, it is OK because we can load new gdt/cs/ds/..., change MSR_EFER and stack, and do long jump.
> For how to link two different versions of libc, I don't know why I have this issue. I write my code, even including early_printk().I don't call any library. In all c files, I only include my .h files without any .h files in the library.

I don't know all the detail, but I believe that if your program has a
main function, you are using libc.

> For the fact a binary is either 32- or 64-bit, I maybe make you confused. I admit we can't link a 32-bit object with a 64-bit object. I just want to implement the same functionality of ".code32" and ".code64" in assembly.
> As you know, I can define a section in assembly as 32-bit code, then the compiler (or GAS?) will regard the part as 32 bit. Likewise, I also can define a section as 64-bit code in the same assembly file.
> For example, in the same assembly file, we can write as follows.
> ENTRY(entry32)  .code32  mov $8, %eax  .code64  mov $8, %rax
> Then when running, the first "mov" will be regarded as 32 bit instruction and the second as 64-bit.

You don't need to change modes in this situation. Here's an example:

GAS LISTING mix.s 			page 1


   1              	# mix.s
   2              	# Mix 32-bit and 64-bit.
   3              	# Bob Plantz - 7 Jan. 2010
   4              	        .text
   5              	        .globl  main
   6              	main:
   7 0000 55       	        pushq   %rbp        # save caller's base
pointer
   8 0001 4889E5   	        movq    %rsp, %rbp  # establish our base
pointer
   9              	
  10 0004 4889C3   	        movq    %rax, %rbx  # 64-bit
  11 0007 89C3     	        movl    %eax, %ebx  # 32-bit
  12 0009 6689C3   	        movw    %ax, %bx    # 16-bit
  13              	        
  14 000c 88E0     	        movb    %ah, %al    # 8 bits in 32-bit mode
  15 000e 4188C0   	        movb    %al, %r8b   # 8 bits in 64-bit mode
  16              	       
  17 0011 B8000000 	        movl    $0, %eax    # return 0 to caller
  17      00
  18 0016 4889EC   	        movq    %rbp, %rsp  # restore stack pointer
  19 0019 5D       	        popq    %rbp        # restore caller's base
pointer
  20 001a C3       	        ret                 # back to caller

Notice that most of the mov instructions have a REX prefix (41 or 48).
That's because they use 64-bit mode features.

The mov instructions on lines 11, 12, 14, and 17 do not use 64-bit mode
features. The CPU is in 64-bit mode when it executes this code, but
these instructions are exactly the same as in 32-bit mode.

The pushq and popq instructions do not need a REX prefix because the CPU
is in 64-bit mode. They treat the stack in 64-bit units.

I still don't understand why you don't simply recompile all your C code
on a 64-bit system. Then it will all be 64-bit.

--Bob



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux