/* Ajith - Syntax Higlighter - End ----------------------------------------------- */

2.19.2008

Compilation process in GCC

Compiling a C program - Compiling a simple C program using gcc.

Above article is a high level view of compliation in gcc. In present article let us see in depth view of the compilation order or flow in gcc which is a default C compiler in GCC.

gcc goes through a sequence of different intermediate steps before generating final executable. Those intermediate steps are the result of different tools which are invoked internally to complete the compilation of the source code.

The whole Compilation process is broken down into following phases:

  • Preprocessing
  • Compilation
  • Assembly
  • Linking

As an example, we will examine these compilation stages individually using the same ‘hello.c’ program given below:

#include <stdio.h>
#define STRING "Hello World"

int main (void)
{
printf ("My First program - %s\n",STRING);
return 0;
}

NOTE: We dont need to manually go through all the intermediate stages to generate a executable using gcc. All these stages are directly taken care transparently by gcc internally, and can be seen using the -v option.

Although 'hello.c' program is very simple it uses external header files and libraries, and so exercises all the major steps of the compilation process. If we compile the 'hello.c' with a simple command namely gcc hello.c then we end-up creating an executable file called a.out.

Back in the days of the PDP computer, a.out stood for "assembler output". Today, it simply means an older executable file format. Modern versions of Unix and Linux use the ELF executable file format. The ELF format is much more sophisticated. So even though the default filename of the output of gcc is "a.out", its actually in ELF format.

The Preprocessor
Basically C Preprocessor is responsible for 3 tasks namely:
  • Text Substitution,
  • Stripping of Comments, and
  • File Inclusion.
Text Substitution and File Inclusion is requested in our source code using Preprocessor Directives.

The lines in our code that begin with the “#” character are preprocessor directives.

In 'hello.c' program the first preprocessor directive requests a standard header file, stdio.h, be included into our source file. The other one requests a string substitution to take place in our code.

So in preprocessor stage those included header files and defined macros are expanded and merged within the source file to produce a transitory source file, which the standard calls a Translation unit. Translation units are also known as compilation units.

The C preprocessor, often known as cpp, is a macro processor that is used automatically by the C compiler to transform a C program before compilation. To perform just preprocessing operation use the following command:
[bash]$ cpp hello.c > hello.i
The result is a file named hello.i which contains the source code with all macros expanded.

By convention, preprocessed files are given the file extension ‘.i’ for C programs and ‘.ii’ for C++ programs.

NOTE: By default the preprocessed file is not saved to disk unless the -save-temps option is used in gcc so we are just using the redirection operator to save a copy of preprocessed file.

By using gcc's “-E” flag we can directly do the pre-processing operation.
[bash]$ gcc -E hello.c -o hello.i
Now we will try to check the contents of the preproccessed input file. Since the stdio.h file is fairly large the resultant output is cleaned up a bit.
# 1 "hello.c"
# 1 "/usr/include/stdio.h" 1 3
# 1 "/usr/include/_ansi.h" 1 3
# 1 "/usr/include/sys/config.h" 1 3
# 14 "/usr/include/sys/config.h" 3
# 25 "/usr/include/sys/config.h" 3
# 44 "/usr/include/sys/config.h" 3

# 40 "/usr/include/stdio.h" 2 3
# 1 "/usr/include/sys/reent.h" 1 3

int main(void){

printf ("My First Program - %s\n", "HELLO WORLD" );
return 0;
}
Since our program has requested the stdio.h header be included into our source which in turn, requested a whole bunch of other header files. So, the preprocessor made a note of the file and line number where the request was made and made this information available to the next steps in the compilation process. Thus, the lines,
# 40 "/usr/include/stdio.h" 2 3
# 1 "/usr/include/sys/reent.h" 1 3
indicates that the reent.h file was requested on line 40 of stdio.h. The preprocessor creates a line number and file name entry before what might be "interesting" to subsequent compilation steps, so that if there is an error, the compiler can report exactly where the error occurred.

The Compiler
The next stage of the process is the actual compilation of the preprocessed source code to assembly language, for a specific processor (Depending upon the target processor architecture the source code is converted into particular assembly language and it can be known as cross-compilation).

By using “-S” flag with gcc we can convert the preprocessed C source code into assembly language without creating an object file:
[bash]$ gcc -Wall -S hello.i -o hello.s
The resulting assembly language is stored in the file ‘hello.s’.

NOTE: The assembly code generated depends upon the PC architecture and other reasons. I am just attaching the assembly code generated with the Cygwin - Linux Emulator.


NOTE: Above Assembly code contains a call to the external printf function.

The Assembler
We know that MACHINES (i.e. a computer) can understand only Machine-Level Code. So we require an ASSEMBLER that converts assembly code in "hello.s" file into machine code. Eventhough it is a straightforward one-to-one mapping of assembly language statements to their machine language counterparts, it is tedious and error-prone if done manually.

NOTE: ASSEMBLER was one of the first software tools developed after the invention of the digital electronic computer.

If there are any calls to external functions in the assembly code, the Assembler leaves the addresses of the external functions undefined, to be filled in later by the Linker.

The Assembler as in gcc can be invoked as shown below.
[bash]$ as hello.s -o hello.o
As with gcc, the output file is specified with the -o option. The resulting file ‘hello.o’ contains the machine level code for 'hello.c' program.

or

By using “-c” flag in gcc we can convert the assembly code into machine level code:
[bash]$ gcc -c hello.c
The Linker
The final stage of the compilation process is producing a single executable program file by linking set of object files. An object file and an executable file come in several formats such as ELF (Executable and Linking Format) and COFF (Common Object-File Format). For example, ELF is used on Linux systems, while COFF is used on Windows systems.

In practice, an executable file requires many external functions from system and C run-time libraries. The linker will resolve all of these dependencies and plug in the actual address of the functions.

The linker also does a few additional tasks for us. It combines our program with some standard routines that are needed to make our program run. For example, there is standard code required at the beginning of our program that sets up the running environment, such as passing in command-line parameters and environment variables. Also, there is code that needs to be run at the end of our program so that it can pass back a return code, among other tasks. It turns out that this is no small amount of code.

Actually the mechanism used internally by gcc to link up different files is a bit complicated. For example, the full command for linking the 'hello.c' program might look as shown below:
[bash]$ ld -dynamic-linker /lib/ld-linux.so.2/usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc-lib/i686/3.3.1/crtbegin.o-L/usr/lib/gcc-lib/i686/3.3.1 hello.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh/usr/lib/gcc-lib/i686/3.3.1/crtend.o /usr/lib/crtn.o
Luckily we are never asked to type the above command directly -- the entire linking process is handled transparently by gcc when invoked as follows:
[bash]$ gcc hello.o
This links the object file ‘hello.o’ to the C standard library, and produces an executable file ‘a.out’.
[bash]$ ./a.out
My First Program - Hello World
An object file for a C++ program can be linked to the C++ standard library in the same way with a single g++ command.

2.18.2008

Compiling a simple C program using GCC

Let us see how to compile a simple C program using gcc compiler, for this we will try out with classical hello world program in C language which is given below:

#include <stdio.h>
int main (void)
{
printf ("Hello, world!\n");
return 0;
}

We will save the above 'C' code in a file named 'hello.c'. Inorder to compile a C file with gcc, use the following command:
[bash]$ gcc -Wall hello.c -o hello

This compiles the source code in ‘hello.c’ to machine code and stores it in an executable file ‘hello’.

NOTE: The output file for the machine code is specified using the -o option. This option is usually given as the last argument on the command line. If it is omitted, the output is written to a default file called ‘a.out’. If a file with the same name as the executable file already exists in the current directory it will be overwritten.

The option -Wall turns on all the most commonly-used compiler warnings---it is recommended that you always use this option! There are many other warning options, but -Wall is the most important. GCC will not produce any warnings unless they are enabled. Compiler warnings are an essential aid in detecting problems when programming in C and C++.

In this case, the compiler does not produce any warnings with the -Wall option, since the program is completely valid. Source code which does not produce any warnings is said to compile cleanly.

To run the program, type the path name of the executable like this:
[bash]$ ./hello
Hello, world!

This loads the executable file into memory and causes the CPU to begin executing the instructions contained within it. The path ./ refers to the current directory, so ./hello loads and runs the executable file ‘hello’ located in the current directory.

For a detailed view about how the who compilation process goes checkout Compilation process in GCC.

2.14.2008

World’s first optical drive for cell phones

That's really suprising an optical drive for a mobile phone but it is true.

Vmedia Research, Inc. unveiled its mobile entertainment media technology at the GSMA Mobile World Congress in Barcelona. Vmedia™ is an innovative miniature optical disc format designed to bring the quality of a big screen experience and the simplicity of home entertainment to mobile consumer electronics.

The technology consists of the ‘Vmedia disc’ that plays on the world’s smallest blue laser optical drive, the ‘Vmedia drive’. The disc – 32mm in diameter and cased in a slim cartridge – would retail at the same price as a DVD and have the same release dates.

Despite the tiny size, the company claimed that there wouldn’t be a loss in quality: A single disc can hold a complete feature-length movie at 576p resolution by combining blue laser technology with advanced video (H.264) and audio codecs (AAC).

The first generation of the single-layer Vmedia disc has a capacity of 1GB, which will hold the typical movie plus extra features such as out-takes, mobile phone wallpapers and ringtones. The storage could go up to 2GB once the company comes out with dual-layer discs in 2009.

Designed to withstand the rigours of today’s mobile applications, the Vmedia drives are resistant to shock and vibration, and also feature low power consumption, company officials said.

Vmedia has tied up with Indian mobile company, Spice Mobile. The company’s chairman Dr B K Modi introduced the Spice Movie Phone – the world’s first Vmedia-enabled mobile phone – at the Mobile World Congress in Barcelona.

Spice will be distributing Indian and Hollywood content on Vmedia throughout South Asia. “We believe the Movie Phone will become the most popular multimedia communications platform worldwide and may even change the way we all view movies, just as the iPod changed the way we listen to music.” he said.

That's great Spice Mobile is the first company to launch a phone with the optical drive.

2.12.2008

OpD2d - Direct to disk audio recorder

It all began long back .... Long Long ago ....

It was the period when Online Music Sharing was ruling the Internet ... major companies from Music industry started using DRM Codenamed: Digital Rights Management or Digital Restrictions Management in their albums to Copy-Protect (It is what a Mu$ic company says) or to kill Online Music Sharing.

Later DRM controlled and prevented copying or conversion of music by end users to other formats. Which made Music Companies $RICH$ as every user has to buy his own copy of a song inorder to listen it.

Then came the GEEK's (or HACKER's according to Music Industry and government's) with a temporary solution until they are able to crack the DRM. It can be called as "Get it when it enters the cave".

Whenever a Audio file is played in a PC the digital data in the Audio file is converted into Analog stuff in the sound card. Sound cards usually have digital-to-analog converters, that converts data in digital format to an analog signal which is sent to a speaker or headphone. This is how we can hear the MUSIC dude ....

Geek's did a trick by simply recording the output Analog signal from the sound card with a simple software program like OpD2d.

OpD2d (Direct to disk audio recorder) is a simple audio recording application with an odd old interface. It can be used to easily record anything that gets into your sound card and save it directly onto your hard disk.

It was originally written to make use of the Yamaha SW1000 XG soundcard's ability to record the digital audio it is playing. This is a really useful feature since I can simply record streaming audio from the Internet directly onto my hard disk, then burn it onto CD. This app supports all Windows based operating system's from Window98 to XP.

The present version 1.3 of OpD2d, even supports to save the recorded content directly into an MP3 file. And this software comes with a "FREE USAGE" tag. So why to waste time start using it

http://www.opcode.co.uk/opd2d/default.asp

Better do a Google search with OpD2d as the download link may be a broken one ...

2.10.2008

How to Enable USB device

Today a desktop computer equipped with a CD writer or a DVD burner is very rare in most of the companies. But a much larger security threat is posed by the open USB ports where mischievous office workers can just plugin the Flash Pen Drive, External Hard Disk or their iPod music player and transfer corporate data or even copy licensed software to their memory sticks in seconds. Sometimes even used for delivering trojans or spyware into the companies networks.

So called SMART ADMINISTRATORS disabled USB Drive access by changing the BIOS settings and then locking it up with a "PASSWORD". Even though they are a step ahead as we don't know the password for BIOS but it is not a fool proof way. As BIOS password can be broken in a simple way.

While some ADMINS simply remove the USB port option to the PC. While some for temporary restriction simply disable "Write Access" to USB port so that no one can copy data files from PC into a USB. Let us see how to disable WRITE PROTECTION option.

NOTE: It is the time to make hands dirty so backup the Registry.

Open the Windows Registry by giving "regedit" command in command prompt. If regedit is not disabled then you can see the registry editor. Now on the Left side of the editor open the following registry key

HKEY_LOCAL_MACHINE\System\CurrentControlSet\ Control\StorageDevicePolicies

If your sysadmin has enabled WRITE protection then you can see a DWORD called WriteProtect with its value as 1 in the Right side of the editor. Set the value of WriteProtect as 0 to disable the write protection.

Note: Remember that the above trick works only with Windows XP SP2.

If you like to go a step further and enable connecting USB storage devices to their computers, Open registry and navigate to the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UsbStor

Now in the Right pane of the registry editor you can see a DWORD called Start with its value as 4 (Hexadecimal). Change the value to 3 to enable the USB storage devices.