Monday, November 12, 2012

ARM Architecture

ARM Architecture in naive terms.

ARM is 32 bit architecture. It is based on the RISC cpu design. It means if has fewer instructions set.
Fewer instruction set may mean
a) Limited functionality.
b) Less power demand. Using the RISC approach, the ARM processor requires only 35,000 transistors, compared to the millions in many conventional processor chips, resulting in a much lower power usage

Normally a) above is okay, as in small embedded devices need 'not' to perform zillions of operation as required by the Home PC. But, by design ARM also has improved data store and transfer capability during various calculations.  This makes processing more efficient

About b) above,  Ah, thats what we need for hand held devices. Long battery backup.


Refrences:
http://en.wikipedia.org/wiki/ARM_architecture#cite_note-cortex-a50_announce-22

Saturday, November 10, 2012

Symbol Resolution, Weak Symbols, How compiler resolves multiple Global Symbols


Whats Symbol Resolution

Linker need to find the definition of all global before generating an .exe or .out file. This process of finding and association each global symbol with a definition is called symbol resolution (again talking in loose term, avoiding shared libs).

For local symbols, resolution is straight forward, the local symbols must be defined in same module. For static local/global variable are also kind of easy, compiler wants them to be defined in same compilation unit i.e. a file.

However, resolving reference for global variable is tricky. When compiler encounter a global undefined symbol, it makes an entry into the linker table, assumption here is that it must be defined in one of other modules.

If after all of its tries, linker is not able to resolve the the global symbols, it generates a undefined symbol.

There is also one more level of difficulty of global symbol resolution, what if global symbol is defined in more than  one linking modules.



How Linker Resolved multiply defined Global symbols, Strong/Weak Linking

At compile time compiler exports each global symbol as strong or weak to the assembler. Assembler add this information into the symbol table.

What goes as Strong symbols
All function definitions, all initialized global variables.

What goes as Weak symbols
Uninitialized global variables.

Rules of resolving multiply defined global symbols
1)  Muliple defined global symbol found, they are given as linker error.
2) Given a strong and a weak symbol(s), linker chooses the strong symbol.
3) If multiple weak symbols, linker is free to choose anything. 

So, be very careful for rule 2 and 3 as you would be in blind spot linker wont stop and give you any error and you may get unexpected results.

e.g consider this example for Rule 2.

/* file1.c */
#include
int glob = 100;
int main()
{
   file2Fun();
   printf("%d", glob);
   return 0;
}



/*file2.c*/

int glob;

void file2Fun()
{
   glob = 200;
}


Well, if you know rule 2 you wont be expecting value of global  as 100 anymore, because file2.c effectively access glob of file1.



 consider this example for Rule 3.

/* file1.c */
#include
int glob ;
int main()
{

   glob = 100;
   file2Fun();
   printf("%d", glob);
   return 0;
}


/*file2.c*/

int glob;

void file2Fun()
{
   glob = 200;
}


IF this is the case, you would never know what would be the value of glob. As both places symbol is weak and linker is free to choose any.

Even more difficult type of buggs would welcome you if suppose in one file glob is define as int and other file as double. If you try to use glob as double variable you may not be able to store those bigger values there.