How is linux simultaneously 32bit and 64bit? Or is that something handled in glibc? - linux

How is Linux simultaneously 32bit and 64bit? Or is that something handled in glibc?
I run CentOS 5.3 and it is a "64 bit" version; although I build things for 64 bit and 32 bit. From what I think I know, Windows supposedly has a 32bit emulator. Does Linux do the same thing? Is it in userspace or kernel space?
If libc handles it, is it kind of like a emulator that says, I'll link with 32 bit apps, but speak 64 bit to the kernel?

The cpu can execute both 64 and 32bit instructions and the kernel can switch between modes. The only limitation is that you cannot link 32bit programs against 64bit libraries so you must have both 32 and 64bit versions of libc, etc. installed.

Nothing is stopping the cpu from switching from 64bit to 32bit. It just switches.
You can have a 64 bit kernel, and run 32bit apps. You can even have a 32bit kernel and run 64bit apps(Mac os x).
However you need the libaries they use to also be 32bit or or 64bit, which is why you might see files called lib64 or lib32 on linux for the 64bit or 32bit libaries.

Because x86_64 processors are designed over x86 technology, they are still able to support 32-bit programs without any hardware emulation, like what you would need to run x86 programs in a PowerPC or Sparc environment. In Linux, all you need to do is install the necessary software libraries to run the 32-bit software.

Related

Assembly Level Programming in macos using NASM

My prof. has asked me to do assembly language programming in linux through NASM X86 64 bit.
Since,I have Mac with me the programs will need to be modified for the MacOS, may be due to the system calls and whatever other reasons.
How shall I replicate the same environment as asked by my prof.in my macos ?
Will it be good enough if I install linux in my Macbook ?
If yes, then what will be the difference between the "Linux in Windows PC" and "Linux in Mac" in terms of programs that I will write ?
If no, then how shall I deal with that ?
Will it be good enough if I install linux in my Macbook ?
Installing it in a VM might be more convenient (you don't need to reboot to go from MacOS to Linux), but installing it directly on your Macbook will also work.
If yes, then what will be the difference between the "Linux in Windows PC" and "Linux in Mac" in terms of programs that I will write ?
"Mac vs. PC" marketing terminology not withstanding, Macs run the same types of x64 CPUs as "regular PCs" and Linux doesn't care which other operating systems are also installed on your system. So if you write assembly on a Linux on an x64 Mac, you'll use the exact same x64 instructions and Linux syscalls as you would when running Linux on an x64 non-Mac.

suggest any way to make executables build on 64 run on 32 bit ubuntu

I had an executable and source code of software made on 64 bit linux want to make that executable run on 32 bit linux. How it can be done in how many ways any easy process
Thankss
The only way I see is to virtualize a 64bits Linux on Virtualbox for example.
But your processor must have a 64bit architecture and support virtualization...

Building for PowerPC 405 machine running Linux 2.4.18 with a Mac G4 running 2.4.27

I presently run Linux 2.4.27 (Debian Sarge) on a PowerPC Mac G4 machine. I need to write software for Linux 2.4.18 running on a PowerPC 405 machine, and the binaries I am producing on my Mac G4 running 2.4.27 with both GCC 3.3.5 and GCC 2.95.3 (I have both installed) are having problems; I have built a static version of cURL with both compilers that segfaults when run on this 2.4.18 PowerPC 405 machine. I have noticed that binaries built on a 2.4.18 kernel with a cross compiler work (x86->PPC), however. Unfortunately, the machine that makes those binaries is unavailable to me, and I am really trying to get the Mac to be the "one stop shop" in this development effort (I want to be able to test and run on this machine).
I cannot find a 2.4.18 distribution of Linux for PowerPC anywhere. What can I do to build binaries that will work on a PowerPC 405 machine running Linux 2.4.18 given a PowerPC Mac G4 running a 2.4.27 kernel? Is building GLIBC for 2.2.5 and setting the compilers to use it the answer, or do I have to somehow build a whole kernel of 2.4.18 to support what I am trying to do?
Several weeks later, it appears that the solution suggested by user sessyargc.jp was indeed to use a cross compiler. I am accepting my answer only because I want to close this question out and I can find no way to give sessyargc.jp credit, since he/she only made a comment. Still, thank you sessyargc.jp for pointing the way!
I solved a similar problem in the past, I used the QEMU emulator on my x86 machine.
QEMU emulates the PowerPC-405 CPU too, here is the list of emulated PowerPC CPUs https://github.com/hackndev/qemu/blob/master/target-ppc/STATUS.
QEMU turns your PC in an hypervisor i.e. works like VirtualBox. But QEMU can also emulate CPUs that are different from the host PC one.
You can install and run a PowerPC Linux VM on a standard(cheap) x86 PC and compile your binaries directly in that VM. CPU emulation is a bit slow, but it works.
Regards

Linux stdlibc++ linker error on different computers

I wrote an application in C++ for linux (X11, GLX) and it is working alright on my development computer (32-bit linux on 64-bit capable hardware). However, when I ran it on a 64-bit linux downstairs, I received an error telling me the linker failed to link stdlibc++.so.6, but I thought 32-bit compiled application could run on 64-bit kernels and Oses as well? At least that is the case in Windows... Do I have to separately compile 32 and 64 bit with different libs?
And how do I properly distribute my application? It's a game, and currently you have to run a makefile to let it move its dependencies to the /usr/lib/ directory (a kind of amateur installer). Will this work on all mainstream linux distro's? And are there better, neater ways to release your application?

When compiling x64 code, what's the difference between "x86_amd64" and "amd64"?

When compiling code with VC++, MSDN gives you the option between using the x86_amd64 toolset or the amd64 toolset (when calling vcvarsall.bat).
How do I choose between those two when compile x64 code? Will the amd64 option churn out more efficient x64 machine code than the cross compiler?
It has nothing to do with efficiency. The native and cross-compiler will both generate the same machine code. You will however gain some benefits by running a native 64-bit compiler process on a 64-bit workstation (larger registers, larger memory space, etc...).
The native compiler will only run on an 64-bit copy of Windows, so if your workstation is 32-bit this compiler won't even run.
The cross-compiler is meant to run on x86 machines even though it will run on a 64-bit copy of Windows via WoW; however, there is no reason to do this.
The page you link says it quite well:
x64 on x86 (x64 cross-compiler)
Allows
you to create output files for x64.
This version of cl.exe runs as a
32-bit process, native on an x86
machine and under WOW64 on a 64-bit
Widows operating system.
x64 on x64
Allows you to create output
files for x64. This version of cl.exe
runs as a native process on an x64
machine.
Thanks to Brian R. Bondy for the quote formatting
From what you linked:
x64 on x86 (x64 cross-compiler)
Allows
you to create output files for x64.
This version of cl.exe runs as a
32-bit process, native on an x86
machine and under WOW64 on a 64-bit
Widows operating system.
x64 on x64
Allows you to create output
files for x64. This version of cl.exe
runs as a native process on an x64
machine.
Paraphrased:
If you use x86_amd64, then you are typically developing on an x86 machine and you want to create x64 files that run natively on x64. You could also use this option on an x64 machine but your compiler will be running under WOW64 emulation.
If you use AMD64, then you are developing on an x64 machine and you want to create x64 files that run natively on x64. The compiler is running natively in x64. This option is more efficient to build x64 programs.
You may wonder why you would ever develop an x64 program on an x86 computer, since you can't run it you can't debug it. Well it's still useful for example if you have a build server which is x86 and that build server needs to generate both x86 and x64 outputs.
How is it possible for a compiler to run under x64 if it is an x86 based program (x86_amd64)? That is the same reason you can run any x86 program on your x64 machine... Thanks to WOW64 emulation.
What is WOW64 emulation:
WOW64 emulation happens when you run an x86 program on an x64 computer (or IA64). WOW64 stands for Windows 32 on Windows 64. It is an emulation layer on top of x64 machines which allow you to execute x86 programs.
Your file system operations will be redirected to WOW64 folders and your registry will be redirected to a subnode as well. For example when you try to obtain the folder for program files it will return c:\program files (x86)\ if you are using WOW64 but it will return c:\program files\ if you are using x64.
Another example, for the registry if you try to write to HKLM\Software\Something it will really redirect you to HKLM\SOFTWARE\Wow6432Node\Something without your x86 program's knowledge.
Running a native x64 build will be more efficient than running through WOW64 emulation Why? Because you don't have that extra emulation layer of transforming your 32bit calls into 64bit ones.
By the way if you are running the x64 version of Windows you can see which processes are running through WOW64 because they will have a *32 appended to the process name in the process list.

Resources