Wednesday, July 15, 2009

"C" calling convention

So, what the hell is a calling convention ?

Calling convention is a scheme that specifies how the called function receives the parameter from the caller function and how it sends the results back to the caller function.

Calling conventions can differ in the following respects:

1. Setting up and clearing of the function call(stack frame) between the caller and the callee.
2. How the parameters are being passed from the caller to the callee; i.e. whether the parameters are passed in the registers or on the stack or on both.
3. The order in which the parameters are passed; i.e. from left to right or from right to left.

Here I am going to explain the C calling convention. The calling convention divides the rules between the caller and the callee.

Callers' rule:

1. Save the registers: EBX, ECX, EDX etc. so that their contents are not disturbed during the function call.
2. Push the arguments on the stack from right to left.
3. Call the subroutine.
4. Clean the stack.
5. Get the return status from the subroutine in EAX register.
6. Pop the registers back.

Callee's rule:

1. Save the EBP register, to preserve the stack frame.
2. Move the ESP register into EBP register, make a new stack frame.
Note: The caller does not expect the callee to change the EBP register.
3. Make room for the local variables. As with parameters the local variables will be accessed relative to the EBP register.
4. Save the "callee saved" registers,these are EDI and ESI
5. //Code the callee wants to execute.
6. Place the return value in the EAX register.
7. Restore the values of any "Callee saved" registers i.e. ESI and EDI. Registers must be popped in the reverse order of pushing.
8. Free up the local variables by adding some amount to the ESP register. A less error prone way is to "mov %esp %ebp".
9. Immediately before returning, restore the caller’s base pointer by popping the EBP register off the stack.

Remember, this was the first thing that we did, on entering the subroutine.

10. Finally we do a RET, this will pop off the return address and make a jump to it.

Hope this explains the C calling convention......

Monday, July 13, 2009

extern “C” and __cdecl

In short:
__cdecl is the “C” calling convention whereas
extern “C” is the “C” naming convention.

C as well as C++ code may have __cdecl as the calling convention.

C++ code uses extern “C” to denote that the function is “C” function and the compiler should not mangle the name of the function in the generated object code.

If C++ code uses __cdecl before the function; this means that it follows the “C” calling convention but C++ naming convention; it will have mangled names.

If a C++ code uses both the extern “C” as well as __cdecl it means that the said function uses “C” calling convention as well as the “C” naming convention. The name of the function will be same in the generated object code. There will be no name mangling.

Tuesday, June 23, 2009

Official F*ck count of the linux 2.6.30 kernel

Yesterday I downloaded and compiled the 2.6.30 linux kernel source from http://kernel.org/. After booting from the newly compiled kernel I thought of counting the "Official Fuck Count" of the 2.6.30 kernel.

Here's the result:

[vijaush@pheonix linux-2.6.30]$ find . -exec grep -i fuck {} \; -print
* (And this is the fucking 'basic' method).
./net/ipv4/netfilter/nf_nat_snmp_basic.c
/* Fuck, we are miserable poor guys... */
./net/core/skbuff.c
/* If you fuck with this, update ret_from_syscall code too. */ \
./arch/sparc/include/asm/system_64.h
/* XXX Fucking Cypress... */
./arch/sparc/kernel/head_32.S
/* Some BIOS's are fucked and don't set all MTRRs the same! */
./arch/x86/kernel/cpu/mtrr/generic.c
* Some Athlon laptops have really fucked PST tables.
./arch/x86/kernel/cpu/cpufreq/powernow-k7.c
* fucking with the memory controller because it needs to know the
./arch/mips/sgi-ip22/ip22-setup.c
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't try to access
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't try to access
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't try to access
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't even give the
* IOC3 is fucked fucked beyond believe ... Don't try to access
./arch/mips/pci/ops-bridge.c
/* master list of VME vectors -- don't fuck with this */
./arch/m68k/include/asm/sun3ints.h
/* Fucking broken ABI */
./arch/parisc/kernel/sys_parisc.c
* What the fuck is going on here? We leave junk in the beginning
./sound/oss/opl3.c
case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
./fs/xfs/xfs_btree.h
/* Only Sun can take such nice parts and fuck up the programming interface
/* This card is _fucking_ hot... */
./drivers/net/sunhme.c
* These chips are basically fucked by design, and getting this driver
./drivers/ide/cmd640.c
registered, to prevent the link/init ordering from fucking
./drivers/mtd/mtd_blkdevs.c
* brain-damage, it's managed to fuck things up one step further..
./drivers/watchdog/shwdt.c
/* Am I fucking pedantic or what? */
./drivers/scsi/qlogicpti.h
BUG? Why does turning the chroma comb on fuck up color?
./drivers/media/video/bt819.c
* Wirzenius wrote this portably, Torvalds fucked it up :-)
./lib/vsprintf.c
[vijaush@pheonix linux-2.6.30]$


That's total of 32 count in 19 files.


Also have a look at this site http://www.vidarholen.net/contents/wordcount/. It has a graph of all the swear words used in the linux kernel.

Monday, June 22, 2009

Logging into Fedora 11 as the root user

Today i tried to log into my system as the 'root' user and GDM will not allow me to log into the system. Looks like that GDM does not allow root user to log into the GUI mode.

To overcome this limitation, you can edit the /etc/pam.d/gdm-password file and comment the line saying:

auth required pam_succeed_if.so user != root quiet



That's it, restart the GDM and login as 'root' user.

Friday, June 12, 2009

Google suggestion

Recently I was searching for a "How to" on LFS (Linux From Scratch), so I opened up http://www.google.com and typed in the following keywords "How to" and here's what Google suggestion came up with first.



I am preety sure, I know that !!! *LOL*