4

I was inspecting the preprocessed output of my C program and happened to look at the header file wordsize.h

It is located in /usr/include/i386-linux-gnu/bits/wordsize.h

the file contains only one macro

#define __WORDSIZE   32

My question is, is the wordsize then being decided by the compiler that is installed or does it has something to do with the OS that I have installed (32 bit or 64 bit) or does it have something to do with the Hardware configuration of my machine.

I am new to development under Linux.

6

In general wordsize is decided upon target architecture when compiling. Your compiler will normally compile using wordsize for current system.

Using gcc (among others) you can also tune this by using various flags. E.g. on a 64-bit host you can compile for 32-bit machine, or force 32-bit words.

-m32  # int, long and pointer to 32 bits, generates code for i386.
-m64  # int, long and pointer to 64 bits, generates code for x86-64.
-mx32 # int, long and pointer to 32 bits, generates code for x86-64.

You should also look at limits.h and inttypes.h to see the usage of this definition.


For cross-compiling check out multilib (32-bit link on SO) and search tha web.

Check what flags your GCC was built with by:

gcc -v

As to the sizes they are usually closely related to the central processing unit and related – such as maximum size of a memory address, size of CPU registers etc.

For a quick glimpse, you do not need to understand much of this, but depending on where you are it can give some insight:

If you use gcc and compile with the -S flag you can also look at the assembly instructions. Here, a bit confusing, on e.g. 32-bit machine a word is 16 bit and long is 32-bit. (__WORDSIZE)

So e.g. movl $123, %eax means move long (32-bit - __WORDSIZE) 123 to eax register, and movw means move word (16-bit).


This is naming conventions, – and only to say that WORDSIZE can mean more then one thing. You can also come across code where they e.g. define something like

#define WORD_SIZE 16

as it all depends on context. If you read data from a file or stream where source has word-size of 16 bit, this would be natural. Only to point out that do not always assume word-size means __WORDSIZE when read it in code.


Example with user defined WORD_SIZE would not affect instruction set in the generated machine code. For GCC in general I would recommend this book. (Unfortunately it is a bit old – but have yet to find a similar easy to read more up to date book. (Not that I have looked that hard.) It is short, concise and sweet. And if you only keep in mind that things can have changed, such as added features etc., it gives good introduction non the less.)

It gives a quick and nice introduction of the various aspects when compiling. Look at chapter 11 for a nice compile chain explanation.


I do not know of any options in GCC to compile 16-bit. One way to do it would be to write in assembly using .code16 to instruct the code should be 16-bits.

Example:

    .file "hello.s"
    .text
    .code16            /* Tel GAS to use 16-bit instructions. */
.globl start, _start
start:
_start:
    movb $0x48, %al
        ...

This is needed by e.g. boot loaders such as GRUB and LILO for the code present at MBR on your hard drive.

Reason for this is that when your computer boots up the CPU is in a special mode where it does not have 32-bit but max 16-bit instructions AKA Real Mode.

In short what happens is that BIOS do a hardware test, then it loads the first 512 bytes of your boot disk into memory and leaves control to that code starting at address 0. This code in turn locates where next stage of files reside, load these into memory and continue executing finally entering Protected Mode where you have normal 32-bit mode.

  • after reading your answer i want to ask that if the WORDSIZE has been defined as 16bits then would that show some effect on the opcode that is generated by GCC when i compile with the -S flag. i mean would the instructions be different. – ArunMKumar May 4 '13 at 11:48
  • @ArunKumar: No, it would not affect the assembly code. I should have been more consistent in using __WORDSIZE in my answer as well. Never use underscore in front of defines (unless you really know what you are doing) as they are (normally) reserved for the compiler. Also if you added a define like __WORDSIZE 16 that would most likely be overruled by the compiler to 32. If you want to cross-compile 64-bit code on a 32-bit machine you would also need multilib. Note that this adds a lot of extra. – Runium May 4 '13 at 12:09
  • i am looking into multilib.. heard about it ryt now only and also tried the -m32, -m64 and -mx32 flags. all compilation except the one with -m32 failed. can i assume that i dont have a multilib gcc installed on my system. how do i check for that? – ArunMKumar May 4 '13 at 12:32
3

Here's what mine has:

% cat /usr/include/bits/wordsize.h 
/* Determine the wordsize from the preprocessor defines.  */

#if defined __x86_64__
# define __WORDSIZE 64
# define __WORDSIZE_COMPAT32    1
#else
# define __WORDSIZE 32
#endif

So it's determined by wordsize.h, which comes with your compiler. But an intelligent one will select an appropriate size.

2

You should be able to choose the default wordsize at compilation time, generally by using one of the -m32 or -m64 options.

/usr/include/i386-linux-gnu/bits/wordsize.h is designed to be used when compiling 32 bit applications.

There should be a /usr/include/x86_64-linux-gnu/bits/wordsize.h containing a 64 bit __WORDSIZE definition.

This change was introduced with Ubuntu 11.4: https://wiki.ubuntu.com/MultiarchSpec

If -m64 fails, you might have a 32 bit distribution. uname -m will tell you.

Although it is possible to cross compile 64 bit binaries on a 32 bit system, that would be inconvenient as you won't have any simple way to run them there.

If your CPU is a 64 bit model (check with lscpu), you might want to install a 64 bit distribution to be able to easily build multiarch packages.

  • Compilation with the -m64 option failed while the one with -m32 flag succeded. – ArunMKumar May 4 '13 at 13:23
  • Answer updated. – jlliagre May 4 '13 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.