Tuesday, November 06, 2007

Is it 32-bit or 64-bit

How to find whether an executable or a running process is 32-bit or 64-bit. I will show you in Solaris as well as in Linux. It will be based on this simple C program.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        unsigned int t;
        t=(unsigned int)atoi(argv[1]);
        sleep(t);
        exit(0);
}

In Solaris, you can differentiate it from either the dynamic dependencies of the executable or the address space of a running process

$ uname -a
SunOS myserver 5.10 Generic_118833-36 sun4u sparc SUNW,UltraSPARC-IIi-cEngine

$ isainfo -v
64-bit sparcv9 applications
        vis
32-bit sparc applications
        vis v8plus div32 mul32

$ which gcc
/usr/sfw/bin/gcc

$ gcc --version
gcc (GCC) 3.4.3 (csl-sol210-3_4-branch+sol_rpath)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc -o a64 -m64 a.c

$ gcc -o a32 -m32 a.c

$ ldd a32
        libc.so.1 =>     /lib/libc.so.1
        libm.so.2 =>     /lib/libm.so.2

$ ldd a64
        libc.so.1 =>     /lib/64/libc.so.1
        libm.so.2 =>     /lib/64/libm.so.2

$ ./a32 100 &
[1] 3392

$ pmap 3392
3392:   ./a32 100
00010000       8K r-x--  /export/home/chihung/tmp/a32
00020000       8K rwx--  /export/home/chihung/tmp/a32
FF280000     864K r-x--  /lib/libc.so.1
FF368000      32K rwx--  /lib/libc.so.1
FF370000       8K rwx--  /lib/libc.so.1
FF3A0000      24K rwx--    [ anon ]
FF3B0000     184K r-x--  /lib/ld.so.1
FF3EE000       8K rwx--  /lib/ld.so.1
FF3F0000       8K rwx--  /lib/ld.so.1
FFBFE000       8K rwx--    [ stack ]
 total      1152K

$ ./a64 100 &
[2] 3394

$ pmap 3394
3394:   ./a64 100
0000000100000000          8K r-x--  /export/home/chihung/tmp/a64
0000000100100000          8K rwx--  /export/home/chihung/tmp/a64
FFFFFFFF7F000000          8K rwx--    [ anon ]
FFFFFFFF7F100000         24K rwx--    [ anon ]
FFFFFFFF7F200000        920K r-x--  /lib/sparcv9/libc.so.1
FFFFFFFF7F3E6000         64K rwx--  /lib/sparcv9/libc.so.1
FFFFFFFF7F3F6000          8K rwx--  /lib/sparcv9/libc.so.1
FFFFFFFF7F500000          8K rwx--    [ anon ]
FFFFFFFF7F600000        176K r-x--  /lib/sparcv9/ld.so.1
FFFFFFFF7F72C000         16K rwx--  /lib/sparcv9/ld.so.1
FFFFFFFF7FFFE000          8K rw---    [ stack ]
         total         1248K

In Linux, you can do the same as what we did for Solaris.
CentOS-5 32-bit

$ uname -a
Linux myserver 2.6.18-8.1.8.el5 #1 SMP Tue Jul 10 06:50:22 EDT 2007 i686 i686 i386 GNU/Linux

$ gcc --version
gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc -m32 -o a32 a.c

$ ./a32 100 &
[1] 4305

$ ldd a32
        linux-gate.so.1 =>  (0x008ef000)
        libc.so.6 => /lib/libc.so.6 (0x0070a000)
        /lib/ld-linux.so.2 (0x006e8000)

$ cat /proc/4305/maps
00477000-00478000 r-xp 00477000 00:00 0          [vdso]
006e8000-00701000 r-xp 00000000 fd:00 7307266    /lib/ld-2.5.so
00701000-00702000 r-xp 00018000 fd:00 7307266    /lib/ld-2.5.so
00702000-00703000 rwxp 00019000 fd:00 7307266    /lib/ld-2.5.so
0070a000-00841000 r-xp 00000000 fd:00 7310310    /lib/libc-2.5.so
00841000-00843000 r-xp 00137000 fd:00 7310310    /lib/libc-2.5.so
00843000-00844000 rwxp 00139000 fd:00 7310310    /lib/libc-2.5.so
00844000-00847000 rwxp 00844000 00:00 0
08048000-08049000 r-xp 00000000 fd:00 1444299    /home/chihung/a32
08049000-0804a000 rw-p 00000000 fd:00 1444299    /home/chihung/a32
b7fa6000-b7fa7000 rw-p b7fa6000 00:00 0
b7fbf000-b7fc0000 rw-p b7fbf000 00:00 0
bfb33000-bfb48000 rw-p bfb33000 00:00 0          [stack]

RHEL3 64-bit
$ gcc --version
gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-52)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ uname -a
Linux myserver 2.4.21-32.ELsmp #1 SMP Fri Apr 15 21:03:28 EDT 2005 x86_64 x86_64 x86_64 GNU/Linux

$ gcc -m64 -o a64 a.c

$ ./a64 100 &
[1] 21632

$ ldd a64
        libc.so.6 => /lib64/tls/libc.so.6 (0x0000002a9567c000)
        /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x0000002a95556000)

$ cat /proc/21632/maps
0000000000400000-0000000000401000 r-xp 0000000000000000 08:02 1255888  /home/chihung/a64
0000000000500000-0000000000501000 rw-p 0000000000000000 08:02 1255888  /home/chihung/a64
0000002a95556000-0000002a9566b000 r-xp 0000000000000000 08:02 537723  /lib64/ld-2.3.2.so
0000002a9566b000-0000002a9566c000 rw-p 0000000000015000 08:02 537723  /lib64/ld-2.3.2.so
0000002a9566c000-0000002a9566d000 rw-p 0000000000000000 00:00 0
0000002a9567c000-0000002a957b7000 r-xp 0000000000000000 08:02 944733  /lib64/tls/libc-2.3.2.so
0000002a957b7000-0000002a958b7000 ---p 000000000013b000 08:02 944733  /lib64/tls/libc-2.3.2.so
0000002a958b7000-0000002a958bc000 rw-p 000000000013b000 08:02 944733  /lib64/tls/libc-2.3.2.so
0000002a958bc000-0000002a958c1000 rw-p 0000000000000000 00:00 0
0000007fbfff9000-0000007fc0000000 rw-p ffffffffffffb000 00:00 0

Labels: ,

0 Comments:

Post a Comment

<< Home